:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #1c2321;
  --muted: #64706c;
  --border: #dde3e1;
  --accent: #0f766e;
  --accent-soft: #d5ede9;
  --danger: #b42318;
  --off: #a3adaa;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121615;
    --surface: #1b2120;
    --text: #e6ecea;
    --muted: #93a19c;
    --border: #2c3533;
    --accent: #2dd4bf;
    --accent-soft: #163a36;
    --danger: #f97066;
    --off: #56615e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans Hebrew", "Noto Sans Arabic", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

button, input, select { font: inherit; color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  border: 0; background: none; padding: 8px 12px; border-radius: 10px; cursor: pointer; color: var(--muted);
}
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.topbar-end { margin-inline-start: auto; display: flex; align-items: center; gap: 10px; }
.topbar-end select { padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); }

.conn { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--off); }
.conn.live::before { background: #22c55e; }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.toolbar h1 { font-size: 20px; margin: 0; }

.btn {
  border: 1px solid var(--border); background: var(--surface); padding: 8px 14px; border-radius: 10px; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #062a26; } }
.icon-btn { border: 0; background: none; cursor: pointer; padding: 4px 6px; border-radius: 8px; color: var(--muted); }
.icon-btn:hover { background: var(--bg); }

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

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px;
}
.card.offline { opacity: 0.6; }
.card-head { display: flex; align-items: center; gap: 8px; }
.card-head h3 { margin: 0; font-size: 16px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta { display: flex; flex-direction: column; color: var(--muted); font-size: 12.5px; margin: 4px 0 10px; }

.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dot.on { background: #22c55e; box-shadow: 0 0 0 3px #22c55e33; }
.dot.off { background: var(--danger); }

.entities { display: grid; gap: 6px; }
.entity {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 10px; border-radius: 10px; background: var(--bg); border: 0; text-align: start; width: 100%;
}
.entity.clickable { cursor: pointer; }
.entity.clickable:hover { outline: 1px solid var(--border); }
.entity .label { color: var(--muted); }
.entity .value { font-weight: 650; font-size: 17px; font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }
.entity .value small { font-weight: 400; color: var(--muted); font-size: 13px; }

.switch {
  position: relative; display: inline-flex; align-items: center; gap: 6px;
  min-width: 78px; height: 32px; padding-inline: 36px 10px; border-radius: 16px;
  border: 0; background: var(--off); color: #fff; cursor: pointer; font-size: 13px; font-weight: 600;
  transition: background 0.15s;
}
.switch .knob {
  position: absolute; inset-inline-start: 4px; top: 4px; width: 24px; height: 24px; border-radius: 50%; background: #fff;
  transition: inset-inline-start 0.15s;
}
.switch.on { background: var(--accent); padding-inline: 10px 36px; }
.switch.on .knob { inset-inline-start: calc(100% - 28px); }
@media (prefers-color-scheme: dark) { .switch.on { color: #062a26; } }
.switch:disabled { cursor: not-allowed; opacity: 0.6; }
.switch.small { min-width: 44px; height: 24px; padding: 0; }
.switch.small .knob { width: 18px; height: 18px; top: 3px; inset-inline-start: 3px; }
.switch.small.on .knob { inset-inline-start: 23px; }

.rule { display: flex; flex-direction: column; gap: 2px; margin-top: 8px; color: var(--muted); font-size: 14px; }
.rule b { color: var(--text); font-weight: 600; }

.empty { color: var(--muted); text-align: center; padding: 40px 16px; }
.error { color: var(--danger); }

dialog {
  width: min(680px, calc(100vw - 32px)); border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); padding: 16px;
}
dialog::backdrop { background: #0008; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.dialog-head h2 { margin: 0; font-size: 17px; }

.ranges { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.ranges button { border: 1px solid var(--border); background: none; border-radius: 8px; padding: 4px 12px; cursor: pointer; }
.ranges button.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

.chart svg { width: 100%; height: auto; direction: ltr; touch-action: none; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.chart .area { fill: var(--accent); opacity: 0.12; }
.chart .marker { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .tip { fill: var(--text); font-size: 12px; font-weight: 600; paint-order: stroke; stroke: var(--surface); stroke-width: 4px; }

form label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 13px; color: var(--muted); }
form label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
form input:not([type="checkbox"]), form select {
  padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
fieldset { border: 1px solid var(--border); border-radius: 10px; margin: 0 0 12px; padding: 10px 12px 2px; }
legend { font-weight: 650; padding: 0 6px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; }

@media (max-width: 560px) {
  .topbar { gap: 8px; }
  .topbar-end { margin-inline-start: 0; width: 100%; justify-content: space-between; }
  .grid { grid-template-columns: 1fr; }
}

.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-box { width: min(380px, 100%); }
.login-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.login-head .brand { font-size: 20px; }
.login-head select { padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); }
.login-box h1 { font-size: 20px; margin: 0 0 12px; }
.login-box .card { display: flex; flex-direction: column; padding: 18px; }
.login-box small { color: var(--muted); font-size: 12px; }
.code-input { font-size: 22px; letter-spacing: 0.25em; text-align: center; font-variant-numeric: tabular-nums; }
.btn.wide { width: 100%; padding: 11px; font-weight: 600; }
