:root {
  color-scheme: light dark;
  --bg: light-dark(#f6f6f4, #111113);
  --surface: light-dark(#ffffff, #19191d);
  --surface-subtle: light-dark(#eeeeea, #24242a);
  --surface-hover: light-dark(#e5e5e0, #2e2e36);
  --ink: light-dark(#141414, #f4f4f2);
  --muted: light-dark(#64645f, #9e9ea7);
  --border: light-dark(#e4e4df, #2c2c34);
  --border-strong: light-dark(#cbcbc5, #43434f);
  --accent: light-dark(#0f766e, #2dd4bf);
  --accent-soft: light-dark(#e6f4f1, #132d2b);
  --accent-ink: light-dark(#0b5c56, #5eead4);
  --danger: light-dark(#b91c1c, #f87171);
  --danger-soft: light-dark(#fef2f2, #2f1719);
  --topbar-h: 58px;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-synthesis: none;

  &:has(> head > meta[name="color-scheme"][content="dark"]) {
    color-scheme: dark;
  }
  &:has(> head > meta[name="color-scheme"][content="light"]) {
    color-scheme: light;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 300px; min-height: 100dvh; background: var(--bg); color: var(--ink); }
button, input, textarea, select { font: inherit; }
button { color: inherit; cursor: pointer; }
button:disabled { cursor: default; opacity: .5; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button, a { -webkit-tap-highlight-color: transparent; }
svg { width: 18px; height: 18px; flex: 0 0 auto; vertical-align: middle; }
h1, h2, h3, p { margin: 0; }
h1, h2, h3, b, strong { font-weight: 700; }
small { font-size: 12px; }

/* Minimal Top Bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--topbar-h);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 960px;
  height: 100%;
  margin: 0 auto;
  padding-inline: clamp(14px, 3vw, 24px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  text-decoration: none;
  color: var(--ink);
  white-space: nowrap;
}
.brand img { border-radius: 8px; border: 1px solid var(--border); }
.brand strong { font-size: 16px; letter-spacing: -.02em; }

.primary-nav {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px;
  border-radius: 999px;
  background: var(--surface-subtle);
  border: 1px solid var(--border);
}
.primary-nav button {
  min-height: 34px;
  padding: 0 15px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  transition: background .12s, color .12s;
}
.primary-nav button:hover { color: var(--ink); }
.primary-nav button[aria-current="page"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .08);
}

.topbar-actions { display: flex; align-items: center; gap: 8px; }
.pip-cta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: var(--ink);
  color: var(--bg);
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
}
.pip-cta:hover { opacity: .88; }
.pip-cta[aria-pressed="true"] { background: var(--accent); color: #fff; }
.more-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.more-btn:hover { background: var(--surface-subtle); }

/* Centered Single-Column Main Stage */
.page-main {
  max-width: 820px;
  margin: 0 auto;
  padding: clamp(22px, 4vw, 40px) clamp(14px, 3vw, 24px) 64px;
  container-type: inline-size;
}
.page-main:focus { outline: none; }

/* Shared Buttons */
.primary, .secondary, .text-button {
  min-height: 42px;
  border-radius: 999px;
  font-weight: 650;
  font-size: 14px;
  padding: 8px 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
}
.primary { border: 1px solid transparent; background: var(--ink); color: var(--bg); }
.primary:hover:not(:disabled) { opacity: .88; }
.primary.accent-btn { background: var(--accent); color: #fff; }
.primary.danger-btn { background: var(--danger); color: #fff; }
.secondary { border: 1px solid var(--border-strong); background: var(--surface); color: var(--ink); }
.secondary:hover:not(:disabled) { background: var(--surface-subtle); }
.text-button { border: 0; padding: 6px 10px; min-height: 34px; background: transparent; color: var(--muted); font-size: 13px; }
.text-button:hover { color: var(--ink); text-decoration: underline; }

.icon-btn {
  display: grid;
  place-items: center;
  min-inline-size: 36px;
  min-block-size: 36px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
}
.icon-btn:hover:not(:disabled) { background: var(--surface-subtle); border-color: var(--border-strong); }

/* =========================================================
   View 1: Focused Study Stage (Homepage)
   ========================================================= */
.study-wrap {
  max-width: 680px;
  margin: 0 auto;
  display: grid;
  gap: 16px;
}

.study-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.deck-picker {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.deck-select-native {
  min-height: 38px;
  padding: 0 30px 0 13px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-weight: 650;
  cursor: pointer;
}
.study-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.scope-pills {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--surface-subtle);
  border: 1px solid var(--border);
}
.scope-pills button {
  min-height: 30px;
  padding: 0 11px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.scope-pills button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
}

/* Centerpiece Quiz Card */
.quiz-stage {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: clamp(22px, 4vw, 34px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, .04);
}
.quiz-stage-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 22px;
}
.pill-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 11px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-size: 12px;
  font-weight: 650;
}
.quiz-term {
  font-size: clamp(1.75rem, 1.35rem + 1.5vw, 2.5rem);
  line-height: 1.2;
  letter-spacing: -.03em;
  margin-bottom: 10px;
  overflow-wrap: anywhere;
}
.quiz-hint {
  color: var(--muted);
  font-size: 15px;
  line-height: 1.55;
  margin-bottom: 24px;
}
.quiz-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
@container (max-width: 520px) {
  .quiz-options { grid-template-columns: 1fr; }
}
.quiz-opt {
  display: flex;
  align-items: center;
  gap: 11px;
  min-height: 56px;
  padding: 12px 15px;
  border: 1px solid var(--border-strong);
  border-radius: 15px;
  background: var(--surface);
  color: var(--ink);
  text-align: left;
  font-size: 15px;
  line-height: 1.4;
  transition: border-color .12s, background .12s;
}
.quiz-opt:hover:not(:disabled) {
  border-color: var(--ink);
  background: var(--surface-subtle);
}
.quiz-opt .opt-key {
  flex: 0 0 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--surface-subtle);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.quiz-opt.correct {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-ink);
}
.quiz-opt.correct .opt-key { background: var(--accent); color: #fff; }
.quiz-opt.incorrect {
  border-color: var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
  text-decoration: line-through;
}

.quiz-feedback {
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.quiz-feedback-body b { display: block; font-size: 15px; }
.quiz-feedback-body b.is-ok { color: var(--accent-ink); }
.quiz-feedback-body b.is-bad { color: var(--danger); }
.quiz-feedback-body p { margin-top: 5px; font-size: 14px; color: var(--muted); line-height: 1.55; }
.quiz-feedback-actions { display: flex; align-items: center; gap: 8px; }

/* Subtle Companion Strip Below Quiz */
.companion-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.companion-strip-text b { display: block; font-size: 14px; }
.companion-strip-text small { display: block; margin-top: 2px; color: var(--muted); }
.companion-strip-actions { display: flex; align-items: center; gap: 8px; }
.companion-strip-actions button { min-height: 36px; padding: 5px 14px; font-size: 13px; }
.kbd-footer {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}
.kbd-footer kbd {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 5px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
}

/* =========================================================
   View 2: AI Extract Page
   ========================================================= */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 20px;
}
.card + .card { margin-top: 14px; }
.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}
.section-head h1 { font-size: clamp(1.4rem, 1.15rem + 1vw, 1.85rem); letter-spacing: -.03em; }
.section-head p { margin-top: 4px; font-size: 14px; color: var(--muted); }

.extract-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(0, .95fr);
  gap: 16px;
  align-items: start;
}
@container (max-width: 680px) {
  .extract-grid { grid-template-columns: 1fr; }
}
.preset-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.preset-chip {
  min-height: 32px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 12px;
  font-weight: 600;
}
.preset-chip:hover { background: var(--surface-subtle); }
.field-group { display: grid; gap: 6px; margin-bottom: 12px; }
.field-group label { font-size: 13px; font-weight: 650; }
.text-input, .text-area {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  padding: 10px 12px;
  font-size: 14px;
}
.text-area { min-height: 200px; resize: vertical; line-height: 1.6; }
.file-label { position: relative; overflow: hidden; cursor: pointer; }
.file-label > input { position: absolute; inset: 0; opacity: 0; width: 100%; cursor: pointer; }

.preview-list { display: grid; gap: 8px; max-height: 400px; overflow-y: auto; }
.preview-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
}
.preview-card input[type="checkbox"] { width: 17px; height: 17px; margin-top: 4px; accent-color: var(--accent); cursor: pointer; }
.preview-card-fields { display: grid; gap: 6px; }
.preview-card-fields input {
  width: 100%;
  border: 1px solid transparent;
  border-radius: 7px;
  background: var(--surface);
  color: var(--ink);
  padding: 5px 8px;
  font-size: 13px;
}
.preview-card-fields input:focus { border-color: var(--accent); outline: none; }
.preview-card-fields input.p-term { font-weight: 700; }

/* =========================================================
   View 3: Library (Decks + Card Editor + Wrong Answers)
   ========================================================= */
.deck-list { display: grid; gap: 12px; }
.deck-row-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px 18px;
  display: grid;
  gap: 10px;
}
.deck-row-card[data-active="true"] { border-color: var(--accent); }
.deck-row-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.deck-row-top b { font-size: 15px; display: block; }
.deck-row-top small { display: block; margin-top: 3px; color: var(--muted); }
.deck-row-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.deck-row-actions button { min-height: 36px; padding: 5px 13px; font-size: 13px; }

progress.mastery-bar {
  appearance: none;
  border: 0;
  overflow: clip;
  border-radius: 999px;
  background: var(--surface-subtle);
  inline-size: 100%;
  block-size: 6px;
  display: block;
}
progress.mastery-bar::-webkit-progress-bar { background: transparent; }
progress.mastery-bar::-webkit-progress-value { background: var(--accent); border-radius: 999px; }
progress.mastery-bar::-moz-progress-bar { background: var(--accent); border-radius: 999px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 14px 0; }
.search-box {
  flex: 1 1 200px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  min-height: 38px;
  border-radius: 11px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
}
.search-box input { flex: 1; min-width: 0; border: 0; background: transparent; color: var(--ink); font-size: 13px; outline: none; }
.filter-pills { display: flex; flex-wrap: wrap; gap: 6px; }
.filter-pills button {
  min-height: 32px;
  padding: 0 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.filter-pills button[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }

.card-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 15px;
  padding: 14px 16px;
  display: grid;
  gap: 6px;
}
.card-item + .card-item { margin-top: 10px; }
.card-item-head { display: flex; align-items: center; gap: 8px; }
.card-item-head strong { font-size: 16px; overflow-wrap: anywhere; }
.card-badge {
  margin-left: auto;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-subtle);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}
.card-badge[data-status="mastered"] { background: var(--accent-soft); color: var(--accent-ink); }
.card-item p { font-size: 14px; line-height: 1.5; }
.card-item small { color: var(--muted); }

/* Secondary Drawer Dialog (Settings / Stats / Backup) */
.drawer-dialog {
  margin: 0 0 0 auto;
  width: min(400px, 100vw);
  height: 100dvh;
  max-height: 100dvh;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
}
.drawer-dialog[open] { display: flex; flex-direction: column; }
.drawer-dialog::backdrop { background: rgba(0, 0, 0, .42); backdrop-filter: blur(2px); }
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.drawer-head h2 { font-size: 16px; }
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px 32px;
  display: grid;
  gap: 16px;
  align-content: start;
}
.drawer-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 15px;
  display: grid;
  gap: 10px;
}
.drawer-section h3 { font-size: 14px; }
.mini-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  text-align: center;
}
.mini-stats div { padding: 10px 6px; border-radius: 12px; background: var(--bg); }
.mini-stats b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.mini-stats small { color: var(--muted); }

.segments { display: flex; flex-wrap: wrap; gap: 6px; }
.segments button {
  flex: 0 1 auto;
  min-height: 36px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-subtle);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.segments button[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }

.switch-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  min-height: 40px;
  font-size: 13px;
}
.switch-row input[type="checkbox"] {
  appearance: none;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  padding: 3px;
  background: var(--border-strong);
  margin: 0;
  cursor: pointer;
  flex-shrink: 0;
}
.switch-row input[type="checkbox"]::after {
  content: "";
  display: block;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  transition: translate .15s;
}
.switch-row input[type="checkbox"]:checked { background: var(--accent); }
.switch-row input[type="checkbox"]:checked::after { translate: 18px; }

/* In-Page Floating Companion Dock (Fallback) */
.floating-dock {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 35;
  width: min(350px, calc(100vw - 24px));
  max-height: calc(100dvh - 80px);
  overflow-y: auto;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--border-strong);
  border-radius: 20px;
  padding: 15px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .24);
}

/* Modals & Toast */
.modal-dialog {
  width: min(380px, calc(100% - 32px));
  padding: 22px;
  border: 1px solid var(--border-strong);
  border-radius: 22px;
  background: var(--surface);
  color: var(--ink);
}
.modal-dialog::backdrop { background: rgba(0, 0, 0, .5); backdrop-filter: blur(2px); }
.modal-dialog h1 { font-size: 18px; }
.modal-dialog p { margin: 10px 0 18px; color: var(--muted); font-size: 14px; line-height: 1.5; }
.actions { display: flex; gap: 10px; }
.actions > * { flex: 1 1 0; }

.empty, .loading {
  display: grid;
  gap: 10px;
  padding: 32px 16px;
  text-align: center;
  justify-items: center;
  color: var(--muted);
  font-size: 13px;
}
.empty b { color: var(--ink); font-size: 15px; }

.toast {
  position: fixed;
  z-index: 50;
  bottom: calc(18px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translate(-50%, 8px);
  max-width: min(500px, calc(100vw - 24px));
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s, transform .15s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 680px) {
  .brand strong { display: none; }
  .pip-cta span, .more-btn span { display: none; }
  .pip-cta, .more-btn { padding: 0 10px; }
  .primary-nav button { padding: 0 11px; font-size: 12px; }
  .kbd-footer { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; }
}
