/**
 * Sclavus AI Frontend Chat UI v11.0.0
 *
 * Hoja de estilos completamente rediseñada con un enfoque en la claridad,
 * la consistencia y una experiencia de usuario profesional.
 * Incluye soporte para Markdown y previsualización de archivos.
 */

/* --- 1. Variables y Estilos Globales --- */
:root {
    --sai-primary-color: #145e05; /* Un verde corporativo más oscuro */
    --sai-user-bubble-bg: var(--sai-primary-color);
    --sai-user-bubble-text: #ffffff;
    --sai-bot-bubble-bg: #f1f1f5; /* Un gris más suave */
    --sai-bot-bubble-text: #2c2c2e;
    --sai-chat-bg: #ffffff;
    --sai-font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --sai-width: 380px;
    --sai-height: 550px;
    --sai-radius: 12px;
    --sai-shadow: 0 5px 25px rgba(0, 0, 0, 0.1);
}

/* Contenedor principal para aislar estilos */
.sai-chat-container {
    width: var(--sai-width);
    height: var(--sai-height);
    display: flex;
    flex-direction: column;
    background: var(--sai-chat-bg);
    border-radius: var(--sai-radius);
    box-shadow: var(--sai-shadow);
    overflow: hidden;
    font-family: var(--sai-font-family);
    transition: all 0.3s ease;
    font-size: 14px; /* Tamaño de fuente base */
}

@media (max-width: 480px) {
    .sai-chat-container {
        width: 100%;
        height: 100%;
        max-height: 80vh;
        border-radius: 0;
    }
}

/* --- 2. Cabecera y Pie de Página --- */
.sai-chat-header {
    background: var(--sai-primary-color);
    color: var(--sai-user-bubble-text);
    padding: 15px 20px;
    flex-shrink: 0;
}
.sai-chat-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
}
.sai-chat-footer {
    text-align: center;
    padding: 8px;
    font-size: 12px;
    color: #aaa;
    background: #f9f9f9;
    border-top: 1px solid #e7e7e7;
    flex-shrink: 0;
}
.sai-chat-footer a { color: #999; text-decoration: none; }
.sai-chat-footer a:hover { color: var(--sai-primary-color); }

/* --- 3. Caja de Chat y Mensajes --- */
.sai-chat-box {
    flex-grow: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.sai-chat-box::-webkit-scrollbar { width: 6px; }
.sai-chat-box::-webkit-scrollbar-track { background: transparent; }
.sai-chat-box::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }

.sai-chat-message {
    padding: 10px 15px;
    border-radius: 18px;
    line-height: 1.5;
    max-width: 80%;
    animation: sai-fade-in-up 0.4s ease-out;
    word-wrap: break-word;
    font-size: 12px;
}
.sai-message-user {
    background: var(--sai-user-bubble-bg);
    color: var(--sai-user-bubble-text);
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}
.sai-message-bot {
    background: var(--sai-bot-bubble-bg);
    color: var(--sai-bot-bubble-text);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}
.sai-message-bot.sai-loading-bubble {
    width: 60px; height: 35px; /* Tamaño fijo para la burbuja de carga */
    display: flex; align-items: center; justify-content: center;
}
.sai-message-bot.sai-loading-bubble::after {
    content: ''; display: inline-block; width: 5px; height: 5px;
    background: #999; border-radius: 50%;
    animation: sai-typing-dots 1s infinite;
    box-shadow: 10px 0 0 #999, 20px 0 0 #999;
}
.sai-message-bot-error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }

/* --- 4. Estilos para Markdown Renderizado --- */
.sai-message-bot a { color: var(--sai-primary-color); text-decoration: underline; }
.sai-message-bot ul, .sai-message-bot ol { padding-left: 20px; margin: 5px 0; }
.sai-message-bot strong { font-weight: 600; }
.sai-message-bot pre {
    background-color: #2d2d2d;
    color: #f8f8f2;
    padding: 15px;
    border-radius: 8px;
    font-family: "Courier New", Courier, monospace;
    font-size: 11px;
    white-space: pre-wrap;
    position: relative;
    margin: 10px 0;
}
.sai-message-bot code {
    background-color: rgba(0,0,0,0.05);
    padding: 2px 4px;
    border-radius: 4px;
    font-size: 0.9em;
}
.sai-message-bot pre code { background: none; padding: 0; }
.sai-copy-button {
    position: absolute;
    top: 8px; right: 8px;
    background: #444; color: white; border: none;
    padding: 4px 8px; border-radius: 4px; cursor: pointer;
    font-size: 10px; opacity: 0.7; transition: opacity 0.2s;
}
.sai-message-bot pre:hover .sai-copy-button { opacity: 1; }

/* --- 5. Área de Entrada y Adjuntos --- */
.sai-chat-input-wrapper { border-top: 1px solid #e7e7e7; padding: 10px; flex-shrink: 0; }
.sai-chat-input-area { display: flex; align-items: flex-end; gap: 10px; }
.sai-file-input { display: none; }

.sai-attach-button {
    background: transparent; border: none; border-radius: 50%;
    width: 40px; height: 40px; flex-shrink: 0; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    color: #777; transition: all 0.2s;
}
.sai-attach-button:hover { background-color: #f1f1f1; color: var(--sai-primary-color); }
.sai-chat-input {
    flex-grow: 1; border: none; background: #f1f1f1;
    border-radius: 20px; padding: 10px 18px; font-size: 12px;
    resize: none; outline: none; transition: background-color 0.2s;
    min-height: 40px; max-height: 120px; box-sizing: border-box;
    line-height: 1.4;
}
.sai-chat-input:focus { background-color: #e9e9e9; }
.sai-send-button {
    background: var(--sai-primary-color); color: white; border: none; border-radius: 50%;
    width: 40px; height: 40px; flex-shrink: 0; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
}
.sai-send-button:hover { background: var(--sai-primary-hover); transform: scale(1.05); }
.sai-send-button:disabled { background: #ccc; cursor: not-allowed; transform: scale(1); }

/* Previsualización de Archivos */
.sai-file-preview {
    display: flex; align-items: center; background: #eef5ff;
    border: 1px solid #b3d4ff; border-radius: 8px;
    padding: 5px 10px; margin-bottom: 10px; font-size: 13px;
    animation: sai-fade-in-up 0.3s;
}
.sai-file-preview img {
    width: 32px; height: 32px;
    border-radius: 4px;
    margin-right: 10px;
    object-fit: cover;
}
.sai-file-preview span {
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex-grow: 1;
}
.sai-file-preview-remove {
    background: transparent; border: none; cursor: pointer;
    color: #777; margin-left: 10px; font-size: 20px;
    padding: 0; line-height: 1;
}
.sai-file-preview-remove:hover { color: #d63638; }

/* --- 6. Animaciones --- */
@keyframes sai-fade-in-up { from { opacity: 0; transform: translateY(15px); } to { opacity: 1; transform: translateY(0); } }
@keyframes sai-typing-dots {
    0% { box-shadow: 10px 0 0 transparent, 20px 0 0 transparent; }
    50% { box-shadow: 10px 0 0 currentColor, 20px 0 0 transparent; }
    100% { box-shadow: 10px 0 0 currentColor, 20px 0 0 currentColor; }
}
/* === ESTILOS PARA SISTEMA DE CONVERSACIONES EN FRONTEND === */

/* Botón de nueva conversación en el header */
.sai-new-conversation-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    color: white;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
}

.sai-new-conversation-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: translateY(-50%) rotate(180deg);
}

.sai-chat-header {
    position: relative;
}

/* Animación deshabilitada para carga de historial */
.sai-chat-message.no-animate {
    animation: none;
}

/* Indicador de historial cargado */
.sai-chat-box.history-loaded::before {
    content: '--- Historial de conversación ---';
    display: block;
    text-align: center;
    color: #999;
    font-size: 11px;
    padding: 10px 0;
    border-bottom: 1px solid #e7e7e7;
    margin-bottom: 15px;
}

/* Mejorar visualización en móviles */
@media (max-width: 480px) {
    .sai-new-conversation-btn {
        width: 28px;
        height: 28px;
        font-size: 16px;
        right: 10px;
    }
}

/* === ESTILOS FASE 2: CAPACIDADES MULTIMODALES === */

/* Preview de múltiples archivos */
.sai-files-preview {
    padding: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    background: #f9f9f9;
    border-top: 1px solid #e7e7e7;
}

.sai-file-preview-item {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    max-width: 200px;
    font-size: 12px;
}

.sai-file-preview-item img {
    width: 40px;
    height: 40px;
    object-fit: cover;
    border-radius: 4px;
}

.sai-file-icon {
    font-size: 24px;
}

.sai-file-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 500;
}

.sai-file-size {
    font-size: 10px;
    color: #666;
}

.sai-file-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #ff4444;
    color: white;
    border: 2px solid white;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.sai-file-remove:hover {
    background: #cc0000;
    transform: scale(1.1);
}

/* Drag & Drop */
.sai-chat-container.sai-drag-over {
    border: 2px dashed var(--sai-primary-color);
    background: rgba(20, 94, 5, 0.05);
}

.sai-chat-container.sai-drag-over::before {
    content: 'Suelta los archivos aquí';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 18px;
    font-weight: 600;
    color: var(--sai-primary-color);
    z-index: 1000;
    pointer-events: none;
}

/* Badges de archivos en mensajes */
.sai-message-files {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.sai-file-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    font-size: 11px;
    font-weight: 500;
}

.sai-message-user .sai-file-badge {
    background: rgba(255, 255, 255, 0.3);
    color: white;
}

/* Botón TTS (Text-to-Speech) */
.sai-tts-button {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.05);
    border: none;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
    z-index: 10;
}

.sai-tts-button:hover {
    background: rgba(0, 0, 0, 0.1);
    transform: scale(1.1);
}

.sai-tts-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.sai-message-bot {
    position: relative;
    padding-right: 40px; /* Espacio para botón TTS */
}

/* Estado de procesamiento multimedia */
.sai-loading-bubble {
    font-style: italic;
    color: #666;
}

/* Responsive para archivos múltiples */
@media (max-width: 480px) {
    .sai-files-preview {
        gap: 6px;
    }
    
    .sai-file-preview-item {
        max-width: 140px;
        padding: 6px 10px;
        font-size: 11px;
    }
    
    .sai-file-preview-item img {
        width: 32px;
        height: 32px;
    }
}

/* Soporte para imágenes generadas en mensajes */
.sai-generated-image {
    max-width: 100%;
    border-radius: 8px;
    margin: 10px 0;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Reproductor de audio inline */
.sai-audio-player {
    width: 100%;
    margin: 10px 0;
    border-radius: 20px;
}