/*
 * Drawings in motion (DESIGN.md "Drawings in motion").
 *
 * A drawing that moves is the same SVG file as its still (static/img/illustrations/<name>.svg) put into the page
 * inline, so the page can tell it when to arrive, when to replay and when to stop. The file carries only hooks:
 *
 *   <svg data-play="arrive live">            what this drawing does (arrive = draws once, live = a gentle idle)
 *     <path class="dg-ink" pathLength="1" style="--i:3">      a stroke that draws on, 3rd in order
 *     <circle class="dg-pop" style="--i:9">                    a fill that settles in
 *     <g class="dg-sway" style="--a:2deg">                     the accent part that idles
 *
 * Its resting state is the finished picture, so a still (an <img>, print, no script, reduced motion, paused) is the
 * designed frame. Everything below is a no-preference, not-paused rule; none of it is needed to read the drawing.
 *
 * The three promises (DESIGN.md 9.1, 13.6):
 *   - ink draws ONCE, on arrival (>= 20% in view), and replays only on intent (hover, tap, a state change);
 *   - an idle moves only the accent and only while the drawing is in view (`is-live`), by transform and opacity;
 *   - one switch stops it all: prefers-reduced-motion, or the visitor's own "Pause animations" (html[data-motion=paused]).
 * An entrance never fills forwards: the finished element returns to its base style, so it never holds a transform
 * that would become the containing block of a fixed child (the entrance-fill rule).
 *
 * The theme: an inline drawing is rewritten for dark by drawings-theme.css (generated from the same map as the dark
 * twins, scripts/build_illustration_dark.py), so one file serves both themes.
 */
:root {
  --dg-ease: cubic-bezier(.25, 1, .5, 1);      /* the one arrival curve: out-quart-ish, no overshoot */
  --dg-ease-io: cubic-bezier(.45, 0, .55, 1);  /* idles: ease in and out, no bounce */
  --dg-draw: 560ms;                            /* one stroke */
  --dg-step: 30ms;                             /* between strokes (times --i) */
}

/* ---- Placement (DESIGN.md "Drawings in motion", Placement) -------------------------------------------------------
   A drawing is placed by a TIER, not by a pixel size, and aligned by its INK, not its 200x200 canvas.
     hero    the drawing of a first screen            close   the drawing over a closing ask
     state   an error or dead-end page                inline  inside a card or beside a ledger
     quiet   the close of a utility page (Help, Contact), a notch under close
   Each tier is a clamp() of the viewport, tuned on the object (not the canvas: the ink is 26 to 88% of it), so a drawing
   is about 55 to 195px of object at 1440 and still reads at 390. --dg-k is a wide object's optical scale (a wide ticket
   and a narrow lamp look alike at one tier); --dg-l/-r/-t/-b/-f are the ink's empty margins and its floor as fractions
   of the canvas, measured by scripts/measure_illustration_ink.cjs and set on each drawing by the helpers. */
:root {
  --dg-hero:   clamp(144px, calc(8vw + 112px), 232px);
  --dg-close:  clamp(120px, calc(8.5vw + 56px), 184px);
  --dg-state:  clamp(144px, calc(9vw + 80px), 224px);
  --dg-inline: clamp(96px, calc(5vw + 48px), 120px);
  --dg-quiet:  clamp(112px, calc(6vw + 56px), 152px);   /* a utility page's close (Help, Contact): a notch under the close tier */
  --dg-gap: 20px;                              /* floor shadow to the heading it stands over, on a phone */
}
@media (min-width: 900px) { :root { --dg-gap: 28px; } }

.dg { display: inline-block; flex: none; line-height: 0; vertical-align: top; }
.dg > svg { display: block; width: 100%; height: 100%; }

.dg-tier { --dg-size: var(--dg-close); --dg-px: calc(var(--dg-size) * var(--dg-k, 1)); width: var(--dg-px); height: var(--dg-px); aspect-ratio: 1; flex: none; }
img.dg-tier { display: block; }
.dg-tier--hero   { --dg-size: var(--dg-hero); }
.dg-tier--close  { --dg-size: var(--dg-close); }
.dg-tier--state  { --dg-size: var(--dg-state); }
.dg-tier--inline { --dg-size: var(--dg-inline); }
.dg-tier--quiet  { --dg-size: var(--dg-quiet); }

/* A numeric size in the app (porcelain Illustration): the layout box is the INK, the empty top and the empty floor taken off, so the gap to a
   title or a button is the gap the layout says. --dg-px is the canvas the component drew (nominal size x optical scale). */
.dg-fit { margin-block: calc(var(--dg-px, 0px) * var(--dg-t, 0) * -1) calc(var(--dg-px, 0px) * var(--dg-b, 0) * -1); }

/* Align the INK. The empty margin of a drawing is a fraction of its own canvas, so a negative margin of that much puts its
   visible edge on the line a heading or a card edge sits on. */
.dg-flush-l { margin-inline-start: calc(var(--dg-px, 0px) * var(--dg-l, 0) * -1); }
.dg-flush-r { margin-inline-end:   calc(var(--dg-px, 0px) * var(--dg-r, 0) * -1); }
.dg-center  { display: block; margin-inline: auto; }
/* Centre the INK on the axis it is centred on (a door's ink sits 4% right of its canvas's middle; a wide notebook's 6% left). */
.dg-ink-center { position: relative; inset-inline-start: calc(var(--dg-px, 0px) * (var(--dg-r, 0) - var(--dg-l, 0)) / 2); }
/* Tuck the INK's top under what is above it (a drawing that follows text on a phone): the canvas's empty top is not a gap. */
.dg-tuck { margin-block-start: calc(var(--dg-px, 0px) * var(--dg-t, 0) * -1 + var(--dg-tuck, 14px)); }
/* Stand on a line: the floor shadow's middle sits on the next thing down (a rule, a card's edge, a pill's foot). */
.dg-sit  { margin-block-end: calc(var(--dg-px, 0px) * (1 - var(--dg-f, .85)) * -1); }
/* Stand over a heading with a fixed gap, whatever the drawing and its size: the shadow's bottom edge to the heading. */
.dg-over { margin-block-end: calc(var(--dg-gap) - var(--dg-px, 0px) * (1 - var(--dg-f, .85) - .035)); }
/* Hang a flat thing over the edge of what it is placed on (a calendar on a card): a share of its own height. */
.dg-hang { margin-block-end: calc(var(--dg-px, 0px) * var(--dg-hang, .22) * -1); }

/* A thing that is PLACED (a ticket, a stub, a calendar, a note) may lean a few degrees; standing furniture never does,
   because a turned host turns its floor shadow too. It settles onto its place as it arrives (a lift and a little more lean,
   landing; no overshoot). The lean is the base style, so the finished picture, print and reduced motion all keep it. */
.dg-pin { transform: rotate(var(--dg-tilt, 0deg)); transform-origin: 50% 80%; }

/* A state that is the whole page (an error, a note that is gone) on a phone held sideways: stacked, the drawing and the words
   are taller than the window and the way out (the button) falls below the fold. Beside each other they fit and read as one
   statement. Under 520px of height only, from 560px wide: a short window that is also narrow has no room beside. */
@media (max-height: 520px) and (min-width: 560px) {
  .dg-state-row { display: grid; grid-template-columns: auto minmax(0, 1fr); column-gap: clamp(20px, 4vw, 40px); align-items: center;
                  justify-items: start; text-align: start; max-width: min(40rem, 100%); }
  .dg-state-row > .dg-tier { --dg-size: clamp(104px, 30vh, 150px); grid-column: 1; grid-row: 1 / span 8; margin: 0; inset-inline-start: 0; justify-self: center; }
  .dg-state-row > :not(.dg-tier) { grid-column: 2; }
  .dg-state-row .justify-center { justify-content: flex-start; }
  .dg-state-row .mb-8 { margin-bottom: 1rem; }
  .dg-state-row .mt-12 { margin-top: 1rem; }
  .dg-state-row .pt-8 { padding-top: 1rem; }
  .dg-state-row .mkt-h2 { margin-top: 0; }
}

/* Parts that can be moved turn about their own middle unless the file says otherwise. */
.dg-pop, .dg-rise, .dg-spin, .dg-spin-rev, .dg-swing { transform-box: fill-box; transform-origin: center; }
.dg-sway { transform-box: fill-box; transform-origin: var(--o, 50% 100%); }
.dg-bob, .dg-drift, .dg-fall, .dg-feed { transform-box: fill-box; transform-origin: center; }

@media (prefers-reduced-motion: no-preference) {

  /* Hidden until it arrives. Only under html.dg-js (the head script sets it when motion is allowed), so with no
     script the finished picture is simply there. */
  html.dg-js:not([data-motion="paused"]) .dg[data-play~="arrive"]:not(.is-in) .dg-ink { stroke-dasharray: 1 2; stroke-dashoffset: 1.02; stroke-opacity: 0; fill-opacity: 0; }
  html.dg-js:not([data-motion="paused"]) .dg[data-play~="arrive"]:not(.is-in) :is(.dg-pop, .dg-fade, .dg-rise, .dg-feed) { opacity: 0; }

  /* Arrival: ink first, in drawing order; the fill follows each stroke; accents last. */
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in .dg-ink {
    animation: dg-draw var(--dg-draw) var(--dg-ease) calc(var(--i, 0) * var(--dg-step)) backwards,
               dg-fill 320ms ease-out calc(var(--i, 0) * var(--dg-step) + var(--dg-draw) - 200ms) backwards;
  }
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in .dg-fade { animation: dg-fade 380ms ease-out calc(var(--i, 0) * var(--dg-step)) backwards; }
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in .dg-pop  { animation: dg-pop 420ms var(--dg-ease) calc(var(--i, 0) * var(--dg-step) + 80ms) backwards; }
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in .dg-rise { animation: dg-in-rise 480ms var(--dg-ease) calc(var(--i, 0) * var(--dg-step)) backwards; }
  /* a slip that feeds up and stops (the receipt): once, forward only */
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in .dg-feed { animation: dg-feed 1100ms var(--dg-ease) calc(var(--i, 0) * var(--dg-step)) backwards; }

  /* Replay: the same animations under other names, so switching the class restarts them with no reflow trick. */
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in.is-r .dg-ink { animation-name: dg-draw-r, dg-fill-r; }
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in.is-r .dg-fade { animation-name: dg-fade-r; }
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in.is-r .dg-pop  { animation-name: dg-pop-r; }
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in.is-r .dg-rise { animation-name: dg-in-rise-r; }
  html:not([data-motion="paused"]) .dg[data-play~="arrive"].is-in.is-r .dg-feed { animation-name: dg-feed-r; }

  /* Idle: only the accent, only while in view. A drawing that also arrives waits for its own arrival (--lead). */
  .dg[data-play~="arrive"][data-play~="live"] { --lead: 1.4s; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-spin     { animation: dg-spin     var(--t, 5s)   linear var(--lead, 0s) infinite; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-spin-rev { animation: dg-spin-rev var(--t, 5s)   linear var(--lead, 0s) infinite; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-tick     { animation: dg-spin     var(--t, 24s)  linear var(--lead, 0s) infinite; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-sway     { animation: dg-sway     var(--t, 4.2s) var(--dg-ease-io) calc(var(--lead, 0s) + var(--d, 0s)) infinite alternate; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-swing    { animation: dg-swing    var(--t, 3.2s) var(--dg-ease-io) calc(var(--lead, 0s) + var(--d, 0s)) infinite alternate; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-bob      { animation: dg-bob      var(--t, 3.6s) var(--dg-ease-io) calc(var(--lead, 0s) + var(--d, 0s)) infinite alternate; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-drift    { animation: dg-drift    var(--t, 6s)   var(--dg-ease-io) calc(var(--lead, 0s) + var(--d, 0s)) infinite alternate; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-breathe  { animation: dg-breathe  var(--t, 3.8s) var(--dg-ease-io) calc(var(--lead, 0s) + var(--d, 0s)) infinite alternate; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-steam    { animation: dg-steam    var(--t, 3.6s) var(--dg-ease-io) calc(var(--lead, 0s) + var(--d, 0s)) infinite; }
  html:not([data-motion="paused"]) .dg[data-play~="live"].is-live .dg-fall     { animation: dg-fall     var(--t, 2.4s) linear calc(var(--lead, 0s) + var(--d, 0s)) infinite; }

  /* A pinned thing lands on its place while it is drawn: one settle, on the host, never forwards. */
  html.dg-js:not([data-motion="paused"]) .dg-pin[data-play~="arrive"]:not(.is-in) { opacity: 0; }
  html:not([data-motion="paused"]) .dg-pin[data-play~="arrive"].is-in { animation: dg-settle 640ms var(--dg-ease) backwards; }
  html:not([data-motion="paused"]) .dg-pin[data-play~="arrive"].is-in.is-r { animation-name: dg-settle-r; }

  /* An idle does not go on for ever: after a few cycles in view (drawings.js adds is-rest) it holds the pose it is in. A
     part that only fades (steam, a falling grain) goes back to its own base instead of freezing half-gone. */
  .dg.is-rest * { animation-play-state: paused !important; }
  .dg.is-rest :is(.dg-steam, .dg-fall, .dg-breathe) { animation: none !important; }
}

/* Stopped: reduced motion, or the visitor's own pause. The base style is the finished picture, so removing the
   animations is the whole job. */
@media (prefers-reduced-motion: reduce) {
  .dg, .dg * { animation: none !important; transition: none !important; }
}
html[data-motion="paused"] .dg, html[data-motion="paused"] .dg * { animation: none !important; transition: none !important; }

/* ---- keyframes ----------------------------------------------------------------------------------------------
   Arrivals name only their `from`: the `to` is the element's own base style, so a part with its own opacity (a floor
   shadow at .6) lands on it and does not jump. */
/* A round cap on a very short stroke would show as a dot at the start of the dash, however far it is offset: the stroke
   is also held invisible until the draw has begun (stroke-opacity), then drawn. */
@keyframes dg-draw   { from { stroke-dasharray: 1 2; stroke-dashoffset: 1.02; stroke-opacity: 0; } 3% { stroke-opacity: 1; } to { stroke-dasharray: 1 2; stroke-dashoffset: 0; } }
@keyframes dg-draw-r { from { stroke-dasharray: 1 2; stroke-dashoffset: 1.02; stroke-opacity: 0; } 3% { stroke-opacity: 1; } to { stroke-dasharray: 1 2; stroke-dashoffset: 0; } }
@keyframes dg-fill   { from { fill-opacity: 0; } }
@keyframes dg-fill-r { from { fill-opacity: 0; } }
@keyframes dg-fade   { from { opacity: 0; } }
@keyframes dg-fade-r { from { opacity: 0; } }
@keyframes dg-pop    { from { opacity: 0; transform: scale(.55); } }
@keyframes dg-pop-r  { from { opacity: 0; transform: scale(.55); } }
@keyframes dg-in-rise   { from { opacity: 0; transform: translateY(6px); } }
@keyframes dg-in-rise-r { from { opacity: 0; transform: translateY(6px); } }
@keyframes dg-feed   { from { opacity: 0; transform: translateY(var(--y, 28px)); } }
@keyframes dg-feed-r { from { opacity: 0; transform: translateY(var(--y, 28px)); } }
@keyframes dg-settle   { from { opacity: 0; transform: translateY(-12px) rotate(calc(var(--dg-tilt, 0deg) * 2.6)); } 40% { opacity: 1; } }
@keyframes dg-settle-r { from { opacity: 0; transform: translateY(-12px) rotate(calc(var(--dg-tilt, 0deg) * 2.6)); } 40% { opacity: 1; } }

@keyframes dg-spin     { to { transform: rotate(360deg); } }
@keyframes dg-spin-rev { to { transform: rotate(-360deg); } }
@keyframes dg-sway     { from { transform: rotate(calc(var(--a, 2.5deg) * -1)); } to { transform: rotate(var(--a, 2.5deg)); } }
@keyframes dg-swing    { from { transform: rotate(calc(var(--a, 14deg) * -1)); } to { transform: rotate(var(--a, 14deg)); } }
@keyframes dg-bob      { from { transform: translateY(0); } to { transform: translateY(calc(var(--b, 2px) * -1)); } }
@keyframes dg-drift    { from { transform: translateX(calc(var(--b, 6px) * -1)); } to { transform: translateX(var(--b, 6px)); } }
@keyframes dg-breathe  { from { opacity: var(--lo, .38); } to { opacity: var(--hi, .7); } }
@keyframes dg-steam    { 0% { opacity: 0; transform: translateY(8px); } 30% { opacity: .8; } 100% { opacity: 0; transform: translateY(-14px); } }
@keyframes dg-fall     { 0% { opacity: 0; transform: translateY(calc(var(--b, 6px) * -1)); } 25% { opacity: 1; } 100% { opacity: 0; transform: translateY(var(--b, 6px)); } }

/* The footer's "Pause animations" control (data-motion-toggle; static/js/drawings.js). It styles itself as the
   footer's other text buttons do; this only keeps it from looking like a link to nowhere without a script. */
[data-motion-toggle][hidden] { display: none; }
