:root {
  --bg: oklch(1 0 0);
  --surface: oklch(0.968 0.003 250);
  --surface-2: oklch(0.938 0.005 250);
  --line: oklch(0.885 0.006 250);
  --ink: oklch(0.21 0.012 255);
  --ink-2: oklch(0.44 0.018 255);
  --faint: oklch(0.54 0.02 255);
  --primary: oklch(0.5 0.13 50);
  --primary-ink: oklch(0.46 0.12 50);
  --primary-soft: oklch(0.955 0.022 55);
  --graphite: oklch(0.24 0.012 255);
  --success-soft: oklch(0.955 0.028 150);
  --success-ink: oklch(0.42 0.12 150);
  --warn-soft: oklch(0.96 0.035 80);
  --warn-ink: oklch(0.45 0.11 70);
  --radius: 8px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: oklch(0.19 0.012 255);
    --surface: oklch(0.235 0.013 255);
    --surface-2: oklch(0.28 0.014 255);
    --line: oklch(0.33 0.015 255);
    --ink: oklch(0.94 0.006 250);
    --ink-2: oklch(0.78 0.012 250);
    --faint: oklch(0.65 0.015 250);
    --primary: oklch(0.62 0.14 55);
    --primary-ink: oklch(0.78 0.12 60);
    --primary-soft: oklch(0.29 0.04 55);
    --graphite: oklch(0.16 0.01 255);
    --success-soft: oklch(0.28 0.05 150);
    --success-ink: oklch(0.8 0.11 150);
    --warn-soft: oklch(0.3 0.05 80);
    --warn-ink: oklch(0.85 0.1 80);
  }
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; scroll-padding-top: 5rem; }
body {
  font-family: "IBM Plex Sans Thai", system-ui, sans-serif;
  background: var(--bg); color: var(--ink);
  line-height: 1.8; font-size: 1rem;
}
code, pre, .mono { font-family: "IBM Plex Mono", ui-monospace, monospace; }
.skip { position: absolute; left: -9999px; }
.skip:focus { left: 1rem; top: 1rem; background: var(--primary); color: #fff; padding: .5rem 1rem; border-radius: 4px; z-index: 20; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  height: 56px; padding: 0 1.25rem;
  background: var(--graphite); color: oklch(0.97 0.002 250);
}
.brand { display: flex; align-items: center; gap: .65rem; text-decoration: none; color: inherit; }
.brand span { display: flex; flex-direction: column; line-height: 1.15; }
.brand strong { font-size: .95rem; }
.brand small { font-size: .7rem; opacity: .6; }
.repo { color: inherit; opacity: .7; text-decoration: none; font-size: .8rem; }
.repo:hover { opacity: 1; }

.shell { max-width: 1180px; margin: 0 auto; padding: 0 1.25rem; }
@media (min-width: 960px) {
  .shell { display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 3rem; align-items: start; }
}

.toc { padding: 1.5rem 0 .5rem; }
@media (min-width: 960px) {
  .toc { position: sticky; top: 72px; max-height: calc(100vh - 90px); overflow-y: auto; padding-bottom: 2rem; }
}
.toc-label { font-size: .72rem; letter-spacing: .06em; text-transform: uppercase; color: var(--faint); margin-bottom: .6rem; }
.toc ul { list-style: none; display: flex; flex-direction: column; gap: 1px; }
.toc a {
  display: flex; gap: .6rem; align-items: baseline;
  padding: .4rem .55rem; border-radius: 6px;
  color: var(--ink-2); text-decoration: none; font-size: .9rem;
}
.toc a:hover { background: var(--surface); color: var(--ink); }
.toc a[aria-current="page"] { background: var(--primary-soft); color: var(--primary-ink); font-weight: 600; }
.toc .num { font-family: "IBM Plex Mono", monospace; font-size: .72rem; color: var(--faint); }
.toc a[aria-current="page"] .num { color: inherit; }

main { padding: 1.5rem 0 3rem; min-width: 0; }
article { max-width: 72ch; }
article > * + * { margin-top: 1rem; }
h1 { font-size: 1.85rem; line-height: 1.35; letter-spacing: -.01em; text-wrap: balance; }
h2 { font-size: 1.3rem; margin-top: 2.75rem; padding-bottom: .4rem; border-bottom: 2px solid var(--line); text-wrap: balance; }
h3 { font-size: 1.05rem; margin-top: 1.75rem; }
p, li { text-wrap: pretty; }
ul, ol { padding-left: 1.35rem; }
li + li { margin-top: .3rem; }
li::marker { color: var(--primary-ink); }
a { color: var(--primary-ink); }
strong { font-weight: 600; }

:not(pre) > code {
  font-size: .86em; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 4px; padding: .08em .35em;
}
pre {
  background: var(--graphite); color: oklch(0.93 0.005 250);
  border-radius: var(--radius); padding: .9rem 1.1rem;
  overflow-x: auto; font-size: .85rem; line-height: 1.65;
}
pre code { background: none; border: 0; padding: 0; color: inherit; }

blockquote {
  border-left: 3px solid var(--primary);
  background: var(--surface); border-radius: 0 var(--radius) var(--radius) 0;
  padding: .8rem 1.1rem; color: var(--ink-2);
}
blockquote > * + * { margin-top: .5rem; }

table { border-collapse: collapse; width: 100%; font-size: .92rem; display: block; overflow-x: auto; }
th, td { border-bottom: 1px solid var(--line); padding: .5rem .8rem; text-align: left; vertical-align: top; }
th { background: var(--surface); font-weight: 600; font-size: .82rem; color: var(--ink-2); white-space: nowrap; }

hr { border: 0; border-top: 1px solid var(--line); margin: 2.5rem 0; }
img { max-width: 100%; }

.pagers { display: flex; justify-content: space-between; gap: 1rem; margin-top: 3.5rem; }
.pager {
  display: flex; flex-direction: column; gap: .15rem;
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: .7rem 1rem; text-decoration: none; color: var(--ink);
  max-width: 48%; transition: border-color .15s ease;
}
.pager:hover { border-color: var(--primary); }
.pager span { font-size: .75rem; color: var(--faint); }
.pager strong { font-size: .9rem; font-weight: 600; }
.pager.next { text-align: right; }

.foot {
  border-top: 1px solid var(--line); margin-top: 2rem;
  padding: 1.5rem 1.25rem 3rem; text-align: center;
  font-size: .82rem; color: var(--faint);
}
.foot p + p { margin-top: .3rem; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { transition: none !important; }
}

/* ---------- interactive diagrams ---------- */
.dg { margin: 1.75rem 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 1.1rem; }
.dg-title { font-weight: 600; font-size: .95rem; margin-bottom: .9rem; }
.dg-note { margin-top: .9rem; font-size: .82rem; color: var(--faint); }
.dg-panel { margin-top: .9rem; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: .85rem 1rem; font-size: .9rem; }
.dg-panel-item > * + * { margin-top: .5rem; }
.dg-panel-title { font-weight: 600; }
.dg-panel pre { margin-top: .6rem; font-size: .8rem; }
.dg-hint { color: var(--faint); font-size: .85rem; }
.dg-who { font-size: .8rem; color: var(--primary-ink); }
.dg-error { background: var(--warn-soft); color: var(--warn-ink); }

/* pipeline */
.dg-track { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: .4rem; }
.dg-track > li { flex: 1 1 130px; min-width: 0; }
.dg-pipeline.is-vertical .dg-track { flex-direction: column; }
.dg-node {
  width: 100%; text-align: left; cursor: pointer; font: inherit;
  display: flex; flex-direction: column; gap: .1rem;
  padding: .6rem .7rem; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--ink-2); transition: border-color .15s ease, background .15s ease;
}
.dg-node:hover { border-color: var(--primary); }
.dg-node.is-active { border-color: var(--primary); background: var(--primary-soft); color: var(--primary-ink); }
.dg-node.is-done { border-color: var(--line); opacity: .85; }
.dg-node-num { font-family: "IBM Plex Mono", monospace; font-size: .68rem; color: var(--faint); }
.dg-node.is-active .dg-node-num { color: inherit; }
.dg-node-label { font-size: .85rem; font-weight: 600; line-height: 1.4; }
.dg-node-sub { font-size: .72rem; color: var(--faint); }
.dg-controls { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-top: .8rem; }
.dg-controls button {
  font: inherit; font-size: .82rem; cursor: pointer; padding: .35rem .8rem;
  border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--ink-2);
}
.dg-controls button:hover { border-color: var(--primary); color: var(--primary-ink); }
.dg-progress { font-family: "IBM Plex Mono", monospace; font-size: .78rem; color: var(--faint); }

/* zones */
.dg-zone-row { display: flex; flex-wrap: wrap; align-items: stretch; gap: .5rem; }
.dg-zone { flex: 1 1 190px; min-width: 0; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: .7rem; }
.dg-zone-name { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--faint); margin-bottom: .5rem; }
.dg-zone ul { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .35rem; }
.dg-zone li { margin: 0; }
.dg-chip {
  width: 100%; text-align: left; font: inherit; font-size: .82rem; cursor: pointer;
  padding: .4rem .55rem; border-radius: 5px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); transition: border-color .15s ease;
}
.dg-chip:hover { border-color: var(--primary); }
.dg-chip.is-active { border-color: var(--primary); background: var(--primary-soft); color: var(--primary-ink); font-weight: 600; }
.dg-arrow { align-self: center; color: var(--faint); font-size: 1.1rem; }
@media (max-width: 640px) { .dg-arrow { transform: rotate(90deg); width: 100%; text-align: center; } }

/* layers */
.dg-stack { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .3rem; }
.dg-stack li { margin: 0; }
.dg-layer {
  width: 100%; display: flex; justify-content: space-between; align-items: baseline; gap: .75rem;
  font: inherit; font-size: .85rem; text-align: left; cursor: pointer;
  padding: .55rem .8rem; border: 1px solid var(--line); border-radius: 5px;
  background: var(--bg); color: var(--ink); transition: border-color .15s ease;
}
.dg-layer:hover { border-color: var(--primary); }
.dg-layer.is-active { border-color: var(--primary); background: var(--primary-soft); color: var(--primary-ink); }
.dg-layer-label { font-weight: 600; }
.dg-layer-meta { font-family: "IBM Plex Mono", monospace; font-size: .72rem; color: var(--faint); white-space: nowrap; }
.dg-layer.is-active .dg-layer-meta { color: inherit; }

/* compare */
.dg-compare { display: grid; gap: .75rem; }
@media (min-width: 640px) { .dg-compare { grid-template-columns: 1fr 1fr; } }
.dg-col { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: .8rem 1rem; font-size: .87rem; }
.dg-col ul { margin: 0; padding-left: 1.1rem; }
.dg-col pre { margin-top: .6rem; font-size: .78rem; }
.dg-col-title { font-weight: 600; margin-bottom: .5rem; }
.dg-col-good { border-color: color-mix(in oklch, var(--success-ink) 35%, var(--line)); }
.dg-col-good .dg-col-title { color: var(--success-ink); }
.dg-col-bad { border-color: color-mix(in oklch, var(--warn-ink) 35%, var(--line)); }
.dg-col-bad .dg-col-title { color: var(--warn-ink); }

/* timeline */
.dg-timeline { list-style: none; padding: 0; margin: 0; }
.dg-event { display: flex; gap: .7rem; align-items: flex-start; padding: .5rem 0; border-bottom: 1px solid var(--line); }
.dg-event:last-child { border-bottom: 0; }
.dg-event > div { flex: 1; min-width: 0; }
.dg-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--line-strong, var(--faint)); margin-top: .55rem; flex: none; }
.dg-event.is-good .dg-dot { background: var(--success-ink); }
.dg-event.is-bad .dg-dot { background: var(--warn-ink); }
.dg-event.is-key .dg-dot { background: var(--primary); }
.dg-event-label { font-size: .88rem; font-weight: 600; }
.dg-event-detail { font-size: .82rem; color: var(--ink-2); }
.dg-when { font-family: "IBM Plex Mono", monospace; font-size: .72rem; color: var(--faint); white-space: nowrap; }

/* quiz */
.dg-quiz ul { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .4rem; }
.dg-quiz li { margin: 0; }
.dg-q {
  width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  font: inherit; font-size: .88rem; font-weight: 600; text-align: left; cursor: pointer;
  padding: .55rem .8rem; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--ink);
}
.dg-q:hover { border-color: var(--primary); }
.dg-q-mark { font-family: "IBM Plex Mono", monospace; color: var(--primary-ink); }
.dg-a { padding: .6rem .9rem; font-size: .87rem; color: var(--ink-2); background: var(--bg); border: 1px solid var(--line); border-top: 0; border-radius: 0 0 6px 6px; margin-top: -.4rem; }
