:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1d2433;
  --muted: #6b7489;
  --line: #e3e7f0;
  --primary: #3b6fd8;
  --primary-soft: #e8effc;
  --good: #1f9d63;
  --good-soft: #e3f6ec;
  --bad: #d94848;
  --bad-soft: #fde9e9;
  --warn: #c98a0b;
  --warn-soft: #fdf3dc;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(20, 30, 60, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151c; --card: #1c212b; --text: #e7eaf2; --muted: #98a1b5; --line: #2c3340;
    --primary: #6c96ec; --primary-soft: #22304a; --good: #45c487; --good-soft: #1b3328;
    --bad: #ef6b6b; --bad-soft: #3a2224; --warn: #e3ad3d; --warn-soft: #3a3020;
    --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg); color: var(--text); font-size: 16px; line-height: 1.6;
  padding-bottom: calc(70px + env(safe-area-inset-bottom));
}
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .5em; }
h1 { font-size: 1.4rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1rem; }

/* ---------- 頂部與底部導覽 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; white-space: nowrap; }
.student-switch { display: flex; gap: 6px; flex: 1; overflow-x: auto; }
.student-switch button {
  border: 1px solid var(--line); background: transparent; color: var(--text);
  padding: 4px 14px; border-radius: 999px; font-size: .9rem; cursor: pointer; white-space: nowrap;
}
.student-switch button.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.icon-btn { font-size: 1.2rem; padding: 4px; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10; display: flex;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; padding: 6px 0 8px;
  font-size: .75rem; color: var(--muted); position: relative;
}
.tabbar a span { font-size: 1.3rem; line-height: 1.3; }
.tabbar a.active { color: var(--primary); font-weight: 600; }
.badge {
  position: absolute; top: 2px; left: calc(50% + 6px); background: var(--bad); color: #fff;
  font-size: .7rem; border-radius: 999px; padding: 0 6px; min-width: 18px; text-align: center;
}

.main { max-width: 860px; margin: 0 auto; padding: 16px; }

/* ---------- 卡片與元件 ---------- */
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  border: 1px solid var(--line); padding: 16px; margin-bottom: 14px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 640px) { .grid2 { grid-template-columns: 1fr; } }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 10px; }
.center { text-align: center; }
.empty { text-align: center; color: var(--muted); padding: 28px 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font-size: .95rem; cursor: pointer; font-family: inherit;
  min-height: 40px;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.good { background: var(--good); border-color: var(--good); color: #fff; }
.btn.bad { color: var(--bad); border-color: var(--bad); }
.btn.sm { padding: 4px 10px; min-height: 32px; font-size: .85rem; }
.btn.block { width: 100%; }
.btn.big { padding: 12px 18px; font-size: 1.05rem; }

.tag {
  display: inline-block; font-size: .78rem; padding: 1px 8px; border-radius: 999px;
  background: var(--primary-soft); color: var(--primary); white-space: nowrap;
}
.tag.bad { background: var(--bad-soft); color: var(--bad); }
.tag.good { background: var(--good-soft); color: var(--good); }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.gray { background: var(--line); color: var(--muted); }

.banner { border-radius: 12px; padding: 12px 14px; margin-bottom: 14px; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
.banner.info { background: var(--primary-soft); color: var(--primary); }
.banner.good { background: var(--good-soft); color: var(--good); }

/* 表單 */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=date], input[type=number], input[type=password], input[type=search], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

/* 掌握度 */
.meter { height: 8px; background: var(--line); border-radius: 999px; overflow: hidden; min-width: 60px; }
.meter > i { display: block; height: 100%; border-radius: 999px; }
.m-low { background: var(--bad); } .m-mid { background: var(--warn); } .m-high { background: var(--good); }
.pct { font-variant-numeric: tabular-nums; font-size: .85rem; min-width: 3em; text-align: right; }

.list-item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.list-item:first-child { border-top: 0; }
.list-item .grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 倒數 */
.countdown { display: flex; align-items: center; gap: 14px; }
.countdown .days { font-size: 2.2rem; font-weight: 800; color: var(--primary); line-height: 1; }

/* 題目卡 */
.q-card { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; background: var(--card); }
.q-card.wrong { border-left: 4px solid var(--bad); }
.q-card.right { border-left: 4px solid var(--good); }
.q-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.q-num { font-weight: 700; }
.q-text { margin: 6px 0; overflow-x: auto; }
.q-text p { margin: .3em 0; }
.answer-row { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: .92rem; margin: 6px 0; }
.answer-row dt { color: var(--muted); }
.answer-row dd { margin: 0; }
.explain { background: var(--bg); border-radius: 10px; padding: 10px 12px; margin-top: 8px; font-size: .93rem; overflow-x: auto; }
.explain p { margin: .35em 0; }
.tip { margin-top: 6px; font-size: .9rem; }
details summary { cursor: pointer; color: var(--primary); font-size: .9rem; }

.thumbs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.thumbs a, .thumbs .thumb {
  flex: 0 0 auto; width: 84px; height: 110px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center; background: var(--bg); font-size: .8rem; color: var(--muted);
}
.thumbs img { width: 100%; height: 100%; object-fit: cover; }

/* 選擇題 */
.choices { display: grid; gap: 8px; margin: 10px 0; }
.choice {
  text-align: left; border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 10px; padding: 10px 12px; font: inherit; cursor: pointer; display: flex; gap: 10px;
}
.choice b { color: var(--primary); }
.choice.correct { border-color: var(--good); background: var(--good-soft); }
.choice.picked-wrong { border-color: var(--bad); background: var(--bad-soft); }
.choice:disabled { cursor: default; }

.result { border-radius: 10px; padding: 8px 12px; margin-top: 8px; font-weight: 600; }
.result.ok { background: var(--good-soft); color: var(--good); }
.result.no { background: var(--bad-soft); color: var(--bad); }

/* 複習卡 */
.flash { min-height: 260px; }
.rate-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.rate-grid .btn { flex-direction: column; gap: 0; padding: 8px 4px; line-height: 1.3; }
.rate-grid .btn small { color: var(--muted); font-size: .72rem; }
.rate-grid .btn.good small { color: rgba(255, 255, 255, .88); }
.progress { height: 6px; background: var(--line); border-radius: 999px; overflow: hidden; margin-bottom: 14px; }
.progress > i { display: block; height: 100%; background: var(--primary); }

/* 錯誤類型條 */
.bar-row { display: grid; grid-template-columns: 7.5em 1fr 2.5em; gap: 8px; align-items: center; font-size: .88rem; margin: 4px 0; }
.bar { height: 10px; background: var(--line); border-radius: 999px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--primary); border-radius: 999px; }

/* 弱點樹 */
.subject-block summary { list-style: none; display: flex; align-items: center; gap: 10px; color: var(--text); font-size: 1rem; }
.subject-block summary::-webkit-details-marker { display: none; }
.chapter { margin: 10px 0 0 4px; padding-left: 10px; border-left: 3px solid var(--line); }
.chapter-head { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: .95rem; }
.concept-row {
  display: grid; grid-template-columns: 1fr 64px 3em auto; align-items: center; gap: 2px 8px;
  grid-template-areas: "name name name btn" "meta meter pct btn"; padding: 7px 0; font-size: .92rem;
}
.concept-row .name { grid-area: name; }
.concept-row .meta { grid-area: meta; }
.concept-row .meter { grid-area: meter; }
.concept-row .pct { grid-area: pct; }
.concept-row .btn { grid-area: btn; }
.answer-row dd p, .tip p, .result p { margin: 0; }
.tip p { display: inline; }

/* 報告 */
.report h2 { font-size: 1rem; margin-top: 1em; }
.report ul { padding-left: 1.3em; }

/* 疊加層 */
.overlay { position: fixed; inset: 0; background: rgba(10, 15, 30, .45); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; }
.overlay[hidden] { display: none; }
.overlay-box { background: var(--card); border-radius: var(--radius); padding: 24px 28px; text-align: center; max-width: 320px; }
.overlay-box p { white-space: pre-line; margin: 0; }
.spinner { width: 36px; height: 36px; border: 4px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 12px; }
.spinner.inline { width: 18px; height: 18px; border-width: 3px; margin: 0; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #222a; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 60; max-width: 90vw;
  background: rgba(30, 36, 50, .92); font-size: .92rem;
}
dialog {
  border: none; border-radius: var(--radius); padding: 20px; background: var(--card); color: var(--text);
  width: min(440px, calc(100vw - 32px)); box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(10, 15, 30, .45); }

.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: transparent; color: var(--text); padding: 8px 6px; font: inherit; font-size: .88rem; cursor: pointer; }
.seg button.active { background: var(--primary); color: #fff; }

.subject-table { width: 100%; border-collapse: collapse; }
.subject-table td { padding: 4px 4px 4px 0; }
.katex-display { overflow-x: auto; overflow-y: hidden; }

/* 登入 */
body.logged-out .tabbar, body.logged-out .student-switch, body.logged-out #settings-link, body.logged-out #logout-btn { display: none; }
body.logged-out { padding-bottom: 0; }
.login-card { max-width: 380px; margin: 40px auto; }
button.icon-btn { background: none; border: 0; cursor: pointer; color: inherit; }
.me-name { padding: 4px 10px; font-weight: 600; }

@media (max-width: 400px) { .brand { font-size: .92rem; } .topbar { gap: 8px; } }
