:root {
    /* Backgrounds */
    --bg-main: #0b0f19;
    --bg-panel: rgba(17, 24, 39, 0.75);
    --bg-card: rgba(30, 41, 59, 0.5);
    --bg-metric: rgba(15, 23, 42, 0.6);
    
    /* Accents & Gradients */
    --accent: #0ea5e9;
    --accent-glow: rgba(14, 165, 233, 0.4);
    --gradient-primary: linear-gradient(135deg, #0ea5e9, #3b82f6);
    --gradient-hover: linear-gradient(135deg, #38bdf8, #60a5fa);
    
    /* Borders */
    --border-color: rgba(255, 255, 255, 0.08);
    --border-highlight: rgba(255, 255, 255, 0.15);
    
    /* Typography */
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    
    /* Status Colors */
    --color-online: #10b981;
    --color-offline: #64748b;
    --color-warning: #f59e0b;
    --color-danger: #ef4444;

    /* Fonts */
    --font-ui: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
}

/* Light mode overrides */
body.light-mode {
    --bg-main: #f1f5f9;
    --bg-panel: rgba(255, 255, 255, 0.85);
    --bg-card: rgba(255, 255, 255, 0.65);
    --bg-metric: rgba(241, 245, 249, 0.85);
    
    --border-color: rgba(0, 0, 0, 0.08);
    --border-highlight: rgba(0, 0, 0, 0.2);
    
    --text-primary: #0f172a;
    --text-secondary: #475569;
    
    background-image: 
        radial-gradient(at 0% 0%, rgba(14, 165, 233, 0.1) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(139, 92, 246, 0.1) 0px, transparent 50%);
}
body.light-mode .leaflet-container { background: #e2e8f0; }
body.light-mode .custom-div-icon {
    color: #0f172a;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

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

body {
    background-color: var(--bg-main);
    background-image: 
        radial-gradient(at 0% 0%, rgba(14, 165, 233, 0.15) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(139, 92, 246, 0.15) 0px, transparent 50%);
    background-attachment: fixed;
    color: var(--text-primary);
    font-family: var(--font-ui);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* --- TYPOGRAPHY & UTILS --- */
h1, h2, h3 { font-weight: 600; letter-spacing: -0.02em; }
a { color: var(--accent); text-decoration: none; transition: color 0.2s; }
a:hover { color: #38bdf8; }
.hidden { display: none !important; }
.w-100 { width: 100%; }

/* --- BUTTONS --- */
.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    background: rgba(255, 255, 255, 0.03);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    font-family: var(--font-ui);
    font-weight: 500;
    font-size: 0.9rem;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}
.btn:hover { 
    background: rgba(255, 255, 255, 0.08); 
    border-color: var(--border-highlight);
    transform: translateY(-1px);
}
.btn:active { transform: translateY(0); }

.btn-primary { 
    background: var(--gradient-primary); 
    color: #fff; 
    border: none;
    box-shadow: 0 4px 12px var(--accent-glow);
}
.btn-primary:hover { 
    background: var(--gradient-hover); 
    box-shadow: 0 6px 16px var(--accent-glow);
}

.btn-danger { background: rgba(239, 68, 68, 0.1); color: var(--color-danger); border-color: rgba(239, 68, 68, 0.3); }
.btn-danger:hover { background: var(--color-danger); color: #fff; box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4); }

.btn-outline { border-color: var(--accent); color: var(--accent); background: transparent; }
.btn-outline:hover { background: var(--accent); color: #fff; box-shadow: 0 4px 12px var(--accent-glow); }

.btn-sm { padding: 0.35rem 0.6rem; font-size: 0.8rem; }

/* --- LOGIN PAGE --- */
.login-body { display: flex; align-items: center; justify-content: center; }
.login-container { width: 100%; max-width: 420px; padding: 1rem; }
.login-box {
    background: var(--bg-panel);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    padding: 2.5rem 2rem;
    border-radius: 16px;
    border: 1px solid var(--border-highlight);
    box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5);
    text-align: center;
    animation: fade-in-up 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
@keyframes fade-in-up {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.login-box h1 { margin-bottom: 0.5rem; background: var(--gradient-primary); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.login-box p { color: var(--text-secondary); margin-bottom: 2rem; }
.form-group { margin-bottom: 1.5rem; text-align: left; }
.form-group label { display: block; margin-bottom: 0.5rem; color: var(--text-secondary); font-size: 0.9rem; font-weight: 500; }
.form-group input, .form-group select {
    width: 100%;
    padding: 0.85rem 1rem;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 1rem;
    transition: all 0.2s;
}
.form-group input:focus, .form-group select:focus {
    border-color: var(--accent);
    outline: none;
    box-shadow: 0 0 0 3px var(--accent-glow);
}
select option {
    background: var(--bg-panel);
    color: var(--text-primary);
    padding: 0.5rem;
}

.table-select {
    width: 100%;
    padding: 0.4rem 0.6rem;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 0.9rem;
    transition: all 0.2s;
    outline: none;
}
.table-select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

/* --- HEADER --- */
.app-header {
    height: 65px;
    background: var(--bg-panel);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    z-index: 10;
}
.logo { display: flex; align-items: center; gap: 0.75rem; }
.logo h1 { font-size: 1.4rem; color: var(--text-primary); }
.header-actions { display: flex; gap: 1rem; align-items: center; }

/* --- LAYOUT --- */
.app-container {
    display: flex;
    flex: 1;
    overflow: hidden;
}
.map-section {
    flex: 1;
    position: relative;
    z-index: 1;
}

/* --- LEAFLET UI DARK MODE OVERRIDES --- */
.leaflet-control-attribution {
    display: none !important;
}

.leaflet-bar {
    border: 1px solid var(--border-color) !important;
    box-shadow: 0 4px 10px rgba(0,0,0,0.5) !important;
}

.leaflet-bar a {
    background-color: var(--bg-card) !important;
    color: var(--text-primary) !important;
    border-bottom: 1px solid var(--border-color) !important;
}

.leaflet-bar a:hover {
    background-color: var(--bg-panel) !important;
    color: var(--accent) !important;
}

.leaflet-bar a.leaflet-disabled {
    background-color: var(--bg-base) !important;
    color: var(--text-secondary) !important;
}
#map {
    width: 100%;
    height: 100%;
    background-color: #0f172a;
}
.sidebar {
    width: 380px;
    background: var(--bg-panel);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    z-index: 5;
    box-shadow: -4px 0 15px rgba(0,0,0,0.2);
}
.sidebar-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
}
.sidebar-header h2 { font-size: 1.15rem; color: var(--text-primary); }
.vehicle-list {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}
.empty-state { text-align: center; color: var(--text-secondary); padding: 3rem 0; font-weight: 300; }

/* --- VEHICLE CARD --- */
.vehicle-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1.25rem;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    animation: fade-in-up 0.5s ease-out forwards;
}
.vehicle-card:hover {
    border-color: var(--border-highlight);
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(0,0,0,0.2);
}
.vehicle-card.active-follow {
    border-color: var(--accent);
    box-shadow: 0 0 15px var(--accent-glow);
}
.vc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.25rem;
}
.vc-title { display: flex; align-items: center; gap: 0.6rem; }
.vc-name { font-size: 1.2rem; font-weight: 600; }
.vc-status { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: var(--text-secondary); font-weight: 500; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background-color: var(--color-offline); display: inline-block; }
.vehicle-card.online .status-dot { background-color: var(--color-online); box-shadow: 0 0 10px var(--color-online); }
.vehicle-card.online .status-text { color: var(--color-online); }

.vc-metrics {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}
.metric {
    background: var(--bg-metric);
    padding: 0.75rem;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.03);
    transition: background 0.2s;
}
.metric:hover { background: rgba(255,255,255,0.05); }
.m-label { display: block; font-size: 0.7rem; color: var(--text-secondary); text-transform: uppercase; margin-bottom: 0.3rem; letter-spacing: 0.5px; font-weight: 500; }
.m-value { font-family: var(--font-mono); font-size: 1.15rem; font-weight: 600; color: var(--text-primary); }
.m-value small { font-size: 0.75rem; color: var(--text-secondary); font-weight: 400; }

.m-danger { color: var(--color-danger); text-shadow: 0 0 8px rgba(239, 68, 68, 0.4); }
.m-warning { color: var(--color-warning); text-shadow: 0 0 8px rgba(245, 158, 11, 0.4); }

.vc-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}
.vc-actions button { flex: 1; }

.vc-details { margin-top: 1.25rem; padding-top: 1.25rem; border-top: 1px solid var(--border-color); font-size: 0.9rem; }
.metric-row { display: flex; justify-content: space-between; margin-bottom: 0.5rem; align-items: center; }
.metric-row .m-label { margin: 0; color: var(--text-secondary); }
.metric-row .m-value { font-size: 0.95rem; }

/* --- RESPONSIVE MOBILE/TABLET --- */
@media (max-width: 768px) {
    .app-container { flex-direction: column; }
    .map-section { height: 45vh; flex: none; }
    .sidebar { width: 100%; height: 55vh; border-left: none; border-top: 1px solid var(--border-color); }
    .vehicle-list { padding: 1rem; gap: 1rem; }
    .app-header { padding: 0 1rem; }
}

/* --- LEAFLET OVERRIDES --- */
.leaflet-container { background: #0f172a; font-family: var(--font-ui); }
.leaflet-popup-content-wrapper { 
    background: var(--bg-panel); 
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    color: var(--text-primary); 
    border: 1px solid var(--border-highlight); 
    border-radius: 12px; 
    box-shadow: 0 15px 35px rgba(0,0,0,0.6);
}
.leaflet-popup-content { margin: 0.75rem 1rem; }
.leaflet-popup-tip { background: var(--bg-panel); border: 1px solid var(--border-color); }

/* --- POPUP DASHBOARD --- */
.pd { min-width: 220px; font-family: var(--font-ui); }
.pd-header {
    font-weight: 700; font-size: 0.95rem;
    margin-bottom: 0.65rem; padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
    letter-spacing: -0.01em;
    color: var(--text-secondary);
}
.pd-main {
    display: flex; align-items: center;
    gap: 1rem; margin-bottom: 0.75rem;
}
.pd-gear {
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    width: 52px; height: 52px;
    border-radius: 10px;
    border: 2px solid var(--accent);
    background: rgba(14, 165, 233, 0.1);
    flex-shrink: 0;
    transition: all 0.3s;
}
.pd-gear-label {
    font-size: 0.45rem; text-transform: uppercase;
    color: var(--text-secondary); letter-spacing: 0.8px;
    font-weight: 600; line-height: 1;
}
.pd-gear-value {
    font-family: var(--font-mono);
    font-size: 1.5rem; font-weight: 700;
    color: var(--accent); line-height: 1.1;
}
.pd-speed-block {
    display: flex; align-items: baseline; gap: 0.3rem;
}
.pd-speed-value {
    font-family: var(--font-mono);
    font-size: 2.4rem; font-weight: 700;
    line-height: 1; letter-spacing: -0.03em;
    background: var(--gradient-primary);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}
.pd-speed-unit {
    font-size: 0.85rem; color: var(--text-secondary); font-weight: 500;
}
.pd-bars { display: flex; flex-direction: column; gap: 0.45rem; }
.pd-bar-row {
    display: grid;
    grid-template-columns: 32px 1fr 40px;
    align-items: center; gap: 0.5rem;
}
.pd-bar-label {
    font-size: 0.6rem; text-transform: uppercase;
    color: var(--text-secondary); font-weight: 600;
    letter-spacing: 0.5px;
}
.pd-bar-track {
    height: 6px; background: rgba(255,255,255,0.08);
    border-radius: 3px; overflow: hidden;
}
.pd-bar-fill {
    height: 100%; border-radius: 3px;
    background: var(--accent);
    transition: width 0.3s ease;
}
.pd-bar-thr { background: var(--color-online); }
.pd-bar-value {
    font-family: var(--font-mono);
    font-size: 0.72rem; text-align: right; font-weight: 500;
}
.custom-div-icon {
    background: var(--bg-panel);
    border: 2px solid var(--color-offline);
    border-radius: 50%;
    color: white;
    text-align: center;
    line-height: 28px;
    font-size: 16px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.4);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
@keyframes pulse-online {
    0% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
    70% { box-shadow: 0 0 0 15px rgba(16, 185, 129, 0); }
    100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}
.custom-div-icon.online { 
    border-color: var(--color-online); 
    background: rgba(16, 185, 129, 0.1);
    animation: pulse-online 2s infinite;
}

/* --- ADMIN --- */
.admin-container { padding: 2.5rem 2rem; max-width: 1000px; margin: 0 auto; overflow-y: auto; flex: 1; width: 100%; }
.admin-section { 
    background: var(--bg-panel); 
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border-highlight); 
    border-radius: 16px; 
    padding: 2.5rem; 
    margin-bottom: 2.5rem; 
    box-shadow: 0 15px 35px rgba(0,0,0,0.3);
    animation: fade-in-up 0.5s ease-out forwards;
}
.admin-section h2 { margin-bottom: 1.5rem; background: var(--gradient-primary); -webkit-background-clip: text; -webkit-text-fill-color: transparent; border-bottom: 1px solid var(--border-color); padding-bottom: 0.75rem; }
.admin-table { width: 100%; border-collapse: separate; border-spacing: 0; margin-bottom: 1.5rem; }
.admin-table th, .admin-table td { padding: 1rem; text-align: left; border-bottom: 1px solid var(--border-color); }
.admin-table th { color: var(--text-secondary); font-weight: 600; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.8px; padding-bottom: 1.25rem; }
.admin-table tr { transition: background 0.2s; }
.admin-table tr:hover { background: rgba(255,255,255,0.04); }
.admin-table td { color: var(--text-primary); font-size: 0.95rem; vertical-align: middle; }

.ping-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.25rem 0.6rem;
    border-radius: 9999px;
    font-size: 0.8rem;
    font-weight: 500;
    font-family: var(--font-mono);
}
.ping-online {
    background: rgba(16, 185, 129, 0.15);
    color: var(--color-online);
    border: 1px solid rgba(16, 185, 129, 0.3);
}
.ping-offline {
    background: rgba(100, 116, 139, 0.15);
    color: var(--text-secondary);
    border: 1px solid rgba(100, 116, 139, 0.3);
}
