/* Lab article bridge — carries the main site's design (sciel/styles.css) onto
 * the self-contained lab articles.
 *
 * Each lab page keeps its own inline <style> for layout (the .wrap grid, the
 * table of contents, cards, callouts, charts). This sheet is linked AFTER that
 * inline block and supplies what the main site owns:
 *
 *   - the four palettes ({terra, mono} x {dark, light}) with the same token
 *     values as sciel/styles.css, plus the older lab token names mapped onto
 *     them (--line, --paper*, --warm, --surface-2/3, --code*);
 *   - the type system from sciel/fonts.css (Instrument Sans / Source Serif 4 /
 *     JetBrains Mono), switched by data-font exactly as on the main site;
 *   - links, inline code, code blocks, tables and blockquotes in the main
 *     site's style;
 *   - the chrome: top bar, the floating control pill, progress bar,
 *     back-to-top and code-copy buttons (mounted by controls.js).
 *
 * Palette selectors carry two or three attributes so they beat the pages'
 * inline `:root {...}` and `:root[data-palette="..."]` blocks. Chrome selectors
 * are anchored to `body >` where an article reuses the same class name
 * (lesson8 has its own .controls, notes.html its own .progress).
 */

/* ============================ tokens ============================ */

:root {
  --font-display: "Anthropic Serif", "Tiempos Headline", "Source Serif 4", Georgia, serif;
  --font-sans: "Anthropic Sans", "Styrene B", "Instrument Sans", system-ui, sans-serif;
  --font-serif: "Anthropic Serif", "Tiempos Text", "Source Serif 4", Georgia, serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Consolas, monospace;
  --font-body: var(--font-sans);
}
:root[data-font="serif"] { --font-body: var(--font-serif); }
:root[data-font="sf"] { --font-display: var(--font-sans); }

/* terra dark (default) */
:root[data-palette="terra"][data-theme="dark"] {
  color-scheme: dark;
  --background: #141413;
  --surface: #1D1C1A;
  --surface-raised: #272624;
  --border: #33322E;
  --text: #ECEAE2;
  --ink-2: #CECCC2;
  --muted: #A6A49A;
  --accent: #C46A4A;
  --accent-2: #D6835F;
  --accent-soft: #30211A;
  --paper-3: #201F1D;
  --shadow: 0 1px 2px rgba(0, 0, 0, .45), 0 10px 30px rgba(0, 0, 0, .30);
}

/* terra light */
:root[data-palette="terra"][data-theme="light"] {
  color-scheme: light;
  --background: #FAF9F5;
  --surface: #F0EEE6;
  --surface-raised: #E8E6DC;
  --border: #DEDCD1;
  --text: #141413;
  --ink-2: #3D3D3A;
  --muted: #7A796F;
  --accent: #9C4A2C;
  --accent-2: #7C3A22;
  --accent-soft: #EFE0D5;
  --paper-3: #EDEBE3;
  --shadow: 0 1px 2px rgba(20, 20, 19, .05), 0 10px 26px rgba(20, 20, 19, .07);
}

/* mono dark */
:root[data-palette="mono"][data-theme="dark"] {
  color-scheme: dark;
  --background: #141413;
  --surface: #1B1B1A;
  --surface-raised: #242423;
  --border: #2E2E2C;
  --text: #E6E6E3;
  --ink-2: #C9C9C5;
  --muted: #9A9A95;
  --accent: #E6E6E3;
  --accent-2: #A8A8A3;
  --accent-soft: #262625;
  --paper-3: #1F1F1E;
  --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 10px 30px rgba(0, 0, 0, .34);
}

/* mono light */
:root[data-palette="mono"][data-theme="light"] {
  color-scheme: light;
  --background: #FAFAF9;
  --surface: #F2F2F0;
  --surface-raised: #E8E8E5;
  --border: #E0E0DC;
  --text: #141413;
  --ink-2: #3A3A38;
  --muted: #75756F;
  --accent: #141413;
  --accent-2: #55554F;
  --accent-soft: #EAEAE7;
  --paper-3: #EFEFEC;
  --shadow: 0 1px 2px rgba(20, 20, 19, .05), 0 10px 26px rgba(20, 20, 19, .07);
}

/* older lab token names, resolved against the palette above */
:root[data-palette][data-theme] {
  --ink: var(--text);
  --line: var(--border);
  --paper: var(--background);
  --paper-2: var(--surface);
  --surface-2: var(--surface-raised);
  --surface-3: var(--surface-raised);
  --warm: var(--accent);
  --code: var(--ink-2);
  --code-ink: var(--ink-2);
  --code-bg: var(--surface);
  --card-bg: var(--surface);
  --card-hover: var(--surface-raised);
  --strong-color: var(--text);
}

/* ============================ base ============================ */

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--background) !important;
  background-image: none !important;
  color: var(--text) !important;
  font-family: var(--font-body) !important;
  font-size: 17px;
  font-weight: 460;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
:root[data-font="serif"] body { font-size: 18px; font-weight: 450; }

::selection { background: color-mix(in srgb, var(--accent) 34%, transparent); color: var(--text); }

h1, h2, h3, h4, h5, h6 {
  color: var(--text);
  font-family: var(--font-display) !important;
  font-weight: 500;
  letter-spacing: -.015em;
}
:root[data-font="sf"] :is(h1, h2, h3, h4) { font-weight: 600; letter-spacing: -.02em; }

/* the old accent rule under the title belongs to the previous design */
h1::after { content: none !important; }

strong, b { color: var(--text); font-weight: 650; }

a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  text-decoration-color: color-mix(in srgb, var(--accent) 45%, transparent);
}
a:hover { color: var(--accent-2); text-decoration-color: currentColor; }
a:focus-visible { outline: 1px solid var(--accent); outline-offset: 3px; border-radius: 2px; }

li::marker { color: var(--muted); }

code, kbd, samp { font-family: var(--font-mono); }

body :not(pre) > code {
  padding: .1em .38em;
  color: var(--ink-2);
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 14%, transparent);
  border-radius: 5px;
  font-size: .86em;
  font-weight: 500;
}

pre {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

blockquote { border-left-color: var(--accent); }

/* tables: hairline rows, header in mono small caps on --paper-3 */
table { background: transparent; border: 0; border-radius: 0; }
th, td { border-bottom: 1px solid var(--border); }
thead th {
  color: var(--muted);
  background: var(--paper-3);
  font-family: var(--font-mono) !important;
  font-size: .68rem;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
}
tbody tr:last-child td { border-bottom: 0; }

figcaption { color: var(--muted); }

/* ============================ chrome: top bar ============================ */

.topbar { border-bottom: 1px solid var(--border); }

.topbar-inner {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 15px 28px;
}

.topbar .wordmark {
  font-family: var(--font-body);
  font-weight: 500;
  font-size: 1.16rem;
  letter-spacing: -.015em;
  color: var(--text);
  text-decoration: none;
}
.topbar .wordmark:hover { color: var(--accent); }

.topnav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  font-family: var(--font-body);
  font-size: .82rem;
  font-weight: 500;
}
.topnav a { color: var(--muted); text-decoration: none; transition: color .15s ease; }
.topnav a:hover, .topnav a[aria-current="page"] { color: var(--text); }

/* ============================ chrome: controls ============================ */

body > .controls {
  position: fixed;
  z-index: 80;
  right: calc(16px + env(safe-area-inset-right, 0px));
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}
body > .controls button {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  transition: color .15s ease, background-color .15s ease;
}
body > .controls button:hover { color: var(--text); background: var(--surface-raised); }
body > .controls button[aria-pressed="true"] { color: var(--accent); }
body > .controls button:focus-visible { outline: 1px solid var(--accent); outline-offset: 2px; }
body > .controls svg {
  display: block;
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.55;
  stroke-linecap: round;
  stroke-linejoin: round;
}
body > .controls svg .fill { fill: currentColor; stroke: none; }
body > .controls .btn-font svg { fill: currentColor; stroke: none; width: 19px; height: 19px; }

body > .progress {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 90;
  width: 0;
  height: 2px;
  background: var(--accent);
  transition: width .1s linear;
}

.to-top {
  position: fixed;
  z-index: 79;
  right: calc(16px + env(safe-area-inset-right, 0px));
  bottom: calc(70px + env(safe-area-inset-bottom, 0px));
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  color: var(--text);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  border: 1px solid var(--border);
  border-radius: 50%;
  box-shadow: var(--shadow);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transform: translateY(6px);
  transition: opacity .25s ease, transform .25s ease, visibility .25s;
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}
.to-top.is-shown { opacity: 1; visibility: visible; transform: none; }
.to-top:hover { border-color: var(--accent); }
.to-top svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

/* ---------------------------- code blocks + copy ---------------------------- */

.code-block { position: relative; margin: 1.5rem 0; }
.code-block pre {
  margin: 0;
  padding: 1.2rem 3.7rem 1.15rem 1.2rem;
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: .8rem;
  line-height: 1.62;
}
.code-block pre code {
  display: block;
  padding: 0;
  overflow: visible;
  color: var(--ink-2);
  background: transparent;
  border: 0;
  font-size: inherit;
  line-height: inherit;
  tab-size: 2;
  white-space: pre;
}

.code-copy {
  position: absolute;
  top: 10px;
  right: 10px;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  padding: 0;
  color: var(--muted);
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: color .15s ease, border-color .15s ease, background-color .15s ease;
}
.code-copy span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.code-copy svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.code-copy:hover { color: var(--text); border-color: var(--accent); }
.code-copy:focus-visible { outline: 1px solid var(--accent); outline-offset: 3px; }
.code-copy.is-copied { color: var(--accent); border-color: var(--accent); }

/* copy buttons some articles ship inside their own <pre> */
pre > .copy,
pre > .copy-btn {
  position: absolute;
  top: 8px;
  right: 9px;
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  padding: 0;
  color: var(--muted);
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 8px;
  appearance: none;
  font-size: 0;
  cursor: pointer;
}
pre > .copy::before,
pre > .copy-btn::before {
  content: "";
  width: 14px;
  height: 14px;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.6'%3E%3Crect x='8' y='8' width='11' height='11' rx='1.5'/%3E%3Cpath d='M5 16H4.5A1.5 1.5 0 0 1 3 14.5v-10A1.5 1.5 0 0 1 4.5 3h10A1.5 1.5 0 0 1 16 4.5V5'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.6'%3E%3Crect x='8' y='8' width='11' height='11' rx='1.5'/%3E%3Cpath d='M5 16H4.5A1.5 1.5 0 0 1 3 14.5v-10A1.5 1.5 0 0 1 4.5 3h10A1.5 1.5 0 0 1 16 4.5V5'/%3E%3C/svg%3E") center / contain no-repeat;
}
pre > .copy:hover,
pre > .copy-btn:hover { color: var(--text); border-color: var(--accent); }
pre > .copy:focus-visible,
pre > .copy-btn:focus-visible { outline: 1px solid var(--accent); outline-offset: 3px; }

/* ============================ layout ============================ */

/* Keep the reading rail on the left even when an imported article puts <main>
   before its table of contents in the DOM. */
.wrap:has(> .toc) {
  grid-template-areas: "toc main";
  grid-template-columns: minmax(0, 255px) minmax(0, 1fr) !important;
  align-items: start;
}
.wrap:has(> .toc) > .toc { grid-area: toc; min-width: 0; }
.wrap:has(> .toc) > main { grid-area: main; min-width: 0; }
.wrap:not(:has(> .toc)) {
  grid-template-areas: none;
  grid-template-columns: minmax(0, 1fr) !important;
}
.wrap:not(:has(> .toc)) > main { grid-area: auto; }

/* table-of-contents labels in the main site's mono small caps */
.toc-title {
  font-family: var(--font-mono) !important;
  font-size: .66rem !important;
  font-weight: 500;
  letter-spacing: .16em !important;
  color: var(--muted) !important;
}

/* Metadata labels stay useful as text, but lose the pill/chip treatment. */
.badge,
.badge.red,
.tag,
.tag.best,
.tag.exact,
.pill,
.st,
.st.good {
  display: inline;
  border: 0 !important;
  border-radius: 0 !important;
  background: transparent !important;
  padding: 0 !important;
  box-shadow: none !important;
}
.st { margin: 0 0 0 .55em !important; }

/* ---------------- article banner ---------------- */
.article-banner {
  position: relative;
  margin: 1.2rem 0 1.8rem;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.article-banner img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 2.4 / 1;
  object-fit: cover;
  border: 0;
  border-radius: 0;
}
:root[data-palette="terra"][data-theme="dark"] .article-banner img { filter: brightness(.9) saturate(.94); }
:root[data-palette="mono"] .article-banner img { filter: grayscale(1) contrast(1.04); }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .to-top, body > .progress { transition: none; }
}

@media (max-width: 930px) {
  .wrap:has(> .toc) {
    grid-template-areas: "toc" "main";
    grid-template-columns: minmax(0, 1fr) !important;
  }
}

@media (max-width: 700px) {
  .topbar-inner { padding: 15px 16px; }

  /* keep the TOC reachable on phones: a compact horizontally-scrollable row */
  .wrap:has(> .toc) > .toc {
    position: static;
    max-height: none;
    display: flex;
    align-items: center;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    padding: 0 0 10px;
    margin-bottom: 4px;
    white-space: nowrap;
  }
  .wrap:has(> .toc) > .toc > * { flex: 0 0 auto; white-space: nowrap; }
  .wrap:has(> .toc) > main { grid-area: main; min-width: 0; }

  body { font-size: 16px; }
  :root[data-font="serif"] body { font-size: 17px; }

  .article-banner { margin: 1rem 0 1.5rem; border-radius: 12px; }
  .article-banner img { aspect-ratio: 16 / 9; }

  table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  :not(pre) > code { overflow-wrap: anywhere; }
  img, svg, video, canvas { max-width: 100%; height: auto; }

  .code-block pre { padding: 2.5rem 1rem 1rem; }
  .code-copy { top: .45rem; right: .45rem; }
}
