:root {
  --bg: #0f1419;
  --panel: #1a2029;
  --panel-2: #222b36;
  --line: #2c3745;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #3b82f6;
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #22c55e;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  height: 56px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; cursor: pointer; white-space: nowrap; }
.logo { font-size: 20px; }
.nav { display: flex; gap: 6px; }
.nav-btn {
  background: transparent; border: 1px solid transparent; color: var(--muted);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.nav-btn.active { background: var(--panel-2); color: var(--text); border-color: var(--line); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.icon-btn {
  position: relative; background: transparent; border: 1px solid var(--line);
  color: var(--text); width: 38px; height: 38px; border-radius: 9px; cursor: pointer; font-size: 17px;
}
.icon-btn.on { border-color: var(--accent); }
.bell-badge {
  position: absolute; top: -6px; right: -6px; background: var(--danger); color: #fff;
  font-size: 11px; min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px;
  display: flex; align-items: center; justify-content: center;
}

/* ---------- Buttons ---------- */
.btn { background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 8px 13px; font-size: 14px; cursor: pointer; }
.btn:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; border: 1px solid var(--line); color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-sm { padding: 6px 10px; font-size: 13px; }

/* ---------- Views ---------- */
.view { height: calc(100vh - 56px); overflow-y: auto; }
.view[hidden] { display: none; }

/* ---------- Dashboard ---------- */
.dash-head { display: flex; align-items: center; justify-content: space-between; padding: 20px 22px 8px; }
.dash-head h2 { margin: 0; font-size: 20px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; padding: 14px 22px 28px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; cursor: pointer; transition: border-color .15s, transform .05s;
  border-left: 4px solid var(--accent);
}
.card:hover { border-color: var(--accent); }
.card:active { transform: scale(0.995); }
.card-top { display: flex; align-items: center; gap: 10px; }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex: 0 0 auto; }
.card-name { font-weight: 600; font-size: 16px; }
.card-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.card-row { display: flex; align-items: center; gap: 14px; margin-top: 12px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.card-badge { margin-left: auto; background: var(--danger); color: #fff; border-radius: 20px; font-size: 12px; padding: 2px 9px; }
.card-addr { margin-top: 10px; font-size: 12px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; }

.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; }
.dot.online { background: var(--ok); }
.dot.offline { background: var(--muted); }

/* ---------- Detail ---------- */
.detail { display: flex; flex-direction: column; padding: 0; }
.detail-topbar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.child-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); flex: 0 0 auto; }
.status { color: var(--muted); font-size: 13px; }
.detail-body { flex: 1; display: grid; grid-template-columns: 1fr 380px; min-height: 0; }

.map-wrap { position: relative; }
#map { height: 100%; width: 100%; background: #0b0f14; }
.map-hint {
  position: absolute; bottom: 12px; left: 12px; right: 12px; z-index: 500;
  background: rgba(17, 23, 31, 0.92); border: 1px solid var(--line); color: var(--muted);
  font-size: 12px; padding: 8px 10px; border-radius: 8px; display: none;
}
.map-hint.show { display: block; }
.map-controls { position: absolute; top: 12px; right: 12px; z-index: 500; display: flex; flex-direction: column; gap: 6px; }
.map-ctrl { width: 36px; height: 36px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--text); cursor: pointer; font-size: 15px; }

/* ---------- Side + tabs ---------- */
.side { border-left: 1px solid var(--line); background: var(--bg); overflow-y: auto; display: flex; flex-direction: column; }
.tabs { display: flex; gap: 2px; padding: 8px 8px 0; position: sticky; top: 0; background: var(--bg); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tab { position: relative; background: transparent; border: none; color: var(--muted); padding: 10px 11px; cursor: pointer; font-size: 13px; white-space: nowrap; border-bottom: 2px solid transparent; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-badge { background: var(--danger); color: #fff; border-radius: 20px; font-size: 10px; padding: 1px 6px; margin-left: 5px; }
.tabpane { padding: 14px; display: flex; flex-direction: column; gap: 14px; }
.tabpane[hidden] { display: none; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.panel h3 { margin: 0 0 10px; font-size: 14px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.panel-head h3 { margin: 0; }
.danger-panel { border-color: rgba(239,68,68,.35); }

.muted { color: var(--muted); }
.small { font-size: 12px; }

.enroll-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.enroll-row code { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; letter-spacing: 1px; font-size: 14px; }

/* ---------- Fields ---------- */
select, input {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; font-size: 14px; outline: none; font-family: inherit;
}
select:focus, input:focus { border-color: var(--accent); }
.fld { display: block; font-size: 13px; color: var(--muted); margin-bottom: 11px; }
.fld input { width: 100%; margin-top: 5px; }
input[type="color"] { padding: 2px; height: 38px; width: 60px; }
.field-row { display: flex; gap: 10px; margin-bottom: 10px; }
.field-row label { flex: 1; font-size: 12px; color: var(--muted); }
.field-row input { width: 100%; margin-top: 4px; }
.btn-row { display: flex; gap: 8px; margin-bottom: 8px; }
.chk { display: flex; align-items: center; gap: 8px; font-size: 13px; margin: 8px 0; cursor: pointer; }
.chk input { width: auto; }

/* ---------- Filters / chips ---------- */
.filter-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); border-radius: 20px; padding: 4px 11px; font-size: 12px; cursor: pointer; }
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---------- Lists ---------- */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.list li { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; font-size: 13px; }
.list li.sos { border-color: var(--danger); background: rgba(239, 68, 68, 0.12); }
.list li.warn { border-color: var(--warn); }
.list li.unread { box-shadow: inset 3px 0 0 var(--accent); }
.list .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.list .ts { color: var(--muted); font-size: 11px; }
.list .acts { display: flex; gap: 8px; align-items: center; }
.link { color: var(--accent); cursor: pointer; font-size: 12px; }
.link.danger { color: var(--danger); }

.media { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.media-img { max-width: 120px; max-height: 120px; border-radius: 8px; border: 1px solid var(--line); cursor: pointer; display: block; }
.media audio { width: 100%; height: 34px; }

.fence-toggles { display: flex; gap: 12px; margin-top: 6px; }
.fence-toggles label { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); cursor: pointer; }
.fence-toggles input { width: auto; }

/* ---------- Auth ---------- */
.auth { position: fixed; inset: 0; background: var(--bg); display: flex; align-items: center; justify-content: center; z-index: 2000; }
.auth[hidden] { display: none; }
.auth-card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 32px; width: 340px; max-width: 92vw; text-align: center; }
.auth-logo { font-size: 20px; font-weight: 700; margin-bottom: 18px; }
.auth-card h3 { margin: 0 0 6px; }
.auth-card input { width: 100%; margin-top: 12px; }
.auth-card .btn { width: 100%; margin-top: 16px; }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: 360px; max-width: 100%; }
.modal-card h3 { margin: 0 0 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }

/* ---------- Lightbox ---------- */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: flex; align-items: center; justify-content: center; z-index: 3000; cursor: zoom-out; padding: 20px; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 95vw; max-height: 92vh; border-radius: 8px; }

/* ---------- Toast ---------- */
.toast-wrap { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 4000; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: var(--panel-2); border: 1px solid var(--line); color: var(--text); padding: 10px 16px; border-radius: 10px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.4); }
.toast.sos { border-color: var(--danger); background: rgba(239,68,68,.18); }

/* ---------- Responsive ---------- */
@media (max-width: 820px) {
  body { overflow: auto; }
  .nav-btn { padding: 7px 10px; }
  .detail-body { grid-template-columns: 1fr; }
  .map-wrap { height: 44vh; }
  .side { border-left: none; border-top: 1px solid var(--line); }
  .view { height: auto; overflow: visible; }
  .detail { height: auto; }
}
