:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e1e4e8;
  --text: #1d2228;
  --muted: #667085;
  --accent: #2f6fde;
  --accent-contrast: #ffffff;
  --ok: #1f9d55;
  --warn: #c27c0e;
  --err: #d03b3b;
  --on: #f5b301;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 10%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #1a1e24;
    --surface-2: #232830;
    --border: #2e343d;
    --text: #e6e9ee;
    --muted: #98a2b3;
    --accent: #5b8ff0;
    --accent-contrast: #0b1220;
    --ok: #3cc47c;
    --warn: #e0a13a;
    --err: #f06b6b;
    --on: #ffcc33;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--accent); }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; }
.brand svg { width: 22px; height: 22px; fill: var(--accent); }
.tabs { display: flex; gap: 4px; }
.tabs a {
  padding: 6px 12px; border-radius: 8px; text-decoration: none; color: var(--muted); font-weight: 550;
}
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a.active { background: var(--surface-2); color: var(--text); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
  .topbar-right { margin-left: auto; }
  .tabs { order: 3; width: 100%; }
}

main { max-width: 1280px; margin: 0 auto; padding: 20px 16px 60px; }

/* ---------- generic ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.ok { color: var(--ok); }
.pill.warn { color: var(--warn); }
.pill.err { color: var(--err); }

.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 14px; border-radius: 8px;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--err); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; }

input[type=text], input[type=password], input[type=number], input[type=search], input[type=email], input[type=url], select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
input[type=range] { accent-color: var(--accent); width: 100%; }

.page-head { display: flex; align-items: center; gap: 12px; margin: 4px 0 18px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-head .spacer { flex: 1; }
.page-head input[type=search] { width: min(320px, 100%); }
.muted { color: var(--muted); }
.empty {
  padding: 40px 20px; text-align: center; color: var(--muted);
  border: 1px dashed var(--border); border-radius: var(--radius);
}

/* ---------- devices ---------- */
.area { margin-bottom: 28px; }
.area h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 10px; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px;
}
.card h3 { margin: 0; font-size: 15px; }
.card .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.entities { margin-top: 10px; display: flex; flex-direction: column; gap: 10px; }
.entity { display: flex; flex-direction: column; gap: 6px; padding-top: 10px; border-top: 1px solid var(--border); }
.entity:first-child { border-top: 0; padding-top: 0; }
.entity-head { display: flex; align-items: center; gap: 10px; }
.entity-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entity-state { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.entity.unavailable .entity-name { color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--border); flex: none; }
.dot.on { background: var(--on); box-shadow: 0 0 6px var(--on); }
.readings { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--muted); font-size: 12px; }
.controls { display: flex; flex-direction: column; gap: 6px; }
.ctl { display: grid; grid-template-columns: 96px 1fr auto; align-items: center; gap: 8px; font-size: 13px; }
.ctl > label { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctl .val { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 48px; text-align: right; }
.ctl select, .ctl input[type=text], .ctl input[type=password] { width: 100%; }
.ctl .pair { display: flex; gap: 6px; }
.ctl .pair input { width: 72px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
details.secondary summary { cursor: pointer; color: var(--muted); font-size: 12px; margin-top: 10px; }
details.secondary .entities { margin-top: 8px; }

/* toggle switch */
.switch { position: relative; width: 40px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span {
  position: absolute; inset: 0; border-radius: 999px; background: var(--border); transition: background .15s; cursor: pointer;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgb(0 0 0 / 30%);
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:disabled + span { opacity: .5; cursor: default; }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- integrations ---------- */
.section { margin-bottom: 28px; }
.section h2 { font-size: 16px; margin: 0 0 10px; }
.list { display: flex; flex-direction: column; gap: 8px; }
.row {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px;
}
.row .main { flex: 1; min-width: 180px; }
.row .title { font-weight: 600; }
.row .sub { color: var(--muted); font-size: 12px; }
.row .buttons { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- dialogs ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgb(0 0 0 / 30%);
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dlg-head { display: flex; align-items: center; gap: 10px; padding: 16px 18px 8px; }
.dlg-head h2 { margin: 0; font-size: 18px; flex: 1; }
.dlg-body { padding: 6px 18px 12px; max-height: 65vh; overflow: auto; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 16px; border-top: 1px solid var(--border); }
.dlg-body .desc { margin: 0 0 12px; white-space: pre-line; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > label { font-weight: 600; font-size: 13px; }
.field .help { color: var(--muted); font-size: 12px; }
.field .error { color: var(--err); font-size: 12px; }
.field.inline { flex-direction: row; align-items: center; gap: 10px; }
.field.inline > label { font-weight: 500; }
.field input, .field select, .field textarea { width: 100%; }
.field .checks { display: flex; flex-direction: column; gap: 4px; }
fieldset.sect { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 0; margin: 0 0 12px; }
fieldset.sect legend { font-weight: 600; font-size: 13px; padding: 0 4px; }
.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; background: var(--surface-2); }
.alert.err { color: var(--err); }
.alert.warn { color: var(--warn); }
.menu-options { display: flex; flex-direction: column; gap: 8px; }
.menu-options .btn { text-align: left; }
.int-list { display: flex; flex-direction: column; max-height: 50vh; overflow: auto; margin-top: 10px; }
.int-item {
  display: flex; justify-content: space-between; gap: 10px; padding: 9px 10px; border-radius: 8px;
  cursor: pointer; border: 0; background: none; font: inherit; color: inherit; text-align: left;
}
.int-item:hover, .int-item:focus-visible { background: var(--surface-2); }
.int-item .brand-name { color: var(--muted); font-size: 12px; }
.progress { height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin-top: 10px; }
.progress > div { height: 100%; background: var(--accent); transition: width .3s; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 1s linear infinite; margin: 8px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- runtime ---------- */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.logs {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding: 10px 12px; height: 55vh; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; margin: 0;
}
.logs .l-installer, .logs .l-manager { color: var(--accent); }
.logs .l-service { color: var(--muted); }
.logs .l-err { color: var(--err); }
.logs .l-warn { color: var(--warn); }

/* ---------- sign-in ---------- */
.small { font-size: 12px; }
.login .buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.login { max-width: 380px; margin: 12vh auto; }
.login h1 { font-size: 22px; margin: 0 0 4px; }
.login form { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }

/* ---------- toasts ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: calc(100vw - 32px); }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 8px; padding: 10px 14px; box-shadow: var(--shadow); max-width: 420px;
}
.toast.err { border-left-color: var(--err); }
.toast.ok { border-left-color: var(--ok); }
