/**
 * Avatar Styles
 * D-ID AI Avatar component styling
 * Phase 1: Basic Avatar - Read AI Insights
 */

/* ========================================
   AVATAR MODAL
   ======================================== */

.avatar-modal-content {
    border: none;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    /* Create isolated stacking context to prevent external repaints */
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    isolation: isolate;
    contain: layout style paint;
}

.avatar-modal-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-bottom: none;
    padding: 1rem 1.5rem;
}

.avatar-modal-header .btn-close {
    filter: brightness(0) invert(1);
    opacity: 0.8;
}

.avatar-modal-header .btn-close:hover {
    opacity: 1;
}

.avatar-modal-body {
    padding: 0;
    background: #1a1a2e;
    min-height: 450px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* Prevent layout shifts */
    position: relative;
    overflow: hidden;
    /* Isolate from external repaints */
    transform: translateZ(0);
    contain: layout style paint;
}

/* Prevent avatar modal from being affected by external mouse events */
#avatarModal {
    z-index: 99999 !important;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
}

#avatarModal .modal-dialog {
    transform: translate(-50%, -50%) translateZ(0) !important;
    -webkit-transform: translate(-50%, -50%) translateZ(0) !important;
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    margin: 0 !important;
}

/* Force avatar modal backdrop to extremely high z-index */
#avatarModal + .modal-backdrop,
.modal-backdrop.show + #avatarModal ~ .modal-backdrop {
    z-index: 99998 !important;
}

/* When avatar modal is open, ensure it captures all mouse events */
#avatarModal.show {
    pointer-events: auto !important;
}

#avatarModal.show .modal-dialog {
    pointer-events: auto !important;
}

#avatarModal.show .modal-content {
    pointer-events: auto !important;
}

.avatar-modal-footer {
    background: #f8f9fa;
    border-top: 1px solid #e9ecef;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1.5rem;
}

/* ========================================
   LOADING STATE
   ======================================== */

.avatar-loading {
    text-align: center;
    padding: 3rem 2rem;
    color: white;
}

.avatar-loading-spinner {
    margin-bottom: 1.5rem;
}

.avatar-loading-spinner .spinner-border {
    width: 4rem;
    height: 4rem;
    border-width: 0.3rem;
    color: #667eea !important;
}

.avatar-loading-text {
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
    color: #fff;
}

.avatar-loading-subtext {
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.6) !important;
}

.avatar-progress {
    max-width: 300px;
    margin: 1.5rem auto 0;
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.1);
}

.avatar-progress .progress-bar {
    background: linear-gradient(90deg, #667eea, #764ba2);
    border-radius: 3px;
}

/* ========================================
   VIDEO CONTAINER
   ======================================== */

.avatar-video-container {
    width: 100%;
    position: relative;
    background: #000;
    /* Portrait aspect ratio - taller than wide */
    aspect-ratio: 9 / 16;
    max-height: 70vh;
    display: flex;
    align-items: center;
    justify-content: center;
    /* Force GPU compositing layer to prevent flicker */
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    isolation: isolate;
}

.avatar-video {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
    background: #000;
    /* Force GPU compositing and prevent repaints */
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
}

.avatar-video-status {
    position: absolute;
    bottom: 60px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.avatar-video-container.speaking .avatar-video-status {
    opacity: 1;
}

.avatar-video-status i {
    animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ========================================
   CONTROLS
   ======================================== */

.avatar-controls {
    display: flex;
    gap: 0.5rem;
}

.avatar-controls .btn {
    width: 36px;
    height: 36px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s ease;
}

.avatar-controls .btn:hover {
    transform: scale(1.1);
}

/* ========================================
   ERROR STATE
   ======================================== */

.avatar-error {
    text-align: center;
    padding: 3rem 2rem;
    color: white;
}

.avatar-error-icon {
    font-size: 3rem;
    color: #dc3545;
    margin-bottom: 1rem;
}

.avatar-error h6 {
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
}

.avatar-error p {
    color: rgba(255, 255, 255, 0.6) !important;
    margin-bottom: 1.5rem;
}

/* ========================================
   NOT AVAILABLE STATE
   ======================================== */

.avatar-not-available {
    text-align: center;
    padding: 3rem 2rem;
    color: white;
}

.avatar-info-icon {
    font-size: 3rem;
    color: #17a2b8;
    margin-bottom: 1rem;
}

.avatar-not-available h6 {
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
}

.avatar-not-available p {
    color: rgba(255, 255, 255, 0.6) !important;
}

/* ========================================
   LISTEN BUTTON
   ======================================== */

.avatar-listen-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 500;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.avatar-listen-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
    color: white;
}

.avatar-listen-btn:active {
    transform: translateY(0);
}

.avatar-listen-btn i {
    font-size: 1rem;
}

.avatar-listen-btn.btn-sm {
    padding: 0.35rem 0.75rem;
    font-size: 0.8rem;
}

.avatar-listen-btn.btn-sm i {
    font-size: 0.85rem;
}

/* Outline variant */
.avatar-listen-btn-outline {
    background: transparent;
    border: 2px solid #667eea;
    color: #667eea;
    box-shadow: none;
}

.avatar-listen-btn-outline:hover {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-color: transparent;
}

/* ========================================
   FLOATING AVATAR BUTTON
   ======================================== */

.floating-avatar-btn {
    position: fixed;
    bottom: 30px;
    right: 30px;
    z-index: 1050;
}

.floating-avatar-btn button {
    width: 60px;
    height: 60px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    transition: all 0.3s ease;
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.5);
}

.floating-avatar-btn button:hover {
    transform: scale(1.1) translateY(-2px);
    box-shadow: 0 8px 30px rgba(102, 126, 234, 0.6);
}

.floating-avatar-btn button i {
    font-size: 1.5rem;
}

/* Pulse animation for attention */
.floating-avatar-btn.pulse button {
    animation: avatar-pulse 2s infinite;
}

@keyframes avatar-pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(102, 126, 234, 0.7);
    }
    70% {
        box-shadow: 0 0 0 15px rgba(102, 126, 234, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(102, 126, 234, 0);
    }
}

/* ========================================
   INSIGHTS CARD WITH AVATAR BUTTON
   ======================================== */

.insights-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.insights-card-header .avatar-listen-btn {
    margin-left: auto;
}

/* ========================================
   RESPONSIVE ADJUSTMENTS
   ======================================== */

@media (max-width: 576px) {
    .avatar-modal-body {
        min-height: 350px;
        max-height: 85vh;
    }

    .avatar-video-container {
        max-height: 60vh;
    }

    .avatar-video {
        object-fit: cover;
    }

    .avatar-loading {
        padding: 2rem 1rem;
    }

    .avatar-loading-spinner .spinner-border {
        width: 3rem;
        height: 3rem;
    }

    .avatar-progress {
        max-width: 250px;
    }

    .floating-avatar-btn {
        bottom: 20px;
        right: 20px;
    }

    .floating-avatar-btn button {
        width: 50px;
        height: 50px;
    }

    .floating-avatar-btn button i {
        font-size: 1.25rem;
    }
}

/* ========================================
   DARK MODE SUPPORT
   ======================================== */

@media (prefers-color-scheme: dark) {
    .avatar-modal-footer {
        background: #2d2d2d;
        border-top-color: #3d3d3d;
    }
}

/* ========================================
   ANIMATION UTILITIES
   ======================================== */

.avatar-fade-in {
    animation: avatarFadeIn 0.3s ease;
}

@keyframes avatarFadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* Video container - prevent flicker with stable dimensions */
.avatar-video-container {
    will-change: opacity;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

.avatar-video {
    will-change: auto;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

/* Loading state - stable positioning */
.avatar-loading {
    will-change: opacity;
    backface-visibility: hidden;
}

.avatar-slide-up {
    animation: avatarSlideUp 0.3s ease;
}

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

/* ========================================
   AUDIO-ONLY PLAYER STYLES
   ======================================== */

.avatar-audio-container {
    text-align: center;
    padding: 3rem 2rem;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    min-height: 280px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.audio-visualization {
    margin-bottom: 1.5rem;
}

.audio-visualization i {
    color: #28a745;
    opacity: 0.8;
    transition: all 0.3s ease;
}

.audio-visualization i.audio-pulse {
    animation: audioPulse 1.5s ease-in-out infinite;
}

@keyframes audioPulse {
    0%, 100% {
        transform: scale(1);
        opacity: 0.8;
    }
    50% {
        transform: scale(1.1);
        opacity: 1;
    }
}

.audio-status-text {
    font-size: 1.1rem;
    color: #fff;
    margin-bottom: 1rem;
}

.audio-progress-bar {
    width: 100%;
    max-width: 300px;
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.1);
    overflow: hidden;
}

.audio-progress-bar > div {
    height: 100%;
    border-radius: 3px;
    background: linear-gradient(90deg, #28a745, #20c997);
    transition: width 0.3s ease;
}

/* ========================================
   LIVEAVATAR INTERACTIVE STYLES
   ======================================== */

#liveAvatarModal {
    z-index: 99999 !important;
}

#liveAvatarModal .modal-content {
    background: #1a1a2e;
    border: 1px solid #333;
    border-radius: 12px;
    overflow: hidden;
}

#liveAvatarModal .modal-header {
    background: linear-gradient(135deg, #0d6efd 0%, #0dcaf0 100%);
    border-bottom: none;
    padding: 0.75rem 1rem;
}

#liveAvatarModal .modal-footer {
    background: #16213e;
    border-top: 1px solid #333;
    padding: 0.5rem 1rem;
}

/* Chat Messages */
.chat-message {
    margin-bottom: 0.75rem;
}

.chat-message .bg-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
}

.chat-message .bg-info {
    background: linear-gradient(135deg, #0dcaf0 0%, #0d6efd 100%) !important;
}

/* LiveAvatar Video Area */
#liveAvatarVideo {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
}

#avatarPlaceholder {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
}

/* Loading Overlay */
#liveAvatarLoading {
    background: rgba(26, 26, 46, 0.95);
}

/* Input Area */
#liveAvatarInput {
    background: #1a1a2e !important;
    border-color: #333 !important;
    color: #fff !important;
}

#liveAvatarInput:focus {
    background: #16213e !important;
    border-color: #0dcaf0 !important;
    box-shadow: 0 0 0 0.25rem rgba(13, 202, 240, 0.25) !important;
}

#liveAvatarInput::placeholder {
    color: rgba(255, 255, 255, 0.5);
}

/* Status Badge Animations */
#liveAvatarStatus.bg-warning {
    animation: statusPulse 1.5s ease-in-out infinite;
}

#liveAvatarStatus.bg-info {
    animation: thinkingPulse 0.8s ease-in-out infinite;
}

@keyframes statusPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

@keyframes thinkingPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ========================================
   DRAGGABLE & RESIZABLE MODAL
   ======================================== */

/* Draggable dialog */
.avatar-draggable-dialog {
    transition: none !important;
}

.avatar-draggable-dialog.avatar-dragging {
    transition: none !important;
    cursor: move !important;
}

/* Drag handle (header) */
.avatar-drag-handle {
    cursor: move;
    user-select: none;
}

.avatar-drag-handle:hover {
    background: linear-gradient(135deg, #0b5ed7 0%, #0aa2c0 100%) !important;
}

/* Resizable modal content */
.avatar-resizable {
    min-width: 400px;
    min-height: 350px;
    resize: none; /* We use custom resize */
    overflow: hidden;
}

/* Resize handle (bottom-right corner) */
.avatar-resize-handle {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 20px;
    height: 20px;
    cursor: nwse-resize;
    background: linear-gradient(135deg, transparent 50%, rgba(255,255,255,0.3) 50%);
    border-bottom-right-radius: inherit;
    z-index: 10;
}

.avatar-resize-handle:hover {
    background: linear-gradient(135deg, transparent 50%, rgba(13, 202, 240, 0.6) 50%);
}

.avatar-resize-handle::before {
    content: '';
    position: absolute;
    bottom: 3px;
    right: 3px;
    width: 8px;
    height: 8px;
    border-right: 2px solid rgba(255,255,255,0.5);
    border-bottom: 2px solid rgba(255,255,255,0.5);
}

/* Resizing state */
.avatar-draggable-dialog.avatar-resizing {
    transition: none !important;
}

.avatar-draggable-dialog.avatar-resizing .modal-content {
    transition: none !important;
}

/* When dragging/resizing, lower z-index of other modals */
body.modal-open .modal:not(#liveAvatarModal) {
    z-index: 1050 !important;
}

/* Ensure liveAvatarModal stays on top but can be moved */
#liveAvatarModal.show .modal-dialog {
    /* Remove default centering when dragged */
}

#liveAvatarModal .modal-dialog.avatar-draggable-dialog {
    /* Allow custom positioning */
    will-change: left, top;
}
