/* pure-cbor documentation: the one-page tour. Layout, the scroll-linked trace
   panels, the playground and the streaming demo. Builds on style.css. */

:root {
  --tour-width: 72rem;
  --trace-width: 22rem;

  /* One hue per CBOR major type, for byte cells and annotations. */
  --major-0: #dbe8fb; --major-0-ink: #1d4f9c;
  --major-1: #e8e0fa; --major-1-ink: #5a3aa8;
  --major-2: #d7f0ec; --major-2-ink: #136b5e;
  --major-3: #e1f2d9; --major-3-ink: #2f6a1c;
  --major-4: #fbe6d4; --major-4-ink: #94460d;
  --major-5: #f9dde7; --major-5-ink: #9b2458;
  --major-6: #f7eccb; --major-6-ink: #7a5a06;
  --major-7: #e9e7e1; --major-7-ink: #4a4945;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --major-0: #1c2c47; --major-0-ink: #9cc2ff;
    --major-1: #2a2244; --major-1-ink: #c9b5ff;
    --major-2: #15322e; --major-2-ink: #7fd8c8;
    --major-3: #1d2f19; --major-3-ink: #a5d985;
    --major-4: #3a2615; --major-4-ink: #f4ad6a;
    --major-5: #3a1c28; --major-5-ink: #f28bc3;
    --major-6: #352d14; --major-6-ink: #e6c766;
    --major-7: #2c2c2a; --major-7-ink: #cfcdc4;
  }
}

/* ------------------------------------------------------------ header */

.one-page .header-inner { max-width: 90rem; }
.chapter-nav a[aria-current="true"] { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }

.reading-progress { height: 2px; background: transparent; }
.reading-progress span {
  display: block;
  height: 100%;
  background: var(--accent);
  transform-origin: 0 50%;
  transform: scaleX(var(--progress, 0));
}

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

.tour-layout {
  max-width: 90rem;
  margin: 0 auto;
  padding: 0 1.5rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}

.tour-main {
  min-width: 0;
  width: 100%;
  max-width: var(--tour-width);
  margin: 0 auto;
  padding: 2.5rem 0 5rem;
}

/* Prose keeps a reading measure; wide blocks opt out. */
.hero > *, .chapter > *, .topic > *, .steps > .step > * { max-width: var(--measure); }
.chapter > .span-wide, .topic > .span-wide, .chapter > .scrolly { max-width: none; }

.tour-toc { display: none; }

@media (min-width: 1440px) {
  .tour-layout { grid-template-columns: 13.5rem minmax(0, 1fr); column-gap: 3rem; }
  .tour-main { margin: 0; }
  .tour-toc {
    display: block;
    position: sticky;
    top: calc(var(--header-height) + 1.5rem);
    align-self: start;
    max-height: calc(100vh - var(--header-height) - 3rem);
    overflow-y: auto;
    padding: 2.75rem 0.25rem 2rem 0;
    font-size: 0.86rem;
    scrollbar-width: thin;
  }
}

.toc-chapters > li > a { font-weight: 600; color: var(--text-2); }
.toc-chapters > li.is-current > a { color: var(--accent-text); border-left-color: var(--accent); }
.toc-steps { list-style: none; margin: 0.1rem 0 0.6rem; padding: 0 0 0 0.6rem; display: none; }
.toc-chapters > li.is-current > .toc-steps { display: block; }
.toc-steps a { display: block; padding: 0.14rem 0 0.14rem 0.75rem; color: var(--muted); text-decoration: none; font-size: 0.82rem; line-height: 1.35; border-left: 2px solid transparent; margin-left: -1px; }
.toc-steps a:hover { color: var(--text); }
.toc-steps a[aria-current="true"] { color: var(--text); border-left-color: var(--accent-line); }
.toc-steps code { font-size: 0.95em; background: none; border: 0; padding: 0; }

/* ------------------------------------------------------------ hero and chapters */

.one-page .hero { padding: 2rem 0 1rem; }
.one-page .hero h1 { font-size: clamp(2.3rem, 1.6rem + 3vw, 3.6rem); max-width: 18ch; line-height: 1.06; margin-bottom: 1.25rem; }
.one-page .hero .lede { max-width: 42rem; }

.hero-actions { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 1.5rem; }

.button {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font: inherit;
  font-size: 0.92rem;
  font-weight: 600;
  line-height: 1.2;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
}
.button:hover { background: var(--surface-2); }
.button.primary { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.button.primary:hover { background: color-mix(in srgb, var(--accent) 88%, var(--text)); }
.button:disabled { opacity: 0.5; cursor: default; }

.chapter { padding-top: 1rem; }
.chapter + .chapter { margin-top: 5rem; border-top: 1px solid var(--border); padding-top: 4rem; }
.chapter-intro { margin-bottom: 2.5rem; }
.chapter-intro h2 { font-size: clamp(1.9rem, 1.5rem + 1.6vw, 2.6rem); letter-spacing: -0.025em; margin: 0 0 1rem; }
.chapter-intro .lede { margin-bottom: 0; }

.chapter h3 { font-size: clamp(1.3rem, 1.15rem + 0.6vw, 1.55rem); margin-top: 3.5rem; margin-bottom: 1rem; }
.chapter h4 { font-size: 1.12rem; margin-top: 2.25rem; }
.topic:first-of-type > h3:first-child, .chapter > .topic:first-child > h3:first-child { margin-top: 0; }
.chapter-intro + .topic > h3:first-child { margin-top: 0; }
#overview > h3:first-child { margin-top: 1rem; }

.closing .closing-links { display: flex; flex-wrap: wrap; gap: 0.6rem; }

/* Wide figures inside prose sections may use the full tour width. */
.topic > figure.diagram, .topic > figure.chart, .topic > .table-wrap { max-width: none; }
.one-page .diagram:not(.compact) { margin-left: 0; }

/* ------------------------------------------------------------ scrolly: steps and the trace panel */

.scrolly { position: relative; }
.step { padding: 0.25rem 0 0.75rem; }
.step > h3:first-child { margin-top: 3rem; }
.steps > .step:first-child > h3:first-child { margin-top: 0; }

.trace-inner {
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem 1.1rem 1.1rem;
  box-shadow: 0 10px 30px -18px rgba(0, 0, 0, 0.35);
}

.trace-title {
  margin: 0 0 0.85rem;
  font-size: 0.74rem;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.trace-title code { text-transform: none; letter-spacing: 0; font-size: 1.05em; }

.flow { list-style: none; margin: 0 0 1rem; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.flow > li { margin: 0; position: relative; }
.flow > li + li::before {
  content: "";
  position: absolute;
  left: 1.15rem;
  top: -0.5rem;
  height: 0.5rem;
  border-left: 1.5px solid var(--border-strong);
}

.flow-node {
  display: block;
  padding: 0.38rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: 0.8rem;
  line-height: 1.3;
  cursor: pointer;
  transition: background-color 0.25s, border-color 0.25s, color 0.25s, box-shadow 0.25s, opacity 0.25s;
}
.flow-node code { font-size: 0.95em; background: none; border: 0; padding: 0; color: inherit; }
.flow-file { display: block; font-family: var(--font-mono); font-size: 0.68rem; color: var(--muted); margin-top: 0.1rem; }
.flow-alt { border-style: dashed; }
.flow-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem; }
.flow-fan { display: grid; grid-template-columns: repeat(auto-fill, minmax(6.4rem, 1fr)); gap: 0.4rem; }
.flow-node:hover { border-color: var(--border-strong); }
.flow-node.is-active {
  background: color-mix(in srgb, var(--accent) 18%, var(--surface));
  border-color: var(--accent);
  color: var(--text);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}
.flow-node.is-active .flow-file { color: var(--accent-text); }

.trace-bytes { display: flex; flex-wrap: wrap; gap: 0.45rem 0.4rem; margin: 0 0 0.75rem; }
.trace-group { display: inline-flex; flex-direction: column; gap: 0.2rem; }
.trace-group-bytes { display: inline-flex; gap: 2px; }
.trace-group-label { font-size: 0.66rem; color: var(--muted); white-space: nowrap; transition: color 0.25s; }
.trace-byte {
  font-family: var(--font-mono);
  font-size: 0.76rem;
  min-width: 1.6rem;
  text-align: center;
  padding: 0.22rem 0.1rem;
  border-radius: 5px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  color: var(--muted);
  transition: background-color 0.25s, border-color 0.25s, color 0.25s;
}
.trace-byte.is-active { background: color-mix(in srgb, var(--hot) 22%, var(--surface)); border-color: var(--hot); color: var(--text); font-weight: 650; }
.trace-group.is-active .trace-group-label { color: var(--hot); font-weight: 600; }

.trace-output { margin: 0 0 0.6rem; font-size: 0.85rem; }
.trace-output code { background: var(--code-bg); }
.trace-output [data-out] { border-radius: 3px; transition: background-color 0.25s, color 0.25s; }
.trace-output [data-out].is-active { background: var(--hot-soft); color: var(--hot); font-weight: 650; }

.trace-note { margin: 0; font-size: 0.86rem; line-height: 1.5; color: var(--text-2); min-height: 4.5em; }

.step.is-current > h3:first-child { color: var(--text); }
.step > h3:first-child { transition: color 0.3s; }
.js .step:not(.is-current) > h3:first-child { color: var(--text-2); }

@media (min-width: 1100px) {
  .scrolly {
    display: grid;
    grid-template-columns: minmax(0, var(--measure)) minmax(18rem, var(--trace-width));
    column-gap: 3rem;
    align-items: start;
  }
  .trace { position: sticky; top: calc(var(--header-height) + 1.5rem); max-height: calc(100vh - var(--header-height) - 2.5rem); overflow-y: auto; }
}

/* Narrow screens: the trace becomes a compact bar pinned to the bottom of
   the viewport while its chapter is on screen. */
@media (max-width: 1099.98px) {
  .trace { position: sticky; bottom: 0.5rem; z-index: 15; margin-top: 1rem; }
  .trace-inner { padding: 0.6rem 0.75rem 0.7rem; backdrop-filter: blur(8px); background: color-mix(in srgb, var(--surface) 92%, transparent); }
  .trace .flow, .trace .trace-group-label, .trace .trace-output, .trace .trace-title { display: none; }
  .trace-bytes { gap: 0.25rem; margin-bottom: 0.35rem; }
  .trace-byte { min-width: 1.35rem; font-size: 0.68rem; padding: 0.12rem 0; }
  .trace-note { font-size: 0.76rem; line-height: 1.4; min-height: 0; }
}

/* ------------------------------------------------------------ playground */

.playground {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  overflow: hidden;
  box-shadow: 0 18px 40px -30px rgba(0, 0, 0, 0.45);
}
@media (min-width: 960px) {
  .playground { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); }
  .pg-input { border-right: 1px solid var(--border); }
}
.pg-panel { padding: 1.1rem 1.25rem 1.3rem; min-width: 0; }
.pg-input { background: var(--surface-2); }

.pg-modes { display: inline-flex; padding: 3px; gap: 3px; border-radius: 999px; background: var(--surface-3); margin-bottom: 1rem; }
.pg-modes button {
  font: inherit; font-size: 0.86rem; font-weight: 600;
  border: 0; border-radius: 999px; padding: 0.35rem 0.85rem;
  background: transparent; color: var(--text-2); cursor: pointer;
}
.pg-modes button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }

.pg-label { display: block; margin: 0.9rem 0 0.35rem; font-size: 0.74rem; font-weight: 650; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); }
.pg-label:first-child { margin-top: 0; }
.pg-sub { text-transform: none; letter-spacing: 0; font-weight: 400; margin-left: 0.4rem; }
.pg-select, .pg-textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
}
.pg-select { padding: 0.45rem 0.55rem; font-size: 0.92rem; }
.pg-textarea { font-family: var(--font-mono); font-size: 0.86rem; line-height: 1.55; padding: 0.6rem 0.7rem; resize: vertical; min-height: 8.5rem; background: var(--code-bg); }
.pg-hint { font-size: 0.8rem; color: var(--muted); margin: 0.5rem 0 0.75rem; line-height: 1.5; }
.pg-options { border: 0; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 0.4rem 1.2rem; font-size: 0.88rem; }
.pg-options legend { padding: 0; margin-bottom: 0.35rem; font-size: 0.74rem; font-weight: 650; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); }
.pg-options label { display: inline-flex; align-items: center; gap: 0.4rem; cursor: pointer; }
.pg-options input { accent-color: var(--accent); width: 1rem; height: 1rem; }

.pg-status { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 1rem; min-height: 1.8rem; }
.pg-chip { font-size: 0.82rem; padding: 0.2rem 0.65rem; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-2); }
.pg-chip strong { color: var(--text); }
.pg-chip.good { background: var(--good-soft); }
.pg-chip.bad { background: var(--hot-soft); border-color: var(--hot); color: var(--text); }
.pg-chip code { background: none; border: 0; padding: 0; }

.pg-bytes { display: flex; flex-wrap: wrap; gap: 3px; font-family: var(--font-mono); font-size: 0.8rem; max-height: 15rem; overflow-y: auto; padding: 2px; }
.pg-byte {
  min-width: 1.75rem;
  text-align: center;
  padding: 0.2rem 0;
  border-radius: 5px;
  border: 1px solid transparent;
  cursor: default;
  outline: none;
}
.pg-byte.is-header { font-weight: 650; }
.pg-byte.is-payload { opacity: 0.85; }
.pg-byte.is-error { background: var(--hot-soft) !important; border-color: var(--hot); color: var(--hot) !important; }
.pg-byte.is-unread { background: var(--surface-2); color: var(--muted); border-style: dashed; border-color: var(--border-strong); }
.pg-byte.is-linked { border-color: var(--text); box-shadow: 0 0 0 1px var(--text); opacity: 1; }
.pg-more { align-self: center; font-family: var(--font-sans); font-size: 0.8rem; color: var(--muted); padding: 0 0.4rem; }

.pg-items { list-style: none; margin: 0; padding: 0; font-size: 0.84rem; max-height: 18rem; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; }
.pg-items li { margin: 0; display: grid; grid-template-columns: minmax(6.5rem, 11rem) minmax(0, 1fr); gap: 0.75rem; padding: 0.22rem 0.6rem 0.22rem calc(0.6rem + var(--depth, 0) * 1.1rem); border-bottom: 1px solid var(--border); cursor: default; }
.pg-items li:last-child { border-bottom: 0; }
.pg-items li.is-linked { background: var(--accent-soft); }
.pg-items .pg-item-hex { font-family: var(--font-mono); font-size: 0.78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pg-items .pg-item-text { min-width: 0; overflow-wrap: anywhere; }
.pg-items .pg-role { color: var(--muted); margin-right: 0.35rem; }
.pg-items .pg-kind { color: var(--muted); }
.pg-items li.is-error { background: var(--hot-soft); color: var(--text); }
.pg-items li.pg-empty { display: block; color: var(--muted); }

.pg-value { margin: 0; max-height: 16rem; overflow: auto; }
.pg-value code { white-space: pre; }

.major-0 { background: var(--major-0); color: var(--major-0-ink); }
.major-1 { background: var(--major-1); color: var(--major-1-ink); }
.major-2 { background: var(--major-2); color: var(--major-2-ink); }
.major-3 { background: var(--major-3); color: var(--major-3-ink); }
.major-4 { background: var(--major-4); color: var(--major-4-ink); }
.major-5 { background: var(--major-5); color: var(--major-5-ink); }
.major-6 { background: var(--major-6); color: var(--major-6-ink); }
.major-7 { background: var(--major-7); color: var(--major-7-ink); }
.pg-items .pg-item-hex.major-0, .pg-items .pg-item-hex.major-1, .pg-items .pg-item-hex.major-2, .pg-items .pg-item-hex.major-3,
.pg-items .pg-item-hex.major-4, .pg-items .pg-item-hex.major-5, .pg-items .pg-item-hex.major-6, .pg-items .pg-item-hex.major-7 {
  background: none;
}

/* ------------------------------------------------------------ streaming demo */

.stream-demo {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  padding: 1.1rem 1.25rem 1.2rem;
  margin: 0 0 3rem;
}
.stream-head { display: block; margin-bottom: 1rem; }
.stream-title { display: block; font-weight: 650; }
.stream-sub { display: block; font-size: 0.9rem; color: var(--text-2); }
.stream-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem 1.25rem; margin-bottom: 1rem; font-size: 0.9rem; }
.stream-controls input[type="range"] { flex: 1 1 12rem; accent-color: var(--accent); }
.stream-controls output { font-family: var(--font-mono); font-weight: 650; }
.stream-buttons { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.stream-buttons .button { font-size: 0.85rem; padding: 0.4rem 0.8rem; }

.stream-bytes { display: flex; flex-wrap: wrap; row-gap: 6px; font-family: var(--font-mono); font-size: 0.64rem; margin-bottom: 0.6rem; }
.stream-byte {
  width: 1.45rem;
  height: 1.55rem;
  display: inline-grid;
  place-items: center;
  color: var(--text-2);
  border-top: 2px solid transparent;
  opacity: 0.42;
  transition: opacity 0.2s, background-color 0.2s;
}
.stream-byte.item-a { background: var(--accent-soft); }
.stream-byte.item-b { background: color-mix(in srgb, var(--accent-soft) 55%, var(--surface-2)); }
.stream-byte.is-split { background: var(--hot-soft); border-top-color: var(--hot); }
.stream-byte.item-start { margin-left: 3px; border-top-left-radius: 4px; border-bottom-left-radius: 4px; }
.stream-byte.item-end { border-top-right-radius: 4px; border-bottom-right-radius: 4px; }
.stream-byte.chunk-start { box-shadow: inset 2px 0 0 var(--text); }
.stream-byte.is-pushed { opacity: 1; color: var(--text); }
.stream-byte.is-latest { outline: 1px solid var(--accent); outline-offset: -1px; }

.legend-swatch.stream-swatch-whole { background: var(--accent-soft); border: 1px solid var(--accent-line); }
.legend-swatch.stream-swatch-split { background: var(--hot-soft); border: 1px solid var(--hot); }
.legend-swatch.stream-swatch-boundary { background: var(--text); width: 3px; }
.legend-swatch.stream-swatch-pushed { background: var(--surface-3); border: 1px solid var(--border-strong); }
.stream-legend { margin-bottom: 1rem; }

.stream-log-wrap { max-height: 16rem; overflow-y: auto; margin-bottom: 0.75rem; }
.stream-log { font-size: 0.85rem; margin: 0; }
.stream-log td, .stream-log th { padding-top: 0.3rem; padding-bottom: 0.3rem; }
.stream-log tr.is-latest td { background: var(--accent-soft); }
.stream-log .muted { color: var(--muted); }
.stream-summary { font-size: 0.92rem; margin-bottom: 0.5rem; min-height: 1.5em; }

@media (prefers-reduced-motion: reduce) {
  .flow-node, .trace-byte, .trace-group-label, .trace-output [data-out], .stream-byte, .step > h3:first-child { transition: none; }
}

@media (forced-colors: active) {
  .flow-node.is-active, .trace-byte.is-active, .pg-byte.is-linked { outline: 2px solid Highlight; }
  .stream-byte.chunk-start { border-left: 2px solid CanvasText; }
}
