:root {
  /* ink-splat mask for the device colour marker (filled, not stroked: an
     8-unit stroke on a 200 viewBox is a hairline at 14px) */
  --brush: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200"><path d="M87 70C76 53 72 31 53 32C35 32 29 51 39 64C46 73 62 79 69 86C75 93 70 101 61 101L35 101C18 101 17 121 32 128C43 133 62 121 69 130C76 139 58 146 56 158C53 175 67 187 83 181C99 175 94 155 96 145C99 126 121 123 132 139C143 156 163 152 171 138C179 124 168 110 151 109C133 109 128 94 137 83C147 71 170 72 180 55C193 34 178 10 157 10C128 10 124 36 113 58C105 74 96 84 87 70Z"/><circle cx="88" cy="17" r="10"/><circle cx="183" cy="91" r="10"/><circle cx="18" cy="181" r="10"/><circle cx="130" cy="181" r="10"/></svg>');
  --bg: #ffffff;
  --panel: #ffffff;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --fg: #1f2937;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 1px 3px rgba(0,0,0,.06);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f8fafc;
  color: var(--fg);
}
h1 { font-size: 16px; font-weight: 600; margin: 0; }
h2 { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 10px; }
button, select, input {
  font: inherit; color: var(--fg); background: #fff;
  border: 1px solid var(--line-strong); border-radius: var(--radius); padding: 7px 10px;
}
button { cursor: pointer; }
button:hover { background: #f9fafb; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #1d4ed8; }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* layout */
header.bar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 20px; background: #fff; border-bottom: 1px solid var(--line);
}
header.bar .status { margin-left: auto; color: var(--muted); font-size: 12px; }
.pagefoot {
  display: flex; gap: 18px; justify-content: center;
  padding: 18px 20px 28px; border-top: 1px solid var(--line); margin-top: 8px;
}
.pagefoot a { color: var(--muted); font-size: 13px; text-decoration: none; }
.pagefoot a:hover { color: var(--accent); text-decoration: underline; }

/* subtle "new data" pulse: glows in and fades back out */
.livedot {
  width: 9px; height: 9px; border-radius: 50%; flex: none;
  background: #cbd5e1; margin-left: 12px; transition: background .4s;
}
.livedot.pulse { background: #16a34a; animation: livepulse 1.6s ease-out; }
@keyframes livepulse {
  0%   { box-shadow: 0 0 0 0 rgba(22,163,74,0);   background: #cbd5e1; }
  25%  { box-shadow: 0 0 8px 3px rgba(22,163,74,.45); background: #16a34a; }
  60%  { box-shadow: 0 0 6px 2px rgba(22,163,74,.25); background: #16a34a; }
  100% { box-shadow: 0 0 0 0 rgba(22,163,74,0);   background: #cbd5e1; }
}
@media (prefers-reduced-motion: reduce) { .livedot.pulse { animation: none; } }
.viewtoggle { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; }
.viewtoggle button { border: none; border-radius: 0; padding: 6px 14px; background: #fff; color: var(--muted); font-weight: 500; }
.viewtoggle button + button { border-left: 1px solid var(--line-strong); }
.viewtoggle button.active { background: var(--accent); color: #fff; }

/* logs */
#logs { display: flex; flex-direction: column; gap: 16px; }
.logs-group h2 {
  display: flex; align-items: center; gap: 8px; margin-bottom: 10px;
  cursor: pointer; user-select: none;
}
.logs-group .count { color: var(--muted); font-weight: 500; }
.logs-group .fold {
  width: 0; height: 0; flex: none; transition: transform .12s;
  border-left: 5px solid currentColor; border-top: 4px solid transparent; border-bottom: 4px solid transparent;
  transform: rotate(90deg);
}
.logs-group.folded .fold { transform: rotate(0deg); }
.logs-group.folded h2 { margin-bottom: 0; }
.logs-group.folded .log-list,
.logs-group.folded .gpsmap,
.logs-group.folded .more-btn { display: none; }
/* preview: only the newest rows until expanded */
.logs-group:not(.expanded) .log-row.more { display: none; }
.logs-group:not(.expanded) .log-row.faded { opacity: .4; }
.more-btn {
  width: 100%; margin-top: 8px; padding: 6px; font-size: 12px; font-weight: 500;
  color: var(--accent); background: var(--accent-soft); border: 1px solid #dbeafe;
}
.more-btn:hover { background: #dbeafe; }
.log-list { display: flex; flex-direction: column; }
.log-row { display: flex; gap: 12px; align-items: baseline; padding: 5px 4px; border-top: 1px solid var(--line); font-size: 13px; }
.log-row:first-child { border-top: none; }
.log-time { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; flex: none; width: 130px; }
.log-dev { font-weight: 600; white-space: nowrap; flex: none; width: 110px; overflow: hidden; text-overflow: ellipsis; }
.log-dev i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 5px; }
.log-body { flex: 1; min-width: 0; }
.log-body a { color: var(--accent); text-decoration: none; white-space: nowrap; }
.log-body a:hover { text-decoration: underline; }
.log-body code { word-break: break-all; font-size: 12px; background: #f1f5f9; padding: 0 3px; border-radius: 3px; }
.bat-chip {
  font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
  color: var(--accent); background: var(--accent-soft); border: 1px solid #bfdbfe;
  border-radius: 6px; padding: 1px 7px;
}
.bat-chip:hover { background: #dbeafe; }

/* battery-id popup */
#batPopup {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .4); z-index: 2000;
  display: flex; align-items: center; justify-content: center;
}
.bat-box { width: min(560px, 92vw); display: flex; flex-direction: column; gap: 12px; }
.bat-field { display: flex; flex-direction: column; gap: 4px; }
.bat-label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.bat-field code {
  font-size: 12px; background: #f1f5f9; border: 1px solid var(--line); border-radius: 6px;
  padding: 8px 10px; word-break: break-all; line-height: 1.6;
}
.gpsmap {
  height: 320px; min-height: 180px; resize: vertical; overflow: hidden;
  border-radius: 8px; margin-bottom: 12px; position: relative; z-index: 0;
}
.gpsmap .leaflet-container { border-radius: 8px; font: inherit; }
.log-empty { color: var(--muted); padding: 30px; text-align: center; }
.layout {
  display: grid; grid-template-columns: 280px 1fr; gap: 16px;
  padding: 16px; align-items: start;
}
.card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); padding: 16px;
}
aside { display: flex; flex-direction: column; gap: 16px; }
main { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

/* devices */
.devlist { display: flex; flex-direction: column; gap: 2px; }
.grp { margin-bottom: 4px; }
.grp-head {
  display: flex; align-items: center; gap: 8px; padding: 5px 6px; cursor: pointer;
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em;
  color: var(--muted); border-bottom: 1px solid var(--line);
}
.grp-head input { width: 15px; height: 15px; accent-color: var(--accent); }
.grp .dev { padding-left: 10px; }
.dev {
  display: flex; align-items: center; gap: 8px; padding: 6px 6px; border-radius: 6px;
}
.dev:hover { background: #f9fafb; }
.dev input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); }
/* Colour marker. Deliberately a brush stroke, not a disc — a coloured circle
   here reads as an online/offline LED, which is what .dot next to the name is. */
.dev .swatch {
  width: 14px; height: 14px; flex: none; border-radius: 0;
  background: var(--line-strong);
  -webkit-mask: var(--brush) center / contain no-repeat;
  mask: var(--brush) center / contain no-repeat;
}
.dev .name {
  flex: 1; min-width: 0; font-weight: 500; padding: 4px 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dev .sub { font-size: 11px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.dot.on { background: #16a34a; box-shadow: 0 0 0 3px rgba(22,163,74,.15); }
.dot.off { background: #9ca3af; }
.dev .note {
  border: none; background: transparent; cursor: pointer; padding: 2px 4px;
  border-radius: 6px; font-size: 13px; line-height: 1; opacity: .45;
}
.dev .note:hover { background: #eef2f7; opacity: 1; }
.dev .note.has { opacity: 1; }

.dev .edit {
  border: none; background: transparent; color: var(--muted); cursor: pointer;
  padding: 2px 4px; border-radius: 6px; font-size: 14px; line-height: 1;
}
.dev .edit:hover { background: #eef2f7; color: var(--fg); }
.addrow { display: flex; gap: 6px; margin-top: 10px; }
.addrow button { flex: 1; }

/* modal */
#modal {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .4);
  display: flex; align-items: center; justify-content: center; z-index: 2000;
}
.modal-box { width: 320px; display: flex; flex-direction: column; gap: 12px; }
.fld { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.fld input, .fld textarea { font: inherit; font-size: 14px; color: var(--fg); }
.fld textarea { resize: vertical; }
.fld input[type=color] { height: 34px; padding: 2px; width: 60px; }
.purge-row { display: flex; gap: 6px; }
.purge-row input { flex: 1; min-width: 0; }
.modal-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.modal-actions .spacer { flex: 1; }
button.danger { color: #dc2626; border-color: #fecaca; }
button.danger:hover { background: #fef2f2; }

/* hover tooltip */
.tip {
  position: absolute; pointer-events: none; z-index: 5; display: none;
  transform: translate(-50%, -100%); background: #fff; color: var(--fg);
  border: 1px solid var(--line-strong); border-radius: 6px; box-shadow: var(--shadow);
  padding: 6px 8px; font-size: 12px; white-space: nowrap; line-height: 1.5;
}
.tip .t { color: var(--muted); margin-bottom: 2px; }
.chart { position: relative; }

/* metrics */
.metrics { display: flex; flex-direction: column; gap: 4px; }
.metrics label { display: flex; align-items: center; gap: 8px; padding: 4px 6px; border-radius: 6px; cursor: pointer; }
.metrics label:hover { background: #f9fafb; }
.metrics input { accent-color: var(--accent); }
.metrics .unit { color: var(--muted); font-size: 12px; }

/* timeframe */
.timeframe { display: flex; flex-direction: column; gap: 14px; }
.tf-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.tf-grow { flex: 1; }
.tf-readout { font-size: 13px; color: var(--muted); }
.tf-readout b { color: var(--fg); font-weight: 600; }
/* live toggle — same footprint as the Full button; the outline lights up when
   armed and fades away when off. Fades to invisible (not display:none) when
   unavailable so the row never reflows. */
.livebtn {
  display: inline-flex; align-items: center; gap: 7px;
  border-color: transparent; background: transparent; color: var(--muted);
  transition: color .18s, background .18s, border-color .18s, opacity .25s;
}
.livebtn::before {
  content: ''; width: 7px; height: 7px; border-radius: 50%;
  background: currentColor; opacity: .45;
}
.livebtn:hover { border-color: var(--line-strong); background: #fff; color: var(--fg); }
.livebtn.on {
  border-color: var(--accent); background: var(--accent-soft); color: var(--accent);
}
.livebtn.on::before { background: #16a34a; opacity: 1; animation: liveblink 2s ease-in-out infinite; }
@keyframes liveblink { 0%,100% { opacity: 1; } 50% { opacity: .25; } }
.livebtn.gone { opacity: 0; pointer-events: none; }
@media (prefers-reduced-motion: reduce) { .livebtn.on::before { animation: none; } }

/* draggable brush band */
.band {
  position: relative; height: 46px; border-radius: 8px;
  background: #f1f5f9; border: 1px solid var(--line); user-select: none; touch-action: none;
  overflow: hidden;
}
.band .ticks { position: absolute; inset: 0; pointer-events: none; }
.band .ticks span {
  position: absolute; top: 0; bottom: 0; border-left: 1px solid #e2e8f0;
  font-size: 10px; color: var(--muted); padding: 2px 0 0 4px; white-space: nowrap;
}
.brush {
  position: absolute; top: 0; bottom: 0;
  background: rgba(37, 99, 235, .18); border: 1px solid var(--accent);
  border-radius: 6px; cursor: grab; min-width: 8px;
}
.brush:active { cursor: grabbing; }
.brush .grip {
  position: absolute; top: 6px; left: 50%; transform: translateX(-50%);
  width: 22px; height: 5px; border-radius: 3px; background: var(--accent); opacity: .7;
}
.brush .handle {
  position: absolute; top: 0; bottom: 0; width: 12px; cursor: ew-resize;
  display: flex; align-items: center; justify-content: center;
}
.brush .handle::before { content: ''; width: 3px; height: 60%; border-radius: 2px; background: var(--accent); }
.brush .handle.left { left: -6px; } .brush .handle.right { right: -6px; }

/* charts */
.chart {
  resize: vertical; overflow: hidden; min-height: 220px; height: 300px;
  display: flex; flex-direction: column;
}
.chart-head { flex: none; }
.chart h3 { margin: 0 0 6px; font-size: 13px; font-weight: 600; color: var(--fg); }
.chart .legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-bottom: 8px; font-size: 11px; color: var(--muted); }
.chart .legend .lg { display: inline-flex; align-items: center; cursor: pointer; user-select: none; }
.chart .legend .lg i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; margin-right: 5px; flex: none; }
.chart .legend .lg b { color: var(--fg); font-weight: 600; margin-left: 5px; min-width: 1ch; }
.chart .legend .lg.off { opacity: .4; text-decoration: line-through; }
.plot-host { position: relative; flex: 1; min-height: 0; }
.chart-arrow {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 3;
  width: 26px; height: 46px; padding: 0; border: 1px solid var(--line-strong);
  background: rgba(255,255,255,.85); color: var(--muted); border-radius: 7px;
  font-size: 20px; line-height: 1; cursor: pointer; opacity: 0; transition: opacity .12s;
}
.plot-host:hover .chart-arrow { opacity: 1; }
.chart-arrow:hover { color: var(--accent); border-color: var(--accent); background: #fff; }
.chart-arrow.left { left: 6px; } .chart-arrow.right { right: 6px; }
.empty { color: var(--muted); padding: 40px; text-align: center; }
.u-legend { font-size: 12px; }

/* login */
#login { position: fixed; inset: 0; background: #f8fafc; display: flex; align-items: center; justify-content: center; }
#login .card { width: 300px; display: flex; flex-direction: column; gap: 12px; }
.err { color: #dc2626; font-size: 12px; min-height: 14px; }
.hidden { display: none !important; }

/* secondary pages (/raw-source, /command-center) */
.page-body { padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.q-row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.q-row .fld { gap: 4px; }
.hint { font-weight: 400; text-transform: none; letter-spacing: 0; color: var(--muted); }
.chk { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.chk input { accent-color: var(--accent); }
.preview {
  display: block; font-size: 13px; background: #f1f5f9; border: 1px solid var(--line);
  border-radius: 6px; padding: 8px 10px; word-break: break-all;
}
.count { color: var(--muted); font-weight: 500; font-size: 12px; }

.rawtable-wrap { overflow-x: auto; }
.rawtable { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.rawtable th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line-strong);
  position: sticky; top: 0; background: #fff;
}
.rawtable td { padding: 5px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.rawtable .c-ts, .rawtable .c-imsi { white-space: nowrap; font-variant-numeric: tabular-nums; }
.rawtable .c-imsi { color: var(--muted); }
.rawtable .c-raw code { font-size: 12px; word-break: break-all; }
.topic-tag {
  font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 5px;
  background: #f1f5f9; color: var(--muted);
}
.topic-tag.t-hb { background: #eff6ff; color: #2563eb; }
.topic-tag.t-info { background: #f5f3ff; color: #7c3aed; }
.topic-tag.t-status { background: #ecfdf5; color: #16a34a; }
.loadmore { text-align: center; color: var(--muted); font-size: 12px; padding: 10px; }
.log-body b.ok { color: #16a34a; } .log-body b.fail { color: #dc2626; }
.log-body b.warn { color: #d97706; }
.log-body .tw {
  display: inline-block; font-size: 11.5px; background: #f1f5f9; color: var(--muted);
  border-radius: 5px; padding: 0 6px; margin-right: 4px; white-space: nowrap;
}
#mDid { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--muted); }

/* ---------- command center ---------- */
.dev .proto { font-size: 10px; color: var(--muted); letter-spacing: .02em; }
.dev .proto.p-131 { color: #16a34a; }
.dev .proto.p-122 { color: #d97706; }

/* permission chooser */
.perm-row { display: flex; gap: 10px; flex-wrap: wrap; margin: 10px 0 14px; }
.perm {
  flex: 1 1 150px; display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 10px 12px; text-align: left; border: 1px solid var(--line-strong); background: #fff;
}
.perm b { font-size: 13px; color: var(--fg); }
.perm span { font-size: 11.5px; color: var(--muted); }
.perm:hover { border-color: var(--accent); }
.perm.on { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.perm.on b { color: var(--accent); }

/* per-device preview of what a mode switch will publish */
.plan { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.gps-params { display: flex; flex-wrap: wrap; gap: 10px 22px; margin: 0 0 14px; }
.gps-params .fld { flex: 1 1 220px; }
.gps-params input { width: 100%; }

.plan-row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 7px 10px; border: 1px solid var(--line); border-radius: 7px; font-size: 12.5px;
}
.plan-row.same { opacity: .55; }
.plan-row.bad { border-color: #fecaca; background: #fef2f2; }
.plan-row code { font-size: 11.5px; color: var(--muted); word-break: break-all; flex-basis: 100%; }
.plan-note { color: var(--muted); }
.tag { font-size: 10.5px; background: #f1f5f9; color: var(--muted); border-radius: 4px; padding: 1px 5px; }

/* config editor */
.cfg-form { display: flex; flex-direction: column; gap: 14px; margin-top: 12px; }
.cfg-form .fld-name { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--fg); }
.cfg-line { display: flex; gap: 6px; }
.cfg-line input { flex: 1; min-width: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.cfg-pick { width: 46px; flex: none; padding: 2px; cursor: pointer; }
.cfg-form input.bad, .mask-sum.bad { border-color: #dc2626; background: #fef2f2; }
.mask-sum { display: flex; align-items: baseline; gap: 8px; padding: 6px 0; }
.mask-sum code.bad { color: #dc2626; background: none; }
.mask-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 2px 10px; }
.mask-item { display: flex; align-items: flex-start; gap: 8px; padding: 5px 6px; border-radius: 6px; cursor: pointer; }
.mask-item:hover { background: #f8fafc; }
.mask-item input { accent-color: var(--accent); margin-top: 2px; }
.mask-item b { display: block; font-size: 12.5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.mask-item .hint { display: block; }

/* toasts — top right, never block interaction, fade out on their own */
#toasts {
  position: fixed; top: 14px; right: 16px; z-index: 3000;
  display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}
.toast {
  background: #fff; border: 1px solid var(--line-strong); border-left: 3px solid var(--muted);
  border-radius: 8px; box-shadow: 0 4px 14px rgba(15,23,42,.10);
  padding: 9px 14px; font-size: 13px; color: var(--fg); max-width: 320px;
  animation: toastin .22s ease-out; transition: opacity .35s, transform .35s;
}
.toast.ok { border-left-color: #16a34a; }
.toast.fail { border-left-color: #dc2626; }
.toast.warn { border-left-color: #d97706; }
.toast.out { opacity: 0; transform: translateX(12px); }
@keyframes toastin { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: none; } }

/* responsive */
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .q-row { flex-direction: column; align-items: stretch; }
}
