:root {
  --bg: #f5f2ea;
  --surface: #fffdf8;
  --surface-2: #ece7db;
  --ink: #1b2320;
  --ink-2: #5b625e;
  --ink-3: #959a96;
  --line: rgba(27, 35, 32, .09);
  --brand: #1f3b33;
  --brand-ink: #f8f5ed;
  --ochre: #d99a3e;
  --vermilion: #cc5a3a;
  --new: #2f6fdf;
  --learn: #d2453f;
  --review: #2b8a4a;
  --r1: #d2453f;
  --r2: #c9851d;
  --r3: #2b8a4a;
  --r4: #2f6fdf;
  --shadow: 0 1px 0 rgba(27, 35, 32, .04), 0 8px 24px -12px rgba(27, 35, 32, .18);
  --radius: 20px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --gutter: 20px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  --font-round: ui-rounded, "SF Pro Rounded", -apple-system, system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1210;
    --surface: #161c19;
    --surface-2: #212924;
    --ink: #eef0ea;
    --ink-2: #a3aaa5;
    --ink-3: #6c746f;
    --line: rgba(238, 240, 234, .08);
    --brand: #e9b25f;
    --brand-ink: #18120a;
    --new: #6c9cff;
    --learn: #ff6b62;
    --review: #4cc274;
    --r1: #ff6b62;
    --r2: #f0a63c;
    --r3: #4cc274;
    --r4: #6c9cff;
    --shadow: 0 0 0 1px rgba(255, 255, 255, .04);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }
svg { display: block; }
[hidden] { display: none !important; }

.view {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 52px; padding: 0 22px; border-radius: 16px;
  font-weight: 600; font-size: 17px; letter-spacing: -.01em;
  transition: transform .12s ease, opacity .12s ease, background-color .2s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; }
.btn-primary { background: var(--brand); color: var(--brand-ink); }
.btn-secondary { background: var(--surface-2); color: var(--ink); }
.btn-block { width: 100%; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 22px;
  display: grid; place-items: center; color: var(--ink);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg, .back-btn svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#btn-more svg { fill: currentColor; stroke: none; }

/* ---------- Login ---------- */
.login {
  margin: auto; width: 100%; max-width: 420px;
  padding: calc(var(--safe-t) + 24px) var(--gutter) calc(var(--safe-b) + 24px);
  text-align: center;
}
.login-logo { border-radius: 22px; box-shadow: 0 12px 30px -14px rgba(0, 0, 0, .45); }
.login h1 { font: 700 34px/1.1 var(--font-round); margin: 20px 0 8px; letter-spacing: -.02em; }
.login-sub { color: var(--ink-2); font-size: 15px; margin: 0 0 32px; }
#login-form { display: grid; gap: 12px; text-align: left; }
.field { display: block; background: var(--surface); border-radius: 16px; padding: 10px 16px 8px; box-shadow: var(--shadow); }
.field span { display: block; font-size: 12px; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }
.field input {
  width: 100%; border: 0; outline: 0; background: none; color: var(--ink);
  font: 17px/1.5 var(--font); padding: 2px 0 2px;
}
.field:focus-within { box-shadow: var(--shadow), 0 0 0 2px var(--brand); }
.form-error { min-height: 20px; margin: 2px 4px; color: var(--learn); font-size: 14px; }

/* ---------- Topbar ---------- */
.topbar { padding: calc(var(--safe-t) + 4px) 12px 0; background: var(--bg); z-index: 2; }
.topbar-row { display: flex; align-items: center; justify-content: space-between; height: 48px; }
.large-title { font: 700 34px/1.1 var(--font-round); letter-spacing: -.025em; margin: 6px 8px 2px; }
.studied { margin: 0 8px 12px; color: var(--ink-2); font-size: 15px; min-height: 21px; }

.sync-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px 0 10px;
  border-radius: 18px; background: var(--surface); box-shadow: var(--shadow);
  font-size: 15px; font-weight: 600; color: var(--ink-2);
}
.sync-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.sync-btn.busy svg { animation: spin 1s linear infinite; }
.sync-btn.ok { color: var(--review); }
.sync-btn.err { color: var(--learn); }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Deck list ---------- */
.deck-scroll {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain;
  padding: 4px var(--gutter) calc(var(--safe-b) + 28px);
  position: relative;
}
.ptr { height: 0; display: grid; place-items: center; overflow: visible; }
.ptr span {
  width: 22px; height: 22px; border-radius: 50%; border: 2.5px solid var(--ink-3); border-top-color: transparent;
  transform: translateY(-34px); opacity: 0; transition: opacity .15s;
}
.ptr.armed span { opacity: 1; }
.ptr.loading span { opacity: 1; animation: spin .8s linear infinite; }

.deck-list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.deck-list:empty { display: none; }
.deck-list ul { list-style: none; margin: 0; padding: 0; }
.deck-row {
  display: flex; align-items: center; min-height: 60px; padding: 0 14px 0 0;
  position: relative; transition: background-color .15s;
}
.deck-row:active { background: var(--surface-2); }
.deck-row::after { content: ""; position: absolute; left: 16px; right: 0; bottom: 0; height: 1px; background: var(--line); }
.deck-list > li:last-child > .deck-row::after { display: none; }
.deck-toggle { width: 40px; height: 60px; display: grid; place-items: center; color: var(--ink-3); flex: none; }
.deck-toggle svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; transition: transform .2s; }
.deck-toggle.open svg { transform: rotate(90deg); }
.deck-toggle.none { pointer-events: none; }
.deck-name { flex: 1; min-width: 0; padding: 10px 8px 10px 0; font-size: 17px; font-weight: 500; overflow-wrap: anywhere; line-height: 1.3; }
.deck-name.filtered { color: var(--new); }
.deck-counts { display: flex; gap: 4px; flex: none; font: 600 15px/1 var(--font-round); font-variant-numeric: tabular-nums; }
.deck-counts span { min-width: 28px; text-align: right; }
.deck-counts .zero { color: var(--ink-3); opacity: .5; }
.c-new { color: var(--new); }
.c-learn { color: var(--learn); }
.c-review { color: var(--review); }

.empty { text-align: center; color: var(--ink-2); padding: 40px 16px; }
.legend { display: flex; justify-content: center; gap: 18px; font-size: 13px; font-weight: 600; margin: 18px 0 0; }
.legend span::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 4px; background: currentColor; margin-right: 6px; vertical-align: 1px; }

/* ---------- Study ---------- */
.view-study { background: var(--bg); }
.study-bar { display: flex; align-items: center; gap: 4px; padding-bottom: 6px; padding-left: 4px; padding-right: 4px; }
.back-btn { width: 44px; height: 44px; display: grid; place-items: center; color: var(--ink); border-radius: 22px; }
.back-btn:active { background: var(--surface-2); }
.study-head { flex: 1; min-width: 0; text-align: center; }
.study-deck { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.counts { display: inline-flex; gap: 14px; font: 600 14px/1.4 var(--font-round); font-variant-numeric: tabular-nums; }
.counts span { position: relative; }
.counts span.active::after { content: ""; position: absolute; left: -2px; right: -2px; bottom: -2px; height: 2px; border-radius: 1px; background: currentColor; }

.card-stage {
  flex: 1; min-height: 0; position: relative;
  margin: 2px 10px 0; border-radius: 26px 26px 22px 22px; overflow: hidden;
  background: var(--surface); box-shadow: var(--shadow);
}
#card-frame { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: transparent; opacity: 1; transition: opacity .12s ease; }
#card-frame.fade { opacity: 0; }
.flag-strip { position: absolute; left: 0; right: 0; top: 0; height: 5px; z-index: 1; }
.flag-strip[data-flag="1"] { background: #e5484d; }
.flag-strip[data-flag="2"] { background: #f59e0b; }
.flag-strip[data-flag="3"] { background: #30a46c; }
.flag-strip[data-flag="4"] { background: #3e63dd; }
.flag-strip[data-flag="5"] { background: #e93d82; }
.flag-strip[data-flag="6"] { background: #12a594; }
.flag-strip[data-flag="7"] { background: #8e4ec6; }

.done { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 32px; gap: 6px; }
.done-mark svg { width: 72px; height: 72px; fill: none; stroke: var(--review); stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.done-mark circle { stroke-dasharray: 132; stroke-dashoffset: 132; animation: draw .6s .05s ease forwards; }
.done-mark path { stroke-dasharray: 34; stroke-dashoffset: 34; animation: draw .35s .5s ease forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }
.done h2 { font: 700 26px/1.2 var(--font-round); margin: 14px 0 0; letter-spacing: -.02em; }
.done p { color: var(--ink-2); margin: 0 0 22px; max-width: 30ch; }

.answer-bar { padding: 10px 10px calc(var(--safe-b) + 6px); }
.btn-reveal { width: 100%; min-height: 64px; font-size: 18px; border-radius: 20px; }
.ratings { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate {
  min-height: 64px; border-radius: 18px; background: var(--surface); box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  transition: transform .1s ease, background-color .15s;
  position: relative; overflow: hidden;
}
.rate::before { content: ""; position: absolute; left: 0; right: 0; top: 0; height: 4px; background: var(--rc); }
.rate small { font: 600 13px/1 var(--font-round); color: var(--rc); font-variant-numeric: tabular-nums; min-height: 13px; }
.rate span { font-size: 15px; font-weight: 600; }
.rate:active { transform: scale(.95); background: var(--surface-2); }
.rate:disabled, .btn-reveal:disabled { opacity: .55; }
.r1 { --rc: var(--r1); } .r2 { --rc: var(--r2); } .r3 { --rc: var(--r3); } .r4 { --rc: var(--r4); }

/* ---------- Lehrer ---------- */
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.pill-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px 0 10px;
  border-radius: 18px; background: var(--brand); color: var(--brand-ink); font-size: 15px; font-weight: 600;
}
.pill-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.stand { font-size: 14px; color: var(--ink-3); margin-right: 8px; }
.loading-note, .muted { color: var(--ink-3); text-align: center; padding: 18px 0; margin: 0; }
.muted { text-align: left; padding: 4px 0; }
.fineprint { font-size: 13px; color: var(--ink-3); margin: 16px 4px 0; line-height: 1.45; }

.student-list { display: grid; gap: 12px; }
.s-card {
  display: block; width: 100%; text-align: left; background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 16px 14px; transition: transform .12s;
}
.s-card:active { transform: scale(.985); }
.s-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.s-head strong { font: 700 22px/1.2 var(--font-round); letter-spacing: -.01em; }
.s-err { color: var(--learn); margin: 0; }
.s-figs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.s-figs b { display: block; font: 600 22px/1.1 var(--font-round); font-variant-numeric: tabular-nums; }
.s-figs span { font-size: 12px; color: var(--ink-3); }
.mini-bars { display: flex; align-items: flex-end; gap: 3px; height: 34px; margin: 14px 0 10px; border-bottom: 1px solid var(--line); }
.mini-bars i { flex: 1; background: var(--ink-3); opacity: .45; border-radius: 3px 3px 0 0; min-height: 0; }
.mini-bars i.now { background: var(--brand); opacity: 1; }
.s-foot { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); }
.s-flags { font-weight: 600; color: var(--ink); }

.badge {
  display: inline-flex; align-items: center; gap: 5px; height: 26px; padding: 0 10px 0 8px; border-radius: 13px;
  font-size: 13px; font-weight: 600; white-space: nowrap; flex: none;
}
.badge svg { width: 15px; height: 15px; fill: currentColor; }
.badge svg .cut { fill: none; stroke: var(--surface); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.b-red { background: color-mix(in srgb, var(--learn) 16%, transparent); color: var(--learn); }
.b-amber { background: color-mix(in srgb, var(--r2) 18%, transparent); color: color-mix(in srgb, var(--r2) 75%, var(--ink)); }
.b-ok { background: color-mix(in srgb, var(--review) 14%, transparent); color: var(--review); }

#student-detail { padding-top: 8px; }
.detail-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); margin: 0 2px 12px; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); padding: 12px 14px; }
.tile span { display: block; font-size: 13px; color: var(--ink-2); }
.tile b { display: block; font: 600 26px/1.2 var(--font-round); font-variant-numeric: tabular-nums; margin: 2px 0; }
.tile small { font-size: 12px; color: var(--ink-3); }
.note { background: color-mix(in srgb, var(--r2) 14%, var(--surface)); border-radius: 14px; padding: 10px 14px; margin: 12px 0 0; font-size: 14px; }
.panel { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; margin-top: 12px; }
.panel h2 { font-size: 17px; font-weight: 650; margin: 0 0 10px; display: flex; justify-content: space-between; align-items: baseline; }
.panel h2 small { font-size: 13px; color: var(--ink-3); font-weight: 500; }
.panel .fineprint { margin: 10px 0 0; }

.day-chart { width: 100%; height: auto; display: block; overflow: visible; }
.day-chart .grid { stroke: var(--line); stroke-width: 1; }
.day-chart .tick { font: 11px var(--font); fill: var(--ink-3); }
.day-chart .bar { fill: var(--ink-3); opacity: .55; }
.day-chart .bar.now { fill: var(--brand); opacity: 1; }
.day-chart .hit { fill: transparent; cursor: pointer; }
.day-chart .hit.on { fill: var(--surface-2); opacity: .5; }
.chart-tip { font-size: 13px; color: var(--ink-2); margin: 6px 0 0; min-height: 18px; }

.btn-bars { display: grid; gap: 8px; }
.bb-row { display: grid; grid-template-columns: 72px 1fr 52px; align-items: center; gap: 10px; font-size: 14px; }
.bb-name { color: var(--ink-2); }
.bb-track { height: 12px; background: var(--surface-2); border-radius: 6px; overflow: hidden; }
.bb-track i { display: block; height: 100%; background: var(--brand); border-radius: 0 4px 4px 0; }
.bb-val { white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }

.sessions { list-style: none; margin: 0; padding: 0; }
.sess { padding: 10px 0; border-top: 1px solid var(--line); }
.sess:first-child { border-top: 0; padding-top: 2px; }
.sess-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 15px; }
.sess-meta { font-size: 13px; color: var(--ink-2); margin-top: 3px; }
.reasons { margin: 6px 0 0; padding-left: 18px; font-size: 13px; }
.reasons li { margin: 2px 0; }
.mini-decks { list-style: none; margin: 0; padding: 0; }
.mini-decks li { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); font-size: 15px; }
.mini-decks li:first-child { border-top: 0; }

/* ---------- Sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .32); z-index: 20; animation: fade .2s ease; }
.sheet {
  position: fixed; left: 8px; right: 8px; bottom: calc(var(--safe-b) + 8px); z-index: 21;
  background: var(--surface); border-radius: 28px; padding: 8px 8px 8px;
  box-shadow: 0 20px 60px -20px rgba(0, 0, 0, .5);
  animation: rise .26s cubic-bezier(.2, .9, .3, 1);
  max-height: 80dvh; overflow-y: auto;
}
.sheet-grip { width: 36px; height: 5px; border-radius: 3px; background: var(--surface-2); margin: 2px auto 8px; }
.sheet-title { font-size: 13px; font-weight: 600; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; margin: 6px 14px 8px; }
.sheet-text { color: var(--ink-2); margin: 0 14px 14px; font-size: 15px; }
.sheet-item {
  display: flex; align-items: center; gap: 14px; width: 100%; min-height: 54px; padding: 0 14px;
  border-radius: 16px; text-align: left; font-size: 17px;
}
.sheet-item:active { background: var(--surface-2); }
.sheet-item:disabled { opacity: .4; }
.sheet-item svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; color: var(--ink-2); }
.sheet-item.danger { color: var(--learn); }
.sheet-item.danger svg { color: var(--learn); }
.sheet-sep { height: 1px; background: var(--line); margin: 6px 14px; }
.flags { display: flex; gap: 8px; padding: 4px 14px 12px; flex-wrap: wrap; }
.flag-dot { width: 38px; height: 38px; border-radius: 19px; display: grid; place-items: center; background: var(--surface-2); }
.flag-dot i { width: 18px; height: 18px; border-radius: 9px; display: block; }
.flag-dot.on { box-shadow: 0 0 0 2.5px var(--ink); }
.sheet .btn { width: 100%; margin-top: 6px; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; top: calc(var(--safe-t) + 10px); transform: translate(-50%, -20px);
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 22px;
  font-size: 15px; font-weight: 500; opacity: 0; pointer-events: none; z-index: 30;
  transition: opacity .2s, transform .25s cubic-bezier(.2, .9, .3, 1); max-width: calc(100% - 40px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 700px) {
  .view { align-items: center; }
  .view > * { width: 100%; max-width: 640px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
