/* ===== Distill-inspired editorial stylesheet ===== */

:root {
  --serif: 'Source Serif 4', Georgia, 'Times New Roman', serif;
  --sans: -apple-system, system-ui, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', 'SFMono-Regular', Menlo, monospace;

  --bg: #faf9f7;
  --text: #2d2d2d;
  --text-light: #6b7280;
  --accent: #5b6abf;
  --coral: #c0523a;
  --teal: #2b7d7a;
  --gold: #a08229;
  --border: #e5e2dc;
  --figure-bg: #ffffff;
}

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

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.22rem;
  line-height: 1.68;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ---- Progress bar ---- */
.progress-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 2px;
  z-index: 50;
  background: transparent;
}
.progress-bar__fill {
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--teal), var(--accent));
  transition: width 120ms linear;
}

/* ---- Article container ---- */
.article {
  max-width: 820px;
  margin: 0 auto;
  padding: 2rem 2rem 6rem;
}

/* ---- Header ---- */
.back-link {
  display: inline-block;
  font-family: var(--sans);
  font-size: 0.88rem;
  color: var(--text-light);
  text-decoration: none;
  margin-bottom: 2rem;
}
.back-link:hover { color: var(--text); }

.kicker {
  font-family: var(--sans);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--text-light);
  margin: 0 0 0.75rem;
}

.article-header h1 {
  font-family: var(--serif);
  font-size: clamp(2.2rem, 5vw, 3.2rem);
  font-weight: 700;
  line-height: 1.12;
  letter-spacing: -0.03em;
  margin: 0 0 1.2rem;
  max-width: 18ch;
}

.abstract {
  font-size: 1.15rem;
  color: #4b5563;
  line-height: 1.65;
  margin: 0 0 0.75rem;
  max-width: 56ch;
}

.byline {
  display: flex;
  gap: 1.5rem;
  font-family: var(--sans);
  font-size: 0.85rem;
  color: var(--text-light);
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

/* ---- Table of contents ---- */
.toc {
  margin: 3rem 0;
  padding: 1.5rem 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.toc ul {
  margin: 0;
  padding-left: 0;
  list-style: none;
  display: grid;
  gap: 0.4rem;
}

.toc__link {
  font-family: var(--sans);
  font-size: 0.95rem;
  color: var(--text-light);
  text-decoration: none;
  transition: color 150ms;
}
.toc__link:hover,
.toc__link.is-active {
  color: var(--text);
}

/* ---- Sections ---- */
section {
  margin-top: 4rem;
  scroll-margin-top: 1rem;
}

section h2 {
  font-family: var(--serif);
  font-size: clamp(1.5rem, 3.5vw, 2rem);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.02em;
  margin: 0 0 1.5rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.num {
  color: var(--text-light);
  font-weight: 400;
  margin-right: 0.25em;
}

section h3 {
  font-family: var(--serif);
  font-size: 1.15rem;
  font-weight: 600;
  margin: 2rem 0 0.5rem;
}

p {
  margin: 0 0 1rem;
  max-width: 65ch;
}

code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: rgba(0,0,0,0.04);
  padding: 0.1em 0.4em;
  border-radius: 4px;
}

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

/* ---- Example / step lists ---- */
.example-list {
  margin: 0.5rem 0 1rem;
  padding-left: 1.5rem;
  max-width: 65ch;
}
.example-list li {
  margin-bottom: 0.5rem;
}

/* ---- Math blocks ---- */
.math-block {
  margin: 1.5rem 0;
  padding: 1rem 0;
  overflow-x: auto;
}

.katex-display { margin: 0 !important; }
.katex { font-size: 1.05em; }

/* ---- Interactive figures ---- */
.interactive-figure {
  margin: 2rem 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--figure-bg);
  overflow: hidden;
}

.wide-figure {
  max-width: 1060px;
  width: calc(100% + 6rem);
  margin-left: -3rem;
  margin-right: -3rem;
}

/* ---- Aside / notes ---- */
.aside {
  margin: 1.25rem 0;
  padding: 0.85rem 1.1rem;
  border-left: 3px solid var(--border);
  background: #f5f4f1;
  border-radius: 0 6px 6px 0;
  font-size: 0.95rem;
  color: var(--text-light);
  max-width: 65ch;
}
.aside strong {
  color: var(--text);
}

.figure-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.5rem;
  align-items: center;
  padding: 1rem 1.25rem;
  background: #f8f7f4;
  border-bottom: 1px solid var(--border);
  font-family: var(--sans);
  font-size: 0.85rem;
  color: var(--text-light);
}

.figure-controls--grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem 1.5rem;
}

.figure-controls label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  white-space: nowrap;
}

.figure-controls .full-width {
  grid-column: 1 / -1;
}

.figure-controls input[type="range"] {
  flex: 1;
  min-width: 80px;
  accent-color: var(--coral);
}

.figure-controls span {
  min-width: 3.5ch;
  text-align: right;
  font-family: var(--mono);
  font-size: 0.82rem;
}

.figure-canvas {
  padding: 0.75rem;
}

.figure-canvas canvas {
  display: block;
  width: 100%;
  height: auto;
}

figcaption {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.5rem;
  padding: 0.75rem 1.25rem;
  background: #f8f7f4;
  border-top: 1px solid var(--border);
  font-family: var(--sans);
  font-size: 0.82rem;
  color: var(--text-light);
  line-height: 1.5;
}

.readout {
  white-space: nowrap;
}
.readout strong {
  color: var(--text);
  font-weight: 600;
}

.readout-text {
  flex-basis: 100%;
  font-style: italic;
}

/* ---- Joint grid (marginals / conditionals) ---- */
.joint-grid {
  display: grid;
  grid-template-columns: 1fr 260px;
  grid-template-rows: auto 1fr;
  gap: 0;
}

.joint-grid__top {
  grid-column: 1;
  grid-row: 1;
  padding: 0.75rem 0.75rem 0;
}

.joint-grid__note {
  grid-column: 2;
  grid-row: 1;
  padding: 0.75rem;
  font-family: var(--sans);
  font-size: 0.82rem;
  color: var(--text-light);
  border-left: 1px solid var(--border);
}

.joint-grid__main {
  grid-column: 1;
  grid-row: 2;
  padding: 0 0.75rem 0.75rem;
}

.joint-grid__right {
  grid-column: 2;
  grid-row: 2;
  padding: 0.75rem;
  border-left: 1px solid var(--border);
  display: flex;
  align-items: stretch;
}

.joint-grid__right canvas,
.joint-grid__main canvas,
.joint-grid__top canvas {
  display: block;
  width: 100%;
  height: auto;
}

.joint-grid__main canvas {
  aspect-ratio: 1 / 1;
}

.strip-head {
  margin-bottom: 0.35rem;
  font-family: var(--sans);
  font-size: 0.82rem;
}
.strip-head h3 {
  font-family: var(--sans);
  font-size: 0.85rem;
  font-weight: 600;
  margin: 0;
  color: var(--text);
}
.strip-head p {
  margin: 0;
  color: var(--text-light);
  font-size: 0.8rem;
}

.note-label {
  font-family: var(--sans);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-light);
  margin: 0 0 0.5rem;
}

.note-copy {
  font-size: 0.82rem;
  margin: 0.5rem 0 0;
}

.color-teal { color: var(--teal); }
.color-gold { color: var(--gold); }
.color-coral { color: var(--coral); }

/* ---- Try-this / preset links ---- */
.try-this {
  margin: 1.5rem 0;
  padding: 0.75rem 0;
  font-family: var(--sans);
  font-size: 0.9rem;
  color: var(--text-light);
}

.try-this__sep { display: inline-block; width: 1rem; }

.preset-link {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: rgba(91, 106, 191, 0.3);
  text-underline-offset: 2px;
  transition: text-decoration-color 150ms;
}
.preset-link:hover {
  text-decoration-color: var(--accent);
}

.draw-status {
  font-family: var(--mono);
  font-size: 0.78rem;
}

.dataset-label {
  font-weight: 600;
  color: var(--text);
}

/* ---- Data table ---- */
.data-table-wrap {
  margin: 1rem 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: auto;
  max-height: 14rem;
  background: var(--figure-bg);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: 0.85rem;
}

.data-table th,
.data-table td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: right;
  white-space: nowrap;
}

.data-table th:first-child,
.data-table td:first-child {
  text-align: left;
}

.data-table thead th {
  position: sticky;
  top: 0;
  background: #f8f7f4;
  color: var(--text-light);
  font-weight: 600;
}

.data-table tbody tr:last-child td {
  border-bottom: 0;
}

.small-text {
  font-size: 0.88rem;
  color: var(--text-light);
}

/* ---- Takeaways ---- */
.takeaway-list {
  margin: 0;
  padding: 0;
}

.takeaway {
  padding: 1rem 0;
  border-bottom: 1px solid var(--border);
}

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

.takeaway dt {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 1.1rem;
  margin: 0 0 0.25rem;
}

.takeaway dd {
  margin: 0;
  color: var(--text-light);
  max-width: 55ch;
}

/* ---- Footer ---- */
.article-footer {
  margin-top: 4rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-family: var(--sans);
  font-size: 0.85rem;
  color: var(--text-light);
}

/* ---- Canvas defaults ---- */
canvas {
  display: block;
  max-width: 100%;
}

#learningCanvas {
  cursor: crosshair;
}

#conditionalMainCanvas {
  touch-action: none;
}

/* ---- Responsive ---- */
@media (max-width: 860px) {
  .wide-figure {
    width: 100%;
    margin-left: 0;
    margin-right: 0;
  }
}

@media (max-width: 720px) {
  body { font-size: 1.1rem; }

  .article { padding: 1.5rem 1.25rem 4rem; }

  .wide-figure {
    width: 100%;
    margin-left: 0;
    margin-right: 0;
  }

  .article-header h1 { font-size: 1.8rem; }

  .figure-controls--grid {
    grid-template-columns: 1fr;
  }

  .joint-grid {
    grid-template-columns: 1fr;
  }

  .joint-grid__note {
    grid-column: 1;
    grid-row: 2;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  .joint-grid__right {
    grid-column: 1;
    grid-row: auto;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  figcaption {
    flex-direction: column;
    gap: 0.3rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
