:root {
  color-scheme: dark;
  --ink: #080d12;
  --panel: #111c27;
  --panel-2: #172838;
  --cream: #ead7a4;
  --paper: #f5ebcf;
  --muted: #8394a2;
  --red: #df173c;
  --amber: #ffc84a;
  --blue: #355f82;
  --line: rgba(234, 215, 164, 0.22);
  font-family: "Arial Narrow", "Roboto Condensed", Impact, system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(circle at 50% 0%, rgba(53, 95, 130, 0.18), transparent 44rem),
    repeating-linear-gradient(90deg, rgba(255,255,255,0.012) 0 1px, transparent 1px 42px),
    var(--ink);
  color: var(--paper);
  overscroll-behavior: none;
  user-select: none;
}

button { font: inherit; }

.game-shell {
  width: min(1180px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) clamp(10px, 2vw, 24px) max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { color: var(--amber); font-size: 2.2rem; line-height: 1; }
h1 { margin: 0; font-size: clamp(1.05rem, 2.5vw, 1.55rem); letter-spacing: .12em; text-transform: uppercase; }
.brand p { margin: 3px 0 0; color: var(--muted); font-size: .72rem; letter-spacing: .18em; }

.hud { display: flex; gap: 7px; align-items: stretch; }
.hud-chip, .icon-button {
  min-height: 42px;
  border: 1px solid var(--line);
  background: rgba(17, 28, 39, .88);
  color: var(--paper);
  border-radius: 2px;
}
.hud-chip { display: flex; align-items: center; justify-content: center; gap: 6px; padding: 0 11px; }
.hud-chip b { font-size: 1rem; letter-spacing: .08em; }
.hud-chip small { color: var(--amber); font-size: .8rem; }
.hud-chip img { width: 8px; height: 20px; }
.floor-chip { min-width: 70px; }
.weapon-chip { min-width: 88px; }
.key-ring { min-width: 76px; gap: 8px; padding-inline: 10px; }
.key-token {
  width: 14px;
  height: 7px;
  position: relative;
  display: block;
  border: 2px solid currentColor;
  border-radius: 50% 0 0 50%;
  color: #41505a;
  opacity: .4;
  transition: color .15s, opacity .15s, filter .15s;
}
.key-token::after {
  content: "";
  position: absolute;
  left: 9px;
  top: 1px;
  width: 7px;
  border-top: 2px solid currentColor;
  box-shadow: 3px 2px 0 -1px currentColor;
}
.key-token.owned { opacity: 1; filter: drop-shadow(0 0 4px currentColor); }
.key-green.owned { color: #54d97b; }
.key-blue.owned { color: #4da3ff; }
.key-yellow.owned { color: #ffd34e; }
.icon-button { padding: 0 10px; font-size: .66rem; letter-spacing: .1em; cursor: pointer; }
.icon-button:hover { border-color: var(--cream); }

.stage-wrap {
  position: relative;
  width: 100%;
  min-width: 0;
  aspect-ratio: 12 / 7;
  max-height: 70vh;
  background: #05090d;
  border: 1px solid rgba(234, 215, 164, .28);
  box-shadow: 0 18px 70px rgba(0, 0, 0, .52), inset 0 0 0 3px rgba(0,0,0,.55);
  overflow: hidden;
  touch-action: none;
}

#gameCanvas { display: block; width: 100%; max-width: 100%; height: 100%; }

.toast {
  position: absolute;
  left: 50%;
  top: 9%;
  transform: translate(-50%, -8px);
  max-width: 75%;
  padding: 8px 13px;
  background: rgba(5, 9, 13, .87);
  border-left: 3px solid var(--amber);
  color: var(--paper);
  font-size: clamp(.62rem, 1.7vw, .82rem);
  letter-spacing: .12em;
  text-align: center;
  opacity: 0;
  transition: opacity .16s, transform .16s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.shaft-map {
  position: absolute;
  top: 18px;
  right: 14px;
  height: 150px;
  display: grid;
  grid-template-rows: auto 1fr auto;
  justify-items: center;
  gap: 3px;
  opacity: .85;
}
.map-label { font: 9px/1 monospace; color: #92a0aa; }
.map-track { position: relative; width: 25px; border: 1px solid rgba(255,255,255,.2); background: rgba(0,0,0,.35); }
.map-track::before, .map-track::after { content: ""; position: absolute; top: 0; bottom: 0; border-left: 1px solid rgba(255,255,255,.12); }
.map-track::before { left: 8px; }
.map-track::after { left: 16px; }
.map-car { position: absolute; width: 5px; height: 5px; transform: translateY(50%); transition: bottom .16s linear; }
.car-a { left: 2px; background: #ffc84a; }
.car-b { left: 10px; background: #63d1c2; }
.car-c { left: 18px; background: #d977ff; }
.map-player { position: absolute; left: -5px; width: 3px; height: 2px; background: #f3f0e4; transition: bottom .1s linear; }

.controls { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.move-controls { display: flex; gap: 8px; }
.control-button {
  width: clamp(62px, 11vw, 86px);
  height: clamp(54px, 8vw, 68px);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: 1px solid rgba(234, 215, 164, .3);
  border-radius: 3px;
  background: #14222e;
  color: var(--paper);
  box-shadow: inset 0 -4px 0 rgba(0,0,0,.25);
  touch-action: none;
}
.control-button:active, .control-button.pressed { transform: translateY(2px); box-shadow: inset 0 -2px 0 rgba(0,0,0,.25); background: #203749; }
.control-button span { font-size: 1.2rem; }
.control-button small { font-size: .58rem; letter-spacing: .12em; }
.close-button { margin-left: auto; border-color: rgba(255, 200, 74, .55); color: var(--amber); flex-direction: column; }
.shoot-button { background: #481524; border-color: rgba(223, 23, 60, .75); flex-direction: column; }
.desktop-help { margin: 0; text-align: center; color: #677987; font-size: .7rem; letter-spacing: .09em; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(3, 6, 9, .88);
  backdrop-filter: blur(5px);
}
.modal.visible { display: flex; }
.modal-card {
  width: min(520px, 100%);
  padding: clamp(22px, 5vw, 38px);
  border: 1px solid rgba(234, 215, 164, .35);
  background: linear-gradient(145deg, #172735, #0e1821 72%);
  box-shadow: 0 28px 90px #000;
}
.story-art { height: 125px; position: relative; overflow: hidden; margin: -8px 0 22px; border-bottom: 4px solid #2a3a44; }
.story-art img { position: absolute; bottom: 0; }
.story-player { width: 53px; left: 12%; animation: story-run .45s steps(2) infinite; }
.story-cop { width: 58px; right: 13%; }
.story-cop.cop-two { right: 1%; transform: scale(.82); opacity: .65; }
.story-door { position: absolute; left: 42%; bottom: 0; width: 90px; height: 118px; border: 6px solid #344652; background: #060a0d; }
.story-door span { position: absolute; top: 0; bottom: 0; width: 46%; background: #223441; }
.story-door span:first-child { left: 0; }
.story-door span:last-child { right: 0; }
@keyframes story-run { 50% { transform: translateY(-3px) rotate(-1deg); } }
.eyebrow { margin: 0 0 7px; color: var(--amber); font-size: .67rem; letter-spacing: .25em; }
.modal h2 { margin: 0; font-size: clamp(2.3rem, 10vw, 4.6rem); line-height: .95; letter-spacing: .04em; }
.story-lines { margin: 20px 0 0; padding: 0; list-style: none; counter-reset: story; }
.story-lines li { padding: 7px 0; border-bottom: 1px solid rgba(255,255,255,.08); font-size: clamp(.74rem, 2.6vw, .9rem); letter-spacing: .1em; }
.story-aside { margin: 15px 0 22px; color: var(--muted); font-size: .72rem; letter-spacing: .16em; }
.primary-button {
  width: 100%;
  min-height: 56px;
  border: 0;
  background: var(--red);
  color: white;
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: .18em;
  cursor: pointer;
  box-shadow: inset 0 -5px 0 rgba(0,0,0,.23);
}
.primary-button:hover { filter: brightness(1.1); }
.seed-line { min-height: 1em; margin: 12px 0 -12px; text-align: center; color: #667987; font: 10px monospace; }
.result-card { text-align: center; }
.result-card > p:not(.eyebrow) { color: var(--muted); line-height: 1.5; }
.result-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; margin: 25px 0; background: rgba(255,255,255,.1); }
.result-stats span { padding: 13px 4px; background: var(--panel); }
.result-stats small, .result-stats b { display: block; }
.result-stats small { color: var(--muted); font-size: .58rem; letter-spacing: .1em; }
.result-stats b { margin-top: 5px; font-size: 1.1rem; }

@media (hover: hover) and (pointer: fine) {
  .controls { display: none; }
}

@media (max-width: 640px), (hover: none) and (pointer: coarse) {
  html, body { width: 100%; height: 100%; overflow: hidden; }
  .game-shell {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100dvh;
    min-height: 0;
    margin: 0;
    padding: 0;
    display: block;
  }
  .topbar {
    position: absolute;
    z-index: 5;
    top: max(8px, env(safe-area-inset-top));
    left: 10px;
    right: 10px;
    align-items: center;
    flex-wrap: wrap;
    gap: 5px;
    pointer-events: none;
  }
  .brand p, .desktop-help, #pauseButton, #muteButton { display: none; }
  .brand-mark { font-size: 1.7rem; }
  .brand {
    width: 100%;
    min-width: 0;
    padding: 5px 7px;
    background: linear-gradient(90deg, rgba(5,9,13,.88), rgba(5,9,13,0));
    text-shadow: 0 2px 4px #000;
  }
  h1 { white-space: nowrap; }
  .hud { width: 100%; justify-content: flex-end; gap: 4px; }
  .hud-chip, .icon-button { min-height: 36px; }
  .hud-chip { padding-inline: 8px; }
  .weapon-chip { min-width: 72px; }
  .key-ring { min-width: 60px; gap: 6px; padding-inline: 7px; }
  .icon-button { font-size: .56rem; padding: 0 7px; }
  .stage-wrap {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    max-height: none;
    aspect-ratio: auto;
    border: 0;
    box-shadow: none;
  }
  #gameCanvas {
    position: absolute;
    inset: 0 auto 0 0;
    width: auto;
    height: 100%;
    max-width: none;
  }
  .toast { z-index: 3; top: 18%; }
  .shaft-map { height: 110px; top: 102px; right: 8px; }
  .controls {
    position: absolute;
    z-index: 5;
    left: max(10px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    bottom: max(10px, env(safe-area-inset-bottom));
    display: flex;
    margin: 0;
    pointer-events: none;
  }
  .control-button { pointer-events: auto; opacity: .9; }
}

@media (min-width: 641px) and (hover: hover) and (pointer: fine) and (max-height: 650px) and (orientation: landscape) {
  .game-shell { min-height: 100vh; padding-top: 6px; padding-bottom: 6px; }
  .topbar { position: absolute; z-index: 3; top: 10px; left: 20px; right: 20px; pointer-events: none; }
  .topbar button { pointer-events: auto; }
  .stage-wrap { max-height: calc(100vh - 12px); width: auto; align-self: center; }
  .controls { position: fixed; inset: auto 10px 10px; z-index: 4; pointer-events: none; }
  .control-button { pointer-events: auto; opacity: .82; }
  .desktop-help { display: none; }
}

@media (orientation: landscape) and (max-width: 640px),
       (orientation: landscape) and (hover: none) and (pointer: coarse) {
  #gameCanvas {
    inset: 50% auto auto 0;
    width: 100%;
    height: auto;
    max-height: none;
    transform: translateY(-50%);
  }
}
