/* =========================================
   1. VARIABLES Y RESET GENERAL
   ========================================= */
:root {
    --bg-color: #FFFFFF;
    --sidebar-bg-color: #F9FAFB;
    
    /* CAMBIO: Fondo del asistente transparente */
    --message-assistant-bg-color: transparent;
    
    /* CAMBIO: Fondo del usuario gris suave (antes verde) */
    --message-user-bg-color: #E5E7EB;
    
    --border-color: #E5E7EB;
    --text-color: #1F2937;
    --text-color-light: #111827;
    --text-color-subtle: #6B7280;
    --hover-bg-color: #E5E7EB;
    --primary-color: #10B981;
    --accent-color: #7a59f5;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    overflow: hidden;
}

/* =========================================
   2. LAYOUT PRINCIPAL (APP)
   ========================================= */
#app-layout {
    display: flex;
    height: 100vh;
    position: relative;
}

/* --- SIDEBAR --- */
#sidebar {
    /* CAMBIO: Más ancho para que se vea más profesional */
    width: 300px; 
    background-color: #F9FAFB;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #E5E7EB;
    flex-shrink: 0;
    z-index: 50;
    transition: transform 0.3s ease-in-out;
}

#sidebar-header {
    padding: 1.25rem 1rem 1rem 1rem;
    border-bottom: none; 
}

/* Logo Centrado */
.logo-container {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1.5rem;
    padding-left: 0;
}

.logo-container img {
    height: 28px;
    width: auto;
}

/* BOTÓN NUEVO CHAT */
#new-chat-btn {
    width: 100%;
    padding: 0.75rem 1rem;
    background-color: #FFFFFF;
    border: 1px solid #E5E7EB;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    border-radius: 8px;
    color: #374151;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    text-align: left;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 10px;
}

#new-chat-btn::before {
    content: '\f573'; /* Código Unicode de fa-file-signature */
    font-family: "Font Awesome 5 Free";
    font-weight: 900; /* Solid */
    font-size: 0.95rem; /* Un pelín más pequeño para que quede fino */
    color: #4B5563;
    margin-right: 6px; /* Un poco más de aire con el texto */
}

/* Efecto Hover: Oscurecer el icono al pasar el ratón */
#new-chat-btn:hover::before {
    color: #111827;
}

#new-chat-btn:hover {
    background-color: #FFFFFF;
    border-color: #D1D5DB;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    color: #111827;
}

/* NAVEGACIÓN E HISTORIAL */
#history-nav {
    flex-grow: 1;
    overflow-y: auto;
    padding: 0.5rem 0.75rem;
    margin-top: 0.5rem;
}

#history-nav::-webkit-scrollbar { width: 5px; }
#history-nav::-webkit-scrollbar-track { background: transparent; }
#history-nav::-webkit-scrollbar-thumb { background-color: #E5E7EB; border-radius: 20px; }
#history-nav:hover::-webkit-scrollbar-thumb { background-color: #D1D5DB; }

#history-nav p {
    margin: 0 0 0.5rem 0.5rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #9CA3AF;
    font-weight: 600;
}

#history-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

/* CONTROLES (La solución al salto) */
.history-item-controls {
    /* CAMBIO: Posición absoluta para flotar ENCIMA sin empujar */
    position: absolute;
    right: 8px; /* Pegado a la derecha */
    top: 50%;
    transform: translateY(-50%); /* Centrado vertical perfecto */
    
    display: flex; /* Siempre flex, ocultamos con opacidad */
    align-items: center;
    gap: 2px;
    
    opacity: 0; /* Invisible por defecto */
    transition: opacity 0.2s ease;
    
    /* Fondo para tapar el texto que haya debajo */
    background: linear-gradient(90deg, rgba(229,231,235,0) 0%, #E5E7EB 20%);
    padding-left: 10px;
    border-radius: 0 8px 8px 0;
}

/* Ajuste de fondo específico para cuando el item está activo */
.history-item.active .history-item-controls {
    background: linear-gradient(90deg, rgba(243,244,246,0) 0%, #F3F4F6 20%);
}

.history-item:hover .history-item-controls,
.history-item.active:hover .history-item-controls {
    opacity: 1; /* Aparece suavemente */
}

/* En móvil o táctil, mostramos siempre los del activo */
.history-item.active .history-item-controls {
    opacity: 1; 
}

.control-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 0.8rem;
    padding: 4px;
    color: #6B7280; /* Un poco más oscuro para que se vea bien */
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s, background-color 0.2s;
}

.control-btn:hover {
    background-color: #FFFFFF;
    color: #111827;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

/* FOOTER */
#sidebar-footer {
    padding: 1rem;
    border-top: 1px solid #E5E7EB;
    background-color: #F9FAFB;
}

#logout-btn {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: none;
    border-radius: 8px;
    background-color: transparent;
    color: #4B5563;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    text-align: left;
    display: flex;
    align-items: center;
    gap: 10px;
}

#logout-btn::before {
    content: '\f2f5'; 
    font-family: "Font Awesome 5 Free";
    font-weight: 900;
    font-size: 0.9rem;
    opacity: 0.7;
}

#logout-btn:hover {
    background-color: #000000; /* Negro corporativo */
    color: #FFFFFF;            /* Texto blanco */
}

/* =========================================
   3. ÁREA DE CHAT (Main)
   ========================================= */
#chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: relative;
    flex-grow: 1;
    background-color: var(--bg-color);
}

header { display: none; }
#mobile-header {
    display: none;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    background-color: var(--bg-color);
}

#menu-toggle-btn {
    background: none;
    border: none;
    font-size: 1.25rem;
    cursor: pointer;
    color: var(--text-color);
}

/* --- CAJA DE MENSAJES --- */
#chat-box {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    padding: 2rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    box-sizing: border-box;
}

/* Estilos de Scrollbar para el Chat (Igual que Sidebar) */
#chat-box::-webkit-scrollbar {
    width: 5px;
}
#chat-box::-webkit-scrollbar-track {
    background: transparent;
}
#chat-box::-webkit-scrollbar-thumb {
    background-color: #E5E7EB;
    border-radius: 20px;
}
#chat-box:hover::-webkit-scrollbar-thumb {
    background-color: #D1D5DB;
}

.message-wrapper {
    display: flex;
    gap: 12px;
    width: 100%;
    align-items: flex-start;
}

.user-wrapper {
    flex-direction: row-reverse;
}

/* CAMBIO: Avatar del usuario en gris */
.user-message-content {
    background-color: var(--message-user-bg-color);
    border-radius: 12px 12px 0 12px;
    margin-left: auto;
}

.assistant-wrapper {
    display: grid;
    grid-template-columns: 36px minmax(0, 1fr); 
    gap: 0 12px;
}

.assistant-avatar {
    grid-column: 1;
    grid-row: 1;
    background-color: white; 
    padding: 2px;
}

.avatar {
    width: 36px;
    height: 36px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.avatar img {
    width: 100%;
    height: 100%;
    border-radius: 4px;
    object-fit: contain;
}

.assistant-message-content {
    grid-column: 2;
    grid-row: 1;
    background-color: var(--message-assistant-bg-color);
    border-radius: 12px 12px 12px 0;
    border: 1px solid transparent;
}

.message-content {
    padding: 0.75rem 1rem;
    line-height: 1.6;
    font-size: 0.95rem;
    color: var(--text-color);
    width: fit-content;
    max-width: 100%;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
}

/* Markdown Básico */
.message-content p { margin: 0 0 0.75rem 0; }
.message-content p:last-child { margin: 0; }
.message-content ul, .message-content ol { padding-left: 1.5rem; margin: 0.5rem 0; }
.message-content code {
    background: rgba(0,0,0,0.05);
    padding: 2px 4px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.9em;
    color: #e01e5a;
}

/* =========================================
   BLOQUES DE CÓDIGO (SOLUCIÓN DEFINITIVA)
   ========================================= */

/* 1. El contenedor principal del bloque */
.code-block-wrapper {
    margin: 12px 0;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #4b5563; /* Borde gris medio */
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

/* 2. La cabecera (Header) - Gris Oscuro (#2d2d2d) */
.code-block-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #2d2d2d !important; /* Gris oscuro, no negro */
    padding: 8px 12px;
    color: #e5e7eb;
    font-family: sans-serif;
    font-size: 0.75rem;
    border-bottom: 1px solid #404040;
}

/* 3. El cuerpo del código (PRE) - Gris Medio (#383838) */
.code-block-wrapper pre {
    background-color: #383838 !important; /* FORZAMOS EL GRIS */
    color: #f3f4f6 !important;
    margin: 0 !important;
    padding: 16px 20px !important;
    border: none !important;
    border-radius: 0 !important;
    overflow-x: auto;
    width: 100%;
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    display: block;
    box-sizing: border-box;
}

/* Scrollbars para el código */
.code-block-wrapper pre::-webkit-scrollbar {
    height: 8px;
}
.code-block-wrapper pre::-webkit-scrollbar-thumb {
    background-color: #4a4a4a;
    border-radius: 4px;
}
.code-block-wrapper pre::-webkit-scrollbar-track {
    background-color: #2d2d2d;
}

/* 4. ¡LA CLAVE! El texto interno (CODE) */
/* A veces este elemento tiene fondo negro por defecto y tapa al gris */
.code-block-wrapper pre code {
    background-color: transparent !important; /* Que sea transparente para ver el gris del padre */
    color: #f3f4f6 !important; /* Texto claro */
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.9rem;
    padding: 0;
    border: none;
}

/* 5. El botón de copiar */
.copy-code-btn {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 4px;
    color: #d1d5db;
    cursor: pointer;
    font-size: 0.75rem;
    padding: 4px 8px;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s;
}

.copy-code-btn:hover {
    background-color: rgba(255,255,255,0.1);
    color: #ffffff;
}

/* --- BOTONES DE FEEDBACK Y ACCIONES --- */
.message-actions {
    grid-column: 2;
    grid-row: 2;
    display: flex;
    gap: 6px;
    padding-top: 6px;
    padding-left: 2px;
    width: 100%; /* Asegura que ocupe todo el ancho para poder empujar el botón */
}

.feedback-btn {
    background: transparent;
    border: 1px solid transparent;
    cursor: pointer;
    font-size: 0.85rem;
    color: #9CA3AF;
    padding: 4px 6px;
    border-radius: 6px;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.feedback-btn:hover {
    background-color: rgba(0,0,0,0.05);
    color: var(--text-color);
    transform: scale(1.05);
}

.feedback-btn.selected { transform: scale(1); cursor: default; }
.feedback-btn.selected[data-type="positive"] { color: #10B981; background-color: rgba(16, 185, 129, 0.1); }
.feedback-btn.selected[data-type="negative"] { color: #EF4444; background-color: rgba(239, 68, 68, 0.1); }

/* --- ESTADO "PENSANDO..." --- */
.thinking-state {
    color: #6b7280;
    font-style: italic;
    font-size: 0.95rem;
    display: flex;
    align-items: center;
    gap: 8px;
    animation: pulse 1.5s infinite ease-in-out;
}

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

.thinking-state i { font-size: 0.9rem; }

/* --- FUENTES Y CITAS (CHIPS) --- */
.sources-container {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #e5e7eb;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.sources-label {
    font-size: 0.7rem;
    color: #6b7280;
    width: 100%;
    margin-bottom: 4px;
    font-weight: 700;
    text-transform: uppercase;
}

.source-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background-color: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 6px 10px;
    text-decoration: none;
    color: #374151;
    font-size: 0.8rem;
    transition: all 0.2s ease;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    max-width: 100%;
}

.source-chip:hover {
    background-color: #f9fafb;
    border-color: #d1d5db;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0,0,0,0.08);
}

.source-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 200px;
}

/* =========================================
   4. FOOTER (Estilo Gemini)
   ========================================= */
footer {
    flex-shrink: 0;
    width: 100%;
    background: linear-gradient(180deg, rgba(255,255,255,0) 0%, #ffffff 50%);
    padding-bottom: 1.5rem;
    position: relative;
    z-index: 20;
}

#chat-form-wrapper {
    max-width: 800px;
    margin: 0 auto;
    padding: 0 1rem;
    display: flex;
    flex-direction: column;
}

/* --- LA CÁPSULA PRINCIPAL --- */
.input-group-container {
    background-color: #ffffff;
    border-radius: 28px;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    border: 1px solid #e5e7eb;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    transition: all 0.2s ease;
}

.input-group-container:focus-within {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
    border-color: #d1d5db;
}

/* --- DRAG & DROP --- */
.drag-active-zone {
    border: 2px dashed #0b57d0 !important;
    background-color: #f0f8ff !important;
    transform: scale(1.01);
}
.drag-active-zone * { pointer-events: none; }

/* --- INPUT DE TEXTO --- */
#user-input {
    width: 100%;
    border: none;
    background: transparent;
    resize: none;
    font-size: 1rem;
    color: var(--text-color);
    padding: 4px 0 12px 4px;
    outline: none;
    line-height: 1.5;
    max-height: 200px;
    font-family: inherit;
}

/* --- BARRA DE HERRAMIENTAS --- */
.input-tools-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.tools-left { display: flex; align-items: center; }

#attach-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    color: #1f1f1f;
    transition: transform 0.2s;
}

.icon-circle {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
}

#attach-btn:hover .icon-circle { background-color: rgba(0,0,0,0.06); }

.tools-right { display: flex; align-items: center; gap: 12px; }

/* --- SELECTOR DE MODELO --- */
#model-selector-wrapper { position: relative; }

.model-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    background-color: transparent;
    border: none;
    font-size: 0.85rem;
    font-weight: 600;
    color: #444746;
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 8px;
    transition: background-color 0.2s;
}

.model-pill:hover { background-color: rgba(0,0,0,0.06); }
.model-pill .arrow-icon { font-size: 0.7rem; margin-top: 2px; }

.model-menu {
    position: absolute;
    bottom: 120%;
    right: 0;
    width: 260px;
    background-color: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.15), 0 8px 24px rgba(0,0,0,0.1);
    padding: 8px;
    z-index: 100;
    border: 1px solid #e0e0e0;
}

.model-option {
    display: flex;
    align-items: center;
    padding: 10px;
    border-radius: 8px;
    cursor: pointer;
    color: #1f1f1f;
}

.model-option:hover { background-color: #f2f2f2; }
.model-option.selected { background-color: #e8f0fe; color: #0b57d0; }

.option-icon { margin-right: 12px; display: flex; align-items: center; }
.option-details { flex: 1; }
.option-title { font-size: 0.9rem; font-weight: 600; display: block; }
.option-desc { font-size: 0.75rem; color: #757575; }
.model-option.selected .option-desc { color: #0b57d0; }
.check-icon { display: none; font-size: 0.9rem; }
.model-option.selected .check-icon { display: block; }

/* --- BOTÓN ENVIAR --- */
#send-btn {
    background-color: transparent;
    border: none;
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #1f1f1f;
    border-radius: 50%;
    transition: all 0.2s ease;
}

#send-btn:hover {
    background-color: #000000;
    color: #ffffff;
    transform: scale(1.05);
}

/* Estado del botón cuando está generando (Botón Stop) */
#send-btn.generating {
    background-color: #ef4444 !important; /* Rojo */
    color: white !important;
}

#send-btn.generating:hover {
    background-color: #dc2626 !important; /* Rojo más oscuro */
    transform: scale(1.05);
}

/* --- ARCHIVOS ADJUNTOS (CHIPS) --- */
#file-preview-container {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    padding: 0 4px 10px 4px;
    max-height: 150px;
    overflow-y: auto;
}

.file-preview-item {
    display: flex;
    align-items: center;
    background-color: #ffffff;
    border: 1px solid #e0e0e0;
    border-radius: 12px;
    padding: 8px 12px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    transition: all 0.2s ease;
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    max-width: 220px;
}

.file-preview-item:hover { transform: translateY(-2px); box-shadow: 0 4px 8px rgba(0,0,0,0.08); }
.file-preview-item .file-icon { font-size: 1.2rem; margin-right: 10px; }
.file-preview-item .file-details { display: flex; flex-direction: column; overflow: hidden; margin-right: 10px; }
.file-preview-item .file-name { font-size: 0.85rem; font-weight: 600; color: #333; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-preview-item .file-size { font-size: 0.7rem; color: #888; }
.file-preview-item .remove-file-btn { background: #f3f4f6; border: none; border-radius: 50%; width: 20px; height: 20px; display: flex; align-items: center; justify-content: center; cursor: pointer; font-size: 1rem; color: #666; line-height: 1; transition: all 0.2s; }
.file-preview-item .remove-file-btn:hover { background-color: #fee2e2; color: #ef4444; }

@keyframes popIn { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } }

.disclaimer { text-align: center; font-size: 0.7rem; color: #757575; margin-top: 12px; }

/* =========================================
   5. LOGIN & AUTH
   ========================================= */
#auth-container {
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #F3F4F6;
    background-image: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('/static/assets/background.jpg');
    background-size: cover;
    background-position: center;
}

.auth-form-container {
    width: 100%;
    max-width: 400px;
    padding: 2.5rem;
    background-color: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    text-align: center;
    position: relative;
}

.logo-auth { margin-bottom: 2rem; }
.logo-auth img { height: 40px; width: auto; }
.auth-form-container h2 { margin: 0 0 1rem 0; color: #111827; }
.auth-form-container p { color: #6B7280; margin-bottom: 2rem; }

.auth-btn {
    display: block; width: 100%; padding: 0.75rem; background-color: #1F2937; color: white; border: none; border-radius: 6px; font-weight: 600; text-decoration: none; cursor: pointer; transition: background-color 0.2s; margin-bottom: 1rem; box-sizing: border-box;
}
.auth-btn:hover { background-color: #111827; }
.auth-btn.admin { background-color: transparent; color: #4B5563; border: 1px solid #D1D5DB; }
.auth-btn.admin:hover { background-color: #F3F4F6; color: #111827; }

#language-switcher {
    position: absolute; top: 1rem; right: 1rem; display: flex; gap: 4px; background: rgba(255,255,255,0.5); padding: 4px; border-radius: 6px;
}
#language-switcher button { background: none; border: none; padding: 4px 8px; font-size: 0.8rem; font-weight: 600; color: #6B7280; cursor: pointer; border-radius: 4px; }
#language-switcher button.active { background-color: #fff; color: #111827; box-shadow: 0 1px 2px rgba(0,0,0,0.1); }

/* =========================================
   6. RESPONSIVE
   ========================================= */
.hidden { display: none !important; }
#overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 40; backdrop-filter: blur(2px); }

@media (max-width: 768px) {
    #app-layout { flex-direction: column; }
    #sidebar { position: fixed; height: 100%; transform: translateX(-100%); }
    #app-layout.sidebar-visible #sidebar { transform: translateX(0); }
    #mobile-header { display: flex; justify-content: space-between; }
    #chat-box { padding: 1rem; }
    .message-content { max-width: 90%; }
    .assistant-wrapper { grid-template-columns: 32px minmax(0, 1fr); }
    .avatar, .assistant-avatar { width: 32px; height: 32px; }
}

/* =========================================
   FOTO DE PERFIL REDONDA Y MEJORADA
   ========================================= */
.user-avatar {
    /* DIMENSIONES FIJAS */
    width: 42px !important;
    height: 42px !important;
    
    /* LA CLAVE: Esto impide que el flexbox lo "aplaste" de los lados */
    min-width: 42px !important; 
    
    /* FORMA */
    border-radius: 50% !important; /* Círculo matemático */
    
    /* LIMPIEZA */
    padding: 0 !important;
    border: none !important;
    margin-right: 10px !important; /* Separación con el texto */
    
    /* CONTENIDO */
    overflow: hidden !important; /* Recorta lo que sobre */
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    flex-shrink: 0 !important; /* Doble seguridad anti-encogimiento */
    
    /* Sombra sutil para que destaque sobre el fondo */
    box-shadow: 0 2px 5px rgba(0,0,0,0.1); 
}

.user-profile-pic {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover !important;
    object-position: center top !important; /* Enfocamos un poco más arriba para la cara */
    display: block !important;
    border-radius: 0 !important;
}

/* =========================================
   AVATAR ASISTENTE (VEN) - REDONDO
   ========================================= */
/* 1. El contenedor circular (El fondo negro) */
.assistant-avatar {
    /* DIMENSIONES TOTALES (Incluyendo el margen) */
    width: 42px !important;
    height: 42px !important;
    min-width: 42px !important;
    flex-shrink: 0 !important;
    
    /* FORMA */
    border-radius: 50% !important;
    overflow: hidden !important;
    
    /* ESTILO */
    /* AQUÍ ESTÁ LA CLAVE: Añadimos relleno interno */
    padding: 6px !important; /* Ajusta este valor si quieres más/menos margen */
    
    /* El color de fondo del favicon parece ser negro/oscuro, 
       así que lo ponemos negro para que haga de marco. */
    background-color: #000000 !important; 
    
    border: none !important;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1); 
    
    /* CENTRADO */
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    box-sizing: border-box !important; /* Vital para que el padding no aumente el tamaño total */
}

/* 2. La imagen del logo dentro (La "V") */
.assistant-avatar img {
    width: 100% !important;
    height: 100% !important;
    /* 'contain' para que la V entera quepa dentro del espacio disponible */
    object-fit: contain !important; 
    border-radius: 0 !important;
}

/* =========================================
   ESTILO DE TABLAS PROFESIONAL
   ========================================= */

/* 1. Contenedor para Scroll Horizontal */
.table-container {
    width: 100%;
    overflow-x: auto; /* Permite scroll si la tabla es gigante */
    margin: 1.5rem 0;
    border-radius: 8px; /* Bordes redondeados del contenedor */
    border: 1px solid #E5E7EB;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05); /* Sombrita sutil */
}

/* 2. La Tabla en sí */
.message-content table {
    width: 100%;
    border-collapse: collapse; /* Une los bordes para que sean finos */
    font-size: 0.9rem; /* Un pelín más pequeño que el texto normal queda mejor */
    background-color: #ffffff;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* 3. Cabecera (Header) */
.message-content th {
    background-color: #F9FAFB; /* Gris muy suave */
    color: #111827; /* Texto oscuro */
    font-weight: 600;
    text-align: left;
    padding: 12px 16px; /* Mucho aire para que no se pegue */
    border-bottom: 2px solid #E5E7EB;
    white-space: nowrap; /* IMPORTANTE: Evita que los títulos se partan como "Caracte rística" */
    min-width: 120px;
}

/* 4. Celdas del cuerpo */
.message-content td {
    padding: 12px 16px;
    color: #374151;
    border-bottom: 1px solid #E5E7EB;
    vertical-align: top; /* Alineación arriba para lectura fácil */
    line-height: 1.5;
    min-width: 180px; /* Obliga a que la celda sea ancha. Adiós al texto aplastado. */
    word-wrap: break-word;
}

/* 5. Efecto Cebra (Filas alternas) */
.message-content tr:nth-child(even) {
    background-color: #F9FAFB; /* Gris clarito cada dos filas */
}

/* 6. Hover (Resaltar fila al pasar ratón) */
.message-content tr:hover {
    background-color: #F3F4F6;
}

/* 7. Scrollbar personalizada para la tabla */
.table-container::-webkit-scrollbar {
    height: 6px; /* Barra horizontal fina */
}
.table-container::-webkit-scrollbar-thumb {
    background-color: #D1D5DB;
    border-radius: 10px;
}
.table-container::-webkit-scrollbar-track {
    background: transparent;
}

/* =================================================================
   1. ESTILOS DEL HISTORIAL (SWAP + MENÚ FLOTANTE) - VERSIÓN FINAL
   ================================================================= */

.history-item {
    position: relative;
    display: flex;
    justify-content: space-between; /* Mantiene título e iconos separados */
    align-items: center;
    
    /* --- MEDIDAS ORIGINALES DE PRODUCTIVO --- */
    padding: 0.6rem 0.75rem;  /* Volvemos al relleno grande */
    gap: 10px;                /* Separación interna */
    
    /* Quitamos el min-height forzado para que el padding mande */
    /* min-height: 34px;  <-- ELIMINADO */
    
    font-size: 0.85rem;
    border-radius: 8px;       /* Productivo usa 8px, no 6px */
    cursor: pointer;
    color: #4B5563;
    border: 1px solid transparent; /* Mantiene la estructura para que no salte */
    
    transition: background-color 0.2s ease, color 0.2s ease;
    
    /* VITAL: Mantenemos esto para que el menú de 3 puntos no se corte */
    overflow: visible; 
}

/* Título del Chat */
.history-item-title {
    flex-grow: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
    margin-right: 4px;
}

/* Contenedor de Acciones (Derecha) */
.history-actions {
    /* CLAVE: Medidas fijas y cuadradas para evitar el óvalo aplastado */
    width: 24px; 
    height: 24px;
    
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0; 
    
    /* El anclaje para el menú desplegable */
    position: relative; 
}

/* A. La Chincheta (Estilo Contorno) */
.pinned-icon {
    font-size: 0.8rem;
    transform: rotate(45deg);
    display: block;
    color: transparent; 
    -webkit-text-stroke: 1.5px #374151; 
}

/* B. El Botón de 3 Puntos */
.history-menu-btn {
    background: transparent;
    border: none;
    color: #374151;
    cursor: pointer;
    font-size: 0.9rem;
    padding: 0;
    
    /* AL SER EL PADRE 24x24, ESTO CREA UN CÍRCULO PERFECTO */
    border-radius: 50%;
    width: 100%;
    height: 100%;
    
    display: none; /* Oculto por defecto */
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
}

.history-menu-btn:hover, .history-menu-btn.active {
    background-color: #D1D5DB; /* Gris del círculo */
    color: #000;
}

/* --- LÓGICA DE INTERCAMBIO (SWAP) --- */
/* 1. Si está fijado: Hover oculta pin, muestra menú */
.history-item.pinned-item:hover .pinned-icon { display: none; }
.history-item.pinned-item:hover .history-menu-btn { display: flex; }

/* 2. Si no está fijado: Hover muestra menú */
.history-item:hover .history-menu-btn { display: flex; }

/* 3. Si menú abierto: Mostrar menú siempre */
.history-menu-btn.active { display: flex !important; }
.history-item:has(.history-menu-btn.active) .pinned-icon { display: none; }


/* --- EL MENÚ DESPLEGABLE (Dropdown) --- */
.history-dropdown {
    position: absolute;
    top: 100%; /* Justo debajo del botón */
    right: 0;
    width: 150px;
    background-color: #FFFFFF;
    border: 1px solid #E5E7EB;
    border-radius: 8px;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    z-index: 9999; /* ¡Encima de todo! */
    padding: 4px;
    display: none; /* Oculto */
    flex-direction: column;
}

.history-dropdown.show { 
    display: flex; 
    animation: fadeIn 0.1s ease-out; 
}

.dropdown-item {
    background: transparent;
    border: none;
    text-align: left;
    padding: 8px 12px;
    font-size: 0.85rem;
    color: #374151;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
}
.dropdown-item:hover { background-color: #F3F4F6; color: #111827; }
.dropdown-item.delete { color: #EF4444; }
.dropdown-item.delete:hover { background-color: #FEF2F2; }


/* =================================================================
   2. LISTA DE VENAPPS (SIDEBAR) - VERSIÓN PULIDA
   ================================================================= */

/* Contenedor Principal */
#venapps-container {
    padding: 10px 14px;
    border-bottom: 1px solid #E5E7EB;
    background-color: #F9FAFB;
}

/* Cabecera "MIS ASISTENTES" */
.venapps-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    font-weight: 700;
    color: #6B7280;
    text-transform: uppercase;
    margin-bottom: 8px;
    letter-spacing: 0.05em;
}

/* Botón [+] */
#create-venapp-btn {
    background: none;
    border: none;
    color: var(--primary-color);
    cursor: pointer;
    font-size: 0.9rem;
    padding: 2px 6px;
    border-radius: 4px;
    transition: background 0.2s;
}
#create-venapp-btn:hover { background-color: #E5E7EB; }

/* Lista Scrollable */
#venapps-list {
    display: flex;
    flex-direction: column;
    gap: 2px; /* Gap pequeño para compactar */
    max-height: 150px;
    overflow-y: auto;
}

/* --- ITEM INDIVIDUAL (BOT) --- */
.venapp-item {
    display: flex;
    align-items: center;
    
    /* Dimensiones compactas (Igual que el historial) */
    padding: 6px 10px; 
    min-height: 32px;
    
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    color: #374151;
    transition: all 0.2s;
    border: 1px solid transparent;
}

.venapp-item:hover {
    background-color: #FFFFFF;
    border-color: #E5E7EB;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.venapp-item.active {
    background-color: #EEF2FF; /* Azul muy suave */
    color: #4F46E5;            /* Indigo */
    border-color: #C7D2FE;
    font-weight: 600;
}

/* Icono Emoji */
.venapp-icon {
    margin-right: 10px;
    font-size: 1.1rem;
    line-height: 1;
    width: 20px; /* Ancho fijo para alinear */
    text-align: center;
}

/* --- FIX LOGO GIGANTE --- */
.venapp-logo-img {
    width: 20px !important;
    height: 20px !important;
    min-width: 20px !important;
    object-fit: contain;
    margin-right: 10px;
    display: block;
}

/* Nombre del Bot */
.venapp-name {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Botón de Borrar (X) */
.venapp-delete {
    visibility: hidden; /* Oculto hasta hover */
    background: transparent;
    border: none;
    color: #9CA3AF;
    cursor: pointer;
    
    /* Tamaño y centrado */
    font-size: 1.1rem;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    
    border-radius: 4px;
    transition: all 0.2s;
}

/* Mostrar X al pasar el ratón sobre la fila */
.venapp-item:hover .venapp-delete {
    visibility: visible;
}

.venapp-delete:hover {
    background-color: #FEE2E2;
    color: #EF4444;
}

/* =================================================================
   3. MODAL PROFESIONAL (NUEVO CREADOR)
   ================================================================= */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.25); 
    backdrop-filter: blur(2px); /* El desenfoque ayuda a leer aunque el fondo sea claro */
    z-index: 2000; display: flex; justify-content: center; align-items: center;
}

/* CLASE UTILITARIA VITAL (Asegúrate de no haberla borrado antes) */
.hidden { display: none !important; }

.professional-modal {
    width: 600px;
    max-width: 95%;
    border-radius: 16px;
    background-color: #FFFFFF;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    display: flex;
    flex-direction: column;
    font-family: inherit;
    animation: slideUp 0.3s ease-out;
}

/* Header Modal */
.modal-header-pro {
    padding: 24px 24px 0 24px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}
.header-text h3 { margin: 0; font-size: 1.25rem; font-weight: 700; color: #111827; }
.header-text p { margin: 4px 0 0 0; color: #6B7280; font-size: 0.9rem; }
.close-modal-btn { font-size: 1.5rem; color: #9CA3AF; background: transparent; border: none; cursor: pointer; }

/* Body Modal */
.modal-body-pro { padding: 24px; }
.pro-section { margin-bottom: 24px; }
.section-label { display: block; font-size: 0.8rem; font-weight: 700; color: #374151; text-transform: uppercase; margin-bottom: 8px; }

/* Inputs Identidad */
.identity-row { display: flex; gap: 12px; margin-bottom: 10px; }
.emoji-picker-wrapper input {
    width: 48px; height: 48px; font-size: 1.5rem; text-align: center;
    border: 1px solid #E5E7EB; border-radius: 10px; background-color: #F9FAFB;
}
.name-input-wrapper { flex: 1; }
.name-input-wrapper input {
    width: 100%; height: 48px; padding: 0 16px; font-size: 1rem;
    border: 1px solid #E5E7EB; border-radius: 10px; box-sizing: border-box;
}
.desc-input {
    width: 100%; padding: 10px 12px; font-size: 0.9rem;
    border: 1px solid transparent; background-color: #F9FAFB; border-radius: 8px; box-sizing: border-box;
}
.desc-input:focus, .name-input-wrapper input:focus, .emoji-picker-wrapper input:focus {
    background-color: #FFF; border-color: #6366f1; outline: none;
}

/* Prompt Area */
.label-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.badge-optional { font-size: 0.7rem; background: #EEF2FF; color: #4F46E5; padding: 2px 8px; border-radius: 12px; }
.prompt-container { border: 1px solid #E5E7EB; border-radius: 10px; overflow: hidden; }
.prompt-container textarea {
    width: 100%; border: none; padding: 16px; font-family: sans-serif; font-size: 0.95rem; resize: vertical; outline: none; min-height: 120px; box-sizing: border-box;
}
.prompt-tips { background-color: #F9FAFB; padding: 8px 16px; font-size: 0.75rem; color: #6B7280; border-top: 1px solid #E5E7EB; }

/* Settings (RAG Switch) */
.settings-box { background-color: #F9FAFB; border: 1px solid #E5E7EB; border-radius: 10px; padding: 16px; }
.setting-row { display: flex; justify-content: space-between; align-items: center; }
.setting-title { font-weight: 600; font-size: 0.9rem; color: #111827; }
.setting-desc { font-size: 0.8rem; color: #6B7280; display: block; }

.switch-pro { position: relative; display: inline-block; width: 44px; height: 24px; }
.switch-pro input { opacity: 0; width: 0; height: 0; }
.slider-pro { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #E5E7EB; transition: .3s; border-radius: 34px; }
.slider-pro:before { position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px; background-color: white; transition: .3s; border-radius: 50%; box-shadow: 0 1px 2px rgba(0,0,0,0.15); }
input:checked + .slider-pro { background-color: #10B981; }
input:checked + .slider-pro:before { transform: translateX(20px); }

/* Footer Modal */
.modal-footer-pro { display: flex; justify-content: flex-end; gap: 12px; margin-top: 10px; padding-top: 10px; border-top: 1px solid #F3F4F6; }
.btn-ghost { padding: 10px 20px; background: transparent; color: #4B5563; border: none; cursor: pointer; }
.btn-primary-pro { padding: 10px 24px; background-color: #111827; color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; }
.btn-primary-pro:hover { background-color: #000; }

@keyframes slideUp { from {transform: translateY(20px); opacity: 0;} to {transform: translateY(0); opacity: 1;} }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: translateY(0); } }

/* --- ESTILOS PARA EL PICKER DE EMOJIS (NUEVO) --- */
.emoji-picker-container {
    position: absolute;
    top: 100%;
    left: 0;
    background: white;
    border: 1px solid #E5E7EB;
    border-radius: 12px;
    box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1);
    padding: 10px;
    display: grid;
    grid-template-columns: repeat(5, 1fr); /* 5 columnas */
    gap: 5px;
    z-index: 3000;
    width: 200px;
    margin-top: 8px;
}
.emoji-option {
    font-size: 1.2rem;
    padding: 5px;
    cursor: pointer;
    text-align: center;
    border-radius: 6px;
    transition: background 0.2s;
}
.emoji-option:hover { background-color: #F3F4F6; transform: scale(1.2); }

.venapp-logo-img {
    width: 20px !important;       /* Tamaño pequeño fijo */
    height: 20px !important;
    min-width: 20px !important;   /* Evita que se aplaste */
    object-fit: contain;          /* Mantiene proporción */
    margin-right: 10px;           /* Espacio con el texto */
    display: block;
}

/* =================================================================
   REGLAS DE CAPAS (SOLUCIÓN MENÚ OCULTO)
   ================================================================= */

/* 1. Item Activo (Seleccionado) */
/* Nivel 10: Está por encima de los normales, pero por debajo de tu ratón */
.history-item.active {
    background-color: #F3F4F6; /* Gris muy suave (Original Productivo) */
    color: #111827;
    font-weight: 600;
    z-index: 10; 
}

/* 2. Item en Hover (Pasando el ratón) */
/* Nivel 50: ¡IMPORTANTE! Gana al activo para que el menú flote bien */
.history-item:hover {
    background-color: #E5E7EB; /* Gris hover un poco más oscuro */
    color: #111827;
    z-index: 50; 
}

/* 3. Seguridad Total: Si tiene el menú abierto */
/* Nivel 100: Máxima prioridad (Funciona en navegadores modernos) */
.history-item:has(.history-dropdown.show) {
    z-index: 100;
    background-color: #E5E7EB; /* Mantener color de hover */
}
/* =========================================
   MODAL DE CONFIRMACIÓN (CUSTOM ALERT)
   ========================================= */

/* Reutilizamos .modal-overlay, pero definimos la caja específica */
.confirmation-box {
    background: white;
    padding: 24px;
    border-radius: 12px;
    width: 100%;
    max-width: 320px; /* Pequeño y coqueto */
    text-align: center;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    animation: popIn 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.confirm-icon {
    width: 48px;
    height: 48px;
    background-color: #FEE2E2; /* Rojo muy suave */
    color: #DC2626; /* Rojo peligro */
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px auto;
    font-size: 1.2rem;
}

.confirmation-box h3 {
    margin: 0 0 8px 0;
    color: #111827;
    font-size: 1.1rem;
    font-weight: 600;
}

.confirmation-box p {
    margin: 0 0 24px 0;
    color: #6B7280;
    font-size: 0.9rem;
    line-height: 1.4;
}

.confirm-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.confirm-actions button {
    flex: 1;
    padding: 10px;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
}

/* Botón Cancelar (Gris) */
.btn-ghost {
    background-color: transparent;
    color: #374151;
    border: 1px solid #E5E7EB;
}
.btn-ghost:hover {
    background-color: #F9FAFB;
}

/* Botón Eliminar (Rojo) */
.btn-danger {
    background-color: #000000; /* Negro puro */
    color: white;
    border: none;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.btn-danger:hover {
    background-color: #333333; /* Gris muy oscuro al pasar el ratón */
    transform: translateY(-1px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

const translations = {
    es: {
        // ... tus otras claves ...
        confirmTitle: "Confirmar acción",
        btnCancel: "Cancelar",
        btnDelete: "Eliminar",
        // Asegúrate de tener deleteConfirm configurado:
        deleteConfirm: "¿Estás seguro de que quieres borrar esta conversación?",
        // ...
    },
    en: {
        // ... tus otras claves ...
        confirmTitle: "Confirm Action",
        btnCancel: "Cancel",
        btnDelete: "Delete",
        // Asegúrate de tener deleteConfirm configurado:
        deleteConfirm: "Are you sure you want to delete this conversation?",
        // ...
    }
};
// Función Genérica para pedir confirmación con Promesa
function askConfirmation(message) {
    return new Promise((resolve) => {
        const modal = document.getElementById('confirm-modal');
        const titleEl = document.getElementById('confirm-title');
        const msgEl = document.getElementById('confirm-message');
        const btnYes = document.getElementById('btn-confirm-yes');
        const btnCancel = document.getElementById('btn-confirm-cancel');

        // Textos
        const t = translations[currentLang];
        if(titleEl) titleEl.textContent = t.confirmTitle;
        if(msgEl) msgEl.textContent = message;
        if(btnYes) btnYes.textContent = t.btnDelete;
        if(btnCancel) btnCancel.textContent = t.btnCancel;

        // Mostrar
        if(modal) modal.classList.remove('hidden');
        
        // Foco automático en el botón de confirmar para accesibilidad
        if(btnYes) btnYes.focus();

        // --- FUNCIÓN DE LIMPIEZA ---
        const cleanup = () => {
            if(modal) modal.classList.add('hidden');
            btnYes.removeEventListener('click', handleYes);
            btnCancel.removeEventListener('click', handleNo);
            // IMPORTANTE: Quitamos el "oído" del teclado para no duplicar acciones
            document.removeEventListener('keydown', handleKeydown);
        };

        // --- MANEJADORES ---
        const handleYes = () => { cleanup(); resolve(true); };
        const handleNo = () => { cleanup(); resolve(false); };

        // --- NUEVO: SOPORTE PARA TECLADO (ENTER / ESC) ---
        const handleKeydown = (e) => {
            if (e.key === 'Enter') {
                e.preventDefault(); // Evita saltos de línea raros
                handleYes();
            } else if (e.key === 'Escape') {
                handleNo();
            }
        };

        // Activamos los listeners
        btnYes.addEventListener('click', handleYes);
        btnCancel.addEventListener('click', handleNo);
        document.addEventListener('keydown', handleKeydown);
    });
}
