:root {
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #efedf8;
  --text: #1c1a2e;
  --muted: #6c6887;
  --border: #e2dff0;
  --primary: #5b4cf0;
  --primary-ink: #ffffff;
  --primary-soft: #ebe8ff;
  --new: #2f6fec;
  --learn: #d9432f;
  --due: #1b9a5a;
  --again: #d9432f;
  --hard: #c77a0a;
  --good: #1b9a5a;
  --easy: #2f6fec;
  --flag: #e0a106;
  --shadow: 0 1px 2px rgba(20, 16, 60, .06), 0 4px 16px rgba(20, 16, 60, .06);
  --radius: 16px;
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111019; --surface: #1b1a26; --surface-2: #252333; --text: #eeecf8; --muted: #9d99b8;
    --border: #2e2c3e; --primary: #8b80ff; --primary-ink: #120f2b; --primary-soft: #2a2650;
    --new: #6b9bff; --learn: #ff7a66; --due: #4fd18b; --again: #ff7a66; --hard: #f0b04a; --good: #4fd18b; --easy: #6b9bff;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111019; --surface: #1b1a26; --surface-2: #252333; --text: #eeecf8; --muted: #9d99b8;
  --border: #2e2c3e; --primary: #8b80ff; --primary-ink: #120f2b; --primary-soft: #2a2650;
  --new: #6b9bff; --learn: #ff7a66; --due: #4fd18b; --again: #ff7a66; --hard: #f0b04a; --good: #4fd18b; --easy: #6b9bff;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

#app { min-height: 100%; display: flex; flex-direction: column; }
#topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
#title { flex: 1; margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#topActions { display: flex; gap: 4px; }
#view { flex: 1; width: 100%; max-width: 760px; margin: 0 auto; padding: 4px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px); outline: none; }
body.no-tabs #view { padding-bottom: calc(env(safe-area-inset-bottom) + 24px); }
#tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
body.no-tabs #tabbar { display: none; }
#tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 600; }
#tabbar a.active { color: var(--primary); }
#tabbar a.active svg { stroke-width: 2.4; }

.icon-btn { display: inline-grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 12px; background: transparent; color: var(--text); cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.on { color: var(--primary); background: var(--primary-soft); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 10px 18px;
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); font-weight: 600; cursor: pointer;
  text-decoration: none; transition: transform .08s ease, background .15s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.btn.soft { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.btn.danger { color: var(--again); }
.btn.block { width: 100%; }
.btn.lg { min-height: 54px; font-size: 17px; border-radius: 14px; }
.btn.sm { min-height: 34px; padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn svg { width: 20px; height: 20px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row > .grow { flex: 1; }
.stack > * + * { margin-top: 12px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.center { text-align: center; }
.section-title { margin: 22px 4px 10px; font-size: 13px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.list > * + * { border-top: 1px solid var(--border); }
.list-item { display: flex; align-items: center; gap: 12px; padding: 14px 16px; text-decoration: none; color: inherit; cursor: pointer; background: none; border-left: 0; border-right: 0; border-bottom: 0; width: 100%; text-align: left; }
.list-item:hover { background: var(--surface-2); }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.counts { display: flex; gap: 10px; font-variant-numeric: tabular-nums; font-weight: 700; font-size: 15px; }
.c-new { color: var(--new); } .c-learn { color: var(--learn); } .c-due { color: var(--due); }
.counts span.zero { color: var(--muted); opacity: .45; font-weight: 500; }

.hero { display: flex; gap: 16px; align-items: center; }
.ring { position: relative; width: 84px; height: 84px; flex: none; }
.ring svg { width: 84px; height: 84px; transform: rotate(-90deg); }
.ring circle { stroke-width: 9; fill: none; }
.ring .bg { stroke: var(--surface-2); }
.ring .fg { stroke: var(--primary); transition: stroke-dashoffset .6s ease; }
.ring .lbl { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 800; font-size: 18px; }
.stat-pills { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; flex: 1; }
.pill { background: var(--surface-2); border-radius: 12px; padding: 8px 10px; }
.pill b { display: block; font-size: 20px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.pill span { font-size: 12px; color: var(--muted); }

.fab { position: fixed; right: max(16px, calc(50% - 364px)); bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); z-index: 15; width: 58px; height: 58px; border-radius: 18px; border: 0; background: var(--primary); color: var(--primary-ink); display: grid; place-items: center; box-shadow: 0 8px 24px rgba(91, 76, 240, .35); cursor: pointer; }

/* Study */
.study { display: flex; flex-direction: column; min-height: calc(100dvh - 90px - env(safe-area-inset-top)); }
.study-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-bottom: 12px; }
.progress > div { height: 100%; background: var(--primary); transition: width .3s; }
.flashcard { flex: 1; display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; box-shadow: var(--shadow); padding: 22px 20px; overflow-wrap: anywhere; position: relative; }
.flashcard .content { font-size: 21px; line-height: 1.45; text-align: center; margin: auto 0; }
.flashcard .content img { max-width: 100%; height: auto; border-radius: 10px; }
.flashcard hr { border: 0; border-top: 1px dashed var(--border); margin: 18px 0; width: 100%; }
.flashcard .extra { font-size: 16px; color: var(--muted); text-align: center; margin-top: 10px; }
.flashcard .flag-dot { position: absolute; top: 14px; left: 14px; width: 12px; height: 12px; border-radius: 50%; background: var(--flag); }
.cloze { color: var(--primary); font-weight: 700; }
.cloze-blank { color: var(--primary); font-weight: 700; background: var(--primary-soft); padding: 0 8px; border-radius: 6px; }
.answer-bar { position: sticky; bottom: 0; padding: 12px 0 calc(env(safe-area-inset-bottom) + 6px); background: linear-gradient(transparent, var(--bg) 30%); }
.rate-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate { display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 60px; padding: 8px 4px; border-radius: 14px; border: 0; color: #fff; font-weight: 700; cursor: pointer; }
.rate small { font-weight: 600; opacity: .9; font-size: 12px; }
.rate.r1 { background: var(--again); } .rate.r2 { background: var(--hard); } .rate.r3 { background: var(--good); } .rate.r4 { background: var(--easy); }
:root[data-theme="dark"] .rate, :root:not([data-theme="light"]) .rate { color: #fff; }
.typein { width: 100%; margin-top: 16px; }
.diff { font-family: ui-monospace, monospace; font-size: 17px; margin-top: 12px; text-align: center; }
.diff .ok { color: var(--good); } .diff .bad { color: var(--again); text-decoration: line-through; } .diff .miss { color: var(--hard); }
.ai-verdict { margin-top: 14px; border-radius: 14px; padding: 12px 14px; background: var(--surface-2); font-size: 15px; text-align: left; }
.ai-verdict b { display: block; margin-bottom: 2px; }
.ai-verdict.v4, .ai-verdict.v3 { border-left: 4px solid var(--good); }
.ai-verdict.v2 { border-left: 4px solid var(--hard); }
.ai-verdict.v1 { border-left: 4px solid var(--again); }

/* Voice */
.voice { display: flex; flex-direction: column; align-items: stretch; gap: 14px; min-height: calc(100dvh - 100px); }
.orb-wrap { display: grid; place-items: center; padding: 10px 0; }
.orb { width: 140px; height: 140px; border-radius: 50%; border: 0; cursor: pointer; background: radial-gradient(circle at 35% 30%, #a79dff, var(--primary) 60%, #3a2bc4); box-shadow: 0 10px 40px rgba(91,76,240,.35); display: grid; place-items: center; color: #fff; transition: transform .2s; }
.orb svg { width: 48px; height: 48px; }
.orb.listening { animation: pulse 1.3s ease-in-out infinite; }
.orb.speaking { animation: breathe 2s ease-in-out infinite; }
.orb.thinking { animation: spin 1.2s linear infinite; background: conic-gradient(from 0deg, var(--primary), #a79dff, var(--primary)); }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(91,76,240,.45); } 50% { box-shadow: 0 0 0 26px rgba(91,76,240,0); } }
@keyframes breathe { 0%,100% { transform: scale(1); } 50% { transform: scale(1.06); } }
@keyframes spin { to { transform: rotate(360deg); } }
.voice-state { text-align: center; font-weight: 700; color: var(--primary); min-height: 24px; }
.transcript { min-height: 52px; text-align: center; font-size: 18px; color: var(--muted); font-style: italic; }
.voice-log { display: flex; flex-direction: column; gap: 8px; }
.bubble { padding: 10px 14px; border-radius: 16px; max-width: 88%; font-size: 15px; }
.bubble.me { align-self: flex-end; background: var(--primary); color: var(--primary-ink); border-bottom-right-radius: 4px; }
.bubble.ai { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 99px; padding: 6px 12px; font-size: 14px; cursor: pointer; }
.chip.on { background: var(--primary-soft); color: var(--primary); border-color: transparent; font-weight: 600; }

/* Forms */
label.field { display: block; }
label.field > span { display: block; font-size: 13px; font-weight: 700; color: var(--muted); margin: 0 2px 6px; }
.input, textarea.input, select.input { width: 100%; min-height: 46px; padding: 10px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); outline: none; }
textarea.input { min-height: 96px; resize: vertical; line-height: 1.45; }
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field-tools { display: flex; gap: 2px; margin-top: 4px; flex-wrap: wrap; }
.field-tools button { border: 0; background: none; color: var(--muted); padding: 6px 8px; border-radius: 8px; cursor: pointer; font-weight: 700; font-size: 14px; display: inline-flex; align-items: center; gap: 4px; }
.field-tools button:hover { background: var(--surface-2); color: var(--text); }
.field-tools button svg { width: 18px; height: 18px; }
.field-tools button.rec { color: var(--again); }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px 6px; border-radius: 9px; font-weight: 600; font-size: 14px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; }
.switch input { width: 46px; height: 28px; appearance: none; background: var(--border); border-radius: 99px; position: relative; cursor: pointer; transition: background .2s; flex: none; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked { background: var(--primary); }
.switch input:checked::after { transform: translateX(18px); }
.preview { font-size: 15px; padding: 12px; border: 1px dashed var(--border); border-radius: 12px; }

/* Browse */
.browse-item .q { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.browse-item .a { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.new { color: var(--new); } .badge.learn, .badge.relearn { color: var(--learn); } .badge.review { color: var(--due); } .badge.susp { color: var(--hard); }

/* Stats */
.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 560px) { .kpis { grid-template-columns: repeat(4, 1fr); } }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px; }
.kpi b { display: block; font-size: 24px; font-variant-numeric: tabular-nums; }
.kpi span { font-size: 13px; color: var(--muted); }
.heat { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 1fr); gap: 3px; overflow-x: auto; padding-bottom: 4px; }
.heat i { width: 13px; height: 13px; border-radius: 3px; background: var(--surface-2); }
.heat i.l1 { background: color-mix(in srgb, var(--primary) 30%, var(--surface-2)); }
.heat i.l2 { background: color-mix(in srgb, var(--primary) 55%, var(--surface-2)); }
.heat i.l3 { background: color-mix(in srgb, var(--primary) 80%, var(--surface-2)); }
.heat i.l4 { background: var(--primary); }
.bars { display: flex; align-items: flex-end; gap: 4px; height: 120px; }
.bars div { flex: 1; background: var(--primary); border-radius: 4px 4px 0 0; min-height: 2px; position: relative; }
.bars div.zero { background: var(--surface-2); }
.bars-labels { display: flex; gap: 4px; font-size: 10px; color: var(--muted); margin-top: 4px; }
.bars-labels span { flex: 1; text-align: center; }
.stack-bar { display: flex; height: 14px; border-radius: 99px; overflow: hidden; background: var(--surface-2); }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 13px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: middle; }

/* Modal / sheet */
.backdrop { position: fixed; inset: 0; z-index: 50; background: rgba(10, 8, 30, .45); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
@media (min-width: 640px) { .backdrop { align-items: center; } }
.sheet { width: 100%; max-width: 560px; max-height: 88dvh; overflow: auto; background: var(--bg); border-radius: 22px 22px 0 0; padding: 18px 16px calc(env(safe-area-inset-bottom) + 18px); animation: up .2s ease; }
@media (min-width: 640px) { .sheet { border-radius: 22px; } }
.sheet h2 { margin: 0 0 12px; font-size: 19px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h2 { margin: 0; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.menu .list-item { font-weight: 600; }
.menu .list-item svg { color: var(--muted); }

#toast { position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 20px); transform: translate(-50%, 20px); z-index: 60; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none; transition: all .25s; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { background: none; border: 0; color: var(--primary); font-weight: 800; margin-left: 10px; cursor: pointer; }
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 70; width: 100%; height: 100%; }

.chat { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.chat .bubble.ai { max-width: 100%; white-space: pre-wrap; }
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty .big { font-size: 48px; margin-bottom: 6px; }
.gen-card { padding: 12px 14px; display: flex; gap: 10px; align-items: flex-start; }
.gen-card input[type=checkbox] { width: 20px; height: 20px; margin-top: 3px; accent-color: var(--primary); flex: none; }
.gen-card .q { font-weight: 600; }
.gen-card .a { color: var(--muted); font-size: 14px; }
.spinner { width: 20px; height: 20px; border: 3px solid var(--primary-soft); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
kbd { font-size: 11px; opacity: .7; }
.done-screen { text-align: center; padding: 30px 10px; }
.done-screen .big { font-size: 64px; }
.install-banner { display: flex; align-items: center; gap: 12px; background: var(--primary-soft); color: var(--text); border-radius: 14px; padding: 12px 14px; margin-bottom: 14px; }
.rec-live { background: var(--again) !important; color: #fff !important; animation: pulse 1.3s ease-in-out infinite; }
.level { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; width: 160px; margin: 0 auto; }
.level > div { height: 100%; width: 0; background: var(--primary); transition: width .08s; }
