/* Chain Reaction — layout + neon UI */

/* Self-hosted fonts (latin subset) — no external dependency, works offline.
   Glyphs the pixel fonts lack (↶ ⟲ ☰ emoji) fall back to the system font. */
@font-face {
  font-family: "Press Start 2P";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("assets/fonts/press-start-2p.woff2") format("woff2");
}
@font-face {
  font-family: "VT323";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("assets/fonts/vt323.woff2") format("woff2");
}

:root {
  --bg: #0a0a14;
  --panel: rgba(255, 255, 255, 0.05);
  --accent: #21e6ff;
  --text: #eaf6ff;
  --muted: #7c8db0;
}

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

html, body {
  margin: 0;
  height: 100%;
  /* Neon glows over deep space so the desktop letterbox isn't dead black */
  background:
    radial-gradient(ellipse at 50% -8%, rgba(124, 92, 252, 0.20), transparent 55%),
    radial-gradient(ellipse at 12% 108%, rgba(33, 230, 255, 0.12), transparent 50%),
    radial-gradient(ellipse at 88% 108%, rgba(255, 46, 168, 0.12), transparent 50%),
    #07070f;
  color: var(--text);
  font-family: "Trebuchet MS", system-ui, -apple-system, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* The animated neon grid + random light pulses live on the #bg canvas; CRT
   scanlines sit on top. The board canvas is opaque and above both, so
   gameplay stays crisp. */
#bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
  display: block;
}
body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.18) 0 1px, transparent 1px 3px);
  opacity: 0.5;
}
#app { position: relative; z-index: 1; }

#app {
  /* --board is sized by whichever is smallest: width, a hard cap, or the
     height left over after the topbar + controls. That last term is what
     stops vertical clipping on short phones. */
  height: 100vh;
  --reserved: 164px;
  --board: min(92vw, 520px, calc(100vh - var(--reserved)));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 12px;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}
@supports (height: 100dvh) {
  /* dvh tracks the shrinking/growing mobile toolbars */
  #app {
    height: 100dvh;
    --board: min(92vw, 520px, calc(100dvh - var(--reserved)));
  }
}

.topbar {
  width: var(--board);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.title {
  font-family: "Press Start 2P", system-ui, monospace;
  font-size: clamp(11px, 3.1vw, 20px);
  letter-spacing: 1px;
  line-height: 1.4;
  white-space: nowrap;
  text-align: center;
  background: linear-gradient(90deg, #21e6ff, #ff2ea8);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 0 10px rgba(33, 230, 255, 0.5)) drop-shadow(0 0 16px rgba(255, 46, 168, 0.25));
}

.scores { display: flex; gap: 10px; }

.stat {
  background: var(--panel);
  border: 1px solid rgba(120, 160, 255, 0.15);
  border-radius: 12px;
  padding: 6px 14px;
  text-align: center;
  min-width: 78px;
}
.stat .label {
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--muted);
  text-transform: uppercase;
}
.stat .value {
  font-size: clamp(16px, 4vw, 22px);
  font-weight: 800;
  display: inline-block;
}
#score.bump {
  animation: scoreBump 0.32s ease-out;
}
/* keyframes scoreBump defined once in the retro section below */

#board {
  width: var(--board);
  height: var(--board);
  border-radius: 18px;
  background: rgba(10, 12, 24, 0.6);
  box-shadow: 0 0 60px rgba(33, 230, 255, 0.08), inset 0 0 40px rgba(0, 0, 0, 0.6);
  display: block;
}

.controls {
  width: var(--board);
  display: flex;
  gap: 10px;
  justify-content: space-between;
  align-items: center;
}

button.btn {
  flex: 1;
  background: var(--panel);
  border: 1px solid rgba(120, 160, 255, 0.2);
  color: var(--text);
  font-family: inherit;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 1px;
  padding: 12px;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.08s ease, border-color 0.2s ease, background 0.2s ease;
}
button.btn:hover { border-color: var(--accent); background: rgba(33, 230, 255, 0.08); }
button.btn:active { transform: scale(0.96); }
button.btn:disabled { opacity: 0.35; cursor: default; }

.icon-btn {
  flex: 0 0 auto;
  width: 44px;
  padding: 12px 0;
  font-size: 16px;
}

/* Game over modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 6, 14, 0.78);
  backdrop-filter: blur(6px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.modal-backdrop.show { display: flex; }

.modal {
  width: min(86vw, 380px);
  max-height: 88vh;
  overflow-y: auto;
  background: linear-gradient(180deg, #15172b, #0c0d18);
  border: 1px solid rgba(120, 160, 255, 0.25);
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  box-shadow: 0 0 80px rgba(255, 46, 168, 0.18);
}
.modal h2 {
  margin: 0 0 6px;
  font-size: 28px;
  letter-spacing: 2px;
  background: linear-gradient(90deg, #21e6ff, #ff2ea8);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.modal .row {
  display: flex;
  justify-content: space-between;
  padding: 8px 4px;
  border-bottom: 1px solid rgba(120, 160, 255, 0.12);
  font-size: 15px;
}
.modal .row .k { color: var(--muted); }
.modal .row .v { font-weight: 800; }
.modal .actions { margin-top: 20px; display: flex; flex-direction: column; gap: 10px; }
.modal button.primary {
  background: linear-gradient(90deg, #21e6ff, #2a7bff);
  color: #04121f;
  border: none;
}
.modal button.primary:hover { filter: brightness(1.1); }

/* Easy-mode toggle when active */
button.btn.active {
  border-color: #27e07a;
  background: rgba(39, 224, 122, 0.14);
  color: #8df7bb;
}

/* Tutorial */
.modal.tutorial { min-height: 320px; display: flex; flex-direction: column; }
.tut-canvas {
  width: 100%;
  height: 104px;
  display: block;
  margin-bottom: 8px;
}
.tut-body {
  flex: 1;
  font-size: 15px;
  line-height: 1.55;
  color: #c9d8f5;
  margin: 6px 0 14px;
  min-height: 72px;
}
.tut-dots { display: flex; gap: 8px; justify-content: center; margin-bottom: 6px; }
.tut-dots .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: rgba(120, 160, 255, 0.25);
  transition: background 0.2s ease, transform 0.2s ease;
}
.tut-dots .dot.on { background: var(--accent); transform: scale(1.3); box-shadow: 0 0 10px var(--accent); }

/* Difficulty toggle inside the tutorial (settings) */
.tut-setting {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 12px;
}
.tut-setting-label {
  font-size: 11px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--muted);
}
button.easy-toggle {
  flex: 0 0 auto;
  min-width: 96px;
  padding: 8px 18px;
  font-size: 13px;
}

/* Menu */
.menu-list { display: flex; flex-direction: column; gap: 8px; margin: 12px 0 14px; }
.menu-settings {
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 1px solid rgba(120, 160, 255, 0.14);
  padding-top: 14px;
}
.menu-settings .tut-setting { justify-content: space-between; margin: 0; }

/* High scores */
.hs-list {
  list-style: none;
  counter-reset: hs;
  margin: 12px 0 16px;
  padding: 0;
  max-height: 42vh;
  overflow-y: auto;
  text-align: left;
}
.hs-list li {
  counter-increment: hs;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 4px;
  border-bottom: 1px solid rgba(120, 160, 255, 0.1);
}
.hs-list li::before {
  content: counter(hs);
  color: var(--muted);
  font-weight: 800;
  width: 20px;
  text-align: right;
}
.hs-score { font-weight: 800; font-size: 17px; }
.hs-meta { margin-left: auto; color: var(--muted); font-size: 12px; }
.hs-empty { color: var(--muted); text-align: center; padding: 16px; }

/* Achievements */
.ach-count { font-size: 13px; color: var(--muted); -webkit-text-fill-color: var(--muted); }
.ach-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 12px 0 16px;
  max-height: 48vh;
  overflow-y: auto;
}

/* Achievement toast */
.ach-toast {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translate(-50%, -160%);
  display: flex;
  gap: 12px;
  align-items: center;
  background: linear-gradient(180deg, #15172b, #0c0d18);
  border: 1px solid rgba(255, 210, 63, 0.5);
  box-shadow: 0 0 30px rgba(255, 210, 63, 0.25);
  border-radius: 14px;
  padding: 10px 18px;
  z-index: 60;
  transition: transform 0.35s cubic-bezier(0.2, 1.3, 0.4, 1);
  pointer-events: none;
}
.ach-toast.show { transform: translate(-50%, 0); }
.ach-toast-badge { font-size: 24px; color: #ffd23f; filter: drop-shadow(0 0 8px rgba(255, 210, 63, 0.6)); }
.ach-toast-title { font-size: 10px; letter-spacing: 2px; color: var(--muted); }
.ach-toast-name { font-weight: 800; }

/* Dev panel (only injected when the URL has ?dev) */
.dev-panel {
  position: fixed;
  bottom: 8px;
  left: 8px;
  display: flex;
  flex-direction: column-reverse; /* toggle stays at the bottom, list opens upward */
  gap: 4px;
  z-index: 50;
  opacity: 0.9;
  max-height: 80vh;
  overflow: auto;
}
.dev-panel button {
  font: 600 11px/1 "Trebuchet MS", system-ui, sans-serif;
  background: #15172b;
  color: #9fb3e0;
  border: 1px solid rgba(120, 160, 255, 0.3);
  border-radius: 6px;
  padding: 5px 9px;
  cursor: pointer;
  text-align: left;
}
.dev-panel button:hover { border-color: #21e6ff; color: #fff; }

/* Narrow mobile: tighten the main control row */
@media (max-width: 430px) {
  .controls { gap: 6px; }
  button.btn { font-size: 12px; padding: 11px 8px; letter-spacing: 0.5px; }
  .icon-btn { width: 38px; padding: 11px 0; }
  #app { gap: 10px; --reserved: 150px; }
}

/* Short viewports: shrink gaps so the board never clips */
@media (max-height: 700px) {
  #app { gap: 8px; --reserved: 138px; }
  .stat { padding: 5px 12px; }
}

/* ============================================================
   GameVolt portal "chrome" — applied ONLY to the meta UI
   (menus, modals, high scores, achievements, toast). The game
   board and its neon HUD keep their own identity.
   ============================================================ */
:root {
  --gv-primary: #7c5cfc;
  --gv-surface: #181c30;
  --gv-text: #f0f0ff;
  --gv-muted: #a0a4c0;
  --gv-border: rgba(124, 92, 252, 0.2);
  --gv-grad: linear-gradient(135deg, #7c5cfc 0%, #38bdf8 50%, #06d6a0 100%);
  --gv-bronze: #cd7f32;
  --gv-silver: #c0c0c0;
  --gv-gold: #ffd700;
  --gv-platinum: #b4ffff;
  --gv-radius: 16px;
}

/* Panels / modals */
.modal {
  background: linear-gradient(180deg, #1a1f38, #10142a);
  border: 1px solid var(--gv-border);
  border-radius: var(--gv-radius);
  font-family: "Nunito", system-ui, sans-serif;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5), 0 0 40px rgba(124, 92, 252, 0.12);
}
.modal h2 {
  font-family: "Fredoka", system-ui, sans-serif;
  font-weight: 700;
  letter-spacing: 1px;
  background: var(--gv-grad);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.modal .row .k { color: var(--gv-muted); }

/* Buttons inside chrome (control-row buttons on the game screen stay neon) */
.modal .btn {
  font-family: "Nunito", system-ui, sans-serif;
  font-weight: 800;
  letter-spacing: 0.5px;
  background: var(--gv-surface);
  border: 1px solid var(--gv-border);
  color: var(--gv-text);
  border-radius: 10px;
}
.modal .btn:hover { border-color: var(--gv-primary); background: rgba(124, 92, 252, 0.14); }
.modal .btn.primary { background: var(--gv-grad); color: #0a0c18; border: none; }
.modal .btn.primary:hover { filter: brightness(1.08); }
.modal .btn.easy-toggle.active { border-color: #06d6a0; background: rgba(6, 214, 160, 0.16); color: #7ef0c8; }

/* High-score ranks: gold / silver / bronze for the top three */
.hs-list li:nth-child(1)::before { color: var(--gv-gold); }
.hs-list li:nth-child(2)::before { color: var(--gv-silver); }
.hs-list li:nth-child(3)::before { color: var(--gv-bronze); }

/* Achievements — tier headers + trophy cards */
.ach-tier-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: "Fredoka", system-ui, sans-serif;
  font-weight: 700;
  letter-spacing: 2px;
  font-size: 13px;
  text-align: left;
  margin: 14px 0 8px;
}
.ach-tier-head span { color: var(--gv-muted); font-size: 12px; letter-spacing: 1px; }
.ach-tier-head.bronze { color: var(--gv-bronze); }
.ach-tier-head.silver { color: var(--gv-silver); }
.ach-tier-head.gold { color: var(--gv-gold); }
.ach-tier-head.platinum { color: var(--gv-platinum); }

.ach-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 8px;
  margin-bottom: 6px;
}
.ach-card {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 12px 8px;
  text-align: center;
}
.ach-card.locked { opacity: 0.42; filter: grayscale(0.7); }
.ach-card.unlocked.bronze { border-color: rgba(205, 127, 50, 0.45); background: rgba(205, 127, 50, 0.07); }
.ach-card.unlocked.silver { border-color: rgba(192, 192, 192, 0.45); background: rgba(192, 192, 192, 0.07); }
.ach-card.unlocked.gold { border-color: rgba(255, 215, 0, 0.45); background: rgba(255, 215, 0, 0.08); }
.ach-card.unlocked.platinum {
  border-color: rgba(180, 255, 255, 0.5);
  background: rgba(180, 255, 255, 0.08);
  box-shadow: 0 0 18px rgba(180, 255, 255, 0.15);
}
.ach-card-icon { font-size: 28px; line-height: 1; margin-bottom: 6px; }
.ach-card-name { font-weight: 800; font-size: 13px; color: var(--gv-text); }
.ach-card-desc { font-size: 11px; color: var(--gv-muted); margin-top: 2px; }

/* Trophy toast — GameVolt style: slides up from the bottom, tier-coloured */
.ach-toast {
  top: auto;
  bottom: 24px;
  left: 50%;
  transform: translate(-50%, 180%);
  font-family: "Nunito", system-ui, sans-serif;
  border: 1px solid rgba(255, 215, 0, 0.4);
  box-shadow: 0 0 30px rgba(255, 215, 0, 0.2);
}
.ach-toast.show { transform: translate(-50%, 0); }
.ach-toast-label { font-size: 9px; letter-spacing: 3px; color: var(--gv-muted); font-weight: 700; }
.ach-toast-row { display: flex; gap: 8px; align-items: baseline; }
.ach-toast-tier { font-size: 9px; font-weight: 800; letter-spacing: 2px; }
.ach-toast-tier.bronze { color: var(--gv-bronze); }
.ach-toast-tier.silver { color: var(--gv-silver); }
.ach-toast-tier.gold { color: var(--gv-gold); }
.ach-toast-tier.platinum { color: var(--gv-platinum); }
.ach-toast-name { font-family: "Fredoka", system-ui, sans-serif; font-size: 15px; font-weight: 700; color: #fff; }
.ach-toast-badge { font-size: 26px; }

/* End-screen reactor rank */
.go-rank {
  font-family: "Fredoka", system-ui, sans-serif;
  font-weight: 700;
  letter-spacing: 3px;
  font-size: 14px;
  margin: -2px 0 14px;
}
.go-rank.stable { color: #06d6a0; text-shadow: 0 0 14px rgba(6, 214, 160, 0.5); }
.go-rank.overload { color: #ffb02e; text-shadow: 0 0 14px rgba(255, 176, 46, 0.5); }
.go-rank.breach { color: #ff2ea8; text-shadow: 0 0 16px rgba(255, 46, 168, 0.6); }

/* Dev panel — collapsible header so it can be tucked away on mobile */
.dev-panel.collapsed .dev-body { display: none; }
.dev-toggle {
  font: 700 11px/1 "Trebuchet MS", system-ui, sans-serif;
  background: #1a1f38;
  color: #21e6ff;
  border: 1px solid rgba(33, 230, 255, 0.4);
  border-radius: 6px;
  padding: 6px 9px;
  cursor: pointer;
  text-align: left;
  letter-spacing: 1px;
}
.dev-body { display: flex; flex-direction: column; gap: 4px; margin-top: 4px; }

/* ============================================================
   Retro arcade / CRT pass — menus lean into a TV-game look.
   Press Start 2P for headings, VT323 for readouts; CRT scanlines
   over the menu screens. Keeps the GameVolt colour tokens.
   ============================================================ */

/* In-game HUD readouts */
.stat .label { font-family: "VT323", monospace; font-size: 13px; letter-spacing: 2px; }
.stat .value { font-family: "VT323", monospace; font-size: clamp(22px, 5.5vw, 30px); line-height: 1; }
@keyframes scoreBump {
  0% { transform: scale(1); }
  35% { transform: scale(1.4); color: #21e6ff; text-shadow: 0 0 18px rgba(33, 230, 255, 0.9); }
  100% { transform: scale(1); }
}

/* Control-row buttons → chunky arcade text */
button.btn {
  font-family: "VT323", monospace;
  font-size: 19px;
  letter-spacing: 1px;
}

/* Menu / modal screens */
.modal {
  font-family: "VT323", monospace;
  border-width: 2px;
}
.modal h2 {
  font-family: "Press Start 2P", system-ui, monospace;
  font-size: clamp(13px, 4vw, 19px);
  line-height: 1.5;
  margin-bottom: 14px;
}
.modal .row { font-size: 19px; }
.modal .row .k { letter-spacing: 1px; }
.modal .btn {
  font-family: "VT323", monospace;
  font-size: 19px;
  letter-spacing: 1px;
  border-radius: 8px;
}

/* CRT scanlines over any open menu/overlay (pointer-events: none) */
.modal-backdrop::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.22) 0 1px, transparent 1px 3px);
  opacity: 0.45;
  z-index: 2;
  mix-blend-mode: multiply;
}

/* High-score readout */
.hs-score { font-family: "VT323", monospace; font-size: 22px; }
.hs-meta { font-family: "VT323", monospace; font-size: 15px; }
.hs-list li::before { font-family: "VT323", monospace; font-size: 20px; }
.hs-empty { font-family: "VT323", monospace; font-size: 18px; }

/* Achievements */
.ach-tier-head { font-family: "Press Start 2P", system-ui, monospace; font-size: 10px; }
.ach-tier-head span { font-family: "VT323", monospace; font-size: 15px; letter-spacing: 0; }
.ach-count { font-family: "VT323", monospace; font-size: 17px; }
.ach-card-name { font-family: "VT323", monospace; font-size: 16px; }
.ach-card-desc { font-family: "VT323", monospace; font-size: 13px; line-height: 1.15; }

/* End-screen rank + tutorial + toast */
.go-rank { font-family: "Press Start 2P", system-ui, monospace; font-size: 11px; letter-spacing: 2px; line-height: 1.6; }
.tut-body { font-family: "VT323", monospace; font-size: 19px; line-height: 1.3; }
.tut-setting-label { font-family: "VT323", monospace; font-size: 15px; }
.ach-toast-name { font-family: "Press Start 2P", system-ui, monospace; font-size: 11px; line-height: 1.5; }
.ach-toast-label, .ach-toast-tier { font-family: "VT323", monospace; }

/* Keyboard focus ring (only for keyboard nav, not mouse clicks) */
button:focus { outline: none; }
button:focus-visible {
  outline: 2px solid #21e6ff;
  outline-offset: 2px;
  box-shadow: 0 0 14px rgba(33, 230, 255, 0.5);
}
.modal button:focus-visible {
  outline-color: #a78bfa;
  box-shadow: 0 0 16px rgba(124, 92, 252, 0.55);
}

/* "RESUMED RUN" indicator — brief notice when a saved game is restored */
.resume-note {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  left: 50%;
  transform: translate(-50%, -140%);
  z-index: 40;
  pointer-events: none;
  font-family: "VT323", monospace;
  font-size: 18px;
  letter-spacing: 2px;
  color: #21e6ff;
  background: rgba(10, 12, 24, 0.9);
  border: 1px solid rgba(33, 230, 255, 0.4);
  box-shadow: 0 0 22px rgba(33, 230, 255, 0.25);
  border-radius: 10px;
  padding: 7px 16px;
  transition: transform 0.4s cubic-bezier(0.2, 1.3, 0.4, 1);
}
.resume-note.show { transform: translate(-50%, 0); }

/* Run goals (shown in the menu) */
.goals-box {
  text-align: left;
  margin: 4px 0 14px;
  padding: 10px 12px;
  border: 1px solid rgba(124, 92, 252, 0.25);
  border-radius: 12px;
  background: rgba(124, 92, 252, 0.06);
}
.goals-title {
  display: flex;
  justify-content: space-between;
  font-family: "Press Start 2P", system-ui, monospace;
  font-size: 9px;
  letter-spacing: 1px;
  color: var(--gv-muted);
  margin-bottom: 8px;
}
.goals-title span { color: #a78bfa; }
.goal {
  display: flex;
  gap: 8px;
  align-items: center;
  font-family: "VT323", monospace;
  font-size: 17px;
  color: var(--gv-muted);
  padding: 2px 0;
}
.goal-mark { color: #626580; width: 16px; text-align: center; }
.goal.done { color: #8df7bb; }
.goal.done .goal-mark { color: #06d6a0; }

/* Score-based rank line on the end screen */
.go-scorerank {
  font-family: "VT323", monospace;
  font-size: 19px;
  letter-spacing: 1px;
  color: #a78bfa;
  margin: -8px 0 14px;
}

/* Goal-completion toast tier */
.ach-toast-tier.goal { color: #21e6ff; }
