:root {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #eef1f5; --text: #17202b; --muted: #5d6877;
  --border: #dde2e9; --accent: #2f6fde; --accent-text: #ffffff; --accent-soft: #e3ecfc;
  --good: #1f8f4e; --good-soft: #def3e6; --bad: #c8382f; --bad-soft: #fbe3e1; --warn: #b7791f; --warn-soft: #fdf0d9;
  --again: #c8382f; --hard: #b7791f; --goodr: #1f8f4e; --easy: #2f6fde;
  --radius: 14px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 10px rgba(0,0,0,.04);
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1318; --surface: #181e26; --surface-2: #222a35; --text: #e8edf3; --muted: #9aa6b5;
    --border: #2c3542; --accent: #5b8ff0; --accent-soft: #1d2b45; --good: #43c07a; --good-soft: #15301f;
    --bad: #ef6a60; --bad-soft: #3a1b19; --warn: #e0a84a; --warn-soft: #3a2c14; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1318; --surface: #181e26; --surface-2: #222a35; --text: #e8edf3; --muted: #9aa6b5;
  --border: #2c3542; --accent: #5b8ff0; --accent-soft: #1d2b45; --good: #43c07a; --good-soft: #15301f;
  --bad: #ef6a60; --bad-soft: #3a1b19; --warn: #e0a84a; --warn-soft: #3a2c14; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
  min-height: 100vh; padding-bottom: calc(24px + var(--safe-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.topbar {
  position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  padding: calc(10px + var(--safe-top)) 16px 10px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .back { font-size: 17px; }
main { max-width: 920px; margin: 0 auto; padding: 16px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 11px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 550; text-decoration: none; color: var(--text);
  user-select: none; -webkit-user-select: none;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn.small { min-height: 34px; padding: 0 11px; font-size: 15px; border-radius: 9px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; pointer-events: none; }
.icon-btn { min-width: 44px; padding: 0 10px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px;
}
.card h2 { font-size: 18px; margin: 0 0 8px; }
.card h3 { font-size: 16px; margin: 0 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.spacer { flex: 1; }
.hidden { display: none !important; }

.topic-tile { cursor: pointer; display: block; color: inherit; text-decoration: none; }
.topic-tile .name { font-size: 20px; font-weight: 650; }
.pill {
  display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 13px; font-weight: 600;
  background: var(--surface-2); color: var(--muted);
}
.pill.due { background: var(--accent-soft); color: var(--accent); }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }

.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--good); border-radius: 99px; }

.banner { border-radius: 12px; padding: 12px 14px; margin-bottom: 14px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.banner.warn { background: var(--warn-soft); color: var(--text); }
.banner.info { background: var(--accent-soft); }

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 14px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=number], input[type=password], input[type=search], select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-size: 17px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 11px; padding: 3px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; min-height: 38px; padding: 0 13px; border-radius: 9px; cursor: pointer; font-weight: 550; }
.seg button.on { background: var(--surface); box-shadow: var(--shadow); }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; }
.switch input { width: 51px; height: 31px; appearance: none; -webkit-appearance: none; background: var(--surface-2); border-radius: 99px; position: relative; cursor: pointer; border: 1px solid var(--border); flex-shrink: 0; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 25px; height: 25px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: left .15s; }
.switch input:checked { background: var(--good); border-color: var(--good); }
.switch input:checked::after { left: 22px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 99px; padding: 7px 13px; cursor: pointer; font-size: 15px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* study */
.study-wrap { max-width: 760px; margin: 0 auto; }
.progress-line { height: 4px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-bottom: 14px; }
.progress-line > span { display: block; height: 100%; background: var(--accent); transition: width .25s; }
.timer { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 10px 0 0; }
.timer > span { display: block; height: 100%; background: var(--warn); transform-origin: left; }
.q-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.q-text { font-size: 22px; font-weight: 600; line-height: 1.35; margin: 4px 0 16px; white-space: pre-wrap; }
.q-sub { font-size: 15px; color: var(--muted); margin-bottom: 6px; }
.cloze { font-size: 21px; line-height: 2.2; }
.cloze input {
  display: inline-block; width: auto; min-width: 90px; min-height: 38px; padding: 4px 8px; font-size: 19px;
  border: 0; border-bottom: 2px solid var(--accent); border-radius: 6px 6px 0 0; background: var(--accent-soft); margin: 0 2px;
}
.cloze input.ok { border-color: var(--good); background: var(--good-soft); }
.cloze input.bad { border-color: var(--bad); background: var(--bad-soft); text-decoration: line-through; }
.fix { color: var(--good); font-weight: 650; margin: 0 4px; }
.choices { display: grid; gap: 10px; }
.choice {
  text-align: left; padding: 14px 16px; border-radius: 12px; border: 1.5px solid var(--border); background: var(--surface);
  cursor: pointer; font-size: 18px; display: flex; gap: 12px; align-items: flex-start; min-height: 54px;
}
.choice .k { font-weight: 700; color: var(--muted); min-width: 18px; }
.choice.sel { border-color: var(--accent); background: var(--accent-soft); }
.choice.right { border-color: var(--good); background: var(--good-soft); }
.choice.wrong { border-color: var(--bad); background: var(--bad-soft); }
.tf { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.tf .choice { justify-content: center; font-weight: 650; }
.feedback { border-radius: 12px; padding: 12px 14px; margin-top: 14px; }
.feedback.ok { background: var(--good-soft); }
.feedback.no { background: var(--bad-soft); }
.feedback .title { font-weight: 700; margin-bottom: 4px; }
.explain { margin-top: 8px; font-size: 16px; color: var(--text); opacity: .9; }
.ratings { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.ratings button { min-height: 58px; border-radius: 12px; border: 1.5px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 650; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ratings button small { font-weight: 500; color: var(--muted); font-size: 12px; }
.ratings button.r1 { color: var(--again); } .ratings button.r2 { color: var(--hard); }
.ratings button.r3 { color: var(--goodr); } .ratings button.r4 { color: var(--easy); }
.ratings button.default { border-color: currentColor; box-shadow: inset 0 0 0 1px currentColor; }
.study-actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.kbd-hint { font-size: 13px; color: var(--muted); margin-top: 12px; }
@media (hover: none) { .kbd-hint { display: none; } }

/* lists */
.list { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.list .item { padding: 12px 14px; border-top: 1px solid var(--border); display: flex; gap: 12px; align-items: center; }
.list .item:first-child { border-top: 0; }
.list .item .main { flex: 1; min-width: 0; }
.list .item .title { font-weight: 600; }
.list .item .sub { font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.list .item.clickable { cursor: pointer; }
.list .item input[type=checkbox] { width: 22px; height: 22px; flex-shrink: 0; }

.stat { text-align: center; }
.stat .n { font-size: 28px; font-weight: 700; }
.stat .l { font-size: 13px; color: var(--muted); }
.stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
.spark { display: flex; align-items: flex-end; gap: 4px; height: 80px; }
.spark > div { flex: 1; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; opacity: .85; }
.spark-labels { display: flex; gap: 4px; font-size: 11px; color: var(--muted); }
.spark-labels > div { flex: 1; text-align: center; }

/* modal + toast */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 640px) { .modal-bg { align-items: center; } }
.modal {
  background: var(--surface); width: 100%; max-width: 620px; max-height: 90vh; overflow: auto;
  border-radius: 18px 18px 0 0; padding: 18px 18px calc(18px + var(--safe-bottom));
}
@media (min-width: 640px) { .modal { border-radius: 18px; } }
.modal h2 { margin-top: 0; }
.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 60; max-width: 90vw;
  box-shadow: 0 4px 20px rgba(0,0,0,.2); font-size: 15px;
}
.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.dup { outline: 2px dashed var(--warn); }
.spinner { width: 18px; height: 18px; border: 2.5px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
details summary { cursor: pointer; font-weight: 600; min-height: 32px; }
