/* ==========================================================================
   Tracking platform — design system + application shell
   Dark fleet-operations theme. Tokens first, then components, layout, responsive.
   ========================================================================== */
:root {
  /* Palette — clean light theme, royal-blue primary (BoldTrack-style) */
  --bg: #EEF1F6;
  --panel: #FFFFFF;
  --panel-2: #F3F6FB;
  --panel-3: #E7ECF4;
  --primary: #2563EB;
  --primary-dim: #1D4FD1;
  --teal: #16A34A;
  --amber: #E08E0B;
  --red: #E5484D;

  /* Text */
  --text: #172033;
  --text-muted: #5B667A;
  --text-dim: #8A94A6;

  /* Lines & surfaces */
  --border: #E2E8F1;
  --border-soft: #EBEFF5;
  --overlay: rgba(20, 28, 45, .45);

  /* Radii / shadow / spacing */
  --r-sm: 6px; --r: 10px; --r-lg: 14px; --r-full: 999px;
  --shadow: 0 6px 24px rgba(20,30,60,.12);
  --shadow-sm: 0 1px 3px rgba(20,30,60,.08);
  --sidebar-w: 236px;
  --topbar-h: 58px;
  --bottom-nav-h: 60px;
  --ff: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body, #app-root { height: 100%; margin: 0; }
#app-root { overflow: hidden; }
body {
  font-family: var(--ff);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.dim { color: var(--text-dim); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.grow { flex: 1; }
.row { display: flex; align-items: center; gap: 10px; }
.col { display: flex; flex-direction: column; gap: 10px; }
.wrap { flex-wrap: wrap; }
h1,h2,h3,h4 { margin: 0; font-weight: 650; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #c7d0dd; border-radius: 10px; }
::-webkit-scrollbar-track { background: transparent; }

/* Icons (Lucide) */
.i { width: 18px; height: 18px; stroke-width: 2; vertical-align: middle; flex: none; }
.i-sm { width: 15px; height: 15px; }
.i-lg { width: 22px; height: 22px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px; justify-content: center;
  padding: 8px 14px; border-radius: var(--r-sm); border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 13px; font-weight: 550;
  cursor: pointer; white-space: nowrap; transition: background .12s, border-color .12s, opacity .12s;
}
.btn:hover { background: var(--panel-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dim); }
.btn.danger { background: transparent; border-color: #f0c0c3; color: var(--red); }
.btn.danger:hover { background: rgba(240,100,100,.12); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.sm { padding: 5px 9px; font-size: 12px; }
.btn.icon { padding: 7px; }

/* ---------- Inputs ---------- */
.input, select.input, textarea.input {
  width: 100%; padding: 9px 11px; border-radius: var(--r-sm);
  border: 1px solid var(--border); background: #ffffff; color: var(--text);
  font-size: 13px; font-family: inherit; outline: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.18); }
.input::placeholder { color: var(--text-dim); }
label.field { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.field-group { margin-bottom: 14px; }
.input-icon { position: relative; }
.input-icon .i { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--text-dim); }
.input-icon .input { padding-left: 34px; }

/* ---------- Badges / status ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px;
  border-radius: var(--r-full); font-size: 11px; font-weight: 600; line-height: 1.6;
  border: 1px solid transparent; white-space: nowrap;
}
.badge .dot { width: 7px; height: 7px; border-radius: 50%; }
.badge.online { background: rgba(32,201,151,.14); color: var(--teal); }
.badge.online .dot { background: var(--teal); }
.badge.offline { background: rgba(240,100,100,.14); color: var(--red); }
.badge.offline .dot { background: var(--red); }
.badge.stale { background: rgba(245,185,66,.15); color: var(--amber); }
.badge.stale .dot { background: var(--amber); }
.badge.moving { background: rgba(37,99,235,.15); color: var(--primary); }
.badge.idle { background: rgba(148,163,184,.14); color: var(--text-muted); }
.badge.sim { background: rgba(245,185,66,.16); color: var(--amber); border-color: rgba(245,185,66,.35); }
.badge.alarm { background: rgba(240,100,100,.16); color: var(--red); }
.badge.warning { background: rgba(245,185,66,.16); color: var(--amber); }
.badge.info { background: rgba(148,163,184,.14); color: var(--text-muted); }
.badge.neutral { background: var(--panel-2); color: var(--text-muted); }

/* ---------- Cards ---------- */
.card {
  background: var(--panel); border: 1px solid var(--border-soft);
  border-radius: var(--r-lg); box-shadow: var(--shadow-sm);
}
.card-pad { padding: 16px; }
.card-head { padding: 14px 16px; border-bottom: 1px solid var(--border-soft); display: flex; align-items: center; gap: 10px; }
.card-head h3 { font-size: 14px; }

/* Stat cards */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; }
.stat {
  background: var(--panel); border: 1px solid var(--border-soft); border-radius: var(--r-lg);
  padding: 15px 16px; display: flex; flex-direction: column; gap: 8px; position: relative; overflow: hidden;
}
.stat .label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 27px; font-weight: 700; line-height: 1; }
.stat .sub { font-size: 12px; color: var(--text-dim); }
.stat .ico { position: absolute; top: 14px; right: 14px; width: 34px; height: 34px; border-radius: 9px;
  display: grid; place-items: center; background: var(--panel-2); color: var(--primary); }
.stat.ok .ico { color: var(--teal); } .stat.ok .value { color: var(--teal); }
.stat.warn .ico { color: var(--amber); } .stat.warn .value { color: var(--amber); }
.stat.bad .ico { color: var(--red); } .stat.bad .value { color: var(--red); }
.stat.pri .ico { color: var(--primary); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
table.tbl th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-muted); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--panel); z-index: 1;
}
table.tbl td { padding: 10px 12px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
table.tbl tr:hover td { background: rgba(0,0,0,.03); }
table.tbl tr.clickable { cursor: pointer; }
table.tbl td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- States ---------- */
.state { display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 40px 20px; text-align: center; color: var(--text-muted); }
.state .i { width: 34px; height: 34px; color: var(--text-dim); }
.state h4 { color: var(--text); font-weight: 600; }
.spinner { width: 26px; height: 26px; border: 3px solid var(--panel-3); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { background: linear-gradient(90deg, var(--panel-2) 25%, var(--panel-3) 37%, var(--panel-2) 63%);
  background-size: 400% 100%; animation: sk 1.3s ease infinite; border-radius: var(--r-sm); }
@keyframes sk { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }

/* ---------- Toasts ---------- */
#toasts { position: fixed; top: 16px; right: 16px; z-index: 4000; display: flex; flex-direction: column; gap: 10px; }
.toast { background: var(--panel); border: 1px solid var(--border); border-left: 3px solid var(--primary);
  border-radius: var(--r); padding: 11px 14px; box-shadow: var(--shadow); min-width: 240px; max-width: 360px;
  display: flex; gap: 10px; align-items: flex-start; animation: slidein .2s ease; }
.toast.success { border-left-color: var(--teal); } .toast.error { border-left-color: var(--red); }
.toast.warn { border-left-color: var(--amber); }
.toast .i { color: var(--primary); } .toast.success .i { color: var(--teal); } .toast.error .i { color: var(--red); }
@keyframes slidein { from { transform: translateX(20px); opacity: 0; } }

/* ---------- Dialog ---------- */
.modal-backdrop { position: fixed; inset: 0; background: var(--overlay); z-index: 3000;
  display: grid; place-items: center; padding: 16px; animation: fade .15s ease; }
@keyframes fade { from { opacity: 0; } }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: var(--r-lg);
  width: 100%; max-width: 460px; box-shadow: var(--shadow); max-height: 90vh; display: flex; flex-direction: column; }
.modal-head { padding: 15px 18px; border-bottom: 1px solid var(--border-soft); display: flex; align-items: center; }
.modal-head h3 { font-size: 15px; } .modal-body { padding: 18px; overflow-y: auto; }
.modal-foot { padding: 14px 18px; border-top: 1px solid var(--border-soft); display: flex; justify-content: flex-end; gap: 10px; }

/* ==========================================================================
   App shell
   ========================================================================== */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; grid-template-rows: 100%; height: 100%; }
.sidebar { background: #ffffff; border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 16px; height: var(--topbar-h);
  border-bottom: 1px solid var(--border); flex: none; }
.brand .logo { width: 30px; height: 30px; border-radius: 8px; background: linear-gradient(135deg, var(--primary), #6aa6ff);
  display: grid; place-items: center; color: #fff; }
.brand b { font-size: 15px; letter-spacing: .2px; } .brand small { display: block; color: var(--text-dim); font-size: 10px; }
.nav { flex: 1; overflow-y: auto; padding: 10px 10px 20px; }
.nav .group { font-size: 10px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-dim);
  padding: 14px 10px 6px; }
.nav-primary { display: flex; flex-direction: column; gap: 2px; padding-bottom: 8px; }
.nav-tools { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 2px; }
.nav-item, .nav-link { display: flex; align-items: center; gap: 11px; padding: 9px 11px; border-radius: var(--r-sm);
  color: var(--text-muted); font-weight: 500; margin-bottom: 2px; }
.nav-item { font-size: 13.5px; font-weight: 550; }
.nav-item .ni-ic { display: inline-flex; }
.nav-link { font-size: 13px; }
.nav-item:hover, .nav-link:hover { background: var(--panel); color: var(--text); }
.nav-item.active, .nav-link.active { background: rgba(37,99,235,.14); color: var(--primary); }
.nav-item.active .i, .nav-link.active .i { color: var(--primary); }
.nav-item .count { margin-left: auto; font-size: 11px; background: var(--red); color: #fff;
  border-radius: var(--r-full); padding: 0 7px; line-height: 1.5; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { height: var(--topbar-h); flex: none; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 14px; padding: 0 18px; background: var(--bg); }
.topbar .page-title { font-size: 16px; font-weight: 650; }
.topbar .page-sub { font-size: 12px; color: var(--text-dim); }
.hamburger { display: none; }
.tb-brand { display: none; align-items: center; gap: 9px; font-size: 15px; font-weight: 650; }
.tb-brand .logo { width: 28px; height: 28px; border-radius: 8px; color: #fff;
  background: linear-gradient(135deg, var(--primary), #6aa6ff); display: grid; place-items: center; }
.content { flex: 1; overflow-y: auto; padding: 20px; min-height: 0; }
.content.flush { padding: 0; }           /* for full-bleed map screens */

/* Bottom tab bar — mobile only (shown in the responsive block). */
.bottom-nav { display: none; }

.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); }
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); }
.conn.live .dot { background: var(--teal); box-shadow: 0 0 0 3px rgba(32,201,151,.18); }
.conn.stale .dot { background: var(--amber); }
.conn.down .dot { background: var(--red); }

.usermenu { display: flex; align-items: center; gap: 9px; cursor: default; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--panel-2); display: grid; place-items: center;
  color: var(--primary); font-weight: 700; font-size: 13px; }

.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h2 { font-size: 20px; }
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.chip-toggle { padding: 5px 11px; border: 1px solid var(--border); border-radius: var(--r-full);
  background: transparent; color: var(--text-muted); font-size: 12px; cursor: pointer; }
.chip-toggle.on { background: rgba(37,99,235,.15); border-color: var(--primary); color: var(--primary); }

/* ---------- Service hub (app-style tool grid) ---------- */
.service-group { margin-bottom: 22px; }
.service-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim);
  font-weight: 600; margin: 0 2px 10px; }
.service-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 12px; }
.service-tile { display: flex; flex-direction: column; align-items: center; gap: 9px; text-align: center;
  padding: 18px 10px; background: var(--panel); border: 1px solid var(--border-soft);
  border-radius: var(--r-lg); color: var(--text); transition: border-color .12s, background .12s, transform .08s; }
.service-tile:hover { border-color: var(--primary); background: var(--panel-2); }
.service-tile:active { transform: translateY(1px); }
.service-tile .ti { width: 46px; height: 46px; border-radius: 13px; display: grid; place-items: center;
  background: var(--panel-2); color: var(--primary); }
.service-tile:hover .ti { background: rgba(37,99,235,.16); }
.service-tile .tl { font-size: 12.5px; font-weight: 600; line-height: 1.25; }

/* ---------- Mine (account) ---------- */
.mine { max-width: 560px; margin: 0 auto; }
.mine-head { display: flex; align-items: center; gap: 15px; }
.mine-avatar { width: 58px; height: 58px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--panel-2), var(--panel-3)); color: var(--primary); font-weight: 700; font-size: 21px; }
.mine-email { font-size: 17px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; }
.mine-group-title { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim);
  font-weight: 600; margin: 22px 4px 8px; }
.mine-list { overflow: hidden; }
.mine-row { display: flex; align-items: center; gap: 12px; padding: 13px 15px; color: var(--text);
  font-weight: 500; font-size: 14px; border-bottom: 1px solid var(--border-soft); }
.mine-row:last-child { border-bottom: 0; }
.mine-row:hover { background: var(--panel-2); }
.mine-row .i { color: var(--text-muted); }
.mine-row:hover .i { color: var(--primary); }
.mine-row .i-sm { color: var(--text-dim); }
.mine-signout { width: 100%; margin-top: 22px; padding: 12px; }
.mine-foot { text-align: center; color: var(--text-dim); font-size: 12px; margin-top: 16px; }

/* ---------- Status strip (Monitor quick filters) ---------- */
.status-strip { display: flex; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--r-lg); overflow: hidden; box-shadow: var(--shadow); }
.status-strip .sc { flex: 1 1 0; min-width: 52px; display: flex; flex-direction: column; align-items: center;
  gap: 3px; padding: 9px 6px; cursor: pointer; border-right: 1px solid var(--border-soft);
  transition: background .12s; }
.status-strip .sc:last-child { border-right: 0; }
.status-strip .sc:hover { background: var(--panel-2); }
.status-strip .sc.on { background: rgba(37,99,235,.16); box-shadow: inset 0 -2px 0 var(--primary); }
.status-strip .sc .n { font-size: 18px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.status-strip .sc .l { font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; }
.status-strip .sc.ok .n { color: var(--teal); }
.status-strip .sc.pri .n { color: var(--primary); }
.status-strip .sc.warn .n { color: var(--amber); }
.status-strip .sc.bad .n { color: var(--red); }

/* ---------- Board: donut + ranking bars ---------- */
.donut-wrap { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.donut-svg { flex: none; }
.donut-num { fill: var(--text); font-size: 26px; font-weight: 700; }
.donut-lbl { fill: var(--text-muted); font-size: 10px; text-transform: uppercase; letter-spacing: .05em; }
.donut-legend { flex: 1; min-width: 120px; display: flex; flex-direction: column; gap: 9px; }
.dl-row { display: flex; align-items: center; gap: 9px; font-size: 13px; }
.dl-dot { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.dl-label { color: var(--text-muted); }
.dl-val { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; }
.rank { display: flex; flex-direction: column; gap: 13px; }
.rank-row { display: flex; flex-direction: column; gap: 5px; }
.rank-head { display: flex; align-items: baseline; gap: 8px; font-size: 12.5px; }
.rank-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.rank-val { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rank-u { font-size: 11px; font-weight: 500; color: var(--text-dim); margin-left: 2px; }
.rank-track { height: 7px; background: var(--panel-2); border-radius: var(--r-full); overflow: hidden; }
.rank-fill { height: 100%; border-radius: var(--r-full); min-width: 4px; transition: width .4s ease; }

/* ---------- Video camera grid ---------- */
.cam-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.cam-tile { border: 1px solid var(--border); border-radius: var(--r); overflow: hidden; background: #0b1320; }
.cam-tile.open { border-color: var(--primary); }
.cam-view { aspect-ratio: 16 / 9; display: grid; place-items: center; position: relative; padding: 12px;
  background: repeating-linear-gradient(45deg, #0b1320, #0b1320 10px, #0d1526 10px, #0d1526 20px); }
.cam-ph { text-align: center; color: var(--text-dim); }
.cam-ph .i { color: var(--text-dim); }
.cam-ph-t { margin-top: 8px; font-weight: 600; font-size: 13px; color: var(--text-muted); }
.cam-ph-t.cam-off { color: var(--text-dim); font-weight: 500; margin-top: 0; }
.cam-ph-s { font-size: 11px; margin-top: 4px; color: var(--text-dim); max-width: 220px; }
.cam-live-badge { position: absolute; top: 7px; left: 7px; background: var(--red); color: #fff; font-size: 9px;
  font-weight: 700; letter-spacing: .05em; padding: 1px 6px; border-radius: 3px; }
.cam-bar { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-top: 1px solid var(--border); background: var(--panel); }
.cam-ch { font-weight: 700; font-size: 12px; }
.cam-stat { margin-left: auto; font-size: 11px; color: var(--text-muted); }
.cam-rx { color: var(--teal); font-weight: 600; }
.cam-btn { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; padding: 0;
  border: 1px solid var(--border); border-radius: var(--r-sm); background: var(--panel-2); color: var(--text); cursor: pointer; }
.cam-btn:hover { background: var(--panel-3); }
.cam-btn.on { background: var(--red); border-color: var(--red); color: #fff; }
.cam-btn:disabled { opacity: .5; cursor: not-allowed; }
.cam-btn .i { width: 15px; height: 15px; }

/* ---------- Login ---------- */
.login { height: 100%; display: grid; place-items: center; background:
  radial-gradient(1200px 500px at 70% -10%, rgba(37,99,235,.14), transparent), var(--bg); padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--r-lg); box-shadow: var(--shadow); padding: 28px; }
.login-brand { display: flex; align-items: center; gap: 11px; margin-bottom: 20px; }
.login .pw { position: relative; }
.login .pw .toggle { position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  background: none; border: 0; color: var(--text-dim); cursor: pointer; padding: 4px; }

/* ---------- Map screens ---------- */
.map-layout { display: grid; grid-template-columns: 320px 1fr; height: 100%; min-height: 0; }
.map-side { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.map-side.collapsed { display: none; }
.map-wrap { position: relative; min-width: 0; }
#map, .mapel { position: absolute; inset: 0; background: #E4E9F0; }
.leaflet-container { background: #E4E9F0; font-family: var(--ff); }
/* Dark map: filter the standard OSM tiles (no keyed provider needed).
   Leaflet's className option puts the class on the layer container. */
.leaflet-layer.dark-tiles { filter: invert(1) hue-rotate(180deg) brightness(0.92) contrast(0.9) saturate(0.75); }
.leaflet-control-layers, .leaflet-bar a { background: var(--panel) !important; color: var(--text) !important; border-color: var(--border) !important; }
.leaflet-control-layers-expanded { padding: 8px 10px; }
.leaflet-bar a { border-bottom-color: var(--border) !important; }
.leaflet-control-attribution { background: rgba(255,255,255,.82) !important; color: var(--text-dim) !important; }
.leaflet-control-attribution a { color: var(--text-muted) !important; }
.veh-list { overflow-y: auto; flex: 1; }
.veh-item { padding: 10px 14px; border-bottom: 1px solid var(--border-soft); cursor: pointer; display: flex; gap: 10px; align-items: center; }
.veh-item:hover { background: var(--panel-2); } .veh-item.sel { background: rgba(37,99,235,.12); }
.veh-item .meta { min-width: 0; flex: 1; } .veh-item .title { font-weight: 600; font-size: 13px; }
.veh-item .sub { font-size: 11.5px; color: var(--text-muted); }
.map-overlay { position: absolute; z-index: 600; }
.map-tools { top: 12px; right: 12px; display: flex; flex-direction: column; gap: 8px; }
.map-overlay.status-top { top: 12px; left: 12px; right: 62px; max-width: 580px; }
@media (max-width: 820px) { .map-overlay.status-top { right: 12px; max-width: none; } }
.detail-panel { position: absolute; top: 12px; right: 12px; width: 300px; max-height: calc(100% - 24px);
  overflow-y: auto; z-index: 650; }
.kv { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--border-soft); font-size: 13px; }
.kv .k { color: var(--text-muted); } .kv .v { text-align: right; }

.marker-veh { border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 2px rgba(0,0,0,.3); }

/* Permanent vehicle name labels */
.leaflet-tooltip.veh-label { background: rgba(14,22,36,.92); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--r-sm); font-size: 11px; font-weight: 600; padding: 2px 7px; box-shadow: var(--shadow-sm); }
.leaflet-tooltip.veh-label::before { display: none; }
.leaflet-tooltip.veh-tip { background: var(--panel); color: var(--text); border: 1px solid var(--border); }

/* Map settings sheet (left side, below the status strip, clear of the detail panel) */
.map-settings { top: 72px; left: 12px; width: 238px; z-index: 670; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 0;
  font-size: 13px; border-bottom: 1px solid var(--border-soft); }
.set-row:last-of-type { border-bottom: 0; }
.switch { width: 38px; height: 22px; border-radius: var(--r-full); background: var(--panel-3);
  border: 1px solid var(--border); position: relative; cursor: pointer; flex: none; padding: 0; transition: background .15s; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--text-muted); transition: transform .15s, background .15s; }
.switch.on { background: rgba(37,99,235,.5); border-color: var(--primary); }
.switch.on::after { transform: translateX(16px); background: #fff; }
.seg-group { display: inline-flex; border: 1px solid var(--border); border-radius: var(--r-sm); overflow: hidden; }
.seg { padding: 4px 12px; background: transparent; color: var(--text-muted); border: 0; border-right: 1px solid var(--border);
  cursor: pointer; font-size: 12px; font-weight: 600; }
.seg:last-child { border-right: 0; }
.seg.on { background: var(--primary); color: #fff; }

/* Enriched selected-vehicle detail panel */
.detail-metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.detail-metrics .dm { background: var(--panel-2); border-radius: var(--r-sm); padding: 8px 6px; text-align: center; }
.dm-l { font-size: 10px; text-transform: uppercase; letter-spacing: .03em; color: var(--text-muted); margin-bottom: 4px; }
.dm-v { font-size: 18px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.dm-u { font-size: 11px; font-weight: 500; color: var(--text-dim); margin-left: 2px; }
.detail-kv .kv:last-child { border-bottom: 0; }
@media (max-width: 820px) { .map-settings { left: 12px; right: 12px; width: auto; top: 64px; } }

/* ---------- Grid helpers ---------- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.section-gap { margin-top: 18px; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

/* ---------- Phone: bottom tab bar replaces the sidebar ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .tb-brand { display: flex; }
  .topbar { padding: 0 14px; gap: 10px; }
  .topbar .page-title { display: none; }
  .hide-xs { display: none; }

  .content { padding: 14px; }
  .content.flush { padding: 0; }

  /* Bottom nav is a normal flex child of .main, so content (and full-bleed maps)
     naturally sit above it — no fixed overlay, no content clipping. */
  .bottom-nav { display: flex; flex: none; background: #ffffff; border-top: 1px solid var(--border);
    height: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px); }
  .bottom-nav .nav-item { flex: 1; flex-direction: column; gap: 3px; margin: 0; padding: 7px 2px;
    border-radius: 0; justify-content: center; position: relative;
    font-size: 10.5px; font-weight: 600; color: var(--text-dim); }
  .bottom-nav .nav-item:hover { background: transparent; color: var(--text-muted); }
  .bottom-nav .nav-item .ni-ic .i { width: 23px; height: 23px; }
  .bottom-nav .nav-item.active { background: transparent; color: var(--primary); }
  .bottom-nav .nav-item.active .i { color: var(--primary); }
  .bottom-nav .nav-item .count { position: absolute; top: 5px; left: 50%; margin: 0;
    transform: translateX(3px); padding: 0 5px; }

  .map-layout { grid-template-columns: 1fr; }
  .map-side { position: absolute; z-index: 700; bottom: 0; left: 0; right: 0; height: 50%;
    border-right: 0; border-top: 1px solid var(--border); border-radius: var(--r-lg) var(--r-lg) 0 0; }
  .detail-panel { width: auto; left: 12px; }
  .stats { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
  .service-grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
}

/* ==========================================================================
   Mobile-native finish — make it feel installed, not like a site in a browser
   ========================================================================== */
* { -webkit-tap-highlight-color: transparent; }
/* Interactive chrome shouldn't be text-selectable or double-tap-zoomable. */
.btn, button, .nav-item, .nav-link, .chip-toggle, .seg, .switch, .service-tile,
.cam-btn, .veh-item, .mine-row, .bottom-nav .nav-item, .status-strip .sc, .stop-row {
  -webkit-user-select: none; user-select: none; touch-action: manipulation;
}
/* Contain scrolling so a pane's overscroll never pulls the whole app or triggers
   pull-to-refresh. */
.content, .veh-list, .nav, .modal-body, .table-wrap, .map-settings, .detail-panel, #t-list {
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
}
/* Honour the notch / home indicator. env() is 0 on non-notched/desktop. */
@supports (padding: max(0px)) {
  .topbar { padding-top: env(safe-area-inset-top, 0px); height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px)); }
}

@media (max-width: 820px) {
  /* 16px inputs stop iOS Safari from zooming the page on focus. */
  .input, select.input, textarea.input, input, select, textarea { font-size: 16px; }
  .content { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  /* Comfortable touch targets. */
  .chip-toggle { padding: 8px 14px; }
  .veh-item { padding: 13px 14px; }
  .mine-row { padding: 15px; }
  .cam-btn { width: 34px; height: 34px; }
}

/* On touch devices, don't let :hover styling stick after a tap. */
@media (hover: none) {
  .service-tile:hover { background: var(--panel); border-color: var(--border-soft); }
  .service-tile:hover .ti { background: var(--panel-2); }
  .nav-item:hover:not(.active), .nav-link:hover:not(.active) { background: transparent; color: var(--text-muted); }
  .nav-item:hover:not(.active) .i, .nav-link:hover:not(.active) .i { color: inherit; }
  .veh-item:hover:not(.sel) { background: transparent; }
  .mine-row:hover { background: transparent; }
  .mine-row:hover .i { color: var(--text-muted); }
  .status-strip .sc:hover:not(.on) { background: var(--panel); }
  .btn.ghost:hover { background: transparent; }
}

/* ==========================================================================
   Mobile design polish (≤ 820px)
   ========================================================================== */
@media (max-width: 820px) {

  /* --- Page headers: stack title + actions vertically --- */
  .page-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .page-head h2 { font-size: 18px; }
  .page-head .grow { display: none; }
  .page-head .row, .page-head .filters { width: 100%; }
  /* Wrap action buttons in page head */
  .page-head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

  /* --- Toolbars: stack filters vertically, full width --- */
  .toolbar { flex-direction: column; align-items: stretch; gap: 10px; }
  .toolbar .input, .toolbar select.input { max-width: none !important; width: 100%; }
  .toolbar .btn { width: 100%; justify-content: center; }
  .toolbar .filters { width: 100%; }

  /* --- Search inputs in page-head: full width --- */
  .page-head .input-icon { width: 100%; }

  /* --- Stats cards: 2-column on phone, compact --- */
  .stats { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat { padding: 12px 14px; }
  .stat .value { font-size: 22px; }
  .stat .ico { width: 28px; height: 28px; border-radius: 7px; }
  .stat .ico .i { width: 15px; height: 15px; }

  /* --- Tables: mobile card view for key tables --- */
  /* Scrollable table hint: subtle gradient on right edge */
  .table-wrap { position: relative; }
  .table-wrap::after {
    content: ""; position: absolute; top: 0; right: 0; bottom: 0; width: 24px;
    background: linear-gradient(to right, transparent, rgba(238,241,246,.85));
    pointer-events: none; opacity: 0; transition: opacity .2s;
  }
  .table-wrap.has-scroll::after { opacity: 1; }

  /* Table cells: tighter padding */
  table.tbl th, table.tbl td { padding: 8px 10px; font-size: 12px; }
  table.tbl th { font-size: 10px; }

  /* --- Map screens (Live Monitor) --- */
  /* Slide-up sheet for vehicle list: starts collapsed as a peek bar */
  .map-side { height: 45%; transition: height .25s ease; }
  .map-side.collapsed { height: 0; min-height: 0; overflow: hidden; border-top: 0; }
  /* Sheet drag handle */
  .map-side::before {
    content: ""; display: block; width: 36px; height: 4px; background: var(--border);
    border-radius: 2px; margin: 8px auto 4px; flex-shrink: 0;
  }
  /* Search and filters in the side panel: tighter */
  .map-side .input-icon { width: 100%; }
  .map-side .filters { overflow-x: auto; flex-wrap: nowrap; gap: 6px; padding-bottom: 4px;
    -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .map-side .filters::-webkit-scrollbar { display: none; }
  .map-side .chip-toggle { flex-shrink: 0; padding: 6px 12px; font-size: 11px; }

  /* Status strip: smaller text for phone */
  .status-strip .sc .n { font-size: 15px; }
  .status-strip .sc .l { font-size: 9px; }
  .status-strip .sc { padding: 7px 4px; }

  /* Detail panel: full width, smaller */
  .detail-panel { left: 8px; right: 8px; top: 8px; max-height: 45%;
    border-radius: var(--r-lg); font-size: 13px; }
  .detail-metrics .dm-v { font-size: 15px; }
  .detail-metrics { grid-template-columns: repeat(3, 1fr); gap: 6px; }

  /* Map tools: smaller buttons */
  .map-tools { top: 8px; right: 8px; }
  .map-tools .btn { padding: 6px; }
  .map-tools .btn .i { width: 16px; height: 16px; }

  /* Map settings panel: full width on mobile */
  .map-settings { left: 8px !important; right: 8px !important; width: auto !important; top: 58px !important; }

  /* --- Vehicle detail page --- */
  /* Compact the button row on vehicle detail */
  .veh-actions { display: flex; gap: 8px; flex-wrap: wrap; }
  .veh-actions .btn { font-size: 12px; padding: 7px 10px; flex: 1 1 auto; min-width: 0; }

  /* KV pairs: slightly tighter */
  .kv { font-size: 12px; padding: 5px 0; }
  .kv .k { font-size: 11px; }

  /* --- Trips page: stack vertically, bigger map --- */
  .toolbar label { flex-wrap: wrap; }

  /* --- Alerts / Events filters --- */
  .toolbar select.input { width: 100% !important; max-width: none !important; }

  /* --- Camera grid: 2-up on phone --- */
  .cam-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }

  /* --- Modals: nearly full screen on phone --- */
  .modal-backdrop { padding: 10px; align-items: flex-end; }
  .modal { max-width: none; width: 100%; max-height: 92vh;
    border-radius: var(--r-lg) var(--r-lg) var(--r-sm) var(--r-sm); }
  .modal-body { padding: 16px; }
  .modal-foot { padding: 12px 16px; }
  .modal-foot .btn { flex: 1; }

  /* --- Toasts: full width, bottom-aligned on phone --- */
  #toasts { top: auto; bottom: calc(var(--bottom-nav-h) + 12px + env(safe-area-inset-bottom, 0px));
    right: 12px; left: 12px; }
  .toast { min-width: 0; max-width: none; width: 100%; }

  /* --- Login: nicer phone layout --- */
  .login { padding: 20px; align-items: flex-start; padding-top: max(60px, 12vh); }
  .login-card { padding: 22px; }
  .login-brand { margin-bottom: 16px; }

  /* --- Donut chart: stack vertically on very small screens --- */
  .donut-wrap { flex-direction: column; align-items: center; }
  .donut-legend { min-width: unset; align-self: stretch; }

  /* --- Board overview: tighter activity rows --- */
  .board-activity-row { padding: 9px 12px !important; }

  /* --- Service hub: 3-column grid --- */
  .service-grid { grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .service-tile { padding: 14px 8px; }
  .service-tile .ti { width: 40px; height: 40px; border-radius: 11px; }
  .service-tile .tl { font-size: 11.5px; }

  /* --- Mine (account): tighten spacing --- */
  .mine { padding: 0; }
  .mine-head { padding: 14px; }
  .mine-avatar { width: 48px; height: 48px; font-size: 18px; }
  .mine-email { font-size: 15px; }
  .mine-group-title { margin: 18px 4px 6px; }
  .mine-signout { margin-top: 18px; }

  /* --- Card head: wrap children --- */
  .card-head { flex-wrap: wrap; gap: 8px; padding: 12px 14px; }
  .card-head h3 { font-size: 13px; }
  .card-pad { padding: 14px; }

  /* --- Badge: slightly smaller on phone --- */
  .badge { font-size: 10px; padding: 2px 7px; }

  /* --- User menu in topbar: compact --- */
  .usermenu .hide-sm { display: none; }
  .usermenu { gap: 6px; }
  .avatar { width: 30px; height: 30px; font-size: 12px; }

  /* --- Topbar connection indicator: icon only --- */
  .conn #conn-text { display: none; }
  .conn .dot { width: 7px; height: 7px; }
}

/* Extra small phones (≤ 420px) */
@media (max-width: 420px) {
  .stats { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat .value { font-size: 20px; }
  .stat .label { font-size: 11px; }
  .service-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .service-tile .tl { font-size: 10.5px; }

  .status-strip { border-radius: var(--r); }
  .status-strip .sc .n { font-size: 13px; }
  .status-strip .sc .l { font-size: 8px; }
  .status-strip .sc { padding: 6px 2px; gap: 2px; }

  .detail-panel { font-size: 12px; }
  .detail-metrics .dm-v { font-size: 13px; }
  .detail-metrics .dm-l { font-size: 9px; }
  .dm-u { font-size: 10px; }

  /* Bottom nav: tighter */
  .bottom-nav .nav-item { font-size: 9.5px; padding: 6px 2px; }
  .bottom-nav .nav-item .ni-ic .i { width: 21px; height: 21px; }

  .page-head h2 { font-size: 16px; }
  .chip-toggle { padding: 6px 11px; font-size: 11px; }
  .btn { font-size: 12px; padding: 7px 11px; }
  .btn.sm { padding: 5px 8px; font-size: 11px; }

  .cam-grid { grid-template-columns: 1fr; }
}

/* Landscape phone: shorter bottom bar, side padding for notch */
@media (max-width: 820px) and (orientation: landscape) {
  :root { --bottom-nav-h: 46px; }
  .bottom-nav .nav-item .ni-ic .i { width: 20px; height: 20px; }
  .bottom-nav .nav-item { font-size: 9px; gap: 1px; }
  .content { padding-top: 10px; padding-bottom: 10px; }
  .content.flush { padding: 0; }
  .map-side { height: 55%; }
  .detail-panel { max-height: 50%; }
  .topbar { height: 44px; }
  .topbar .tb-brand b { font-size: 13px; }
  .topbar .tb-brand .logo { width: 24px; height: 24px; }
  /* Extra notch-safe padding in landscape */
  .bottom-nav { padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px); }
}

/* --- PWA standalone: remove URL bar padding --- */
@media (display-mode: standalone) {
  body { overscroll-behavior: none; }
}

/* --- Trips page map: taller on mobile --- */
@media (max-width: 900px) {
  .grid-2 .card [style*="height:460px"],
  .grid-2 .card [style*="height:280px"] { height: 320px !important; }
}

/* --- Smooth transitions for interactive elements on touch --- */
@media (hover: none) and (pointer: coarse) {
  .btn:active, .chip-toggle:active, .service-tile:active, .veh-item:active,
  .mine-row:active, .nav-item:active { opacity: .7; transition: opacity .08s; }
  .btn:active { transform: scale(.97); }
}
