:root {
  --bg: #f4f6f8; --surface: #ffffff; --surface-2: #eef1f4; --text: #1b2430; --muted: #566274;
  --border: #d5dbe3; --accent: #0b6e8a; --accent-text: #ffffff; --accent-soft: #dff0f5;
  --danger: #b42318; --ok: #1a7f4b; --warn: #8f5500; --focus: #0b6e8a;
  --radius: 10px; --gap: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b; --surface: #1a2029; --surface-2: #232b36; --text: #e6eaf0; --muted: #9eaabb;
    --border: #2f3946; --accent: #5cc2e0; --accent-text: #06222b; --accent-soft: #17323c;
    --danger: #ff8f84; --ok: #62d69c; --warn: #f0b45a; --focus: #5cc2e0;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.5 var(--font); min-height: 100vh; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; }
p { margin: 0 0 .75em; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); } .mono { font-family: var(--mono); font-size: .9em; word-break: break-all; }
.noscript { padding: 16px; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }
.hint { color: var(--muted); font-size: .85rem; margin: 4px 0 0; }
.error-text { color: var(--danger); }

/* ------------------------------------------------------------------ topbar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { display: flex; align-items: center; gap: 12px; padding: 10px 16px; max-width: 1200px; margin: 0 auto; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 1.05rem; text-decoration: none; color: var(--text); margin-right: auto; }
.topbar select { width: auto; padding: 6px 8px; }
.nav { display: flex; gap: 4px; overflow-x: auto; padding: 0 16px 8px; max-width: 1200px; margin: 0 auto; scrollbar-width: thin; }
.nav a { white-space: nowrap; padding: 6px 12px; border-radius: 999px; text-decoration: none; color: var(--muted); font-size: .95rem; }
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--text); font-weight: 600; }
main { max-width: 1200px; margin: 0 auto; padding: 16px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.page-head h1 { margin: 0; margin-right: auto; }

/* ----------------------------------------------------------------- controls */
.btn { font: inherit; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; min-height: 40px; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger); color: var(--surface); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-icon { width: 40px; padding: 0; font-size: 1.3rem; line-height: 1; }
.btn-sm { min-height: 32px; padding: 4px 10px; font-size: .9rem; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; min-height: 40px; }
textarea { min-height: 120px; font-family: var(--mono); font-size: .9rem; }
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
input[type="file"] { padding: 6px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
.check { display: flex; align-items: center; gap: 8px; margin: 4px 0; cursor: pointer; }
.check span { font-weight: 400; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1 1 auto; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 16px; }

/* -------------------------------------------------------------------- cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: var(--gap); align-items: start; }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.card-head h2 { margin: 0; margin-right: auto; font-size: 1.1rem; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .8rem; border: 1px solid var(--border); color: var(--muted); }
.badge-ok { color: var(--ok); border-color: var(--ok); }
.badge-bad { color: var(--danger); border-color: var(--danger); }
.badge-warn { color: var(--warn); border-color: var(--warn); }
.point { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-top: 1px solid var(--border); flex-wrap: wrap; }
.point:first-of-type { border-top: 0; }
.point-name { flex: 1 1 120px; }
.point-name small { display: block; color: var(--muted); }
.point-value { font-size: 1.25rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.point-value .unit { font-size: .9rem; font-weight: 400; color: var(--muted); margin-left: 2px; }
.stepper { display: flex; gap: 4px; align-items: center; }
.stepper input { width: 86px; text-align: center; }
.toggle { min-width: 76px; font-weight: 600; }
.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.cmd-status { flex-basis: 100%; font-size: .85rem; color: var(--muted); }
.cmd-status.ok { color: var(--ok); } .cmd-status.bad { color: var(--danger); }

/* ------------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: .93rem; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.actions { white-space: nowrap; text-align: right; }
td.actions .btn { margin-left: 4px; }

/* ------------------------------------------------------------------- dialog */
dialog.modal { border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 24px); background: var(--surface); color: var(--text); }
dialog.modal-wide { width: min(920px, calc(100vw - 24px)); }
dialog.modal::backdrop { background: rgba(0, 0, 0, .5); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; }
.modal-body { padding: 16px; overflow-y: auto; max-height: calc(100vh - 190px); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding: 12px 16px; border-top: 1px solid var(--border); }
.secret-box { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px; margin-bottom: 10px; }
.secret-box dt { font-size: .8rem; color: var(--muted); }
.secret-box dd { margin: 0 0 8px; display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.warning { border-left: 4px solid var(--warn); background: var(--surface-2); padding: 10px 12px; border-radius: 6px; margin-bottom: 12px; }

/* -------------------------------------------------------------------- toast */
.toasts { position: fixed; bottom: 16px; left: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; align-items: center; z-index: 100; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; max-width: 520px; box-shadow: 0 4px 16px rgba(0, 0, 0, .25); pointer-events: auto; }
.toast-error { background: var(--danger); color: var(--bg); }

/* -------------------------------------------------------------------- login */
.login { max-width: 400px; margin: 10vh auto 0; padding: 0 16px; }
.login .card { padding: 24px; }
.login .lang { text-align: right; margin-bottom: 8px; }

/* -------------------------------------------------------------------- chart */
.chart-wrap { position: relative; }
.chart-svg { width: 100%; height: auto; display: block; touch-action: pan-y; }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-axis { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.chart-area { fill: var(--accent); opacity: .12; }
.chart-cursor { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart-tip { position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: .8rem; padding: 4px 8px; border-radius: 6px; white-space: nowrap; transform: translate(-50%, -110%); }
.range-tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.range-tabs .btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); font-weight: 600; }
.chart-summary { display: flex; gap: 16px; flex-wrap: wrap; color: var(--muted); font-size: .9rem; margin-top: 8px; }

/* -------------------------------------------------------------- permissions */
.perm-device { border-top: 1px solid var(--border); padding: 12px 0; }
.perm-device:first-child { border-top: 0; }
.perm-limits { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 6px; }
.perm-limits input { width: 90px; }
.tabs-inline { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
@media (max-width: 520px) {
  main { padding: 12px 16px; }
  td.actions { white-space: normal; }
}

.section-gap { margin-top: 24px; }
