/* kidenv design system — the polished, kid-friendly baseline every app starts from.
   Build to these tokens and components: it's how a generated app reaches "app-store" polish.
   See DESIGN.md for the rules and CHECKLIST.md for the gate. Vanilla CSS, zero deps. */

:root {
  color-scheme: dark;

  /* --- Palette: bright and friendly, never harsh. Tuned for AA contrast on --surface. --- */
  --brand:        #5b4bff;   /* primary action / identity */
  --brand-ink:    #ffffff;   /* text on --brand */
  --accent:       #ff4ecd;   /* playful highlight — hot pink */
  --sun:          #ffe03d;   /* warm secondary (electric yellow) */

  /* Feedback colors — encouraging, not shaming. "Try again" is a calm slate, never red. */
  --go:           #1faa59;   /* start / correct / success (green) */
  --go-ink:       #ffffff;
  --stop:         #ff6b5e;   /* stop / pause (warm coral, not alarm-red) */
  --stop-ink:     #fff;
  --calm:         #7a8fa8;   /* gentle "try again" — neutral, low-anxiety */

  /* Space theme surfaces (dark-always: no light-mode flip for this game) */
  --surface:      #080c1e;   /* deep space background */
  --card:         #111830;   /* slightly lighter card surface */
  --card2:        #1a2445;   /* secondary card / panel */
  --ink:          #f0f4ff;   /* primary text — bright on dark */
  --ink-soft:     #8fa3cc;   /* secondary text */
  --line:         #2a3a60;   /* hairlines / borders */
  --neon-green:   #39d353;   /* alien color */
  --neon-teal:    #00e5cc;   /* teal accent */

  /* Shape, space, type. Generous and rounded reads as friendly. */
  --radius:       18px;
  --radius-lg:    28px;
  --tap-min:      64px;                       /* min touch target for kid hands (> WCAG 44px) */
  --tap-primary:  clamp(64px, 10vh, 96px);    /* hero buttons */
  --gap:          clamp(0.4rem, 1.5vw, 0.8rem);
  --pad:          clamp(0.5rem, 2vw, 1rem);
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --shadow:    0 6px 24px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 14px 48px rgba(0, 0, 0, 0.65);
  --glow-green: 0 0 16px rgba(57, 211, 83, 0.6);
  --glow-yellow: 0 0 20px rgba(255, 224, 61, 0.7);
  --glow-pink: 0 0 18px rgba(255, 78, 205, 0.6);
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }

body {
  margin: 0;
  height: 100svh;
  font: clamp(13px, 1.8vw, 16px)/1.5 var(--font);
  color: var(--ink);
  background: var(--surface);
  display: flex;
  align-items: stretch;
  padding:
    max(0px, env(safe-area-inset-top))
    max(0px, env(safe-area-inset-right))
    max(0px, env(safe-area-inset-bottom))
    max(0px, env(safe-area-inset-left));
}

/* --- Layout helpers --- */
.app {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.stack { display: grid; gap: var(--gap); }
.row { display: flex; flex-wrap: wrap; gap: var(--gap); justify-content: center; }

.card {
  background: var(--card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-lg);
  padding: var(--pad);
  width: 100%;
}

/* --- Typography --- */
h1 { font-size: clamp(1.4rem, 5vw, 2.2rem); line-height: 1.1; margin: 0; }
h2 { font-size: clamp(1.1rem, 3.5vw, 1.6rem); margin: 0 0 0.3em; }
p  { color: var(--ink-soft); margin: 0.2em 0; }

/* SR-only utility */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* --- Buttons --- */
.btn {
  -webkit-tap-highlight-color: transparent;
  appearance: none; border: 0; cursor: pointer;
  font: inherit; font-weight: 800;
  color: var(--brand-ink); background: var(--brand);
  min-height: var(--tap-min); min-width: var(--tap-min);
  padding: 0 clamp(0.75rem, 3vw, 1.4rem);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4em;
  transition: transform 0.08s ease, filter 0.15s ease;
}
.btn:hover  { filter: brightness(1.08); }
.btn:active { transform: translateY(2px) scale(0.97); }
.btn:focus-visible { outline: 4px solid color-mix(in srgb, var(--brand) 55%, white); outline-offset: 3px; }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; transform: none; filter: none; }

.btn-xl {
  min-height: var(--tap-primary);
  font-size: clamp(1rem, 3vw, 1.3rem);
  border-radius: var(--radius-lg);
  padding: 0 clamp(1.2rem, 4vw, 2rem);
}
.btn-go   { background: var(--go);  color: var(--go-ink); }
.btn-stop { background: var(--stop); color: #3d0000; }
.btn-cool { background: var(--sun);  color: #1a1000; }
.btn-ghost {
  background: color-mix(in srgb, var(--brand) 15%, var(--card));
  color: var(--ink-soft); box-shadow: none; border: 1px solid var(--line);
}
.btn-icon {
  appearance: none; border: 0; cursor: pointer; background: transparent;
  color: var(--ink-soft); font-size: 1.4rem; line-height: 1;
  min-width: var(--tap-min); min-height: var(--tap-min);
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius); transition: color 0.15s;
}
.btn-icon:hover { color: var(--ink); }

/* Big readout */
.display {
  font-variant-numeric: tabular-nums; font-weight: 800;
  font-size: clamp(1.4rem, 5vw, 2.2rem); line-height: 1; letter-spacing: 0.02em;
  color: var(--sun); text-shadow: var(--glow-yellow);
}

/* ===================================================================
   GAME CARD — the single-screen layout
   =================================================================== */
.game-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  width: min(38rem, 100%);
  height: 100svh;
  max-height: 100svh;
  overflow: hidden;
  border-radius: 0;
  padding: var(--pad);
  background:
    radial-gradient(120% 80% at 50% 110%, color-mix(in srgb, var(--brand) 20%, transparent), transparent 70%),
    var(--card);
}

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  flex-shrink: 0;
}
.game-title-bar {
  font-size: clamp(1rem, 3.5vw, 1.5rem); line-height: 1; margin: 0;
  color: var(--sun); text-shadow: var(--glow-yellow); display: flex; align-items: center; gap: 0.4em;
}
.title-alien-icon { font-size: 1.1em; }

/* DOM HUD */
.hud {
  display: flex; align-items: center; gap: var(--gap);
  background: color-mix(in srgb, var(--card2) 90%, transparent);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 0.3rem var(--pad);
  flex-shrink: 0;
}
.hud-stat {
  display: flex; flex-direction: column; align-items: center; gap: 0.05rem;
}
.hud-problem-area { flex: 1; }
.hud-label {
  font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft);
}
.hud-value {
  font-size: clamp(1.2rem, 4vw, 1.8rem); line-height: 1; color: var(--sun); text-shadow: var(--glow-yellow);
}
.problem-display {
  background: color-mix(in srgb, var(--neon-teal) 12%, var(--card));
  border: 2px solid color-mix(in srgb, var(--neon-teal) 50%, transparent);
  border-radius: var(--radius); padding: 0.2rem 0.75rem;
  min-width: 8rem; text-align: center;
  box-shadow: 0 0 12px rgba(0,229,204,0.3);
}
#problem-text {
  font-size: clamp(1.1rem, 4vw, 1.6rem); font-weight: 900; color: #fff; letter-spacing: 0.04em;
}

/* Streak bar */
.streak-bar {
  display: flex; align-items: center; gap: 0.4rem;
  padding: 0.2rem var(--pad);
  background: color-mix(in srgb, var(--card) 80%, transparent);
  border-radius: var(--radius); border: 1px solid var(--line);
  flex-shrink: 0;
}
.streak-label {
  font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft);
  min-width: 2.8rem;
}
.streak-pips { display: flex; gap: 0.25rem; }
.pip {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--line); border: 2px solid var(--ink-soft);
  transition: background 0.2s, box-shadow 0.2s;
}
.pip.pip-active {
  background: var(--accent); border-color: var(--accent); box-shadow: var(--glow-pink);
}
.streak-count { font-size: 0.85rem; font-weight: 800; color: var(--accent); min-width: 1.2rem; }
.streak-mult  { font-size: 0.8rem; font-weight: 800; color: var(--sun); text-shadow: var(--glow-yellow); }

/* Stage: Phaser canvas + overlays */
.stage-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--line);
}
#game {
  position: absolute; inset: 0;
}
#game canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* ===================================================================
   OVERLAYS — float above the Phaser canvas
   =================================================================== */
.overlay {
  position: absolute; inset: 0; z-index: 5;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: var(--gap);
  padding: var(--pad);
  text-align: center;
  /* Semi-transparent so the starfield + player ship show through behind the menu —
     fills what would otherwise read as dead space (arcade ambiance, not a void). */
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  backdrop-filter: blur(3px);
  overflow-y: auto;
  overflow-x: hidden;
}
.overlay[hidden] { display: none; }

.overlay h2 {
  font-size: clamp(1.2rem, 5vw, 2rem); color: var(--sun); text-shadow: var(--glow-yellow); margin: 0;
}
.overlay p { color: var(--ink-soft); font-weight: 600; margin: 0; font-size: clamp(0.85rem, 2.5vw, 1rem); }
.overlay-alien {
  font-size: clamp(2rem, 8vw, 3.5rem); margin: 0; line-height: 1;
  animation: alien-bob 2s ease-in-out infinite;
}

/* Setup sections (operation / difficulty pickers inside start overlay) */
.setup-section {
  display: flex; flex-direction: column; align-items: center; gap: 0.35rem; width: 100%;
}
.setup-lbl {
  font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft);
}
.op-row, .diff-row { gap: 0.4rem; justify-content: center; }

.btn-op, .btn-diff {
  min-height: var(--tap-min); flex: 1 1 auto;
  font-size: clamp(0.8rem, 2.2vw, 1rem);
  background: color-mix(in srgb, var(--brand) 18%, var(--card));
  color: var(--ink-soft); border: 2px solid transparent;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.btn-op:hover, .btn-diff:hover {
  background: color-mix(in srgb, var(--brand) 32%, var(--card)); color: var(--ink);
}
.btn-op.btn-selected, .btn-diff.btn-selected {
  background: var(--brand); color: #fff;
  border-color: color-mix(in srgb, var(--neon-teal) 60%, var(--brand));
  box-shadow: 0 0 12px rgba(91,75,255,0.5);
}

.best-score-txt {
  font-size: clamp(0.8rem, 2vw, 0.9rem); color: var(--ink-soft); min-height: 1.2em; margin: 0;
}

/* Wave-complete / game-over overlays */
.wave-stars {
  font-size: clamp(1.6rem, 6vw, 2.5rem); line-height: 1;
  animation: star-bounce 0.6s ease;
}
.gameover-icon {
  font-size: clamp(2.5rem, 9vw, 4rem); margin: 0; line-height: 1;
  animation: alien-celebrate 1s ease infinite;
}
.score-row {
  display: flex; flex-direction: column; align-items: center; gap: 0.1rem;
}
.score-lbl {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft);
}
.score-val {
  font-size: clamp(2rem, 8vw, 3rem); color: var(--sun); text-shadow: var(--glow-yellow);
}
.new-best {
  background: color-mix(in srgb, var(--sun) 18%, var(--card));
  border: 2px solid var(--sun); border-radius: var(--radius);
  padding: 0.3rem 0.8rem; font-size: clamp(0.9rem, 2.5vw, 1.1rem);
  font-weight: 900; color: var(--sun); text-shadow: var(--glow-yellow);
  animation: pop 0.5s ease;
}
.new-best[hidden] { display: none; }

/* Alien answer badges — DOM buttons overlaid above Phaser canvas */
.alien-badge {
  position: absolute;
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  padding: 0.15rem 0.7rem;
  background: color-mix(in srgb, var(--neon-green) 18%, var(--card));
  border: 2px solid var(--neon-green);
  border-radius: 12px;
  font-size: clamp(0.95rem, 3vw, 1.2rem);
  font-weight: 900;
  color: #fff;
  text-align: center;
  box-shadow: var(--glow-green);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  display: flex; align-items: center; justify-content: center;
  transform: translateX(-50%);
  transition: background 0.12s, border-color 0.12s;
  z-index: 8;
  /* Ensure badges are only visible when game is active */
  pointer-events: auto;
}
.alien-badge:hover { filter: brightness(1.15); }
.alien-badge.badge-wrong {
  background: color-mix(in srgb, var(--stop) 30%, var(--card));
  border-color: var(--stop);
  animation: badge-wobble 0.4s ease;
}

/* Keyboard input bar (shown during active play) */
.type-row {
  display: flex; gap: 0.5rem; align-items: center;
  padding: 0.3rem 0;
  flex-shrink: 0;
}
.type-row[hidden] { display: none; }
.type-input {
  flex: 1; min-height: var(--tap-min); padding: 0 0.75rem;
  border: 2px solid var(--line); border-radius: var(--radius);
  background: var(--card2); color: var(--ink);
  font: inherit; font-size: clamp(0.9rem, 2.5vw, 1.1rem); font-weight: 700;
  text-align: center; outline: none;
  transition: border-color 0.15s;
  -moz-appearance: textfield;
}
.type-input::-webkit-inner-spin-button,
.type-input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.type-input:focus { border-color: var(--neon-teal); box-shadow: 0 0 10px rgba(0,229,204,0.35); }
.type-submit { flex-shrink: 0; font-size: clamp(0.85rem, 2.5vw, 1rem); }

/* ===================================================================
   Settings sheet (dialog)
   =================================================================== */
.sheet {
  position: fixed; inset: auto 0 0; margin: 0;
  width: 100%; max-width: 38rem; left: 50%; transform: translateX(-50%);
  border: 0; border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: var(--card2); color: var(--ink);
  box-shadow: 0 -8px 40px rgba(0,0,0,0.6);
  max-height: 85svh; overflow-y: auto; padding: 0;
}
.sheet::backdrop { background: rgba(0,0,0,0.55); backdrop-filter: blur(2px); }
.sheet-body { display: flex; flex-direction: column; gap: 1rem; padding: 1.5rem var(--pad) 2rem; }

.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field-label {
  font-size: 0.8rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.07em; color: var(--ink-soft);
}
.field-hint { font-size: 0.75rem; color: color-mix(in srgb, var(--ink-soft) 70%, transparent); }

/* Segmented control */
.segmented {
  display: flex; gap: 0.3rem; background: var(--card); border-radius: var(--radius);
  padding: 0.3rem;
}
.seg {
  flex: 1; min-height: 52px; border: 0; border-radius: calc(var(--radius) - 4px);
  background: transparent; color: var(--ink-soft); font: inherit; font-size: 0.9rem;
  font-weight: 700; cursor: pointer; transition: background 0.15s, color 0.15s;
}
.seg[aria-pressed="true"] { background: var(--brand); color: #fff; }
.seg:hover:not([aria-pressed="true"]) { background: color-mix(in srgb, var(--brand) 20%, transparent); color: var(--ink); }

/* Range (slider) */
.range {
  width: 100%; accent-color: var(--brand); cursor: pointer;
  height: 6px; border-radius: 3px;
}

/* Switch toggle */
.switch {
  appearance: none; border: 0; cursor: pointer; position: relative;
  width: 52px; height: 28px; border-radius: 14px;
  background: var(--line); transition: background 0.2s;
}
.switch::before {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; border-radius: 50%; background: #fff;
  transition: transform 0.2s;
}
.switch[aria-checked="true"] { background: var(--go); }
.switch[aria-checked="true"]::before { transform: translateX(24px); }

/* ===================================================================
   ANIMATIONS
   =================================================================== */
@keyframes alien-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
@keyframes alien-celebrate {
  0%, 100% { transform: rotate(-8deg) scale(1); }
  50% { transform: rotate(8deg) scale(1.1); }
}
@keyframes badge-wobble {
  0%, 100% { transform: translateX(-50%); }
  25% { transform: translateX(calc(-50% - 6px)); }
  75% { transform: translateX(calc(-50% + 6px)); }
}
@keyframes star-bounce {
  0% { transform: scale(0.5); opacity: 0; }
  60% { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

/* --- Motion: opt every animation out for kids/parents who need calm. --- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ===================================================================
   RESPONSIVE — one screen, no overflow on any device
   =================================================================== */

/* Very small phones (320px wide) */
@media (max-width: 360px) {
  :root { --pad: 0.4rem; --gap: 0.35rem; }
  .btn-op, .btn-diff { font-size: 0.75rem; padding: 0 0.35rem; }
  .op-row { display: grid; grid-template-columns: 1fr 1fr; }
  .problem-display { min-width: 6rem; }
}

/* Landscape phone (height ≤ 420px) — squash vertical spacing aggressively */
@media (max-height: 420px) and (orientation: landscape) {
  :root { --pad: 0.3rem; --gap: 0.2rem; --tap-min: 48px; --tap-primary: 48px; }
  .game-card { border-radius: 0; }
  .topbar { display: none; }
  .hud { padding: 0.15rem var(--pad); }
  .streak-bar { display: none; }
  .overlay { gap: 0.2rem; padding: 0.3rem 0.5rem; justify-content: flex-start; padding-top: 0.4rem; }
  .overlay-alien { display: none; }
  .overlay h2 { font-size: 1rem; }
  .overlay > p { display: none; }
  .setup-section { gap: 0.15rem; }
  .setup-lbl { font-size: 0.6rem; }
  .btn-op, .btn-diff { min-height: 44px; font-size: 0.8rem; padding: 0 0.4rem; }
  .btn-xl { min-height: 48px; font-size: 0.95rem; padding: 0 1rem; }
  .best-score-txt { display: none; }
  .type-row { padding: 0.15rem 0; }
}

/* Tablets and up: give the card a max-width and show it centered */
@media (min-width: 640px) {
  .game-card { border-radius: var(--radius-lg); height: min(92svh, 56rem); }
  .app { padding: 0.5rem; }
}
