/* ========== COMPANION REACTION ANIMATIONS ========== */

/* Main companion avatar container with reaction states */
.companion-avatar {
    position: relative;
    display: inline-block;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.companion-avatar img {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 50%;
    will-change: transform;
}

/* Reaction overlay effects */
.reaction-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 50%;
    pointer-events: none;
    opacity: 0;
    transition: all 0.3s ease;
}

/* ========== REACTION ANIMATIONS WITH TIME LIMITS ========== */

/* Typing/Thinking Animation - Limited to 2.5s max */
@keyframes thinking-pulse {
    0%, 100% { 
        transform: scale(1); 
        opacity: 0.8; 
    }
    50% { 
        transform: scale(1.05); 
        opacity: 0.4; 
    }
}

@keyframes thinking-dots {
    0%, 20% { opacity: 0; }
    50% { opacity: 1; }
    100% { opacity: 0; }
}

.companion-avatar.thinking {
    animation: thinking-pulse 2s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.thinking::after {
    content: "⋯";
    position: absolute;
    top: -10px;
    right: -5px;
    background: rgba(108, 117, 125, 0.9);
    color: white;
    font-size: 14px;
    padding: 2px 6px;
    border-radius: 10px;
    animation: thinking-dots 1.5s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

/* Excited/Happy Animation - Limited to 1.5s */
@keyframes excited-bounce {
    0%, 20%, 50%, 80%, 100% { 
        transform: translateY(0) scale(1); 
    }
    40% { 
        transform: translateY(-8px) scale(1.1); 
    }
    60% { 
        transform: translateY(-4px) scale(1.05); 
    }
}

.companion-avatar.excited {
    animation: excited-bounce 1.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.excited.reaction-strong {
    animation-duration: 1.8s;
}

.companion-avatar.excited.reaction-subtle {
    animation-duration: 1.2s;
    transform: scale(0.98);
}

/* Surprised Animation - Limited to 1s */
@keyframes surprised-shock {
    0% { 
        transform: scale(1) rotate(0deg); 
    }
    20% { 
        transform: scale(1.15) rotate(-2deg); 
    }
    40% { 
        transform: scale(1.1) rotate(2deg); 
    }
    60% { 
        transform: scale(1.05) rotate(-1deg); 
    }
    100% { 
        transform: scale(1) rotate(0deg); 
    }
}

.companion-avatar.surprised {
    animation: surprised-shock 1s ease-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.surprised.reaction-strong {
    animation-duration: 1.2s;
}

/* Curious Animation - Limited to 1.8s */
@keyframes curious-tilt {
    0%, 100% { 
        transform: rotate(0deg) scale(1); 
    }
    25% { 
        transform: rotate(-3deg) scale(1.02); 
    }
    75% { 
        transform: rotate(3deg) scale(1.02); 
    }
}

.companion-avatar.curious {
    animation: curious-tilt 1.8s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.curious::after {
    content: "?";
    position: absolute;
    top: -12px;
    right: -8px;
    background: rgba(13, 202, 240, 0.9);
    color: white;
    font-size: 12px;
    font-weight: bold;
    padding: 2px 6px;
    border-radius: 8px;
    animation: fade-in-out 1.8s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

/* Laughing Animation - Limited to 1.2s */
@keyframes laughing-shake {
    0%, 10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90%, 100% {
        transform: translateX(0);
    }
    5%, 15%, 25%, 35%, 45%, 55%, 65%, 75%, 85%, 95% {
        transform: translateX(-2px) scale(1.05);
    }
}

.companion-avatar.laughing {
    animation: laughing-shake 1.2s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.laughing.reaction-strong {
    animation-duration: 1.5s;
}

/* Blushing Animation - Limited to 2s */
@keyframes blushing-glow {
    0%, 100% { 
        filter: hue-rotate(0deg) brightness(1); 
        transform: scale(1);
    }
    50% { 
        filter: hue-rotate(10deg) brightness(1.1); 
        transform: scale(1.02);
    }
}

.companion-avatar.blushing {
    animation: blushing-glow 2s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.blushing::after {
    content: "💭";
    position: absolute;
    top: -10px;
    left: -10px;
    font-size: 16px;
    animation: fade-in-out 2s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

/* Contemplative Animation - Limited to 2.5s */
@keyframes contemplative-drift {
    0%, 100% { 
        transform: translateY(0) rotate(0deg); 
        opacity: 0.9;
    }
    33% { 
        transform: translateY(-2px) rotate(-1deg); 
        opacity: 0.7;
    }
    66% { 
        transform: translateY(2px) rotate(1deg); 
        opacity: 0.8;
    }
}

.companion-avatar.contemplative {
    animation: contemplative-drift 2.5s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.contemplative::after {
    content: "🤔";
    position: absolute;
    top: -8px;
    right: -8px;
    font-size: 14px;
    animation: fade-in-out 2.5s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

/* Love Animation - Limited to 2.2s */
@keyframes love-pulse {
    0%, 100% { 
        transform: scale(1); 
        filter: hue-rotate(0deg);
    }
    50% { 
        transform: scale(1.08); 
        filter: hue-rotate(20deg) brightness(1.1);
    }
}

.companion-avatar.love {
    animation: love-pulse 2.2s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

.companion-avatar.love::after {
    content: "💖";
    position: absolute;
    top: -12px;
    right: -12px;
    font-size: 18px;
    animation: heart-float 2.2s ease-in-out;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

/* ========== SHARED UTILITY ANIMATIONS ========== */

@keyframes fade-in-out {
    0%, 100% { 
        opacity: 0; 
        transform: scale(0.8);
    }
    20%, 80% { 
        opacity: 1; 
        transform: scale(1);
    }
}

@keyframes heart-float {
    0%, 100% { 
        opacity: 0; 
        transform: translateY(0) scale(0.8);
    }
    20%, 80% { 
        opacity: 1; 
        transform: translateY(-5px) scale(1);
    }
}

/* ========== INTENSITY MODIFIERS ========== */

.companion-avatar.reaction-subtle {
    opacity: 0.8;
}

.companion-avatar.reaction-moderate {
    opacity: 1;
}

.companion-avatar.reaction-strong {
    opacity: 1;
    transform: scale(1.02);
}

/* ========== ANIMATION CLEANUP AND SAFEGUARDS ========== */

/* Ensure all animations stop after their duration */
.companion-avatar.thinking,
.companion-avatar.excited,
.companion-avatar.surprised,
.companion-avatar.curious,
.companion-avatar.laughing,
.companion-avatar.blushing,
.companion-avatar.contemplative,
.companion-avatar.love {
    animation-fill-mode: forwards;
    animation-iteration-count: 1;
}

/* Clear all pseudo-elements after animation */
.companion-avatar:not(.thinking):not(.curious):not(.blushing):not(.contemplative):not(.love)::after {
    display: none !important;
}

/* Performance optimization - prevent layout thrashing */
.companion-avatar {
    contain: layout style;
    will-change: transform, opacity, filter;
}

/* Prevent multiple overlapping animations */
.companion-avatar.animating {
    pointer-events: none;
}

/* Mobile optimization */
@media (max-width: 768px) {
    .companion-avatar {
        will-change: auto; /* Reduce GPU usage on mobile */
    }
    
    .companion-avatar::after {
        font-size: 12px; /* Smaller overlay elements */
    }
}

/* Reduced motion accessibility */
@media (prefers-reduced-motion: reduce) {
    .companion-avatar,
    .companion-avatar::after {
        animation-duration: 0.3s !important;
        animation-iteration-count: 1 !important;
    }
    
    .companion-avatar.thinking {
        animation: none !important;
        opacity: 0.8;
    }
}

/* ========== DEBUG HELPERS ========== */

/* Debug mode - shows animation boundaries */
.companion-avatar.debug {
    border: 2px dashed #ffc107;
    background: rgba(255, 193, 7, 0.1);
}

.companion-avatar.debug::before {
    content: attr(class);
    position: absolute;
    bottom: -20px;
    left: 0;
    font-size: 10px;
    color: #ffc107;
    white-space: nowrap;
    pointer-events: none;
}