/* The Lab's assembling screens (js/assemble.js). A stage's main screenshot is cut into parts, built twice:
     .as-under   inside the .dk-shot, above its <img> and below the moment (z 2): parts at rest, behind the fingers
     .as-over    in the .dk-dev beside the shot, above the plate: parts in flight; assemble.js gives it the shot's own
                 width, height and matrix3d, so it lies on the glass
   Each part is a box in percent of its screen (--as-px/py/pw/ph) over a background that is the whole screenshot
   sized and shifted to show that rect (--as-bw/bh/bx/by); the file is set by assemble.js once the screen's <img> has loaded.
   The base rules are the still version: parts at rest, the over layer clear, the <img> showing.
   Without assemble.js (no script, reduced motion, a .dk-still stage) none of these exist. */

#lab .as-under{position:absolute;inset:0;z-index:1;pointer-events:none}
#lab .as-over{position:absolute;z-index:2;transform-origin:0 0;pointer-events:none;container-type:inline-size}
#lab .as-part{position:absolute;left:var(--as-px);top:var(--as-py);width:var(--as-pw);height:var(--as-ph);
  background-repeat:no-repeat;background-size:var(--as-bw) var(--as-bh);background-position:var(--as-bx) var(--as-by)}
#lab .as-under .as-part{opacity:1}
#lab .as-over .as-part{opacity:0;box-shadow:0 10px 22px rgba(0,0,0,.45)}
/* the rim of the screen, lit as the last part lands (centre clear, so the app stays readable) */
#lab .as-glow{position:absolute;inset:0;opacity:0;pointer-events:none;
  box-shadow:inset 0 0 0 max(2px,.6cqw) rgba(255,255,255,.9),inset 0 0 6cqw 1.6cqw rgba(255,255,255,.5)}
/* …and a warm halo that spills past the edge of the glass onto the bezel, so the landing reads on a light screenshot too.
   It is a child of the over layer, which nothing clips (the shot clips the rim); its own outer shadow is clear inside it */
#lab .as-halo{position:absolute;inset:0;opacity:0;pointer-events:none;
  box-shadow:0 0 max(5px,1.6cqw) max(2px,.6cqw) rgba(255,222,150,.95),0 0 max(18px,7cqw) max(4px,1.8cqw) rgba(255,205,110,.6)}

/* ─ the exploded view: a pinned stage (data-as=x, a · c), scrubbed by --p (js/device.js) ─
   Part i starts to come down at p = --as-s and has landed 0.06 later (--as-k runs 0 → 1 across that), so the parts are
   pressed down one at a time and the last one lands at --as-pend (js/assemble.js). While a part is up (--as-k < 1) its
   over twin hovers above the glass, shifted, tilted and enlarged by --as-ex/-ey/-er/-es; the moment it lands the under twin
   takes over (opacity is a 0 → 1 step across the last thousandth of --as-k). The <img> shows once the last part is down.
   Scoped to .as-ready, which assemble.js sets once every screen on the stage has its file: until then the stage is the
   still version, because a part without its file would fly clear. Transform and opacity only, and no script per frame. */
#lab .as-on.as-ready[data-as=x].dk-live .as-part{--as-k:clamp(0,calc((var(--p) - var(--as-s)) / .06),1)}
#lab .as-on.as-ready[data-as=x].dk-live .as-over .as-part{
  box-shadow:none;
  transform:translate(calc((1 - var(--as-k)) * var(--as-ex)),calc((1 - var(--as-k)) * var(--as-ey))) rotate(calc((1 - var(--as-k)) * var(--as-er))) scale(calc(1 + (1 - var(--as-k)) * var(--as-es)));
  opacity:calc((1 - var(--as-k)) * 1000)}
/* a part's shadow is its ::after (opacity only: a box-shadow is never animated) and fades as the part nears the glass */
#lab .as-on.as-ready[data-as=x].dk-live .as-over .as-part::after{content:"";position:absolute;inset:0;pointer-events:none;
  box-shadow:0 10px 22px rgba(0,0,0,.45);opacity:calc(1 - var(--as-k))}
#lab .as-on.as-ready[data-as=x].dk-live .as-under .as-part{opacity:calc(var(--as-k) * 1000 - 999)}
#lab .as-on.as-ready[data-as=x].dk-live .dk-shot>img{opacity:calc((var(--p) - var(--as-pend)) * 1000)}
/* the rim and the halo peak at pend + 0.03 and are out by pend + 0.06 */
#lab .as-on.as-ready[data-as=x].dk-live :is(.as-glow,.as-halo){opacity:calc(min(var(--p) - var(--as-pend),var(--as-pend) + .06 - var(--p)) * 30)}
/* …and the hand that holds the device feels the last part land: the plate, the glass and the parts above it sink 3px
   together and come back (the CSS translate property, which .dk-dev itself already uses) */
#lab .as-on.as-ready[data-as=x].dk-live .dk-dev>:is(.dk-plate,.dk-shot,.as-over){
  translate:0 calc(clamp(0,min(var(--p) - var(--as-pend),var(--as-pend) + .06 - var(--p)) / .03,1) * 3px)}

/* ─ rain in: a tile (data-as=r, the b stages) rains its parts in when it rises, in about 1.6 s (js/assemble.js) ─
   assemble.js puts .as-busy and .as-rain on the figure when it starts and takes them off when the last part has landed, so
   none of this applies to a tile that never becomes .as-ready: it stays the still version. (.as-busy is also a smash's, on
   either kind of stage, and hides the screenshots and the moments the same way.) Each part carries its own numbers
   (--as-rx/-ry: where it starts, in cqw of its screen; --as-rr: its turn; --as-cx/-cy: the bend of its path; --as-rd: when
   it leaves). The over part flies in and is gone as it lands; its under twin appears at that same moment, behind the fingers.
   Every screen's <img> and every moment is hidden while it rains, so nothing but the parts is on the glass. Transform and
   opacity only (the over part's shadow is the base rule's, still, never animated), and no script per frame. */
#lab .as-on.as-busy .dk-shot>img,
#lab .as-on.as-busy :is(.dk-shot,.dk-dev)>.kn-moment{visibility:hidden}
#lab .as-on[data-as=r].as-rain .as-over .as-part{
  animation:as-rain 700ms cubic-bezier(.3,.7,.4,1.2) both;animation-delay:var(--as-rd)}
#lab .as-on[data-as=r].as-rain .as-under .as-part{
  animation:as-land 1ms linear both;animation-delay:calc(var(--as-rd) + 700ms)}
@keyframes as-rain{
  0%{transform:translate(var(--as-rx),var(--as-ry)) rotate(var(--as-rr)) scale(.5);opacity:1}
  40%{transform:translate(calc(var(--as-rx) * .25 + var(--as-cx)),calc(var(--as-ry) * .25 + var(--as-cy))) rotate(calc(var(--as-rr) * .2)) scale(1.18)}
  99.9%{transform:none;opacity:1}
  100%{opacity:0}}
@keyframes as-land{from{opacity:0}to{opacity:1}}
/* the snap: a white flash on the part as it lands. It is the under part's ::after, since the over part's is the x stages'
   shadow; and it outlasts the rain by a moment, so .as-caught keeps it going (the same animation, not a new one). A smash's
   rebuild is .as-caught too, but .as-mended: its parts never snap */
#lab .as-on[data-as=r]:is(.as-rain,.as-caught:not(.as-mended)) .as-under .as-part::after{content:"";position:absolute;inset:0;pointer-events:none;
  background:#fff;animation:as-snap 220ms linear both;animation-delay:calc(var(--as-rd) + 700ms)}
@keyframes as-snap{0%,100%{opacity:0}50%{opacity:.7}}

/* …and the hand that holds the device catches it: for 450 ms after the last part lands (a tile's rain, or a rebuilt stage that is
   assembled), the plate, the glass and the parts above it sink 3px and come back (the CSS translate property, which .dk-dev itself
   already uses for the rise; never .dk-dev), and the rim and the halo flare once, as they do when scroll presses a pinned stage
   together. An animation out-ranks the scroll-linked translate and opacity of a pinned stage, and hands them back when it ends */
#lab .as-on.as-caught .dk-dev>:is(.dk-plate,.dk-shot,.as-over){animation:as-catch 260ms ease-in-out}
#lab .as-on.as-caught :is(.as-glow,.as-halo){animation:as-flare 400ms ease-out}
@keyframes as-catch{0%,100%{translate:0 0}50%{translate:0 3px}}
@keyframes as-flare{0%,100%{opacity:0}25%{opacity:.9}}

/* ─ smash: a tap or a click on an assembled stage's device (data-as=x or r) throws its parts clear and they come home, in about
   1.2 s (js/assemble.js) ─
   assemble.js puts .as-busy and .as-smash on the figure and takes them off when the last part is home. Each part carries its
   own throw (--as-sx/-sy: where it flies, in cqw of its screen; --as-sr: its turn). While it flies every over part shows and
   every under twin is clear; the selectors carry one more class than the pinned stage's scroll-linked rules above, so they win
   without !important. The keyframes have no end: a part comes home to whatever its underlying transform is, which on a pinned
   stage is the live --as-k state (where scroll has put it by then). A part casts its shadow only while it is off the glass: the
   shadow is its ::after (on a tile too: the over part's own box-shadow, which the rain keeps, is off here), and as-shade fades it
   in as the part leaves, to full at the far end of the throw, and out as the part comes home, on the part's own delay, duration
   and curve (so it is clear while the part waits its turn, and gone when it lands, on a light screen too). Transform and opacity
   only; no shadow is animated. */
#lab .as-on.as-ready.as-smash.dk-live[data-as] .as-over .as-part{
  box-shadow:none;opacity:1;animation:as-smash 1100ms cubic-bezier(.2,.8,.3,1) backwards;animation-delay:calc(var(--as-i) * 15ms)}
#lab .as-on.as-ready.as-smash.dk-live[data-as] .as-under .as-part{opacity:0}
#lab .as-on.as-ready.as-smash.dk-live[data-as] .as-over .as-part::after{content:"";position:absolute;inset:0;pointer-events:none;
  box-shadow:0 10px 22px rgba(0,0,0,.45);animation:as-shade 1100ms cubic-bezier(.2,.8,.3,1) backwards;animation-delay:calc(var(--as-i) * 15ms)}
#lab .as-on.as-ready.as-smash.dk-live[data-as=r] .as-over .as-part::after{opacity:0}
@keyframes as-smash{
  0%{transform:none}
  38%{transform:translate(var(--as-sx),var(--as-sy)) rotate(var(--as-sr)) scale(.92)}}
@keyframes as-shade{
  0%{opacity:0}
  38%{opacity:1}}
/* an assembled stage's device is something to tap (a pinned stage's is, strictly, only once scroll has assembled it); the
   browser's own tap flash is off, as it is on every other tap target of the site, for the parts are the answer (the property
   inherits, so what inside the device does its own job on a tap, and is no smash, keeps the browser's flash) */
#lab .as-on.as-ready:not(.as-busy) .dk-dev{cursor:pointer}
#lab .as-on .dk-dev{-webkit-tap-highlight-color:transparent}
#lab .as-on .dk-dev :is(a,button,input,select,textarea,label,[role=button]){-webkit-tap-highlight-color:initial}

/* ─ the desktop fit: on a wider screen a device grows by --fit toward the cap its markup gives it, as far as its room allows
   (js/assemble.js, the phone's --z is css/device.css's) ─
   The box is --fit times its old width (--w0) and its negative margins give the difference back, so it keeps its old
   footprint: nothing around it moves, and the plate grows down and to both sides from the same top while the stage crops what
   passes its bottom (a margin percentage is of the width, so the bottom one is the old width times the plate's shape,
   --fit-hw). A moment that spans the device keeps the old box, as it does under the phone's zoom. */
@media (min-width:620px){
  #lab .dk .dk-dev[style*="--fit"]{--w0:min(100%,calc(var(--dh) * var(--ar)));width:calc(var(--w0) * var(--fit));
    margin:0 calc(var(--w0) * (1 - var(--fit)) / 2) calc(var(--w0) * (1 - var(--fit)) * var(--fit-hw))}
  #lab .dk .dk-dev.dk-full[style*="--fit"]{--w0:min(100%,960px)}
  #lab .dk .dk-dev[style*="--fit"]>.kn-moment{right:auto;width:calc(100% / var(--fit));left:calc((var(--fit) - 1) / 2 / var(--fit) * 100%)}
}
