/* ==============================================================================
   PLAZA LAMA - FLAT UI LOGISTICS SYSTEM
   Colores Corporativos:
     --bg-main: #FFFFFA (Blanco base de fondo)
     --primary: #2698D6 (Azul Plaza Lama)
     --secondary: #E80010 (Rojo Plaza Lama)
   Bordes 1px, esquinas rectas (0px), sin sombras, estética flat limpia de alta fidelidad
   ============================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;500;600;700;800&display=swap');

:root {
    --bg-main: #FFFFFA;
    --bg-panel: #FFFFFF;
    --bg-header: #FFFFFF;
    --bg-hover: #F0F7FD;
    --bg-subtle: #F8FAFC;
    --border-color: #E2E8F0;
    --border-dark: #CBD5E1;
    --primary: #2698D6;
    --primary-hover: #1E82B8;
    --primary-light: #EBF6FC;
    --secondary: #E80010;
    --secondary-hover: #C5000D;
    --secondary-light: #FFF1F2;
    --text-primary: #0F172A;
    --text-secondary: #64748B;
    --text-muted: #94A3B8;
    --bubble-sent: #EBF6FC;
    --bubble-sent-border: #BAE6FD;
    --bubble-recv: #FFFFFF;
    --bubble-recv-border: #E2E8F0;
    --accent-red: #E80010;
    --accent-blue: #2698D6;
    --accent-yellow: #D97706;
    --accent-green: #10B981;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border-radius: 0 !important;
    box-shadow: none !important;
    font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    outline: none;
}

body {
    background-color: var(--bg-main);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Scrollbars Flat */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
::-webkit-scrollbar-track {
    background: var(--bg-main);
}
::-webkit-scrollbar-thumb {
    background: var(--border-dark);
}
::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* ==============================================================================
   TOP NAVIGATION BAR
   ============================================================================== */
.top-navbar {
    height: 30px;
    background-color: var(--bg-header);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
    z-index: 100;
}

.brand-section {
    display: flex;
    align-items: center;
    gap: 0;
}

.brand-logo-img {
    height: 22px;
    width: auto;
    max-width: 90px;
    object-fit: contain;
    display: block;
}

.brand-title {
    font-size: 13px;
    font-weight: 700;
    color: var(--primary);
    letter-spacing: -0.2px;
}

.brand-badge {
    font-size: 9px;
    padding: 1px 4px;
    background: var(--secondary-light);
    border: 1px solid var(--secondary);
    color: var(--secondary);
    font-weight: 700;
    text-transform: uppercase;
}

.nav-tabs {
    display: flex;
    height: 100%;
}

.nav-tab-btn {
    background: transparent;
    border: none;
    border-left: 1px solid var(--border-color);
    border-right: 1px solid transparent;
    color: var(--text-secondary);
    padding: 0 10px;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    height: 100%;
    transition: none;
}

.nav-tab-btn:hover {
    background: var(--bg-hover);
    color: var(--primary);
}

.nav-tab-btn.active {
    background: var(--primary-light);
    color: var(--primary);
    border-bottom: 2px solid var(--primary);
}

.user-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.status-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: var(--text-primary);
    padding: 2px 6px;
    border: 1px solid var(--border-color);
    background: var(--bg-subtle);
    font-weight: 500;
}

.status-dot {
    width: 6px;
    height: 6px;
    background-color: var(--primary);
    display: inline-block;
}

/* ==============================================================================
   MAIN CONTAINER & PANELS
   ============================================================================== */
.main-content {
    flex: 1;
    display: flex;
    height: calc(100vh - 30px);
    overflow: hidden;
}

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

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

/* ==============================================================================
   CHATS VIEW
   ============================================================================== */
.chats-container {
    display: flex;
    width: 100%;
    height: 100%;
}

.chats-sidebar {
    width: 380px;
    min-width: 320px;
    background: var(--bg-panel);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-subtle);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-search {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-panel);
}

.flat-input {
    width: 100%;
    background: var(--bg-panel);
    border: 1px solid var(--border-dark);
    color: var(--text-primary);
    padding: 8px 12px;
    font-size: 13px;
    transition: none;
}

.flat-input:focus {
    border-color: var(--primary);
    outline: none;
}

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

.chat-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    background: transparent;
    transition: none;
}

.chat-item:hover {
    background: var(--bg-hover);
}

.chat-item.active {
    background: var(--primary-light);
    border-left: 4px solid var(--primary);
}

.chat-avatar {
    width: 44px;
    height: 44px;
    background: var(--primary-light);
    border: 1px solid var(--bubble-sent-border);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 12px;
    font-weight: 700;
    color: var(--primary);
    flex-shrink: 0;
}

.chat-info {
    flex: 1;
    min-width: 0;
}

.chat-top-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
}

.chat-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-time {
    font-size: 11px;
    color: var(--text-secondary);
}

.chat-last-msg {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.unread-badge {
    background: var(--secondary);
    color: #FFFFFF;
    font-size: 11px;
    font-weight: 700;
    padding: 1px 6px;
    margin-left: 6px;
    display: inline-block;
}

/* Chat Main Window */
.chat-window {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-main);
}

.chat-header {
    height: 56px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
}

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

.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 16px 24px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--bg-main);
}

.bubble-wrap {
    display: flex;
    flex-direction: column;
    max-width: 75%;
}

.bubble-wrap.sent {
    align-self: flex-end;
}

.bubble-wrap.recv {
    align-self: flex-start;
}

.bubble {
    padding: 8px 12px;
    font-size: 13.5px;
    line-height: 1.4;
    word-break: break-word;
    border: 1px solid var(--border-color);
    position: relative;
    background: var(--bg-panel);
    color: var(--text-primary);
}

.bubble-wrap.sent .bubble {
    background: var(--bubble-sent);
    border-color: var(--bubble-sent-border);
}

.bubble-wrap.recv .bubble {
    background: var(--bubble-recv);
    border-color: var(--bubble-recv-border);
}

.bubble-meta {
    font-size: 10px;
    color: var(--text-secondary);
    text-align: right;
    margin-top: 4px;
}

.bubble-img {
    max-width: 280px;
    max-height: 280px;
    object-fit: cover;
    border: 1px solid var(--border-color);
    margin-bottom: 6px;
    display: block;
    cursor: pointer;
}

.chat-input-bar {
    height: 60px;
    background: var(--bg-panel);
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 10px;
}

.flat-btn {
    background: var(--primary);
    color: #FFFFFF;
    border: 1px solid var(--primary);
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: none;
}

.flat-btn:hover {
    background: var(--primary-hover);
    border-color: var(--primary-hover);
}

.flat-btn-outline {
    background: var(--bg-panel);
    color: var(--text-primary);
    border: 1px solid var(--border-dark);
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: none;
}

.flat-btn-outline:hover {
    background: var(--bg-hover);
    border-color: var(--primary);
    color: var(--primary);
}

.flat-btn-secondary {
    background: var(--secondary);
    color: #FFFFFF;
    border: 1px solid var(--secondary);
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: none;
}

.flat-btn-secondary:hover {
    background: var(--secondary-hover);
    border-color: var(--secondary-hover);
}

/* ==============================================================================
   COMPONENTE BURBUJA IA (REGLAS EXACTAS DE LA ESPECIFICACIÓN)
   ============================================================================== */
.ia-card { 
    background: #FFFFFF; 
    border: 1px solid var(--primary); 
    border-radius: 0; 
    box-shadow: none; 
    padding: 10px; 
    font-family: 'Outfit', sans-serif; 
    color: var(--text-primary); 
    font-size: 13px; 
    margin-top: 8px;
}

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

.ia-badge {
    font-size: 10.5px;
    font-weight: 700;
    padding: 2px 7px;
    text-transform: uppercase;
    border: 1px solid;
    display: inline-block;
    line-height: 1.3;
}

.ia-badge-recibo {
    background: #FEF3C7;
    border-color: #F59E0B;
    color: #92400E;
}

.ia-badge-factura {
    background: #E0F2FE;
    border-color: #0284C7;
    color: #0369A1;
}

.ia-badge-conduce {
    background: var(--primary-light);
    border-color: var(--primary);
    color: var(--primary);
    font-weight: 800;
}

.ia-badge-pendiente,
.ia-badge-facturado {
    background: #FEF9C3;
    border-color: #EAB308;
    color: #854D0E;
}

.ia-badge-programado {
    background: #E0F2FE;
    border-color: #38BDF8;
    color: #0369A1;
}

.ia-badge-enviado {
    background: #FFEDD5;
    border-color: #F97316;
    color: #9A3412;
    font-weight: 700;
}

.ia-badge-entregado {
    background: #D1FAE5;
    border-color: #10B981;
    color: #065F46;
    font-weight: 800;
}

.ia-badge-devuelto {
    background: var(--secondary-light);
    border-color: var(--secondary);
    color: #991B1B;
    font-weight: 700;
}

.ia-badge-delivery {
    background: #E0F2FE;
    border-color: var(--primary);
    color: #0284C7;
}

.ia-badge-instalador {
    background: #F3E8FF;
    border-color: #A855F7;
    color: #6B21A8;
}

.ia-badge-desconocido {
    background: var(--secondary-light);
    border-color: var(--secondary);
    color: #991B1B;
}

.ia-grid { 
    display: grid; 
    grid-template-columns: 125px 1fr; 
    border-top: 1px solid var(--border-color); 
    border-left: 1px solid var(--border-color); 
    margin-top: 6px; 
}

.ia-cell { 
    padding: 6px 8px; 
    border-right: 1px solid var(--border-color); 
    border-bottom: 1px solid var(--border-color); 
    word-break: break-word;
    background: #FFFFFF;
}

.ia-label { 
    background: var(--bg-subtle); 
    color: var(--text-secondary); 
    font-weight: 600; 
    font-size: 11px;
}

/* ==============================================================================
   PANEL DESPACHOS IA (TABLA Y GESTOR COMPLETO)
   ============================================================================== */
.despachos-pane {
    flex: 1;
    display: block;
    padding: 0 !important;
    overflow-y: auto;
    overflow-x: auto;
    background: var(--bg-main);
    position: relative;
    height: 100%;
}

.section-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 6px;
    gap: 10px;
    flex-wrap: wrap;
}

.filter-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.inline-stats-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    font-weight: 600;
    flex-wrap: wrap;
}

.stats-bar {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 8px;
    margin-bottom: 10px;
}

.stat-box {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    padding: 6px 12px;
    display: flex;
    flex-direction: column;
}

.stat-val {
    font-size: 18px;
    font-weight: 800;
    line-height: 1.1;
    color: var(--primary);
}

.stat-lbl {
    font-size: 10px;
    color: var(--text-secondary);
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.5px;
    margin-top: 1px;
}

.comm-nav-btn {
    border: 1px solid var(--border-color);
    background: #FFFFFF;
    color: var(--text-main);
    width: 17px;
    height: 17px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 8.5px;
    line-height: 1;
    border-radius: 0;
    transition: background 0.1s ease;
}
.comm-nav-btn:hover:not(:disabled) {
    background: var(--bg-hover);
    color: var(--primary);
    border-color: var(--primary);
}

.table-container {
    width: 100%;
    min-width: 100%;
    overflow: visible;
    background: var(--bg-panel);
    border-top: 1px solid var(--border-color);
}

.flat-table {
    width: 100%;
    min-width: 100%;
    border-collapse: separate !important;
    border-spacing: 0;
    font-size: 12px;
    text-align: left;
}

.flat-table th {
    background: #f1f5f9;
    color: #334155;
    padding: 5px 8px;
    border-bottom: 2px solid var(--primary);
    border-right: 1px solid var(--border-color);
    border-top: 1px solid var(--border-color);
    font-weight: 700;
    font-size: 11px;
    line-height: 1.2;
    text-transform: uppercase;
    letter-spacing: 0.2px;
    position: sticky;
    top: 0;
    z-index: 30;
    box-shadow: 0 2px 4px rgba(0,0,0,0.06);
}

.flat-table th:first-child {
    border-left: 1px solid var(--border-color);
}

.flat-table td {
    padding: 4px 8px;
    border-bottom: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
    color: var(--text-primary);
    background: #FFFFFF;
    font-size: 11.5px;
    line-height: 1.25;
}

.flat-table td:first-child {
    border-left: 1px solid var(--border-color);
}

.flat-table tr:hover td {
    background: var(--bg-hover);
}

/* Filas según antigüedad del documento */
.flat-table tr.row-antiguedad-verde td {
    background: #f0fdf4;
}
.flat-table tr.row-antiguedad-verde td:first-child {
    border-left: 5px solid #00a884;
}
.flat-table tr.row-antiguedad-verde:hover td {
    background: #dcfce7 !important;
}

.flat-table tr.row-antiguedad-amarillo td {
    background: #fefce8;
}
.flat-table tr.row-antiguedad-amarillo td:first-child {
    border-left: 5px solid #fbbc05;
}
.flat-table tr.row-antiguedad-amarillo:hover td {
    background: #fef08a !important;
}

.flat-table tr.row-antiguedad-naranja td {
    background: #fff7ed;
}
.flat-table tr.row-antiguedad-naranja td:first-child {
    border-left: 5px solid #ff9800;
}
.flat-table tr.row-antiguedad-naranja:hover td {
    background: #fed7aa !important;
}

.flat-table tr.row-antiguedad-rojo td {
    background: #fef2f2;
}
.flat-table tr.row-antiguedad-rojo td:first-child {
    border-left: 5px solid #ea4335;
}
.flat-table tr.row-antiguedad-rojo:hover td {
    background: #fee2e2 !important;
}

/* ==============================================================================
   MODAL / DRAWER
   ============================================================================== */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.6);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: var(--bg-panel);
    border: 2px solid var(--primary);
    width: 90%;
    max-width: 750px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
}

.modal-header {
    background: var(--bg-subtle);
    border-bottom: 1px solid var(--border-color);
    padding: 12px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-body {
    padding: 18px;
    overflow-y: auto;
    flex: 1;
    background: var(--bg-panel);
}

.modal-close {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
}

.modal-close:hover {
    color: var(--secondary);
}

pre.code-json {
    background: var(--bg-subtle);
    border: 1px solid var(--border-color);
    padding: 12px;
    font-family: monospace;
    font-size: 12px;
    color: #0369A1;
    overflow-x: auto;
    white-space: pre-wrap;
}
