:root {
  --bg: #ffffff;
  --bg-soft: #f7f7f8;
  --bg-hover: #efeff1;
  --text: #1f2328;
  --text-soft: #59636e;
  --border: #e4e4e7;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --code-bg: #f4f4f5;
  --note: #2563eb;
  --tip: #15803d;
  --important: #7c3aed;
  --warning: #b45309;
  --caution: #dc2626;
  --topbar-h: 56px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111113;
    --bg-soft: #18181b;
    --bg-hover: #232327;
    --text: #e4e4e7;
    --text-soft: #a1a1aa;
    --border: #2e2e33;
    --accent: #818cf8;
    --accent-soft: #1e1b4b;
    --code-bg: #1f1f23;
    --note: #60a5fa;
    --tip: #4ade80;
    --important: #c084fc;
    --warning: #fbbf24;
    --caution: #f87171;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111113;
  --bg-soft: #18181b;
  --bg-hover: #232327;
  --text: #e4e4e7;
  --text-soft: #a1a1aa;
  --border: #2e2e33;
  --accent: #818cf8;
  --accent-soft: #1e1b4b;
  --code-bg: #1f1f23;
  --note: #60a5fa;
  --tip: #4ade80;
  --important: #c084fc;
  --warning: #fbbf24;
  --caution: #f87171;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { scroll-padding-top: calc(var(--topbar-h) + 16px); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.65 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  height: var(--topbar-h); padding: 0 16px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 650; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; color: var(--accent); }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; flex: none;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text-soft); font-size: 18px; cursor: pointer;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.menu-btn { display: none; }
.theme-btn { margin-left: auto; }

.search { position: relative; flex: 1; max-width: 420px; margin-left: 24px; }
.search input {
  width: 100%; height: 36px; padding: 0 36px 0 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg-soft); color: var(--text); font: inherit; font-size: 14px;
}
.search input:focus { outline: none; border-color: var(--accent); background: var(--bg); }
.search-key {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  padding: 0 6px; border: 1px solid var(--border); border-radius: 4px;
  font: 12px/18px ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--text-soft); pointer-events: none;
}
.search input:focus + .search-key { display: none; }
.search-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  max-height: 70vh; overflow-y: auto;
  margin: 0; padding: 6px; list-style: none;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 12px 32px rgb(0 0 0 / 0.15);
}
.search-results li a { display: block; padding: 8px 10px; border-radius: 6px; color: var(--text); }
.search-results li a:hover, .search-results li[aria-selected="true"] a { background: var(--accent-soft); text-decoration: none; }
.search-results .r-title { font-weight: 600; font-size: 14px; }
.search-results .r-meta { font-size: 12px; color: var(--text-soft); }
.search-results .r-snippet { font-size: 13px; color: var(--text-soft); margin-top: 2px; }
.search-results mark { background: color-mix(in srgb, var(--accent) 25%, transparent); color: inherit; border-radius: 2px; }
.search-results .empty { padding: 10px; font-size: 14px; color: var(--text-soft); }

/* ---------- Layout ---------- */
.shell {
  display: grid;
  grid-template-columns: 272px minmax(0, 1fr) 224px;
  max-width: 1440px; margin: 0 auto;
}
.sidebar, .toc {
  position: sticky; top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h)); overflow-y: auto;
  padding: 20px 16px 40px;
}
.sidebar { border-right: 1px solid var(--border); font-size: 14px; }
.nav-home { display: block; padding: 6px 10px; border-radius: 6px; color: var(--text); font-weight: 600; }
.nav-group { margin-top: 12px; }
.nav-group.sub { margin: 2px 0 0 10px; }
.nav-group summary {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: 6px; cursor: pointer; list-style: none;
  font-size: 13px; font-weight: 650; color: var(--text);
}
.nav-group.sub summary { font-weight: 600; color: var(--text-soft); }
.nav-group summary::-webkit-details-marker { display: none; }
.nav-group summary::before {
  content: ""; flex: none; width: 6px; height: 6px;
  border: solid currentColor; border-width: 0 1.5px 1.5px 0;
  transform: rotate(-45deg); transition: transform 0.15s; opacity: 0.6;
}
.nav-group[open] > summary::before { transform: rotate(45deg); }
.nav-group summary:hover { background: var(--bg-hover); }
.nav-group ul { margin: 2px 0 0; padding: 0; list-style: none; }
.sidebar a { display: block; padding: 5px 10px 5px 24px; border-radius: 6px; color: var(--text-soft); }
.sidebar .nav-home { padding-left: 10px; }
.sidebar a:hover { background: var(--bg-hover); color: var(--text); text-decoration: none; }
.sidebar a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.content { min-width: 0; padding: 32px 48px 64px; }
.doc { max-width: 820px; }

.toc { font-size: 13px; }
.toc-title { margin: 0 0 8px; font-weight: 650; color: var(--text); }
.toc ul { margin: 0; padding: 0; list-style: none; border-left: 1px solid var(--border); }
.toc li a { display: block; padding: 3px 0 3px 12px; margin-left: -1px; border-left: 2px solid transparent; color: var(--text-soft); }
.toc li.l3 a { padding-left: 24px; }
.toc li a:hover { color: var(--text); text-decoration: none; }
.toc li a.active { color: var(--accent); border-left-color: var(--accent); }

/* ---------- Article ---------- */
.doc h1, .doc h2, .doc h3, .doc h4 { line-height: 1.3; font-weight: 650; }
.doc h1 { margin: 0 0 16px; font-size: 2rem; letter-spacing: -0.01em; }
.doc h2 { margin: 40px 0 12px; padding-bottom: 6px; font-size: 1.45rem; border-bottom: 1px solid var(--border); }
.doc h3 { margin: 28px 0 8px; font-size: 1.15rem; }
.doc h4 { margin: 20px 0 8px; font-size: 1rem; }
.doc :is(h2, h3, h4) { position: relative; }
.doc .anchor { position: absolute; left: -1.1em; padding-right: 0.3em; color: var(--text-soft); opacity: 0; font-weight: 400; }
.doc :is(h2, h3, h4):hover .anchor, .doc .anchor:focus { opacity: 1; text-decoration: none; }
.doc p, .doc ul, .doc ol { margin: 0 0 14px; }
.doc li + li { margin-top: 4px; }
.doc code {
  padding: 0.12em 0.4em; border-radius: 5px;
  background: var(--code-bg); font: 0.875em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.doc pre {
  margin: 0 0 16px; padding: 14px 16px; overflow-x: auto;
  border: 1px solid var(--border); border-radius: 8px; background: var(--code-bg) !important;
  font-size: 13.5px; line-height: 1.55;
}
.doc pre code { padding: 0; background: none; font-size: inherit; }
.doc img {
  display: block; max-width: 100%; height: auto; margin: 8px 0 20px;
  border: 1px solid var(--border); border-radius: 8px; cursor: zoom-in;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.06);
}
.doc hr { border: 0; border-top: 1px solid var(--border); margin: 32px 0; }
.table-wrap { overflow-x: auto; margin: 0 0 18px; border: 1px solid var(--border); border-radius: 8px; }
.doc table { width: 100%; border-collapse: collapse; font-size: 14.5px; }
.doc th, .doc td { padding: 8px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.doc th { background: var(--bg-soft); font-weight: 600; }
.doc tr:last-child td { border-bottom: 0; }
.doc .num { text-align: right; white-space: nowrap; }
.doc blockquote {
  margin: 0 0 16px; padding: 10px 16px;
  border-left: 4px solid var(--border); border-radius: 0 8px 8px 0;
  background: var(--bg-soft); color: var(--text);
}
.doc blockquote > :last-child { margin-bottom: 0; }
.doc blockquote.callout { --c: var(--note); border-left-color: var(--c); background: color-mix(in srgb, var(--c) 8%, var(--bg)); }
.doc blockquote.callout > p:first-child > strong:first-child { color: var(--c); }
.callout-tip { --c: var(--tip) !important; }
.callout-important { --c: var(--important) !important; }
.callout-warning { --c: var(--warning) !important; }
.callout-caution { --c: var(--caution) !important; }
.doc :target { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: var(--accent-soft); } to { background: transparent; } }

.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; max-width: 820px; margin-top: 48px; }
.pager a {
  display: block; padding: 12px 16px;
  border: 1px solid var(--border); border-radius: 8px; color: var(--text); font-weight: 600;
}
.pager a:hover { border-color: var(--accent); text-decoration: none; }
.pager span { display: block; font-size: 12px; font-weight: 400; color: var(--text-soft); }
.pager .next { text-align: right; grid-column: 2; }
.source-link { max-width: 820px; margin-top: 24px; font-size: 13px; }
.source-link a { color: var(--text-soft); }

.lightbox {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 24px;
  background: rgb(0 0 0 / 0.8); cursor: zoom-out;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; box-shadow: 0 20px 60px rgb(0 0 0 / 0.5); }

/* ---------- Responsive ---------- */
@media (max-width: 1200px) {
  .shell { grid-template-columns: 260px minmax(0, 1fr); }
  .toc { display: none; }
}
@media (max-width: 860px) {
  .shell { display: block; }
  .menu-btn { display: inline-grid; }
  .search { margin-left: 0; }
  .search-key { display: none; }
  .brand { display: none; }
  .sidebar {
    position: fixed; top: var(--topbar-h); left: 0; bottom: 0; z-index: 20;
    width: min(300px, 85vw); height: auto;
    background: var(--bg); transform: translateX(-100%); transition: transform 0.2s;
    box-shadow: 0 0 40px rgb(0 0 0 / 0.2);
  }
  body.nav-open .sidebar { transform: none; }
  .content { padding: 24px 16px 48px; }
  .doc h1 { font-size: 1.6rem; }
  .doc .anchor { display: none; }
  .pager { grid-template-columns: 1fr; }
  .pager .next { grid-column: 1; }
}
@media print {
  .topbar, .sidebar, .toc, .pager, .source-link { display: none; }
  .shell { display: block; }
}
