:root {
  --bg: #0f172a;
  --bg2: #1e293b;
  --bg3: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --green: #22c55e;
  --red: #ef4444;
  --orange: #f59e0b;
  --gray: #64748b;
  --radius: 10px;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

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

html, body { height: 100%; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* Topbar */
.topbar {
  height: 52px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 14px;
  background: var(--bg2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  position: relative;
  z-index: 1002;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand .logo {
  width: 26px; height: 26px; border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), #2563eb);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 13px;
}
.brand h1 { font-size: 17px; font-weight: 700; letter-spacing: 0.3px; }

.stats { display: flex; gap: 16px; margin-left: auto; font-size: 13px; color: var(--muted); }
.stat b { font-size: 15px; color: var(--text); }
.stat-online b { color: var(--green); }
.stat-offline b { color: var(--gray); }
.stat-alarm b { color: var(--orange); }

.actions { display: flex; gap: 8px; }
.icon-btn {
  width: 34px; height: 34px; border: none; border-radius: 8px;
  background: var(--bg3); color: var(--text); font-size: 15px;
  cursor: pointer; position: relative;
}
.icon-btn:hover { background: #475569; }
.add-btn {
  background: var(--green); color: #0f172a; font-size: 19px; font-weight: 700; line-height: 1;
}
.add-btn:hover { background: #2dd36b; }
.badge {
  position: absolute; top: -5px; right: -5px;
  min-width: 16px; height: 16px; padding: 0 4px;
  background: var(--orange); color: #0f172a;
  border-radius: 9px; font-size: 10px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.hidden { display: none !important; }

/* Layout */
#app {
  height: calc(100% - 52px);
  display: flex;
  position: relative;
}

#map { flex: 1; z-index: 1; background: #0b1220; }

/* Sidebar */
#sidebar {
  width: 320px;
  min-width: 240px;
  background: var(--bg2);
  border-right: 1px solid rgba(255, 255, 255, 0.06);
  display: flex;
  flex-direction: column;
  z-index: 2;
}
.search-row { padding: 12px; border-bottom: 1px solid rgba(255, 255, 255, 0.06); }
#search {
  width: 100%; padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--bg3); background: var(--bg3); color: var(--text);
  font-size: 13px; outline: none;
}
#search:focus { border-color: var(--accent); }

.filters { display: flex; gap: 6px; padding: 10px 12px; border-bottom: 1px solid rgba(255,255,255,0.06); }
.chip {
  flex: 1; padding: 6px 0; border: none; border-radius: 7px;
  background: var(--bg3); color: var(--muted); font-size: 12px; cursor: pointer;
}
.chip.active { background: var(--accent); color: #0f172a; font-weight: 600; }
.chip-alarm.active { background: var(--orange); }

#device-list { flex: 1; overflow-y: auto; list-style: none; }
#device-list li {
  display: flex; flex-direction: column; gap: 2px;
  padding: 10px 12px; border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  cursor: pointer; position: relative;
}
#device-list li:hover { background: var(--bg3); }
#device-list li.selected { background: rgba(56, 189, 248, 0.12); border-left: 3px solid var(--accent); }
.dev-row { display: flex; align-items: center; gap: 8px; }
.dev-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.dot-online { background: var(--green); box-shadow: 0 0 6px var(--green); }
.dot-offline { background: var(--gray); }
.dot-alarm { background: var(--orange); box-shadow: 0 0 6px var(--orange); }
.dot-nogps { background: var(--red); }
.dev-name { font-weight: 600; font-size: 13px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev-plate { font-size: 11px; color: var(--muted); }
.dev-meta { font-size: 11px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; padding-left: 18px; }
.dev-meta .spd { color: var(--text); font-weight: 600; }
.dev-meta .alarm-tag { color: var(--orange); font-weight: 600; }
.empty { padding: 24px; text-align: center; color: var(--muted); font-size: 13px; }

/* Panels (alerts / detail) */
.panel {
  position: absolute; top: 10px; right: 10px; bottom: 10px;
  width: 340px; max-width: 90vw;
  background: var(--bg2); border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius); box-shadow: var(--shadow);
  z-index: 1001; display: flex; flex-direction: column; overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid rgba(255,255,255,0.06);
}
.panel-head h2 { font-size: 15px; }

#alerts-list { flex: 1; overflow-y: auto; list-style: none; }
#alerts-list li {
  padding: 10px 14px; border-bottom: 1px solid rgba(255,255,255,0.05);
  font-size: 13px;
}
.alert-title { font-weight: 600; }
.alert-title.alarm-type { color: var(--orange); }
.alert-imei { color: var(--muted); font-size: 11px; }
.alert-time { color: var(--muted); font-size: 11px; margin-top: 2px; }

#detail-body { flex: 1; overflow-y: auto; padding: 14px; font-size: 13px; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.detail-item {
  background: var(--bg3); border-radius: 8px; padding: 8px 10px;
  display: flex; flex-direction: column; gap: 2px;
}
.detail-item .k { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; }
.detail-item .v { font-size: 14px; font-weight: 600; }
.detail-item .v.ok { color: var(--green); }
.detail-item .v.bad { color: var(--red); }
.detail-addr { margin-top: 10px; font-size: 12px; color: var(--muted); }
.detail-addr b { color: var(--text); }

#playback {
  border-top: 1px solid rgba(255,255,255,0.08);
  padding: 12px 14px;
}
.pb-controls { display: flex; align-items: center; gap: 10px; }
.pb-btn {
  width: 34px; height: 34px; border: none; border-radius: 50%;
  background: var(--accent); color: #0f172a; font-size: 14px; cursor: pointer;
}
#pb-slider { flex: 1; accent-color: var(--accent); }
.pb-time { font-size: 11px; color: var(--muted); min-width: 70px; text-align: right; }

/* Map markers */
.vmarker {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--c, #64748b);
  border: 2.5px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.4s ease;
}
.vmarker .arrow { font-size: 13px; color: #fff; line-height: 1; }
.vmarker.alarm { animation: pulse 1.2s infinite; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7); }
  70% { box-shadow: 0 0 0 12px rgba(245, 158, 11, 0); }
  100% { box-shadow: 0 0 0 0 rgba(245, 158, 11, 0); }
}

.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--bg2); color: var(--text); }
.leaflet-popup-content { margin: 10px 12px; font-size: 12px; }
.pop-title { font-weight: 700; font-size: 14px; margin-bottom: 4px; }
.pop-row { display: flex; justify-content: space-between; gap: 14px; }
.pop-row .m { color: var(--muted); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--bg3); color: var(--text);
  padding: 10px 18px; border-radius: 8px; box-shadow: var(--shadow);
  font-size: 13px; z-index: 2000; border-left: 4px solid var(--accent);
}

/* Modal: adicionar dispositivo */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(2, 6, 23, 0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 2001; padding: 16px;
}
.modal {
  width: 380px; max-width: 100%;
  background: var(--bg2); border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-hint { padding: 10px 14px 0; font-size: 12px; color: var(--muted); }
#add-form { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; }
.field input {
  padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--bg3); background: var(--bg3); color: var(--text);
  font-size: 13px; outline: none;
}
.field input:focus { border-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.btn {
  padding: 9px 16px; border: none; border-radius: 8px;
  font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn.ghost { background: var(--bg3); color: var(--text); }
.btn.ghost:hover { background: #475569; }
.btn.primary { background: var(--accent); color: #0f172a; }
.btn.primary:hover { background: #67cdfa; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

/* Leaflet controls dark-ish */
.leaflet-control-zoom a {
  background: var(--bg2) !important; color: var(--text) !important;
  border-color: var(--bg3) !important;
}
.leaflet-control-layers, .leaflet-control-layers-expanded {
  background: var(--bg2) !important; color: var(--text) !important;
}

/* Responsive */
@media (max-width: 720px) {
  #sidebar { width: 260px; }
  .stats { gap: 8px; font-size: 11px; }
  .stat b { font-size: 13px; }
  .panel { width: 92vw; }
  .topbar { gap: 8px; }
  .brand h1 { font-size: 15px; }
}
