:root {
  --bg: #fff8ee;
  --card: #ffffff;
  --ink: #2b2433;
  --muted: #6d6478;
  --line: #eadfce;
  --accent: #d6216f;
  --accent-soft: #fde6f0;
  --good: #2e8b3d;
  --good-soft: #e3f5e5;
  --warn: #b86a00;
  --warn-soft: #fff1d9;
  --c-thousands: #7b3fa0;
  --c-hundreds: #e0403a;
  --c-tens: #e7a300;
  --c-ones: #1f7fd6;
  --wood: #9a5a2c;
  --rod: #a9a9b3;
  --grid: rgba(0, 0, 0, .18);
  --radius: 18px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1a22; --card: #28242f; --ink: #f2edf6; --muted: #b3a9bf; --line: #3b3545;
    --accent: #ff5c9d; --accent-soft: #46243a; --good: #6fd17d; --good-soft: #1f3a24;
    --warn: #ffb547; --warn-soft: #3d3020;
    --c-thousands: #b27be0; --c-hundreds: #ff6f66; --c-tens: #ffc533; --c-ones: #5cb0ff;
    --wood: #7a4826; --rod: #77737f; --grid: rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1d1a22; --card: #28242f; --ink: #f2edf6; --muted: #b3a9bf; --line: #3b3545;
  --accent: #ff5c9d; --accent-soft: #46243a; --good: #6fd17d; --good-soft: #1f3a24;
  --warn: #ffb547; --warn-soft: #3d3020;
  --c-thousands: #b27be0; --c-hundreds: #ff6f66; --c-tens: #ffc533; --c-ones: #5cb0ff;
  --wood: #7a4826; --rod: #77737f; --grid: rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 400 20px/1.7 'Baloo Bhaijaan 2', 'Noto Naskh Arabic', Tahoma, sans-serif;
}
#app { max-width: 860px; margin: 0 auto; padding: 16px 16px 48px; }
h1, h2 { line-height: 1.3; margin: 0; }
h2 { font-size: 1.5rem; margin-bottom: 12px; }
b { color: var(--accent); font-weight: 700; }
a { color: inherit; }

.c-thousands { --c: var(--c-thousands); }
.c-hundreds { --c: var(--c-hundreds); }
.c-tens { --c: var(--c-tens); }
.c-ones { --c: var(--c-ones); }

/* ---- Buttons */
.btn {
  font: inherit; font-weight: 700; font-size: 1.05rem; border: 2px solid var(--line);
  background: var(--card); color: var(--ink); border-radius: 999px; padding: 8px 22px;
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
  min-height: 48px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: .4; cursor: default; }
.btn:focus-visible, .option:focus-visible, .pm:focus-visible, .stage:focus-visible { outline: 3px solid var(--c-ones); outline-offset: 2px; }

/* ---- Home */
.home-head { text-align: center; padding: 24px 0 16px; }
.home-head h1 { font-size: 2.6rem; color: var(--accent); }
.home-head p { color: var(--muted); max-width: 560px; margin: 8px auto 0; }
.home-badge { display: inline-block; background: var(--accent-soft); color: var(--accent); font-weight: 700; padding: 2px 16px; border-radius: 999px; }
.toc { list-style: none; padding: 0; margin: 16px 0 0; display: grid; gap: 8px; }
.toc a, .toc div {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-radius: 14px;
  background: var(--card); border: 2px solid var(--line); text-decoration: none;
}
.toc .ready a { border-color: var(--accent); }
.toc .ready a:hover { background: var(--accent-soft); }
.toc .soon { opacity: .55; }
.toc-n { flex: none; width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-weight: 800; }
.toc-title { flex: 1; font-weight: 600; }
.toc-best { color: var(--warn); font-weight: 700; font-size: .9rem; }
.toc-page { color: var(--muted); font-size: .9rem; }
.toc .ready .toc-page { color: var(--accent); font-weight: 700; }

/* ---- Lesson frame */
.lesson-head { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.back { color: var(--muted); text-decoration: none; font-size: .95rem; }
.lesson-n { color: var(--muted); font-size: .95rem; }
.lesson-head h1 { font-size: 2rem; color: var(--accent); }

.stages { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stage {
  font: inherit; font-weight: 700; border: 2px solid var(--line); background: var(--card); color: var(--muted);
  border-radius: 14px; padding: 8px 6px; cursor: pointer; display: flex; flex-direction: column; align-items: center; line-height: 1.3;
}
.stage-icon { font-size: 1.4rem; }
.stage.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.stage.past { color: var(--good); border-color: var(--good); }

.card { background: var(--card); border: 2px solid var(--line); border-radius: var(--radius); padding: 20px; min-height: 360px; }
.card-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 14px; }
.dots { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; flex: 1; }
.dots span { width: 10px; height: 10px; border-radius: 50%; background: var(--line); }
.dots .on { background: var(--accent); width: 24px; border-radius: 6px; }
.dots .past { background: var(--good); }

.kicker { color: var(--muted); font-size: .9rem; margin-bottom: 4px; }
.goals { list-style: none; padding: 12px 16px; margin: 0 0 16px; background: var(--good-soft); border-radius: 14px; }
.goals li::before { content: '✔ '; color: var(--good); }
.goals .goals-h { font-weight: 700; }
.goals .goals-h::before { content: ''; }
.say { margin: 0 0 12px; }
.tip { margin: 12px 0; padding: 10px 16px; border-radius: 14px; background: var(--warn-soft); border-inline-start: 6px solid var(--warn); }
.tip::before { content: '💡 '; }

/* ---- Abacus */
.abacus { width: 100%; max-width: 320px; margin: 8px auto; }
.abacus-svg { width: 100%; display: block; }
.ab-base { fill: var(--wood); }
.ab-rod { fill: var(--rod); }
.ab-label { fill: #fff; font: 700 14px 'Baloo Bhaijaan 2', Tahoma, sans-serif; text-anchor: middle; }
.bead { fill: var(--c); stroke: rgba(0, 0, 0, .25); stroke-width: 1; transition: opacity .3s; }
.bead.over { stroke: var(--ink); stroke-width: 2.5; stroke-dasharray: 4 3; }
.abacus [data-place].hl .ab-rod { fill: var(--accent); }
.abacus [data-place].hl .bead { filter: drop-shadow(0 0 4px var(--c)); }
.ab-controls { display: grid; grid-template-columns: repeat(4, 1fr); padding: 0 4%; gap: 4px; }
.ab-ctl { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.pm {
  width: 44px; height: 40px; border-radius: 12px; border: 2px solid var(--c); background: var(--card);
  color: var(--c); font: 800 1.5rem/1 system-ui, sans-serif; cursor: pointer;
}
.pm:active { background: var(--c); color: #fff; }

/* ---- Blocks */
.blocks { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: center; gap: 14px; margin: 12px 0; min-height: 40px; }
.bgroup { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 4px; padding: 6px; border-radius: 10px; transition: background .3s; }
.bgroup.hl { background: var(--accent-soft); outline: 2px dashed var(--accent); }
.piece { display: block; }
.piece .face { fill: var(--c); }
.piece .top { fill: var(--c); filter: brightness(1.25); }
.piece .side { fill: var(--c); filter: brightness(.75); }
.piece .grid line { stroke: var(--grid); stroke-width: .8; }
.piece.cube { width: 64px; }
.piece.flat { width: 46px; }
.piece.rod { width: 9px; height: 46px; }
.piece.unit { width: 11px; }
.bgroup.c-tens { max-width: 120px; }
.bgroup.c-ones { max-width: 80px; }
.bgroup.c-hundreds { max-width: 210px; }
.ten-make-one { display: flex; align-items: center; justify-content: center; gap: 14px; flex-wrap: wrap; margin: 12px 0; }
.ten-make-one .ten { max-width: 260px; }
.ten-make-one .eq { font-size: 2.4rem; font-weight: 800; color: var(--accent); }
.tm-side { display: flex; flex-direction: column; align-items: center; }
.tm-side small { color: var(--muted); }

/* ---- Place-value chart */
.pv-chart { border-collapse: collapse; margin: 12px auto; width: 100%; max-width: 460px; table-layout: fixed; }
.pv-chart th { background: var(--c); color: #fff; padding: 6px 4px; font-size: .95rem; border: 2px solid var(--card); }
.pv-chart td { border: 2px solid var(--line); text-align: center; font-size: 1.8rem; font-weight: 700; height: 56px; position: relative; color: var(--ink); transition: background .3s; }
.pv-chart td.hl { background: var(--accent-soft); }
.pv-chart th.hl { outline: 3px solid var(--accent); }
.pv-chart tr.sum td { border-top: 4px solid var(--accent); }
.carry { position: absolute; top: 2px; inset-inline-start: 6px; font-size: .9rem; width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--good); color: var(--good); line-height: 18px; }
.addition { position: relative; max-width: 460px; margin: 0 auto; padding-inline-end: 0; }
.addition .plus { position: absolute; inset-inline-end: -26px; top: 50%; font-size: 2rem; font-weight: 800; color: var(--accent); }

/* ---- Number view */
.number-view { text-align: center; margin: 10px 0; }
.big-number { font-size: 3.4rem; font-weight: 800; line-height: 1.1; letter-spacing: 2px; }
.big-number span { color: var(--c); padding: 0 2px; border-radius: 8px; transition: background .3s; }
.big-number span.hl { background: var(--accent-soft); }
.words { font-size: 1.3rem; font-weight: 600; color: var(--muted); }

/* ---- Story and playground */
.story-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 12px 28px; }
.story-row > .abacus { flex: 0 1 320px; margin: 0; }
.story-row > div:last-child { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.narration { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 6px; }
.narration li { padding: 6px 14px; background: var(--bg); border-radius: 12px; animation: rise .4s ease-out; }
.narration li.done { background: var(--good-soft); font-weight: 700; }
.playground { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); gap: 16px; align-items: start; }

/* ---- Examples */
.example .ex-show { margin-bottom: 4px; }
.steps { list-style: arabic-indic; padding-inline-start: 1.4em; margin: 8px 0; }
.steps li { margin: 4px 0; animation: rise .4s ease-out; }
.result:not(:empty) { background: var(--good-soft); border-radius: 14px; padding: 4px 8px; margin: 8px 0; animation: rise .4s ease-out; }
.example > .btn { display: flex; margin: 12px auto 0; }

/* ---- Questions */
.prompt { font-size: 1.3rem; font-weight: 600; margin: 0 0 10px; }
.answer {
  display: block; margin: 12px auto; width: 220px; max-width: 100%; text-align: center; font: 800 2.2rem 'Baloo Bhaijaan 2', Tahoma, sans-serif;
  border: 3px solid var(--accent); border-radius: 16px; padding: 4px 8px; background: var(--card); color: var(--ink);
}
.answer:focus { outline: 4px solid var(--accent-soft); }
.options { display: grid; gap: 10px; margin: 12px 0; }
.option {
  font: inherit; text-align: start; padding: 10px 16px; border-radius: 14px; border: 2px solid var(--line);
  background: var(--card); color: var(--ink); cursor: pointer; min-height: 52px;
}
.option.picked { border-color: var(--accent); background: var(--accent-soft); }
.option b { color: inherit; }
.feedback:empty { display: none; }
.feedback { border-radius: 14px; padding: 10px 16px; margin: 12px 0; animation: rise .3s ease-out; }
.feedback.good { background: var(--good-soft); color: var(--good); font-weight: 700; }
.feedback.good b { color: var(--good); }
.feedback.try { background: var(--warn-soft); }
.feedback.show { background: var(--accent-soft); }
.feedback.info { background: var(--bg); color: var(--muted); }
.q-actions { display: flex; justify-content: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.pop { display: inline-block; font-size: 1.6rem; animation: pop .5s ease-out; }

.finish { text-align: center; padding: 20px 0; }
.stars { font-size: 4rem; letter-spacing: 8px; color: var(--line); }
.stars .on { color: #f5b400; display: inline-block; animation: pop .6s ease-out backwards; }
.stars .on:nth-child(2) { animation-delay: .2s; }
.stars .on:nth-child(3) { animation-delay: .4s; }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
@keyframes pop { 0% { transform: scale(.2); } 70% { transform: scale(1.3); } 100% { transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

@media (max-width: 640px) {
  body { font-size: 18px; }
  .card { padding: 14px; }
  .playground { grid-template-columns: 1fr; }
  .big-number { font-size: 2.8rem; }
  .pv-chart td { font-size: 1.5rem; height: 48px; }
  .pv-chart th { font-size: .8rem; }
  .addition { margin-inline-end: 28px; }
  .btn { padding: 8px 16px; }
}
