:root {
    --primary: #3b82f6;
    --primary-dark: #2563eb;
    --secondary: #8b5cf6;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --dark: #1e293b;
    --darker: #0f172a;
    --light: #f8fafc;
    --gray: #64748b;
    --sidebar-width: 280px;
    --header-height: 56px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--darker);
    color: var(--light);
    display: flex;
    flex-direction: column;
    -webkit-tap-highlight-color: transparent;
    -webkit-user-select: none;
    user-select: none;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 3px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 3px;
}

/* ======================= */
/* HEADER */
/* ======================= */
.header {
    background: var(--dark);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding: 6px 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 1000;
    gap: 6px;
    min-height: var(--header-height);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
}

.logo {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 16px;
    font-weight: 700;
    color: var(--primary);
    white-space: nowrap;
}

.logo i {
    font-size: 18px;
}

.sync-info {
    display: none;
}

.info-badge {
    display: none;
}

.sync-buttons {
    display: flex;
    gap: 3px;
    align-items: center;
}

.btn {
    padding: 7px 9px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: rgba(255, 255, 255, 0.08);
    color: var(--light);
    border: 1px solid rgba(255, 255, 255, 0.1);
    white-space: nowrap;
    min-width: 32px;
    min-height: 32px;
    -webkit-tap-highlight-color: transparent;
}

.btn:active {
    transform: scale(0.95);
    background: rgba(255, 255, 255, 0.15);
}

.btn span {
    display: none;
}

.btn i {
    font-size: 14px;
}

.btn-primary {
    background: var(--primary);
    border: none;
}

.btn-success {
    background: var(--success);
    border: none;
}

.btn-secondary {
    background: var(--secondary);
    border: none;
}

.spinner {
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ======================= */
/* MAIN CONTAINER */
/* ======================= */
.container {
    flex: 1;
    display: flex;
    position: relative;
    overflow: hidden;
    min-height: 0;
}

/* ======================= */
/* SIDEBAR */
/* ======================= */
.sidebar {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    max-width: 280px;
    background: var(--dark);
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    z-index: 999;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
}

.sidebar.visible {
    transform: translateX(0);
}

.sidebar-header {
    padding: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0;
}

.sidebar-header h3 {
    font-size: 15px;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.search-box {
    position: relative;
    margin-bottom: 8px;
}

.search-box input {
    width: 100%;
    padding: 8px 10px 8px 30px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 6px;
    color: white;
    font-size: 13px;
}

.search-box i {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--gray);
    font-size: 12px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 4px;
}

.stat-card {
    padding: 5px 4px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 6px;
    text-align: center;
}

.stat-value {
    font-size: 14px;
    font-weight: 700;
}

.stat-label {
    font-size: 8px;
    color: var(--gray);
    text-transform: uppercase;
}

.device-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    -webkit-overflow-scrolling: touch;
}

.device-item {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 8px;
    padding: 8px;
    margin-bottom: 4px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border-left: 3px solid var(--gray);
}

.device-item:active {
    background: rgba(255, 255, 255, 0.08);
}

.device-item.active {
    background: rgba(59, 130, 246, 0.12);
    border-color: rgba(59, 130, 246, 0.3);
    border-left-color: var(--primary);
}

.device-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
}

.device-name {
    font-weight: 600;
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.status-badge {
    font-size: 8px;
    padding: 2px 7px;
    border-radius: 8px;
    font-weight: 600;
    text-transform: uppercase;
}

.status-moving {
    background: rgba(16, 185, 129, 0.2);
    color: var(--success);
}
.status-stopped {
    background: rgba(245, 158, 11, 0.2);
    color: var(--warning);
}
.status-offline {
    background: rgba(239, 68, 68, 0.2);
    color: var(--danger);
}

.device-metrics {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
}

.metric {
    text-align: center;
    padding: 4px 2px;
    background: rgba(0, 0, 0, 0.25);
    border-radius: 4px;
}

.metric-value {
    font-size: 10px;
    font-weight: 700;
}

.metric-label {
    font-size: 8px;
    color: var(--gray);
}

.empty-state {
    text-align: center;
    padding: 30px 15px;
    color: var(--gray);
    font-size: 13px;
}

.empty-state i {
    font-size: 32px;
    margin-bottom: 10px;
    opacity: 0.4;
    display: block;
}

/* ======================= */
/* MAP */
/* ======================= */
.map-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    min-width: 0;
}

#map {
    flex: 1;
    z-index: 1;
}

/* ======================= */
/* LEGEND */
/* ======================= */
.legend {
    position: absolute;
    bottom: 55px;
    right: 5px;
    z-index: 500;
    background: rgba(15, 23, 42, 0.92);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 9px;
    backdrop-filter: blur(5px);
    pointer-events: none;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-bottom: 3px;
}

.legend-item:last-child {
    margin-bottom: 0;
}

.legend-color {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* ======================= */
/* BOTTOM PANEL */
/* ======================= */
.bottom-panel {
    background: var(--dark);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 40px;
    transition: height 0.3s ease;
}

.bottom-panel.expanded {
    height: 40vh;
    max-height: 250px;
}

.panel-header {
    padding: 6px 10px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    min-height: 40px;
}

.panel-header h4 {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
}

.panel-header h4 span {
    color: var(--gray);
    font-size: 10px;
    font-weight: normal;
}

.panel-controls {
    display: flex;
    gap: 4px;
    align-items: center;
}

.panel-controls .btn {
    padding: 4px 6px;
    font-size: 10px;
    min-height: auto;
}

#recordCount {
    font-size: 10px;
    color: var(--gray);
}

.history-content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 10px;
}

.history-table thead {
    position: sticky;
    top: 0;
    z-index: 2;
}

.history-table th {
    text-align: left;
    padding: 5px 6px;
    background: var(--dark);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--gray);
    font-size: 9px;
    text-transform: uppercase;
    white-space: nowrap;
}

.history-table td {
    padding: 5px 6px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    font-size: 10px;
    white-space: nowrap;
}

.history-table td a {
    color: var(--primary);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 3px;
}

.speed-badge {
    display: inline-block;
    padding: 1px 5px;
    border-radius: 6px;
    font-weight: 600;
    font-size: 9px;
}

.speed-low {
    background: rgba(16, 185, 129, 0.2);
    color: var(--success);
}
.speed-medium {
    background: rgba(245, 158, 11, 0.2);
    color: var(--warning);
}
.speed-high {
    background: rgba(239, 68, 68, 0.2);
    color: var(--danger);
}

/* ======================= */
/* TOAST */
/* ======================= */
.toast-container {
    position: fixed;
    top: 60px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 6px;
    width: 90%;
    max-width: 320px;
    pointer-events: none;
}

.toast {
    padding: 10px 12px;
    border-radius: 8px;
    background: rgba(15, 23, 42, 0.97);
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: white;
    font-size: 12px;
    display: flex;
    align-items: flex-start;
    gap: 8px;
    animation: slideDown 0.3s ease;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.5);
    pointer-events: auto;
}

.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
.toast.warning { border-left: 3px solid var(--warning); }
.toast.info { border-left: 3px solid var(--primary); }

.toast i {
    font-size: 16px !important;
    flex-shrink: 0;
    margin-top: 1px;
}

@keyframes slideDown {
    from { transform: translateY(-20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

@keyframes slideOut {
    from { transform: translateY(0); opacity: 1; }
    to { transform: translateY(-20px); opacity: 0; }
}

/* ======================= */
/* LEAFLET OVERRIDES */
/* ======================= */
.leaflet-popup-content-wrapper {
    background: white;
    color: black;
    border-radius: 8px !important;
    border: 1px solid rgba(255, 255, 255, 0.15);
}

.leaflet-popup-tip {
    background: rgba(15, 23, 42, 0.95);
}

.leaflet-popup-content {
    margin: 8px 12px !important;
    font-size: 12px !important;
    line-height: 1.6 !important;
}

.leaflet-tooltip {
    background: rgba(15, 23, 42, 0.9) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: white !important;
    border-radius: 8px !important;
    padding: 3px 7px !important;
    font-size: 10px !important;
    font-weight: 600 !important;
}

.leaflet-control-zoom {
    border: none !important;
}

.leaflet-control-zoom a {
    background: rgba(15, 23, 42, 0.9) !important;
    color: white !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    width: 28px !important;
    height: 28px !important;
    line-height: 28px !important;
    font-size: 16px !important;
}

/* ======================= */
/* TABLET & DESKTOP */
/* ======================= */
@media (min-width: 768px) {
    .header {
        padding: 8px 16px;
    }
    
    .logo {
        font-size: 20px;
    }
    
    .logo i {
        font-size: 22px;
    }
    
    .sync-info {
        display: flex;
        gap: 10px;
    }
    
    .info-badge {
        display: flex;
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 8px;
        padding: 5px 10px;
        font-size: 12px;
        align-items: center;
        gap: 6px;
    }
    
    .pulse-dot {
        width: 7px;
        height: 7px;
        border-radius: 50%;
        background: var(--success);
        animation: pulse 2s infinite;
    }
    
    @keyframes pulse {
        0%, 100% { opacity: 1; box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
        50% { opacity: 0.7; box-shadow: 0 0 0 6px rgba(16, 185, 129, 0); }
    }
    
    .btn {
        padding: 8px 12px;
        font-size: 13px;
        gap: 6px;
    }
    
    .btn span {
        display: inline;
    }
    
    /* Sidebar - Desktop */
    .sidebar {
        position: relative;
        transform: translateX(0);
        width: 280px;
        max-width: 280px;
    }
    
    .stats-grid {
        grid-template-columns: repeat(4, 1fr);
    }
    
    .stat-card {
        padding: 6px 4px;
    }
    
    .stat-value {
        font-size: 15px;
    }
    
    .stat-label {
        font-size: 8px;
    }
    
    .device-item {
        padding: 10px;
        margin-bottom: 6px;
    }
    
    .device-name {
        font-size: 13px;
    }
    
    .metric-value {
        font-size: 11px;
    }
    
    .legend {
        bottom: 60px;
        right: 10px;
        font-size: 11px;
    }
    
    .bottom-panel {
        height: 250px;
    }
    
    .bottom-panel.expanded {
        height: 250px;
    }
    
    .panel-header h4 {
        font-size: 14px;
    }
    
    .history-table {
        font-size: 12px;
    }
    
    .history-table th {
        font-size: 10px;
        padding: 8px 10px;
    }
    
    .history-table td {
        font-size: 12px;
        padding: 8px 10px;
    }
    
    .speed-badge {
        font-size: 11px;
    }
    
    .toast-container {
        top: 80px;
        right: 20px;
        left: auto;
        transform: none;
        width: auto;
        max-width: 350px;
    }
    
    .toast {
        font-size: 13px;
        padding: 12px 16px;
    }
}