/* ==========================================================================
   Animations
   Hero entrance, hero diagram loop, step drawings, scroll reveal,
   pause control and reduced motion. js/main.js toggles these classes:
     .on      a diagram part has lit up            .live  the diagram loop runs
     .idle    diagram loop paused (off screen)      .in    element revealed
     .run     step drawing is on screen             .paused  user paused motion
   ========================================================================== */


/* Hero entrance: each block rises in, one after another
   -------------------------------------------------------------------------- */

.hero > * {
  animation: rise .8s var(--ease) both;
}

.hero > :nth-child(2) { animation-delay: .06s; }
.hero > :nth-child(3) { animation-delay: .12s; }
.hero > :nth-child(4) { animation-delay: .18s; }
.hero > :nth-child(5) { animation-delay: .28s; }
.hero > :nth-child(6) { animation-delay: .36s; }


/* Hero diagram
   1. Each node and link lights up once (.on), left to right.
   2. Then a comet runs along the links and a ring pings on each node,
      one after another, every 4 seconds (.live).
   -------------------------------------------------------------------------- */

.node {
  transition: color .5s var(--ease);
}

.node svg {
  transition: border-color .5s var(--ease), stroke .5s var(--ease), background-color .5s var(--ease);
}

.node.on {
  color: var(--text);
}

.node.on svg {
  border-color: var(--green-ink);
  stroke: var(--green-ink);
}

.node-end .check {
  stroke-dasharray: 1;
  transition: stroke-dashoffset .5s var(--ease) .2s;
}

.js .node-end:not(.on) .check {
  stroke-dashoffset: 1;
}

.node-end.on svg {
  background: var(--green-ink);
  stroke: #fff;
}

/* Link fill */
.link::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--green-soft);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform .5s var(--ease);
}

.link.on::after {
  transform: scaleX(1);
}

/* Comet that travels along each link */
.link::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 45%;
  height: 100%;
  background: linear-gradient(90deg, transparent, var(--green));
  transform: translateX(-101%);
}

/* Ring that pings around each node */
.node-ring {
  position: absolute;
  inset: 0;
  border: 1px solid var(--green);
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
}

.chain.live .link::before {
  animation: comet-x 4s linear infinite;
}

.chain.live .node-ring {
  animation: ring 4s var(--ease) infinite;
}

/* Stagger: children alternate node, link, node, link, node… */
.chain.live > :nth-child(3) .node-ring,
.chain.live > :nth-child(4)::before { animation-delay: .8s; }

.chain.live > :nth-child(5) .node-ring,
.chain.live > :nth-child(6)::before { animation-delay: 1.6s; }

.chain.live > :nth-child(7) .node-ring,
.chain.live > :nth-child(8)::before { animation-delay: 2.4s; }

.chain.live > :nth-child(9) .node-ring { animation-delay: 3.2s; }

.chain.idle *,
.chain.idle *::before {
  animation-play-state: paused;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(14px); }
}

@keyframes comet-x {
  0%        { transform: translateX(-101%); }
  20%, 100% { transform: translateX(225%); }
}

@keyframes comet-y {
  0%        { transform: translateY(-101%); }
  20%, 100% { transform: translateY(225%); }
}

@keyframes ring {
  0%        { opacity: .5; transform: scale(1); }
  22%, 100% { opacity: 0; transform: scale(1.5); }
}


/* Step drawings (Connect, Find, Fix)
   Drawn once when revealed (.in), then a light loop while on screen (.run).
   -------------------------------------------------------------------------- */

.anim {
  display: block;
  width: 200px;
  height: 120px;
  fill: none;
  stroke: var(--line);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.anim * {
  animation-play-state: paused;
}

.anim.run * {
  animation-play-state: running;
}

.anim .dot {
  fill: var(--text);
  stroke: none;
}

.wire,
.branch {
  stroke: var(--line-strong);
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  transition: stroke-dashoffset 1s var(--ease) .2s;
}

.in .wire,
.in .branch {
  stroke-dashoffset: 0;
}

/* Connect: pulses travel the wires into the diamond, with a rest in each loop */
.pulse {
  stroke: var(--green);
  stroke-dasharray: .12 1.88;
  opacity: 0;
  transition: opacity .4s 1.1s;
  animation: flow 3.2s linear infinite;
}

.in .pulse {
  opacity: 1;
}

.p2 { animation-duration: 2.8s; animation-delay: -1.1s; }
.p3 { animation-duration: 3.6s; animation-delay: -2.3s; }

.hub {
  stroke: var(--text);
}

.hub-in {
  stroke: var(--green);
  transform-box: fill-box;
  transform-origin: center;
  animation: breathe 2.4s ease-in-out infinite;
}

/* Find: a bar scans the code and the hot line turns green */
.code {
  stroke-width: 6;
}

.code-hit {
  transition: stroke .5s 1.4s;
}

.in .code-hit {
  stroke: var(--green);
}

.beam {
  fill: rgb(40 134 66 / .12);
  stroke: none;
  animation: scan 3.6s cubic-bezier(.45, 0, .55, 1) infinite;
}

/* Fix: a dot travels the branch and lands on the main line */
.trunk {
  stroke: var(--text);
}

.branch {
  stroke: var(--green);
}

.anim .dot-merge {
  fill: var(--green);
  transform-box: fill-box;
  transform-origin: center;
  animation: land 3.2s var(--ease) infinite;
}

.runner {
  display: none;
  fill: var(--green-ink);
  stroke: var(--surface);
  stroke-width: 2;
}

@supports (offset-path: path("M0 0H1")) {
  .runner {
    display: block;
    offset-path: path("M40 66C56 66 52 30 72 30H92C112 30 108 66 124 66");
    offset-rotate: 0deg;
    animation: travel 3.2s cubic-bezier(.45, 0, .55, 1) infinite;
  }
}

@keyframes flow {
  from { stroke-dashoffset: 2; }
  to   { stroke-dashoffset: 0; }
}

@keyframes breathe {
  50% { transform: scale(.8); }
}

@keyframes scan {
  0%        { opacity: 0; transform: translateY(-12px); }
  15%, 70%  { opacity: 1; }
  85%, 100% { opacity: 0; transform: translateY(76px); }
}

@keyframes travel {
  0%        { opacity: 0; offset-distance: 0%; }
  10%, 66%  { opacity: 1; }
  72%, 100% { opacity: 0; offset-distance: 100%; }
}

@keyframes land {
  0%, 68%   { transform: scale(1); }
  76%       { transform: scale(1.5); }
  88%, 100% { transform: scale(1); }
}


/* Scroll reveal (only when JS is running)
   -------------------------------------------------------------------------- */

.js .reveal {
  opacity: 0;
  transform: translateY(12px);
  transition: opacity .7s var(--ease), transform .7s var(--ease);
}

.js .reveal.in {
  opacity: 1;
  transform: none;
}

/* "What it finds": the findings light up one after another */
.js .lit b {
  color: var(--faint);
  transition: color .6s var(--ease);
}

.js .lit.in b {
  color: var(--text);
}

.lit b:nth-of-type(2) { transition-delay: .12s; }
.lit b:nth-of-type(3) { transition-delay: .24s; }
.lit b:nth-of-type(4) { transition-delay: .36s; }
.lit b:nth-of-type(5) { transition-delay: .48s; }
.lit b:nth-of-type(6) { transition-delay: .6s; }
.lit b:nth-of-type(7) { transition-delay: .72s; }
.lit b:nth-of-type(8) { transition-delay: .84s; }


/* Pause control
   -------------------------------------------------------------------------- */

.paused *,
.paused *::before,
.paused *::after {
  animation-play-state: paused !important;
}


/* Vertical diagram on smaller screens
   -------------------------------------------------------------------------- */

@media (max-width: 900px) {
  .link::after {
    transform: scaleY(0);
    transform-origin: top;
  }

  .link.on::after {
    transform: scaleY(1);
  }

  .link::before {
    width: 100%;
    height: 45%;
    background: linear-gradient(180deg, transparent, var(--green));
    transform: translateY(-101%);
  }

  .chain.live .link::before {
    animation-name: comet-y;
  }
}


/* Reduced motion: everything shows in its final state, nothing loops
   -------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }

  .js .reveal {
    opacity: 1;
    transform: none;
  }

  .motion-toggle,
  .pulse,
  .runner,
  .beam {
    display: none;
  }

  .wire,
  .branch {
    stroke-dashoffset: 0;
  }

  .code-hit {
    stroke: var(--green);
  }
}
