:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1f2328;
  --muted: #656d76;
  --border: #d0d7de;
  --accent: #2563eb;
  --code-bg: #f6f8fa;
  --table-head: #f6f8fa;
  --hover: #f6f8fa;
  --sidebar-w: 17rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e10;
    --fg: #e6e6e6;
    --muted: #9aa0a6;
    --border: #2a2a30;
    --accent: #6ea8fe;
    --code-bg: #17171a;
    --table-head: #17171a;
    --hover: #17171a;
  }
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

html,
body {
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  line-height: 1.65;
}

.skip-link {
  position: absolute;
  left: -9999px;
}

.skip-link:focus {
  left: 1rem;
  top: 1rem;
  z-index: 10;
  background: var(--bg);
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
}

/* Layout */
.layout {
  display: flex;
  align-items: flex-start;
  gap: 2.5rem;
  max-width: 66rem;
  margin: 0 auto;
  padding: 0 1.25rem;
}

.sidebar {
  position: sticky;
  top: 0;
  align-self: flex-start;
  flex: 0 0 var(--sidebar-w);
  width: var(--sidebar-w);
  max-height: 100vh;
  overflow-y: auto;
  padding: 1.5rem 0;
}

.home {
  display: inline-block;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  margin-bottom: 1.25rem;
}

.home:hover {
  color: var(--accent);
}

/* Page header: title + tabs */
.pagehead {
  max-width: 66rem;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 0;
}

.pagehead .home {
  margin-bottom: 1rem;
}

.pagehead h1 {
  font-size: clamp(1.8rem, 4vw, 2.4rem);
  line-height: 1.2;
  letter-spacing: -0.02em;
  margin: 0 0 1rem;
}

.tabs {
  display: flex;
  gap: 0.25rem;
  border-bottom: 1px solid var(--border);
}

.tab {
  padding: 0.6rem 0.9rem;
  margin-bottom: -1px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.95rem;
  border-bottom: 2px solid transparent;
}

.tab:hover {
  color: var(--fg);
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* Index */
/* The "Contents" toggle only appears on mobile; on desktop the disclosure is
   always open and the summary is hidden, so the sidebar looks unchanged. */
.toc-summary {
  display: none;
  list-style: none;
  cursor: pointer;
}

.toc-summary::-webkit-details-marker {
  display: none;
}

.toc {
  font-size: 0.875rem;
}

.toc-group {
  margin-bottom: 0.4rem;
}

.toc-group > summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.3rem 0;
  font-weight: 600;
  color: var(--fg);
}

.toc-group > summary::-webkit-details-marker {
  display: none;
}

.toc-group > summary::before {
  content: "›";
  color: var(--muted);
  font-weight: 700;
  transition: transform 0.15s ease;
}

.toc-group[open] > summary::before {
  transform: rotate(90deg);
}

.toc ul {
  list-style: none;
  margin: 0.15rem 0 0.6rem;
  padding: 0;
  margin-left: 0.4rem;
  border-left: 1px solid var(--border);
}

.toc li a {
  display: block;
  padding: 0.25rem 0.6rem;
  margin-left: -1px;
  color: var(--muted);
  text-decoration: none;
  border-left: 2px solid transparent;
  line-height: 1.35;
}

.toc li a:hover {
  color: var(--fg);
  background: var(--hover);
}

.toc li a.active {
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}

/* Article */
.prose {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 46rem;
  padding: 1.5rem 0 5rem;
}

.prose h2 {
  font-size: 1.6rem;
  line-height: 1.25;
  letter-spacing: -0.01em;
  margin: 2.5rem 0 0.75rem;
  scroll-margin-top: 1rem;
}

.prose h3 {
  font-size: 1.2rem;
  margin: 1.75rem 0 0.5rem;
  scroll-margin-top: 1rem;
}

.prose h4 {
  font-size: 1rem;
  margin: 1.5rem 0 0.4rem;
  scroll-margin-top: 1rem;
}

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

.prose a {
  color: var(--accent);
}

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

.prose ul,
.prose ol {
  margin: 0 0 1rem;
  padding-left: 1.4rem;
}

.prose li {
  margin: 0.25rem 0;
}

.prose blockquote {
  margin: 1rem 0;
  padding: 0.25rem 1rem;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

.prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0 1.5rem;
  font-size: 0.95rem;
  display: block;
  overflow-x: auto;
}

.prose th,
.prose td {
  border: 1px solid var(--border);
  padding: 0.5rem 0.7rem;
  text-align: left;
  vertical-align: top;
}

.prose th {
  background: var(--table-head);
  font-weight: 600;
}

.prose code {
  background: var(--code-bg);
  padding: 0.15em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

/* On narrow screens the index becomes a sticky bar that stays visible while
   reading, labelled with the current section, and drops down as an overlay. */
@media (max-width: 900px) {
  .layout {
    flex-direction: column;
    gap: 0.5rem;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 20;
    width: 100%;
    flex: none;
    max-height: none;
    overflow: visible;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    padding: 0.5rem 0 0.6rem;
  }

  .pagehead {
    padding-top: 1rem;
  }

  .toc-disclosure {
    position: relative;
  }

  .toc-summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.7rem 0.85rem;
    font-weight: 600;
    border: 1px solid var(--border);
    border-radius: 8px;
  }

  .toc-current {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .toc-summary::after {
    content: "›";
    color: var(--muted);
    font-weight: 700;
    flex: 0 0 auto;
    transition: transform 0.15s ease;
  }

  .toc-disclosure[open] > .toc-summary::after {
    transform: rotate(90deg);
  }

  /* Index drops down as a scrollable overlay instead of pushing the article */
  .toc {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    z-index: 20;
    max-height: 70vh;
    overflow-y: auto;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    padding: 0.5rem 0.85rem 0.75rem;
    font-size: 0.95rem;
  }

  .toc-group.current > summary {
    color: var(--accent);
  }

  /* Roomier tap targets on touch screens */
  .toc-group > summary {
    padding: 0.55rem 0;
  }

  .toc li a {
    padding: 0.5rem 0.7rem;
  }

  .prose {
    max-width: none;
  }

  .prose h2,
  .prose h3,
  .prose h4 {
    scroll-margin-top: 4.5rem;
  }
}
