:root {
  color-scheme: dark;
  --red: #e43126;
  --green: #24a942;
  --yellow: #f4d331;
  --blue: #172aa4;
  --brick: #780910;
  --cream: #fff1c7;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; overscroll-behavior: none; }
body {
  margin: 0;
  display: grid;
  place-items: center;
  background:
    linear-gradient(rgba(0,0,0,.18), rgba(0,0,0,.56)),
    repeating-linear-gradient(0deg, #51070b 0 3px, #6d090e 3px 28px, #300407 28px 31px),
    #240306;
  color: var(--cream);
  font-family: ui-monospace, "SFMono-Regular", Menlo, monospace;
  touch-action: manipulation;
}

main { width: min(1040px,100%); padding: 18px; }
header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 3px solid var(--yellow);
  margin-bottom: 13px;
  padding-bottom: 9px;
}
small { color: #f1d6aa; letter-spacing: .11em; text-shadow: 1px 1px #000; }
h1 { font-size: 42px; letter-spacing: -.13em; line-height: .85; margin: 4px 0 0; text-shadow: 2px 2px #090106; }
h1 i { font-style: normal; color: var(--red); }
h1 em { font-style: normal; color: var(--green); }
h1 b { color: var(--yellow); margin-left: .12em; }

.tools { display: flex; flex-wrap: wrap; justify-content: end; gap: 6px; }
.tools > * {
  min-height: 34px;
  border: 1px solid #d7a829;
  border-radius: 2px;
  background: #180b10;
  color: inherit;
  padding: 7px;
  font: inherit;
}
.tools button { min-width: 38px; }

.screen {
  position: relative;
  aspect-ratio: 16/10;
  overflow: hidden;
  background: #cef4f1;
  border: 6px solid #7d8383;
  border-top-color: #d9dddd;
  border-left-color: #b8bcbc;
  border-bottom-color: #34383b;
  box-shadow: 0 18px 55px #090004, 0 0 0 3px #14070a;
  touch-action: none;
}
canvas { display: block; width: 100%; height: 100%; image-rendering: pixelated; outline: 0; touch-action: none; }
canvas:focus-visible { box-shadow: inset 0 0 0 3px var(--yellow); }
#notice {
  position: absolute;
  left: 8px;
  top: 8px;
  max-width: calc(100% - 16px);
  padding: 5px 8px;
  background: rgba(9,7,12,.82);
  border-left: 3px solid var(--yellow);
  color: #fff4d4;
  font-size: 11px;
  pointer-events: none;
}
#start {
  position: absolute;
  left: 50%;
  bottom: 9%;
  transform: translateX(-50%);
  min-width: 220px;
  padding: 12px 16px;
  border: 0;
  background: var(--yellow);
  color: #171000;
  font: 900 13px monospace;
  box-shadow: 0 5px #8e7012;
}
#start:active { transform: translate(-50%,3px); box-shadow: 0 2px #8e7012; }

#story-overlay {
  position: absolute;
  z-index: 8;
  inset: 0;
  display: grid;
  place-items: center;
  padding: clamp(12px,4vw,38px);
  background:
    linear-gradient(rgba(3,5,23,.2), rgba(3,2,12,.48)),
    url("assets/images/story-experiment.png") center / 100% 100% no-repeat;
  image-rendering: pixelated;
  color: var(--cream);
}
.story-card {
  width: min(620px,100%);
  max-height: 100%;
  overflow: auto;
  padding: clamp(16px,3.4vw,34px);
  border: 3px solid var(--yellow);
  border-right-color: #8d6912;
  border-bottom-color: #8d6912;
  background: linear-gradient(145deg, rgba(52,7,10,.91), rgba(18,4,6,.94));
  box-shadow: 0 0 0 3px #170609, 0 12px 35px rgba(0,0,0,.68);
  text-align: center;
  touch-action: pan-y;
  overscroll-behavior: contain;
}
.story-card small { color: #f0c735; }
.story-card h2 {
  margin: 8px 0 16px;
  font-size: clamp(30px,6vw,50px);
  letter-spacing: -.13em;
  line-height: .9;
  text-shadow: 2px 2px #090106;
}
.story-card h2 i { color: var(--red); font-style: normal; }
.story-card h2 em { color: var(--green); font-style: normal; }
.story-card h2 b { color: var(--yellow); margin-left: .14em; }
#story-text {
  margin: 0 auto;
  max-width: 54ch;
  color: #fff1c7;
  font-size: clamp(12px,2vw,16px);
  line-height: 1.55;
  text-align: left;
  text-wrap: pretty;
  white-space: pre-line;
}
#story-text p { margin: 0 0 .9em; }
#story-text p:last-child { margin-bottom: 0; }
.story-scroll-cue { display: none; }
#story-continue {
  min-width: min(250px,100%);
  min-height: 48px;
  margin-top: 20px;
  border: 2px solid #fff4aa;
  border-radius: 2px;
  background: var(--yellow);
  color: #171000;
  padding: 10px 18px;
  box-shadow: 0 5px #8e7012;
  font: 900 13px/1 ui-monospace, "SFMono-Regular", Menlo, monospace;
  letter-spacing: .05em;
  cursor: pointer;
}
#story-continue:hover { background: #fff06a; }
#story-continue:active { transform: translateY(3px); box-shadow: 0 2px #8e7012; }
#story-continue:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }

.team-panel {
  margin-top: 12px;
  padding: 9px;
  border: 2px solid #b98d1e;
  border-top-color: var(--yellow);
  border-left-color: #e8c43f;
  background:
    linear-gradient(90deg, rgba(120,9,16,.48), transparent 42%),
    #180b10;
  box-shadow: inset 0 0 0 2px #31060a, 0 7px 20px rgba(7,0,2,.35);
}
.team-heading {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  margin: 0 2px 7px;
}
.team-heading small { color: var(--yellow); font-size: 10px; }
.team-heading p { margin: 2px 0 0; }
#team-status { color: #fff1c7; font-size: 11px; }
.team-help { color: #d9c8a4; font-size: 10px; text-align: right; }
kbd {
  display: inline-grid;
  min-width: 21px;
  min-height: 20px;
  place-items: center;
  border: 1px solid #d7a829;
  border-bottom-width: 3px;
  border-radius: 2px;
  background: #35090d;
  color: #fff1c7;
  font: 700 10px/1 inherit;
}
.team-roster {
  display: grid;
  grid-template-columns: repeat(5,minmax(0,1fr));
  gap: 7px;
}
.team-member {
  --animal-accent: var(--red);
  position: relative;
  display: grid;
  grid-template-columns: 44px minmax(0,1fr);
  grid-template-rows: 1fr 1fr;
  align-items: center;
  min-width: 0;
  min-height: 58px;
  overflow: hidden;
  border: 2px solid #695f51;
  border-radius: 2px;
  background: linear-gradient(145deg,#342b29,#171316 70%);
  color: #e4d8b9;
  padding: 5px 7px 5px 4px;
  font: inherit;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
}
.team-member:nth-child(2), .team-member:nth-child(4) { --animal-accent: var(--green); }
.team-member:nth-child(3) { --animal-accent: var(--yellow); }
.team-member:nth-child(5) { --animal-accent: #e9b52d; }
.team-member::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: var(--animal-accent);
  opacity: .45;
}
.team-member:hover:not([aria-disabled="true"]) { border-color: var(--animal-accent); background: #302021; }
.team-member:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }
.team-face {
  position: relative;
  grid-row: 1 / 3;
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  overflow: hidden;
  border: 1px solid #82745e;
  background: #cef4f1;
  box-shadow: inset 0 0 0 2px rgba(0,0,0,.24);
}
.team-portrait {
  display: block;
  width: 24px;
  height: 24px;
  background-image: url("assets/images/characters.png");
  background-position: 0 0;
  background-repeat: no-repeat;
  image-rendering: pixelated;
  transform: scale(1.5);
}
.team-member:nth-child(2) .team-portrait { background-position: 0 -25px; }
.team-member:nth-child(3) .team-portrait { background-position: 0 -50px; }
.team-member:nth-child(4) .team-portrait { background-position: 0 -75px; }
.team-member:nth-child(5) .team-portrait { background-position: 0 -100px; }
.team-name {
  align-self: end;
  overflow: hidden;
  color: #fff1c7;
  font-size: 12px;
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.team-state {
  align-self: start;
  overflow: hidden;
  color: #ad9f82;
  font-size: 8px;
  letter-spacing: .04em;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.team-member.is-rescued { border-color: color-mix(in srgb,var(--animal-accent),#fff1c7 24%); }
.team-member.is-rescued::before { opacity: 1; }
.team-member.is-active {
  border-color: var(--yellow);
  background: linear-gradient(145deg,#5a1116,#211015 72%);
  box-shadow: inset 0 0 0 1px #f7e29a, 0 0 14px rgba(244,211,49,.24);
}
.team-member.is-active .team-name { color: var(--yellow); }
.team-member.is-active .team-state { color: #fff1c7; }
.team-member.is-active::after {
  content: "▶";
  position: absolute;
  z-index: 2;
  top: 4px;
  right: 5px;
  color: var(--yellow);
  font-size: 11px;
  line-height: 1;
  text-shadow: 1px 1px #000;
}
.team-member.is-locked,
.team-member[aria-disabled="true"] { cursor: not-allowed; filter: saturate(.2); opacity: .58; }
.team-member.is-locked .team-face::after,
.team-member[aria-disabled="true"] .team-face::after {
  content: "\1f512";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(15,10,11,.64);
  color: var(--yellow);
  font-size: 16px;
  text-shadow: 1px 1px #000;
}

.touch { display: none; justify-content: space-between; align-items: end; gap: 12px; padding-top: 12px; touch-action: none; }
.touch-move, .touch-actions { display: flex; align-items: end; gap: 8px; }
.touch button {
  width: 56px;
  height: 52px;
  border: 1px solid #c19925;
  border-radius: 8px;
  background: linear-gradient(#5e161d,#27080c);
  color: #fff;
  box-shadow: 0 4px #100104;
  font: 700 16px monospace;
  touch-action: none;
  user-select: none;
}
.touch button.active { transform: translateY(3px); box-shadow: 0 1px #100104; }
.touch .jump { width: 74px; height: 69px; color: var(--yellow); }
.touch .action { width: 74px; height: 61px; color: #8ff2a3; font-size: 12px; }
.touch .shoot { width: 74px; height: 61px; color: #ffd45a; font-size: 12px; }

footer { display: flex; justify-content: space-between; gap: 18px; color: #ecd6ae; font-size: 12px; padding-top: 11px; }
footer p { margin: 0; max-width: 720px; }
footer div { display: flex; gap: 14px; white-space: nowrap; }
footer button { border: 0; background: none; color: inherit; text-decoration: underline; font: inherit; }

#tile-dialog {
  width: min(96vw,1320px);
  max-width: none;
  max-height: 92vh;
  padding: 16px;
  overflow: hidden;
  border: 3px solid var(--yellow);
  border-radius: 3px;
  background: #180b10;
  color: var(--cream);
  box-shadow: 0 24px 80px #000;
  font-family: inherit;
}
#tile-dialog::backdrop { background: rgba(5,0,2,.82); }
.tile-dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.tile-dialog-head h2 { margin: 3px 0 0; color: var(--yellow); font-size: 24px; }
#tile-dialog > p { margin: 10px 0; color: #ecd6ae; font-size: 12px; }
#tile-close {
  min-height: 36px;
  border: 1px solid #d7a829;
  border-radius: 2px;
  background: #3d0b10;
  color: var(--cream);
  padding: 8px 12px;
  font: inherit;
}
.tile-scroll { max-height: calc(92vh - 112px); overflow: auto; overscroll-behavior: contain; }
#tile-atlas {
  display: grid;
  grid-template-columns: repeat(12,96px);
  gap: 6px;
  width: max-content;
  padding: 5px 5px 14px;
}
.tile-card {
  min-height: 111px;
  padding: 5px;
  border: 1px solid #5f5555;
  background: #222a2e;
  color: #f7edcf;
}
.tile-card b { display: block; margin-top: 4px; color: var(--yellow); font-size: 11px; }
.tile-card span { display: block; margin-top: 2px; font-size: 9px; line-height: 1.14; }
.tile-sprite {
  width: 72px;
  height: 72px;
  margin: auto;
  background-color: #cef4f1;
  background-image: url("assets/images/tiles-game.png");
  background-repeat: no-repeat;
  /* The source atlas is 320×200 with a 25px cell pitch. Keep a true 3×
     scale so the 75px background offsets used by the debug viewer line up
     exactly with the same cells atlasTile() draws in the game. */
  background-size: 960px 600px;
  image-rendering: pixelated;
}

@media (pointer: coarse), (max-width: 820px) {
  body { display: block; }
  main { padding: 8px; }
  header { align-items: center; margin-bottom: 7px; }
  small, footer p { display: none; }
  h1 { font-size: 29px; }
  .tools { max-width: 70%; }
  .tools select { max-width: 105px; }
  .touch { display: flex; }
  footer { justify-content: center; }
  .team-panel { margin-top: 8px; }
  .team-heading { align-items: center; }
  .team-member { min-height: 52px; grid-template-columns: 40px minmax(0,1fr); padding-right: 4px; }
  .team-face { width: 36px; height: 36px; }
}

@media (max-width: 590px) {
  header { display: block; }
  h1 { margin-bottom: 10px; }
  .tools { max-width: none; justify-content: start; }
  .touch button { width: 48px; height: 48px; }
  .touch-actions { max-width: 214px; flex-wrap: wrap; justify-content: flex-end; }
  .touch .jump { width: 66px; }
  .touch .action, .touch .shoot { width: 60px; }
  .team-help { max-width: 155px; }
  .team-roster { gap: 4px; }
  .team-member {
    display: flex;
    min-height: 56px;
    justify-content: center;
    padding: 5px 2px;
  }
  .team-member::before { inset: auto 0 0; width: auto; height: 3px; }
  .team-face { flex: 0 0 40px; width: 40px; height: 40px; }
  .team-name, .team-state { position: absolute; }
  .team-name {
    inset: auto 2px 2px;
    z-index: 1;
    padding: 2px 1px 0;
    background: rgba(12,4,5,.82);
    font-size: 9px;
    text-align: center;
  }
  .team-state { clip: rect(0 0 0 0); clip-path: inset(50%); width: 1px; height: 1px; }
  #story-overlay { padding: 8px; }
  .story-card { padding: 13px; }
  .story-card h2 { margin-bottom: 10px; }
  #story-text { font-size: 11px; line-height: 1.4; }
  #story-continue { min-height: 44px; margin-top: 12px; }
  #tile-dialog { width: 98vw; padding: 10px; }
  .tile-dialog-head h2 { font-size: 18px; }
}

@media (max-height: 560px) and (orientation: landscape) {
  header, footer { display: none; }
  main { padding: 3px; }
  .screen { width: min(72vw,calc((100dvh - 60px)*1.6),640px); margin: auto; }
  .team-panel {
    display: block;
    width: min(72vw,calc((100dvh - 60px)*1.6),640px);
    margin: 4px auto 0;
    padding: 3px;
  }
  .team-heading { display: none; }
  .team-roster { gap: 3px; }
  .team-member { min-height: 44px; padding-block: 2px; }
  .team-face { width: 36px; height: 36px; }
  .story-card {
    display: flex;
    max-height: calc(100% - 4px);
    flex-direction: column;
    overflow: hidden;
    padding: 9px 13px;
  }
  .story-card h2 { margin: 5px 0 8px; font-size: 34px; }
  #story-text {
    min-height: 0;
    overflow: auto;
    padding-right: 7px;
    overscroll-behavior: contain;
  }
  #story-continue {
    flex: 0 0 auto;
    min-height: 40px;
    margin-top: 7px;
    padding-block: 7px;
  }
  .story-scroll-cue {
    display: block;
    flex: 0 0 auto;
    margin-top: 3px;
    color: #f0c735;
    font-size: 8px;
    letter-spacing: .08em;
  }
  .touch { position: fixed; inset: 50% 6px auto; transform: translateY(-50%); padding: 0; }
  .touch button { width: 45px; height: 45px; opacity: .9; }
  .touch .jump, .touch .action, .touch .shoot { height: 56px; }
  .touch-actions button:nth-last-child(-n+2) { display: none; }
}
