/* Procedural Generation — layout and type. Content lives in content.js. */

:root {
  /* Same as the course homepage (docs/src/theme/tokens.css). */
  --paper: #ffffff;
  --ink: #111111;
  --muted: #6f6f6f;
  --faint: #c8c8c8;
  --line: #e3e3e3; /* soft dividers inside controls */
  --hover: #f2f2f2; /* button hover */
  --code-bg: #2b2b2b; /* code panels: softer than black */
  --code-line: #424242; /* dividers inside code panels */
  --radius: 6px;

  --sans: "Helvetica Neue", Helvetica, Arial, system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --bar-h: 2.75rem;
  --pad: calc(clamp(0.875rem, 2.4vw, 2.5rem) * var(--pad-scale, 1));
  --gutter: clamp(0.75rem, 1.5vw, 1.75rem);
  --measure: 36em;
}

/* Scales up on wide screens so the projector stays readable. */
html {
  font-size: calc(clamp(14px, 0.45vw + 10px, 19px) * var(--text-scale, 1));
  scroll-behavior: smooth;
  scroll-padding-top: var(--bar-h);
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

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

body {
  margin: 0;
  padding-top: var(--bar-h);
  background: var(--paper);
  color: var(--ink);
  font: 400 1rem/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: inherit; }

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

::selection { background: var(--ink); color: var(--paper); }

/* --- Persistent bar ------------------------------------------------------ */

.bar {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 10;
  height: var(--bar-h);
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: var(--gutter);
  padding: 0 var(--pad);
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.bar-title {
  align-self: center;
  font-weight: 700;
  font-size: 0.875rem;
  letter-spacing: -0.01em;
  text-decoration: none;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.2s;
}

.is-scrolled .bar-title { opacity: 1; }

.bar-nav {
  display: flex;
  gap: clamp(0.75rem, 2vw, 2rem);
  height: 100%;
  margin: 0;
  padding: 0;
  list-style: none;
}

.bar-nav a {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.5em;
  height: 100%;
  font: 0.85rem/1 var(--sans);
  text-decoration: none;
  color: var(--muted);
  white-space: nowrap;
}

.bar-nav a:hover,
.bar-nav a[aria-current] { color: var(--ink); }

/* The current section: a thin line on the bar's soft bottom edge. */
.bar-nav a[aria-current]::after {
  content: "";
  position: absolute;
  inset: auto 0 -1px 0;
  height: 1px;
  background: var(--ink);
}

@media (max-width: 1100px) {
  .bar-name { display: none; }
}

/* --- Masthead ------------------------------------------------------------ */

.masthead {
  display: flex;
  align-items: flex-end;
  min-height: calc(62svh - var(--bar-h));
  padding: var(--pad);
}

/* With a cover: the picture fills most of the first screen, inside the page
   margins, behind the title. */
.masthead.has-cover {
  position: relative;
  min-height: calc(100svh - var(--bar-h));
}

.masthead-cover {
  position: absolute;
  inset: var(--pad);
  z-index: 0;
}

.masthead-cover > canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

/* Centered in the open space of the cover's bottom two rows (half the area
   inside the margins); the title is two lines, about 1.9em tall. */
.has-cover .masthead-title {
  position: relative;
  z-index: 1;
  margin-bottom: max(0px, calc((50svh - var(--bar-h) / 2 - var(--pad) - 1.9em) / 2));
}

.masthead-title {
  margin: 0;
  font-size: clamp(2.5rem, 6.5vw, 7rem);
  font-weight: 600;
  line-height: 0.95;
  letter-spacing: -0.03em;
  max-width: 9em;
}

/* --- Sections ------------------------------------------------------------ */

.section {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  column-gap: var(--gutter);
  align-items: start;
  align-content: start;
  min-height: calc(50svh - var(--bar-h)); /* a section can be made taller in Edit mode */
  padding: calc(var(--pad) * 1.1) var(--pad) calc(var(--pad) * 1.75);
}

/* Column split comes from layout.numberColumns in content.js (set by app.js). */
.section-num {
  grid-column: 1 / span var(--num-cols, 5);
  position: sticky;
  top: calc(var(--bar-h) + var(--pad) * 1.1);
  font-size: calc(clamp(3.25rem, 8vw, 8rem) * var(--num-scale, 1));
  font-weight: 700;
  line-height: 0.76;
  letter-spacing: -0.05em;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  user-select: none;
}

/* Content never grows wider than 9 of the 12 columns: a narrower number
   column leaves room on the right instead of enlarging everything. */
.section-body {
  grid-column: var(--body-start, 6) / -1;
  min-width: 0;
  max-width: calc((100vw - 2 * var(--pad) - 11 * var(--gutter)) / 12 * 9 + 8 * var(--gutter));
}

.section-title {
  margin: 0 0 clamp(1rem, 2.4vw, 2rem);
  font-size: clamp(1.5rem, 2.6vw, 2.75rem);
  font-weight: 600;
  line-height: 1.05;
  letter-spacing: -0.02em;
}

@media (max-width: 760px) {
  .section-num,
  .section-body { grid-column: 1 / -1; }

  .section-num {
    position: static;
    margin-bottom: 1.25rem;
    font-size: 4.5rem;
  }
}

/* --- Blocks -------------------------------------------------------------- */

.block + .block { margin-top: calc(clamp(1.25rem, 2.2vw, 2.25rem) * var(--block-space, 1)); }

figure { margin: 0; }

.caption {
  margin-top: 0.75rem;
  max-width: var(--measure);
  font: 0.85rem/1.45 var(--sans);
  color: var(--muted);
}

code {
  font-family: var(--mono);
  font-size: 0.9em;
}

/* Text */
.text { max-width: var(--measure); font-size: 1.0625rem; }
.text p { margin: 0; }
.text p + p { margin-top: 0.9em; }
.text a { text-decoration-thickness: 1px; text-underline-offset: 0.18em; }
.text a:hover { text-decoration-thickness: 2px; }

/* Image */
.image img {
  display: block;
  width: 100%;
  height: auto;
  background: var(--faint);
}

/* Link — consecutive links stack into one ruled list */
.link {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--gutter);
  padding: 0.9rem 0;
  border-top: 1px solid var(--ink);
  border-bottom: 1px solid var(--ink);
  text-decoration: none;
}

.block-link + .block-link { margin-top: -1px; }

.link-text {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-width: 0;
}

.link-description {
  max-width: var(--measure);
  font-size: 0.875rem;
  line-height: 1.45;
  color: var(--muted);
}

/* With a preview image: image, then title and description, then site. */
.link.has-image { align-items: start; }

.link-image {
  flex-shrink: 0;
  width: 9rem;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  background: var(--faint);
}

.link.has-image .link-text { flex: 1; }

.link-label {
  font-size: 1.25rem;
  font-weight: 500;
  line-height: 1.25;
  letter-spacing: -0.01em;
}

.link-meta {
  flex-shrink: 0;
  font: 0.85rem/1 var(--sans);
  color: var(--muted);
}

.link:hover .link-label {
  text-decoration: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: 0.18em;
}

/* Code */
.code-head {
  padding: 0.5rem 1.25rem;
  background: var(--code-bg);
  border-bottom: 1px solid var(--code-line);
  border-radius: var(--radius) var(--radius) 0 0;
  font: 500 0.8rem/1 var(--sans);
  color: #9a9a9a;
}

.code-body {
  margin: 0;
  padding: 1.25rem;
  overflow-x: auto;
  background: var(--code-bg);
  border-radius: var(--radius);
  color: var(--paper);
  font: 0.875rem/1.6 var(--mono);
  tab-size: 2;
}

.code-body code { font-size: inherit; }
.code-head + .code-body { border-radius: 0 0 var(--radius) var(--radius); }

/* Placeholder */
.placeholder {
  display: flex;
  align-items: baseline;
  gap: 1rem;
  max-width: var(--measure);
  padding: 0.75rem 1rem;
  border: 1px dashed var(--muted);
  font: 0.9rem/1.4 var(--sans);
  color: var(--muted);
}

.placeholder-tag {
  flex-shrink: 0;
  font-size: 0.7rem;
  color: var(--ink);
}

/* --- Demos --------------------------------------------------------------- */

.demo { border: 1px solid var(--ink); }

.demo-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 2.25rem;
  padding: 0 0 0 0.9rem;
  border-bottom: 1px solid var(--line);
  font: 500 0.85rem/1 var(--sans);
}

.demo-reset {
  align-self: stretch;
  padding: 0 0.9rem;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font: 400 0.85rem/1 var(--sans);
  cursor: pointer;
}

.demo-reset:hover { background: var(--hover); color: var(--ink); }

.demo-stage {
  position: relative;
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.demo-stage > canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.demo-controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  border-top: 1px solid var(--ink);
}

.control {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 0.9rem;
  border-right: 1px solid var(--line);
}

.control-head {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  font: 500 0.85rem/1 var(--sans);
}

.control-value { color: var(--muted); font-variant-numeric: tabular-nums; }

.control input[type="range"] { width: 100%; margin: 0; accent-color: var(--ink); }
.control input[type="checkbox"] { width: 1.1rem; height: 1.1rem; margin: 0; accent-color: var(--ink); }

.control select {
  padding: 0.3rem 0.4rem;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
  font: 0.85rem var(--sans);
}

/* Empty slot */
.demo.is-empty { border-style: dashed; border-color: var(--muted); }
.demo.is-empty .demo-bar { border-bottom: 1px dashed var(--muted); color: var(--muted); }

.demo-x {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  stroke: var(--faint);
  stroke-width: 1;
}

.demo-x line { vector-effect: non-scaling-stroke; }

.demo-empty {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 0.4rem 0.7rem;
  background: var(--paper);
  font: 0.85rem/1 var(--sans);
  color: var(--muted);
  white-space: nowrap;
}

/* --- Galleries ----------------------------------------------------------- */

.block-heading {
  margin: 0 0 1rem;
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.gallery .caption { margin: -0.5rem 0 1.25rem; }

/* One row when there's room (auto-fit stretches the items across it). */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(var(--tile-min, 6.5rem), 1fr));
  gap: 1.25rem var(--tile-gap, 0.5rem);
  margin: 0;
  padding: 0;
  list-style: none;
}

.gallery-item {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

/* No border: the drawings sit on the page like the title-screen trees. */
.gallery-thumb {
  display: block;
  aspect-ratio: 1;
}

.gallery-thumb > canvas { display: block; width: 100%; height: 100%; }

/* Hover blurs (sliders in Edit mode set the variables):
   hovering a tile blurs the other tiles in its row; hovering a row, or the
   space beside it in the left column, blurs the other rows. */
.gallery-grid li,
.blocks > .block { transition: filter var(--blur-speed, 0.2s) ease-out; }

.gallery-grid:not(.is-dense):has(.gallery-item:hover, .gallery-item:focus-visible) li:not(:has(.gallery-item:hover, .gallery-item:focus-visible)) {
  filter: blur(var(--tile-blur, 1px));
}

/* Not for the dense reference grids (the toolbox): no blur from them or on them. */
.blocks:has(> .block-gallery:not(.is-dense):hover) > .block:not(:hover, .is-dense) {
  filter: blur(var(--row-blur, 1.5px));
}

/* The hover area beside each row: the left column, level with the row. */
@media (min-width: 761px) {
  .block-gallery::before {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    right: 100%;
    width: calc((100vw - 2 * var(--pad)) * var(--num-cols, 5) / 12);
  }
}

@media (prefers-reduced-motion: reduce) {
  .gallery-grid li, .blocks > .block { transition: none; }
}

.gallery-label {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding-top: 0.45rem;
}

.gallery-name { font-size: 0.9rem; font-weight: 500; }

.gallery-meta {
  overflow: hidden;
  font-size: 0.8rem;
  line-height: 1.3;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.gallery-missing { font-size: 0.85rem; color: var(--muted); }

/* --- Viewer: drawing left, code right ------------------------------------ */

body:has(.viewer[open]) { overflow: hidden; }

.viewer {
  width: 100vw;
  height: 100vh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: var(--paper);
  color: var(--ink);
}

.viewer::backdrop { background: var(--paper); }
.viewer:focus { outline: none; }

.viewer[open] {
  display: grid;
  grid-template-rows: var(--bar-h) minmax(0, 1fr);
}

.viewer-bar {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: var(--gutter);
  padding: 0 0.5rem 0 var(--pad);
  border-bottom: 1px solid var(--line);
}

.viewer-heading {
  display: flex;
  align-items: baseline;
  align-self: center;
  gap: 1rem;
  min-width: 0;
  white-space: nowrap;
}

/* Interface text is the sans; monospace is kept for code (the rule, the symbol strip, the code). */
.viewer-count { font-size: 0.875rem; font-variant-numeric: tabular-nums; color: var(--muted); }
.viewer-meta { font-size: 0.875rem; color: var(--muted); }

/* Rules and other code in the gallery labels. */
.gallery-meta.is-code { font: 0.7rem/1.3 var(--mono); }
.viewer-meta.is-code { font: 0.75rem/1 var(--mono); }

.viewer-meta { overflow: hidden; text-overflow: ellipsis; }

.viewer-title { font-weight: 600; }

.viewer-actions { display: flex; align-items: center; gap: 0.25rem; }

.viewer-button,
.viewer-hide,
.viewer-reveal {
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font: 400 0.875rem/1 var(--sans);
  cursor: pointer;
}

.viewer-button {
  padding: 0.55rem 0.85rem;
  color: var(--muted);
}

.viewer-button:hover { background: var(--hover); color: var(--ink); }

.viewer-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  min-height: 0;
}

.viewer-left {
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* the long symbol strip mustn't widen it */
  grid-template-rows: minmax(0, 1fr) auto;
  min-height: 0;
}

.viewer-stage { position: relative; min-height: 0; }
.viewer-stage > canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

.viewer-code {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  background: var(--code-bg);
  color: var(--paper);
}

.viewer-code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 2.25rem;
  padding: 0 0 0 1.25rem;
  border-bottom: 1px solid var(--code-line);
  font: 500 0.8rem/1 var(--sans);
  color: #9a9a9a;
}

.viewer-hide { align-self: stretch; padding: 0 1.1rem; color: #9a9a9a; }
.viewer-hide:hover { color: var(--paper); }

.viewer-pre {
  position: relative; /* lines' offsetTop is measured from here */
  margin: 0;
  padding: 1.25rem 1.25rem 2rem 0;
  overflow: auto;
  font: 0.8rem/1.6 var(--mono);
  tab-size: 2;
  counter-reset: line;
}

.code-line {
  display: block;
  min-height: 1.6em;
  white-space: pre;
}

.code-line::before {
  counter-increment: line;
  content: counter(line);
  display: inline-block;
  width: 3.25em;
  padding-right: 1.25em;
  text-align: right;
  color: #6a6a6a;
  user-select: none;
}

.code-comment { color: #8f8f8f; }

/* While the player runs: the function at work, and the line doing it. */
.code-line.in-block { background: #333; }
.code-line.is-current { background: #4a4a4a; }
.code-line.is-current::before { color: var(--paper); }

/* --- Player, under the drawing ------------------------------------------- */

.viewer-player {
  border-top: 1px solid var(--line);
  font: 400 0.875rem/1 var(--sans);
}

.viewer-player[hidden] { display: none; }

/* The symbol string, with the one being read marked. */
.viewer-strip {
  overflow: hidden;
  min-height: 2.5rem;
  padding: 0.5rem var(--pad) 0.85rem;
  border-bottom: 1px solid var(--line);
  font: 400 0.85rem/1 var(--mono);
  letter-spacing: 0.05em;
  white-space: nowrap;
  color: var(--muted);
}

.viewer-strip mark {
  padding: 0.15em 0.2em;
  background: var(--ink);
  color: var(--paper);
}

.viewer-controls {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  min-height: 2.75rem;
  padding: 0 calc(var(--pad) - 0.85rem);
}

.viewer-control {
  padding: 0.55rem 0.85rem;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

.viewer-control:hover { background: var(--hover); }

.viewer-speed-label {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-left: 0.75rem;
  color: var(--muted);
}

.viewer-speed { width: 7rem; accent-color: var(--ink); }

.viewer-caption {
  display: block;
  min-height: 2rem;
  padding: 0.65rem var(--pad) 0;
  font-weight: 400;
  color: var(--muted);
  white-space: nowrap;
}

/* Blurred until revealed */
.viewer-reveal { display: none; }

.viewer-pre { transition: filter 0.45s ease-out; } /* the blur clears gently */

.viewer-code.is-hidden .viewer-pre {
  filter: blur(7px);
  user-select: none;
}

@media (prefers-reduced-motion: reduce) {
  .viewer-pre { transition: none; }
}

.viewer-code.is-hidden .viewer-hide { visibility: hidden; }

/* The whole panel is the button; the label sits in the middle. */
.viewer-code.is-hidden .viewer-reveal {
  display: grid;
  place-items: center;
  position: absolute;
  inset: 0;
  width: 100%;
  color: var(--paper);
}

/* Plain text over the blur; brightens a little on hover. */
.viewer-reveal span {
  font-size: 1rem;
  opacity: 0.8;
  transition: opacity 0.15s;
}

.viewer-reveal:hover span { opacity: 1; }

@media (max-width: 760px) {
  .viewer-body {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  }
  .viewer-meta { display: none; }
}

/* --- Lists --------------------------------------------------------------- */

.list {
  max-width: var(--measure);
  margin: 0;
  padding-left: 1.1em;
  font-size: 1.0625rem;
  list-style: square;
}

.list li + li { margin-top: 0.5em; }

/* --- Steps: one demo in selectable steps, picture left, code right ------- */

/* Unboxed: no outer border or dividers; the code panel is the only fill. */
.steps-tabs {
  display: flex;
  gap: 0.25rem;
  margin: 0 0 0.75rem -0.6rem; /* line the first tab's text up with the content edge */
}

.steps-tab {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: -1px; /* the selected tab's underline sits on the divider */
  padding: 0.7rem 0.6rem;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font: 400 0.95rem/1 var(--sans);
  cursor: pointer;
}

.steps-tab-num { font-variant-numeric: tabular-nums; }
.steps-tab:hover { color: var(--ink); }
.steps-tab[aria-selected="true"] { border-bottom-color: var(--ink); color: var(--ink); }

/* The code takes the width of its longest line; the picture takes the rest. */
.steps-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  column-gap: calc(var(--gutter) * 1.5); /* room between the picture and the code */
  align-items: start; /* the code panel is as tall as its code, not the picture */
}

.steps-visual {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* Never taller than --stage-max-height: a wide column narrows the picture instead. */
.steps-stage {
  position: relative;
  width: min(100%, calc(var(--stage-max-height, 26rem) * var(--stage-ratio, 640 / 200)));
  aspect-ratio: var(--stage-ratio, 640 / 200);
}

.steps-controls select {
  max-width: 16rem;
  padding: 0.3rem 0.4rem;
  border: 1px solid var(--faint);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
  font: 0.9rem var(--sans);
}

.steps-stage > canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

.steps-controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  column-gap: var(--gutter);
  padding-right: var(--gutter);
}

.steps-controls .control { padding: 0.75rem 0; border: 0; }

.steps-controls[hidden] { display: none; }

.steps-caption {
  margin-top: auto;
  padding: 0.6rem var(--gutter) 0 0;
  font-size: 1rem;
  line-height: 1.4;
}

.steps-code {
  display: flex;
  flex-direction: column;
  min-width: 0;
  max-width: 55vw; /* sized by its longest line (steps.js) */
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--code-bg);
  color: var(--paper);
}

.steps-code-head {
  padding: 0.75rem 1.1rem;
  border-bottom: 1px solid var(--code-line);
  font: 500 0.8rem/1 var(--sans);
  color: #9a9a9a;
}

.steps-pre {
  margin: 0;
  padding: 1rem 1.25rem 1rem 0;
  overflow-x: auto;
  font: 0.8rem/1.6 var(--mono);
  counter-reset: line;
}

.steps-pre .code-line::before { width: 2.25em; padding-right: 1em; }

.steps-note {
  width: 0;
  min-width: 100%; /* wrap to the code's width, don't widen the panel */
  margin: auto 0 0;
  padding: 0.75rem 1.1rem;
  border-top: 1px solid var(--code-line);
  font-size: 0.8rem;
  line-height: 1.45;
  color: #9a9a9a;
}

.steps-missing { font-size: 0.85rem; color: var(--muted); }

@media (max-width: 760px) {
  .steps-body { grid-template-columns: minmax(0, 1fr); }
  .steps-tab { padding: 0.7rem 0.75rem; font-size: 0.85rem; }
}

/* Gallery titles sit in the number column, level with the bottom of their
   row (the tile labels), right-aligned so they stay clear of the big
   section number. */
@media (min-width: 761px) {
  .block-gallery { position: relative; }

  .block-gallery > .block-heading {
    position: absolute;
    bottom: 0;
    right: calc(100% + var(--gutter));
    width: 8.5em;
    margin: 0;
    font-size: 1rem;
    line-height: 1.25;
    text-align: right;
    text-wrap: balance;
  }
}

.nowrap { white-space: nowrap; }

/* --- Intro: blocks before section 01, in the content column -------------- */

/* A screen to itself: scrolling past the title shows only this. */
.intro {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  column-gap: var(--gutter);
  align-content: center;
  min-height: calc(100svh - var(--bar-h));
  padding: calc(var(--pad) * 2) var(--pad);
  scroll-margin-top: var(--bar-h);
}

/* --- Sketch: canvas left, editable code right ---------------------------- */

.sketch {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  column-gap: var(--gutter);
  align-items: start;
}

.sketch-visual { min-width: 0; }
.sketch-stage { position: relative; }
.sketch-stage > canvas { position: absolute; inset: 0; width: 100%; height: 100%; }

.sketch-message {
  min-height: 1.4em;
  margin: 0.4rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.sketch-message.is-error { color: var(--ink); }

.sketch-code {
  position: relative;
  transition: background-color 0.45s ease-out;
  display: flex;
  flex-direction: column;
  min-width: 24rem;
  max-width: 55vw;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--code-bg);
  color: var(--paper);
}

.sketch-code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 2.5rem;
  padding: 0 0.5rem 0 1.1rem;
  border-bottom: 1px solid var(--code-line);
  font: 500 0.8rem/1 var(--sans);
  color: #9a9a9a;
}

.sketch-reset {
  padding: 0.45rem 0.6rem;
  border: 0;
  background: transparent;
  color: #9a9a9a;
  font: 400 0.8rem/1 var(--sans);
  cursor: pointer;
}

.sketch-reset:hover { color: var(--paper); }
.sketch-reset[hidden] { display: none; }
.sketch-actions { display: flex; }

/* Hidden: the panel keeps its size but is blank white, with "Show code". */
.sketch-code > :not(.sketch-show) { transition: opacity 0.45s ease-out; }
.sketch-show { display: none; }

.sketch-code.is-hidden { background-color: var(--paper); }
.sketch-code.is-hidden > :not(.sketch-show) { opacity: 0; pointer-events: none; }

.sketch-code.is-hidden .sketch-show {
  display: grid;
  place-items: center;
  position: absolute;
  inset: 0;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 400 1rem/1 var(--sans);
  cursor: pointer;
  transition: color 0.15s;
}

.sketch-code.is-hidden .sketch-show:hover { color: var(--ink); }

@media (prefers-reduced-motion: reduce) {
  .sketch-code, .sketch-code > * { transition: none; }
}

.sketch-editor {
  display: block;
  min-width: 100%;
  max-width: 55vw;
  min-height: 12rem;
  margin: 0;
  padding: 1rem 1.1rem;
  border: 0;
  outline: none;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: var(--paper);
  caret-color: var(--paper);
  font: 0.8rem/1.6 var(--mono);
  tab-size: 2;
  white-space: pre;
  overflow-x: auto;
}

.sketch-editor[hidden] { display: none; }
.sketch-editor:focus { background: #262626; }

.sketch-note {
  width: 0;
  min-width: 100%; /* wrap to the code's width, don't widen the panel */
  margin: 0;
  padding: 0.75rem 1.1rem;
  border-top: 1px solid var(--code-line);
  font-size: 0.8rem;
  line-height: 1.45;
  color: #9a9a9a;
}

@media (max-width: 760px) {
  .intro > .section-body { grid-column: 1 / -1; }
  .sketch { grid-template-columns: minmax(0, 1fr); }
  .sketch-code { width: auto; max-width: none; }
}

/* --- Sketch animation ------------------------------------------------------ */

.sketch-player { margin-top: 0.25rem; }

.sketch-controls {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin-left: -0.6rem;
}

.sketch-control {
  padding: 0.5rem 0.6rem;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: 400 0.9rem/1 var(--sans);
  cursor: pointer;
}

.sketch-control:hover { background: var(--hover); }

.sketch-speed-label {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-left: 0.75rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.sketch-speed { width: 7rem; accent-color: var(--ink); }

/* Stop only makes sense while animating. */
.sketch:not(.is-animating) .sketch-control:nth-child(3) { display: none; }

.sketch-timeline-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.4rem;
}

.sketch-timeline { flex: 1; min-width: 0; margin: 0; accent-color: var(--ink); }

.sketch-counter {
  min-width: 5.5em;
  font: 0.75rem/1 var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  text-align: right;
}

.sketch-caption {
  min-height: 1.4em;
  margin: 0.5rem 0 0;
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap; /* one line */
}

.sketch-vars {
  min-height: 1.4em;
  margin: 0.25rem 0 0;
  font: 0.75rem/1.5 var(--mono);
  color: var(--muted);
  white-space: pre-wrap;
}

.sketch-caption:empty,
.sketch-vars:empty { display: none; }

/* Read-only, numbered code while animating. */
.sketch-pre {
  margin: 0;
  padding: 1rem 1.1rem 1rem 0;
  font: 0.8rem/1.6 var(--mono);
  counter-reset: line;
}

.sketch-pre[hidden] { display: none; }
.sketch-pre .code-line::before { width: 2.25em; padding-right: 1em; }
.code-annot { color: #a8a8a8; }

/* --- Procedural title: squares over the heading's transparent text ------- */

.masthead { position: relative; }
.masthead-title.is-procedural { color: transparent; }
.masthead-title.is-procedural::selection { color: transparent; background: rgba(0, 0, 0, 0.12); }

.title-canvas {
  position: absolute;
  z-index: 2;
  pointer-events: none;
}

/* While the title is being edited, show the real text instead. */
.masthead-title.is-procedural:focus { color: var(--ink); }
.masthead:has(.masthead-title:focus) .title-canvas { visibility: hidden; }

/* Switch between versions of the sketch's code. */
.sketch-switch { display: flex; gap: 0.25rem; margin-left: -0.5rem; }

.sketch-switch-button {
  padding: 0.45rem 0.5rem;
  border: 0;
  border-bottom: 1px solid transparent;
  background: transparent;
  color: #8a8a8a;
  font: 400 0.8rem/1 var(--sans);
  cursor: pointer;
}

.sketch-switch-button:hover { color: var(--paper); }
.sketch-switch-button[aria-pressed="true"] { color: var(--paper); border-bottom-color: var(--paper); }

/* Steps: per-step note and takeaway, toggles and the seed field. */
.steps-step-note,
.steps-takeaway {
  max-width: var(--measure);
  margin: 0.35rem var(--gutter) 0 0;
  font-size: 0.875rem;
  line-height: 1.45;
  color: var(--muted);
}

.steps-takeaway { color: var(--ink); }
.steps-step-note:empty,
.steps-takeaway:empty { display: none; }

.control.is-toggle { flex-direction: row; align-items: center; gap: 0.5rem; }
.control.is-toggle input { width: 1rem; height: 1rem; margin: 0; accent-color: var(--ink); }
.control.is-toggle .control-label { font-size: 0.9rem; }

.steps-seed-row { display: flex; align-items: center; gap: 0.5rem; }

.steps-seed {
  width: 5.5em;
  padding: 0.25rem 0.4rem;
  border: 1px solid var(--faint);
  border-radius: 0;
  font: 0.85rem var(--mono);
}

.steps-new-seed {
  padding: 0.3rem 0.4rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 400 0.9rem/1 var(--sans);
  cursor: pointer;
}

.steps-new-seed:hover { color: var(--ink); background: var(--hover); }

/* Dense galleries (the toolbox): small tiles in a grid of many rows. */
.gallery-grid.is-dense {
  grid-template-columns: repeat(auto-fill, minmax(var(--dense-tile, 5.5rem), 1fr));
  gap: 1rem var(--tile-gap, 0.5rem);
}

.gallery-grid.is-dense .gallery-name { font-size: 0.8rem; }
.gallery-grid.is-dense .gallery-meta { font-size: 0.65rem; }
.gallery-grid.is-dense .gallery-label { padding-top: 0.3rem; }

/* Steps: stage buttons under the tabs, and the reset button. */
.steps-stages {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem 0.75rem;
  margin: -0.35rem 0 0.75rem;
}

.steps-stages[hidden] { display: none; }
.steps-stages:empty { min-height: 1.45rem; } /* a step without stages keeps the row's height */

/* Notes under the code: room for three lines, so panels match across steps. */
.steps-note { min-height: calc(3 * 1.45em + 1.5rem); }

.steps-stage-button {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  padding: 0.3rem 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 400 0.85rem/1 var(--sans);
  cursor: pointer;
}

.steps-stage-button:hover,
.steps-stage-button[aria-pressed="true"] { color: var(--ink); }
.steps-stage-button[aria-pressed="true"] span:last-child { text-decoration: underline; text-underline-offset: 0.3em; }

.control.is-reset { justify-content: flex-end; }

.steps-reset {
  align-self: flex-start;
  padding: 0.3rem 0.4rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 400 0.9rem/1 var(--sans);
  cursor: pointer;
}

.steps-reset:hover { color: var(--ink); background: var(--hover); }

/* Toolbox grids span several rows: their titles sit level with the first row. */
@media (min-width: 761px) {
  .block-gallery.is-dense > .block-heading { top: 0; bottom: auto; }
}

/* --- Sketchbook: description beside an index sheet of numbered pieces ---- */

.sketchbook {
  display: grid;
  grid-template-columns: minmax(12rem, 1fr) minmax(0, 2.6fr);
  column-gap: calc(var(--gutter) * 1.5);
  align-items: start;
}

.sketchbook-title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.sketchbook-years { margin: 0.5rem 0 0; font-size: 0.85rem; color: var(--muted); }
.sketchbook-subtitle { margin: 1rem 0 0; font-size: 0.9rem; font-style: italic; line-height: 1.4; }

.sketchbook-description {
  margin-top: 1rem;
  font-size: 0.8rem;
  line-height: 1.55;
}

.sketchbook-description p { margin: 0; }
.sketchbook-description p + p { margin-top: 0.8em; }

/* The rows above and below the grid: small labels on a hairline. */
.sketchbook-rule-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--tile-gap, 0.5rem);
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--ink);
  font-size: 0.65rem;
  line-height: 1.3;
}

.sketchbook-rule-row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sketchbook-rule-row span:last-child { text-align: right; }

.sketchbook-rule-row.is-foot {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-top: 1rem;
  padding: 0.35rem 0 0;
  border-top: 1px solid var(--ink);
  border-bottom: 0;
  color: var(--muted);
}

.sketchbook-rule-row.is-foot span:nth-child(2) { text-align: center; }

.sketchbook-grid {
  display: grid;
  grid-template-columns: repeat(var(--sketch-columns, 4), minmax(0, 1fr));
  gap: 1rem var(--tile-gap, 0.75rem);
  margin: 0.75rem 0 0;
  padding: 0;
  list-style: none;
}

.sketchbook-item { display: flex; flex-direction: column; gap: 0.25rem; min-width: 0; }
.sketchbook-num { font-size: 0.65rem; }
.sketchbook-caption { font-size: 0.7rem; color: var(--muted); }

.sketchbook-frame {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: var(--sketch-ratio, 3 / 2); /* height is 2/3 of the width */
  overflow: hidden;
  border: 1px solid var(--line);
}

.sketchbook-frame > canvas,
.sketchbook-frame > img { position: absolute; inset: 0; width: 100%; height: 100%; }
.sketchbook-frame > img { object-fit: cover; }
.sketchbook-frame.is-code { cursor: pointer; }
.sketchbook-frame.is-code:hover { border-color: var(--faint); }
.sketchbook-frame.is-empty { border-style: dashed; border-color: var(--faint); }
.sketchbook-empty { font-size: 0.7rem; color: var(--muted); }

@media (max-width: 900px) {
  .sketchbook { grid-template-columns: minmax(0, 1fr); row-gap: 1.5rem; }
  .sketchbook-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Wide sections: number and title on the first row, then the content across
   the full page width (the body's children join the section's grid). */
@media (min-width: 761px) {
  .section.is-wide > .section-num { position: static; grid-row: 1; }
  .section.is-wide > .section-body { display: contents; }
  .section.is-wide .section-title { grid-column: var(--body-start, 6) / -1; grid-row: 1; align-self: start; margin-bottom: 0; }
  .section.is-wide .blocks,
  .section.is-wide .edit-add { grid-column: 1 / -1; }
  .section.is-wide .blocks { margin-top: clamp(1.5rem, 3vw, 2.5rem); }
  .section.is-wide .edit-style-button { grid-column: var(--body-start, 6) / -1; }
  .section.is-wide .edit-resize-x { display: none; }
}

/* Intro asides: a short note in the left three columns, and the sketch
   across the rest of the page, out to the right margin. */
.intro-aside {
  grid-column: 1 / 4;
  grid-row: 1;
  align-self: center;
  min-width: 0;
  padding-right: calc(var(--gutter) * 2);
}

.intro:has(.intro-aside) > .section-body {
  grid-column: 4 / -1;
  grid-row: 1;
  max-width: none;
}

.aside-title { margin: 0 0 0.5rem; font-size: 0.95rem; font-weight: 600; }
.aside-text, .aside-note { font-size: 0.8rem; line-height: 1.5; }
.aside-text p, .aside-note p { margin: 0; }
.aside-note { margin-top: 0.75rem; color: var(--muted); }

.aside-code {
  margin: 0.75rem 0 0;
  font: 0.72rem/1.6 var(--mono);
  white-space: pre;
  overflow-x: auto;
}

@media (max-width: 760px) {
  .intro-aside { grid-column: 1 / -1; grid-row: auto; padding: 0 0 1.5rem; }
  .intro:has(.intro-aside) > .section-body { grid-column: 1 / -1; grid-row: auto; }
}

/* A hidden aside: its space kept, only "Show rules" visible until opened. */
.aside { position: relative; }
.aside-content { transition: opacity 0.45s ease-out; }
.aside.is-hidden .aside-content { opacity: 0; pointer-events: none; }
.aside-show { display: none; }

.aside.is-hidden .aside-show {
  display: grid;
  place-items: center;
  position: absolute;
  inset: 0;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 400 1rem/1 var(--sans);
  cursor: pointer;
  transition: color 0.15s;
}

.aside.is-hidden .aside-show:hover { color: var(--ink); }

.aside-hide {
  margin: 0.75rem 0 0 -0.4rem;
  padding: 0.3rem 0.4rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 400 0.8rem/1 var(--sans);
  cursor: pointer;
}

.aside-hide:hover { color: var(--ink); background: var(--hover); }

@media (prefers-reduced-motion: reduce) {
  .aside-content { transition: none; }
}

/* Viewer transport: icon buttons, then the timeline across the rest. */
.viewer-control.is-icon {
  display: grid;
  place-items: center;
  width: 2.1rem;
  height: 2.1rem;
  padding: 0;
  color: var(--ink);
}

.viewer-control.is-icon svg { fill: currentColor; }
.viewer-control.is-play { width: 2.5rem; }

.viewer-timeline {
  flex: 1;
  min-width: 6rem;
  margin: 0 0.75rem 0 0.5rem;
  accent-color: var(--ink);
}

.viewer-counter {
  min-width: 7.5em;
  font: 0.75rem/1 var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  text-align: right;
}

.viewer-controls .viewer-speed { width: 5rem; }

/* Fine print: credits and asides. */
.text.is-small { max-width: none; font-size: 0.75rem; line-height: 1.45; color: var(--muted); white-space: nowrap; }
.text.is-small a { color: inherit; }
.block-heading + .block-text.text.is-small,
.block-heading + .text.is-small { margin-top: 0.35rem; }

/* Sketchbook starter: copy or download a standalone copy. */
.starter { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.starter-buttons { display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem 0.5rem; margin-left: -0.5rem; }

.starter-button {
  padding: 0.4rem 0.5rem;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: 500 0.85rem/1 var(--sans);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.25em;
  cursor: pointer;
}

.starter-button:hover { background: var(--hover); }
.starter-status { font-size: 0.75rem; color: var(--muted); }
.starter-note { margin: 0.5rem 0 0; font-size: 0.75rem; line-height: 1.5; color: var(--muted); }
