/* Scroll story: a picture that stays in view while the steps of a job scroll past, each step changing its own
   part of the picture. story.js sets --s1, --s2 ... (0 to 1, one per step) and --ss (the whole story).
   Every one falls back to 1, so with no script the picture is finished and the steps are a plain list.

   Set these on the story to fit the site:
     --ss-top        how far below the top of the screen the picture sticks (clear of a sticky header)
     --ss-top-small  the same on a phone
     --ss-bg         the page colour behind the picture on a phone (the steps pass under it)
     --ss-accent     the colour of the progress line and the step numbers */
.ss { --ss-top: 5.5rem; --ss-top-small: 4rem; --ss-bg: #fff; --ss-accent: currentColor;
  display: grid; grid-template-columns: minmax(0, 1.08fr) minmax(0, .92fr); gap: clamp(1.5rem, 5vw, 4.5rem); align-items: start; }

/* The picture */
.ss-stage { position: sticky; top: var(--ss-top); }
.ss-frame { position: relative; aspect-ratio: 4 / 3; border-radius: 18px; overflow: hidden; contain: paint; background: var(--ss-bg); }
.ss-layer { position: absolute; inset: 0; }
.ss-layer > svg, .ss-layer > img { display: block; width: 100%; height: 100%; object-fit: cover; }

/* Which step drives a layer: class ss-p2 means "follow step 2". */
.ss-p1 { --p: var(--s1, 1); } .ss-p2 { --p: var(--s2, 1); } .ss-p3 { --p: var(--s3, 1); } .ss-p4 { --p: var(--s4, 1); }
.ss-p5 { --p: var(--s5, 1); } .ss-p6 { --p: var(--s6, 1); } .ss-p7 { --p: var(--s7, 1); } .ss-p8 { --p: var(--s8, 1); }

/* How a layer arrives. */
.ss-wipe-right { clip-path: inset(0 calc((1 - var(--p, 1)) * 100%) 0 0); }
.ss-wipe-left { clip-path: inset(0 0 0 calc((1 - var(--p, 1)) * 100%)); }
.ss-wipe-down { clip-path: inset(0 0 calc((1 - var(--p, 1)) * 100%) 0); }
.ss-wipe-up { clip-path: inset(calc((1 - var(--p, 1)) * 100%) 0 0 0); }
.ss-iris { clip-path: circle(calc(var(--p, 1) * 90%) at var(--ss-x, 50%) var(--ss-y, 55%)); }
.ss-fade { opacity: var(--p, 1); }
.ss-rise { opacity: var(--p, 1); transform: translateY(calc((1 - var(--p, 1)) * 14px)); }

/* Something that travels with a wipe's edge (a roller, a hose, a saw): visible only while the wipe is moving. */
.ss-edge { position: absolute; top: 0; bottom: 0; left: calc(var(--p, 1) * 100%); width: 0; pointer-events: none;
  opacity: min(calc(var(--p, 1) * 14), calc((1 - var(--p, 1)) * 14), 1); }
.ss-edge-down { position: absolute; left: 0; right: 0; top: calc(var(--p, 1) * 100%); height: 0; pointer-events: none;
  opacity: min(calc(var(--p, 1) * 14), calc((1 - var(--p, 1)) * 14), 1); }

/* A caption on the picture that names the step now showing. */
.ss-caption { position: absolute; left: 12px; bottom: 12px; z-index: 3; display: grid; }
.ss-caption > * { grid-area: 1 / 1; opacity: 0; transition: opacity .25s; }
.ss[data-ss-at="1"] .ss-caption > :nth-child(1), .ss[data-ss-at="2"] .ss-caption > :nth-child(2), .ss[data-ss-at="3"] .ss-caption > :nth-child(3),
.ss[data-ss-at="4"] .ss-caption > :nth-child(4), .ss[data-ss-at="5"] .ss-caption > :nth-child(5), .ss[data-ss-at="6"] .ss-caption > :nth-child(6),
.ss[data-ss-at="7"] .ss-caption > :nth-child(7), .ss[data-ss-at="8"] .ss-caption > :nth-child(8) { opacity: 1; }
.ss:not(.ss-on) .ss-caption > :last-child { opacity: 1; }

/* The progress line under the picture. */
.ss-meter { height: 3px; margin-top: 14px; border-radius: 3px; background: color-mix(in srgb, var(--ss-accent) 18%, transparent); overflow: hidden; }
.ss-meter::after { content: ""; display: block; height: 100%; background: var(--ss-accent); transform-origin: left; transform: scaleX(var(--ss, 1)); }

/* The steps */
.ss-steps { list-style: none; margin: 0; padding: 0; counter-reset: ss; }
.ss-step { counter-increment: ss; padding: 1.1rem 0; }
.ss-step::before { content: counter(ss, decimal-leading-zero); display: block; font-size: .78rem; font-weight: 700; letter-spacing: .14em; color: var(--ss-accent); margin-bottom: .5rem; }
.ss-step > :first-child { margin-top: 0; }
.ss-step > :last-child { margin-bottom: 0; }

/* While the story is running: each step gets room to be scrolled through, and the one being read stands out. */
.ss-on .ss-step { min-height: 74vh; display: flex; flex-direction: column; justify-content: center; opacity: .34; transition: opacity .3s; }
.ss-on .ss-step.is-now { opacity: 1; }
.ss-on .ss-step:last-child { min-height: 58vh; }

@media (max-width: 860px) {
  .ss { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .ss-stage { top: var(--ss-top-small); z-index: 2; background: var(--ss-bg); padding: .5rem 0 .75rem; }
  .ss-frame { aspect-ratio: 16 / 11; max-height: 44vh; margin-inline: auto; }
  /* Each step is about as tall as the space below the picture, so the next one arrives as this one leaves. */
  .ss-on .ss-step { min-height: 46vh; justify-content: flex-start; padding-top: 1.1rem; }
  .ss-on .ss-step:last-child { min-height: 40vh; }
}
/* Still: scripts off, or the visitor asks for less motion. The finished picture, the steps as a list. */
.ss:not(.ss-on) .ss-stage { position: static; }
.ss:not(.ss-on) .ss-edge, .ss:not(.ss-on) .ss-edge-down, .ss:not(.ss-on) .ss-meter { display: none; }
@media print {
  .ss { display: block; }
  .ss-stage { position: static; }
  .ss-on .ss-step { min-height: 0; opacity: 1; }
  .ss-edge, .ss-edge-down, .ss-meter { display: none; }
}
