:root {
  --brand: #27abf3;
  --brand-strong: #0f8fd6;
  --brand-soft: rgba(39, 171, 243, .16);
  --bg: #0b1520;
  --panel: #101e2c;
  --panel-2: #152637;
  --line: #22384d;
  --text: #e6eef5;
  --muted: #8ea3b6;
  --ok: #3ecf8e;
  --warn: #f5b942;
  --bad: #ff5d5d;
  --topbar-h: 56px;
  --sidebar-w: 248px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  overflow: hidden;
}

/* ---------- top bar ---------- */
.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 16px;
  padding: 0 16px;
  background: linear-gradient(180deg, #0e1d2c, #0b1724);
  border-bottom: 1px solid var(--line);
  position: relative; z-index: 20;
}
.topbar::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px;
  background: linear-gradient(90deg, var(--brand), transparent 60%);
}
.brand img { height: 26px; display: block; }
.site { display: flex; flex-direction: column; min-width: 0; line-height: 1.15; }
.site-name { font-weight: 600; letter-spacing: .2px; }
.page-title { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status {
  margin-left: auto; display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line);
  font-size: 12px; color: var(--muted); white-space: nowrap;
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status[data-state="ok"] .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(62, 207, 142, .2); }
.status[data-state="stale"] .dot { background: var(--warn); }
.status[data-state="down"] .dot { background: var(--bad); }
.status[data-state="demo"] .dot { background: var(--brand); }
.user { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.user[hidden] { display: none; }
.user-name { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.logout {
  font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 999px; padding: 5px 12px; cursor: pointer;
}
.logout:hover { border-color: var(--brand); }
.logout[hidden] { display: none; }
body.viewer .w-cmd, body.viewer .w-sp, body.viewer .w-spval, body.viewer .w-select, body.viewer .w-toggle { cursor: not-allowed; }

/* ---------- login ---------- */
.login-page { display: grid; place-items: center; min-height: 100%; overflow: auto; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 28px 24px 22px; box-shadow: 0 20px 50px rgba(0, 0, 0, .45); position: relative; overflow: hidden;
}
.login-card::before { content: ""; position: absolute; left: 0; right: 0; top: 0; height: 3px; background: linear-gradient(90deg, var(--brand), transparent 70%); }
.login-card img { height: 30px; display: block; margin-bottom: 18px; }
.login-card h1 { font-size: 18px; margin: 0 0 2px; }
.login-card .sub { color: var(--muted); font-size: 13px; margin: 0 0 20px; }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin: 0 0 6px; }
.login-card input {
  width: 100%; padding: 11px 12px; border-radius: 9px; border: 1px solid var(--line); background: #07111a;
  color: var(--text); font: inherit; font-size: 15px; margin-bottom: 14px;
}
.login-card input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.login-card input.code { letter-spacing: 8px; font-size: 22px; text-align: center; }
.login-card .primary {
  width: 100%; padding: 11px; border-radius: 9px; border: 0; background: var(--brand); color: #04121d;
  font: inherit; font-weight: 600; font-size: 15px; cursor: pointer;
}
.login-card .primary:disabled { opacity: .7; cursor: progress; }
.login-card .link { background: none; border: 0; color: var(--brand); font: inherit; font-size: 13px; cursor: pointer; padding: 0; margin-top: 14px; }
.login-card .msg { min-height: 18px; font-size: 12.5px; margin-top: 10px; color: var(--muted); }
.login-card .msg.ok { color: var(--ok); }
.login-card .msg.bad { color: var(--bad); }
.login-card .foot { margin-top: 18px; font-size: 11.5px; color: var(--muted); }

.menu-toggle {
  display: none; background: none; border: 0; color: var(--text); padding: 6px; cursor: pointer; border-radius: 6px;
}

/* ---------- layout ---------- */
.layout { display: flex; height: calc(100% - var(--topbar-h)); }
.sidebar {
  width: var(--sidebar-w); flex: none; overflow-y: auto;
  background: var(--panel); border-right: 1px solid var(--line);
  padding: 12px 8px 24px;
}
.nav-group { margin-bottom: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 8px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none; font-size: 13.5px;
  border: 0; background: none; text-align: left; cursor: pointer;
}
.nav-item:hover { background: var(--panel-2); }
.nav-item[aria-current="page"] { background: var(--brand-soft); color: #fff; box-shadow: inset 3px 0 0 var(--brand); }
.nav-head { font-weight: 600; }
.nav-head.static { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .6px; cursor: default; padding-top: 14px; }
.nav-head.static:hover { background: none; }
.nav-children { margin: 2px 0 6px 12px; padding-left: 8px; border-left: 1px solid var(--line); }
.nav-children .nav-item { font-size: 13px; color: #c9d6e2; padding: 6px 10px; }

.main { flex: 1; min-width: 0; overflow: auto; background: #05090e; }
.viewport { position: relative; margin: 0 auto; }

/* ---------- graphic stage ---------- */
.stage {
  position: absolute; left: 0; top: 0; transform-origin: 0 0;
  background: #000; overflow: hidden;
  font-family: Arial, sans-serif;
}
.stage[hidden] { display: none; }
.w { position: absolute; overflow: hidden; }
.w-img, .w-multi img { display: block; width: 100%; height: 100%; }
.w-multi img { object-fit: fill; }
.w-text, .w-val { white-space: nowrap; line-height: normal; }
.w-val { transition: color .2s, background-color .2s; }
.w-val.nodata { opacity: .55; }
.w.stale { opacity: .6; filter: saturate(.4); }   /* last known value, device not answering */
.w-val.changed { animation: flash .6s ease-out; }
@keyframes flash { from { text-shadow: 0 0 6px var(--brand); } to { text-shadow: none; } }

.w-link { cursor: pointer; }
.w-link.button {
  display: flex; align-items: center; justify-content: center;
  border-radius: 4px; border: 1px solid #8fa5b8;
  background: linear-gradient(180deg, #f3f6f9, #d9e1e8);
  color: #0b1520; white-space: nowrap; text-overflow: ellipsis;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.w-link.button:hover { border-color: var(--brand); box-shadow: 0 0 0 2px rgba(39, 171, 243, .45); background: #fff; }
.w-link.text:hover { text-decoration: underline; }
.w-link.hotspot { border-radius: 4px; }
.w-link.hotspot:hover { box-shadow: inset 0 0 0 2px var(--brand); background: rgba(39, 171, 243, .08); }
.w-link.button.off { cursor: not-allowed; opacity: .55; }
.w-link.button.off:hover { box-shadow: none; border-color: #8fa5b8; background: linear-gradient(180deg, #f3f6f9, #d9e1e8); }
.w-link.text.off { cursor: default; }
.w-link.text.off:hover { text-decoration: none; }

.w-sp {
  display: flex; flex-direction: column; align-items: stretch; overflow: visible;
  border: 1px solid #5c7286; border-radius: 6px; background: #0d1824;
}
.sp-btn {
  flex: 1; border: 0; background: transparent; color: var(--brand); cursor: pointer;
  font-size: 13px; line-height: 1; padding: 0;
}
.sp-btn:hover { background: var(--brand-soft); color: #fff; }
.sp-val {
  flex: 1.2; display: flex; align-items: center; justify-content: center;
  font: 700 13px/1 Arial, sans-serif; color: #fff; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.w-sp .sub {
  position: absolute; left: -20px; right: -20px; top: 100%; margin-top: 2px;
  font: 600 10px/1.2 system-ui, sans-serif; text-align: center; color: var(--brand); text-shadow: 0 1px 2px #000;
}
.w-spval, .w-select {
  display: flex; align-items: center; justify-content: center; gap: 6px; overflow: visible;
  border: 1px dashed rgba(39, 171, 243, .55); border-radius: 4px; cursor: pointer;
  background: rgba(13, 24, 36, .55); padding: 0 6px;
}
.w-spval:hover, .w-select:hover { border-style: solid; border-color: var(--brand); background: rgba(39, 171, 243, .15); }
.w-select { justify-content: space-between; }
.w-select .caret { color: var(--brand); font-size: .9em; }
.w-toggle { display: flex; overflow: visible; border: 1px solid #8fa5b8; border-radius: 5px; background: #d9e1e8; }
.w-toggle .seg {
  flex: 1; border: 0; background: transparent; font: inherit; color: inherit; cursor: pointer; padding: 0 6px;
}
.w-toggle .seg + .seg { border-left: 1px solid #8fa5b8; }
.w-toggle .seg.on { background: var(--brand); color: #04121d; }
.w-toggle .seg:not(.on):hover { background: #fff; }
.w-spval .sub, .w-select .sub, .w-toggle .sub {
  position: absolute; left: 0; right: 0; top: 100%; margin-top: 2px;
  font: 600 10px/1.2 system-ui, sans-serif; text-align: center; color: var(--brand); text-shadow: 0 1px 2px #000;
}
.w-spval[data-state="done"] .sub, .w-select[data-state="done"] .sub, .w-toggle[data-state="done"] .sub { color: var(--ok); }
.w-spval[data-state="error"] .sub, .w-select[data-state="error"] .sub, .w-toggle[data-state="error"] .sub { color: var(--bad); }
.w-sp[data-state="pending"] { border-color: var(--brand); }
.w-sp[data-state="pending"] .sp-val { color: var(--brand); }
.w-sp[data-state="done"] .sub { color: var(--ok); }
.w-sp[data-state="error"] .sub { color: var(--bad); }

.w-cmd {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  border-radius: 4px; border: 1px solid #8fa5b8; cursor: pointer;
  background: linear-gradient(180deg, #f3f6f9, #d9e1e8); color: #0b1520;
  overflow: visible;
}
.w-cmd:hover { border-color: var(--brand); box-shadow: 0 0 0 2px rgba(39, 171, 243, .45); }
.w-cmd.disabled { cursor: not-allowed; opacity: .5; box-shadow: none; }
.w-cmd .sub {
  position: absolute; left: 0; right: 0; top: 100%; margin-top: 2px;
  font: 600 10px/1.2 system-ui, sans-serif; text-align: center; letter-spacing: .3px;
  color: var(--brand); pointer-events: none; text-shadow: 0 1px 2px #000;
}
.w-cmd[data-state="pending"] { border-color: var(--brand); background: linear-gradient(180deg, #e7f5fe, #c9e9fb); }
.w-cmd[data-state="pending"] .sub::before {
  content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 4px; vertical-align: 0;
  border-radius: 50%; border: 1.5px solid var(--brand); border-right-color: transparent; animation: spin .7s linear infinite;
}
.w-cmd[data-state="done"] .sub { color: var(--ok); }
.w-cmd[data-state="error"] .sub { color: var(--bad); }
@keyframes spin { to { transform: rotate(360deg); } }

.w-gauge svg { width: 100%; height: 100%; display: block; overflow: visible; }

/* ---------- override popover ---------- */
.popover {
  position: fixed; z-index: 50; min-width: 220px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .5); padding: 12px;
}
.popover h3 { margin: 0 0 2px; font-size: 13px; }
.popover p { margin: 0 0 10px; font-size: 11.5px; color: var(--muted); }
.popover .opts { display: grid; gap: 6px; }
.popover button {
  font: inherit; padding: 8px 10px; border-radius: 7px; cursor: pointer; text-align: left;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
}
.popover button:hover { border-color: var(--brand); background: var(--brand-soft); }
.popover button.primary { background: var(--brand); border-color: var(--brand); color: #04121d; font-weight: 600; }
.popover input {
  width: 100%; padding: 8px 10px; border-radius: 7px; border: 1px solid var(--line);
  background: #07111a; color: var(--text); font: inherit; margin-bottom: 6px;
}

.empty { padding: 48px 24px; color: var(--muted); text-align: center; }

/* ---------- small screens ---------- */
@media (max-width: 860px) {
  .menu-toggle { display: block; }
  .sidebar {
    position: fixed; top: var(--topbar-h); bottom: 0; left: 0; z-index: 30;
    transform: translateX(-100%); transition: transform .18s ease;
    box-shadow: 8px 0 24px rgba(0, 0, 0, .4);
  }
  body.nav-open .sidebar { transform: none; }
  .status .status-text { display: none; }
  .user-name { display: none; }
  .brand img { height: 20px; }
  .topbar { gap: 10px; padding: 0 10px; }
}
