/* =============================================================================
 * TWE UI — motion.css   v2.1.0   2026-09-04
 *
 * Requires tokens.css loaded FIRST. Load order: tokens.css -> motion.css ->
 * component files. Components load last on purpose, so a component can
 * override a default here without reaching for !important.
 *
 * WHY: the 2026-09-04 audit found 26 of 39 tools with effectively zero motion
 * — every state change an instant flip, which is what makes a UI read as "a
 * legacy SAP type program" even when the colours are right: the eye gets no
 * continuity cue, so nothing feels connected to the click that caused it. A
 * few tools have the opposite problem. So motion is defined ONCE, here.
 *
 * ---------------------------------------------------------------------------
 * THE HOUSE MOTION SPEC
 * ---------------------------------------------------------------------------
 *  1. DURATION BY PURPOSE, never by taste:
 *       --twe-dur-instant  press feedback, the frame under the finger
 *       --twe-dur-fast     state changes: hover, press, toggle, focus, checked
 *       --twe-dur          movement and reveals: expand, collapse, tab, reorder
 *       --twe-dur-slow     larger travel: drawer, sheet
 *       --twe-dur-enter    OVERLAYS ONLY: modal, popover, toast, palette
 *  2. CURVE BY DIRECTION: --twe-ease for entrances (decelerating — it arrives
 *     and settles), --twe-ease-out for exits (it leaves without lingering),
 *     --twe-ease-in-out only for a loop that returns to its start.
 *  3. HARD CEILING 250ms on any interactive affordance. Past that the control
 *     feels laggy, not polished — and a user clicking twenty table rows pays
 *     the cost twenty times.
 *  4. TRANSFORM AND OPACITY ONLY. width/height/top/left/margin animate layout,
 *     and at the 125%/150% per-monitor DPI normal on these workstations a
 *     layout animation over a dense table visibly janks.
 *  5. WHAT MUST NEVER MOVE: table and list rows (data being scanned; movement
 *     breaks column geometry and fights sticky headers); text being read;
 *     anything under a cursor mid-task (a control must not slide out from
 *     under a pointer already on it); anything in the field theme — a tablet
 *     in direct sun is a task-completion device. Enforced in §7, not left to
 *     each tool to remember.
 *
 * THE ONE HARD RULE, and it is mechanical: every animation and transition in
 * every TWE tool must be REACHABLE BY TOKEN — use --twe-dur-* and --twe-ease-*,
 * never a literal `200ms` or cubic-bezier(). Not style policing: the
 * reduced-motion override in tokens.css works by ZEROING those tokens, so a
 * hardcoded duration is immune to it and the user who asked their OS for less
 * motion still gets it. Need longer than a token? Multiply one — a spinner is
 * calc(var(--twe-dur-slow) * 4), still token-reachable, still collapses.
 * ========================================================================== */


/* ================================================ 1. cross-document nav ====
 * THE HIGHEST-VALUE RULE IN THIS FILE.
 *
 * ~28 of 30 fleet tools are multi-page FastAPI apps: every nav click is a full
 * document load — white flash, header visibly rebuilding, which users read as
 * "the app restarted". Chromium supports CROSS-DOCUMENT view transitions,
 * giving those apps SPA-grade transitions with ZERO JavaScript, no framework,
 * no build step: the browser snapshots the old document, loads the new one and
 * interpolates. Requires same-origin navigation and BOTH documents opted in,
 * so motion.css belongs in the shared base template, not on one page.
 *
 * Floor: cross-document is Chromium 126+. Older Chromium (the Tatara /
 * Electron-28 floor at 120) ignores @view-transition and navigates exactly as
 * it does today — purely additive, no state in which it makes an old browser
 * worse.
 */
@supports (view-transition-name: none) {

  @view-transition { navigation: auto; }

  /* Default for every snapshot group: the shared house curve, so a page
   * transition feels like the same product as a button hover. */
  ::view-transition-group(*) {
    animation-duration: var(--twe-dur);
    animation-timing-function: var(--twe-ease);
  }

  /* Cross-fade AND a small lift. A pure cross-fade reads as a slideshow; 6px
   * of travel reads as a page arriving. */
  ::view-transition-old(root) {
    animation: twe-fade-out var(--twe-dur-fast) var(--twe-ease-out) both;
  }
  ::view-transition-new(root) {
    animation: twe-slide-up var(--twe-dur) var(--twe-ease) both;
  }

  /* Persistent chrome stays PUT across a navigation. Naming an element makes
   * the browser morph it between documents instead of cross-fading it with the
   * page; killing its old/new animation stops the double-exposure flicker you
   * get when a header is byte-identical on both pages. A view-transition-name
   * must be UNIQUE per document — one .twe-vt-header, one .twe-vt-main, one
   * .twe-vt-rail per page, or Chromium aborts the transition entirely and
   * falls back to a plain navigation. */
  .twe-vt-header { view-transition-name: twe-header; }
  .twe-vt-rail   { view-transition-name: twe-rail; }
  .twe-vt-main   { view-transition-name: twe-main; }

  ::view-transition-old(twe-header), ::view-transition-new(twe-header),
  ::view-transition-old(twe-rail),   ::view-transition-new(twe-rail) {
    animation: none;
    mix-blend-mode: normal;
  }
  ::view-transition-group(twe-header),
  ::view-transition-group(twe-rail) { animation-duration: var(--twe-dur-fast); }

  /* Reduced motion must be handled EXPLICITLY here: tokens.css zeroes
   * animation-duration on `*, *::before, *::after`, and the ::view-transition-*
   * tree is not matched by the universal selector — without this block a
   * reduced-motion user still gets the full page animation. */
  @media (prefers-reduced-motion: reduce) {
    ::view-transition-group(*),
    ::view-transition-old(*),
    ::view-transition-new(*) { animation: none !important; }
  }
}

/* PAIRING — for the app shell, deliberately NOT this file:
 * view transitions hide the load; Speculation Rules removes it. Adding a
 *   <script type="speculationrules">
 * block whose JSON is  {"prerender":[{"where":{"href_matches": <slash-star>},
 *   "eagerness":"moderate"}]}   (href_matches = a slash then an asterisk,
 * written literally in the template — it cannot appear inside a CSS comment)
 * to the base template makes same-origin navigations feel instant — the next
 * document is already parsed when the click lands. It needs a per-app URL
 * policy (never prerender a POST target or a destructive GET), so it lives in
 * the shell where that policy is known. Chromium-only, ignored elsewhere, no
 * egress beyond same-origin. */


/* ============================================== 2. keyframes (stable API) ===
 * Sibling stylesheets reference these BY NAME, so the names are frozen: a
 * missing animation-name is not an error, it just silently does nothing. Add,
 * never rename. Keyframes carry no duration — the consumer supplies it from a
 * token, which is what keeps the hard rule enforceable.
 */

/* CONTRACT for the consumer: the element must set a gradient background and
 * `background-size: 200% 100%`, or there is nothing for the position to travel
 * across and the skeleton sits inert. */
@keyframes twe-shimmer {
  from { background-position: 200% 0; }
  to   { background-position: -200% 0; }
}

@keyframes twe-fade-in  { from { opacity: 0; } to { opacity: 1; } }
@keyframes twe-fade-out { from { opacity: 1; } to { opacity: 0; } }

/* From 0.96, not 0.5: a big scale-up reads as a cartoon, a small one reads as
 * the element committing to the screen. */
@keyframes twe-scale-in {
  from { opacity: 0; transform: scale(0.96); }
  to   { opacity: 1; transform: scale(1); }
}
@keyframes twe-slide-up {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
/* Toasts land rather than slide — a hair of scale with the travel. */
@keyframes twe-toast-in {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes twe-spin { to { transform: rotate(360deg); } }

/* Opacity only: a pulsing scale on a live element makes the text around it
 * reflow-shimmer at 150% DPI. */
@keyframes twe-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.55; }
}
@keyframes twe-reveal-up {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Indeterminate progress: long by design, so it is built FROM a token rather
 * than written as a literal. Dual-encode (kit rule 4) — reduced motion freezes
 * this, so a loader must also carry a text label or aria-busy. */
.twe-spin { animation: twe-spin calc(var(--twe-dur-slow) * 4) linear infinite; }


/* ================================================== 3. reveal on scroll ====
 * Declared INSIDE prefers-reduced-motion: no-preference so these utilities do
 * not exist at all for a user who opted out — safer than declaring them and
 * relying on an override, because an override can lose to specificity while an
 * undeclared rule cannot.
 */
@media (prefers-reduced-motion: no-preference) {

  .twe-reveal {
    animation: twe-reveal-up var(--twe-dur-enter) var(--twe-ease) both;
    animation-delay: var(--twe-delay, 0s);
  }

  /* Stagger a list without touching markup. --twe-delay is the author knob;
   * its default is a token so it stays inside the reduced-motion zeroing. */
  .twe-stagger > * {
    animation: twe-reveal-up var(--twe-dur-enter) var(--twe-ease) both;
    animation-delay: calc(var(--twe-stagger-i, 0) * var(--twe-delay, var(--twe-dur-instant)));
  }
  .twe-stagger > *:nth-child(1) { --twe-stagger-i: 0; }
  .twe-stagger > *:nth-child(2) { --twe-stagger-i: 1; }
  .twe-stagger > *:nth-child(3) { --twe-stagger-i: 2; }
  .twe-stagger > *:nth-child(4) { --twe-stagger-i: 3; }
  .twe-stagger > *:nth-child(5) { --twe-stagger-i: 4; }
  .twe-stagger > *:nth-child(6) { --twe-stagger-i: 5; }
  /* Cap the ramp: past six, the last card arrives after the user has started
   * reading, which is worse than no animation at all. */
  .twe-stagger > *:nth-child(n + 7) { --twe-stagger-i: 6; }

  /* view() ties the animation to the element's own position in the
   * scrollport — a reveal needs NO IntersectionObserver and no JS. Guarded
   * because it is Chromium 115+; below that the time-based animation above
   * already ran on load, which is a fine fallback since content is visible
   * either way and nothing is JS-gated. */
  @supports (animation-timeline: view()) {
    .twe-reveal,
    .twe-stagger > * {
      animation-timeline: view();
      animation-range: entry 0% entry 60%;
      animation-delay: 0s;   /* a scroll timeline has no time axis to delay on */
    }
  }
}


/* ================================================ 4. overlays: enter/exit ===
 * @starting-style and transition-behavior: allow-discrete are both Chromium
 * 117 — BELOW the 120 floor, so they are safe unguarded.
 *
 * WHY allow-discrete is load-bearing: display and overlay are discrete
 * properties. Without it they flip on frame 1, the element leaves the top layer
 * immediately, and the exit transition has nothing left to animate. You get the
 * classic half-built feel — a graceful entrance and a hard disappearance. Both
 * properties must be listed, not just display.
 */
[popover],
dialog {
  transition:
    opacity   var(--twe-dur-enter) var(--twe-ease),
    transform var(--twe-dur-enter) var(--twe-ease),
    display   var(--twe-dur-enter) allow-discrete,
    overlay   var(--twe-dur-enter) allow-discrete;
}
[popover]:popover-open,
dialog[open] { opacity: 1; transform: none; }

/* The closed state is also the EXIT target — what the element transitions back
 * toward when :popover-open / [open] stops matching. */
[popover]:not(:popover-open),
dialog:not([open]) { opacity: 0; transform: translateY(4px) scale(0.98); }

/* @starting-style supplies the "before it existed" state. Without it the
 * element paints at its open state on frame 1 and there is no entrance. */
@starting-style {
  [popover]:popover-open,
  dialog[open] { opacity: 0; transform: translateY(4px) scale(0.98); }
}

/* Backdrop: MOTION ONLY. Its colour and blur belong to overlay.css — this file
 * must not race a sibling stylesheet over the same declarations. */
dialog::backdrop,
[popover]::backdrop {
  opacity: 0;
  transition:
    opacity var(--twe-dur-enter) var(--twe-ease-out),
    display var(--twe-dur-enter) allow-discrete,
    overlay var(--twe-dur-enter) allow-discrete;
}
dialog[open]::backdrop,
[popover]:popover-open::backdrop { opacity: 1; }
@starting-style {
  dialog[open]::backdrop,
  [popover]:popover-open::backdrop { opacity: 0; }
}

/* NO-JS DEGRADATION: dialog and popover need script (or popovertarget) to open
 * at all, so with JS off nothing here applies and the element stays
 * display:none. Any flow that must work without script needs a server-rendered
 * page, not a dialog — an app-shell decision; this file only owns the motion. */


/* ============================================ 5. spring — one case only ====
 * A spring is DIRECT-MANIPULATION feedback: honest when the user's own gesture
 * threw the object (a toggle knob, a dragged card settling), noise on anything
 * the system initiates — modal, toast, page load. So the house has exactly one,
 * scoped to the knob. linear() is Chromium 113, below the floor.
 *
 * --twe-ease-spring is declared LOCALLY here, not in tokens.css, on purpose: a
 * fleet-wide bounce token invites bouncing everything. Promote it upstream only
 * if a second legitimate direct-manipulation case appears.
 */
.twe-switch__knob,
.twe-toggle__knob {
  --twe-ease-spring: linear(
    0, 0.402 7.4%, 0.711 15.3%, 0.929 23.7%, 1.008 28.3%,
    1.057 33.3%, 1.07 39%, 1.045 47.6%, 1.006 61%, 0.995 71.4%, 1
  );
  transition: transform var(--twe-dur-slow) var(--twe-ease-spring);
}


/* =============================================== 6. pointer affordances ====
 * (hover: hover) and (pointer: fine) is a CAPABILITY query, not a breakpoint —
 * do not swap it for a width. A touch device that inherits :hover keeps the
 * state stuck after the tap, so a "lifted" card stays lifted until you touch
 * something else. These tools run on field tablets; that is a real defect here,
 * not a theoretical one.
 */
@media (hover: hover) and (pointer: fine) {

  .twe-hover-lift {
    transition:
      transform  var(--twe-dur-fast) var(--twe-ease),
      box-shadow var(--twe-dur-fast) var(--twe-ease);
  }
  /* The shadow deepens WITH the travel. Lifting without moving the shadow is
   * the tell of a fake lift — the light source has to agree with the motion. */
  .twe-hover-lift:hover,
  .twe-hover-lift:focus-visible {   /* keyboard parity: focus gets the same
                                     * lift, so keyboard users are not the ones
                                     * told the UI is flat. */
    transform: translateY(-2px);
    box-shadow: var(--twe-elev-2), var(--twe-highlight);
  }

  .twe-press-scale { transition: transform var(--twe-dur-instant) var(--twe-ease-out); }
  .twe-press-scale:active { transform: scale(0.97); }
}


/* ======================================== 7. never-move, enforced here =====
 * Rule 5 of the spec as CSS instead of as advice — a comment does not survive
 * a hurried copy-paste into a new tool, a selector does.
 */

/* A transform on a <tr> breaks the table's column geometry, drags cells out
 * from under a sticky header, and moves data mid-scan. Rows may change colour
 * and elevation; they may not move. */
tr.twe-hover-lift,
tr.twe-reveal,
li.twe-reveal > * {
  transform: none !important;
  animation-name: none !important;
}

/* Field theme: its tokens do not zero the durations (that is reduced-motion's
 * job), so travel is removed by selector. Colour and elevation feedback
 * survives — only movement is cut. */
[data-twe-theme='field'] .twe-reveal,
[data-twe-theme='field'] .twe-stagger > *,
[data-twe-theme='field'] .twe-hover-lift,
[data-twe-theme='field'] .twe-hover-lift:hover,
[data-twe-theme='field'] .twe-press-scale:active,
[data-twe-theme='field'] .twe-switch__knob,
[data-twe-theme='field'] .twe-toggle__knob {
  animation: none !important;
  transition: none !important;
  transform: none !important;
}

/* Escape hatch for the element that genuinely must sit still — a live log tail,
 * a chart being read, a print preview. A paired class keeps the intent in the
 * markup and reviewable, instead of an inline style nobody can grep for. */
.twe-no-motion,
.twe-no-motion * {
  animation: none !important;
  transition: none !important;
}

/* will-change is deliberately absent above. It pins a compositor layer for the
 * element's whole life; across the hundreds of cards and rows these tools
 * render it costs more memory than the jank it saves. Add it locally, on the
 * one element that measurably needs it. */
