/* Doc layout: topbar, sticky sidebar with segmented tabs, centred main column, chapter nav.
   Prose sits directly on the page background; only cards, callouts and tables are raised. */

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--topbar-height) + 1rem);
  -webkit-text-size-adjust: 100%;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

body.shell {
  --sidebar-bg: var(--bg);
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

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

/* Buttons share one quiet look: UI font, 44px target, subtle press feedback. */
.topbar button,
.tabs button {
  font-family: var(--font-ui);
  min-height: var(--tap);
  cursor: pointer;
}

.topbar button:active,
.tabs button:active {
  transform: scale(0.97);
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--gutter);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  font-family: var(--font-ui);
  font-size: var(--text-sm);
}

.topbar a.topbar__site {
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  color: var(--fg);
  text-decoration: none;
  white-space: nowrap;
}

.topbar__page {
  flex: 1;
  min-width: 0;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__page::before {
  content: "/";
  margin-right: var(--space-3);
  color: var(--border);
}

.topbar button {
  font-size: var(--text-sm);
  font-weight: 700;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: var(--radius);
  padding: 0 0.9em;
}

.topbar button:hover {
  color: var(--fg);
  background: var(--subtle);
}

.topbar button:focus-visible,
.tabs button:focus-visible,
.sidebar a:focus-visible,
.chapter-nav a:focus-visible,
.card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.drawer-toggle {
  display: none;
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  align-items: start;
}

/* Sidebar: sits on the page background, no panel. */
.sidebar {
  position: sticky;
  top: var(--topbar-height);
  height: calc(100vh - var(--topbar-height));
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--sidebar-bg);
  padding: 0 var(--space-3);
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  overscroll-behavior: contain;
}

/* Tabs: a segmented control. The box-shadow paints the sidebar colour around the pill group
   so links scrolling underneath it stay hidden. */
.tabs {
  display: flex;
  gap: 2px;
  position: sticky;
  top: var(--space-3);
  z-index: 1;
  margin: var(--space-3) 0;
  padding: 3px;
  background: var(--subtle);
  border-radius: var(--radius);
  box-shadow: 0 0 0 var(--space-3) var(--sidebar-bg);
}

.tabs button {
  flex: 1;
  font-size: var(--text-xs);
  font-weight: 700;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: calc(var(--radius) - 3px);
  padding: 0 0.25em;
  transition: background-color 0.15s ease, color 0.15s ease;
}

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

.tabs button[aria-selected="true"] {
  color: var(--fg);
  background: var(--surface);
  box-shadow: var(--shadow-card);
}

.tabpanel {
  padding: var(--space-2) 0 var(--space-6);
}

.tabpanel[hidden] {
  display: none;
}

.tabpanel ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

/* The prose bullet dots do not belong in navigation. */
.tabpanel li {
  margin: 0;
}

.prose .tabpanel li::before {
  content: none;
}

/* Nested h3 entries: indented and smaller. */
.tabpanel ul ul {
  margin: 0 0 var(--space-1) var(--space-3);
}

.tabpanel a {
  display: block;
  padding: 0.5em 0.75em;
  color: var(--muted);
  text-decoration: none;
  font-weight: 400;
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius-code) var(--radius-code) 0;
  line-height: 1.35;
}

.tabpanel ul ul a {
  font-size: var(--text-xs);
  padding: 0.4em 0.75em;
}

.tabpanel a:hover {
  color: var(--fg);
  background: var(--subtle);
}

.tabpanel a.is-active,
.tabpanel a[aria-current="page"] {
  background: var(--subtle);
  border-left-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
}

.tabpanel .nav-empty {
  color: var(--muted);
  padding: 0.5em 0.75em;
}

/* Chapter list is numbered: the real chapters sit between "Overview" (first) and "All" (last). */
.tabpanel[data-panel="chapters"] > ul {
  counter-reset: chapter;
}

.tabpanel[data-panel="chapters"] > ul > li:not(:first-child):not(:last-child) > a::before {
  counter-increment: chapter;
  content: counter(chapter) ". ";
  font-variant-numeric: tabular-nums;
}

/* Main */
.main {
  min-width: 0;
  padding: var(--space-6) var(--space-6) var(--space-8);
}

.content {
  max-width: var(--measure-wide);
  margin: 0 auto;
}

.content > :not(.chapter, section),
.content section > * {
  max-width: var(--measure);
  margin-left: auto;
  margin-right: auto;
}

.content section > .wide,
.content > .wide,
.content > .card-grid,
.content > .table-wrap,
.content > figure,
.content section > .table-wrap,
.content section > figure,
.content section > .cols-2 {
  max-width: var(--measure-wide);
}

.content section {
  margin: 0;
}

.content section > section,
.content .chapter,
.content .chapter > section {
  max-width: none;
}

/* A section after the first starts with a rule above its h2, to mark the break. */
.content section + section > h2:first-child {
  margin-top: 0;
  padding-top: 2.2rem;
  border-top: 1px solid var(--border);
}

.content section + section {
  margin-top: 2.2rem;
}

.chapter + .chapter {
  margin-top: var(--space-8);
}

.chapter-head {
  max-width: var(--measure);
  margin: 0 auto;
}

.chapter-head .kicker {
  margin: 0 0 var(--space-2);
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.content h1:first-child {
  margin-top: 0;
}

.content .page-lede {
  color: var(--muted);
  font-family: var(--font-ui);
  font-size: 1.1rem;
}

/* Cards on the page landing and the home page */
.prose .card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr));
  gap: var(--space-4);
  max-width: var(--measure-wide);
  margin: 0 auto;
  padding: 0;
  list-style: none;
}

.prose .card-grid > li {
  margin: 0;
}

.prose .card-grid > li::before {
  content: none;
}

.prose a.card {
  display: block;
  height: 100%;
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  color: var(--fg);
  font-family: var(--font-ui);
  font-weight: 400;
  text-decoration: none;
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.prose a.card:hover {
  box-shadow: var(--shadow-lift);
  transform: translateY(-1px);
}

.prose a.card:active {
  transform: scale(0.98);
}

.card .card__title {
  margin: 0 0 var(--space-2);
  padding: 0;
  border: 0;
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  color: var(--fg);
}

.card .card__text {
  margin: 0 0 var(--space-2);
  font-size: var(--text-sm);
  color: var(--muted);
}

.card .card__meta {
  margin: 0;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--muted);
}

/* Prev / next: two card buttons */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  gap: var(--space-4);
  max-width: var(--measure);
  margin: var(--space-7) auto 0;
  font-family: var(--font-ui);
}

.chapter-nav a {
  display: block;
  flex: 1;
  min-width: 0;
  min-height: var(--tap);
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  border: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  color: var(--fg);
  font-weight: 700;
  text-decoration: none;
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.chapter-nav a:hover {
  box-shadow: var(--shadow-lift);
}

.chapter-nav a:active {
  transform: scale(0.98);
}

.chapter-nav .chapter-nav__next {
  text-align: right;
}

.chapter-nav small {
  display: block;
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.chapter-nav span.chapter-nav__spacer {
  flex: 1;
}

.drawer-backdrop {
  display: none;
}

/* Mobile */
@media (max-width: 899px) {
  .drawer-toggle {
    display: inline-block;
  }

  .layout {
    display: block;
  }

  /* The drawer is a raised panel, so the sidebar colour switches to the surface. */
  body.shell {
    --sidebar-bg: var(--surface);
  }

  .sidebar {
    position: fixed;
    top: var(--topbar-height);
    left: 0;
    bottom: 0;
    z-index: 40;
    width: min(var(--sidebar-width), 88vw);
    height: auto;
    border-right: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: none;
    visibility: hidden;
  }

  body.drawer-open .sidebar {
    transform: translateX(0);
    visibility: visible;
    box-shadow: var(--shadow-float);
  }

  body.drawer-open .drawer-backdrop {
    display: block;
    position: fixed;
    inset: var(--topbar-height) 0 0 0;
    z-index: 35;
    background: var(--backdrop);
  }

  body.drawer-open {
    overflow: hidden;
  }

  .main {
    padding: var(--space-5) var(--gutter) var(--space-7);
  }

  .topbar {
    gap: var(--space-2);
  }

  .content h1 { font-size: 1.8rem; }
  .content h2 { font-size: 1.4rem; }

  .chapter-nav {
    flex-direction: column;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar,
  .tabs button,
  .prose a.card,
  .chapter-nav a { transition: none; }
}
