/* split-reveal: a heading or a statement that arrives in reading order. The script splits it into words (FX.split) and
   marks its state on the element: data-split="armed" (waiting, its first state), "in" (arriving), "done"; for the
   read-along, "css" (each word on its own view timeline) or "js" (the same from the script, where a browser has no
   scroll timelines). Without the script, and under reduced motion, it is plain text: nothing here applies.
   Every rule sits in :where(), so any rule of the page wins. */

:where([data-fx~="split-reveal"]) {
  /* knobs */
  --split-dur: 0.9s;       /* lines, focus: how long one line or one word takes (divided by the site's --fx-pace) */
  --split-stagger: 90ms;   /* lines: the wait between two lines */
  --split-step: 40ms;      /* focus: the wait between two words */
  --split-dim: 0.3;        /* ink, focus: how faint a word is before it is read, 0 to 1 */
  --split-blur: 0.16em;    /* focus: how soft a word is before it is read */
  --split-line: 0.62;      /* ink: the reading line, a share of the screen's height from its top */
  --split-spread: 14%;     /* ink: the share of the screen's height in which one line inks from its first word to its last */
  --split-soft: 7%;        /* ink: the share of the screen's height one word takes to ink */
  --split-tint: var(--fx-tint);   /* ink: the color of the words inside <em> and <mark> */
}

/* A first-screen heading that waits for its entrance (data-split-wait, with the one line in the page's head that sets
   data-fx-js before the first paint): held back until the script is ready, shown after 3 s if it never is. */
:where(html[data-fx-js]) :where([data-fx~="split-reveal"][data-split-wait]:not([data-fx-ready~="split-reveal"])) {
  opacity: 0;
  animation: fx-split-wait 1ms linear 3s forwards;
}
@keyframes fx-split-wait { to { opacity: 1; } }

/* lines: each word is a window and its inner box rises into it; the words of one line rise together. */
:where(html[data-fx-js]) :where([data-fx~="split-reveal"][data-fx-preset="lines"]:is([data-split="armed"], [data-split="in"])) :where(.fx-w) {
  clip-path: inset(-0.1em -0.6em -0.14em -0.6em);
}
:where(html[data-fx-js]) :where([data-fx~="split-reveal"][data-fx-preset="lines"][data-split="armed"]) :where(.fx-wi) {
  transform: translateY(calc(100% + 0.2em));
}
:where([data-fx~="split-reveal"][data-fx-preset="lines"][data-split="in"]) :where(.fx-wi) {
  transition: transform calc(var(--split-dur) / var(--fx-pace, 1)) var(--fx-ease) calc(var(--fx-l, 0) * var(--split-stagger) / var(--fx-pace, 1));
}

/* The phrase that matters: the words inside <mark> stand in the tint in every form (and never on the browser's
   yellow); in the read-along the words inside <em> do too. */
:where([data-fx~="split-reveal"]) :where(mark) { background: none; color: var(--split-tint); }
:where([data-fx~="split-reveal"][data-fx-preset="ink"]) :where(em) { color: var(--split-tint); }

/* ink: the words wait faint and take their full color as their line comes up to the reading line, first word first. */
:where([data-fx~="split-reveal"][data-fx-preset="ink"][data-split="css"]) :where(.fx-wi) {
  animation: fx-split-ink linear both;
  animation-timeline: view();
  animation-range:
    cover calc((1 - var(--split-line)) * 100% - var(--split-soft) - (1 - var(--_k, 1)) * var(--split-spread))
    cover calc((1 - var(--split-line)) * 100% - (1 - var(--_k, 1)) * var(--split-spread));
}
@keyframes fx-split-ink { from { opacity: var(--split-dim); } to { opacity: 1; } }

/* focus: every word stands as a soft, faint copy of itself; in reading order the sharp word comes in over it and the
   copy goes. Only opacity moves: the blur is drawn once. */
:where(html[data-fx-js]) :where([data-fx~="split-reveal"][data-fx-preset="focus"]:is([data-split="armed"], [data-split="in"])) :where(.fx-w) {
  position: relative;
}
:where(html[data-fx-js]) :where([data-fx~="split-reveal"][data-fx-preset="focus"]:is([data-split="armed"], [data-split="in"])) :where(.fx-w)::before {
  /* the copy is not read aloud. A browser without this form (Safari before 17.4, Firefox before 128) has no copy:
     there the words simply fade in, which is better than a copy a screen reader says twice. */
  content: attr(data-t) / "";
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
  white-space: nowrap;
  pointer-events: none;
  filter: blur(var(--split-blur));
  opacity: var(--split-dim);
}
:where(html[data-fx-js]) :where([data-fx~="split-reveal"][data-fx-preset="focus"][data-split="armed"]) :where(.fx-wi) { opacity: 0; }
:where([data-fx~="split-reveal"][data-fx-preset="focus"][data-split="in"]) :where(.fx-wi) {
  transition: opacity calc(var(--split-dur) / var(--fx-pace, 1)) var(--fx-ease-io) calc(var(--fx-w, 0) * var(--split-step) / var(--fx-pace, 1));
}
:where([data-fx~="split-reveal"][data-fx-preset="focus"][data-split="in"]) :where(.fx-w)::before {
  opacity: 0;
  transition: opacity calc(var(--split-dur) / var(--fx-pace, 1)) var(--fx-ease-io) calc(var(--fx-w, 0) * var(--split-step) / var(--fx-pace, 1));
}

/* Reduced motion: the script arms nothing, and these keep it so if the setting changes while a page is open. */
@media (prefers-reduced-motion: reduce) {
  :where([data-fx~="split-reveal"]) :where(.fx-w) { clip-path: none !important; }
  :where([data-fx~="split-reveal"]) :where(.fx-wi) { transform: none !important; opacity: 1 !important; animation: none !important; transition: none !important; }
  :where([data-fx~="split-reveal"]) :where(.fx-w)::before { content: none !important; }
  :where([data-fx~="split-reveal"][data-split-wait]) { opacity: 1 !important; animation: none !important; }
}
