/* Motion: press feedback, entrances, tooltips, toasts, dialogs and loading
   states. Loaded after components.css.

   Principles (Apple's fluid interface guidelines, translated for the web):
   - Response first: controls react on pointer-down, instantly; nothing waits
     for the release.
   - Springs, not durations: transforms use the spring curves from tokens.css
     (damping 1 by default; the slightly bouncy one only after a gesture).
   - Spatial consistency: things leave the way they came and grow out of the
     element that opened them (transform-origin at the trigger).
   - Materials materialize: glass surfaces arrive with scale and blur together,
     not as a plain fade.
   - Only interactive things react to the pointer; no decorative loops.
   Gesture-driven motion (swiping toasts, dragging borders) runs on spring.js;
   closing animations that must be interruptible run on motion.js transition().
   Movement is only declared when motion is welcome; with reduced motion the
   spring durations are 0 and short cross-fades remain. */

/* ---------- Keyframes ---------- */

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

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

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

/* A glass surface arriving: grows out of its origin while the blur resolves. */
@keyframes materialize {
  from {
    opacity: 0;
    transform: scale(0.94);
    filter: blur(6px);
  }
}

@keyframes drop-in {
  from {
    opacity: 0;
    transform: translateY(-4px) scale(0.98);
  }
}

@keyframes check {
  from {
    transform: scale(0.82);
  }
}

/* A rejected entry: the same "no" as a refused password on the lock screen. */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-6px); }
  35% { transform: translateX(5px); }
  55% { transform: translateX(-3px); }
  75% { transform: translateX(2px); }
}

@keyframes pulse-ring {
  0% { box-shadow: 0 0 0 0 var(--pulse-color, var(--color-pulse-danger)); }
  70% { box-shadow: 0 0 0 7px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

@keyframes countdown {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}

@keyframes page-progress {
  0% { transform: scaleX(0); }
  20% { transform: scaleX(0.35); }
  60% { transform: scaleX(0.7); }
  100% { transform: scaleX(0.92); }
}

@keyframes toast-enter {
  from {
    opacity: 0;
    transform: translateX(calc(100% + var(--space-4)));
  }
}

/* ---------- Links and text ---------- */

html {
  scroll-behavior: smooth;
}

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

a {
  transition: color var(--duration-fast), text-decoration-color var(--duration-base);
}

a:hover {
  text-decoration-color: currentColor;
}

/* Quiet links (footer, menus) reveal their underline on hover. */
.footer__links a,
.auth-page__switch a {
  text-decoration-line: underline;
  text-decoration-color: transparent;
}

.footer__links a:hover,
.auth-page__switch a:hover {
  text-decoration-color: currentColor;
}

.field__label {
  transition: color var(--duration-fast);
}

.field:focus-within .field__label {
  color: var(--color-accent);
}

/* ---------- Press feedback ----------
   On pointer-down and instant (a short ease-out in), released with the spring,
   so a press that is cancelled by dragging away springs back. */

/* :where() keeps specificity at zero so absolutely positioned buttons
   (password toggle) keep their own position. */
:where(.btn) {
  position: relative;
}

.btn {
  transition: background var(--duration-fast), border-color var(--duration-fast),
    color var(--duration-fast), box-shadow var(--duration-base),
    transform var(--duration-spring) var(--ease-out);
}

.btn:active {
  transform: scale(var(--press-scale));
  transition-duration: var(--duration-fast), var(--duration-fast), var(--duration-fast),
    var(--duration-fast), var(--duration-press);
  transition-timing-function: ease-out;
}

.btn--icon:active {
  transform: scale(0.9);
}

.password-field__toggle:active {
  transform: translateY(-50%) scale(0.9);
}

.btn .icon {
  transition: transform var(--duration-spring) var(--ease-out);
}

@media (hover: hover) {
  .btn--primary:hover {
    box-shadow: var(--shadow-accent);
  }

  .btn--danger:hover {
    box-shadow: var(--shadow-danger);
  }

  /* Trailing icons (arrows) hint in the direction the button goes. */
  .btn:hover > .icon:last-child:not(:first-child) {
    transform: translateX(2px);
  }
}

/* Other pressable surfaces get the same instant press. */
:is(.navbar__link, .user-menu__item, .user-menu__trigger, .language-switch__option,
  .pagination__link, .radio-option, .navbar__status) {
  transition: background var(--duration-fast), color var(--duration-fast),
    border-color var(--duration-fast), transform var(--duration-spring) var(--ease-out);
}

:is(.navbar__link, .user-menu__item, .user-menu__trigger, .language-switch__option,
  .pagination__link, .navbar__status):active {
  transform: scale(var(--press-scale));
  transition-duration: var(--duration-press);
  transition-timing-function: ease-out;
}

.radio-option:active {
  transform: scale(0.99);
  transition-duration: var(--duration-press);
}

/* Busy state while a form submits: the label stays (keeps the width) but
   fades, and a spinner takes its place. */
.btn[data-busy] {
  pointer-events: none;
}

.btn[data-busy] > * {
  opacity: 0;
}

.btn[data-busy]::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 1rem;
  height: 1rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

/* ---------- Tooltips (icon-only buttons and [data-tooltip]) ----------
   They grow out of the element they describe. */

@media (hover: hover) {
  [data-tooltip] {
    position: relative;
  }

  .password-field__toggle[data-tooltip] {
    position: absolute;
  }

  [data-tooltip]::before {
    content: attr(data-tooltip);
    position: absolute;
    top: calc(100% + 6px);
    left: 50%;
    z-index: var(--z-dropdown);
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    background: var(--color-text);
    color: var(--color-text-inverse);
    box-shadow: var(--shadow-popover);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    letter-spacing: var(--tracking-xs);
    line-height: var(--leading-snug);
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transform: translateX(-50%) scale(0.92);
    transform-origin: top center;
    transition: opacity var(--duration-fast), transform var(--duration-spring) var(--ease-out);
  }

  [data-tooltip]:hover::before,
  [data-tooltip]:focus-visible::before {
    opacity: 1;
    transform: translateX(-50%);
    transition-delay: 400ms;
  }

  /* Pressing dismisses the tooltip at once. */
  [data-tooltip]:active::before,
  [data-tooltip][data-busy]::before {
    opacity: 0;
    transition-delay: 0ms;
  }

  /* The source chips sit at the bottom of the viewport: tooltip above. */
  .source-toggles [data-tooltip]::before {
    top: auto;
    bottom: calc(100% + 6px);
    transform-origin: bottom center;
  }

  /* Buttons at the right edge open their tooltip to the left. */
  .navbar__end [data-tooltip]::before {
    left: auto;
    right: 0;
    transform: scale(0.92);
    transform-origin: top right;
  }

  .navbar__end [data-tooltip]:hover::before,
  .navbar__end [data-tooltip]:focus-visible::before {
    transform: none;
  }
}

/* ---------- Navbar and user menu ---------- */

.user-menu__trigger .icon--chevron {
  transition: transform var(--duration-spring) var(--ease-out);
}

.user-menu[open]:not([data-closing]) .user-menu__trigger .icon--chevron {
  transform: rotate(180deg);
}

.avatar {
  transition: box-shadow var(--duration-base);
}

.user-menu__trigger:hover .avatar,
.user-menu[open] .avatar {
  box-shadow: 0 0 0 2px var(--color-bg), 0 0 0 4px var(--color-accent-border);
}

.user-menu__item .icon {
  transition: color var(--duration-fast);
}

.user-menu__item:hover .icon {
  color: var(--color-accent);
}

@media (prefers-reduced-motion: no-preference) {
  /* The menu grows out of the avatar that opened it (navbar.js plays the way back). */
  .user-menu[open] .user-menu__panel {
    transform-origin: top right;
    animation: materialize var(--duration-spring) var(--ease-out);
  }

  /* The phone menu unfolds from the bar, under the button that opened it. */
  .navbar[data-open] .navbar__nav {
    transform-origin: top right;
    animation: drop-in var(--duration-spring) var(--ease-out);
  }
}

/* ---------- Form fields ---------- */

.select {
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}

@media (prefers-reduced-motion: no-preference) {
  .checkbox__input:checked,
  .radio-option__input:checked {
    animation: check var(--duration-spring) var(--ease-out);
  }
}

/* ---------- Cards, plans and lists ----------
   Only surfaces that act on a click respond to hover. */

.plan-card {
  transition: box-shadow var(--duration-base), border-color var(--duration-base);
}

@media (hover: hover) {
  .plan-card:hover {
    box-shadow: var(--shadow-raised);
    border-color: var(--color-accent-border);
  }

  .plan-card--current:hover {
    border-color: var(--color-accent);
    box-shadow: 0 0 0 1px var(--color-accent), var(--shadow-raised);
  }
}

.result-item__title a,
.citation,
.pagination__link {
  transition: color var(--duration-fast), background var(--duration-fast),
    border-color var(--duration-fast), transform var(--duration-spring) var(--ease-out);
}

@media (hover: hover) {
  a.citation:hover {
    transform: translateX(2px);
  }
}

a.citation:active {
  transform: scale(0.99);
  transition-duration: var(--duration-press);
}

.badge {
  transition: background var(--duration-fast), border-color var(--duration-fast);
}

/* Details-based panels: content unfolds from the summary that opened it. */
@media (prefers-reduced-motion: no-preference) {
  .filter-panel[open] .filter-panel__body,
  .research-steps[open] .research-steps__list {
    transform-origin: top;
    animation: drop-in var(--duration-spring) var(--ease-out);
  }
}

/* ---------- Page progress (top bar during navigation, motion.js) ---------- */

.page-progress {
  position: fixed;
  inset: 0 0 auto;
  z-index: var(--z-toast);
  height: 2px;
  background: var(--color-accent);
  box-shadow: 0 0 8px var(--color-glow);
  transform: scaleX(0);
  transform-origin: left;
  opacity: 0;
  pointer-events: none;
}

.page-progress[data-active] {
  opacity: 1;
  /* Short delay: fast page loads never show the bar. */
  transition: opacity var(--duration-base) 150ms;
  animation: page-progress 6s cubic-bezier(0.1, 0.6, 0.3, 1) 150ms forwards;
}

/* ---------- Toasts ----------
   They slide in from the right edge and leave the same way; a swipe to the
   right throws one out (toast.js, spring.js). */

.toast {
  position: relative;
  overflow: hidden;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}

@media (prefers-reduced-motion: no-preference) {
  .toast {
    animation: toast-enter var(--duration-spring-slow) var(--ease-out);
  }
}

@media (prefers-reduced-motion: reduce) {
  .toast {
    animation: fade-in var(--duration-base) ease-out;
  }
}

/* Taken over by toast.js while dragged or animated. */
.toast[data-held] {
  animation: none;
}

/* Auto-hide countdown along the bottom edge; pauses on hover, focus and touch. */
.toast[data-timed]::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: 2px;
  background: var(--toast-accent, var(--color-info));
  opacity: 0.35;
  transform-origin: left;
  animation: countdown var(--toast-duration, 5s) linear forwards;
}

.toast[data-paused]::after {
  animation-play-state: paused;
}

/* ---------- Dialogs ----------
   A modal task materializes in place and dims what is behind it; closing
   (modal.js) retraces the same path from wherever the opening had got to. */

@media (prefers-reduced-motion: no-preference) {
  .modal[open] {
    animation: materialize var(--duration-spring) var(--ease-out);
  }
}

@media (prefers-reduced-motion: reduce) {
  .modal[open] {
    animation: fade-in var(--duration-base) ease-out;
  }
}

.modal[open]::backdrop {
  animation: fade-in var(--duration-base) ease-out;
}

/* Onboarding tour: animateClose() fades it out. */
.tour[open] {
  animation: fade-in var(--duration-base) ease-out;
}

@media (prefers-reduced-motion: no-preference) {
  .tour__card {
    animation: materialize var(--duration-spring) var(--ease-out);
  }

  .tour__spotlight,
  .tour__card {
    transition: top var(--duration-spring) var(--ease-out),
      left var(--duration-spring) var(--ease-out), width var(--duration-spring) var(--ease-out),
      height var(--duration-spring) var(--ease-out);
  }
}

/* ---------- Entrances (only when motion is welcome) ----------
   Small distances and one spring; the content settles rather than flies. */

@media (prefers-reduced-motion: no-preference) {
  .page-header,
  .auth-card,
  .auth-page__switch,
  .hero .container > *,
  .error-page > * {
    animation: rise var(--duration-spring-slow) var(--ease-out) backwards;
  }

  .auth-page__switch { animation-delay: 90ms; }
  .hero .container > :nth-child(2), .error-page > :nth-child(2) { animation-delay: calc(1 * var(--stagger)); }
  .hero .container > :nth-child(3), .error-page > :nth-child(3) { animation-delay: calc(2 * var(--stagger)); }
  .hero .container > :nth-child(4), .error-page > :nth-child(4) { animation-delay: calc(3 * var(--stagger)); }

  /* Staggered groups: each child settles a moment after the previous one. */
  .page > .stack > *,
  .plan-grid > *,
  .landing-features .grid-auto > *,
  .meter-grid > *,
  .member-list > * {
    animation: rise var(--duration-spring-slow) var(--ease-out) backwards;
    animation-delay: calc(60ms + var(--i, 0) * var(--stagger));
  }

  :is(.page > .stack, .plan-grid, .landing-features .grid-auto, .meter-grid, .member-list) > :nth-child(2) { --i: 1; }
  :is(.page > .stack, .plan-grid, .landing-features .grid-auto, .meter-grid, .member-list) > :nth-child(3) { --i: 2; }
  :is(.page > .stack, .plan-grid, .landing-features .grid-auto, .meter-grid, .member-list) > :nth-child(4) { --i: 3; }
  :is(.page > .stack, .plan-grid, .landing-features .grid-auto, .meter-grid, .member-list) > :nth-child(5) { --i: 4; }
  :is(.page > .stack, .plan-grid, .landing-features .grid-auto, .meter-grid, .member-list) > :nth-child(n + 6) { --i: 5; }

  .alert {
    animation: drop-in var(--duration-spring) var(--ease-out) backwards;
  }

  .field__error {
    animation: drop-in var(--duration-spring) var(--ease-out) backwards;
  }

  /* A field that came back with a server-side error shakes once. */
  .field--invalid :is(.input, .select, .textarea) {
    animation: shake 420ms ease-out 150ms;
  }

  /* Elements below the fold, revealed by motion.js when scrolled into view. */
  [data-reveal] {
    opacity: 0;
    transform: translateY(12px);
    transition: opacity var(--duration-spring-slow) var(--ease-out),
      transform var(--duration-spring-slow) var(--ease-out);
    transition-delay: calc(var(--i, 0) * var(--stagger));
  }

  [data-reveal="shown"] {
    opacity: 1;
    transform: none;
  }
}

/* ---------- Auto-hiding navbar and docking search bar ---------- */

@media (prefers-reduced-motion: no-preference) {
  .navbar {
    transition: transform var(--duration-spring-slow) var(--ease-out),
      box-shadow var(--duration-base), background var(--duration-base);
  }

  /* The docked bar and sticky sidebars follow the navbar up and down. */
  .search-dock[data-docked] > .search-bar,
  .decision-layout__sticky,
  .preview-page > .source-panel {
    transition: top var(--duration-spring-slow) var(--ease-out);
  }
}

/* Thinking indicator of the assistant (pulses while the model thinks). */
@keyframes thinking-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.7); }
}

@media (prefers-reduced-motion: no-preference) {
  .thinking--live:not([data-done]) .thinking__pulse,
  .live-plan:not([data-done]) .thinking__pulse {
    animation: thinking-pulse 1.2s ease-in-out infinite;
  }
}

/* Loading placeholders: a highlight sweeps across (shows that something loads). */
@keyframes shimmer {
  from { background-position: 150% 0; }
  to { background-position: -50% 0; }
}

@media (prefers-reduced-motion: no-preference) {
  .skeleton {
    background-image: linear-gradient(
      90deg,
      transparent 0%,
      var(--color-bg-subtle) 45%,
      var(--color-surface) 50%,
      var(--color-bg-subtle) 55%,
      transparent 100%
    );
    background-size: 200% 100%;
    background-repeat: no-repeat;
    animation: shimmer 1.4s ease-in-out infinite;
  }
}

/* Planned research: tasks still being researched pulse softly. */
@media (prefers-reduced-motion: no-preference) {
  .live-plan__task[data-state="running"] .live-plan__name::before {
    --pulse-color: var(--color-pulse);
    animation: pulse-ring 1.6s ease-out infinite;
  }
}

/* ---------- Product preview (landing page, product-demo.js) ---------- */

/* Elements a scene reveals, the words of the streamed answer, the caret while
   typing and the ring of a click. */
@keyframes demo-caret {
  50% {
    opacity: 0;
  }
}

@keyframes demo-click {
  from {
    opacity: 0.6;
    transform: scale(0.3);
  }
  to {
    opacity: 0;
    transform: scale(1);
  }
}

@media (prefers-reduced-motion: no-preference) {
  .product-demo [data-demo-enter] {
    animation: rise var(--duration-spring-slow) var(--ease-out) both;
  }

  .product-demo [data-demo-enter] .result-item {
    animation: rise var(--duration-spring-slow) var(--ease-out) both;
  }

  .product-demo [data-demo-enter] .result-item:nth-child(2) {
    animation-delay: calc(var(--stagger) * 2);
  }

  .product-demo [data-demo-enter] .result-item:nth-child(3) {
    animation-delay: calc(var(--stagger) * 4);
  }

  .product-demo .demo-word {
    animation: fade-in var(--duration-base) ease-out both;
  }

  .demo-input[data-demo-typing]::after {
    content: "";
    display: inline-block;
    width: 1px;
    height: 1.2em;
    margin-left: 1px;
    vertical-align: text-bottom;
    background: currentColor;
    animation: demo-caret 1s steps(1) infinite;
  }

  .product-demo__cursor::after {
    content: "";
    position: absolute;
    top: -0.75rem;
    left: -0.75rem;
    width: 1.5rem;
    height: 1.5rem;
    border-radius: var(--radius-full);
    background: var(--color-pulse);
    opacity: 0;
  }

  .product-demo__cursor[data-demo-click]::after {
    animation: demo-click var(--duration-spring) var(--ease-out);
  }

  .product-demo [data-demo-pressed] {
    transition: transform var(--duration-press) ease-out;
  }
}

/* Cookie consent: the card settles in when a page loads without a choice. */
@media (prefers-reduced-motion: no-preference) {
  .cookie-consent--blocking {
    animation: fade-in var(--duration-base) ease-out;
  }

  .cookie-consent__card {
    animation: materialize var(--duration-spring-slow) var(--ease-out);
  }
}
