/*
 * Template: Margin Notes (longform)
 * Literary technical monograph stylesheet with Light, Sepia, and Dark reading modes,
 * asymmetric sidenote layout (with Popover API presentation on narrow viewports),
 * and interactive spring-physics laboratory styling.
 */

:root {
  /* Core palette tokens (Light monograph default) */
  --paper: #fbf9f5;
  --surface: #f3efe7;
  --surface-elevated: #ffffff;
  --ink: #181715;
  --ink-secondary: #3f3b35;
  --ink-muted: #5d574e;
  --cobalt: #1d4ed8;
  --cobalt-hover: #1e40af;
  --cobalt-soft: rgba(29, 78, 216, 0.09);
  --ochre: #b45309;
  --ochre-text: #92400e;
  --ochre-soft: rgba(180, 83, 9, 0.09);
  --rule: #e4ddd0;
  --rule-strong: #cfc5b4;
  --code-bg: #f3efe7;
  --code-kw: #1d4ed8;
  --code-fn: #9a3412;
  --code-num: #0f766e;
  --code-cm: #6b6459;
  --shadow-popover: 0 18px 40px -12px rgba(24, 23, 21, 0.22);

  /* Typography */
  --font-serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --reader-scale: 1;

  /* Spacing tokens */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;
  --space-8: 4.5rem;

  /* Radius tokens */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-pill: 999px;

  /* Layout measurements */
  --measure: 66ch;
  --margin-col: 256px;
  --gutter: clamp(2rem, 4vw, 3.75rem);
  --bar-height: 56px;
}

/* Sepia book mode */
html[data-theme="sepia"] {
  --paper: #f4ecd8;
  --surface: #eae0c7;
  --surface-elevated: #f8f2e3;
  --ink: #271f16;
  --ink-secondary: #493b2b;
  --ink-muted: #65533e;
  --cobalt: #1e40af;
  --cobalt-hover: #1e3a8a;
  --cobalt-soft: rgba(30, 64, 175, 0.1);
  --ochre: #9a4408;
  --ochre-text: #7c3506;
  --ochre-soft: rgba(154, 68, 8, 0.1);
  --rule: #d8c8ab;
  --rule-strong: #c0ab87;
  --code-bg: #ebe1c9;
  --code-kw: #1e40af;
  --code-fn: #9a3412;
  --code-num: #115e59;
  --code-cm: #6d5b44;
  --shadow-popover: 0 18px 40px -12px rgba(39, 31, 22, 0.25);
}

/* Dark ink mode */
html[data-theme="dark"] {
  --paper: #141311;
  --surface: #1d1b18;
  --surface-elevated: #24211d;
  --ink: #eee9df;
  --ink-secondary: #ccc4b7;
  --ink-muted: #a39a8c;
  --cobalt: #60a5fa;
  --cobalt-hover: #93c5fd;
  --cobalt-soft: rgba(96, 165, 250, 0.14);
  --ochre: #f59e0b;
  --ochre-text: #fbbf24;
  --ochre-soft: rgba(245, 158, 11, 0.13);
  --rule: #2e2a24;
  --rule-strong: #443e35;
  --code-bg: #1b1916;
  --code-kw: #93c5fd;
  --code-fn: #fbbf24;
  --code-num: #5eead4;
  --code-cm: #91887b;
  --shadow-popover: 0 20px 44px -10px rgba(0, 0, 0, 0.65);
}

/* Base & themed browser surfaces */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  background-color: var(--paper);
  color: var(--ink);
  font-family: var(--font-serif);
  font-size: 16px;
  scroll-behavior: smooth;
  accent-color: var(--cobalt);
  scrollbar-color: var(--rule-strong) var(--paper);
  scrollbar-width: thin;
}

body {
  margin: 0;
  background-color: var(--paper);
  color: var(--ink);
  line-height: 1.68;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: clip;
  transition: background-color 180ms ease, color 180ms ease;
}

::selection {
  background-color: var(--cobalt-soft);
  color: var(--ink);
}

::-webkit-scrollbar {
  width: 9px;
  height: 9px;
}

::-webkit-scrollbar-track {
  background: var(--paper);
}

::-webkit-scrollbar-thumb {
  background-color: var(--rule-strong);
  border-radius: var(--radius-pill);
  border: 2px solid var(--paper);
}

:focus-visible {
  outline: 2px solid var(--cobalt);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  top: -100px;
  left: var(--space-4);
  z-index: 200;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-sans);
  font-size: 0.875rem;
  font-weight: 500;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  text-decoration: none;
}

.skip-link:focus {
  top: var(--space-3);
}

/* Sticky reading toolbar */
.reading-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}

.reading-bar-inner {
  max-width: 1040px;
  margin: 0 auto;
  min-height: var(--bar-height);
  padding: var(--space-2) clamp(1rem, 3vw, 2rem);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
}

.bar-brand {
  display: flex;
  align-items: center;
  gap: var(--space-5);
  min-width: 0;
}

.brand-link {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--ink);
  text-decoration: none;
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.bar-nav {
  display: flex;
  align-items: center;
  gap: var(--space-4);
}

.bar-nav a {
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--ink-muted);
  text-decoration: none;
  padding: var(--space-1) 0;
  transition: color 140ms ease;
}

.bar-nav a:hover {
  color: var(--cobalt);
}

.bar-controls {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  justify-content: flex-end;
}

.control-group {
  display: inline-flex;
  align-items: center;
  background-color: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  padding: 2px;
}

.ctrl-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink-secondary);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 500;
  min-height: 32px;
  min-width: 34px;
  padding: 0 9px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  cursor: pointer;
  transition: background-color 140ms ease, color 140ms ease;
}

.ctrl-btn:hover:not(:disabled) {
  color: var(--ink);
  background-color: var(--surface-elevated);
}

.ctrl-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.theme-btn[aria-checked="true"] {
  background-color: var(--surface-elevated);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.scale-readout {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  color: var(--ink-muted);
  min-width: 44px;
  text-align: center;
  user-select: none;
}

.progress-badge {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  color: var(--ochre-text);
  min-width: 38px;
  text-align: right;
}

.progress-track {
  position: absolute;
  left: 0;
  right: 0;
  bottom: -1px;
  height: 2px;
  background: transparent;
  overflow: hidden;
}

.progress-fill {
  width: 100%;
  height: 100%;
  background-color: var(--cobalt);
  transform-origin: left center;
  transform: scaleX(0);
  will-change: transform;
}

/* Main page shell */
.page-shell {
  max-width: 1040px;
  margin: 0 auto;
  padding: clamp(2.5rem, 6vw, 4.75rem) clamp(1rem, 3vw, 2rem) var(--space-8);
  scroll-margin-top: var(--bar-height);
}

/* Monograph header */
.essay-header {
  max-width: var(--measure);
  margin-bottom: var(--space-7);
}

.essay-title {
  margin: 0 0 var(--space-4);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: clamp(2.15rem, 4.4vw, 3.25rem);
  line-height: 1.12;
  letter-spacing: -0.025em;
  color: var(--ink);
}

.essay-standfirst {
  margin: 0 0 var(--space-5);
  font-family: var(--font-serif);
  font-size: calc(clamp(1.125rem, 1.8vw, 1.325rem) * var(--reader-scale));
  line-height: 1.55;
  color: var(--ink-secondary);
}

.essay-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--space-3) var(--space-4);
  margin: 0 0 var(--space-6);
  padding: var(--space-4) 0;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  font-family: var(--font-sans);
}

.meta-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.meta-item dt {
  font-size: 0.72rem;
  color: var(--ink-muted);
}

.meta-item dd {
  margin: 0;
  font-size: 0.84rem;
  font-weight: 500;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.lead-diagram {
  margin: var(--space-6) 0 0;
  padding: var(--space-4) var(--space-4) var(--space-3);
  background-color: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
}

.lead-diagram svg {
  display: block;
  width: 100%;
  height: auto;
}

.lead-diagram figcaption {
  margin-top: var(--space-2);
  font-family: var(--font-sans);
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--ink-muted);
}

/* Two-column editorial prose + margin sidenotes */
.essay-section {
  margin-bottom: var(--space-7);
}

.prose-row {
  display: grid;
  grid-template-columns: minmax(0, var(--measure)) minmax(240px, 1fr);
  column-gap: var(--gutter);
  align-items: start;
}

.prose-col {
  font-size: calc(1.125rem * var(--reader-scale));
  line-height: 1.74;
  color: var(--ink);
}

.prose-col h2 {
  margin: 0 0 var(--space-4);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: calc(1.55rem * var(--reader-scale));
  line-height: 1.25;
  letter-spacing: -0.015em;
  color: var(--ink);
  scroll-margin-top: calc(var(--bar-height) + 1.5rem);
}

.prose-col p {
  margin: 0 0 var(--space-5);
}

.prose-col p:last-child {
  margin-bottom: 0;
}

.lead-paragraph::first-letter {
  font-family: var(--font-serif);
  font-weight: 600;
  float: left;
  font-size: 3.35em;
  line-height: 0.84;
  padding-right: 0.14em;
  padding-top: 0.06em;
  color: var(--ochre);
}

.prose-col a {
  color: var(--cobalt);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.prose-col a:hover {
  color: var(--cobalt-hover);
}

.prose-col code {
  font-family: var(--font-mono);
  font-size: 0.84em;
  background-color: var(--surface);
  border: 1px solid var(--rule);
  padding: 0.08em 0.36em;
  border-radius: var(--radius-sm);
  color: var(--ink);
}

.editorial-quote {
  margin: var(--space-6) 0;
  padding: var(--space-5) var(--space-5);
  border-top: 1px solid var(--rule-strong);
  border-bottom: 1px solid var(--rule-strong);
  background-color: transparent;
}

.editorial-quote p {
  margin: 0 0 var(--space-3);
  font-style: italic;
  font-size: calc(1.22rem * var(--reader-scale));
  line-height: 1.58;
  color: var(--ink);
}

.editorial-quote footer {
  font-family: var(--font-sans);
  font-size: 0.82rem;
  color: var(--ink-muted);
}

/* Sidenote reference button */
.sidenote-ref {
  appearance: none;
  border: 0;
  background: var(--ochre-soft);
  color: var(--ochre-text);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  vertical-align: super;
  padding: 2px 5px;
  margin: 0 2px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background-color 140ms ease, color 140ms ease;
}

.sidenote-ref:hover,
.sidenote-ref.is-active {
  background: var(--ochre);
  color: var(--paper);
}

/* Margin sidenotes: desktop margin presentation, narrow viewport Popover API */
.sidenote-stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

.sidenote[popover] {
  display: block;
  position: static;
  inset: auto;
  margin: 3.1rem 0 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ink-muted);
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  line-height: 1.58;
  overflow: visible;
  width: auto;
  max-width: none;
  transition: color 160ms ease, background-color 160ms ease;
}

.sidenote-stack .sidenote[popover] {
  margin-top: 2.2rem;
}

.sidenote-stack .sidenote[popover]:not(:first-child) {
  margin-top: 0;
}

.sidenote.is-highlighted {
  color: var(--ink);
}

.sidenote-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-1);
}

.sidenote-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--ochre-text);
  border-bottom: 1px solid var(--ochre);
  padding-bottom: 1px;
}

.sidenote-close {
  display: none;
  appearance: none;
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.sidenote p {
  margin: 0;
}

.sidenote code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  color: var(--ink);
}

/* Embedded Spring Physics & Easing Explorer */
.lab-section {
  margin: var(--space-7) 0;
  padding: clamp(1.25rem, 3vw, 2rem);
  background-color: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-lg);
  scroll-margin-top: calc(var(--bar-height) + 1.5rem);
}

.lab-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}

.lab-title {
  margin: 0 0 var(--space-2);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: clamp(1.35rem, 2.4vw, 1.65rem);
  line-height: 1.25;
  color: var(--ink);
}

.lab-subtitle {
  margin: 0;
  max-width: 58ch;
  font-family: var(--font-sans);
  font-size: 0.875rem;
  line-height: 1.55;
  color: var(--ink-muted);
}

.lab-subtitle code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  color: var(--ink);
}

.lab-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.btn-primary,
.btn-secondary {
  appearance: none;
  font-family: var(--font-sans);
  font-size: 0.84rem;
  font-weight: 500;
  min-height: 40px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.btn-primary {
  background-color: var(--cobalt);
  color: #ffffff;
  border: 1px solid var(--cobalt);
}

.btn-primary:hover {
  background-color: var(--cobalt-hover);
  border-color: var(--cobalt-hover);
}

.btn-secondary {
  background-color: var(--surface-elevated);
  color: var(--ink);
  border: 1px solid var(--rule-strong);
}

.btn-secondary:hover {
  border-color: var(--cobalt);
  color: var(--cobalt);
}

.lab-presets {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-5);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--rule);
}

.preset-btn {
  appearance: none;
  background-color: var(--surface-elevated);
  color: var(--ink-secondary);
  border: 1px solid var(--rule);
  border-radius: var(--radius-pill);
  font-family: var(--font-sans);
  font-size: 0.8rem;
  font-weight: 500;
  min-height: 36px;
  padding: 0 14px;
  cursor: pointer;
  transition: background-color 140ms ease, color 140ms ease, border-color 140ms ease;
}

.preset-btn:hover {
  border-color: var(--cobalt);
  color: var(--ink);
}

.preset-btn.is-active {
  background-color: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.lab-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(260px, 0.95fr);
  gap: var(--space-6);
  align-items: start;
}

.lab-visuals {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.curve-frame {
  background-color: var(--surface-elevated);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
}

.curve-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
  margin-bottom: var(--space-2);
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--ink-muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 2px;
}

.swatch-spring {
  background-color: var(--cobalt);
  height: 3px;
}

.swatch-ease {
  border-top: 2px dashed var(--ink-muted);
}

.swatch-target {
  border-top: 2px dashed var(--ochre);
}

#spring-curve-svg {
  display: block;
  width: 100%;
  height: auto;
}

.puck-stage {
  background-color: var(--surface-elevated);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  cursor: pointer;
  transition: border-color 140ms ease;
}

.puck-stage:hover {
  border-color: var(--cobalt);
}

.puck-track-row {
  display: grid;
  grid-template-columns: 82px minmax(0, 1fr);
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.track-label {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--ink-muted);
}

.track-rail {
  position: relative;
  height: 30px;
  display: flex;
  align-items: center;
}

.track-rail::before {
  content: "";
  position: absolute;
  left: 12px;
  right: 12px;
  height: 2px;
  background-color: var(--rule-strong);
  border-radius: 2px;
}

.track-mark {
  position: absolute;
  top: 50%;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 1.5px solid var(--rule-strong);
  background-color: var(--surface-elevated);
  transform: translateY(-50%);
}

.track-start {
  left: 8px;
}

.track-end {
  right: 8px;
}

.puck {
  position: absolute;
  left: 0;
  top: 50%;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  transform: translate3d(0, -50%, 0);
  will-change: transform;
}

.puck-spring {
  background-color: var(--cobalt);
  box-shadow: 0 2px 8px rgba(29, 78, 216, 0.35);
}

.puck-ease {
  background-color: var(--ink-muted);
  opacity: 0.75;
}

.puck-hint {
  margin: var(--space-2) 0 0;
  font-family: var(--font-sans);
  font-size: 0.76rem;
  color: var(--ink-muted);
}

/* Lab sliders & telemetry */
.lab-controls {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.slider-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.slider-label-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-family: var(--font-sans);
  font-size: 0.85rem;
}

.slider-label-row label {
  font-weight: 500;
  color: var(--ink);
}

.slider-label-row output {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
  color: var(--cobalt);
}

.slider-field input[type="range"] {
  width: 100%;
  height: 28px;
  margin: 0;
  cursor: pointer;
}

.slider-help {
  font-family: var(--font-sans);
  font-size: 0.75rem;
  color: var(--ink-muted);
}

.telemetry-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
  margin: var(--space-2) 0 0;
  padding-top: var(--space-4);
  border-top: 1px solid var(--rule);
}

.telemetry-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.telemetry-cell dt {
  font-family: var(--font-sans);
  font-size: 0.74rem;
  color: var(--ink-muted);
}

.telemetry-cell dd {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.95rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

/* Code snippet block */
.code-figure {
  margin: var(--space-5) 0;
  background-color: var(--code-bg);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.code-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-4);
  border-bottom: 1px solid var(--rule);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--ink-muted);
}

.copy-code-btn {
  appearance: none;
  border: 1px solid var(--rule-strong);
  background-color: var(--surface-elevated);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 500;
  min-height: 32px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: border-color 140ms ease, color 140ms ease;
}

.copy-code-btn:hover {
  border-color: var(--cobalt);
  color: var(--cobalt);
}

.code-figure pre {
  margin: 0;
  padding: var(--space-4);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.84rem;
  line-height: 1.62;
  color: var(--ink);
}

.code-figure pre code {
  background: transparent;
  border: 0;
  padding: 0;
  font-size: inherit;
}

.tok-kw { color: var(--code-kw); }
.tok-fn { color: var(--code-fn); }
.tok-num { color: var(--code-num); }
.tok-cm { color: var(--code-cm); font-style: italic; }

/* Related essays & dispatch newsletter */
.aftermatter-section {
  margin-top: var(--space-8);
  padding-top: var(--space-7);
  border-top: 1px solid var(--rule);
  scroll-margin-top: calc(var(--bar-height) + 1rem);
}

.aftermatter-inner {
  max-width: var(--measure);
}

.section-heading {
  margin: 0 0 var(--space-5);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.5rem;
  line-height: 1.25;
  color: var(--ink);
}

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

.related-item {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto;
  gap: var(--space-4);
  align-items: baseline;
  padding: var(--space-4) 0;
  border-bottom: 1px solid var(--rule);
}

.related-item:first-child {
  border-top: 1px solid var(--rule);
}

.related-issue,
.related-time {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  color: var(--ochre-text);
}

.related-time {
  color: var(--ink-muted);
}

.related-link {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--ink);
  text-decoration: none;
  transition: color 140ms ease;
}

.related-link:hover {
  color: var(--cobalt);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.related-body p {
  margin: var(--space-1) 0 0;
  font-family: var(--font-sans);
  font-size: 0.875rem;
  line-height: 1.55;
  color: var(--ink-muted);
}

.preview-toast {
  margin: var(--space-4) 0 0;
  padding: var(--space-3) var(--space-4);
  background-color: var(--ochre-soft);
  color: var(--ink);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-md);
  font-family: var(--font-sans);
  font-size: 0.85rem;
}

/* Newsletter dispatch */
.dispatch-box {
  background-color: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-lg);
  padding: clamp(1.5rem, 3.5vw, 2.25rem);
}

.dispatch-copy p {
  margin: 0 0 var(--space-5);
  font-family: var(--font-sans);
  font-size: 0.94rem;
  line-height: 1.6;
  color: var(--ink-secondary);
}

.form-row {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.form-row input[type="email"] {
  flex: 1 1 220px;
  min-height: 44px;
  padding: 0 var(--space-4);
  font-family: var(--font-sans);
  font-size: 0.94rem;
  background-color: var(--surface-elevated);
  color: var(--ink);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-md);
}

.form-row input[type="email"]::placeholder {
  color: var(--ink-muted);
}

.form-row .btn-primary {
  min-height: 44px;
  padding: 0 var(--space-5);
}

.form-feedback {
  margin: var(--space-3) 0 0;
  min-height: 1.2em;
  font-family: var(--font-sans);
  font-size: 0.85rem;
  color: var(--ink-secondary);
}

.form-feedback.is-error {
  color: var(--ochre-text);
}

.form-feedback.is-success {
  color: var(--cobalt);
}

/* Footer */
.site-footer {
  border-top: 1px solid var(--rule);
  margin-top: var(--space-7);
}

.footer-inner {
  max-width: 1040px;
  margin: 0 auto;
  padding: var(--space-6) clamp(1rem, 3vw, 2rem) var(--space-7);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-5);
  flex-wrap: wrap;
  font-family: var(--font-sans);
  font-size: 0.84rem;
  color: var(--ink-muted);
}

.footer-brand {
  margin: 0 0 var(--space-1);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1rem;
  color: var(--ink);
}

.footer-colophon p:last-child {
  margin: 0;
}

.footer-links {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-2);
}

.back-to-top {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--cobalt);
  text-decoration: none;
  font-weight: 500;
}

.back-to-top:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* Narrow viewports: single-column essay + Popover API sidenotes */
@media (max-width: 1079px) {
  .prose-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .sidenote[popover] {
    display: none;
  }

  .sidenote[popover]:popover-open,
  .sidenote.is-popover-fallback-open {
    display: block;
    position: fixed;
    inset: auto var(--space-4) var(--space-5) var(--space-4);
    margin: 0 auto;
    max-width: 460px;
    padding: var(--space-4) var(--space-5);
    background-color: var(--surface-elevated);
    color: var(--ink);
    border: 1px solid var(--ochre);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-popover);
    z-index: 300;
  }

  .sidenote[popover]::backdrop {
    background-color: rgba(24, 23, 21, 0.35);
    backdrop-filter: blur(2px);
  }

  .sidenote-close {
    display: inline-flex;
  }
}

@media (max-width: 860px) {
  .lab-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .bar-nav {
    display: none;
  }
}

@media (max-width: 600px) {
  .reading-bar-inner {
    padding: var(--space-2) var(--space-3);
    gap: var(--space-2);
  }

  .brand-link {
    font-size: 0.95rem;
    gap: 6px;
  }

  .bar-controls {
    flex-wrap: nowrap;
    gap: 6px;
  }

  .scale-readout,
  .theme-label {
    display: none;
  }

  .ctrl-btn {
    min-height: 36px;
    min-width: 34px;
    padding: 0 7px;
  }

  .progress-badge {
    display: none;
  }

  .essay-meta {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-3) var(--space-4);
  }

  .related-item {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-1);
  }

  .footer-links {
    align-items: flex-start;
  }

  .puck-track-row {
    grid-template-columns: 72px minmax(0, 1fr);
  }
}

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

  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
