/* =====================================================================
   Methods Lab — shared stylesheet
   Layout: the site's paper-and-crimson editorial shell, with a second,
   instrument-like layer for code, data and figures set in mono.
   Tokens: the site palette verbatim, plus two chart series validated for
   colour-vision deficiency and contrast in both themes.
   ===================================================================== */

:root {
  color-scheme: light dark;

  /* site palette (identical to index.html) */
  --paper: #F1F0ED;
  --card: #FDFDFB;
  --ink: #22211F;
  --muted: #6B6861;
  --crimson: #A51C30;
  --crimson-soft: #C4535F;
  --hairline: #DCD9D2;

  /* lab layer */
  --panel: #F7F6F3;
  --panel-edge: #E4E1DA;
  --code-ink: #2E2C28;
  --code-key: #8A2946;
  --code-str: #2C6B4F;
  --code-num: #1766AE;
  --code-com: #8E8A81;
  --code-fn: #6A4B9C;

  /* chart series: validated all-pairs, light surface
     #A51C30 / #1766AE — normal ΔE 27.8, CVD ΔE 20.8, contrast 7.3 / 5.8 */
  --s1: #A51C30;
  --s2: #1766AE;
  --s1-fill: rgba(165, 28, 48, 0.14);
  --s2-fill: rgba(23, 102, 174, 0.14);
  --grid: #DFDCD5;
  --ghost: #ACA69B;

  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --serif: "Newsreader", Georgia, "Times New Roman", serif;

  box-sizing: border-box;
  /* Matches what the artifact skeleton puts on :root, so the pages served
     without that skeleton behave the same under a phone's system bars. */
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --paper: #1B1B1A;
    --card: #242423;
    --ink: #E9E6E0;
    --muted: #9B968D;
    --crimson: #E0707F;
    --crimson-soft: #C4535F;
    --hairline: #3A3833;
    --panel: #201F1E;
    --panel-edge: #343230;
    --code-ink: #DCD8D1;
    --code-key: #E58C9E;
    --code-str: #7FC2A0;
    --code-num: #7FB6E8;
    --code-com: #7E7A72;
    --code-fn: #B59BE0;
    /* validated dark surface: #DB6476 / #4A95DD — normal ΔE 24.7, CVD ΔE 15.8 */
    --s1: #DB6476;
    --s2: #4A95DD;
    --s1-fill: rgba(219, 100, 118, 0.18);
    --s2-fill: rgba(74, 149, 221, 0.18);
    --grid: #38362F;
    --ghost: #6E6A62;
  }
}

:root[data-theme="light"] { color-scheme: light; }

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #1B1B1A;
  --card: #242423;
  --ink: #E9E6E0;
  --muted: #9B968D;
  --crimson: #E0707F;
  --crimson-soft: #C4535F;
  --hairline: #3A3833;
  --panel: #201F1E;
  --panel-edge: #343230;
  --code-ink: #DCD8D1;
  --code-key: #E58C9E;
  --code-str: #7FC2A0;
  --code-num: #7FB6E8;
  --code-com: #7E7A72;
  --code-fn: #B59BE0;
  --s1: #DB6476;
  --s2: #4A95DD;
  --s1-fill: rgba(219, 100, 118, 0.18);
  --s2-fill: rgba(74, 149, 221, 0.18);
  --grid: #38362F;
  --ghost: #6E6A62;
}

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

html {
  font-size: clamp(1rem, 0.1075rem + 0.952vw, 1.38rem);
  scroll-behavior: smooth;
  scroll-padding-top: calc(4.6rem + env(safe-area-inset-top, 0px));
  -webkit-text-size-adjust: 100%;
}
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-optical-sizing: auto;
  font-size: 1.1rem;
  line-height: 1.66;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--crimson); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }
a:focus-visible, button:focus-visible, summary:focus-visible {
  outline: 2px solid var(--crimson);
  outline-offset: 3px;
  border-radius: 2px;
}

.wrap {
  max-width: 72rem;
  margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.narrow { max-width: 42rem; }

/* ---- header ---- */
body::before {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  height: env(safe-area-inset-top, 0px);
  background: var(--paper);
  z-index: 11;
}
header.lab {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  background: color-mix(in srgb, var(--paper) 93%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--hairline);
  z-index: 10;
}
header.lab .bar {
  display: flex;
  align-items: baseline;
  gap: 1rem;
  flex-wrap: wrap;
  padding-block: 0.85rem;
}
.wordmark { font-weight: 600; font-size: 1rem; color: var(--ink); white-space: nowrap; }
.wordmark .sep { color: var(--muted); font-weight: 400; margin: 0 0.45rem; }
.wordmark .here { color: var(--crimson); }
header.lab nav { display: flex; gap: 1.2rem; margin-left: auto; flex-wrap: wrap; }
header.lab nav a {
  color: var(--muted);
  font-size: 0.95rem;
  border-bottom: 2px solid transparent;
  padding-block: 0.1rem;
}
header.lab nav a:hover { color: var(--crimson); text-decoration: none; }
header.lab nav a[aria-current="page"] { color: var(--crimson); border-bottom-color: var(--crimson); }
/* The way back to the main site, set apart from the lab's own pages. */
header.lab nav a.home-link { padding-right: 1.2rem; border-right: 1px solid var(--hairline); border-bottom: none; }
header.lab nav .lbl-short { display: none; }
.theme-toggle {
  flex: none; align-self: center;
  width: 2rem; height: 2rem; padding: 0;
  border: 1px solid var(--hairline); border-radius: 999px;
  background: none; color: var(--muted); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  appearance: none; -webkit-appearance: none;
}
.theme-toggle:hover { color: var(--crimson); border-color: var(--crimson); }
.theme-toggle svg { width: 1rem; height: 1rem; display: block; }
:root[data-theme="dark"] .icon-sun, :root[data-theme="light"] .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon-sun { display: none; }
  :root:not([data-theme="light"]):not([data-theme="dark"]) .icon-moon { display: block; }
}
:root:not([data-theme]) .icon-moon { display: none; }
@media (prefers-color-scheme: dark) { :root:not([data-theme]) .icon-moon { display: block; } }
:root[data-theme="dark"] .icon-moon { display: block; }

/* ---- typography ---- */
h1 {
  font-size: clamp(2.1rem, 4.6vw, 3.3rem);
  font-weight: 500;
  line-height: 1.07;
  letter-spacing: -0.015em;
  margin: 0 0 1rem;
  text-wrap: balance;
}
h2 {
  font-size: 1.6rem;
  font-weight: 500;
  letter-spacing: -0.01em;
  line-height: 1.2;
  margin: 0 0 1rem;
  text-wrap: balance;
}
h2 .num {
  font-family: var(--mono);
  font-size: 0.72em;
  color: var(--crimson);
  margin-right: 0.6rem;
  font-weight: 500;
}
h3 {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--muted);
  margin: 0 0 1rem;
  font-family: var(--mono);
}
p { margin: 0 0 1.1rem; max-width: 42rem; }
.lead { font-size: 1.22rem; color: var(--ink); max-width: 40rem; }
.sub { color: var(--muted); }
section.band { border-top: 1px solid var(--hairline); }
section.band > .wrap { padding-block: clamp(3rem, 7vh, 5rem); }
.eyebrow {
  font-family: var(--mono);
  font-size: 0.74rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--crimson);
  margin: 0 0 0.9rem;
}

/* ---- goal box: what the method is for, before anything else ---- */
dl.goal {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0;
  margin: 1.8rem 0 0;
  max-width: 46rem;
  border-top: 1px solid var(--hairline);
}
dl.goal dt {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--crimson);
  padding-top: 0.95rem;
}
dl.goal dd {
  margin: 0.2rem 0 0;
  padding-bottom: 0.95rem;
  border-bottom: 1px solid var(--hairline);
  font-size: 1.05rem;
  line-height: 1.6;
}
@media (min-width: 46rem) {
  dl.goal { grid-template-columns: 9rem minmax(0, 1fr); column-gap: 1.6rem; }
  dl.goal dt { padding-bottom: 0.95rem; border-bottom: 1px solid var(--hairline); }
  dl.goal dd { padding-top: 0.95rem; margin-top: 0; }
}

/* ---- key-number tiles ---- */
.tiles {
  /* Fixed counts, not auto-fit: four tiles in a three-column track leave one
     stranded in a row of its own. */
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px;
  background: var(--hairline);
  border: 1px solid var(--hairline);
  margin: 2rem 0;
}
.tile { background: var(--card); padding: 1.1rem 1.15rem; min-width: 0; }
.tile .k {
  font-family: var(--mono);
  font-size: 1.55rem;
  font-weight: 500;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  display: block;
}
.tile .k.alt { color: var(--s2); }
.tile .k.hot { color: var(--crimson); }
.tile .l {
  display: block;
  font-size: 0.86rem;
  color: var(--muted);
  margin-top: 0.3rem;
  line-height: 1.4;
}

/* ---- method cards ---- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(19rem, 1fr)); gap: 1.4rem; }
.card {
  background: var(--card);
  border: 1px solid var(--hairline);
  padding: 1.5rem 1.5rem 1.3rem;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.card .tag {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 0.7rem;
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.card .tag b { color: var(--crimson); font-weight: 500; }
.card h2 { font-size: 1.3rem; margin-bottom: 0.7rem; }
.card p { font-size: 1rem; margin-bottom: 1rem; }
.card .go { margin-top: auto; font-family: var(--mono); font-size: 0.85rem; }
.card .go::after { content: " →"; }

/* ---- figures ---- */
figure { margin: 2.2rem 0; }
figure.wide { max-width: 100%; }
.plot { width: 100%; overflow-x: auto; }
.plot svg { display: block; width: 100%; height: auto; max-width: 100%; }
figcaption {
  font-size: 0.92rem;
  color: var(--muted);
  margin-top: 0.75rem;
  max-width: 40rem;
  line-height: 1.55;
}
figcaption b { color: var(--ink); font-weight: 600; }
.legend {
  display: flex;
  gap: 1.1rem;
  flex-wrap: wrap;
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.6rem;
}
.legend span { display: inline-flex; align-items: center; gap: 0.4rem; }
.legend i {
  width: 0.85rem; height: 0.55rem; border-radius: 2px; display: inline-block;
}
.legend i.l1 { background: var(--s1); }
.legend i.l2 { background: var(--s2); }
.legend i.l3 { background: transparent; border: 1.5px dashed var(--ghost); height: 0; }

/* ---- code: folded by default, so the prose leads ---- */
details.codeblock {
  border: 1px solid var(--panel-edge);
  background: var(--panel);
  margin: 1.6rem 0;
  min-width: 0;
}
details.codeblock > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.7rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.8rem;
  font-family: var(--mono);
  font-size: 0.76rem;
  color: var(--muted);
}
details.codeblock > summary::-webkit-details-marker { display: none; }
details.codeblock > summary::marker { content: ""; }
details.codeblock > summary::before {
  content: "+";
  color: var(--crimson);
  font-weight: 500;
  width: 0.7rem;
  display: inline-block;
}
details.codeblock[open] > summary::before { content: "\2212"; }
details.codeblock[open] > summary { border-bottom: 1px solid var(--panel-edge); }
details.codeblock > summary:hover { color: var(--ink); }
.codeblock .lang {
  border: 1px solid var(--panel-edge);
  border-radius: 2px;
  padding: 0.05rem 0.35rem;
  color: var(--crimson);
  background: var(--card);
}
.codeblock .file { color: var(--ink); }
.codeblock .copy {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 0.74rem;
  background: none;
  border: 1px solid var(--panel-edge);
  border-radius: 2px;
  color: var(--muted);
  padding: 0.12rem 0.45rem;
  cursor: pointer;
}
.codeblock .copy:hover { color: var(--crimson); border-color: var(--crimson); }
.codeblock pre {
  margin: 0;
  font-variant-ligatures: none;   /* R's <- must read as two characters */
  padding: 0.85rem 0.9rem;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.6;
  color: var(--code-ink);
  tab-size: 2;
}
.codeblock code { font-family: inherit; }
.hl-k { color: var(--code-key); }
.hl-s { color: var(--code-str); }
.hl-n { color: var(--code-num); }
.hl-c { color: var(--code-com); font-style: italic; }
.hl-f { color: var(--code-fn); }
code.inline {
  font-family: var(--mono);
  font-variant-ligatures: none;
  font-size: 0.86em;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  border-radius: 2px;
  padding: 0.05em 0.3em;
}

/* ---- tables ---- */
.tablewrap { overflow-x: auto; margin: 1.8rem 0; min-width: 0; }
table.data {
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
  width: 100%;
  min-width: 30rem;
}
table.data caption {
  text-align: left;
  font-family: var(--mono);
  font-size: 0.74rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  padding-bottom: 0.6rem;
}
table.data th, table.data td {
  text-align: right;
  padding: 0.42rem 0.7rem;
  border-bottom: 1px solid var(--hairline);
  white-space: nowrap;
}
table.data th { color: var(--muted); font-weight: 500; border-bottom-color: var(--ink); }
table.data th:first-child, table.data td:first-child { text-align: left; }
/* a prose comparison reads left-aligned, not right */
table.data.cmp th, table.data.cmp td { text-align: left; white-space: normal; }
table.data.cmp { min-width: 36rem; }
table.data tbody tr:last-child td { border-bottom: none; }
table.data td.pos { color: var(--s1); }
table.data td.name { font-family: var(--serif); font-size: 0.95rem; white-space: normal; }
table.data .star { color: var(--crimson); }
table.data tr.rule td { border-top: 1px solid var(--ink); }

/* ---- range across simulated surveys, under a mean ---- */
table.data .rng { display: block; font-size: 0.7rem; color: var(--muted); }

/* ---- table notes, as under a table in a paper ---- */
p.tnote { font-size: 0.86rem; line-height: 1.5; max-width: 46rem; margin: -1rem 0 1.8rem; }
p.tnote i { font-style: italic; color: var(--ink); }

/* ---- references ---- */
ol.refs { list-style: none; margin: 1.4rem 0 0; padding: 0; max-width: 44rem; counter-reset: r; }
ol.refs li {
  counter-increment: r;
  display: grid;
  grid-template-columns: 1.9rem minmax(0, 1fr);
  gap: 0.7rem;
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--hairline);
  font-size: 0.98rem;
  line-height: 1.55;
}
ol.refs li::before {
  content: "[" counter(r) "]";
  font-family: var(--mono);
  font-size: 0.74rem;
  color: var(--crimson);
  padding-top: 0.3rem;
}
ol.refs li:last-child { border-bottom: none; }
ol.refs .what {
  display: block;
  color: var(--muted);
  font-size: 0.92rem;
  margin-top: 0.15rem;
}

/* ---- prose lists ---- */
.narrow ul { max-width: 42rem; margin: 0 0 1.1rem; padding-left: 1.1rem; }
.narrow ul li { margin-bottom: 0.5rem; padding-left: 0.25rem; }
.narrow ul li::marker { color: var(--crimson); }

/* ---- callouts ---- */
.note {
  border-left: 2px solid var(--crimson);
  padding: 0.1rem 0 0.1rem 1.1rem;
  margin: 1.8rem 0;
  max-width: 42rem;
}
.note p:last-child { margin-bottom: 0; }
.note .lab {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--crimson);
  display: block;
  margin-bottom: 0.3rem;
}

/* ---- step rail ---- */
ol.steps { list-style: none; margin: 2rem 0; padding: 0; counter-reset: s; max-width: 44rem; }
ol.steps li {
  counter-increment: s;
  display: grid;
  grid-template-columns: 2.2rem 1fr;
  gap: 1rem;
  padding-bottom: 1.5rem;
  position: relative;
}
ol.steps li::before {
  content: counter(s, decimal-leading-zero);
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--crimson);
  padding-top: 0.28rem;
}
ol.steps li:not(:last-child)::after {
  content: "";
  position: absolute;
  left: 0.72rem;
  top: 1.9rem;
  bottom: 0.3rem;
  width: 1px;
  background: var(--hairline);
}
ol.steps b { font-weight: 600; }
ol.steps p { margin: 0.2rem 0 0; font-size: 1rem; }

/* ---- run box ---- */
.runbox {
  background: var(--card);
  border: 1px solid var(--hairline);
  padding: 1.3rem 1.4rem;
  margin: 2rem 0;
}
.runbox h3 { margin-bottom: 0.8rem; }
.runbox pre {
  font-family: var(--mono);
  font-size: 0.8rem;
  background: var(--panel);
  border: 1px solid var(--panel-edge);
  padding: 0.7rem 0.8rem;
  overflow-x: auto;
  margin: 0 0 0.9rem;
  line-height: 1.6;
}
.runbox p:last-child { margin-bottom: 0; }

/* ---- contact ---- */
.contact {
  border-top: 1px solid var(--hairline);
  padding-top: 1.5rem;
  margin-top: 2.4rem;
}
.contact h3 { margin-bottom: 0.6rem; }
.contact p:last-child { margin-bottom: 0; }

/* ---- footer ---- */
footer.lab { border-top: 1px solid var(--hairline); }
footer.lab .bar {
  display: flex;
  gap: 1rem;
  justify-content: space-between;
  flex-wrap: wrap;
  padding-block: 1.6rem;
  font-size: 0.9rem;
  color: var(--muted);
}

/* ---- tooltip ---- */
.tip {
  position: fixed;
  pointer-events: none;
  background: var(--card);
  border: 1px solid var(--hairline);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
  font-family: var(--mono);
  font-size: 0.74rem;
  line-height: 1.45;
  color: var(--ink);
  padding: 0.35rem 0.5rem;
  border-radius: 2px;
  z-index: 50;
  white-space: pre;
}
.tip[hidden] { display: none !important; }

@media (min-width: 60rem) {
  .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (max-width: 700px) {
  /* Two rows: name and toggle, then the section nav under them. */
  header.lab .bar { row-gap: 0.45rem; }
  .wordmark { flex: 1 1 auto; font-size: 0.92rem; }
  .theme-toggle { order: 2; margin-left: auto; }
  header.lab nav { order: 3; flex-basis: 100%; margin-left: 0; gap: 1rem; }
  header.lab nav .lbl-long { display: none; }
  header.lab nav .lbl-short { display: inline; }
  header.lab nav a.home-link { padding-right: 1rem; }
  header.lab nav a { font-size: 0.88rem; }
  html { scroll-padding-top: calc(6.4rem + env(safe-area-inset-top, 0px)); }
  .codeblock .head .note-txt { display: none; }
}

@media (max-width: 640px) {
  .tile .k { font-size: 1.3rem; }
  .lead { font-size: 1.1rem; }
  /* Figures scroll inside their own container rather than shrinking their
     labels into illegibility. The page itself never scrolls sideways. */
  .plot svg { min-width: 32rem; }
  figure.wide .plot svg { min-width: 36rem; }
}
