:root {
  --bg: #0c0f14;
  --surface: #141a22;
  --surface-2: #1c2430;
  --border: #2a3544;
  --text: #e8eef5;
  --text-secondary: #9aabbc;
  --text-muted: #6b7c8f;
  --accent: #3d9cf0;
  --accent-dim: #2a7fc7;
  --warn: #e8a838;
  --danger: #e05050;
  --code-bg: #0a0e14;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --max: 820px;
  --toc-w: 280px;
}

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

html { scroll-behavior: smooth; scroll-padding-top: 4.5rem; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  font-size: 16px;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Top bar */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 100;
  height: 3.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  background: rgba(12, 15, 20, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text);
  letter-spacing: -0.02em;
}
.brand span { color: var(--accent); }
.topbar-links {
  display: flex;
  gap: 1rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
}
.topbar-links a { color: inherit; }
.topbar-links a:hover { color: var(--text); text-decoration: none; }
.toc-toggle {
  display: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.35rem 0.7rem;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: var(--toc-w) 1fr;
  max-width: 1200px;
  margin: 0 auto;
  padding-top: 3.5rem;
  min-height: 100vh;
}

/* TOC sidebar */
.toc-side {
  position: sticky;
  top: 3.5rem;
  height: calc(100vh - 3.5rem);
  overflow-y: auto;
  padding: 1.5rem 1rem 2rem 1.25rem;
  border-right: 1px solid var(--border);
  background: var(--bg);
  font-size: 0.82rem;
}
.toc-side h2 {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.85rem;
  font-weight: 600;
}
.toc-side ul { list-style: none; }
.toc-side > ul > li > a {
  display: none; /* hide self-referential top link */
}
.toc-side ul ul {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.toc-side ul ul a {
  display: block;
  color: var(--text-secondary);
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  line-height: 1.35;
  text-decoration: none;
}
.toc-side ul ul a:hover {
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}

/* Main content */
.main {
  padding: 2rem 2rem 4rem;
  max-width: calc(var(--max) + 4rem);
}

.main > header {
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}
.main > header .title,
.main h1 {
  font-size: clamp(1.5rem, 3vw, 2rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.25;
  margin-bottom: 0.75rem;
}
.lead {
  color: var(--text-secondary);
  font-size: 1.05rem;
}

.main h2 {
  font-size: 1.35rem;
  font-weight: 700;
  margin: 2.75rem 0 1rem;
  padding-top: 0.5rem;
  letter-spacing: -0.02em;
  border-top: 1px solid var(--border);
  padding-top: 1.75rem;
}
.main h2:first-of-type { border-top: none; margin-top: 1.5rem; }
.main h3 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 1.75rem 0 0.65rem;
  color: var(--text);
}
.main h4 {
  font-size: 1rem;
  font-weight: 600;
  margin: 1.25rem 0 0.5rem;
}

.main p {
  margin-bottom: 1rem;
  color: var(--text-secondary);
}
.main p strong, .main li strong { color: var(--text); }

.main ul, .main ol {
  margin: 0.75rem 0 1.25rem 1.25rem;
  color: var(--text-secondary);
}
.main li { margin-bottom: 0.4rem; }
.main li::marker { color: var(--accent); }

.main hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem 0;
}

/* Hide duplicate top TOC in body if present */
.main > nav#TOC { display: none; }

/* Tables */
.main table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.25rem 0 1.75rem;
  font-size: 0.9rem;
  overflow-x: auto;
  display: block;
}
.main thead { background: var(--surface); }
.main th, .main td {
  border: 1px solid var(--border);
  padding: 0.6rem 0.75rem;
  text-align: left;
  color: var(--text-secondary);
}
.main th {
  color: var(--text);
  font-weight: 600;
  white-space: nowrap;
}
.main tbody tr:nth-child(even) { background: rgba(255,255,255,0.02); }

/* Code & pre */
.main pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.15rem;
  overflow-x: auto;
  margin: 1rem 0 1.5rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.5;
  color: #c5d4e8;
}
.main code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--surface-2);
  padding: 0.12em 0.35em;
  border-radius: 4px;
  color: #9ec5f0;
}
.main pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

/* Math display */
.main .math.display,
.main span.math {
  color: var(--text);
  overflow-x: auto;
}

/* Danger callout */
.danger-note {
  background: rgba(224, 80, 80, 0.1);
  border: 1px solid rgba(224, 80, 80, 0.35);
  border-left: 3px solid var(--danger);
  border-radius: 8px;
  padding: 1rem 1.15rem;
  margin: 1.25rem 0;
  color: var(--text-secondary);
}
.danger-note strong { color: var(--danger); }

.budget-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem 1.5rem;
  margin: 1.25rem 0;
  font-family: var(--mono);
  font-size: 0.88rem;
  line-height: 1.7;
  color: var(--text-secondary);
  white-space: pre-wrap;
}

/* Footer */
.site-footer {
  border-top: 1px solid var(--border);
  padding: 1.5rem;
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  grid-column: 1 / -1;
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .toc-side {
    position: fixed;
    left: 0; top: 3.5rem;
    width: min(var(--toc-w), 85vw);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 90;
    border-right: 1px solid var(--border);
    box-shadow: 8px 0 30px rgba(0,0,0,0.4);
  }
  .toc-side.open { transform: translateX(0); }
  .toc-toggle { display: block; }
  .main { padding: 1.5rem 1.15rem 3rem; }
}
