:root {
    --bg-main: #06090F;
    --bg-glass: rgba(255, 255, 255, 0.04);
    --border-glass: rgba(255, 255, 255, 0.08);
    --primary: #2563EB;
    --primary-hover: #3B82F6;
    --text-primary: #F8FAFC;
    --text-secondary: #94A3B8;
}

* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Outfit', sans-serif; }

@keyframes pulse-red {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.4); }
    50%       { box-shadow: 0 0 0 6px rgba(239,68,68,0); }
}

/* Botones de comandos GPS en el HUD */
.cmd-gps-btn {
    background: rgba(56,189,248,0.05);
    border: 1px solid rgba(56,189,248,0.2);
    border-radius: 6px;
    color: #f1f5f9;
    font-size: 10px;
    font-weight: 600;
    padding: 7px 8px;
    cursor: pointer;
    font-family: 'Outfit', sans-serif;
    transition: all 0.2s;
    text-align: left;
}
.cmd-gps-btn:hover {
    background: rgba(56,189,248,0.12);
    border-color: rgba(56,189,248,0.4);
    color: #38bdf8;
}
.cmd-gps-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Calles del mapa en blanco: brighten roads on dark CartoDB tiles */
.map-tiles-bright {
    filter: brightness(1.8) contrast(1.1) saturate(0.3);
}

.map-tiles-bright-labels {
    filter: brightness(1.6) contrast(1.3);
    opacity: 0.7;
}

.map-google-dark {
    filter: invert(90%) hue-rotate(180deg) brightness(85%) contrast(90%);
}

body {
    background-color: var(--bg-main); 
    color: var(--text-primary);
    overflow: hidden;
    height: 100vh;
    /* Safe area para PWA standalone en iPhone */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    box-sizing: border-box;
}

/* Layout Core */
.layout-wrapper {
    display: flex;
    height: 100vh;
    width: 100vw;
}

.main-column {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

/* Context Topbar */
.topbar {
    height: 70px;
    display: flex; 
    align-items: center; 
    padding: 0 40px;
    border-bottom: 1px solid var(--border-glass);
    background: rgba(15, 23, 42, 0.5); /* Semi transparent for glass effect */
    backdrop-filter: blur(10px);
    z-index: 10;
}

.topbar label { 
    margin:0; 
    margin-right: 15px; 
    color: #38BDF8; 
    font-weight: 600;
}

.topbar select { 
    width: 300px; 
    padding: 8px 12px; 
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border-glass);
    border-radius: 8px;
    color: white;
}

/* Sidebar */
.sidebar {
    width: 260px; 
    background: rgba(10, 15, 28, 0.92); 
    backdrop-filter: blur(20px);
    border-right: 1px solid var(--border-glass); 
    padding: 25px 18px;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    height: 100vh;
    overflow-y: auto;
    overflow-x: hidden;
}
.logo h2 { font-weight: 800; background: linear-gradient(to right, #38BDF8, #818CF8); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 18px; }
.logo { margin-bottom: 20px; }
.nav-group-label { font-size: 9px; color: #334155; letter-spacing: 2px; font-weight: 700; text-transform: uppercase; padding: 12px 8px 6px; margin-top: 4px; }
nav a { display: flex; align-items: center; gap: 8px; color: var(--text-secondary); text-decoration: none; padding: 10px 12px; margin-bottom: 4px; border-radius: 8px; transition: all 0.2s; font-size: 14px; position: relative; border-left: 2px solid transparent; }
nav a:hover { background: rgba(56,189,248,0.07); color: #cbd5e1; }
nav a.active { background: rgba(56,189,248,0.12); color: #38BDF8; border-left: 2px solid #38BDF8; font-weight: 600; }
.nav-icon { font-size: 16px; width: 20px; text-align: center; flex-shrink: 0; }
.nav-live-dot { width: 6px; height: 6px; background: #10b981; border-radius: 50%; margin-left: auto; box-shadow: 0 0 6px #10b981; animation: pulse-green 2s infinite; }

/* Main Content Area */
.content { 
    flex: 1; 
    padding: 40px; 
    overflow-y: auto;
    overflow-x: hidden;
    background: radial-gradient(circle at top right, rgba(37, 99, 235, 0.05), transparent 40%); 
}
.tab-content { display: none; }
.tab-content.active { display: block; }

/* El mapa ocupa toda la columna restante (sin usar position:absolute) */
.map-section {
    display: none;
    flex: 1;
    flex-direction: column;
    padding: 10px;
    gap: 8px;
    min-height: 0;
}
.map-section.active {
    display: flex;
}
/* map-container fills remaining space */
.map-container {
    flex: 1;
    position: relative;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid rgba(255,255,255,0.08);
    min-height: 0;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

header h1 { font-size: 32px; margin-bottom: 8px; color: #E2E8F0; }
header p { color: var(--text-secondary); margin-bottom: 35px; font-size: 15px; }

.glass { background: var(--bg-glass); backdrop-filter: blur(12px); border: 1px solid var(--border-glass); border-radius: 12px; padding: 25px; }
.mt-4 { margin-top: 25px; }

/* Grid Forms Premium Density */
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; }
.form-group { margin-bottom: 0px; }
label { display: block; font-size: 12px; margin-bottom: 5px; color: var(--text-secondary); font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;}
input, select { width: 100%; padding: 8px 12px; background: rgba(15, 23, 42, 0.4); border: 1px solid var(--border-glass); border-radius: 6px; color: #F8FAFC; outline: none; transition: border-color 0.2s; font-size: 13px;}
input:focus, select:focus { border-color: #38BDF8; background: rgba(15, 23, 42, 0.8); }
input[type="file"] { padding: 6px; }

.btn { padding: 10px 20px; border: none; border-radius: 6px; cursor: pointer; font-weight: 600; font-size:13px; transition: all 0.2s; color:white; display:inline-block; }
.btn.primary { background: var(--primary); }
.btn.primary:hover { background: var(--primary-hover); transform: translateY(-1px); }

table { width: 100%; border-collapse: separate; border-spacing: 0; }
th, td { padding: 12px 10px; text-align: left; border-bottom: 1px solid var(--border-glass); font-size: 13px; }
th { color: var(--text-secondary); font-weight: 600; text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px;}

/* Modals Premium Fix */
.modal { display: none; position: fixed; z-index: 10000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.6); backdrop-filter:blur(5px); }
.modal-content { 
    background-color: #0f172a; 
    margin: 5vh auto; 
    width: 600px; 
    padding: 30px; 
    border-radius:12px; 
    border:1px solid var(--border-glass); 
    box-shadow: 0 10px 40px rgba(0,0,0,0.5); 
    max-height: 90vh; /* IMPORTANT: Evita que se coma los botones */
    overflow-y: auto; 
}

.map-section { height: calc(100vh - 115px); display: none; flex-direction: column; gap: 8px; }
.map-section.active { display: flex; }
.map-header { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom:15px;}
.map-selector { width: 300px; margin-bottom:0;}
.map-container { flex: 1; padding: 0; border-radius: 16px; overflow: hidden; position: relative; }
#leaflet-map { width: 100%; height: 100%; z-index: 1; }

/* Map Toolbar */
.map-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    background: rgba(10, 15, 28, 0.7);
    backdrop-filter: blur(10px);
    border: 1px solid var(--border-glass);
    border-radius: 10px;
    flex-shrink: 0;
    gap: 10px;
    flex-wrap: wrap;
}
.map-btn {
    padding: 6px 12px;
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 6px;
    color: #94a3b8;
    cursor: pointer;
    font-size: 12px;
    font-family: 'Outfit', sans-serif;
    transition: all 0.2s;
    white-space: nowrap;
}
.map-btn:hover { background: rgba(56,189,248,0.15); color: #38bdf8; border-color: rgba(56,189,248,0.3); }
.map-select {
    padding: 6px 12px;
    background: rgba(15,23,42,0.5);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 6px;
    color: white;
    font-family: 'Outfit', sans-serif;
    font-size: 13px;
    min-width: 200px;
}

/* Pulse animation */
@keyframes pulse-green {
    0%, 100% { opacity: 1; box-shadow: 0 0 6px #10b981; }
    50% { opacity: 0.6; box-shadow: 0 0 12px #10b981; }
}

.leaflet-control-zoom-in, .leaflet-control-zoom-out { filter: invert(100%) hue-rotate(180deg) brightness(95%) contrast(90%); }
/* Leaflet Dark Popup Overrides */
.leaflet-popup-content-wrapper {
    background: transparent;
    box-shadow: none;
    padding: 0;
}
.leaflet-popup-tip {
    background: rgba(15, 23, 42, 0.95);
}
.leaflet-popup-content {
    margin: 0 !important;
}

/* Responsive Fixes */
@media (max-width: 1024px) {
    .sidebar { width: 220px; padding: 25px 15px; }
    .topbar { padding: 0 20px; }
    .content { padding: 25px; }
}

@media (max-width: 768px) {
    /* Si el mapa está activo, no debe existir scroll en el body de toda la layout */
    body:has(.main-column.map-active) { overflow: hidden !important; }

    .layout-wrapper { display: flex; flex-direction: column; height: 100vh !important; }
    
    .sidebar { 
        position: fixed;
        top: 0;
        left: -320px;
        width: 280px; 
        height: 100vh;
        height: 100dvh;
        padding: 20px;
        /* Safe area top para cuando se abre como overlay en iPhone PWA */
        padding-top: calc(20px + env(safe-area-inset-top));
        border-right: 1px solid var(--border-glass);
        background: rgba(10, 15, 28, 0.98);
        z-index: 3000;
        transition: left 0.3s ease;
        flex-direction: column;
        overflow-y: auto;
    }
    .sidebar.open {
        left: 0;
    }

    /* Overlay oscuro al abrir sidebar en móvil */
    #sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.6);
        z-index: 2999;
        backdrop-filter: blur(2px);
    }
    #sidebar-overlay.visible {
        display: block;
    }
    
    .mobile-only { display: block !important; }
    .hide-on-mobile { display: none !important; }
    
    nav { 
        flex-direction: column;
        overflow-x: hidden;
    }
    nav a { 
        padding: 12px; 
        font-size: 14px;
        border-left: 2px solid transparent !important;
        border-bottom: none;
    }
    nav a.active { border-left: 2px solid #38BDF8 !important; border-bottom: none; }

    .main-column { flex: 1; height: 100vh !important; display: flex; flex-direction: column; overflow: hidden; position: relative; }
    
    .topbar { 
        display: flex !important; 
        padding: 8px 12px !important; 
        min-height: 50px !important; 
        align-items: center !important; 
        justify-content: space-between !important;
        background: rgba(10, 15, 28, 0.95) !important;
        border-bottom: 1px solid var(--border-glass) !important;
    }



    .content { padding: 12px; overflow: visible; }
    
    .grid-3, .grid-form { grid-template-columns: 1fr !important; }
    
    .modal-content { 
        width: 95% !important; 
        margin: 2vh auto !important; 
        padding: 15px !important; 
    }
    
    .map-section { 
        flex: 1; padding: 0 !important; gap: 0; 
    }
    .map-container { 
        border-radius: 0; border-left: none; border-right: none; border-bottom: none; 
    }
    .map-toolbar {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        overflow-x: scroll !important;
        overflow-y: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x !important;
        padding: 8px 10px !important;
        padding-top: calc(8px + env(safe-area-inset-top)) !important;
        gap: 8px !important;
        background: rgba(10, 15, 28, 0.95) !important;
        width: 100% !important;
        max-width: 100vw !important;
        box-sizing: border-box !important;
        z-index: 1000 !important;
        scrollbar-width: none !important;
    }
    .map-toolbar::-webkit-scrollbar {
        display: none !important;
    }

    .map-toolbar > div {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        align-items: center !important;
        gap: 6px !important;
        flex-shrink: 0 !important;
    }
    .map-btn, .map-select { 
        padding: 6px 10px !important; 
        font-size: 11px !important; 
        flex-shrink: 0 !important;
        white-space: nowrap !important;
    }

    
    /* Fleet Panel (Lista de Vehículos) - Escondido al lado izquierdo modo drawer en móvil */
    #fleet-panel {
        left: -300px !important; /* Escondido por defecto en móvil */
        width: 250px !important;
        top: 0 !important;
        bottom: 0 !important;
        border-radius: 0 12px 12px 0 !important;
        background: rgba(10, 15, 30, 0.95) !important;
        transition: left 0.3s ease;
        z-index: 1002 !important; 
    }
    #fleet-panel.open {
        left: 0 !important;
    }

    /* HUD del Vehículo - Se vuelve una tarjeta flotante premium compacta en móvil */
    #vehicle-hud {
        width: calc(100% - 24px) !important;
        left: 12px !important;
        right: 12px !important;
        bottom: 12px !important;
        border-radius: 16px !important;
        padding: 12px !important;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 12px) !important;
        background: rgba(10, 15, 30, 0.96) !important;
        box-shadow: 0 10px 30px rgba(0,0,0,0.8) !important;
        z-index: 1001 !important;
        margin: 0 !important;
        border: 1px solid rgba(56, 189, 248, 0.25) !important;
        max-height: 80vh !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }
    #vehicle-hud::before {
        display: none !important;
    }
    
    /* Ajustes internos para HUD compacto en móvil */
    #vehicle-hud #hud-patente {
        font-size: 16px !important;
    }
    #vehicle-hud #hud-velocidad {
        font-size: 20px !important;
    }
    #vehicle-hud #hud-motor {
        font-size: 16px !important;
    }
    #vehicle-hud .cmd-gps-btn {
        padding: 6px 8px !important;
        font-size: 9px !important;
    }

    /* Playback Panel en Móvil - Tarjeta flotante compacta */
    #playback-panel {
        width: calc(100% - 24px) !important;
        left: 12px !important;
        right: 12px !important;
        top: auto !important;
        bottom: 12px !important;
        border-radius: 16px !important;
        padding: 15px !important;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 15px) !important;
        margin: 0 !important;
        border: 1px solid rgba(139, 92, 246, 0.3) !important;
        background: rgba(10, 15, 30, 0.96) !important;
        box-shadow: 0 10px 30px rgba(0,0,0,0.8) !important;
        z-index: 1100 !important;
    }
}


/* Marcador de vehiculo en mapa: sin recorte */.vehicle-marker { background: transparent !important; border: none !important; overflow: visible !important; }.vehicle-marker > div { overflow: visible !important; }

/* ── Responsive Columns: Asignaciones Table ────────────────────────────────── */
/* Teléfono/IMEI SIM - ocultar en pantallas < 640px */
@media (max-width: 640px) {
    .hide-col-sm { display: none !important; }
    /* También ocultar la celda td correspondiente */
    #table-dispositivos td:nth-child(6) { display: none; }
}

/* IMEI Traccar - ocultar en pantallas < 900px */
@media (max-width: 900px) {
    .hide-col-md { display: none !important; }
    #table-dispositivos td:nth-child(5) { display: none; }
}

/* Table container: permitir scroll horizontal en móvil */
.table-container { overflow: visible; }
