/* ==========================================================================
   AEG Equipamiento y Proyectos — Modern Effects & Scroll-Driven Animations
   Strictly gated behind @supports & @media (prefers-reduced-motion)
   ========================================================================== */

@layer effects {
  /* ------------------------------------------------------------------------
     1. AEG Logo SVG Kinetic Circuit & AC Sine Wave Animations
     ------------------------------------------------------------------------ */
  @keyframes pulse-busbar {
    0%, 100% {
      opacity: 0.85;
      filter: drop-shadow(0 0 4px rgb(232 44 74 / 0.4));
    }
    50% {
      opacity: 1;
      filter: drop-shadow(0 0 12px rgb(232 44 74 / 0.95));
    }
  }

  @keyframes rotate-stator {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }

  @keyframes flow-current {
    to {
      stroke-dashoffset: -48;
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    .aeg-svg-busbar {
      animation: pulse-busbar 3s ease-in-out infinite;
    }

    .aeg-svg-sine-flow {
      stroke-dasharray: 8 4;
      animation: flow-current 2.2s linear infinite;
    }

    .aeg-svg-orbit {
      transform-origin: 100px 88px;
      animation: rotate-stator 24s linear infinite;
    }
  }

  /* ------------------------------------------------------------------------
     2. Top-Layer Entry & Exit Animations (@starting-style + allow-discrete)
     ------------------------------------------------------------------------ */
  .site-menu[popover] {
    opacity: 0;
    transform: translateY(-8px) scale(0.97);
    transition-property: opacity, transform, display, overlay;
    transition-duration: 0.24s;
    transition-timing-function: var(--ease-out-expo);
    transition-behavior: allow-discrete;
  }

  .site-menu[popover]:is(:popover-open, .\:popover-open):where(:not([data-closing])) {
    opacity: 1;
    transform: translateY(0) scale(1);

    @starting-style {
      opacity: 0;
      transform: translateY(-8px) scale(0.97);
    }
  }

  /* Modal Dialog Top-Layer Transitions */
  dialog.dossier-dialog {
    opacity: 0;
    transition-property: opacity, transform, display, overlay;
    transition-duration: 0.28s;
    transition-timing-function: var(--ease-out-expo);
    transition-behavior: allow-discrete;

    @media (prefers-reduced-motion: no-preference) {
      transform: translateY(14px) scale(0.96);
    }
  }

  dialog.dossier-dialog[open]:where(:not([data-closing])) {
    opacity: 1;

    @starting-style {
      opacity: 0;
    }

    @media (prefers-reduced-motion: no-preference) {
      transform: translateY(0) scale(1);

      @starting-style {
        transform: translateY(14px) scale(0.96);
      }
    }
  }

  dialog.dossier-dialog::backdrop {
    background-color: rgb(5 7 12 / 0);
    transition-property: background-color, display, overlay;
    transition-duration: 0.28s;
    transition-timing-function: ease-out;
    transition-behavior: allow-discrete;
  }

  dialog.dossier-dialog[open]:where(:not([data-closing]))::backdrop {
    background-color: rgb(5 7 12 / 0.72);

    @starting-style {
      background-color: rgb(5 7 12 / 0);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .site-menu,
    dialog.dossier-dialog,
    dialog.dossier-dialog::backdrop {
      transition-duration: 0.1s;
    }
  }

  /* ------------------------------------------------------------------------
     3. Native CSS Scroll-Driven Animations (Progress, Shrinking Header, Reveals, Parallax)
     ------------------------------------------------------------------------ */
  @media (prefers-reduced-motion: no-preference) {
    /* 3A. Scroll Progress Busbar */
    @supports (animation-timeline: scroll()) {
      @keyframes grow-progress {
        from { transform: scaleX(0); }
        to { transform: scaleX(1); }
      }

      .scroll-progress {
        animation: grow-progress auto linear;
        animation-timeline: scroll(block root);
      }
    }

    /* 3B. Shrinking Header on Scroll */
    @supports ((animation-timeline: scroll()) and (animation-range: 0% 100%)) {
      @keyframes header-compact {
        from {
          block-size: 5.15rem;
          background-color: rgb(11 14 20 / 0.62);
        }
        to {
          block-size: 4.15rem;
          background-color: rgb(11 14 20 / 0.94);
          box-shadow: 0 12px 32px -12px rgb(0 0 0 / 0.72);
        }
      }

      .site-header {
        animation: header-compact auto linear both;
        animation-timeline: scroll(block root);
        animation-range: 0px 150px;
      }
    }

    /* 3C. Scroll Entry Reveal Effects via view() timeline */
    @supports ((animation-timeline: view()) and (animation-range: entry)) {
      @keyframes reveal-up {
        from {
          opacity: 0;
          transform: translateY(28px) scale(0.97);
        }
        to {
          opacity: 1;
          transform: translateY(0) scale(1);
        }
      }

      .reveal-item {
        animation: reveal-up auto var(--ease-out-expo) backwards;
        animation-timeline: view(block);
        animation-range: entry 4% entry 32%;
      }

      /* 3D. Hero Parallax Depth */
      @keyframes hero-parallax {
        from {
          transform: translateY(0);
        }
        to {
          transform: translateY(-42px);
        }
      }

      .hero-parallax {
        animation: hero-parallax auto linear both;
        animation-timeline: scroll(block root);
        animation-range: 0px 650px;
      }
    }
  }

  /* Fallback class when JS IntersectionObserver handles reveal */
  .reveal-item.is-pending {
    opacity: 0;
    transform: translateY(24px);
    transition: opacity 0.55s var(--ease-out-expo), transform 0.55s var(--ease-out-expo);
  }

  .reveal-item.is-pending.is-visible {
    opacity: 1;
    transform: translateY(0);
  }

  /* ------------------------------------------------------------------------
     4. Cross-Document, Directional & Shared-Group View Transitions
     ------------------------------------------------------------------------ */
  @view-transition {
    navigation: auto;
  }

  @media (prefers-reduced-motion: reduce) {
    @view-transition {
      navigation: none;
    }
  }

  ::view-transition {
    pointer-events: none;
  }

  /* Disable root snapshot during scoped in-page filter/tab View Transitions */
  :root.is-local-vt {
    view-transition-name: none;
  }

  :root.is-local-vt .site-header,
  :root.is-local-vt .site-header .site-logo,
  :root.is-local-vt .site-header.is-wide .site-menu a[aria-current="page"],
  :root.is-local-vt .wa-fab {
    view-transition-name: none;
  }

  @media (prefers-reduced-motion: no-preference) {
    .site-header {
      view-transition-name: site-header;
    }

    .site-header .site-logo {
      view-transition-name: header-logo;
    }

    .site-header.is-wide .site-menu a[aria-current="page"] {
      view-transition-name: nav-active-pill;
    }

    .wa-fab {
      view-transition-name: wa-fab;
    }

    .vt-card {
      view-transition-class: vt-card;
    }
  }

  /* Smooth gliding morph for active navbar indicator & shared thumbnails */
  ::view-transition-group(nav-active-pill),
  ::view-transition-group(shared-card-media) {
    animation-duration: 0.42s;
    animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
  }

  /* Default cross-document transition when direction is unknown */
  @keyframes page-fade-out {
    to {
      opacity: 0;
      transform: scale(0.985);
    }
  }

  @keyframes page-fade-in {
    from {
      opacity: 0;
      transform: translateY(14px) scale(0.99);
    }
  }

  /* Directional forward / backward keyframes (compositor-only transform + opacity) */
  @keyframes slide-to-left {
    to {
      transform: translateX(-9%) scale(0.975);
      opacity: 0;
    }
  }

  @keyframes slide-from-right {
    from {
      transform: translateX(9%) scale(0.985);
      opacity: 0;
    }
  }

  @keyframes slide-to-right {
    to {
      transform: translateX(9%) scale(0.975);
      opacity: 0;
    }
  }

  @keyframes slide-from-left {
    from {
      transform: translateX(-9%) scale(0.985);
      opacity: 0;
    }
  }

  ::view-transition-group(root) {
    animation: 0.44s cubic-bezier(0.22, 1, 0.36, 1) both;
  }

  ::view-transition-old(root) {
    animation: 0.28s cubic-bezier(0.32, 0, 0.67, 0) both page-fade-out;
  }

  ::view-transition-new(root) {
    animation: 0.44s cubic-bezier(0.22, 1, 0.36, 1) both page-fade-in;
  }

  html:active-view-transition-type(forward)::view-transition-old(root) {
    animation-name: slide-to-left;
  }

  html:active-view-transition-type(forward)::view-transition-new(root) {
    animation-name: slide-from-right;
  }

  html:active-view-transition-type(backward)::view-transition-old(root) {
    animation-name: slide-to-right;
  }

  html:active-view-transition-type(backward)::view-transition-new(root) {
    animation-name: slide-from-left;
  }

  /* Group Element Transitions for filtering Projects, Brands & Gallery Swaps */
  @keyframes vt-card-in {
    from {
      opacity: 0;
      transform: translateY(18px) scale(0.94);
    }
  }

  @keyframes vt-card-out {
    to {
      opacity: 0;
      transform: scale(0.92);
    }
  }

  ::view-transition-group(.vt-card) {
    animation-duration: 0.38s;
    animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
  }

  ::view-transition-new(.vt-card):only-child {
    animation: 0.34s cubic-bezier(0.22, 1, 0.36, 1) both vt-card-in;
  }

  ::view-transition-old(.vt-card):only-child {
    animation: 0.22s ease-in both vt-card-out;
  }

  /* Staggered Above-the-Fold Hero Choreography */
  @keyframes hero-stagger-up {
    from {
      opacity: 0;
      transform: translateY(18px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    .hero__content > *,
    .page-hero__grid > div:first-child > * {
      animation: hero-stagger-up 0.58s var(--ease-out-expo) backwards;
    }

    .hero__content > :nth-child(1),
    .page-hero__grid > div:first-child > :nth-child(1) { animation-delay: 0.03s; }
    .hero__content > :nth-child(2),
    .page-hero__grid > div:first-child > :nth-child(2) { animation-delay: 0.09s; }
    .hero__content > :nth-child(3),
    .page-hero__grid > div:first-child > :nth-child(3) { animation-delay: 0.15s; }
    .hero__content > :nth-child(4),
    .page-hero__grid > div:first-child > :nth-child(4) { animation-delay: 0.21s; }
    .hero__content > :nth-child(5) { animation-delay: 0.27s; }

    .hero-slider,
    .kpi-deck .kpi-tile {
      animation: hero-stagger-up 0.62s var(--ease-out-expo) backwards;
    }

    .hero-slider { animation-delay: 0.14s; }
    .kpi-deck .kpi-tile:nth-child(1) { animation-delay: 0.12s; }
    .kpi-deck .kpi-tile:nth-child(2) { animation-delay: 0.18s; }
    .kpi-deck .kpi-tile:nth-child(3) { animation-delay: 0.24s; }
    .kpi-deck .kpi-tile:nth-child(4) { animation-delay: 0.30s; }
  }

  @media (prefers-reduced-motion: reduce) {
    ::view-transition-group(*),
    ::view-transition-old(*),
    ::view-transition-new(*) {
      animation: none !important;
    }
  }

  /* ------------------------------------------------------------------------
     5. Spring-Eased WhatsApp Concierge Popover & Marquee Motion
     ------------------------------------------------------------------------ */
  @keyframes wa-fade-in {
    from { opacity: 0; }
  }

  @keyframes wa-grow {
    from { scale: 0.88; }
  }

  .wa-panel:is(:popover-open, .\:popover-open) {
    animation: wa-fade-in 150ms ease-out both;
  }

  @media (prefers-reduced-motion: no-preference) {
    .wa-panel:is(:popover-open, .\:popover-open) {
      /* Inline linear() spring curve per natural-interactions guidance */
      animation:
        wa-grow 320ms linear(0, 0.45 15%, 1.06 38%, 0.98 58%, 1.01 78%, 1) both,
        wa-fade-in 150ms ease-out both;
    }

    @keyframes marquee-scroll {
      from { transform: translateX(0); }
      to { transform: translateX(-50%); }
    }

    .marquee-shell[data-animated="true"] {
      overflow: hidden;
      mask-image: linear-gradient(90deg, transparent, #000 7%, #000 93%, transparent);
    }

    .marquee-shell[data-animated="true"] .marquee-track {
      animation: marquee-scroll 38s linear infinite;
    }

    .marquee-shell[data-animated="true"]:is(:hover, :focus-within) .marquee-track {
      animation-play-state: paused;
    }
  }

  /* ------------------------------------------------------------------------
     6. Scroll-Driven Hero Carousel Slide Effects (view(inline))
     ------------------------------------------------------------------------ */
  @keyframes hero-slide-focus {
    0% {
      scale: 0.94;
      opacity: 0.72;
    }
    50% {
      scale: 1;
      opacity: 1;
    }
    100% {
      scale: 0.94;
      opacity: 0.72;
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    @supports ((animation-timeline: view()) and (animation-range: entry)) {
      .hero-slider__track > .hero-slide > .hero-slide__inner {
        animation: hero-slide-focus auto linear both;
        animation-timeline: view(inline);
      }
    }
  }
}
