/**
 * Kian AI - Estilos Estáticos Base
 * Maquetación, tipografía y estilos estructurales con efecto sin bordes.
 */

/* Reset y Base */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border: none;
    /* Eliminar bordes por defecto */
}

/* Bloquear el rebote de toda la página (Pull-to-refresh nativo que rompe la app) */
html,
body {
    height: 100%;
    /* Fallback */
    height: 100dvh;
    /* Dynamic Viewport Height: Clave para móviles */
    width: 100%;
    overflow: hidden;
    /* Evita scroll en el body, solo permitido en contenedores internos */
    overscroll-behavior: none;
    /* Desactiva el rebote elástico global */
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-family-base);
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-normal);
    line-height: 1.6;
    color: var(--text-primary);
    background: var(--bg-primary);
}

/* Tipografía */
h1,
h2,
h3,
h4,
h5,
h6 {
    font-weight: var(--font-weight-bold);
    line-height: 1.3;
    margin-bottom: var(--spacing-md);
}

p {
    margin-bottom: var(--spacing-md);
}

a {
    color: var(--primary);
    text-decoration: none;
}

/* Code & Preformatted Text */
code,
pre {
    font-family: var(--font-family-mono);
    font-size: var(--font-size-sm);
}

/* Inline Code */
code:not(pre code) {
    background: rgba(255, 255, 255, 0.1);
    padding: 2px 5px;
    border-radius: 4px;
    color: #e6e6e6;
    word-break: break-word;
    /* Prevents overflow in inline code */
}

/* Code Blocks Container */
pre {
    background: #1e1e1e !important;
    /* Atom One Dark bg match */
    margin-bottom: var(--spacing-md);
    border-radius: var(--border-radius-md);
    overflow: hidden;
    /* Important for the header */
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
    position: relative;
    border: 1px solid rgba(255, 255, 255, 0.05);
    max-width: 100%;
}

/* Code Block Header (Language + Copy) */
.code-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.05);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    font-family: var(--font-family-base);
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.code-lang {
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Actual Code Area */
pre code.hljs {
    padding: 12px !important;
    overflow-x: auto;
    /* Horizontal scroll */
    white-space: pre;
    /* Maintain formatting */
    display: block;
    background: transparent !important;
    /* Let pre handle bg */
}

/* Copy Code Button */
.btn-copy-code {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    border-radius: 4px;
    transition: all 0.2s;
}

.btn-copy-code:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

/* Mermaid Diagrams */
.mermaid {
    background: rgba(255, 255, 255, 0.05);
    padding: 15px;
    border-radius: var(--border-radius-md);
    margin-bottom: var(--spacing-md);
    display: flex;
    justify-content: center;
    overflow-x: auto;
    /* Permite scroll si el diagrama es muy ancho */
}

/* Tables */
table {
    border-collapse: collapse;
    width: 100%;
    margin-bottom: var(--spacing-md);
    font-size: 0.9em;
    border-radius: 8px;
    overflow: hidden;
}

th,
td {
    padding: 10px 14px;
    text-align: left;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

th {
    background: rgba(255, 255, 255, 0.05);
    font-weight: 600;
}

tr:last-child td {
    border-bottom: none;
}


/* Layout Principal anclado */
#app {
    width: 100%;
    height: 100%;
    /* Hereda el 100dvh */
    display: flex;
    flex-direction: column;
    position: relative;
    background: var(--bg-primary);
}

/* Header respetando el Notch (Safe Area) */
.app-header {
    padding-top: env(safe-area-inset-top);
    /* Baja el header si hay notch */
    height: calc(var(--header-height) + env(safe-area-inset-top));
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-left: var(--spacing-lg);
    padding-right: var(--spacing-lg);
    flex-shrink: 0;
    /* Nunca se encoge */
    z-index: var(--z-sticky);
    box-shadow: var(--shadow-lg);
}

.header-title {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    font-size: var(--font-size-xl);
    font-weight: var(--font-weight-bold);
    background: var(--primary-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
}

/* Contenedor de mensajes sólido */
.messages-container {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 5px;
    /* Compactado de var(--spacing-lg) a 5px */
    gap: 10px;
    /* Compactado de var(--spacing-lg) a 10px */
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    transition: opacity 0.3s ease;
}

/* Asegurar que el último mensaje no quede pegado al borde o tapado */
.messages-container::after {
    content: '';
    display: block;
    height: 10px;
    /* Reducido acorde al nuevo gap */
    flex-shrink: 0;
}

/* Ocultar scrollbar pero permitir scroll */
.messages-container::-webkit-scrollbar {
    display: none;
}

.messages-container {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

/* ... (resto de estilos de messages-container se mantienen igual si no se tocan) ... */

/* Botones dentro de la cápsula (override para quitar espacios) */
.input-wrapper .btn-icon {
    width: 36px;
    height: 36px;
    padding: 0;
    margin: 0;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    flex-shrink: 0;
    transition: all var(--transition-fast);
}

.input-wrapper .btn-icon:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

/* El textarea se expande pero con límite estricto de 3 líneas */
.input-wrapper.expanded .input-field {
    min-height: 24px;
    max-height: 72px;
    margin-bottom: 0;
}

/* Textarea integrado */
.input-field {
    flex: 1;
    background: transparent;
    padding: 8px 4px;
    color: var(--text-primary);
    font-family: var(--font-family-base);
    font-size: var(--font-size-base);
    resize: none;
    min-height: 24px;
    max-height: 72px;
    line-height: 24px;
    outline: none;
    overflow-y: auto;
    scrollbar-width: none;
    transition: height 0.2s ease;
}

/* Contenedor de mensaje (Avatar eliminado) */
.message {
    display: flex;
    margin-bottom: 5px;
    /* Compactado de var(--spacing-md) a 5px */
    animation: slideIn 0.3s ease-out;
    max-width: 98%;
}

.message.user {
    margin-left: auto;
    flex-direction: row-reverse;
}

/* Burbuja de mensaje */
.message-bubble {
    padding: 5px 8px;
    /* Compactado: 5px V, 8px H */
    border-radius: 18px;
    font-size: var(--font-size-base);
    line-height: 1.5;
    position: relative;
    word-wrap: break-word;
    box-shadow: var(--shadow-md);
    transition: all var(--transition-fast);
    overflow: hidden;
    max-width: 100%;
}

.message.user .message-bubble {
    background: var(--msg-user-bg);
    color: var(--msg-user-text);
    border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
    background: var(--msg-bot-bg);
    color: var(--msg-bot-text);
    border-bottom-left-radius: 4px;
}

/* Botón Copiar */
.copy-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    background: hsla(240, 30%, 5%, 0.6);
    border-radius: 6px;
    color: var(--text-secondary);
    border: none;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    cursor: pointer;
    opacity: 0.7;
    transition: all 0.2s ease;
    z-index: 10;
}

.copy-btn svg {
    width: 16px;
    height: 16px;
    pointer-events: none;
}

.message-bubble:hover .copy-btn {
    opacity: 1;
}

.copy-btn:hover {
    background: var(--bg-tertiary);
    color: var(--cyan);
    transform: scale(1.1);
}

/* Attachments en mensajes */
.message-attachments {
    margin-top: var(--spacing-sm);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* --- GRID DE IMÁGENES (El Collage) --- */
.media-grid {
    display: grid;
    gap: 2px;
    border-radius: 12px;
    overflow: hidden;
    width: 100%;
    max-width: 320px;
    min-height: 160px;
}

/* ESTILOS BASE DE IMAGEN */
.media-grid img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    cursor: zoom-in;
    transition: filter 0.2s;
    background: var(--bg-tertiary);
}

.media-grid img:hover {
    filter: brightness(0.9);
}

/* --- LÓGICA DEL COLLAGE SEGÚN CANTIDAD --- */

/* 1 Imagen: Mostrar completa con un ratio máximo */
.media-grid[data-count="1"] {
    grid-template-columns: 1fr;
    min-height: 180px;
}

.media-grid[data-count="1"] img {
    max-height: 300px;
    object-fit: contain;
    background: rgba(0, 0, 0, 0.1);
}

/* 2 Imágenes: Dos columnas exactas */
.media-grid[data-count="2"] {
    grid-template-columns: 1fr 1fr;
    min-height: 160px;
}

.media-grid[data-count="2"] img {
    aspect-ratio: 1 / 1;
}

/* 3 Imágenes: Una grande arriba, dos abajo */
.media-grid[data-count="3"] {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1.5fr 1fr;
    min-height: 240px;
}

.media-grid[data-count="3"] img:first-child {
    grid-column: span 2;
    aspect-ratio: 16 / 9;
}

.media-grid[data-count="3"] img:nth-child(2),
.media-grid[data-count="3"] img:nth-child(3) {
    aspect-ratio: 1 / 1;
}

/* 4 o más Imágenes: Cuadrícula 2x2 */
.media-grid[data-count="4"],
.media-grid[data-count="more"] {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    min-height: 320px;
}

.media-grid[data-count="4"] img,
.media-grid[data-count="more"] img {
    aspect-ratio: 1 / 1;
}

/* --- ARCHIVOS DE DOCUMENTOS (Lista separada) --- */
.documents-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.file-attachment {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg-tertiary);
    padding: 10px 14px;
    border-radius: 8px;
    font-size: var(--font-size-sm);
    color: var(--text-primary);
    text-decoration: none;
    transition: background 0.2s;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.file-attachment:hover {
    background: rgba(255, 255, 255, 0.1);
}

.file-icon {
    flex-shrink: 0;
    width: 24px;
    height: 24px;
    color: var(--cyan);
}

/* Input Area anclado y seguro */
.input-area {
    flex-shrink: 0;
    width: 100%;
    background: var(--bg-primary);

    /* Padding Modificado (Compacto) */
    padding-top: 5px;
    padding-bottom: calc(5px + env(safe-area-inset-bottom));
    padding-left: 0;
    /* 0px Lateral */
    padding-right: 0;

    z-index: var(--z-sticky);
}

.input-container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.input-attachments {
    display: flex;
    flex-wrap: wrap;
    gap: var(--spacing-sm);
}

.attachment-preview {
    position: relative;
    background: var(--bg-tertiary);
    border-radius: var(--border-radius-md);
    padding: var(--spacing-sm);
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    font-size: var(--font-size-sm);
}

.attachment-preview img {
    max-width: 50px;
    /* Tamaño máximo para la vista previa */
    max-height: 50px;
    object-fit: contain;
    border-radius: var(--border-radius-sm);
}

.attachment-remove {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: var(--error);
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 9px;
}

/* Wrapper del input (La Cápsula Principal) */
.input-wrapper {
    display: flex;
    align-items: center;
    /* CENTRADO VERTICAL EN REPOSO */
    gap: 0;
    /* CERO */
    background: var(--bg-secondary);
    border-radius: 26px;
    padding: 0;
    /* CERO Padding Lateral */
    /* Curva "Spring" suave */
    transition: all 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
    width: 80%;
    margin: 0 auto;
    min-height: 36px;
    /* Altura cómoda para 1 fila */
    box-shadow: var(--shadow-md);
    position: relative;
}

/* Grupo de botones de acción */
.input-actions-group {
    display: flex;
    flex-direction: row;
    gap: 0;
    /* CERO */
    align-items: center;
    transition: all 0.3s ease;
    height: 36px;
}

/* --- ESTADO EXPANDIDO (Layout Flexbox Robusto) --- */
.input-wrapper.expanded {
    border-radius: var(--border-radius-xl);
    align-items: flex-end;
    /* Alinea todo abajo por defecto (Botones Mic/Send) */
    padding: 10px 0;
    width: 100%;
    min-height: auto;
    /* Dejar que crezca según contenido */
}

/* Botón Adjuntar en modo expandido: Sin reglas especiales, flujo natural */

/* 2. Grupo de Acciones: Columna Vertical a la DERECHA (Flujo Natural) */
.input-wrapper.expanded .input-actions-group {
    display: flex;
    flex-direction: column;
    /* Vertical */
    gap: 8px;
    position: static;
    /* NO ABSOLUTE - Parte del flujo */
    margin-left: 4px;
    /* Separación del textarea */
    margin-bottom: 2px;
    /* Ajuste fino abajo */
    height: auto;
}

/* 3. Textarea: Ocupa el espacio central */
.input-wrapper.expanded .input-field {
    min-height: 24px;
    max-height: 72px;
    /* Límite 3 líneas */
    margin-right: 0;
    /* Ya no necesitamos margen, el grupo de acciones ocupa su espacio */
    margin-bottom: 4px;
    /* Un poco de aire abajo */
    margin-top: 4px;
}

/* Textarea integrado */
.input-field {
    flex: 1;
    background: transparent;
    padding: 10px 0;
    /* Padding vertical para ayudar al centrado del placeholder */
    color: var(--text-primary);
    font-family: var(--font-family-base);
    font-size: var(--font-size-base);
    resize: none;
    height: auto;
    /* Alturas y scroll */
    min-height: 24px;
    max-height: 72px;
    line-height: 24px;
    /* Altura de línea clara */
    outline: none;
    overflow-y: auto;
    scrollbar-width: none;
    transition: height 0.2s ease;
    /* ELIMINADO: display: flex y align-self: center solicitado por usuario */
    display: block;
}

/* Ocultar scrollbar en WebKit */
.input-field::-webkit-scrollbar {
    display: none;
}

.input-field::placeholder {
    color: var(--text-tertiary);
}

/* Estilos base para todos los botones de icono */
.btn-icon {
    width: 36px;
    height: 36px;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    flex-shrink: 0;
    transition: all var(--transition-fast);
}

.btn-icon:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

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

/* Botones dentro de la cápsula (override) */
.input-wrapper .btn-icon {
    width: 36px;
    height: 36px;
    padding: 0;
    /* Solicitud: CERO padding */
    margin: 0;
    /* Solicitud: CERO margin */
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    flex-shrink: 0;
    transition: all var(--transition-fast);
}

.input-wrapper .btn-icon:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

/* Botón de enviar específico */
#send-btn {
    background: var(--cyan);
    color: var(--bg-primary);
}

#send-btn:hover {
    background: var(--lime-green);
    transform: scale(1.05);
}

/* Botón activo (micrófono) */
.input-wrapper .btn-icon.active {
    color: var(--error);
    background: rgba(255, 69, 58, 0.1);
    animation: pulse 1.5s infinite;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: var(--text-primary);
    /* Ajustado de white a text-primary */
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.btn-danger {
    background: linear-gradient(135deg, var(--error), hsl(0, 70%, 45%));
    color: white;
    box-shadow: var(--shadow-md);
}

.btn-danger:hover {
    background: linear-gradient(135deg, hsl(0, 70%, 55%), var(--error));
    transform: scale(1.02);
}

.btn:disabled,
.btn-icon:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Iconos */
.icon {
    width: 15px;
    height: 15px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.icon svg {
    width: 100%;
    height: 100%;
}

/* Modal */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: hsla(240, 30%, 5%, 0.7);
    backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--z-modal-backdrop);
    padding: var(--spacing-lg);
}

.modal {
    background: var(--bg-secondary);
    border-radius: var(--border-radius-xl);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-xl);
    /* Sombra pronunciada para el modal */
}

.modal-header,
.modal-footer {
    padding: var(--spacing-lg);
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
}

.modal-header::after,
.modal-footer::before {
    content: '';
    position: absolute;
    left: var(--spacing-lg);
    right: var(--spacing-lg);
    height: 1px;
    background: hsla(180, 15%, 70%, 0.1);
    /* Línea divisoria sutil */
}

.modal-header::after {
    bottom: 0;
}

.modal-footer::before {
    top: 0;
}

.modal-body {
    padding: var(--spacing-lg);
}

.modal-footer {
    justify-content: flex-end;
    gap: var(--spacing-md);
}

/* Form elements */
.form-group {
    margin-bottom: var(--spacing-lg);
}

.form-label {
    display: block;
    margin-bottom: var(--spacing-xs);
    font-weight: var(--font-weight-medium);
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
}

.form-input {
    width: 100%;
    padding: var(--spacing-sm) var(--spacing-md);
    background: var(--bg-primary);
    /* Más oscuro que el fondo del modal */
    border-radius: var(--border-radius-md);
    color: var(--text-primary);
    font-family: var(--font-family-base);
    font-size: var(--font-size-base);
    outline: none;
    transition: box-shadow var(--transition-fast);
}

.form-input:focus {
    box-shadow: none;
    /* Eliminado el efecto de resplandor */
}

.form-hint {
    margin-top: var(--spacing-xs);
    font-size: var(--font-size-sm);
    color: var(--text-tertiary);
}

/* Utilidades */
.hidden {
    display: none !important;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}