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

/* Light theme (default) */
:root {
  --bg: #EEF1F6; --panel: #FFFFFF; --line: #E2E8F0; --fg: #0F1724; --dim: #5B6B7C;
  --red: #C62E2E; --red-soft: #FDECEC; --amber: #B26A00; --amber-soft: #FFF4E2;
  --green: #178A45; --green-soft: #E7F6EC; --blue: #1E62D0; --blue-soft: #EBF2FD;
  --btn: #F3F6FA; --btn-hover: #E7ECF3; --line-strong: #CBD5E1;
  --shadow: 0 1px 2px rgba(15,23,36,.05), 0 4px 12px rgba(15,23,36,.05);
  --hdr: linear-gradient(135deg, #10161F 0%, #1C2734 100%); --hdr-fg: #FFFFFF;
}
/* Dark theme (control-room night mode) */
:root[data-theme="dark"] {
  --bg: #0C1017; --panel: #141B24; --line: #26303C; --fg: #E7EDF4; --dim: #8A97A5;
  --red: #F86A60; --red-soft: #3A1416; --amber: #E3A93C; --amber-soft: #33270D;
  --green: #46C06A; --green-soft: #102917; --blue: #5B96F7; --blue-soft: #12233F;
  --btn: #1D2632; --btn-hover: #27323F; --line-strong: #33404E;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
  --hdr: linear-gradient(135deg, #10161F 0%, #16202B 100%); --hdr-fg: #E7EDF4;
}

html, body { height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 14px/1.5 "Inter", -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ---------- header ---------- */
header { display: flex; align-items: center; gap: 28px; padding: 14px 22px; background: var(--hdr); color: var(--hdr-fg); }
.brand { display: flex; align-items: center; gap: 12px; }
.logo-dot { width: 14px; height: 14px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #FF7A5C, #D8342C 70%); box-shadow: 0 0 12px rgba(232,74,44,.8); flex: none; }
.logo-dot.big { width: 22px; height: 22px; margin: 0 auto 12px; }
.brand-name { font-weight: 800; letter-spacing: 4px; font-size: 16px; line-height: 1.1; }
.brand-site { color: #93A1B0; font-size: 12px; font-weight: 500; }
.kpis { display: flex; gap: 10px; margin-left: auto; }
.kpi { display: flex; align-items: baseline; gap: 7px; background: rgba(255,255,255,.07); border: 1px solid rgba(255,255,255,.1); border-radius: 10px; padding: 6px 14px; }
.kpi-num { font-size: 19px; font-weight: 800; font-variant-numeric: tabular-nums; }
.kpi-lbl { font-size: 11px; color: #93A1B0; text-transform: uppercase; letter-spacing: .6px; }
.kpi.hot { background: rgba(216,52,44,.25); border-color: rgba(255,110,90,.5); }
.kpi.hot .kpi-num { color: #FF9A8A; }
.hdr-right { display: flex; gap: 8px; align-items: center; }
.pill { padding: 3px 12px; border-radius: 999px; font-size: 11px; font-weight: 700; letter-spacing: 1px; background: var(--green); color: #fff; }
.pill.off { background: var(--red); }
header button { border: 1px solid rgba(255,255,255,.22); background: transparent; color: var(--hdr-fg); }
header button:hover { background: rgba(255,255,255,.12); }
button.icon { padding: 6px 10px; }

/* ---------- layout ---------- */
main { flex: 1; display: grid; grid-template-columns: 360px 1fr 290px; min-height: 0; }
aside { background: var(--bg); overflow-y: auto; padding: 16px 14px; }
#alertPane { border-right: 1px solid var(--line); }
#devicePane { border-left: 1px solid var(--line); }
h2 { font-size: 11px; text-transform: uppercase; letter-spacing: 2px; color: var(--dim); margin: 20px 2px 10px; font-weight: 700; }
h2:first-child { margin-top: 2px; }
h2 .count { background: var(--btn-hover); color: var(--dim); border-radius: 999px; padding: 1px 9px; font-size: 10px; letter-spacing: 0; margin-left: 4px; }
#map { min-height: 0; }
.empty { color: var(--dim); font-size: 13px; padding: 14px; text-align: center; border: 1px dashed var(--line-strong); border-radius: 12px; }

/* ---------- alert cards ---------- */
.alert { border: 1px solid var(--line); border-radius: 14px; padding: 12px; margin-bottom: 10px; background: var(--panel); box-shadow: var(--shadow); }
.alert.active { border-color: var(--red); animation: flash 1.1s infinite alternate; }
.alert.acked { border-color: var(--amber); }
.alert.resolved { opacity: .62; }
@keyframes flash { from { box-shadow: var(--shadow); } to { box-shadow: 0 0 0 4px var(--red-soft), var(--shadow); } }
.alert-row { display: flex; gap: 10px; align-items: flex-start; }
.type-ico { width: 38px; height: 38px; border-radius: 11px; display: flex; align-items: center; justify-content: center; font-size: 18px; flex: none; background: var(--btn); }
.t-sos, .t-man_down { background: var(--red-soft); }
.t-silent { background: var(--amber-soft); }
.t-welfare_missed, .t-comms_lost { background: var(--amber-soft); }
.t-test { background: var(--blue-soft); }
.alert-main { flex: 1; min-width: 0; }
.alert .type { font-weight: 700; font-size: 13.5px; letter-spacing: .2px; }
.alert.active .type { color: var(--red); }
.alert.acked .type { color: var(--amber); }
.alert.test .type { color: var(--blue); }
.alert .meta { color: var(--dim); font-size: 12px; margin-top: 2px; }
.alert-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.alert-id { color: var(--dim); font-size: 11px; font-variant-numeric: tabular-nums; }
.chip { font-size: 10px; font-weight: 800; letter-spacing: .8px; padding: 3px 9px; border-radius: 999px; }
.chip.red { background: var(--red); color: #fff; }
.chip.amber { background: var(--amber); color: #fff; }
.chip.grey { background: var(--btn-hover); color: var(--dim); }
.esc { background: var(--red); color: #fff; font-size: 10px; padding: 2px 8px; border-radius: 999px; margin-left: 6px; vertical-align: middle; animation: escflash .6s infinite alternate; }
@keyframes escflash { from { opacity: 1; } to { opacity: .55; } }
.note { background: var(--btn); border-radius: 10px; padding: 8px 10px; font-size: 12.5px; color: var(--fg); margin-top: 10px; }
.alert .actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.alert.test .actions { margin-top: 0; }

/* ---------- buttons ---------- */
button { border: 1px solid var(--line-strong); background: var(--btn); color: var(--fg); border-radius: 9px; padding: 7px 14px; cursor: pointer; font: inherit; font-size: 13px; font-weight: 600; transition: background .12s, transform .06s; }
button:hover { background: var(--btn-hover); }
button:active { transform: translateY(1px); }
button.danger { background: var(--red); border-color: var(--red); color: #fff; }
button.warn { background: var(--amber); border-color: var(--amber); color: #fff; }
button.ok { background: var(--green); border-color: var(--green); color: #fff; }
button.ghost { background: transparent; }

/* ---------- devices ---------- */
.device { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 14px; margin-bottom: 8px; border: 1px solid var(--line); background: var(--panel); box-shadow: var(--shadow); cursor: pointer; }
.device.in-alert { border-color: var(--red); background: var(--red-soft); }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--btn-hover); color: var(--dim); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex: none; position: relative; }
.avatar.on { background: var(--green-soft); color: var(--green); }
.avatar.on::after { content: ""; position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%; background: var(--green); border: 2px solid var(--panel); }
.dev-main { flex: 1; min-width: 0; }
.device .nm { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device .sub, .timer .sub { color: var(--dim); font-size: 11.5px; }
.danger-text { color: var(--red); }
.batt { font-size: 11.5px; font-weight: 600; font-variant-numeric: tabular-nums; }
.batt-ok { color: var(--green); } .batt-mid { color: var(--amber); } .batt-low { color: var(--red); }
.device .watch { font-size: 12px; padding: 5px 10px; }

/* ---------- welfare timers ---------- */
.timer { border: 1px solid var(--line); border-radius: 14px; padding: 11px 12px; margin-bottom: 8px; background: var(--panel); box-shadow: var(--shadow); }
.timer-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.timer .nm { font-weight: 600; font-size: 13.5px; }
.timer .tleft { font-weight: 800; font-variant-numeric: tabular-nums; font-size: 17px; }
.timer .bar { height: 5px; border-radius: 999px; background: var(--btn-hover); margin-top: 9px; overflow: hidden; }
.timer .bar-fill { height: 100%; border-radius: 999px; background: var(--blue); transition: width 1s linear; }
.timer.low { border-color: var(--amber); }
.timer.low .tleft { color: var(--amber); }
.timer.low .bar-fill { background: var(--amber); }

/* ---------- map pins ---------- */
.pin { width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font: 700 11px "Inter", sans-serif; color: #fff; border: 3px solid #fff; box-shadow: 0 2px 8px rgba(0,0,0,.35); }
.pin-on { background: #178A45; }
.pin-off { background: #8A97A5; }
.pin-alert { background: #D8342C; animation: pulse 1.2s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(216,52,44,.55); } 100% { box-shadow: 0 0 0 16px rgba(216,52,44,0); } }

/* ---------- misc ---------- */
.quiet { color: var(--dim); font-size: 12px; }
.err { color: var(--red); font-size: 12px; min-height: 16px; margin-top: 6px; }
.hidden { display: none !important; }
textarea { width: 100%; resize: vertical; border-radius: 10px; border: 1px solid var(--line-strong); background: var(--bg); color: var(--fg); padding: 10px 12px; font: inherit; }
textarea:focus, input:focus { outline: 2px solid var(--blue); outline-offset: 0; border-color: transparent; }

/* ---------- login ---------- */
.login { position: fixed; inset: 0; background: var(--hdr); z-index: 3000; display: flex; align-items: center; justify-content: center; }
.login-box { text-align: center; width: 310px; background: var(--panel); border-radius: 18px; padding: 34px 28px; box-shadow: 0 14px 44px rgba(0,0,0,.35); }
.login-box h1 { letter-spacing: 7px; margin-bottom: 2px; color: var(--fg); font-size: 24px; }
.login-box p { color: var(--dim); margin-bottom: 20px; }
.login-box input { width: 100%; padding: 12px; border-radius: 10px; border: 1px solid var(--line-strong); background: var(--bg); color: var(--fg); margin-bottom: 10px; text-align: center; font-size: 16px; }
.login-box button { width: 100%; padding: 12px; font-size: 14.5px; background: var(--red); border-color: var(--red); color: #fff; font-weight: 700; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(8,12,18,.72); z-index: 2000; display: flex; align-items: center; justify-content: center; backdrop-filter: blur(2px); }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 18px; width: min(720px, 94vw); box-shadow: 0 18px 60px rgba(0,0,0,.4); }
.modal-hdr { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; font-weight: 700; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
#liveVideo { width: 100%; aspect-ratio: 4/3; background: #000; border-radius: 12px; }

.leaflet-container { font: inherit; }

/* ---------- v0.4: team assist ---------- */
.t-assist { background: var(--amber-soft); }
.alert.assist.active { border-color: var(--amber); animation: none; box-shadow: 0 0 0 3px var(--amber-soft), var(--shadow); }
.alert.assist.active .type { color: var(--amber); }
.chip.amber-solid { background: var(--amber); color: #fff; }

/* ---------- v0.4: readiness ---------- */
.device.not-ready { border-left: 4px solid var(--amber); }
h2 button.mini { font-size: 10px; padding: 3px 9px; letter-spacing: .5px; margin-left: 6px; text-transform: none; }
pre.report { max-height: 56vh; overflow: auto; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 14px; font: 12px/1.6 ui-monospace, "SF Mono", Menlo, monospace; white-space: pre-wrap; }
.modal-box.wide { width: min(860px, 96vw); max-height: 92vh; overflow-y: auto; }

/* ---------- v0.4: talkback + captions ---------- */
.video-tools { display: flex; align-items: center; gap: 12px; margin-top: 10px; }
#talkBtn.talking { background: var(--green); border-color: var(--green); animation: escflash .6s infinite alternate; }
.captions { margin-top: 10px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; font-size: 12.5px; max-height: 120px; overflow-y: auto; color: var(--fg); }
.captions div { padding: 2px 0; border-bottom: 1px dashed var(--line); }
.captions div:last-child { border-bottom: none; }

/* ---------- v0.4: incident + policies ---------- */
.media-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 12.5px; }
.media-row audio { height: 32px; flex: 1; }
.policy-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; background: var(--bg); }
.policy-add { margin-top: 14px; }
.policy-add input, .policy-ask input { width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line-strong); background: var(--bg); color: var(--fg); margin-bottom: 8px; font: inherit; }
.policy-ask { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 14px; display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: start; }
.policy-ask div { grid-column: 1 / -1; white-space: pre-wrap; }

/* ---------- v0.5: competitive parity (setup, reports, toasts) ---------- */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; border-bottom: 1px solid var(--line); padding-bottom: 10px; }
.tab { background: transparent; border: 1px solid var(--line-strong); border-radius: 8px; padding: 6px 14px; font-size: 12.5px; }
.tab.active { background: var(--red); border-color: var(--red); color: #fff; }
.tab-body h3.sub-h { font-size: 11px; text-transform: uppercase; letter-spacing: 2px; color: var(--dim); margin: 18px 2px 8px; }
.esc-level { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; background: var(--bg); }
.esc-hdr { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; font-size: 13px; }
.esc-hdr input, .contact-row input:not([type="checkbox"]), .inline-form input, .inline-form select {
  padding: 6px 9px; border-radius: 8px; border: 1px solid var(--line-strong); background: var(--panel); color: var(--fg); font: inherit; font-size: 12.5px;
}
.contact-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 5px 0; font-size: 12px; }
.contact-row label { display: flex; align-items: center; gap: 3px; color: var(--dim); }
.inline-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 12px; font-size: 12.5px; color: var(--dim); }
.team-tag { font-size: 10px; background: var(--btn-hover); color: var(--dim); border-radius: 6px; padding: 1px 7px; font-weight: 600; vertical-align: middle; }
.warn-text { color: var(--amber); }
.report-kpis { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.rkpi { border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; background: var(--bg); min-width: 108px; }
.rkpi-num { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.rkpi-lbl { font-size: 10.5px; color: var(--dim); text-transform: uppercase; letter-spacing: .6px; }
.btn-link { text-decoration: none; display: inline-block; padding: 7px 12px; border: 1px solid var(--line-strong); border-radius: 9px; color: var(--fg); font-size: 13px; font-weight: 600; }
.btn-link:hover { background: var(--btn-hover); }
#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 4000; display: flex; flex-direction: column; gap: 8px; max-width: 380px; }
.toast { background: var(--panel); border: 1px solid var(--line-strong); border-left: 4px solid var(--green); border-radius: 12px; padding: 10px 14px; font-size: 12.5px; box-shadow: 0 6px 20px rgba(0,0,0,.25); transition: opacity .5s; }
.toast.warn { border-left-color: var(--amber); }
.toast.bad { border-left-color: var(--red); }
.toast.fade { opacity: 0; }

@media (max-width: 980px) {
  main { grid-template-columns: 1fr; grid-template-rows: auto 320px auto; }
  #alertPane, #devicePane { border: none; }
  .kpis { display: none; }
}
