:root {
  --bg: #0b0e14;
  --panel: #141922;
  --line: #2a3341;
  --ink: #e6edf3;
  --dim: #8b98a9;
  --accent: #ffb454;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 1rem 3rem;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

header {
  display: flex;
  align-items: center;
  gap: 1rem;
  max-width: 40rem;
  margin: 0 auto;
  padding: 1.25rem 0;
}

h1 { font-size: 1.25rem; letter-spacing: 0.08em; margin: 0; }
h2 { font-size: 1rem; letter-spacing: 0.06em; margin: 0 0 0.5rem; }

main { max-width: 40rem; margin: 0 auto; }

button {
  font: inherit;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
}

button:hover { border-color: var(--accent); }

#cabinets { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.75rem; }

#cabinets button {
  width: 100%;
  text-align: left;
  padding: 1rem;
  display: grid;
  gap: 0.25rem;
}

.cab-title { color: var(--accent); letter-spacing: 0.06em; }
.cab-desc, .cab-controls { color: var(--dim); font-size: 0.85rem; }

#hud {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 0;
  color: var(--dim);
  letter-spacing: 0.06em;
}

#hud-score { color: var(--accent); margin-left: auto; }

/* Controls live inside the stage, not the page header, so they remain
   reachable in fullscreen -- where the header is not rendered at all. */
#controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.25rem 0 0.5rem;
}

#controls button {
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  letter-spacing: 0.06em;
}

#back { margin-right: auto; }

canvas {
  display: block;
  /* Constrained on both axes and left to its declared aspect ratio. A
     width-only cap makes a portrait playfield taller than the window, which
     pushes the ground below the fold. */
  width: auto;
  height: auto;
  max-width: min(30rem, 100%);
  max-height: calc(100svh - 11rem);
  margin: 0 auto;
  /* Overridden per game from the manifest; this is only the fallback. */
  aspect-ratio: 1;
  background: #070a0f;
  border: 1px solid var(--line);
  border-radius: 8px;
  touch-action: none;
}

#hint { color: var(--dim); font-size: 0.85rem; text-align: center; }

#board {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
  margin-top: 1rem;
}

#entry { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

input {
  font: inherit;
  text-transform: uppercase;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.4rem;
  width: 5rem;
}

#verdict { color: var(--dim); font-size: 0.85rem; min-height: 1.5em; }
#verdict.bad { color: #ff7b72; }
#verdict.good { color: #7ee787; }



/* ---- immersive play -------------------------------------------------------
   Two mechanisms, because iOS Safari will not put a non-video element into
   real fullscreen. `:fullscreen` handles every other browser; the
   `.immersive` class is the fallback that simply fills the viewport. Both
   produce the same layout, so the game does not need to know which is in use.
   Safe-area insets keep the HUD clear of notches and home indicators.        */

#stage:fullscreen,
#stage.immersive {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin: 0;
  padding:
    calc(env(safe-area-inset-top) + 0.5rem)
    calc(env(safe-area-inset-right) + 0.5rem)
    calc(env(safe-area-inset-bottom) + 0.5rem)
    calc(env(safe-area-inset-left) + 0.5rem);
  background: var(--bg);
  overflow: auto;
}

#stage:fullscreen #hud,
#stage.immersive #hud,
#stage:fullscreen #controls,
#stage.immersive #controls {
  width: min(100%, 34rem);
  padding: 0;
}

/* As large as fits, in whatever proportion the game declared. Constraining
   both axes and letting the aspect ratio do the rest handles square and
   portrait playfields without special-casing either. */
#stage:fullscreen canvas,
#stage.immersive canvas {
  width: auto;
  height: auto;
  max-width: calc(100vw - 1rem);
  max-height: calc(100svh - 7rem);
}

/* The hint is guidance for a first play, not something to spend screen on. */
#stage:fullscreen #hint,
#stage.immersive #hint { display: none; }

/* Keep the score table reachable but out of the way while playing. */
#stage:fullscreen #board,
#stage.immersive #board { display: none; }

#stage:fullscreen #gameover,
#stage.immersive #gameover { width: min(100%, 34rem); }

/* Comfortable touch targets. 44px is the accepted minimum. */
@media (pointer: coarse) {
  button, input { min-height: 44px; }
  #fullscreen { min-height: 36px; }
}

/* ---- D-pad ----------------------------------------------------------------
   Three-column grid so the arrows sit where a thumb expects them. Rendered
   only when the game's manifest asks for it. Hidden on devices that have a
   real pointer and no touch, where the keyboard is the better control.       */

/* A bare `display: grid` here outranks the user-agent [hidden] rule, which
   left the pad on screen permanently. Restore the attribute's meaning. */
#pad[hidden] { display: none; }

#pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 0.5rem;
  width: min(15rem, 70vw);
  margin: 0.75rem auto 0;
  touch-action: none;
}

.pad-btn {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  font-size: 1.4rem;
  line-height: 1;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink);
  /* A control you hold should not select text or fire a phantom double-tap. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.pad-btn:active { background: var(--line); border-color: var(--accent); }

.pad-up    { grid-column: 2; grid-row: 1; }
.pad-left  { grid-column: 1; grid-row: 2; }
.pad-down  { grid-column: 2; grid-row: 2; }
.pad-right { grid-column: 3; grid-row: 2; }

/* Keyboard is better where there is a real pointer and no touch at all. */
@media (pointer: fine) and (hover: hover) and (any-pointer: fine) {
  #pad.auto-hide { display: none; }
}

/* In immersive play the pad sits directly under the playfield. */
#stage:fullscreen #pad,
#stage.immersive #pad { margin-top: 0.5rem; }

#paused-note {
  text-align: center;
  color: var(--accent);
  letter-spacing: 0.1em;
  min-height: 1.5em;
  margin: 0.25rem 0 0;
}

/* ---- cabinet thumbnails ---------------------------------------------------
   Each game draws its own icon, so the shell needs no per-game assets and
   no knowledge of what any game looks like.                                 */

#cabinets button {
  grid-template-columns: auto 1fr;
  grid-template-areas:
    "icon title"
    "icon desc"
    "icon controls";
  align-items: center;
  column-gap: 0.9rem;
}

.cab-icon {
  grid-area: icon;
  width: 64px;
  height: 64px;
  border-radius: 8px;
  background: #0d1117;
  border: 1px solid var(--line);
}

.cab-title { grid-area: title; }
.cab-desc { grid-area: desc; }
.cab-controls { grid-area: controls; }

/* The start screen is the canvas itself, so it only needs a pointer cue. */
#screen.attract { cursor: pointer; }

/* ---- score dialog ---------------------------------------------------------
   Anchored to the top of the *visible* viewport rather than centred. When the
   on-screen keyboard opens it covers the lower half of the screen, so a
   centred dialog ends up behind it. JS keeps this box aligned to
   visualViewport, which is the only reliable way to track the keyboard.     */

#modal[hidden] { display: none; }

#modal {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  height: 100%;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: max(env(safe-area-inset-top), 1rem) 1rem 1rem;
  background: rgba(7, 10, 15, 0.82);
  overflow-y: auto;
}

#modal-card {
  width: min(24rem, 100%);
  background: var(--panel);
  border: 1px solid var(--border-strong, var(--line));
  border-radius: 10px;
  padding: 1.1rem;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
}

#modal-title { color: var(--accent); margin-bottom: 0.6rem; }

#final { margin: 0 0 0.75rem; color: var(--ink); }

#result-line { margin: 0 0 0.6rem; color: var(--dim); font-size: 0.9rem; }
#result-line.good { color: #7ee787; }
#result-line.bad { color: #ff7b72; }

#result-scores {
  margin: 0 0 1rem;
  max-height: 42vh;
  overflow-y: auto;
}

/* The player's own row. Marked with more than colour alone so it still reads
   for anyone who cannot distinguish it. */
#result-scores li.me {
  color: var(--accent);
  font-weight: 700;
}
#result-scores li.me span { color: var(--accent); }
#result-scores li.me::marker { color: var(--accent); }

#result-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; }
#result-actions button { flex: 1 1 8rem; }

/* ---- score tables ---------------------------------------------------------
   Three columns: position, initials, score. No list markers -- those sit
   outside the content box and get clipped by the scroll container, and they
   cannot be aligned, which double-digit positions need.                      */

#scores, #result-scores {
  list-style: none;
  margin: 0;
  padding: 0;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}

#scores li, #result-scores li {
  display: grid;
  grid-template-columns: 2.5ch 1fr auto;
  gap: 0.9rem;
  align-items: baseline;
  padding: 0.22rem 0.15rem;
}

#scores li + li, #result-scores li + li {
  border-top: 1px solid rgba(255, 255, 255, 0.05);
}

/* The empty state is prose, not a row, so it must not be squeezed into the
   narrow rank column. */
#scores li.empty, #result-scores li.empty { display: block; padding: 0.3rem 0; }

#scores .rank, #result-scores .rank { text-align: right; color: var(--dim); }
#scores .who, #result-scores .who { color: var(--ink); letter-spacing: 0.12em; }
#scores .pts, #result-scores .pts { color: var(--ink); }

#scores li.me, #result-scores li.me { color: var(--accent); font-weight: 700; }
#scores li.me .rank, #result-scores li.me .rank,
#scores li.me .who, #result-scores li.me .who,
#scores li.me .pts, #result-scores li.me .pts { color: var(--accent); }

/* ---- shooter pad ----------------------------------------------------------
   Left and right are held; the middle button fires. Wider than the D-pad
   because both thumbs are in play.                                          */

#shootpad[hidden] { display: none; }

#shootpad {
  display: grid;
  grid-template-columns: 1fr 1.4fr 1fr;
  gap: 0.6rem;
  width: min(22rem, 94vw);
  margin: 0.75rem auto 0;
  touch-action: none;
}

#shootpad .pad-btn { aspect-ratio: auto; min-height: 60px; }
#shootpad .shoot-fire { font-size: 0.95rem; letter-spacing: 0.14em; color: var(--accent); }

@media (pointer: fine) and (hover: hover) and (any-pointer: fine) {
  #shootpad.auto-hide { display: none; }
}

#stage:fullscreen #shootpad,
#stage.immersive #shootpad { margin-top: 0.5rem; }
