/*
  Motion. Every animation here answers one of: what appeared (reveal), who is talking
  (voice wave), what changed (state pops), or where you went (page transition).
  All of it collapses to instant under prefers-reduced-motion.

  Reveal is Speechify's own: opacity 0, blur 8px, 5px rise, to rest.
*/

@media (prefers-reduced-motion: no-preference) {
  /* ---------------------------------------------------------------- reveal */
  .reveal,
  .rise {
    animation: reveal var(--t-slow) var(--ease) both;
    animation-delay: calc(var(--i, 0) * 70ms);
  }
  .stagger > * {
    animation: reveal var(--t-slow) var(--ease) both;
    animation-delay: calc(var(--i, 0) * 70ms + 80ms);
  }
  .htmx-added.rise,
  .htmx-added .rise {
    animation: reveal 0.5s var(--ease) both;
  }

  /* below-the-fold blocks reveal as they scroll in, where the browser supports it */
  @supports (animation-timeline: view()) {
    .on-scroll {
      animation: reveal linear both;
      animation-timeline: view();
      animation-range: entry 0% entry 40%;
    }
  }

  /* ---------------------------------------------------------------- voice wave */
  .wave i {
    animation: wave 0.8s linear infinite;
    animation-play-state: paused;
  }
  .wave i:nth-child(2) {
    animation-delay: -0.15s;
  }
  .wave i:nth-child(3) {
    animation-delay: -0.3s;
  }
  .wave i:nth-child(4) {
    animation-delay: -0.45s;
  }
  .wave i:nth-child(5) {
    animation-delay: -0.6s;
  }
  .brand:hover .wave i,
  .wave.is-live i {
    animation-play-state: running;
  }
  /* "speaking": a few beats while a question is read out, then rest */
  .is-speaking .wave i,
  .wave.is-speaking i {
    animation-play-state: running;
    animation-iteration-count: 3;
  }

  /* ---------------------------------------------------------------- the bot's face */
  /* live: a slow breath; speaking (a question being read out): a few small nods */
  .face.is-live {
    animation: face-breathe 3.2s var(--ease-std) infinite;
  }
  .is-speaking .face,
  .face.is-speaking {
    animation: face-nod 0.46s var(--ease-std) 4;
  }

  /* ---------------------------------------------------------------- question reveal */
  /* app.js splits [data-speak] into .w spans; the words blur in one after another. */
  .speak .w {
    display: inline;
    animation: reveal 0.42s var(--ease) both;
    animation-delay: calc(var(--w) * 45ms);
  }
  /* what follows the question waits for it to finish */
  .after-speak {
    animation: reveal 0.5s var(--ease) both;
    animation-delay: calc(var(--speak-ms, 0ms) + 120ms + var(--i, 0) * 60ms);
  }

  /* ---------------------------------------------------------------- state pops */
  .chip input:checked + span {
    animation: pop 0.32s var(--ease-out);
  }
  .chip input:checked + span .icon {
    animation: icon-in 0.3s var(--ease-out) both;
  }
  .bars .bar-fill {
    animation: grow 0.9s var(--ease) both;
    animation-delay: calc(var(--i, 0) * 60ms + 200ms);
    transform-origin: bottom center;
  }
  .meter > span {
    animation: fill 1.1s var(--ease) both;
    animation-delay: 0.25s;
  }
  .seg-current::after {
    animation: pulse 2.4s var(--ease-std) infinite;
  }
  .typing i {
    animation: typing 1.2s var(--ease-std) infinite;
  }
  .typing i:nth-child(2) {
    animation-delay: 0.15s;
  }
  .typing i:nth-child(3) {
    animation-delay: 0.3s;
  }

  /* ---------------------------------------------------------------- page transitions */
  @view-transition {
    navigation: auto;
  }
  ::view-transition-old(root) {
    animation: vt-out 0.18s var(--ease-std) both;
  }
  ::view-transition-new(root) {
    animation: vt-in 0.42s var(--ease) both;
  }
  ::view-transition-group(nav-pill) {
    animation-duration: 0.42s;
    animation-timing-function: var(--ease);
  }
  ::view-transition-old(sidebar),
  ::view-transition-new(sidebar),
  ::view-transition-old(topbar),
  ::view-transition-new(topbar) {
    animation: none;
  }
}

@keyframes reveal {
  from {
    opacity: 0;
    filter: blur(8px);
    transform: translateY(5px);
  }
  to {
    opacity: 1;
    filter: blur(0);
    transform: none;
  }
}
@keyframes face-breathe {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-3px);
  }
}
@keyframes face-nod {
  0%,
  100% {
    transform: scale(1) rotate(0);
  }
  50% {
    transform: scale(1.06) rotate(-4deg);
  }
}
@keyframes wave {
  0%,
  100% {
    transform: scaleY(1);
  }
  50% {
    transform: scaleY(0.3);
  }
}
@keyframes pop {
  from {
    transform: scale(0.96);
  }
  to {
    transform: scale(1);
  }
}
@keyframes icon-in {
  from {
    opacity: 0;
    transform: scale(0.4);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
@keyframes grow {
  from {
    transform: scaleY(0);
  }
  to {
    transform: scaleY(1);
  }
}
@keyframes fill {
  from {
    transform: scaleX(0);
  }
  to {
    transform: scaleX(1);
  }
}
@keyframes pulse {
  0%,
  100% {
    opacity: 0.35;
  }
  50% {
    opacity: 1;
  }
}
@keyframes typing {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}
@keyframes vt-out {
  to {
    opacity: 0;
  }
}
@keyframes vt-in {
  from {
    opacity: 0;
    filter: blur(6px);
    transform: translateY(6px);
  }
}

/* in-flight HTMX requests */
.when-busy {
  display: none;
}
.htmx-request .when-idle {
  display: none;
}
.htmx-request .when-busy {
  display: inline-flex;
}
form.htmx-request {
  opacity: 0.7;
  pointer-events: none;
  transition: opacity 0.2s;
}
