[hidden] { display: none !important; }

/* Theme tokens — switched via html[data-theme]; contrast via data-contrast.
   Default palette = high contrast (body ≥ 7:1, muted ≥ 4.5:1). */
:root,
html[data-theme="black"] {
  --bg: #000000;
  --bg-panel: #0a0a0a;
  --bg-elevated: #111111;
  --bg-user: #1a1a1a;
  --border: #3a3a3a;
  --border-strong: #555555;
  --text: #f5f5f5;
  --text-muted: #c8c8c8;
  --text-dim: #c8c8c8;
  --btn-bg: #ffffff;
  --btn-fg: #000000;
  --btn-hover: #e8e8e8;
  --bar-fill: #c8c8c8;
  --ok: #5a9a6e;
  --ok-bg: rgba(90, 154, 110, 0.12);
  --danger: #b85c5c;
  --danger-bg: rgba(184, 92, 92, 0.12);
  --warn: #a89050;
  --warn-bg: rgba(168, 144, 80, 0.1);
  --theme-color: #000000;
  color-scheme: dark;
}

/* Softer dark — still AAA body; muted ≥ AA */
html[data-theme="darkgray"] {
  --bg: #1a1a1a;
  --bg-panel: #222222;
  --bg-elevated: #2a2a2a;
  --bg-user: #333333;
  --border: #555555;
  --border-strong: #6e6e6e;
  --text: #f5f5f5;
  --text-muted: #c8c8c8;
  --text-dim: #c8c8c8;
  --btn-bg: #e8e8e8;
  --btn-fg: #1a1a1a;
  --btn-hover: #d4d4d4;
  --bar-fill: #c8c8c8;
  --ok: #6aaa7e;
  --ok-bg: rgba(106, 170, 126, 0.14);
  --danger: #c07070;
  --danger-bg: rgba(192, 112, 112, 0.14);
  --warn: #b8a060;
  --warn-bg: rgba(184, 160, 96, 0.12);
  --theme-color: #1a1a1a;
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg: #ffffff;
  --bg-panel: #f7f7f7;
  --bg-elevated: #f0f0f0;
  --bg-user: #e8e8e8;
  --border: #c8c8c8;
  --border-strong: #9a9a9a;
  --text: #111111;
  --text-muted: #4a4a4a;
  --text-dim: #5a5a5a;
  --btn-bg: #1a1a1a;
  --btn-fg: #ffffff;
  --btn-hover: #333333;
  --bar-fill: #555555;
  --ok: #2f6b45;
  --ok-bg: rgba(47, 107, 69, 0.1);
  --danger: #9a3a3a;
  --danger-bg: rgba(154, 58, 58, 0.1);
  --warn: #7a6220;
  --warn-bg: rgba(122, 98, 32, 0.1);
  --theme-color: #ffffff;
  color-scheme: light;
}

/* Sepia / warm low-blue — AAA body contrast */
html[data-theme="sepia"] {
  --bg: #f4ecd8;
  --bg-panel: #efe4cc;
  --bg-elevated: #e8dcc0;
  --bg-user: #e0d4b4;
  --border: #c8b890;
  --border-strong: #a09068;
  --text: #2a2118;
  --text-muted: #5a4a3a;
  --text-dim: #6a5848;
  --btn-bg: #2a2118;
  --btn-fg: #f4ecd8;
  --btn-hover: #524536;
  --bar-fill: #5a4a3a;
  --ok: #3d6b4a;
  --ok-bg: rgba(61, 107, 74, 0.12);
  --danger: #8a3a3a;
  --danger-bg: rgba(138, 58, 58, 0.12);
  --warn: #7a5a20;
  --warn-bg: rgba(122, 90, 32, 0.12);
  --theme-color: #f4ecd8;
  color-scheme: light;
}

/* system → black (dark) / light (light) via media */
html[data-theme="system"] {
  --bg: #000000;
  --bg-panel: #0a0a0a;
  --bg-elevated: #111111;
  --bg-user: #1a1a1a;
  --border: #3a3a3a;
  --border-strong: #555555;
  --text: #f5f5f5;
  --text-muted: #c8c8c8;
  --text-dim: #c8c8c8;
  --btn-bg: #ffffff;
  --btn-fg: #000000;
  --btn-hover: #e8e8e8;
  --bar-fill: #c8c8c8;
  --ok: #5a9a6e;
  --ok-bg: rgba(90, 154, 110, 0.12);
  --danger: #b85c5c;
  --danger-bg: rgba(184, 92, 92, 0.12);
  --warn: #a89050;
  --warn-bg: rgba(168, 144, 80, 0.1);
  --theme-color: #000000;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  html[data-theme="system"] {
    --bg: #ffffff;
    --bg-panel: #f7f7f7;
    --bg-elevated: #f0f0f0;
    --bg-user: #e8e8e8;
    --border: #c8c8c8;
    --border-strong: #9a9a9a;
    --text: #111111;
    --text-muted: #4a4a4a;
    --text-dim: #5a5a5a;
    --btn-bg: #1a1a1a;
    --btn-fg: #ffffff;
    --btn-hover: #333333;
    --bar-fill: #555555;
    --ok: #2f6b45;
    --ok-bg: rgba(47, 107, 69, 0.1);
    --danger: #9a3a3a;
    --danger-bg: rgba(154, 58, 58, 0.1);
    --warn: #7a6220;
    --warn-bg: rgba(122, 98, 32, 0.1);
    --theme-color: #ffffff;
    color-scheme: light;
  }
}

/* Standard contrast — slightly softer than high; body still ≥ 7:1 */
html[data-contrast="standard"][data-theme="black"],
html[data-contrast="standard"][data-theme="system"] {
  --border: #2a2a2a;
  --border-strong: #3a3a3a;
  --text: #ececec;
  --text-muted: #a8a8a8;
  --text-dim: #909090;
}

html[data-contrast="standard"][data-theme="darkgray"] {
  --border: #444444;
  --border-strong: #555555;
  --text: #e0e0e0;
  --text-muted: #b0b0b0;
  --text-dim: #9a9a9a;
}

html[data-contrast="standard"][data-theme="light"] {
  --border: #d8d8d8;
  --border-strong: #b8b8b8;
  --text: #1a1a1a;
  --text-muted: #5a5a5a;
  --text-dim: #8a8a8a;
}

html[data-contrast="standard"][data-theme="sepia"] {
  --border: #d4c4a0;
  --border-strong: #b8a878;
  --text: #3b2f22;
  --text-muted: #6a5848;
  --text-dim: #8a7868;
}

@media (prefers-color-scheme: light) {
  html[data-contrast="standard"][data-theme="system"] {
    --border: #d8d8d8;
    --border-strong: #b8b8b8;
    --text: #1a1a1a;
    --text-muted: #5a5a5a;
    --text-dim: #8a8a8a;
  }
}

:root {
  --radius: 10px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Segoe UI", sans-serif;
  --font-size: 15px;
  --line-height: 1.6;
  --content-width: 760px;
  --maxw: var(--content-width);
}

html[data-font="rounded"] {
  --font: "Hiragino Maru Gothic ProN", "Tsukushi A Round Gothic", "Yu Gothic",
    "Noto Sans TC", "PingFang TC", -apple-system, BlinkMacSystemFont, sans-serif;
}

html[data-font-size="small"] { --font-size: 14px; }
html[data-font-size="standard"] { --font-size: 15px; }
html[data-font-size="large"] { --font-size: 18px; }
html[data-font-size="xlarge"] { --font-size: 22px; }

html[data-line-height="compact"] { --line-height: 1.4; }
html[data-line-height="standard"] { --line-height: 1.6; }
html[data-line-height="relaxed"] { --line-height: 1.85; }

html[data-width="narrow"] { --content-width: 560px; }
html[data-width="standard"] { --content-width: 760px; }
html[data-width="wide"] { --content-width: 960px; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-weight: 400;
  font-size: var(--font-size);
  line-height: var(--line-height);
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100dvh; }

a {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 2px;
}
a:hover { color: var(--text); opacity: 0.85; }

#app {
  max-width: var(--content-width);
  margin: 0 auto;
  min-height: 100dvh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

html[data-reduce-motion="1"] *,
html[data-reduce-motion="1"] *::before,
html[data-reduce-motion="1"] *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
  scroll-behavior: auto !important;
}

/* ——— Topbar ——— */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 20px;
  padding: 14px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-size: 1.125rem;
  font-weight: 500;
  letter-spacing: -0.01em;
  color: var(--text);
}

.top-meta {
  color: var(--text-muted);
  font-size: 0.8125rem;
  margin-right: auto;
}

.top-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 16px;
  margin-left: auto;
}

.nav-btn {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 0;
  cursor: pointer;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.2;
}

.nav-btn:hover,
.nav-btn.active {
  color: var(--text);
}

.nav-btn.subtle {
  color: var(--text-dim);
}
.nav-btn.subtle:hover { color: var(--text-muted); }

/* ——— Panels ——— */
.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.auth-panel {
  align-items: center;
  justify-content: center;
  padding: 32px 16px 48px;
}

.auth-card {
  width: min(400px, 100%);
  padding: 28px 22px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elevated);
}

.auth-card h1 {
  font-size: 1.375rem;
  margin: 0 0 8px;
  font-weight: 500;
  line-height: 1.3;
}

.lede {
  color: var(--text-muted);
  margin: 0 0 20px;
  line-height: 1.5;
  font-size: 0.875rem;
}

.tabs {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.tab {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 8px 0;
  margin-bottom: -1px;
  cursor: pointer;
  font: inherit;
  font-size: 0.875rem;
  border-bottom: 1px solid transparent;
}

.tab.active {
  color: var(--text);
  border-bottom-color: var(--text);
}

#auth-form {
  display: grid;
  gap: 12px;
}

#auth-form label {
  display: grid;
  gap: 6px;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

#auth-form input {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  font: inherit;
}

#auth-form input:focus {
  outline: none;
  border-color: var(--border-strong);
}

.primary {
  border: none;
  background: var(--btn-bg);
  color: var(--btn-fg);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  cursor: pointer;
  font: inherit;
  font-weight: 500;
}

.primary:hover { background: var(--btn-hover); }

.form-error {
  color: var(--danger);
  margin: 0;
  font-size: 0.875rem;
}

/* ——— Chat stream ——— */
.chat-panel { position: relative; }

.stream {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  position: relative;
  scroll-behavior: smooth;
}

html[data-reduce-motion="1"] .stream {
  scroll-behavior: auto;
}

.msg {
  line-height: var(--line-height);
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 1em;
}

.msg.assistant {
  align-self: stretch;
  max-width: 100%;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 0;
}

.msg.user {
  align-self: flex-end;
  max-width: 85%;
  padding: 10px 14px;
  background: var(--bg-user);
  border: none;
  border-radius: 12px;
  color: var(--text);
}

.msg-title {
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  margin-bottom: 6px;
}

.msg.msg-loading .loading-text {
  color: var(--text-muted);
  margin: 0;
}

.msg.msg-error {
  padding-left: 10px;
  border-left: 2px solid var(--danger);
}

.msg.msg-error .msg-title {
  color: var(--danger);
}

.msg.assistant.msg-fresh,
.qcard.msg-fresh,
.tutor-confirm.msg-fresh {
  box-shadow: inset 2px 0 0 var(--text);
  background: var(--bg-elevated);
  transition: box-shadow 0.5s ease, background 0.5s ease;
}

.msg.assistant.msg-fresh-out,
.qcard.msg-fresh-out,
.tutor-confirm.msg-fresh-out {
  box-shadow: inset 2px 0 0 transparent;
  background: transparent;
}

.msg.user.msg-fresh {
  outline: 1px solid var(--border-strong);
}

.msg .md h1,
.msg .md h2,
.msg .md h3 {
  font-family: var(--font);
  font-weight: 500;
  margin: 0.7em 0 0.35em;
  line-height: 1.3;
}

.msg .md h1 { font-size: 1.2rem; }
.msg .md h2 { font-size: 1.1rem; }
.msg .md h3 { font-size: 1rem; }

.msg .md p { margin: 0.45em 0; }
.msg .md ul,
.msg .md ol { padding-left: 1.2em; }

.msg .md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--bg-elevated);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

.msg .md strong { color: var(--text); font-weight: 500; }

/* ——— Question card ——— */
.qcard {
  align-self: stretch;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: var(--radius);
  padding: 16px;
}

.qcard .meta {
  color: var(--text-muted);
  font-size: 0.8125rem;
  margin-bottom: 8px;
}

.qcard .stem {
  color: var(--text);
  font-size: 1em;
  font-weight: 500;
  margin-bottom: 12px;
  white-space: pre-wrap;
  line-height: var(--line-height);
}

.qcard .source {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: 10px;
}

.opts { display: grid; gap: 8px; }

.opt {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  cursor: pointer;
  font: inherit;
  font-size: 1em;
  text-align: left;
  line-height: var(--line-height);
}

.opt:hover {
  border-color: var(--border-strong);
}

.opt.correct {
  border-color: var(--ok);
  background: var(--ok-bg);
}

.opt.wrong {
  border-color: var(--danger);
  background: var(--danger-bg);
}

.opt:disabled {
  opacity: 0.9;
  cursor: default;
}

/* ——— Composer ——— */
.composer {
  position: sticky;
  bottom: 0;
  padding: 8px 12px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--border);
}

.chips {
  display: flex;
  gap: 4px 14px;
  overflow-x: auto;
  padding: 4px 4px 10px;
  scrollbar-width: none;
}

.chips::-webkit-scrollbar { display: none; }

.chips button {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 0;
  cursor: pointer;
  font: inherit;
  font-size: 0.8125rem;
  white-space: nowrap;
  line-height: 1.2;
}

.chips button:hover { color: var(--text); }

.chips button.is-pressed {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.chips button:disabled {
  opacity: 0.55;
  cursor: wait;
}

.new-content-chip {
  display: block;
  width: 100%;
  box-sizing: border-box;
  border: 1px solid var(--border-strong);
  background: var(--bg-panel);
  color: var(--text);
  padding: 8px 10px;
  margin: 0 0 8px;
  font: inherit;
  font-size: 0.8125rem;
  cursor: pointer;
  border-radius: var(--radius-sm);
  text-align: center;
}

.new-content-chip:hover {
  border-color: var(--text-muted);
}

.new-content-chip[hidden] {
  display: none !important;
}

.tutor-new-content-chip {
  margin: 0;
  border-radius: 0;
  border-left: 0;
  border-right: 0;
}

.ask-form {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 6px 6px 14px;
}

.ask-form input {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 8px 0;
  font: inherit;
  min-width: 0;
}

.ask-form input:focus { outline: none; }
.ask-form input::placeholder { color: var(--text-dim); }

.send-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--btn-bg);
  color: var(--btn-fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.send-btn:hover { background: var(--btn-hover); }

.send-btn svg {
  width: 16px;
  height: 16px;
  display: block;
}

/* ——— Page panels ——— */
.page-panel {
  padding: 20px 16px 48px;
  overflow-y: auto;
}

.page-panel h2 {
  font-size: 1.25rem;
  font-weight: 500;
  margin: 0 0 14px;
  line-height: 1.3;
}

.page-body { display: grid; gap: 12px; }

.stat,
.note {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: var(--radius);
  padding: 14px;
}

.stat h3,
.note h3 {
  margin: 0 0 8px;
  font-size: 0.9375rem;
  font-weight: 500;
}

.muted { color: var(--text-muted); }
.warn { color: var(--warn); }
.ok { color: var(--ok); }

/* ——— Category progress ——— */
.cat-progress .cat-source {
  font-size: 0.8125rem;
  margin-bottom: 12px;
}

.cat-subjects { display: grid; gap: 8px; }

.cat-subject {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-panel);
  overflow: hidden;
}

.cat-subject.is-weak {
  border-color: rgba(184, 92, 92, 0.45);
}

.cat-subject-toggle {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 2px 10px;
  align-items: center;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 12px 14px 8px;
  cursor: pointer;
  font: inherit;
}

.cat-subject-title {
  font-size: 0.9375rem;
  font-weight: 500;
}

.cat-subject-summary {
  grid-column: 1;
  color: var(--text-muted);
  font-size: 0.8125rem;
}

.cat-chevron {
  grid-column: 2;
  grid-row: 1 / span 2;
  width: 8px;
  height: 8px;
  border-right: 1.5px solid var(--text-muted);
  border-bottom: 1.5px solid var(--text-muted);
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
  justify-self: end;
}

.cat-subject.open .cat-chevron { transform: rotate(45deg); }

.cat-subject-total { padding: 0 14px 10px; }

.cat-list {
  display: none;
  gap: 10px;
  padding: 0 12px 12px;
  border-top: 1px solid var(--border);
}

.cat-subject.open .cat-list {
  display: grid;
  padding-top: 10px;
}

.cat-row { padding: 8px 4px; }

.cat-row.is-weak {
  background: var(--danger-bg);
  border-radius: 6px;
  padding: 8px 6px;
}

.cat-row-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
}

.cat-unit {
  font-size: 0.875rem;
  line-height: 1.35;
  word-break: break-word;
}

.cat-sched {
  flex-shrink: 0;
  font-size: 0.7rem;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  white-space: nowrap;
}

.cat-sched-today {
  color: var(--btn-fg);
  background: var(--btn-bg);
  border-color: transparent;
}

.cat-sched-done {
  color: var(--ok);
  border-color: rgba(90, 154, 110, 0.35);
}

.cat-sched-upcoming {
  color: var(--text-muted);
  border-color: var(--border);
}

.cat-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-top: 6px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.cat-bar {
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  overflow: hidden;
}

.cat-bar > span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--bar-fill);
}

.cat-row.is-weak .cat-bar > span,
.cat-subject.is-weak > .cat-subject-total .cat-bar > span {
  background: var(--warn);
}

/* ——— Ask tutor ——— */
.ask-tutor-btn {
  margin-top: 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  cursor: pointer;
  font: inherit;
  font-size: 0.8125rem;
}

.ask-tutor-btn:hover {
  color: var(--text);
  border-color: var(--border-strong);
}

/* ——— Tutor FAB + panel ——— */
.tutor-fab {
  position: fixed;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 40;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-weight: 500;
  font-family: var(--font);
  font-size: 0.75rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  cursor: pointer;
}

.tutor-fab:hover { background: var(--btn-hover); }

.tutor-panel {
  position: fixed;
  right: 16px;
  bottom: calc(76px + env(safe-area-inset-bottom));
  z-index: 41;
  width: min(400px, calc(100vw - 24px));
  height: min(620px, calc(100dvh - 120px));
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-panel);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

.tutor-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 14px 10px;
  border-bottom: 1px solid var(--border);
}

.tutor-panel-head strong {
  font-size: 0.9375rem;
  font-weight: 500;
}

.tutor-stream {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.tutor-form {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.tutor-form input {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text);
  border-radius: var(--radius);
  padding: 10px 12px;
  font: inherit;
  min-width: 0;
}

.tutor-form input:focus {
  outline: none;
  border-color: var(--border-strong);
}

.tutor-form .send-btn { flex-shrink: 0; }

.tutor-confirm {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--bg-elevated);
}

.tutor-confirm .meta {
  color: var(--text-muted);
  font-size: 0.8125rem;
}

.tutor-confirm .stem {
  color: var(--text);
  font-weight: 500;
  margin: 6px 0 10px;
  white-space: pre-wrap;
}

.citations {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.citations-title {
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

.citation-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 8px;
  margin-bottom: 6px;
  background: var(--bg-elevated);
}

.citation-item summary {
  cursor: pointer;
  list-style: none;
}

.citation-item summary::-webkit-details-marker { display: none; }

.cite-num {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.cite-badge {
  display: inline-block;
  margin-right: 4px;
  font-size: 0.7rem;
  color: var(--text-muted);
  border: 1px solid var(--border);
  padding: 0 4px;
}

.cite-badge.bank {
  color: var(--text);
}

.cite-snippet {
  margin-top: 6px;
  white-space: pre-wrap;
  color: var(--text-muted);
  line-height: 1.45;
}

.cite-link {
  margin-top: 4px;
}

.cite-link a {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ——— Brand / exam track ——— */
.brand-wrap {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.brand-en {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 0.875rem;
  letter-spacing: 0.02em;
}

.track-badge {
  font-size: 0.75rem;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  line-height: 1.3;
}

.subject-list {
  list-style: none;
  margin: 0 0 18px;
  padding: 0;
  display: grid;
  gap: 6px;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.subject-list li {
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.track-fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin: 0;
}

.track-fieldset legend {
  padding: 0 4px;
  color: var(--text-muted);
  font-size: 0.8125rem;
}

.track-option {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
  font-size: 0.875rem;
}

.track-option.disabled {
  opacity: 0.55;
}

.track-soon {
  margin-left: auto;
  font-size: 0.75rem;
}

.page-lede {
  margin: -4px 16px 12px;
  font-size: 0.8125rem;
}

.q-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 10px;
}

.q-badge {
  font-size: 0.75rem;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 7px;
  line-height: 1.3;
}

.q-badge.past { color: var(--text); }
.q-badge.generated { color: var(--warn); border-color: var(--border-strong); }
.q-badge.warn { color: var(--warn); }

.opt.picked {
  border-color: var(--border-strong);
  background: var(--bg-user);
}

.scope-toggle {
  display: flex;
  gap: 12px;
  margin: 8px 0 12px;
}

.scope-btn {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 0;
  font: inherit;
  font-size: 0.8125rem;
  cursor: pointer;
  border-bottom: 1px solid transparent;
}

.scope-btn.active {
  color: var(--text);
  border-bottom-color: var(--text);
}

.ghost {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.8125rem;
  cursor: pointer;
  padding: 0;
}
.ghost:hover { color: var(--text); }

.secondary {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  cursor: pointer;
}
.secondary:hover { border-color: var(--border-strong); }
.secondary:disabled,
.primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ——— Past exams ——— */
.past-year {
  margin: 0 0 20px;
}

.past-year > h3 {
  font-size: 1rem;
  font-weight: 500;
  margin: 0 0 10px;
}

.past-term {
  margin: 0 0 14px;
}

.past-term-label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.past-papers {
  display: grid;
  gap: 10px;
}

.past-paper {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.past-paper-title {
  font-weight: 500;
  font-size: 0.9375rem;
}

.past-paper-meta {
  font-size: 0.8125rem;
  margin: 4px 0 10px;
}

.past-paper-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  align-items: center;
}

.past-exam-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 10px 16px;
}

.past-timer {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-size: 1.125rem;
  font-weight: 500;
}

.past-timer.warn { color: var(--warn); }

.past-nav-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 14px 0;
  align-items: center;
}

.past-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 24px;
}

.past-num {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  border-radius: 6px;
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
}

.past-num.answered { color: var(--text); border-color: var(--border-strong); }
.past-num.current { background: var(--btn-bg); color: var(--btn-fg); border-color: var(--btn-bg); }

.past-result-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: baseline;
  margin: 6px 0;
}

/* ——— Responsive ——— */
@media (max-width: 640px) {
  .topbar {
    padding: 12px 14px;
    gap: 8px 12px;
  }

  .top-meta {
    order: 3;
    flex-basis: 100%;
    margin-right: 0;
  }

  .top-nav {
    margin-left: 0;
    gap: 4px 12px;
  }

  .msg.user { max-width: 92%; }

  .auth-card h1 { font-size: 1.25rem; }

  .tutor-panel {
    inset: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100dvh;
    border-radius: 0;
  }

  .tutor-fab {
    right: 14px;
    bottom: calc(14px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 520px) {
  .cat-subject-toggle { padding: 12px; }
  .cat-meta { gap: 6px 10px; }
  .cat-unit { font-size: 0.84rem; }
}

/* ——— Settings ——— */
.settings-form {
  display: grid;
  gap: 18px;
  margin-bottom: 24px;
}

.settings-group {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.settings-group legend,
.settings-label {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 10px;
  padding: 0;
}

.settings-group fieldset {
  border: 0;
  margin: 0;
  padding: 0;
  min-width: 0;
}

.settings-options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
}

.settings-options label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.875rem;
  color: var(--text-muted);
  cursor: pointer;
}

.settings-options label:has(input:checked) {
  color: var(--text);
}

.settings-options input[type="radio"],
.settings-options input[type="checkbox"] {
  accent-color: var(--text);
}

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

.settings-hint {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 0;
}

.settings-preview {
  border: 1px solid var(--border);
  background: var(--bg-panel);
  border-radius: var(--radius);
  padding: 16px;
}

.settings-preview h3 {
  margin: 0 0 12px;
  font-size: 0.9375rem;
  font-weight: 500;
}

.settings-preview .preview-tutor {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 1em;
  line-height: var(--line-height);
  color: var(--text);
}

.settings-preview .preview-tutor .meta {
  color: var(--text-muted);
  font-size: 0.8125rem;
  margin-bottom: 6px;
}

.nav-btn-settings-guest {
  margin-left: auto;
}
