:root {
  --paper: #fbfaf7;
  --surface: #ffffff;
  --ink: #15171c;
  --ink-soft: #414855;
  --muted: #6b7280;
  --line: #e4e0d8;
  --line-strong: #cfc9bd;
  --gold: #a9761c;
  --gold-soft: #f4ead2;
  --green: #146c43;
  --green-soft: #e3f2e9;
  --red: #a3282d;
  --red-soft: #fbe7e7;
  --blue: #1e4e79;
  --blue-soft: #e4eef7;
  --code-bg: #12141a;
  --code-ink: #e6e9ef;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: ui-sans-serif, -apple-system, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --sidebar: 288px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--blue); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { color: var(--gold); }

.skip { position: absolute; left: -9999px; top: 0; background: var(--ink); color: #fff; padding: 10px 16px; z-index: 100; }
.skip:focus { left: 8px; top: 8px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 18px;
  padding: 12px 28px;
  background: rgba(251, 250, 247, 0.92);
  backdrop-filter: saturate(140%) blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar .brand { display: flex; align-items: baseline; gap: 10px; font-weight: 650; letter-spacing: -0.01em; }
.topbar .brand small { font-weight: 500; color: var(--muted); letter-spacing: 0.04em; text-transform: uppercase; font-size: 11px; }
.topbar nav { margin-left: auto; display: flex; gap: 16px; font-size: 14px; }
.topbar nav a { color: var(--ink-soft); text-decoration: none; }
.topbar nav a:hover { color: var(--gold); }

/* layout */
.layout { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); max-width: 1480px; margin: 0 auto; }

.sidebar {
  position: sticky; top: 57px; align-self: start;
  height: calc(100vh - 57px); overflow-y: auto;
  padding: 24px 16px 60px 26px;
  border-right: 1px solid var(--line);
  font-size: 14px;
}
.sidebar h2 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 20px 0 6px; font-weight: 650; }
.sidebar h2:first-child { margin-top: 0; }
.sidebar ol { list-style: none; margin: 0; padding: 0; }
.sidebar a { display: block; padding: 5px 10px; border-radius: 6px; color: var(--ink-soft); text-decoration: none; line-height: 1.35; border-left: 2px solid transparent; }
.sidebar a:hover { background: #f1eee7; color: var(--ink); }
.sidebar a.active { background: var(--gold-soft); color: #6d4a0d; border-left-color: var(--gold); font-weight: 600; }
.sidebar .toc { margin-top: 4px; border-top: 1px solid var(--line); padding-top: 10px; }
.sidebar .toc a { font-size: 13px; color: var(--muted); padding: 3px 10px; }
.sidebar .toc a:hover { color: var(--ink); }
.sidebar .toc .lvl3 { padding-left: 22px; font-size: 12.5px; }

main { padding: 34px 48px 120px; max-width: 1060px; min-width: 0; }

/* typography */
h1 { font-size: clamp(28px, 3.2vw, 38px); line-height: 1.15; letter-spacing: -0.022em; margin: 0 0 10px; font-weight: 700; }
.lede { font-size: 18px; color: var(--ink-soft); margin: 0 0 26px; max-width: 74ch; }
h2 { font-size: 23px; letter-spacing: -0.015em; margin: 54px 0 12px; padding-top: 16px; border-top: 1px solid var(--line); font-weight: 680; }
h3 { font-size: 17px; margin: 28px 0 8px; font-weight: 660; }
h4 { font-size: 15px; margin: 22px 0 6px; font-weight: 660; }
p { margin: 0 0 14px; max-width: 78ch; }
ul, ol { max-width: 78ch; padding-left: 22px; margin: 0 0 16px; }
li { margin: 4px 0; }
li > ul, li > ol { margin: 4px 0 6px; }
strong { font-weight: 640; }
hr { border: 0; border-top: 1px solid var(--line); margin: 34px 0; }

code {
  font-family: var(--mono); font-size: 0.875em;
  background: #efece4; padding: 1px 5px; border-radius: 4px;
  border: 1px solid #e3ded3;
}
pre {
  background: var(--code-bg); color: var(--code-ink);
  padding: 16px 18px; border-radius: 10px; overflow-x: auto;
  font-family: var(--mono); font-size: 13px; line-height: 1.6;
  margin: 0 0 18px; border: 1px solid #23262f;
}
pre code { background: none; border: 0; padding: 0; color: inherit; font-size: inherit; }

blockquote {
  margin: 0 0 18px; padding: 12px 16px;
  background: var(--blue-soft); border-left: 3px solid var(--blue);
  border-radius: 0 8px 8px 0; font-size: 14.5px;
}
blockquote p:last-child { margin-bottom: 0; }

/* tables */
.table-wrap { overflow-x: auto; margin: 0 0 22px; border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; background: var(--surface); }
th, td { text-align: left; padding: 9px 13px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 680; background: #f7f5f0; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fcfbf8; }
td code { font-size: 12px; }

/* page furniture */
.crumbs { font-size: 12.5px; color: var(--muted); margin: 0 0 14px; letter-spacing: 0.02em; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--gold); }
.pagefoot {
  display: flex; gap: 16px; justify-content: space-between;
  margin-top: 56px; padding-top: 20px; border-top: 1px solid var(--line);
  font-size: 14px;
}
.pagefoot a { display: block; text-decoration: none; }
.pagefoot .dir { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); }
.pagefoot .next { text-align: right; margin-left: auto; }

/* home cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; margin: 24px 0 8px; }
.card {
  display: block; padding: 16px 18px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; text-decoration: none; color: inherit;
  transition: border-color 120ms ease, transform 120ms ease;
}
.card:hover { border-color: var(--line-strong); transform: translateY(-1px); color: inherit; }
.card b { display: block; font-size: 15px; margin-bottom: 4px; }
.card span { color: var(--muted); font-size: 13.5px; line-height: 1.5; }

.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; margin: 0 0 26px; }
.facts div { background: var(--surface); padding: 14px 16px; }
.facts dt { font-size: 11px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); margin: 0 0 4px; }
.facts dd { margin: 0; font-size: 20px; font-weight: 660; letter-spacing: -0.01em; }
.facts dd span { font-size: 12px; font-weight: 500; color: var(--muted); }

/* diagram figures */
figure.diagram { margin: 26px 0 30px; border: 1px solid var(--line-strong); border-radius: 14px; overflow: hidden; background: var(--surface); box-shadow: 0 1px 2px rgba(20,23,28,.04), 0 12px 28px -22px rgba(20,23,28,.5); }
figure.diagram .cap { display: flex; align-items: center; gap: 12px; padding: 10px 14px; background: #171a21; color: #dfe4ec; font-size: 13px; border-bottom: 1px solid #23262f; }
figure.diagram .cap b { font-weight: 620; }
figure.diagram .cap .kind { font-family: var(--mono); font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.08em; color: #8f98ab; border: 1px solid #2d313b; padding: 2px 7px; border-radius: 20px; }
figure.diagram .cap a { margin-left: auto; color: #cbd3e1; font-size: 12px; text-decoration: none; border: 1px solid #2d313b; padding: 3px 9px; border-radius: 6px; white-space: nowrap; }
figure.diagram .cap a:hover { color: #fff; border-color: #4a5060; }
figure.diagram .frame { display: block; background: #020617; }
figure.diagram .frame img { display: block; width: 100%; height: auto; }
figure.diagram.narrow .frame img { max-width: 780px; margin: 0 auto; }
figure.diagram figcaption { padding: 11px 16px; font-size: 13.5px; color: var(--muted); background: #f7f5f0; border-top: 1px solid var(--line); }

@media (min-width: 1081px) { figure.diagram { margin-left: -28px; margin-right: -28px; } }

@media (max-width: 1080px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 16px 20px; }
  .sidebar h2 { display: none; }
  .sidebar .toc { display: none; }
  .sidebar ol { display: flex; flex-wrap: wrap; gap: 4px; }
  main { padding: 24px 20px 80px; }
  .topbar nav { display: none; }
}

@media print {
  .sidebar, .topbar, .pagefoot, .crumbs { display: none; }
  body { background: #fff; }
  main { padding: 0; max-width: none; }
  figure.diagram { box-shadow: none; break-inside: avoid; }
  pre { background: #f4f4f4; color: #111; border-color: #ddd; }
  h2 { break-after: avoid; }
}
