:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-2: #f1f4f9;
  --text: #1c2533;
  --muted: #5b6678;
  --line: #e2e7ef;
  --primary: #1f5b8f;
  --primary-soft: #e6f0f9;
  --accent: #0f9d6b;
  --tip: #0f9d6b;  --tip-bg: #e8f7f0;
  --note: #2563a8; --note-bg: #e8f1fb;
  --warn: #b45309; --warn-bg: #fdf3e2;
  --role: #6d3fc0; --role-bg: #f1ebfc;
  --step: #1f5b8f; --step-bg: #eef3f9;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 12px;
  --font: 'Sarabun', 'K2D', system-ui, sans-serif;
  --head: 'K2D', 'Sarabun', system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141b; --surface: #161d27; --surface-2: #1c2531; --text: #e4e9f1; --muted: #9aa6b8; --line: #2a3442;
    --primary: #7fb4e6; --primary-soft: #1b2b3d; --accent: #3fcf97;
    --tip: #4fd6a0; --tip-bg: #13291f; --note: #86b7ee; --note-bg: #152232; --warn: #f0b060; --warn-bg: #2d2214;
    --role: #b79af0; --role-bg: #241c36; --step: #86b7ee; --step-bg: #172230;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
  }
}
:root[data-theme="dark"] {
  --bg: #0f141b; --surface: #161d27; --surface-2: #1c2531; --text: #e4e9f1; --muted: #9aa6b8; --line: #2a3442;
  --primary: #7fb4e6; --primary-soft: #1b2b3d; --accent: #3fcf97;
  --tip: #4fd6a0; --tip-bg: #13291f; --note: #86b7ee; --note-bg: #152232; --warn: #f0b060; --warn-bg: #2d2214;
  --role: #b79af0; --role-bg: #241c36; --step: #86b7ee; --step-bg: #172230;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }
html { scroll-padding-top: 76px; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16.5px/1.75 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* top bar */
.topbar { position: sticky; top: 0; z-index: 30; height: 60px; display: flex; align-items: center; gap: 12px; padding: 0 20px;
  background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-family: var(--head); font-size: 17px; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand b { color: var(--primary); }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
#navSearch { width: 220px; height: 36px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); color: var(--text); padding: 0 12px; font: inherit; font-size: 14px; }
.icon-btn { width: 36px; height: 36px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface-2); color: var(--text); cursor: pointer; font-size: 16px; }
.menu-btn { display: none; width: 38px; height: 38px; border: 0; background: none; padding: 8px; cursor: pointer; flex-direction: column; justify-content: space-between; }
.menu-btn span { display: block; height: 2px; background: var(--text); border-radius: 2px; }

/* layout */
.layout { display: grid; grid-template-columns: 290px minmax(0, 1fr) 230px; max-width: 1560px; margin: 0 auto; }
.sidebar { position: sticky; top: 60px; height: calc(100vh - 60px); overflow-y: auto; padding: 18px 14px 30px; border-right: 1px solid var(--line); }
.nav-group { margin-bottom: 14px; }
.nav-part { font: 600 12px/1.4 var(--head); letter-spacing: .04em; text-transform: uppercase; color: var(--muted); padding: 6px 10px; }
.sidebar a { display: flex; gap: 10px; align-items: baseline; padding: 6px 10px; border-radius: 8px; color: var(--text); font-size: 14.5px; line-height: 1.45; }
.sidebar a:hover { background: var(--surface-2); text-decoration: none; }
.sidebar a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.sidebar a.hide { display: none; }
.nav-num { flex: 0 0 22px; text-align: right; color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.sidebar a.active .nav-num { color: var(--primary); }
.nav-foot { margin-top: 18px; padding: 10px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }

.content { min-width: 0; padding: 34px 44px 80px; }
article { max-width: 900px; }
.toc { position: sticky; top: 60px; height: calc(100vh - 60px); overflow-y: auto; padding: 30px 16px; font-size: 13.5px; }
.toc-title { font: 600 12px var(--head); color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 8px; }
.toc a { display: block; padding: 3px 0 3px 10px; color: var(--muted); border-left: 2px solid var(--line); line-height: 1.45; }
.toc a.l3 { padding-left: 22px; font-size: 13px; }
.toc a.on { color: var(--primary); border-left-color: var(--primary); }

/* typography */
.eyebrow { font: 600 13px var(--head); color: var(--accent); letter-spacing: .02em; margin-bottom: 4px; }
h1, h2, h3, h4 { font-family: var(--head); line-height: 1.35; color: var(--text); position: relative; }
h1 { font-size: 34px; margin: 0 0 10px; }
h2 { font-size: 25px; margin: 44px 0 12px; padding-top: 14px; border-top: 1px solid var(--line); }
h3 { font-size: 20px; margin: 30px 0 8px; }
h4 { font-size: 17px; margin: 22px 0 6px; }
.anchor { position: absolute; left: -22px; opacity: 0; color: var(--muted); font-weight: 400; }
h2:hover .anchor, h3:hover .anchor { opacity: 1; text-decoration: none; }
.lead { font-size: 18.5px; color: var(--muted); margin: 0 0 24px; }
p, li { max-width: 78ch; }
ol, ul { padding-left: 1.5em; }
li { margin: 4px 0; }
li > ul, li > ol { margin: 4px 0; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .88em; background: var(--surface-2); border: 1px solid var(--line); padding: 1px 6px; border-radius: 6px; }
pre { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; overflow-x: auto; }
pre code { background: none; border: 0; padding: 0; }
kbd, .btn { display: inline-block; font: 600 .86em var(--head); padding: 1px 9px; border-radius: 7px; background: var(--primary); color: #fff; white-space: nowrap; }
strong { font-weight: 700; }
hr { border: 0; border-top: 1px solid var(--line); margin: 32px 0; }

/* figures */
.fig { margin: 18px 0 26px; }
.fig a { display: block; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); background: var(--surface); cursor: zoom-in; }
.fig img { display: block; width: 100%; height: auto; }
.fig figcaption { font-size: 14px; color: var(--muted); margin-top: 8px; text-align: center; }
.fig figcaption b { color: var(--text); font-weight: 600; margin-right: 4px; }
.fig-mobile a { max-width: 320px; margin: 0 auto; border-radius: 22px; }

/* callouts */
.callout { border-radius: var(--radius); padding: 12px 16px 12px 18px; margin: 18px 0; border-left: 4px solid; }
.callout p { margin: 4px 0; }
.callout ul, .callout ol { margin: 4px 0; }
.callout-title { font: 700 14px var(--head); margin-bottom: 2px; }
.callout-tip { background: var(--tip-bg); border-color: var(--tip); } .callout-tip .callout-title { color: var(--tip); }
.callout-note { background: var(--note-bg); border-color: var(--note); } .callout-note .callout-title { color: var(--note); }
.callout-warn { background: var(--warn-bg); border-color: var(--warn); } .callout-warn .callout-title { color: var(--warn); }
.callout-role { background: var(--role-bg); border-color: var(--role); } .callout-role .callout-title { color: var(--role); }
.callout-step { background: var(--step-bg); border-color: var(--step); } .callout-step .callout-title { color: var(--step); }
blockquote { margin: 16px 0; padding: 4px 16px; border-left: 4px solid var(--line); color: var(--muted); }

/* tables */
.table-wrap { overflow-x: auto; margin: 16px 0 22px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 15px; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { background: var(--surface-2); font-family: var(--head); font-weight: 600; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }

/* cards on index */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; margin: 18px 0 28px; }
.card { display: block; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); color: var(--text); }
.card:hover { text-decoration: none; border-color: var(--primary); }
.card b { display: block; font-family: var(--head); font-size: 16.5px; margin-bottom: 4px; color: var(--primary); }
.card span { font-size: 14px; color: var(--muted); line-height: 1.55; display: block; }

/* pager */
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 60px; }
.pager a { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; background: var(--surface); color: var(--text); font-family: var(--head); }
.pager a:hover { border-color: var(--primary); text-decoration: none; }
.pager small { display: block; color: var(--muted); font-size: 12.5px; }
.pager .next { text-align: right; }

/* lightbox */
.lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(8, 12, 18, .88); display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 30px; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: calc(100vh - 110px); border-radius: 10px; background: #fff; }
.lb-cap { color: #e4e9f1; margin-top: 12px; font-size: 15px; text-align: center; }
.lb-close { position: absolute; top: 14px; right: 18px; width: 42px; height: 42px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .14); color: #fff; font-size: 26px; cursor: pointer; }

/* responsive */
@media (max-width: 1200px) { .layout { grid-template-columns: 270px minmax(0, 1fr); } .toc { display: none; } }
@media (max-width: 860px) {
  .layout { display: block; }
  .menu-btn { display: flex; }
  .sidebar { position: fixed; top: 60px; left: 0; bottom: 0; width: 290px; height: auto; background: var(--surface); z-index: 25; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  body.nav-open .sidebar { transform: none; }
  .content { padding: 22px 16px 60px; }
  #navSearch { width: 130px; }
  .brand span { font-size: 15px; }
  h1 { font-size: 27px; } h2 { font-size: 22px; }
  .pager { grid-template-columns: 1fr; }
}
@media (max-width: 480px) { .brand span b { display: none; } #navSearch { display: none; } }

/* print */
@media print {
  .topbar, .sidebar, .toc, .pager, .anchor { display: none !important; }
  .layout { display: block; } .content { padding: 0; } body { background: #fff; font-size: 12pt; }
  .fig { break-inside: avoid; } .fig a { box-shadow: none; }
  h2 { break-after: avoid; }
}
