/* tweakflix — MOTION: the product field.
   ==================================================================
   THE PATTERN. This is the one way this brand animates product marks,
   and it is a system component rather than a homepage decoration.

   A field is any element that already contains real product tiles in
   the finished, static composition. Script measures those tiles, paints
   them onto a canvas laid over the same box, and springs them into
   place; the DOM tiles go transparent for exactly as long as the canvas
   is up. Nothing is invented on the canvas: the bed fill, the mark ink,
   the radius and both boxes are read back off the live CSS, so the
   painted tile is the CSS tile and the two cannot drift.

   IT DOES NOT STOP. After the entrance the field keeps breathing —
   1.1px and 0.57 degrees per body on beats that never coincide — and a
   spotlight steps through the plans every six seconds, taking the
   purchase card with it. Both are painted on the canvas; what CSS owns
   is section 1b below.

   WHAT MOVES IS THE INVENTORY. Not particles, not an abstract field —
   the same marks, at the same size, in the same brand colours the
   catalogue uses 700px below. The spectacle and the merchandising are
   one gesture.

   THREE THINGS THIS FILE OWES THE PAGE
   1. The static composition never depends on it. Every rule here is
      either inert or scoped to .is-live, which only script can set.
   2. It never delays first paint. There is no entry state to undo:
      the tiles are visible, and go transparent only once a first
      canvas frame exists.
   3. It disappears under prefers-reduced-motion, with JS off, and on a
      refused 2D context, leaving the page exactly as authored.

   Specimen, parameters, degradations and budget:
   styleguide/components-motion.html
   Behaviour: styleguide/assets/product-field.js
   ================================================================== */

/* ------------------------------------------------------------------
   1. THE HOST
   The element the canvas covers. It is already a positioning context
   on every page that uses one; this states the requirement rather than
   assuming it.
   ------------------------------------------------------------------ */
.c-field {
  position: relative;

  /* How far the canvas may paint past the host. A tile leaning toward a
     pointer near the host's edge would otherwise be sliced by the canvas
     bounds, which reads as a rendering fault rather than as motion. */
  --field-bleed: 1.5rem;
}

/* The canvas is laid over the host's whole box plus that bleed, never a
   layout participant and never a hit target: every control underneath stays
   clickable and every caption stays selectable.

   `color` is not decoration here. It is the one value the canvas cannot read
   off a tile — the ink its drop shadow is drawn in — so it is declared in CSS
   with the rest of the palette and read back by script, rather than being
   written as a number in JavaScript. */

.c-field__canvas {
  position: absolute;
  inset-block-start: calc(var(--field-bleed) * -1);
  inset-inline-start: calc(var(--field-bleed) * -1);
  inline-size: calc(100% + var(--field-bleed) * 2);
  block-size: calc(100% + var(--field-bleed) * 2);
  /* base.css caps every replaced element at the width of its container. The
     bleed is the one case where that cap is wrong: it would clip the canvas
     back to the host box and slice a tile leaning past the right edge. */
  max-inline-size: none;
  max-block-size: none;
  display: block;
  pointer-events: none;
  color: color-mix(in srgb, var(--color-ink) 14%, transparent);
}

/* The DOM tile stays in the document — it is what holds the layout, and
   what the canvas measures every time the field is re-laid out — and
   only gives up its ink while the canvas is live. */
.c-field.is-live .c-field__body { opacity: 0; }

/* The caption layer sits above the canvas so a mark in flight still
   says which product it is by the time it lands. It is text, so it
   arrives by fading rather than by moving: the travel belongs to the
   object, not to its label. */
.c-field__caption {
  position: relative;
  z-index: var(--layer-raised);
}

.c-field.is-live .c-field__caption {
  animation: c-field-caption var(--duration-reveal-slow) var(--ease-reveal) both;
  animation-delay: calc(var(--stagger-step) * 5);
}

@keyframes c-field-caption {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ------------------------------------------------------------------
   1b. THE TWO IDLE LAYERS
   The canvas owns both — the ambient wander and the spotlight's scale,
   lift and ink are painted, not animated in CSS. What CSS owns is the
   part of each that lives in the DOM: the caption of a plan that has
   stepped back, and the purchase card's crossfade.

   The hierarchy this defends, loudest last:
     ticker   constant, slow, peripheral — texture, and it never
              changes speed for anything
     ambient  1.1px and 0.57deg per body, 11-27s beats — life, never
              movement; two frames 200ms apart differ by ~0.1px
     spotlight  every 6s, one plan forward and the card following it —
              the only thing on this page that reads as an event
   Three motions coexist only because two of them are below the
   threshold of "something moved". If a fourth is ever proposed, one of
   these has to go.
   ------------------------------------------------------------------ */

/* A plan that is not the one in front loses a step of contrast in its
   caption too, so the label agrees with the tile it belongs to. */
.c-field.is-live [data-plan-label] {
  transition: opacity var(--duration-lift) var(--ease-lift);
}

.c-field.is-live .is-recede [data-plan-label] { opacity: 0.7; }

/* The purchase card crossfades between plans: out over --duration-exit,
   which is what the script waits before rewriting every field of it, and
   back in over the same step. A cut would read as a glitch and a slide
   would compete with the spotlight. */
[data-pick-live] {
  transition: opacity var(--duration-exit) var(--ease-out);
}

[data-pick-live].is-swap { opacity: 0; }

/* Belt and braces. Script returns before it touches the DOM under
   reduced motion, so .is-live is never set and these rules never
   match; they exist so that a field left live by a change of
   preference mid-session still resolves to the static composition. */
@media (prefers-reduced-motion: reduce) {
  .c-field.is-live .c-field__body { opacity: 1; }
  .c-field.is-live .c-field__canvas { display: none; }
  .c-field.is-live .c-field__caption { animation: none; }
  .c-field.is-live [data-plan-label] { transition: none; opacity: 1; }
  [data-pick-live] { transition: none; }
  [data-pick-live].is-swap { opacity: 1; }
}

/* A field inside a gutter-width container would push its bleed past the
   viewport and widen the document, which is a layout defect whatever caused
   it. The section that holds a field ends it at its own inline edge and
   leaves the block axis alone. */
.ps-section:has(.c-field) { overflow-x: clip; }

/* ------------------------------------------------------------------
   2. THE SPECIMEN FIELD
   The canonical demonstration used on components-motion.html: a bed,
   five real plans, one caption each. A page adopting the pattern
   supplies its own composition and keeps only the .c-field hooks.
   ------------------------------------------------------------------ */
.c-field--specimen {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: var(--space-6) var(--space-8);
  padding: var(--space-6);
  border-radius: var(--radius-xl);
  background-color: var(--color-surface-tile);
  box-shadow: var(--edge-light);
}

.c-field--specimen .c-field__caption {
  display: grid;
  justify-items: center;
  gap: var(--space-2);
}

.c-field--specimen .c-tile {
  /* The field's own surface, as a property list rather than a class:
     --tile-mark is gone with the geometric figures, and the mark inside is
     now sized by --tile-subject-pct against the bed the component builds. */
  --tile-size: 5rem;
  --tile-radius: var(--radius-xl);
  --tile-pad: var(--space-3);
  box-shadow: var(--edge-light), var(--elevation-raised);
}

@media (min-width: 48rem) {
  .c-field--specimen {
    gap: var(--space-8) var(--space-10);
    padding: var(--space-8);
  }

  .c-field--specimen .c-tile { --tile-size: 6.5rem; }
}

.c-field__name {
  max-width: none;
  font-size: var(--text-sm-size);
  line-height: var(--text-sm-line);
  letter-spacing: var(--text-sm-track);
  font-weight: var(--weight-semibold);
  color: var(--color-ink);
  white-space: nowrap;
}

/* ------------------------------------------------------------------
   3. THE PARAMETER SHEET
   A specimen page has to state the numbers, or the next person guesses
   them. Two columns from tablet up, one on the phone.
   ------------------------------------------------------------------ */
.c-field-doc {
  display: grid;
  gap: var(--space-4);
  margin-block-start: var(--space-5);
}

.c-field-doc > div {
  display: grid;
  gap: var(--space-1);
  padding-block-end: var(--space-4);
  border-block-end: 1px solid var(--color-border);
}

.c-field-doc dt {
  font-size: var(--text-sm-size);
  line-height: var(--text-sm-line);
  font-weight: var(--weight-semibold);
  color: var(--color-ink);
}

.c-field-doc dd {
  max-width: var(--measure);
  font-size: var(--text-sm-size);
  line-height: var(--text-sm-line);
  color: var(--color-ink-secondary);
}

.c-field-doc code {
  font-family: var(--font-mono);
  font-size: var(--text-xs-size);
  color: var(--color-ink);
}

@media (min-width: 48rem) {
  .c-field-doc > div {
    grid-template-columns: 14rem minmax(0, 1fr);
    column-gap: var(--space-6);
    align-items: baseline;
  }
}

