:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #15181d;
  --muted: #5d6570;
  --border: #dde1e6;
  --accent: #c8321f;
  --accent-ink: #ffffff;
  --accent-soft: #fbe7e3;
  --new: #2f6fde;
  --learn: #d0442c;
  --review: #1f8a4c;
  --again: #d0442c;
  --hard: #b7791f;
  --good: #1f8a4c;
  --easy: #2f6fde;
  --cloze: #2f6fde;
  --cloze-bg: #e6eefc;
  --lvl-E: #99ccff;
  --lvl-A85: #c4d79b;
  --lvl-A: #fff200;
  --lvl-P: #ffc000;
  --lvl-ink: #1b1b1b;
  --chart-1: #2f6fde;
  --st-new: #9aa6b5;
  --st-learning: #d08a1f;
  --st-young: #7fc8a0;
  --st-mature: #1f8a4c;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --surface: #181b21; --surface-2: #22262e; --text: #eceef1; --muted: #9aa3ae;
    --border: #2c313a; --accent: #ef5a43; --accent-soft: #3a1f1b; --new: #6d9cf0; --learn: #f07a63;
    --review: #4cc37f; --again: #f07a63; --hard: #e0a84a; --good: #4cc37f; --easy: #6d9cf0;
    --cloze: #8fb3f5; --cloze-bg: #1f2b44; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
    --chart-1: #6d9cf0; --st-new: #5c6573; --st-learning: #e0a84a; --st-young: #2f7a52; --st-mature: #5fd393;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #181b21; --surface-2: #22262e; --text: #eceef1; --muted: #9aa3ae;
  --border: #2c313a; --accent: #ef5a43; --accent-soft: #3a1f1b; --new: #6d9cf0; --learn: #f07a63;
  --review: #4cc37f; --again: #f07a63; --hard: #e0a84a; --good: #4cc37f; --easy: #6d9cf0;
  --cloze: #8fb3f5; --cloze-bg: #1f2b44; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  --chart-1: #6d9cf0; --st-new: #5c6573; --st-learning: #e0a84a; --st-young: #2f7a52; --st-mature: #5fd393;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.45;
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }

#app { max-width: 720px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }
main { flex: 1; padding: 16px 16px calc(96px + env(safe-area-inset-bottom)); }
main.study { padding-bottom: 16px; display: flex; flex-direction: column; min-height: 100dvh; }

/* Header */
.topbar { display: flex; align-items: center; gap: 12px; padding: calc(12px + env(safe-area-inset-top)) 16px 4px; }
.topbar h1 { font-size: 1.5rem; margin: 0; letter-spacing: -.01em; flex: 1; }
.topbar .sub { color: var(--muted); font-size: .85rem; }
.iconbtn { width: 40px; height: 40px; border-radius: 12px; border: 0; background: transparent; display: grid; place-items: center; }
.iconbtn:hover { background: var(--surface-2); }
.iconbtn svg { width: 22px; height: 22px; }

/* Bottom nav */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center;
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px);
  border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); z-index: 10;
}
.tabbar nav { display: flex; width: 100%; max-width: 720px; }
.tabbar a { flex: 1; text-decoration: none; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 10px 0 8px; font-size: .75rem; font-weight: 600; }
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.on { color: var(--accent); }

/* Cards / surfaces */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.hero { padding: 20px; margin-bottom: 20px; }
.hero .kpis { display: flex; gap: 20px; margin: 4px 0 16px; }
.kpi b { display: block; font-size: 1.75rem; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi span { font-size: .8rem; color: var(--muted); font-weight: 600; }
.kpi.new b { color: var(--new); } .kpi.learn b { color: var(--learn); } .kpi.review b { color: var(--review); }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: .8rem; font-weight: 600; }
.chip.streak { background: var(--accent-soft); color: var(--accent); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 700; background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.block { width: 100%; padding: 14px; font-size: 1.05rem; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.ghost { background: transparent; border: 1px solid var(--border); }
.btn.danger { color: var(--again); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

h2.section { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 24px 4px 10px; }

.decklist { display: flex; flex-direction: column; gap: 10px; }
.deck { display: flex; align-items: center; gap: 14px; padding: 14px 16px; text-decoration: none; color: inherit; }
.deck .code { width: 44px; height: 44px; flex: none; border-radius: 12px; display: grid; place-items: center; font-weight: 800; font-size: .85rem; background: var(--accent-soft); color: var(--accent); }
.deck .info { flex: 1; min-width: 0; }
.deck .name { font-weight: 700; }
.deck .meta { font-size: .8rem; color: var(--muted); }
.bar { height: 4px; background: var(--surface-2); border-radius: 4px; margin-top: 8px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--review); border-radius: 4px; }
.counts { display: flex; gap: 10px; font-weight: 700; font-variant-numeric: tabular-nums; font-size: .95rem; }
.counts .n { color: var(--new); } .counts .l { color: var(--learn); } .counts .r { color: var(--review); }
.counts .z { color: var(--muted); opacity: .5; }
.chev { color: var(--muted); }

.proto { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--border); text-decoration: none; color: inherit; }
.proto:first-child { border-top: 0; }
.proto .pcode { font-weight: 800; font-size: .8rem; color: var(--muted); width: 52px; flex: none; }
.proto .ptitle { flex: 1; font-weight: 600; }

/* Level badge */
.lvl { display: inline-flex; align-items: center; justify-content: center; min-width: 30px; padding: 2px 8px; border-radius: 6px; font-size: .75rem; font-weight: 800; color: var(--lvl-ink); }
.lvl-E { background: var(--lvl-E); } .lvl-A85 { background: var(--lvl-A85); } .lvl-A { background: var(--lvl-A); } .lvl-P { background: var(--lvl-P); }

/* Study */
.studybar { display: flex; align-items: center; gap: 8px; padding-top: env(safe-area-inset-top); }
.studybar .counts { flex: 1; justify-content: center; }
.studybar .counts .cur { text-decoration: underline; text-underline-offset: 4px; text-decoration-thickness: 2px; }
.progress { height: 3px; background: var(--surface-2); border-radius: 3px; margin: 6px 0 14px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); transition: width .3s; }
.flash { flex: 1; display: flex; flex-direction: column; padding: 20px; cursor: pointer; user-select: text; }
.flash .tags { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; }
.flash .proto-tag { font-size: .8rem; font-weight: 700; color: var(--muted); }
.flash .q { font-size: 1.25rem; line-height: 1.5; }
.flash .a { font-size: 1.25rem; line-height: 1.5; }
.flash hr { border: 0; border-top: 1px dashed var(--border); margin: 18px 0; width: 100%; }
.flash .extra { margin-top: 16px; padding: 12px 14px; background: var(--surface-2); border-radius: 12px; font-size: .95rem; color: var(--text); }
.flash .src { margin-top: auto; padding-top: 18px; font-size: .78rem; color: var(--muted); }
.cloze { font-weight: 800; color: var(--cloze); background: var(--cloze-bg); padding: 0 6px; border-radius: 6px; }
.tap-hint { text-align: center; color: var(--muted); font-size: .85rem; margin-top: 12px; }
.answers { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; padding-bottom: env(safe-area-inset-bottom); }
.answers button { border: 0; border-radius: 14px; padding: 10px 4px 12px; background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--border); display: flex; flex-direction: column; align-items: center; gap: 2px; }
.answers button b { font-size: 1rem; }
.answers button small { font-size: .75rem; color: var(--muted); font-weight: 600; }
.answers .again b { color: var(--again); } .answers .hard b { color: var(--hard); } .answers .good b { color: var(--good); } .answers .easy b { color: var(--easy); }
.answers button:active { transform: scale(.97); }
.reveal { margin-top: 14px; padding-bottom: env(safe-area-inset-bottom); }
.cram-tag { font-size: .75rem; font-weight: 800; color: var(--hard); text-transform: uppercase; letter-spacing: .05em; }

.done { text-align: center; padding: 40px 20px; }
.done .big { font-size: 3rem; }
.done h2 { margin: 8px 0; }
.done p { color: var(--muted); }

/* Browse */
.search { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; }
.filters { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0; scrollbar-width: none; }
.filters button { flex: none; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: .85rem; font-weight: 600; color: var(--muted); }
.filters button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.result { padding: 14px 16px; border-top: 1px solid var(--border); }
.result:first-child { border-top: 0; }
.result .head { display: flex; gap: 8px; align-items: center; font-size: .8rem; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.result .body { font-size: .95rem; }
.result .body .ans { color: var(--muted); margin-top: 4px; }
.result .suspended { color: var(--hard); }
.more { text-align: center; color: var(--muted); padding: 14px; font-size: .85rem; }

/* Settings */
.setting { padding: 14px 16px; border-top: 1px solid var(--border); }
.setting:first-child { border-top: 0; }
.setting label { font-weight: 700; display: block; }
.setting .help { font-size: .82rem; color: var(--muted); margin: 2px 0 10px; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 8px 4px; border-radius: 9px; font-weight: 700; font-size: .9rem; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.stepper { display: flex; align-items: center; gap: 12px; }
.stepper button { width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); font-size: 1.2rem; font-weight: 700; }
.stepper output { font-weight: 800; font-size: 1.1rem; min-width: 48px; text-align: center; font-variant-numeric: tabular-nums; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.note { font-size: .82rem; color: var(--muted); padding: 0 4px; }

/* Onboarding */
.onboard { padding: 32px 20px; }
.onboard h1 { font-size: 1.9rem; margin: 0 0 8px; letter-spacing: -.02em; }
.onboard p { color: var(--muted); }
.levelpick { display: flex; flex-direction: column; gap: 10px; margin: 18px 0; }
.levelpick button { display: flex; align-items: center; gap: 14px; text-align: left; padding: 14px 16px; border-radius: 14px; border: 2px solid var(--border); background: var(--surface); }
.levelpick button.on { border-color: var(--accent); }
.levelpick .lvl { min-width: 44px; padding: 6px; font-size: .9rem; }
.levelpick b { display: block; }
.levelpick small { color: var(--muted); }
.disclaimer { background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; font-size: .88rem; }

/* Toast sits at the top so it never covers the answer buttons */
.toast { position: fixed; left: 50%; top: calc(12px + env(safe-area-inset-top)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: .9rem; z-index: 60; box-shadow: var(--shadow); max-width: calc(100% - 32px); text-align: center; }
/* Overflow menu anchored to the study bar */
.studybar { position: relative; }
.menu { position: absolute; right: 0; top: 46px; z-index: 20; min-width: 220px; padding: 6px; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 11px 12px; border-radius: 8px; font-weight: 600; }
.menu button:hover, .menu button:focus-visible { background: var(--surface-2); }
.iconbtn:disabled { opacity: .3; cursor: default; }
.flagmark { color: var(--hard); display: inline-flex; margin-left: auto; }
.answers.two { grid-template-columns: 1fr 1fr; }
.btn.small { padding: 6px 10px; font-size: .8rem; border-radius: 9px; }
a.btn { text-decoration: none; color: inherit; }
a.btn.primary { color: var(--accent-ink); }
.done-actions { display: flex; flex-direction: column; gap: 10px; align-items: center; margin-top: 16px; }
.proto { padding-right: 12px; }
.proto-main { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; text-decoration: none; color: inherit; }

/* Formulary-vs-protocol conflict warning */
.conflict { margin-top: 12px; padding: 12px 14px; border-radius: 12px; border: 1px solid color-mix(in srgb, var(--hard) 55%, transparent); background: color-mix(in srgb, var(--hard) 12%, var(--surface)); font-size: .92rem; }
.conflict b { display: block; color: var(--hard); margin-bottom: 4px; }
.conflict-foot { margin-top: 6px; font-size: .78rem; color: var(--muted); }

/* Accessibility */
:focus-visible { outline: 3px solid var(--cloze); outline-offset: 2px; border-radius: 6px; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; } }

/* Beta review */
.beta { display: inline-block; vertical-align: middle; font-size: .65rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; padding: 3px 7px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); }
.linkbtn { border: 0; background: none; padding: 8px 0 0; color: var(--accent); font-weight: 700; font-size: .82rem; text-decoration: underline; text-underline-offset: 3px; }
.issue-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chipbtn { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 8px 12px; font-size: .85rem; font-weight: 600; color: var(--text); }
.chipbtn.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chipbtn.good.on { background: var(--good); border-color: var(--good); }
.setting label { margin-top: 2px; }

/* Banners */
.banners { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.banner { border-radius: 12px; padding: 10px 14px; font-size: .88rem; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.banner.info { background: var(--cloze-bg); }
.banner.warn { background: var(--accent-soft); }
.banner .btn { margin-left: auto; }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: flex; align-items: flex-end; justify-content: center; padding: 16px; }
.modal { width: 100%; max-width: 560px; max-height: 85dvh; overflow: auto; padding: 16px; }
@media (min-width: 640px) { .modal-backdrop { align-items: center; } }
.modal-head { display: flex; align-items: center; gap: 8px; }
.modal-head h2 { font-size: 1.1rem; margin: 0; flex: 1; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 12px; }
.help { font-size: .82rem; color: var(--muted); }
.report-text { font-family: ui-monospace, Consolas, monospace; font-size: .8rem; resize: vertical; }
.info-card { border-top: 1px solid var(--border); padding: 12px 0; }
.info-title { margin-bottom: 6px; }
.info-grid { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 0; font-size: .88rem; }
.info-grid dt { color: var(--muted); }
.info-grid dd { margin: 0; font-variant-numeric: tabular-nums; }
.hist { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.hist .h { font-size: .75rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.hist .r1 { color: var(--again); }
.pill { display: inline-block; font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.warn { color: var(--hard); }
.result-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; }

/* Stats */
.pad { padding: 16px; }
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 600px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile { padding: 14px; display: flex; flex-direction: column; gap: 2px; }
.tile span { font-size: .75rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.tile b { font-size: 1.7rem; line-height: 1.15; font-variant-numeric: tabular-nums; }
.tile small { color: var(--muted); font-size: .78rem; }
.stack { display: flex; gap: 2px; height: 16px; border-radius: 4px; overflow: hidden; background: var(--surface-2); }
.stack span { min-width: 3px; }
.seg-new { background: var(--st-new); } .seg-learning { background: var(--st-learning); }
.seg-young { background: var(--st-young); } .seg-mature { background: var(--st-mature); }
.legend { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-top: 10px; font-size: .82rem; color: var(--muted); align-items: center; }
.legend b { color: var(--text); font-variant-numeric: tabular-nums; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.fc { display: flex; align-items: flex-end; gap: 2px; height: 120px; border-bottom: 1px solid var(--border); }
.fc-col { flex: 1; height: 100%; display: flex; align-items: flex-end; }
.fc-col i { display: block; width: 100%; background: var(--chart-1); border-radius: 4px 4px 0 0; }
.fc-col:hover i { opacity: .75; }
.fc-axis { display: flex; justify-content: space-between; font-size: .72rem; color: var(--muted); margin-top: 4px; }
.heatmap { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 1fr); gap: 3px; justify-content: start; }
.hm { display: inline-block; width: 14px; height: 14px; border-radius: 3px; background: var(--surface-2); }
.hm.l1 { background: color-mix(in srgb, var(--chart-1) 30%, var(--surface-2)); }
.hm.l2 { background: color-mix(in srgb, var(--chart-1) 55%, var(--surface-2)); }
.hm.l3 { background: color-mix(in srgb, var(--chart-1) 78%, var(--surface-2)); }
.hm.l4 { background: var(--chart-1); }
.hm.fut { opacity: .3; }
.legend .hm { width: 12px; height: 12px; }
.deckstat { padding: 12px 16px; border-top: 1px solid var(--border); }
.deckstat:first-child { border-top: 0; }
.ds-head { display: flex; justify-content: space-between; gap: 10px; font-size: .9rem; }
.ds-head span { color: var(--muted); font-size: .8rem; font-variant-numeric: tabular-nums; }
.bar.two { position: relative; height: 6px; }
.bar.two i { position: absolute; left: 0; top: 0; height: 100%; }
.bar.two .seen { background: var(--st-young); }
.bar.two .mat { background: var(--st-mature); }
.loading { padding: 60px 20px; text-align: center; color: var(--muted); }
kbd { font-family: inherit; font-size: .7rem; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; color: var(--muted); }
@media (hover: none) { .kbd-hint { display: none; } }
@media (max-width: 360px) { .flash .q, .flash .a { font-size: 1.1rem; } .answers button { padding: 8px 2px 10px; } }
