/* EMBER — The Lab level chrome (from serpent/styles.css) plus the fire map. Ember on dark. */

@font-face { font-family: "Press Start 2P"; font-style: normal; font-weight: 400; font-display: swap; src: url("/press-start-2p.woff2") format("woff2"); }
@font-face { font-family: "Press Start 2P Fallback"; src: local("Courier New"); size-adjust: 166.64%; ascent-override: 60%; descent-override: 0%; line-gap-override: 0%; }

:root {
  --bg: #0a0a12;
  --surface: #12121d;
  --surface-2: #1a1a2a;
  --line: #28283c;
  --text: #e9e9f2;
  --dim: #9aa0b5;
  --cyan: #45f0ff;
  --magenta: #ff3d8b;
  --gold: #ffc857;
  --green: #41ff8b;
  --red: #ff5560;
  --ember: #ff7043;
  --pixel: "Press Start 2P", "Press Start 2P Fallback", "Courier New", monospace;
  --body: -apple-system, "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
  --radius: 8px;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --glow-cyan: 0 0 14px rgba(69, 240, 255, 0.35);
  --glow-ember: 0 0 16px rgba(255, 112, 67, 0.45);
  --space-gutter: clamp(16px, 4vw, 48px);
}

*, *::before, *::after { box-sizing: border-box; }

html { color-scheme: dark; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(1200px 500px at 50% -10%, rgba(255, 112, 67, 0.07), transparent 70%),
    radial-gradient(900px 400px at 80% 110%, rgba(255, 200, 87, 0.05), transparent 70%),
    var(--bg);
  color: var(--text);
  font-family: var(--body);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

::selection { background: rgba(255, 112, 67, 0.3); }
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: 12px; top: -48px;
  z-index: 200;
  background: var(--cyan);
  color: #05050d;
  padding: 10px 16px;
  border-radius: 6px;
  font-family: var(--mono);
  font-weight: 700;
  text-decoration: none;
  transition: transform 0.2s var(--ease-out);
}
.skip-link:focus-visible { transform: translateY(60px); }

:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 2px;
  box-shadow: 0 0 0 5px rgba(69, 240, 255, 0.15);
}

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

/* ---------- header ---------- */

.site-header {
  padding: 26px var(--space-gutter) 10px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.lab-link {
  align-self: flex-start;
  font-family: var(--pixel);
  font-size: 10px;
  color: var(--dim);
  text-decoration: none;
  padding: 6px 2px;
  transition: color 0.15s, text-shadow 0.15s;
}
.lab-link:hover { color: var(--cyan); text-shadow: var(--glow-cyan); }

.level { margin: 0; font-family: var(--pixel); font-size: 9px; letter-spacing: 3px; color: var(--gold); }

.wordmark {
  margin: 6px 0 4px;
  font-family: var(--pixel);
  font-size: clamp(22px, 4.5vw, 34px);
  letter-spacing: 3px;
  color: var(--text);
  text-shadow: var(--glow-ember), 3px 3px 0 var(--magenta);
}

.tagline {
  margin: 0;
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--dim);
}

.cursor { color: var(--ember); animation: blink 1.1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- layout ---------- */

main {
  flex: 1;
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: 20px var(--space-gutter) 72px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.game-panel { display: flex; flex-direction: column; gap: 14px; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.action-row, .chip-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.map-code {
  margin: 0;
  font-family: var(--pixel);
  font-size: 9px;
  letter-spacing: 1px;
  color: var(--dim);
}
.map-code output { color: var(--gold); font-family: var(--mono); font-size: 13px; font-weight: 700; letter-spacing: 2px; }

.btn, .chip {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
  text-decoration: none;
  transition: transform 0.12s var(--ease-out), border-color 0.12s, box-shadow 0.12s, color 0.12s;
}

.btn {
  gap: 8px;
  padding: 10px 14px;
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 12.5px;
  font-weight: 700;
}
.btn:hover, .chip:hover { border-color: var(--cyan); box-shadow: 0 0 12px rgba(69, 240, 255, 0.22); transform: translateY(-1px); }
.btn:active, .chip:active { transform: translateY(1px); }
.btn:disabled, .chip:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }
.btn[aria-disabled="true"] { opacity: 0.7; cursor: progress; transform: none; }

.btn-primary { background: var(--ember); border-color: var(--ember); color: #1a0703; }
.btn-primary:hover { box-shadow: var(--glow-ember); border-color: var(--gold); }
.btn-ghost { background: transparent; }
/* Fixed "SOUND" label; the struck-through look shows it is off (aria-pressed="false"). */
.btn-sound[aria-pressed="false"] { color: var(--dim); text-decoration: line-through; text-decoration-thickness: 2px; }
.btn-big { font-family: var(--pixel); font-size: 11px; padding: 14px 20px; }

.chip {
  padding: 8px 14px;
  background: var(--bg);
  color: var(--dim);
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.chip.is-active {
  border-color: var(--ember);
  color: var(--ember);
  background: rgba(255, 112, 67, 0.08);
  box-shadow: inset 0 0 0 1px rgba(255, 112, 67, 0.35);
}

.play-layout { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); }
/* Phones: HUD strip, map, tools, main button, status - everything needed to
   play fits in one screen. From 720px the HUD and help move to a side column. */
.stage { display: flex; flex-direction: column; gap: 10px; min-width: 0; }

/* ---------- canvas ---------- */

.canvas-wrap {
  position: relative;
  width: 100%;
  padding: 8px;
  background: linear-gradient(180deg, var(--surface), rgba(18, 18, 29, 0.5));
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: inset 0 0 32px rgba(255, 112, 67, 0.05);
}

#mapCanvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 3 / 2;
  background: var(--bg);
  border-radius: 4px;
  touch-action: none;
  image-rendering: pixelated;
  cursor: crosshair;
}

.cursor-ring {
  position: absolute;
  left: 0; top: 0;
  pointer-events: none;
  border: 2px solid var(--gold);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.6), 0 0 10px rgba(255, 200, 87, 0.5);
}

/* ---------- overlay (fire out) ---------- */

.overlay {
  position: absolute;
  inset: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  text-align: center;
  padding: 16px;
  border-radius: 6px;
  background: rgba(10, 10, 18, 0.88);
  overflow: auto;
}
/* Centre with auto margins, not justify-content: if the results ever overflow
   (small phones), the top stays reachable by scrolling instead of being cut. */
.overlay > :first-child { margin-top: auto; }
.overlay > :last-child { margin-bottom: auto; }

.overlay-title {
  margin: 0;
  font-family: var(--pixel);
  font-size: clamp(14px, 4vw, 24px);
  letter-spacing: 3px;
  color: var(--ember);
  text-shadow: var(--glow-ember), 2px 2px 0 var(--magenta);
}
.overlay-grade { margin: 0; font-family: var(--mono); font-size: 12px; letter-spacing: 1px; text-transform: uppercase; color: var(--gold); }
.overlay-stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 18px; margin: 0; }
.overlay-stats div { min-width: 0; }
.overlay-stats dt { font-family: var(--pixel); font-size: 7px; color: var(--dim); letter-spacing: 1px; }
.overlay-stats dd { margin: 2px 0 0; font-family: var(--mono); font-size: 1.05rem; font-weight: 700; color: var(--text); }
.overlay-cells { margin: 0; font-family: var(--mono); font-size: 11px; color: var(--dim); }
.overlay-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.final-badge { margin: 0; color: var(--gold); font-family: var(--pixel); font-size: 10px; letter-spacing: 1px; }

/* ---------- main controls + status ---------- */

.main-row { display: flex; flex-wrap: wrap; gap: 8px; }
.main-row #btnMain { flex: 1 1 220px; }

/* Room for two lines is reserved so short and long messages don't make the
   page jump; it sits below the controls, so a longer one never moves them. */
.status {
  min-height: 3em;
  margin: 0;
  color: var(--dim);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.5;
}

/* ---------- HUD ---------- */

/* Compact strip on phones: budget / saved / burning, then clock + wind. */
.hud { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.hud > div {
  min-width: 0;
  padding: 7px 9px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.hud > div > span { display: block; margin-bottom: 4px; font-family: var(--pixel); font-size: 7px; line-height: 1.4; color: var(--dim); letter-spacing: 1px; }
.hud strong { display: block; font-family: var(--mono); font-size: clamp(0.85rem, 3.6vw, 1rem); color: var(--gold); white-space: nowrap; }
.hud .hud-wide { grid-column: span 2; }
.hud .wind-line { display: flex; align-items: center; gap: 6px; white-space: normal; }
.hud-sub { display: inline; margin-left: 6px; font-family: var(--mono); font-size: 11px; color: var(--dim); }
.hud-budget strong { display: inline; }
.wind-arrow {
  display: inline-block;
  width: 1.4em;
  text-align: center;
  font-size: 1.3rem;
  color: var(--cyan);
  transition: transform 0.4s var(--ease-out);
}
.forecast { display: block; margin-top: 2px; font-family: var(--mono); font-size: 11px; color: var(--dim); }

/* ---------- tools ---------- */

/* Tools always sit right under the map, in one row. Below 960px each
   button is a compact stack (swatch + key, name, cost); wider screens also
   show the one-line hint. */
.tools { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.tool-btn {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "swatch cost kbd" "name name name";
  gap: 5px 6px;
  align-items: center;
  min-width: 0;
  min-height: 52px;
  padding: 7px 8px;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.12s, box-shadow 0.12s, transform 0.12s var(--ease-out);
}
.tool-btn:hover { border-color: var(--cyan); transform: translateY(-1px); }
.tool-btn[aria-pressed="true"] { border-color: var(--ember); box-shadow: inset 0 0 0 1px var(--ember), var(--glow-ember); }
.tool-btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.tool-swatch { grid-area: swatch; width: 16px; height: 16px; border-radius: 3px; background: var(--swatch); box-shadow: 0 0 8px var(--swatch); }
.tool-name { grid-area: name; font-family: var(--pixel); font-size: 8px; line-height: 1.5; letter-spacing: 1px; overflow-wrap: anywhere; }
.tool-hint { grid-area: hint; display: none; font-family: var(--mono); font-size: 12px; color: var(--dim); }
.tool-cost { grid-area: cost; font-family: var(--mono); font-size: 11px; color: var(--dim); }
.tool-btn kbd {
  grid-area: kbd;
  justify-self: end;
  min-width: 22px;
  padding: 2px 5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 12px;
  text-align: center;
  color: var(--gold);
  background: var(--bg);
}

.help { margin: 0; color: var(--dim); font-family: var(--mono); font-size: 12px; line-height: 1.6; }

/* ---------- panels: legend + model note ---------- */

.panel {
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.panel-title { margin: 0 0 12px; font-family: var(--pixel); font-size: 10px; letter-spacing: 2px; color: var(--gold); line-height: 1.6; }

.legend { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px 16px; }
.legend li { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--text); }
.swatch { flex: none; width: 16px; height: 16px; border-radius: 3px; background: var(--swatch); border: 1px solid rgba(255, 255, 255, 0.12); }

.model-note p { margin: 0 0 12px; max-width: 72ch; color: var(--text); }
.model-note .why { color: var(--gold); }
.model-note .formula-label, .model-note .formula-key { color: var(--dim); font-size: 14px; }
.formula {
  margin: 0 0 14px;
  padding: 12px 14px;
  overflow-x: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.7;
  color: var(--cyan);
}
.sources-title { margin: 18px 0 8px; font-family: var(--pixel); font-size: 9px; letter-spacing: 2px; color: var(--dim); }
.sources { margin: 0; padding-left: 20px; display: grid; gap: 10px; font-size: 14px; }
.sources a { overflow-wrap: anywhere; }
.src-note { display: block; color: var(--dim); font-size: 13px; }

/* ---------- footer ---------- */

.site-footer {
  border-top: 1px solid var(--line);
  padding: 22px var(--space-gutter);
  text-align: center;
  color: var(--dim);
  font-size: 13px;
}
.site-footer p { margin: 0 0 8px; }
.site-footer p:last-child { margin-bottom: 0; }
.site-footer a { color: var(--cyan); text-decoration: none; }
.site-footer a:hover { text-decoration: underline; text-shadow: var(--glow-cyan); }

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(440px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-left: 3px solid var(--red);
  color: var(--text);
  padding: 12px 14px;
  border-radius: 8px;
  font-size: 13.5px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  animation: toast-in 0.22s var(--ease-out) both;
}
.toast-info { border-left-color: var(--cyan); }
.toast-success { border-left-color: var(--green); }
.toast.out { opacity: 0; transform: translateY(6px); transition: opacity 0.25s, transform 0.25s; }

@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }

/* ---------- responsive ---------- */

@media (min-width: 720px) {
  /* Map column + side column (HUD on top, help under it). The stage spans
     both rows; the flexible second row absorbs its extra height. */
  .play-layout { grid-template-columns: minmax(0, 1fr) 260px; grid-template-rows: auto 1fr; align-items: start; column-gap: 14px; }
  .stage { grid-column: 1; grid-row: 1 / span 2; }
  .hud { grid-column: 2; grid-row: 1; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .help { grid-column: 2; grid-row: 2; }
  .hud > div { padding: 10px 12px; }
  .hud > div > span { margin-bottom: 6px; font-size: 8px; }
  .hud strong { font-size: 1.2rem; }
  .hud-budget strong { display: block; }
  .hud-sub { display: block; margin: 2px 0 0; }
  .hud .hud-wide { grid-column: 1 / -1; }
  .hud .wind-line { gap: 8px; }
  .forecast, .hud-sub { font-size: 12px; }
}

@media (min-width: 960px) {
  .play-layout { grid-template-columns: minmax(0, 1fr) 300px; }
  .tool-btn { grid-template-columns: auto 1fr auto; grid-template-areas: "swatch name kbd" "swatch hint kbd"; gap: 4px 10px; min-height: 56px; padding: 10px 12px; }
  .tool-btn kbd { padding: 4px 6px; min-width: 26px; }
  .tool-swatch { width: 18px; height: 18px; }
  .tool-name { font-size: 9px; }
  .tool-hint { display: block; }
  .tool-cost { display: none; }
}

@media (max-width: 599px) {
  /* Phones: the map is too short to hold the results, so they sit right under
     it (the burnt-out map stays visible above) instead of covering it. */
  .overlay { position: static; margin-top: 8px; background: var(--bg); border: 1px solid var(--line); }
  /* Every pixel of width goes to the map cells. */
  .canvas-wrap { padding: 4px; }
  /* A tighter top bar so HUD, map and tools fit in one phone screen. */
  .topbar { padding: 8px; gap: 8px; }
  .action-row, .chip-row { gap: 6px; }
  .topbar .btn, .topbar .chip { min-height: 40px; padding: 6px 10px; font-size: 11px; }
  .map-code output { font-size: 12px; }
  .game-panel { gap: 12px; }
  /* Touch screens: key hints make way for the cost (keys stay listed in the help). */
  .tool-btn { grid-template-areas: "swatch cost cost" "name name name"; }
  .tool-btn kbd { display: none; }
  .tool-cost { font-size: 10px; }
  .wind-arrow { font-size: 1.05rem; width: 1.2em; }
}

@media (max-width: 420px) {
  .btn-big { font-size: 10px; padding: 12px 14px; }
  .overlay-stats dd { font-size: 0.95rem; }
  .overlay { gap: 5px; padding: 8px; }
  .overlay-stats { gap: 4px 14px; }
}

@media (max-width: 359px) {
  .tool-btn { padding: 7px 6px; }
  .tool-name { font-size: 7px; }
  .hud > div { padding: 6px 7px; }
}

/* ---------- light theme (the main site embeds labs with ?theme=light) ----------
   /theme-light.css swaps the shared tokens; these cover Ember's own accent and
   the few surfaces that were hard-coded dark. */

html[data-theme="light"],
html.theme-light {
  --ember: #c2410c;           /* >= 4.5:1 on white for small text */
  --glow-ember: 0 0 0 transparent;
}
html[data-theme="light"] .canvas-wrap,
html.theme-light .canvas-wrap {
  background: var(--surface-2);
  box-shadow: none;
}
html[data-theme="light"] #mapCanvas,
html.theme-light #mapCanvas { background: #0a0a12; }
html[data-theme="light"] .overlay,
html.theme-light .overlay { background: rgba(255, 255, 255, 0.94); }
html[data-theme="light"] .chip.is-active,
html.theme-light .chip.is-active {
  color: var(--ember);
  background: rgba(194, 65, 12, 0.08);
  box-shadow: inset 0 0 0 1px rgba(194, 65, 12, 0.35);
}
html[data-theme="light"] .btn-primary,
html.theme-light .btn-primary { color: #ffffff; }
html[data-theme="light"] .wordmark,
html.theme-light .wordmark { text-shadow: 2px 2px 0 var(--magenta); }
html[data-theme="light"] .overlay-title,
html.theme-light .overlay-title { text-shadow: none; }
html[data-theme="light"] ::selection,
html.theme-light ::selection { background: rgba(194, 65, 12, 0.2); }

/* ---------- reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .cursor { animation: none; }
}
