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

:root {
  --bg: #f6f5f1;
  --bg-raised: #ffffff;
  --bg-sunken: #eeece6;
  --ink: #1e2320;
  --ink-soft: #5b635e;
  --ink-faint: #646c66;
  --rule: #ddd9d0;
  --rule-strong: #c8c3b8;
  --accent: #2f6f5e;
  --accent-soft: #e4efea;
  --pass: #2f6f5e;
  --pass-soft: #e6f0ec;
  --fail: #a53a2c;
  --fail-soft: #f8e9e6;
  --tests: #6b5aa6;
  --tests-soft: #eeeaf7;
  --shadow: 0 1px 2px rgb(30 35 32 / 6%), 0 6px 20px rgb(30 35 32 / 5%);
  --radius: 10px;
  --topbar-h: 56px;
  --font-ui: ui-sans-serif, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-read: ui-serif, Georgia, "Iowan Old Style", "Times New Roman", serif;
  --font-mono: ui-monospace, SFMono-Regular, "Cascadia Mono", Consolas, monospace;
}

/* app.js resolves the system preference on boot and always writes an explicit
   data-theme, so the dark palette needs one selector rather than two. */
:root[data-theme="light"] {
  color-scheme: light;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14181a;
  --bg-raised: #1b2023;
  --bg-sunken: #101416;
  --ink: #e6e8e6;
  --ink-soft: #a3ada8;
  --ink-faint: #79837e;
  --rule: #2b3235;
  --rule-strong: #3b4448;
  --accent: #6fc0a5;
  --accent-soft: #1d2f2a;
  --pass: #6fc0a5;
  --pass-soft: #172a25;
  --fail: #e08a7c;
  --fail-soft: #2e1e1b;
  --tests: #a99ae0;
  --tests-soft: #232036;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 6px 20px rgb(0 0 0 / 25%);
}

/* ------------------------------------------------------------------ base */

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--topbar-h) + 16px);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  border-radius: 0 0 var(--radius) 0;
}

.skip-link:focus {
  left: 0;
}

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

/* ---------------------------------------------------------------- topbar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: color-mix(in srgb, var(--bg-raised) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.brand {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.doc-tabs {
  display: flex;
  gap: 6px;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}

.doc-tabs::-webkit-scrollbar {
  display: none;
}

.doc-tab {
  flex: 0 0 auto;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink-soft);
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}

.doc-tab:hover {
  background: var(--bg-sunken);
  color: var(--ink);
}

.doc-tab[aria-current="true"] {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
  color: var(--accent);
  font-weight: 600;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--rule);
  background: var(--bg-raised);
  color: var(--ink-soft);
  cursor: pointer;
}

.icon-btn:hover {
  color: var(--ink);
  border-color: var(--rule-strong);
}

.nav-toggle {
  display: none;
}

.icon-moon {
  display: none;
}

:root[data-theme="dark"] .icon-sun {
  display: none;
}

:root[data-theme="dark"] .icon-moon {
  display: block;
}

.switch {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.switch input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  cursor: pointer;
}

.link-btn {
  border: 0;
  background: none;
  padding: 0;
  font: inherit;
  font-size: 12px;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

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

.layout {
  display: grid;
  grid-template-columns: 270px minmax(0, 1fr);
  align-items: start;
  max-width: 1240px;
  margin: 0 auto;
  padding: 0 24px;
  gap: 32px;
}

.sidebar {
  position: sticky;
  top: var(--topbar-h);
  max-height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
  padding: 24px 0 40px;
  scrollbar-width: thin;
}

.toc {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.toc a {
  display: block;
  padding: 5px 10px;
  border-left: 2px solid transparent;
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.4;
  text-decoration: none;
  border-radius: 0 4px 4px 0;
}

.toc a:hover {
  background: var(--bg-sunken);
  color: var(--ink);
}

.toc a.lvl-3 {
  padding-left: 22px;
  font-size: 12.5px;
}

.toc a.active {
  border-left-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}

.run-progress {
  margin-top: 22px;
  padding: 12px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg-raised);
}

.run-progress-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-size: 12px;
  color: var(--ink-soft);
  margin-bottom: 8px;
}

.run-bar {
  height: 5px;
  border-radius: 999px;
  background: var(--bg-sunken);
  overflow: hidden;
}

.run-bar span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.25s ease;
}

.content {
  min-width: 0;
  padding: 8px 0 120px;
}

/* The measure is set on the text blocks, not the article, so wide tables can
   use the whole column while prose stays at a readable line length. */
.doc {
  max-width: 1000px;
  outline: none;
}

.doc > h1,
.doc > h2,
.doc > h3,
.doc > h4,
.doc > p,
.doc > ul,
.doc > ol,
.doc > blockquote,
.doc > hr,
.doc > .turn {
  /* Fixed rather than ch-based: the prose is serif and the cards are not, so
     a ch measure would set two different line lengths. */
  max-width: 680px;
}

/* --------------------------------------------------------------- content */

.doc h1,
.doc h2,
.doc h3,
.doc h4 {
  font-family: var(--font-ui);
  line-height: 1.25;
  letter-spacing: -0.01em;
  scroll-margin-top: calc(var(--topbar-h) + 16px);
}

.doc h1 {
  font-size: 30px;
  margin: 32px 0 8px;
}

.doc h2 {
  font-size: 21px;
  margin: 52px 0 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--rule);
}

.doc h3 {
  font-size: 16px;
  margin: 38px 0 10px;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 700;
}

.doc h4 {
  font-size: 15px;
  margin: 26px 0 8px;
}

.h-anchor .anchor-link {
  margin-left: 8px;
  color: var(--ink-faint);
  text-decoration: none;
  opacity: 0;
  font-weight: 400;
}

.h-anchor:hover .anchor-link {
  opacity: 1;
}

.doc p {
  font-family: var(--font-read);
  font-size: 17px;
  line-height: 1.68;
  margin: 0 0 16px;
}

.doc ul,
.doc ol {
  font-family: var(--font-read);
  font-size: 17px;
  line-height: 1.68;
  padding-left: 24px;
  margin: 0 0 18px;
}

.doc li {
  margin-bottom: 8px;
}

.doc li::marker {
  color: var(--ink-faint);
}

.doc strong {
  font-weight: 700;
  color: var(--ink);
}

.doc em {
  font-style: italic;
}

.doc code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: var(--bg-sunken);
  border: 1px solid var(--rule);
  border-radius: 5px;
  padding: 1px 5px;
}

.doc a {
  color: var(--accent);
}

.doc hr {
  border: 0;
  border-top: 1px solid var(--rule);
  margin: 36px 0;
}

.doc blockquote {
  margin: 0 0 18px;
  padding: 2px 0 2px 16px;
  border-left: 3px solid var(--rule-strong);
  color: var(--ink-soft);
}

.doc blockquote p:last-child {
  margin-bottom: 0;
}

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

.table-wrap {
  overflow-x: auto;
  margin: 0 0 24px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg-raised);
}

.doc table {
  border-collapse: collapse;
  width: 100%;
  /* Scroll inside .table-wrap on narrow screens rather than crushing the
     five-column scoring sheets into unreadable slivers. */
  min-width: 520px;
  font-size: 14px;
  line-height: 1.5;
}

.doc th,
.doc td {
  text-align: left;
  vertical-align: top;
  padding: 10px 14px;
  border-bottom: 1px solid var(--rule);
}

.doc thead th {
  background: var(--bg-sunken);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-soft);
  white-space: nowrap;
}

.doc tbody tr:last-child td {
  border-bottom: 0;
}

.doc tbody tr:hover td {
  background: color-mix(in srgb, var(--accent-soft) 45%, transparent);
}

.doc td.fill-in {
  min-width: 68px;
  background-image: linear-gradient(var(--rule) 1px, transparent 1px);
  background-size: 100% 26px;
  background-position: 0 24px;
  background-repeat: no-repeat;
}

/* ------------------------------------------------------------ copy chips */

.chip {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  max-width: 100%;
  text-align: left;
  font: inherit;
  color: var(--ink);
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
  border-radius: 7px;
  padding: 3px 8px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.chip:hover {
  background: color-mix(in srgb, var(--accent) 18%, var(--bg-raised));
  border-color: var(--accent);
}

.chip .chip-icon {
  color: var(--accent);
  flex: 0 0 auto;
  transform: translateY(2px);
}

.chip.copied {
  background: var(--pass-soft);
  border-color: var(--pass);
}

/* ----------------------------------------------------------- turn blocks */

.turn {
  margin: 0 0 20px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg-raised);
  box-shadow: var(--shadow);
  overflow: hidden;
  scroll-margin-top: calc(var(--topbar-h) + 16px);
}

.turn-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--rule);
  background: var(--bg-sunken);
}

.turn-id {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 5px;
  padding: 2px 8px;
}

.turn-phase {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-faint);
}

.turn-done {
  display: none;
  align-items: center;
  gap: 4px;
  margin-left: auto;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--pass);
}

.turn.is-done .turn-done {
  display: inline-flex;
}

.turn.is-done .turn-copy {
  background: var(--pass-soft);
}

.turn-copy {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 16px;
  cursor: pointer;
  font: inherit;
  color: inherit;
  transition: background 0.12s ease;
}

.turn-copy:hover {
  background: var(--accent-soft);
}

.turn-text {
  flex: 1 1 auto;
  font-family: var(--font-read);
  font-size: 17.5px;
  line-height: 1.6;
}

.turn-text::before {
  content: "\201C";
}

.turn-text::after {
  content: "\201D";
}

.turn-copy-action {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  border-radius: 7px;
  padding: 5px 10px;
  background: var(--bg-raised);
  position: sticky;
  top: 8px;
}

.turn-copy:hover .turn-copy-action {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.turn-copy-icon {
  display: inline-flex;
}

.turn-meta {
  border-top: 1px dashed var(--rule);
  padding: 12px 16px 14px;
  background: color-mix(in srgb, var(--bg-sunken) 55%, transparent);
}

.turn-meta-row {
  font-family: var(--font-ui) !important;
  font-size: 13.5px !important;
  line-height: 1.55 !important;
  margin: 0 0 6px !important;
  padding-left: 12px;
  border-left: 2px solid var(--rule-strong);
  color: var(--ink-soft);
}

.turn-meta-row:last-child {
  margin-bottom: 0 !important;
}

.turn-meta-row.is-test {
  border-left-color: var(--tests);
}

.turn-meta-row.is-test strong:first-child {
  color: var(--tests);
}

.turn-meta-row.is-pass {
  border-left-color: var(--pass);
}

.turn-meta-row.is-pass strong:first-child {
  color: var(--pass);
}

.turn-meta-row.is-fail {
  border-left-color: var(--fail);
}

.turn-meta-row.is-fail strong:first-child {
  color: var(--fail);
}

/* Turns-only mode: strip the script sections down to a run sheet. Sections
   with no turns in them (the transition protocol, the scoring sheets) are left
   intact — hiding their contents would leave nothing but bare headings. */
body.compact .doc > [data-section-turns="1"]:not(.turn):not(h1):not(h2):not(h3) {
  display: none;
}

body.compact .turn-meta {
  display: none;
}

body.compact .doc h2 {
  margin-top: 34px;
}

body.compact .turn {
  margin-bottom: 10px;
}

/* ----------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 12px);
  z-index: 60;
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  max-width: min(90vw, 560px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* --------------------------------------------------- welcome / file open */

.loader {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.loader[hidden] {
  display: none;
}

.loader-card {
  max-width: 460px;
  padding: 34px 32px 28px;
  border: 1px solid var(--rule);
  border-radius: 14px;
  background: var(--bg-raised);
  box-shadow: var(--shadow);
  text-align: center;
}

.loader-mark {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  margin: 0 auto 16px;
  border-radius: 14px;
  background: var(--accent-soft);
  color: var(--accent);
}

.loader-card h1 {
  margin: 0 0 8px;
  font-size: 21px;
  letter-spacing: -0.01em;
}

.loader-card p {
  margin: 0 0 20px;
  color: var(--ink-soft);
  font-size: 14.5px;
  line-height: 1.6;
}

.loader-card code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--bg-sunken);
  padding: 1px 5px;
  border-radius: 4px;
}

.primary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 22px;
  border: 0;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:hover {
  background: color-mix(in srgb, var(--accent) 85%, #000);
}

.loader-hint {
  margin: 12px 0 0 !important;
  font-size: 13px !important;
  color: var(--ink-faint) !important;
}

.loader-note {
  margin: 22px 0 0 !important;
  padding-top: 16px;
  border-top: 1px solid var(--rule);
  font-size: 12.5px !important;
  line-height: 1.55 !important;
  color: var(--ink-faint) !important;
  text-align: left;
}

.text-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--bg-raised);
  color: var(--ink-soft);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.text-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.drop-veil {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(3px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s ease;
}

.drop-veil.show {
  opacity: 1;
}

.drop-veil span {
  padding: 26px 44px;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: var(--bg-raised);
  color: var(--accent);
  font-size: 16px;
  font-weight: 600;
}

.loaded-files {
  margin-top: 16px;
  padding: 12px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--bg-raised);
}

.loaded-files[hidden] {
  display: none;
}

.loaded-files-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-size: 12px;
  color: var(--ink-soft);
  margin-bottom: 7px;
}

.loaded-files ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.loaded-files li {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--ink-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px 0;
}

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

@media (max-width: 940px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    padding: 0 18px;
    gap: 0;
  }

  .nav-toggle {
    display: grid;
  }

  .brand,
  .text-btn span {
    display: none;
  }

  .text-btn {
    padding: 0 9px;
  }

  .sidebar {
    display: none;
    position: static;
    max-height: none;
    padding: 16px 0 0;
    border-bottom: 1px solid var(--rule);
  }

  .sidebar.open {
    display: block;
  }

  .doc p,
  .doc ul,
  .doc ol {
    font-size: 16px;
  }

  .turn-copy {
    flex-direction: column;
    gap: 10px;
  }

  .turn-copy-action {
    align-self: flex-end;
    position: static;
  }
}

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

@media print {
  .topbar,
  .sidebar,
  .toast,
  .turn-copy-action,
  .anchor-link {
    display: none !important;
  }

  body {
    background: #fff;
  }

  .layout {
    display: block;
    padding: 0;
  }

  .turn {
    break-inside: avoid;
    box-shadow: none;
  }
}
