/* =============================================================================
   FleetView — design system
   Breakpoints:  desktop > 900px · tablet ≤ 900px (drawer + hamburger)
                 phone ≤ 640px (bottom sheets, full-screen chat) · tiny ≤ 359px
   Z-index scale (keep in sync): map 0 · map overlays 10–20 · scrim 40
                 drawer / sheets 50 · header 60 · chat bubble 70 · chat panel 80
   ============================================================================= */
:root {
  --slate-50: #f8fafc;  --slate-100: #f1f5f9; --slate-200: #e2e8f0; --slate-300: #cbd5e1;
  --slate-400: #94a3b8; --slate-500: #64748b; --slate-600: #475569; --slate-700: #334155;
  --slate-800: #1e293b; --slate-900: #0f172a;

  --accent: #0d9488; --accent-dark: #0f766e; --accent-light: #ccfbf1; --accent-50: #f0fdfa;

  --status-moving: #16a34a; --status-idle: #d97706; --status-stopped: #64748b; --status-offline: #dc2626;

  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px; --space-5: 20px;
  --space-6: 24px; --space-8: 32px; --space-10: 40px;

  --radius-sm: 8px; --radius-md: 12px; --radius-lg: 16px; --radius-xl: 20px; --radius-pill: 999px;

  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.07), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08), 0 1px 3px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.12), 0 2px 6px rgba(15, 23, 42, 0.06);
  --shadow-xl: 0 24px 56px rgba(15, 23, 42, 0.18), 0 4px 12px rgba(15, 23, 42, 0.08);

  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --dur: 220ms;
  --header-h: 64px;
  --tap: 40px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  /* Floating chat button: offset from the screen edge PLUS the safe area (home indicator,
     notch). Pages never let content pass under it: on tablet/desktop they keep a right-hand
     lane free (--fab-lane); on phones they end above a bottom dock that holds the button
     (--dock-h). --fab-clear is the extra space at the end of every scrolling page. */
  --fab-size: 56px;
  --fab-gap: var(--space-6);
  --fab-right: calc(var(--fab-gap) + var(--safe-right));
  --fab-bottom: calc(var(--fab-gap) + var(--safe-bottom));
  --fab-clear: calc(var(--fab-bottom) + var(--fab-size) + var(--space-4));
  --fab-lane: calc(var(--fab-right) + var(--fab-size) + var(--space-3));
  --dock-h: 0px;
}
@media (max-width: 900px) {
  :root { --header-h: 60px; --tap: 44px; --fab-gap: var(--space-4); }
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { height: 100%; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100%;
  background: var(--slate-50);
  color: var(--slate-800);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
a { color: var(--accent-dark); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button, a, input, select, label { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2, h3 { margin: 0; }

.icon {
  width: 20px; height: 20px; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

.skeleton {
  background: linear-gradient(90deg, var(--slate-100) 25%, var(--slate-200) 37%, var(--slate-100) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease infinite;
  border-radius: var(--radius-md);
}
.skeleton-row { height: 56px; margin: 10px 16px; }
@keyframes shimmer { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: 44px; padding: 0 var(--space-5);
  border-radius: var(--radius-md); border: none;
  font-size: 15px; font-weight: 600;
  transition: background var(--dur) var(--ease), transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 1px 2px rgba(13, 148, 136, 0.3); }
.btn-primary:hover { background: var(--accent-dark); }
.btn-primary:disabled { opacity: 0.6; cursor: default; transform: none; }

.btn-icon {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-width: var(--tap); height: var(--tap); padding: 0;
  background: #fff; border: 1px solid var(--slate-200); border-radius: var(--radius-md);
  color: var(--slate-600);
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.btn-icon:hover { background: var(--slate-50); color: var(--slate-900); border-color: var(--slate-300); }

.count-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 26px; height: 22px; padding: 0 7px;
  border-radius: var(--radius-pill);
  background: var(--slate-100); color: var(--slate-600);
  font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.count-badge-inverse { background: rgba(255, 255, 255, 0.22); color: #fff; }

/* ---------- Login ---------- */
.login-page {
  min-height: 100vh; min-height: 100dvh;
  display: flex; align-items: center; justify-content: center;
  padding: max(var(--space-6), var(--safe-top)) var(--space-4) max(var(--space-6), var(--safe-bottom));
  background:
    radial-gradient(1200px 600px at 10% -10%, #ccfbf1 0%, rgba(204, 251, 241, 0) 55%),
    radial-gradient(900px 500px at 110% 110%, #e0f2fe 0%, rgba(224, 242, 254, 0) 55%),
    var(--slate-50);
}
.login-card {
  width: 100%; max-width: 420px;
  background: #fff;
  border: 1px solid var(--slate-200);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  padding: var(--space-8) var(--space-8) var(--space-8);
}
.login-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-8); }
.login-brand { display: flex; align-items: center; gap: var(--space-3); }
.login-brand .brand-name { font-weight: 800; font-size: 20px; letter-spacing: -0.02em; color: var(--slate-900); }
.login-card h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; color: var(--slate-900); margin-bottom: var(--space-1); }
.login-card .subtitle { color: var(--slate-500); font-size: 15px; margin: 0 0 var(--space-6); }
.login-card .btn-primary { width: 100%; min-height: 48px; margin-top: var(--space-2); }

.field { margin-bottom: var(--space-4); }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--slate-600); margin-bottom: 6px; }
.field input, .field select {
  width: 100%; height: 48px; padding: 0 var(--space-3);
  border: 1px solid var(--slate-300); border-radius: var(--radius-md);
  font-size: 16px; background: #fff; color: var(--slate-900);
  outline: none;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.field input:focus, .field select:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-light); }
.form-error {
  display: none;
  background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca;
  border-radius: var(--radius-md); padding: var(--space-3); font-size: 14px; margin-bottom: var(--space-4);
}
.form-error.visible { display: block; }

/* ---------- Logo ---------- */
.logo-mark {
  width: 36px; height: 36px; border-radius: 10px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #14b8a6, var(--accent-dark));
  box-shadow: 0 2px 6px rgba(13, 148, 136, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.2);
  color: #fff;
}
.logo-mark .icon { width: 20px; height: 20px; stroke-width: 2.2; }

/* ---------- Language switch (shared by login + header) ---------- */
.lang-switch {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--slate-100); border-radius: var(--radius-md);
}
.lang-switch button {
  min-width: 40px; height: 32px; padding: 0 var(--space-2);
  border: none; border-radius: 9px; background: transparent;
  color: var(--slate-500); font-size: 13px; font-weight: 700; letter-spacing: 0.02em;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.lang-switch button.active { background: #fff; color: var(--accent-dark); box-shadow: var(--shadow-sm); }

/* ---------- App shell ---------- */
.app-shell { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.app-header {
  position: relative; z-index: 60;
  height: calc(var(--header-h) + var(--safe-top));
  padding: var(--safe-top) max(var(--space-6), var(--safe-right)) 0 max(var(--space-6), var(--safe-left));
  display: flex; align-items: center; gap: var(--space-4);
  background: rgba(255, 255, 255, 0.96);
  -webkit-backdrop-filter: saturate(180%) blur(12px); backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--slate-200);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; min-height: 44px; text-decoration: none; flex-shrink: 0; }
.brand .logo-mark { width: 34px; height: 34px; }
.brand-name { font-weight: 800; font-size: 18px; letter-spacing: -0.02em; color: var(--slate-900); }

.app-nav { display: flex; gap: var(--space-1); margin: 0 auto; }
.app-nav a {
  display: flex; align-items: center; gap: var(--space-2);
  height: 40px; padding: 0 var(--space-4);
  border-radius: var(--radius-md);
  color: var(--slate-500); text-decoration: none; font-size: 14px; font-weight: 600;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.app-nav a .nav-icon { width: 18px; height: 18px; }
.app-nav a:hover { background: var(--slate-100); color: var(--slate-800); }
.app-nav a.active { background: var(--accent-50); color: var(--accent-dark); box-shadow: inset 0 0 0 1px var(--accent-light); }

.header-right { display: flex; align-items: center; gap: var(--space-2); margin-left: auto; }
.app-nav + .header-right { margin-left: 0; }
.btn-logout { padding: 0 var(--space-3); height: 40px; font-size: 14px; font-weight: 600; }
.btn-logout .icon { width: 18px; height: 18px; }
.nav-toggle { display: none; }
.nav-toggle .icon-close { display: none; }
.nav-open .nav-toggle .icon-open { display: none; }
.nav-open .nav-toggle .icon-close { display: block; }

.scrim {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(15, 23, 42, 0.4);
  opacity: 0; visibility: hidden;
  transition: opacity var(--dur) var(--ease), visibility 0s linear var(--dur);
}
.scrim.visible { opacity: 1; visibility: visible; transition: opacity var(--dur) var(--ease); }

.app-main { flex: 1; min-height: 0; position: relative; }
.view { position: absolute; inset: 0; display: none; }
#view-map.active { display: flex; }
#view-overview.active, #view-trips.active { display: block; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.page {
  width: 100%; max-width: 1280px; margin: 0 auto;
  padding: var(--space-8) max(var(--space-8), var(--fab-lane)) var(--fab-clear) max(var(--space-8), var(--safe-left));
}
.page-title { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; color: var(--slate-900); margin-bottom: var(--space-6); }

.panel {
  background: #fff;
  border: 1px solid var(--slate-200);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
  padding: var(--space-5) var(--space-6);
  min-width: 0;
}
.panel h2 { font-size: 16px; font-weight: 700; color: var(--slate-900); }
.panel .panel-desc { font-size: 13px; color: var(--slate-500); margin: var(--space-1) 0 var(--space-4); }

/* ---------- Map view ---------- */
.map-sidebar {
  width: 360px; flex-shrink: 0;
  display: flex; flex-direction: column;
  background: #fff;
  border-right: 1px solid var(--slate-200);
  min-height: 0;
}
.sheet-handle { display: none; }
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-5) var(--space-5) var(--space-2);
}
.sidebar-title { display: flex; align-items: center; gap: var(--space-2); }
.sidebar-title h2 { font-size: 17px; font-weight: 700; color: var(--slate-900); }
.sidebar-close { display: none; }
.sidebar-controls { padding: var(--space-2) var(--space-5) var(--space-4); border-bottom: 1px solid var(--slate-200); }

.search-field { position: relative; display: block; margin-bottom: var(--space-3); }
.search-field .icon { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--slate-400); pointer-events: none; }
.search-input {
  width: 100%; height: 42px; padding: 0 var(--space-3) 0 40px;
  border: 1px solid var(--slate-200); border-radius: var(--radius-md);
  background: var(--slate-50); font-size: 15px; outline: none;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background var(--dur) var(--ease);
  -webkit-appearance: none; appearance: none;
}
.search-input:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 4px var(--accent-light); }

.status-filters { display: flex; flex-wrap: wrap; gap: 6px; }
.status-chip {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 var(--space-3);
  border: 1px solid var(--slate-200); border-radius: var(--radius-pill);
  background: #fff; color: var(--slate-600);
  font-size: 13px; font-weight: 600;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.status-chip .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.status-chip:not(.active) { color: var(--slate-400); background: var(--slate-50); }
.status-chip:not(.active) .dot { opacity: 0.35; }
.status-chip.active { border-color: transparent; color: #fff; }
.status-chip.active .dot { background: #fff !important; }
.status-chip[data-status="moving"] .dot { background: var(--status-moving); }
.status-chip[data-status="idle"] .dot { background: var(--status-idle); }
.status-chip[data-status="stopped"] .dot { background: var(--status-stopped); }
.status-chip[data-status="offline"] .dot { background: var(--status-offline); }
.status-chip[data-status="moving"].active { background: var(--status-moving); }
.status-chip[data-status="idle"].active { background: var(--status-idle); }
.status-chip[data-status="stopped"].active { background: var(--status-stopped); }
.status-chip[data-status="offline"].active { background: var(--status-offline); }

.vehicle-list { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.vehicle-row {
  display: flex; align-items: center; gap: var(--space-3);
  min-height: 64px; padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--slate-100);
  cursor: pointer;
  transition: background var(--dur) var(--ease);
}
.vehicle-row:hover { background: var(--slate-50); }
.vehicle-row.selected { background: var(--accent-50); box-shadow: inset 3px 0 0 var(--accent); }
.vehicle-row .status-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; box-shadow: 0 0 0 3px rgba(148, 163, 184, 0.15); }
.vehicle-row .v-info { flex: 1; min-width: 0; }
.vehicle-row .v-plate { font-weight: 700; font-size: 15px; color: var(--slate-900); letter-spacing: 0.01em; }
.vehicle-row .v-meta { font-size: 13px; color: var(--slate-500); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.vehicle-row .v-meta.warn { color: #b91c1c; }
.vehicle-row .v-time { font-size: 12px; color: var(--slate-400); flex-shrink: 0; font-variant-numeric: tabular-nums; }
.list-empty { padding: var(--space-8) var(--space-5); text-align: center; color: var(--slate-400); font-size: 14px; }

.map-container { flex: 1; min-width: 0; position: relative; }
/* Leaflet stacks its panes at z-index 200–1000. Without an isolated stacking context those
   panes paint OVER every overlay below (drawer, detail card, chat bubble, toggles) — the root
   cause of the "invisible overlays on the map page" bug. Keep the map at z-index 0. */
#leaflet-map, #trip-map { z-index: 0; isolation: isolate; }
#leaflet-map { position: absolute; inset: 0; background: #e5e7eb; }
.leaflet-container { font-family: inherit; }
.leaflet-bar { border: none !important; box-shadow: var(--shadow-md) !important; border-radius: var(--radius-md) !important; overflow: hidden; }
.leaflet-bar a, .leaflet-bar a:hover { width: 36px !important; height: 36px !important; line-height: 36px !important; font-size: 20px !important; color: var(--slate-700) !important; border-bottom-color: var(--slate-200) !important; }
.leaflet-control-attribution { font-size: 10px !important; background: rgba(255, 255, 255, 0.8) !important; border-top-left-radius: 6px; }
/* The map credit line normally sits in the bottom-right corner, i.e. under the chat button. */
#leaflet-map .leaflet-bottom.leaflet-right { right: calc(var(--fab-right) + var(--fab-size) + var(--space-2)); }
#leaflet-map .leaflet-bottom.leaflet-right .leaflet-control-attribution { border-top-right-radius: 6px; }

.map-status {
  position: absolute; z-index: 10;
  left: var(--space-4); bottom: var(--space-4);
  display: flex; flex-direction: column-reverse; align-items: flex-start; gap: var(--space-2);
  pointer-events: none;
}
.last-updated-pill, .demo-badge {
  display: inline-flex; align-items: center; gap: var(--space-2);
  height: 32px; padding: 0 var(--space-3);
  border-radius: var(--radius-pill);
  font-size: 12.5px; font-weight: 600; white-space: nowrap;
  box-shadow: var(--shadow-md);
}
.last-updated-pill { background: rgba(255, 255, 255, 0.96); color: var(--slate-600); border: 1px solid var(--slate-200); }
.pulse-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 2s ease infinite; }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(13, 148, 136, 0.45); } 50% { box-shadow: 0 0 0 5px rgba(13, 148, 136, 0); } }
.demo-badge { background: #fff7ed; color: #9a3412; border: 1px solid #fed7aa; }

.map-toggle-geofences {
  position: absolute; z-index: 10; top: var(--space-3); right: var(--space-3);
  display: inline-flex; align-items: center; gap: var(--space-2);
  height: 40px; padding: 0 var(--space-3) 0 var(--space-2);
  background: #fff; border: 1px solid var(--slate-200); border-radius: var(--radius-pill);
  box-shadow: var(--shadow-md);
  font-size: 13px; font-weight: 600; color: var(--slate-700);
  cursor: pointer; user-select: none;
}
.map-toggle-geofences input { position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
.map-toggle-geofences .icon { display: none; width: 20px; height: 20px; }
.switch {
  position: relative; width: 34px; height: 20px; border-radius: var(--radius-pill);
  background: var(--slate-300); transition: background var(--dur) var(--ease); flex-shrink: 0;
}
.switch::after {
  content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; box-shadow: var(--shadow-sm); transition: transform var(--dur) var(--ease);
}
.map-toggle-geofences input:checked + .switch { background: var(--accent); }
.map-toggle-geofences input:checked + .switch::after { transform: translateX(14px); }
.map-toggle-geofences input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }

.sidebar-toggle { display: none; }

/* Vehicle detail card — floating on desktop/tablet, bottom sheet on phone */
.vehicle-detail-card {
  position: absolute; z-index: 20;
  top: 64px; right: var(--space-3);
  width: 340px; max-height: calc(100% - 64px - var(--fab-bottom) - var(--fab-size) - var(--space-3));
  display: flex; flex-direction: column;
  background: #fff; border: 1px solid var(--slate-200);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-xl);
  overflow: hidden;
  opacity: 0; visibility: hidden; transform: translateY(-6px) scale(0.98);
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease), visibility 0s linear var(--dur);
}
.vehicle-detail-card.visible { opacity: 1; visibility: visible; transform: none; transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease); }
.vd-header {
  display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-3);
  padding: var(--space-4) var(--space-4) var(--space-3) var(--space-5);
}
.vd-title { min-width: 0; }
.vd-plate { font-weight: 800; font-size: 19px; letter-spacing: 0.01em; color: var(--slate-900); margin-bottom: 6px; }
.vd-close { flex-shrink: 0; border-color: transparent; background: var(--slate-100); }
.vd-body { padding: 0 var(--space-5) var(--space-5); overflow-y: auto; }
.vd-stats { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); margin-bottom: var(--space-3); }
.vd-stat { background: var(--slate-50); border: 1px solid var(--slate-100); border-radius: var(--radius-md); padding: 10px var(--space-3); min-width: 0; }
.vd-stat-label { font-size: 12px; color: var(--slate-500); font-weight: 600; margin-bottom: 2px; }
.vd-stat-value { font-size: 17px; font-weight: 700; color: var(--slate-900); font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vd-stat-value small { font-size: 12px; font-weight: 600; color: var(--slate-500); margin-left: 2px; }
.vd-row { display: flex; justify-content: space-between; gap: var(--space-4); padding: 10px 0; font-size: 14px; border-top: 1px solid var(--slate-100); }
.vd-row .vd-label { color: var(--slate-500); flex-shrink: 0; }
.vd-row .vd-value { color: var(--slate-800); font-weight: 600; text-align: right; min-width: 0; overflow-wrap: anywhere; }

.status-badge {
  display: inline-flex; align-items: center; gap: 6px;
  height: 24px; padding: 0 10px; border-radius: var(--radius-pill);
  font-size: 12px; font-weight: 700;
}
.status-badge::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-badge.moving { background: #dcfce7; color: #166534; }
.status-badge.idle { background: #fef3c7; color: #92400e; }
.status-badge.stopped { background: var(--slate-100); color: var(--slate-600); }
.status-badge.offline { background: #fee2e2; color: #991b1b; }

/* ---------- Overview ---------- */
.kpi-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: var(--space-4); margin-bottom: var(--space-6); }
.kpi-tile {
  position: relative; overflow: hidden;
  background: #fff; border: 1px solid var(--slate-200); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
  padding: var(--space-4) var(--space-5) var(--space-5);
  min-width: 0;
}
.kpi-tile::before { content: ''; position: absolute; left: 0; top: 0; right: 0; height: 3px; background: var(--slate-200); }
.kpi-total::before { background: linear-gradient(90deg, #14b8a6, var(--accent-dark)); }
.kpi-tile .kpi-label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--slate-500); font-weight: 600; margin-bottom: var(--space-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-tile .kpi-label .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.kpi-tile .kpi-value { font-size: 32px; font-weight: 800; letter-spacing: -0.02em; color: var(--slate-900); font-variant-numeric: tabular-nums; line-height: 1.1; }
.kpi-tile.accent-moving::before, .kpi-tile.accent-moving .dot { background: var(--status-moving); }
.kpi-tile.accent-idle::before, .kpi-tile.accent-idle .dot { background: var(--status-idle); }
.kpi-tile.accent-stopped::before, .kpi-tile.accent-stopped .dot { background: var(--status-stopped); }
.kpi-tile.accent-offline::before, .kpi-tile.accent-offline .dot { background: var(--status-offline); }
.kpi-tile.accent-moving .kpi-value { color: var(--status-moving); }
.kpi-tile.accent-idle .kpi-value { color: var(--status-idle); }
.kpi-tile.accent-stopped .kpi-value { color: var(--status-stopped); }
.kpi-tile.accent-offline .kpi-value { color: var(--status-offline); }

.distance-panel { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: var(--space-8); margin-bottom: var(--space-4); }
.distance-total { font-size: 44px; font-weight: 800; letter-spacing: -0.03em; color: var(--slate-900); font-variant-numeric: tabular-nums; line-height: 1.1; }
.distance-total small { font-size: 20px; font-weight: 700; color: var(--slate-500); margin-left: 4px; }
.distance-sub { margin-top: var(--space-2); font-size: 14px; color: var(--slate-600); }
.distance-top h3 { font-size: 13px; font-weight: 700; color: var(--slate-500); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: var(--space-3); }
.distance-bars { display: flex; flex-direction: column; gap: var(--space-3); }
.panel-grid { display: grid; grid-template-columns: 1.2fr 1fr 1fr; gap: var(--space-4); }
.status-bars { margin-top: var(--space-4); display: flex; flex-direction: column; gap: var(--space-4); }
.bar-row { font-size: 14px; }
.bar-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--space-3); margin-bottom: 6px; }
.bar-label { display: flex; align-items: center; gap: var(--space-2); color: var(--slate-700); font-weight: 600; min-width: 0; }
.bar-label .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.bar-count { font-weight: 700; color: var(--slate-900); font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar-count span { color: var(--slate-400); font-weight: 600; margin-left: 6px; }
.bar-track { height: 8px; background: var(--slate-100); border-radius: var(--radius-pill); overflow: hidden; }
.bar-fill { height: 100%; border-radius: var(--radius-pill); transition: width 500ms var(--ease); }

.mini-list { display: flex; flex-direction: column; gap: 6px; max-height: 280px; overflow-y: auto; overscroll-behavior: contain; }
.mini-list-item {
  display: flex; justify-content: space-between; align-items: center; gap: var(--space-3);
  min-height: 44px; padding: 0 var(--space-3);
  background: var(--slate-50); border: 1px solid var(--slate-100); border-radius: var(--radius-md);
  font-size: 14px;
}
.mini-list-item.empty { justify-content: center; color: var(--slate-400); text-align: center; padding: var(--space-3); }
.mini-list-item .mli-plate { font-weight: 700; color: var(--slate-800); }
.mini-list-item .mli-meta { color: var(--slate-500); font-size: 13px; white-space: nowrap; }

/* ---------- Trips ---------- */
.trip-filters {
  display: grid; grid-template-columns: minmax(220px, 1.5fr) minmax(150px, 1fr) minmax(150px, 1fr) auto;
  gap: var(--space-4); align-items: end; margin-bottom: var(--space-5);
}
.trip-filters .field { margin-bottom: 0; min-width: 0; }
.trip-filters select, .trip-filters input[type=date] { height: 44px; font-size: 15px; -webkit-appearance: none; appearance: none; }
.trip-filters select {
  padding-right: 36px;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 12px center / 18px;
}
.trip-filters input[type=date] { min-width: 0; }
.trip-search-btn { height: 44px; white-space: nowrap; }
.trip-search-btn .icon { width: 18px; height: 18px; }

.trip-summary { display: flex; flex-wrap: wrap; gap: var(--space-3); margin-bottom: var(--space-4); }
.trip-stat { background: #fff; border: 1px solid var(--slate-200); border-radius: var(--radius-md); padding: 10px var(--space-4); box-shadow: var(--shadow-xs); }
.trip-stat-label { font-size: 12px; font-weight: 600; color: var(--slate-500); }
.trip-stat-value { font-size: 20px; font-weight: 800; color: var(--slate-900); font-variant-numeric: tabular-nums; }
.trip-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--space-4); align-items: start; }
#trip-map { height: 460px; border-radius: var(--radius-lg); border: 1px solid var(--slate-200); box-shadow: var(--shadow-xs); }
.trip-table-wrap { padding: 0; overflow: hidden; }
.trip-table-scroll { max-height: 460px; overflow-y: auto; }
.trip-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.trip-table th {
  position: sticky; top: 0; z-index: 1;
  text-align: left; background: var(--slate-50); color: var(--slate-500);
  font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  padding: 12px var(--space-4); border-bottom: 1px solid var(--slate-200);
}
.trip-table td { padding: 12px var(--space-4); border-bottom: 1px solid var(--slate-100); color: var(--slate-700); font-variant-numeric: tabular-nums; white-space: nowrap; }
.trip-table tr:last-child td { border-bottom: none; }
.trip-table tbody tr:hover td { background: var(--slate-50); }
.trip-cards { display: none; }
.trip-card { background: #fff; border: 1px solid var(--slate-200); border-radius: var(--radius-lg); box-shadow: var(--shadow-xs); padding: var(--space-4); }
.trip-card + .trip-card { margin-top: var(--space-3); }
.trip-card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); margin-bottom: var(--space-3); }
.trip-card-time { font-weight: 700; color: var(--slate-900); font-size: 15px; }
.trip-card-index { font-size: 12px; font-weight: 700; color: var(--slate-400); }
.trip-card-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-2); }
.trip-card-stat { background: var(--slate-50); border-radius: var(--radius-sm); padding: var(--space-2) 10px; min-width: 0; }
.trip-card-stat .k { font-size: 11.5px; font-weight: 600; color: var(--slate-500); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.trip-card-stat .v { font-size: 15px; font-weight: 700; color: var(--slate-900); font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty-state { padding: var(--space-10) var(--space-5); text-align: center; color: var(--slate-400); font-size: 15px; }
.empty-state.error { color: var(--status-offline); }

/* ---------- Chat ---------- */
.chat-bubble {
  position: fixed; z-index: 70;
  right: var(--fab-right); bottom: var(--fab-bottom);
  width: var(--fab-size); height: var(--fab-size); border-radius: 50%; border: none;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #14b8a6, var(--accent-dark)); color: #fff;
  box-shadow: 0 10px 24px rgba(13, 148, 136, 0.35), 0 2px 6px rgba(15, 23, 42, 0.15);
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease), opacity var(--dur) var(--ease);
}
.chat-bubble .icon { width: 26px; height: 26px; }
.chat-bubble:hover { transform: translateY(-1px) scale(1.04); }
.chat-open .chat-bubble { opacity: 0; pointer-events: none; transform: scale(0.8); }

.chat-panel {
  position: fixed; z-index: 80;
  right: max(var(--space-6), var(--safe-right)); bottom: max(var(--space-6), var(--safe-bottom));
  width: 400px; height: min(640px, calc(100dvh - 2 * var(--space-6) - var(--header-h)));
  display: flex; flex-direction: column;
  background: #fff; border: 1px solid var(--slate-200);
  border-radius: var(--radius-xl); box-shadow: var(--shadow-xl);
  overflow: hidden;
  opacity: 0; visibility: hidden; transform: translateY(12px) scale(0.98); transform-origin: bottom right;
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease), visibility 0s linear var(--dur);
}
.chat-panel.open { opacity: 1; visibility: visible; transform: none; transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease); }
.chat-panel-header {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--slate-200);
  flex-shrink: 0;
}
.chat-avatar {
  width: 40px; height: 40px; border-radius: 12px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #14b8a6, var(--accent-dark)); color: #fff;
}
.chat-avatar .icon { width: 22px; height: 22px; }
.chat-heading { flex: 1; min-width: 0; }
.chat-title { font-weight: 700; font-size: 16px; color: var(--slate-900); }
.chat-subtitle { font-size: 12.5px; color: var(--slate-500); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-close { border-color: transparent; background: var(--slate-100); }
.chat-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-messages {
  position: relative;
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: var(--space-4); display: flex; flex-direction: column; gap: var(--space-3);
}
.chat-intro { margin: auto 0; text-align: center; padding: var(--space-4) var(--space-2); }
.chat-intro .chat-avatar { width: 52px; height: 52px; border-radius: 16px; margin-bottom: var(--space-3); }
.chat-intro .chat-avatar .icon { width: 28px; height: 28px; }
.chat-intro-title { font-weight: 700; font-size: 17px; color: var(--slate-900); margin-bottom: var(--space-1); }
.chat-intro-sub { font-size: 14px; color: var(--slate-500); }
.has-messages .chat-intro { display: none; }

.chat-msg { max-width: 86%; padding: 10px 14px; border-radius: 18px; font-size: 15px; line-height: 1.5; overflow-wrap: anywhere; }
.chat-msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 6px; white-space: pre-wrap; }
.chat-msg.assistant { align-self: flex-start; background: var(--slate-100); color: var(--slate-800); border-bottom-left-radius: 6px; }
.chat-msg.assistant p { margin: 0; }
.chat-msg.assistant p + p, .chat-msg.assistant p + ul, .chat-msg.assistant ul + p { margin-top: var(--space-2); }
.chat-msg.assistant ul { margin: 0; padding-left: 18px; }
.chat-msg.assistant li + li { margin-top: 2px; }
.chat-msg.assistant strong { font-weight: 700; color: var(--slate-900); }
.chat-msg.system { align-self: center; max-width: 92%; text-align: center; background: #fef2f2; color: #991b1b; font-size: 13.5px; border-radius: var(--radius-md); }
.chat-typing { align-self: flex-start; display: inline-flex; align-items: center; gap: 10px; background: var(--slate-100); border-radius: 18px; border-bottom-left-radius: 6px; padding: 12px 14px; color: var(--slate-500); font-size: 13px; }
.typing-dots { display: inline-flex; gap: 4px; }
.typing-dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--slate-400); animation: typing 1.2s infinite ease-in-out; }
.typing-dots i:nth-child(2) { animation-delay: 0.15s; }
.typing-dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-4px); opacity: 1; } }

.chat-suggestions { display: flex; flex-wrap: wrap; gap: var(--space-2); padding: 0 var(--space-4) var(--space-3); flex-shrink: 0; }
.has-messages .chat-suggestions { display: none; }
.chat-chip {
  min-height: 36px; padding: 7px var(--space-3); text-align: left;
  background: #fff; border: 1px solid var(--slate-200); border-radius: var(--radius-md);
  font-size: 13.5px; color: var(--slate-700); line-height: 1.35;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.chat-chip:hover { background: var(--accent-50); border-color: var(--accent-light); }
.chat-input-row {
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  border-top: 1px solid var(--slate-200); background: #fff; flex-shrink: 0;
}
.chat-input-row input {
  flex: 1; min-width: 0; height: 44px; padding: 0 var(--space-4);
  border: 1px solid var(--slate-200); border-radius: var(--radius-pill);
  background: var(--slate-50); font-size: 16px; outline: none;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background var(--dur) var(--ease);
}
.chat-input-row input:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 4px var(--accent-light); }
.chat-send {
  width: 44px; height: 44px; flex-shrink: 0; border: none; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff;
  transition: background var(--dur) var(--ease), opacity var(--dur) var(--ease);
}
.chat-send:hover { background: var(--accent-dark); }
.chat-send:disabled { opacity: 0.45; cursor: default; }
.chat-send .icon { width: 20px; height: 20px; margin-left: -2px; }
.chat-unavailable { margin: auto; padding: var(--space-6); text-align: center; color: var(--slate-500); font-size: 14px; }
.chat-unavailable .cu-title { font-weight: 700; color: var(--slate-800); margin-bottom: var(--space-1); font-size: 16px; }

/* ---------- Leaflet markers ---------- */
.fv-marker {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  display: flex; align-items: center; justify-content: center;
}
.fv-marker .arrow { transition: transform 300ms ease; }
.fv-cluster {
  background: var(--slate-800); color: #fff; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 12px;
  border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}


/* ---------- Shared page components ---------- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-4); margin-bottom: var(--space-6); }
.page-head .page-title { margin-bottom: var(--space-1); }
.page-sub { margin: 0; color: var(--slate-500); font-size: 14px; }
.demo-badge-inline { box-shadow: none; flex-shrink: 0; }
.demo-badge-inline[hidden] { display: none; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.kpi-tile small { font-size: 18px; font-weight: 700; color: var(--slate-500); margin-left: 2px; }
.notice {
  display: flex; gap: var(--space-3); align-items: flex-start;
  margin-bottom: var(--space-4); padding: var(--space-4) var(--space-5);
  background: #fffbeb; border: 1px solid #fde68a; border-radius: var(--radius-lg); color: #78350f;
}
.notice .icon { color: #b45309; margin-top: 1px; }
.notice-title { font-weight: 700; margin-bottom: 2px; }
.notice-body { font-size: 14px; line-height: 1.5; }
.select-field select {
  width: 100%; height: 42px; padding: 0 36px 0 var(--space-3);
  border: 1px solid var(--slate-200); border-radius: var(--radius-md); font-size: 15px;
  -webkit-appearance: none; appearance: none;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 12px center / 18px;
}
.select-field select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-light); }
.btn-secondary { background: #fff; color: var(--slate-700); border: 1px solid var(--slate-200); flex-shrink: 0; }
.btn-secondary:hover { background: var(--slate-50); border-color: var(--slate-300); }

.panel-link {
  display: inline-flex; align-items: center; gap: 4px; min-height: 44px; margin-top: var(--space-2);
  font-size: 14px; font-weight: 600; color: var(--accent-dark); text-decoration: none;
}
.panel-link .icon { width: 16px; height: 16px; }
.panel-link:hover { text-decoration: underline; }

/* =============================================================================
   Large tablet / small laptop
   ============================================================================= */
@media (max-width: 1100px) {
  .kpi-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .kpi-total { grid-column: span 3; }
  .kpi-tile.kpi-total { display: flex; align-items: center; justify-content: space-between; }
  .kpi-tile.kpi-total .kpi-label { margin-bottom: 0; font-size: 14px; }
  .panel-grid { grid-template-columns: 1fr 1fr; }
  .panel-grid > .panel:first-child { grid-column: span 2; }
  .trip-layout { grid-template-columns: minmax(0, 1fr); }
  #trip-map { height: 380px; }
  .trip-table-scroll { max-height: none; }
  .trip-filters { grid-template-columns: 1fr 1fr; }
  .trip-filters .field-vehicle { grid-column: span 2; }
  .trip-search-btn { grid-column: span 2; }
}

/* =============================================================================
   Tablet & phone (touch layouts): hamburger nav, vehicles drawer, 44px targets
   ============================================================================= */
@media (max-width: 900px) {
  body { font-size: 15px; }
  .app-header { gap: var(--space-2); padding-left: max(var(--space-4), var(--safe-left)); padding-right: max(var(--space-4), var(--safe-right)); }

  .nav-toggle { display: inline-flex; }
  .btn-logout { width: var(--tap); padding: 0; height: var(--tap); }
  .logout-label { display: none; }
  .lang-switch { padding: 0; gap: 0; background: #fff; box-shadow: inset 0 0 0 1px var(--slate-200); }
  .lang-switch button { min-width: 44px; height: 44px; border-radius: var(--radius-md); }
  .lang-switch button.active { background: var(--accent-50); box-shadow: inset 0 0 0 1px var(--accent-light); }
  .nav-open .chat-bubble, .drawer-open .chat-bubble { opacity: 0; pointer-events: none; transform: scale(0.8); }

  .app-nav {
    position: absolute; top: 100%; left: 0; right: 0;
    margin: 0; flex-direction: column; gap: 2px;
    padding: var(--space-2) max(var(--space-3), var(--safe-right)) var(--space-3) max(var(--space-3), var(--safe-left));
    background: #fff; border-bottom: 1px solid var(--slate-200);
    box-shadow: var(--shadow-lg);
    border-bottom-left-radius: var(--radius-lg); border-bottom-right-radius: var(--radius-lg);
    opacity: 0; visibility: hidden; transform: translateY(-8px);
    transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease), visibility 0s linear var(--dur);
  }
  .app-nav.open { opacity: 1; visibility: visible; transform: none; transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease); }
  .app-nav a { height: 52px; padding: 0 var(--space-4); font-size: 16px; gap: var(--space-3); }
  .app-nav a .nav-icon { width: 22px; height: 22px; }

  /* Vehicles drawer (left) */
  .map-sidebar {
    position: fixed; z-index: 50;
    top: calc(var(--header-h) + var(--safe-top)); bottom: 0; left: 0;
    width: min(380px, 88vw);
    border-right: none;
    box-shadow: var(--shadow-xl);
    transform: translateX(-104%);
    transition: transform 280ms var(--ease);
    padding-bottom: var(--safe-bottom);
  }
  .map-sidebar.open { transform: translateX(0); }
  .sidebar-head { padding: var(--space-4) var(--space-4) var(--space-2) var(--space-5); }
  .sidebar-close { display: inline-flex; border-color: transparent; background: var(--slate-100); }
  .search-input { height: 48px; font-size: 16px; }
  .status-chip { height: 44px; padding: 0 14px; font-size: 14px; }
  .vehicle-row { min-height: 68px; }

  /* Floating map controls */
  .leaflet-bar a, .leaflet-bar a:hover { width: 44px !important; height: 44px !important; line-height: 44px !important; font-size: 22px !important; }
  .map-toggle-geofences { height: 44px; }

  .sidebar-toggle {
    display: inline-flex; align-items: center; gap: var(--space-2);
    position: absolute; z-index: 10;
    left: max(var(--space-4), var(--safe-left)); bottom: max(var(--space-4), var(--safe-bottom));
    height: 48px; padding: 0 var(--space-3) 0 var(--space-4);
    border: none; border-radius: var(--radius-pill);
    background: var(--slate-900); color: #fff;
    font-size: 15px; font-weight: 700;
    box-shadow: 0 10px 24px rgba(15, 23, 42, 0.28);
    transition: transform var(--dur) var(--ease), opacity var(--dur) var(--ease);
  }
  .sidebar-toggle:active { transform: scale(0.97); }
  .map-status { bottom: calc(max(var(--space-4), var(--safe-bottom)) + 48px + var(--space-3)); left: max(var(--space-4), var(--safe-left)); }

  .vehicle-detail-card { top: 68px; width: min(360px, calc(100% - 2 * var(--space-3))); max-height: calc(100% - 68px - var(--fab-bottom) - var(--fab-size) - var(--space-3)); }
  .vd-close, .sidebar-close, .chat-close { width: 44px; height: 44px; }

  .page { padding: var(--space-6) max(var(--space-6), var(--fab-lane)) var(--fab-clear) max(var(--space-6), var(--safe-left)); }
  .trip-filters select, .trip-filters input[type=date] { height: 48px; font-size: 16px; }
  .trip-search-btn { height: 48px; font-size: 16px; }

  .chat-chip { min-height: 44px; font-size: 14px; }

}

/* =============================================================================
   Phone: bottom sheets, full-screen chat, compact header
   ============================================================================= */
@media (max-width: 640px) {
  .brand .logo-mark { width: 32px; height: 32px; }
  .brand-name { font-size: 17px; }

  /* Vehicles list becomes a bottom sheet */
  .map-sidebar {
    top: auto; left: 0; right: 0; bottom: 0;
    width: 100%; height: min(84dvh, 760px);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    transform: translateY(104%);
  }
  .map-sidebar.open { transform: translateY(0); }
  .sheet-handle { display: block; width: 40px; height: 5px; border-radius: var(--radius-pill); background: var(--slate-300); margin: 10px auto 0; flex-shrink: 0; }
  .sidebar-head { padding-top: var(--space-2); }
  .sidebar-controls { padding-left: var(--space-4); padding-right: var(--space-4); }
  .vehicle-row { padding-left: var(--space-4); padding-right: var(--space-4); }

  .map-toggle-geofences { width: 44px; padding: 0; justify-content: center; }
  .map-toggle-geofences .switch, .map-toggle-geofences .toggle-label { display: none; }
  .map-toggle-geofences .icon { display: block; color: var(--slate-400); }
  .map-toggle-geofences:has(input:checked) { background: var(--accent-50); border-color: var(--accent-light); }
  .map-toggle-geofences:has(input:checked) .icon { color: var(--accent-dark); }


  /* Vehicle detail: bottom sheet */
  .vehicle-detail-card {
    position: fixed; z-index: 50;
    top: auto; left: 0; right: 0; bottom: 0;
    width: 100%; max-height: 72dvh;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    border-left: none; border-right: none; border-bottom: none;
    padding-bottom: var(--safe-bottom);
    transform: translateY(104%);
    transition: transform 300ms var(--ease), visibility 0s linear 300ms;
    opacity: 1;
  }
  .vehicle-detail-card.visible { transform: translateY(0); transition: transform 300ms var(--ease); }
  .vd-header { padding: var(--space-2) var(--space-4) var(--space-3) var(--space-5); }
  .detail-open .sidebar-toggle, .detail-open .map-status, .detail-open .chat-bubble { opacity: 0; pointer-events: none; }

  /* Overview */
  .page { padding: var(--space-5) max(var(--space-4), var(--safe-right)) var(--space-6) max(var(--space-4), var(--safe-left)); }
  .page-title { font-size: 22px; margin-bottom: var(--space-4); }
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); margin-bottom: var(--space-4); }
  .kpi-total { grid-column: span 2; }
  .kpi-tile { padding: var(--space-3) var(--space-4) var(--space-4); }
  .kpi-tile .kpi-value { font-size: 28px; }
  .panel { padding: var(--space-4); border-radius: var(--radius-lg); }
  .panel-grid { grid-template-columns: minmax(0, 1fr); gap: var(--space-3); }
  .panel-grid > .panel:first-child { grid-column: auto; }
  .mini-list { max-height: none; overflow: visible; }

  /* Trips */
  .trip-filters { padding: var(--space-4); gap: var(--space-3); }
  #trip-map { height: 260px; }
  .trip-table-wrap { display: none; }
  .trip-cards { display: block; }
  .trip-stat { flex: 1; min-width: 0; }

  /* Chat: full-screen sheet that tracks the visual viewport (on-screen keyboard) */
  .chat-panel {
    top: var(--vv-top, 0px); left: 0; right: 0; bottom: auto;
    width: 100%; height: var(--vv-height, 100dvh);
    border: none; border-radius: 0;
    transform: translateY(24px); transform-origin: bottom center;
  }
  .chat-panel.open { transform: none; }
  .chat-panel-header { padding-top: calc(var(--space-3) + var(--safe-top)); padding-left: max(var(--space-4), var(--safe-left)); padding-right: max(var(--space-3), var(--safe-right)); min-height: 64px; }
  .chat-messages { padding: var(--space-4) max(var(--space-4), var(--safe-right)) var(--space-4) max(var(--space-4), var(--safe-left)); }
  .chat-input-row { padding-bottom: max(var(--space-3), var(--safe-bottom)); padding-left: max(var(--space-3), var(--safe-left)); padding-right: max(var(--space-3), var(--safe-right)); }
  .chat-input-row input { height: 48px; }
  .chat-send { width: 48px; height: 48px; }
  .chat-chip { width: 100%; }
  html.chat-open, html.chat-open body { overflow: hidden; }

  .page-head { flex-direction: column; gap: var(--space-2); margin-bottom: var(--space-4); }
  .distance-panel { grid-template-columns: minmax(0, 1fr); gap: var(--space-5); }
  .distance-total { font-size: 36px; }
  .kpi-tile small { font-size: 16px; }
  .notice { padding: var(--space-3) var(--space-4); }
  .hist-plot { height: 170px; gap: 4px; }
  .hist-col-gap { margin-left: 6px; }
  .hist-count { font-size: 11px; }
  .hist-tick { font-size: 10.5px; }
}

@media (max-width: 359px) {
  .brand-name { display: none; }
  .brand { min-width: 44px; justify-content: center; }
  .trip-card-stat { padding: var(--space-2); }
  .trip-card-stat .k { white-space: normal; overflow: visible; line-height: 1.25; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}

/* Explicit "Last report" column in the vehicle list; offline / never-reported legend. */
.v-time { display: flex; flex-direction: column; align-items: flex-end; text-align: right; line-height: 1.2; cursor: help; }
.v-time-label { font-size: 10.5px; font-weight: 600; color: var(--slate-400); text-transform: uppercase; letter-spacing: 0.03em; }
.status-legend { display: flex; gap: 6px; align-items: flex-start; margin: var(--space-2) 0 0; font-size: 12px; line-height: 1.45; color: var(--slate-500); }
.status-legend .icon { width: 14px; height: 14px; flex-shrink: 0; margin-top: 2px; color: var(--slate-400); }
.vd-stat[title] { cursor: help; }
.vd-stat-value.vd-stat-text { white-space: normal; font-size: 14px; line-height: 1.25; overflow: visible; text-overflow: clip; }

/* Titled vehicle list: sticky column titles, labelled secondary fields. */
.vehicle-list-head {
  display: flex; justify-content: space-between; gap: var(--space-3);
  padding: 8px var(--space-5) 8px calc(var(--space-5) + 22px);
  background: var(--slate-50); border-bottom: 1px solid var(--slate-200);
  font-size: 10.5px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--slate-400);
}
.vehicle-row { align-items: flex-start; }
.vehicle-row .status-dot { margin-top: 6px; }
.v-line1 { display: flex; align-items: baseline; gap: var(--space-2); flex-wrap: wrap; }
.v-status { font-size: 12.5px; font-weight: 600; color: var(--slate-500); }
.v-status.s-moving { color: #15803d; }
.v-status.s-idle { color: #b45309; }
.v-status.s-offline, .v-status.s-never { color: #b91c1c; }
.v-field { display: flex; gap: 6px; align-items: baseline; margin-top: 3px; min-width: 0; font-size: 12.5px; }
.v-label { flex-shrink: 0; font-size: 10px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--slate-400); }
.v-val { color: var(--slate-600); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.vehicle-row .v-time { margin-top: 2px; font-size: 12.5px; color: var(--slate-500); }
.vd-kicker { font-size: 10.5px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--slate-400); margin-bottom: 2px; }
.mini-head { display: flex; justify-content: space-between; padding: 0 var(--space-3) 6px; font-size: 10.5px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--slate-400); }
.distance-top .mini-head { padding: 0 0 6px; }
@media (max-width: 640px) {
  .vehicle-list-head { padding-left: calc(var(--space-4) + 22px); padding-right: var(--space-4); }
  .v-field { font-size: 13px; }
}

/* Chat markdown: numbered lists, headings, inline code, and tables that scroll sideways on phones. */
.chat-msg.assistant ol { margin: 0; padding-left: 20px; }
.chat-msg.assistant p + ol, .chat-msg.assistant ol + p, .chat-msg.assistant p + .chat-table-wrap, .chat-msg.assistant .chat-table-wrap + p,
.chat-msg.assistant ul + .chat-table-wrap, .chat-msg.assistant .chat-table-wrap + ul { margin-top: var(--space-2); }
.chat-msg.assistant .chat-h { font-weight: 700; color: var(--slate-900); }
.chat-msg.assistant em { font-style: italic; }
.chat-msg.assistant code { font-size: 13px; background: rgba(15, 23, 42, 0.06); padding: 1px 5px; border-radius: 5px; }
.chat-table-wrap { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--slate-200); border-radius: var(--radius-sm); background: #fff; }
.chat-table { border-collapse: collapse; width: 100%; font-size: 13px; line-height: 1.35; }
.chat-table th { text-align: left; white-space: nowrap; background: var(--slate-50); color: var(--slate-600); font-weight: 700; font-size: 12px; padding: 7px 10px; border-bottom: 1px solid var(--slate-200); }
.chat-table td { padding: 6px 10px; border-bottom: 1px solid var(--slate-100); color: var(--slate-800); white-space: nowrap; overflow-wrap: normal; }
.chat-table tr:last-child td { border-bottom: none; }
.chat-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.chat-msg.assistant:has(.chat-table-wrap) { max-width: 94%; }

/* ---------- Chat dock (phones) ----------
   On phones a floating button can't get a free lane beside the content, so scrolling pages end
   above a slim bottom bar that holds the chat button plus an "Ask about your fleet…" prompt.
   The live map stays full-screen with the button floating over the map itself. */
.chat-dock { display: none; }
@media (max-width: 640px) {
  :root { --dock-h: calc(var(--fab-size) + 2 * var(--fab-gap) + var(--safe-bottom)); }
  body[data-view]:not([data-view="map"]) .view { bottom: var(--dock-h); }
  body[data-view]:not([data-view="map"]) .chat-dock {
    display: flex; align-items: center;
    position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; height: var(--dock-h);
    padding: 0 calc(var(--fab-right) + var(--fab-size) + var(--space-3)) var(--safe-bottom) max(var(--space-4), var(--safe-left));
    background: #fff; border-top: 1px solid var(--slate-200); box-shadow: 0 -6px 18px rgba(15, 23, 42, 0.05);
  }
  .chat-dock-prompt {
    flex: 1; min-width: 0; height: 48px; padding: 0 var(--space-4);
    display: flex; align-items: center; gap: var(--space-2);
    border: 1px solid var(--slate-200); border-radius: var(--radius-pill); background: var(--slate-50);
    color: var(--slate-500); font: inherit; font-size: 15px; text-align: left; cursor: pointer;
  }
  .chat-dock-prompt .icon { width: 18px; height: 18px; flex-shrink: 0; color: var(--accent); }
  .chat-dock-prompt span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-open .chat-dock, .nav-open .chat-dock { visibility: hidden; }
}
