:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --sidebar: #0f172a;
  --bg: #f8fafc;
  --card: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
}

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

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }

/* Layout */
.app-layout { display: flex; min-height: 100vh; }
.app-shell { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 100vh; }
.sidebar {
  width: 260px; background: var(--sidebar); color: #fff;
  display: flex; flex-direction: column; flex-shrink: 0;
  height: 100vh; position: sticky; top: 0;
  overflow: hidden;
}
.sidebar-overlay {
  display: none;
}
.mobile-topbar {
  display: none;
}
.sidebar-brand {
  padding: 1.5rem; border-bottom: 1px solid rgba(255,255,255,.1);
  flex-shrink: 0;
}
.sidebar-brand h1 { font-size: 1.25rem; }
.sidebar-brand p { font-size: .75rem; color: #94a3b8; margin-top: .25rem; }
.sidebar-nav {
  flex: 1; min-height: 0; padding: 1rem;
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.sidebar-nav::-webkit-scrollbar { width: 6px; }
.sidebar-nav::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,.2); border-radius: 3px;
}
.sidebar-nav::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,.35); }
.nav-link {
  display: flex; align-items: center; gap: .75rem;
  padding: .65rem 1rem; border-radius: .5rem;
  color: #cbd5e1; font-size: .875rem; margin-bottom: .25rem;
  transition: background .15s;
}
.nav-link:hover { background: rgba(255,255,255,.08); color: #fff; }
.nav-link.active { background: var(--primary); color: #fff; }
.sidebar-footer {
  padding: 1rem; border-top: 1px solid rgba(255,255,255,.1);
  font-size: .8rem; color: #94a3b8; flex-shrink: 0;
}
.main-content { flex: 1; overflow: auto; padding: 1.5rem; }

/* Mobile topbar */
.menu-toggle {
  width: 44px; height: 44px; border: none; background: transparent;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  gap: 5px; cursor: pointer; border-radius: .5rem; flex-shrink: 0;
}
.menu-toggle:hover { background: #f1f5f9; }
.menu-toggle-bar {
  display: block; width: 20px; height: 2px; background: var(--text); border-radius: 1px;
  transition: transform .2s, opacity .2s;
}
body.nav-open .menu-toggle-bar:nth-child(1) { transform: translateY(7px) rotate(45deg); }
body.nav-open .menu-toggle-bar:nth-child(2) { opacity: 0; }
body.nav-open .menu-toggle-bar:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

.mobile-topbar-brand {
  flex: 1; min-width: 0; line-height: 1.2;
}
.mobile-topbar-brand strong { display: block; font-size: .95rem; }
.mobile-topbar-brand span {
  display: block; font-size: .75rem; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.mobile-topbar-account {
  width: 44px; height: 44px; display: flex; align-items: center; justify-content: center;
  font-size: 1.25rem; border-radius: .5rem; flex-shrink: 0;
}
.mobile-topbar-account:hover { background: #f1f5f9; }

/* Cards & buttons */
.page-header { margin-bottom: 1.5rem; }
.page-header h2 { font-size: 1.5rem; font-weight: 700; }
.page-header p { color: var(--muted); font-size: .875rem; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: .75rem; padding: 1.25rem; box-shadow: 0 1px 3px rgba(0,0,0,.04); }
.card-flush { padding: 0; overflow: hidden; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card { display: flex; align-items: center; gap: 1rem; }
.stat-icon { width: 48px; height: 48px; border-radius: .75rem; display: flex; align-items: center; justify-content: center; font-size: 1.5rem; }
.stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-label { font-size: .8rem; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: .5rem 1rem; border-radius: .5rem; font-size: .875rem;
  font-weight: 500; border: none; cursor: pointer; transition: background .15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: #f1f5f9; color: var(--text); }
.btn-secondary:hover { background: #e2e8f0; }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-block { width: 100%; }
.btn-sm { padding: .35rem .65rem; font-size: .75rem; }

/* Forms */
.input, select.input, textarea.input {
  width: 100%; padding: .5rem .75rem; border: 1px solid var(--border);
  border-radius: .5rem; font-size: .875rem; outline: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.15); }
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: .8rem; font-weight: 500; margin-bottom: .35rem; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }

/* Tables */
.table { width: 100%; border-collapse: collapse; font-size: .875rem; }
.table th { text-align: left; padding: .75rem 1rem; background: #f8fafc; border-bottom: 1px solid var(--border); font-weight: 600; color: var(--muted); }
.table td { padding: .75rem 1rem; border-bottom: 1px solid var(--border); }
.table tr:hover td { background: #f8fafc; }

/* Badges */
.badge { display: inline-block; padding: .2rem .6rem; border-radius: 999px; font-size: .7rem; font-weight: 600; }
.badge-online { background: #dcfce7; color: #166534; }
.badge-offline { background: #f1f5f9; color: #475569; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-info { background: #dbeafe; color: #1e40af; }

/* Login */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, #1e3a5f, #2563eb); }
.login-card { background: #fff; border-radius: 1rem; padding: 2rem; width: 100%; max-width: 420px; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
.login-card h1 { text-align: center; font-size: 1.5rem; }
.subtitle { text-align: center; color: var(--muted); margin: .5rem 0 1.5rem; font-size: .875rem; }
.demo-credentials { margin-top: 1rem; padding: .75rem; background: #f8fafc; border-radius: .5rem; font-size: .75rem; color: var(--muted); }

@media (max-width: 768px) {
  .login-page { padding: 1rem; align-items: flex-start; padding-top: 2rem; }
  .login-card { padding: 1.5rem; max-width: 100%; }
}

/* Alerts */
.alert { padding: .75rem 1rem; border-radius: .5rem; margin-bottom: 1rem; font-size: .875rem; }
.alert-error { background: #fee2e2; color: #991b1b; }
.alert-success { background: #dcfce7; color: #166534; }

/* Tabs */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1.5rem; overflow-x: auto; }
.tab { padding: .75rem 1rem; font-size: .875rem; font-weight: 500; color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; background: none; border-top: none; border-left: none; border-right: none; }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab:hover { color: var(--text); }

/* Map */
.map-layout { display: flex; height: calc(100vh - 3rem); margin: -1.5rem; }
.map-sidebar { width: 300px; border-right: 1px solid var(--border); background: #fff; overflow-y: auto; }
.map-container { flex: 1; }
.device-item { padding: .75rem 1rem; border-bottom: 1px solid var(--border); cursor: pointer; }
.device-item:hover, .device-item.active { background: #eff6ff; }
#map { height: 100%; width: 100%; }
#vehicle-map { height: 100%; width: 100%; min-height: 420px; }

.vehicle-layout { display: grid; grid-template-columns: 1fr 320px; gap: 1rem; }
.vehicle-map-wrap { overflow: hidden; min-height: 420px; }
.vehicle-stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.live-badge { font-size: .7rem; padding: .2rem .5rem; border-radius: 999px; font-weight: 600; white-space: nowrap; }

@media (max-width: 900px) {
  .vehicle-layout { grid-template-columns: 1fr; }
}

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 1rem; }
.modal { background: #fff; border-radius: .75rem; width: 100%; max-width: 500px; max-height: 90vh; overflow-y: auto; }
.modal-header { padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); font-weight: 600; }
.modal-body { padding: 1.25rem; }
.modal-footer { padding: 1rem 1.25rem; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: .5rem; }
.hidden { display: none !important; }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; flex-wrap: wrap; gap: .5rem; }
.setup-list { margin: 1rem 0; padding-left: 1.25rem; font-size: .875rem; color: var(--muted); }
.setup-list li { margin-bottom: .35rem; }

@media (max-width: 768px) {
  .mobile-topbar {
    display: flex; align-items: center; gap: .75rem;
    padding: .5rem .75rem; background: #fff;
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 100;
  }

  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: min(280px, 88vw); max-width: 100%;
    height: 100dvh;
    z-index: 1200;
    transform: translateX(-105%);
    transition: transform .25s ease;
    box-shadow: none;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,.25);
  }

  .sidebar-overlay {
    display: block;
    position: fixed; inset: 0;
    background: rgba(15,23,42,.5);
    z-index: 1100;
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s ease;
  }
  .sidebar-overlay.open {
    opacity: 1;
    pointer-events: auto;
  }

  body.nav-open { overflow: hidden; }

  .nav-link {
    padding: .85rem 1rem;
    font-size: .95rem;
    min-height: 48px;
  }

  .main-content { padding: 1rem; }
  .page-header h2 { font-size: 1.25rem; }
  .stats-grid { grid-template-columns: 1fr 1fr; gap: .75rem; }
  .stat-value { font-size: 1.25rem; }
  .form-row { grid-template-columns: 1fr; }
  .card { padding: 1rem; }
  .card-flush { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table { min-width: 560px; }

  .map-layout {
    flex-direction: column;
    height: calc(100dvh - 57px);
    margin: -1rem;
  }
  .map-sidebar { width: 100%; max-height: 35vh; flex-shrink: 0; }
  .map-container { flex: 1; min-height: 280px; }

  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar .btn { width: 100%; }

  .modal { max-height: 85dvh; }
  .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .vehicle-stats-grid { grid-template-columns: 1fr; }
}

@media (min-width: 769px) {
  .table-wrap { overflow: visible; }
}
