:root {
  color-scheme: light;
  --paper: #f5f8fc;
  --grid: #dfe7f2;
  --surface: #ffffff;
  --surface-2: #eef3fa;
  --ink: #1b2a4a;
  --ink-soft: #52627f;
  --line: #d3dceb;
  --primary: #2f6feb;
  --primary-ink: #ffffff;
  --sun: #ffc53d;
  --sun-ink: #3a2a00;
  --good: #23864d;
  --good-bg: #e3f5ea;
  --oops: #c4553a;
  --oops-bg: #fdeee8;
  --shadow: 0 2px 0 rgba(27, 42, 74, 0.12), 0 8px 24px rgba(27, 42, 74, 0.08);
  --radius: 18px;
  --display: "Fredoka", "Trebuchet MS", "Arial Rounded MT Bold", system-ui, sans-serif;
  --body: "Atkinson Hyperlegible", "Verdana", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --paper: #111a2c;
    --grid: #1a2640;
    --surface: #1a2540;
    --surface-2: #22304f;
    --ink: #e8eef9;
    --ink-soft: #a6b4cf;
    --line: #2d3c5e;
    --primary: #6d9cff;
    --primary-ink: #0d1628;
    --sun: #ffcf5c;
    --good: #5fd18f;
    --good-bg: #173a2a;
    --oops: #ff9e85;
    --oops-bg: #3d2320;
    --shadow: 0 2px 0 rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #111a2c;
  --grid: #1a2640;
  --surface: #1a2540;
  --surface-2: #22304f;
  --ink: #e8eef9;
  --ink-soft: #a6b4cf;
  --line: #2d3c5e;
  --primary: #6d9cff;
  --primary-ink: #0d1628;
  --sun: #ffcf5c;
  --good: #5fd18f;
  --good-bg: #173a2a;
  --oops: #ff9e85;
  --oops-bg: #3d2320;
  --shadow: 0 2px 0 rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 28px 28px;
  color: var(--ink);
  font-family: var(--body);
  font-size: 18px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--sun); outline-offset: 2px; }
img { max-width: 100%; }

.wrap {
  max-width: 820px;
  margin: 0 auto;
  padding-inline: 16px;
  padding-block: 12px 32px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: 100vh;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 0;
  padding: 4px;
  border-radius: 12px;
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--sun);
  color: var(--sun-ink);
  font-family: var(--display);
  font-weight: 700;
  font-size: 20px;
  transform: rotate(-6deg);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.15);
}
.brand-text {
  font-family: var(--display);
  font-weight: 700;
  font-size: 26px;
  letter-spacing: 0.01em;
}
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  color: var(--ink-soft);
  background: var(--surface);
  border: 1px solid var(--line);
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.toggle input { width: 18px; height: 18px; accent-color: var(--primary); }

.foot {
  margin-top: auto;
  text-align: center;
  font-size: 14px;
  color: var(--ink-soft);
}

/* Type */
h1, h2, h3 {
  font-family: var(--display);
  font-weight: 600;
  line-height: 1.15;
  text-wrap: balance;
  margin: 0;
}
h1 { font-size: clamp(30px, 6vw, 42px); }
h2 { font-size: clamp(22px, 4.5vw, 28px); }
h3 { font-size: 20px; }
p { margin: 0; }
.lede { color: var(--ink-soft); max-width: 60ch; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 10px 22px;
  border-radius: 14px;
  border: 2px solid transparent;
  font-family: var(--display);
  font-weight: 600;
  font-size: 19px;
  background: var(--primary);
  color: var(--primary-ink);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.2);
  transition: transform 0.08s ease;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2); }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }
.btn.secondary {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}
.btn.sun { background: var(--sun); color: var(--sun-ink); }
.btn-row { display: flex; flex-wrap: wrap; gap: 12px; }

/* Home */
.home { display: flex; flex-direction: column; gap: 22px; }
.home-head { display: flex; flex-direction: column; gap: 8px; }
.length-picker {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  color: var(--ink-soft);
}
.chip {
  border: 2px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.chip[aria-pressed="true"] {
  border-color: var(--primary);
  background: var(--primary);
  color: var(--primary-ink);
}
.events {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
}
.event {
  --tint: var(--primary);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  text-align: left;
  padding: 18px;
  border-radius: var(--radius);
  border: 2px solid color-mix(in srgb, var(--tint) 45%, var(--line));
  background: color-mix(in srgb, var(--tint) 10%, var(--surface));
  box-shadow: var(--shadow);
  transition: transform 0.12s ease;
}
.event:hover { transform: translateY(-3px) rotate(-0.4deg); }
.event-icon {
  font-size: 34px;
  line-height: 1;
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: color-mix(in srgb, var(--tint) 22%, var(--surface));
}
.event-name { font-family: var(--display); font-weight: 600; font-size: 21px; }
.event-blurb { font-size: 15px; color: var(--ink-soft); }
.event-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
  font-size: 13px;
  font-weight: 700;
}
.pill {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}
.pill.best { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, var(--line)); }

/* Quiz */
.quiz { display: flex; flex-direction: column; gap: 16px; }
.quiz-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 16px;
  color: var(--ink-soft);
}
.quiz-event { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; color: var(--ink); }
.progress {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}
.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--line);
}
.dot.now { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 25%, var(--surface)); }
.dot.right { background: var(--good); border-color: var(--good); }
.dot.wrong { background: var(--oops); border-color: var(--oops); }

.card {
  background: var(--surface);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(18px, 4vw, 28px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.q-label {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.speak {
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 14px;
  letter-spacing: 0;
  text-transform: none;
  font-weight: 700;
  color: var(--ink);
  white-space: nowrap;
}
.q-text {
  font-family: var(--display);
  font-weight: 500;
  font-size: clamp(22px, 4.6vw, 28px);
  line-height: 1.3;
  text-wrap: pretty;
}
.q-figure { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.q-image { border-radius: 12px; border: 1px solid var(--line); max-height: 340px; width: auto; object-fit: contain; background: var(--surface-2); }
.q-credit { font-size: 12px; color: var(--ink-soft); text-align: center; }

.choices { display: grid; gap: 10px; }
.choice {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  text-align: left;
  padding: 14px 16px;
  min-height: 60px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: var(--surface-2);
  font-size: 19px;
  transition: border-color 0.1s ease, background 0.1s ease;
}
.choice:hover:not([disabled]) { border-color: var(--primary); }
.choice .key {
  flex: none;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--surface);
  border: 2px solid var(--line);
  font-family: var(--display);
  font-weight: 600;
}
.choice[disabled] { cursor: default; }
.choice.is-right { border-color: var(--good); background: var(--good-bg); }
.choice.is-right .key { background: var(--good); border-color: var(--good); color: var(--surface); }
.choice.is-wrong { border-color: var(--oops); background: var(--oops-bg); }
.choice.is-wrong .key { background: var(--oops); border-color: var(--oops); color: var(--surface); }
.choice.faded { opacity: 0.55; }

.type-row { display: flex; gap: 10px; flex-wrap: wrap; }
.type-row input {
  flex: 1 1 220px;
  min-width: 0;
  min-height: 56px;
  padding: 10px 16px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: var(--surface-2);
  font-size: 20px;
}
.type-row input:focus { border-color: var(--primary); outline: none; }
.type-hint { font-size: 15px; color: var(--ink-soft); }

.feedback {
  border-radius: 14px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  animation: pop 0.25s ease-out;
}
.feedback.good { background: var(--good-bg); border: 2px solid var(--good); }
.feedback.oops { background: var(--oops-bg); border: 2px solid var(--oops); }
.feedback-title { font-family: var(--display); font-weight: 600; font-size: 22px; }
.feedback.good .feedback-title { color: var(--good); }
.feedback.oops .feedback-title { color: var(--oops); }
.feedback .answer { font-weight: 700; }
.feedback .why { color: var(--ink); }

.hint-box {
  font-size: 16px;
  background: color-mix(in srgb, var(--sun) 22%, var(--surface));
  border: 1px dashed color-mix(in srgb, var(--sun) 70%, var(--line));
  border-radius: 12px;
  padding: 10px 14px;
}
.link-btn {
  background: none;
  border: 0;
  padding: 4px 0;
  color: var(--primary);
  font-weight: 700;
  font-size: 16px;
  text-decoration: underline;
  text-underline-offset: 3px;
  align-self: flex-start;
}
.after { display: flex; flex-direction: column; gap: 14px; }
.after:empty { display: none; }
.quit-row { display: flex; justify-content: flex-end; }

/* Results */
.results { display: flex; flex-direction: column; gap: 18px; }
.score-card { align-items: center; text-align: center; }
.stars { font-size: 46px; letter-spacing: 6px; line-height: 1; }
.star-off { filter: grayscale(1); opacity: 0.3; }
.score-big {
  font-family: var(--display);
  font-weight: 700;
  font-size: clamp(48px, 12vw, 72px);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.score-big small { font-size: 0.45em; color: var(--ink-soft); }
.review { display: flex; flex-direction: column; gap: 10px; }
.study-next {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-size: 15px;
  font-weight: 700;
  color: var(--ink-soft);
}
.pill.topic { color: var(--ink); background: color-mix(in srgb, var(--sun) 25%, var(--surface)); }
.review-item {
  background: var(--surface);
  border: 2px solid var(--line);
  border-left: 6px solid var(--oops);
  border-radius: 14px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 17px;
}
.review-image { max-height: 140px; width: auto; align-self: flex-start; border-radius: 8px; border: 1px solid var(--line); }
.review-item .answer { color: var(--good); font-weight: 700; }
.review-item .yours { color: var(--ink-soft); font-size: 15px; }

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 5; }
.confetti i {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 14px;
  border-radius: 2px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }
@keyframes pop { from { transform: scale(0.97); opacity: 0.4; } to { transform: scale(1); opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .confetti { display: none; }
}
@media (max-width: 480px) {
  body { font-size: 17px; }
  .btn { width: 100%; }
  .events { grid-template-columns: 1fr; }
}

/* ---------- Codebusters ---------- */
.cb h1 { font-family: var(--display); }
.cb-h2 { font-family: var(--display); font-size: 22px; margin: 6px 0 -8px; }
.cb-note { font-size: 15px; color: var(--ink-soft); }
.cb-top { display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 28px; }
.cb-back { align-self: center; }
.cb-sample { font-size: 18px; gap: 2px; display: flex; align-items: center; justify-content: center; }
.cb-sample .glyph { width: 22px; height: 22px; }
.cb-sample .glyph.tall { width: 17px; height: 24px; }
.cb-sample .ct { font-family: var(--display); font-weight: 700; font-size: 16px; letter-spacing: 0; }
.cb-sample .ct.small { font-size: 12px; }
.pill.memo { color: #b03a6a; border-color: #e7a7c1; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .pill.memo { color: #ff9cc3; border-color: #7a3552; } }
:root[data-theme="dark"] .pill.memo { color: #ff9cc3; border-color: #7a3552; }

.glyph { width: 30px; height: 30px; display: block; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.glyph.tall { width: 24px; height: 32px; }
.glyph .fill, .chart .fill { fill: currentColor; stroke: none; }
.chart { width: 100%; height: auto; max-width: 560px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.chart text { fill: currentColor; stroke: none; font-family: var(--display); font-weight: 700; font-size: 20px; text-anchor: middle; }
.cb-chart { display: flex; justify-content: center; margin: 12px 0; padding: 10px; background: var(--surface-2); border-radius: 12px; }

.cb-learn { font-size: 17px; }
.cb-learn summary { font-family: var(--display); font-weight: 600; font-size: 20px; cursor: pointer; }
.cb-learn[open] summary { margin-bottom: 10px; }
.cb-learn p, .cb-learn ul { margin: 0 0 10px; }
.cb-learn code { font-size: 16px; background: var(--surface-2); padding: 1px 6px; border-radius: 6px; letter-spacing: 0.05em; }

.cb-puzzle { display: flex; flex-direction: column; gap: 16px; }
.cb-points { font-weight: 700; color: var(--primary); }
.cb-info { font-size: 17px; }
.cb-grid { display: flex; flex-wrap: wrap; gap: 14px 22px; align-items: flex-end; }
.cb-word { display: flex; gap: 4px; align-items: flex-end; }
.cb-word.wide { flex-wrap: wrap; }
.cb-cell { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.cb-keyletter { font-size: 12px; font-weight: 700; color: var(--primary); line-height: 1; }
.cb-code { min-height: 32px; display: flex; align-items: flex-end; justify-content: center; }
.ct { font-family: "DejaVu Sans Mono", ui-monospace, Menlo, Consolas, monospace; font-weight: 700; font-size: 20px; line-height: 1; }
.ct.tap { font-size: 15px; white-space: nowrap; }
.ct.bacon { font-size: 13px; letter-spacing: 1px; white-space: nowrap; }
.cb-punct { font-weight: 700; font-size: 20px; align-self: flex-end; padding-bottom: 6px; }
.cb-in {
  width: 32px; height: 38px; padding: 0; text-align: center; text-transform: uppercase;
  font-family: var(--display); font-weight: 600; font-size: 20px;
  border: 2px solid var(--line); border-radius: 8px; background: var(--surface-2); color: var(--ink);
}
.cb-word.wide .cb-in { width: 58px; }
.cb-in:focus { border-color: var(--primary); outline: none; background: var(--surface); }
.cb-in.given { background: color-mix(in srgb, var(--sun) 30%, var(--surface)); border-color: color-mix(in srgb, var(--sun) 70%, var(--line)); }
.cb-in.revealed { background: color-mix(in srgb, var(--primary) 15%, var(--surface)); color: var(--primary); }
.cb-in.dup { border-color: #b98a00; box-shadow: inset 0 -3px 0 #e0a126; }
.cb-in.wrong { border-color: var(--oops); background: var(--oops-bg); }
.cb-in[readonly] { cursor: default; }
.cb-result:empty { display: none; }
.cb-score { font-family: var(--display); font-weight: 700; font-size: 26px; }
.cb-actions .btn { min-height: 46px; font-size: 17px; padding: 8px 16px; }
.cb-timer { font-weight: 700; font-variant-numeric: tabular-nums; color: var(--ink-soft); }
.cb-timer.over { color: var(--oops); }

.cb-table-wrap { overflow-x: auto; max-width: 100%; }
.cb-table { border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.cb-table th, .cb-table td { border: 1px solid var(--line); padding: 3px 5px; text-align: center; min-width: 22px; }
.cb-table th { background: var(--surface-2); }
.cb-table.vig { font-family: "DejaVu Sans Mono", ui-monospace, monospace; font-size: 12px; }
.cb-table.vig td, .cb-table.vig th { padding: 1px 3px; min-width: 16px; }
.tap-table { font-size: 18px; font-weight: 700; }
.tap-table td, .tap-table th { min-width: 44px; padding: 6px; }
.cb-freq-table td { min-width: 26px; }
.cb-freq-table .cb-in { width: 24px; height: 28px; font-size: 15px; border-radius: 6px; border-width: 1px; }
.cb-bacon-table { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 2px 12px; font-family: "DejaVu Sans Mono", ui-monospace, monospace; font-size: 14px; margin: 8px 0 12px; }
.cb-bacon-table b { display: inline-block; min-width: 30px; font-family: var(--body); }
.cb-sga-phrase { display: flex; flex-wrap: wrap; gap: 10px 18px; margin: 8px 0 12px; }
.cb-sga-word { display: flex; gap: 4px; }
.cb-sga-letter { display: flex; flex-direction: column; align-items: center; font-size: 13px; }
.cb-sga-letter .glyph { width: 20px; height: 27px; }

.cb-sheet { max-width: min(760px, calc(100vw - 24px)); width: 100%; max-height: calc(100vh - 32px); border: 2px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--ink); padding: 18px; box-shadow: var(--shadow); }
.cb-sheet::backdrop { background: rgba(10, 20, 40, 0.5); }
.cb-sheet h3 { font-family: var(--display); font-size: 18px; margin: 16px 0 6px; }
.cb-sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; position: sticky; top: -18px; background: var(--surface); padding: 6px 0; z-index: 1; }
.cb-sheet-head h2 { font-family: var(--display); margin: 0; }
.cb-sheet-head .btn { flex: 0 0 auto; min-height: 40px; font-size: 16px; padding: 6px 14px; }

.cb-drill-face { display: flex; justify-content: center; padding: 10px; }
.cb-drill-face .glyph { width: 90px; height: 90px; stroke-width: 2; }
.cb-drill-face .ct.tap { font-size: 44px; }
.cb-drill-choices { grid-template-columns: repeat(2, 1fr); }
.cb-drill-choices .choice { justify-content: center; font-size: 26px; min-height: 64px; }
.cb-drill-choices.faces .glyph { width: 48px; height: 48px; }
.cb-drill-choices.faces .ct.tap { font-size: 26px; }
.cb-inline { display: inline-flex; vertical-align: middle; }
.cb-inline .glyph { width: 28px; height: 28px; }
.cb-results { width: 100%; border-collapse: collapse; }
.cb-results th, .cb-results td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); }
.cb-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.cb-actions .btn:first-child { grid-column: 1 / -1; }
@media (min-width: 640px) { .cb-actions { display: flex; } .cb-actions .btn:first-child { grid-column: auto; } }
