:root {
  color-scheme: light;

  --bg: #ffffff;
  --bg-soft: #f6f8fc;
  --bg-sunken: #eef2f9;
  --surface: #ffffff;
  --text: #16203a;
  --text-soft: #4d5d7a;
  --text-faint: #7686a3;
  --border: #e2e8f3;
  --border-strong: #ccd6e6;
  --accent: #0e6fd6;
  --accent-soft: #e8f1fe;
  --accent-contrast: #ffffff;
  --brand-1: #14b8a6;
  --brand-2: #22d3ee;
  --brand-3: #4f7cf7;
  --shadow-sm: 0 1px 2px rgb(16 28 56 / 6%), 0 1px 3px rgb(16 28 56 / 5%);
  --shadow-lg: 0 24px 60px -20px rgb(16 28 56 / 28%), 0 4px 12px rgb(16 28 56 / 8%);

  --code-bg: #f6f8fc;
  --code-border: #e2e8f3;
  --code-text: #253352;
  --hl-keyword: #a5308a;
  --hl-string: #1c7c4a;
  --hl-number: #b45309;
  --hl-comment: #7686a3;
  --hl-title: #0e6fd6;
  --hl-attr: #8250df;
  --hl-meta: #64748b;

  --topbar-h: 3.5rem;
  --sidebar-w: 16.5rem;
  --aside-w: 15rem;
  --content-max: 48rem;
  --radius: 0.625rem;

  --font-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Inter,
    Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --bg: #0b111f;
  --bg-soft: #0f1728;
  --bg-sunken: #0a101c;
  --surface: #111a2c;
  --text: #e7eefb;
  --text-soft: #a7b6d0;
  --text-faint: #7f8fab;
  --border: #1e2a42;
  --border-strong: #2b3a58;
  --accent: #58b0ff;
  --accent-soft: #14263e;
  --accent-contrast: #06101f;
  --shadow-sm: 0 1px 2px rgb(0 0 0 / 40%);
  --shadow-lg: 0 24px 60px -20px rgb(0 0 0 / 70%), 0 4px 12px rgb(0 0 0 / 40%);

  --code-bg: #0c1424;
  --code-border: #1e2a42;
  --code-text: #d7e2f5;
  --hl-keyword: #f472b6;
  --hl-string: #7ee2b8;
  --hl-number: #fbbf24;
  --hl-comment: #6b7c99;
  --hl-title: #7cc4ff;
  --hl-attr: #c4a6ff;
  --hl-meta: #8ba0c0;
}

:root:not([data-theme="light"]) {
  color-scheme: dark;

  --bg: #0b111f;
  --bg-soft: #0f1728;
  --bg-sunken: #0a101c;
  --surface: #111a2c;
  --text: #e7eefb;
  --text-soft: #a7b6d0;
  --text-faint: #7f8fab;
  --border: #1e2a42;
  --border-strong: #2b3a58;
  --accent: #58b0ff;
  --accent-soft: #14263e;
  --accent-contrast: #06101f;
  --shadow-sm: 0 1px 2px rgb(0 0 0 / 40%);
  --shadow-lg: 0 24px 60px -20px rgb(0 0 0 / 70%), 0 4px 12px rgb(0 0 0 / 40%);

  --code-bg: #0c1424;
  --code-border: #1e2a42;
  --code-text: #d7e2f5;
  --hl-keyword: #f472b6;
  --hl-string: #7ee2b8;
  --hl-number: #fbbf24;
  --hl-comment: #6b7c99;
  --hl-title: #7cc4ff;
  --hl-attr: #c4a6ff;
  --hl-meta: #8ba0c0;
}

*, *::before, *::after { box-sizing: border-box; }

html { scroll-behavior: smooth; scroll-padding-top: calc(var(--topbar-h) + 1rem); }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

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

img { max-width: 100%; }
[hidden] { display: none !important; }

a { color: var(--accent); text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent); text-underline-offset: 0.2em; }
a:hover { text-decoration-color: currentColor; }

svg { width: 1.25rem; height: 1.25rem; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
svg.icon-filled { fill: currentColor; stroke: none; }
svg.icon-tangled { width: 1.4rem; height: 1.4rem; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.skip-link {
  position: absolute; left: 0.5rem; top: -4rem; z-index: 100;
  padding: 0.5rem 0.9rem; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-contrast);
  text-decoration: none; transition: top 0.15s;
}
.skip-link:focus { top: 0.5rem; }

/* --------------------------------- topbar ------------------------------ */

.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 0.75rem;
  height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) clamp(0.75rem, 2vw, 1.5rem) 0;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex; align-items: center; gap: 0.5rem;
  color: var(--text); text-decoration: none; font-weight: 700;
  letter-spacing: -0.02em; white-space: nowrap;
}
.brand img { display: block; }
.brand-name { font-size: 1.15rem; }
.brand-tag {
  font-size: 0.68rem; font-weight: 700; letter-spacing: 0.1em;
  padding: 0.1rem 0.4rem; border-radius: 999px;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 22%, transparent);
}

.icon-button {
  display: inline-grid; place-items: center;
  width: 2.25rem; height: 2.25rem; flex: none;
  border: 1px solid transparent; border-radius: var(--radius);
  background: none; color: var(--text-soft); cursor: pointer;
}
.icon-button:hover { background: var(--bg-soft); color: var(--text); border-color: var(--border); }

.nav-toggle { display: none; }

.search-trigger {
  display: flex; align-items: center; gap: 0.5rem;
  margin-left: auto;
  min-width: 0; max-width: 20rem; flex: 1 1 13rem;
  height: 2.25rem; padding: 0 0.6rem;
  font: inherit; font-size: 0.875rem; color: var(--text-faint);
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: var(--radius); cursor: pointer; text-align: left;
}
.search-trigger:hover { border-color: var(--border-strong); color: var(--text-soft); }
.search-trigger span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-trigger svg { width: 1rem; height: 1rem; flex: none; }
.search-trigger kbd {
  font-family: var(--font-mono); font-size: 0.7rem; padding: 0.05rem 0.35rem;
  border: 1px solid var(--border-strong); border-bottom-width: 2px;
  border-radius: 5px; color: var(--text-faint); background: var(--bg);
}

.topbar-actions { display: flex; align-items: center; gap: 0.25rem; }

.theme-toggle .icon-moon { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: block; }
:root:not([data-theme="light"]) .theme-toggle .icon-sun { display: none; }
:root:not([data-theme="light"]) .theme-toggle .icon-moon { display: block; }

/* --------------------------------- shell ------------------------------- */

.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  align-items: start;
  max-width: 96rem; margin-inline: auto;
}

.sidebar {
  position: sticky; top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
  height: calc(100dvh - var(--topbar-h) - env(safe-area-inset-top, 0px));
  overflow-y: auto; overscroll-behavior: contain;
  border-right: 1px solid var(--border);
  scrollbar-width: thin;
}
.sidebar-inner { padding: 1.5rem 1rem 4rem 1.25rem; }

.nav-section + .nav-section { margin-top: 1.5rem; }
.nav-title {
  margin: 0 0 0.5rem; padding-left: 0.6rem;
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.09em;
  text-transform: uppercase; color: var(--text-faint);
}
.sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar a {
  display: block; padding: 0.35rem 0.6rem; border-radius: 0.5rem;
  font-size: 0.9rem; color: var(--text-soft); text-decoration: none;
  border-left: 2px solid transparent;
}
.sidebar a:hover { background: var(--bg-soft); color: var(--text); }
.sidebar a[aria-current="page"] {
  color: var(--accent); background: var(--accent-soft); font-weight: 600;
}

.sidebar-scrim { display: none; }

main { min-width: 0; }

/* ---------------------------------- doc -------------------------------- */

.doc {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--aside-w);
  gap: clamp(1.5rem, 4vw, 3.5rem);
  padding: clamp(1.75rem, 4vw, 3rem) clamp(1rem, 3vw, 2.5rem) 5rem;
}
.doc-home { grid-template-columns: minmax(0, 1fr); }

.doc-main { min-width: 0; max-width: var(--content-max); }
.doc-home .doc-main { margin-inline: auto; max-width: 56rem; }

.doc-header { margin-bottom: 2rem; }
.eyebrow {
  margin: 0 0 0.35rem; font-size: 0.75rem; font-weight: 700;
  letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent);
}
.doc-header h1 {
  margin: 0; font-size: clamp(1.9rem, 1.4rem + 2vw, 2.6rem);
  line-height: 1.15; letter-spacing: -0.025em; font-weight: 750;
}

.doc-aside { min-width: 0; }
.toc {
  position: sticky;
  top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px) + 2rem);
  max-height: calc(100dvh - var(--topbar-h) - 5rem);
  overflow-y: auto; scrollbar-width: thin;
  border-left: 1px solid var(--border); padding-left: 1rem;
}
.toc-title {
  margin: 0 0 0.6rem; font-size: 0.72rem; font-weight: 700;
  letter-spacing: 0.09em; text-transform: uppercase; color: var(--text-faint);
}
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc a {
  display: block; padding: 0.22rem 0; font-size: 0.83rem;
  color: var(--text-soft); text-decoration: none; line-height: 1.45;
}
.toc a:hover { color: var(--text); }
.toc a.is-active { color: var(--accent); font-weight: 600; }
.toc-d1 a { padding-left: 0.85rem; font-size: 0.8rem; }

/* ---------------------------------- prose ------------------------------ */

.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }

.prose h2, .prose h3, .prose h4, .prose h5 {
  position: relative;
  margin: 2.5rem 0 0.75rem; line-height: 1.25;
  letter-spacing: -0.015em; font-weight: 700; scroll-margin-top: 5rem;
}
.prose h2 { font-size: 1.5rem; }
.prose h3 { font-size: 1.2rem; }
.prose h4 { font-size: 1.05rem; }
.prose h5 { font-size: 0.97rem; color: var(--text-soft); }

.prose .section-head {
  font-size: 1.5rem;
  margin-top: 3rem;
  padding-top: 1.75rem;
  border-top: 1px solid var(--border);
}
.prose > .section-head:first-child { margin-top: 0; padding-top: 0; border-top: 0; }

.heading-anchor {
  position: absolute; left: -1.1rem; width: 1.1rem;
  color: var(--text-faint); text-decoration: none; font-weight: 400;
  opacity: 0; transition: opacity 0.12s;
}
.prose :is(h2, h3, h4, h5):hover .heading-anchor,
.heading-anchor:focus-visible { opacity: 1; }

.prose p, .prose ul, .prose ol { margin: 0 0 1.1rem; }
.prose ul, .prose ol { padding-left: 1.4rem; }
.prose li { margin-bottom: 0.4rem; }
.prose li > ul, .prose li > ol { margin: 0.4rem 0 0; }

.prose strong { font-weight: 650; color: var(--text); }

.prose blockquote {
  margin: 1.25rem 0; padding: 0.75rem 1.1rem;
  border-left: 3px solid var(--accent);
  background: var(--bg-soft); border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--text-soft);
}
.prose blockquote > :last-child { margin-bottom: 0; }

.prose hr { margin: 2.5rem 0; border: 0; border-top: 1px solid var(--border); }

.prose :not(pre) > code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  padding: 0.12em 0.36em;
  border-radius: 5px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  color: var(--code-text);
  word-break: break-word;
}
.prose a > code { color: inherit; }

.prose pre {
  position: relative;
  margin: 1.25rem 0; padding: 1rem 1.1rem;
  overflow-x: auto;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: var(--radius);
  font-size: 0.85rem; line-height: 1.6;
  scrollbar-width: thin;
}
.prose pre code {
  font-family: var(--font-mono);
  background: none; border: 0; padding: 0; color: var(--code-text);
}

.copy-button {
  position: absolute; top: 0.5rem; right: 0.5rem;
  display: inline-flex; align-items: center; gap: 0.3rem;
  padding: 0.22rem 0.5rem;
  font: inherit; font-size: 0.72rem; font-family: var(--font-sans);
  color: var(--text-soft); background: var(--bg);
  border: 1px solid var(--border-strong); border-radius: 6px;
  cursor: pointer; opacity: 0; transition: opacity 0.12s;
}
.prose pre:hover .copy-button, .copy-button:focus-visible { opacity: 1; }
.copy-button:hover { color: var(--text); border-color: var(--accent); }
.copy-button.is-copied { color: var(--accent); border-color: var(--accent); }

.table-wrap {
  margin: 1.25rem 0; overflow-x: auto; scrollbar-width: thin;
  border: 1px solid var(--border); border-radius: var(--radius);
}
.prose table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.prose th, .prose td {
  padding: 0.6rem 0.85rem; text-align: left; vertical-align: top;
  border-bottom: 1px solid var(--border);
}
.prose thead th {
  background: var(--bg-soft); font-size: 0.78rem;
  letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-soft);
  white-space: nowrap;
}
.prose tbody tr:last-child td { border-bottom: 0; }

.external-link::after {
  content: "↗"; font-size: 0.75em; margin-left: 0.15em;
  color: var(--text-faint); text-decoration: none;
}

/* ---------------------------------- pager ------------------------------ */

.pager {
  display: grid; grid-template-columns: 1fr 1fr; gap: 1rem;
  margin-top: 4rem; padding-top: 1.5rem; border-top: 1px solid var(--border);
}
.pager-link {
  display: flex; flex-direction: column; gap: 0.15rem;
  padding: 0.85rem 1.1rem; text-decoration: none;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow-sm);
}
.pager-link:hover { border-color: var(--accent); }
.pager-next { text-align: right; }
.pager-dir { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-faint); }
.pager-label { font-weight: 600; color: var(--text); }

/* ---------------------------------- home ------------------------------- */

.hero { text-align: center; padding: 1rem 0 2.5rem; }
.hero-logo { margin-bottom: 1rem; }
.hero-title {
  margin: 0; font-size: clamp(2.4rem, 1.6rem + 3.4vw, 3.6rem);
  line-height: 1.05; letter-spacing: -0.04em; font-weight: 780;
}
.hero-title span {
  background: linear-gradient(105deg, var(--brand-1), var(--brand-2) 45%, var(--brand-3));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.hero-sub {
  max-width: 38rem; margin: 0.9rem auto 0;
  font-size: 1.1rem; color: var(--text-soft);
}
.hero-actions { display: flex; flex-wrap: wrap; gap: 0.75rem; justify-content: center; margin-top: 1.75rem; }

.button {
  display: inline-flex; align-items: center; gap: 0.4rem;
  padding: 0.6rem 1.15rem; border-radius: var(--radius);
  font-weight: 600; font-size: 0.95rem; text-decoration: none;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong);
}
.button:hover { border-color: var(--accent); color: var(--accent); }
.button-primary {
  color: var(--accent-contrast); border-color: transparent;
  background: linear-gradient(105deg, var(--brand-2), var(--brand-3));
}
.button-primary:hover { color: var(--accent-contrast); filter: brightness(1.08); }

.card-grid {
  display: grid; gap: 1rem; margin: 1.5rem 0 2rem;
  grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
}
.card {
  display: block; padding: 1.1rem 1.25rem; text-decoration: none;
  color: inherit; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card:hover { border-color: var(--accent); transform: translateY(-2px); }
@media (prefers-reduced-motion: reduce) { .card:hover { transform: none; } }
.prose .card h3 {
  margin: 0 0 0.3rem; padding: 0; border: 0;
  font-size: 1rem; font-weight: 650; color: var(--text);
}
.prose .card p { margin: 0; font-size: 0.88rem; color: var(--text-soft); line-height: 1.55; }
.prose .card-grid { margin: 1.75rem 0 2.25rem; }

/* --------------------------------- search ------------------------------ */

.search-dialog {
  width: min(42rem, calc(100vw - 2rem));
  max-height: min(34rem, calc(100dvh - 6rem));
  margin: 4.5rem auto auto; padding: 0;
  display: flex; flex-direction: column;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 0.9rem;
  box-shadow: var(--shadow-lg); overflow: hidden;
}
.search-dialog::backdrop { background: rgb(8 14 28 / 45%); backdrop-filter: blur(2px); }
.search-dialog:not([open]) { display: none; }

.search-head {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.75rem 0.9rem; border-bottom: 1px solid var(--border);
}
.search-icon { flex: none; color: var(--text-faint); }
.search-input {
  flex: 1; min-width: 0; font: inherit; font-size: 1rem;
  background: none; border: 0; color: var(--text); outline: none;
}
.search-close {
  font: inherit; font-size: 0.7rem; font-family: var(--font-mono);
  padding: 0.15rem 0.4rem; color: var(--text-faint);
  background: var(--bg-soft); border: 1px solid var(--border-strong);
  border-radius: 5px; cursor: pointer;
}

.search-results { overflow-y: auto; padding: 0.4rem; scrollbar-width: thin; }
.search-results:empty { display: none; }
.search-hit {
  display: block; padding: 0.6rem 0.75rem; border-radius: 0.5rem;
  text-decoration: none; color: inherit;
}
.search-hit:hover, .search-hit.is-selected { background: var(--accent-soft); }
.search-hit-crumb { font-size: 0.72rem; color: var(--text-faint); }
.search-hit-title { font-weight: 600; font-size: 0.95rem; color: var(--text); }
.search-hit-text {
  margin: 0.15rem 0 0; font-size: 0.83rem; color: var(--text-soft);
  line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
.search-hit mark { background: color-mix(in srgb, var(--brand-2) 34%, transparent); color: inherit; border-radius: 3px; }
.search-empty { margin: 0; padding: 1.5rem; text-align: center; font-size: 0.88rem; color: var(--text-faint); }

/* ------------------------------ highlight.js --------------------------- */

.hljs-comment, .hljs-quote { color: var(--hl-comment); font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-doctag,
.hljs-name, .hljs-type { color: var(--hl-keyword); }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-selector-attr,
.hljs-selector-pseudo { color: var(--hl-string); }
.hljs-number, .hljs-symbol, .hljs-bullet, .hljs-variable,
.hljs-template-variable, .hljs-deletion { color: var(--hl-number); }
.hljs-title, .hljs-section, .hljs-selector-id, .hljs-title.function_,
.hljs-title.class_ { color: var(--hl-title); font-weight: 600; }
.hljs-attr, .hljs-attribute, .hljs-property, .hljs-params { color: var(--hl-attr); }
.hljs-meta, .hljs-comment .hljs-doctag { color: var(--hl-meta); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 700; }

/* ------------------------------- responsive ---------------------------- */

@media (max-width: 1100px) {
  .doc { grid-template-columns: minmax(0, 1fr); }
  .doc-aside { display: none; }
}

@media (max-width: 860px) {
  .nav-toggle { display: inline-grid; }
  .shell { grid-template-columns: minmax(0, 1fr); }

  .sidebar {
    position: fixed; z-index: 45;
    inset: calc(var(--topbar-h) + env(safe-area-inset-top, 0px)) auto 0 0;
    width: min(20rem, 84vw);
    background: var(--bg);
    transform: translateX(-102%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-lg);
  }
  .sidebar.is-open { transform: none; }
  @media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }

  .sidebar-scrim {
    display: block; position: fixed; z-index: 44;
    inset: calc(var(--topbar-h) + env(safe-area-inset-top, 0px)) 0 0 0;
    background: rgb(8 14 28 / 45%);
  }
  .sidebar-scrim[hidden] { display: none; }

  .brand-tag { display: none; }
  .search-trigger kbd { display: none; }
  .doc-main { max-width: none; }
  .pager { grid-template-columns: minmax(0, 1fr); }
  .search-dialog { margin-top: 1.5rem; }
}

@media (max-width: 420px) {
  .search-trigger span { display: none; }
  .search-trigger { flex: 0 0 auto; width: 2.25rem; justify-content: center; }
}

@media print {
  .topbar, .sidebar, .doc-aside, .pager, .search-dialog, .skip-link { display: none !important; }
  .doc { display: block; padding: 0; }
  body { font-size: 11pt; }
}

@media (min-width: 46rem) {
  .doc-home .card-grid { grid-template-columns: repeat(2, 1fr); }
}
