:root {
  --bg: #0b1220;
  --panel: #111827;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --online: #22c55e;
  --offline: #f59e0b;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: system-ui, sans-serif; background: var(--bg); color: var(--text); }
#login {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border-radius: 16px;
  padding: 24px;
}
.card h1 { margin: 0 0 8px; font-size: 1.4rem; }
.card p { margin: 0 0 16px; color: var(--muted); font-size: 0.9rem; }
label { display: block; font-size: 0.8rem; color: var(--muted); margin: 10px 0 4px; }
input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #334155;
  background: #0b1220;
  color: var(--text);
}
button {
  width: 100%;
  margin-top: 16px;
  padding: 12px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #0b1220;
  font-weight: 700;
}
.err { color: #f87171; min-height: 1.2em; font-size: 0.85rem; margin-top: 8px; }
.card a { color: var(--accent); }
#app { display: none; height: 100%; position: relative; }
#map { height: 100%; width: 100%; }
body.ui-desk #app.desk-app {
  height: 100%;
}
body.ui-desk #app.desk-app.is-open {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
}
.desk-side {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #0f172a;
  border-right: 1px solid #1e293b;
  z-index: 1002;
}
.desk-brand {
  padding: 14px 14px 6px;
  font-size: 0.95rem;
  font-weight: 800;
  letter-spacing: 0.02em;
}
.desk-brand span {
  display: block;
  margin-top: 2px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.01em;
}
body.ui-desk .desk-side .topbar {
  position: static;
  max-width: none;
  flex-wrap: wrap;
  padding: 8px 12px 10px;
  pointer-events: auto;
}
body.ui-desk .desk-side .topbar input {
  flex: 1 1 100%;
  width: 100%;
  max-width: none;
  height: 36px;
  font-size: 0.88rem;
}
body.ui-desk .desk-side .topbar .chip,
body.ui-desk .desk-side .topbar button {
  height: 34px;
}
.unit-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0 8px 8px;
}
.unit-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin: 0 0 4px;
  padding: 8px 8px;
  border: 1px solid #1e293b;
  border-radius: 8px;
  background: #111827;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-weight: 600;
}
.unit-row:hover { border-color: #38bdf8; }
.unit-row.is-on { border-color: #fbbf24; background: #1e293b; }
.unit-row .dot {
  width: 10px;
  height: 10px;
  border-radius: 99px;
  flex: 0 0 auto;
}
.unit-row.st-blue .dot { background: #2563eb; }
.unit-row.st-violet .dot { background: #7c3aed; }
.unit-row.st-red .dot { background: #dc2626; }
.unit-row.st-black .dot { background: #64748b; }
.unit-row.st-panic .dot { background: #ef4444; box-shadow: 0 0 0 3px rgba(239,68,68,.35); }
.unit-row .und { font-weight: 800; min-width: 2.2em; }
.unit-row .plate { flex: 1; color: var(--muted); font-size: 0.78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.unit-row .spd { font-size: 0.72rem; color: #94a3b8; }
.desk-out {
  width: calc(100% - 24px);
  margin: 8px 12px 14px;
  height: 40px;
  cursor: pointer;
}
.evt-panel {
  width: min(420px, calc(100% - 16px));
}
.evt-list {
  margin-top: 8px;
  max-height: min(62vh, 520px);
  overflow-y: auto;
}
.evt-row {
  border: 1px solid #1e293b;
  border-radius: 8px;
  padding: 8px 9px;
  margin-bottom: 6px;
  background: #0b1220;
}
.evt-row .evt-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.78rem;
  font-weight: 700;
}
.evt-row .evt-time { color: var(--muted); font-weight: 600; font-size: 0.72rem; white-space: nowrap; }
.evt-row .evt-label { margin-top: 4px; font-size: 0.82rem; font-weight: 800; }
.evt-row .evt-sub { margin-top: 2px; font-size: 0.72rem; color: var(--muted); }
.evt-row.kind-panic { border-color: #dc2626; }
.evt-row.kind-panic .evt-label { color: #fca5a5; }
.evt-row.kind-geo { border-color: #38bdf8; }
.evt-row.kind-geo .evt-label { color: #7dd3fc; }
.evt-row.kind-ign .evt-label { color: #c4b5fd; }
.desk-map { position: relative; min-width: 0; height: 100%; }
.layout-switch {
  display: block;
  margin: 8px 12px 12px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  text-decoration: none;
}
.login-switch { margin-top: 14px; text-align: center; }
body.ui-phone .topbar input { height: 40px; font-size: 1rem; flex: 1 1 140px; max-width: 200px; }
body.ui-phone .topbar button,
body.ui-phone .chip { height: 40px; font-size: 0.85rem; padding: 0 12px; }
body.ui-phone .hist-actions button,
body.ui-phone .hist-replay-btn { min-height: 40px; }
.topbar {
  position: absolute;
  z-index: 1000;
  left: 8px;
  top: max(8px, env(safe-area-inset-top, 0px));
  display: flex;
  gap: 6px;
  align-items: center;
  pointer-events: none;
  max-width: calc(100% - 16px);
}
.topbar input,
.topbar button,
.topbar .chip {
  pointer-events: auto;
  height: 32px;
  line-height: 1;
}
.topbar input, .topbar button, .chip {
  width: auto;
  margin: 0;
}
.topbar input {
  flex: 0 1 120px;
  width: 120px;
  min-width: 72px;
  max-width: 160px;
  padding: 0 10px;
  font-size: 0.82rem;
  border-radius: 8px;
  border: 1px solid #334155;
  background: #111827e6;
  color: var(--text);
}
.chip, .topbar button {
  padding: 0 10px;
  border-radius: 8px;
  background: #111827e6;
  color: var(--text);
  border: 1px solid #334155;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
}
.topbar button {
  cursor: pointer;
  background: #1e293b;
}
.topbar button.tb-btn-active {
  background: #0ea5e9;
  color: #0b1220;
  border-color: #38bdf8;
}
.hist-panel {
  position: absolute;
  z-index: 1001;
  left: 8px;
  top: 48px;
  width: min(280px, calc(100% - 16px));
  max-height: calc(100% - 56px);
  overflow-y: auto;
  background: #111827f0;
  border: 1px solid #334155;
  border-radius: 10px;
  padding: 10px 12px;
  pointer-events: auto;
  backdrop-filter: blur(6px);
}
.hist-panel.hidden { display: none; }
.hist-head {
  font-size: 0.85rem;
  font-weight: 700;
  margin-bottom: 8px;
}
.hist-lbl {
  font-size: 0.72rem;
  color: var(--muted);
  margin: 6px 0 3px;
}
.hist-panel select,
.hist-panel input[type="datetime-local"] {
  width: 100%;
  padding: 6px 8px;
  font-size: 0.8rem;
  border-radius: 8px;
  border: 1px solid #334155;
  background: #0b1220;
  color: var(--text);
  margin: 0;
}
.hist-custom.hidden { display: none; }
.hist-actions {
  display: flex;
  gap: 6px;
  margin-top: 10px;
}
.hist-actions button {
  flex: 1;
  width: auto;
  margin: 0;
  padding: 8px 6px;
  font-size: 0.78rem;
  border-radius: 8px;
  border: 0;
  cursor: pointer;
  font-weight: 700;
}
.hist-primary { background: var(--accent); color: #0b1220; }
.hist-secondary { background: #334155; color: var(--text); }
.hist-msg {
  margin-top: 8px;
  font-size: 0.72rem;
  color: var(--muted);
  min-height: 2.4em;
  line-height: 1.35;
}
.hist-msg.err { color: #f87171; }
.hist-start, .hist-end {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 800;
  color: #fff;
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.4);
}
.hist-start { background: #16a34a; }
.hist-end { background: #dc2626; }
.hist-replay.hidden { display: none; }
.hist-replay { margin-top: 10px; padding-top: 8px; border-top: 1px solid #334155; }
.hist-replay-row {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}
.hist-replay-btn {
  width: 36px;
  height: 32px;
  margin: 0;
  padding: 0;
  border: 1px solid #334155;
  border-radius: 8px;
  background: #1e293b;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 700;
}
.hist-replay-btn:hover { background: #334155; }
.hist-speed {
  flex: 1;
  height: 32px;
  padding: 0 8px;
  font-size: 0.78rem;
  border-radius: 8px;
  border: 1px solid #334155;
  background: #0b1220;
  color: var(--text);
  margin: 0;
}
#histSeek {
  width: 100%;
  margin: 4px 0 0;
  accent-color: var(--accent);
}
.hist-play-time {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 4px;
  line-height: 1.35;
}
.hist-mode-badge {
  position: absolute;
  z-index: 1001;
  left: 50%;
  transform: translateX(-50%);
  top: max(8px, env(safe-area-inset-top, 0px));
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 999px;
  background: #7c3aede6;
  border: 1px solid #a78bfa;
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  pointer-events: auto;
}
.hist-mode-badge.hidden { display: none; }
.hist-mode-badge button {
  margin: 0;
  width: auto;
  padding: 4px 10px;
  font-size: 0.72rem;
  border-radius: 999px;
  border: 0;
  background: #fff;
  color: #5b21b6;
  cursor: pointer;
  font-weight: 800;
}
.hist-play-marker {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  min-height: 34px;
  padding: 0 7px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  color: #fff;
  background: #2563eb;
  border: 3px solid #fef08a;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.5), 0 2px 8px rgba(0,0,0,.45);
}
.popup b { font-size: 1rem; }
.popup { min-width: 180px; }
.popup-und {
  font-size: 1.15rem;
  font-weight: 800;
  margin-bottom: 8px;
  color: #0f172a;
}
.popup-row {
  font-size: 0.88rem;
  color: #334155;
  margin: 4px 0;
}
.popup-row .k {
  display: inline-block;
  min-width: 88px;
  color: #64748b;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.popup-time {
  margin-top: 8px;
  font-size: 0.75rem;
  color: #94a3b8;
}
.status-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  color: #fff;
}
.status-pill.st-blue { background: #2563eb; }
.status-pill.st-violet { background: #7c3aed; }
.status-pill.st-red { background: #dc2626; }
.status-pill.st-black { background: #111827; }
.leaflet-container { background: #0b1220; }
.unit-pin {
  background: transparent;
  border: 0;
}
.unit-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  min-height: 32px;
  padding: 0 7px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  color: #fff;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.45);
  white-space: nowrap;
  cursor: pointer;
}
.unit-label.is-selected {
  border-color: #fbbf24;
  box-shadow: 0 0 0 3px rgba(251, 191, 36, 0.55), 0 1px 4px rgba(0,0,0,.45);
}
.unit-label.st-panic {
  background: #b91c1c !important;
  border-color: #fef08a;
  animation: panic-pulse 0.85s ease-in-out infinite;
  flex-direction: column;
  gap: 1px;
  min-width: 40px;
  min-height: 40px;
  padding: 2px 6px;
  font-size: 13px;
}
.panic-badge {
  display: block;
  font-size: 8px;
  font-weight: 900;
  letter-spacing: 0.06em;
  line-height: 1;
  color: #fef08a;
}
.unit-pin.panic-marker {
  z-index: 2000 !important;
}
@keyframes panic-pulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.85), 0 0 14px rgba(239, 68, 68, 0.55);
    transform: scale(1);
  }
  50% {
    box-shadow: 0 0 0 12px rgba(239, 68, 68, 0), 0 0 22px rgba(239, 68, 68, 0.85);
    transform: scale(1.1);
  }
}
.popup-panic {
  background: #dc2626;
  color: #fff;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  padding: 6px 8px;
  margin: -12px -12px 8px;
  border-radius: 8px 8px 0 0;
  text-transform: uppercase;
}
.status-pill.st-panic { background: #dc2626; }
.leaflet-control-zoom {
  border: 0 !important;
  box-shadow: 0 1px 6px rgba(0,0,0,.35) !important;
}
.leaflet-bottom.leaflet-right .leaflet-control {
  margin-bottom: max(16px, env(safe-area-inset-bottom, 0px));
  margin-right: max(12px, env(safe-area-inset-right, 0px));
}
.unit-label.st-blue { background: #2563eb; }
.unit-label.st-violet { background: #7c3aed; }
.unit-label.st-red { background: #dc2626; }
.unit-label.st-black { background: #111827; }
