/* ==========================================================================
 * XUI Motion & Micro-Animation System
 * Restrained studio choreography: crisp, purposeful, never sluggish.
 * Controlled via <html data-motion="full|subtle|reduced"> and OS preference.
 * ========================================================================== */

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

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

@keyframes xui-pop-in {
  from {
    opacity: 0;
    transform: translateY(calc(-4px * var(--motion-scale))) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes xui-pop-out {
  from {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  to {
    opacity: 0;
    transform: translateY(calc(-4px * var(--motion-scale))) scale(0.97);
  }
}

@keyframes xui-dialog-in {
  from {
    opacity: 0;
    transform: translateY(calc(8px * var(--motion-scale))) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes xui-dialog-out {
  from {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  to {
    opacity: 0;
    transform: translateY(calc(8px * var(--motion-scale))) scale(0.98);
  }
}

@keyframes xui-toast-in {
  from {
    opacity: 0;
    transform: translateX(calc(16px * var(--motion-scale)));
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes xui-toast-out {
  from {
    opacity: 1;
    transform: translateX(0);
  }
  to {
    opacity: 0;
    transform: translateX(calc(16px * var(--motion-scale)));
  }
}

@keyframes xui-slide-up {
  from {
    opacity: 0;
    transform: translateY(calc(10px * var(--motion-scale)));
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes xui-spin {
  to { transform: rotate(360deg); }
}

/* Shimmer sweeps a composited pseudo-element (transform only) —
 * never animates background-position, which repaints the whole surface every frame. */
@keyframes xui-skeleton-sweep {
  from { transform: translateX(-100%); }
  to { transform: translateX(100%); }
}

@keyframes xui-pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.86); }
}

@keyframes xui-progress-indeterminate {
  0% { transform: translateX(-100%) scaleX(0.35); }
  50% { transform: translateX(25%) scaleX(0.6); }
  100% { transform: translateX(220%) scaleX(0.35); }
}

/* Toast lifetime meter: drains a composited bar (transform only) so the auto-dismiss
 * countdown stays visible and can be paused on hover without repainting. */
@keyframes xui-toast-countdown {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}

/* Reusable Animation Utility Classes
 * Enter uses --d-base; paired exit uses --d-exit and must stay <= enter (R-MOTION-01).
 */
.xui-anim-fade {
  animation: xui-fade-in var(--d-base) var(--ease-out) both;
}

.xui-anim-fade-out {
  animation: xui-fade-out var(--d-exit) var(--ease-out) both;
}

.xui-anim-pop {
  animation: xui-pop-in var(--d-fast) var(--ease-out) both;
}

.xui-anim-pop-out {
  animation: xui-pop-out var(--d-exit) var(--ease-out) both;
}

.xui-anim-slide-up {
  animation: xui-slide-up var(--d-base) var(--ease-out) both;
}

.xui-anim-spin {
  animation: xui-spin 760ms linear infinite;
}

/* Reduced Motion Contract: explicit data-motion="reduced" OR OS setting (unless user explicitly chose full) */
:root[data-motion="reduced"] *,
:root[data-motion="reduced"] *::before,
:root[data-motion="reduced"] *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
  scroll-behavior: auto !important;
}

@media (prefers-reduced-motion: reduce) {
  :root:not([data-motion="full"]) *,
  :root:not([data-motion="full"]) *::before,
  :root:not([data-motion="full"]) *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Essential Motion: 加载指示器必须继续转 ----------
 * 上面两条兜底把所有动画压成 duration 0.01ms / 跑 1 次，圆环会永远停在某一帧，
 * 看起来就是一个静止的「C」，用户读不出「进行中」——这比没有动效更糟。
 * WCAG 2.3.3 针对的是非必要动效；spinner / 不确定进度条属于状态反馈，保留但放慢。
 * 选择器权重 (0,3,0) 高于上面的 (0,2,0)，同层 !important 下生效。
 */
:root[data-motion="reduced"] .xui-spinner,
:root[data-motion="reduced"] .xui-progress.is-indeterminate .xui-progress-fill {
  animation-duration: 1600ms !important;
  animation-iteration-count: infinite !important;
}

@media (prefers-reduced-motion: reduce) {
  :root:not([data-motion="full"]) .xui-spinner,
  :root:not([data-motion="full"]) .xui-progress.is-indeterminate .xui-progress-fill {
    animation-duration: 1600ms !important;
    animation-iteration-count: infinite !important;
  }
}
