:root {
  --bg: #f7f8fb;
  --panel: #ffffff;
  --panel-hi: #f3f4f7;
  --border: #e5e7eb;
  --text: #111827;
  --text-dim: #4b5563;
  --muted: #9ca3af;
  --primary: #4f46e5;
  --primary-hi: #4338ca;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --shadow-sm: 0 1px 2px rgba(17,24,39,0.04), 0 1px 3px rgba(17,24,39,0.06);
  --shadow-md: 0 4px 6px rgba(17,24,39,0.05), 0 10px 15px -3px rgba(17,24,39,0.07);
  --shadow-lg: 0 10px 15px -3px rgba(17,24,39,0.08), 0 20px 25px -5px rgba(17,24,39,0.1);
  --radius: 10px;
  --radius-sm: 6px;
}

@supports (font-variation-settings: normal) {
  :root { font-family: 'Inter var', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
}
:root:not(:has(*)) { font-family: 'Inter', -apple-system, sans-serif; }

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
/* [hidden] must beat later class rules that set display:flex/grid. */
[hidden] { display: none !important; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input { font: inherit; }
input {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  background: var(--panel);
  outline: none;
  transition: border-color 0.1s, box-shadow 0.1s;
}
input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,0.12);
}

/* -------- layout -------- */
.shell {
  display: grid;
  grid-template-columns: 320px 1fr;
  grid-template-rows: 56px 1fr;
  grid-template-areas: "top top" "side main";
  height: 100%;
  min-height: 0;
}
.topbar {
  grid-area: top;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
  z-index: 10;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand-logo {
  width: 28px;
  height: 28px;
  color: var(--primary);
  flex-shrink: 0;
}
.brand-name {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.1px;
  line-height: 1.2;
}
.brand-tag {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.2px;
  margin-top: 1px;
}
.conn {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--text-dim);
  padding: 6px 12px;
  background: var(--panel-hi);
  border-radius: 999px;
}
.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  transition: background 0.2s;
}
.conn.ok .conn-dot {
  background: var(--success);
  box-shadow: 0 0 6px rgba(16,185,129,0.5);
}
.conn.err .conn-dot { background: var(--danger); }

.sidebar {
  grid-area: side;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.count {
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.2px;
  text-transform: uppercase;
  font-weight: 500;
}
.count span { color: var(--text); font-weight: 600; font-size: 13px; text-transform: none; }
.device-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

/* device row */
.dev-item {
  padding: 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.1s, box-shadow 0.1s;
  margin-bottom: 4px;
  border: 1px solid transparent;
}
.dev-item:hover { background: var(--panel-hi); }
.dev-item.active {
  background: #eef2ff;
  border-color: rgba(79,70,229,0.2);
}
.dev-head {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 10px;
}
.dev-name {
  font-weight: 600;
  font-size: 13.5px;
  color: var(--text);
  line-height: 1.3;
  word-break: break-word;
}
.dev-id {
  font-size: 11px;
  color: var(--muted);
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  margin-top: 2px;
}
.dev-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  font-size: 11.5px;
  color: var(--text-dim);
}
.dev-meta b { color: var(--text); font-weight: 600; }

.chip {
  font-size: 10.5px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  white-space: nowrap;
  flex-shrink: 0;
}
.chip.moving  { background: rgba(16,185,129,0.12); color: #047857; }
.chip.idle    { background: rgba(245,158,11,0.12); color: #b45309; }
.chip.no_gps  { background: rgba(139,92,246,0.12); color: #6d28d9; }
.chip.offline { background: rgba(156,163,175,0.15); color: #4b5563; }

/* -------- map -------- */
.map-wrap {
  grid-area: main;
  position: relative;
  min-width: 0;
  background: #e7eaf1;
}
#map { position: absolute; inset: 0; }
.leaflet-container {
  font-family: inherit;
  background: #e7eaf1;
}
.leaflet-control-attribution {
  background: rgba(255,255,255,0.85) !important;
  font-size: 10.5px !important;
}

/* custom marker */
.nc-marker {
  width: 34px;
  height: 34px;
  transform: translate(-50%, -50%);
  position: relative;
}
.nc-marker .ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--primary);
  opacity: 0.18;
  animation: pulse 2s ease-out infinite;
}
.nc-marker .pin {
  position: absolute;
  inset: 8px;
  background: var(--primary);
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(17,24,39,0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 12px;
  line-height: 1;
}
.nc-marker.moving  .pin { background: var(--success); }
.nc-marker.idle    .pin { background: var(--warning); }
.nc-marker.offline .pin { background: var(--muted); }
.nc-marker.offline .ring, .nc-marker.stale .ring { display: none; }
.nc-marker.stale .pin { background: var(--muted); opacity: 0.85; }
.nc-marker .tag {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translate(-50%, 4px);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: var(--shadow-sm);
  color: var(--text);
}
@keyframes pulse {
  0%   { transform: scale(0.65); opacity: 0.45; }
  100% { transform: scale(2.2);  opacity: 0;    }
}

/* -------- floating device card -------- */
.device-card {
  position: absolute;
  top: 16px;
  right: 16px;
  width: min(360px, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 18px;
  z-index: 500;
  animation: card-in 0.18s ease-out;
}
@keyframes card-in {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.card-close {
  position: absolute;
  top: 10px;
  right: 10px;
}
.card-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding-right: 28px;
}
.card-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: #eef2ff;
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.card-icon svg { width: 22px; height: 22px; }
.card-id { flex: 1; min-width: 0; }
.card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.2;
  word-break: break-word;
}
.card-sub {
  font-size: 11.5px;
  color: var(--muted);
  margin-top: 3px;
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
}
.card-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 16px;
}
.stat {
  padding: 10px;
  background: var(--panel-hi);
  border-radius: var(--radius-sm);
  text-align: center;
}
.stat-label {
  display: block;
  font-size: 10.5px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  font-weight: 500;
}
.stat-val {
  display: block;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  margin-top: 3px;
  font-variant-numeric: tabular-nums;
}
.card-actions {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-top: 16px;
}

/* -------- buttons -------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 9px 14px;
  border: 0;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.1s, transform 0.05s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hi); }
.btn-ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--panel-hi); }
.icon-btn {
  background: transparent;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  border-radius: 6px;
  padding: 0;
}
.icon-btn:hover { background: var(--panel-hi); color: var(--text); }

/* -------- modal -------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15,23,42,0.6);
  backdrop-filter: blur(4px);
  animation: fade 0.15s ease-out;
}
.modal-body {
  position: relative;
  background: #0f172a;
  border-radius: 14px;
  width: min(860px, 100%);
  max-height: calc(100vh - 40px);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  animation: modal-in 0.18s ease-out;
  color: #fff;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-in {
  from { opacity: 0; transform: scale(0.96); }
  to   { opacity: 1; transform: scale(1); }
}
.modal-head {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}
.modal-title {
  font-weight: 600;
  font-size: 14px;
  color: #fff;
}
.modal-sub {
  font-size: 11.5px;
  color: #94a3b8;
  margin-top: 2px;
}
.modal-tabs {
  margin-left: auto;
  display: flex;
  gap: 4px;
  padding: 4px;
  background: rgba(255,255,255,0.06);
  border-radius: 8px;
}
.tab {
  background: transparent;
  border: 0;
  color: #cbd5e1;
  font-size: 12px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.1s;
}
.tab:hover { color: #fff; }
.tab.active { background: var(--primary); color: #fff; }
.modal-head .icon-btn { color: #cbd5e1; }
.modal-head .icon-btn:hover { background: rgba(255,255,255,0.1); color: #fff; }
.modal-video {
  position: relative;
  background: #000;
  aspect-ratio: 16 / 9;
  width: 100%;
}
#video-el {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}
.video-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: #e2e8f0;
  font-size: 13px;
  background: rgba(15,23,42,0.5);
  pointer-events: none;
}
.video-overlay.hidden { display: none; }
.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* -------- toast -------- */
.toast-root {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
}
.toast {
  background: #111827;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow-md);
  animation: toast-in 0.15s;
  max-width: 380px;
}
.toast.err { background: var(--danger); }
.toast.ok  { background: var(--success); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* -------- empty -------- */
.empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* -------- mobile -------- */
.mobile-only { display: none; }
@media (max-width: 768px) {
  .shell {
    grid-template-columns: 1fr;
    grid-template-areas: "top" "main";
  }
  .sidebar {
    /* Explicit override — on mobile the sidebar is a fixed-position drawer,
       not a grid cell. `grid-area: auto` frees it from the grid layout. */
    grid-area: auto;
    position: fixed;
    top: 56px;
    bottom: 0;
    left: 0;
    width: min(320px, 88vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 50;
    box-shadow: var(--shadow-lg);
    /* Fallback for iOS — ensure the drawer is actually rendered. */
    visibility: visible;
    will-change: transform;
  }
  #sidebar.open { transform: translateX(0) !important; }
  .mobile-only { display: inline-flex !important; }
  #sidebar-toggle {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    cursor: pointer;
    z-index: 60;
    min-width: 36px;
    min-height: 36px;
  }
  .device-card {
    top: auto;
    /* Leave headroom above Leaflet zoom controls (bottom-right) so they
       don't overlap the card's actions. */
    bottom: 80px;
    right: 12px;
    left: 12px;
    width: auto;
  }
  .topbar { padding: 0 12px; gap: 10px; }
  .brand-tag { display: none; }
  .leaflet-control-zoom { display: none !important; }
  /* Backdrop when the sidebar drawer is open. */
  #sidebar.open::after {
    content: '';
    position: fixed;
    top: 56px;
    left: min(320px, 88vw);
    right: 0;
    bottom: 0;
    background: rgba(17,24,39,0.45);
    z-index: -1;
  }
}

/* hide Leaflet's default attribution icon but keep text */
.leaflet-control-attribution { padding: 2px 6px; }
