:root {
  --bg: #111827;
  --panel: #1f2937;
  --panel-alt: #0f172a;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --accent: #38bdf8;
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #fb7185;
  --cell: 64px;
  --hud-h: 46px;
  --stage-pad: 10px;
  /*
   * Vertical space the play area does NOT get: the HUD bar plus the padding
   * above and below the stage. Canvases size themselves against
   * calc(100dvh - var(--play-vspace)), so this has to stay in step with
   * --hud-h and --stage-pad by hand.
   */
  --play-vspace: 66px;
  /*
   * Extra height the play surface gives up to on-screen controls. Zero on a
   * desktop and in landscape, where the controls float over the corners; a real
   * band upright, where a tall board would otherwise run down under a thumb.
   */
  --touch-reserve: 0px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, sans-serif;
  background: radial-gradient(circle at top, #1e3a8a, var(--bg) 40%);
  color: var(--text);
  min-height: 100vh;
}

a {
  color: var(--accent);
}

.hub {
  max-width: 1320px;
  margin: 0 auto;
  padding: 24px clamp(14px, 2vw, 28px);
}

.hero {
  text-align: center;
  margin-bottom: 24px;
}

.hero h1 {
  margin: 0 0 10px;
  font-size: clamp(1.8rem, 4vw, 2.6rem);
}

.hero p {
  margin: 0;
  color: var(--muted);
}

.game-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.game-card {
  background: linear-gradient(145deg, rgba(31, 41, 55, 0.9), rgba(15, 23, 42, 0.92));
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.32);
}

/*
 * The picture on a hub card.
 *
 * Fixed aspect rather than a fixed height, so every card in the grid shows the
 * same shape at whatever width the columns come out at — and `overflow: hidden`
 * on the rounded box is what rounds the SVG's corners, since the drawings
 * themselves carry no clip path (see thumbs.js for why).
 */
.game-thumb {
  display: block;
  aspect-ratio: 16 / 10;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(56, 189, 248, 0.22);
  transition: transform 120ms ease, border-color 120ms ease;
}

.game-thumb svg {
  display: block;
  width: 100%;
  height: 100%;
}

.game-thumb:hover,
.game-thumb:focus-visible {
  transform: translateY(-2px);
  border-color: rgba(56, 189, 248, 0.7);
}

@media (prefers-reduced-motion: reduce) {
  .game-thumb {
    transition: none;
  }

  .game-thumb:hover,
  .game-thumb:focus-visible {
    transform: none;
  }
}

.game-card h2 {
  margin: 0;
  font-size: 1.2rem;
}

.game-card p {
  margin: 0;
  color: var(--muted);
  min-height: 44px;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

button,
.btn {
  background: var(--accent);
  border: none;
  border-radius: 8px;
  color: #001018;
  font-weight: 700;
  padding: 8px 12px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.secondary,
.btn.secondary {
  background: #334155;
  color: var(--text);
}

.btn.back-link {
  margin-top: 16px;
}

.panel {
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid rgba(56, 189, 248, 0.32);
  border-radius: 12px;
  padding: 12px;
}

/* ── Game shell: a HUD bar above a stage that takes the rest ───── */

/*
 * Game pages are an app, not a document: the shell is exactly one viewport
 * tall and nothing scrolls except the drawer. Everything that used to live in
 * the sidebar is either a HUD chip (live state) or a drawer section (menus,
 * logs, instructions).
 */
body.game-body {
  height: 100dvh;
  overflow: hidden;
  /*
   * Under a thumb a game page is an app, not a document. Without these a drag
   * across the canvas pans the page, a double tap zooms into it, a long press
   * raises the copy/define callout over the top of whatever you were doing,
   * and every tap flashes a grey box. `manipulation` is deliberately not
   * `none`: it kills the double-tap zoom and the old 300ms click delay while
   * leaving pinch-to-zoom, which is somebody's accessibility setting and not
   * ours to take away. The canvas itself takes `none` further down, because
   * there a one-finger drag IS the input.
   */
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

/* Text you are meant to be able to select, in spite of the rule above. */
body.game-body input,
body.game-body textarea,
body.game-body .log,
body.game-body .drawer-body p {
  user-select: text;
  -webkit-user-select: text;
}

.game-shell {
  position: relative;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hud {
  flex: 0 0 auto;
  height: var(--hud-h);
  display: flex;
  align-items: center;
  gap: 10px;
  /*
   * Landscape on a notched phone puts the cutout down one side, so the padding
   * has to take the safe-area inset as well as its own. This only does anything
   * when the page asks for it with `viewport-fit=cover` in its viewport meta.
   */
  padding: 0 max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  background: linear-gradient(180deg, rgba(2, 6, 23, 0.94), rgba(2, 6, 23, 0.66));
  border-bottom: 1px solid rgba(56, 189, 248, 0.28);
  backdrop-filter: blur(6px);
  z-index: 30;
}

.hud-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--accent);
  white-space: nowrap;
  flex: 0 0 auto;
}

/*
 * The chips take the slack in the middle. They scroll sideways rather than
 * wrap, because wrapping would change the HUD's height and resize the canvas
 * underneath it mid-game.
 */
.hud-chips {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}

.hud-chips::-webkit-scrollbar {
  display: none;
}

.chip {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(31, 41, 55, 0.78);
  border: 1px solid rgba(148, 163, 184, 0.22);
  white-space: nowrap;
}

.chip-label {
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
}

.chip-value {
  font-size: 0.98rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.chip.good .chip-value {
  color: var(--good);
}

.chip.warn .chip-value {
  color: var(--warn);
}

.chip.bad .chip-value {
  color: var(--bad);
}

.chip.wide .chip-value {
  font-size: 0.85rem;
  font-weight: 600;
  max-width: 24ch;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

.hud-btn {
  background: rgba(2, 6, 23, 0.72);
  color: var(--accent);
  border: 1px solid rgba(56, 189, 248, 0.4);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.15;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 150ms ease;
}

.hud-btn:hover {
  background: rgba(56, 189, 248, 0.18);
}

.hud-btn.text {
  color: #001018;
  background: var(--accent);
  border-color: transparent;
}

.hud-btn.text:hover {
  background: #7dd3fc;
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: var(--stage-pad);
  /*
   * The stage CENTRES what it holds, so shrinking the canvas is only half of
   * making room for a control band — centred in the full height, a shorter
   * canvas just floats down into the band again. The stage has to give up the
   * same strip, and then the two agree by construction rather than by luck:
   * the canvas max-height is exactly this box's content height.
   */
  padding-bottom: calc(var(--stage-pad) + var(--touch-reserve));
  overflow: hidden;
}

/* For games whose play area is DOM rather than a canvas (typing, flashcards). */
.stage-card {
  width: min(100%, 860px);
  max-height: 100%;
  overflow-y: auto;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid rgba(56, 189, 248, 0.32);
  border-radius: 14px;
  padding: clamp(14px, 2.4vw, 26px);
}

/* ── Menu drawer ───────────────────────────────────────────────── */

.drawer-scrim {
  position: absolute;
  inset: 0;
  z-index: 40;
  background: rgba(2, 6, 23, 0.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
}

.drawer-scrim.open {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  width: min(94vw, 390px);
  display: flex;
  flex-direction: column;
  background: rgba(9, 15, 30, 0.98);
  border-left: 1px solid rgba(56, 189, 248, 0.35);
  box-shadow: -18px 0 44px rgba(2, 6, 23, 0.55);
  transform: translateX(100%);
  visibility: hidden;
  /*
   * Delay the visibility flip until the slide-out has finished, so the panel
   * animates away instead of vanishing on the first frame.
   */
  transition: transform 220ms ease, visibility 0s linear 220ms;
}

.drawer.open {
  transform: translateX(0);
  visibility: visible;
  transition: transform 220ms ease, visibility 0s linear 0s;
}

.drawer-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.2);
}

.drawer-head h2 {
  margin: 0;
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
}

.drawer-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px;
  display: grid;
  gap: 16px;
  align-content: start;
}

.drawer-section > h3 {
  margin: 0 0 8px;
  font-size: 0.92rem;
}

.drawer-section p {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.5;
}

.drawer-section p strong {
  color: var(--text);
}

.drawer-section label {
  display: block;
  margin-bottom: 4px;
  font-size: 0.85rem;
  color: var(--muted);
}

.drawer .stats {
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
}

.stats {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  margin: 10px 0;
}

.stat {
  background: rgba(31, 41, 55, 0.8);
  border-radius: 10px;
  padding: 8px;
  text-align: center;
}

.stat .label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
}

.stat .value {
  font-size: 1.2rem;
  font-weight: 700;
}

canvas {
  --canvas-ratio: 1.7778;
  /* A one-finger drag on a play surface is input, never a scroll. */
  touch-action: none;
  width: min(100%, calc((100dvh - var(--play-vspace) - var(--touch-reserve)) * var(--canvas-ratio)));
  max-width: 100%;
  max-height: calc(100dvh - var(--play-vspace) - var(--touch-reserve));
  height: auto;
  aspect-ratio: var(--canvas-ratio);
  display: block;
  margin: 0 auto;
  border-radius: 10px;
  border: 1px solid rgba(56, 189, 248, 0.25);
  background: #020617;
}

/*
 * Two canvases in the same box: a WebGL one carrying the 3D view, and a
 * transparent 2D one over it carrying the HUD. The 3D games need a depth
 * buffer, which means a GL context, which means the canvas can no longer take
 * `fillText` — so the flat stuff moves up a layer instead of being rewritten.
 *
 * The stack takes over the sizing the bare `canvas` rule would have done, so
 * the two layers cannot disagree about their size, and the border moves to the
 * stack so it is not drawn twice. `pointer-events: none` on the top layer lets
 * clicks and pointer moves reach the canvas underneath, which is where the
 * games listen for them.
 */
.canvas-stack {
  position: relative;
  /*
   * Fills the stage rather than holding a 16:9 box inside it. The stack used
   * to carry `aspect-ratio: 1.7778` and a width derived from the viewport
   * height, which kept the play area at the shape of the 960x540 backing store
   * the games were hard coded to — so a wide window got bars down both sides
   * and a tall one got them top and bottom, and either way the game was drawn
   * at 960x540 and scaled.
   *
   * `scripts/core/stage.js` sizes the backing stores from this box instead, so
   * the box is free to be whatever the browser gives it and the game is drawn
   * at that size. The 3D games hold their own aspect internally: the HUD keeps
   * a fixed design height and gains width, and the 3D camera simply sees more
   * of the world on a wider screen.
   *
   * `.stage` centres its child, so the stretch has to be asked for here — it
   * is a grid, and every 2D game in the hub still wants the centring.
   */
  justify-self: stretch;
  align-self: stretch;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  border-radius: 10px;
  border: 1px solid rgba(56, 189, 248, 0.25);
  overflow: hidden;
  background: #020617;
}

.canvas-stack > canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  aspect-ratio: auto;
  margin: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
}

.canvas-stack > .hud-layer {
  pointer-events: none;
}

.tetris-canvas {
  --canvas-ratio: 0.5;
  width: min(100%, calc((100dvh - var(--play-vspace) - var(--touch-reserve)) * 0.5));
  aspect-ratio: 1 / 2;
  max-height: calc(100dvh - var(--play-vspace) - var(--touch-reserve));
  margin: 0 auto;
}

#pong-canvas {
  --canvas-ratio: 1.8095;
}

#slice-canvas {
  --canvas-ratio: 1.7391;
}

#invaders-canvas {
  --canvas-ratio: 1.3846;
}

#racer-canvas {
  --canvas-ratio: 1.5652;
}

#adventure-canvas {
  --canvas-ratio: 1.5;
}

#breakout-canvas {
  --canvas-ratio: 1.3333;
}

#snake-canvas,
#tanks-canvas {
  --canvas-ratio: 1;
}

.adventure-section {
  margin: 10px 0;
}

/* Mud Ridge: the truck stat read-out in the garage. */
.truck-specs {
  display: grid;
  gap: 3px;
  margin-top: 10px;
}

.truck-specs .stat-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.8rem;
}

.truck-specs .label {
  color: var(--muted);
}

.truck-specs .bar {
  color: var(--accent);
  letter-spacing: 1px;
  white-space: nowrap;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.section-head h3 {
  margin: 0;
}

.spellbook {
  display: grid;
  gap: 8px;
}

.spell {
  border-radius: 9px;
  padding: 7px 8px;
  border: 1px solid rgba(148, 163, 184, 0.2);
  background: rgba(2, 6, 23, 0.38);
}

.spell.unlocked {
  border-color: rgba(52, 211, 153, 0.45);
}

.spell.locked {
  opacity: 0.72;
}

.spell .small {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 2px;
}

.talent-tree {
  display: grid;
  gap: 10px;
}

.talent-branch h4 {
  margin: 0 0 6px;
  font-size: 0.95rem;
}

.talent-nodes {
  display: grid;
  gap: 6px;
}

.talent-node {
  width: 100%;
  text-align: left;
  padding: 8px;
  background: #1e293b;
  color: var(--text);
  border: 1px solid #334155;
  border-radius: 8px;
  display: grid;
  gap: 3px;
}

.talent-node small {
  color: var(--muted);
  font-size: 0.76rem;
}

.talent-node.available {
  border-color: rgba(56, 189, 248, 0.65);
  box-shadow: 0 0 0 1px rgba(56, 189, 248, 0.2) inset;
}

.talent-node.unlocked {
  border-color: rgba(52, 211, 153, 0.8);
  background: rgba(16, 185, 129, 0.2);
}

#pacman-canvas,
canvas#board {
  --canvas-ratio: 1;
}

.shop {
  display: grid;
  gap: 8px;
  margin-bottom: 8px;
}

.shop-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  background: #1e293b;
  color: var(--text);
  border: 1px solid #334155;
  border-radius: 8px;
  cursor: pointer;
}

.shop-item:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.shop-item.owned {
  border-color: rgba(52, 211, 153, 0.8);
  background: rgba(16, 185, 129, 0.18);
  opacity: 1;
}

.shop-item .shop-name {
  display: block;
  font-weight: 700;
}

.shop-item .shop-desc {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  margin-top: 2px;
}

.shop-item .shop-cost {
  flex: 0 0 auto;
  font-weight: 700;
  color: var(--warn);
}

.shop-item.owned .shop-cost {
  color: var(--good);
}

ul.log {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;
  max-height: 240px;
  overflow: auto;
  border: 1px solid rgba(148, 163, 184, 0.16);
  border-radius: 9px;
}

ul.log:empty {
  border: none;
}

ul.log li {
  padding: 6px 8px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.2);
  font-size: 0.9rem;
}

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  background: rgba(2, 6, 23, 0.95);
  border: 1px solid rgba(52, 211, 153, 0.5);
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--good);
  font-weight: 700;
  opacity: 0;
  transform: translateY(8px);
  transition: all 220ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.game-over-overlay {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.72);
  display: grid;
  place-items: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
  z-index: 1200;
}

.game-over-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.game-over-card {
  width: min(92vw, 360px);
  background: #0b1220;
  border: 1px solid rgba(56, 189, 248, 0.5);
  border-radius: 14px;
  padding: 18px;
  text-align: center;
  box-shadow: 0 14px 40px rgba(2, 6, 23, 0.5);
}

.game-over-title {
  margin: 0 0 8px;
  text-transform: lowercase;
  letter-spacing: 0.04em;
}

.game-over-detail {
  margin: 0 0 12px;
  color: var(--muted);
}

.game-over-btn {
  min-width: 140px;
}

/* The optional second button. It only appears when a game asks for one, and it
   sits under the first rather than squeezing both onto one line. */
.game-over-alt {
  display: block;
  margin: 8px auto 0;
}

/* The way out, on every card. Set apart from the buttons above by a rule
   because these two leave the game and the ones above it do not — a "Main menu"
   sitting flush under "Play again" is one mis-tap away from throwing a run
   away. Side by side when there are two of them, and the pair shrinks to fit
   rather than pushing the card wider. */
.card-exits {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid rgba(148, 163, 184, 0.25);
}

.card-exits .game-over-btn {
  min-width: 0;
  flex: 1 1 0;
  padding-inline: 10px;
}

/* Same card, different reason for being there: the pause card is a hold rather
   than an ending, so it is a shade lighter over the game than the results card
   and its title is not the last word on anything. */
.pause-overlay {
  background: rgba(2, 6, 23, 0.62);
}

.board {
  width: min(100%, calc(100dvh - var(--play-vspace) - var(--touch-reserve)), 940px);
  max-width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  border: 2px solid rgba(56, 189, 248, 0.5);
  border-radius: 8px;
  overflow: hidden;
}

.square {
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  font-size: clamp(1.2rem, 2.8vw, 2rem);
  cursor: pointer;
  position: relative;
  user-select: none;
}

.square.light {
  background: #f1f5f9;
  color: #0f172a;
}

.square.dark {
  background: #64748b;
  color: #f8fafc;
}

.square.selected {
  outline: 3px solid var(--warn);
  outline-offset: -3px;
}

.square.legal::after {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(52, 211, 153, 0.7);
  position: absolute;
}

.piece {
  transition: transform 220ms ease;
  font-weight: 700;
}

.piece.white {
  color: #f8fafc;
  text-shadow: 0 1px 0 #0f172a, 0 0 3px rgba(15, 23, 42, 0.75);
}

.piece.black {
  color: #0b1220;
  text-shadow: 0 1px 0 #e2e8f0, 0 0 3px rgba(226, 232, 240, 0.8);
}

.piece.animating {
  transform: scale(1.15);
}

input,
select {
  width: 100%;
  background: #0b1220;
  color: var(--text);
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid rgba(148, 163, 184, 0.3);
  padding: 6px;
  text-align: left;
  font-size: 0.9rem;
}

@media (max-width: 720px) {
  :root {
    --hud-h: 42px;
    --play-vspace: 58px;
  }

  .hud-title {
    display: none;
  }

  .chip {
    padding: 2px 8px;
  }

  .chip-value {
    font-size: 0.9rem;
  }
}

/* ── Fullscreen support ────────────────────────────────────────── */

/*
 * The whole shell goes fullscreen, not just the play surface, so the HUD and
 * the drawer stay available. Because the shell is already sized to the
 * viewport, going fullscreen only trades browser chrome for play area and the
 * canvas sizing math carries over unchanged — which matters, because
 * getCanvasPoint() in scripts/core/input.js maps pointer events through the
 * canvas's own border box. Stretching that box (as the old object-fit rule
 * did) silently offset every click.
 */
.game-shell:fullscreen,
.game-shell:-webkit-full-screen {
  width: 100dvw;
  height: 100dvh;
  background: #020617;
}

/*
 * A list of keys, for the games that print one in the drawer.
 *
 * Not `.stats`: that is an auto-fitting grid of centred cards, which is right
 * for six short numbers and wrong for seventeen "do this — press that" rows.
 * Laid out as one row per binding, action on the left and the key on the right,
 * so the eye can run down either column.
 */
.keylist {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 10px 0;
}
.keylist div {
  align-items: baseline;
  background: rgba(31, 41, 55, 0.55);
  border-radius: 6px;
  display: flex;
  gap: 12px;
  justify-content: space-between;
  padding: 6px 10px;
}
.keylist div:nth-child(odd) {
  background: rgba(31, 41, 55, 0.8);
}
.keylist span:first-child {
  color: #cbd5e1;
}
.keylist kbd {
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid rgba(148, 163, 184, 0.45);
  border-radius: 5px;
  color: #e2e8f0;
  font-family: inherit;
  font-size: 0.85em;
  font-weight: 600;
  padding: 2px 7px;
  text-align: right;
  white-space: nowrap;
}
/*
 * A binding can need several keys and a word or two between them ("Left click
 * or F"), so the right-hand side of a row is its own flex line rather than a
 * single <kbd>. It wraps, because "1 2 3 or Right click" does not fit a narrow
 * drawer on a phone and a key list that clips is no list at all.
 */
.keylist .keys {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: flex-end;
}
.keylist .keyword {
  color: #94a3b8;
  font-size: 0.85em;
}
/* The group headings inside a generated key list. */
.drawer-section h4 {
  color: #93c5fd;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  margin: 14px 0 0;
  text-transform: uppercase;
}
.drawer-section h4:first-child {
  margin-top: 0;
}

/*
 * A `.stat` built from two bare spans put its label and its value on one line
 * with nothing between them, so "Blade" and "Chipped Blade" read as
 * "BladeChipped Blade". Stacking them gives the pair the same shape as the ones
 * that use the .label/.value classes.
 */
.stat > span:first-child {
  color: #94a3b8;
  display: block;
  font-size: 0.76em;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.stat > span:last-child {
  display: block;
  margin-top: 2px;
}

/*
 * Immersive games: the page gets out of the way.
 *
 * A 3D game marked `data-immersive` draws its own top bar and its own menu
 * inside the canvas (`scripts/core/hud3d.js`), so the page's header strip and
 * slide-over drawer would be a second, competing set of the same controls in a
 * different visual language, sitting outside the frame. The game fills the
 * viewport instead.
 *
 * The header is *not* removed and not `display: none`. It is moved out of
 * sight but kept in the layout tree, so its buttons stay in the tab order and
 * stay readable by a screen reader — canvas text is readable by neither, and
 * `hud3d` works by forwarding its clicks to these very buttons. Take them out
 * and both the accessibility and the wiring go with them.
 */
.game-shell[data-immersive] {
  height: 100dvh;
  gap: 0;
  padding: 0;
}

.game-shell[data-immersive] > .hud {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.game-shell[data-immersive] .stage {
  padding: 0;
}

.game-shell[data-immersive] .canvas-stack {
  border: 0;
  border-radius: 0;
}

/* ── On-screen controls ────────────────────────────────────────── */

/*
 * Built by scripts/core/touch.js on any device that reports a coarse pointer.
 * They float over the play surface rather than stealing height from it: on a
 * phone in landscape the canvas is already the whole screen, and giving a
 * quarter of it away to a control strip costs more than the controls are worth.
 * The cost of overlaying is that they sit on top of the picture, which is what
 * the transparency and the bottom-corner placement are for.
 *
 * `pointer-events: none` on the layer and `auto` on the controls means the two
 * corners are live and everything between them still reaches the canvas, so a
 * game that also wants a drag or a swipe keeps it.
 */
.touch-controls {
  position: absolute;
  inset: auto 0 0 0;
  height: var(--touch-h, min(40dvh, 200px));
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 max(14px, env(safe-area-inset-left)) max(14px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-right));
  pointer-events: none;
  z-index: 40;
  touch-action: none;
}

.touch-controls.is-hidden {
  display: none;
}

.touch-left,
.touch-right {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  pointer-events: none;
}

.touch-right {
  flex-direction: row-reverse;
  flex-wrap: nowrap;
  justify-content: flex-start;
}

.touch-pad {
  position: relative;
  /* Against the shorter dimension, for the same reason as the buttons: on a
     390px phone the pad and three buttons have to share one row. */
  width: clamp(96px, min(26dvh, 30dvw), 152px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 50%, rgba(15, 23, 42, 0.5), rgba(15, 23, 42, 0.74));
  border: 1px solid rgba(148, 163, 184, 0.35);
  backdrop-filter: blur(3px);
  pointer-events: auto;
  touch-action: none;
  transition: border-color 0.12s ease;
}

.touch-pad.is-active {
  border-color: rgba(56, 189, 248, 0.7);
}

/* A left/right or up/down pair is a lozenge, so its shape says which way it goes. */
.touch-pad[data-kind="lr"],
.touch-pad[data-kind="ud"] {
  border-radius: 999px;
}

.touch-pad[data-kind="lr"] {
  width: clamp(132px, min(30dvw, 34dvh), 200px);
  aspect-ratio: 2.4;
}

.touch-pad[data-kind="ud"] {
  width: clamp(70px, 16dvh, 96px);
  aspect-ratio: 0.38;
}

.touch-arrow {
  position: absolute;
  color: rgba(226, 232, 240, 0.62);
  font-size: 0.85rem;
  line-height: 1;
  pointer-events: none;
}

.touch-arrow-up { top: 9%; left: 50%; transform: translateX(-50%); }
.touch-arrow-down { bottom: 9%; left: 50%; transform: translateX(-50%); }
.touch-arrow-left { left: 9%; top: 50%; transform: translateY(-50%); }
.touch-arrow-right { right: 9%; top: 50%; transform: translateY(-50%); }

/* The knob follows the thumb, which is the only feedback that the pad is a
   region and not four buttons you keep missing. */
.touch-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 38%;
  aspect-ratio: 1;
  margin: -19% 0 0 -19%;
  border-radius: 50%;
  background: rgba(56, 189, 248, 0.32);
  border: 1px solid rgba(56, 189, 248, 0.55);
  pointer-events: none;
  transition: transform 0.06s linear;
}

.touch-pad[data-kind="lr"] .touch-knob { width: 16%; margin: -8% 0 0 -8%; aspect-ratio: 1; }
.touch-pad[data-kind="ud"] .touch-knob { width: 62%; margin: -31% 0 0 -31%; }

.touch-btn {
  /*
   * Sized against the SHORTER dimension of the two, not just the height: three
   * buttons across the bottom of a 390px phone have to fit next to each other
   * or the row wraps and the third one lands somewhere nobody aimed for.
   */
  width: clamp(52px, min(13dvh, 15dvw), 74px);
  aspect-ratio: 1;
  border-radius: 50%;
  border: 1px solid rgba(148, 163, 184, 0.4);
  background: rgba(15, 23, 42, 0.66);
  backdrop-filter: blur(3px);
  color: #e2e8f0;
  font-size: 1.3rem;
  line-height: 1;
  display: grid;
  place-items: center;
  padding: 0;
  pointer-events: auto;
  touch-action: none;
  transition: transform 0.06s ease, background 0.12s ease, border-color 0.12s ease;
}

.touch-btn.is-active {
  background: rgba(56, 189, 248, 0.34);
  border-color: rgba(56, 189, 248, 0.8);
  transform: scale(0.93);
}

/*
 * The first button in the row is the one the thumb lands on without moving, so
 * it is the biggest and it is the one a game should list first.
 */
.touch-right .touch-btn:first-child {
  width: clamp(62px, min(16dvh, 18dvw), 92px);
  font-size: 1.55rem;
}

/*
 * LANDSCAPE overlays, PORTRAIT makes room.
 *
 * In landscape a phone is already giving the canvas its full height and a wide
 * game leaves empty margins at the sides, so floating the controls over the
 * corners costs nothing anybody misses. Upright is the opposite: a tall or
 * square board fills the width and runs all the way down, and the bottom of it
 * — where the stack is in Tetris, where you are in Snake — is exactly where a
 * thumb would be. So upright, the controls get a band of their own and the
 * canvas is told to stop above it.
 *
 * `--play-vspace` is the height the canvas may not use. It inherits, so raising
 * it on the shell reaches the canvas sizing rule without that rule knowing the
 * controls exist.
 */
@media (orientation: portrait) {
  html[data-touch="on"] .game-shell:has(.touch-controls) {
    --touch-h: clamp(140px, 22dvh, 190px);
    --touch-reserve: var(--touch-h);
  }

  html[data-touch="on"] .touch-controls {
    align-items: center;
  }
}

/* ── "Turn your phone sideways" ────────────────────────────────── */

.rotate-hint {
  position: absolute;
  inset: var(--hud-h) 0 0 0;
  z-index: 60;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
  background: rgba(2, 6, 23, 0.93);
  backdrop-filter: blur(4px);
}

.rotate-hint p {
  margin: 0;
  color: var(--muted);
  max-width: 26ch;
}

.rotate-hint p strong {
  color: var(--text);
  font-size: 1.15rem;
}

.rotate-hint-icon {
  font-size: 2.6rem;
  animation: rotate-nudge 2.4s ease-in-out infinite;
}

.rotate-hint button {
  margin-top: 8px;
}

@keyframes rotate-nudge {
  0%, 55%, 100% { transform: rotate(0deg); }
  70%, 85% { transform: rotate(90deg); }
}

@media (prefers-reduced-motion: reduce) {
  .rotate-hint-icon {
    animation: none;
  }
}

/*
 * Touch phones are short in landscape, and the HUD is the one thing on a game
 * page that can afford to give height back.
 */
@media (pointer: coarse) and (max-height: 460px) {
  :root {
    --hud-h: 38px;
    --play-vspace: 50px;
  }
}

/*
 * The one line of instructions that only makes sense on a touch screen, added
 * to the drawer by scripts/core/touch.js and marked out from the keyboard
 * instructions it sits next to.
 */
.touch-note {
  margin: 8px 0;
  padding: 8px 10px;
  border-left: 2px solid rgba(56, 189, 248, 0.6);
  background: rgba(56, 189, 248, 0.08);
  border-radius: 0 6px 6px 0;
  color: var(--text);
}
