:root {
  --bg: #f4f5f7; --card: #ffffff; --fg: #1b2130; --muted: #687187; --line: #e1e4ea;
  --field: #f7f8fa; --accent: #2457d6; --accent-fg: #ffffff;
  --ok: #16803c; --ok-bg: #e5f5ea; --bad: #b42318; --bad-bg: #fdecea;
  --lamp-off: #d5d9e0; --lamp-on: #f5a524; --lamp-glow: rgba(245, 165, 36, .45);
  --code: #eef0f3; --shadow: 0 1px 2px rgba(16, 24, 40, .05);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111419; --card: #191d24; --fg: #e4e7ec; --muted: #8c94a6; --line: #29303b;
    --field: #13171d; --accent: #6b9bff; --accent-fg: #0b1020;
    --ok: #4ade80; --ok-bg: #14301f; --bad: #f87171; --bad-bg: #3a1716;
    --lamp-off: #333a46; --lamp-on: #fbbf24; --lamp-glow: rgba(251, 191, 36, .35);
    --code: #222832; --shadow: none;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--fg);
       font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
code, .mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12.5px; }
code { background: var(--code); padding: 1px 5px; border-radius: 4px; overflow-wrap: anywhere; }
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
     margin: 0 0 10px; font-weight: 600; }

/* header */
header { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; padding: 10px 16px;
         background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
header h1 { font-size: 16px; margin: 0; }
nav { display: flex; gap: 4px; }
.tab { background: none; border: 0; color: var(--muted); padding: 6px 10px; border-radius: 6px; font: inherit; cursor: pointer; }
.tab[aria-selected="true"] { color: var(--fg); background: var(--code); font-weight: 600; }
#status { margin-left: auto; font-size: 12px; color: var(--muted); }
#status.down { color: var(--bad); }

/* controls */
button { font: inherit; cursor: pointer; border-radius: 6px; padding: 6px 12px;
         border: 1px solid var(--accent); background: var(--accent); color: var(--accent-fg); }
button.ghost { background: transparent; color: var(--accent); }
button.danger { background: transparent; color: var(--bad); border-color: var(--bad); }
button.small { padding: 3px 9px; font-size: 12.5px; }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; }
input[type=text], input[type=password], input[type=number], textarea, select {
  width: 100%; padding: 6px 9px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--field); color: var(--fg); font: inherit; }
select { width: auto; }
textarea { min-height: 56px; resize: vertical; font-family: ui-monospace, Consolas, monospace; }
label { font-size: 12px; color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; margin-bottom: 10px; }
.row.between h2 { margin: 0; }
.push { margin-left: auto; }
.narrow { width: 160px !important; }
.stack { display: grid; gap: 8px; }
.meta { color: var(--muted); font-size: 12px; }
.empty { color: var(--muted); font-style: italic; margin: 0; }
.err { color: var(--bad); font-size: 12.5px; min-height: 1em; margin: 0; }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px; box-shadow: var(--shadow); min-width: 0; }

/* views */
.view { padding: 16px; max-width: 1280px; margin: 0 auto; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 360px), 1fr)); align-items: start; }
.empty-state { max-width: 560px; margin: 40px auto; }
.empty-state h2 { font-size: 15px; text-transform: none; letter-spacing: 0; color: var(--fg); }
.console { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr) minmax(0, 1.45fr); }
@media (max-width: 860px) { .console { grid-template-columns: minmax(0, 1fr); } }
.col { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

/* login */
#login { position: fixed; inset: 0; background: var(--bg); display: grid; place-items: center; z-index: 10; padding: 16px; }
#loginForm { width: min(100%, 320px); display: grid; gap: 10px; }

/* device card */
.dev { display: flex; flex-direction: column; gap: 12px; }
.dev.offline .live { opacity: .5; }
.dev-head { display: flex; align-items: flex-start; gap: 10px; }
.dev-title { min-width: 0; flex: 1; }
.dev-name { font-size: 15px; font-weight: 600; overflow-wrap: anywhere; }
.dev-id { color: var(--muted); }
.badge { font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 99px; white-space: nowrap; }
.badge.on { color: var(--ok); background: var(--ok-bg); }
.badge.off { color: var(--bad); background: var(--bad-bg); }
.section-label { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 6px; }

.leds { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.led { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 6px;
       background: var(--field); color: var(--fg); border: 1px solid var(--line); }
.led .lamp { width: 22px; height: 22px; border-radius: 50%; background: var(--lamp-off); transition: background .15s, box-shadow .15s; }
.led[aria-pressed="true"] .lamp { background: var(--lamp-on); box-shadow: 0 0 10px 2px var(--lamp-glow); }
.led .state { font-size: 11.5px; color: var(--muted); }
.led.pending .lamp { animation: pulse .8s infinite alternate; }
@keyframes pulse { from { opacity: 1; } to { opacity: .35; } }

.buttons { display: flex; gap: 8px; }
.sw { flex: 1; text-align: center; padding: 6px; border-radius: 6px; border: 1px solid var(--line);
      font-size: 12.5px; color: var(--muted); transition: background .1s; }
.sw.pressed { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.sw .when { display: block; font-size: 10.5px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 2px 12px; margin: 0; font-size: 12.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.result { font-size: 12px; color: var(--muted); }
.result b { color: var(--fg); font-weight: 500; }

details { border-top: 1px solid var(--line); padding-top: 8px; }
summary { cursor: pointer; font-size: 12.5px; color: var(--muted); user-select: none; }
details[open] summary { margin-bottom: 8px; }
.settings { display: grid; grid-template-columns: 1fr 110px; gap: 8px; align-items: end; }
.settings .full { grid-column: 1 / -1; }
.activity { list-style: none; margin: 0; padding: 0; font-size: 12px; max-height: 220px; overflow-y: auto; }
.activity li { display: grid; grid-template-columns: 64px 18px 1fr; gap: 6px; padding: 2px 0; }
.activity .t { color: var(--muted); }
.activity .k-tx { color: var(--accent); }
.activity .k-conn { color: var(--muted); font-style: italic; }

/* console */
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
td, th { text-align: left; padding: 5px 6px; border-top: 1px solid var(--line); vertical-align: top; overflow-wrap: anywhere; }
th { font-size: 12px; color: var(--muted); font-weight: 500; border-top: 0; }
#log { max-height: 640px; overflow-y: auto; }
.client { border-top: 1px solid var(--line); padding: 8px 0; }
.client:first-child { border-top: 0; padding-top: 0; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); margin-right: 6px; }
.src-web { color: var(--accent); }
.tag { font-size: 11px; color: var(--muted); }
.bin { color: var(--muted); }

#toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); background: var(--fg); color: var(--bg);
         padding: 8px 14px; border-radius: 8px; font-size: 13px; opacity: 0; pointer-events: none; transition: opacity .2s;
         max-width: calc(100% - 32px); }
#toast.show { opacity: 1; }
#toast.error { background: var(--bad); color: #fff; }
