/**
 * FLOW: page transitions and the flow nav (opt-in; approved 2026-11-04 for
 * image-led portfolios). Built to its own file, dist/fluid-flow.min.css
 * (package export "./flow"); link it after core on every page that should
 * flow. assets/js/flow.js adds the shared-element names, the direction and
 * the glide. Gate: scripts/flow-check.mjs (npm run test:flow).
 *
 *   <head>
 *     <link rel="stylesheet" href="fluid.min.css">
 *     <link rel="stylesheet" href="fluid-flow.min.css">
 *     <script src="assets/js/flow.js"></script>      (in <head>, no defer)
 *     <link rel="expect" href="#main" blocking="render">  (optional, see below)
 *   </head>
 *
 * 1. PAGE TRANSITIONS (cross-document view transitions, same origin). Both
 *    pages link this file. Only under prefers-reduced-motion: no-preference;
 *    a browser without them (Firefox today) loads the page as usual.
 *    - default: a soft cross-fade of the whole page, --duration-slower
 *      (553ms at the default --motion-base; data-motion scales it, and
 *      data-motion="reduced" on <html> makes it instant), eased in and out.
 *    - data-flow="plate" | "title": the page's big image and its title. One
 *      of each outside a link carries the name; flow.js names the plate and
 *      title inside the clicked link instead (a thumbnail, a slide), so the
 *      thumbnail grows into the next page's plate, and on the way back the
 *      plate shrinks into the thumbnail that links to the page you left.
 *    - data-flow-dir="next" | "prev" on a link (or an ancestor): the new page
 *      eases in from the inline end (next) or start (prev), the old one out
 *      the other way; RTL mirrors it. flow.js passes it as a view-transition
 *      type (flow-next, flow-prev) or, without types, as data-flow-dir on
 *      <html>. "none" turns it off. A flow nav sets it for its own links by
 *      position (after the current item: next). Back and Forward reverse it.
 *      While a page slides, the plate and title are not morphed.
 *
 * 2. THE FLOW NAV: a horizontal list of titles (projects, chapters).
 *
 *   <nav class="flow-nav" aria-label="Projects">
 *     <a class="flow-nav__step" href="oak.html" rel="prev" data-flow-dir="prev">
 *       <span aria-hidden="true">←</span><span class="sr-only">Previous: Oak</span></a>
 *     <ol class="flow-nav__list" role="list">
 *       <li><a href="oak.html">Oak</a></li>
 *       <li><a href="walnut.html" aria-current="page">Walnut</a></li>
 *       <li><a href="cherry.html">Cherry</a></li>
 *     </ol>
 *     <a class="flow-nav__step" href="cherry.html" rel="next" data-flow-dir="next">…</a>
 *   </nav>
 *
 *    The current item (aria-current="page") sits in the centre in the text
 *    colour and a heavier weight; the rest are muted. The ends fade out
 *    (3rem, or a tenth of the list on a phone so a long current title stays
 *    clear); while a link in the list has keyboard focus the fade is off, so
 *    neither the link nor its outline is ever faded. The
 *    arrows are optional. flow.js centres the current item on load and, after
 *    a click, restores the list where it was on the last page and glides the
 *    new current item to the centre (ease in and out; a jump under reduced
 *    motion). Without JS it is a plain scrolling list of links. One flow nav
 *    per page holds still during a transition (the first; flow.js names it).
 *
 * rel="expect" … blocking="render" (Chromium) holds the first paint until
 * #main is parsed, so the nav and plate exist when the new page is revealed.
 *
 * Look hooks, read with fallbacks and never declared (set them on :root in
 * a child theme): --flow-duration, --flow-ease, --flow-shift (how far a page
 * slides, default 6vw), --flow-nav-font, --flow-nav-size, --flow-nav-gap,
 * --flow-nav-color (resting items), --flow-nav-fade (the edge masks).
 */

@media (prefers-reduced-motion: no-preference) {
  @view-transition { navigation: auto; }
}

@layer components {
  /* ---------- Page transitions ---------- */
  @media (prefers-reduced-motion: no-preference) {
    /* The old and new images inherit both from their group. */
    ::view-transition-group(*) {
      animation-duration: var(--flow-duration, var(--duration-slower));
      animation-timing-function: var(--flow-ease, cubic-bezier(0.65, 0, 0.35, 1));
    }

    [data-flow="plate"]:not(a *) { view-transition-name: flow-plate; }
    [data-flow="title"]:not(a *) { view-transition-name: flow-title; }

    /* A thumbnail and a plate rarely share a ratio: both fill the moving box. */
    ::view-transition-old(flow-plate),
    ::view-transition-new(flow-plate) { block-size: 100%; object-fit: cover; }

    /* The nav shows only its live new state, so its glide is seen once. */
    ::view-transition-old(flow-nav) { display: none; }
    ::view-transition-new(flow-nav) { animation: none; }

    /* Direction: a type where the browser has them, else data-flow-dir.
       :is() forgives a browser that does not know the type selector. */
    :root:is([data-flow-dir], :active-view-transition-type(flow-next, flow-prev)) [data-flow] { view-transition-name: none; }
    :root:is([data-flow-dir="next"], :active-view-transition-type(flow-next)) { --_flow-x: var(--flow-shift, 6vw); }
    :root:is([data-flow-dir="prev"], :active-view-transition-type(flow-prev)) { --_flow-x: calc(-1 * var(--flow-shift, 6vw)); }
    :root[dir="rtl"] { --_flow-sign: -1; }
    :root:is([data-flow-dir], :active-view-transition-type(flow-next, flow-prev))::view-transition-old(root) { animation-name: flow-out; }
    :root:is([data-flow-dir], :active-view-transition-type(flow-next, flow-prev))::view-transition-new(root) { animation-name: flow-in; }
  }

  @keyframes flow-out { to { opacity: 0; translate: calc(-1 * var(--_flow-sign, 1) * var(--_flow-x)); } }
  @keyframes flow-in { from { opacity: 0; translate: calc(var(--_flow-sign, 1) * var(--_flow-x)); } }

  /* ---------- The flow nav ---------- */
  .flow-nav {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    min-inline-size: 0;
    font: var(--flow-nav-size, var(--step-0)) / 1.3 var(--flow-nav-font, var(--font-body));
    /* Not an animation: flow.js reads the glide's length here, so it
       follows --motion-base, data-motion and reduced motion. */
    animation-duration: var(--flow-duration, var(--duration-slower));
  }

  .flow-nav__list {
    --_fade: var(--flow-nav-fade, min(3rem, 10%));
    flex: 1;
    display: flex;
    gap: var(--flow-nav-gap, var(--space-6));
    min-inline-size: 0;
    margin: 0;
    padding: 0;
    list-style: none;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scroll-behavior: auto;
    /* Where the browser scrolls a focused link into view itself (no JS),
       it scrolls it past the fade, not to the very edge. */
    scroll-padding-inline: var(--_fade);
    scrollbar-width: none;
    mask-image: linear-gradient(to right, transparent, #000 var(--_fade) calc(100% - var(--_fade)), transparent);
  }

  /* A focused link is never half faded (its outline neither). */
  .flow-nav__list:has(:focus-visible) { mask-image: none; }

  .flow-nav__list > li { flex: none; margin: 0; }

  /* Half the list on each side, so the first and last items can centre.
     Spacers, not padding: padding would set the list's minimum width. */
  .flow-nav__list::before,
  .flow-nav__list::after { content: ""; flex: none; inline-size: 50%; }

  .flow-nav a {
    display: block;
    padding-block: calc((2.75rem - 1lh) / 2);
    color: var(--flow-nav-color, var(--color-text-muted));
    text-decoration-line: none;
    white-space: nowrap;
  }

  .flow-nav a:hover { color: var(--color-text); text-decoration-line: underline; }
  .flow-nav a:focus-visible { outline-offset: -2px; }

  .flow-nav [aria-current="page"] {
    color: var(--color-text);
    font-weight: calc(var(--weight-medium) + var(--_dark-weight, 0));
  }

  .flow-nav__step {
    flex: none;
    inline-size: 2.75rem;
    text-align: center;
  }

  @media (forced-colors: active) {
    .flow-nav [aria-current="page"] { text-decoration-line: underline; }
  }
}
