/* =========================================================
   LLM 101 · PART 03｜用好 AI
   1. Tokens   2. Base   3. Header   4. Hero   5. Layout/TOC
   6. Section  7. Figure   8. Diagram   9. Callout/Cards
   10. Player/Quiz   11. Appendix   12. Responsive
   ========================================================= */

/* ---------- 1. Tokens ---------- */
:root {
  --bg: #f6f8fc;
  --surface: #ffffff;
  --surface-2: #eef4fb;
  --text: #0b1f33;
  --muted: #4b6478;
  --border: #d8e2ee;
  --primary: #1d4ed8;
  --primary-weak: #dbeafe;
  --accent: #0f766e;
  --warn: #b45309;
  --danger: #b91c1c;
  --shadow: 0 10px 30px rgba(11, 31, 51, .08);

  --on-primary: #ffffff;
  --header-bg: rgba(246, 248, 252, .88);
  --code-bg: #eef4fb;
  --ok: #15803d;
  --ok-weak: #dcfce7;
  --danger-weak: #fee2e2;
  --warn-weak: #fef3c7;
  --focus: #1d4ed8;

  --font-sans: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", system-ui, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --maxw: 1180px;
  --radius: 16px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #08111f;
  --surface: #0f1b2d;
  --surface-2: #15243a;
  --text: #e8eff8;
  --muted: #9db0c6;
  --border: #253850;
  --primary: #7cb3ff;
  --primary-weak: #132a4a;
  --accent: #4fd1c5;
  --warn: #fbbf24;
  --danger: #fca5a5;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);

  --on-primary: #08111f;
  --header-bg: rgba(8, 17, 31, .88);
  --code-bg: #0b1728;
  --ok: #6ee7b7;
  --ok-weak: #0c2f26;
  --danger-weak: #3a1518;
  --warn-weak: #3a2a0a;
  --focus: #93c5fd;
  color-scheme: dark;
}

/* ---------- 2. Base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 1.0625rem;
  line-height: 1.85;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { line-height: 1.3; text-wrap: balance; margin: 0 0 .6em; }
p { margin: 0 0 1em; }
ul, ol { margin: 0 0 1em; padding-left: 1.4em; }
li { margin-bottom: .35em; }
a { color: var(--primary); text-underline-offset: 3px; }
code, kbd, samp {
  font-family: var(--font-mono);
  font-size: .88em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .08em .35em;
}
strong { font-weight: 700; }
img, svg { max-width: 100%; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
  border-radius: 6px;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 12px;
  top: -60px;
  z-index: 100;
  background: var(--primary);
  color: var(--on-primary);
  padding: 10px 16px;
  border-radius: 0 0 10px 10px;
  text-decoration: none;
  font-weight: 700;
  transition: top .15s ease;
}
.skip-link:focus { top: 0; }

/* ---------- 3. Header ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--header-bg);
  backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.header-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  text-decoration: none;
  font-weight: 800;
  letter-spacing: -.01em;
}
.brand-mark {
  width: 32px; height: 32px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: var(--primary-weak);
}
.brand-mark svg { width: 24px; height: 24px; }
.brand-bar-1 { fill: var(--primary); opacity: .38; }
.brand-bar-2 { fill: var(--primary); opacity: .66; }
.brand-bar-3 { fill: var(--primary); }
.brand-text { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; line-height: 1.15; }
.brand-text b { font-size: 1.02rem; letter-spacing: .01em; }
.brand-text span { font-size: .72rem; color: var(--muted); font-weight: 600; }
.header-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.icon-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: border-color .15s ease, color .15s ease, background .15s ease;
}
.icon-btn:hover { border-color: var(--primary); color: var(--primary); }
.header-toc {
  display: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .86rem;
  font-weight: 700;
  padding: 7px 12px;
  border-radius: 9px;
  cursor: pointer;
}
.header-toc:hover { border-color: var(--primary); color: var(--primary); }
.icon {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-sun { display: none; }
.icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

.reading-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: -1px;
  height: 3px;
  background: var(--surface-2);
}
.reading-progress span {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  transition: width .12s linear;
}

/* ---------- 4. Hero ---------- */
.hero {
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
}
.hero-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(520px 320px at 12% 8%, rgba(29, 78, 216, .13), transparent 70%),
    radial-gradient(430px 300px at 90% 22%, rgba(15, 118, 110, .11), transparent 70%),
    linear-gradient(180deg, transparent, var(--bg));
}
.hero-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(rgba(29, 78, 216, .045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(29, 78, 216, .045) 1px, transparent 1px);
  background-size: 44px 44px;
  -webkit-mask-image: linear-gradient(180deg, rgba(0, 0, 0, .7), transparent 78%);
  mask-image: linear-gradient(180deg, rgba(0, 0, 0, .7), transparent 78%);
}
[data-theme="dark"] .hero-bg {
  background:
    radial-gradient(520px 320px at 12% 8%, rgba(124, 179, 255, .14), transparent 70%),
    radial-gradient(430px 300px at 90% 22%, rgba(79, 209, 197, .10), transparent 70%),
    linear-gradient(180deg, transparent, var(--bg));
}
[data-theme="dark"] .hero-bg::after {
  background-image:
    linear-gradient(rgba(124, 179, 255, .06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(124, 179, 255, .06) 1px, transparent 1px);
}
.hero-inner {
  position: relative;
  max-width: var(--maxw);
  min-width: 0;
  margin: 0 auto;
  padding: 72px 24px 64px;
  display: grid;
  grid-template-columns: minmax(0, 1.08fr) minmax(320px, .92fr);
  gap: 56px;
  align-items: center;
}
.eyebrow {
  display: block;
  margin: 0 0 14px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--primary);
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .14em;
}
.hero h1 {
  font-size: clamp(2.2rem, 4.7vw, 3.45rem);
  line-height: 1.1;
  letter-spacing: -.035em;
  margin: 0;
}
.hero-sub {
  display: block;
  margin: .32em 0 20px;
  font-size: calc(clamp(2.2rem, 4.7vw, 3.45rem) * .56);
  line-height: 1.25;
  letter-spacing: -.015em;
  color: var(--primary);
  font-weight: 800;
  text-wrap: balance;
}
.hero .lead {
  font-size: 1.11rem;
  line-height: 1.8;
  color: var(--muted);
  max-width: 54ch;
}
.hero-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 22px 0 26px;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
}
.hero-actions { display: flex; flex-wrap: wrap; gap: 10px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: .68rem 1.15rem;
  border: 1px solid transparent;
  border-radius: 11px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .95rem;
  font-weight: 700;
  line-height: 1.4;
  cursor: pointer;
  text-decoration: none;
  transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease, color .15s ease, background .15s ease;
}
.btn-primary {
  background: var(--primary);
  border-color: transparent;
  color: var(--on-primary);
  box-shadow: 0 6px 18px rgba(29, 78, 216, .22);
}
.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 24px rgba(29, 78, 216, .28);
  filter: none;
}
.btn-ghost { border-color: var(--border); }
.btn-ghost:hover { border-color: var(--primary); color: var(--primary); }
.btn-sm {
  padding: .45rem .8rem;
  font-size: .86rem;
  border-radius: 9px;
}
.btn[disabled] { opacity: .45; cursor: not-allowed; }

.hero-map {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 20px 20px 16px;
  box-shadow: var(--shadow);
}
.hero-map-title {
  margin: 0 0 10px;
  font-size: .74rem;
  font-weight: 800;
  letter-spacing: .13em;
  color: var(--muted);
}
.hero-map ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.hero-map li { margin-bottom: 0; }
.hero-map a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 9px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--text);
  text-decoration: none;
  font-size: .92rem;
  line-height: 1.45;
  transition: background .15s ease, color .15s ease;
}
.hero-map a:hover { background: var(--surface-2); color: var(--primary); }
.hero-map .hm-num {
  flex: none;
  width: 2.3em;
  font-family: var(--font-mono);
  font-size: .76rem;
  font-weight: 800;
  color: var(--primary);
}
.hero-map .hm-label { min-width: 0; font-weight: 700; }
.hero-map .hm-sub {
  display: inline;
  margin-left: 6px;
  font-size: .76rem;
  font-weight: 500;
  color: var(--muted);
}
.hero-map-foot {
  margin: 14px 0 0;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: .8rem;
  line-height: 1.7;
  color: var(--muted);
}

/* ---------- 5. Layout / TOC ---------- */
.layout {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 48px 24px 72px;
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  gap: 48px;
  align-items: start;
}
.sidebar { position: sticky; top: 88px; }
.toc-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px;
  font-size: .88rem;
  box-shadow: var(--shadow);
}
/* D-04：TOC 改成 <details class="toc-card" open>＋<summary> */
.toc-card > summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: .9rem;
  font-weight: 800;
  color: var(--text);
}
.toc-card > summary::-webkit-details-marker { display: none; }
.toc-card > summary::after {
  content: "▾";
  margin-left: 2px;
  color: var(--muted);
  font-size: .8rem;
  transition: transform .2s ease;
}
.toc-card[open] > summary::after { transform: rotate(180deg); }
.toc-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 10px;
  font-size: .76rem;
  font-weight: 800;
  letter-spacing: .06em;
  color: var(--muted);
}
.toc-count {
  margin-left: auto;
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: 0;
  color: var(--muted);
}
.toc-nav { display: grid; gap: 4px; padding: 4px 2px 8px; }
.toc-group { border-radius: 10px; border-top: 1px solid var(--border); }
.toc-group:first-of-type { border-top: 0; }
.toc-group-summary {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 10px;
  grid-template-areas:
    "num count chevron"
    "title title chevron";
  align-items: center;
  column-gap: 8px;
  row-gap: 1px;
  padding: 10px 9px;
  list-style: none;
  cursor: pointer;
  border-radius: 8px;
  color: var(--text);
  font-size: .82rem;
  line-height: 1.45;
  transition: background .15s ease, color .15s ease;
}
.toc-group-summary::-webkit-details-marker { display: none; }
.toc-group-summary:hover { background: var(--surface-2); }
.toc-group-summary:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: -2px;
}
.toc-group-num {
  grid-area: num;
  font-family: var(--font-mono);
  font-size: .66rem;
  font-weight: 800;
  letter-spacing: .06em;
  color: var(--muted);
}
.toc-group-title {
  grid-area: title;
  min-width: 0;
  font-size: .88rem;
  font-weight: 700;
  line-height: 1.45;
}
.toc-group-count {
  grid-area: count;
  justify-self: end;
  font-size: .7rem;
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
}
.toc-group-summary::after {
  content: "";
  grid-area: chevron;
  justify-self: end;
  align-self: center;
  width: 6px;
  height: 6px;
  margin-bottom: 2px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg);
  opacity: .65;
  transition: transform .2s ease, opacity .2s ease;
}
.toc-group[open] > .toc-group-summary { color: var(--primary); }
.toc-group[open] > .toc-group-summary .toc-group-title { color: var(--primary); }
.toc-group[open] > .toc-group-summary::after {
  transform: rotate(225deg);
  opacity: 1;
}
.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 1px;
}
.toc-group .toc-list { padding: 0 0 8px; }
.toc-link {
  display: grid;
  grid-template-columns: 2.5em minmax(0, 1fr);
  gap: 6px;
  align-items: baseline;
  padding: 7px 9px;
  border-left: 3px solid transparent;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  font-size: .86rem;
  line-height: 1.45;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.toc-link:hover { background: var(--surface-2); color: var(--text); }
.toc-link[aria-current="true"] {
  background: var(--primary-weak);
  color: var(--primary);
  font-weight: 800;
  border-left-color: var(--primary);
  padding-left: 9px;
}
.toc-num { font-family: var(--font-mono); font-size: .74rem; font-weight: 800; opacity: .85; }

/* 手機目錄關閉鈕：桌面隱藏，overlay 內固定在卡片右上。 */
.toc-close { display: none; }

/* 桌面側欄：目錄卡開啟時 nav 自身捲動，避免長目錄超出視窗。 */
@media (min-width: 860px) {
  .toc-card[open] .toc-nav {
    max-height: calc(100vh - 190px);
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
  }
  .toc-card[open] .toc-nav::-webkit-scrollbar { width: 7px; }
  .toc-card[open] .toc-nav::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 999px;
  }
}

.layout > main { min-width: 0; }
.layout > .sidebar { min-width: 0; }
.article { max-width: 760px; min-width: 0; }

/* ---------- 6. Section ---------- */
.unit-block { margin-bottom: 26px; }
.unit-band {
  margin: 0 0 30px;
  padding: 30px 32px 28px;
  border: 1px solid var(--border);
  border-left: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: var(--shadow);
  background:
    radial-gradient(1100px 260px at 100% -10%, var(--primary-weak), transparent 62%),
    var(--surface);
}
.unit-kicker {
  margin: 0 0 6px;
  font-size: .76rem;
  font-weight: 800;
  letter-spacing: .1em;
  color: var(--accent);
  text-transform: uppercase;
}
.unit-band h2 {
  font-size: clamp(1.85rem, 3.8vw, 2.7rem);
  margin: 0 0 8px;
}
.unit-band-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  margin: 0 0 6px;
}
.unit-number {
  margin: 0;
  font-family: var(--font-mono);
  font-size: .82rem;
  font-weight: 800;
  letter-spacing: .12em;
  color: var(--primary);
}
.unit-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.unit-meta li {
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: .74rem;
  font-weight: 600;
  color: var(--muted);
}
.unit-sub {
  margin: 6px 0 0;
  color: var(--primary);
  font-size: clamp(1.05rem, 2vw, 1.3rem);
  font-weight: 700;
}
.unit-lead { margin: 0; color: var(--muted); font-size: .95rem; }
/* D-03：band 內導言回到 P02 的節奏（也讓 UNIT band 高度 ≥300px） */
.unit-band .unit-lead {
  max-width: 68ch;
  margin: 14px 0 0;
  font-size: 1.02rem;
  line-height: 1.85;
}

.lesson-section {
  min-width: 0;
  padding-bottom: 46px;
  margin-bottom: 40px;
  border-bottom: 1px solid var(--border);
  scroll-margin-top: 96px;
}
.lesson-section:last-child { border-bottom: 0; }
.section-head { margin-bottom: 12px; }
.section-kicker {
  display: block;
  margin: 0 0 4px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  color: var(--primary);
  font-family: var(--font-mono);
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .1em;
}
.section-head h3 {
  font-size: clamp(1.5rem, 2.7vw, 1.95rem);
  letter-spacing: -.02em;
  margin: 0;
}
.section-lead {
  font-size: 1.1rem;
  line-height: 1.85;
  color: var(--text);
  padding-left: 0;
  border-left: 0;
  margin-bottom: 18px;
}

.cite {
  font-size: .72em;
  vertical-align: super;
  line-height: 0;
  text-decoration: none;
  color: var(--primary);
  font-weight: 700;
  padding: 0 1px;
}
.cite:hover { text-decoration: underline; }

/* ---------- 7. Figure ---------- */
.figure {
  margin: 30px 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.figure-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 9px;
  padding: 12px 18px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: .8rem;
}
.figure-tag {
  flex: none;
  padding: 3px 9px;
  border: 0;
  border-radius: 999px;
  background: var(--primary-weak);
  color: var(--primary);
  font-weight: 800;
  font-family: var(--font-mono);
  font-size: .7rem;
  letter-spacing: .07em;
  white-space: nowrap;
}
.figure-title { font-size: .9rem; font-weight: 700; line-height: 1.5; margin-right: auto; }
.scroll-hint {
  margin-left: auto;
  color: var(--muted);
  font-size: .72rem;
  display: none;
}
.figure-body { padding: 20px; min-width: 0; }
.figure-scroll { overflow-x: auto; }
.figure-inner { min-width: 0; }
figcaption {
  display: block;
  padding: 0 20px 18px;
  font-size: .85rem;
  line-height: 1.75;
  color: var(--muted);
}
figcaption b { color: var(--text); }

.js .figure.reveal { opacity: 0; transform: translateY(12px); transition: opacity .5s ease, transform .5s ease; }
.js .figure.reveal.is-visible { opacity: 1; transform: none; }

/* ---------- 8. Diagram (HTML/CSS) ---------- */
.dgm {
  --gap: 12px;
  display: grid;
  gap: var(--gap);
  font-size: .86rem;
  line-height: 1.55;
}
.dgm-title {
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .04em;
  color: var(--muted);
  text-transform: uppercase;
  margin: 0;
}
.dgm-note {
  margin: 0;
  font-size: .76rem;
  color: var(--muted);
}
.dgm-row {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: var(--gap);
  align-items: stretch;
}
.dgm-row-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.dgm-row-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.dgm-card {
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg);
  padding: 11px 12px;
}
.dgm-card h4 {
  margin: 0 0 4px;
  font-size: .84rem;
  line-height: 1.4;
}
.dgm-card p { margin: 0; font-size: .78rem; color: var(--muted); line-height: 1.55; }
.dgm-card.is-primary { background: var(--primary-weak); border-color: var(--primary); }
.dgm-card.is-accent { background: var(--ok-weak); border-color: var(--ok); }
.dgm-card.is-warn { background: var(--warn-weak); border-color: var(--warn); }
.dgm-card.is-mute { background: var(--surface-2); color: var(--muted); }
.dgm-card.is-mute h4 { color: var(--muted); }

.dgm-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 11px;
  border-radius: 9px;
  font-size: .8rem;
  font-weight: 800;
  background: var(--primary);
  color: var(--on-primary);
}
.dgm-head.is-alt { background: var(--accent); }
.dgm-head.is-warn { background: var(--warn); color: var(--on-primary); }
.dgm-head.is-mute { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }

.dgm-arrow {
  align-self: center;
  justify-self: center;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
}
.dgm-arrow::before { content: "→"; }
.dgm-steps {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  counter-reset: step;
}
.dgm-step {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  padding: 10px 11px 10px 34px;
  font-size: .8rem;
}
.dgm-step::before {
  counter-increment: step;
  content: counter(step);
  position: absolute;
  left: 9px;
  top: 10px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--on-primary);
  font-size: .68rem;
  font-weight: 800;
  display: grid;
  place-items: center;
  font-family: var(--font-mono);
}
.dgm-step b { display: block; font-size: .82rem; margin-bottom: 2px; }
.dgm-step span { color: var(--muted); font-size: .76rem; }
.dgm-step.is-ok::before { background: var(--ok); }
.dgm-step.is-warn::before { background: var(--warn); }
.dgm-step.is-danger::before { background: var(--danger); }

.dgm-bar {
  display: flex;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg);
}
.dgm-bar-seg {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .72rem;
  font-weight: 700;
  color: var(--text);
  border-right: 1px solid var(--surface);
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
}
.dgm-bar-seg:last-child { border-right: 0; }
.seg-1 { background: var(--primary-weak); }
.seg-2 { background: var(--ok-weak); }
.seg-3 { background: var(--warn-weak); }
.seg-4 { background: var(--surface-2); }
.seg-5 { background: var(--danger-weak); }
.dgm-scale {
  position: relative;
  height: 26px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  font-size: .68rem;
  color: var(--muted);
}
.dgm-scale span { position: absolute; top: 4px; white-space: nowrap; }
.dgm-scale span:nth-child(1) { left: 0; transform: none; }
.dgm-scale span:nth-child(2) { left: 50%; transform: translateX(-50%); }
.dgm-scale span:nth-child(3) { left: 100%; transform: translateX(-100%); }

.dgm-col-stack { display: grid; gap: 8px; }
.dgm-tier {
  display: grid;
  grid-template-columns: 96px 1fr;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 11px;
  background: var(--bg);
}
.dgm-tier b { font-size: .8rem; }
.dgm-tier span { font-size: .78rem; color: var(--muted); }

.dgm-loop {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  align-items: stretch;
}
.dgm-loop .dgm-card { position: relative; }
.dgm-loop .dgm-card::after {
  content: "→";
  position: absolute;
  right: -9px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: .9rem;
}
.dgm-loop .dgm-card:last-child::after { content: "↺"; }
.dgm-loop-foot {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  font-size: .76rem;
  color: var(--muted);
}

.dgm-matrix {
  display: grid;
  grid-template-columns: 88px 1fr 1fr 1fr;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.dgm-matrix > div {
  padding: 9px 11px;
  border-bottom: 1px solid var(--border);
  font-size: .8rem;
}
.dgm-matrix > div:nth-child(4n+1) { background: var(--surface-2); font-weight: 700; }
.dgm-matrix--tools { grid-template-columns: 116px 1fr 1fr; }
.dgm-matrix--tools > div:nth-child(3n+1) { background: var(--surface-2); font-weight: 700; }


.dgm-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin: 6px 0 0;
  font-size: .74rem;
  color: var(--muted);
}
.dgm-legend span { display: inline-flex; align-items: center; gap: 5px; }
.dgm-legend .dot {
  width: 9px; height: 9px;
  border-radius: 2px;
  display: inline-block;
}
.dgm-loop-return {
  margin: -2px 0 0;
  font-size: .76rem;
  color: var(--muted);
  text-align: center;
}
.dgm-perm--skills .dgm-perm-row { grid-template-columns: 84px repeat(4, minmax(0, 1fr)); }

.dgm-perm { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.dgm-perm-row {
  display: grid;
  grid-template-columns: 84px repeat(3, minmax(0, 1fr));
  border-bottom: 1px solid var(--border);
}
.dgm-perm-row:last-child { border-bottom: 0; }
.dgm-perm-row > * {
  padding: 9px 11px;
  border-right: 1px solid var(--border);
  font-size: .8rem;
  display: flex;
  align-items: center;
}
.dgm-perm-row > *:last-child { border-right: 0; }
.dgm-perm-row b { background: var(--surface-2); font-size: .8rem; }
.dgm-perm-row.is-head span { background: var(--surface-2); font-weight: 700; font-size: .76rem; color: var(--muted); }
.dgm-matrix > div:last-child { border-bottom: 0; }

.dgm-json {
  max-width: 100%;
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--code-bg);
  font-family: var(--font-mono);
  font-size: .74rem;
  line-height: 1.6;
  overflow-x: auto;
  white-space: pre;
}

.dgm-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .7rem;
  font-weight: 800;
  border: 1px solid var(--border);
  background: var(--surface);
  white-space: nowrap;
}
.dgm-badge.is-allow { background: var(--ok-weak); border-color: var(--ok); color: var(--ok); }
.dgm-badge.is-ask { background: var(--warn-weak); border-color: var(--warn); color: var(--warn); }
.dgm-badge.is-deny { background: var(--danger-weak); border-color: var(--danger); color: var(--danger); }

.dgm-list { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.dgm-list li {
  display: grid;
  grid-template-columns: 18px 1fr;
  gap: 8px;
  font-size: .8rem;
  line-height: 1.5;
}
.dgm-list li::before { content: "•"; color: var(--accent); font-weight: 800; }
.dgm-list li.is-cross::before { content: "✕"; color: var(--danger); }
.dgm-list li.is-check::before { content: "✓"; color: var(--ok); }

/* ---------- 9. Callout / Cards ---------- */
.callout {
  margin: 24px 0;
  padding: 15px 18px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: 12px;
  background: var(--surface);
  font-size: .93rem;
}
.callout p { margin: 0; font-size: .93rem; line-height: 1.8; }
.callout p + p { margin-top: .5em; }
.callout strong {
  display: block;
  margin-bottom: 3px;
  font-size: .8rem;
  font-weight: 800;
  letter-spacing: .08em;
  color: var(--primary);
}
.callout-note { border-left-color: var(--primary); background: var(--surface-2); }
.callout-warn { border-left-color: var(--warn); background: var(--warn-weak); }
.callout-danger { border-left-color: var(--danger); background: var(--danger-weak); }

.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin: 20px 0; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin: 20px 0; }
.mini-card {
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface);
  padding: 13px 14px;
}
.mini-card h4 { margin: 0 0 4px; font-size: .86rem; }
.mini-card p { margin: 0; font-size: .8rem; color: var(--muted); line-height: 1.6; }

.table-wrap {
  margin: 20px 0;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 11px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
  min-width: 460px;
}
th, td {
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
th { background: var(--surface-2); font-size: .78rem; letter-spacing: .02em; }
tr:last-child td { border-bottom: 0; }
td code { font-size: .8em; }

/* ---------- 10. Player / Interactive ---------- */
.lab {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  padding: 14px;
  margin: 22px 0;
}
.lab-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.lab-title { font-weight: 800; font-size: .9rem; margin: 0; }
.lab-note { margin-left: auto; font-size: .72rem; color: var(--muted); }
.lab-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }

.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.seg-btn {
  min-height: 38px;
  padding: 0 12px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: .84rem;
  font-weight: 700;
  cursor: pointer;
}
.seg-btn:hover { color: var(--text); background: var(--surface-2); }
.seg-btn[aria-pressed="true"], .seg-btn.is-active {
  background: var(--primary);
  color: var(--on-primary);
}

.chk {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 11px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  font-size: .82rem;
  cursor: pointer;
  min-height: 40px;
}
.chk input { width: 16px; height: 16px; accent-color: var(--primary); }
.chk.is-on { border-color: var(--primary); background: var(--primary-weak); }


.select {
  min-height: 38px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .84rem;
}

.out {
  max-width: 100%;
  overflow-x: auto;
  margin: 8px 0 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--code-bg);
  font-family: var(--font-mono);
  font-size: .76rem;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 3em;
}
.out.is-json { white-space: pre; overflow-x: auto; }

.live {
  min-height: 1.6em;
  font-size: .82rem;
  color: var(--muted);
}
.live b { color: var(--text); }

.verdict {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  font-size: .84rem;
}
.verdict[data-state="allow"] { border-color: var(--ok); background: var(--ok-weak); }
.verdict[data-state="ask"] { border-color: var(--warn); background: var(--warn-weak); }
.verdict[data-state="deny"] { border-color: var(--danger); background: var(--danger-weak); }

.opt-list { display: grid; gap: 6px; margin: 0 0 10px; padding: 0; list-style: none; }
.opt-list button {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .84rem;
  text-align: left;
  cursor: pointer;
}
.opt-list button[aria-pressed="true"] { border-color: var(--primary); background: var(--primary-weak); }

.player-track {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}
.step-card {
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  padding: 8px 10px;
  font-size: .78rem;
  opacity: .55;
  transition: opacity .2s ease, border-color .2s ease;
}
.step-card.is-active { opacity: 1; border-color: var(--primary); background: var(--primary-weak); }
.step-card.is-done { opacity: .9; border-color: var(--ok); }
.step-card .step-num { font-family: var(--font-mono); font-size: .7rem; color: var(--primary); font-weight: 700; display: block; }
.player-controls { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.player-status { font-size: .84rem; margin: 6px 0 0; }
.player-status b { display: inline-block; margin-right: 6px; }

/* Quiz */
.quiz-app { margin: 20px 0; }
.quiz-score {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  font-size: .82rem;
  color: var(--muted);
  margin-bottom: 12px;
}
.quiz-q {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  padding: 14px;
  margin: 0 0 14px;
}
.q-num { font-family: var(--font-mono); font-size: .76rem; color: var(--primary); font-weight: 700; }
.quiz-q legend { font-weight: 700; font-size: .92rem; padding: 0; margin: 4px 0 10px; }
.options { display: grid; gap: 7px; }
.option {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  width: 100%;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: .86rem;
  text-align: left;
  cursor: pointer;
}
.option:hover { border-color: var(--primary); }
.option-key {
  flex: 0 0 auto;
  width: 20px; height: 20px;
  border-radius: 5px;
  background: var(--surface-2);
  display: grid;
  place-items: center;
  font-family: var(--font-mono);
  font-size: .72rem;
  font-weight: 700;
}
.option.is-correct { border-color: var(--ok); background: var(--ok-weak); }
.option.is-wrong { border-color: var(--danger); background: var(--danger-weak); }
.option[aria-disabled="true"] { cursor: default; }
.feedback {
  margin-top: 8px;
  padding: 9px 11px;
  border-radius: 8px;
  background: var(--surface-2);
  font-size: .82rem;
  line-height: 1.6;
}
.feedback[hidden] { display: none; }
.quiz-summary {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: .86rem;
}

/* ---------- 11. Appendix ---------- */
.glossary { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.glossary > div {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  padding: 10px 12px;
}
.glossary dt { font-weight: 800; font-size: .86rem; }
.glossary dt span { font-weight: 500; color: var(--muted); font-size: .78rem; }
.glossary dd { margin: 3px 0 0; font-size: .8rem; color: var(--muted); line-height: 1.6; }

.ref-list { list-style: none; margin: 0; padding: 0; counter-reset: ref; }
.ref-list li {
  display: grid;
  grid-template-columns: 40px 1fr;
  gap: 10px;
  padding: 11px 0;
  border-bottom: 1px solid var(--border);
  scroll-margin-top: 100px;
  font-size: .84rem;
}
.ref-list li:last-child { border-bottom: 0; }
.ref-num { font-family: var(--font-mono); color: var(--primary); font-weight: 700; font-size: .8rem; }
.ref-list p { margin: 3px 0 0; font-size: .76rem; color: var(--muted); }
.ref-list a { word-break: break-word; }

.site-footer {
  margin-top: 20px;
  padding: 28px 0 56px;
  border-top: 1px solid var(--border);
  background: transparent;
  display: flex;
  justify-content: space-between;
  gap: 18px;
  font-size: .8rem;
  line-height: 1.7;
  color: var(--muted);
}
.site-footer p { margin: 0; }
.footer-brand { font-weight: 800; color: var(--text); }
.footer-note { max-width: 44ch; }

/* ---------- 12. Responsive ---------- */
@media (max-width: 1080px) {
  .hero-inner { gap: 36px; }
  .layout { grid-template-columns: 214px minmax(0, 1fr); gap: 32px; }
}

@media (max-width: 859px) {
  body { font-size: 1.02rem; }
  .header-inner { height: 58px; padding: 0 16px; }
  .brand-mark { width: 29px; height: 29px; }
  .brand-text span { display: block; }
  .header-toc { display: inline-flex; }

  .hero-inner {
    grid-template-columns: 1fr;
    gap: 30px;
    padding: 46px 18px 40px;
  }
  .hero h1 { font-size: clamp(1.95rem, 8vw, 2.6rem); }
  .hero-sub { font-size: calc(clamp(1.95rem, 8vw, 2.6rem) * .6); }
  .hero .lead { font-size: 1rem; max-width: none; }
  .layout { grid-template-columns: 1fr; padding: 28px 18px 56px; gap: 22px; }
  .article { max-width: none; }

  .sidebar { position: static; order: 0; }
  .toc-card { display: none; }
  body.toc-open { overflow: hidden; }
  body.toc-open::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 90;
    background: rgba(8, 17, 31, .45);
  }
  body.toc-open .toc-card[open] {
    display: block;
    position: fixed;
    top: 70px;
    left: 12px;
    right: 12px;
    z-index: 130;
    max-height: calc(100vh - 86px);
    max-height: calc(100dvh - 86px);
    overflow-y: auto;
    overscroll-behavior: contain;
    box-shadow: 0 24px 70px rgba(8, 17, 31, .3);
  }
  body.toc-open .toc-card[open] > summary {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--surface);
    padding: 10px 12px;
    padding-right: 74px;
  }
  .toc-card > summary { padding: 10px 12px; }
  .toc-card[open] > summary { padding-right: 72px; }
  .toc-card > summary { padding-right: 74px; }
  .toc-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 9px;
    right: 10px;
    z-index: 2;
    min-width: 46px;
    min-height: 34px;
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: .76rem;
    font-weight: 800;
    cursor: pointer;
  }
  .toc-group-summary { padding: 11px 10px; }
  .toc-group-title { font-size: .86rem; }

  .section-lead { font-size: 1.02rem; }

  .figure-body { padding: 14px; }
  .figure-head { padding: 11px 14px; }
  .figure-title { font-size: .85rem; }
  .figure figcaption { padding: 0 14px 15px; font-size: .82rem; }
  .scroll-hint { display: inline; margin-left: 0; }

  .site-footer { flex-direction: column; padding-bottom: 36px; }
  .footer-note { max-width: none; }
  .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .glossary { grid-template-columns: 1fr; }
  .dgm-row-2, .dgm-row-3 { grid-template-columns: 1fr; }
  .dgm-row { grid-auto-flow: row; grid-auto-columns: auto; }
  .dgm-arrow::before { content: "↓"; }
  .dgm-loop { grid-template-columns: 1fr; }
  .dgm-loop .dgm-card::after { content: "↓"; right: 50%; top: auto; bottom: -12px; transform: translateX(50%); }
  .dgm-loop .dgm-card:last-child::after { content: ""; }
  .dgm-tier { grid-template-columns: 1fr; gap: 2px; }
  .dgm-matrix { grid-template-columns: 1fr !important; }
  .dgm-matrix > div:nth-child(4n+1) { background: var(--surface-2); }
  .dgm-matrix--tools { grid-template-columns: 72px 1fr 1fr !important; font-size: .76rem; }
  .dgm-perm-row { grid-template-columns: 1fr; }
  .dgm-perm--skills .dgm-perm-row { grid-template-columns: 1fr; }
  .dgm-perm-row.is-head { display: none; }
  .dgm-perm-row > * { border-right: 0; border-bottom: 1px solid var(--border); }
  .dgm-perm-row > *:last-child { border-bottom: 0; }
  .dgm-perm-row span[data-col]::before {
    content: attr(data-col) "：";
    font-size: .74rem;
    font-weight: 700;
    color: var(--muted);
    margin-right: 8px;
  }
  .dgm-bar { height: 30px; }
  .dgm-bar-seg { font-size: 0; color: transparent; }
  .dgm-legend { display: flex; }
  .lesson-section { padding-bottom: 34px; margin-bottom: 30px; }
  .unit-band { padding: 22px 18px 20px; }
  .unit-band h2 { margin: 4px 0 6px; }
  .unit-band-row { gap: 8px 12px; }
  .table-wrap { border-radius: 10px; }
  th, td { padding: 8px 10px; }
}

@media (max-width: 460px) {
  .hero-meta { gap: 6px; }
  .chip { font-size: .74rem; }
  .btn { width: 100%; }
  .hero-actions .btn { width: auto; flex: 1 1 auto; }
  .segmented { width: 100%; }
  .seg-btn { flex: 1 1 40%; min-height: 40px; font-size: .8rem; }
  .dgm { font-size: .82rem; }
  .dgm-steps { grid-template-columns: 1fr; }
  .ref-list li { grid-template-columns: 32px 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
  .figure.reveal { opacity: 1; transform: none; }
}

@media print {
  .site-header, .sidebar, .hero-actions, .hero-map, .player-controls,
  .lab, .skip-link, .header-actions, .back-to-top { display: none !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .layout { display: block; max-width: none; }
  .article { max-width: none; }
  .figure { break-inside: avoid; }
  .figure.reveal { opacity: 1 !important; transform: none !important; }
  a { color: #000; text-decoration: none; }
}

/* 回到頁首：固定右下，捲過 900px 才顯示。 */
.back-to-top {
  position: fixed;
  right: 18px;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 90;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow);
  font-size: 1.2rem;
  font-weight: 800;
  text-decoration: none;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity .2s ease, transform .2s ease;
}
.back-to-top.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: none;
}
