/*
 * learning-loop reader.
 *
 * Designed for one device first: an iPad Pro held in portrait, at night, by
 * someone reading for twenty minutes. Every choice below follows from that.
 *
 * No web fonts. `ui-serif` resolves to New York on iPadOS and `ui-monospace`
 * to SF Mono, both already on the device. That means no font download, no
 * flash of unstyled text, and a reader that renders instantly with the network
 * off, which is the whole point of the offline cache.
 */

:root {
  color-scheme: light dark;

  /* Warm paper rather than white: less glare under a lamp, and it makes the
     diagrams' fills read as part of the page instead of floating on it. */
  --paper: #faf8f5;
  --paper-raised: #fffdfa;
  --ink: #1b1917;
  --ink-soft: #4a453d;
  --ink-mute: #736c60;
  --rule: #e4ded2;
  --rule-soft: #efeae0;
  --accent: #2f5d8a;
  --accent-soft: #e8eef5;
  --warn: #8a4b2f;

  /* Diagram palette, consumed by the CSS variables that the build injects
     into every inline SVG. --dg-ink is left to inherit currentColor. */
  --dg-fill: #f1ece1;
  --dg-accent: #e3ebf3;
  --dg-note: #f7f2dc;

  --measure: 34rem;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(28, 25, 20, .05), 0 8px 24px -12px rgba(28, 25, 20, .12);

  --font-body: ui-serif, "New York", Charter, "Iowan Old Style", Georgia, serif;
  --font-ui: ui-sans-serif, -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

/* Dark first via the media query, so the default "system" setting works, then
   again under [data-theme] so the manual toggle wins in both directions. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #15171a;
    --paper-raised: #1c1f23;
    --ink: #e7e3db;
    --ink-soft: #bdb8ae;
    --ink-mute: #8b8579;
    --rule: #2b2f34;
    --rule-soft: #23262a;
    --accent: #8fb6dd;
    --accent-soft: #1e2a36;
    --warn: #d79a76;
    --dg-fill: #232830;
    --dg-accent: #26313d;
    --dg-note: #2e2a1f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px -12px rgba(0, 0, 0, .5);
  }
}
:root[data-theme="dark"] {
  --paper: #15171a;
  --paper-raised: #1c1f23;
  --ink: #e7e3db;
  --ink-soft: #bdb8ae;
  --ink-mute: #8b8579;
  --rule: #2b2f34;
  --rule-soft: #23262a;
  --accent: #8fb6dd;
  --accent-soft: #1e2a36;
  --warn: #d79a76;
  --dg-fill: #232830;
  --dg-accent: #26313d;
  --dg-note: #2e2a1f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px -12px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }

html {
  /* Stops iOS inflating text in landscape. */
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  /* 20px is a deliberate step up from a desktop default: the iPad is held
     further away than a phone and closer than a monitor. */
  font-size: 20px;
  line-height: 1.75;
  font-variant-numeric: oldstyle-num proportional-nums;
  hanging-punctuation: first last;
  -webkit-font-smoothing: antialiased;
  padding: 0 0 env(safe-area-inset-bottom);
  overflow-wrap: break-word;
}
@media (max-width: 560px) { body { font-size: 18.5px; line-height: 1.7; } }

/* ---------------------------------------------------------------- chrome */

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

.bar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: max(10px, env(safe-area-inset-top)) 20px 10px;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid transparent;
  font-family: var(--font-ui);
  font-size: 14px;
  transition: transform .22s ease, border-color .22s ease;
}
.bar.scrolled { border-bottom-color: var(--rule); }
/* Hide the bar on the way down, bring it back on the way up: on a tablet the
   header is pure overhead while reading, and one thumb-flick away when not. */
.bar.hidden { transform: translateY(-100%); }
.bar a { color: var(--ink-mute); text-decoration: none; }
.bar a:hover { color: var(--accent); }
.bar .spacer { flex: 1; }

.iconbtn {
  appearance: none;
  border: 1px solid var(--rule);
  background: var(--paper-raised);
  color: var(--ink-soft);
  border-radius: 99px;
  /* 44px is the minimum comfortable touch target; nothing tappable is smaller. */
  min-width: 44px;
  min-height: 34px;
  padding: 0 13px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.iconbtn:active { transform: scale(.97); }
.iconbtn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

/* ---------------------------------------------------------------- layout */

main { max-width: var(--measure); margin: 0 auto; padding: 30px 22px 10px; }
.wide { max-width: 46rem; }
footer.foot {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 30px 22px 60px;
  border-top: 1px solid var(--rule);
  font-family: var(--font-ui);
  font-size: 13.5px;
  color: var(--ink-mute);
}
.wide + .foot, .foot.wide { max-width: 46rem; }
/* Footer navigation is chrome, not prose: match the header rather than
   inheriting the in-article link treatment. */
.foot a {
  color: var(--ink-mute);
  text-decoration: none;
  min-height: 44px;
  display: inline-block;
  line-height: 44px;
}
.foot a:hover { color: var(--accent); }
.foot p { margin: 6px 0; }

/* ---------------------------------------------------------------- heads */

h1 {
  font-family: var(--font-ui);
  font-size: 2.1rem;
  line-height: 1.14;
  letter-spacing: -.021em;
  font-weight: 660;
  margin: 6px 0 12px;
  text-wrap: balance;
}
.lede {
  font-size: 1.12rem;
  line-height: 1.55;
  color: var(--ink-soft);
  margin: 0 0 22px;
  text-wrap: pretty;
}
.meta {
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--ink-mute);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 9px;
  margin: 0 0 4px;
}
.chip {
  font-family: var(--font-ui);
  font-size: 11.5px;
  letter-spacing: .05em;
  text-transform: uppercase;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 99px;
  padding: 3px 10px;
  text-decoration: none;
  white-space: nowrap;
}

.prose h2 {
  font-family: var(--font-ui);
  font-size: 1.06rem;
  letter-spacing: .085em;
  text-transform: uppercase;
  font-weight: 620;
  color: var(--ink-mute);
  margin: 2.7em 0 .1em;
  padding-bottom: .5em;
  border-bottom: 1px solid var(--rule);
}
.prose h3 {
  font-family: var(--font-ui);
  font-size: 1.34rem;
  line-height: 1.24;
  letter-spacing: -.012em;
  font-weight: 640;
  margin: 1.9em 0 .5em;
  text-wrap: pretty;
}
.prose h4 {
  font-family: var(--font-ui);
  font-size: 1.02rem;
  font-weight: 620;
  margin: 1.6em 0 .35em;
  color: var(--ink-soft);
}
.prose > p:first-of-type { margin-top: .4em; }
.prose p { margin: 0 0 1.15em; }
.prose a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: .18em; }
.prose strong { font-weight: 640; }
.prose ul, .prose ol { padding-left: 1.15em; margin: 0 0 1.2em; }
.prose li { margin: 0 0 .5em; }
.prose li::marker { color: var(--ink-mute); }
.prose blockquote {
  margin: 1.5em 0;
  padding: 2px 0 2px 20px;
  border-left: 2px solid var(--accent);
  color: var(--ink-soft);
  font-style: italic;
}
.prose hr { border: 0; border-top: 1px solid var(--rule); margin: 2.4em 0; }

/* Contents. A details element so it costs nothing when ignored. */
.toc {
  font-family: var(--font-ui);
  font-size: 14.5px;
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin: 0 0 30px;
}
.toc summary {
  cursor: pointer;
  font-size: 11.5px;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--ink-mute);
  font-weight: 600;
}
.toc ol { margin: 12px 0 4px; padding-left: 1.2em; }
.toc li { margin: .3em 0; }
.toc a { color: var(--ink-soft); text-decoration: none; }
.toc a:hover { color: var(--accent); }

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

.prose code {
  font-family: var(--font-mono);
  font-size: .82em;
  background: var(--rule-soft);
  border: 1px solid var(--rule);
  border-radius: 5px;
  padding: .08em .34em;
  overflow-wrap: break-word;
}
.prose pre {
  font-family: var(--font-mono);
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 15px 17px;
  /* Code scrolls inside its own box; the page itself must never scroll
     sideways on a tablet. */
  overflow-x: auto;
  font-size: 14.5px;
  line-height: 1.6;
  margin: 1.5em 0;
  -webkit-overflow-scrolling: touch;
}
.prose pre code { background: none; border: 0; padding: 0; font-size: inherit; }

/* ---------------------------------------------------------------- diagrams */

.diagram {
  margin: 2em 0;
  padding: 0;
  position: relative;
}
.diagram-scroll {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 20px 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  /* The SVGs inherit this as --dg-ink via currentColor. */
  color: var(--ink-soft);
}
.diagram-scroll:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.diagram svg { display: block; margin: 0 auto; max-width: 100%; height: auto; }
.diagram figcaption {
  font-family: var(--font-ui);
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--ink-mute);
  margin-top: 10px;
  text-wrap: pretty;
}
.diagram-zoom {
  position: absolute;
  top: 8px; right: 8px;
  appearance: none;
  border: 1px solid var(--rule);
  background: color-mix(in srgb, var(--paper-raised) 85%, transparent);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  color: var(--ink-mute);
  font-family: var(--font-ui);
  font-size: 12px;
  border-radius: 99px;
  min-height: 30px;
  padding: 0 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s;
}
.diagram:hover .diagram-zoom, .diagram-zoom:focus-visible { opacity: 1; }
/* No hover on a touch screen, so show it always. Dense diagrams are the main
   reason this reader needs a full-screen mode at all. */
@media (hover: none) { .diagram-zoom { opacity: 1; } }

dialog.lightbox {
  border: 0;
  padding: 0;
  width: 100vw;
  max-width: 100vw;
  height: 100dvh;
  max-height: 100dvh;
  background: var(--paper);
  color: var(--ink);
  overscroll-behavior: contain;
}
dialog.lightbox::backdrop { background: rgba(0, 0, 0, .6); }
.lightbox .lb-body {
  height: 100%;
  overflow: auto;
  padding: max(56px, calc(env(safe-area-inset-top) + 46px)) 16px 24px;
  display: grid;
  place-items: center;
  -webkit-overflow-scrolling: touch;
}
/* Let the diagram grow past the viewport so pinch-zoom has something to bite. */
.lightbox .lb-body svg { width: auto; min-width: 100%; max-width: none; height: auto; }
.lightbox .lb-close {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: 12px;
  z-index: 2;
}

/* ---------------------------------------------------------------- callouts */

.source-note, .lab-meta, .expect {
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.55;
}
.source-note { color: var(--ink-mute); margin: -.4em 0 1.6em; }
.lab-meta {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 13px 16px;
  margin: 0 0 1.8em;
  color: var(--ink-soft);
}
.expect {
  display: flex;
  gap: 10px;
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 12px 15px;
  margin: 1.1em 0 2em;
  color: var(--ink-soft);
}
.expect span {
  flex: none;
  font-size: 11px;
  letter-spacing: .07em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--accent);
  padding-top: .28em;
}

.prompt { position: relative; margin: 1.3em 0; }
.prompt pre {
  margin: 0;
  background: var(--rule-soft);
  /* A prompt is prose to paste, not code to read in columns, so wrap it.
     Horizontal scrolling a 300-character prompt on a tablet is unusable. */
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: visible;
  /* Clear the copy button. Without this the button sits on top of the first
     line and hides the start of the prompt. */
  padding-top: 40px;
}
.copy {
  position: absolute;
  top: 8px; right: 8px;
  appearance: none;
  border: 1px solid var(--rule);
  background: var(--paper-raised);
  color: var(--ink-mute);
  font-family: var(--font-ui);
  font-size: 12px;
  border-radius: 7px;
  min-height: 30px;
  padding: 0 10px;
  cursor: pointer;
}
.copy.done { color: var(--accent); border-color: var(--accent); }

.takeaways {
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 6px 20px;
  margin: 1.6em 0;
}

/* ---------------------------------------------------------------- cards */

.queue { list-style: none; padding: 0; margin: 0; display: grid; gap: 14px; }
.card {
  display: block;
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 14px;
  padding: 18px 20px;
  text-decoration: none;
  color: inherit;
  box-shadow: var(--shadow);
  transition: transform .14s ease, border-color .14s ease;
  -webkit-tap-highlight-color: transparent;
}
.card:active { transform: scale(.99); }
.card:hover { border-color: var(--accent); }
.card h2 {
  font-family: var(--font-ui);
  font-size: 1.2rem;
  line-height: 1.28;
  letter-spacing: -.012em;
  font-weight: 640;
  margin: 10px 0 7px;
  color: var(--ink);
  text-wrap: pretty;
}
.card p { margin: 0; font-size: .96rem; line-height: 1.55; color: var(--ink-soft); }
.card .meta { margin: 0; }
.card.requested { border-left: 3px solid var(--warn); }

.stars { color: var(--accent); font-family: var(--font-ui); letter-spacing: .08em; }
.empty {
  border: 1px dashed var(--rule);
  border-radius: 14px;
  padding: 30px 22px;
  text-align: center;
  color: var(--ink-mute);
  font-family: var(--font-ui);
  font-size: 15px;
}

/* ---------------------------------------------------------------- rating */

.rate {
  margin: 3em 0 1em;
  padding: 22px;
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: 14px;
  font-family: var(--font-ui);
}
.rate h2 {
  font-family: var(--font-ui);
  font-size: 1.1rem;
  font-weight: 640;
  margin: 0 0 4px;
  border: 0;
  padding: 0;
  text-transform: none;
  letter-spacing: -.01em;
  color: var(--ink);
}
.rate .hint { font-size: 13.5px; color: var(--ink-mute); margin: 0 0 18px; }
.rate fieldset { border: 0; padding: 0; margin: 0 0 16px; }
.rate legend {
  font-size: 11.5px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ink-mute);
  font-weight: 600;
  padding: 0 0 8px;
}
.opts { display: flex; flex-wrap: wrap; gap: 8px; }
.opt {
  appearance: none;
  border: 1px solid var(--rule);
  background: var(--paper);
  color: var(--ink-soft);
  border-radius: 99px;
  min-height: 44px;
  padding: 0 16px;
  font: inherit;
  font-size: 14.5px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background .12s, border-color .12s;
}
.opt:active { transform: scale(.97); }
.opt[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--paper);
  font-weight: 560;
}
.rate textarea {
  width: 100%;
  min-height: 76px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  padding: 11px 13px;
  resize: vertical;
}
.rate textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.rate .actions { display: flex; align-items: center; gap: 12px; margin-top: 14px; }
.rate .primary {
  appearance: none;
  border: 0;
  background: var(--accent);
  color: var(--paper);
  border-radius: 99px;
  min-height: 46px;
  padding: 0 24px;
  font: inherit;
  font-size: 15.5px;
  font-weight: 570;
  cursor: pointer;
}
.rate .primary:disabled { opacity: .45; cursor: default; }
.rate .status { font-size: 13.5px; color: var(--ink-mute); }

.nextup {
  display: block;
  margin: 2.2em 0 0;
  padding: 16px 18px;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: var(--paper-raised);
  text-decoration: none;
  color: inherit;
  font-family: var(--font-ui);
}
.nextup small {
  display: block;
  font-size: 11.5px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ink-mute);
  margin-bottom: 5px;
}
.nextup strong { font-weight: 600; font-size: 1.02rem; line-height: 1.3; }

.offline {
  position: fixed;
  bottom: max(14px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-ui);
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 99px;
  z-index: 70;
  box-shadow: var(--shadow);
}

@media print {
  .bar, .progress, .rate, .diagram-zoom, .copy, .nextup, .offline { display: none !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  main { max-width: none; }
  .diagram-scroll { border: 0; padding: 0; }
}
