/* ── Design tokens ─────────────────────────────────────────── */

:root {
  --bg: #ffffff;
  --bg-muted: #f6f7f9;
  --bg-sidebar: #f5f5f7;
  --bg-active: #e8f0fe;
  --bg-code: #f4f4f6;
  --border: #e6e6ea;
  --border-strong: #d0d0d5;
  --text: #1a1a1f;
  --text-muted: #5c5c66;
  --text-dim: #8a8a94;
  --link: #1e63d0;
  --link-hover: #164b9f;
  --accent: #2563eb;
  --max-content: 72ch;
  --sidebar-w: 272px;
  --toc-w: 224px;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
}

:root[data-theme='dark'] {
  --bg: #15161a;
  --bg-muted: #1c1d22;
  --bg-sidebar: #101115;
  --bg-active: #1e2a44;
  --bg-code: #1d1e23;
  --border: #2a2b32;
  --border-strong: #3a3b44;
  --text: #e7e7ea;
  --text-muted: #a7a8b1;
  --text-dim: #7a7b84;
  --link: #8cb4ff;
  --link-hover: #a9c7ff;
  --accent: #6aa0ff;
}

/* Honour OS preference when no explicit override is set */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #15161a;
    --bg-muted: #1c1d22;
    --bg-sidebar: #101115;
    --bg-active: #1e2a44;
    --bg-code: #1d1e23;
    --border: #2a2b32;
    --border-strong: #3a3b44;
    --text: #e7e7ea;
    --text-muted: #a7a8b1;
    --text-dim: #7a7b84;
    --link: #8cb4ff;
    --link-hover: #a9c7ff;
    --accent: #6aa0ff;
  }
}

/* ── Reset + base ──────────────────────────────────────────── */

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

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

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

/* ── Sidebar ───────────────────────────────────────────────── */

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 20px 16px 40px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 8px 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
}
.brand:hover {
  text-decoration: none;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.brand-tag {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}

.nav-group {
  margin-top: 18px;
}
.nav-group:first-child {
  margin-top: 0;
}

.nav-group-title {
  margin: 0 0 8px;
  padding: 0 8px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-list a {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 7px;
  font-size: 13.5px;
  color: var(--text-muted);
  line-height: 1.4;
}
.nav-list a:hover {
  background: var(--border);
  color: var(--text);
  text-decoration: none;
}
.nav-list a[aria-current='page'] {
  background: var(--bg-active);
  color: var(--link);
  font-weight: 600;
}
.nav-list .ext {
  margin-left: auto;
  color: var(--text-dim);
  font-size: 11px;
}

/* ── Main layout ───────────────────────────────────────────── */

.content {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--toc-w);
  gap: 40px;
  padding: 20px 40px 80px;
  min-width: 0;
}

@media (max-width: 1080px) {
  .content {
    grid-template-columns: 1fr;
  }
  .toc-rail {
    display: none;
  }
}

.content-wrap {
  max-width: var(--max-content);
  min-width: 0;
  margin: 0 auto;
  width: 100%;
}

.content-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0 24px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 28px;
}

.crumb {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-dim);
}

.theme-toggle {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.theme-toggle:hover {
  border-color: var(--border-strong);
  color: var(--text);
}
.theme-toggle .icon-sun {
  display: none;
}
.theme-toggle .icon-moon {
  display: inline-block;
}
:root[data-theme='dark'] .theme-toggle .icon-sun,
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .theme-toggle .icon-sun {
    display: inline-block;
  }
  :root:not([data-theme='light']) .theme-toggle .icon-moon {
    display: none;
  }
}
:root[data-theme='dark'] .theme-toggle .icon-moon {
  display: none;
}

/* ── TOC rail ──────────────────────────────────────────────── */

.toc-rail {
  position: sticky;
  top: 24px;
  align-self: flex-start;
  max-height: calc(100vh - 48px);
  overflow-y: auto;
  padding-top: 40px;
}

.toc h4 {
  margin: 0 0 12px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.toc ul {
  list-style: none;
  margin: 0;
  padding: 0;
  border-left: 1px solid var(--border);
}
.toc li {
  margin: 0;
}
.toc a {
  display: block;
  padding: 4px 0 4px 12px;
  margin-left: -1px;
  border-left: 1px solid transparent;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.4;
}
.toc a:hover {
  color: var(--text);
  text-decoration: none;
  border-left-color: var(--border-strong);
}
.toc-l3 a {
  padding-left: 24px;
  font-size: 12.5px;
}

/* ── Prose / markdown typography ───────────────────────────── */

.prose {
  font-size: 15px;
  line-height: 1.7;
  word-wrap: break-word;
}

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

.prose h1,
.prose h2,
.prose h3,
.prose h4 {
  color: var(--text);
  line-height: 1.25;
  font-weight: 700;
  margin: 2em 0 0.8em;
  scroll-margin-top: 24px;
}
.prose h1 {
  font-size: 2em;
  margin-top: 0;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border);
}
.prose h2 {
  font-size: 1.45em;
  padding-bottom: 0.25em;
  border-bottom: 1px solid var(--border);
}
.prose h3 {
  font-size: 1.2em;
}
.prose h4 {
  font-size: 1em;
}

.prose p {
  margin: 0 0 1em;
}

.prose ul,
.prose ol {
  margin: 0 0 1em;
  padding-left: 1.5em;
}
.prose li {
  margin: 0.25em 0;
}
.prose li > p {
  margin: 0.4em 0;
}

.prose blockquote {
  margin: 1em 0;
  padding: 0.4em 1em;
  border-left: 3px solid var(--border-strong);
  color: var(--text-muted);
  background: var(--bg-muted);
  border-radius: 0 6px 6px 0;
}

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

.prose code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-code);
  padding: 0.15em 0.4em;
  border-radius: 4px;
}
.prose pre {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  overflow-x: auto;
  margin: 1.2em 0;
  line-height: 1.5;
}
.prose pre code {
  background: transparent;
  padding: 0;
  font-size: 13px;
}

.prose table {
  display: block;
  overflow-x: auto;
  border-collapse: collapse;
  margin: 1.2em 0;
  font-size: 13.5px;
}
.prose thead {
  background: var(--bg-muted);
}
.prose th,
.prose td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
}
.prose th {
  font-weight: 600;
}

.prose img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
}

.prose input[type='checkbox'] {
  margin-right: 6px;
}

/* Anchor affordance on hover — copy link target on click via JS */
.prose h1,
.prose h2,
.prose h3,
.prose h4 {
  position: relative;
}
.prose h1 > a.hash-link,
.prose h2 > a.hash-link,
.prose h3 > a.hash-link,
.prose h4 > a.hash-link {
  display: inline-block;
  margin-left: 0.25em;
  color: var(--text-dim);
  opacity: 0;
  transition: opacity 0.15s;
  font-weight: 400;
}
.prose h1:hover > a.hash-link,
.prose h2:hover > a.hash-link,
.prose h3:hover > a.hash-link,
.prose h4:hover > a.hash-link {
  opacity: 1;
}

/* ── Mobile ────────────────────────────────────────────────── */

.sidebar-toggle {
  display: none;
}

@media (max-width: 860px) {
  body {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(320px, 82vw);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.24);
  }
  body.nav-open .sidebar {
    transform: translateX(0);
  }
  body.nav-open::after {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 30;
  }
  .sidebar-toggle {
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 50;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 9px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }
  .content {
    padding: 56px 20px 60px;
  }
}
