/* --- TEMA & ESTILOS --- */
 @import url('color.css');

:root {
    --font-sans: 'Poppins', system-ui, -apple-system, sans-serif; 
    --font-mono: 'Fira Code', monospace;
}
* { box-sizing: border-box; margin: 0; padding: 0; font-family: var(--font-sans); -webkit-tap-highlight-color: transparent; }

/* Prevenir zoom y scroll horizontal */
html, body { 
    width: 100%;
    height: 100%;
    overflow: hidden;
    touch-action: manipulation;
}
/* Permitir selección en el área de chat */
#chat-box, .bubble, .bubble p, .bubble span, .bubble code, .bubble pre {
    -webkit-user-select: text;
    user-select: text;
}
html { overscroll-behavior: none; }

/* Ocultar barras de scroll */
::-webkit-scrollbar { display: none; }
* { scrollbar-width: none; }
body { background-color: var(--bg-core); color: var(--text-main); height: 100dvh; width: 100%; display: flex; flex-direction: column; overflow: hidden; }

main {
    flex: 1;
    position: relative;
    overflow: hidden;
}

/* Header */
header { padding: 14px 16px; display: flex; justify-content: space-between; align-items: center; background: rgba(17, 16, 22, 0.98); border-bottom: 1px solid rgba(230,230,230,0.05); z-index: 20; flex-shrink: 0; }
.logo { font-weight: 800; font-size: 1.2rem; letter-spacing: -0.5px; background: var(--grad-main); -webkit-background-clip: text; -webkit-text-fill-color: transparent; display: flex; align-items: center; gap: 8px; cursor: default; }
.status-badge { font-size: 0.7rem; background: var(--border-accent); border: 1px solid var(--border-accent); color: var(--color-purple-medium); padding: 2px 8px; border-radius: 12px; font-weight: 500; }

.controls { display: flex; gap: 6px; }
.icon-btn { background: transparent; border: none; color: var(--text-dim); padding: 8px; border-radius: 8px; cursor: pointer; display: flex; transition: 0.2s; position: relative; }
.icon-btn:hover { background: var(--color-hover); color: var(--text-main); }
.badge { position: absolute; top: 4px; right: 4px; background: var(--accent); width: 6px; height: 6px; border-radius: 50%; }

/* Chat Area */
#chat-box { 
    position: absolute;
    inset: 0;
    overflow-y: auto; 
    overflow-x: hidden;
    padding: 10px 12px 120px 12px; /* Padding inferior reducido */
    display: flex; 
    flex-direction: column; 
    gap: 16px; 
    scroll-behavior: smooth; 
    max-width: 850px; 
    margin: 0 auto; 
    width: 100%; 
    box-sizing: border-box;
}
.msg { 
    display: flex; 
    opacity: 0; 
    animation: fadeUp 0.3s forwards; 
    max-width: 100%;
    overflow-x: hidden;
    position: relative;
    width: 100%;
}
.msg.user { flex-direction: row-reverse; justify-content: flex-start; z-index: 1; }
.msg.user .bubble-col { max-width: 90%; }

.bubble-col { 
    display: flex; 
    flex-direction: column; 
    gap: 2px; 
    max-width: 100%; 
    overflow-x: hidden;
}
.msg.user .bubble-col { align-items: flex-end; }
.msg.ai .bubble-col { max-width: 90%; }

/* BUBBLE STYLING & MARKDOWN */
.bubble { 
    padding: 8px 12px; 
    border-radius: 14px; 
    font-size: 0.95rem; 
    line-height: 1.6; 
    position: relative; 
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: break-word;
    text-align: left; 
    hyphens: auto;
    width: auto;
    overflow: visible;
    display: inline-block;
}
.msg.ai .bubble { background: transparent; color: var(--text-bubble-ai); padding-left: 0; padding-top: 0; }
.msg.user .bubble { background: var(--bg-session); color: var(--text-bubble-user); border-bottom-right-radius: 2px; text-align: left; }

/* Meta Data Footer */
.meta { 
    display: flex; 
    gap: 10px; 
    align-items: center; 
    margin-top: 10px; 
    padding-top: 8px; 
    border-top: 1px solid rgba(230,230,230,0.08); 
    opacity: 0.8; 
    width: 100%;
    font-size: 0.75rem; 
    justify-content: space-between; 
}
.msg-actions { display: flex; align-items: center; gap: 8px; }
.msg-time { font-size: 0.65rem; color: var(--text-time); font-family: var(--font-mono); }
.icon-btn-mini {
    background: transparent;
    border: none;
    color: #666;
    cursor: pointer;
    padding: 4px; 
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s, background 0.2s;
}
.icon-btn-mini:hover { color: #a5b4fc; background: rgba(99, 102, 241, 0.05); }
.icon-btn-mini.speaking { color: #a855f7; animation: pulse 1s infinite; }

/* Markdown Element Coloring */
.bubble strong, .bubble b { color: var(--color-purple-light); font-weight: 700; }

/* Estilos para mensajes de búsqueda */
.msg.search-status {
    color: var(--color-purple-medium);
    font-style: italic;
    opacity: 0.9;
    animation: pulse 1.5s infinite;
}

.msg.search-results {
    border-left: 3px solid var(--color-purple-vibrant);
    background: rgba(168, 85, 247, 0.05);
    padding: 12px 16px;
    border-radius: 8px;
}

.msg.search-results h2 {
    color: var(--color-purple-light);
    margin-bottom: 8px;
}

.msg.search-results a {
    color: var(--color-purple-vibrant);
    text-decoration: none;
    border-bottom: 1px dotted var(--color-purple-vibrant);
}

.msg.search-results a:hover {
    border-bottom-style: solid;
}

/* Estilos para indicadores de estado */
.status-thinking {
    color: var(--color-blue-light);
    font-style: italic;
    display: flex;
    align-items: center;
    gap: 8px;
    animation: pulse 1.5s infinite;
}

.status-searching {
    color: var(--color-purple-light);
    font-style: italic;
    display: flex;
    align-items: center;
    gap: 8px;
    animation: pulse 1.5s infinite;
}

.status-processing {
    color: var(--color-warning);
    font-style: italic;
    display: flex;
    align-items: center;
    gap: 8px;
    animation: pulse 1.5s infinite;
}

/* Estilos para mensajes de error */
.msg.error {
    color: var(--color-error);
    background: rgba(255, 107, 107, 0.05);
    padding: 10px 14px;
    border-radius: 8px;
    border-left: 3px solid var(--color-error);
} /* Tono violeta */
.bubble em, .bubble i { color: #a1a1aa; font-style: italic; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { font-weight: 700; margin-top: 0.8em; margin-bottom: 0.4em; line-height: 1.3; text-align: left; }
.bubble h1 { font-size: 1.4em; background: var(--grad-main); -webkit-background-clip: text; -webkit-text-fill-color: transparent; display: inline-block; }
.bubble h2 { font-size: 1.2em; color: var(--color-purple-light); } /* Tono violeta */
.bubble h3 { font-size: 1.1em; color: var(--color-purple-medium); } 
.bubble h4 { font-size: 1.0em; color: var(--color-purple-light); text-transform: uppercase; letter-spacing: 0.5px; } /* Tono violeta */
.bubble p { margin-bottom: 0.6em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin-left: 1.4em; margin-bottom: 0.8em; }
.bubble li { margin-bottom: 4px; padding-left: 4px; }
.bubble li::marker { color: var(--color-purple-darker); font-weight: bold; }
.bubble blockquote { border-left: 3px solid var(--accent); background: var(--border-accent); padding: 8px 12px; margin: 10px 0; border-radius: 0 8px 8px 0; color: var(--color-blue-light); font-style: italic; }
.bubble a { color: var(--color-purple-darker); text-decoration: none; border-bottom: 1px dotted var(--color-purple-darker); transition: 0.2s; }
.bubble a:hover { color: var(--color-purple-medium); border-bottom-style: solid; }
.bubble pre { 
    background: var(--bg-modal); 
    border: 1px solid var(--border-light); 
    border-radius: 8px; 
    padding: 0; 
    margin: 12px 0; 
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    text-align: left; 
    max-width: 100%;
    scrollbar-width: none;
}
.bubble pre::-webkit-scrollbar { display: none; }
.bubble pre code { 
    padding: 12px; 
    background: transparent; 
    display: block; 
    font-family: var(--font-mono); 
    font-size: 0.85em; 
    color: var(--text-bubble-ai); 
    white-space: pre;
    min-width: min-content;
}
.bubble :not(pre) > code { background: var(--color-selected); color: var(--color-purple-medium); padding: 2px 5px; border-radius: 4px; font-size: 0.85em; font-family: var(--font-mono); }

/* KaTeX Bloques - Fórmulas matemáticas en bloques con scroll horizontal SIN barra */
.katex-display {
    max-width: 100%;
    margin: 12px 0;
    background: var(--bg-modal);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    padding: 12px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    scrollbar-width: none;
}
.katex-display::-webkit-scrollbar { display: none; }

/* Asegurar que todo dentro de KaTeX no se desborde */
.katex-display > * {
    display: inline-block;
    min-width: min-content;
}

/* KaTeX inline */
.katex {
    font-size: inherit;
    white-space: nowrap;
}

/* Mermaid diagrams */
.mermaid {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    scrollbar-width: none;
}
.mermaid::-webkit-scrollbar { display: none; }

/* Tablas responsive con scroll sin barra */
.bubble table {
    max-width: 100%;
    border-collapse: collapse;
    margin: 12px 0;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}
.bubble table::-webkit-scrollbar { display: none; }

.bubble table, .bubble th, .bubble td {
    border: 1px solid var(--border-light);
}

.bubble th {
    background: var(--bg-modal);
    padding: 8px;
    text-align: left;
    font-weight: 700;
}

.bubble td {
    padding: 8px;
}

.bubble tr:nth-child(even) {
    background: rgba(99, 102, 241, 0.05);
}

/* Wrapper para tablas con scroll */
.bubble table {
    min-width: 100%;
    white-space: nowrap;
}

/* Images & UI Elements within chat */
.search-step { border: 1px solid var(--border-light); background: var(--bg-modal); border-radius: 8px; padding: 8px 12px; display: flex; align-items: center; gap: 10px; font-size: 0.8rem; color: var(--text-meta); margin-bottom: 10px; animation: pulse 2s infinite; text-align: left; }
.chat-img { max-width: 100%; border-radius: 8px; margin-top: 6px; border: 1px solid var(--border-light); }
.code-head { display: flex; justify-content: space-between; background: var(--bg-tabs); padding: 6px 12px; font-size: 0.75rem; color: var(--text-dim); border-bottom: 1px solid var(--border-light); font-family: var(--font-mono); }
.copy-btn { background: none; border: none; color: inherit; cursor: pointer; transition: 0.2s; }
.copy-btn:hover { color: var(--text-main); }
.model-tag { font-size: 0.65rem; color: var(--color-purple-vibrant); border: 1px solid var(--border-accent); padding: 0 4px; border-radius: 4px; }

.search-badge { 
    font-size: 0.65rem; 
    color: var(--color-purple-light); 
    background: rgba(168, 85, 247, 0.1); 
    border: 1px solid var(--border-accent); 
    padding: 0 4px; 
    border-radius: 4px; 
    margin-left: 6px;
}
.tts-btn { background: none; border: none; color: #555; cursor: pointer; padding: 2px; display: flex; transition: color 0.2s; }
.tts-btn:hover { color: #ddd; }
.tts-btn.speaking { color: #a855f7; animation: pulse 1s infinite; }

/* Input Area */
.input-area {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 12px; 
    background: transparent; /* Ahora transparente */
    max-width: 850px; 
    margin: 0 auto; 
    width: 100%; 
    z-index: 30; 
}

/* Preview Box */
#preview-box { 
    position: absolute; 
    bottom: 100%; 
    left: 12px; 
    right: 12px;
    background: transparent; 
    padding: 0; 
    gap: 8px; 
    overflow-x: auto;
    white-space: nowrap;
    margin-bottom: 8px;
    display: none; /* Oculto por defecto */
}
#preview-box:not(:empty) {
    display: flex; /* Visible solo cuando tiene contenido */
    padding: 8px 0;
}
.prev-item {
    background: #1e1d24; 
    border: 1px solid #333; 
    padding: 6px 10px; 
    border-radius: 10px; 
    display: inline-flex; 
    align-items: center; 
    gap: 8px; 
    box-shadow: 0 5px 15px rgba(0,0,0,0.3);
    flex-shrink: 0;
    max-width: 200px;
}
.prev-icon { width: 24px; height: 24px; background: var(--bg-session); border-radius: 6px; display: flex; align-items: center; justify-content: center; font-size: 14px; flex-shrink: 0; }
.prev-info { display: flex; flex-direction: column; overflow: hidden; }
.prev-name { font-size: 0.75rem; color: var(--text-main); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.prev-del { background: var(--border-light); border: none; color: var(--text-label); width: 18px; height: 18px; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 10px; margin-left: auto; flex-shrink: 0; }
.prev-del:hover { background: var(--color-warning); color: white; }

/* DYNAMIC INPUT WRAPPER */
.input-wrapper { 
    background: var(--bg-input); /* Restaurado el fondo original */
    border: 1px solid var(--color-hover); /* Restaurado el borde original */
    border-radius: 24px; 
    padding: 8px 8px 8px 14px; /* Restaurado el padding original (con más a la izq) */
    display: flex; 
    align-items: flex-end; 
    gap: 8px; /* Restaurado el gap original */
    transition: 0.2s; 
}
.input-wrapper:focus-within { border-color: var(--accent); } /* Restaurado el color de foco original */

/* Grupos Laterales (Default: Fila) */
.input-side {
    display: flex;
    gap: 6px;
    align-items: center;
    transition: all 0.3s ease;
}

/* Estado Expandido (Columnas Laterales) */
.input-wrapper.expanded {
    align-items: flex-end; 
}
.input-wrapper.expanded .input-side {
    flex-direction: column;
    padding-bottom: 4px; /* Ajuste fino */
    gap: 12px; /* Espacio vertical ajustado */
}

/* --- MODO INMERSIVO MÓVIL --- 
   Si estamos en móvil (max-width: 768px) Y expandido:
   1. Expandimos el ancho más allá del padding del padre.
   2. Pegamos al fondo (sin margin-bottom).
   3. Quitamos border-radius inferior.
*/
@media (max-width: 768px) {
    .input-wrapper.expanded {
        /* Los márgenes negativos ahora anulan el padding del .input-area transparente */
        margin: 0 -12px -12px -12px; 
        width: calc(100% + 24px);    
        border-radius: 24px 24px 0 0; 
        border-bottom: none;          
        border-left: none;
        border-right: none;
        
        /* Ajustes específicos para el padding y gap del wrapper en modo inmersivo */
        padding-left: 10px; /* Reducido pero no al mínimo */
        padding-right: 10px; /* Reducido pero no al mínimo */
        gap: 4px; /* Gap ajustado */
    }
    .input-wrapper.expanded .input-side {
        flex-direction: column;
        padding-bottom: 2px; 
        gap: 8px; 
    }
}

textarea { flex: 1; background: transparent; border: none; color: var(--text-main); resize: none; max-height: 140px; outline: none; font-size: 16px; padding-bottom: 6px; height: 24px; line-height: 1.5; min-width: 0; }

/* Botones de acción */
.act-btn { 
    width: 36px; 
    height: 36px; 
    border-radius: 50%; 
    border: none; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    cursor: pointer; 
    color: var(--text-dim); 
    background: transparent; 
    transition: all 0.2s ease; 
    flex-shrink: 0;
    position: relative;
}

.act-btn:hover { 
    color: var(--text-main); 
    background: rgba(230, 230, 230, 0.08); 
}

/* Botón de enviar */
.send-btn { 
    background: var(--grad-main); 
    color: white;
    box-shadow: 0 2px 10px rgba(99, 102, 241, 0.3);
}

.send-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.send-btn:active {
    transform: translateY(0);
    box-shadow: 0 2px 6px rgba(99, 102, 241, 0.3);
}

/* Botón de micrófono */
.mic-btn {
    position: relative;
}

.mic-btn.active, 
.act-btn#btn-stt.active { 
    color: white; 
    background: #ef4444;
    animation: pulse 1.5s infinite; 
}

/* Estilos para los iconos SVG */
.act-btn svg {
    width: 20px;
    height: 20px;
    fill: none; /* Importante: Sin relleno */
    stroke: currentColor; /* El color viene del texto */
    stroke-width: 2; /* Grosor de línea consistente */
    stroke-linecap: round; /* Bordes redondeados */
    stroke-linejoin: round; /* Uniones redondeadas */
}

/* Modals & Utils - Mantenidos igual */
.modal-overlay { position: fixed; inset: 0; background: rgba(10,10,14,0.85); backdrop-filter: blur(5px); display: none; justify-content: center; align-items: center; z-index: 100; padding: 20px; }
.modal { background: var(--bg-modal); width: 100%; max-width: 500px; border-radius: 20px; border: 1px solid var(--border-light); padding: 0; box-shadow: 0 20px 50px rgba(0,0,0,0.6); overflow: hidden; }
.modal h2 { font-weight: 600; color: var(--text-main); font-size: 1.1rem; display: flex; justify-content: space-between; align-items: center; }

/* Botón de cierre del modal - Mejorado para accesibilidad */
.modal-close-btn {
    background: none;
    border: none;
    color: #888;
    font-size: 1.6rem;
    cursor: pointer;
    margin-left: auto;
    padding: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s;
    width: 36px;
    height: 36px;
}

.modal-close-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    transform: scale(1.1);
}

.modal-close-btn:active {
    background: rgba(255, 255, 255, 0.2);
}
.field label { display: block; font-size: 0.8rem; color: var(--text-label); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600; }
.field textarea, .field input { width: 100%; background: var(--bg-field); border: 1px solid var(--border-light); padding: 14px; border-radius: 8px; color: var(--text-main); outline: none; font-family: var(--font-sans); font-size: 15px; }
.field textarea, .field input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--color-focus); }
.field textarea { min-height: 120px; resize: vertical; }
.btn-primary { background: var(--grad-main); border: none; color: var(--text-main); padding: 14px; border-radius: 10px; font-weight: 600; cursor: pointer; width: 100%; transition: transform 0.1s; font-size: 0.9rem; }
.btn-primary:active { transform: scale(0.98); }
.btn-secondary { background: #2a2930; border: 1px solid #333; color: #ccc; padding: 14px; border-radius: 10px; font-weight: 600; cursor: pointer; width: 100%; font-size: 0.9rem; }
.btn-secondary:hover { background: #333; border-color: #555; }

.tabs { display: flex; background: var(--bg-tabs); border-bottom: 1px solid var(--border-light); padding: 0; margin: 0; border-radius: 20px 20px 0 0; }
.tab-btn { flex: 1; background: none; border: none; padding: 18px 0; color: #71717a; cursor: pointer; font-weight: 500; transition: all 0.2s; font-size: 0.9rem; }
.tab-btn.active { color: var(--text-main); background: var(--bg-session); border-bottom: 2px solid var(--accent); }
.tab-btn:hover { color: var(--text-main); background: var(--border-accent); }
.tab-content { display: none; padding: 24px; flex-direction: column; gap: 16px; min-height: 400px; }
.tab-content.active { display: flex; }

.session-list { display: flex; flex-direction: column; gap: 8px; max-height: 300px; overflow-y: auto; padding: 8px; }
.session-item { background: var(--bg-session); padding: 14px; border-radius: 10px; border: 1px solid var(--border-lighter); cursor: pointer; display: flex; justify-content: space-between; align-items: center; transition: all 0.2s; }
.session-item:hover { background: var(--border-accent); border-color: var(--border-accent); }
.session-item.active { border-color: var(--accent); background: var(--color-selected); }
.session-title { color: var(--text-main); font-size: 0.9rem; font-weight: 500; }
.session-meta { display: flex; flex-direction: column; }
.row-del { color: #ff6b6b; background: none; border: none; cursor: pointer; padding: 4px; }

#toasts { position: fixed; bottom: 80px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 10px; pointer-events: none; }
.toast { background: var(--bg-toast); border: 1px solid var(--border-light); padding: 10px 16px; border-radius: 10px; color: var(--text-main); font-size: 0.85rem; animation: fadeUp 0.3s; border-left: 3px solid; box-shadow: 0 5px 15px rgba(0,0,0,0.3); pointer-events: auto; }
.toast.success { border-color: var(--color-success); } .toast.error { border-color: var(--color-error); } .toast.info { border-color: var(--color-info); }

.modal-overlay.visible {
    display: flex;
}

 @keyframes fadeUp { from { opacity:0; transform:translateY(10px); } to { opacity:1; transform:translateY(0); } }
 @keyframes fadeIn { to { opacity: 1; } }
 @keyframes pulse { 0% { opacity:0.6; } 50% { opacity:1; } 100% { opacity:0.6; } }