:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #6b7385;
  --border: #dde1e8;
  --primary: #2f5d8a;
  --primary-hover: #244a6f;
  --primary-soft: #e8f0f8;
  --ok: #1f7a4d;
  --ok-soft: #e4f4ec;
  --bad: #b3261e;
  --bad-soft: #fbe9e7;
  --warn: #8a5a00;
  --warn-soft: #fff4d6;
  --radius: 8px;
  --series-1: #2a78d6;
  --grid: #e6e8ec;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171d;
    --surface: #1d2129;
    --text: #e4e7ee;
    --muted: #99a1b3;
    --border: #323846;
    --primary: #6fa3d8;
    --primary-hover: #8cb8e4;
    --primary-soft: #1f2d3d;
    --ok: #5cc592;
    --ok-soft: #173327;
    --bad: #f08a80;
    --bad-soft: #3a1c1a;
    --warn: #e8c060;
    --warn-soft: #3a3016;
    --series-1: #3987e5;
    --grid: #2a2f3a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
a { color: var(--primary); }
h1 { font-size: 1.6rem; margin: 0.4em 0 0.6em; }
h2 { font-size: 1.2rem; margin: 1em 0 0.5em; }
h3 { font-size: 1.05rem; }
code, pre { font-family: ui-monospace, Consolas, monospace; font-size: 0.9em; }
pre { background: var(--bg); padding: 12px; border-radius: var(--radius); overflow-x: auto; }
.muted { color: var(--muted); font-weight: normal; font-size: 0.9em; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }
.pre { white-space: pre-wrap; }
.nowrap { white-space: nowrap; }

/* layout */
.topbar {
  display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--text); }
.topbar nav { display: flex; gap: 16px; flex: 1; flex-wrap: wrap; }
.topbar nav a { text-decoration: none; color: var(--text); }
.topbar nav a:hover { color: var(--primary); }
.who { display: flex; gap: 12px; align-items: center; margin-left: auto; }
.container { max-width: 1000px; margin: 0 auto; padding: 16px 16px 80px; }
.head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grow { flex: 1; min-width: 200px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 12px 0; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 20px; margin-bottom: 16px;
}
.card.narrow { max-width: 420px; margin: 40px auto; }
.card.highlight { border-color: var(--primary); background: var(--primary-soft); }
.link-card { text-decoration: none; color: inherit; justify-content: space-between; align-items: center; }
.inline { display: inline; }

/* forms */
input, select, textarea, button, .button {
  font: inherit; color: inherit; border-radius: 6px;
}
input:not([type=checkbox]):not([type=radio]):not([type=file]), select, textarea {
  border: 1px solid var(--border); background: var(--surface); padding: 7px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 0.92rem; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-size: 1rem; }
.row > label { min-width: 120px; }
.row > label.check { min-width: 0; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; margin: 0; }
fieldset[hidden] { display: none; }
legend { padding: 0 6px; font-weight: 600; }
input.num { width: 90px !important; }
button, .button {
  display: inline-block; padding: 7px 16px; border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; text-decoration: none; color: var(--text); white-space: nowrap;
}
button:hover, .button:hover { border-color: var(--primary); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.primary:hover { background: var(--primary-hover); }
button.danger { color: var(--bad); }
button.link { border: 0; background: none; padding: 0; color: var(--primary); cursor: pointer; }
button.link.danger, a.danger { color: var(--bad); }

/* flash & badges */
.flash { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 12px; border: 1px solid; }
.flash ul { margin: 6px 0 0; padding-left: 20px; }
.flash-info { background: var(--primary-soft); border-color: var(--primary); }
.flash-success { background: var(--ok-soft); border-color: var(--ok); }
.flash-error { background: var(--bad-soft); border-color: var(--bad); }
.badge {
  display: inline-block; font-size: 0.78rem; padding: 1px 8px; border-radius: 99px;
  background: var(--primary-soft); color: var(--primary); margin-right: 4px; white-space: nowrap;
}
.badge.muted { background: var(--bg); color: var(--muted); }
.badge.tag { background: transparent; border: 1px solid var(--border); color: var(--muted); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.diff-1 { background: var(--ok-soft); color: var(--ok); }
.badge.diff-2 { background: var(--warn-soft); color: var(--warn); }
.badge.diff-3 { background: var(--bad-soft); color: var(--bad); }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; background: var(--surface); margin-bottom: 16px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 0.85rem; color: var(--muted); font-weight: 600; }
tr.disabled { opacity: 0.55; }
.tabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.tabs a { padding: 6px 16px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--primary); border-color: var(--primary); }

/* stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; text-decoration: none; color: var(--text); display: flex; flex-direction: column;
}
.stat b { font-size: 1.7rem; line-height: 1.2; }
.stat span { color: var(--muted); font-size: 0.88rem; }
.stat.warn { border-color: var(--warn); background: var(--warn-soft); }
.bar { display: inline-block; width: 120px; height: 8px; background: var(--bg); border-radius: 4px; vertical-align: middle; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--series-1); }

/* questions */
.question .q-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.q-actions { margin-left: auto; display: flex; gap: 12px; }
.qid { font-weight: 700; color: var(--muted); }
.stem { white-space: pre-wrap; font-size: 1.05rem; margin-bottom: 10px; }
.stem-preview { color: var(--muted); font-size: 0.92rem; }
.q-image { max-width: 100%; max-height: 360px; border-radius: 6px; border: 1px solid var(--border); margin: 6px 0 10px; display: block; }
.options { list-style: none; padding: 0; margin: 8px 0; display: flex; flex-direction: column; gap: 6px; }
.options.tf { flex-direction: row; gap: 12px; }
.options.compact { gap: 2px; font-size: 0.92rem; }
.options.compact li.is-key { color: var(--ok); font-weight: 600; }
.option {
  display: flex; flex-direction: row; align-items: center; gap: 10px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 6px; cursor: pointer; font-size: 1rem;
}
.option:hover { border-color: var(--primary); }
.option:has(input:checked) { background: var(--primary-soft); border-color: var(--primary); }
.option.review { cursor: default; }
/* After checking, right/wrong must beat the "picked" highlight above. */
.options .option.is-key { background: var(--ok-soft); border-color: var(--ok); }
.options .option.is-wrong { background: var(--bad-soft); border-color: var(--bad); }
.letter { font-weight: 700; color: var(--muted); min-width: 1.2em; }
.picked { margin-left: auto; font-size: 0.8rem; color: var(--primary); font-weight: 600; }
input.fill { max-width: 420px; }
.kv { display: flex; gap: 12px; margin: 6px 0; }
.kv > span:first-child { color: var(--muted); min-width: 3em; }
.explanation { margin-top: 10px; padding: 10px 12px; background: var(--bg); border-radius: 6px; font-size: 0.95rem; }
.verdict { font-weight: 700; font-size: 0.9rem; }
.verdict.right { color: var(--ok); }
.verdict.wrong { color: var(--bad); }
.verdict.review, .verdict.partial { color: var(--warn); }
.question.needs-review { border-color: var(--warn); }
.check-row { margin-top: 8px; }
.score-input { flex-direction: row; align-items: center; margin-left: auto; gap: 6px; }
.score-banner { font-size: 1.1rem; margin: 8px 0 16px; display: flex; gap: 16px; align-items: baseline; flex-wrap: wrap; }
.score-banner b { font-size: 2rem; color: var(--primary); }
.pickable { display: block; cursor: pointer; }
.pickable.in-exam { opacity: 0.6; cursor: default; }

/* editor */
.option-editor { list-style: none; padding: 0; margin: 0 0 8px; display: flex; flex-direction: column; gap: 6px; }
.option-editor li { display: flex; align-items: center; gap: 8px; }
.preview { border: 1px dashed var(--border); border-radius: 6px; padding: 8px 12px; min-height: 1em; }
.preview[hidden] { display: none; }
.current-image { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.chapters { list-style: none; padding-left: 16px; margin: 8px 0; display: flex; flex-direction: column; gap: 6px; }
.chapters .row, .card .row { align-items: center; }
.title-input { font-weight: 600; }
.login-hint { margin-top: 12px; line-height: 1.7; }

/* sticky bars */
.sticky-bar {
  position: sticky; top: 54px; z-index: 5; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 16px; margin-bottom: 16px; display: flex; gap: 16px;
  align-items: center; flex-wrap: wrap;
}
.exam-bar .timer { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 700; }
.exam-bar .timer.urgent { color: var(--bad); }
.exam-bar button { margin-left: auto; }
.exam-bar .timer + button { margin-left: 0; }
.exam-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.exam-card h2 { margin-top: 0; }
.pager { display: flex; gap: 16px; justify-content: center; margin: 16px 0; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.filters select, .filters input { width: auto !important; flex: 1; min-width: 130px; }

/* charts & analysis */
.chart { margin: 8px 0; position: relative; }
.table-view { margin-top: 8px; }
.table-view summary { cursor: pointer; color: var(--primary); font-size: 0.9rem; }
.opt-dist { display: inline-flex; gap: 10px; flex-wrap: wrap; font-variant-numeric: tabular-nums; }
.opt-dist .key { font-weight: 700; color: var(--ok); }
.item-analysis td { vertical-align: middle; }
.class-card { flex-direction: column; align-items: flex-start; display: flex; }
.class-card h2 { margin: 0; }

@media (max-width: 640px) {
  /* Brand + account on the first row, nav as one scrollable row underneath. */
  .topbar { padding: 8px 16px 0; gap: 4px 12px; }
  .topbar nav { order: 3; flex: 1 0 100%; flex-wrap: nowrap; overflow-x: auto; gap: 18px; padding: 6px 0 8px; }
  .topbar nav a { white-space: nowrap; }
  .who { margin-left: auto; }
  /* Tables scroll sideways instead of breaking words mid-character. */
  .table-wrap th, .table-wrap td { white-space: nowrap; }
  h1 { font-size: 1.35rem; }
  .card { padding: 14px; }
  .topbar { position: static; }  /* the exam bar takes the sticky slot on phones */
  .sticky-bar { top: 0; }
}
.head form.row { flex-wrap: nowrap; }
.head form.row input { width: auto; min-width: 0; }

/* course lessons */
.crumbs { font-size: 0.9rem; color: var(--muted); margin-top: 4px; }
.crumbs a { color: var(--muted); }
.lesson-group h2 { margin-top: 24px; }
.lesson-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.lesson-card { display: flex; flex-direction: column; gap: 4px; text-decoration: none; color: var(--text); margin: 0; }
.lesson-card:hover { border-color: var(--primary); }
.lesson-card h3 { margin: 0; font-size: 1.1rem; }
.lesson-summary { color: var(--muted); margin: 4px 0 8px; text-align: justify; }
.lesson-frame-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.lesson-frame { display: block; width: 100%; height: 80vh; border: 0; background: #fff; }
.lesson-footer { margin-top: 16px; }
.practice-card { align-items: center; }
.lesson-nav { display: flex; justify-content: space-between; gap: 12px; margin: 16px 0; }
.review-link { font-size: 0.9rem; margin-top: 8px; display: inline-block; }

/* question visibility & stats */
.question.is-hidden { border-style: dashed; }
.question.is-hidden .stem { color: var(--muted); }
.badge-button { border: 0; cursor: pointer; font: inherit; font-size: 0.78rem; padding: 1px 8px; }
.bulk-bar { gap: 8px; }
.bulk-bar button { padding: 4px 10px; font-size: 0.9rem; }
.bulk-sep { margin-left: 12px; }
.q-stats { margin: 8px 0 0; font-size: 0.85rem; }
.narrow-form { max-width: 640px; }

/* teacher "view as student" */
.preview-banner {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap; justify-content: center;
  padding: 8px 16px; background: var(--warn-soft); color: var(--warn); border-bottom: 1px solid var(--warn);
  font-size: 0.92rem;
}
.preview-banner button { padding: 4px 12px; font-size: 0.88rem; }
.hint-list { color: var(--muted); font-size: 0.9rem; margin: 0; padding-left: 20px; }

/* self-practice form */
.practice-progress { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 0.92rem; }
.practice-group + .practice-group { margin-top: 12px; }
.chapter-list { display: flex; flex-direction: column; gap: 4px; margin: 6px 0 0 26px; }
.chapter-line { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; }
.chapter-line:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.bar.small { width: 80px; height: 6px; }
.stack.tight { gap: 6px; }
.reset-card { align-items: center; }
@media (max-width: 640px) { .chapter-line .bar.small { display: none; } .chapter-list { margin-left: 0; } }
.save-status { font-size: 0.8rem; }
