/* Doc layout: topbar, sticky left pane, 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: one shared .btn look, defined below. */

/* Buttons: any link or button that should read as a button. Surface fill, 1px border,
   no underline in any state (the .prose a rules are beaten by .prose a.btn specificity). */
.btn,
.prose a.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  min-height: var(--tap);
  padding: 0.5em 1.1em;
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  font-weight: 700;
  line-height: 1.25;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}

.btn:hover,
.prose a.btn:hover {
  background: var(--subtle);
  color: var(--fg);
  text-decoration: none;
  box-shadow: var(--shadow-card);
}

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

.btn:focus-visible,
.prose a.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn .icon {
  flex: none;
}

/* Ghost: no fill or border until hovered (topbar buttons). */
.btn--ghost,
.prose a.btn--ghost {
  color: var(--muted);
  background: none;
  border-color: transparent;
}

.btn.is-disabled {
  opacity: 0.4;
  pointer-events: none;
}

/* 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);
}

.sidebar 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;
}

/* Left pane: back button, heading, a flat list of entries, prev/next pair at the bottom. */
.pane {
  display: flex;
  flex-direction: column;
  min-height: 100%;
  padding: var(--space-3) 0 0;
}

.pane__list {
  margin-bottom: var(--space-4);
}

.pane .pane__label {
  margin: var(--space-3) 0 var(--space-2);
  padding: 0 0.75em;
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

/* Chapter heading: number then title. */
.pane .pane__title {
  display: flex;
  gap: 0.5em;
  margin: var(--space-3) 0;
  padding: 0 0.75em;
  border: 0;
  font-size: 1.05rem;
  font-weight: 800;
  line-height: 1.3;
  letter-spacing: -0.01em;
  color: var(--fg);
}

.pane__num {
  flex: none;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.prose a.pane__back {
  display: flex;
  justify-content: flex-start;
}

/* Prev / next pair, stuck to the bottom of the pane. The padding and fill hide entries
   scrolling underneath it. */
.pane__pager {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: var(--space-2);
  margin-top: auto;
  padding: var(--space-3) 0;
  background: var(--sidebar-bg);
}

.pane__pager-btn {
  flex: 1 1 0;
  padding: 0.5em;
}

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

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

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

.pane__list a {
  display: flex;
  gap: 0.6em;
  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;
}

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

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

.pane__text {
  min-width: 0;
}

/* One-line chapter summary under a chapter title. */
.pane__list small {
  display: block;
  margin-top: 0.15em;
  font-size: var(--text-xs);
  font-weight: 400;
  color: var(--muted);
}

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

/* 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.btn {
  flex: 1;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 0;
  min-width: 0;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  text-align: left;
}

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

.chapter-nav a.chapter-nav__next {
  align-items: flex-end;
  text-align: right;
}

.chapter-nav .chapter-nav__name {
  display: block;
  margin-top: 0.15em;
}

.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,
  .prose a.card,
  .btn,
  .chapter-nav a { transition: none; }
}
