/*
 * Foundation - Animations
 * Keyframes, loading states, and Turbo Frame styles
 *
 * Following Rails conventions:
 * - Performance-optimized animations
 * - Accessible motion preferences
 * - Turbo Frame integration
 */

/* Page transitions (View Transitions, which Turbo runs on every visit).
   Only the content area moves; the sidebar and tab bar stay put.

   Switching sections from the sidebar or tab bar cross-fades, the new page
   settling up into place. Following a link inside a page pushes the new
   page in from the right over the old one, which slides a third of the way
   left and dims, and going back reverses that exact path, as iOS and iPad
   navigation do. html[data-nav] is set per click in application.js. */

.app-content {
  view-transition-name: main-content;
  /* Prevent flash by creating stacking context */
  isolation: isolate;
  transform: translateZ(0);
}

.sidebar {
  view-transition-name: sidebar;
}

.bottom-nav {
  view-transition-name: bottom-nav;
}

::view-transition-old(sidebar),
::view-transition-old(bottom-nav) {
  animation: none;
  opacity: 0;
}

::view-transition-new(sidebar),
::view-transition-new(bottom-nav) {
  animation: none;
}

::view-transition-group(main-content) {
  background: var(--bg);
  animation-duration: var(--spring-dur);
  animation-timing-function: var(--spring);
}

::view-transition-old(main-content) {
  animation: page-fade-out 120ms ease-out both;
}

::view-transition-new(main-content) {
  animation: page-settle-in var(--spring-dur) var(--spring) both;
}

html[data-nav="push"][data-turbo-visit-direction="forward"]::view-transition-old(main-content) {
  animation: page-push-under var(--spring-dur) var(--spring) both;
}

html[data-nav="push"][data-turbo-visit-direction="forward"]::view-transition-new(main-content) {
  animation: page-push-over var(--spring-dur) var(--spring) both;
  box-shadow: -8px 0 24px oklch(0 0 0 / 0.08);
}

/* Back: the leaving page is the top one, sliding off to the right, and the
   page under it comes back from the left. Its own keyframes rather than the
   push played in reverse, since reversing would also reverse the spring
   (a slow start into a hard stop); both directions start fast and settle. */
html[data-nav="push"][data-turbo-visit-direction="back"]::view-transition-old(main-content) {
  z-index: 1;
  animation: page-pop-off var(--spring-dur) var(--spring) both;
  box-shadow: -8px 0 24px oklch(0 0 0 / 0.08);
}

html[data-nav="push"][data-turbo-visit-direction="back"]::view-transition-new(main-content) {
  animation: page-pop-from-under var(--spring-dur) var(--spring) both;
}

/* A refresh of the same page (a morph) doesn't move */
html[data-turbo-visit-direction="none"]::view-transition-old(main-content),
html[data-turbo-visit-direction="none"]::view-transition-new(main-content) {
  animation: none;
}

@keyframes page-fade-out {
  to { opacity: 0; }
}

@keyframes page-settle-in {
  from { opacity: 0; transform: translateY(8px); }
}

@keyframes page-push-over {
  from { transform: translateX(100%); }
}

@keyframes page-push-under {
  to { transform: translateX(-30%); filter: brightness(0.92); }
}

@keyframes page-pop-off {
  to { transform: translateX(100%); }
}

@keyframes page-pop-from-under {
  from { transform: translateX(-30%); filter: brightness(0.92); }
}

/* Reduced motion: every page change is a short cross-fade, nothing slides */
@media (prefers-reduced-motion: reduce) {
  ::view-transition-old(main-content),
  ::view-transition-new(main-content) {
    animation: page-fade-out 150ms ease-out both !important;
    box-shadow: none !important;
  }

  ::view-transition-new(main-content) {
    animation-direction: reverse !important;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

/* Turbo Frame Styles */
turbo-frame {
  display: block;
}
