/* ==========================================================================
   AI ASSISTANT - ESTILOS PRINCIPALES
   ========================================================================== */

:root {
    /* Colores principales - personalizables desde admin */
    --ai-primary: #E30613;
    --ai-secondary: #000000;
    --ai-accent: #FFD700;
    --ai-text: #FFFFFF;
    
    /* Colores derivados */
    --ai-primary-light: rgba(227, 6, 19, 0.1);
    --ai-primary-dark: #B5050F;
    --ai-secondary-light: rgba(0, 0, 0, 0.05);
    
    /* Sombras */
    --ai-shadow-light: 0 2px 12px rgba(0, 0, 0, 0.08);
    --ai-shadow-medium: 0 6px 24px rgba(0, 0, 0, 0.12);
    --ai-shadow-heavy: 0 12px 48px rgba(0, 0, 0, 0.18);
    
    /* Transiciones */
    --ai-transition-fast: 0.15s ease;
    --ai-transition-normal: 0.3s ease;
    --ai-transition-slow: 0.5s ease;
    
    /* Bordes */
    --ai-border-radius-small: 8px;
    --ai-border-radius-medium: 16px;
    --ai-border-radius-large: 24px;
    --ai-border-radius-circle: 50%;
    
    /* Espaciado */
    --ai-spacing-xs: 4px;
    --ai-spacing-sm: 8px;
    --ai-spacing-md: 12px;
    --ai-spacing-lg: 16px;
    --ai-spacing-xl: 24px;
    
    /* Tipografía */
    --ai-font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    --ai-font-size-sm: 12px;
    --ai-font-size-base: 14px;
    --ai-font-size-lg: 15px;
    --ai-font-size-xl: 18px;
}

/* ==========================================================================
   BURBUJA DEL ASISTENTE
   ========================================================================== */

.ai-assistant-widget {
    font-family: var(--ai-font-family);
    font-size: var(--ai-font-size-base);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Burbuja principal */
/* ✅ CORREGIDO: Eliminados !important innecesarios para mejor compatibilidad */
.ai-bubble-button {
    position: fixed;
    bottom: var(--ai-spacing-xl);
    right: var(--ai-spacing-xl);
    width: 60px;
    height: 60px;
    border-radius: var(--ai-border-radius-circle);
    background: var(--ai-primary);
    display: flex; 
    visibility: visible;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--ai-shadow-heavy);
    z-index: 999999;
    border: none;
    padding: 0;
    margin: 0;
    transition: all var(--ai-transition-normal);
    animation: ai-bubble-appear 0.5s ease forwards;
    opacity: 1;
    transform: scale(1);
    -webkit-tap-highlight-color: transparent;
}

.ai-bubble-button.ai-visible {
    display: flex;
    opacity: 1;
    transform: scale(1);
}

.ai-bubble-button:hover {
    transform: scale(1.1);
    box-shadow: 0 8px 32px rgba(227, 6, 19, 0.3);
}

.ai-bubble-button:active {
    transform: scale(0.95);
    transition: transform var(--ai-transition-fast);
}

.ai-bubble-button.ai-loading {
    animation: ai-pulse 2s ease-in-out infinite;
}

/* Posiciones alternativas */
.ai-assistant-widget[data-position="bottom-left"] .ai-bubble-button {
    right: auto;
    left: var(--ai-spacing-xl);
}

.ai-assistant-widget[data-position="top-right"] .ai-bubble-button {
    bottom: auto;
    top: var(--ai-spacing-xl);
}

.ai-assistant-widget[data-position="top-left"] .ai-bubble-button {
    bottom: auto;
    top: var(--ai-spacing-xl);
    right: auto;
    left: var(--ai-spacing-xl);
}

/* Icono de la burbuja */
.ai-bubble-icon {
    width: 100%;
    height: 100%;
    border-radius: var(--ai-border-radius-circle);
    object-fit: cover;
    object-position: center;
    pointer-events: none;
    display: block;
}

.ai-bubble-default {
    width: 24px;
    height: 24px;
    color: var(--ai-text);
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.ai-bubble-default svg {
    width: 100%;
    height: 100%;
    fill: currentColor;
}

/* Efecto de pulso */
.ai-pulse-effect {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: inherit;
    border: 2px solid var(--ai-primary);
    animation: ai-pulse-ring 2s ease-out infinite;
    pointer-events: none;
    z-index: -1;
}

/* Badge de notificación */
.ai-notification-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    padding: 0 4px;
    border-radius: 10px;
    background: #FF4444;
    color: white;
    font-size: var(--ai-font-size-sm);
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid white;
    pointer-events: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.ai-notification-badge.ai-hidden {
    display: none;
}

/* ==========================================================================
   VENTANA DE CHAT
   ========================================================================== */

/* ✅ CORREGIDO: Z-index alto pero sin !important */
.ai-chat-container {
    position: fixed;
    bottom: calc(60px + var(--ai-spacing-xl) + 12px);
    right: var(--ai-spacing-xl);
    width: 380px;
    max-width: 95vw;
    height: 600px;
    max-height: 80vh;
    background: white;
    border-radius: 16px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
    z-index: 1000000;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(0,0,0,0.1);
    animation: ai-chat-appear 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    transition: all 0.3s ease;
}

@media screen and (max-width: 480px) {
    .ai-chat-container {
        width: 100% !important;
        height: 80% !important;
        max-height: 100% !important;
        bottom: 0 !important;
        right: 0 !important;
        border-radius: 0 !important;
        max-width: 100% !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
        background: white !important;
    }
    
    .ai-bubble-button {
        bottom: 15px !important;
        right: 15px !important;
        width: 55px !important;
        height: 55px !important;
    }
}

.ai-chat-container.ai-visible {
    opacity: 1;
    transform: translateY(0) scale(1);
}

.ai-chat-container.ai-hidden {
    display: none;
}

/* Posiciones alternativas para el chat */
.ai-assistant-widget[data-position="bottom-left"] .ai-chat-container {
    right: auto;
    left: var(--ai-spacing-xl);
}

.ai-assistant-widget[data-position="top-right"] .ai-chat-container {
    bottom: auto;
    top: calc(60px + var(--ai-spacing-xl) + 8px);
}

.ai-assistant-widget[data-position="top-left"] .ai-chat-container {
    bottom: auto;
    top: calc(60px + var(--ai-spacing-xl) + 8px);
    right: auto;
    left: var(--ai-spacing-xl);
}

/* ==========================================================================
   ENCABEZADO DEL CHAT
   ========================================================================== */

.ai-chat-header {
    background: #e6e6e6;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #333;
    flex-shrink: 0;
}

.ai-header-info {
    display: flex;
    align-items: center;
    gap: var(--ai-spacing-md);
    flex: 1;
    min-width: 0; /* Para truncamiento de texto */
}

/* Avatar */
.ai-assistant-avatar {
    width: 28px !important;
    height: 28px !important;
    min-width: 28px;
    min-height: 28px;
    max-width: 28px;
    max-height: 28px;
    border-radius: 50% !important;
    object-fit: cover !important;
    object-position: center !important;
    flex-shrink: 0;
    background: #fff;
    margin-right: 8px;
    display: block;
}

.ai-default-avatar {
    background: rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
}

.ai-default-avatar svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

/* Información de texto */
.ai-header-text {
    flex: 1;
    min-width: 0; /* Permite truncamiento */
}

.ai-assistant-name {
    font-size: var(--ai-font-size-lg);
    font-weight: 700;
    margin: 0 0 2px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-status-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: var(--ai-font-size-sm);
    opacity: 0.9;
}

.ai-status-dot {
    width: 8px;
    height: 8px;
    border-radius: var(--ai-border-radius-circle);
    background: #4ADE80;
    display: inline-block;
    transition: background-color var(--ai-transition-normal);
}

.ai-status-dot.ai-status-offline {
    background: #FF6B6B;
}

.ai-status-dot.ai-status-online {
    background: #4ADE80;
}

.ai-status-text {
    font-size: 11px;
}

/* Controles del header */
.ai-header-controls {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.ai-control-btn {
    background: rgba(255, 255, 255, 0.12);
    border: none;
    color: white;
    width: 34px;
    height: 34px;
    border-radius: var(--ai-border-radius-small);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color var(--ai-transition-fast);
    padding: 0;
    margin: 0;
}

.ai-control-btn:hover {
    background: rgba(255, 255, 255, 0.25);
}

.ai-control-btn:active {
    transform: scale(0.95);
}

.ai-control-btn svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}

/* ==========================================================================
   ÁREA DE MENSAJES
   ========================================================================== */

.ai-messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    height: 420px;
    max-height: 420px;
    background: #f9f9fb;
    scroll-behavior: smooth;
}

/* Scrollbar personalizado */
.ai-messages-container::-webkit-scrollbar {
    width: 6px;
}

.ai-messages-container::-webkit-scrollbar-track {
    background: transparent;
    border-radius: 3px;
}

.ai-messages-container::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.1);
    border-radius: 3px;
}

.ai-messages-container::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.2);
}

/* Wrapper de mensajes */
.ai-message-wrapper {
    display: flex;
    max-width: 100%;
    animation: ai-message-appear 0.3s ease forwards;
    opacity: 0;
    transform: translateY(8px);
}

.ai-message-wrapper-user {
    justify-content: flex-end;
}

.ai-message-wrapper-assistant {
    justify-content: flex-start;
}

/* Mensajes individuales */
.ai-message {
    max-width: 85%;
    padding: 10px 12px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.35;
    word-wrap: break-word;
    white-space: pre-wrap;
    position: relative;
}

.ai-message-user {
    background: #e53935;
    color: #fff;
    margin-left: auto;
}

.ai-message-assistant {
    background: #cfe3ff;
    color: #111;
    margin-right: auto;
}

.ai-message-error {
    background: #FFE5E5;
    color: #D32F2F;
    border: 1px solid #FFCDD2;
}

/* Contenido del mensaje */
.ai-message-content {
    word-break: break-word;
}

.ai-message-content a {
    color: inherit;
    text-decoration: underline;
    transition: opacity var(--ai-transition-fast);
}

.ai-message-content a:hover {
    opacity: 0.8;
}

/* Timestamp */
.ai-message-time {
    font-size: var(--ai-font-size-sm);
    opacity: 0.6;
    margin-top: 4px;
    display: block;
    text-align: right;
}

/* ==========================================================================
   INDICADOR DE "ESCRIBIENDO..."
   ========================================================================== */

.ai-typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    background: #e8e8ec;
    border-radius: var(--ai-border-radius-medium);
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    animation: ai-typing-appear 0.3s ease;
}

.ai-typing-dot {
    width: 8px;
    height: 8px;
    background: #888;
    border-radius: var(--ai-border-radius-circle);
    animation: ai-dot-bounce 1.4s infinite ease-in-out;
}

.ai-typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.ai-typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

/* ==========================================================================
   ÁREA DE ENTRADA
   ========================================================================== */

.ai-input-container {
    display: flex;
    align-items: center;
    gap: var(--ai-spacing-sm);
    padding: var(--ai-spacing-md) var(--ai-spacing-lg);
    background: white;
    border-top: 1px solid #f0f0f0;
    flex-shrink: 0;
    padding-bottom: calc(var(--ai-spacing-md) + env(safe-area-inset-bottom));
}

/* Campo de entrada */
.ai-message-input {
    flex: 1;
    padding: 10px 14px;
    border: 2px solid #e8e8ec;
    border-radius: var(--ai-border-radius-large);
    font-size: var(--ai-font-size-base);
    outline: none;
    transition: all var(--ai-transition-fast);
    font-family: inherit;
    resize: none;
    min-height: 20px;
    max-height: 120px;
    line-height: 1.4;
}

.ai-message-input:focus {
    border-color: var(--ai-primary);
    box-shadow: 0 0 0 3px var(--ai-primary-light);
}

.ai-message-input::placeholder {
    color: #999;
    opacity: 1;
}

/* Botón de enviar */
.ai-send-button {
    width: 38px;
    height: 38px;
    border: none;
    border-radius: var(--ai-border-radius-circle);
    background: var(--ai-primary);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--ai-transition-fast);
    flex-shrink: 0;
    padding: 0;
    margin: 0;
}

.ai-send-button:hover:not(:disabled) {
    background: var(--ai-primary-dark);
    transform: scale(1.05);
}

.ai-send-button:active:not(:disabled) {
    transform: scale(0.95);
}

.ai-send-button:disabled {
    background: #ccc;
    cursor: not-allowed;
    opacity: 0.6;
    transform: none !important;
}

.ai-send-button svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
}

/* Botón de micrófono */
.ai-mic-button {
    width: 38px;
    height: 38px;
    border: none;
    border-radius: var(--ai-border-radius-circle);
    background: #666;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--ai-transition-fast);
    flex-shrink: 0;
    padding: 0;
    margin: 0;
}

.ai-mic-button:hover:not(:disabled) {
    background: #444;
    transform: scale(1.05);
}

.ai-mic-button:active:not(:disabled) {
    transform: scale(0.95);
}

.ai-mic-button.recording {
    background: #e53935;
    animation: ai-pulse 1.5s ease-in-out infinite;
}

.ai-mic-button:disabled {
    background: #ccc;
    cursor: not-allowed;
    opacity: 0.6;
}

.ai-mic-button svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

@keyframes ai-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* ==========================================================================
   ANIMACIONES
   ========================================================================== */

@keyframes ai-bubble-appear {
    0% {
        opacity: 0;
        transform: scale(0.8);
    }
    70% {
        transform: scale(1.05);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes ai-chat-appear {
    0% {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes ai-message-appear {
    0% {
        opacity: 0;
        transform: translateY(8px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes ai-typing-appear {
    0% {
        opacity: 0;
        transform: translateY(5px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes ai-pulse-ring {
    0% {
        transform: scale(1);
        opacity: 0.8;
    }
    100% {
        transform: scale(1.6);
        opacity: 0;
    }
}

@keyframes ai-pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
}

@keyframes ai-dot-bounce {
    0%, 80%, 100% {
        transform: scale(0.7);
        opacity: 0.4;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ==========================================================================
   RESPONSIVE DESIGN
   ========================================================================== */

@media (max-width: 480px) {
    .ai-chat-container {
        width: calc(100vw - 20px);
        right: 10px;
        left: 10px;
        bottom: 80px;
        max-height: 70vh;
    }
    
    .ai-bubble-button {
        bottom: 20px;
        right: 20px;
        width: 56px;
        height: 56px;
    }
    
    .ai-assistant-widget[data-position="bottom-left"] .ai-bubble-button {
        left: 20px;
        right: auto;
    }
    
    .ai-assistant-widget[data-position="top-right"] .ai-chat-container {
        top: 80px;
        bottom: auto;
    }
    
    .ai-assistant-widget[data-position="top-left"] .ai-chat-container {
        top: 80px;
        bottom: auto;
    }
    
    .ai-message {
        max-width: 90%;
    }
}

/* Soporte para dark mode */
@media (prefers-color-scheme: dark) {
    .ai-chat-container {
        background: #1a1a1a;
        border-color: rgba(255, 255, 255, 0.1);
    }
    
    .ai-messages-container {
        background: #1f1f1f;
    }
    
    .ai-message-input {
        background: #2d2d2d;
        border-color: #404040;
        color: #e0e0e0;
    }
    
    .ai-input-container {
        background: #1a1a1a;
        border-top-color: rgba(255, 255, 255, 0.1);
    }
    
    .ai-typing-indicator {
        background: #2d2d2d;
    }
    
    .ai-typing-dot {
        background: #666;
    }
    
    .ai-messages-container::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.1);
    }
    
    .ai-messages-container::-webkit-scrollbar-thumb:hover {
        background: rgba(255, 255, 255, 0.2);
    }
}

/* ==========================================================================
   ESTADOS ESPECIALES
   ========================================================================== */

/* Mensaje de bienvenida */
.ai-message-welcome .ai-message-content {
    font-weight: 500;
}

/* Mensaje con acción (botones, etc) */
.ai-message-action {
    background: #F5F5F5 !important;
    color: #333 !important;
    border: 1px solid #E0E0E0;
}

/* Transiciones suaves para cambios de estado */
.ai-transition-all {
    transition: all var(--ai-transition-normal);
}

/* ✅ CORREGIDO: Clases utilitarias sin !important */
/* Clases utilitarias */
.ai-hidden {
    display: none;
}

.ai-visible {
    display: flex;
}

.ai-opacity-0 {
    opacity: 0;
}

.ai-opacity-100 {
    opacity: 1;
}

.ai-pointer-events-none {
    pointer-events: none;
}

.ai-pointer-events-auto {
    pointer-events: auto;
}

/* Asegurar que las animaciones funcionen en todos los navegadores */
.ai-animate {
    animation-duration: 0.3s;
    animation-fill-mode: both;
}

/* ==========================================================================
   ACCESIBILIDAD
   ========================================================================== */

/* Focus visible para navegación con teclado */
.ai-bubble-button:focus-visible,
.ai-control-btn:focus-visible,
.ai-send-button:focus-visible,
.ai-message-input:focus-visible {
    outline: 2px solid var(--ai-accent);
    outline-offset: 2px;
}

/* Screen reader only */
.ai-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;
}

/* Mejorar contraste para accesibilidad */
.ai-message-user,
.ai-message-assistant {
    color-scheme: light dark;
}

/* ==========================================================================
   COMPATIBILIDAD CON NAVEGADORES ANTIGUOS
   ========================================================================== */

/* ✅ NUEVO: Forzar visibilidad en modo incógnito */
.ai-assistant-widget {
    position: relative;
    z-index: 999998;
}

/* Fallback para browsers sin soporte de CSS custom properties */
.ai-bubble-button {
    background: #E30613; /* Fallback */
}

/* Fallback para animaciones */
.no-js .ai-bubble-button,
.no-js .ai-chat-container,
.no-js .ai-message-wrapper {
    animation: none;
    opacity: 1;
    transform: none;
}

/* Soporte para Internet Explorer 11 */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
    .ai-chat-container {
        min-width: 380px; /* IE11 necesita width fijo */
    }
}