/* SPDX-License-Identifier: AGPL-3.0-or-later
 *
 * The card you clicked is the thing that becomes the page.
 *
 * docs/PLAN.md § The islands boundary settles this: card->detail morphing is
 * same-document View Transitions driven by Turbo, "which is better than a JS
 * animation library for that job because the browser does it". It is better
 * for a reason worth writing down - the browser is animating snapshots of two
 * documents in the top layer, off the main thread, and it cannot get the
 * start and end geometry wrong because it measured both itself. A library
 * animating a cloned node has to guess at both and re-lays-out the page to do
 * it.
 *
 * Turbo checks `prefers-reduced-motion` before it ever calls
 * startViewTransition (Snapshot#prefersViewTransitions, turbo 2.0.23), so the
 * whole mechanism switches itself off for anyone who asked - there is no rule
 * here that needs to repeat it, and adding one would only rot.
 */

/* One name, not one per item. The morph only needs the pair to agree, and a
   name per card would mean a per-item inline style on every card on the wall
   - forbidden here, and pointless besides. morph_controller.js moves the
   class to whichever card was clicked, so exactly one element in each
   document ever carries the name. Two would be worse than none: duplicate
   view-transition-names make the browser skip the transition entirely. */
.card.morphing,
.treasure { view-transition-name: treasure; }

/* The wall is a multi-column layout, and a fragmented element cannot be
   captured - the browser abandons the whole transition rather than snapshot
   half a card. `.card { break-inside: avoid }` in wall.css is what keeps a
   card in one piece, so it is load-bearing for this as well as for the
   layout, and removing it would break an animation two files away. */

/* The masthead is the same object on both pages, so it is lifted out of the
   page's cross-fade and morphs in place. Without this it fades out and back
   in, which reads as the header reloading - the one thing the whole effect is
   supposed to deny. */
.masthead { view-transition-name: masthead; }

::view-transition-group(treasure) {
  /* 340ms. The browser's 250ms default is tuned for a cross-fade; this group
     also travels most of the viewport and changes size by an order of
     magnitude, and at 250ms that arrives before the eye has followed it. */
  animation-duration: .34s;
  /* The same curve the cards use to lift under a pointer, so a card that
     rises 3px on hover and a card that becomes a page are visibly the same
     object behaving consistently. */
  animation-timing-function: cubic-bezier(.2, .8, .2, 1);
}
::view-transition-group(masthead) { animation-duration: .34s; }

/* Snapshots are stretched to fill the group's animating box by default, and
   the box goes from a 260px card to a full-measure article - so every glyph
   in both snapshots is scaled on a different axis for the whole 340ms, which
   is the smearing that makes most View Transition demos look cheap.
 *
 * `object-fit: none` keeps each snapshot at the size it was actually
 * rendered, and `top left` pins them to the corner the reader's eye is
 * already on - the title. The frame grows; the text inside it does not
 * distort, it dissolves. */
::view-transition-old(treasure),
::view-transition-new(treasure) {
  inline-size: 100%;
  block-size: 100%;
  object-fit: none;
  object-position: top left;
  overflow: clip;
}
/* The outgoing card is a small thing being left behind, so it goes early and
   quickly; the incoming page is what is being arrived at, so it comes in over
   the back half and is the one still moving when the group lands. Equal
   cross-fades read as a dissolve between two pictures. A lead and a follow
   read as one thing turning into another. */
::view-transition-old(treasure) { animation: treasure-out .17s ease-in both; }
::view-transition-new(treasure) { animation: treasure-in .28s .06s ease-out both; }

@keyframes treasure-out { to { opacity: 0; } }
@keyframes treasure-in { from { opacity: 0; } }

/* Everything that is not the card or the masthead - the drawbridge, the other
   cards, the prose - is the root snapshot.
 *
 * These two overlap completely and deliberately. A lead-and-follow works for
 * the card because the root snapshot is still painted underneath it; the root
 * has nothing underneath it at all. Nothing in the ::view-transition tree is
 * the real page - the real page stops being painted the moment the
 * transition starts - so a gap between the two root animations is a gap where
 * the only thing on screen is the flat canvas colour, with the atmosphere
 * gone. A quarter of a second of the room's lights going out. */
::view-transition-old(root) { animation: room-out .26s linear both; }
::view-transition-new(root) { animation: room-in .26s linear both; }

@keyframes room-out { to { opacity: 0; } }
@keyframes room-in { from { opacity: 0; } }

/* ---- The forge ---------------------------------------------------------- */

/* The Furnace is one treasure and four ways out, and three of those ways used
   to look identical: the page was replaced and a different treasure was
   there. Keeping a thing, shelving it and burning it are not the same event
   and should not read as the same event.
 *
   So the plate is a named element on this surface too. Keep and shelve morph
   it - the next treasure comes up into the same frame the last one left, the
   way a queue behaves - and the burn does what the surface is named for. */
.forge-plate { view-transition-name: plate; }

::view-transition-group(plate) {
  animation-duration: .34s;
  animation-timing-function: cubic-bezier(.2, .8, .2, 1);
}
/* The next one rises out of the forge rather than fading in on the spot. */
::view-transition-new(plate) { animation: plate-rise .3s .04s cubic-bezier(.2, .8, .2, 1) both; }
@keyframes plate-rise { from { opacity: 0; translate: 0 1.4rem; } }

/* The burn.
 *
 * `data-moment` is on <html> of the page being entered, and that is the only
 * place it can be: the pseudo-element tree belongs to the new document, so
 * the outgoing plate cannot be told what is happening to it by the document
 * it is leaving. items_controller sets it for exactly one render.
 *
 * The plate is consumed from the bottom up. The mask is twice the element's
 * height - transparent across its lower half, opaque across its upper half,
 * with a soft ramp between the two - so at `0% 0%` the plate sees only the
 * opaque half and is whole, at `0% 100%` it sees only the transparent half
 * and is gone, and in between the ramp is a char line travelling upward
 * through it. A `clip-path` would do the same thing with a razor edge, which
 * is a sheet of paper being cut rather than burned.
 *
 * It brightens before it darkens, because that is the order those things
 * happen in, and it sinks a little as it goes: what is left of a burning
 * thing is not held up by anything.
 *
 * Nothing here needs a reduced-motion guard - Turbo does not start a View
 * Transition at all for a reader who asked for less - and the Restraint Dial
 * cuts every ::view-transition animation to a frame in atmosphere.css. */
:root[data-moment="burn"]::view-transition-old(plate) {
  mask-image: linear-gradient(to top, #0000 0 50%, #000 62% 100%);
  mask-size: 100% 200%;
  mask-repeat: no-repeat;
  animation: burn-away .66s cubic-bezier(.45, .05, .55, 1) both;
}
@keyframes burn-away {
  0% {
    mask-position: 0% 0%;
    filter: brightness(1) saturate(1);
    translate: 0 0;
  }
  16% {
    filter: brightness(1.22) saturate(1.3) sepia(.22) contrast(1.03);
  }
  100% {
    mask-position: 0% 100%;
    filter: brightness(.28) saturate(.22) sepia(.4);
    translate: 0 .9rem;
  }
}
/* The plate is burning on top of the wall arriving underneath it, so the wall
   must not also be fading in over the top of the fire. The root pair keeps
   its own overlap - see the note above - and the plate simply outlives it. */
