:root {
  --bg: #f6f4ff;
  --card: #ffffff;
  --ink: #1f1b3a;
  --muted: #6b6790;
  --line: #e6e2fb;
  --brand: #6d5dfc;
  --ok: #16a34a;
  --bad: #e11d48;
  --radius: 20px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: 0;
  background: none;
  touch-action: manipulation;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- Home ---------- */
.hero { text-align: center; padding: 8px 0 0; }
.logo {
  font-size: 48px;
  font-weight: 900;
  letter-spacing: -1px;
  background: linear-gradient(90deg, #6d5dfc, #ec4899);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.hero p { margin: 2px 0 0; color: var(--muted); font-size: 18px; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.op-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px 10px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  border: 2px solid color-mix(in srgb, var(--c) 25%, transparent);
  box-shadow: 0 5px 0 color-mix(in srgb, var(--c) 40%, transparent), 0 10px 24px rgba(40, 30, 120, .07);
  transition: transform .08s;
}
.op-card:active { transform: translateY(4px); box-shadow: 0 1px 0 color-mix(in srgb, var(--c) 40%, transparent); }
.op-sym {
  width: 66px; height: 66px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 42px; font-weight: 900; line-height: 1;
  color: #fff;
  background: var(--c);
}
.op-name { font-size: 20px; font-weight: 800; }
.op-hint { font-size: 14px; color: var(--muted); }

/* ---------- Generic ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 14px 20px;
  border-radius: 16px;
  font-weight: 800; font-size: 18px;
  background: var(--brand); color: #fff;
  box-shadow: 0 4px 0 #4b3fd1;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #4b3fd1; }
.btn.ghost { background: var(--card); color: var(--ink); border: 2px solid var(--line); box-shadow: 0 4px 0 var(--line); }
.btn.danger { background: var(--card); color: var(--bad); border: 2px solid #fecdd3; box-shadow: 0 4px 0 #fecdd3; }
.btn.colored { background: var(--c); box-shadow: 0 4px 0 color-mix(in srgb, var(--c) 70%, #000); }
.wide { width: 100%; }

.panel {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  border: 2px solid var(--line);
}
.panel h2 { margin: 0 0 10px; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; font-weight: 700; }

.seg { display: inline-flex; background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { padding: 8px 14px; border-radius: 9px; font-weight: 800; color: var(--muted); min-width: 46px; }
.seg button.on { background: var(--brand); color: #fff; }

.head { display: flex; align-items: center; gap: 12px; }
.head h1 { font-size: 26px; margin: 0; flex: 1; }
.icon-btn {
  width: 46px; height: 46px; flex: none;
  border-radius: 14px;
  background: var(--card);
  border: 2px solid var(--line);
  font-size: 20px; font-weight: 900;
  display: grid; place-items: center;
}
.badge {
  width: 46px; height: 46px; flex: none;
  border-radius: 50%;
  background: var(--c); color: #fff;
  display: grid; place-items: center;
  font-size: 30px; font-weight: 900;
}

/* ---------- Levels ---------- */
.levels { display: flex; flex-direction: column; gap: 12px; }
.level {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex; align-items: center; gap: 14px;
  border: 2px solid var(--line);
  box-shadow: 0 4px 0 var(--line);
  text-align: left;
  width: 100%;
}
button.level:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.level.custom { flex-direction: column; align-items: stretch; }
.level .top { display: flex; align-items: center; gap: 14px; }
.level .num {
  width: 46px; height: 46px; flex: none;
  border-radius: 14px;
  background: var(--c); color: #fff;
  font-weight: 900; font-size: 22px;
  display: grid; place-items: center;
}
.level .txt { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.level .lbl { font-weight: 800; font-size: 19px; }
.level .meta { font-size: 14px; color: var(--muted); }
.level .go { font-size: 24px; color: var(--c); font-weight: 900; }
.dots { color: var(--c); letter-spacing: 2px; font-size: 13px; }

.chips { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin: 14px 0; }
.chip {
  padding: 10px 0;
  border-radius: 12px;
  background: var(--bg);
  font-weight: 800; font-size: 18px;
  border: 2px solid var(--line);
}
.chip.on { background: var(--c); color: #fff; border-color: var(--c); }

/* ---------- Play ---------- */
.play { display: flex; flex-direction: column; gap: 12px; flex: 1; }
.topbar { display: flex; align-items: center; gap: 12px; }
.progress { flex: 1; height: 14px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress .bar { height: 100%; background: var(--c); border-radius: 99px; transition: width .3s; }
.count { font-weight: 800; color: var(--muted); min-width: 52px; text-align: right; font-variant-numeric: tabular-nums; }

.stage {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px;
  min-height: 200px;
}
.streak { font-weight: 800; color: #ea580c; height: 24px; opacity: 0; transition: opacity .2s; }
.streak.show { opacity: 1; }
.problem {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center;
  gap: .28em;
  font-size: clamp(30px, 10vw, 58px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.problem .sym { color: var(--c); }
.answer {
  min-width: 2.2em;
  min-height: 1.35em;
  line-height: 1.25;
  padding: 0 .25em;
  border-radius: 14px;
  border: 3px dashed var(--c);
  text-align: center;
  background: var(--card);
}
.answer .caret { display: inline-block; width: 3px; height: .9em; background: var(--c); vertical-align: -0.08em; animation: blink 1s steps(1) infinite; }
.problem.right .answer { border-style: solid; border-color: var(--ok); background: #dcfce7; color: var(--ok); animation: pop .3s; }
.problem.wrong .answer { border-style: solid; border-color: var(--bad); background: #ffe4e6; color: var(--bad); text-decoration: line-through; animation: shake .4s; }

.feedback { min-height: 70px; text-align: center; font-size: 22px; font-weight: 800; }
.feedback .good { color: var(--ok); display: inline-block; animation: pop .3s; }
.feedback .bad { color: var(--brand); display: inline-block; animation: pop .3s; font-size: 20px; }
.feedback .fix { display: block; color: var(--ink); font-size: 26px; margin-top: 4px; font-variant-numeric: tabular-nums; }
.feedback .fix b { color: var(--ok); }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.key {
  height: clamp(56px, 9vh, 74px);
  border-radius: 16px;
  background: var(--card);
  font-size: 30px; font-weight: 900;
  border: 2px solid var(--line);
  box-shadow: 0 4px 0 var(--line);
}
.key:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.key.ok { background: var(--c); color: #fff; border-color: transparent; box-shadow: 0 4px 0 color-mix(in srgb, var(--c) 70%, #000); }
.key.del { color: var(--muted); font-size: 26px; }
.keypad.waiting {
  display: flex; flex-direction: column; gap: 10px;
  min-height: calc(4 * clamp(56px, 9vh, 74px) + 30px);
}
.keypad .next { height: clamp(56px, 9vh, 74px); flex: none; margin-top: auto; font-size: 22px; }

.explain {
  background: var(--card);
  border: 2px solid color-mix(in srgb, var(--c) 30%, transparent);
  border-radius: var(--radius);
  padding: 12px 16px 14px;
  animation: rise .35s;
}
.explain h3 { margin: 0 0 4px; font-size: 17px; color: var(--c); }
.explain .tip { color: var(--muted); font-size: 15px; font-weight: 700; margin-top: 8px; }
.explain .eq { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; padding: 2px 0; line-height: 1.3; }
.explain .eq b { color: var(--c); }
.explain .pl { display: inline-block; min-width: 6.2em; font-size: 14px; color: var(--muted); font-weight: 700; }
.explain .chk { margin-top: 8px; color: var(--ok); font-size: 17px; font-weight: 800; font-variant-numeric: tabular-nums; }

/* ---------- Result ---------- */
.result { text-align: center; display: flex; flex-direction: column; gap: 14px; }
.stars { font-size: 60px; letter-spacing: 6px; margin-top: 12px; }
.star { display: inline-block; animation: pop .4s backwards; }
.star:nth-child(2) { animation-delay: .15s; }
.star:nth-child(3) { animation-delay: .3s; }
.star.off { filter: grayscale(1); opacity: .25; }
.result h1 { margin: 0; font-size: 30px; }
.score { font-size: 22px; font-weight: 800; color: var(--muted); }
.score b { color: var(--ink); font-size: 44px; }
.facts { display: flex; justify-content: center; gap: 10px; }
.fact { background: var(--card); border: 2px solid var(--line); border-radius: 16px; padding: 10px 14px; min-width: 100px; }
.fact b { display: block; font-size: 22px; }
.fact span { font-size: 13px; color: var(--muted); }
.mist { text-align: left; }
.mist li { padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.mist li:last-child { border-bottom: 0; }
.mist ul { list-style: none; padding: 0; margin: 0; }
.mist .given { color: var(--bad); text-decoration: line-through; font-size: 15px; margin-left: 6px; }
.mist .ok { color: var(--ok); }
.btns { display: flex; gap: 12px; }
.btns .btn { flex: 1; }

/* ---------- Stats ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; margin-bottom: 8px; }
.stat b { display: block; font-size: 21px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.lvl-row { display: grid; grid-template-columns: 1.3fr 2fr 48px; align-items: center; gap: 10px; padding: 5px 0; font-size: 14px; font-weight: 700; }
.lvl-row .pct { text-align: right; font-variant-numeric: tabular-nums; }
.lvl-row .n { color: var(--muted); font-weight: 600; font-size: 12px; }
.meter { height: 10px; background: var(--line); border-radius: 99px; overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--c); border-radius: 99px; }
.empty { color: var(--muted); font-size: 15px; margin: 0; }

.recent { list-style: none; margin: 0; padding: 0; }
.recent li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
.recent li:last-child { border-bottom: 0; }
.recent .dot { width: 30px; height: 30px; border-radius: 50%; background: var(--c); color: #fff; display: grid; place-items: center; font-weight: 900; flex: none; }
.recent .what { flex: 1; display: flex; flex-direction: column; }
.recent .what small { color: var(--muted); }
.recent .sc { font-weight: 800; font-variant-numeric: tabular-nums; text-align: right; }
.recent .sc small { display: block; color: var(--muted); font-weight: 600; }

.tricky { display: flex; flex-wrap: wrap; gap: 8px; }
.tricky span { background: #ffe4e6; color: #9f1239; border-radius: 12px; padding: 6px 10px; font-weight: 800; font-variant-numeric: tabular-nums; }
.tricky small { opacity: .7; margin-left: 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; top: max(20px, env(safe-area-inset-top)); transform: translateX(-50%);
  background: var(--ink); color: #fff;
  padding: 12px 18px; border-radius: 14px;
  font-weight: 700; z-index: 10;
  box-shadow: 0 10px 30px rgba(0,0,0,.2);
}

@keyframes pop { 0% { transform: scale(.6); opacity: 0; } 70% { transform: scale(1.12); opacity: 1; } 100% { transform: scale(1); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20%,60% { transform: translateX(-8px); } 40%,80% { transform: translateX(8px); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes rise { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
