:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #e9e9ee;
  --pressed: #d9d9de;
  --text: #000000;
  --text-2: #6c6c70;
  --text-3: #8e8e93;
  --sep: rgba(60, 60, 67, 0.2);
  --accent: #2459e0;
  --on-accent: #ffffff;
  --danger: #d70015;
  --new: #0a64d6;
  --learn: #b85000;
  --due: #1b7a33;
  --again: #d1302c;
  --hard: #b85c00;
  --good: #1e8a3a;
  --easy: #1f5fd0;
  --series-1: #2a78d6;
  --grid: rgba(60, 60, 67, 0.16);
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --pressed: #3a3a3c;
    --text: #ffffff;
    --text-2: #98989f;
    --text-3: #7c7c80;
    --sep: rgba(84, 84, 88, 0.65);
    --accent: #5b8cff;
    --danger: #ff453a;
    --new: #4ea1ff;
    --learn: #ffa340;
    --due: #3fd06b;
    --again: #d8403c;
    --hard: #c46a0c;
    --good: #23913f;
    --easy: #2f6ce0;
    --series-1: #3987e5;
    --grid: rgba(235, 235, 245, 0.14);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.35 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
button, input, textarea, select { font: inherit; color: inherit; }
button { touch-action: manipulation; }
.boot { padding: 40vh 0 0; text-align: center; color: var(--text-2); }

/* ---- screens & navigation */
.screen { min-height: 100dvh; padding-bottom: calc(32px + env(safe-area-inset-bottom)); }
.nav {
  position: sticky; top: 0; z-index: 20;
  display: grid; align-items: center;
  grid-template-columns: minmax(max-content, 1fr) minmax(0, auto) minmax(max-content, 1fr);
  min-height: calc(44px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 8px 0;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
}
.nav-title {
  font-weight: 600; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  min-width: 0; padding: 0 4px;
}
.nav-left, .nav-right { display: flex; align-items: center; gap: 2px; min-width: 0; }
.nav-right { justify-content: flex-end; }
.nav-btn {
  display: inline-flex; align-items: center; gap: 2px;
  min-height: 44px; min-width: 44px; padding: 0 8px;
  background: none; border: 0; color: var(--accent); cursor: pointer; white-space: nowrap;
}
.nav-btn:disabled { color: var(--text-3); cursor: default; }
.nav-btn.strong { font-weight: 600; }
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn.back { padding-left: 2px; }
.nav-btn.back svg { width: 24px; height: 24px; margin-right: -2px; }
.large-title { font-size: 34px; font-weight: 700; letter-spacing: 0.01em; margin: 2px 20px 14px; }

/* ---- grouped lists */
.section { margin: 0 16px 26px; }
.section-header {
  font-size: 13px; text-transform: uppercase; letter-spacing: 0.02em;
  color: var(--text-2); margin: 0 16px 7px;
}
.section-footer { font-size: 13px; color: var(--text-2); margin: 7px 16px 0; }
.group { background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.row {
  position: relative; display: flex; align-items: center; gap: 12px;
  width: 100%; min-height: 44px; padding: 11px 16px;
  background: none; border: 0; text-align: left;
}
.row + .row::before {
  content: ""; position: absolute; top: 0; left: 16px; right: 0;
  border-top: 0.5px solid var(--sep);
}
button.row { cursor: pointer; }
button.row:active:not(:disabled) { background: var(--pressed); }
button.row:disabled { cursor: default; }
.row.action { color: var(--accent); }
.row.destructive { color: var(--danger); }
.row.center { justify-content: center; }
.row-main { flex: 1; min-width: 0; }
.row-title { overflow-wrap: anywhere; }
.row-title.clamp {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.row-sub {
  font-size: 13px; color: var(--text-2); margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.row-value { color: var(--text-2); white-space: nowrap; }
.chevron { width: 14px; height: 14px; color: var(--text-3); flex: none; }
.row svg.lead { width: 22px; height: 22px; flex: none; }
.row input[type="text"], .row input[type="search"], .row input:not([type]) {
  flex: 1; min-width: 0; border: 0; background: none; outline: none; font-size: 17px; padding: 0;
}
.row input.right { text-align: right; color: var(--text-2); }
.row input.invalid { color: var(--danger); }
.row label { flex: 1; }
textarea.field {
  display: block; width: 100%; min-height: 96px; resize: none; border: 0; outline: none;
  background: none; padding: 12px 16px; font-size: 17px; line-height: 1.4;
}

/* ---- counts */
.counts { display: inline-flex; gap: 12px; font-variant-numeric: tabular-nums; font-size: 16px; }
.c-new { color: var(--new); }
.c-learn { color: var(--learn); }
.c-due { color: var(--due); }
.dots { display: inline-flex; gap: 4px; margin-left: auto; padding-left: 8px; }
.dot { width: 8px; height: 8px; border-radius: 50%; }
.dot.s0 { background: var(--new); }
.dot.s1, .dot.s3 { background: var(--learn); }
.dot.s2 { background: var(--due); }

/* ---- controls */
.segmented {
  display: grid; grid-auto-columns: 1fr; grid-auto-flow: column; gap: 2px;
  padding: 2px; border-radius: 9px; background: var(--surface-2); margin: 0 0 18px;
}
.segmented button {
  border: 0; background: none; border-radius: 7px; padding: 6px 4px; font-size: 14px; cursor: pointer;
}
.segmented button[aria-pressed="true"] {
  background: var(--surface); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}
.switch { appearance: none; -webkit-appearance: none; position: relative; flex: none;
  width: 51px; height: 31px; border-radius: 16px; background: var(--surface-2); cursor: pointer; margin: 0;
  transition: background 0.2s; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px;
  border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.25); transition: transform 0.2s; }
.switch:checked { background: #34c759; }
.switch:checked::after { transform: translateX(20px); }
.stepper { display: inline-flex; background: var(--surface-2); border-radius: 9px; flex: none; }
.stepper button { width: 46px; height: 32px; border: 0; background: none; font-size: 20px; cursor: pointer; }
.stepper button + button { border-left: 1px solid var(--sep); }
.stepper button:disabled { color: var(--text-3); }
.pill-btn {
  border: 0; border-radius: 999px; padding: 7px 14px; font-size: 15px; cursor: pointer;
  background: var(--surface-2); color: var(--accent);
}
.pill-btn:disabled { color: var(--text-3); cursor: default; }
.primary-btn {
  display: block; width: 100%; border: 0; border-radius: 14px; padding: 15px; cursor: pointer;
  background: var(--accent); color: var(--on-accent); font-weight: 600; font-size: 17px;
}
.cloze-bar { display: flex; align-items: center; gap: 8px; padding: 10px 16px 0; flex-wrap: wrap; }
.cloze-bar .hint { margin-left: auto; font-size: 13px; color: var(--text-2); }

/* ---- banners & empty states */
.banner {
  display: flex; gap: 12px; align-items: flex-start;
  margin: 0 16px 20px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--surface); font-size: 15px;
}
.banner .banner-text { flex: 1; }
.banner .banner-text strong { display: block; margin-bottom: 2px; }
.banner .x { border: 0; background: none; color: var(--text-3); padding: 0 2px; cursor: pointer; font-size: 20px; line-height: 1; }
.empty { text-align: center; padding: 18vh 32px 0; color: var(--text-2); }
.empty svg { width: 56px; height: 56px; color: var(--text-3); }
.empty h2 { color: var(--text); font-size: 22px; margin: 12px 0 6px; }
.empty .primary-btn { margin-top: 22px; }
.empty .link-btn { margin-top: 12px; border: 0; background: none; color: var(--accent); cursor: pointer; padding: 10px; }

/* ---- study */
.study { display: flex; flex-direction: column; height: 100dvh; background: var(--surface); }
.study .nav { background: color-mix(in srgb, var(--surface) 85%, transparent); }
.study-status {
  display: flex; align-items: center; gap: 14px; padding: 4px 20px 8px;
  font-variant-numeric: tabular-nums; font-size: 15px;
}
.study-status .done { margin-left: auto; color: var(--text-2); }
.card-area {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 28px 24px 40px; text-align: center; cursor: pointer;
}
.card-text { font-size: 24px; line-height: 1.4; white-space: pre-wrap; overflow-wrap: anywhere; }
.card-extra { font-size: 18px; color: var(--text-2); white-space: pre-wrap; overflow-wrap: anywhere; }
.card-divider { border: 0; border-top: 1px solid var(--sep); margin: 26px 0; }
.cloze-blank { color: var(--accent); font-weight: 700; }
.answer-bar {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
}
.answer-bar.single { grid-template-columns: 1fr; }
.rate-btn {
  border: 0; border-radius: 14px; padding: 10px 2px 9px; cursor: pointer; color: #fff;
  display: flex; flex-direction: column; align-items: center; gap: 1px; min-height: 60px; justify-content: center;
}
.rate-btn b { font-size: 17px; }
.rate-btn small { font-size: 13px; opacity: 0.92; font-variant-numeric: tabular-nums; }
.rate-btn:active { filter: brightness(0.88); }
.r1 { background: var(--again); } .r2 { background: var(--hard); }
.r3 { background: var(--good); } .r4 { background: var(--easy); }
.show-btn {
  border: 0; border-radius: 14px; min-height: 60px; cursor: pointer;
  background: var(--accent); color: var(--on-accent); font-weight: 600; font-size: 17px;
}
.finished { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px; color: var(--text-2); }
.finished svg { width: 56px; height: 56px; color: var(--due); }
.finished h2 { color: var(--text); margin: 14px 0 6px; font-size: 24px; }
.finished .primary-btn { margin-top: 28px; max-width: 320px; }

/* ---- charts */
.chart { padding: 14px 16px 12px; }
.chart-readout { font-size: 15px; min-height: 22px; margin-bottom: 8px; font-variant-numeric: tabular-nums; }
.chart-readout .muted { color: var(--text-2); }
.chart-plot { position: relative; height: 128px; border-bottom: 1px solid var(--text-3); }
.chart-plot .gridline { position: absolute; left: 0; right: 0; top: 0; border-top: 1px dashed var(--grid); }
.chart-plot .ymax { position: absolute; right: 0; top: -2px; transform: translateY(-100%);
  font-size: 11px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.bars { position: absolute; inset: 0; display: flex; align-items: flex-end; gap: 2px; }
.bar-col {
  flex: 1; height: 100%; display: flex; align-items: flex-end; justify-content: center;
  border: 0; background: none; padding: 0; cursor: pointer; border-radius: 4px 4px 0 0;
}
.bar-col.active { background: var(--grid); }
.bar { display: block; width: 100%; max-width: 22px; background: var(--series-1); border-radius: 4px 4px 0 0; }
.chart-x { display: flex; justify-content: space-between; font-size: 11px; color: var(--text-2); margin-top: 5px; }
.chart details { margin-top: 10px; font-size: 13px; color: var(--text-2); }
.chart summary { cursor: pointer; }
.chart table { width: 100%; border-collapse: collapse; margin-top: 6px; font-variant-numeric: tabular-nums; }
.chart td { padding: 2px 0; border-top: 0.5px solid var(--sep); color: var(--text); }
.chart td:last-child { text-align: right; }

/* ---- overlays */
#overlay { position: fixed; inset: 0; z-index: 50; display: none; }
#overlay.open { display: block; }
.backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.35); display: flex; animation: fade 0.15s; }
.backdrop.center { align-items: center; justify-content: center; padding: 24px; }
.backdrop.bottom { align-items: flex-end; justify-content: center;
  padding: 0 8px calc(8px + env(safe-area-inset-bottom)); }
.dialog {
  width: min(300px, 100%); background: var(--surface); border-radius: 16px; overflow: hidden;
  text-align: center; animation: pop 0.15s;
}
.dialog-body { padding: 20px 16px 16px; }
.dialog-body h3 { margin: 0 0 4px; font-size: 17px; }
.dialog-body p { margin: 0; font-size: 14px; color: var(--text-2); }
.dialog-body input {
  width: 100%; margin-top: 14px; padding: 9px 10px; font-size: 16px; border-radius: 8px;
  border: 1px solid var(--sep); background: var(--bg); outline: none;
}
.dialog-actions { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border-top: 0.5px solid var(--sep); }
.dialog-actions button { border: 0; background: none; padding: 12px; color: var(--accent); cursor: pointer; font-size: 17px; }
.dialog-actions button + button { border-left: 0.5px solid var(--sep); }
.dialog-actions button.strong { font-weight: 600; }
.dialog-actions button.destructive { color: var(--danger); }
.dialog-actions button:disabled { color: var(--text-3); }
.sheet { width: min(500px, 100%); display: flex; flex-direction: column; gap: 8px; animation: rise 0.18s; }
.sheet .group button { width: 100%; justify-content: center; color: var(--accent); font-size: 19px; min-height: 56px; }
.sheet .group button.destructive { color: var(--danger); }
.sheet .cancel { font-weight: 600; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(1.08); opacity: 0; } }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 14px;
  background: rgba(30,30,32,0.92); color: #fff; font-size: 15px; text-align: center;
  transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media (min-width: 700px) {
  .screen, .study { max-width: 680px; margin: 0 auto; }
}
