:root, [data-theme="dark"] {
  --bg-deep: #120c08;
  --bg: #1a120c;
  --panel: #221610;
  --ink: #f0e6d6;
  --muted: #a89880;
  --line: #3a2a1e;
  --accent: #e07040;
  --link: #e8b04b;
  --card: #1e140e;
  --shadow: 0 18px 50px rgba(0,0,0,.5);
  color-scheme: dark;
}
[data-theme="light"] {
  --bg-deep: #e9ddc8;
  --bg: #f5efe1;
  --panel: #fdf9ef;
  --ink: #2b2118;
  --muted: #6e5f4c;
  --line: #d8c9ae;
  --accent: #a03a1e;
  --link: #8a5a12;
  --card: #fffdf6;
  --shadow: 0 18px 50px rgba(90,60,30,.16);
  color-scheme: light;
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: .001s !important; transition-duration: .001s !important; }
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 1.0625rem/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
html:not(.js) .needs-js { display: none !important; }
.skip {
  position: absolute; left: -999px; top: 0; z-index: 50;
  background: var(--accent); color: #04211c; padding: .5rem 1rem; border-radius: 0 0 .5rem 0;
}
.skip:focus { left: 0; }
a { color: var(--link); }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2 { font-family: Georgia, "Times New Roman", serif; line-height: 1.12; text-wrap: balance; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: .9rem;
  padding: .55rem 1rem;
  background: color-mix(in srgb, var(--bg-deep) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.home { color: var(--ink); text-decoration: none; font-weight: 650; white-space: nowrap; }
.topnav { display: flex; gap: .15rem; overflow-x: auto; scrollbar-width: none; }
.topnav::-webkit-scrollbar { display: none; }
.topnav a {
  color: var(--muted); text-decoration: none; font-size: .85rem; white-space: nowrap;
  padding: .3rem .55rem; border-radius: 2rem;
}
.topnav a[aria-current="true"] { color: var(--ink); background: var(--line); }
.spacer { flex: 1; }
.chip {
  border: 1px solid var(--line); border-radius: 2rem; padding: .25rem .8rem;
  color: var(--ink); text-decoration: none; font-size: .85rem; white-space: nowrap;
}
.icon-btn {
  background: none; border: 1px solid var(--line); border-radius: 50%;
  width: 2.75rem; height: 2.75rem; display: grid; place-items: center;
  color: var(--ink); cursor: pointer;
}
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.icon-btn svg[hidden] { display: none; }

/* hero */
.hero-wrap {
  max-width: 60rem; margin: 0 auto; padding: 4.5rem 1.25rem 1rem; text-align: center;
}
.kicker {
  color: var(--muted); text-transform: uppercase; letter-spacing: .14em;
  font-size: .8rem; font-weight: 700; margin: 0 0 1rem;
}
.hero-text h1 { font-size: clamp(2.4rem, 6vw, 4.2rem); margin: 0 0 1.2rem; }
.deck { font-size: clamp(1.1rem, 2.4vw, 1.35rem); color: var(--muted); max-width: 46rem; margin: 0 auto; }
.hero-cta { margin: 2rem 0 0; }
.cta, .btn {
  display: inline-block; background: var(--accent); color: #04211c;
  font-weight: 700; text-decoration: none; border: none; cursor: pointer;
  padding: .7rem 1.5rem; border-radius: 2rem; font-size: 1rem;
}
[data-theme="light"] .cta, [data-theme="light"] .btn { color: #fff; }
.btn.ghost { background: none; border: 1px solid var(--line); color: var(--ink); font-weight: 600; }
.btn:disabled { opacity: .5; cursor: default; }

/* chapters are centred articles; the loom is the shared engine */
main { max-width: 60rem; margin: 0 auto; padding: 0 1.25rem; }
.chapter { max-width: 46rem; margin-left: auto; margin-right: auto; }

/* chapters */
.chapter { margin: 0 0 6rem; }
.chapter:last-child { margin-bottom: 2rem; }
.ch-no {
  display: inline-block; font: 700 .8rem ui-monospace, monospace;
  border: 1px solid var(--line); border-radius: .4rem; padding: .1rem .45rem; margin-right: .5rem;
  color: var(--accent); vertical-align: .1em;
}
.chapter h2 { font-size: clamp(1.7rem, 3.6vw, 2.5rem); margin: .4rem 0 .3rem; }
.sci { color: var(--muted); font-style: italic; margin: 0 0 1rem; }
.lede { font-size: 1.2rem; line-height: 1.55; margin: 0 0 1.5rem; }
[data-theme="light"] .ch-sacal { --accent: #b45309; }
[data-theme="light"] .ch-pelican { --accent: #be185d; }
[data-theme="light"] .ch-sarpe { --accent: #15803d; }
.figure { margin: 0 0 1.5rem; }
.figure img { width: 100%; height: auto; display: block; border-radius: .9rem; box-shadow: var(--shadow); }
.figure.small img { border-radius: .7rem; }
figcaption { color: var(--muted); font-size: .88rem; margin-top: .55rem; }
.credit { opacity: .85; }
.fig-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); gap: 1.25rem; }
.stats div { background: var(--card); border: 1px solid var(--line); border-radius: .7rem; padding: .7rem .9rem; }
.stats dt { color: var(--muted); font-size: .76rem; text-transform: uppercase; letter-spacing: .08em; }
.stats dd { margin: .15rem 0 0; font-weight: 700; font-size: 1.05rem; }

/* river strip */
.river h3, .tl h3 { margin: 0 0 .3rem; font-size: 1.25rem; font-family: Georgia, serif; }
.chapter, .finder, .quiz, .closing, .sources { scroll-margin-top: 3.5rem; }
.swimmer .fish { display: inline-block; transform: scaleX(-1); }
.river.bump .swimmer { animation: bump .5s ease-out 2; }

/* timeline */

/* sections after the story */
.finder, .quiz, .closing, .sources { max-width: 60rem; margin: 0 auto; padding: 4rem 1.25rem 0; }
.finder h2, .quiz h2, .closing h2, .sources h2 { font-size: clamp(1.8rem, 4vw, 2.6rem); margin: 0 0 .6rem; }
.finder > .lede, .quiz > .lede { color: var(--muted); margin: 0 0 1.5rem; }
.finder-grid { display: grid; grid-template-columns: 1fr; gap: 1.5rem; }
@media (min-width: 60rem) { .finder-grid { grid-template-columns: 1.2fr 1fr; } }
.finder-tabs { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.finder-tab { background: var(--card); border: 1px solid var(--line); color: var(--ink); border-radius: 2rem; padding: .65rem 1.1rem; cursor: pointer; font-size: .9rem; min-height: 2.75rem; }
.finder-tab[aria-pressed="true"] { background: var(--accent); color: #04211c; border-color: var(--accent); font-weight: 700; }
[data-theme="light"] .finder-tab[aria-pressed="true"] { color: #fff; }
.js .finder-panel > summary { display: none; }
.finder-panel { border: 1px solid var(--line); border-radius: .9rem; padding: 1rem 1.25rem; margin-bottom: .7rem; background: var(--card); }
.js .finder-panel { display: none; }
.js .finder-panel.open { display: block; }
.finder-panel summary { cursor: pointer; font-weight: 700; }
.js .finder-panel ul { margin-top: 0; }
.finder-panel li:first-child { border-top: none; }
.badge { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; border-radius: 2rem; padding: .1rem .6rem; margin-left: .5rem; white-space: nowrap; }
.badge.home { background: var(--accent); color: #04211c; font-weight: 700; }
[data-theme="light"] .badge.home { color: #fff; }
.badge.neighbour { border: 1px solid var(--line); color: var(--muted); }

/* quiz */
#quiz-form { display: grid; gap: 1rem; }
.qcard { border: 1px solid var(--line); border-radius: 1rem; padding: 1.1rem 1.25rem; background: var(--card); margin: 0; }
.qcard legend { padding: 0 .5rem; font-weight: 600; }
.qno { display: inline-grid; place-items: center; width: 1.7rem; height: 1.7rem; border-radius: 50%; background: var(--line); font-size: .85rem; margin-right: .4rem; }
.opt { display: block; margin: .45rem 0; cursor: pointer; }
.opt input { position: absolute; opacity: 0; }
.opt span { display: block; border: 1px solid var(--line); border-radius: .7rem; padding: .55rem .9rem; transition: border-color .2s, background .2s; }
.opt input:checked + span { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); font-weight: 600; }
.opt input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.qcard.right { border-color: #22c55e; }
.qcard.wrong { border-color: #f87171; }
.qverdict { margin: .6rem 0 0; font-weight: 700; }
.qcard.right .qverdict { color: #22c55e; }
.qcard.wrong .qverdict { color: #f87171; }
[data-theme="light"] .qcard.right { border-color: #15803d; }
[data-theme="light"] .qcard.wrong { border-color: #b91c1c; }
[data-theme="light"] .qcard.right .qverdict { color: #15803d; }
[data-theme="light"] .qcard.wrong .qverdict { color: #b91c1c; }
.scoreboard { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; margin-top: 1.25rem; background: var(--card); border: 1px solid var(--line); border-radius: 1rem; padding: 1rem 1.25rem; }
.score-line b { font-size: 1.3rem; color: var(--accent); }
.score-verdict { font-weight: 600; flex: 1; min-width: 12rem; }
.score-btns { display: flex; gap: .6rem; }
.score-note { width: 100%; color: var(--muted); font-size: .9rem; }
.answers { margin-top: 1rem; color: var(--muted); }

/* closing, sources, footer */
.closing p { font-size: 1.15rem; }
.source-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1.5rem; margin-bottom: 2.5rem; }
.source-grid h3 { font-size: 1rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin: 0 0 .5rem; }
.source-grid ul { list-style: none; margin: 0; padding: 0; }
.source-grid li { margin-bottom: .6rem; font-size: .93rem; }
.fig-note { color: var(--muted); }
.site-footer { text-align: center; color: var(--muted); padding: 3rem 1rem 2.5rem; margin-top: 2rem; border-top: 1px solid var(--line); }
.footernav { display: flex; flex-wrap: wrap; justify-content: center; gap: .4rem 1.1rem; margin-bottom: 1.2rem; }
.footernav a { color: var(--muted); font-size: .9rem; }
.missing { max-width: 40rem; margin: 20vh auto; text-align: center; padding: 0 1.25rem; }

@media (max-width: 59.99rem) {
  .topbar { flex-wrap: wrap; row-gap: .35rem; }
  .topnav { display: flex; order: 10; flex: 1 1 100%; padding-bottom: .15rem; }
  .chapter, .finder, .quiz, .closing, .sources { scroll-margin-top: calc(46svh + 7.5rem); }
  .map-hint { display: none; }
}
@media (min-width: 60rem) {
  .hero-wrap { padding-top: 5.5rem; }
}

/* loom */
.band { width: 100%; height: auto; display: block; border-radius: .4rem; }
.js .weave .wrow { opacity: 0; }
.js .weave .wrow.lit { opacity: 1; transition: opacity .25s; }
.loom-beam { height: .9rem; border-radius: .5rem; background: linear-gradient(180deg, #8a5f36, #5e3d20); box-shadow: var(--shadow); }
.loom-beam.top { margin-bottom: .4rem; } .loom-beam.bottom { margin-top: .4rem; }
.hero-loom { max-width: 44rem; margin: 2.5rem auto 0; }
.weave-status { color: var(--muted); font-size: .85rem; margin: .5rem 0 0; min-height: 1.2em; }

/* shed demo */
.shed { background: var(--card); border: 1px solid var(--line); border-radius: 1rem; padding: 1.25rem; margin: 1.5rem 0; text-align: center; }
.warp { display: flex; justify-content: center; gap: .55rem; height: 3.2rem; align-items: stretch; }
.warp i { width: 3px; background: var(--muted); border-radius: 2px; }
.warp i:nth-child(odd) { transform: translateY(-.5rem); }
.warp i:nth-child(even) { transform: translateY(.5rem); }
.shuttle { font-size: 1.4rem; color: var(--accent); }
.js .shuttle { animation: shuttle 3s ease-in-out infinite; display: inline-block; }
@keyframes shuttle { 0%, 100% { transform: translateX(-6rem); } 50% { transform: translateX(6rem); } }
.shed-cap { color: var(--muted); font-size: .88rem; margin: .5rem 0 0; }

/* motif cards */
.mgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: 1.25rem; margin: 1.5rem 0; }
.mcard { background: var(--card); border: 1px solid var(--line); border-radius: 1rem; padding: 1.1rem; }
.mcard .motif { width: 100%; height: auto; border-radius: .5rem; }
.mcard h3 { margin: .8rem 0 .2rem; font-size: 1.15rem; }
.mregion { color: var(--muted); font-size: .85rem; margin: 0 0 .4rem; }
.mmeaning { margin: 0; font-size: .95rem; }
.aka { color: var(--muted); font-size: .85rem; font-style: italic; margin: .4rem 0 0; }

/* regions */
.region-tabs { display: flex; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.region-tab { background: var(--card); border: 1px solid var(--line); color: var(--ink); border-radius: 2rem; padding: .65rem 1.1rem; cursor: pointer; font-size: .9rem; min-height: 2.75rem; }
.region-tab[aria-pressed="true"] { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 700; }
.js .region-band { display: none; }
.js .region-band.open { display: block; }
.region-band h3 { margin: 0 0 .2rem; }
.pal-note { color: var(--muted); margin: 0 0 .8rem; font-size: .92rem; }

/* dye vat */
.vat { display: grid; grid-template-columns: 1fr; gap: 1.5rem; margin: 1.5rem 0; }
@media (min-width: 60rem) { .vat { grid-template-columns: 1.5fr 1fr; } }
.vat-matrix { border-collapse: collapse; width: 100%; }
.vat-matrix th, .vat-matrix td { padding: .35rem; text-align: center; font-size: .82rem; }
.vat-matrix thead th { color: var(--muted); font-weight: 600; }
.vat-matrix tbody th { text-align: left; font-size: .85rem; }
.js .vat-static { display: none; }
html:not(.js) .vat-cell { display: none; }
.vat-cell { background: var(--card); border: 1px solid var(--line); border-radius: .7rem; padding: .5rem .3rem; cursor: pointer; width: 100%; color: var(--ink); }
.vat-cell.picked { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.vat-cell .chip, .chip-svg { display: block; width: 2rem; height: 2rem; margin: 0 auto .3rem; border-radius: .5rem; border: 1px solid var(--line); }
.vat-static .cell-name, .vat-cell .cell-name { display: block; font-size: .75rem; color: var(--muted); }
.vat-note { color: var(--muted); font-size: .88rem; }
.skein-panel { background: var(--card); border: 1px solid var(--line); border-radius: 1rem; padding: 1.25rem; text-align: center; align-self: start; }
.skein { display: flex; justify-content: center; gap: .4rem; margin-bottom: .8rem; }
.skein i { width: 1.1rem; height: 4.5rem; border-radius: .6rem; background: var(--line); }
.skein-name { font-weight: 700; margin: 0; min-height: 1.4em; }
.badge.holds { background: #2f7a3d; color: #fff; }
.badge.washes { background: none; border: 1px solid var(--line); color: var(--muted); }
.badge.doc { background: var(--accent); color: #fff; font-weight: 700; }
.badge.atrib { border: 1px solid var(--line); color: var(--muted); }
.skein-sent { color: var(--muted); font-size: .9rem; min-height: 1.2em; }
.vat-hint { font-size: .88rem; margin: .2rem 0 0; }

/* wool road */
.steps { display: grid; grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr)); gap: .5rem; margin-bottom: 1.2rem; }
.step { background: var(--card); border: 1px solid var(--line); color: var(--ink); border-radius: .8rem; padding: .6rem; cursor: pointer; text-align: left; min-height: 2.75rem; }
.step.done { border-color: var(--accent); }
.step-no { display: inline-grid; place-items: center; width: 1.5rem; height: 1.5rem; border-radius: 50%; background: var(--line); font-size: .75rem; margin-right: .4rem; }
.step.done .step-no { background: var(--accent); color: #fff; }
.step-t { font-weight: 700; font-size: .85rem; }
.step-d { display: block; color: var(--muted); font-size: .78rem; margin-top: .2rem; }
.js .steps-static { display: none; }
.steps-static { color: var(--muted); }
.js .road-band .wrow { opacity: .12; }
.js .road-band .wrow.lit { opacity: 1; transition: opacity .4s; }

/* studio */
.studio { background: var(--card); border: 1px solid var(--line); border-radius: 1rem; padding: 1.25rem; margin: 1.5rem 0; }
@keyframes loomflash { 0%, 100% { box-shadow: none; } 30% { box-shadow: 0 0 0 3px var(--accent); } }
.studio.flash { animation: loomflash 1.2s ease-out; }
.pick-group { margin-bottom: 1rem; }
.pick-label { display: block; font-weight: 700; font-size: .85rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: .5rem; }
.pick { background: var(--bg); border: 1px solid var(--line); color: var(--ink); border-radius: .8rem; padding: .5rem .7rem; cursor: pointer; font-size: .85rem; margin: 0 .4rem .4rem 0; min-height: 2.75rem; vertical-align: top; }
.pick[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.pick .mini { width: 3.2rem; height: auto; display: block; margin: 0 auto .2rem; border-radius: .3rem; }
.swatches { display: inline-flex; gap: 2px; margin-right: .4rem; vertical-align: middle; }
.swatches i { width: 1rem; height: 1rem; border-radius: .25rem; background: var(--line); display: inline-block; }
.studio-loom { max-width: 34rem; margin: 0 auto; }
#loom-canvas { width: 100%; height: auto; display: block; border-radius: .4rem; image-rendering: pixelated; }
html:not(.js) #loom-canvas { display: none; }
.js .loom-static { display: none; }
.studio-ctl { display: flex; align-items: center; gap: .8rem; flex-wrap: wrap; margin: 1rem 0 0; }
.studio-note, .studio-done { color: var(--muted); font-size: .9rem; }
#dl-btn[href="#"] { opacity: .5; pointer-events: none; }

/* finder */
.finder-grid { display: grid; grid-template-columns: 1fr; gap: 1.5rem; }
@media (min-width: 60rem) { .finder-grid { grid-template-columns: 1fr 1fr; } }
.pick-row { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.fcard { background: var(--card); border: 1px solid var(--line); border-radius: .9rem; padding: 1rem 1.2rem; margin-bottom: .8rem; }
.fcard h3 { margin: 0 0 .2rem; }
