:root {
  --bg: #eef2ff;
  --surface: #ffffff;
  --text: #1e1b4b;
  --muted: #6b7280;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --key: #ffffff;
  --key-shadow: #c7d2fe;
  --good: #16a34a;
  --good-bg: #dcfce7;
  --bad: #dc2626;
  --bad-bg: #fee2e2;
  --border: #e0e7ff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111027;
    --surface: #1c1a3a;
    --text: #eef2ff;
    --muted: #a5b4fc;
    --primary: #818cf8;
    --primary-ink: #111027;
    --key: #26244d;
    --key-shadow: #0b0a1d;
    --good: #4ade80;
    --good-bg: #143322;
    --bad: #f87171;
    --bad-bg: #3b1616;
    --border: #2e2b5c;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100%;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
}

.screen { display: none; flex-direction: column; align-items: center; min-height: calc(100dvh - 32px); }
.screen.active { display: flex; }

h1 { margin: 8px 0 4px; font-size: 2rem; text-align: center; }
.sub { margin: 0 0 24px; color: var(--muted); text-align: center; }
.label { font-size: 1rem; font-weight: 600; margin: 16px 0 12px; align-self: stretch; text-align: center; }

.logo {
  margin-top: 32px;
  width: 96px; height: 96px;
  border-radius: 24px;
  background: var(--primary);
  color: var(--primary-ink);
  display: grid; place-items: center;
  font-size: 64px; font-weight: 800;
  box-shadow: 0 8px 0 var(--key-shadow);
}

/* Setup */
.choices { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; width: 100%; }
.choice {
  font: inherit; font-size: 1.75rem; font-weight: 700;
  padding: 20px 0;
  border: 2px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 4px 0 var(--key-shadow);
  cursor: pointer;
}
.choice:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--key-shadow); }

.custom { width: 100%; margin-top: 24px; text-align: center; }
.custom label { color: var(--muted); font-size: 0.9rem; }
.custom-row { display: flex; gap: 8px; margin-top: 8px; }
.custom input {
  flex: 1; min-width: 0;
  font: inherit; font-size: 1.25rem;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
.custom input:focus { outline: none; border-color: var(--primary); }
.error { color: var(--bad); min-height: 1.2em; font-size: 0.9rem; margin: 8px 0 0; }

.btn {
  font: inherit; font-weight: 700; font-size: 1.1rem;
  padding: 14px 20px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }

/* Game */
.hud { display: flex; justify-content: space-between; width: 100%; gap: 8px; }
.hud-item { display: flex; flex-direction: column; align-items: center; flex: 1; font-weight: 700; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.hud-label { font-size: 0.75rem; font-weight: 500; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

.bar { width: 100%; height: 8px; border-radius: 4px; background: var(--border); margin: 12px 0 16px; overflow: hidden; }
#bar-fill { height: 100%; width: 0; background: var(--primary); transition: width 0.3s ease; }

.card {
  width: 100%;
  background: var(--surface);
  border: 3px solid var(--border);
  border-radius: 24px;
  padding: 24px 16px 16px;
  text-align: center;
  transition: background 0.15s, border-color 0.15s;
}
.card.correct { background: var(--good-bg); border-color: var(--good); }
.card.wrong { background: var(--bad-bg); border-color: var(--bad); animation: shake 0.35s; }

.problem { font-size: clamp(2.75rem, 14vw, 4rem); font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.answer {
  margin: 12px auto 0;
  min-height: 1.3em;
  font-size: clamp(2.25rem, 11vw, 3rem);
  font-weight: 700;
  color: var(--primary);
  border-bottom: 3px dashed var(--border);
  width: 60%;
  font-variant-numeric: tabular-nums;
}
.placeholder { color: var(--border); }
.feedback { min-height: 1.6em; margin-top: 10px; font-size: 1.15rem; font-weight: 700; }
.card.correct .feedback { color: var(--good); }
.card.wrong .feedback { color: var(--bad); }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 100%; margin-top: 16px; }
.key {
  font: inherit; font-size: 1.75rem; font-weight: 700;
  padding: 14px 0;
  border: none;
  border-radius: 14px;
  background: var(--key);
  color: var(--text);
  box-shadow: 0 4px 0 var(--key-shadow);
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
}
.key:active, .key.pressed { transform: translateY(3px); box-shadow: 0 1px 0 var(--key-shadow); }
.key.alt { color: var(--muted); }
.key.go { background: var(--primary); color: var(--primary-ink); }
.keypad.locked .key { opacity: 0.6; pointer-events: none; }

.link { margin-top: 16px; background: none; border: none; color: var(--muted); font: inherit; text-decoration: underline; cursor: pointer; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}

/* Summary */
.big-emoji { font-size: 72px; margin-top: 16px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 100%; }
.stat {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 12px 4px;
  display: flex; flex-direction: column; align-items: center;
}
.stat-value { font-size: 1.35rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }

#missed-wrap { width: 100%; }
.missed { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.missed li {
  background: var(--bad-bg);
  color: var(--text);
  border: 2px solid var(--bad);
  border-radius: 10px;
  padding: 6px 10px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.all { width: 100%; margin-top: 20px; background: var(--surface); border: 2px solid var(--border); border-radius: 14px; padding: 10px 14px; }
.all summary { cursor: pointer; font-weight: 600; }
.all table { width: 100%; border-collapse: collapse; margin-top: 8px; font-variant-numeric: tabular-nums; }
.all th, .all td { padding: 6px 4px; text-align: left; border-bottom: 1px solid var(--border); }
.all th { color: var(--muted); font-weight: 500; font-size: 0.85rem; }
.all td.ok { color: var(--good); font-weight: 700; }
.all td.no { color: var(--bad); font-weight: 700; }

.actions { display: flex; flex-direction: column; gap: 10px; width: 100%; margin-top: 24px; }
