:root {
  --bg: #f4f7ff;
  --card: #ffffff;
  --ink: #1f2a44;
  --muted: #6b7591;
  --primary: #4f7cff;
  --primary-ink: #ffffff;
  --good: #22b573;
  --bad: #ff5d5d;
  --warn: #ffb020;
  --line: #e3e8f5;
  --radius: 18px;
  --shadow: 0 4px 14px rgba(40, 60, 120, .10);
  --unit: #4f7cff;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  font-family: "Mali", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 18px;
  line-height: 1.6;
  min-height: 100vh;
}
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--primary); color: #fff;
  box-shadow: var(--shadow);
}
.topbar h1 { flex: 1; margin: 0; font-size: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: rgba(255,255,255,.2); border: 0; color: #fff; width: 40px; height: 40px; border-radius: 12px; font-size: 28px; line-height: 1; }
.icon-btn[hidden] { display: block; visibility: hidden; }
.stars-pill { background: rgba(255,255,255,.22); padding: 4px 12px; border-radius: 999px; font-weight: 700; font-size: 16px; }

main { max-width: 760px; margin: 0 auto; padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); }

/* ---------- cards / grids ---------- */
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.list { display: grid; gap: 12px; }
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; border: 0; text-align: left; width: 100%;
  display: flex; align-items: center; gap: 14px;
  transition: transform .1s;
}
.card:active { transform: scale(.98); }
.card .emoji { font-size: 40px; line-height: 1; flex: none; }
.card .meta { flex: 1; min-width: 0; }
.card .meta b { display: block; font-size: 19px; }
.card .meta small { color: var(--muted); font-size: 14px; display: block; }
.card.tile { flex-direction: column; text-align: center; padding: 20px 12px; border-top: 6px solid var(--unit); }
.card.tile .emoji { font-size: 52px; }
.card.locked { opacity: .5; }
.badge { font-size: 14px; padding: 2px 10px; border-radius: 999px; background: var(--line); color: var(--muted); white-space: nowrap; }
.lv { display: inline-block; font-size: 12px; padding: 0 8px; border-radius: 999px; font-weight: 700; margin-left: 2px; }
.lv1 { background: #dcf7ea; color: #108a55; }
.lv2 { background: #fff0d6; color: #b46a00; }
.lv3 { background: #ffe1e1; color: #c0392b; }
.badge.done { background: #dcf7ea; color: #108a55; }

.section-title { font-size: 18px; color: var(--muted); margin: 22px 4px 10px; font-weight: 700; }
.hero { background: var(--unit); color: #fff; border-radius: var(--radius); padding: 20px; margin-bottom: 8px; box-shadow: var(--shadow); }
.hero h2 { margin: 0 0 4px; font-size: 26px; }
.hero p { margin: 0; opacity: .92; }

/* ---------- lesson pages ---------- */
.page { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.page h2 { margin: 0 0 8px; font-size: 22px; color: var(--unit); }
.page p { margin: 8px 0; }
.page .note { background: #fff6dd; border-left: 5px solid var(--warn); padding: 10px 12px; border-radius: 10px; margin: 12px 0; }
.page .tip  { background: #e8f0ff; border-left: 5px solid var(--primary); padding: 10px 12px; border-radius: 10px; margin: 12px 0; }
.widget { margin: 14px 0 4px; padding: 14px; border-radius: 14px; background: #f7f9ff; border: 2px dashed var(--line); }
.widget svg { max-width: 100%; height: auto; display: block; margin: 0 auto; touch-action: none; }

.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 16px; }
.dots { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.dots i { width: 9px; height: 9px; border-radius: 50%; background: var(--line); }
.dots i.on { background: var(--unit); }

/* ---------- buttons ---------- */
.btn {
  border: 0; border-radius: 14px; padding: 12px 20px; font-weight: 700; font-size: 18px;
  background: var(--primary); color: var(--primary-ink); box-shadow: 0 3px 0 rgba(0,0,0,.15);
  min-height: 48px;
}
.btn:active { transform: translateY(2px); box-shadow: none; }
.btn.ghost { background: var(--card); color: var(--ink); border: 2px solid var(--line); box-shadow: none; }
.btn.good { background: var(--good); }
.btn.small { padding: 6px 14px; min-height: 38px; font-size: 16px; }
.btn:disabled { opacity: .45; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin: 10px 0; }

/* ---------- practice ---------- */
.progress-bar { height: 12px; background: var(--line); border-radius: 999px; overflow: hidden; margin-bottom: 14px; }
.progress-bar > i { display: block; height: 100%; background: var(--good); transition: width .3s; }
.question { font-size: 21px; }
.question .q-visual { margin: 12px 0; }
.choices { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); margin-top: 14px; }
.choice { background: var(--card); border: 3px solid var(--line); border-radius: 14px; padding: 14px; font-size: 20px; font-weight: 600; min-height: 56px; }
.choice.correct { border-color: var(--good); background: #e6faf0; }
.choice.wrong { border-color: var(--bad); background: #ffeaea; }
.answer-row { display: flex; gap: 10px; align-items: center; justify-content: center; flex-wrap: wrap; margin-top: 14px; }
.answer-input { font: inherit; font-size: 24px; width: 120px; padding: 10px; text-align: center; border: 3px solid var(--line); border-radius: 14px; }
.answer-input:focus { outline: none; border-color: var(--primary); }
.frac-input { display: inline-flex; flex-direction: column; align-items: center; gap: 4px; }
.frac-input .answer-input { width: 80px; }
.frac-input hr { width: 90px; border: 0; border-top: 3px solid var(--ink); margin: 0; }
.unit-label { font-size: 20px; color: var(--muted); }
.feedback { margin-top: 14px; padding: 12px 14px; border-radius: 14px; font-weight: 600; }
.feedback.good { background: #e6faf0; color: #0f7a4a; }
.feedback.bad { background: #ffeaea; color: #b42828; }
.feedback .explain { font-weight: 400; color: var(--ink); margin-top: 6px; }
.hint-box { margin-top: 10px; background: #fff6dd; padding: 10px 12px; border-radius: 12px; }

.result { text-align: center; padding: 26px 16px; }
.result .big { font-size: 64px; }
.result .score { font-size: 30px; font-weight: 700; margin: 6px 0; }

/* ---------- reusable fraction display ---------- */
.frac { display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle; line-height: 1.1; margin: 0 3px; font-weight: 700; }
.frac > span:first-child { border-bottom: 2.5px solid currentColor; padding: 0 3px; }

/* ---------- celebration ---------- */
.confetti { position: fixed; top: -20px; font-size: 26px; pointer-events: none; z-index: 50; animation: fall 1.6s ease-in forwards; }
@keyframes fall { to { transform: translateY(105vh) rotate(540deg); opacity: .2; } }
.pop { animation: pop .35s ease; }
@keyframes pop { 50% { transform: scale(1.12); } }
.shake { animation: shake .35s ease; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

@media (max-width: 420px) {
  body { font-size: 17px; }
  .grid { grid-template-columns: 1fr 1fr; }
  .question { font-size: 19px; }
}
