/*
 * Avatar System CSS - Containerized Module
 * =========================================
 * 
 * Namespaced CSS for the avatar system to avoid conflicts with host service.
 * All styles are prefixed with .avatar- or #avatar- to ensure isolation.
 */

/* Avatar Container - Fixed positioning in bottom right corner */
#avatar-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 120px;
    height: 120px;
    border-radius: 50%;
    z-index: 9999;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    /* Ensure perfect circle aspect ratio */
    min-width: 120px;
    min-height: 120px;
    max-width: 120px;
    max-height: 120px;
}

/* Avatar Rive Canvas */
#avatar-rive-canvas {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    /* Ensure canvas maintains aspect ratio */
    object-fit: cover;
    display: block;
}

/* Avatar Fallback Image */
.avatar-fallback-image {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

/* Chat Icon - Small text chat button at bottom */
.avatar-chat-icon {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 18px;
    height: 18px;
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    font-size: 18px;
    z-index: 10001;
    transition: transform 0.2s ease;
    opacity: 0.8;
}

.avatar-chat-icon:hover {
    transform: scale(1.2);
    opacity: 1;
}

.avatar-chat-icon::before {
    content: "💬";
    font-size: 18px;
    display: block;
}

/* Chat Window */
.avatar-chat-window {
    position: fixed;
    bottom: 160px;
    right: 20px;
    width: 320px;
    height: 400px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.15);
    z-index: 10000;
    display: none;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.avatar-chat-window.visible {
    display: flex !important;
}

@keyframes slideInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Chat Header */
.avatar-chat-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    font-weight: 600;
}

.avatar-chat-close {
    background: none;
    border: none;
    color: white;
    font-size: 18px;
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.2s ease;
}

.avatar-chat-close:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Chat Messages */
.avatar-chat-messages {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #f8f9fa;
}

.avatar-chat-message {
    padding: 8px 12px;
    border-radius: 12px;
    max-width: 80%;
    word-wrap: break-word;
    font-size: 14px;
    line-height: 1.4;
}

.avatar-chat-message.user {
    background: #007bff;
    color: white;
    margin-left: auto;
    border-bottom-right-radius: 4px;
}

.avatar-chat-message.assistant {
    background: white;
    color: #333;
    border: 1px solid #e9ecef;
    border-bottom-left-radius: 4px;
}

.avatar-chat-message.welcome {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
    text-align: center;
    margin: 0 auto;
    font-style: italic;
}

/* Chat Input */
.avatar-chat-input-container {
    padding: 16px;
    background: white;
    border-top: 1px solid #e9ecef;
    display: flex;
    gap: 8px;
    align-items: center;
}

.avatar-chat-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid #e9ecef;
    border-radius: 20px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s ease;
}

.avatar-chat-input:focus {
    border-color: #007bff;
    box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}

.avatar-chat-send {
    background: #007bff;
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s ease;
}

.avatar-chat-send:hover {
    background: #0056b3;
}

.avatar-chat-send:disabled {
    background: #6c757d;
    cursor: not-allowed;
}

.avatar-chat-send::before {
    content: "➤";
    font-size: 14px;
}

/* Welcome Message */
.avatar-welcome-bubble {
    position: fixed;
    bottom: 160px;
    right: 20px;
    background: white;
    color: #333;
    padding: 12px 16px;
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    max-width: 280px;
    z-index: 10000;
    font-size: 14px;
    line-height: 1.4;
    border: 1px solid #e9ecef;
    animation: welcomeSlideIn 0.5s ease;
}

.avatar-welcome-bubble::after {
    content: "";
    position: absolute;
    bottom: -8px;
    right: 30px;
    width: 0;
    height: 0;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 8px solid white;
}

@keyframes welcomeSlideIn {
    from {
        opacity: 0;
        transform: translateY(10px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Responsive Design */
@media (max-width: 768px) {
    .avatar-chat-window {
        width: calc(100vw - 40px);
        height: 60vh;
        bottom: 140px;
        right: 20px;
        left: 20px;
    }
    
    .avatar-welcome-bubble {
        max-width: calc(100vw - 80px);
        right: 20px;
        left: 20px;
        text-align: center;
    }
}

/* Avatar Module Wrapper */
.avatar-module {
    contain: layout style paint;
    isolation: isolate;
}

/* Avatar Hover Effects */
#avatar-container:hover {
    transform: translateY(-4px) scale(1.05);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3), 0 0 20px rgba(102, 126, 234, 0.4);
}

/* Voice Chat Active State */
#avatar-container.voice-active {
    box-shadow: 0 0 0 4px rgba(255, 69, 58, 0.8), 0 12px 40px rgba(0, 0, 0, 0.3);
    animation: pulse-border 2s infinite;
    cursor: pointer;
}

#avatar-container.voice-active::after {
    content: "🔴 LISTENING - Click to Stop";
    position: absolute;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(255, 69, 58, 0.9);
    color: white;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: bold;
    white-space: nowrap;
    animation: fadeInOut 3s infinite;
    z-index: 10002;
}

/* Voice Indicator */
.avatar-voice-indicator {
    position: fixed;
    bottom: 150px;
    right: 20px;
    width: 120px;
    height: 40px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 9998;
}

.voice-waves {
    display: flex;
    align-items: center;
    height: 20px;
    width: 80px;
}

.voice-waves:before, 
.voice-waves:after {
    content: "";
    display: block;
    width: 3px;
    height: 100%;
    margin: 0 2px;
    border-radius: 1px;
    animation: voice-wave 0.8s infinite ease-in-out alternate;
    background: #4CD964;
}

.voice-waves:before {
    animation-delay: -0.3s;
}

@keyframes voice-wave {
    0% {
        height: 6px;
    }
    100% {
        height: 18px;
    }
}

@keyframes pulse-border {
    0% {
        box-shadow: 0 0 0 0 rgba(76, 217, 100, 0.8), 0 12px 40px rgba(0, 0, 0, 0.3);
    }
    70% {
        box-shadow: 0 0 0 10px rgba(76, 217, 100, 0), 0 12px 40px rgba(0, 0, 0, 0.3);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(76, 217, 100, 0), 0 12px 40px rgba(0, 0, 0, 0.3);
    }
}

/* Avatar Response Bubble */
.avatar-response-bubble {
    position: fixed;
    bottom: 150px;
    right: 30px;
    background: white;
    color: black;
    padding: 12px;
    border-radius: 12px;
    max-width: 300px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 10000;
    animation: fade-in 0.3s ease-out;
}

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

/* Avatar Chat Window */
.avatar-chat-window {
    position: fixed;
    bottom: 160px;
    right: 20px;
    width: 350px;
    height: 500px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
    z-index: 9998;
    display: none;
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.3s ease;
}

.avatar-chat-window.active {
    display: block;
    opacity: 1;
    transform: translateY(0);
}

/* Avatar Chat Messages Container */
#avatar-chat-messages {
    padding: 16px;
    height: calc(100% - 80px);
    overflow-y: auto;
    border-bottom: 1px solid #e1e5e9;
}

/* Avatar Chat Input Container */
.avatar-chat-input-container {
    padding: 16px;
    display: flex;
    gap: 8px;
    background: #f8f9fa;
    border-radius: 0 0 16px 16px;
}

/* Avatar Chat Input */
#avatar-chat-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid #d1d9e0;
    border-radius: 8px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s ease;
}

#avatar-chat-input:focus {
    border-color: #0078d4;
}

/* Avatar Chat Send Button */
#avatar-chat-send {
    padding: 8px 16px;
    background: #0078d4;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

#avatar-chat-send:hover {
    background: #106ebe;
}

#avatar-chat-send:active {
    background: #005a9e;
}

/* Avatar Animations */
@keyframes avatar-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.02); }
}

.avatar-breathing {
    animation: avatar-pulse 3s ease-in-out infinite;
}

/* Avatar Speaking State */
.avatar-speaking {
    transform: scale(1.05);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3), 0 0 20px rgba(0, 120, 212, 0.5);
    animation: avatar-speaking-pulse 0.5s ease-in-out infinite alternate;
}

@keyframes avatar-speaking-pulse {
    0% { transform: scale(1.05); }
    100% { transform: scale(1.08); }
}

/* Avatar Accessibility */
.avatar-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Avatar High Contrast Support */
@media (prefers-contrast: high) {
    #avatar-container {
        border: 2px solid #000;
        background: #fff;
    }
}

/* Avatar Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    #avatar-container,
    .avatar-chat-window {
        transition: none;
    }
    
    .avatar-breathing {
        animation: none;
    }
}

/* Avatar Keyboard Navigation */
#avatar-container:focus {
    outline: 3px solid #0078d4;
    outline-offset: 2px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2), 0 0 0 4px rgba(0, 120, 212, 0.3);
}

#avatar-container:focus-visible {
    outline: 3px solid #0078d4;
    outline-offset: 2px;
}

/* Avatar Chat Input Focus */
#avatar-chat-input:focus {
    outline: 2px solid #0078d4;
    outline-offset: 1px;
    border-color: #0078d4;
}

/* Avatar Chat Send Button Focus */
#avatar-chat-send:focus {
    outline: 2px solid #0078d4;
    outline-offset: 1px;
    background-color: #106ebe;
}

/* Avatar Screen Reader Enhancements */
.avatar-chat-window[aria-expanded="true"] #avatar-chat-input {
    position: relative;
}

.avatar-chat-window[aria-expanded="false"] {
    display: none;
    visibility: hidden;
}

/* Avatar Status Announcements */
.avatar-status-announcement {
    position: absolute;
    left: -10000px;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

/* Avatar High Contrast Enhanced */
@media (prefers-contrast: high) {
    #avatar-container {
        border: 3px solid #000;
        background: #fff;
        color: #000;
    }
    
    .avatar-chat-window {
        border: 2px solid #000;
        background: #fff;
        color: #000;
    }
    
    #avatar-chat-input {
        border: 2px solid #000;
        background: #fff;
        color: #000;
    }
    
    #avatar-chat-send {
        border: 2px solid #000;
        background: #000;
        color: #fff;
    }
}

/* Avatar Click Feedback */
#avatar-container:active {
    transform: scale(0.95);
    transition: transform 0.1s ease;
}

#avatar-container.clicked {
    animation: click-feedback 0.3s ease;
}

@keyframes click-feedback {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* Pulse animation for voice active indicator */
@keyframes pulse {
    0% { 
        transform: scale(1);
        opacity: 1;
    }
    50% { 
        transform: scale(1.1);
        opacity: 0.8;
    }
    100% { 
        transform: scale(1);
        opacity: 1;
    }
}

/* Enhanced voice active state with better visual feedback */
#avatar-container.voice-active {
    box-shadow: 0 0 0 4px rgba(76, 217, 100, 0.6), 0 12px 40px rgba(0, 0, 0, 0.3);
    animation: pulse-border 2s infinite;
}

/* Improved fade in/out animation for voice status */
@keyframes fadeInOut {
    0%, 100% { opacity: 0.7; }
    50% { opacity: 1; }
}

/* Voice active indicator positioning and styling */
.voice-active-indicator {
    animation: pulse 1.5s infinite !important;
}
