/* === Design Tokens === */
:root {
  --bg-primary: #0a0a0f;
  --bg-secondary: #12121a;
  --bg-card: #1a1a25;
  --bg-code: #0d0d14;
  --border: #2a2a3a;
  --border-hover: #3a3a4f;
  --text-primary: #e8e8f0;
  --text-secondary: #b4b4c6;
  --text-muted: #8b8ba0;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --green: #22c55e;
  --yellow: #eab308;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  --max-width: 860px;
  --radius: 8px;
}

/* Visible keyboard focus on every interactive element. The default browser
   ring is nearly invisible against this dark theme. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}
.sidebar-link:focus-visible,
.search-results a:focus-visible {
  outline-offset: -2px;
}

/* Skip-to-content link for keyboard / screen-reader users. Visually hidden
   until focused, then anchored top-left over the topbar. */
.skip-to-content {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 200;
  background: var(--accent);
  color: #fff;
  padding: 0.5rem 0.85rem;
  font-weight: 600;
  text-decoration: none;
  border-radius: 0 0 var(--radius) 0;
  transform: translateY(-120%);
  transition: transform 0.15s;
}
.skip-to-content:focus-visible {
  transform: translateY(0);
  outline: none;
}

/* Anchor jumps should clear the sticky topbar. */
html { scroll-padding-top: calc(var(--topbar-height) + 0.5rem); }
.markdown-body :where(h1, h2, h3, h4, h5, h6)[id] {
  scroll-margin-top: calc(var(--topbar-height) + 0.5rem);
}

/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; font-size: 16px; }
body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

/* === Layout tokens === */
:root {
  --sidebar-width: 260px;
  --topbar-height: 56px;
}

/* === Top bar === */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(10, 10, 15, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  height: var(--topbar-height);
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  align-items: center;
  gap: 0;
}
.topbar-brand {
  font-weight: 800;
  font-size: 1.05rem;
  color: var(--text-primary);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0 1.25rem;
  height: 100%;
  border-right: 1px solid var(--border);
}
.topbar-brand span { color: var(--accent); font-family: var(--font-mono); }
.sidebar-toggle {
  display: none;
  position: absolute;
  left: 0.75rem;
  top: 0.75rem;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  z-index: 110;
}

/* === Search === */
.topbar-search {
  position: relative;
  padding: 0 1.5rem;
  max-width: 600px;
  width: 100%;
}
.search-field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.75rem;
  transition: border-color 0.15s;
}
.search-field:focus-within { border-color: var(--accent); }
.search-icon {
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1;
}
.search-field input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.9rem;
  min-width: 0;
}
.search-field input::placeholder { color: var(--text-muted); }
.search-kbd {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  color: var(--text-muted);
}
.search-results {
  position: absolute;
  top: calc(100% + 0.5rem);
  left: 1.5rem;
  right: 1.5rem;
  max-height: 60vh;
  overflow-y: auto;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  padding: 0.5rem 0;
  margin: 0;
  list-style: none;
  z-index: 120;
}
.search-results[hidden] { display: none; }
.search-results li {
  padding: 0;
}
.search-results a {
  display: block;
  padding: 0.6rem 1rem;
  color: var(--text-primary);
  text-decoration: none;
  font-size: 0.9rem;
  border-left: 3px solid transparent;
}
.search-results a:hover,
.search-results a.is-active {
  background: var(--bg-card);
  border-left-color: var(--accent);
}
.search-result-context {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: 0.25rem;
  line-height: 1.5;
}
.search-result-page {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-right: 0.5rem;
}
.search-empty {
  padding: 1rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* === Main layout grid === */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: calc(100vh - var(--topbar-height));
}

/* On wide viewports, surface a right-rail "on this page" TOC. */
@media (min-width: 1180px) {
  .layout:has(.toc-rail:not([hidden])) {
    grid-template-columns: var(--sidebar-width) 1fr 220px;
  }
}

.toc-rail {
  position: sticky;
  top: var(--topbar-height);
  align-self: start;
  max-height: calc(100vh - var(--topbar-height));
  overflow-y: auto;
  padding: 2rem 1rem 2rem 0;
  font-size: 0.85rem;
  line-height: 1.45;
}
.toc-rail[hidden] { display: none; }
@media (max-width: 1179px) { .toc-rail { display: none; } }
.toc-rail-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.6rem;
  padding-left: 0.85rem;
}
.toc-rail ul { list-style: none; padding: 0; margin: 0; }
.toc-rail li { margin: 0; }
.toc-rail a {
  display: block;
  padding: 0.25rem 0.85rem;
  color: var(--text-secondary);
  text-decoration: none;
  border-left: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}
.toc-rail a:hover { color: var(--text-primary); }
.toc-rail a.active {
  color: var(--accent);
  border-left-color: var(--accent);
}

/* Hover-revealed `#` anchor next to each section heading. */
.markdown-body :where(h2, h3, h4) { position: relative; }
.heading-anchor {
  position: absolute;
  left: -1.4rem;
  top: 0;
  bottom: 0;
  display: inline-flex;
  align-items: center;
  padding: 0 0.3rem;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}
.markdown-body :where(h2, h3, h4):hover .heading-anchor,
.heading-anchor:focus-visible {
  opacity: 1;
}
.heading-anchor:hover { color: var(--accent); }
@media (max-width: 960px) {
  .heading-anchor { display: none; }
}

/* === Left sidebar === */
.sidebar {
  position: sticky;
  top: var(--topbar-height);
  height: calc(100vh - var(--topbar-height));
  border-right: 1px solid var(--border);
  background: var(--bg-primary);
  overflow-y: auto;
}
.sidebar-inner { padding: 1.5rem 1rem; }
.sidebar-group { margin-bottom: 1.5rem; }
.sidebar-group-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 0 0.75rem 0.4rem;
}
.sidebar-link {
  display: block;
  padding: 0.4rem 0.75rem;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  border-radius: 6px;
  margin-bottom: 0.1rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.sidebar-link:hover { background: var(--bg-card); color: var(--text-primary); }
.sidebar-link.active {
  background: var(--bg-card);
  color: var(--accent);
  font-weight: 600;
}
.sidebar-link-external { opacity: 0.75; }

/* === Content area === */
.content {
  min-width: 0;
  padding: 2rem clamp(1.5rem, 4vw, 4rem);
}
.content > article,
.content > .markdown-body {
  max-width: 860px;
  margin: 0 auto;
}
.content-footer {
  max-width: 860px;
  margin: 4rem auto 0;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* === Markdown Body === */
.markdown-body {
  padding: 0 0 2rem;
}

.markdown-body h1 {
  font-size: 2.25rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.2;
  margin-bottom: 1rem;
}

.sponsor-banner {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  margin: 0 0 1.25rem 0;
  padding: 0.4rem 0.85rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
}

.sponsor-banner a {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  border-bottom: none;
  transition: opacity 0.15s ease;
}

.sponsor-banner a:hover { opacity: 0.85; }

.sponsor-logo {
  height: 22px;
  width: auto;
  display: block;
  border-radius: 4px;
}

.markdown-body h2 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-top: 3rem;
  margin-bottom: 1rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.markdown-body h3 {
  font-size: 1.2rem;
  font-weight: 600;
  margin-top: 2rem;
  margin-bottom: 0.75rem;
  color: var(--text-primary);
}

.markdown-body h4 {
  font-size: 1rem;
  font-weight: 600;
  margin-top: 1.5rem;
  margin-bottom: 0.5rem;
  color: var(--text-secondary);
}

.markdown-body p {
  color: var(--text-secondary);
  margin-bottom: 1rem;
}

.markdown-body strong {
  color: var(--text-primary);
}

.markdown-body a {
  color: var(--accent);
  text-decoration: none;
  transition: color 0.2s;
}
.markdown-body a:hover { color: var(--accent-hover); }

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

/* Lists */
.markdown-body ul, .markdown-body ol {
  margin-bottom: 1rem;
  padding-left: 1.5rem;
  color: var(--text-secondary);
}
.markdown-body li {
  margin-bottom: 0.4rem;
  line-height: 1.6;
}
.markdown-body li strong {
  color: var(--text-primary);
}

/* Code */
.markdown-body code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--bg-card);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  color: var(--accent-hover);
}
.markdown-body pre {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  overflow-x: auto;
  margin: 1rem 0 1.5rem;
  line-height: 1.5;
}
.markdown-body pre code {
  background: none;
  padding: 0;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

/* Tables — stay within the prose column and scroll horizontally when too wide.
   Never break out into the sidebar. */
.markdown-body table {
  display: block;
  overflow-x: auto;
  max-width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  margin: 1.5rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* On very wide viewports, modestly widen tables past the 860px prose column using negative
   margins. The breakpoint is chosen so the negative margin never crosses the sidebar. */
@media (min-width: 1400px) {
  .markdown-body table {
    margin-left: -6rem;
    margin-right: -6rem;
    max-width: calc(100% + 12rem);
  }
}
.markdown-body thead {
  background: var(--bg-secondary);
}
.markdown-body th {
  padding: 0.75rem 1rem;
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
.markdown-body td {
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  color: var(--text-secondary);
  vertical-align: top;
  white-space: normal;
  min-width: 120px;
}
.markdown-body tr:last-child td { border-bottom: none; }
.markdown-body tr:hover td { background: rgba(26, 26, 37, 0.5); }
.markdown-body td:first-child { font-weight: 600; color: var(--text-primary); white-space: nowrap; min-width: 140px; }

@media (max-width: 768px) {
  .markdown-body table {
    width: 100%;
    max-width: 100%;
    margin: 1rem 0;
  }
}

/* Blockquotes */
.markdown-body blockquote {
  border-left: 3px solid var(--accent);
  padding: 0.5rem 1rem;
  margin: 1rem 0;
  background: rgba(99, 102, 241, 0.05);
  border-radius: 0 var(--radius) var(--radius) 0;
}
.markdown-body blockquote p {
  color: var(--text-secondary);
  margin-bottom: 0;
}

/* Loading state */
.loading {
  color: var(--text-muted);
  text-align: center;
  padding: 4rem 0;
}

.content-footer a { color: var(--accent); text-decoration: none; }

/* === Responsive === */

/* Tablet + below: drawer-style sidebar, topbar becomes a single flex row */
@media (max-width: 960px) {
  :root { --sidebar-width: 100%; }
  .topbar {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
    height: var(--topbar-height);
    padding: 0 0.75rem 0 3rem;  /* leave room for hamburger on the left */
  }
  .topbar-brand {
    border-right: none;
    padding: 0;
    height: auto;
    font-size: 1rem;
    flex: 0 0 auto;
    white-space: nowrap;
  }
  .sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    left: 0.25rem;
    top: 50%;
    transform: translateY(-50%);
    width: 2.5rem;
    height: 2.5rem;
    font-size: 1.5rem;
    line-height: 1;
    border-radius: 8px;
    padding: 0;
  }
  .sidebar-toggle:hover,
  .sidebar-toggle:focus-visible {
    background: var(--bg-card);
  }
  .topbar-search {
    flex: 1 1 auto;
    min-width: 0;
    padding: 0;
    max-width: none;
  }
  .search-field {
    padding: 0.35rem 0.6rem;
  }
  .search-results {
    left: 0.75rem;
    right: 0.75rem;
  }
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: var(--topbar-height);
    left: 0;
    right: 0;
    height: calc(100vh - var(--topbar-height));
    width: 280px;
    max-width: 80vw;
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 90;
  }
  .sidebar.open { transform: translateX(0); }
  .content { padding: 2rem 1.25rem; }
  .markdown-body h1 { font-size: 1.75rem; }
  .markdown-body h2 { font-size: 1.35rem; }
}

/* Phone: collapse brand text + slash hint to maximize search space */
@media (max-width: 640px) {
  .topbar {
    padding: 0 0.5rem 0 2.75rem;
    gap: 0.4rem;
  }
  .topbar-brand {
    font-size: 0.95rem;
  }
  .sidebar-toggle {
    left: 0.15rem;
    width: 2.25rem;
    height: 2.25rem;
  }
  .search-field input {
    font-size: 16px;  /* prevents iOS zoom on focus */
  }
  .search-kbd { display: none; }
  .content { padding: 1.5rem 1rem; }
  .markdown-body pre { font-size: 0.8rem; }

  /* Stacked-card table layout: at phone widths, multi-column tables can't
     fit without squishing cells into single-word columns. Each row becomes
     a card; each cell renders inline as "LABEL  value" using the data-label
     set in JS from the corresponding <th>. */
  .markdown-body table {
    display: block;
    border: none;
    border-radius: 0;
    background: none;
    font-size: 0.9rem;
    margin: 1.25rem 0;
    overflow: visible;
  }
  .markdown-body thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }
  .markdown-body tbody { display: block; }
  .markdown-body tr {
    display: block;
    position: relative;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.5rem 0.85rem 0.65rem;
    margin-bottom: 0.6rem;
    transition: border-color 0.15s, background 0.15s;
  }
  .markdown-body tr:hover td { background: transparent; }
  /* Cards that contain a primary link get a chevron affordance and an
     active state so they read as tappable. */
  .markdown-body tr:has(.card-primary-link) {
    padding-right: 2rem;
  }
  .markdown-body tr:has(.card-primary-link)::after {
    content: '›';
    position: absolute;
    right: 0.85rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 1.4rem;
    line-height: 1;
    pointer-events: none;
  }
  .markdown-body tr:has(.card-primary-link:active),
  .markdown-body tr:has(.card-primary-link):active {
    border-color: var(--accent);
    background: rgba(99, 102, 241, 0.08);
  }
  .markdown-body .card-primary-link::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 1;
  }
  .markdown-body td a:not(.card-primary-link) {
    position: relative;
    z-index: 2;
  }
  .markdown-body td {
    display: grid;
    grid-template-columns: 5.5rem 1fr;
    column-gap: 0.75rem;
    align-items: baseline;
    padding: 0.3rem 0;
    border: none;
    min-width: 0;
    white-space: normal;
    color: var(--text-secondary);
    line-height: 1.5;
  }
  .markdown-body td + td {
    border-top: 1px solid var(--border);
    padding-top: 0.45rem;
    margin-top: 0.1rem;
  }
  .markdown-body td:first-child {
    white-space: normal;
    min-width: 0;
    font-weight: 600;
  }
  .markdown-body td::before {
    content: attr(data-label);
    display: block;
    font-family: var(--font-sans);
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-muted);
    line-height: 1.6;
  }
  .markdown-body td:not([data-label])::before { content: ''; }
  /* Single-column tables (rare) and tables without thead fall back to a
     plain stacked layout without the label gutter. */
  .markdown-body td:not([data-label]) {
    display: block;
  }
}

/* Phone-narrow: hide the brand label, keep just the `>_` glyph */
@media (max-width: 420px) {
  .topbar-brand {
    font-size: 0;  /* hide the text node */
  }
  .topbar-brand span {
    font-size: 1rem;  /* keep the `>_` visible */
  }
}
