:root {
  --bg: #ffffff;
  --panel: rgba(255, 255, 255, 0.96);
  --text: #1f2328;
  --muted: #59636e;
  --border: #d1d9e0;
  --accent: #0969da;
  --input: #f6f8fa;
  --ok: #1a7f37;
  --warn: #9a6700;
  --bad: #cf222e;
  --purge: #d1590f;
  --purge-strong: #a8410a;
  --shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1117;
    --panel: rgba(22, 27, 34, 0.96);
    --text: #e6edf3;
    --muted: #9198a1;
    --border: #3d444d;
    --accent: #4493f8;
    --input: #0d1117;
    --ok: #3fb950;
    --warn: #d29922;
    --bad: #f85149;
    --purge: #db6d28;
    --purge-strong: #f0883e;
    --shadow: 0 4px 18px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1117; --panel: rgba(22, 27, 34, 0.96); --text: #e6edf3; --muted: #9198a1;
  --border: #3d444d; --accent: #4493f8; --input: #0d1117; --ok: #3fb950; --warn: #d29922;
  --bad: #f85149; --purge: #db6d28; --purge-strong: #f0883e; --shadow: 0 4px 18px rgba(0, 0, 0, 0.5); color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
#map { position: absolute; inset: 0; }

h1 { font-size: 17px; margin: 0; }
h2 { font-size: 13px; margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* panel */
.panel {
  position: absolute; z-index: 1000; top: 12px; left: 12px; bottom: 12px;
  width: 330px; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); overflow: hidden;
}
.panel.collapsed { display: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 14px 8px 52px; }
.panel-head h1 { white-space: nowrap; }
.head-side { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.head-side .live { white-space: nowrap; }
.account-link { font-size: 12px; color: var(--accent); text-decoration: none; white-space: nowrap; }
.account-link:hover { text-decoration: underline; }
.panel-toggle {
  position: absolute; z-index: 1001; top: 20px; left: 20px; width: 34px; height: 34px;
  border-radius: 8px; border: 1px solid var(--border); background: var(--panel); color: var(--text);
  font-size: 18px; cursor: pointer; box-shadow: var(--shadow);
}
.tabs { display: flex; border-bottom: 1px solid var(--border); padding: 0 8px; }
.tab {
  flex: 1; padding: 8px; background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--muted); font: inherit; cursor: pointer;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tab-body { flex: 1; overflow-y: auto; padding: 10px 14px 14px; }
.panel-foot { border-top: 1px solid var(--border); padding: 6px 14px; }

.field { display: block; margin: 10px 0; font-weight: 500; }
.field select, .field input[type=datetime-local], .field input[type=text], .field input[type=number], .field input[type=password], .field input[type=email] {
  display: block; width: 100%; margin-top: 4px; padding: 6px 8px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--input); color: var(--text); font: inherit;
}
.field input[type=range] { display: block; width: 100%; margin-top: 6px; accent-color: var(--accent); }
.field.inline { display: flex; align-items: center; justify-content: space-between; }
.field input[type=color] { width: 44px; height: 28px; border: 1px solid var(--border); border-radius: 6px; background: none; padding: 1px; }
.field output { float: right; font-weight: 400; color: var(--muted); }
.check { display: flex; gap: 8px; align-items: center; margin: 8px 0; cursor: pointer; }
.check input { accent-color: var(--accent); }
.custom-range { border-left: 3px solid var(--border); padding-left: 10px; }
.btn {
  padding: 6px 14px; border-radius: 6px; border: 1px solid var(--accent); background: var(--accent);
  color: #fff; font: inherit; cursor: pointer;
}
.btn.secondary { background: transparent; color: var(--text); border-color: var(--border); }
.btn.danger { background: transparent; color: var(--bad); border-color: var(--bad); }
/* Purge data: solid orange, louder than the red outline of Delete and clearly a different button */
.btn.purge { background: var(--purge); color: #fff; border-color: var(--purge); font-weight: 600; }
.btn.purge.armed { background: var(--purge-strong); border-color: var(--purge-strong);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--purge) 35%, transparent); }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font: inherit; font-size: 12px; }

.filter-head { display: flex; justify-content: space-between; align-items: baseline; }
.check-list { max-height: 260px; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; }
.check-list .check { margin: 5px 0; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; flex: none; }
.check-list .meta { margin-left: auto; color: var(--muted); font-size: 12px; }

.table-wrap { overflow-x: auto; }
table.stats { width: 100%; border-collapse: collapse; font-size: 12px; }
table.stats th, table.stats td { text-align: right; padding: 4px 5px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.stats th:first-child, table.stats td:first-child { text-align: left; }
table.stats th { color: var(--muted); font-weight: 600; }
table.stats tbody tr.clickable { cursor: pointer; }
table.stats tbody tr.clickable:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }

.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.live.ok .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.live.bad .dot { background: var(--bad); }

/* legend */
.legend {
  position: absolute; z-index: 1000; right: 12px; bottom: 28px; max-width: 260px; max-height: 45vh; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  box-shadow: var(--shadow); font-size: 12px;
}
.legend h3 { font-size: 11px; margin: 0 0 4px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.legend .row { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.legend .scale { display: flex; gap: 4px; margin-bottom: 6px; }
.legend .scale div { text-align: center; color: var(--muted); font-size: 10px; }
.legend .scale span { display: block; width: 24px; height: 16px; border-radius: 3px; margin: 0 auto 2px; }

/* square and station tooltips */
.leaflet-tooltip.cell-tip, .leaflet-tooltip.station-tip {
  font-size: 12px; line-height: 1.35; padding: 6px 8px; min-width: 170px; max-width: 280px; white-space: normal;
  background: var(--panel); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow);
}
.leaflet-tooltip.cell-tip::before { display: none; }
.leaflet-tooltip.station-tip::before { border-top-color: var(--border); }
.cell-tip .muted, .station-card .muted { color: var(--muted); }
.cell-tip .tip-h {
  margin: 6px 0 2px; font-size: 10px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
}
.cell-tip table, .station-card table { border-collapse: collapse; width: 100%; }
.cell-tip td { padding: 1px 0; vertical-align: middle; }
.cell-tip td.num { text-align: right; padding-left: 10px; font-variant-numeric: tabular-nums; }
.cell-tip .swatch, .station-card .swatch { margin-right: 5px; }
.station-card .sc-head { font-size: 13px; }
.station-card .sc-label { margin-top: 1px; }
.station-card .sc-range { font-size: 11px; margin: 4px 0 3px; }
.station-card th { text-align: left; font-weight: normal; color: var(--muted); padding: 1px 12px 1px 0; white-space: nowrap; }
.station-card td { text-align: right; font-variant-numeric: tabular-nums; padding: 1px 0; white-space: nowrap; }
.pulse { animation: pulse 1.6s ease-out 3; }
@keyframes pulse { 0% { stroke-width: 2; stroke-opacity: 1; } 100% { stroke-width: 18; stroke-opacity: 0; } }

/* phone: panel becomes a bottom sheet */
@media (max-width: 640px) {
  .panel { top: auto; left: 0; right: 0; bottom: 0; width: auto; height: 55vh; border-radius: 12px 12px 0 0; }
  .panel-toggle { top: 12px; left: 12px; }
  .panel-head { padding-left: 14px; }
  .legend { right: 8px; bottom: auto; top: 12px; max-width: 46vw; }
}

/* admin page */
.admin { max-width: 1000px; margin: 0 auto; padding: 16px; }
.admin header { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.card { border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; margin: 14px 0; background: var(--panel); }
.card h3 { font-size: 14px; margin: 14px 0 6px; }
.card h3:first-of-type { margin-top: 4px; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 0 14px; }
.admin table { width: 100%; border-collapse: collapse; font-size: 13px; }
.admin th, .admin td { padding: 6px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.admin td.num { text-align: right; }
.admin .actions { white-space: nowrap; }
.admin .actions button { margin-left: 4px; }
.error { color: var(--bad); }
.okmsg { color: var(--ok); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }
code, pre { font: 12px ui-monospace, SFMono-Regular, Consolas, monospace; }
pre.raw { white-space: pre-wrap; word-break: break-all; margin: 0; }

/* admin tabs and node config */
.admin .admin-tabs { padding: 0; margin-top: 12px; }
.admin .admin-tabs .tab { flex: 0 0 auto; padding: 8px 16px; }
.rc-sendas input[readonly] { color: var(--muted); font-variant-numeric: tabular-nums; }
.rc-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.rc-head h2 { margin: 0; }
.admin table.rc-fields td { vertical-align: middle; }
.rc-fields input, .rc-fields select {
  width: 100%; max-width: 260px; padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--input); color: var(--text); font: inherit;
}
.rc-fields tr.changed input, .rc-fields tr.changed select { border-color: var(--accent); }
.rc-fields .reboot { color: var(--warn); font-size: 12px; }
.rc-unlock { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.rc-unlock .field { margin: 0; }
.rc-unlock span { padding-bottom: 7px; }
.admin tr.selected td { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.btn:disabled { opacity: .55; cursor: default; }
.warnmsg { color: var(--warn); }

/* accounts: login, sign-up, approvals */
.head-user { display: inline-flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.admin a { color: var(--accent); }
.card.narrow { max-width: 420px; }
.card h2:first-child { margin-top: 0; }
.banner { border: 1px solid var(--warn); border-radius: 8px; padding: 10px 14px; margin: 14px 0 0;
  background: color-mix(in srgb, var(--warn) 10%, transparent); }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--bad);
  color: #fff; font-size: 11px; line-height: 18px; text-align: center; }
.hp { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }  /* sign-up honeypot */
fieldset.device { border: 1px solid var(--border); border-radius: 8px; margin: 0 0 12px; padding: 4px 12px 8px; min-width: 0; }
fieldset.device legend { font-weight: 600; padding: 0 4px; }
#my-fields fieldset.device { border: 0; padding: 0; margin: 0; }
.linkbtn { background: none; border: 0; padding: 0 0 0 6px; color: var(--bad); font: inherit; font-weight: 400;
  font-size: 12px; cursor: pointer; text-decoration: underline; }
.form-end { margin-top: 14px; }
.appr { border-bottom: 1px solid var(--border); padding: 10px 0; }
.appr:last-child { border-bottom: 0; }
.appr-list { list-style: none; margin: 8px 0; padding: 0; }
.appr-list li { padding: 6px 0; border-top: 1px dashed var(--border); }
.appr-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 6px; }
.appr-actions input.note { flex: 1 1 180px; max-width: 320px; padding: 5px 7px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--input); color: var(--text); font: inherit; }

/* logs tab */
.log-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; margin-bottom: 8px; }
.log-toolbar label { display: inline-flex; align-items: center; gap: 5px; }
.log-toolbar input[type=checkbox] { accent-color: var(--accent); margin: 0; }
.log-toolbar select, .log-toolbar input[type=search] {
  padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--input); color: var(--text); font: inherit;
}
.log-toolbar input[type=search] { flex: 1 1 220px; min-width: 0; }
.log-toolbar input.invalid { border-color: var(--bad); }
.log-view {
  height: 62vh; min-height: 260px; overflow: auto; padding: 4px 0;
  border: 1px solid var(--border); border-radius: 6px; background: var(--input);
  font: 12px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace;
}
.log-view .ll { white-space: pre; padding: 0 8px; }
.log-view.wrap .ll { white-space: pre-wrap; word-break: break-all; }
.log-view .ll:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.log-view .ll-empty { padding: 10px; font-family: system-ui, sans-serif; }
.ll .lt { color: var(--muted); }
.ll .lv { display: inline-block; min-width: 4ch; font-weight: 600; }
.ll .ltag { color: var(--accent); }
.ll .ln { color: var(--muted); font-style: italic; }
.ll.lv-rx .lv { color: var(--ok); }
.ll.lv-tx .lv { color: var(--accent); }
.ll.lv-srv { color: var(--muted); }
.ll.lv-sys .lv, .ll.lv-warn .lv { color: var(--warn); }
.ll.lv-err { color: var(--bad); }
.log-view mark { background: color-mix(in srgb, var(--warn) 40%, transparent); color: inherit; border-radius: 2px; }
.ok-text { color: var(--ok); }
