/* ======================================================================================
   "On this page": the table of contents on every article and guide.
   Markup is written into each article by scripts/add-toc.py; assets/toc.js adds the
   active-section highlight and opens the card beside the article on wide screens.
   Only pebble tokens here (colour, radius, space, motion), so light, dark and every
   Comfort switch follow for free.

   The brand idea is the logo's jumping "d": a pebble that hops up, tips back and leaves
   its shadow dash on the baseline. The mark in the heading is that pebble at rest. The
   dot on the rail is the same pebble: still for every section, hopping for the one you
   are reading.

   Phone and tablet: a native <details> card right after the intro, closed.
   Wide (1200px and up): the same card floats into the right margin and stays put as you
   scroll. It is out of the text flow (negative margin), so the article never shifts.
   ====================================================================================== */

:root {
  --toc-w: 15rem;                  /* the card, when it sits in the margin */
  --toc-gap: 2rem;                 /* air between the article column and the card */
  --toc-top: 6.5rem;               /* where it sticks: under the sticky header */
  --rail: 1.25rem;                 /* x of the rail and the dots, from the list's inner edge */
  --gut: calc(var(--rail) + var(--s3));   /* the link box starts here, to the right of the rail, so its focus ring and tint never cross it */
}

nav.toc {
  margin-block: var(--s6) 0;
  font-size: var(--t-small);
  line-height: 1.35;
}

.toc-box { background: var(--surface); border-radius: var(--r-3); box-shadow: var(--shadow-1); }

/* ---------- the heading row (the <summary>) ---------- */
.toc-head {
  display: flex; align-items: center; gap: var(--s3);
  min-height: 56px; padding: var(--s2) var(--s4) var(--s2) var(--s5);
  list-style: none; cursor: pointer; border-radius: var(--r-3);
  -webkit-tap-highlight-color: transparent;
}
.toc-head::-webkit-details-marker { display: none; }
.toc-head::after {                 /* chevron: points down when closed, up when open */
  content: ""; flex: none; width: 8px; height: 8px; margin: 0 var(--s1) 3px var(--s1);
  border: solid var(--ink-2); border-width: 0 2px 2px 0; transform: rotate(45deg);
  transition: transform calc(var(--mid) * var(--motion)) var(--ease);
}
.toc-box[open] > .toc-head::after { transform: rotate(-135deg) translate(-2px, -2px); margin-bottom: 0; }
@media (hover: hover) { .toc-head:hover .toc-title { color: var(--accent); } }
.toc-head:focus-visible { outline-offset: -3px; }

.toc-title { font: 760 1.0625rem/1.2 var(--font-display); letter-spacing: -0.01em; color: var(--ink); transition: color calc(var(--fast) * var(--motion)) var(--ease); }
.toc-count { margin-left: auto; color: var(--ink-3); font-size: var(--t-micro); font-weight: 700; white-space: nowrap; }

/* the mark: the logo's pebble, lifted and tipped back, with its dash underneath */
.toc-mark { position: relative; flex: none; width: 20px; height: 22px; }
.toc-mark::before {
  content: ""; position: absolute; left: 4px; top: 0; width: 12px; height: 13px;
  border-radius: 46% 46% 50% 50%; background: var(--logo-oddly); transform: rotate(-6deg);
}
.toc-mark::after {
  content: ""; position: absolute; left: 2px; bottom: 1px; width: 16px; height: 2px;
  border-radius: 2px; background: var(--logo-play); opacity: 0.8;
}

/* ---------- the list: a rail with a pebble on it for every section ---------- */
nav.toc ol.toc-list {
  position: relative; list-style: none; margin: 0; max-width: none;
  display: block; padding: var(--s1) var(--s3) var(--s4) var(--s3);
}
nav.toc ol.toc-list > li { position: relative; }
nav.toc ol.toc-list > li::before {   /* the rail, one length per section so a scrolling list keeps it */
  content: ""; position: absolute; left: calc(var(--rail) - 1px); top: 0; bottom: 0; width: 2px; background: var(--line);
}
nav.toc ol.toc-list > li:first-child::before { top: 1.375rem; border-radius: 2px 2px 0 0; }
nav.toc ol.toc-list > li:last-child::before { bottom: 1.25rem; border-radius: 0 0 2px 2px; }
nav.toc ol.toc-list ol { list-style: none; margin: 0; padding: 0; max-width: none; display: block; }
nav.toc ol.toc-list li { margin: 0; padding: 0; max-width: none; }

.toc-list a {
  position: relative; display: block; min-height: 44px; margin-left: var(--gut);
  padding: 11px var(--s3) 11px calc(var(--s5) - var(--s3)); border-radius: var(--r-2);
  color: var(--ink-2); text-decoration: none; overflow-wrap: anywhere;
  transition: color calc(var(--fast) * var(--motion)) var(--ease), background-color calc(var(--fast) * var(--motion)) var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.toc-list ol a { padding-left: calc(var(--s6) - var(--s3)); min-height: 40px; padding-block: 9px; font-size: 0.9375rem; }
@media (hover: hover) { .toc-list a:hover { color: var(--ink); background: var(--surface-2); } }
.toc-list a:focus-visible { outline-offset: -2px; border-radius: var(--r-2); }

/* the pebble on the rail */
.toc-list a::before {
  content: ""; position: absolute; left: calc(var(--rail) - 4px - var(--gut)); top: 50%; width: 8px; height: 8px; margin-top: -4px;
  border-radius: 50%; background: var(--line-strong);
  transition: transform calc(var(--mid) * var(--motion)) var(--ease), background-color calc(var(--mid) * var(--motion)) var(--ease), height calc(var(--mid) * var(--motion)) var(--ease);
}
.toc-list ol a::before { left: calc(var(--rail) - 3px - var(--gut)); width: 6px; height: 6px; margin-top: -3px; }
/* its shadow, which only shows once the pebble has hopped */
.toc-list a::after {
  content: ""; position: absolute; left: calc(var(--rail) - 6px - var(--gut)); top: 50%; width: 12px; height: 2px; margin-top: 8px;
  border-radius: 2px; background: var(--ink); opacity: 0; transform: scaleX(0.4);
  transition: opacity calc(var(--mid) * var(--motion)) var(--ease), transform calc(var(--mid) * var(--motion)) var(--ease);
}
.toc-list ol a::after { margin-top: 7px; }

/* the section you are in */
.toc-list a[aria-current="location"] { color: var(--ink); background: var(--accent-soft); }
.toc-list a[aria-current="location"]::before {
  background: var(--accent); width: 10px; height: 11px; margin-top: -9px; margin-left: -1px;
  transform: rotate(-6deg);
}
.toc-list ol a[aria-current="location"]::before { width: 8px; height: 9px; margin-top: -7px; margin-left: -1px; }
.toc-list a[aria-current="location"]::after { opacity: 0.55; transform: scaleX(1); }

/* the pebble hops in once when the card opens: one nudge, never a loop */
@keyframes toc-hop { 0% { transform: translateY(0); } 40% { transform: translateY(-3px) rotate(-6deg); } 100% { transform: translateY(0) rotate(-6deg); } }
@media (prefers-reduced-motion: no-preference) {
  :root:not(.cf-still) .toc-box[open] .toc-list a[aria-current="location"]::before { animation: toc-hop calc(480ms * var(--motion)) var(--ease) 1; }
}

/* high contrast: say it with ink, not tint */
:root.cf-contrast .toc-list a[aria-current="location"] { box-shadow: inset 0 0 0 2px var(--ink); background: transparent; }
@media (forced-colors: active) {
  .toc-box { box-shadow: none; border: 1px solid CanvasText; }
  .toc-list a::before, .toc-mark::before { background: CanvasText; }
  .toc-list a[aria-current="location"] { outline: 2px solid Highlight; outline-offset: -2px; }
}

/* ---------- jumping to a section: land below the sticky header ---------- */
.prose :is(h2, h3)[id] { scroll-margin-top: var(--s3);   /* html already has scroll-padding-top for the sticky header */ }
@media (prefers-reduced-motion: no-preference) {
  html:has(nav.toc):not(.cf-still) { scroll-behavior: smooth; }
}

/* ---------- wide screens: the card lives in the right margin and follows you ---------- */
@media (min-width: 1200px) {
  /* The article gives up a strip on its right for the card. The reading column does not move
     (it hugs the left), only the full width blocks (grids, button rows) get a little narrower. */
  main:has(> article > nav.toc) { padding-right: calc(var(--toc-w) + var(--toc-gap)); }
  nav.toc {
    float: right; clear: right; position: sticky; top: var(--toc-top);
    width: var(--toc-w); margin: var(--s7) calc(-1 * (var(--toc-w) + var(--toc-gap))) var(--s5) 0;   /* net width below zero: text never wraps around it */
  }
  .toc-head { min-height: 52px; padding-left: var(--s4); }
  .toc-count { display: none; }
  nav.toc ol.toc-list { max-height: calc(100vh - var(--toc-top) - 6.5rem); overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; padding-bottom: var(--s3); }
  .toc-list a { min-height: 36px; padding-block: 8px; font-size: 0.9375rem; }
  .toc-list ol a { min-height: 32px; padding-block: 6px; font-size: 0.875rem; }
}
