:root {
  --bg: #f6f4ff;
  --card: #ffffff;
  --text: #1f1d2e;
  --muted: #6b6880;
  --line: #e4e0f5;
  --primary: #5b4bdb;
  --primary-press: #4a3bc4;
  --on-primary: #ffffff;
  --soft: #ece8ff;
  --right: #15803d;
  --right-bg: #dcfce7;
  --wrong: #b91c1c;
  --wrong-bg: #fee2e2;
  --hmm-bg: #fff7e0;
  --hmm-line: #f2c94c;
  --target: #ffe066;
  --star: #f5b301;

  /* One color family per part of speech; subtypes share their family's color. */
  --noun-bg: #dbeafe;    --noun-fg: #1e40af;
  --verb-bg: #dcfce7;    --verb-fg: #166534;
  --adj-bg: #fce7f3;     --adj-fg: #9d174d;
  --adverb-bg: #fef3c7;  --adverb-fg: #92400e;
  --pronoun-bg: #ede9fe; --pronoun-fg: #5b21b6;
  --prep-bg: #ffedd5;    --prep-fg: #9a3412;
  --conj-bg: #ccfbf1;    --conj-fg: #115e59;
  --interj-bg: #fee2e2;  --interj-fg: #991b1b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17152b;
    --card: #221f3a;
    --text: #eeecf8;
    --muted: #a9a5c4;
    --line: #37335a;
    --primary: #8b7dff;
    --primary-press: #7a6af5;
    --on-primary: #14122a;
    --soft: #2d2950;
    --right: #4ade80;
    --right-bg: #173a26;
    --wrong: #f87171;
    --wrong-bg: #45191c;
    --hmm-bg: #3a3118;
    --hmm-line: #a8872b;
    --target: #8a6d00;

    --noun-bg: #1e3a6b;    --noun-fg: #bfdbfe;
    --verb-bg: #14532d;    --verb-fg: #bbf7d0;
    --adj-bg: #6b1d45;     --adj-fg: #fbcfe8;
    --adverb-bg: #6b4309;  --adverb-fg: #fde68a;
    --pronoun-bg: #43307a; --pronoun-fg: #ddd6fe;
    --prep-bg: #7a3510;    --prep-fg: #fed7aa;
    --conj-bg: #134e4a;    --conj-fg: #99f6e4;
    --interj-bg: #6b1c1c;  --interj-fg: #fecaca;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.6 Nunito, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

.wrap {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px max(16px, env(safe-area-inset-right)) calc(32px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

h1, h2, .badge, .big-star { font-family: Fredoka, Nunito, system-ui, sans-serif; font-weight: 600; }
h1 { line-height: 1.2; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .9rem; }

/* ── Buttons ─────────────────────────────── */

button, .button {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 14px;
  min-height: 50px;
  padding: 10px 18px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:focus-visible, .button:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}
.primary { background: var(--primary); color: var(--on-primary); box-shadow: 0 3px 0 var(--primary-press); }
.primary:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--primary-press); }
.secondary { background: var(--soft); color: var(--text); }
button.small, .button.small { min-height: 40px; padding: 6px 14px; font-size: .95rem; }
.link { background: none; color: var(--primary); min-height: 44px; padding: 4px 0; }
.link.muted { color: var(--muted); font-weight: 600; }

.row { display: flex; gap: 10px; margin-top: 14px; }
.row.wraps { flex-wrap: wrap; }
.grow { flex: 1; }
.stack { display: grid; gap: 10px; margin: 16px 0; }

/* ── Home ─────────────────────────────────── */

.hero { text-align: center; padding: 12px 0 20px; }
.hero h1 { font-size: clamp(2rem, 9vw, 2.8rem); margin: 0 0 6px; color: var(--primary); }
.hero h1 span { color: var(--text); }
.hero p { color: var(--muted); }

.level {
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 20px;
  padding: 16px;
  margin-bottom: 14px;
}
.level.current { border-color: var(--primary); }
.level.locked { opacity: .6; }
.level-head { display: flex; gap: 12px; align-items: flex-start; }
.level-head h2 { font-size: 1.2rem; margin: 2px 0 6px; }
.badge {
  flex: none;
  width: 42px; height: 42px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--soft);
  color: var(--primary);
  font-size: 1.2rem;
}
.level.done .badge { background: var(--right-bg); color: var(--right); }
.level.current .badge { background: var(--primary); color: var(--on-primary); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: .85rem;
  font-weight: 800;
  background: var(--fam-bg);
  color: var(--fam-fg);
}
.lock-msg { margin: 12px 0 0; color: var(--muted); font-size: .95rem; }

.stories { display: grid; gap: 8px; margin-top: 12px; }
.story-btn {
  justify-content: flex-start;
  background: var(--soft);
  color: var(--text);
  width: 100%;
}
.story-name { flex: 1; text-align: left; }
.star { font-size: 1.4rem; color: var(--muted); line-height: 1; }
.star.on { color: var(--star); }
.best { color: var(--muted); font-size: .9rem; }

footer { text-align: center; margin-top: 24px; }

/* ── Parts-of-speech colors ──────────────── */

.fam-noun    { --fam-bg: var(--noun-bg);    --fam-fg: var(--noun-fg); }
.fam-verb    { --fam-bg: var(--verb-bg);    --fam-fg: var(--verb-fg); }
.fam-adj     { --fam-bg: var(--adj-bg);     --fam-fg: var(--adj-fg); }
.fam-adverb  { --fam-bg: var(--adverb-bg);  --fam-fg: var(--adverb-fg); }
.fam-pronoun { --fam-bg: var(--pronoun-bg); --fam-fg: var(--pronoun-fg); }
.fam-prep    { --fam-bg: var(--prep-bg);    --fam-fg: var(--prep-fg); }
.fam-conj    { --fam-bg: var(--conj-bg);    --fam-fg: var(--conj-fg); }
.fam-interj  { --fam-bg: var(--interj-bg);  --fam-fg: var(--interj-fg); }

/* ── Screens ─────────────────────────────── */

.top { display: flex; align-items: center; gap: 12px; min-height: 44px; margin-bottom: 8px; }
.back { color: var(--primary); font-weight: 700; text-decoration: none; padding: 10px 0; white-space: nowrap; }
.crumb { color: var(--muted); font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.screen-title { font-size: 1.7rem; margin: 4px 0 12px; }
.lead { font-size: 1.1rem; }
.section { font-size: 1.3rem; margin: 28px 0 10px; }
.tip { color: var(--muted); font-size: .95rem; }

.card {
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 20px;
  padding: 18px;
}

.type-card {
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 16px;
  padding: 14px 16px;
  margin-bottom: 10px;
}
.type-card .chip { margin-bottom: 8px; font-size: .95rem; }
.type-card p { margin: 0 0 4px; }
.type-card .ex { color: var(--muted); }
.frame { font-style: italic; }

/* Fill in a word */
.progress { height: 10px; background: var(--soft); border-radius: 999px; overflow: hidden; }
.progress div { height: 100%; background: var(--primary); border-radius: 999px; transition: width .3s; }
.count { color: var(--muted); font-size: .9rem; margin: 6px 0 10px; }

.prompt { display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 10px; margin-bottom: 12px; }
.prompt .chip { font-size: 1.15rem; padding: 4px 14px; }
.prompt .hint { color: var(--muted); }

input[type="text"] {
  width: 100%;
  font: inherit;
  font-size: 1.35rem;       /* 16px+ stops iOS from zooming in on focus */
  font-weight: 700;
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
}
input[type="text"]:focus { border-color: var(--primary); outline: none; }

.note { margin-top: 10px; color: var(--wrong); font-weight: 700; }
.hmm {
  margin-top: 12px;
  background: var(--hmm-bg);
  border: 2px solid var(--hmm-line);
  border-radius: 14px;
  padding: 12px 14px;
}
.hmm .row { margin: 10px 0 6px; }
.hmm p:last-child { margin: 0; }

.whatis { margin-top: 14px; }
.whatis summary { cursor: pointer; color: var(--primary); font-weight: 700; padding: 10px 0; }

/* Story */
.story { font-size: 1.15rem; line-height: 2; }
.story p:last-child, .sentence p:last-child { margin: 0; }
mark.w {
  background: var(--fam-bg);
  color: var(--fam-fg);
  padding: 1px 6px;
  border-radius: 6px;
  font-weight: 800;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}
mark.w small {
  font-size: .6em;
  text-transform: uppercase;
  letter-spacing: .03em;
  margin-left: 4px;
  opacity: .85;
}
mark.w.key { font-weight: 600; }
.mine { text-decoration: underline dotted var(--muted); text-underline-offset: 4px; }

/* Quiz */
.dots { display: flex; gap: 8px; justify-content: center; margin: 4px 0 8px; }
.dot { width: 14px; height: 14px; border-radius: 50%; background: var(--line); }
.dot.now { background: var(--primary); }
.dot.right { background: var(--right); }
.dot.wrong { background: var(--wrong); }

.sentence { font-size: 1.25rem; line-height: 1.9; }
mark.target {
  background: var(--target);
  color: inherit;
  padding: 1px 6px;
  border-radius: 6px;
  font-weight: 800;
  box-shadow: 0 0 0 2px var(--star);
}
.question { font-weight: 700; margin: 16px 0 10px; font-size: 1.1rem; }

.choices { display: grid; gap: 10px; }
@media (min-width: 520px) { .choices { grid-template-columns: 1fr 1fr; } }
.choice {
  background: var(--card);
  color: var(--text);
  border: 2px solid var(--line);
  min-height: 56px;
}
.choice:not(:disabled):active { background: var(--soft); }
.choice.right { background: var(--right-bg); border-color: var(--right); color: var(--right); }
.choice.wrong { background: var(--wrong-bg); border-color: var(--wrong); color: var(--wrong); }
.choice.faded { opacity: .5; }
.choice:disabled { cursor: default; }

.explain {
  margin-top: 14px;
  border-radius: 16px;
  padding: 14px 16px;
  border: 2px solid;
}
.explain.right { background: var(--right-bg); border-color: var(--right); }
.explain.wrong { background: var(--wrong-bg); border-color: var(--wrong); }
.explain .primary { width: 100%; margin-top: 6px; }

/* Results */
.result { text-align: center; }
.result h1 { margin: 0 0 6px; font-size: 2rem; }
.big-star { font-size: 4.5rem; line-height: 1; color: var(--muted); }
.result.pass .big-star { color: var(--star); animation: pop .5s ease-out; }
.unlock { background: var(--soft); border-radius: 12px; padding: 10px; font-size: 1.05rem; }
@keyframes pop { 0% { transform: scale(.3); } 70% { transform: scale(1.15); } 100% { transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { .result.pass .big-star { animation: none; } .progress div { transition: none; } }

/* Grammar Helper */
dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 48px);
  border: 0;
  border-radius: 20px;
  padding: 0;
  background: var(--bg);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.dialog-head {
  position: sticky; top: 0;
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px;
  background: var(--bg);
  border-bottom: 2px solid var(--line);
}
.dialog-head h2 { margin: 0; font-size: 1.3rem; }
.helper-body { padding: 14px 16px 16px; }
