:root {
  --bg: #0b1220;
  --panel: #131c2e;
  --panel2: #1b2740;
  --line: #243352;
  --ink: #e6edf6;
  --muted: #8aa0bd;
  --accent: #38bdf8;
  --good: #4ade80;
  --warn: #fbbf24;
  --bad: #f87171;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1200px 600px at 80% -10%, #16243f 0%, var(--bg) 60%);
  color: var(--ink);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif;
}
.hidden { display: none !important; }
.dot { color: var(--accent); font-size: .6em; vertical-align: middle; margin-left: 3px; }

button {
  font: inherit; cursor: pointer; border-radius: 10px; border: 1px solid var(--line);
  padding: 9px 14px; color: var(--ink); background: var(--panel2); transition: .12s;
}
button:hover { border-color: var(--accent); }
button:active { transform: translateY(1px); filter: brightness(1.2); border-color: var(--accent); }
button.primary { background: var(--accent); color: #04263a; border-color: transparent; font-weight: 600; }
button.warn { background: #3a2c10; border-color: #6b5113; color: var(--warn); }
button.ghost { background: transparent; }
button.sm { padding: 5px 10px; font-size: 13px; }
button:disabled { opacity: .4; cursor: not-allowed; }
input, select {
  font: inherit; background: #0e1626; color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; outline: none;
}
input:focus, select:focus { border-color: var(--accent); }

/* ---- login ---- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.login-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 30px; width: 340px; display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.login-card h1 { margin: 0; font-size: 26px; }
.login-card .sub { margin: -6px 0 8px; color: var(--muted); }
.login-card .row { display: flex; gap: 10px; }
.login-card .row button { flex: 1; }
.reg-only { display: none; }
.err { color: var(--bad); min-height: 18px; font-size: 13px; margin: 0; }

/* ---- topbar ---- */
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 12px 20px;
  background: rgba(11,18,32,.8); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; backdrop-filter: blur(6px); z-index: 5;
}
.brand { font-weight: 700; font-size: 18px; }
.spacer { flex: 1; }
.who { color: var(--muted); font-size: 14px; }
.pill { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--panel2); color: var(--muted); border: 1px solid var(--line); }
.pill.ok { color: var(--good); border-color: #1f4d33; }
.pill.bad { color: var(--bad); }

/* ---- layout ---- */
.layout { display: grid; grid-template-columns: 260px 1fr; min-height: calc(100vh - 53px); }
.sidebar { border-right: 1px solid var(--line); padding: 14px; }
.side-head { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 10px; }
.dev-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px; margin-bottom: 8px; cursor: pointer; transition: .12s;
}
.dev-card:hover { border-color: var(--accent); }
.dev-card.active { border-color: var(--accent); background: var(--panel2); }
.dev-card.dragging { opacity: .45; }
.dev-card.drop-to { border-color: var(--accent); border-style: dashed; }
.dev-card .name { font-weight: 600; }
.dev-card .row2 { display: flex; align-items: center; gap: 8px; margin-top: 4px; font-size: 12px; color: var(--muted); }
.sdot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.sdot.online { background: var(--good); box-shadow: 0 0 8px var(--good); }
.sdot.offline { background: var(--bad); }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: #14304a; color: var(--accent); border: 1px solid #1e4865; }
.badge.client { background: #1f3326; color: var(--good); border-color: #2c5a3c; }
.badge.sm { font-size: 10px; }

/* ---- dashboard ---- */
.dash { padding: 22px 26px; }
.empty { color: var(--muted); padding: 40px; text-align: center; }
.dev-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; }
.dev-head h2 { margin: 0; }
.meta { color: var(--muted); font-size: 13px; }
.dev-head-right { display: flex; align-items: center; gap: 10px; }
.status { font-size: 18px; color: var(--muted); }
.status.online { color: var(--good); }
.status.offline { color: var(--bad); }
.section { margin: 22px 0 12px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(135px, 1fr)); gap: 12px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.tile .lbl { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.tile .val { font-size: 30px; font-weight: 700; margin-top: 4px; }
.tile .u { font-size: 14px; color: var(--muted); font-weight: 400; margin-left: 4px; }
.tile .sub { margin-top: 6px; font-size: 12px; color: var(--muted); }
.tile .sub b { color: #f9a8d4; font-weight: 700; }

/* Filter valve (F139 head) status bar — left accent + mode colored by state. */
.valve-box { display: flex; align-items: center; gap: 14px; margin: 14px 0 2px;
  padding: 12px 16px; background: var(--panel); border: 1px solid var(--line);
  border-left: 4px solid var(--muted); border-radius: 12px; }
.valve-box .vlbl { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.valve-box .vmode { font-size: 20px; font-weight: 700; color: var(--ink); }
.valve-box.ok { border-left-color: var(--good); }
.valve-box.ok .vmode { color: var(--good); }
.valve-box.active { border-left-color: var(--warn); }
.valve-box.active .vmode { color: var(--warn); }
.valve-box.fault { border-left-color: var(--bad); }
.valve-box.fault .vmode { color: var(--bad); }
.valve-box.stale .vmode { color: var(--muted); font-weight: 600; font-size: 16px; }
.valve-box .vfault { color: var(--bad); font-size: 12px; margin-left: auto; }
.valve-box.clickable { cursor: pointer; transition: border-color .12s, background .12s; }
.valve-box.clickable:hover { border-color: var(--warn); background: var(--panel2); }
.valve-box .vhint { margin-left: auto; color: var(--muted); font-size: 12px; opacity: .7; white-space: nowrap; }
.valve-box.clickable:hover .vhint { color: var(--warn); opacity: 1; }
.valve-box .vfault + .vhint { margin-left: 12px; }
.temp-tile .temps { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.tprobe { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.tprobe i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; }
.tprobe b { color: var(--ink); font-size: 17px; margin-left: auto; }

.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.chart-box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.chart-box h4 { margin: 0 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.chart-box canvas { width: 100% !important; height: 150px !important; }

.relays { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.relay {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
}
.relay .rname { font-size: 13px; }
.relay .rch { font-size: 11px; color: var(--muted); }
.toggle { width: 46px; height: 26px; border-radius: 999px; background: #2a3a57; position: relative; cursor: pointer; transition: .15s; border: none; padding: 0; }
.toggle::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #cfd8e6; transition: .15s; }
.toggle.on { background: var(--good); }
.toggle.on::after { left: 23px; background: #07210f; }
.toggle:disabled { opacity: .5; cursor: not-allowed; }

/* ---- service ---- */
.service { margin-top: 26px; border-top: 1px dashed var(--line); padding-top: 8px; }
.svc-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.members h4 { color: var(--muted); margin: 10px 0 8px; font-size: 13px; }
.wifi-cfg, .cl-cfg { margin-top: 14px; }
.wifi-cfg h4, .cl-cfg h4 { color: var(--muted); margin: 10px 0 8px; font-size: 13px; font-weight: 600; }
.cl-en { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; color: var(--muted); }
#cl-setpoint { flex: 0 0 130px; min-width: 110px; }
#cl-status { margin-top: 8px; }
.dose-tl { margin-top: 14px; }
.dose-tl-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.dose-tl-bar { position: relative; height: 22px; background: #0e1626; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.dose-seg { position: absolute; top: 0; bottom: 0; background: var(--good); min-width: 2px; box-shadow: 0 0 6px rgba(74,222,128,.7); }
.dose-seg.running { background: var(--accent); box-shadow: 0 0 8px rgba(56,189,248,.9); }
.dose-tl-axis { display: flex; justify-content: space-between; font-size: 10px; color: var(--muted); margin-top: 3px; }
.member { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.member .me { flex: 1; }
.grant-row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.grant-row input { flex: 1; min-width: 160px; }

/* ---- modal ---- */
.modal { position: fixed; inset: 0; background: rgba(4,8,16,.7); display: grid; place-items: center; z-index: 20; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; width: min(420px, 92vw); display: flex; flex-direction: column; gap: 14px; }
.modal-card h3 { margin: 0; }
.modal-card .row { display: flex; gap: 10px; }
.modal-card input { width: 100%; }
.qr-wrap { display: grid; place-items: center; gap: 10px; }
.qr-wrap img { background: #fff; padding: 10px; border-radius: 10px; }
button.full { width: 100%; }
.qr-cam { width: 100%; max-width: 320px; aspect-ratio: 4 / 3; object-fit: cover; align-self: center;
          background: #000; border: 1px solid var(--line); border-radius: 10px; }
.scan-status { min-height: 1.1em; margin: 2px 0; }
.muted { color: var(--muted); font-size: 13px; }
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--panel2); border: 1px solid var(--line); padding: 10px 18px; border-radius: 10px; z-index: 30; opacity: 0; transition: .2s; }
.toast.show { opacity: 1; }
.toast.bad { border-color: var(--bad); color: var(--bad); }

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--line); }
}
