:root {
  --bg: #f5f6f8; --panel: #ffffff; --ink: #1b1f24; --muted: #5d6673; --line: #dfe3e8; --soft: #eef1f4;
  --accent: #2463eb; --ok: #1a8f4c; --ok-bg: #e7f6ed; --warn: #b26b00; --warn-bg: #fff4dc;
  --crit: #c62828; --crit-bg: #fdeaea; --ext: #7b1fa2; --ext-bg: #f6e8fb; --off: #6b7280; --off-bg: #eceef1;
  --band: rgba(26, 143, 76, .12);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 15px;
  color: var(--ink); background: var(--bg);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418; --panel: #1a1e24; --ink: #e6e9ed; --muted: #9aa4b1; --line: #2c323a; --soft: #232830;
    --accent: #6b9bff; --ok: #4cc27f; --ok-bg: #16291e; --warn: #f0b049; --warn-bg: #2e2412;
    --crit: #ff6b6b; --crit-bg: #331718; --ext: #d48cf0; --ext-bg: #2b1833; --off: #9aa4b1; --off-bg: #23272d;
    --band: rgba(76, 194, 127, .14);
  }
}
* { box-sizing: border-box; }
body { margin: 0; }
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 10px; }
h3 { font-size: 14px; margin: 0 0 8px; }
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }
input, select { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; }
input[type=number] { width: 80px; }
button, a.button { background: var(--soft); border: 1px solid var(--line); border-radius: 6px; padding: 5px 12px; cursor: pointer; }
a.button { display: inline-block; color: var(--ink); text-decoration: none; }
button:hover, a.button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.grow { flex: 1; min-width: 200px; }
button.danger { color: var(--crit); }
button:disabled { opacity: .5; cursor: default; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.top { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { display: flex; gap: 10px; align-items: center; }
.logo { font-size: 26px; }
.health { font-size: 13px; color: var(--muted); }
.health .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); margin-right: 5px; }
.health .dot.bad { background: var(--crit); }
.me { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.me input { width: 200px; }
.me input.need { border-color: var(--crit); box-shadow: 0 0 0 2px var(--crit-bg); }
.tabs { display: flex; gap: 4px; padding: 0 20px; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs a { padding: 10px 12px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.badge { background: var(--crit); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; margin-left: 4px; }
.badge.quiet { background: var(--off-bg); color: var(--off); }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--band); }
.banner { margin: 12px 20px 0; padding: 10px 14px; border-radius: 8px; background: var(--crit-bg); color: var(--crit); font-weight: 600; }
main { padding: 16px 20px 60px; max-width: 1400px; }

.summary { display: flex; gap: 8px; flex-wrap: wrap-reverse; margin-bottom: 14px; align-items: center; justify-content: space-between; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { padding: 4px 10px; border-radius: 14px; background: var(--panel); border: 1px solid var(--line); font-size: 13px; cursor: pointer; }
.chip.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.chip b { margin-right: 4px; }
.summary input { width: 240px; }
.searchbar { display: flex; gap: 8px; align-items: center; }
.searchbar button { white-space: nowrap; }

.site { margin-bottom: 20px; }
.site h2 { display: flex; gap: 8px; align-items: baseline; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.tile { display: flex; flex-direction: column; justify-content: flex-start; background: var(--panel); border: 1px solid var(--line); border-left: 5px solid var(--ok); border-radius: 8px; padding: 10px 12px; cursor: pointer; text-align: left; width: 100%; }
.tile:hover { border-color: var(--muted); }
.tile .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .row { display: flex; justify-content: space-between; align-items: flex-end; gap: 6px; margin-top: 4px; }
.tile .temp { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; }
.tile .temp small { font-size: 13px; font-weight: 400; color: var(--muted); }
.tile .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.tile .why { font-size: 12px; margin-top: 4px; }
.lv-ok { border-left-color: var(--ok); }
.lv-warning { border-left-color: var(--warn); background: var(--warn-bg); }
.lv-critical { border-left-color: var(--crit); background: var(--crit-bg); }
.lv-extreme { border-left-color: var(--ext); background: var(--ext-bg); }
.lv-offline { border-left-color: var(--off); background: var(--off-bg); }
.lv-pending { border-left-color: var(--warn); }
.pill.pending { background: var(--warn-bg); color: var(--warn); }
.lv-off { opacity: .55; }
.pill { display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; padding: 1px 7px; border-radius: 9px; background: var(--soft); color: var(--muted); }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warning { background: var(--warn-bg); color: var(--warn); }
.pill.critical { background: var(--crit-bg); color: var(--crit); }
.pill.extreme { background: var(--ext-bg); color: var(--ext); }
.pill.offline { background: var(--off-bg); color: var(--off); }
.spark { display: block; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; margin-bottom: 14px; }
.card header { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.cols { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 14px; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.fields { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 10px; }
.fields label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.plain { font-size: 12px; color: var(--muted); }
.actions { display: flex; gap: 8px; margin-top: 10px; align-items: center; flex-wrap: wrap; }
.dirty-note { font-size: 12px; color: var(--warn); }

.alert { border-left: 5px solid var(--crit); }
.alert.lv-1 { border-left-color: var(--warn); }
.alert.lv-3 { border-left-color: var(--ext); }
.alert.done { border-left-color: var(--ok); opacity: .85; }
.timeline { margin: 8px 0 0; padding: 0; list-style: none; font-size: 13px; }
.timeline li { padding: 3px 0; border-top: 1px dashed var(--line); display: grid; grid-template-columns: 150px 120px 1fr; gap: 8px; }
.timeline li span:first-child { color: var(--muted); }
details summary { cursor: pointer; color: var(--accent); font-size: 13px; }

dialog.detail { width: min(960px, 96vw); border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); padding: 18px 20px; }
dialog::backdrop { background: rgba(0,0,0,.45); }
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 11px; }
.stats { display: flex; gap: 20px; flex-wrap: wrap; margin: 8px 0; font-size: 13px; }
.stats b { font-size: 18px; display: block; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 16px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 90vw; }
.toast.show { opacity: 1; }
.toast.err { background: var(--crit); color: #fff; }
.empty { padding: 30px; text-align: center; color: var(--muted); }
@media (max-width: 600px) {
  .cols { grid-template-columns: 1fr; }
  .summary input { width: 100%; }
  .searchbar { width: 100%; }
  .searchbar input { flex: 1; min-width: 0; }
  .timeline li { grid-template-columns: 1fr; }
  main { padding: 12px; }
}
td.plain { min-width: 130px; }
.cols { grid-template-columns: repeat(auto-fill, minmax(min(380px, 100%), 1fr)); }
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card h1 { font-size: 20px; }
.login-err { color: var(--crit); font-size: 13px; min-height: 1em; }
.me { gap: 10px; }

/* ---------------------------------------------------------------- sound alarm */
.sound-state { white-space: nowrap; }
.sound-state.off { color: var(--crit); border-color: var(--crit); }
.alarm-bar { position: sticky; top: 0; z-index: 20; margin: 0; padding: 10px 20px; background: var(--crit); color: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.25); }
.alarm-bar.flash { animation: alarm-flash 1s steps(2, start) infinite; }
@keyframes alarm-flash { 50% { background: #7f1010; } }
.alarm-bar .ahead { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-weight: 700; font-size: 16px; }
.alarm-bar .ahead .sp { flex: 1; }
.alarm-bar ul { list-style: none; margin: 6px 0 0; padding: 0; }
.alarm-bar li { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 4px 0; border-top: 1px solid rgba(255,255,255,.25); }
.alarm-bar li span { flex: 1; min-width: 220px; }
.alarm-bar button { background: rgba(255,255,255,.15); border-color: rgba(255,255,255,.6); color: #fff; }
.alarm-bar button.strong { background: #fff; color: var(--crit); font-weight: 700; }
.alarm-bar .need-click { background: #fff; color: #000; padding: 2px 8px; border-radius: 6px; font-size: 13px; }
.alarm-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px 20px; margin-bottom: 12px; }
.alarm-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.alarm-form label.inline { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); font-size: 14px; }
.alarm-form input[type=range] { width: 100%; padding: 0; }
.vol { display: flex; align-items: center; gap: 8px; }
.vol output { min-width: 42px; font-weight: 600; color: var(--ink); }
.pill.alarm { background: var(--crit); color: #fff; }
.tile.ringing { box-shadow: 0 0 0 3px var(--crit); }

.pwdlg { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); padding: 18px 20px; width: min(380px, 94vw); }
.pwdlg h2 { margin: 0; }

/* ---------------------------------------------------------------- phones */
@media (max-width: 700px) {
  :root { font-size: 15px; }
  /* 16px stops iOS Safari zooming in when a field is tapped */
  input, select, textarea { font-size: 16px; padding: 8px 10px; }
  input[type=number] { width: 100%; }
  button, a.button { padding: 9px 14px; min-height: 40px; }
  input[type=checkbox] { width: 22px; height: 22px; }
  .top { padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); gap: 8px; }
  .logo { font-size: 22px; }
  h1 { font-size: 16px; }
  .health { font-size: 12px; }
  .me { width: 100%; gap: 6px; }
  .me #whoami { flex: 1; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .me button { min-height: 34px; padding: 5px 10px; font-size: 13px; }
  .tabs { padding: 0 6px; position: sticky; top: 0; z-index: 5; scrollbar-width: none; }
  .tabs a { padding: 12px 10px; }
  .banner { margin: 10px 12px 0; }
  .chips { overflow-x: auto; flex-wrap: nowrap; width: 100%; padding-bottom: 2px; scrollbar-width: none; }
  .chip { white-space: nowrap; padding: 7px 12px; }

  /* two tiles across, compact */
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .tile { padding: 8px 9px; border-left-width: 4px; }
  .tile .temp { font-size: 22px; }
  .tile .spark { width: 100%; max-width: 70px; height: 22px; }
  .tile .meta { font-size: 11px; }
  .site h2 { font-size: 15px; }

  .card { padding: 12px; border-radius: 8px; }
  .fields { gap: 10px; }
  .fields label { flex: 1 1 140px; }

  /* tables become stacked cards: one row = one card, each cell "Label  value" */
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack thead { display: none; }
  table.stack tr { border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; margin-bottom: 10px; background: var(--panel); }
  table.stack td { border: 0; padding: 5px 0; display: grid; grid-template-columns: 96px 1fr; gap: 8px; align-items: center; }
  table.stack td::before { content: attr(data-label); font-size: 12px; color: var(--muted); font-weight: 600; }
  table.stack td:not([data-label])::before, table.stack td[data-label=""]::before { content: none; }
  table.stack td:not([data-label]), table.stack td[data-label=""] { grid-template-columns: 1fr; }
  table.stack td > *, table.stack td > label { grid-column: 2; justify-self: start; }
  table.stack td > input:not([type=checkbox]), table.stack td > select { justify-self: stretch; width: 100%; }
  table.stack td:not([data-label]) > *, table.stack td[data-label=""] > * { grid-column: 1; }

  /* threshold rules: level on top, then Below / Above / For side by side */
  table.rules tr { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)) auto; gap: 6px 8px; align-items: end; }
  table.rules td { display: flex; flex-direction: column; align-items: stretch; gap: 3px; padding: 0; }
  table.rules td > * { grid-column: auto; }
  table.rules td::before { font-size: 11px; }
  table.rules td:first-child { grid-column: 1 / 4; }
  table.rules td:nth-child(5) { grid-column: 1 / -1; order: 9; }
  table.rules td:nth-child(5)::before { content: none; }
  table.rules td:last-child { grid-column: 4; grid-row: 1; justify-content: end; }
  table.rules td:last-child::before { content: none; }
  .card[style*="overflow-x"] { overflow: visible !important; padding: 0; border: 0; background: none; }

  /* sensor detail fills the screen */
  dialog.detail { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; border: 0;
    padding: 14px 12px; padding-top: max(14px, env(safe-area-inset-top)); padding-bottom: max(14px, env(safe-area-inset-bottom)); }
  .chart { height: auto; }
  .stats { gap: 14px; }
  .stats > div { flex: 1 1 40%; }
  button.icon { min-width: 44px; min-height: 44px; font-size: 18px; }

  .toast { bottom: max(18px, env(safe-area-inset-bottom)); }
}

/* Threshold editors: one per row so the rule table (level, limits, minutes, meaning, remove)
   always fits; three side by side squeezed the table past the card edge. */
.editors { display: grid; gap: 14px; max-width: 1000px; }
table.rules input[type=number] { width: 90px; }
table.rules td.plain { min-width: 200px; }
@media (min-width: 701px) {
  .editors .card { overflow-x: auto; }
  .editors .fields { max-width: 560px; }
}
