/**
 * Animations - Sprint 7
 * Mercana E-commerce Platform
 *
 * Animaciones globales, micro-interacciones y efectos de transición
 * para mejorar la experiencia de usuario.
 */

/* =========================================
   SMOOTH SCROLL
   ========================================= */

html {
    scroll-behavior: smooth;
}

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

/* =========================================
   SCROLL TO TOP BUTTON
   ========================================= */

.scroll-to-top {
    position: fixed;
    /* leave space for floating cart + chat widgets */
    bottom: calc(2rem + env(safe-area-inset-bottom, 0px));
    right: calc(2rem + 4.75rem);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--color-primary);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px);
    transition: all var(--transition-normal);
    z-index: 950;
}

.scroll-to-top:hover {
    background: var(--color-primary-dark);
    transform: translateY(-2px) scale(1.05);
    box-shadow: var(--shadow-xl);
}

.scroll-to-top:active {
    transform: translateY(0) scale(0.95);
}

.scroll-to-top.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.scroll-to-top i {
    transition: transform var(--transition-fast);
}

.scroll-to-top:hover i {
    transform: translateY(-2px);
}

/* Mobile positioning */
@media (max-width: 768px) {
    .scroll-to-top {
        bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
        right: calc(1rem + 3.75rem);
        width: 44px;
        height: 44px;
    }
}

/* =========================================
   HOVER ANIMATIONS - CARDS
   ========================================= */

.hover-lift {
    transition: transform var(--transition-normal), box-shadow var(--transition-normal);
}

.hover-lift:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
}

.hover-lift-sm:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.hover-lift-lg:hover {
    transform: translateY(-8px);
    box-shadow: var(--shadow-xl);
}

.hover-scale {
    transition: transform var(--transition-normal);
}

.hover-scale:hover {
    transform: scale(1.02);
}

.hover-scale-sm:hover {
    transform: scale(1.01);
}

.hover-scale-lg:hover {
    transform: scale(1.05);
}

/* =========================================
   HOVER ANIMATIONS - BUTTONS
   ========================================= */

.hover-pulse:hover {
    animation: pulse-animation 0.4s ease-out;
}

@keyframes pulse-animation {
    0% {
        box-shadow: 0 0 0 0 rgba(102, 126, 234, 0.4);
    }
    100% {
        box-shadow: 0 0 0 12px rgba(102, 126, 234, 0);
    }
}

.hover-glow:hover {
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.3);
}

/* =========================================
   HOVER ANIMATIONS - LINKS
   ========================================= */

.link-underline-animation {
    position: relative;
    text-decoration: none;
}

.link-underline-animation::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 0;
    height: 2px;
    background: var(--color-primary);
    transition: width var(--transition-normal);
}

.link-underline-animation:hover::after {
    width: 100%;
}

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

.link-color-transition:hover {
    color: var(--color-primary);
}

/* =========================================
   CART ANIMATIONS
   ========================================= */

/* Badge shake when item added */
.cart-badge-shake {
    animation: badge-shake 0.5s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes badge-shake {
    10%, 90% {
        transform: translateX(-1px) scale(1.1);
    }
    20%, 80% {
        transform: translateX(2px) scale(1.2);
    }
    30%, 50%, 70% {
        transform: translateX(-3px) scale(1.2);
    }
    40%, 60% {
        transform: translateX(3px) scale(1.2);
    }
    100% {
        transform: translateX(0) scale(1);
    }
}

/* Badge bounce */
.cart-badge-bounce {
    animation: badge-bounce 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes badge-bounce {
    0% {
        transform: scale(1);
    }
    30% {
        transform: scale(1.4);
    }
    50% {
        transform: scale(0.9);
    }
    70% {
        transform: scale(1.15);
    }
    100% {
        transform: scale(1);
    }
}

/* Cart badge pulse glow */
.cart-badge-glow {
    animation: badge-glow 0.8s ease-out;
}

@keyframes badge-glow {
    0% {
        box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7);
    }
    50% {
        box-shadow: 0 0 0 8px rgba(239, 68, 68, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(239, 68, 68, 0);
    }
}

/* =========================================
   ADD TO CART BUTTON FEEDBACK
   ========================================= */

.btn-cart-success {
    animation: cart-success 0.4s ease-out;
}

@keyframes cart-success {
    0% {
        transform: scale(1);
    }
    25% {
        transform: scale(0.95);
    }
    50% {
        transform: scale(1.05);
        background: var(--color-success);
    }
    100% {
        transform: scale(1);
    }
}

/* Flying item to cart animation */
.flying-item {
    position: fixed;
    z-index: 9999;
    pointer-events: none;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    animation: fly-to-cart 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards;
}

@keyframes fly-to-cart {
    0% {
        opacity: 1;
        transform: scale(1) rotate(0deg);
    }
    50% {
        opacity: 1;
        transform: scale(0.5) rotate(-15deg);
    }
    100% {
        opacity: 0;
        transform: scale(0.1) rotate(-30deg);
    }
}

/* =========================================
   TOAST ANIMATIONS
   ========================================= */

.toast-enter {
    animation: toast-slide-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes toast-slide-in {
    from {
        opacity: 0;
        transform: translateY(100%) scale(0.9);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.toast-exit {
    animation: toast-slide-out 0.3s ease-in forwards;
}

@keyframes toast-slide-out {
    from {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
    to {
        opacity: 0;
        transform: translateY(-20px) scale(0.9);
    }
}

/* Toast progress bar */
.toast-progress {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 3px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 0 0 0 var(--radius-md);
    animation: toast-progress-bar 4s linear forwards;
}

@keyframes toast-progress-bar {
    from {
        width: 100%;
    }
    to {
        width: 0%;
    }
}

/* =========================================
   FADE ANIMATIONS
   ========================================= */

.fade-in {
    animation: fade-in 0.3s ease-out forwards;
}

.fade-in-up {
    animation: fade-in-up 0.4s ease-out forwards;
}

.fade-in-down {
    animation: fade-in-down 0.4s ease-out forwards;
}

.fade-in-left {
    animation: fade-in-left 0.4s ease-out forwards;
}

.fade-in-right {
    animation: fade-in-right 0.4s ease-out forwards;
}

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

@keyframes fade-in-up {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fade-in-down {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fade-in-left {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes fade-in-right {
    from {
        opacity: 0;
        transform: translateX(20px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.fade-out {
    animation: fade-out 0.3s ease-in forwards;
}

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

/* =========================================
   SCALE ANIMATIONS
   ========================================= */

.scale-in {
    animation: scale-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes scale-in {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.pop-in {
    animation: pop-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes pop-in {
    0% {
        opacity: 0;
        transform: scale(0.5);
    }
    60% {
        opacity: 1;
        transform: scale(1.1);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

/* =========================================
   LOADING SPINNER
   ========================================= */

.spinner {
    width: 24px;
    height: 24px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: white;
    animation: spin 0.8s linear infinite;
}

.spinner-primary {
    border-color: rgba(102, 126, 234, 0.3);
    border-top-color: var(--color-primary);
}

.spinner-sm {
    width: 16px;
    height: 16px;
    border-width: 2px;
}

.spinner-lg {
    width: 40px;
    height: 40px;
    border-width: 4px;
}

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

/* Dots loading */
.loading-dots {
    display: inline-flex;
    gap: 4px;
}

.loading-dots span {
    width: 8px;
    height: 8px;
    background: currentColor;
    border-radius: 50%;
    animation: dots-bounce 1.4s infinite ease-in-out both;
}

.loading-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.loading-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes dots-bounce {
    0%, 80%, 100% {
        transform: scale(0);
    }
    40% {
        transform: scale(1);
    }
}

/* =========================================
   IMAGE REVEAL
   ========================================= */

.image-reveal {
    animation: image-reveal 0.6s ease-out forwards;
}

@keyframes image-reveal {
    from {
        opacity: 0;
        clip-path: inset(100% 0 0 0);
    }
    to {
        opacity: 1;
        clip-path: inset(0 0 0 0);
    }
}

/* =========================================
   RIPPLE EFFECT
   ========================================= */

.ripple-effect {
    position: relative;
    overflow: hidden;
}

.ripple-effect::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    transition: width 0.4s ease-out, height 0.4s ease-out, opacity 0.4s ease-out;
    opacity: 0;
}

.ripple-effect:active::after {
    width: 200px;
    height: 200px;
    opacity: 0;
}

/* =========================================
   WIGGLE (ATTENTION)
   ========================================= */

.wiggle {
    animation: wiggle 0.5s ease-in-out;
}

@keyframes wiggle {
    0%, 100% {
        transform: rotate(0deg);
    }
    25% {
        transform: rotate(-5deg);
    }
    50% {
        transform: rotate(5deg);
    }
    75% {
        transform: rotate(-5deg);
    }
}

/* =========================================
   FLOAT ANIMATION
   ========================================= */

.float {
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-10px);
    }
}

.float-slow {
    animation: float 4s ease-in-out infinite;
}

.float-fast {
    animation: float 2s ease-in-out infinite;
}

/* =========================================
   STAGGER ANIMATIONS (FOR LISTS)
   ========================================= */

.stagger-fade-in > * {
    opacity: 0;
    animation: fade-in-up 0.4s ease-out forwards;
}

.stagger-fade-in > *:nth-child(1) { animation-delay: 0.05s; }
.stagger-fade-in > *:nth-child(2) { animation-delay: 0.1s; }
.stagger-fade-in > *:nth-child(3) { animation-delay: 0.15s; }
.stagger-fade-in > *:nth-child(4) { animation-delay: 0.2s; }
.stagger-fade-in > *:nth-child(5) { animation-delay: 0.25s; }
.stagger-fade-in > *:nth-child(6) { animation-delay: 0.3s; }
.stagger-fade-in > *:nth-child(7) { animation-delay: 0.35s; }
.stagger-fade-in > *:nth-child(8) { animation-delay: 0.4s; }
.stagger-fade-in > *:nth-child(n+9) { animation-delay: 0.45s; }

/* =========================================
   ATTENTION SEEKERS
   ========================================= */

.flash {
    animation: flash 1s ease-in-out;
}

@keyframes flash {
    0%, 50%, 100% {
        opacity: 1;
    }
    25%, 75% {
        opacity: 0.3;
    }
}

.heartbeat {
    animation: heartbeat 1.5s ease-in-out infinite;
}

@keyframes heartbeat {
    0% {
        transform: scale(1);
    }
    14% {
        transform: scale(1.15);
    }
    28% {
        transform: scale(1);
    }
    42% {
        transform: scale(1.15);
    }
    70% {
        transform: scale(1);
    }
}

/* =========================================
   REDUCED MOTION
   ========================================= */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .scroll-to-top {
        transition: none;
    }

    .hover-lift,
    .hover-lift-sm,
    .hover-lift-lg,
    .hover-scale,
    .hover-scale-sm,
    .hover-scale-lg {
        transition: none;
    }

    .hover-lift:hover,
    .hover-lift-sm:hover,
    .hover-lift-lg:hover {
        transform: none;
    }

    .hover-scale:hover,
    .hover-scale-sm:hover,
    .hover-scale-lg:hover {
        transform: none;
    }
}
