/* word-swap: the changing word of a headline. The script keeps the word's own text for a screen reader (the source,
   .fx-sr) and draws the word beside it in a box (.fx-swap): the word that stands (.fx-swap__a, in the flow, so the
   box has its baseline and its width) and the word that arrives (.fx-swap__b, laid over it). A change is a CSS
   animation on the two, started by data-swapping on the box; the box's width follows the new word, or stays the
   widest word's (--swap-width: max). Without the script the element is its own text.
   Every rule sits in :where(), so any rule of the page wins. */

:where([data-fx~="word-swap"]) {
  /* knobs */
  --swap-every: 2800ms;    /* its own list: how long each word stands (divided by the site's --fx-pace) */
  --swap-first: var(--swap-every);   /* its own list: how long the first word stands, counted from the page's load */
  --swap-dur: 600ms;       /* how long one change takes (decode: 450ms) */
  --swap-width: follow;    /* follow: the box's width follows the word | max: the box keeps the widest word's width */
  --swap-align: start;     /* start | center | end: where a word stands in a box wider than itself */
  --swap-bar: var(--fx-act);                   /* wipe: the bar's color */
  --swap-bar-w: 0.2em;                         /* wipe: the bar's width */
  --swap-board: var(--fx-on);                  /* flap: the board's face (the ground's text color: a dark board on a light site) */
  --swap-board-on: var(--fx-ground);           /* flap: the word on it (the ground's own color) */
}

:where(.fx-swap) { position: relative; isolation: isolate; display: inline-grid; justify-items: var(--swap-align); vertical-align: baseline; white-space: nowrap; }
:where(.fx-swap) > :where(*) { grid-area: 1 / 1; }
:where(.fx-swap__size) { visibility: hidden; pointer-events: none; }
:where(.fx-swap__b, .fx-swap__c, .fx-swap__d) { position: absolute; inset-block-start: 0; inset-inline-start: 0; visibility: hidden; }
:where(.fx-swap[data-swap-align="end"]) > :where(.fx-swap__b) { inset-inline: auto 0; }
:where(.fx-swap[data-swap-align="center"]) > :where(.fx-swap__b) { inset-inline: 0; margin-inline: auto; inline-size: max-content; }
:where(.fx-swap[data-swapping]) > :where(.fx-swap__b) { visibility: visible; }

/* roll: the old word rolls out through the top of the box as the new one rolls in from below */
:where([data-fx~="word-swap"]:not([data-fx-preset]), [data-fx~="word-swap"][data-fx-preset="roll"]) :where(.fx-swap) {
  clip-path: inset(-0.12em -0.14em -0.24em -0.14em);
}
:where([data-fx~="word-swap"]:not([data-fx-preset]), [data-fx~="word-swap"][data-fx-preset="roll"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__a) {
  animation: fx-swap-roll-out var(--_dur, 600ms) var(--fx-ease-io) both;
}
:where([data-fx~="word-swap"]:not([data-fx-preset]), [data-fx~="word-swap"][data-fx-preset="roll"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__b) {
  animation: fx-swap-roll-in var(--_dur, 600ms) var(--fx-ease-io) both;
}
@keyframes fx-swap-roll-out { to { transform: translateY(calc(-100% - 0.4em)); } }
@keyframes fx-swap-roll-in { from { transform: translateY(calc(100% + 0.4em)); } }

/* flap: the word stands on a board split across its middle, and the board flips once. Behind: the new word whole.
   On it: the old word's lower half. In front: the old word's upper half falls away, then the new word's lower half
   lands. */
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap) > :where(.fx-swap__a, .fx-swap__b, .fx-swap__c, .fx-swap__d) {
  box-sizing: border-box;
  padding: 0.02em 0.2em 0.06em;
  border-radius: 0.1em;
  background: var(--swap-board);
  color: var(--swap-board-on);
  text-align: var(--swap-align);
}
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap) > :where(.fx-swap__a) { position: relative; z-index: 2; justify-self: stretch; }
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap) > :where(.fx-swap__b, .fx-swap__c, .fx-swap__d) { inset-inline: 0; margin-inline: 0; inline-size: 100%; overflow: hidden; }
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap) > :where(.fx-swap__b) { z-index: 1; }
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap)::after {
  content: "";
  position: absolute;
  z-index: 5;
  inset-inline: 0;
  inset-block-start: calc(50% - 0.5px);
  height: max(1px, 0.02em);
  background: var(--swap-board-on);
  opacity: 0.2;   /* a hairline, faint enough never to read as a line through the word */
  pointer-events: none;
}
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__a) { clip-path: inset(50% 0 0 0); }
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__c) {
  visibility: visible;
  z-index: 3;
  clip-path: inset(0 0 50% 0);
  animation: fx-swap-flap1 var(--_dur, 600ms) linear both;
}
:where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__d) {
  visibility: visible;
  z-index: 4;
  clip-path: inset(50% 0 0 0);
  animation: fx-swap-flap2 var(--_dur, 600ms) linear both;
}
@keyframes fx-swap-flap1 {
  0% { transform: perspective(30em) rotateX(0deg); animation-timing-function: cubic-bezier(0.5, 0, 1, 0.7); }
  50%, 100% { transform: perspective(30em) rotateX(-90deg); }
}
@keyframes fx-swap-flap2 {
  0%, 50% { transform: perspective(30em) rotateX(90deg); animation-timing-function: cubic-bezier(0, 0.4, 0.3, 1); }
  100% { transform: perspective(30em) rotateX(0deg); }
}

/* wipe: a bar sweeps across; before it the old word, behind it the new one. Two windows slide and the word inside
   each slides back by as much, so the words stand still and only the windows move. */
:where(.fx-swap__win) { position: absolute; inset: -0.16em 0 -0.28em 0; overflow: hidden; visibility: hidden; pointer-events: none; }
:where(.fx-swap__win) > :where(span) { display: block; margin-block-start: 0.16em; }
:where(.fx-swap__bar) { position: absolute; inset: -0.06em 0 -0.14em 0; visibility: hidden; pointer-events: none; }
:where(.fx-swap__bar)::before { content: ""; position: absolute; inset-block: 0; inset-inline-start: calc(var(--swap-bar-w) / -2); inline-size: var(--swap-bar-w); background: var(--swap-bar); }
:where([data-fx~="word-swap"][data-fx-preset="wipe"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__a, .fx-swap__b) { visibility: hidden; }
:where([data-fx~="word-swap"][data-fx-preset="wipe"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__win, .fx-swap__bar) { visibility: visible; }
:where([data-fx~="word-swap"][data-fx-preset="wipe"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__win--old) { animation: fx-swap-win-out var(--_dur, 600ms) var(--fx-ease-io) both; }
:where([data-fx~="word-swap"][data-fx-preset="wipe"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__win--old) > :where(span) { animation: fx-swap-win-out-in var(--_dur, 600ms) var(--fx-ease-io) both; }
:where([data-fx~="word-swap"][data-fx-preset="wipe"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__win--new) { animation: fx-swap-win-in var(--_dur, 600ms) var(--fx-ease-io) both; }
:where([data-fx~="word-swap"][data-fx-preset="wipe"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__win--new) > :where(span) { animation: fx-swap-win-in-in var(--_dur, 600ms) var(--fx-ease-io) both; }
:where([data-fx~="word-swap"][data-fx-preset="wipe"]) :where(.fx-swap[data-swapping]) > :where(.fx-swap__bar) { animation: fx-swap-bar var(--_dur, 600ms) var(--fx-ease-io) both; }
@keyframes fx-swap-win-out { to { transform: translateX(100%); } }
@keyframes fx-swap-win-out-in { to { transform: translateX(-100%); } }
@keyframes fx-swap-win-in { from { transform: translateX(-100%); } }
@keyframes fx-swap-win-in-in { from { transform: translateX(100%); } }
@keyframes fx-swap-bar { 0% { transform: translateX(0); opacity: 0; } 12%, 88% { opacity: 1; } 100% { transform: translateX(100%); opacity: 0; } }

/* decode: the letters settle out of scrambled ones, left to right (the script writes them); the box holds its width */
:where([data-fx~="word-swap"][data-fx-preset="decode"]) { --swap-dur: 450ms; }
:where([data-fx~="word-swap"][data-fx-preset="decode"]) :where(.fx-swap[data-swapping]) { clip-path: inset(-0.2em -0.06em -0.3em -0.06em); }

/* Reduced motion: a new word simply stands in the old one's place (the script changes it at once). The visitor's own
   press of the part's play icon ([data-fx-play]) lets the changes move. */
@media (prefers-reduced-motion: reduce) {
  :where([data-fx~="word-swap"]:not([data-fx-play])) :where(.fx-swap, .fx-swap *) { animation: none !important; }
}
@media (forced-colors: active) {
  :where([data-fx~="word-swap"][data-fx-preset="flap"]) :where(.fx-swap) > :where(.fx-swap__a, .fx-swap__b, .fx-swap__c, .fx-swap__d) { border: 1px solid; }
}
