/*
 * Cross-document View Transitions for the Speeches project — shared between
 * /studio/speeches/ (the landing list) and /studio/speeches/<slug>/ (one
 * page per speech), both of which link this file directly with a plain
 * <link rel="stylesheet">, not a frontmatter import.
 *
 * That's deliberate, not incidental: this file lives in public/ specifically
 * so Vite's CSS pipeline never touches it. Importing it from an .astro
 * frontmatter (as `src/styles/*.css` normally would be) sent it through
 * Vite's CSS minifier, which doesn't yet recognize `@view-transition`,
 * `::view-transition-*()`, or `:active-view-transition-type()` and silently
 * DROPS all three — the page still built with no errors, but the opt-in
 * rule itself never reached the browser, so every navigation quietly fell
 * back to a plain page load with none of the transitions below ever firing.
 * public/ assets are copied byte-for-byte with no processing at all, which
 * is the one path in this project that's actually safe for syntax this new.
 *
 * Also why this can't live inside a component's own scoped <style> block
 * even once that bug is sidestepped: ::view-transition-group() and friends
 * are appended to the document's top layer, not inside any normal element,
 * so Astro's per-component style scoping could never reach them regardless.
 *
 * Support as of this writing: cross-document view transitions themselves
 * (the @view-transition rule below) work in Chrome/Edge 126+ and Safari
 * 18.2+. `:active-view-transition-type()` and `ViewTransition.types` (used
 * for the next/prev/back slide, set from the early inline scripts in
 * studio/speeches/[slug].astro and studio/speeches/index.astro — there's
 * no shared transitions.ts module; a classic script can't import one, so
 * both pages carry their own small copy of this logic) are Chrome/Edge-only
 * so far. `view-transition-class` is newer still (Chrome 125+), not yet in
 * Safari or Firefox — see the explicit by-name rule below, which is what's
 * actually relied on. Everywhere none of this applies, navigation is just
 * a normal page load — nothing here has a fallback *to* write, since
 * there's nothing to undo.
 */

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

/* ── Shared chrome stays out of the root capture ──
   The corner share button and the scroll-progress rail are given stable
   view-transition-names (via :global rules in each page's own <style>,
   right next to the component that needs it) so they're pulled out of the
   default root snapshot and, critically, out of the root's next/prev
   directional slide below — chrome shouldn't slide off-screen with the
   article. Both are pixel-identical old vs. new on every navigation (same
   icon, same position, same bar), so there's no cross-fade *or* geometry
   concern here. */
::view-transition-group(vt-share),
::view-transition-group(vt-scroll-progress) {
  animation: none;
}

/* The breadcrumb is NOT pixel-identical old vs. new (its last crumb's
   label is a different speech's title each time), so it isn't named
   unconditionally the way vt-share/vt-scroll-progress are — see the long
   comment in speeches/index.astro for the two bugs that came from doing
   that on the landing <-> speech direction specifically (a duplicated
   breadcrumb, then a geometry snap, neither fully nailed down before both
   attempts were reverted). [slug].astro's early script instead names it,
   via data-vt-pager, only for a next/prev sibling navigation (where both
   documents are this same template, so the crumb trail only ever differs
   by one label, not by crumb count the way landing's 3 vs. a speech's 4
   did) — the much smaller, much safer case where pinning the group like
   this actually holds. Old/new still default-cross-fade between the two
   labels; only the group's geometry (and therefore position) is pinned. */
::view-transition-group(vt-breadcrumb) {
  animation: none;
}

/* The lava canvas also gets its own named group (vt-lava, set alongside the
   others) so it doesn't jostle against the text content's cross-fade, but
   it keeps the *default* cross-fade rather than `animation: none` — the
   incoming document's canvas starts blank until /ai-lava.jpg decodes, so a
   hard cut here would flash black for a frame. No extra rule needed: the
   default animation is exactly what we want for this one. */

/* ── The headline effect: portrait + title morph ──
   Landing → speech only. .sp-portrait/.sp-title in SpeechArticle.astro
   carry these names unconditionally in markup, but [slug].astro's early
   script turns them off (data-vt-morph="off", see the :global rule in
   SpeechArticle.astro's own <style>) for every direction except arriving
   from the landing page — a speech page is never the start of a morph,
   and two different speeches morphing into each other wouldn't mean
   anything anyway (see the directional-slide comment below). The landing
   page names whichever single row matches the navigation, dynamically
   (see nameRowForMorph in its own early script) — never more than one row
   at a time, since two elements sharing a view-transition-name on one
   document silently cancels the whole transition rather than erroring
   loudly, and never on the way back (see markCameFrom's comment in that
   same script for why the reverse morph was tried and dropped). */
::view-transition-group(speech-portrait),
::view-transition-group(speech-title) {
  animation-duration: 0.32s;
  animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
/* Duplicate of the rule above, by class instead of by name — harmless
   where view-transition-class isn't supported (it just never matches),
   and lets a future third named element pick up the same timing for free
   without another by-name rule. */
::view-transition-group(.speech-morph) {
  animation-duration: 0.32s;
  animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
/* Belt-and-suspenders on top of naming an already-rounded element on both
   sides (see nameSpeechRowForMorph in transitions.ts and .sp-portrait in
   SpeechArticle.astro): explicitly re-round the portrait's own transition
   pseudo-elements too, so the circle survives even through the
   size-interpolated middle of the animation (64px landing thumbnail <->
   128px byline portrait), not just at its start/end frames. */
::view-transition-image-pair(speech-portrait),
::view-transition-old(speech-portrait),
::view-transition-new(speech-portrait) {
  border-radius: 50%;
  overflow: hidden;
}
/* The rest of the page (`root` — everything not named) used the UA's
   default cross-fade here, ~0.25s. An earlier attempt at this fix just
   matched that to the morph's then-0.5s duration, on the theory that
   same-duration would mean same-finish-time — it didn't actually read as
   fixed. The real issue wasn't a duration mismatch to begin with: the
   morph isn't just fading, it's visibly GROWING and TRAVELING across the
   screen (a 64px list thumbnail becoming a 128px byline portrait, from
   wherever that row was to wherever the byline sits) for the entire
   animation, and that motion alone reads as "still arriving" for as long
   as it keeps moving, no matter what the rest of the page's cross-fade is
   doing alongside it — two things finishing at the same timestamp doesn't
   look simultaneous when one of them is still sliding and resizing right
   up to that timestamp and the other has been static text the whole time.
   The cubic-bezier(0.2, 0.7, 0.2, 1) curve made this worse: it front-loads
   most of the motion but leaves a long, slow-decaying tail at the very
   end, which reads as continued motion well past when the animation is
   nominally "almost done." So the actual fix (see the morph's own rule
   above) is to make the morph itself shorter and snappier — 0.32s with
   the site's existing fast ease-out curve (cubic-bezier(0.16, 1, 0.3, 1),
   already used for SpeechArticle.astro's own entrance animation) instead
   of a slow half-second deceleration — and bring root down to match, so
   neither one is the long-running outlier and the whole page settles in
   under a third of a second instead of half. This is a plain,
   unconditional rule on purpose, not gated behind
   `:not(:active-view-transition-type(...))` — wrapping an unknown
   pseudo-class (Safari doesn't have :active-view-transition-type) inside
   `:not()` can invalidate the whole selector there, which would've meant
   this fix silently never applied in the one other browser that also runs
   the morph. Instead this relies on plain CSS specificity: the next/prev
   rules below target `:active-view-transition-type(next)::view-transition-
   old(root)` etc., one pseudo-class more specific than the bare
   `::view-transition-old(root)` here, so they still win on that direction
   in browsers that support it — and where they don't, there's no type
   active to conflict with in the first place. */
::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 0.32s;
  animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}

/* ── Drift-and-fade for speech ↔ speech navigation (via the pager) ──
   One motion, like turning a page — not a slide with a separately-arriving
   portrait/title. Earlier versions kept .sp-portrait/.sp-title named (and
   therefore excluded from this `root` capture, the way they still are for
   the landing <-> speech direction above) so they'd hold in place while
   the rest of the article slid past underneath. That read as broken in
   two ways: the pager sits at the bottom of a long article, so the
   browser's captured position for those named elements was often far
   below the viewport — on arrival they'd have to travel all the way up to
   the masthead, landing well after the 0.35s slide had already finished
   ("drop in last"). And a named morph between two *different* speeches
   (Socrates turning into Lincoln, "Apology" stretching into "Gettysburg
   Address") doesn't mean anything the way landing-row → byline does.
   [slug].astro's early script now turns the morph off for every departure
   from a speech page, so .sp-portrait/.sp-title have no name here at all —
   they're ordinary content, captured as part of `root`, and move with
   everything else below.
   Old and new get different timing on purpose: a quick ease-in exit
   (180ms) clears the screen fast, then a slower ease-out entrance (320ms,
   started 80ms into the exit) drifts the new page in — during that
   80-320ms overlap the dark background and lava canvas show through
   between them, reading as a page turn rather than a cut. Small, fixed
   pixel offsets (32px out, 48px in) replace an earlier `6%`-based version,
   which came out to very different real distances on a phone (~23px) vs.
   a wide desktop screen (~86px).
   Chrome/Edge-only (:active-view-transition-type support) — elsewhere this
   selector never matches and the navigation just gets the plain default
   root cross-fade instead of a drift, which still reads fine. */
:active-view-transition-type(next)::view-transition-old(root) {
  animation: speech-drift-out-left 180ms cubic-bezier(0.4, 0, 1, 1) both;
}
:active-view-transition-type(next)::view-transition-new(root) {
  animation: speech-drift-in-right 320ms cubic-bezier(0.2, 0.7, 0.2, 1) 80ms both;
}
:active-view-transition-type(prev)::view-transition-old(root) {
  animation: speech-drift-out-right 180ms cubic-bezier(0.4, 0, 1, 1) both;
}
:active-view-transition-type(prev)::view-transition-new(root) {
  animation: speech-drift-in-left 320ms cubic-bezier(0.2, 0.7, 0.2, 1) 80ms both;
}

@keyframes speech-drift-out-left {
  to { transform: translateX(-32px); opacity: 0; }
}
@keyframes speech-drift-in-right {
  from { transform: translateX(48px); opacity: 0; }
}
@keyframes speech-drift-out-right {
  to { transform: translateX(32px); opacity: 0; }
}
@keyframes speech-drift-in-left {
  from { transform: translateX(-48px); opacity: 0; }
}

/* `back` (speech → landing — the pager's "All speeches" link, the
   breadcrumb, or an actual browser back navigation) deliberately gets no
   slide rule: "back" can land on any row in the list depending on which
   speech you were reading, not a fixed side, so a fixed direction would be
   wrong about as often as it's right. It's a plain root cross-fade, with
   the landing page's "came from" row highlight (see markCameFrom's
   comment in speeches/index.astro) carrying the continuity instead of a
   reverse portrait/title morph — that was tried and dropped; see the same
   comment for why. */
