:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #efeeeb;
  --border: #dddcd7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #8a8984;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --grid: #e7e6e1;
  --good: #1a8a3a;
  --warn: #b77700;
  --crit: #d23b3a;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #33332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #8b8a82;
    --accent: #3987e5;
    --series-1: #3987e5;
    --grid: #2a2a27;
    --good: #3fb760;
    --warn: #e0a020;
    --crit: #e66767;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 .6em; font-weight: 600; }
h1 { font-size: 22px; } h2 { font-size: 18px; } h3 { font-size: 15px; }
code, .mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }
pre { background: var(--surface-2); padding: 12px; border-radius: 8px; overflow: auto; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 0 20px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; color: var(--text); }
.brand .dot { width: 10px; height: 10px; border-radius: 3px; background: var(--accent); }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { padding: 6px 12px; border-radius: 6px; color: var(--text-2); }
.topbar nav a.active, .topbar nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.spacer { flex: 1; }
.bell { position: relative; cursor: pointer; padding: 6px 10px; border-radius: 6px; color: var(--text-2); }
.bell:hover { background: var(--surface-2); }
.badge { position: absolute; top: 0; right: 0; background: var(--crit); color: #fff; border-radius: 9px;
  font-size: 10.5px; padding: 1px 5px; min-width: 16px; text-align: center; }
main { padding: 24px; max-width: 1400px; margin: 0 auto; }

/* ---------- controls ---------- */
button, .btn {
  font: inherit; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 14px; border-radius: 7px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--crit); }
button.small { padding: 4px 9px; font-size: 12.5px; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 7px; padding: 7px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 12.5px; color: var(--text-2); margin: 10px 0 4px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.row > .grow { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.err { color: var(--crit); font-size: 13px; min-height: 1.2em; margin-top: 8px; }
.muted { color: var(--muted); }
.text2 { color: var(--text-2); }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.device-card { cursor: pointer; transition: border-color .15s; }
.device-card:hover { border-color: var(--accent); }
.device-card .pins { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; margin-top: 12px; }
.device-card .pin-v { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-2); }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.on::before { background: var(--good); }
.status.off::before { background: var(--crit); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-weight: 500; color: var(--text-2); font-size: 12.5px; }
td.num { font-variant-numeric: tabular-nums; }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin: 8px 0 18px; }
.tabs a { padding: 9px 14px; color: var(--text-2); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); text-decoration: none; }
.tabs a:hover { text-decoration: none; color: var(--text); }

/* ---------- auth ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; }
.auth-card h1 { display: flex; align-items: center; gap: 10px; }

/* ---------- modal & toast ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--surface); border-radius: 12px; padding: 22px; width: 100%; max-width: 520px; max-height: 90vh; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent); padding: 10px 14px;
  border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; animation: slidein .2s ease-out; }
.toast.warning { border-left-color: var(--warn); }
.toast.critical { border-left-color: var(--crit); }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } }
.lvl { font-size: 11.5px; font-weight: 600; padding: 2px 7px; border-radius: 5px; background: var(--surface-2); }
.lvl.warning { color: var(--warn); } .lvl.critical { color: var(--crit); } .lvl.info { color: var(--accent); }

/* ---------- dashboard grid ---------- */
.dash-toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.dash {
  --row-h: 78px; --gap: 12px;
  display: grid; grid-template-columns: repeat(12, 1fr); grid-auto-rows: var(--row-h); gap: var(--gap);
  position: relative; min-height: 200px;
}
.dash.editing { background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: calc((100% + var(--gap)) / 12) calc(var(--row-h) + var(--gap)); border-radius: 8px; outline: 1px dashed var(--border); }
.widget { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; overflow: hidden; }
.widget .wh { display: flex; align-items: center; gap: 6px; padding: 8px 12px 0; font-size: 12.5px; color: var(--text-2); min-height: 26px; }
.widget .wh .hdr-extra { display: flex; align-items: center; white-space: nowrap; flex-shrink: 0; }
.widget .wh .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.widget .wb { flex: 1; min-height: 0; padding: 6px 12px 10px; display: flex; align-items: center; justify-content: center; position: relative; }
.widget .tools { display: none; gap: 2px; }
.dash.editing .widget .tools { display: flex; }
.dash.editing .widget { cursor: move; user-select: none; }
.dash.editing .widget .wb { pointer-events: none; }
.widget .tools button { padding: 2px 6px; font-size: 12px; border: none; background: transparent; color: var(--text-2); }
.widget .tools button:hover { background: var(--surface-2); }
.widget .resize { display: none; position: absolute; right: 2px; bottom: 2px; width: 14px; height: 14px; cursor: nwse-resize;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); border-radius: 0 0 4px 0; }
.dash.editing .widget .resize { display: block; }
.widget.dragging { opacity: .85; z-index: 5; box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.empty-dash { grid-column: 1 / -1; grid-row: 1 / span 2; display: grid; place-items: center; color: var(--muted); text-align: center; }

/* widget bodies */
.w-value { font-size: clamp(30px, 3.2vw, 40px); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.w-value small { font-size: .45em; color: var(--text-2); font-weight: 500; margin-left: 4px; }
.w-sub { position: absolute; bottom: 6px; right: 12px; font-size: 11px; color: var(--muted); }
.gauge svg { width: 100%; height: 100%; }
.gauge .val { font-weight: 600; font-variant-numeric: tabular-nums; fill: var(--text); }
.gauge .lbl { fill: var(--muted); font-size: 10px; }
.switch { position: relative; width: 64px; height: 34px; border-radius: 17px; background: var(--surface-2); border: 1px solid var(--border);
  cursor: pointer; transition: background .15s; padding: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%;
  background: var(--surface); box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: left .15s; }
.switch.on { background: var(--accent); border-color: var(--accent); }
.switch.on::after { left: 33px; }
.switch:hover { background: var(--surface-2); filter: brightness(.97); }
.switch.on:hover { background: var(--accent); }
.pushbtn { width: 80%; height: 70%; max-height: 64px; font-weight: 600; justify-content: center; font-size: 15px; }
.pushbtn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.slider-wrap { width: 100%; display: flex; align-items: center; gap: 12px; }
.slider-wrap input { flex: 1; padding: 0; accent-color: var(--accent); }
.slider-wrap output { min-width: 3.5em; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.led { width: 42px; height: 42px; border-radius: 50%; background: var(--surface-2); border: 2px solid var(--border); transition: all .2s; }
.led.on { box-shadow: 0 0 18px var(--led-c, var(--good)); background: var(--led-c, var(--good)); border-color: var(--led-c, var(--good)); }
.w-image { width: 100%; height: 100%; position: relative; background: #000; border-radius: 6px; overflow: hidden; display: grid; place-items: center; }
.w-image img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.w-image .overlay { position: absolute; left: 8px; top: 8px; display: flex; flex-direction: column; gap: 4px; }
.w-image .overlay span { background: rgba(0,0,0,.62); color: #fff; padding: 3px 8px; border-radius: 5px; font-size: 13px; font-weight: 600;
  font-variant-numeric: tabular-nums; width: fit-content; }
.w-image + .w-sub { color: #e8e8e8; right: 18px; bottom: 14px; text-shadow: 0 1px 2px #000; }
.w-image .noimg { color: #aaa; font-size: 13px; }
.terminal { width: 100%; height: 100%; display: flex; flex-direction: column; gap: 6px; }
.terminal .log { flex: 1; overflow: auto; background: var(--surface-2); border-radius: 6px; padding: 6px 8px; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.terminal .log div { white-space: pre-wrap; word-break: break-all; }
.terminal form { display: flex; gap: 6px; } .terminal input { padding: 5px 8px; }
.wb.chart-body { padding: 0 6px 6px; flex-direction: column; align-items: stretch; }
.chart-body canvas { width: 100%; flex: 1; min-height: 0; display: block; }
.ranges { display: flex; gap: 2px; }
.ranges button { padding: 1px 7px; font-size: 11.5px; border: none; background: transparent; color: var(--muted); }
.ranges button.on { background: var(--surface-2); color: var(--text); }
.chart-tip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  border-radius: 6px; padding: 5px 8px; font-size: 12px; white-space: nowrap; z-index: 3; }
.chart-tip b { font-variant-numeric: tabular-nums; }

@media (max-width: 760px) {
  main { padding: 14px; }
  .dash { grid-template-columns: repeat(4, 1fr); }
  .dash .widget { grid-column: auto / span 4 !important; grid-row: auto / span var(--h) !important; }
  .topbar { gap: 8px; padding: 0 10px; }
  .brand { font-size: 0; gap: 0; }
  .topbar nav a { padding: 6px 7px; font-size: 13px; }
  #ws-state { display: none; }
  .tabs { overflow-x: auto; white-space: nowrap; scrollbar-width: none; }
  .grid2 { grid-template-columns: 1fr; }
}
