/* ============================================================
   Scrollytelling section — terminal, source, rendered widgets.

   Base styles describe the flat, static layout. The pinned,
   scroll-scrubbed behaviour lives entirely under `.is-driven`,
   which JS adds — so without JS the section degrades to a
   readable panel rather than an empty 640vh gap.

   Colour rule: the aurora gradient is reserved for the mark and
   the page background. Controls use the deep ramp.
   ============================================================ */

.scrolly {
  position: relative;
}

.scrolly__stage {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-5) var(--space-3) var(--space-4);
  max-width: var(--max-width);
  margin: 0 auto;
}

/* ---- progress rail ---- */
.scrolly__rail {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 30;
  height: 2px;
  width: 100%;
  transform: scaleX(0);
  transform-origin: 0 50%;
  background: var(--deep-indigo);
  box-shadow: 0 0 10px #3d3dbb80;
}

/* ---- beat header ---- */
.beat {
  display: flex;
  align-items: baseline;
  gap: 0.9rem;
  font-family: var(--font-mono);
}

.beat__label {
  display: flex;
  align-items: baseline;
  gap: 0.9rem;
  transition: opacity 0.18s linear;
}

.beat__label.is-swapping,
.beat__caption.is-swapping {
  opacity: 0;
}

.beat__index {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.18em;
  color: var(--aurora-cyan);
}

.beat__name {
  font-size: 0.75rem;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--text-0);
  white-space: nowrap;
}

.beat__rule {
  flex: 1;
  height: 1px;
  background: var(--surface-border);
}

.beat__caption {
  max-width: 62ch;
  margin: 0;
  color: var(--text-2);
  font-size: 0.875rem;
  transition: opacity 0.18s linear;
}

.beat__caption strong {
  color: var(--text-0);
  font-weight: 500;
}

.beat__caption code {
  font-family: var(--font-mono);
  font-size: 0.92em;
  color: var(--aurora-blue);
}

/* ---- the frame holding terminal + rendered output ---- */
.scrolly__frame {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: 100%;
}

/* ---- terminal ---- */
.terminal {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: linear-gradient(180deg, #0a1018f2, #060a11f7);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-md);
  box-shadow: 0 30px 90px -40px #000;
  overflow: hidden;
}

.terminal__bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: none;
  padding: 0.55rem 0.8rem;
  border-bottom: 1px solid var(--surface-border);
  background: #ffffff05;
}

.terminal__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ffffff1f;
}

.terminal__dot:first-child {
  background: #ffffff2b;
}

.terminal__path {
  margin-left: 0.35rem;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  letter-spacing: 0.04em;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.terminal__body {
  flex: 1;
  min-height: 0;
  padding: 0.95rem 1.1rem 1.1rem;
  font-family: var(--font-mono);
  font-size: calc(clamp(0.625rem, 0.95vw, 0.8125rem) * var(--code-scale, 1));
  line-height: 1.66;
  overflow: hidden;
}

.terminal__prompt {
  white-space: pre-wrap;
  color: var(--text-1);
}

.terminal__sigil {
  color: var(--aurora-cyan);
}

.terminal__cursor {
  display: inline-block;
  width: 0.58em;
  height: 1.02em;
  margin-left: 1px;
  vertical-align: -0.18em;
  background: var(--aurora-highlight);
  animation: terminal-blink 1.05s steps(1) infinite;
}

@keyframes terminal-blink {
  0%,
  50% {
    opacity: 1;
  }
  50.01%,
  100% {
    opacity: 0;
  }
}

/* ---- `rsc new` output ---- */
.output {
  display: block;
  white-space: pre-wrap;
  color: var(--text-2);
}

.output__line {
  display: block;
}

.output__ok {
  color: #3ddc97;
}

.output__name {
  color: var(--aurora-highlight);
}

.output__note {
  color: #55607a;
}

.output__cmd {
  color: var(--aurora-blue);
}

/* ---- source listing ---- */
.source__heading {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.8rem 0 0.55rem;
  font-size: 0.92em;
  color: var(--text-2);
}

.source__heading::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--surface-border);
}

.source__code {
  display: block;
  white-space: pre;
  overflow-x: auto;
}

.source__line {
  display: block;
  padding-left: 0.625rem;
  margin-left: -0.75rem;
  border-left: 2px solid transparent;
  transition: background-color 0.28s linear, border-color 0.28s linear;
}

.source__line.is-hot {
  background-color: #b865ff14;
  border-left-color: var(--aurora-violet);
}

/* Syntax colours, taken from the aurora ramp. */
.tok-kw { color: var(--aurora-violet); }
.tok-type { color: var(--aurora-cyan); }
.tok-fn { color: var(--aurora-blue); }
.tok-method { color: #8fd0ff; }
.tok-str { color: var(--aurora-word-end); }
.tok-num { color: var(--aurora-indigo); }
.tok-var { color: var(--text-0); }
.tok-punct { color: var(--text-2); }

/* ---- rendered widget panels ---- */
.render {
  display: grid;
  gap: var(--space-2);
  align-content: center;
  min-width: 0;
}

.render.is-grid {
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
}

.render__panel {
  display: grid;
  gap: 0.7rem;
  align-content: start;
  padding: 1.05rem;
  background: var(--surface);
  border: 1px solid var(--surface-border-strong);
  border-radius: var(--radius-md);
  box-shadow: 0 24px 60px -34px #000;
  backdrop-filter: blur(10px);
}

.render__tag {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-family: var(--font-mono);
  font-size: 0.625rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--text-2);
}

.render__tag::before {
  content: "";
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

/* One hue per panel, walked across the ramp — colour as an index into
   the sequence, not decoration. */
[data-panel="counter"] { border-top-color: #00eeff3d; }
[data-panel="counter"] .render__tag { color: var(--aurora-cyan); }
[data-panel="buttons"] { border-top-color: #7a7bff3d; }
[data-panel="buttons"] .render__tag { color: var(--aurora-indigo); }
[data-panel="inputs"] { border-top-color: #b865ff3d; }
[data-panel="inputs"] .render__tag { color: var(--aurora-violet); }
[data-panel="gallery"] { border-top-color: #e85ce03d; }
[data-panel="gallery"] .render__tag { color: var(--aurora-magenta); }

/* ---- widget mocks ---- */
.mock__count {
  font-size: clamp(2.125rem, 4.2vw, 3.625rem);
  font-weight: 300;
  line-height: 1;
  text-align: center;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  color: var(--aurora-highlight);
  text-shadow: 0 0 26px #b865ff59;
}

.mock__caption {
  margin: 0;
  text-align: center;
  font-size: 0.8125rem;
  color: var(--text-2);
}

.mock__row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.mock__row--start {
  justify-content: flex-start;
}

.mock__btn {
  padding: 0.5rem 0.95rem;
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1.2;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: default;
}

.mock__btn--live {
  cursor: pointer;
  transition: transform 0.12s var(--ease-out), filter 0.16s linear;
}

.mock__btn--live:hover {
  filter: brightness(1.14);
}

.mock__btn--live:active {
  transform: scale(0.95);
}

.mock__btn--square {
  width: 42px;
  padding: 0.5rem 0;
  text-align: center;
}

/* Solid fills from the deep ramp — one hue per variant, no gradients. */
.mock__btn--primary {
  background: var(--deep-violet);
  border-color: #b865ff59;
  color: #f6ecff;
  font-weight: 600;
}

.mock__btn--secondary {
  background: var(--deep-indigo);
  border-color: #7a7bff4d;
  color: #e9ecff;
}

.mock__btn--ghost {
  background: #ffffff08;
  border-color: var(--surface-border-strong);
  color: var(--text-1);
}

.mock__btn--danger {
  background: #7d1f2c;
  border-color: #ff5b6a45;
  color: #ffc2c8;
}

.mock__btn--success {
  background: #125c46;
  border-color: #3ddc9740;
  color: #a6f0d1;
}

.mock__chip {
  padding: 0.25rem 0.625rem;
  font-size: 0.6875rem;
  border: 1px solid var(--surface-border-strong);
  border-radius: 999px;
  background: #ffffff08;
  color: var(--text-1);
}

.mock__chip:nth-child(1) { background: #0077cc33; border-color: #00eeff40; color: #b6f3ff; }
.mock__chip:nth-child(2) { background: #3d3dbb33; border-color: #7a7bff40; color: #d2d4ff; }
.mock__chip:nth-child(3) { background: #6b2fb033; border-color: #b865ff40; color: #eeddff; }
.mock__chip:nth-child(4) { background: #9426a033; border-color: #e85ce040; color: #ffd9fb; }

.mock__ctrl {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.8125rem;
}

.mock__switch {
  position: relative;
  flex: none;
  width: 40px;
  height: 22px;
  border-radius: 999px;
  background: var(--deep-cyan);
}

.mock__switch::after {
  content: "";
  position: absolute;
  top: 3px;
  right: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--bg-0);
}

.mock__switch--off {
  background: #ffffff1a;
}

.mock__switch--off::after {
  right: auto;
  left: 3px;
  background: var(--text-2);
}

.mock__track {
  position: relative;
  height: 4px;
  border-radius: 999px;
  background: #ffffff14;
}

.mock__track > i {
  position: absolute;
  inset: 0 42% 0 0;
  border-radius: 999px;
  background: var(--deep-blue);
}

.mock__track > b {
  position: absolute;
  top: 50%;
  left: 58%;
  width: 13px;
  height: 13px;
  margin: -6.5px 0 0 -6.5px;
  border-radius: 50%;
  background: #c9d8ff;
  box-shadow: 0 0 10px #1a5fc4cc;
}

.mock__segmented {
  display: flex;
  border: 1px solid var(--surface-border-strong);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.mock__segmented span {
  flex: 1;
  padding: 0.375rem 0;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-2);
}

.mock__segmented span.is-selected {
  background: #ffffff12;
  color: var(--text-0);
}

/* ---- mark resolve ---- */
.scrolly__mark {
  margin: 0;
  display: grid;
  place-items: center;
  padding: var(--space-4) 0;
}

.scrolly__mark img {
  width: min(36vh, 300px);
  height: auto;
  display: block;
}

.scrolly__mark figcaption {
  margin-top: var(--space-3);
  text-align: center;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--text-2);
}

/* ============================================================
   Driven state — pinned stage, scrubbed reveals.
   ============================================================ */

.scrolly.is-driven {
  height: 640vh;
}

.scrolly.is-driven .scrolly__stage {
  position: sticky;
  top: 0;
  height: 100vh;
  max-width: none;
  grid-template-rows: auto minmax(0, 1fr) auto;
  /* top padding clears the fixed site nav */
  padding: var(--space-5) var(--space-3) var(--space-3);
  overflow: hidden;
}

/* Header and caption share the frame's measure so the three columns
   line up rather than running to the viewport edge. */
.scrolly.is-driven .beat,
.scrolly.is-driven .beat__caption {
  width: 100%;
  max-width: var(--max-width);
  margin-inline: auto;
}

/* Local lift under the composed widgets — the page background's echo. */
.scrolly.is-driven .scrolly__stage::before {
  content: "";
  position: absolute;
  inset: auto 0 0 0;
  height: 60vh;
  pointer-events: none;
  opacity: var(--stage-glow, 0);
  transform: translateY(calc((1 - var(--stage-glow, 0)) * 22%));
  background:
    radial-gradient(120% 100% at 50% 130%, #b865ff33 0%, #7a7bff1c 32%, transparent 68%),
    radial-gradient(80% 80% at 18% 120%, #00eeff24 0%, transparent 60%),
    radial-gradient(80% 80% at 84% 125%, #e85ce021 0%, transparent 60%);
}

.scrolly.is-driven .beat,
.scrolly.is-driven .scrolly__frame-wrap,
.scrolly.is-driven .beat__caption {
  position: relative;
  z-index: 2;
}

/* The frame must never exceed its grid row, or the terminal grows past
   the caption. Stretch rather than centre, and clip. */
.scrolly.is-driven .scrolly__frame-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: stretch;
  justify-items: center;
  min-height: 0;
  overflow: hidden;
}

.scrolly.is-driven .terminal,
.scrolly.is-driven .render {
  min-height: 0;
}

.scrolly.is-driven .scrolly__frame {
  flex-direction: row;
  align-items: stretch;
  gap: 2%;
  width: 100%;
  max-width: var(--max-width);
  height: 100%;
  /* Without this the grid item's automatic minimum size is the terminal's
     full content height, and `height: 100%` never gets a chance. */
  min-height: 0;
}

.scrolly.is-driven .terminal {
  width: calc(var(--code-width, 100) * 1%);
  /* Clear well before the column is narrow enough to reflow the code
     into an unreadable sliver. */
  opacity: clamp(0, calc((var(--code-width, 100) - 14) / 16), 1);
}

.scrolly.is-driven .terminal__body {
  -webkit-mask-image: linear-gradient(180deg, #000 90%, transparent 100%);
  mask-image: linear-gradient(180deg, #000 90%, transparent 100%);
}

.scrolly.is-driven .render {
  width: calc(var(--preview-width, 0) * 1%);
  opacity: clamp(0, calc(var(--preview-width, 0) / 18), 1);
}

.scrolly.is-driven .beat__caption {
  min-height: 3.1em;
}

/* Elements that arrive on cue start hidden only when something is
   driving them. */
.scrolly.is-driven .output__line,
.scrolly.is-driven .source__line,
.scrolly.is-driven .source__heading,
.scrolly.is-driven .render__panel,
.scrolly.is-driven [data-widget] {
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.4s var(--ease-out), transform 0.4s var(--ease-out);
}

.scrolly.is-driven .output__line.is-visible,
.scrolly.is-driven .source__line.is-visible,
.scrolly.is-driven .source__heading.is-visible,
.scrolly.is-driven .render__panel.is-visible,
.scrolly.is-driven [data-widget].is-visible {
  opacity: 1;
  transform: none;
}

/* Until the compose beat there is only one panel. The rest must be out of
   flow, not merely transparent, or they push the counter out of its row. */
.scrolly.is-driven .render:not(.is-grid) .render__panel:not([data-panel="counter"]) {
  display: none;
}

.scrolly.is-driven .scrolly__mark {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  align-content: center;
  opacity: 0;
}

/* ---- static state: reduced motion keeps the flat layout ---- */
.scrolly.is-static .terminal__cursor {
  display: none;
}

.scrolly.is-static .source__code {
  overflow-x: auto;
}

@media (prefers-reduced-motion: reduce) {
  .terminal__cursor {
    animation: none;
  }
}

@media (max-width: 800px) {
  .scrolly.is-driven .scrolly__frame {
    flex-direction: column;
    gap: var(--space-1);
  }

  .scrolly.is-driven .terminal,
  .scrolly.is-driven .render {
    width: 100%;
  }

  .scrolly.is-driven .render.is-grid {
    grid-template-columns: 1fr;
  }
}
