/*
  motion.css
  All the scroll animations, kept in one place. They reuse the site's existing moves:
  the hero "curtain" (clip-path), the navbar underline that grows from the left,
  and the same --ease-out curve, so everything feels like one system.

  Two kinds of animation live here:
  A. Reveal on arrival: plays once when js/reveal.js adds .is-visible.
  B. Scroll-linked:     follows the scrollbar using CSS scroll timelines
                        (animation-timeline). Browsers without support simply
                        show the page without these, nothing breaks.

  Everything sits inside "prefers-reduced-motion: no-preference", so visitors
  who turned motion off in their device settings get a still page.
  Only transform, translate, scale and opacity are animated (cheap for the browser to draw).
*/

@media (prefers-reduced-motion: no-preference) {

  /* ================= A. Reveal on arrival ================= */

  /* ---------- Headings: words rise out of a mask, one after another ----------
     js/motion.js wraps each word of [data-split="rise"] in .split-word > .split-word__inner.
     The outer span hides overflow, the inner one slides up from below it. */
  .js [data-split="rise"] .split-word {
    display: inline-block;
    overflow: hidden;
    vertical-align: top;
    /* A little breathing room so tall letters aren't clipped by the mask */
    padding-block: 0.08em;
    margin-block: -0.08em;
  }

  .js [data-split="rise"] .split-word__inner {
    display: inline-block;
    transform: translateY(110%);
    transition: transform 1.1s var(--ease-out);
    transition-delay: calc(120ms + var(--w, 0) * 70ms);
  }

  .js .is-visible [data-split="rise"] .split-word__inner {
    transform: none;
  }

  /* ---------- Photos: the same bottom-to-top curtain as the hero portrait ----------
     A layer in the page colour covers the photo, then shrinks up and away
     (like the hero's clip-path, but done with transform). The photo underneath
     settles from a slight zoom at the same time.
     Tricky part: we don't clip the frame itself, because IntersectionObserver
     treats a fully clipped element as "not on screen" and would never reveal it. */
  .js .reveal-media,
  .js .reveal.reveal-media {
    opacity: 1;
    transform: none;
    position: relative;
  }

  .js .reveal-media::after {
    content: "";
    position: absolute;
    inset: -1px;
    z-index: 1;
    background: var(--color-bg);
    transform-origin: top;
    pointer-events: none;
    transition: transform 1.4s var(--ease-out);
    transition-delay: calc(var(--reveal-index, 0) * 80ms);
  }

  .js .is-visible .reveal-media::after,
  .js .reveal-media.is-visible::after {
    transform: scaleY(0);
  }

  /* "scale" is separate from "transform", so the hover zoom in about.css / projects.css still works */
  .js .reveal-media img,
  .js .reveal-media video {
    scale: 1.18;
    transition:
      scale 1.8s var(--ease-out),
      transform 1.2s var(--ease-out),
      filter var(--duration-slow) var(--ease-out);
  }

  .js .is-visible .reveal-media img,
  .js .reveal-media.is-visible img,
  .js .is-visible .reveal-media video,
  .js .reveal-media.is-visible video {
    scale: 1;
  }

  /* ---------- About facts: the top rule draws in from the left ----------
     Same idea as the navbar underline. It draws in accent blue,
     then cools down to the normal border colour. */
  .js .fact {
    position: relative;
    border-top-color: transparent;
  }

  .js .fact::before {
    content: "";
    position: absolute;
    top: -1px;
    left: 0;
    right: 0;
    height: 1px;
    background-color: var(--color-accent);
    transform: scaleX(0);
    transform-origin: left;
    transition:
      transform 1.2s var(--ease-out) calc(150ms + var(--reveal-index, 0) * 120ms),
      background-color 1.2s var(--ease-out) calc(1000ms + var(--reveal-index, 0) * 120ms);
  }

  .js .fact.is-visible::before {
    transform: scaleX(1);
    background-color: var(--color-border-strong);
  }

  /* ---------- Skill cards: fade and slide up, one after another ----------
     Shorter than the default reveal. Tricky part: the delay is set per property, so
     only the arrival waits its turn; the hover shadow still reacts straight away. */
  .js .skill-card.reveal {
    --card-delay: calc(var(--reveal-index, 0) * 90ms);
    transition:
      opacity 450ms var(--ease-out) var(--card-delay),
      transform 450ms var(--ease-out) var(--card-delay),
      box-shadow var(--duration-base) var(--ease-out),
      border-color var(--duration-base) var(--ease-out);
  }

  /* ---------- Skill chips: quick fade + scale in after their card ----------
     Uses "scale" for arriving and "translate" for the hover lift, so they never fight. */
  .js .skill-card.reveal .skill-chip {
    opacity: 0;
    scale: 0.9;
    transition:
      opacity 300ms var(--ease-out) calc(var(--card-delay) + 150ms + var(--i, 0) * 40ms),
      scale 300ms var(--ease-out) calc(var(--card-delay) + 150ms + var(--i, 0) * 40ms),
      border-color var(--duration-fast) var(--ease-out),
      box-shadow var(--duration-fast) var(--ease-out),
      translate var(--duration-fast) var(--ease-out);
  }

  .js .skill-card.is-visible .skill-chip {
    opacity: 1;
    scale: 1;
  }

  .skill-chip:hover {
    translate: 0 -2px;
  }

  /* ---------- Project cards: curtain on the photo, then the text lines follow ----------
     The card itself stays put; its photo and text lines do the moving. */
  .js .project-card.reveal {
    opacity: 1;
    transform: none;
  }

  .js .project-card.reveal .project-card__body > * {
    opacity: 0;
    translate: 0 14px;
    transition:
      opacity 800ms var(--ease-out) calc(350ms + var(--n, 0) * 70ms),
      translate 800ms var(--ease-out) calc(350ms + var(--n, 0) * 70ms);
  }

  .project-card__body > :nth-child(2) { --n: 1; }
  .project-card__body > :nth-child(3) { --n: 2; }
  .project-card__body > :nth-child(4) { --n: 3; }
  .project-card__body > :nth-child(5) { --n: 4; }

  .js .project-card.is-visible .project-card__body > * {
    opacity: 1;
    translate: none;
  }

  /* ---------- Contact panel: one slow light sweep, like light across brushed metal ---------- */
  .js .contact__panel {
    position: relative;
    overflow: hidden;
  }

  .js .contact__panel::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(105deg,
      transparent 30%,
      color-mix(in srgb, var(--color-text) 7%, transparent) 50%,
      transparent 70%);
    transform: translateX(-100%);
    pointer-events: none;
    transition: transform 2.4s var(--ease-out) 450ms;
  }

  .js .contact__panel.is-visible::after {
    transform: translateX(100%);
  }
}


/* ================= B. Scroll-linked (follows the scrollbar) ================= */

/* Hidden unless the browser supports scroll timelines */
.scroll-progress {
  display: none;
}

@supports (animation-timeline: scroll()) {
  @media (prefers-reduced-motion: no-preference) {

    /* ---------- Reading progress: a hairline under the navbar ---------- */
    .scroll-progress {
      display: block;
      position: absolute;
      left: 0;
      right: 0;
      bottom: -1px;
      height: 1px;
      background: var(--color-accent);
      transform-origin: left;
      transform: scaleX(0);
      animation: progress-grow linear both;
      animation-timeline: scroll(root block);
    }

    /* ---------- Hero: portrait slowly pushes in as you leave the first screen ---------- */
    .hero__image {
      animation: hero-image-drift linear both;
      animation-timeline: scroll(root block);
      animation-range: 0 100vh;
    }

    /* The name drifts up and dims, so the next section takes over (desktop only,
       on phones the photo sits below the text and you need to read it first) */
    @media (min-width: 900px) {
      .hero__text {
        animation: hero-text-drift linear both;
        animation-timeline: scroll(root block);
        animation-range: 0 100vh;
      }
    }
  }
}

@supports (animation-timeline: view()) {
  @media (prefers-reduced-motion: no-preference) {

    /* ---------- About statement: words light up as you read down ----------
       Each word gets its own slice of the scroll, based on --p (0 = first word, 1 = last). */
    .js [data-split="read"] {
      view-timeline-name: --read;
    }

    .js [data-split="read"] .split-word__inner {
      animation: word-read linear both;
      animation-timeline: --read;
      /* Starts once the paragraph is about a quarter of the way up the screen and
         finishes near the middle, so the words light up where you're reading */
      animation-range:
        cover calc(22% + var(--p, 0) * 28%)
        cover calc(30% + var(--p, 0) * 28%);
    }

    /* ---------- Photos: gentle parallax inside their frame ----------
       The image is made a bit taller than its frame, then slides from -6% to +6%
       while the frame crosses the screen. */
    .parallax {
      position: relative;
      view-timeline-name: --media;
    }

    /* video: the hover clip on project cards moves exactly with its picture */
    .parallax img,
    .parallax video {
      position: absolute;
      top: -8%;
      left: 0;
      width: 100%;
      height: 116%;
      animation: media-parallax linear both;
      animation-timeline: --media;
      animation-range: cover;
    }
  }
}

/* ---------- Keyframes ---------- */
@keyframes progress-grow {
  to {
    transform: scaleX(1);
  }
}

@keyframes hero-image-drift {
  from {
    translate: 0 0;
    scale: 1;
  }
  to {
    translate: 0 4%;
    scale: 1.1;
  }
}

@keyframes hero-text-drift {
  to {
    translate: 0 -10vh;
    opacity: 0.5;
  }
}

@keyframes word-read {
  from {
    opacity: 0.18;
  }
  to {
    opacity: 1;
  }
}

@keyframes media-parallax {
  from {
    translate: 0 -6%;
  }
  to {
    translate: 0 6%;
  }
}
