/*
 * Motion system.
 *
 * Rules that keep motion safe:
 *  - Every decorative animation is inside `prefers-reduced-motion: no-preference`,
 *    so visitors who ask for reduced motion get none of it.
 *  - Entrance animations use `animation-fill-mode: backwards`: the start frame
 *    applies during any delay, but nothing is held after they finish, so they
 *    never override hover transforms or leave a lingering `transform` that
 *    would affect sticky/fixed descendants.
 *  - Only opacity, transform and clip-path are animated (no layout properties),
 *    except the sidebar accordion, which animates grid rows by design.
 */
:root {
  --motion-fast: 150ms;
  --motion: 260ms;
  --motion-slow: 520ms;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --stagger: 20ms;
}

@keyframes page-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
@keyframes rise-in-sm {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
@keyframes fade-in-soft {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes bar-grow {
  from {
    transform: scaleX(0);
  }
  to {
    transform: scaleX(1);
  }
}
@keyframes reveal-x {
  from {
    clip-path: inset(0 100% 0 0);
  }
  to {
    clip-path: inset(0 0 0 0);
  }
}
@keyframes pop {
  0% {
    transform: scale(0.7);
  }
  60% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}
@keyframes slide-down {
  from {
    opacity: 0;
    transform: translateY(-100%);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
@keyframes underline-in {
  from {
    transform: scaleX(0);
  }
  to {
    transform: scaleX(1);
  }
}

/* Sidebar accordion: always rendered, animated with grid rows (0fr <-> 1fr).
   Collapsed content is also `inert` (set in Sidebar.jsx) so it cannot be focused. */
.nav-collapse {
  display: grid;
  grid-template-rows: 0fr;
}
.nav-collapse.open {
  grid-template-rows: 1fr;
}
.nav-collapse-inner {
  min-height: 0;
  overflow: hidden;
}
.nav-collapse:not(.open) .nav-children {
  opacity: 0;
}

@media (prefers-reduced-motion: no-preference) {
  /* --- Page entrances ---------------------------------------------------- */
  .page-enter,
  .narrow-page,
  .status-page,
  .home-body {
    animation: page-in var(--motion) var(--ease-out) backwards;
  }
  .home-hero > * {
    animation: rise-in-sm var(--motion) var(--ease-out) backwards;
  }
  .home-hero > :nth-child(2) {
    animation-delay: 30ms;
  }
  .home-hero > :nth-child(3) {
    animation-delay: 60ms;
  }
  .home-hero > :nth-child(4) {
    animation-delay: 90ms;
  }
  .home-hero > :nth-child(5) {
    animation-delay: 120ms;
  }
  .home-hero > :nth-child(6) {
    animation-delay: 150ms;
  }
  .announcement {
    animation: slide-down var(--motion) var(--ease-out) backwards;
  }

  /* --- Staggered lists and grids ----------------------------------------- */
  .card-grid > *,
  .page-list > *,
  .stat-grid > *,
  .related-grid > *,
  .media-grid > *,
  .bar-list > * {
    animation: rise-in-sm 240ms var(--ease-out) backwards;
  }
  .card-grid > :nth-child(2),
  .page-list > :nth-child(2),
  .stat-grid > :nth-child(2),
  .related-grid > :nth-child(2),
  .media-grid > :nth-child(2),
  .bar-list > :nth-child(2) {
    animation-delay: calc(var(--stagger) * 1);
  }
  .card-grid > :nth-child(3),
  .page-list > :nth-child(3),
  .stat-grid > :nth-child(3),
  .related-grid > :nth-child(3),
  .media-grid > :nth-child(3),
  .bar-list > :nth-child(3) {
    animation-delay: calc(var(--stagger) * 2);
  }
  .card-grid > :nth-child(4),
  .page-list > :nth-child(4),
  .stat-grid > :nth-child(4),
  .related-grid > :nth-child(4),
  .media-grid > :nth-child(4),
  .bar-list > :nth-child(4) {
    animation-delay: calc(var(--stagger) * 3);
  }
  .card-grid > :nth-child(5),
  .page-list > :nth-child(5),
  .stat-grid > :nth-child(5),
  .related-grid > :nth-child(5),
  .media-grid > :nth-child(5),
  .bar-list > :nth-child(5) {
    animation-delay: calc(var(--stagger) * 4);
  }
  .card-grid > :nth-child(6),
  .page-list > :nth-child(6),
  .stat-grid > :nth-child(6),
  .related-grid > :nth-child(6),
  .media-grid > :nth-child(6),
  .bar-list > :nth-child(6) {
    animation-delay: calc(var(--stagger) * 5);
  }
  .card-grid > :nth-child(7),
  .page-list > :nth-child(7),
  .stat-grid > :nth-child(7),
  .related-grid > :nth-child(7),
  .media-grid > :nth-child(7),
  .bar-list > :nth-child(7) {
    animation-delay: calc(var(--stagger) * 6);
  }
  .card-grid > :nth-child(8),
  .page-list > :nth-child(8),
  .stat-grid > :nth-child(8),
  .related-grid > :nth-child(8),
  .media-grid > :nth-child(8),
  .bar-list > :nth-child(8) {
    animation-delay: calc(var(--stagger) * 7);
  }
  .card-grid > :nth-child(9),
  .page-list > :nth-child(9),
  .stat-grid > :nth-child(9),
  .related-grid > :nth-child(9),
  .media-grid > :nth-child(9),
  .bar-list > :nth-child(9) {
    animation-delay: calc(var(--stagger) * 8);
  }
  .card-grid > :nth-child(10),
  .page-list > :nth-child(10),
  .stat-grid > :nth-child(10),
  .related-grid > :nth-child(10),
  .media-grid > :nth-child(10),
  .bar-list > :nth-child(10) {
    animation-delay: calc(var(--stagger) * 9);
  }
  .card-grid > :nth-child(n + 11),
  .page-list > :nth-child(n + 11),
  .stat-grid > :nth-child(n + 11),
  .related-grid > :nth-child(n + 11),
  .media-grid > :nth-child(n + 11),
  .bar-list > :nth-child(n + 11) {
    animation-delay: calc(var(--stagger) * 10);
  }

  /* --- Data ---------------------------------------------------------------- */
  .bar-fill {
    transform-origin: left center;
    animation: bar-grow 700ms var(--ease-out) backwards;
    transition: width var(--motion-slow) var(--ease-out);
  }
  .bar-list > :nth-child(2) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 1);
  }
  .bar-list > :nth-child(3) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 2);
  }
  .bar-list > :nth-child(4) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 3);
  }
  .bar-list > :nth-child(5) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 4);
  }
  .bar-list > :nth-child(6) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 5);
  }
  .bar-list > :nth-child(7) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 6);
  }
  .bar-list > :nth-child(8) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 7);
  }
  .bar-list > :nth-child(9) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 8);
  }
  .bar-list > :nth-child(10) .bar-fill {
    animation-delay: calc(120ms + var(--stagger) * 9);
  }
  .chart-reveal {
    animation: reveal-x 900ms var(--ease-out) backwards;
  }
  .chart-end {
    animation: fade-in-soft var(--motion) var(--ease-out) 750ms backwards;
  }

  /* --- Navigation ---------------------------------------------------------- */
  .nav-collapse {
    transition: grid-template-rows var(--motion) var(--ease-out);
  }
  .nav-collapse .nav-children {
    transition: opacity var(--motion) var(--ease-out);
  }
  .version-select svg {
    transition: transform var(--motion) var(--ease-out);
  }
  .version-select[aria-expanded='true'] svg {
    transform: rotate(180deg);
  }
  .tab[aria-selected='true']::after {
    animation: underline-in var(--motion) var(--ease-out) backwards;
  }
  .segmented button {
    transition: background-color var(--motion-fast), color var(--motion-fast), box-shadow var(--motion-fast);
  }

  /* --- Hover affordances (arrows nudge toward their direction) ------------- */
  .category-card .row svg,
  .pager small svg,
  .hero-search svg {
    transition: transform var(--motion-fast) var(--ease-out);
  }
  .category-card:hover .row svg,
  .pager .next:hover small svg {
    transform: translateX(3px);
  }
  .pager .prev:hover small svg {
    transform: translateX(-3px);
  }
  .hero-search:hover svg {
    transform: scale(1.08);
  }
  .related-card,
  .page-list a {
    transition: background-color var(--motion-fast), border-color var(--motion-fast), transform var(--motion-fast) var(--ease-out);
  }
  .related-card:hover {
    transform: translateY(-1px);
  }

  /* --- Discussions & feedback --------------------------------------------- */
  .comment {
    animation: rise-in-sm 320ms var(--ease-out) backwards;
  }
  .reply-composer,
  .feedback-form,
  .composer-preview,
  .mention-menu {
    animation: rise-in-sm var(--motion) var(--ease-out) backwards;
  }
  .reaction.mine,
  .notif-dot,
  .copy-button.copied svg {
    animation: pop 320ms var(--ease-out) backwards;
  }

  /* --- Dropdown lists (customizable selects) ------------------------------ */
  @supports (appearance: base-select) {
    .select::picker-icon {
      transition: rotate var(--motion) var(--ease-out);
    }
    .select:open::picker-icon {
      rotate: 180deg;
    }
    .select::picker(select) {
      transition:
        opacity var(--motion-fast) var(--ease-out),
        translate var(--motion-fast) var(--ease-out),
        display var(--motion-fast) allow-discrete,
        overlay var(--motion-fast) allow-discrete;
    }
    .select:not(:open)::picker(select) {
      opacity: 0;
      translate: 0 -4px;
    }
    @starting-style {
      .select:open::picker(select) {
        opacity: 0;
        translate: 0 -4px;
      }
    }
  }

  /* --- Theme crossfade (View Transitions API, triggered in useApp.jsx) ----- */
  ::view-transition-old(root),
  ::view-transition-new(root) {
    animation-duration: 320ms;
    animation-timing-function: var(--ease);
  }
}

/* Scroll-driven reveal for homepage sections, where supported. Browsers
   without scroll timelines simply show the sections. */
@supports (animation-timeline: view()) {
  @media (prefers-reduced-motion: no-preference) {
    .home-section {
      animation: rise-in-sm linear both;
      animation-timeline: view();
      animation-range: entry 0% entry 35%;
    }
  }
}
