:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e6e9ef;
  --muted: #9aa3b2;
  --accent: #4f86f7;
  --accent-2: #6ea8fe;
  --green: #34c759;
  --orange: #ff9f0a;
  --red: #ff453a;
  --radius: 10px;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb; --panel: #ffffff; --panel-2: #f0f3f9; --border: #e2e6ee;
    --text: #1a1d24; --muted: #5b6472;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 5px; }

/* Login */
.login {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1b2230, var(--bg));
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card label, .card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input, .card input, .card select, .toolbar input, .toolbar select, .topbar select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 9px 11px; font: inherit;
}
button {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px;
  padding: 9px 14px; font: inherit; font-weight: 600; cursor: pointer;
}
button:hover { background: var(--accent-2); }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); font-weight: 500; }
button.link { background: none; color: var(--accent); padding: 4px; font-weight: 500; }
button.danger-btn { background: var(--red); }

/* Shell */
.app { display: grid; grid-template-columns: 220px 1fr; height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 16px 12px; }
.brand { font-weight: 700; font-size: 16px; display: flex; gap: 8px; align-items: center; padding: 6px 10px 18px; }
#nav { display: flex; flex-direction: column; gap: 3px; }
#nav a {
  display: flex; align-items: center; gap: 8px; padding: 9px 12px; border-radius: 8px;
  color: var(--text); text-decoration: none; font-weight: 500;
}
#nav a:hover { background: var(--panel-2); }
#nav a.active { background: var(--accent); color: #fff; }
.badge { margin-left: auto; background: var(--red); color: #fff; border-radius: 999px; padding: 0 7px; font-size: 11px; line-height: 18px; }
.sidebar-foot { margin-top: auto; padding: 10px; display: flex; flex-direction: column; gap: 6px; }

.content { display: flex; flex-direction: column; overflow: hidden; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 18px; border-bottom: 1px solid var(--border); background: var(--panel); }
.device-picker label { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.topbar-actions { margin-left: auto; }
button.ghost#refresh { padding: 6px 12px; font-size: 16px; }

.view { flex: 1; overflow: auto; padding: 18px; }
#view-map { padding: 0; display: flex; flex-direction: column; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 12px 18px; border-bottom: 1px solid var(--border); }
.toolbar .inline, label.inline { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }

/* Add/Edit place form: lay the fields out in a wrapping row instead of a tall
   single column. The heading and the button row span the full width. */
#place-form, #zone-form { display: flex; flex-wrap: wrap; gap: 12px 16px; align-items: center; }
#place-form h2, #place-form > div, #zone-form h2, #zone-form > div { flex-basis: 100%; }
#place-form input[type="text"], #place-form input[type="number"],
#zone-form input[type="text"], #zone-form input[type="number"] { max-width: 200px; }

/* Map address/place search box + results dropdown. */
.addr-search-wrap { position: relative; display: inline-flex; gap: 4px; align-items: center; }
#addr-results {
  position: absolute; top: 100%; left: 0; z-index: 5; margin-top: 4px; min-width: 260px;
  max-height: 260px; overflow: auto; background: var(--panel);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 28px rgba(0,0,0,.4);
}
.addr-hit { padding: 8px 11px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--border); }
.addr-hit:last-child { border-bottom: 0; }
.addr-hit:hover { background: var(--panel-2); }
#map { flex: 1; min-height: 200px; }
/* History table panel — splits the map view; scrolls independently. */
#history-panel { height: 38vh; overflow: auto; border-top: 1px solid var(--border); background: var(--panel); }
#history-panel table { font-size: 12px; }
#history-panel th { position: sticky; top: 0; background: var(--panel); }
.hist-row { cursor: pointer; }
.hist-row:hover td { background: var(--panel-2); }

/* Lists & cards */
.list { display: flex; flex-direction: column; gap: 10px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card h2 { margin: 0 0 12px; font-size: 16px; }
.card.danger { border-color: #5a2b2b; }
.row { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; display: flex; align-items: center; gap: 12px; }
.row .icon { font-size: 20px; width: 28px; text-align: center; color: var(--muted); }
.row .icon .icon { width: 20px; height: 20px; vertical-align: middle; }
/* Severity dot for the alerts feed (replaces per-event emoji). */
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 999px; background: var(--muted); }
.dot-info { background: var(--accent); }
.dot-warning { background: var(--orange); }
.dot-critical { background: var(--red); }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; }
.row .sub { color: var(--muted); font-size: 12px; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.pill.green { color: var(--green); border-color: var(--green); }
.pill.red { color: var(--red); border-color: var(--red); }
.pill.orange { color: var(--orange); border-color: var(--orange); }
.sev-critical { border-left: 3px solid var(--red); }
.sev-warning { border-left: 3px solid var(--orange); }
.sev-info { border-left: 3px solid var(--accent); }
.ack-btn { background: var(--green); padding: 6px 12px; }
.empty { color: var(--muted); text-align: center; padding: 40px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); font-size: 13px; }
th { color: var(--muted); font-weight: 600; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 11px 18px; border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,.4); z-index: 9999;
}
#health-chart, #ecg-canvas { width: 100%; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 14px; }
#ecg-meta { margin-bottom: 12px; }

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; align-items: center; overflow-x: auto; }
  #nav { flex-direction: row; }
  .sidebar-foot { display: none; }
}

/* Map popups: match the dark console theme (MapLibre defaults to a white bubble,
   which leaves the app's light text unreadable). */
.maplibregl-popup-content {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; box-shadow: 0 8px 28px rgba(0,0,0,.45); font-size: 13px;
}
.maplibregl-popup-content b { color: var(--text); }
.maplibregl-popup-anchor-top .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-right .maplibregl-popup-tip { border-bottom-color: var(--panel-2); }
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-right .maplibregl-popup-tip { border-top-color: var(--panel-2); }
.maplibregl-popup-anchor-left .maplibregl-popup-tip { border-right-color: var(--panel-2); }
.maplibregl-popup-anchor-right .maplibregl-popup-tip { border-left-color: var(--panel-2); }
.maplibregl-popup-close-button { color: var(--muted); font-size: 18px; padding: 0 6px; }

/* ---- LifeTrack brand + inline SVG icon set ---- */
/* Line icons inherit text colour; sit inline with their label. */
.icon {
  width: 18px; height: 18px; flex: none; vertical-align: -4px;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
/* nav/button labels keep icon + text on one line with a tidy gap */
#nav a, .toolbar button, .topbar button { display: inline-flex; align-items: center; gap: 8px; }
.icon-btn { padding: 7px 10px; }
.icon-btn .icon { vertical-align: middle; }

/* The shield-and-pin logo mark (colour, not currentColor). */
.brand-mark {
  width: 26px; height: 26px; flex: none; display: inline-block;
  background: center/contain no-repeat
    url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><path d='M16 3 26 7v9c0 6.6-4.6 11-10 13C10.6 30 6 25.6 6 19V7Z' fill='%234f86f7' opacity='.18'/><path d='M16 3 26 7v9c0 6.6-4.6 11-10 13C10.6 30 6 25.6 6 19V7Z' fill='none' stroke='%234f86f7' stroke-width='1.5'/><path d='M16 11a4 4 0 0 0-4 4c0 3 4 6.5 4 6.5 0 0 4-3.5 4-6.5a4 4 0 0 0-4-4Z' fill='%234f86f7'/><circle cx='16' cy='15' r='1.6' fill='%23fff'/></svg>");
}
.brand-lg { font-size: 24px; gap: 10px; padding: 0 0 6px; }
.brand-lg .brand-mark { width: 34px; height: 34px; }

/* Subtle depth/polish on the active nav item + primary buttons. */
#nav a.active { background: linear-gradient(180deg, var(--accent-2), var(--accent)); box-shadow: 0 4px 14px rgba(79,134,247,.35); }
button:not(.ghost):not(.link) { box-shadow: 0 2px 8px rgba(79,134,247,.25); }
button:active { transform: translateY(1px); }
