/* Distill.pub-style typography and layout.
   Self-contained: no web fonts, no external requests. The body stack falls back
   through Charter -> Georgia -> a generic serif so the page looks right offline. */

/* ---------------------------------------------------------------- tokens -- */

:root {
  --l-body: 680px;
  --l-middle: 900px;
  --l-gutter: 260px;

  --ink: #1a1a1a;
  --ink-soft: #333;
  --ink-faint: #6b6b6b;
  --rule: #e5e5e5;
  --rule-strong: #cfcfcf;
  --bg: #ffffff;
  --bg-soft: #f7f7f5;
  --bg-code: #f4f4f2;
  --accent: #1a6fb4;
  --accent-soft: #eaf2f9;
  --cite: #1a6fb4;
  --mark: #fff6cc;

  --serif: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia,
    "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;
}

/* Dark palette applies when the OS asks for it and the user has not overridden,
   and also when the user explicitly picks dark. */
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --ink: #e8e6e3;
    --ink-soft: #cfccc8;
    --ink-faint: #9a9691;
    --rule: #33312e;
    --rule-strong: #45423e;
    --bg: #171614;
    --bg-soft: #201e1c;
    --bg-code: #232120;
    --accent: #6fb3e8;
    --accent-soft: #1d2a35;
    --cite: #6fb3e8;
    --mark: #4a4020;
  }
}

:root[data-theme="dark"] {
  --ink: #e8e6e3;
  --ink-soft: #cfccc8;
  --ink-faint: #9a9691;
  --rule: #33312e;
  --rule-strong: #45423e;
  --bg: #171614;
  --bg-soft: #201e1c;
  --bg-code: #232120;
  --accent: #6fb3e8;
  --accent-soft: #1d2a35;
  --cite: #6fb3e8;
  --mark: #4a4020;
}

/* ------------------------------------------------------------- structure -- */

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: 1.5rem;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.68;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

.d-skip {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 0.6rem 1rem;
  background: var(--accent);
  color: #fff;
  font-family: var(--sans);
  font-size: 0.85rem;
  border-radius: 0 0 6px 0;
}
.d-skip:focus {
  left: 0;
}

.d-progress {
  position: fixed;
  inset: 0 0 auto 0;
  height: 3px;
  background: transparent;
  z-index: 50;
}
.d-progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 80ms linear;
}

.d-layout {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 2.5rem;
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 2rem 6rem;
}

.d-main {
  width: 100%;
  max-width: var(--l-body);
  min-width: 0;
  flex: 0 1 auto;
}

/* ------------------------------------------------------------------- toc -- */

.d-toc {
  position: sticky;
  top: 2rem;
  flex: 0 0 var(--l-gutter);
  width: var(--l-gutter);
  max-height: calc(100vh - 4rem);
  overflow-y: auto;
  padding-top: 4.5rem;
  font-family: var(--sans);
  font-size: 0.78rem;
  line-height: 1.35;
  border-left: 1px solid transparent;
}

.d-toc-heading {
  margin: 0 0 0.6rem;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.d-toc-list {
  margin: 0;
  padding: 0;
  list-style: none;
  border-left: 1px solid var(--rule);
}

.d-toc-item a {
  display: block;
  padding: 0.2rem 0 0.2rem 0.75rem;
  margin-left: -1px;
  border-left: 2px solid transparent;
  color: var(--ink-faint);
  text-decoration: none;
}
.d-toc-l3 a {
  padding-left: 1.5rem;
}
.d-toc-item a:hover {
  color: var(--ink);
}
.d-toc-item a[aria-current="true"] {
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}
.d-toc-text .d-math {
  font-size: 0.95em;
}

/* ----------------------------------------------------------------- title -- */

.d-title {
  padding: 4rem 0 1.5rem;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 2rem;
}

.d-title h1 {
  margin: 0;
  font-size: 2.35rem;
  line-height: 1.18;
  font-weight: 600;
  letter-spacing: -0.012em;
}

/* The subtitle is an <h3> in the source and stays an <h3> on the page, so the
   heading census of the HTML matches the paper exactly. Styled as a subtitle. */
.d-title h3.d-subtitle {
  margin: 0.9rem 0 0;
  font-size: 1.16rem;
  line-height: 1.5;
  font-weight: 400;
  letter-spacing: 0;
  color: var(--ink-soft);
}

.d-byline {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1.4rem;
  font-family: var(--sans);
  font-size: 0.78rem;
  color: var(--ink-faint);
}
.d-byline-sep {
  opacity: 0.6;
}

.d-theme-toggle {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.9rem;
  height: 1.9rem;
  padding: 0;
  border: 1px solid var(--rule-strong);
  border-radius: 999px;
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.d-theme-toggle:hover {
  border-color: var(--accent);
}
.d-theme-icon {
  width: 0.85rem;
  height: 0.85rem;
  border-radius: 999px;
  background: currentColor;
  box-shadow: inset -0.3rem -0.1rem 0 0 var(--bg);
}
:root[data-theme="dark"] .d-theme-icon {
  box-shadow: none;
}

/* --------------------------------------------------------------- article -- */

.d-article {
  counter-reset: equation;
}

.d-lede {
  margin: 0 0 2.5rem;
  padding: 1.2rem 1.4rem;
  background: var(--bg-soft);
  border-left: 3px solid var(--accent);
  border-radius: 0 4px 4px 0;
  font-size: 0.96rem;
  line-height: 1.6;
  color: var(--ink-soft);
}
.d-lede > :first-child {
  margin-top: 0;
}
.d-lede > :last-child {
  margin-bottom: 0;
}
.d-lede blockquote {
  margin: 0 0 1rem;
  padding: 0;
  border: 0;
}

.d-article p {
  margin: 0 0 1.15rem;
}

.d-article h2,
.d-article h3,
.d-article h4,
.d-article h5,
.d-article h6 {
  position: relative;
  font-weight: 600;
  line-height: 1.28;
  letter-spacing: -0.008em;
  scroll-margin-top: 1.5rem;
}
.d-article h2 {
  margin: 3rem 0 1rem;
  font-size: 1.55rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--rule);
}
.d-article h3 {
  margin: 2.2rem 0 0.8rem;
  font-size: 1.22rem;
}
.d-article h4 {
  margin: 1.8rem 0 0.6rem;
  font-size: 1.05rem;
}
.d-article h5,
.d-article h6 {
  margin: 1.5rem 0 0.5rem;
  font-size: 1rem;
}

.d-anchor {
  margin-left: 0.4rem;
  color: var(--ink-faint);
  text-decoration: none;
  opacity: 0;
  transition: opacity 120ms ease;
}
.d-anchor::after {
  content: "§";
  font-size: 0.8em;
}
.d-article h1:hover .d-anchor,
.d-article h2:hover .d-anchor,
.d-article h3:hover .d-anchor,
.d-article h4:hover .d-anchor,
.d-article h5:hover .d-anchor,
.d-article h6:hover .d-anchor,
.d-anchor:focus {
  opacity: 1;
}

.d-article a {
  color: var(--accent);
  text-decoration: none;
}
.d-article a:hover {
  text-decoration: underline;
}
.d-article strong {
  font-weight: 600;
}
.d-article em {
  font-style: italic;
}

.d-article ul,
.d-article ol {
  margin: 0 0 1.15rem;
  padding-left: 1.4rem;
}
.d-article li {
  margin-bottom: 0.4rem;
}
.d-article li > p:last-child {
  margin-bottom: 0;
}

.d-article blockquote {
  margin: 1.5rem 0;
  padding: 0.1rem 0 0.1rem 1.1rem;
  border-left: 3px solid var(--rule-strong);
  color: var(--ink-soft);
}
.d-article hr {
  margin: 3rem 0;
  border: 0;
  border-top: 1px solid var(--rule);
}

/* -------------------------------------------------------------- equation -- */

.d-equation {
  margin: 1.6rem 0;
  overflow-x: auto;
  overflow-y: hidden;
  text-align: center;
}
mjx-container[display="true"] {
  margin: 0.35rem 0 !important;
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}
.d-math {
  font-size: 1.02em;
}

/* ---------------------------------------------------------------- tables -- */

.d-article table {
  width: 100%;
  margin: 1.6rem 0;
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: 0.82rem;
  line-height: 1.42;
  display: block;
  overflow-x: auto;
  border-top: 1px solid var(--rule-strong);
  border-bottom: 1px solid var(--rule-strong);
}
.d-article thead th {
  text-align: left;
  font-weight: 600;
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid var(--rule-strong);
  white-space: nowrap;
}
.d-article tbody td {
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid var(--rule);
  vertical-align: top;
}
.d-article tbody tr:last-child td {
  border-bottom: 0;
}
.d-article table .d-math {
  font-size: 1em;
}

/* ------------------------------------------------------------------ code -- */

.d-article pre {
  margin: 1.6rem 0;
  padding: 1rem 1.1rem;
  overflow-x: auto;
  background: var(--bg-code);
  border: 1px solid var(--rule);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.45;
}
.d-article code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: var(--bg-code);
  padding: 0.1em 0.32em;
  border-radius: 3px;
}
.d-article pre code {
  background: none;
  padding: 0;
  font-size: inherit;
}

/* ------------------------------------------------------------- citations -- */

.d-cite {
  color: var(--cite);
  text-decoration: none;
  white-space: nowrap;
}
.d-cite:hover {
  text-decoration: underline;
}
.d-cite-anchor {
  display: inline-block;
  width: 0;
  height: 0;
  overflow: hidden;
}

.d-cite-tip {
  position: absolute;
  z-index: 60;
  max-width: 26rem;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--rule-strong);
  border-radius: 5px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  font-family: var(--sans);
  font-size: 0.76rem;
  line-height: 1.4;
}
.d-cite-tip[hidden] {
  display: none;
}
.d-cite-tip ol {
  margin: 0;
  padding-left: 1.1rem;
}
.d-cite-tip li {
  margin: 0.15rem 0;
}
.d-cite-tip .d-tip-n {
  color: var(--ink-faint);
}

.d-references {
  padding-left: 1.6rem;
  font-size: 0.86rem;
  line-height: 1.5;
  counter-reset: none;
}
.d-ref {
  scroll-margin-top: 1.5rem;
}
.d-ref:target {
  background: var(--mark);
}
.d-ref-back {
  display: inline-block;
  margin-left: 0.35rem;
  color: var(--ink-faint);
  text-decoration: none;
  font-size: 0.9em;
}
.d-ref-back::after {
  content: "↩";
}
.d-ref-back:hover {
  color: var(--accent);
}

.d-appendix {
  margin-top: 3rem;
  padding-top: 1rem;
  border-top: 1px solid var(--rule);
}

/* ------------------------------------------------------------ responsive -- */

@media (max-width: 1100px) {
  .d-layout {
    gap: 1.5rem;
    padding: 0 1.25rem 5rem;
  }
  .d-toc {
    flex-basis: 210px;
    width: 210px;
    font-size: 0.74rem;
  }
}

@media (max-width: 900px) {
  .d-layout {
    display: block;
    padding: 0 1.1rem 4rem;
  }
  .d-toc {
    position: static;
    width: auto;
    max-height: none;
    padding-top: 1.5rem;
    margin-bottom: 1rem;
    border: 1px solid var(--rule);
    border-radius: 6px;
    padding: 0.9rem 1rem;
  }
  .d-toc-list {
    border-left: 0;
  }
  .d-toc-item a {
    padding-left: 0.2rem;
    border-left: 0;
  }
  .d-toc-l3 a {
    padding-left: 1rem;
  }
  .d-main {
    max-width: none;
  }
  .d-title {
    padding-top: 2rem;
  }
  .d-title h1 {
    font-size: 1.75rem;
  }
  .d-subtitle {
    font-size: 1.02rem;
  }
  .d-article table {
    font-size: 0.76rem;
  }
}

/* ----------------------------------------------------------------- print -- */

@media print {
  :root {
    --ink: #000;
    --ink-soft: #222;
    --ink-faint: #444;
    --bg: #fff;
    --bg-soft: #fff;
    --bg-code: #fff;
    --rule: #bbb;
    --rule-strong: #999;
    --accent: #000;
    --cite: #000;
  }
  .d-toc,
  .d-progress,
  .d-skip,
  .d-theme-toggle,
  .d-anchor,
  .d-cite-tip,
  .d-ref-back {
    display: none !important;
  }
  .d-layout {
    display: block;
    padding: 0;
    max-width: none;
  }
  .d-main {
    max-width: none;
  }
  .d-article {
    font-size: 11pt;
  }
  .d-article a {
    color: #000;
  }
  .d-article h2,
  .d-article h3 {
    break-after: avoid;
  }
  .d-equation,
  .d-article table,
  .d-article pre,
  .d-article blockquote {
    break-inside: avoid;
  }
  /* Equations must survive printing intact. */
  mjx-container {
    overflow: visible !important;
  }
  .d-lede {
    border-left: 2px solid #999;
  }
}

/* ------------------------------------------------------------ a11y focus -- */

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
  .d-progress-bar {
    transition: none;
  }
}

/* --- lecture audio bar ---------------------------------------------------- */

.d-audio {
  display: flex;
  align-items: center;
  gap: 0.75rem 1rem;
  flex-wrap: wrap;
  margin: 0.5rem 0 2rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--rule);
  border-radius: 10px;
  background: var(--bg-soft);
}

.d-audio-label {
  font-family: var(--sans);
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--ink);
}

.d-audio-meta {
  font-family: var(--sans);
  font-size: 0.8rem;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}

.d-audio-player {
  flex: 1 1 22rem;
  min-width: 15rem;
  height: 2.25rem;
}

.d-audio-download {
  font-family: var(--sans);
  font-size: 0.8rem;
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid transparent;
}
.d-audio-download:hover { border-bottom-color: currentColor; }

@media print {
  .d-audio { display: none; }
}
