/**
 * Chat Widget Styles - MIA (Mi Inteligencia Académica)
 * 
 * Estilos para el widget de chat de MIA.
 * Branding: Púrpura/Magenta con tema espacial.
 * 
 * @version 2.0.0
 * @author Daniel Banegas
 */

/* ============================================
   Variables CSS - Branding MIA
   ============================================ */
#unae-chat-widget {
    /* Colores principales MIA - Púrpura/Magenta */
    --chat-primary: #7C3AED;           /* Púrpura vibrante */
    --chat-primary-dark: #5B21B6;      /* Púrpura oscuro */
    --chat-primary-light: #A78BFA;     /* Púrpura claro */
    --chat-accent: #EC4899;            /* Magenta/Rosa */
    --chat-accent-light: #F472B6;      /* Magenta claro */
    
    /* Fondos */
    --chat-bg: #ffffff;
    --chat-bg-messages: linear-gradient(180deg, #1a0a2e 0%, #16082a 50%, #0d0619 100%);
    --chat-bg-header: linear-gradient(135deg, #5B21B6 0%, #7C3AED 50%, #EC4899 100%);
    
    /* Textos */
    --chat-text: #1e293b;
    --chat-text-light: #94a3b8;
    --chat-text-messages: #e2e8f0;
    
    /* Bordes y sombras */
    --chat-border: #3b2d5a;
    --chat-shadow: 0 10px 40px rgba(124, 58, 237, 0.3);
    --chat-shadow-btn: 0 4px 12px rgba(124, 58, 237, 0.5);
    
    /* Radios y transiciones */
    --chat-radius: 16px;
    --chat-radius-sm: 8px;
    --chat-transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ============================================
   Botón Toggle
   ============================================ */
.chat-toggle-btn {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--chat-primary-dark), var(--chat-primary), var(--chat-accent));
    border: 2px solid rgba(255, 255, 255, 0.2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--chat-shadow-btn), 0 0 20px rgba(236, 72, 153, 0.3);
    transition: transform var(--chat-transition), box-shadow var(--chat-transition);
    z-index: 9999;
    overflow: hidden;
}

/* Botón con imagen de MIA */
.chat-toggle-btn.has-avatar {
    padding: 0;
}

.chat-toggle-btn .mia-avatar-btn {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.chat-toggle-btn:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 25px rgba(124, 58, 237, 0.6), 0 0 30px rgba(236, 72, 153, 0.4);
}

.chat-toggle-btn:active {
    transform: scale(0.95);
}

.chat-toggle-btn svg {
    width: 28px;
    height: 28px;
    color: white;
    transition: transform var(--chat-transition);
}

.chat-toggle-btn .hidden {
    display: none;
}

/* ============================================
   Ventana de Chat
   ============================================ */
.chat-window {
    position: fixed;
    bottom: 100px;
    right: 24px;
    width: 380px;
    height: 550px;
    max-height: calc(100vh - 140px);
    background: var(--chat-bg);
    border-radius: var(--chat-radius);
    box-shadow: var(--chat-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 9998;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    transition: opacity var(--chat-transition), transform var(--chat-transition);
    pointer-events: none;
}

.chat-window.hidden {
    display: flex;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
}

.chat-window.chat-open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ============================================
   Header del Chat - MIA Branding
   ============================================ */
.chat-header {
    background: var(--chat-bg-header);
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    position: relative;
}

/* Colibrí decorativo en el header */
.chat-header::after {
    content: '';
    position: absolute;
    right: 60px;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    height: 40px;
    background-image: var(--mia-colibri-icon);
    background-size: contain;
    background-repeat: no-repeat;
    opacity: 0.3;
    pointer-events: none; /* Permitir clics a través del elemento */
}

.chat-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-header-actions {
    display: flex;
    gap: 8px;
    position: relative;
    z-index: 10; /* Asegurar que los botones estén encima */
}

.chat-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border: 2px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.chat-avatar .avatar-emoji {
    font-size: 24px;
    line-height: 1;
}

.chat-avatar .mia-avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.chat-title h3 {
    margin: 0;
    font-size: 17px;
    font-weight: 600;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.chat-title .mia-subtitle {
    font-size: 11px;
    opacity: 0.85;
    font-weight: 400;
    display: block;
    margin-top: 2px;
}

.chat-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    opacity: 0.9;
}

.status-dot {
    width: 8px;
    height: 8px;
    background: #4ade80;
    border-radius: 50%;
    animation: pulse 2s infinite;
}

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

.chat-action-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.15);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--chat-transition);
}

.chat-action-btn:hover {
    background: rgba(255, 255, 255, 0.25);
}

.chat-action-btn svg {
    width: 18px;
    height: 18px;
}

/* ============================================
   Área de Mensajes
   ============================================ */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: var(--chat-bg-messages);
    position: relative;
}

/* Fondo personalizado con imagen */
.chat-messages.has-custom-background {
    background-size: cover !important;
    background-position: center !important;
    background-repeat: no-repeat !important;
    background-attachment: local !important;
}

/* Fondo con estrellas/partículas */
.chat-messages::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: 
        radial-gradient(1px 1px at 20px 30px, rgba(255, 255, 255, 0.3), transparent),
        radial-gradient(1px 1px at 40px 70px, rgba(255, 255, 255, 0.2), transparent),
        radial-gradient(1px 1px at 50px 160px, rgba(255, 255, 255, 0.3), transparent),
        radial-gradient(1px 1px at 90px 40px, rgba(255, 255, 255, 0.2), transparent),
        radial-gradient(1px 1px at 130px 80px, rgba(255, 255, 255, 0.3), transparent),
        radial-gradient(1.5px 1.5px at 160px 120px, rgba(236, 72, 153, 0.4), transparent);
    pointer-events: none;
    z-index: 0;
}

.chat-messages > * {
    position: relative;
    z-index: 1;
}

.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--chat-primary);
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--chat-accent);
}

/* ============================================
   Mensajes
   ============================================ */
.message {
    display: flex;
    gap: 10px;
    max-width: 85%;
    animation: fadeInMessage 0.3s ease;
}

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

.message.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.message.bot .message-avatar {
    background: linear-gradient(135deg, var(--chat-primary), var(--chat-accent));
    color: white;
    overflow: hidden;
}

.message.bot .message-avatar .mia-msg-avatar {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.message.user .message-avatar {
    background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent-light));
    color: white;
    overflow: hidden;
}

.message.user .message-avatar .user-msg-avatar {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.message-content {
    padding: 12px 16px;
    border-radius: var(--chat-radius);
    line-height: 1.5;
    font-size: 14px;
}

.message.bot .message-content {
    background: linear-gradient(135deg, rgba(91, 33, 182, 0.9), rgba(124, 58, 237, 0.8));
    color: #f1f5f9;
    border-bottom-left-radius: 4px;
    box-shadow: 0 2px 10px rgba(124, 58, 237, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.message.user .message-content {
    background: linear-gradient(135deg, rgba(236, 72, 153, 0.9), rgba(244, 114, 182, 0.8));
    color: white;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 10px rgba(236, 72, 153, 0.3);
}

.message-content p {
    margin: 0 0 8px 0;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content code {
    background: rgba(0, 0, 0, 0.08);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Monaco', 'Consolas', monospace;
    font-size: 13px;
}

.message.user .message-content code {
    background: rgba(255, 255, 255, 0.2);
}

.message-content a {
    color: inherit;
    text-decoration: underline;
}

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

/* Mensaje de error */
.message.error .message-content {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

/* ============================================
   Indicador de Escritura - MIA
   ============================================ */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    background: linear-gradient(135deg, rgba(91, 33, 182, 0.9), rgba(124, 58, 237, 0.8));
    border-radius: var(--chat-radius);
    width: fit-content;
    box-shadow: 0 2px 10px rgba(124, 58, 237, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: var(--chat-accent-light);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) { animation-delay: 0s; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 80%, 100% { transform: translateY(0); }
    40% { transform: translateY(-8px); }
}

/* ============================================
   Área de Input - MIA
   ============================================ */
.chat-input-area {
    padding: 16px;
    background: linear-gradient(180deg, #1a0a2e, #0d0619);
    border-top: 1px solid rgba(124, 58, 237, 0.3);
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

.chat-input-area textarea {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid rgba(124, 58, 237, 0.4);
    border-radius: 24px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
    resize: none;
    max-height: 120px;
    transition: border-color var(--chat-transition), box-shadow var(--chat-transition);
    line-height: 1.4;
    background: rgba(255, 255, 255, 0.05);
    color: #f1f5f9;
}

.chat-input-area textarea:focus {
    border-color: var(--chat-accent);
    box-shadow: 0 0 15px rgba(236, 72, 153, 0.2);
}

.chat-input-area textarea::placeholder {
    color: rgba(148, 163, 184, 0.7);
}

.chat-send-btn {
    width: 48px;
    height: 48px;
    min-width: 48px;
    background: linear-gradient(135deg, var(--chat-primary), var(--chat-accent));
    border: none;
    border-radius: 50%;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform var(--chat-transition), box-shadow var(--chat-transition), opacity var(--chat-transition);
}

.chat-send-btn:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: var(--chat-shadow-btn);
}

.chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.chat-send-btn.loading {
    animation: rotate 1s linear infinite;
}

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

.chat-send-btn svg {
    width: 20px;
    height: 20px;
}

/* ============================================
   Responsive
   ============================================ */
@media (max-width: 480px) {
    .chat-toggle-btn {
        bottom: 16px;
        right: 16px;
        width: 56px;
        height: 56px;
    }
    
    .chat-window {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        height: 100%;
        max-height: 100vh;
        border-radius: 0;
    }
    
    .chat-window.chat-open {
        transform: translateY(0) scale(1);
    }
    
    .chat-header {
        padding: 16px;
        border-radius: 0;
    }
    
    .chat-messages {
        padding: 16px;
    }
    
    .message {
        max-width: 90%;
    }
    
    .chat-input-area {
        padding: 12px;
    }
}

/* ============================================
   Dark Mode Support - MIA ya usa tema oscuro
   ============================================ */
@media (prefers-color-scheme: dark) {
    /* MIA ya usa un tema oscuro por defecto, 
       solo ajustes menores si es necesario */
    #unae-chat-widget {
        --chat-bg: #0d0619;
    }
}

/* ============================================
   Links en mensajes - MIA
   ============================================ */
.message.bot .message-content a {
    color: var(--chat-accent-light);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color var(--chat-transition);
}

.message.bot .message-content a:hover {
    border-bottom-color: var(--chat-accent-light);
    opacity: 1;
}

/* Fuentes citadas en respuestas */
.message-sources {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 12px;
    color: rgba(255, 255, 255, 0.7);
}

.message-sources a {
    color: var(--chat-accent-light) !important;
}

/* ============================================
   Tablas en mensajes - MIA
   ============================================ */
.message.bot .table-wrapper {
    margin: 12px 0;
    overflow-x: auto;
    border-radius: var(--chat-radius-sm);
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.message.bot .chat-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    background: transparent;
}

.message.bot .chat-table thead {
    background: linear-gradient(135deg, rgba(124, 58, 237, 0.3), rgba(236, 72, 153, 0.3));
    border-bottom: 2px solid var(--chat-primary-light);
}

.message.bot .chat-table th {
    padding: 10px 12px;
    text-align: left;
    font-weight: 600;
    color: #ffffff;
    border-right: 1px solid rgba(255, 255, 255, 0.1);
}

.message.bot .chat-table th:last-child {
    border-right: none;
}

.message.bot .chat-table td {
    padding: 8px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    border-right: 1px solid rgba(255, 255, 255, 0.05);
    color: var(--chat-text-messages);
}

.message.bot .chat-table td:last-child {
    border-right: none;
}

.message.bot .chat-table tbody tr:hover {
    background: rgba(255, 255, 255, 0.03);
}

.message.bot .chat-table tbody tr:last-child td {
    border-bottom: none;
}

/* ============================================
   Listas en mensajes - MIA
   ============================================ */
.message.bot .chat-list {
    margin: 8px 0;
    padding-left: 24px;
    color: var(--chat-text-messages);
}

.message.bot .chat-list li {
    margin: 6px 0;
    line-height: 1.6;
    position: relative;
}

.message.bot ul.chat-list {
    list-style: none;
}

.message.bot ul.chat-list li::before {
    content: "▸";
    color: var(--chat-accent-light);
    font-weight: bold;
    position: absolute;
    left: -18px;
}

.message.bot ol.chat-list {
    list-style: decimal;
    list-style-position: outside;
}

.message.bot ol.chat-list li {
    padding-left: 4px;
}

.message.bot ol.chat-list li::marker {
    color: var(--chat-accent-light);
    font-weight: 600;
}

/* ============================================
   Código en mensajes - MIA
   ============================================ */
.message.bot .inline-code {
    background: rgba(255, 255, 255, 0.1);
    color: var(--chat-accent-light);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9em;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.message.bot .code-block {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-left: 3px solid var(--chat-primary-light);
    border-radius: var(--chat-radius-sm);
    padding: 12px 16px;
    margin: 12px 0;
    overflow-x: auto;
    font-family: 'Courier New', Courier, monospace;
    font-size: 13px;
    line-height: 1.5;
}

.message.bot .code-block code {
    color: #e2e8f0;
    background: transparent;
    padding: 0;
    border: none;
}

/* Scrollbar personalizado para tablas y código */
.message.bot .table-wrapper::-webkit-scrollbar,
.message.bot .code-block::-webkit-scrollbar {
    height: 6px;
}

.message.bot .table-wrapper::-webkit-scrollbar-track,
.message.bot .code-block::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 3px;
}

.message.bot .table-wrapper::-webkit-scrollbar-thumb,
.message.bot .code-block::-webkit-scrollbar-thumb {
    background: rgba(124, 58, 237, 0.5);
    border-radius: 3px;
}

.message.bot .table-wrapper::-webkit-scrollbar-thumb:hover,
.message.bot .code-block::-webkit-scrollbar-thumb:hover {
    background: rgba(124, 58, 237, 0.7);
}

/* ============================================
   Accesibilidad
   ============================================ */
@media (prefers-reduced-motion: reduce) {
    .chat-toggle-btn,
    .chat-window,
    .message,
    .chat-send-btn,
    * {
        animation: none !important;
        transition: none !important;
    }
}

/* Focus visible para navegación con teclado */
.chat-toggle-btn:focus-visible,
.chat-action-btn:focus-visible,
.chat-send-btn:focus-visible,
.chat-input-area textarea:focus-visible {
    outline: 3px solid var(--chat-primary-light);
    outline-offset: 2px;
}
