:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --ink: #1d2327;
  --muted: #5d6870;
  --line: #d9dcd6;
  --grid: #e8eae4;
  --accent: #1f5f8b;
  --accent-soft: #e3eef6;
  --c1: #1f6fb2;
  --c2: #c4552d;
  --c3: #2f8a5b;
  --ok: #2f8a5b;
  --bad: #b8402f;
  --warn-bg: #fdf3e2;
  --tip-bg: #e9f3ee;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15191c;
    --surface: #1d2327;
    --ink: #e6e8e3;
    --muted: #9aa5ac;
    --line: #333b40;
    --grid: #2a3135;
    --accent: #7db4dc;
    --accent-soft: #23323d;
    --c1: #6aaee6;
    --c2: #ee8d63;
    --c3: #6cc697;
    --ok: #6cc697;
    --bad: #f08a78;
    --warn-bg: #3a2f1c;
    --tip-bg: #1e3329;
  }
}
:root[data-theme="dark"] {
  --bg: #15191c;
  --surface: #1d2327;
  --ink: #e6e8e3;
  --muted: #9aa5ac;
  --line: #333b40;
  --grid: #2a3135;
  --accent: #7db4dc;
  --accent-soft: #23323d;
  --c1: #6aaee6;
  --c2: #ee8d63;
  --c3: #6cc697;
  --ok: #6cc697;
  --bad: #f08a78;
  --warn-bg: #3a2f1c;
  --tip-bg: #1e3329;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--ink); line-height: 1.65; }
a { color: var(--accent); }
h1 { font-size: 1.7rem; margin: .2em 0 .3em; line-height: 1.3; }
h2 { font-size: 1.15rem; margin: 1.6em 0 .6em; }
h3 { font-size: 1.05rem; margin: 1.8em 0 .4em; }
h4 { font-size: .95rem; margin: 1.2em 0 .2em; color: var(--muted); }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; text-decoration: none; color: var(--ink); }
#menu { display: none; }

.layout { display: grid; grid-template-columns: 240px minmax(0, 1fr); max-width: 1180px; margin: 0 auto; }
#nav { position: sticky; top: 49px; align-self: start; padding: 20px 12px; display: flex; flex-direction: column; gap: 2px; }
#nav a { text-decoration: none; color: var(--ink); padding: 8px 10px; border-radius: 8px; font-size: .93rem; }
#nav a:hover { background: var(--accent-soft); }
#nav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.no {
  display: inline-grid; place-items: center; width: 1.6em; height: 1.6em; margin-right: .5em;
  border-radius: 50%; background: var(--accent); color: var(--surface); font-size: .8em; font-weight: 700;
}
main { padding: 24px 28px 64px; min-width: 0; }

.hero p { max-width: 46em; color: var(--muted); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(280px, 100%), 1fr)); gap: 14px; margin: 20px 0; }
.card {
  display: block; text-decoration: none; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px;
  transition: border-color .15s, transform .15s;
}
.card:hover { border-color: var(--accent); transform: translateY(-2px); }
.cardhead { display: flex; align-items: center; }
.card h2 { margin: 0; font-size: 1.05rem; }
.card p { margin: .5em 0; font-size: .92rem; color: var(--muted); }
.card .unit { color: var(--accent); }
.badge { font-size: .78rem; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.badge.ok { border-color: var(--ok); color: var(--ok); }
.fine { font-size: .82rem; color: var(--muted); max-width: 52em; }

.crumb { font-size: .85rem; color: var(--muted); margin: 0; }
.lead { color: var(--muted); }
.chips { list-style: none; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chips li { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 3px 12px; font-size: .88rem; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 20px 18px; margin-top: 1.6em; }
.panel > h2 { margin-top: .9em; }

.inputs { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(170px, 100%), 1fr)); gap: 10px 14px; margin: 12px 0; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: .86rem; min-width: 0; }
.field select { width: 100%; }
.field span { color: var(--muted); }
.field input[type="number"], .field select {
  font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 8px; min-width: 0;
}
.field input[type="range"] { accent-color: var(--accent); }
.row2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr)); gap: 12px; align-items: start; }

.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 16px 0 4px; border-bottom: 1px solid var(--line); }
.tabs button {
  font: inherit; font-size: .93rem; color: var(--muted); background: none; cursor: pointer;
  border: 1px solid transparent; border-bottom: none; border-radius: 8px 8px 0 0; padding: 7px 14px; margin-bottom: -1px;
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] { color: var(--accent); font-weight: 600; background: var(--surface); border-color: var(--line); }
/* 課程卡片 */
.modes { display: inline-flex; gap: 0; margin: 14px 0 6px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.modes button { font: inherit; font-size: .9rem; border: 0; background: var(--bg); color: var(--muted); padding: 6px 16px; cursor: pointer; }
.modes button[aria-pressed="true"] { background: var(--accent); color: var(--surface); font-weight: 600; }
.lesson { margin: 8px 0; }
.lesson h3 { margin: .6em 0 .4em; }
.lprog { display: flex; align-items: center; gap: 10px; font-size: .82rem; color: var(--muted); }
.lbar { flex: 1; height: 6px; background: var(--grid); border-radius: 999px; overflow: hidden; }
.lbar i { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .25s; }
.lbody p { max-width: 46em; }
.eq { background: var(--bg); border-left: 3px solid var(--accent); padding: 8px 12px; border-radius: 0 6px 6px 0; font-variant-numeric: tabular-nums; }
.lterms { border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; margin: 10px 0; font-size: .86rem; }
.lterms summary { cursor: pointer; color: var(--muted); }
.gloss { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr)); gap: 2px 18px; margin: 6px 0 2px; }
.gloss div { display: flex; gap: 8px; min-width: 0; }
.gloss dt { flex: 0 0 auto; min-width: 3.2em; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.gloss dd { margin: 0; color: var(--ink); overflow-wrap: anywhere; }
.lpredict { background: var(--accent-soft); border-radius: var(--radius); padding: 12px 14px; margin: 12px 0; }
.lpredict .lq { margin: 0 0 8px; }
.opts button.picked { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.lstage { margin: 10px 0; }
.ltask { border: 1px dashed var(--accent); border-radius: 8px; padding: 8px 12px; margin: 10px 0; font-size: .93rem; }
.ltask.ok { border-style: solid; border-color: var(--ok); }
.ltask.ok .lstatus { color: var(--ok); font-weight: 700; }
.linkbtn { font: inherit; font-size: .85rem; background: none; border: 0; color: var(--accent); cursor: pointer; text-decoration: underline; padding: 0; }
.lhinttext { color: var(--muted); }
.readout { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; font-size: .92rem; line-height: 1.8; align-self: center; }
.lnav { display: flex; justify-content: space-between; gap: 10px; margin-top: 16px; }
.primary { font: inherit; background: var(--accent); color: var(--surface); border: 0; border-radius: 8px; padding: 8px 18px; cursor: pointer; font-weight: 600; }
a.primary { display: inline-block; text-decoration: none; }
.lessondone .lnav { flex-wrap: wrap; }
.primary:disabled { opacity: .35; cursor: not-allowed; }
.ghostbtn:disabled { opacity: .35; cursor: not-allowed; }
.spans { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr)); gap: 10px; }
fieldset.span { border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px 10px; margin: 0; display: grid; gap: 6px; }
fieldset.span legend { font-size: .86rem; color: var(--muted); padding: 0 4px; }
.tablewrap { overflow-x: auto; }
.mdtable { border-collapse: collapse; font-size: .84rem; font-variant-numeric: tabular-nums; }
.mdtable th, .mdtable td { border: 1px solid var(--line); padding: 4px 8px; text-align: right; white-space: nowrap; }
.mdtable td:first-child { text-align: left; color: var(--muted); }
.mdtable tr.final td { font-weight: 700; color: var(--ink); }
/* 科目參考文件（Markdown＋MathML） */
.doctoc { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 14px 0 6px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.doc { line-height: 1.75; overflow-wrap: anywhere; }
.doc h2, .doc h3 { scroll-margin-top: 70px; }
.doc h3 { margin-top: 1.6em; padding-top: .4em; border-top: 1px solid var(--line); }
.doc h4 { margin: 1.2em 0 .4em; color: var(--accent); }
.doc p, .doc li { max-width: 48em; }
.doc ul, .doc ol { padding-left: 1.4em; }
.doc li { margin: 3px 0; }
.doc hr { display: none; }
.doc code { font-size: .88em; background: var(--grid); padding: 0 4px; border-radius: 4px; }
.doc .tablewrap { margin: 10px 0; }
.doc .mdtable { font-size: .86rem; }
.doc .mdtable th, .doc .mdtable td { white-space: normal; text-align: left; vertical-align: top; min-width: 5em; }
.doc .mdtable td:first-child { color: var(--ink); font-weight: 600; }
.doc .mdtable th { background: var(--bg); }
.mathblock { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 6px 2.2em; overflow-x: auto; overflow-y: hidden; margin: 10px 0; padding: 8px 12px; background: var(--bg); border-left: 3px solid var(--accent); border-radius: 0 6px 6px 0; }
.doc math { font-size: 1.08em; overflow-wrap: normal; }
.mathblock math { font-size: 1.12em; margin: 0; }
@media (max-width: 480px) { .mathblock math { font-size: .94em; } .mathblock { padding: 8px 6px; } }
.steps { padding-left: 1.4em; margin: 12px 0; }
.steps li { margin: 4px 0; }
.callout { border-radius: 8px; padding: 10px 14px; margin: 12px 0; font-size: .92rem; }
.callout.tip { background: var(--tip-bg); }
.callout.warn { background: var(--warn-bg); }

/* 圖表 */
.plot { width: 100%; height: auto; display: block; }
.plot .grid { stroke: var(--grid); stroke-width: 1; }
.plot .axis { stroke: var(--muted); stroke-width: 1; }
.plot .vline { stroke: var(--muted); stroke-dasharray: 4 4; }
.plot .tick { fill: var(--muted); font-size: 11px; }
.plot .alabel { fill: var(--muted); font-size: 12px; }
.plot .mlabel { fill: var(--ink); font-size: 12px; font-weight: 600; }
.plot .line { fill: none; stroke-width: 2; }
.plot .line.s1 { stroke: var(--c1); }
.plot .line.s2 { stroke: var(--c2); }
.plot .line.s3 { stroke: var(--c3); }
.plot .line.ghost { stroke: var(--muted); stroke-dasharray: 5 4; stroke-width: 1.2; }
.plot .area { stroke: none; opacity: .16; }
.plot .area.s1 { fill: var(--c1); }
.plot .area.s2 { fill: var(--c2); }
.plot .area.s3 { fill: var(--c3); }
.plot .marker { fill: var(--surface); stroke: var(--ink); stroke-width: 2; }
.plot .ahead { fill: var(--ink); }
.mohr { max-width: 380px; }
.plot .ring { fill: none; stroke: var(--c1); stroke-width: 2; }
.plot .dia { stroke: var(--muted); stroke-width: 1.2; }
.plot .dia.now { stroke: var(--c2); stroke-width: 2; }
.plot .pp { fill: var(--c1); }
.plot .p0 { fill: var(--muted); }
.plot .pn { fill: var(--c2); }
.plot .elem { fill: var(--accent-soft); stroke: var(--ink); stroke-width: 1.5; }
.plot .sn { stroke: var(--c2); stroke-width: 2.5; }
.plot .sh { stroke: var(--c3); stroke-width: 2.5; }
.plot .beamline { stroke: var(--ink); stroke-width: 6; stroke-linecap: round; }
.plot .support { fill: none; stroke: var(--ink); stroke-width: 1.5; }
.plot .wall { fill: var(--muted); }
.plot .load { stroke: var(--c2); stroke-width: 2.5; }
.plot .udl { fill: var(--c2); opacity: .25; }
.plot .block { fill: var(--c1); opacity: .28; }
.plot .na { stroke: var(--c2); stroke-dasharray: 5 4; }
.plot .bar { fill: var(--ink); }
.plot .barfill.s1 { fill: var(--c1); }
.plot .barfill.s2 { fill: var(--c2); }
.plot .barfill.s3 { fill: var(--c3); }
.legend { font-size: .82rem; color: var(--muted); margin: 4px 0; }
.legend .k { display: inline-block; width: 14px; height: 3px; vertical-align: middle; margin: 0 4px 0 10px; }
.legend .k.s1 { background: var(--c1); }
.legend .k.s2 { background: var(--c2); }
.legend .k.s3 { background: var(--c3); }

/* 測驗 */
.q { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; margin: 10px 0; }
.q p { margin: 0 0 8px; }
.opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(200px, 100%), 1fr)); gap: 6px; }
.opts button, .ghostbtn {
  font: inherit; text-align: left; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px; cursor: pointer;
}
.opts button:hover:not(:disabled) { border-color: var(--accent); }
.opts button:disabled { cursor: default; }
.opts button.right { border-color: var(--ok); color: var(--ok); font-weight: 600; }
.opts button.wrong { border-color: var(--bad); color: var(--bad); text-decoration: line-through; }
.exp { margin-top: 8px; font-size: .92rem; color: var(--muted); }
.score { font-weight: 700; margin: 12px 0; }
.ghostbtn { margin-top: 6px; }

.pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 32px; }

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  #menu { display: inline-block; font: inherit; background: none; border: 1px solid var(--line); border-radius: 6px; padding: 4px 10px; color: var(--ink); }
  #nav { display: none; position: static; padding: 8px 16px; border-bottom: 1px solid var(--line); background: var(--surface); }
  body.navopen #nav { display: flex; }
  main { padding: 16px 16px 48px; }
  .panel { padding: 4px 12px 14px; }
  h1 { font-size: 1.4rem; }
}
