/* ============================================================================
   Scene: STORY — the hero, then "from idea to production" performed by scroll.
   ----------------------------------------------------------------------------
   Two layouts:
   1. Default (no JS, reduced motion, ?noanim): a normal, complete section.
      Hero, then the finished app as an illustration, then the four steps.
   2. .story.is-pinned (added by js/scenes/story.js only when motion is on):
      one 100svh frame that the scroll animates through. Desktop puts copy left
      and the stage right; phones stack the stage on top of the copy.

   The stage is drawn in a fixed 960x600 design space and scaled with --s, so
   the blueprint (generated from the app's own layout) always registers with it.
   ========================================================================== */

.story {
  --ok: #34d399;
  --warn: #f5b84a;
  --chalk: var(--ink-2);
  position: relative;
  isolation: isolate;
}
[data-theme="light"] .story { --ok: #059669; --warn: #b7791f; }

/* ------------------------------------------------------------ static layout */
.story-pin {
  position: relative;
  display: grid;
  gap: clamp(40px, 6vw, 72px);
  /* Full width with container padding, not a max-width box: the pinned layout
     is full width too, so the box does not jump when the pin takes over
     (a max-width here measured as a 0.49 layout shift on every desktop load). */
  width: 100%;
  padding: clamp(112px, 16vh, 168px) max(var(--gutter), calc((100vw - var(--container)) / 2 + var(--gutter))) var(--sec-pad);
}
.story-copy { display: contents; }
.beat-hero { order: 1; display: grid; gap: clamp(22px, 3vw, 32px); max-width: 920px; }
.story-stage { order: 2; }
.beats { order: 3; }
.story-steps, .idea-point, .idea-line, .bp, .story-cue, .app-sweep, .app-toast { display: none; }

.story-glow {
  position: absolute;
  z-index: -1;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(52% 46% at 74% 38%, var(--accent-glow), transparent 70%),
    radial-gradient(40% 40% at 12% 90%, color-mix(in srgb, var(--accent) 8%, transparent), transparent 70%);
  opacity: 0.7;
}

.beat-hero h1 { max-width: 14ch; }
.beat-hero h1 .ln {
  display: block;
  overflow: hidden;
  /* room for the serif italic's overhang, cancelled out by the margin */
  padding: 0.06em 0.14em 0.1em 0;
  margin: -0.06em -0.14em -0.1em 0;
}
.beat-hero h1 .ln > span { display: block; }

/* Hero intro start state, set from <head> before first paint. If the scripts
   never take over, the failsafe animation shows everything after 2.6s. */
.js-intro .beat-hero > *,
.js-intro .idea-point,
.js-intro .story-glow {
  opacity: 0;
  animation: intro-failsafe 0.01s 2.6s forwards;
}
@keyframes intro-failsafe { to { opacity: 1; } }
.story-sub { max-width: 48ch; color: var(--ink-2); font-size: clamp(1.0625rem, 1.3vw, 1.1875rem); line-height: 1.6; }
.story-ctas { display: flex; flex-wrap: wrap; gap: 12px; }

.beats {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: clamp(28px, 4vw, 48px) clamp(24px, 4vw, 56px);
  border-top: 1px solid var(--line);
  padding-top: clamp(28px, 4vw, 44px);
}
.beat-num {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 12px;
}
.beat-num span { color: var(--accent); }
.beat-title {
  margin-top: 14px;
  font-size: clamp(1.6rem, 2.6vw, 2.2rem);
  font-weight: 510;
  letter-spacing: -0.028em;
  line-height: 1.08;
  text-wrap: balance;
}
.beat-body {
  margin-top: 14px;
  color: var(--ink-2);
  font-size: 1rem;
  line-height: 1.62;
  max-width: 40ch;
}
.story-anchor { position: absolute; }

/* The stage box takes its size from --s so it works without JS too.
   JS sets --s exactly; these breakpoints are the no-JS fallback. */
.story-stage { --s: 0.36; position: relative; width: calc(960px * var(--s)); height: calc(600px * var(--s)); margin-inline: auto; }
@media (min-width: 420px)  { .story-stage { --s: 0.4; } }
@media (min-width: 600px)  { .story-stage { --s: 0.58; } }
@media (min-width: 820px)  { .story-stage { --s: 0.8; } }
@media (min-width: 1080px) { .story-stage { --s: 1; } }

.stage-scaler {
  position: absolute;
  left: 0;
  top: 0;
  width: 960px;
  height: 600px;
  transform: scale(var(--s));
  transform-origin: 0 0;
  perspective: 2000px;
}
.stage-3d {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
}

/* ------------------------------------------------------------ the app (UI) */
.app {
  position: absolute;
  left: 40px;
  top: 34px;
  width: 880px;
  height: 528px;
  display: grid;
  grid-template-columns: 176px minmax(0, 1fr);
  grid-template-rows: 44px minmax(0, 1fr);
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: 14px;
  box-shadow: var(--hl), 0 40px 120px -40px rgba(0, 0, 0, 0.75), 0 0 0 1px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  font-family: var(--font);
  color: var(--ink);
  font-size: 13px;
  letter-spacing: -0.005em;
  --lift: 1;
}
[data-theme="light"] .app { box-shadow: var(--hl), 0 40px 100px -44px rgba(10, 13, 20, 0.35); }

.app-bar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--raised);
}
.app-dots { display: flex; gap: 6px; }
.app-dots i { width: 9px; height: 9px; border-radius: 50%; background: var(--line-3); }
.app-title { font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.04em; color: var(--muted); }
.app-status {
  --live: 1;
  margin-inline-start: auto;
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 24px;
  padding: 0 11px 0 10px;
  border-radius: 999px;
  border: 1px solid var(--line-2);
  font-size: 11px;
  font-weight: 500;
}
.app-status i {
  width: 7px; height: 7px; border-radius: 50%;
  background: color-mix(in srgb, var(--ok) calc(var(--live) * 100%), var(--warn));
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) calc(var(--live) * 22%), transparent);
}
.app-status b { font-weight: 500; }
.st-build { position: absolute; left: 25px; opacity: calc(1 - var(--live)); color: var(--warn); }
.st-live { opacity: var(--live); color: var(--ok); }

.app-side {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 16px 12px;
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--raised) 55%, var(--surface));
}
.app-side span { padding: 8px 10px; border-radius: 6px; color: var(--muted); font-size: 12.5px; }
.app-side span.is-on { background: var(--accent-wash); color: var(--ink); }
.app-logo { display: flex; align-items: center; gap: 8px; color: var(--ink) !important; font-weight: 600; margin-bottom: 10px; }
.app-logo i { width: 12px; height: 12px; border-radius: 3px; background: var(--accent); }

.app-main {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 22px 24px;
  min-width: 0;
}
.app-query {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 46px;
  padding: 0 8px 0 14px;
  border: 1px solid var(--line-2);
  border-radius: 10px;
  background: var(--ground);
}
.q-icon { width: 14px; height: 14px; border: 1.5px solid var(--muted); border-radius: 50%; flex: none; }
.q-text { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: clip; color: var(--ink); font-size: 13.5px; }
.q-text .ch { opacity: var(--t, 1); }
.q-run {
  flex: none;
  height: 30px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  border-radius: 7px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  font-size: 12px;
}

.app-pipe { display: flex; align-items: center; height: 64px; }
.node {
  --on: 1;
  flex: 1 1 0;
  height: 56px;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--accent) calc(var(--on) * 55%), var(--line-2));
  background: color-mix(in srgb, var(--accent) calc(var(--on) * 9%), var(--raised));
  box-shadow: 0 0 calc(var(--on) * 24px) calc(var(--on) * -4px) var(--accent-glow);
  display: grid;
  align-content: center;
  gap: 3px;
}
.node b { font-weight: 600; font-size: 13px; }
.node small { font-family: var(--font-mono); font-size: 10.5px; color: var(--muted); letter-spacing: 0.02em; }
.edge { flex: 0 0 34px; height: 2px; position: relative; background: var(--line-2); }
.edge i { position: absolute; inset: 0; background: var(--accent); transform-origin: left center; transform: scaleX(var(--f, 1)); }

.app-table {
  flex: 1 1 auto;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  display: grid;
  grid-auto-rows: minmax(0, 1fr);
}
.row {
  display: grid;
  grid-template-columns: 1.55fr 0.9fr 1fr;
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  font-size: 12.5px;
  color: var(--ink-2);
}
.row:last-child { border-bottom: 0; }
.row.head { color: var(--muted); font-family: var(--font-mono); font-size: 10.5px; letter-spacing: 0.1em; text-transform: uppercase; background: var(--raised); }
.row span:first-child { color: var(--ink); }
.row.head span:first-child { color: var(--muted); }
.bar { position: relative; height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; }
.bar i { position: absolute; inset: 0; background: linear-gradient(90deg, var(--accent-2), var(--accent)); border-radius: inherit; transform-origin: left center; transform: scaleX(calc(var(--w) * var(--g, 1))); }

.app-metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; height: 76px; }
.metric {
  position: relative;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--raised);
  display: grid;
  align-content: start;
  gap: 4px;
  overflow: hidden;
}
.metric small { font-family: var(--font-mono); font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.metric b { font-size: 21px; font-weight: 560; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.spark { position: absolute; right: 12px; bottom: 12px; width: 92px; height: 26px; overflow: visible; }
.spark path { fill: none; stroke: var(--ok); stroke-width: 1.6; stroke-linejoin: round; vector-effect: non-scaling-stroke; }

.app-sweep {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(105deg, transparent 38%, color-mix(in srgb, var(--ink) 9%, transparent) 50%, transparent 62%);
  transform: translateX(-110%);
}
.app-toast {
  position: absolute;
  left: 12px;
  bottom: 14px;
  width: 162px;
  padding: 11px 12px;
  border-radius: 12px;
  background: var(--raised);
  border: 1px solid var(--line-2);
  box-shadow: 0 24px 60px -24px rgba(0, 0, 0, 0.7);
  display: grid;
  gap: 8px;
  font-size: 11px;
  color: var(--ink-2);
}
.app-toast span { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.app-toast i { width: 14px; height: 14px; border-radius: 50%; background: color-mix(in srgb, var(--ok) 18%, transparent); position: relative; flex: none; }
.app-toast i::after { content: ""; position: absolute; left: 4px; top: 3px; width: 4px; height: 6px; border: solid var(--ok); border-width: 0 1.5px 1.5px 0; transform: rotate(45deg); }

/* -------------------------------------------------------- blueprint (SVG) */
.bp { position: absolute; left: 0; top: 0; width: 960px; height: 600px; overflow: visible; --bpmix: 0; }
.bp .sk {
  fill: none;
  stroke: color-mix(in srgb, var(--accent) calc(var(--bpmix) * 100%), var(--chalk));
  stroke-width: 2.1;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.bp .grid line { stroke: var(--line-2); stroke-width: 1; }
.bp .dim { stroke: var(--accent); stroke-width: 1; fill: none; }
.bp .lbl { font-family: var(--font-mono); font-size: 11px; letter-spacing: 0.08em; fill: var(--accent); }
.bp .note { font-family: var(--font-serif); font-style: italic; font-size: 21px; fill: var(--chalk); }
.bp .note-arrow { fill: none; stroke: var(--chalk); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

/* ---------------------------------------------------------- the idea point */
.idea-point { position: absolute; left: 480px; top: 300px; width: 0; height: 0; }
.idea-point i {
  position: absolute;
  left: -9px; top: -9px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: radial-gradient(circle, #fff 0 26%, var(--accent) 58%, transparent 72%);
  box-shadow: 0 0 22px 6px var(--accent-glow), 0 0 90px 26px color-mix(in srgb, var(--accent) 22%, transparent);
  animation: idea-pulse 2.8s var(--ease-io) infinite;
}
@keyframes idea-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.18); }
}

/* ------------------------------------------------- the handwritten sentence */
.idea-line {
  position: absolute;
  left: 0;
  top: 262px;
  width: 960px;
  margin: 0;
  text-align: center;
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 46px;
  line-height: 1.2;
  letter-spacing: -0.01em;
  color: var(--chalk);
  white-space: nowrap;
  pointer-events: none;
}
.idea-line span { display: inline-block; }
.idea-line .ch { white-space: pre; }

/* =================================================== pinned layout (motion) */
.story.is-pinned .story-pin {
  --copy-left: max(var(--gutter), calc((100vw - var(--container)) / 2 + var(--gutter)));
  --copy-w: min(400px, 30vw);
  display: block;
  height: 100svh;
  max-width: none;
  padding: 0;
}
.story.is-pinned .story-copy {
  display: block;
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--copy-left);
  width: var(--copy-w);
  z-index: 2;
}
.story.is-pinned .beats { display: contents; border: 0; padding: 0; }
.story.is-pinned .beat {
  /* Centred with flex, not a transform: GSAP owns transforms on these. */
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  border: 0;
  padding: 0;
}
.story.is-pinned .beat-hero { right: auto; width: min(66vw, 940px); z-index: 2; gap: clamp(20px, 2.6vh, 30px); }
.story.is-pinned .beat-num { margin: 0; }
.story.is-pinned .beat-title { font-size: clamp(2rem, 3.5vw, 3.4rem); line-height: 1.03; letter-spacing: -0.032em; margin-top: 18px; }
.story.is-pinned .beat-body { margin-top: 20px; font-size: clamp(1rem, 1.15vw, 1.1rem); max-width: 34ch; }
.story.is-pinned .story-cue { display: flex; }
.story.is-pinned .story-stage {
  position: absolute;
  top: 0;
  bottom: 0;
  right: calc(var(--copy-left) - clamp(12px, 2vw, 40px));
  left: calc(var(--copy-left) + var(--copy-w) + clamp(20px, 3vw, 56px));
  width: auto;
  height: auto;
  margin: 0;
}
.story.is-pinned .stage-scaler { left: 50%; top: 50%; transform: translate(-50%, -50%) scale(var(--s)); transform-origin: 50% 50%; }
.story.is-pinned .idea-line { display: block; opacity: 0; }
.story.is-pinned .idea-point,
.story.is-pinned .bp,
.story.is-pinned .app-sweep,
.story.is-pinned .app-toast { display: block; }
.story.is-pinned .app-toast { display: grid; }
.story.is-pinned .story-steps {
  display: grid;
  position: absolute;
  left: 0;
  bottom: clamp(28px, 6vh, 60px);
  grid-template-columns: repeat(4, auto);
  column-gap: 26px;
  padding-top: 14px;
}

.story-cue {
  align-items: center;
  gap: 12px;
  margin-top: clamp(12px, 3vh, 36px);
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.story-cue span {
  position: relative;
  width: 1px;
  height: 36px;
  background: var(--line-2);
  overflow: hidden;
}
.story-cue span::after {
  content: "";
  position: absolute;
  left: 0;
  top: -40%;
  width: 1px;
  height: 40%;
  background: var(--accent);
  animation: cue 2.2s var(--ease-io) infinite;
}
@keyframes cue { to { top: 100%; } }

.ss-track { position: absolute; left: 0; right: 0; top: 0; height: 1px; background: var(--line-2); }
.ss-track i { position: absolute; inset: 0; background: var(--accent); transform-origin: left center; transform: scaleX(var(--p, 0)); }
.ss-label {
  --on: 0;
  font-family: var(--font-mono);
  font-size: 0.65rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: color-mix(in srgb, var(--ink) calc(var(--on) * 100%), var(--muted));
}

/* First paint in pinned mode, before js builds the timeline: hero + point
   only. The timeline owns every one of these from then on. */
.story.is-pinned .beat:not(.beat-hero),
.story.is-pinned .story-steps { visibility: hidden; opacity: 0; }
.story.is-pinned .app,
.story.is-pinned .app-toast { opacity: 0; }
.story.is-pinned .idea-point i::before {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 1px solid var(--accent);
  animation: idea-ring 2.8s var(--ease-io) infinite;
}
@keyframes idea-ring {
  0% { transform: scale(1); opacity: 0.7; }
  80%, 100% { transform: scale(6); opacity: 0; }
}

/* Phones and small tablets: stage on top, copy underneath. */
@media (max-width: 899px) {
  .story.is-pinned .story-pin {
    display: flex;
    flex-direction: column;
    padding: 66px var(--gutter) 0;
  }
  /* The visual leads on a phone: stage on top, sized exactly to its content
     (960x600 is 0.625 tall per unit of width), the copy underneath. */
  .story.is-pinned .story-stage {
    order: -1;
    position: relative;
    inset: auto;
    flex: 0 0 auto;
    height: min(100vw - 2 * var(--gutter), 44svh);
    margin-top: 6px;
    margin-inline: calc(var(--gutter) * -1);
    clip-path: inset(0);
  }
  .story.is-pinned .story-copy { position: relative; inset: auto; width: auto; flex: 1 1 auto; }
  .story.is-pinned .beat { top: 14px; bottom: 64px; justify-content: flex-start; }
  /* The hero is taller than the space under the stage: anchor it to the
     bottom of the screen and let it rise into the stage's empty lower half. */
  .story.is-pinned .beat-hero { width: 100%; gap: 16px; top: auto; bottom: 28px; justify-content: flex-end; }
  .story.is-pinned .beat-hero h1 { font-size: clamp(2.4rem, 11vw, 3.4rem); }
  .story.is-pinned .story-sub { font-size: 0.98rem; }
  .story.is-pinned .story-cue { display: none; }
  .story.is-pinned .beat-title { font-size: clamp(1.7rem, 7.4vw, 2.3rem); margin-top: 12px; }
  .story.is-pinned .beat-body { margin-top: 12px; font-size: 0.95rem; max-width: 40ch; }
  .story.is-pinned .story-steps { bottom: 22px; column-gap: 16px; }
  .ss-label { font-size: 0.58rem; }
}
/* Short phones (iPhone SE and the like): a smaller stage, no step labels, and
   tighter type, or the step text runs into the labels and off the screen
   (found by the layout audit at 320x568, 10 Nov). */
@media (max-width: 899px) and (max-height: 700px) {
  .story.is-pinned .story-stage { height: min(100vw - 2 * var(--gutter), 36svh); }
  .story.is-pinned .story-steps { display: none; }
  .story.is-pinned .beat { top: 6px; bottom: 12px; }
  .story.is-pinned .beat-hero { bottom: 14px; gap: 12px; }
  .story.is-pinned .beat-hero h1 { font-size: clamp(2.1rem, 10vw, 2.6rem); }
  .story.is-pinned .beat-title { font-size: clamp(1.5rem, 7vw, 1.9rem); margin-top: 8px; }
  .story.is-pinned .beat-body { margin-top: 8px; font-size: 0.9rem; line-height: 1.45; }
}
@media (max-width: 680px) {
  .beats { grid-template-columns: minmax(0, 1fr); }
  .story-ctas .btn { flex: 1 1 auto; justify-content: center; }
}
@media (max-width: 460px) {
  .story.is-pinned .story-ctas { flex-direction: column; align-items: stretch; }
}
