/*
 * Scenes: the preorder page's story and its look inside, played by scroll. In each, --p runs from 0 to 1
 * while the scene's stage is pinned: set by a scroll-driven animation where the browser has them, by the
 * scroll-scene controller where it doesn't. Each beat is a phase that goes from 0 to 1 across its own window of --p, and every part
 * of the scene moves by a formula over the phases, so each beat's timing lives in one place.
 * Screens only, with JavaScript on, for visitors who haven't asked for reduced motion. Everyone else gets
 * the static story from components.css, and the scene stays hidden.
 */
@property --p {
  syntax: "<number>";
  inherits: true;
  initial-value: 0;
}

/* Registered as an integer so a calc() rounds to whole degrees for the spirit level's readout. */
@property --tilt-degrees {
  syntax: "<integer>";
  inherits: false;
  initial-value: 0;
}

.story__scene,
.story__level,
.story__cta {
  display: none;
}

/* The drawing, in the brand palette */
.story__scene .home__wall {
  fill: var(--color-emerald);
}

.story__scene .tuft,
.story__scene .tree__crown,
.story__scene .home__vent-frame {
  fill: var(--color-emerald-20);
}

.story__scene .ground {
  fill: var(--color-emerald-40);
}

.story__scene .far-ground {
  fill: var(--color-emerald-60);
}

.story__scene .mountains__far,
.story__vial-tube {
  fill: var(--color-emerald-80);
}

.story__scene .mountains__near {
  fill: var(--color-sage);
}

.story__scene .tree__crown--light,
.story__scene .leg__post,
.story__scene .forklift__tank {
  fill: var(--color-sage-20);
}

.story__scene .tree__trunk,
.story__scene .home__hatch,
.story__scene .leg__pad,
.story__scene .leg__housing,
.story__scene .home__pocket,
.story__scene .forklift__seat {
  fill: var(--color-sage-60);
}

.story__scene .home__seam,
.story__scene .home__vent,
.story__scene .home__sign,
.story__scene .home__hatch-opening {
  fill: var(--color-sage-80);
}

.story__scene .home__skirt,
.story__scene .home__hardware,
.story__scene .forklift__steel,
.story__scene .shadow {
  fill: var(--color-charcoal);
}

/* Rubber, a little greyer than the steel, so the wheels stand out from the chassis */
.story__scene .forklift__tire {
  fill: color-mix(in srgb, var(--color-charcoal), white 22%);
}

.story__scene .mountains__snow,
.story__scene .home__frame,
.story__scene .home__starlink,
.story__vial-body,
.story__vial-bubble {
  fill: var(--color-cream);
}

.story__scene .forklift__hub,
.story__scene .forklift__beacon {
  fill: var(--color-sand);
}

.story__scene .forklift__body {
  fill: var(--color-terracotta);
}

.story__scene .home__door {
  fill: var(--color-cream);
  stroke: var(--color-sand);
  stroke-width: 6;
}

.story__scene .home__glass {
  fill: url(#story-glass);
}

.story__scene #story-glass stop:first-child {
  stop-color: var(--color-emerald-80);
}

.story__scene #story-glass stop:last-child {
  stop-color: var(--color-cream);
}

.story__scene .home__seams {
  fill: url(#story-seams);
  opacity: 0.28;
}

.story__scene .home__fascia {
  stroke: var(--color-sage-80);
  stroke-width: 6;
  opacity: 0.35;
}

.story__scene .home__inset {
  fill: none;
  stroke: var(--color-sand);
  stroke-width: 8;
}

.story__scene .forklift__guard {
  fill: none;
  stroke: var(--color-charcoal);
  stroke-width: 16;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.story__scene .forklift__steer,
.story__scene .forklift__spoke {
  stroke: var(--color-charcoal);
  stroke-linecap: round;
}

.story__scene .forklift__steer {
  stroke-width: 10;
}

.story__scene .forklift__spoke {
  stroke-width: 6;
}

.story__scene .forklift__cylinder {
  stroke: var(--color-sage-20);
  stroke-width: 8;
}

.story__vial-body {
  stroke: var(--color-emerald);
  stroke-width: 5;
}

.story__vial-mark {
  stroke: var(--color-emerald);
  stroke-width: 4;
}

.story__vial-bubble {
  stroke: var(--color-emerald-40);
  stroke-width: 3;
}

/* The inside scene (script/render_inside). Each material sets --face; a box's end is shaded and its top lit
   by mixing that color, so every part reads as a solid in one brand color. */
.inside__scene .face {
  fill: var(--face);
}

.inside__scene .face--end {
  fill: color-mix(in srgb, var(--face) 76%, var(--color-charcoal));
}

.inside__scene .face--top {
  fill: color-mix(in srgb, var(--face) 82%, var(--color-cream));
}

.inside__siding-high {
  --face: var(--color-emerald);
}

.inside__hvac {
  --face: var(--color-emerald-20);
}

.inside__tank-fresh {
  --face: var(--color-emerald-60);
}

.inside__roof,
.inside__counter {
  --face: var(--color-sage-20);
}

.inside__tank-gray,
.inside__sink {
  --face: var(--color-sage-40);
}

.inside__foot,
.inside__actuator {
  --face: var(--color-sage-60);
}

.inside__deck {
  --face: var(--color-sage-20);
}

.inside__trailer {
  --face: var(--color-charcoal);
}

/* Rubber, a little greyer than the trailer's steel */
.inside__tire {
  fill: color-mix(in srgb, var(--color-charcoal), white 22%);
}

.inside__hub {
  fill: var(--color-sage-40);
}

/* Hurricane anchors, painted green like the real ones */
.inside__anchor-plate {
  fill: var(--color-emerald-20);
}

.inside__hoop,
.inside__helix,
.inside__shaft {
  fill: none;
  stroke: var(--color-emerald-20);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.inside__helix {
  stroke-width: 2;
}

.inside__gust {
  fill: none;
  stroke: var(--color-sage-40);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 150 500;
}

/* Still, the scene is the home as it stands on site: on its legs, strapped to the anchors, no flatbed or wind. */
.inside__part--flatbed,
.inside__part--wind,
.inside__strap--deck,
.inside__auger {
  display: none;
}

.inside__chassis,
.inside__siding-low,
.inside__appliance {
  --face: var(--color-charcoal);
}

.inside__floor,
.inside__cabinet {
  --face: var(--color-sand);
}

.inside__shower,
.inside__unit,
.inside__dish,
.inside__mattress {
  --face: var(--color-cream);
}

/* Darker than the floor, so the table and the bed's base stand out against it */
.inside__bed,
.inside__tabletop {
  --face: color-mix(in srgb, var(--color-sand) 55%, var(--color-charcoal));
}

.inside__pillow {
  --face: var(--color-emerald-80);
}

/* The table and the Murphy bed are drawn at every step of their folds; still, they show folded up. */
.inside__fold:not(:first-child) {
  opacity: 0;
}

.inside__tank-hot,
.inside__cushion {
  --face: var(--color-terracotta);
}

.inside__trim,
.inside__door {
  fill: var(--color-cream);
}

.inside__strap {
  fill: var(--color-terracotta);
}

/* Bare metal, like the ratchets on the home, so the pocket reads against the dark siding */
.inside__ratchet {
  fill: var(--color-sage-20);
}

.inside__door {
  stroke: var(--color-sand);
  stroke-width: 2;
}

.inside__glass {
  fill: var(--color-emerald-80);
}

.inside__pocket,
.inside__vent,
.inside__sign,
.inside__panel {
  fill: var(--color-sage-80);
}

.inside__dish {
  stroke: var(--color-sage-40);
  stroke-width: 1.5;
  stroke-linejoin: round;
}

/* Light against the dark chassis, so the forks' way in is easy to spot */
.inside__fork-pocket {
  fill: color-mix(in srgb, var(--color-cream) 55%, var(--color-sage-40));
}

/* See-through, so the toilet and tanks show inside the shower */
.inside__glass-wall {
  fill: var(--color-emerald-80);
  fill-opacity: 0.35;
  stroke: var(--color-sage-40);
  stroke-width: 2;
  stroke-linejoin: round;
}

.inside__cell {
  stroke: var(--color-sage-40);
  stroke-width: 1;
}

.inside__steel {
  stroke: var(--color-sage-40);
  stroke-width: 7;
  stroke-linecap: square;
}

@media screen and (prefers-reduced-motion: no-preference) and (scripting: enabled) {
  /* The full-bleed stage is 100vw, which counts the scrollbar; clip that sliver instead of scrolling sideways. */
  body:has(.story) {
    overflow-x: clip;
  }

  /* Full bleed, and tall enough to scroll through every beat while the stage stays pinned */
  .story {
    width: 100vw;
    max-width: none;
    /* The screen's height as the screen-height controller measured it, so a phone's keyboard can't resize it */
    height: calc(var(--screen-height, 1vh) * 600);
    margin-inline: calc(50% - 50vw);

    /* Beats, in order: each goes from 0 to 1 across its window of --p */
    --carry-t: clamp(0, var(--p) / 0.1, 1);
    --lower-t: clamp(0, (var(--p) - 0.1) / 0.05, 1);
    --backout-t: clamp(0, (var(--p) - 0.15) / 0.06, 1);
    --zoom-t: clamp(0, (var(--p) - 0.19) / 0.06, 1);
    --peaks-t: clamp(0, (var(--p) - 0.25) / 0.06, 1);
    --slope-t: clamp(0, (var(--p) - 0.33) / 0.09, 1);
    --level-t: clamp(0, (var(--p) - 0.45) / 0.1, 1);
    --flat-t: clamp(0, (var(--p) - 0.58) / 0.08, 1);
    --real-t: clamp(0, (var(--p) - 0.82) / 0.08, 1);

    /* The same beats, eased in and out (smoothstep) */
    --carry: calc(var(--carry-t) * var(--carry-t) * (3 - 2 * var(--carry-t)));
    --lower: calc(var(--lower-t) * var(--lower-t) * (3 - 2 * var(--lower-t)));
    --backout: calc(var(--backout-t) * var(--backout-t) * (3 - 2 * var(--backout-t)));
    --zoom: calc(var(--zoom-t) * var(--zoom-t) * (3 - 2 * var(--zoom-t)));
    --peaks: calc(var(--peaks-t) * var(--peaks-t) * (3 - 2 * var(--peaks-t)));
    --slope: calc(var(--slope-t) * var(--slope-t) * (3 - 2 * var(--slope-t)));
    --level: calc(var(--level-t) * var(--level-t) * (3 - 2 * var(--level-t)));
    --flat: calc(var(--flat-t) * var(--flat-t) * (3 - 2 * var(--flat-t)));
    --real: calc(var(--real-t) * var(--real-t) * (3 - 2 * var(--real-t)));

    /* The ground tilts 10° on the slope and the home tilts with it, then levels; as the ground flattens again
       the left leg retracts in step, so the home stays level */
    --ground-tilt: calc(-10deg * var(--slope) * (1 - var(--flat)));
    --home-tilt: calc(10deg * (var(--level) - var(--slope)));
    --lean: calc(var(--slope) - var(--level));
    --intro-start: clamp(0, (0.06 - var(--p)) / 0.02, 1);
    --intro-end: clamp(0, (var(--p) - 0.88) / 0.03, 1);
    --intro: calc(var(--intro-start) + var(--intro-end));

    /* At the end the camera pulls back to make room for the heading; phones have the height already. */
    --final-zoom: 0.8;
    --final-drop: 60px;
  }

  .story__stage {
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100svh;
    overflow: hidden;
  }

  .story__photo {
    display: none;
  }

  .story__scene {
    display: block;
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    pointer-events: none;
  }

  /* The intro and captions sit over the sky, lined up with the page's column */
  .story__intro,
  .story__captions li {
    position: absolute;
    z-index: 1;
    top: var(--space-8);
    left: max(var(--space-4), calc((100% - var(--width-page)) / 2 + var(--space-4)));
    max-width: min(520px, calc(100% - 2 * var(--space-4)));
  }

  .story__intro {
    max-width: min(900px, calc(100% - 2 * var(--space-4)));
    opacity: var(--intro);
    /* Out of the way while hidden, so its Preorder link can't be clicked unseen */
    translate: 0 calc(-200vh * (1 - min(1, 100 * var(--intro))));
  }

  .story__intro:focus-within {
    opacity: 1;
    translate: none;
  }

  /* On a wide, short screen the home rises behind the captions, so each sits on frosted Cream */
  .story__captions li {
    width: fit-content;
    padding: var(--space-2) var(--space-4);
    margin-left: calc(-1 * var(--space-4));
    background: color-mix(in srgb, var(--color-cream) 82%, transparent);
    backdrop-filter: blur(6px);
    border-radius: var(--radius-md);
    font-size: var(--text-h2);
    line-height: 1.15;
  }

  /* Each caption, and the spirit level, shows between its own --from and --to */
  .story__captions li,
  .story__level {
    opacity: calc(clamp(0, (var(--p) - var(--from)) / 0.02, 1) * clamp(0, (var(--to) - var(--p)) / 0.02, 1));
  }

  .story__captions li:nth-child(1) {
    --from: 0.05;
    --to: 0.13;
  }

  .story__captions li:nth-child(2) {
    --from: 0.14;
    --to: 0.24;
  }

  .story__captions li:nth-child(3) {
    --from: 0.25;
    --to: 0.33;
  }

  .story__captions li:nth-child(4) {
    --from: 0.34;
    --to: 0.44;
  }

  .story__captions li:nth-child(5) {
    --from: 0.45;
    --to: 0.57;
  }

  .story__captions li:nth-child(6) {
    --from: 0.58;
    --to: 0.81;
  }

  .story__captions li span {
    opacity: clamp(0, (var(--p) - 0.69) / 0.02, 1);
  }

  /* A cream pill at the bottom of the stage, over the ground, so no crop can cut it off */
  .story__level {
    --from: 0.33;
    --to: 0.81;
    display: flex;
    position: absolute;
    z-index: 1;
    bottom: var(--space-8);
    left: 50%;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-4) var(--space-2) var(--space-2);
    background: var(--color-cream);
    border-radius: 999px;
    color: var(--color-emerald);
    font-size: var(--text-h3);
    font-weight: 400;
    translate: -50% 0;
  }

  /* The vial tilts with the home; its bubble drifts to the high end. */
  .story__vial {
    width: 110px;
    height: auto;
    rotate: var(--home-tilt);
  }

  .story__vial-bubble {
    transform: translateX(calc(68px * (var(--slope) - var(--level))));
  }

  /* Counts the home's tilt, 0° to 10° and back, as it leans and levels */
  .story__level-degrees {
    --tilt-degrees: calc(10 * var(--lean));
    min-width: 2.2em;
    counter-reset: tilt var(--tilt-degrees);
    font-variant-numeric: tabular-nums;
  }

  .story__level-degrees::after {
    content: counter(tilt) "°";
  }

  /* The camera follows the forklift in, zoomed out and set low under the intro, then closes in on the home
     once it's set down, leaving headroom for the home to tip. On a tall phone the story-lift controller raises
     it: by --lift at the start, so the scene sits just under the intro, then by --lift-zoomed once it has zoomed
     in, so the home sits mid-screen, where the photo then fades in. */
  .story__scene .camera {
    transform-box: view-box;
    transform-origin: 800px 1800px;
    transform:
      translate(
        calc((177px + 310px * (1 - var(--carry))) * (1 - var(--zoom))),
        calc(80px - 40px * var(--zoom) + var(--final-drop) * var(--real) - 1px * var(--lift, 0) * (1 - var(--zoom)) - 1px * var(--lift-zoomed, 0) * var(--zoom))
      )
      scale(calc(var(--fit, 1) * (0.62 + 0.38 * var(--zoom)) * (1 - (1 - var(--final-zoom)) * var(--real))));
  }

  /* The scene fills the stage's width, so a wide, short stage shows only a band of it above the ground and
     the home outgrows the frame. Zoom out to keep about 800 units above the ground: (1600 / aspect - 200) / 800. */
  @media (min-aspect-ratio: 17/10) {
    .story {
      --fit: 0.9;
    }
  }

  @media (min-aspect-ratio: 19/10) {
    .story {
      --fit: 0.78;
    }
  }

  @media (min-aspect-ratio: 21/10) {
    .story {
      --fit: 0.68;
    }
  }

  @media (min-aspect-ratio: 23/10) {
    .story {
      --fit: 0.6;
    }
  }

  .story__scene .mountains {
    opacity: calc(var(--peaks) * (1 - var(--flat)) * (1 - var(--real)));
    transform: translateY(calc(120px * (1 - var(--peaks))));
  }

  .story__scene .trees {
    opacity: calc(min(1, 1 - var(--peaks) + var(--flat)) * (1 - var(--real)));
  }

  .story__scene .far-ground {
    opacity: calc(1 - var(--real));
  }

  /* Tilts about the home's right foot, the uphill one */
  .story__scene .ground-tilt {
    transform-box: view-box;
    transform-origin: 1153px 1800px;
    transform: rotate(var(--ground-tilt));
    opacity: calc(1 - var(--real));
  }

  /* On load, the forklift drives in from off screen, wheels turning; scrolling carries on from there. */
  .story__scene .arrival {
    animation: story-arrive 2.4s cubic-bezier(0.2, 0.7, 0.3, 1) both;
  }

  @keyframes story-arrive {
    from {
      transform: translateX(-2000px);
    }
  }

  .story__scene .forklift__spin {
    transform-box: fill-box;
    transform-origin: center;
    animation: story-spin 2.4s cubic-bezier(0.2, 0.7, 0.3, 1) both;
  }

  @keyframes story-spin {
    from {
      transform: rotate(var(--spin));
    }
  }

  .story__scene .forklift {
    transform: translateX(calc(-500px * (1 - var(--carry)) - 2000px * var(--backout)));
  }

  /* A wheel turns 180 / (pi * radius) degrees per unit it rolls. */
  .story__scene .forklift__wheel {
    --spin: -1592deg;
    --roll: 0.796deg;
    transform-box: fill-box;
    transform-origin: center;
    transform: rotate(calc(var(--roll) * (500 * var(--carry) - 2000 * var(--backout))));
  }

  .story__scene .forklift__wheel--rear {
    --spin: -2046deg;
    --roll: 1.023deg;
  }

  .story__scene .forklift__lift,
  .story__scene .home-lift {
    transform: translateY(calc(-150px * (1 - var(--lower))));
  }

  .story__scene .home-carry {
    transform: translateX(calc(-500px * (1 - var(--carry))));
  }

  /* Fainter while the home is up on the forks */
  .story__scene .shadow {
    transform-box: view-box;
    transform-origin: 1153px 1800px;
    transform: rotate(var(--ground-tilt));
    opacity: calc(0.1 + 0.12 * var(--lower));
  }

  .story__scene .home-tilt {
    transform-box: view-box;
    transform-origin: 1153px 1800px;
    transform: rotate(var(--home-tilt));
  }

  /* tan 10° times the distance between the feet (2137 in the photo's pixels, the drawing's units): keeps the
     foot on the slope */
  .story__scene .leg--left .leg__extend {
    transform: translateY(calc(377px * (var(--level) - var(--flat))));
  }

  /* Each foot turns by the angle between the ground and the home, about the middle of its sole, so it lies flat
     on the ground: the ground's tilt less the home's, which is -10° times (level - slope × flat). */
  .story__scene .leg__foot {
    transform-box: fill-box;
    transform-origin: 50% 100%;
    transform: rotate(calc(-10deg * (var(--level) - var(--slope) * var(--flat))));
  }

  .story__scene .home-cartoon {
    opacity: calc(1 - var(--real));
  }

  .story__scene .home-photo {
    opacity: var(--real);
  }

  @media (max-width: 719px) {
    .story {
      --final-zoom: 1;
      /* Lifts the home at the end, leaving room for the Preorder button below it */
      --final-drop: -150px;
    }

    /* Phones show only a sliver of ground under the home, but plenty of sky above it; the story-lift controller
       puts the level just above the home once it's centered. */
    .story__level {
      top: var(--level-top, 42%);
      bottom: auto;
    }

    /* At the end the photo fills the width under a long heading, so the button moves below the home: the
       intro's button shows only at the start, the story's own only at the end. Each is out of the way while
       hidden, and shows when focused. */
    .story__intro .button {
      opacity: var(--intro-start);
      translate: 0 calc(-200vh * (1 - min(1, 100 * var(--intro-start))));
    }

    .story__cta {
      display: inline-flex;
      position: absolute;
      z-index: 1;
      bottom: var(--cta-bottom, var(--space-8));
      left: 50%;
      opacity: var(--intro-end);
      translate: -50% calc(200vh * (1 - min(1, 100 * var(--intro-end))));
    }

    .story__intro .button:focus-visible {
      opacity: 1;
      translate: none;
    }

    .story__cta:focus-visible {
      opacity: 1;
      translate: -50% 0;
    }
  }

  /* Inside, assembled as the visitor scrolls: each part arrives in the order the home is built, and its
     caption lights up. Then a flatbed carries it, and on site hurricane anchors hold it down. Tall enough to
     scroll through every part while the stage stays pinned. */
  .inside {
    height: calc(var(--screen-height, 1vh) * 760);

    --chassis-t: clamp(0, var(--p) / 0.045, 1);
    --legs-t: clamp(0, (var(--p) - 0.06) / 0.04, 1);
    --living-t: clamp(0, (var(--p) - 0.12) / 0.04, 1);
    --table-out-t: clamp(0, (var(--p) - 0.16) / 0.04, 1);
    --bed-t: clamp(0, (var(--p) - 0.21) / 0.03, 1);
    --bed-fold-t: clamp(0, (var(--p) - 0.25) / 0.05, 1);
    --kitchen-t: clamp(0, (var(--p) - 0.33) / 0.045, 1);
    --water-t: clamp(0, (var(--p) - 0.4) / 0.045, 1);
    --hvac-t: clamp(0, (var(--p) - 0.47) / 0.045, 1);
    --frame-t: clamp(0, (var(--p) - 0.54) / 0.04, 1);
    --skin-t: clamp(0, (var(--p) - 0.59) / 0.04, 1);
    --roof-t: clamp(0, (var(--p) - 0.64) / 0.035, 1);
    --solar-t: clamp(0, (var(--p) - 0.675) / 0.035, 1);
    --starlink-t: clamp(0, (var(--p) - 0.72) / 0.04, 1);
    --flatbed-in-t: clamp(0, (var(--p) - 0.79) / 0.04, 1);
    --straps-t: clamp(0, (var(--p) - 0.83) / 0.04, 1);
    --flatbed-out-t: clamp(0, (var(--p) - 0.88) / 0.04, 1);
    --anchors-t: clamp(0, (var(--p) - 0.9) / 0.05, 1);

    --chassis: calc(var(--chassis-t) * var(--chassis-t) * (3 - 2 * var(--chassis-t)));
    --legs: calc(var(--legs-t) * var(--legs-t) * (3 - 2 * var(--legs-t)));
    --living: calc(var(--living-t) * var(--living-t) * (3 - 2 * var(--living-t)));
    --table-out: calc(var(--table-out-t) * var(--table-out-t) * (3 - 2 * var(--table-out-t)));
    --bed: calc(var(--bed-t) * var(--bed-t) * (3 - 2 * var(--bed-t)));
    --bed-fold: calc(var(--bed-fold-t) * var(--bed-fold-t) * (3 - 2 * var(--bed-fold-t)));
    --kitchen: calc(var(--kitchen-t) * var(--kitchen-t) * (3 - 2 * var(--kitchen-t)));
    --water: calc(var(--water-t) * var(--water-t) * (3 - 2 * var(--water-t)));
    --hvac: calc(var(--hvac-t) * var(--hvac-t) * (3 - 2 * var(--hvac-t)));
    --frame: calc(var(--frame-t) * var(--frame-t) * (3 - 2 * var(--frame-t)));
    --skin: calc(var(--skin-t) * var(--skin-t) * (3 - 2 * var(--skin-t)));
    --roof: calc(var(--roof-t) * var(--roof-t) * (3 - 2 * var(--roof-t)));
    --solar: calc(var(--solar-t) * var(--solar-t) * (3 - 2 * var(--solar-t)));
    --starlink: calc(var(--starlink-t) * var(--starlink-t) * (3 - 2 * var(--starlink-t)));
    --flatbed-in: calc(var(--flatbed-in-t) * var(--flatbed-in-t) * (3 - 2 * var(--flatbed-in-t)));
    --straps: calc(var(--straps-t) * var(--straps-t) * (3 - 2 * var(--straps-t)));
    --flatbed-out: calc(var(--flatbed-out-t) * var(--flatbed-out-t) * (3 - 2 * var(--flatbed-out-t)));
    --anchors: calc(var(--anchors-t) * var(--anchors-t) * (3 - 2 * var(--anchors-t)));
  }

  .inside__stage {
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100svh;
    grid-template-rows: minmax(0, 1fr);
    padding-block: var(--space-8);
  }

  /* Fills its cell and fits the drawing inside it (meet), so it never spills over the captions. Parts drop in
     from above the drawing, so it doesn't clip them. */
  .inside__scene {
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
    overflow: visible;
  }

  /* Each caption lights up while its part arrives: from --from to --to */
  .inside__captions li {
    opacity: calc(0.28 + 0.72 * clamp(0, (var(--p) - var(--from)) / 0.02, 1) * clamp(0, (var(--to) - var(--p)) / 0.02, 1));
  }

  .inside__captions li:nth-child(1) {
    --from: 0;
    --to: 0.06;
  }

  .inside__captions li:nth-child(2) {
    --from: 0.06;
    --to: 0.12;
  }

  .inside__captions li:nth-child(3) {
    --from: 0.12;
    --to: 0.21;
  }

  .inside__captions li:nth-child(4) {
    --from: 0.21;
    --to: 0.33;
  }

  .inside__captions li:nth-child(5) {
    --from: 0.33;
    --to: 0.4;
  }

  .inside__captions li:nth-child(6) {
    --from: 0.4;
    --to: 0.47;
  }

  .inside__captions li:nth-child(7) {
    --from: 0.47;
    --to: 0.54;
  }

  .inside__captions li:nth-child(8) {
    --from: 0.54;
    --to: 0.64;
  }

  .inside__captions li:nth-child(9) {
    --from: 0.64;
    --to: 0.72;
  }

  .inside__captions li:nth-child(10) {
    --from: 0.72;
    --to: 0.79;
  }

  .inside__captions li:nth-child(11) {
    --from: 0.79;
    --to: 0.88;
  }

  .inside__captions li:nth-child(12) {
    --from: 0.88;
    --to: 1.1;
  }

  /* Each part drops into place and fades in; the legs come down out of the chassis. */
  .inside__part--chassis {
    opacity: var(--chassis);
    transform: translateY(calc(-300px * (1 - var(--chassis))));
  }

  /* The legs come down out of the chassis, and tuck back up into it (0.45' up) while the flatbed carries it. */
  .inside__part--legs {
    opacity: var(--legs);
    transform: translateY(calc(-60px * (1 - var(--legs)) - 16.5px * (var(--flatbed-in) - var(--flatbed-out))));
  }

  .inside__part--frame {
    opacity: var(--frame);
    transform: translateY(calc(-260px * (1 - var(--frame))));
  }

  .inside__part--water {
    opacity: var(--water);
    transform: translateY(calc(-240px * (1 - var(--water))));
  }

  .inside__part--kitchen {
    opacity: var(--kitchen);
    transform: translateY(calc(-240px * (1 - var(--kitchen))));
  }

  .inside__part--living {
    opacity: var(--living);
    transform: translateY(calc(-240px * (1 - var(--living))));
  }

  .inside__part--bed {
    opacity: var(--bed);
    transform: translateY(calc(-240px * (1 - var(--bed))));
  }

  .inside__part--hvac {
    opacity: var(--hvac);
    transform: translateY(calc(-240px * (1 - var(--hvac))));
  }

  .inside__part--skin {
    opacity: var(--skin);
    transform: translateY(calc(-80px * (1 - var(--skin))));
  }

  .inside__part--roof {
    opacity: var(--roof);
    transform: translateY(calc(-260px * (1 - var(--roof))));
  }

  .inside__part--solar {
    opacity: var(--solar);
    transform: translateY(calc(-220px * (1 - var(--solar))));
  }

  .inside__part--starlink {
    opacity: var(--starlink);
    transform: translateY(calc(-160px * (1 - var(--starlink))));
  }

  .inside__part--straps {
    opacity: var(--straps);
    transform: translateY(calc(-120px * (1 - var(--straps))));
  }

  /* The flatbed drives in along the home's length from the upper left, 20' away, and on out to the lower right. */
  .inside__part--flatbed,
  .inside__part--wind,
  .inside__strap--deck,
  .inside__auger {
    display: inline;
  }

  .inside__part--flatbed {
    --drive: calc(var(--flatbed-out) - 1 + var(--flatbed-in));
    opacity: calc(var(--flatbed-in) * (1 - var(--flatbed-out)));
    transform: translate(calc(600px * var(--drive)), calc(300px * var(--drive)));
  }

  /* The straps move from the flatbed's deck to the anchors as the anchors drop in. Each anchor's auger shows
     on the way down and fades as it lands, screwed into the ground. */
  .inside__strap--deck {
    opacity: calc(1 - var(--anchors));
  }

  .inside__strap--anchor {
    opacity: var(--anchors);
  }

  .inside__part--anchors {
    opacity: min(1, var(--anchors) * 3);
    transform: translateY(calc(-140px * (1 - var(--anchors))));
  }

  .inside__auger {
    opacity: calc(1 - var(--anchors));
  }

  /* Gusts blow while the anchors go in: dashes run along each line, a little out of step. */
  .inside__part--wind {
    opacity: min(1, var(--anchors) * 2);
  }

  .inside__gust {
    animation: inside-wind 1.6s linear infinite;
  }

  .inside__gust:nth-child(2) {
    animation-delay: -0.5s;
  }

  .inside__gust:nth-child(3) {
    animation-delay: -1.1s;
  }

  .inside__gust:nth-child(4) {
    animation-delay: -0.3s;
  }

  .inside__gust:nth-child(5) {
    animation-delay: -0.8s;
  }

  @keyframes inside-wind {
    from {
      stroke-dashoffset: 650;
    }

    to {
      stroke-dashoffset: 0;
    }
  }

  /* The table folds out and stays down; the bed folds down onto it. Each is drawn at every step of its fold, and a
     step shows while the fold is nearest it. */
  .inside__part--living {
    --fold: var(--table-out);
  }

  .inside__part--bed {
    --fold: var(--bed-fold);
  }

  .inside__folds > .inside__fold:nth-child(1) {
    --step: 0;
  }

  .inside__folds > .inside__fold:nth-child(2) {
    --step: 1;
  }

  .inside__folds > .inside__fold:nth-child(3) {
    --step: 2;
  }

  .inside__folds > .inside__fold:nth-child(4) {
    --step: 3;
  }

  .inside__folds > .inside__fold:nth-child(5) {
    --step: 4;
  }

  .inside__folds > .inside__fold:nth-child(6) {
    --step: 5;
  }

  .inside__folds > .inside__fold:nth-child(7) {
    --step: 6;
  }

  .inside__folds > .inside__fold:nth-child(8) {
    --step: 7;
  }

  .inside__folds > .inside__fold:nth-child(9) {
    --step: 8;
  }

  /* As specific as the still rule above, and later, so it wins while the scene plays */
  .inside__part .inside__fold {
    opacity: calc(clamp(0, (var(--fold) * 8 - var(--step) + 0.5) * 100, 1) * clamp(0, (var(--step) + 0.5 - var(--fold) * 8) * 100, 1));
  }

  /* Phones, and any window that isn't clearly wider than tall: the heading and the current caption above the
     drawing; the other captions wait in the same spot. Side by side, the full list and the drawing only both fit
     in a landscape window. */
  @media (max-width: 719px), (max-aspect-ratio: 4/3) {
    .inside__stage {
      grid-template-columns: minmax(0, 1fr);
      grid-template-rows: auto minmax(0, 1fr);
      gap: var(--space-4);
    }

    .inside__captions {
      list-style: none;
      padding-left: 0;
    }

    .inside__captions li {
      grid-area: 1 / 1;
      opacity: calc(clamp(0, (var(--p) - var(--from)) / 0.02, 1) * clamp(0, (var(--to) - var(--p)) / 0.02, 1));
    }

  }

  /* Where CSS can drive animations by scroll, it sets each scene's --p from that scene's own view timeline;
     elsewhere the scroll-scene controller sets it from the scroll position. */
  @supports (animation-timeline: view()) {
    .story,
    .inside {
      view-timeline: --scene block;
      animation: scene-progress linear both;
      animation-timeline: --scene;
      animation-range: contain;
    }

    @keyframes scene-progress {
      from {
        --p: 0;
      }

      to {
        --p: 1;
      }
    }
  }
}
