/* ==========================================================================
   Motion — cross-document view transitions + scroll reveals
   Supported: Chrome/Edge 126+, Safari 18.2+. Other browsers navigate normally.
   ========================================================================== */

/* The @view-transition opt-in itself is inlined in each page's <head>
   (see the <style> block there) so Chrome sees it before first render. */

/* The sidebar is identical on every page: keep it perfectly still. */
.sidebar {
  view-transition-name: sidebar;
}

.main {
  view-transition-name: main;
}

/* Chrome that's identical on every page stays perfectly still:
   the sidebar, and on narrow screens the top bar and menu button. */
::view-transition-group(root),
::view-transition-group(sidebar),
::view-transition-group(topbar),
::view-transition-group(menu-button) {
  animation: none;
}

::view-transition-old(sidebar),
::view-transition-new(sidebar),
::view-transition-old(topbar),
::view-transition-new(topbar),
::view-transition-old(menu-button),
::view-transition-new(menu-button) {
  animation: none;
}

/* Keep the fixed chrome above the page content while it transitions */
::view-transition-group(topbar),
::view-transition-group(menu-button) {
  z-index: 20;
}

/* Main content (everything that isn't morphing):
   the old page clears out quickly, the new page fades in
   as the shared elements are landing. */
::view-transition-group(main) {
  animation: none;
}

::view-transition-old(main) {
  animation: vt-out var(--dur-page-out) ease-out both;
}

::view-transition-new(main) {
  animation: vt-in var(--dur-page-in) var(--ease-out) var(--delay-page-in) both;
}

@keyframes vt-out {
  to {
    opacity: 0;
  }
}

@keyframes vt-in {
  from {
    opacity: 0;
    transform: translateY(0.75rem);
  }
}

/* Sub-page ↔ sub-page (work → work, work ↔ about) -------------------------
   No shared element to morph here, so the content column itself moves, in the
   direction of the sidebar: the old page lifts away and fades out quickly,
   then the new page rises into place (both reversed when going up the list).
   No clip edges and no dimmed-but-visible old page: a large light image
   dimmed over the dark background reads as a flat grey slab.
   transitions.js sets .vt-sub plus .vt-forward (a page lower in the sidebar)
   or .vt-backward (higher up), so Back always plays the motion in reverse.
   Lengths are in vh, not %, because on narrow screens .main is as tall as
   the whole page while only one screen of it is visible. */

/* The hero and title join the page instead of travelling on their own. */
:root.vt-sub .main [style*="view-transition-name"] {
  view-transition-name: none !important;
}

/* Plain stacking instead of the default plus-lighter cross-fade, which adds
   the two pages' colours together: a light image on the old page would
   brighten (wash out) the new one wherever they overlap. */
:root.vt-sub::view-transition-old(main),
:root.vt-sub::view-transition-new(main) {
  mix-blend-mode: normal;
}

:root.vt-sub::view-transition-old(main) {
  animation-duration: var(--dur-page-out);
  animation-timing-function: cubic-bezier(0.5, 0, 0.75, 0); /* ease-in quart */
  animation-delay: 0s;
  animation-fill-mode: both;
}

:root.vt-sub::view-transition-new(main) {
  animation-duration: var(--dur-slow);
  animation-timing-function: var(--ease-out);
  animation-delay: calc(var(--dur-page-out) * 0.5);
  animation-fill-mode: both;
}

:root.vt-forward::view-transition-old(main)  { animation-name: vt-push-out-up; }
:root.vt-forward::view-transition-new(main)  { animation-name: vt-push-in-up; }
:root.vt-backward::view-transition-old(main) { animation-name: vt-push-out-down; }
:root.vt-backward::view-transition-new(main) { animation-name: vt-push-in-down; }

@keyframes vt-push-out-up {
  to {
    opacity: 0;
    transform: translateY(-3vh);
  }
}

@keyframes vt-push-in-up {
  from {
    opacity: 0;
    transform: translateY(6vh);
  }
}

@keyframes vt-push-out-down {
  to {
    opacity: 0;
    transform: translateY(3vh);
  }
}

@keyframes vt-push-in-down {
  from {
    opacity: 0;
    transform: translateY(-6vh);
  }
}

/* Active-item bar slides between nav items. */
::view-transition-group(nav-indicator) {
  animation-duration: var(--dur-morph);
  animation-timing-function: var(--ease-morph);
}

/* Shared elements: thumbnail <-> hero and card caption <-> page title
   (matched by view-transition-name: work-<slug> / work-title-<slug>). */
::view-transition-group(*.work-media),
::view-transition-group(*.work-title) {
  animation-duration: var(--dur-morph);
  animation-timing-function: var(--ease-morph);
}

/* Media: no crossfade. The old snapshot (frozen frame) stays fully opaque and
   the new element — live, and transparent until its first frame is ready —
   is stacked on top. When the new video/image paints the same frame it simply
   covers the old one: no dip, no flash, no double exposure. */
::view-transition-old(*.work-media),
::view-transition-new(*.work-media) {
  height: 100%;
  object-fit: cover;
  animation: none;
  mix-blend-mode: normal;
}

/* A hero with no partner on the other page (e.g. work → work via the sidebar,
   or work → about) leaves/enters with the rest of the page. */
::view-transition-old(*.work-media):only-child,
::view-transition-old(*.work-title):only-child {
  animation: vt-out var(--dur-page-out) ease-out both;
}

::view-transition-new(*.work-media):only-child,
::view-transition-new(*.work-title):only-child {
  animation: vt-in var(--dur-page-in) var(--ease-out) var(--delay-page-in) both;
}

/* Shared elements sit above everything else while they travel,
   and the title passes behind the image/video when their paths cross. */
::view-transition-group(*.work-title) {
  z-index: 10;
}

::view-transition-group(*.work-media) {
  z-index: 11;
}

/* Text: keep the glyphs at their natural size (no stretching),
   anchored top-left, and crossfade weight/color during the move. */
::view-transition-old(*.work-title),
::view-transition-new(*.work-title) {
  width: auto;
  height: 100%;
  animation-duration: var(--dur-morph);
  animation-timing-function: var(--ease-morph);
}

/* Scroll reveals ----------------------------------------------------------
   main.js tags each [data-reveal] element:
   - .reveal-initial  → in view on load: short staggered entrance
                         (skipped when the page arrived via a view transition)
   - .reveal-pending  → below the fold: hidden until it scrolls into view
   Without JS nothing is hidden. */

.reveal-initial {
  animation: reveal-up var(--dur-slow) var(--ease-out) both;
  animation-delay: calc(var(--i, 0) * 60ms);
}

:root.vt .reveal-initial {
  animation: none;
}

/* data-reveal="seq": parts of a page that appear one after another (a work
   page's text, then each detail) — also when the page arrived through a
   transition, in which case they start as the new page lands. */
[data-reveal="seq"] {
  --stagger: 90ms;
}

.reveal-initial[data-reveal="seq"],
.reveal-pending[data-reveal="seq"] {
  animation-delay: calc(var(--i, 0) * var(--stagger));
  transition-delay: calc(var(--i, 0) * var(--stagger));
}

:root.vt .reveal-initial[data-reveal="seq"] {
  animation: reveal-up var(--dur-slow) var(--ease-out) both;
  animation-delay: calc(var(--delay-page-in) + var(--i, 0) * var(--stagger));
}

.reveal-pending {
  opacity: 0;
  transform: translateY(1rem);
  transition:
    opacity var(--dur-slow) var(--ease-out),
    transform var(--dur-slow) var(--ease-out);
  transition-delay: calc(var(--i, 0) * 60ms);
}

.reveal-pending.is-in {
  opacity: 1;
  transform: none;
}

@keyframes reveal-up {
  from {
    opacity: 0;
    transform: translateY(1rem);
  }
}

/* Reduced motion: no page animation, no reveals. */
@media (prefers-reduced-motion: reduce) {
  .reveal-initial {
    animation: none;
  }

  .reveal-pending {
    opacity: 1;
    transform: none;
    transition: none;
  }

}

/* Theme switch -------------------------------------------------------------
   The new theme is revealed by a circle expanding from the theme icon
   (main.js wraps the switch in a same-document view transition and animates
   a clip-path on the new snapshot). While switching, every element-level
   transition and transition name is switched off, so nothing animates on its
   own schedule — that mismatch is what caused the sidebar flash. */
:root.theme-switching *,
:root.theme-switching *::before,
:root.theme-switching *::after {
  transition: none !important;
}

:root.theme-switching .sidebar,
:root.theme-switching .topbar,
:root.theme-switching .menu-button,
:root.theme-switching .main,
:root.theme-switching .nav-indicator,
:root.theme-switching [style*="view-transition-name"] {
  view-transition-name: none !important;
}

/* No crossfade: the old theme stays fully visible underneath; the new theme
   sits on top behind a ripple mask that main.js rewrites every frame.
   The long no-op animation just keeps the transition alive until main.js
   ends it (a transition ends when its animations do). */
:root.theme-switching::view-transition-old(root),
:root.theme-switching::view-transition-new(root) {
  animation: none;
  mix-blend-mode: normal;
}

:root.theme-switching::view-transition-old(root) {
  z-index: 1;
}

:root.theme-switching::view-transition-new(root) {
  z-index: 2;
  animation: theme-hold 60s linear;
}

@keyframes theme-hold {
  from { opacity: 1; }
  to { opacity: 1; }
}
