:root {
  --bg: #f3f6f8;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --text: #13202b;
  --muted: #5d6d7a;
  --border: #d5dde3;
  --accent: #0b6e99;
  --accent-text: #ffffff;
  --ok: #1d8a4a;
  --warn: #c27c0e;
  --alarm: #c62828;
  --off: #6b7785;
  --shadow: 0 2px 10px rgba(10, 30, 45, .10);
  --radius: 12px;
  --tabbar-h: 60px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1418;
    --surface: #152028;
    --surface-2: #1c2a34;
    --text: #e3ebf0;
    --muted: #93a4b0;
    --border: #2a3a46;
    --accent: #3fa7d6;
    --accent-text: #06141c;
    --ok: #4cc27a;
    --warn: #e6a23c;
    --alarm: #ff5b5b;
    --off: #8795a1;
    --shadow: 0 2px 12px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; }
.error { color: var(--alarm); min-height: 1.2em; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 15px; margin: 0 0 10px; }
.spacer { flex: 1; }
.row { display: flex; align-items: center; }
.wrap { flex-wrap: wrap; }
.gap { gap: 8px; }

/* ---------- Buttons & inputs ---------- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 10px; padding: 9px 14px; font: inherit; font-weight: 600;
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--alarm); border-color: var(--alarm); }
.btn.alarm { background: var(--alarm); border-color: var(--alarm); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; min-height: 32px; font-size: 13px; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.icon-btn {
  appearance: none; border: 0; background: transparent; color: inherit; font-size: 20px;
  width: 40px; height: 40px; border-radius: 10px; cursor: pointer;
}
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 10px; min-height: 40px; min-width: 0;
}
input[type=checkbox] { width: 22px; height: 22px; min-height: 0; accent-color: var(--accent); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); margin-bottom: 12px;
}

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 12px; text-align: center; }
.brand { font-weight: 800; letter-spacing: .2px; }
.brand.big { font-size: 26px; }

/* ---------- Layout ---------- */
#app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  order: 0; flex: none; display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: #0b3954; color: #fff; position: relative; z-index: 1000;
}
.topbar select { flex: 1; max-width: 280px; background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.25); }
.topbar select option { color: #000; }
.pill {
  font-size: 12px; font-weight: 800; padding: 4px 10px; border-radius: 99px;
  background: var(--off); color: #fff; text-transform: uppercase; letter-spacing: .4px;
}
.pill.armed { background: var(--ok); }
.pill.disarmed { background: var(--off); }
.pill.alarm { background: var(--alarm); animation: blink 1s infinite; }
.pill.silent { background: var(--warn); }
@keyframes blink { 50% { opacity: .55; } }

main { flex: 1; position: relative; overflow: hidden; order: 2; }
.tab { position: absolute; inset: 0; display: none; }
.tab.active { display: block; }
.tab.scroll { overflow-y: auto; padding: 14px 14px 24px; -webkit-overflow-scrolling: touch; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }

.tabs {
  display: flex; background: var(--surface); border-top: 1px solid var(--border); z-index: 1000;
  order: 3; flex: none; padding-bottom: env(safe-area-inset-bottom);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
.tabs a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; text-decoration: none; color: var(--muted); font-size: 18px;
}
.tabs a span { font-size: 11px; font-weight: 600; }
.tabs a.active { color: var(--accent); }

@media (min-width: 900px) {
  .tabs { order: 1; height: auto; border-top: 0; border-bottom: 1px solid var(--border); padding: 0 8px; }
  .tabs a { flex: none; flex-direction: row; padding: 10px 16px; font-size: 15px; gap: 8px; }
  .tabs a span { font-size: 14px; }
  .tabs a.active { box-shadow: inset 0 -3px 0 var(--accent); }
  .tab.scroll { max-width: 1100px; margin: 0 auto; }
}

/* ---------- Map ---------- */
#map { position: absolute; inset: 0; }
.map-panel {
  position: absolute; z-index: 500; left: 8px; right: 8px; bottom: 8px;
  max-height: 52%; overflow-y: auto; margin: 0; padding: 10px 12px; font-size: 14px;
}
@media (min-width: 900px) {
  .map-panel { left: auto; right: 12px; top: 12px; bottom: auto; width: 360px; max-height: calc(100% - 24px); }
}
.map-panel.collapsed .collapsible { display: none; }
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px 12px; margin: 6px 0 8px; }
.stat { display: flex; flex-direction: column; }
.stat .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
.stat .v { font-weight: 700; }
.v.ok { color: var(--ok); } .v.warn { color: var(--warn); } .v.alarm { color: var(--alarm); }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
@media (max-width: 899px) {
  .actions { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
  .actions::-webkit-scrollbar { display: none; }
  .brand-text { display: none; }
}
.panel-head { display: flex; align-items: center; gap: 8px; }
.panel-head .title { font-weight: 800; font-size: 16px; flex: 1; }
.alarm-banner {
  background: var(--alarm); color: #fff; border-radius: 10px; padding: 8px 10px; margin: 8px 0; font-weight: 700;
}
.range { display: flex; gap: 4px; margin-top: 8px; }
.range .btn.active { border-color: var(--accent); color: var(--accent); }
.anchor-icon { font-size: 22px; line-height: 22px; text-align: center; filter: drop-shadow(0 1px 2px rgba(0,0,0,.4)); }
.leaflet-container { font: inherit; }

/* ---------- Events ---------- */
.events { list-style: none; margin: 0; padding: 0; }
.events li {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; margin-bottom: 8px; display: grid; grid-template-columns: 32px 1fr; gap: 4px 10px;
}
.events .ico { font-size: 22px; grid-row: span 2; }
.events .txt { font-weight: 600; }
.events .meta { font-size: 12px; color: var(--muted); }
.events .thumbs { grid-column: 2; display: flex; gap: 6px; overflow-x: auto; margin-top: 4px; }
.events li.alarm { border-left: 4px solid var(--alarm); }
.events li.warn { border-left: 4px solid var(--warn); }
.thumb { width: 96px; height: 72px; object-fit: cover; border-radius: 8px; cursor: pointer; background: var(--surface-2); flex: none; }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.photo-grid figure { margin: 0; position: relative; }
.photo-grid img { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 10px; cursor: pointer; background: var(--surface-2); }
.photo-grid figcaption { font-size: 11px; color: var(--muted); }
.badge { font-size: 10px; font-weight: 800; padding: 1px 6px; border-radius: 6px; background: var(--accent); color: var(--accent-text); }
.photo-grid .badge { position: absolute; top: 6px; left: 6px; }

/* ---------- Settings ---------- */
fieldset { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); margin: 0 0 12px; padding: 10px 14px; }
legend { font-weight: 800; padding: 0 6px; }
.field { display: grid; grid-template-columns: 1fr 150px; gap: 4px 12px; align-items: center; padding: 8px 0; border-bottom: 1px dashed var(--border); }
.field:last-child { border-bottom: 0; }
.field label { font-weight: 600; }
.field .help { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.field .unit { font-size: 12px; color: var(--muted); margin-left: 4px; }
.field .ctl { display: flex; align-items: center; justify-content: flex-end; }
.field .ctl input:not([type=checkbox]), .field .ctl select { width: 100%; }
.field.changed label::after { content: " •"; color: var(--accent); }
.sticky-actions {
  /* bottom compensates the 24px padding of the scroll container (sticky rect is deflated by it) */
  position: sticky; bottom: -24px; display: flex; justify-content: flex-end; gap: 8px; padding: 10px 0 14px;
  background: linear-gradient(transparent, var(--bg) 30%);
}

/* ---------- System ---------- */
.grid2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 700px) { .grid2 { grid-template-columns: 1fr 1fr; } }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 14px; }
.kv .k { color: var(--muted); }
.cmds, .alerts { list-style: none; margin: 8px 0 0; padding: 0; font-size: 13px; }
.cmds li, .alerts li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.status-pending, .status-sent { color: var(--warn); font-weight: 700; }
.status-done { color: var(--ok); font-weight: 700; }
.status-failed, .status-cancelled { color: var(--alarm); font-weight: 700; }
.raw { font-size: 12px; overflow-x: auto; background: var(--surface-2); padding: 10px; border-radius: 8px; max-height: 360px; }
#cmd-args { flex: 1; min-width: 180px; }
.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin: 4px 0 8px; }
.bar > div { height: 100%; background: var(--accent); }

/* ---------- Lightbox & toast ---------- */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 3000; display: flex; flex-direction: column; }
.lightbox-bar { display: flex; align-items: center; gap: 8px; padding: 10px; color: #fff; padding-top: max(10px, env(safe-area-inset-top)); }
.lightbox img { flex: 1; min-height: 0; object-fit: contain; width: 100%; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + 20px + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 4000;
  box-shadow: var(--shadow); max-width: 90%; font-weight: 600;
}
.toast.err { background: var(--alarm); color: #fff; }
