:root {
  --bg: #fbfbfa;
  --panel: #ffffff;
  --line: #e4e4e1;
  --ink: #1b1b19;
  --muted: #6c6c66;
  --accent: #1f6f5c;
  --accent-ink: #ffffff;
  --ok: #2f7d4f;
  --warn: #9a6216;
  --bad: #a33124;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14150f;
    --panel: #1c1e17;
    --line: #2e3128;
    --ink: #ecece4;
    --muted: #97998c;
    --accent: #6fd3ac;
    --accent-ink: #10231c;
    --ok: #6fd39a;
    --warn: #e0b063;
    --bad: #e88a7d;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ------------------------------------------------------------ chrome */

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.brand {
  font-weight: 650; letter-spacing: -0.02em; font-size: 17px;
  color: var(--ink); text-decoration: none;
}
.brand span { color: var(--muted); font-weight: 400; }

.topbar nav { display: flex; align-items: center; gap: 14px; }

main { max-width: 980px; margin: 0 auto; padding: 26px 22px 80px; }

.head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; margin-bottom: 20px;
}
.head-inline {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 14px;
}
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

h1 { font-size: 22px; margin: 0; letter-spacing: -0.02em; }
h2 { font-size: 15px; margin: 0 0 4px; letter-spacing: -0.01em; }

.back { color: var(--muted); text-decoration: none; font-size: 13px; display: block; margin-bottom: 4px; }
.back:hover { color: var(--ink); }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.empty { color: var(--muted); text-align: center; padding: 26px; font-size: 14px; }

/* ------------------------------------------------------------ cards */

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}

.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }

.device {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 15px 16px;
  text-decoration: none; color: inherit; display: block;
  transition: border-color .12s ease, transform .12s ease;
}
.device:hover { border-color: var(--accent); transform: translateY(-1px); }
.device h3 { margin: 0 0 8px; font-size: 15px; font-family: var(--mono); }

/* --------------------------------------------------------- status */

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 7px; }
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }
.dot.idle { background: var(--muted); }

.health { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 16px; }
.stat {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px;
}
.stat .k { font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
.stat .v { font-size: 19px; margin-top: 3px; font-variant-numeric: tabular-nums; }
.stat .v.bad { color: var(--bad); }
.stat .v.ok { color: var(--ok); }

/* -------------------------------------------------------- controls */

button, .ghost {
  font: inherit; cursor: pointer; border-radius: 7px; padding: 7px 13px;
  border: 1px solid var(--line); background: transparent; color: var(--ink);
  transition: border-color .12s ease, background .12s ease;
}
button:hover, .ghost:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 550; }
button.primary:hover { filter: brightness(1.07); }
button.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
a.ghost { text-decoration: none; display: inline-block; }

.ranges { display: flex; gap: 4px; }
.ranges button { padding: 4px 10px; font-size: 13px; color: var(--muted); }
.ranges button.active { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }

.tabs { display: flex; gap: 4px; margin: 18px 0 14px; }
.tabs button { flex: 1; color: var(--muted); }
.tabs button.active { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }

label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
input, select {
  display: block; width: 100%; margin-top: 5px;
  font: inherit; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 7px; padding: 8px 10px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

form button.primary { width: 100%; margin-top: 4px; padding: 9px; }

.auth { max-width: 420px; margin: 8vh auto; }
.auth h1 { font-size: 24px; margin-bottom: 8px; }
.hint { font-size: 12px; color: var(--muted); margin: -6px 0 12px; }
.error { color: var(--bad); font-size: 13px; margin: 10px 0 0; }

/* ---------------------------------------------------------- tables */

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-weight: 500; color: var(--muted); font-size: 11px;
  text-transform: uppercase; letter-spacing: .06em;
  padding: 6px 8px; border-bottom: 1px solid var(--line);
}
td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: none; }
td.mono, .mono { font-family: var(--mono); font-size: 12.5px; }

.pill {
  display: inline-block; padding: 1px 7px; border-radius: 999px;
  font-size: 11px; font-family: var(--mono);
  border: 1px solid var(--line);
}
.pill.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
.pill.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }

.source-row { display: grid; grid-template-columns: 1fr 1fr 90px 34px; gap: 8px; margin-bottom: 8px; }
.source-row input { margin-top: 0; }
.source-row button { padding: 6px; color: var(--muted); }

/* ---------------------------------------------------------- charts */

.chart { margin-bottom: 18px; }
.chart h3 { font-family: var(--mono); font-size: 13px; margin: 0 0 6px; color: var(--muted); font-weight: 500; }
.uplot { width: 100% !important; }

/* ---------------------------------------------------------- dialog */

dialog {
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--panel); color: var(--ink);
  max-width: min(720px, 92vw); padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
dialog pre {
  background: var(--bg); border: 1px solid var(--line); border-radius: 7px;
  padding: 13px; overflow-x: auto; font-family: var(--mono);
  font-size: 12px; line-height: 1.5; max-height: 46vh;
}
.warn {
  font-size: 13px; color: var(--warn);
  border-left: 2px solid var(--warn); padding-left: 10px; margin: 10px 0;
}
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }

@media (max-width: 620px) {
  .head { flex-direction: column; align-items: stretch; }
  .source-row { grid-template-columns: 1fr 1fr; }
}


/* ----------------------------------------------------------- billing */

.navlink {
  color: var(--muted); text-decoration: none; font-size: 14px;
  padding: 4px 2px; border-bottom: 2px solid transparent;
}
.navlink:hover { color: var(--ink); }
.navlink.active { color: var(--ink); border-bottom-color: var(--accent); }

/* Notices carry the things a user would otherwise email about: a declined
   card, more devices than the plan allows, readings being discarded. Each is
   phrased to say what happened AND what was not lost. */
.notice {
  margin: 0 22px 14px; padding: 11px 14px;
  border: 1px solid var(--line); border-left-width: 3px;
  border-radius: var(--radius); background: var(--panel);
  font-size: 14px; line-height: 1.5;
}
.notice + .notice { margin-top: -4px; }
.notice.warn { border-left-color: var(--warn); }
.notice.info { border-left-color: var(--accent); }
.notice.bad  { border-left-color: var(--bad); }
#b-notices .notice { margin-left: 0; margin-right: 0; }

.current { display: flex; flex-wrap: wrap; gap: 18px; justify-content: space-between; }
.plan-name { font-size: 20px; font-weight: 650; }
.current-meta { display: flex; gap: 26px; flex-wrap: wrap; }
.current-meta .stat { display: flex; flex-direction: column; gap: 2px; }

.meter { margin-bottom: 16px; }
.meter:last-child { margin-bottom: 0; }
.meter-head { display: flex; justify-content: space-between; font-size: 14px; margin-bottom: 5px; }
.meter-head .over { color: var(--bad); font-weight: 600; }
.bar { height: 7px; border-radius: 4px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }
.bar i.over { background: var(--bad); }

.plans { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(215px, 1fr)); }
.plan {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 15px; display: flex; flex-direction: column; gap: 9px;
}
.plan.is-current { border-color: var(--accent); }
.plan-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.plan-head h3 { margin: 0; font-size: 16px; }
.plan .price { font-weight: 650; white-space: nowrap; }
.plan .limits { margin: 0; padding-left: 18px; font-size: 14px; color: var(--muted); }
.plan button { margin-top: auto; }
.plan button[disabled] { opacity: .55; cursor: default; }

/* ───────────────────────── new config wizard ─────────────────────────
   Two panes side by side: their config in, ours merged out. Stacks on a
   narrow screen -- a phone cannot usefully show two code panes at once,
   and pretending otherwise gives two unreadable ones. */
#wizard-dialog { max-width: min(1180px, 96vw); }

.wiz-name { display: flex; align-items: baseline; gap: 10px; margin-bottom: 12px; }
.wiz-name label { display: flex; align-items: baseline; gap: 8px; font-size: 14px; }
.wiz-name input {
  background: var(--bg); border: 1px solid var(--line); border-radius: 7px;
  color: var(--ink); padding: 6px 9px; font-size: 14px; min-width: 220px;
}
.wiz-name .hint { font-size: 12px; color: var(--muted); }

.wiz-panes { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.wiz-panes h3 {
  font-size: 12px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .04em; color: var(--muted); margin: 0 0 6px;
  display: flex; align-items: center; gap: 8px;
}
.wiz-panes textarea,
.wiz-panes pre {
  width: 100%; box-sizing: border-box; height: 46vh; min-height: 260px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 7px;
  padding: 11px; font-family: var(--mono); font-size: 12px; line-height: 1.5;
  color: var(--ink); overflow: auto; margin: 0;
}
.wiz-panes textarea { resize: vertical; }
.wiz-panes textarea:focus { outline: none; border-color: var(--accent); }
.wiz-panes pre { max-height: none; white-space: pre; }

/* Notes are the wizard explaining what it did to the user's file -- an id it
   added, a block it replaced. Silent edits to someone's config would be
   worse than no wizard at all. */
#wiz-notes { margin-top: 10px; display: flex; flex-direction: column; gap: 5px; }
.wiz-note { font-size: 12.5px; padding-left: 10px; border-left: 2px solid var(--line); }
.wiz-note.info  { color: var(--muted); }
.wiz-note.warn  { color: var(--warn); border-left-color: var(--warn); }
.wiz-note.error { color: var(--bad); border-left-color: var(--bad); }

@media (max-width: 900px) {
  .wiz-panes { grid-template-columns: 1fr; }
  .wiz-panes textarea, .wiz-panes pre { height: 32vh; min-height: 180px; }
  .wiz-name { flex-direction: column; align-items: stretch; gap: 4px; }
}
