/* Self-hosted OFL fonts (woff2, subset latin) — the game is fully offline so NO CDN. Chakra Petch gives
   the techno/CRT wordmark; Space Grotesk is the clean UI/body face. font-display:swap avoids invisible text. */
@font-face { font-family: 'Chakra Petch'; font-weight: 600; font-display: swap; src: url('fonts/chakra-petch-600.woff2') format('woff2'); }
@font-face { font-family: 'Chakra Petch'; font-weight: 700; font-display: swap; src: url('fonts/chakra-petch-700.woff2') format('woff2'); }
@font-face { font-family: 'Space Grotesk'; font-weight: 400; font-display: swap; src: url('fonts/space-grotesk-400.woff2') format('woff2'); }
@font-face { font-family: 'Space Grotesk'; font-weight: 500; font-display: swap; src: url('fonts/space-grotesk-500.woff2') format('woff2'); }
@font-face { font-family: 'Space Grotesk'; font-weight: 700; font-display: swap; src: url('fonts/space-grotesk-700.woff2') format('woff2'); }

:root {
  --font-body: 'Space Grotesk', ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-display: 'Chakra Petch', 'Space Grotesk', ui-sans-serif, system-ui, sans-serif;
  --bg: #0a0716;
  --panel: #170d2b;
  --panel-2: #241640;
  --ink: #eaf2ff;
  --muted: #9f93c8;
  --accent: #00f0ff;
  --accent-2: #ff2d95;
  --chip: rgba(28, 18, 48, 0.78);
  --chip-border: rgba(120, 90, 220, 0.35);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

/* CRT scanlines + vignette over the 3D scene only (UI stays crisp above it). */
#fx-overlay {
  position: fixed;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  background:
    /* SOFT scanlines: a gentle gradient ramp (not a hard 1px edge) over a 3px period — the hard edge
       beat against device-pixel-ratio > 1 and produced moiré banding; the ramp dissolves cleanly. */
    repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.15) 0px, rgba(0, 0, 0, 0) 1.5px, rgba(0, 0, 0, 0) 3px),
    radial-gradient(120% 90% at 50% 42%, transparent 52%, rgba(10, 4, 22, 0.55) 100%);
  mix-blend-mode: multiply;
}
/* CRT effect toggle (Settings) — drop the scanlines/vignette but keep the 3D scene. */
body.no-crt #fx-overlay { display: none; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* Boot splash — shown until main.js has the scene up, then fades away. */
#boot {
  position: fixed; inset: 0; z-index: 60;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px;
  background: radial-gradient(120% 90% at 50% 35%, #160a30, #0a0716 70%);
  transition: opacity 0.45s ease;
}
#boot svg { filter: drop-shadow(0 0 16px rgba(0, 240, 255, 0.35)); }
#boot.gone { opacity: 0; pointer-events: none; }
.boot-spin { width: 30px; height: 30px; border-radius: 50%; border: 3px solid rgba(120, 90, 220, 0.25); border-top-color: #00f0ff; animation: bootspin 0.8s linear infinite; }
@keyframes bootspin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .boot-spin { animation-duration: 1.6s; } }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

#stage {
  position: fixed;
  inset: 0;
  z-index: 0;
}
/* Force the canvas to fill the container in CSS pixels regardless of the drawing-buffer
   size. Without this, a device-pixel-ratio > 1 (HiDPI screens, browser page-zoom) makes
   the canvas render larger than the viewport and the board looks zoomed-in / cropped. */
#stage canvas { display: block; width: 100% !important; height: 100% !important; }

/* ---------- HUD ---------- */
#hud-top {
  position: fixed;
  top: calc(8px + var(--safe-t));
  left: 8px;
  right: 8px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
#hud-top > * { pointer-events: auto; }
/* Three balanced zones: empty left / centred title / right controls — so the title stays dead-centre
   regardless of how many buttons sit on the right (the left zone mirrors the right's flex weight). */
.hud-side { flex: 1 1 0; min-width: 0; display: flex; align-items: center; gap: 8px; }
.hud-side.hud-right { justify-content: flex-end; }
.hud-center {
  flex: 0 1 auto;
  min-width: 0;
  text-align: center;
  line-height: 1.25;
}
.hud-title { font-family: var(--font-display); font-weight: 700; font-size: 15px; letter-spacing: 0.3px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.target-dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  vertical-align: middle;
  margin-right: 2px;
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.5);
}
.hud-stats { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.hud-stats b { color: var(--ink); font-weight: 700; }

#hint-bar {
  position: fixed;
  top: calc(58px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%);
  z-index: 9;
  max-width: min(92vw, 540px);
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  background: rgba(14, 16, 24, 0.55);
  padding: 6px 12px;
  border-radius: 999px;
  backdrop-filter: blur(6px);
  pointer-events: none;
}
#hint-bar.pulse { animation: pulse 1.1s ease; }
/* Brief (default) state: read as a short objective with a faint 💡 cue that fuller strategy sits behind
   the Hint button. Once the full strategy is revealed the class is removed and it reads as plain prose. */
#hint-bar.is-brief { font-style: italic; }
#hint-bar.is-brief::after { content: ' 💡'; font-style: normal; opacity: 0.55; }
@keyframes pulse {
  0% { color: var(--accent); transform: translateX(-50%) scale(1.06); }
  100% { color: var(--muted); transform: translateX(-50%) scale(1); }
}

/* ---------- buttons ---------- */
.chip {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 9px 13px;
  border-radius: 12px;
  cursor: pointer;
  backdrop-filter: blur(8px);
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease;
}
.chip:hover { background: var(--panel-2); }
.chip:active { transform: scale(0.94); }
/* Inline icons inherit the button's text colour + crisp stroke; sit on the text baseline. */
.ico { width: 1.1em; height: 1.1em; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chip.icon-only { padding: 9px; }
.chip.icon-only .ico { width: 1.25em; height: 1.25em; }
.chip.subtle { opacity: 0.66; font-weight: 500; }       /* de-emphasised AI Solve (a one-tap solver) */
.chip.subtle:hover { opacity: 1; }
.pad .ico { width: 26px; height: 26px; stroke-width: 2.25; }
.chip.primary { background: var(--accent); color: #06281c; border-color: transparent; }
.chip.primary:hover { background: #4ff0bb; }
.chip.big { font-size: 16px; padding: 13px 26px; }
.chip.active { background: var(--accent-2); color: #fff; }

#actions {
  position: fixed;
  left: 10px;
  bottom: calc(12px + var(--safe-b));
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-start;
}

/* ---------- D-pad ---------- */
#dpad {
  position: fixed;
  right: calc(14px + env(safe-area-inset-right, 0px));
  bottom: calc(14px + var(--safe-b));
  z-index: 10;
  width: 168px;
  height: 168px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
}
.pad {
  appearance: none;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  color: var(--ink);
  font-size: 22px;
  border-radius: 16px;
  cursor: pointer;
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.06s ease, background 0.12s ease;
}
.pad:active { transform: scale(0.9); background: var(--accent); color: #06281c; }
#pad-up { grid-area: 1 / 2; }
#pad-left { grid-area: 2 / 1; }
#pad-right { grid-area: 2 / 3; }
#pad-down { grid-area: 3 / 2; }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  /* Sits just under the hint-bar at the TOP — never over the board centre, so teaching text can't occlude
     the very thing it's teaching about. */
  top: calc(116px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  z-index: 30;
  max-width: min(88vw, 460px);
  text-align: center;
  background: rgba(20, 24, 38, 0.94);
  color: var(--ink);
  padding: 11px 18px;
  border-radius: 14px;
  font-weight: 600;
  font-size: 13.5px;
  line-height: 1.4;
  border: 1px solid var(--chip-border);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(8, 10, 16, 0.62);
  backdrop-filter: blur(4px);
  padding: 16px;
}
.overlay.show { display: flex; animation: fade 0.2s ease; }
/* Panels scale in with an ease-out-expo overshoot — reads far more "produced" than a flat opacity fade. */
.overlay.show .panel { animation: panelIn 0.28s cubic-bezier(0.16, 1, 0.3, 1); }
@keyframes panelIn { from { transform: scale(0.93) translateY(10px); opacity: 0; } to { transform: scale(1) translateY(0); opacity: 1; } }
/* Win stars pop in one at a time (delays set inline per star). `backwards` keeps them hidden until their turn. */
.stars .star { display: inline-block; }
.stars .star.earned { animation: starPop 0.42s cubic-bezier(0.16, 1, 0.3, 1) backwards; }
@keyframes starPop { 0% { transform: scale(0.2); opacity: 0; } 60% { transform: scale(1.35); opacity: 1; } 100% { transform: scale(1); opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .overlay.show .panel, .stars .star.earned { animation: none; } }
/* The level grid is a modal that can open ABOVE the main menu (and the win screen), so it must stack
   on top of the other overlays (which share z-index 40); closing it reveals whatever was beneath. */
#menu-overlay { z-index: 41; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.panel {
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--chip-border);
  border-radius: 22px;
  padding: 26px 24px;
  width: min(92vw, 420px);
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  /* never taller than the screen — overflowing panels scroll instead of spilling off-screen
     (critical for the 40-level select on a phone). dvh tracks the mobile dynamic viewport. */
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  overscroll-behavior: contain;
}
/* The level select: pin the "Select a level" + ✕ header, scroll only the grid beneath it. */
.panel.wide {
  width: min(94vw, 640px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel.wide .panel-head { flex: 0 0 auto; }
.panel.wide .level-grid {
  flex: 1 1 auto;
  min-height: 0;               /* let the grid shrink so it can scroll inside the flex column */
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;         /* re-enable touch scrolling (body sets touch-action: none) */
  -webkit-overflow-scrolling: touch;
  padding: 2px 4px 4px;        /* room so focus rings / active scale aren't clipped */
}
.panel h2 { font-family: var(--font-display); font-weight: 700; letter-spacing: 0.4px; margin: 6px 0 4px; font-size: 22px; }
.win-sub { color: var(--muted); margin: 4px 0 16px; font-size: 14px; line-height: 1.5; }
.win-sub b { color: var(--ink); }
.panel-row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.intro-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 6px; }

/* ---------- settings sheet ---------- */
.settings-list { display: flex; flex-direction: column; margin: 6px 0 4px; text-align: left; }
.settings-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 4px; font-size: 14px; font-weight: 600; border-bottom: 1px solid rgba(120, 90, 220, 0.14); }
.settings-row:last-child { border-bottom: none; }
.toggle { flex: none; width: 46px; height: 26px; padding: 0; border-radius: 999px; border: 1px solid var(--chip-border); background: rgba(20, 12, 36, 0.9); position: relative; cursor: pointer; transition: background 0.16s ease, border-color 0.16s ease; }
.toggle .knob { position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: var(--muted); transition: transform 0.18s cubic-bezier(0.16, 1, 0.3, 1), background 0.16s ease; }
.toggle.on { background: var(--accent); border-color: transparent; box-shadow: 0 0 12px rgba(0, 240, 255, 0.45); }
.toggle.on .knob { transform: translateX(20px); background: #06281c; }
.settings-credit { color: var(--muted); font-size: 11.5px; margin: 12px 0 0; opacity: 0.65; text-align: center; }
.panel-head { display: flex; align-items: center; gap: 10px; justify-content: space-between; margin-bottom: 14px; }
.panel-head h2 { margin: 0; white-space: nowrap; flex: none; }
.panel-head .menu-stars { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.stars { font-size: 40px; color: #ffd24a; letter-spacing: 4px; }
.logo {
  font-family: var(--font-display);
  font-size: 46px;
  font-weight: 700;
  letter-spacing: 7px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 8px;
}
.how { text-align: left; color: var(--muted); font-size: 13.5px; line-height: 1.7; margin: 0 0 18px; padding-left: 18px; }
.how b { color: var(--ink); }

/* ---------- level grid ---------- */
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
  gap: 10px;
}
.level-cell {
  appearance: none;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  color: var(--ink);
  border-radius: 14px;
  padding: 12px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 118px;
  align-items: flex-start;
  transition: transform 0.08s ease, background 0.15s ease;
}
.lc-top { display: flex; align-items: center; justify-content: space-between; width: 100%; }
.lc-diff { display: inline-flex; gap: 3px; }
.lc-diff .pip { width: 5px; height: 5px; border-radius: 50%; background: rgba(159, 147, 200, 0.28); }
.lc-diff .pip.on { background: var(--accent-2); box-shadow: 0 0 5px rgba(255, 45, 149, 0.5); }
.lc-mech { display: flex; gap: 5px; min-height: 15px; margin: 1px 0 2px; }
.lc-mech .mech { width: 15px; height: 15px; color: var(--muted); stroke-width: 1.9; }
.lc-foot { display: flex; align-items: center; justify-content: space-between; width: 100%; margin-top: auto; padding-top: 4px; gap: 8px; }
.level-cell:hover { background: var(--panel-2); }
.level-cell:active { transform: scale(0.96); }
.level-cell.locked { opacity: 0.45; cursor: not-allowed; }
.lc-num { font-family: var(--font-display); font-size: 20px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.lc-name { font-size: 13px; font-weight: 600; }
.lc-stars { font-size: 12px; letter-spacing: 2px; color: #ffd24a; line-height: 1; }
.lc-best { font-size: 11px; color: var(--muted); }
.level-cell.done { border-color: rgba(255, 210, 74, 0.45); }
.menu-stars { margin-left: auto; margin-right: 12px; font-size: 13px; font-weight: 700; color: #ffd24a; white-space: nowrap; }
.stars.unscored { color: var(--muted); text-shadow: none; opacity: 0.6; }
.level-cell.bonus { border-color: rgba(255, 180, 59, 0.4); }
.level-cell.bonus .lc-num { color: #ffb43b; }
.menu-section {
  grid-column: 1 / -1;
  margin: 8px 2px 0;
  font-family: var(--font-display);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: #ffb43b;
}

/* hide the D-pad hint cursor weirdness, keep autoplay obvious */
body.autoplaying #dpad, body.autoplaying #actions { opacity: 0.5; }

/* ---------- synthwave neon polish ---------- */
.hud-title { text-shadow: 0 0 10px rgba(0, 240, 255, 0.35); }
.hud-stats b { color: #5cf3ff; }
.logo {
  text-shadow: 0 0 26px rgba(255, 45, 149, 0.5);
  filter: drop-shadow(0 0 12px rgba(0, 240, 255, 0.35));
}
.chip { box-shadow: 0 0 0 1px rgba(120, 90, 220, 0.12), 0 6px 18px rgba(0, 0, 0, 0.4); }
.chip.primary { color: #07131f; box-shadow: 0 0 18px rgba(0, 240, 255, 0.5); }
.chip.primary:hover { background: #5cf3ff; }
.chip.active { background: var(--accent-2); color: #fff; box-shadow: 0 0 18px rgba(255, 45, 149, 0.6); }
.pad:active { background: var(--accent); color: #07131f; box-shadow: 0 0 20px rgba(0, 240, 255, 0.6); }
.stars { color: #ffd24a; text-shadow: 0 0 20px rgba(255, 200, 64, 0.55); }
.panel { box-shadow: 0 0 0 1px rgba(120, 90, 220, 0.25), 0 20px 70px rgba(0, 0, 0, 0.6), 0 0 60px rgba(123, 31, 162, 0.25); }
#hint-bar { border: 1px solid rgba(120, 90, 220, 0.25); }

/* Larger screens: roomier pad, bottom-centered controls */
@media (min-width: 760px) {
  .hud-title { font-size: 17px; }
  #dpad { width: 188px; height: 188px; }
}

/* Very short landscape (phones held sideways): shrink chrome a bit */
@media (max-height: 430px) {
  #dpad { width: 132px; height: 132px; }
  .pad { font-size: 18px; border-radius: 12px; }
  /* keep the teaching text — just make it compact (it used to vanish entirely in landscape) */
  #hint-bar { font-size: 11px; padding: 4px 10px; max-width: min(94vw, 440px); top: calc(46px + var(--safe-t)); }
  .chip { padding: 7px 10px; font-size: 12px; }
}
