/* ==========================================
   CSS GLOBAL & THEMING (Premium Dark Mode)
   ========================================== */
:root {
    --bg-primary: #0a0b10;
    --bg-secondary: #12141c;
    --bg-tertiary: #191c28;
    --bg-glass: rgba(22, 25, 38, 0.7);
    --border-color: rgba(255, 255, 255, 0.07);
    --border-hover: rgba(255, 255, 255, 0.15);
    
    --text-main: #f3f4f6;
    --text-muted: #8e95a5;
    --text-dark: #000000;

    /* Brand Colors & Channels */
    --accent: #6366f1; /* Indigo */
    --accent-hover: #4f46e5;
    --accent-glow: rgba(99, 102, 241, 0.35);

    --wa-color: #25d366;
    --wa-glow: rgba(37, 211, 102, 0.2);
    
    --ig-gradient: linear-gradient(45deg, #f09433 0%, #e6683c 25%, #dc2743 50%, #cc2366 75%, #bc1888 100%);
    --ig-color: #e1306c;
    --ig-glow: rgba(225, 48, 108, 0.2);

    --fb-color: #0084ff;
    --fb-glow: rgba(0, 132, 255, 0.2);

    --tk-color: #00f2fe;
    --tk-color-alt: #fe0979;
    --tk-glow: rgba(0, 242, 254, 0.2);

    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;

    --font-sans: 'Inter', sans-serif;
    --font-heading: 'Outfit', sans-serif;
    --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-primary);
    color: var(--text-main);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ==========================================
   LAYOUT STRUCTURE
   ========================================== */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
    background: radial-gradient(circle at 50% 0%, #16192a 0%, #0a0b10 100%);
}

/* ==========================================
   SIDEBAR
   ========================================== */
.sidebar {
    width: 260px;
    background-color: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.brand {
    padding: 24px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--border-color);
}

.brand-logo {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, var(--accent) 0%, #a855f7 100%);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    box-shadow: 0 4px 12px var(--accent-glow);
}

.brand-logo svg {
    width: 20px;
    height: 20px;
}

.brand-info h1 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    background: linear-gradient(to right, #ffffff, #d1d5db);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.brand-info span {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.nav-menu {
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-grow: 1;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    border-radius: 8px;
    transition: var(--transition);
}

.nav-item svg {
    width: 18px;
    height: 18px;
    opacity: 0.75;
}

.nav-item:hover {
    color: var(--text-main);
    background-color: rgba(255, 255, 255, 0.03);
}

.nav-item.active {
    color: var(--text-main);
    background-color: rgba(99, 102, 241, 0.12);
    border-left: 3px solid var(--accent);
    padding-left: 13px;
}

.nav-item.active svg {
    color: var(--accent);
    opacity: 1;
}

.sidebar-footer {
    padding: 16px;
    border-top: 1px solid var(--border-color);
    background-color: rgba(0, 0, 0, 0.15);
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-profile .avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background-color: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-family: var(--font-heading);
    color: white;
}

.user-details {
    display: flex;
    flex-direction: column;
}

.user-name {
    font-size: 0.85rem;
    font-weight: 600;
}

.user-role {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ==========================================
   MAIN CONTENT & TABS
   ========================================== */
.main-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.tab-pane {
    display: none;
    height: 100%;
    width: 100%;
}

.tab-pane.active {
    display: block;
}

/* ==========================================
   INBOX LAYOUT
   ========================================== */
.inbox-layout {
    display: flex;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

/* CHATS SIDEBAR */
.chats-sidebar {
    width: 320px;
    background-color: rgba(18, 20, 28, 0.6);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.chats-header {
    padding: 20px 16px 12px 16px;
    border-bottom: 1px solid var(--border-color);
}

.chats-header h2 {
    font-family: var(--font-heading);
    font-size: 1.3rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.search-box {
    position: relative;
    margin-bottom: 12px;
}

.search-box svg {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    color: var(--text-muted);
}

.search-box input {
    width: 100%;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 12px 10px 38px;
    color: var(--text-main);
    font-size: 0.85rem;
    outline: none;
    transition: var(--transition);
}

.search-box input {
    padding-right: 34px; /* espacio para el botón limpiar */
}

.search-box input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

/* Botón "limpiar búsqueda" (aparece solo cuando hay texto) */
.search-clear {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    border: none;
    background: var(--border-color);
    color: var(--text-main);
    border-radius: 50%;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.search-clear:hover {
    background: var(--accent);
    color: #fff;
}

/* Ocultar la X nativa del input type=search (usamos la nuestra) */
.search-box input[type="search"]::-webkit-search-cancel-button {
    -webkit-appearance: none;
    appearance: none;
}

.channel-filters {
    display: flex;
    gap: 4px;
    overflow-x: auto;
    padding-bottom: 4px;
}

/* Custom scrollbar for horizontal filters */
.channel-filters::-webkit-scrollbar {
    height: 3px;
}
.channel-filters::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 3px;
}

.filter-btn {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-muted);
    padding: 6px 10px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: var(--transition);
}

.filter-btn:hover {
    color: var(--text-main);
    border-color: var(--border-hover);
}

.filter-btn.active {
    background-color: var(--accent);
    color: white;
    border-color: var(--accent);
}

.dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    display: inline-block;
}
.dot-wa { background-color: var(--wa-color); }
.dot-ig { background: var(--ig-gradient); }
.dot-fb { background-color: var(--fb-color); }
.dot-tk { background-color: var(--tk-color); }

/* Ícono (más grande) dentro de los botones de filtro de canal */
.filter-btn .filter-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
}
.filter-btn .filter-icon svg {
    width: 17px;
    height: 17px;
}
.filter-icon.ch-whatsapp { color: var(--wa-color); }
.filter-icon.ch-instagram { color: #e1306c; }
.filter-icon.ch-facebook { color: var(--fb-color); }
.filter-icon.ch-tiktok { color: var(--text-main); }
.filter-icon.ch-integrate-abroad { color: #2dd4bf; }
/* En el botón activo, el ícono se vuelve blanco para contrastar con el fondo */
.filter-btn.active .filter-icon { color: #fff; }

/* Filas de filtro (Canal / Área / Tipo / Estado) con chips a la vista */
.filter-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
}
.filter-row-label {
    width: 42px;
    flex-shrink: 0;
    font-size: 0.64rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}
/* El contenedor de chips/canales ocupa el resto del ancho y scrollea si desborda */
.filter-row > .chip-group,
.filter-row > .channel-filters {
    flex: 1;
    min-width: 0;
}

.chip-group {
    display: flex;
    gap: 5px;
    overflow-x: auto;
    padding-bottom: 2px;
}
.chip-group::-webkit-scrollbar { height: 3px; }
.chip-group::-webkit-scrollbar-thumb { background-color: var(--border-color); border-radius: 3px; }

.chip {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-muted);
    padding: 5px 11px;
    border-radius: 20px;
    font-size: 0.72rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: var(--transition);
}
.chip:hover {
    color: var(--text-main);
    border-color: var(--border-hover);
}
.chip.active {
    background-color: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

/* Los chips de Área toman el color de su departamento al activarse */
.chip-group[data-filter="department"] .chip[data-value="comercial"].active {
    background-color: rgba(34, 197, 94, 0.18); color: #4ade80; border-color: rgba(34, 197, 94, 0.45);
}
.chip-group[data-filter="department"] .chip[data-value="marketing"].active {
    background-color: rgba(168, 85, 247, 0.18); color: #c084fc; border-color: rgba(168, 85, 247, 0.45);
}
.chip-group[data-filter="department"] .chip[data-value="legal"].active {
    background-color: rgba(59, 130, 246, 0.18); color: #60a5fa; border-color: rgba(59, 130, 246, 0.45);
}
.chip-group[data-filter="department"] .chip[data-value="it"].active {
    background-color: rgba(245, 158, 11, 0.18); color: #fbbf24; border-color: rgba(245, 158, 11, 0.45);
}

/* ===== Sección Conexiones & Credenciales ===== */
.connection-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 4px;
}
.connection-card-header h3 {
    margin: 0;
}

.conn-status {
    font-size: 0.68rem;
    font-weight: 700;
    padding: 3px 10px;
    border-radius: 999px;
    white-space: nowrap;
    flex-shrink: 0;
}
.conn-on {
    background-color: rgba(16, 185, 129, 0.16);
    color: #34d399;
}
.conn-off {
    background-color: rgba(148, 163, 184, 0.14);
    color: var(--text-muted);
}

.cfg-input {
    width: 100%;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 12px;
    margin-top: 4px;
    color: var(--text-main);
    font-size: 0.85rem;
    outline: none;
    transition: var(--transition);
}
.cfg-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}
.cfg-input[readonly] {
    color: var(--text-muted);
    cursor: not-allowed;
    font-size: 0.78rem;
}

/* Contador de resultados + limpiar filtros */
/* Barra compacta de filtros (botón colapsable + contador) */
.filters-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-top: 10px;
}
.filters-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-muted);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}
.filters-toggle:hover { color: var(--text-main); border-color: var(--border-hover); }
.filters-toggle.open { color: var(--text-main); border-color: var(--accent); }
.filters-toggle > svg:first-of-type { width: 14px; height: 14px; }
.filters-toggle .chevron { width: 14px; height: 14px; transition: transform 0.25s ease; }
.filters-toggle.open .chevron { transform: rotate(180deg); }

.filter-active-count {
    background-color: var(--accent);
    color: #fff;
    font-size: 0.64rem;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.filter-active-count.hidden { display: none; }

.filters-bar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}
.filter-count-inline {
    font-size: 0.72rem;
    color: var(--text-muted);
    white-space: nowrap;
}
.clear-filters-btn {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 6px;
    transition: var(--transition);
}
.clear-filters-btn:hover { background-color: var(--accent-glow); }
.clear-filters-btn.hidden { display: none; }

/* Panel de filtros colapsable */
.filters-collapsible {
    overflow: hidden;
    max-height: 360px;
    margin-top: 4px;
    transition: max-height 0.3s ease, opacity 0.25s ease, margin-top 0.3s ease;
}
.filters-collapsible.collapsed {
    max-height: 0;
    opacity: 0;
    margin-top: 0;
    pointer-events: none;
}

.chats-list {
    flex-grow: 1;
    overflow-y: auto;
}

/* Scrollbar styling */
::-webkit-scrollbar {
    width: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.08);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.15);
}

.chat-item {
    display: flex;
    gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    cursor: pointer;
    position: relative;
    transition: var(--transition);
}

.chat-item:hover {
    background-color: rgba(255, 255, 255, 0.02);
}

.chat-item.active {
    background-color: rgba(99, 102, 241, 0.06);
    border-left: 3px solid var(--accent);
}

.avatar-wrapper {
    position: relative;
    flex-shrink: 0;
}

.avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background-color: var(--bg-tertiary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    color: var(--text-main);
    border: 1px solid var(--border-color);
}

.channel-badge {
    position: absolute;
    bottom: -3px;
    right: -3px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--bg-secondary);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}

.channel-badge svg {
    width: 14px;
    height: 14px;
    color: white;
}

.channel-badge.whatsapp { background-color: var(--wa-color); }
.channel-badge.instagram { background: var(--ig-gradient); }
.channel-badge.facebook { background-color: var(--fb-color); }
.channel-badge.tiktok { background-color: #000; border-color: var(--tk-color); }

.chat-item-content {
    flex-grow: 1;
    min-width: 0;
}

.chat-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.chat-item-name {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-time {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.chat-item-preview {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 6px;
}

.chat-item-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.status-indicator-badge {
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 500;
}

.badge-ai-suggested {
    background-color: rgba(99, 102, 241, 0.15);
    color: #a5b4fc;
    border: 1px solid rgba(99, 102, 241, 0.3);
}

.badge-ai-replied {
    background-color: rgba(16, 185, 129, 0.15);
    color: #a7f3d0;
    border: 1px solid rgba(16, 185, 129, 0.3);
}

.badge-manual {
    background-color: rgba(245, 158, 11, 0.15);
    color: #fde68a;
    border: 1px solid rgba(245, 158, 11, 0.3);
}

.unread-badge {
    background-color: var(--danger);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
}

/* ==========================================
   CHAT VIEWPORT
   ========================================== */
.chat-viewport {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    background-color: rgba(10, 11, 16, 0.4);
    position: relative;
}

.chat-header-bar {
    padding: 16px 24px;
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    z-index: 10;
}

/* Acciones del header (selector Copiloto/Autopiloto + botón de info en móvil).
   Sin esto el contenedor no era flex y en móvil el botón de info se apilaba
   debajo del selector, rompiendo la cabecera. flex-shrink:0 para que no se
   deforme y el nombre del contacto sea el que trunca. (ABR-1276) */
.chat-actions-header {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.active-contact-info {
    display: flex;
    align-items: center;
    gap: 14px;
}

.active-contact-info .avatar {
    width: 46px;
    height: 46px;
}

.name-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.name-row h3 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    font-weight: 600;
}

#active-status-text {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.ai-mode-selector {
    display: flex;
    align-items: center;
    background-color: var(--bg-primary);
    border: 1px solid var(--border-color);
    padding: 3px;
    border-radius: 8px;
}

.mode-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    padding: 0 8px;
}

.mode-toggle-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    transition: var(--transition);
}

.mode-toggle-btn:hover {
    color: var(--text-main);
}

.mode-toggle-btn.active {
    background-color: var(--accent);
    color: white;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
}

#btn-mode-autopilot.active {
    background-color: var(--success);
    box-shadow: 0 2px 8px rgba(16, 185, 129, 0.4);
}

/* MESSAGES LIST AREA */
.messages-container {
    flex-grow: 1;
    /* min-height: 0 permite que este flex-item scrollee internamente en vez de
       crecer con todo el contenido y empujar el panel de copiloto / input fuera
       de la vista (bug clásico de flexbox: el default min-height:auto no encoge). */
    min-height: 0;
    padding: 24px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    text-align: center;
    padding: 40px;
}

.empty-state svg {
    width: 60px;
    height: 60px;
    color: var(--border-hover);
    margin-bottom: 16px;
}

.empty-state h3 {
    font-family: var(--font-heading);
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 6px;
}

.empty-state p {
    font-size: 0.85rem;
    max-width: 300px;
    line-height: 1.4;
}

/* MESSAGE BUBBLES */
.message {
    display: flex;
    flex-direction: column;
    max-width: 70%;
    animation: fadeInUp 0.25s ease-out forwards;
}

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

.message.incoming {
    align-self: flex-start;
}

.message.outgoing {
    align-self: flex-end;
}

.message-meta-info {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.message.incoming .message-meta-info {
    margin-left: 12px;
}

.message.outgoing .message-meta-info {
    margin-right: 12px;
    align-self: flex-end;
}

/* Read receipts (✓ enviado, ✓✓ leído) — visible solo en mensajes propios. */
.message-read-tick {
    color: var(--text-muted);
    font-size: 0.78rem;
    line-height: 1;
    letter-spacing: -1px;
}

.message-read-tick.is-read {
    color: #38bdf8; /* azul tipo WhatsApp para "leído" */
}

.message-bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 0.9rem;
    line-height: 1.5;
    position: relative;
}

.message.incoming .message-bubble {
    background-color: var(--bg-tertiary);
    color: var(--text-main);
    border-top-left-radius: 4px;
    border: 1px solid var(--border-color);
}

.message.outgoing .message-bubble {
    background-color: var(--accent);
    color: white;
    border-top-right-radius: 4px;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.15);
}

.message-sender-tag {
    font-size: 0.65rem;
    padding: 1px 6px;
    border-radius: 4px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-left: 6px;
    display: inline-block;
}

.sender-tag-ai {
    background-color: rgba(255, 255, 255, 0.2);
    color: #fff;
}

.sender-tag-agent {
    background-color: rgba(0, 0, 0, 0.2);
    color: rgba(255, 255, 255, 0.95);
}

.sender-tag-edited {
    background-color: rgba(245, 158, 11, 0.22);
    color: #fbbf24;
    text-transform: none;
    letter-spacing: 0;
}

/* TYPING INDICATOR */
.typing-bubble {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 14px 18px !important;
}

.typing-dot {
    width: 6px;
    height: 6px;
    background-color: var(--text-muted);
    border-radius: 50%;
    animation: typingPulse 1.4s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingPulse {
    0%, 100% { transform: translateY(0); opacity: 0.4; }
    50% { transform: translateY(-4px); opacity: 1; }
}

/* ==========================================
   COPILOT SUGGESTION BOX (H.I.T.L.)
   ========================================== */
.copilot-panel {
    margin: 0 24px 12px 24px;
    background: rgba(99, 102, 241, 0.05);
    border: 1px solid rgba(99, 102, 241, 0.2);
    border-radius: 12px;
    overflow: hidden;
    /* Nunca encoger: si la columna queda corta, el panel mantiene su alto y los
       botones del footer siempre quedan visibles (antes se recortaban). */
    flex-shrink: 0;
    backdrop-filter: blur(8px);
    animation: slideUp 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards;
}

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

.copilot-header {
    background: rgba(99, 102, 241, 0.1);
    padding: 10px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(99, 102, 241, 0.15);
}

.title-with-badge {
    display: flex;
    align-items: center;
    gap: 8px;
}

.copilot-sparkle {
    width: 16px;
    height: 16px;
    color: #818cf8;
}

.copilot-header h4 {
    font-family: var(--font-heading);
    font-size: 0.85rem;
    font-weight: 600;
    color: #e0e7ff;
}

.confidence-badge {
    font-size: 0.7rem;
    background-color: rgba(16, 185, 129, 0.2);
    color: #34d399;
    padding: 1px 6px;
    border-radius: 4px;
    font-weight: 600;
}

.intent-tag {
    font-size: 0.7rem;
    color: #c7d2fe;
    font-weight: 500;
}

.copilot-body {
    padding: 12px 16px;
}

.copilot-body textarea {
    width: 100%;
    height: 70px;
    background: none;
    border: none;
    resize: none;
    color: var(--text-main);
    font-size: 0.85rem;
    line-height: 1.4;
    outline: none;
    font-family: var(--font-sans);
}

.copilot-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 8px 16px;
    background: rgba(0, 0, 0, 0.15);
    border-top: 1px solid rgba(99, 102, 241, 0.08);
}

/* ==========================================
   CHAT INPUT AREA
   ========================================== */
.chat-input-area {
    padding: 16px 24px;
    background-color: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.input-actions-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.input-action-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

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

.input-action-btn:hover {
    color: var(--text-main);
    background-color: rgba(255, 255, 255, 0.05);
}

.ai-status-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-left: auto;
}

.pulse-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background-color: var(--accent);
}

.pulse-dot.active {
    animation: indicatorPulse 1.5s infinite;
}

@keyframes indicatorPulse {
    0% { transform: scale(0.9); opacity: 0.6; box-shadow: 0 0 0 0 rgba(99, 102, 241, 0.4); }
    50% { transform: scale(1.1); opacity: 1; box-shadow: 0 0 0 4px rgba(99, 102, 241, 0); }
    100% { transform: scale(0.9); opacity: 0.6; box-shadow: 0 0 0 0 rgba(99, 102, 241, 0); }
}

.input-form {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

.input-form textarea {
    flex-grow: 1;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 12px 16px;
    color: var(--text-main);
    font-size: 0.9rem;
    outline: none;
    resize: none;
    height: 48px;
    font-family: var(--font-sans);
    line-height: 1.4;
    transition: var(--transition);
}

.input-form textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

/* ==========================================
   CRM PROFILE SIDEBAR
   ========================================== */
.crm-sidebar {
    width: 280px;
    background-color: var(--bg-secondary);
    border-left: 1px solid var(--border-color);
    overflow-y: auto;
    padding: 24px 20px;
    flex-shrink: 0;
}

/* Botón "Volver al chat" dentro de la ficha CRM. Solo visible cuando la ficha
   es un panel deslizable (móvil/tablet); en escritorio es columna fija. (ABR-1276) */
.crm-back-btn { display: none; }

.empty-crm-state {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    text-align: center;
    font-size: 0.85rem;
}

.profile-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    margin-bottom: 24px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
}

.profile-avatar {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background-color: var(--bg-tertiary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8rem;
    font-weight: 700;
    font-family: var(--font-heading);
    margin-bottom: 12px;
    border: 2px solid var(--border-color);
}

.profile-card h3 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 2px;
}

.profile-card p {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 10px;
}

.platform-pill {
    font-size: 0.7rem;
    padding: 3px 10px;
    border-radius: 12px;
    font-weight: 600;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
}

.crm-section {
    margin-bottom: 24px;
}

.crm-section h4 {
    font-family: var(--font-heading);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 12px;
    font-weight: 700;
}

.ai-classification-badges {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.class-badge {
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    padding: 8px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.class-badge .label {
    font-size: 0.65rem;
    color: var(--text-muted);
}

.class-badge .value {
    font-size: 0.8rem;
    font-weight: 600;
}

.value-positive { color: var(--success); }
.value-medium { color: var(--warning); }
.value-high { color: var(--danger); }

.info-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.info-item {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
}

.info-item .label {
    color: var(--text-muted);
}

.info-item .value {
    font-weight: 500;
    color: var(--text-main);
}

.crm-select {
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    border-radius: 6px;
    padding: 2px 6px;
    font-size: 0.8rem;
    outline: none;
}

.tags-container {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 10px;
}

.tag {
    font-size: 0.7rem;
    padding: 3px 8px;
    border-radius: 4px;
    background-color: rgba(99, 102, 241, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.2);
    color: #a5b4fc;
}

.tag-input-wrapper input {
    width: 100%;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 6px 10px;
    color: var(--text-main);
    font-size: 0.75rem;
    outline: none;
}

.crm-section textarea {
    width: 100%;
    height: 70px;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px;
    color: var(--text-main);
    font-size: 0.8rem;
    resize: none;
    outline: none;
    margin-bottom: 8px;
    font-family: var(--font-sans);
}

/* ==========================================
   AI CONFIGURATION PAGE
   ========================================== */
.settings-layout {
    padding: 32px 40px;
    overflow-y: auto;
    height: 100%;
}

.settings-header {
    margin-bottom: 24px;
}

.settings-header h2 {
    font-family: var(--font-heading);
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 6px;
}

.settings-header p {
    color: var(--text-muted);
    font-size: 0.95rem;
}

.settings-grid {
    display: grid;
    grid-template-columns: 1.2fr 1fr;
    gap: 24px;
    margin-bottom: 24px;
}

.settings-card {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 24px;
}

.settings-card h3 {
    font-family: var(--font-heading);
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 6px;
}

.settings-card .description {
    color: var(--text-muted);
    font-size: 0.8rem;
    margin-bottom: 16px;
    line-height: 1.4;
}

.settings-textarea {
    width: 100%;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 14px;
    color: var(--text-main);
    font-family: var(--font-sans);
    font-size: 0.85rem;
    line-height: 1.5;
    outline: none;
    resize: vertical;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.form-group label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-main);
    display: flex;
    justify-content: space-between;
}

.form-group input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
}

.help-text {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.knowledge-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-height: 250px;
    overflow-y: auto;
    margin-bottom: 16px;
    padding-right: 6px;
}

.faq-item {
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 12px;
    position: relative;
}

.faq-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
}

.faq-item-trigger {
    font-size: 0.8rem;
    font-weight: 700;
    color: #a5b4fc;
}

.btn-delete-faq {
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
    font-size: 0.75rem;
}

.faq-item-response {
    font-size: 0.8rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.new-faq-form {
    border-top: 1px solid var(--border-color);
    padding-top: 16px;
}

.new-faq-form h4 {
    font-family: var(--font-heading);
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 10px;
}

.form-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.form-row input {
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 8px 12px;
    color: var(--text-main);
    font-size: 0.8rem;
    outline: none;
}

.settings-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* ==========================================
   ANALYTICS & METRICS
   ========================================== */
.analytics-layout {
    padding: 32px 40px;
    overflow-y: auto;
    height: 100%;
}

.analytics-layout h2 {
    font-family: var(--font-heading);
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 6px;
}

.analytics-layout p {
    color: var(--text-muted);
    font-size: 0.95rem;
    margin-bottom: 24px;
}

.metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
    margin-bottom: 32px;
}

.metric-card {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

.metric-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 12px;
}

.metric-card h3 {
    font-family: var(--font-heading);
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 6px;
    background: linear-gradient(to right, #ffffff, #d1d5db);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.metric-sub {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.pill-green {
    background-color: rgba(16, 185, 129, 0.1);
    color: var(--success);
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 600;
}

.pill-yellow {
    background-color: rgba(245, 158, 11, 0.1);
    color: var(--warning);
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 600;
}

.mini-bar-chart {
    display: flex;
    justify-content: space-around;
    height: 80px;
    align-items: flex-end;
    margin-top: 10px;
}

.bar-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    width: 20%;
}

.bar {
    width: 100%;
    border-radius: 3px 3px 0 0;
}
.bar.wa { background-color: var(--wa-color); }
.bar.ig { background: var(--ig-gradient); }
.bar.fb { background-color: var(--fb-color); }
.bar.tk { background-color: var(--tk-color); }

.bar-val {
    font-size: 0.65rem;
    font-weight: 600;
}
.bar-lbl {
    font-size: 0.65rem;
    color: var(--text-muted);
}

/* CONSOLE MONITOR */
.console-card {
    background-color: #06070a;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 16px;
    font-family: 'Courier New', Courier, monospace;
}

.console-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 10px;
    margin-bottom: 12px;
}

.console-header h3 {
    font-size: 0.85rem;
    color: var(--text-muted);
    font-weight: 600;
}

.console-body {
    max-height: 200px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.log-line {
    font-size: 0.8rem;
    line-height: 1.4;
}

.log-line.info { color: #9ca3af; }
.log-line.success { color: #34d399; }
.log-line.warn { color: #fbbf24; }
.log-line.error { color: #f87171; }

/* ==========================================
   CONTACTS DIRECTORY
   ========================================== */
.contacts-layout {
    padding: 32px 40px;
    overflow-y: auto;
    height: 100%;
}

.contacts-header {
    margin-bottom: 24px;
}

.contacts-header h2 {
    font-family: var(--font-heading);
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 6px;
}

.contacts-header p {
    color: var(--text-muted);
    font-size: 0.95rem;
}

.contacts-table {
    width: 100%;
    border-collapse: collapse;
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
}

.contacts-table th, 
.contacts-table td {
    padding: 16px 20px;
    text-align: left;
    font-size: 0.85rem;
}

.contacts-table th {
    background-color: rgba(0, 0, 0, 0.2);
    font-family: var(--font-heading);
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border-color);
}

.contacts-table td {
    border-bottom: 1px solid var(--border-color);
}

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

.contacts-table tr:hover td {
    background-color: rgba(255, 255, 255, 0.01);
}

/* ==========================================
   BUTTONS & FORM CONTROLS
   ========================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    /* Padding por defecto: los botones sin modificador de tamaño (.btn-sm/.btn-xs)
       quedaban sin padding y apretados (ej. acciones de Configuración IA). (ABR-1276) */
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    border: none;
}

.btn-primary {
    background-color: var(--accent);
    color: white;
}

.btn-primary:hover {
    background-color: var(--accent-hover);
    box-shadow: 0 4px 12px var(--accent-glow);
}

.btn-secondary {
    background-color: var(--bg-tertiary);
    color: var(--text-main);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: rgba(255, 255, 255, 0.08);
    border-color: var(--border-hover);
}

.btn-outline {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-main);
}

.btn-outline:hover {
    background-color: rgba(255, 255, 255, 0.03);
    border-color: var(--border-hover);
}

.btn-outline-danger {
    background: none;
    border: 1px solid rgba(239, 68, 68, 0.3);
    color: #fca5a5;
}

.btn-outline-danger:hover {
    background-color: rgba(239, 68, 68, 0.1);
    border-color: var(--danger);
}

.btn-full {
    width: 100%;
}

.btn-circle {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    flex-shrink: 0;
    padding: 0; /* tamaño fijo: no debe heredar el padding base del .btn */
}
/* El SVG del botón circular (enviar) no trae width/height propios; sin esto se
   expande hasta desbordar el círculo. Lo acotamos a un tamaño proporcionado. */
.btn-circle svg {
    width: 20px;
    height: 20px;
}

.btn-sm {
    padding: 8px 16px;
    font-size: 0.8rem;
}

.btn-xs {
    padding: 6px 10px;
    font-size: 0.75rem;
}

/* ==========================================
   UTILIDADES
   ========================================== */
.hidden { display: none !important; }
.mt { margin-top: 16px; }
.mt-lg { margin-top: 24px; }

/* ==========================================
   COMMENT CONTEXT BAR STYLES
   ========================================== */
.comment-context-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: rgba(99, 102, 241, 0.08);
    border-bottom: 1px solid rgba(99, 102, 241, 0.15);
    padding: 12px 24px;
    animation: fadeIn 0.25s ease-out;
}

.comment-context-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.comment-context-icon {
    width: 32px;
    height: 32px;
    background-color: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--accent);
}

.comment-context-icon svg {
    width: 16px;
    height: 16px;
}

.comment-context-text-wrapper {
    display: flex;
    flex-direction: column;
}

.comment-context-title {
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--text-main);
}

.comment-context-body {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-style: italic;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 400px;
}

.badge-comment-public {
    background-color: rgba(99, 102, 241, 0.2);
    color: #a5b4fc;
    border: 1px solid rgba(99, 102, 241, 0.3);
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 4px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.badge-comment-item {
    background-color: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #cbd5e1;
}

/* ==========================================
   INTEGRATE ABROAD BRANDING & ROLES
   ========================================== */
.dot-iab {
    background-color: #0d9488;
}

.channel-badge.integrate-abroad {
    background-color: #0f172a !important;
    border-color: #0d9488 !important;
}

.platform-pill.integrate-abroad {
    background-color: rgba(13, 148, 136, 0.15);
    color: #2dd4bf;
    border: 1px solid rgba(13, 148, 136, 0.3);
}

.role-badge {
    font-size: 0.7rem;
    padding: 2px 8px;
    border-radius: 4px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    display: inline-block;
    margin-left: 8px;
}

.role-badge.abroader {
    background-color: rgba(13, 148, 136, 0.15);
    color: #2dd4bf;
    border: 1px solid rgba(13, 148, 136, 0.3);
}

.role-badge.abogada {
    background-color: rgba(245, 158, 11, 0.15);
    color: #fbbf24;
    border: 1px solid rgba(245, 158, 11, 0.3);
}

/* Animación de aparición (usada por menús / popovers) */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Feedback de validación en campos requeridos del onboarding */
.input-error {
    border-color: var(--danger) !important;
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.18) !important;
    animation: shakeError 0.35s ease-in-out;
}

@keyframes shakeError {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

/* ===== Badges de Área / Departamento (ruteo de la IA) ===== */
.dept-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.68rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 999px;
    line-height: 1.4;
    white-space: nowrap;
    border: 1px solid transparent;
}

.dept-comercial {
    background-color: rgba(34, 197, 94, 0.16);
    color: #4ade80;
    border-color: rgba(34, 197, 94, 0.35);
}

.dept-marketing {
    background-color: rgba(168, 85, 247, 0.16);
    color: #c084fc;
    border-color: rgba(168, 85, 247, 0.35);
}

.dept-legal {
    background-color: rgba(59, 130, 246, 0.16);
    color: #60a5fa;
    border-color: rgba(59, 130, 246, 0.35);
}

.dept-it {
    background-color: rgba(245, 158, 11, 0.16);
    color: #fbbf24;
    border-color: rgba(245, 158, 11, 0.35);
}

.dept-badge.hidden {
    display: none !important;
}

/* ==========================================================================
   RESPONSIVE / PWA — adaptación a cualquier dispositivo
   --------------------------------------------------------------------------
   Estrategia:
     · ≥1024px  → escritorio (4 paneles, como siempre).
     · 600-1023 → tablet: nav lateral pasa a barra inferior + drawer;
                  Inbox a 2 paneles (lista + chat) con ficha deslizable.
     · <600px   → móvil: flujo de una sola pantalla (lista → chat → ficha).
   El "chrome" móvil (topbar, bottom-nav, scrims, botones atrás/ficha) lo
   inyecta pwa.js y aquí se le da estilo. En escritorio está oculto.
   ========================================================================== */

:root {
    --topbar-h: 56px;
    --bottomnav-h: 62px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

/* Altura dinámica del viewport (evita saltos con las barras del navegador
   móvil y el teclado virtual). */
@supports (height: 100dvh) {
    html, body, .app-container { height: 100dvh; }
}

/* Imágenes/medios nunca desbordan. */
img, svg, video { max-width: 100%; }

/* Mejor experiencia táctil. */
* { -webkit-tap-highlight-color: transparent; }
body { text-rendering: optimizeLegibility; }
body.no-scroll { overflow: hidden; }

/* El "chrome" móvil está oculto por defecto (vista escritorio). */
.mobile-topbar,
.mobile-bottom-nav,
.nav-scrim,
.crm-scrim,
.chat-back-btn,
.chat-info-btn { display: none; }

/* --------------------------------------------------------------------------
   COMPONENTES DEL CHROME MÓVIL (estilo base; se muestran vía media queries)
   -------------------------------------------------------------------------- */

/* Top app bar */
.mobile-topbar {
    align-items: center;
    gap: 6px;
    height: calc(var(--topbar-h) + var(--safe-top));
    padding: var(--safe-top) 6px 0 6px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
    position: relative;
    z-index: 40;
}
.mtb-btn {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-main);
    cursor: pointer;
    border-radius: 10px;
    transition: var(--transition);
}
.mtb-btn:active { background: rgba(255, 255, 255, 0.06); }
.mtb-btn svg { width: 22px; height: 22px; }
.mtb-title {
    flex: 1;
    min-width: 0;
    text-align: center;
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: 1.05rem;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.mtb-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: 0.78rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Bottom navigation */
.mobile-bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--bottomnav-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background-color: var(--bg-secondary); /* fallback */
    background-color: color-mix(in srgb, var(--bg-secondary) 92%, transparent);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--border-color);
    /* Sin `display` aquí: lo controla el default `display:none` (escritorio/tablet)
       y la media query ≤599px que lo pone en `flex` (solo móvil). Antes había un
       `display:flex` fijo que pisaba el `none` y la mostraba en todos los anchos (ABR-1276). */
    justify-content: space-around;
    align-items: stretch;
    z-index: 50;
}
.mbn-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* Más aire entre ícono y label para que el resaltado del activo no se pegue
       al texto (ABR-1276). */
    gap: 6px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px 2px;
    transition: color 0.2s ease;
    position: relative;
}
.mbn-icon { position: relative; display: flex; }
.mbn-icon svg { width: 23px; height: 23px; }
.mbn-label {
    font-size: 0.62rem;
    font-weight: 600;
    letter-spacing: 0.01em;
}
.mbn-item.active { color: var(--accent); }
/* Resaltado del ítem activo: pill detrás del ÍCONO únicamente. El inset vertical
   (-5px) es menor que el gap al label (6px), así nunca toca el texto. */
.mbn-item.active .mbn-icon::after {
    content: "";
    position: absolute;
    inset: -5px -10px;
    background: var(--accent-glow);
    border-radius: 10px;
    z-index: -1;
}
.mbn-badge {
    position: absolute;
    top: -4px;
    right: -8px;
    background: var(--danger);
    color: #fff;
    font-size: 0.6rem;
    font-weight: 700;
    min-width: 15px;
    height: 15px;
    padding: 0 4px;
    border-radius: 999px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--bg-secondary);
}

/* Banner de estado de conexión (API real vs demo). Posición base: pill en la
   esquina inferior derecha (escritorio/tablet). En móvil se recoloca arriba-izq
   (ver bloque ≤599px). pointer-events:none → nunca bloquea la UI debajo. */
#api-banner {
    position: fixed;
    bottom: 10px;
    right: 10px;
    z-index: 9998;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.72rem;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 999px;
    pointer-events: none;
}
#api-banner.api-banner--ok {
    background: rgba(16, 185, 129, 0.16);
    color: #34d399;
    border: 1px solid rgba(16, 185, 129, 0.4);
}
#api-banner.api-banner--demo {
    background: rgba(245, 158, 11, 0.16);
    color: #fbbf24;
    border: 1px solid rgba(245, 158, 11, 0.4);
}
.api-banner-dot { line-height: 1; }

/* Menú del perfil (topbar móvil/tablet): única acción = cerrar sesión. Tocar el
   avatar abre esto en vez del drawer (ABR-1276). Se ancla bajo el avatar (der). */
.profile-menu {
    position: fixed;
    top: calc(var(--safe-top) + var(--topbar-h) - 2px);
    right: 8px;
    z-index: 70;
    min-width: 168px;
    padding: 6px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
    display: none;
}
.profile-menu.open { display: block; }
.profile-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 11px 12px;
    background: none;
    border: none;
    border-radius: 8px;
    color: var(--text-main);
    font-size: 0.9rem;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
}
.profile-menu-item svg { width: 18px; height: 18px; }
.profile-menu-item:active { background: rgba(255, 255, 255, 0.06); }

/* Scrim del drawer */
.nav-scrim {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 55;
}
.nav-scrim.visible { opacity: 1; pointer-events: auto; }

/* Botones Atrás / Ficha dentro de la cabecera del chat */
.chat-back-btn,
.chat-info-btn {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-main);
    cursor: pointer;
    border-radius: 10px;
    transition: var(--transition);
}
.chat-back-btn svg, .chat-info-btn svg { width: 22px; height: 22px; }
.chat-back-btn:active, .chat-info-btn:active { background: rgba(255, 255, 255, 0.06); }

/* Scrim de la ficha CRM deslizable */
.crm-scrim {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 25;
}
.inbox-layout.show-profile .crm-scrim { opacity: 1; pointer-events: auto; }

/* ==========================================================================
   ESCRITORIO GRANDE (≥1440px) — aprovecha el espacio extra
   ========================================================================== */
@media (min-width: 1440px) {
    .chats-sidebar { width: 360px; }
    .crm-sidebar { width: 320px; }
    .message { max-width: 62%; }
}

/* ==========================================================================
   TABLET Y MÓVIL (≤1023px) — aparece el chrome móvil, la sidebar es drawer
   ========================================================================== */
@media (max-width: 1023px) {
    .mobile-topbar { display: flex; }
    /* La bottom-nav NO se muestra en tablet (ABR-1276): la navegación entre
       secciones ya la cubre la sidebar/drawer (botón hamburguesa de la topbar).
       La bottom-nav queda reservada solo para móvil (ver bloque ≤599px). */

    /* La nav lateral se convierte en drawer fuera de pantalla. */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        width: 284px;
        max-width: 82vw;
        padding-top: var(--safe-top);
        transform: translateX(-100%);
        transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 60;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.45);
    }
    .sidebar.drawer-open { transform: translateX(0); }

    /* El contenido principal ocupa todo el ancho. El sitio reservado para la
       bottom-nav se aplica solo en móvil (donde la bottom-nav es visible). */

    /* La pestaña activa pasa a ser un flex-item que rellena la altura
       (debajo de la topbar y encima de la bottom-nav). */
    .tab-pane { height: auto; }
    .tab-pane.active {
        display: flex;
        flex-direction: column;
        flex: 1 1 auto;
        min-height: 0;
    }

    /* La ficha CRM se vuelve un panel deslizable sobre el chat. */
    .inbox-layout { position: relative; overflow: hidden; }
    .crm-sidebar {
        position: absolute;
        top: 0;
        right: 0;
        left: auto;
        height: 100%;
        width: 330px;
        max-width: 88%;
        transform: translateX(100%);
        transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 30;
        box-shadow: -4px 0 24px rgba(0, 0, 0, 0.45);
    }
    .inbox-layout.show-profile .crm-sidebar { transform: translateX(0); }
    .crm-scrim { display: block; }

    /* Botón para abrir la ficha (en cabecera del chat). */
    .chat-info-btn { display: flex; }

    /* Botón "Volver al chat" dentro de la ficha (panel deslizable). Sticky arriba
       y a ancho completo (compensa el padding del panel). (ABR-1276) */
    .crm-back-btn {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        position: sticky;
        top: calc(-1 * 24px);
        margin: -24px -20px 16px;
        padding: 14px 16px;
        width: calc(100% + 40px);
        background: var(--bg-secondary);
        border: none;
        border-bottom: 1px solid var(--border-color);
        color: var(--text-main);
        font-weight: 600;
        font-size: 0.9rem;
        cursor: pointer;
        z-index: 5;
    }
    .crm-back-btn:active { background: var(--bg-tertiary); }
    .crm-back-btn svg { width: 20px; height: 20px; }

    /* Cabecera del chat más compacta. */
    .chat-header-bar { padding: 12px 14px; gap: 10px; }
    .ai-mode-selector .mode-label { display: none; }
    .active-contact-info { gap: 10px; }
    .active-contact-info .avatar { width: 40px; height: 40px; }

    /* Márgenes internos del chat más ajustados. */
    .copilot-panel { margin: 0 14px 10px 14px; }
    .chat-input-area { padding: 12px 14px; }
    .messages-container { padding: 18px 14px; }
    .comment-context-bar { padding: 10px 14px; }

    /* Layouts de páginas con menos padding. */
    .settings-layout,
    .analytics-layout,
    .contacts-layout { padding: 20px 18px; }
    .settings-grid { grid-template-columns: 1fr; gap: 16px; }
    .settings-header h2,
    .analytics-layout h2,
    .contacts-header h2 { font-size: 1.45rem; }

    /* Tabla de contactos: scroll horizontal suave. */
    .contacts-layout { overflow: auto; -webkit-overflow-scrolling: touch; }
    .contacts-table { min-width: 620px; }
}

/* ==========================================================================
   MÓVIL (≤599px) — flujo de una sola pantalla
   ========================================================================== */
@media (max-width: 599px) {
    /* La bottom-nav solo se ve en móvil (ABR-1276) y el contenido reserva su
       alto para que nada quede tapado por ella. */
    .mobile-bottom-nav { display: flex; }
    .main-content { padding-bottom: calc(var(--bottomnav-h) + var(--safe-bottom)); }

    /* Quitamos el menú hamburguesa en móvil: la bottom-nav ya cubre la navegación
       entre secciones (el drawer con perfil/logout sigue accesible tocando el
       avatar). Se usa visibility para conservar el hueco donde irá el indicador
       de conexión. (ABR-1276) */
    #mtb-menu { visibility: hidden; }

    /* El banner de conexión se recoloca donde estaba el hamburguesa (arriba-izq)
       y se compacta a solo el punto de estado, para no tapar el título ni la
       bottom-nav inferior. El texto completo queda para escritorio. */
    #api-banner {
        /* Centrado vertical al medio del topbar para alinearse con el avatar
           (que también está centrado en el topbar). (ABR-1276) */
        top: calc(var(--safe-top) + var(--topbar-h) / 2);
        transform: translateY(-50%);
        left: 10px;
        bottom: auto;
        right: auto;
        padding: 5px 7px;
    }
    #api-banner .api-banner-text { display: none; }
    /* Dentro de un chat la topbar se oculta y la cabecera del chat usa esa esquina
       para el botón "Atrás": ocultamos el banner para no superponerlos. */
    body.inbox-chat-open #api-banner { display: none; }

    /* Al abrir un chat ocultamos la topbar global (la cabecera del chat ya
       tiene su propio botón "Atrás" y el nombre del contacto). */
    body.inbox-chat-open .mobile-topbar { display: none; }

    /* La lista de chats ocupa toda la pantalla... */
    .chats-sidebar {
        width: 100%;
        flex-shrink: 1;
        border-right: none;
    }
    .chats-header { padding: 14px 14px 10px; }
    .chats-header h2 { display: none; } /* el título ya está en la topbar */

    /* ...y el chat entra deslizándose por encima. */
    .chat-viewport {
        position: absolute;
        inset: 0;
        transform: translateX(100%);
        transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 20;
        background: var(--bg-primary);
    }
    .inbox-layout.show-chat .chat-viewport { transform: translateX(0); }

    /* La ficha CRM a pantalla completa. */
    .crm-sidebar { width: 100%; max-width: 100%; }

    /* Botón "Atrás" visible dentro del chat. */
    .chat-back-btn { display: flex; }

    /* Cabecera del chat en móvil: en una sola línea no entra (back + avatar +
       nombre + selector Copiloto/Autopiloto + info se superponen). La partimos en
       dos filas: identidad arriba; selector IA + info abajo, a lo ancho. (ABR-1276) */
    .chat-header-bar { flex-wrap: wrap; row-gap: 10px; }
    .active-contact-info { min-width: 0; flex: 1; }
    /* Acciones (Copiloto/Autopiloto + info) ARRIBA, a lo ancho; la identidad
       (atrás + avatar + nombre) queda en la fila de abajo. order:-1 las adelanta. */
    .chat-actions-header { flex-basis: 100%; order: -1; justify-content: space-between; }
    .name-row { flex-wrap: wrap; gap: 6px; }
    .name-row h3 {
        font-size: 1rem;
        max-width: 60vw;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    #active-status-text {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 70vw;
    }
    .ai-mode-selector { padding: 2px; }
    .mode-toggle-btn { padding: 6px 9px; font-size: 0.72rem; }

    /* Burbujas un poco más anchas para aprovechar la pantalla. */
    .message { max-width: 86%; }

    /* Métricas: una sola columna fluida. */
    .metrics-grid { grid-template-columns: 1fr; gap: 14px; }
    .metric-card h3 { font-size: 1.7rem; }
    .console-body { max-height: 160px; }

    /* Acciones de configuración a lo ancho. */
    .settings-actions { flex-direction: column-reverse; }
    .settings-actions .btn { width: 100%; }
    .new-faq-form .form-row .btn { width: 100%; }

    /* Drawer un poco más ancho en pantallas muy pequeñas. */
    .sidebar { width: 86vw; max-width: 320px; }

    /* Evita el zoom automático de iOS al enfocar campos (necesita ≥16px). */
    input,
    textarea,
    select,
    .cfg-input,
    .search-box input,
    .input-form textarea,
    .copilot-body textarea { font-size: 16px; }

    /* Objetivos táctiles cómodos. */
    .chip, .filter-btn { padding: 7px 12px; font-size: 0.78rem; }
    .nav-item { padding: 14px 16px; }
}

/* ==========================================================================
   MÓVIL EN HORIZONTAL (poca altura) — compacta verticales
   ========================================================================== */
@media (max-height: 480px) and (orientation: landscape) {
    :root { --topbar-h: 46px; --bottomnav-h: 52px; }
    .messages-container { padding-top: 10px; padding-bottom: 10px; }
    .input-actions-bar { margin-bottom: 4px; }
}

/* ==========================================================================
   AJUSTES PWA (app instalada) y accesibilidad
   ========================================================================== */
/* Cuando corre como app instalada, el área segura ya la maneja el SO. */
.pwa-standalone .mobile-topbar { padding-top: max(var(--safe-top), 8px); }

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}

/* Foco visible accesible para navegación por teclado. */
.nav-item:focus-visible,
.btn:focus-visible,
.mbn-item:focus-visible,
.mtb-btn:focus-visible,
.chat-back-btn:focus-visible,
.chat-info-btn:focus-visible,
.chip:focus-visible,
.filter-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ============================================================
   Modal: Enviar plantilla WhatsApp
   Anclado al body, overlay + ficha centrada. Reusa el dark theme
   del CRM. Se muestra/oculta toggleando .hidden.
   ============================================================ */
.tpl-modal-scrim {
    position: fixed;
    inset: 0;
    background: rgba(8, 10, 16, 0.78);
    backdrop-filter: blur(4px);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}
.tpl-modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    width: 100%;
    max-width: 480px;
    max-height: calc(100vh - 32px);
    display: flex;
    flex-direction: column;
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.6);
}
.tpl-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 20px 14px;
    border-bottom: 1px solid var(--border-color);
}
.tpl-modal-header h3 {
    font-family: 'Outfit', sans-serif;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-main);
    margin: 0;
}
.tpl-modal-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.tpl-modal-close:hover {
    background: var(--bg-tertiary);
    color: var(--text-main);
}
.tpl-modal-body {
    padding: 16px 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.tpl-modal-hint {
    color: var(--text-muted);
    font-size: 0.82rem;
    margin: 0 0 4px;
    line-height: 1.4;
}
.tpl-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.tpl-field-label {
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 500;
}
.tpl-field-label small {
    color: var(--text-muted);
    opacity: 0.7;
    font-weight: 400;
    margin-left: 4px;
}
.tpl-field input,
.tpl-field select {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    border-radius: 8px;
    padding: 9px 11px;
    font-size: 0.88rem;
    font-family: inherit;
    width: 100%;
    box-sizing: border-box;
}
.tpl-field input:focus,
.tpl-field select:focus {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    border-color: transparent;
}
.tpl-field-advanced {
    margin-top: 4px;
    padding: 8px 0 0;
    border-top: 1px dashed var(--border-color);
}
.tpl-field-advanced > summary {
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.8rem;
    padding: 6px 0;
    list-style: none;
}
.tpl-field-advanced > summary::-webkit-details-marker { display: none; }
.tpl-field-advanced > summary::before {
    content: '▸';
    display: inline-block;
    margin-right: 6px;
    transition: transform 0.15s;
}
.tpl-field-advanced[open] > summary::before {
    transform: rotate(90deg);
}
.tpl-field-advanced .tpl-field {
    margin-top: 10px;
}
.tpl-modal-error {
    background: rgba(248, 113, 113, 0.08);
    border: 1px solid rgba(248, 113, 113, 0.25);
    color: #fca5a5;
    border-radius: 8px;
    padding: 8px 11px;
    font-size: 0.82rem;
    line-height: 1.4;
}
.tpl-modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 20px 18px;
    border-top: 1px solid var(--border-color);
}
.tpl-modal-submit-loading {
    opacity: 0.65;
    pointer-events: none;
}
