/**
 * Scroll motion: zoom, parallax, opening frames and drawn strokes driven by the scroll
 * position (CSS scroll-driven animations, no JavaScript). The motion is tied to the
 * scrollbar: it stops when scrolling stops and runs backwards when scrolling
 * up. Entrances stay with chain-animations; this file is for continuous
 * motion only. Docs: documentation/scroll-motion.md
 *
 * Modelled on Dolly (usedolly.dev, MIT), written for the theme; no dependency.
 *
 * Utility classes (any element unless noted):
 *   scroll-zoom       slow push: scale 1 → --scroll-zoom while the element
 *                     crosses the screen
 *   scroll-parallax   moves from -depth to +depth while it crosses the screen:
 *                     a positive depth lags behind the page (a background), a
 *                     negative one runs ahead of it (a foreground layer)
 *   scroll-depth-s / scroll-depth-m / scroll-depth-l   2rem, 4rem, 8rem
 *   Both motion classes on one element combine (scale and translate are
 *   separate properties).
 *   scroll-fill       a statement that fills from dim to full color line by
 *                     line as it passes the reading line. Text elements
 *                     only (p, h1-h6, GB headline). js/applari-js/scroll-fill.js
 *                     wraps every word in span.scroll-fill-word on the front
 *                     end; each word has its own view timeline, so the words
 *                     of one line fill together. Without the script, support
 *                     or with reduced motion the text shows in full color.
 *   scroll-draw       stroke icons and lines inside the element draw
 *                     themselves as they enter the screen. Inline SVG only
 *                     (GB icon headlines such as icon-list-item, HTML
 *                     blocks), not <img src="….svg">. The stroke needs
 *                     pathLength="1" on every shape, which CSS cannot set:
 *                     includes/blocks/scroll-draw.php adds it on the front
 *                     end. Put the class on the block or on a container
 *                     around several. Filled icons have nothing to draw.
 *
 * Block variants use the keyframes below in their own files:
 *   cover--zoom, cover--parallax, cover--expand (css/blocks/cover.css)
 *   text-image--zoom, text-image--parallax      (css/blocks/text-image.css)
 *   scroll-scene--zoom                          (css/blocks/scroll-scene.css)
 * Pinned step sequences are the scroll-scene block.
 *
 * Variables:
 *   --scroll-zoom    end scale, default 1.12 (inherits: set it on the block)
 *   --scroll-depth   parallax travel each way, default 4rem (inherits)
 *   --scroll-range   animation-range of a utility element, default cover (the
 *                    whole time on screen); does not inherit
 *   --scroll-fill-dim     opacity of a word before it fills, default 0.25
 *                    (the section's own text color, so every palette works)
 *   --scroll-fill-range   when a word fills, default cover 30% cover 42%:
 *                    from 70% of the screen height down to 58%. Set it on
 *                    the scroll-fill element, it inherits to the words. Text
 *                    that never rises above 58% (the last lines of a page)
 *                    stays partly dim: keep scroll-fill away from the end.
 *   --scroll-draw-range   scroll-draw range, default entry 0% cover 50% (done
 *                    when the shape reaches the middle of the screen); set it
 *                    on the scroll-draw element, it inherits to the shapes
 *   --scroll-expand-x     scroll-expand start inset on both sides, default the
 *                    content width (--container-width), at least 6 % a side
 *   --scroll-expand-y     scroll-expand start inset at the top and bottom,
 *                    default 6 % of the height
 *   --scroll-expand-radius   scroll-expand start corner radius, default 12px
 *
 * Rules:
 *   - Every motion rule sits inside @supports (animation-timeline: view()) and
 *     prefers-reduced-motion: no-preference. Without support (Firefox before
 *     159) or with reduced motion nothing applies: the page is as authored.
 *   - A frame that crops the moving element uses overflow: clip, never hidden.
 *     hidden makes the frame a scroll container, the view timeline attaches
 *     to it and the motion freezes halfway.
 *   - Never on an element that carries chainAnimation: both use the animation
 *     property and one cancels the other. Move a child (the img) instead.
 *   - Not inside a horizontal scroller (cards--carousel, gallery--scroller):
 *     the view timeline attaches to the scroller.
 *   - Front end only: the editor shows the authored state.
 */

@property --scroll-range {
    syntax: "*";
    inherits: false;
}

/* The expand insets keep % as a token: it resolves against the clipped
   element (x of its width, y of its height), not against :root. */
:root {
    --scroll-zoom: 1.12;
    --scroll-depth: 4rem;
    --scroll-expand-x: max(6%, (100% - var(--container-width)) / 2);
    --scroll-expand-y: 6%;
    --scroll-expand-radius: 12px;
}

@keyframes scroll-zoom {
    from { scale: 1; }
    to   { scale: var(--scroll-zoom); }
}

@keyframes scroll-parallax {
    from { translate: 0 calc(-1 * var(--scroll-depth)); }
    to   { translate: 0 var(--scroll-depth); }
}

/* An inset frame opens to the full box. clip-path leaves the image where it
   is, so only the window grows and nothing zooms. */
@keyframes scroll-expand {
    from { clip-path: inset(var(--scroll-expand-y) var(--scroll-expand-x) round var(--scroll-expand-radius)); }
    to   { clip-path: inset(0 round 0); }
}

/* With pathLength="1" one dash spans the whole shape, so the dash offset is
   the share still undrawn. */
@keyframes scroll-fill {
    from { opacity: var(--scroll-fill-dim, 0.25); }
    to   { opacity: 1; }
}

@keyframes scroll-draw {
    from { stroke-dashoffset: 1; }
    to   { stroke-dashoffset: 0; }
}

.scroll-depth-s {
    --scroll-depth: 2rem;
}

.scroll-depth-m {
    --scroll-depth: 4rem;
}

.scroll-depth-l {
    --scroll-depth: 8rem;
}

@supports (animation-timeline: view()) {
    @media (prefers-reduced-motion: no-preference) {
        .scroll-zoom,
        .scroll-parallax {
            animation-timing-function: linear;
            animation-fill-mode: both;
            animation-timeline: view();
            animation-range: var(--scroll-range, cover);
        }

        .scroll-zoom {
            animation-name: scroll-zoom;
        }

        .scroll-parallax {
            animation-name: scroll-parallax;
        }

        .scroll-zoom.scroll-parallax {
            animation-name: scroll-zoom, scroll-parallax;
        }

        /* Inline-block: a view timeline needs a box of its own per word.
           Whitespace stays plain text between the words, so lines wrap as
           authored. */
        .scroll-fill-word {
            display: inline-block;
            animation-name: scroll-fill;
            animation-timing-function: linear;
            animation-fill-mode: both;
            animation-timeline: view();
            animation-range: var(--scroll-fill-range, cover 30% cover 42%);
        }

        /* Only shapes the PHP filter prepared: without pathLength a dash of
           1 would cut the stroke into a dotted line. */
        .scroll-draw :is(path, circle, ellipse, line, polyline, polygon, rect)[pathLength] {
            stroke-dasharray: 1;
            animation-name: scroll-draw;
            animation-timing-function: linear;
            animation-fill-mode: both;
            animation-timeline: view();
            animation-range: var(--scroll-draw-range, entry 0% cover 50%);
        }
    }
}
