:root {
  --canvas: #eceff2;
  --surface: #ffffff;
  --sunken: #f5f7f9;
  --ink: #131a21;
  --muted: #66727e;
  --rule: #d5dbe1;
  --accent: #0b6b57;
  --accent-soft: #e2f0eb;
  --live: #0b6b57;
  --wait: #8a5a00;
  --down: #9c2f1f;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(19, 26, 33, .06), 0 8px 24px -12px rgba(19, 26, 33, .18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --canvas: #10151a;
    --surface: #19212a;
    --sunken: #141b22;
    --ink: #e4eaf0;
    --muted: #8a97a4;
    --rule: #2a343f;
    --accent: #3fbf99;
    --accent-soft: #12332b;
    --live: #3fbf99;
    --wait: #d29a3a;
    --down: #e0705c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 28px -14px rgba(0, 0, 0, .7);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--canvas);
  color: var(--ink);
  font: 15px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}

h1 { font-size: 17px; font-weight: 650; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; font-weight: 650; margin: 0 0 4px; }
p  { margin: 0 0 12px; max-width: 64ch; }
.hint { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.hint:last-child { margin-bottom: 0; }

/* ---------------------------------------------------------------- shell */

.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
@media (max-width: 820px) { .shell { grid-template-columns: 1fr; } }

.rail {
  background: var(--surface);
  border-right: 1px solid var(--rule);
  padding: 20px 14px;
  display: flex; flex-direction: column; gap: 18px;
}
@media (max-width: 820px) { .rail { border-right: none; border-bottom: 1px solid var(--rule); } }

.brand { padding: 0 8px; }
.brand span { display: block; color: var(--muted); font-size: 12.5px; margin-top: 2px; }

.lamp-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius);
  background: var(--sunken); border: 1px solid var(--rule);
}
.lamp { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
[data-state="ready"] .lamp { background: var(--live); box-shadow: 0 0 0 3px color-mix(in srgb, var(--live) 22%, transparent); }
[data-state="qr"] .lamp, [data-state="authenticating"] .lamp, [data-state="starting"] .lamp { background: var(--wait); }
[data-state="disconnected"] .lamp, [data-state="error"] .lamp { background: var(--down); }
.lamp-row b { font-size: 13.5px; font-weight: 600; display: block; }
.lamp-row small { color: var(--muted); font-size: 12px; font-family: var(--mono); }

nav { display: flex; flex-direction: column; gap: 2px; }
@media (max-width: 820px) { nav { flex-direction: row; overflow-x: auto; } }

nav button {
  display: flex; align-items: center; gap: 9px;
  text-align: left; width: 100%; white-space: nowrap;
  padding: 9px 12px; border: none; border-radius: 8px;
  background: none; color: var(--muted);
  font: inherit; font-size: 14px; font-weight: 500; cursor: pointer;
}
nav button:hover { background: var(--sunken); color: var(--ink); }
nav button[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
nav button .badge {
  margin-left: auto; font-family: var(--mono); font-size: 11px;
  background: var(--rule); color: var(--muted); padding: 1px 6px; border-radius: 20px;
}

.rail-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }

/* ---------------------------------------------------------------- main */

main { padding: 26px 28px 60px; max-width: 1000px; }
@media (max-width: 820px) { main { padding: 20px 16px 48px; } }

.page { display: none; }
.page.on { display: block; }
.page > header { margin-bottom: 18px; }
.page > header h1 { margin-bottom: 3px; }
.page > header p { color: var(--muted); font-size: 13.5px; margin: 0; }

.card {
  background: var(--surface); border: 1px solid var(--rule);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
}
.card + .card { margin-top: 16px; }
.card > h2 + .hint { margin-top: -2px; }

.cols { display: grid; gap: 16px; grid-template-columns: 1fr; }
@media (min-width: 900px) { .cols.two { grid-template-columns: 1fr 1fr; align-items: start; } }

/* ---------------------------------------------------------------- stats */

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; background: var(--rule);
         border: 1px solid var(--rule); border-radius: var(--radius); overflow: hidden; margin-bottom: 16px; }
.stats div { background: var(--surface); padding: 13px 15px; }
.stats b { display: block; font-family: var(--mono); font-size: 20px; font-weight: 500; font-variant-numeric: tabular-nums; }
.stats span { font-size: 12px; color: var(--muted); }

/* ---------------------------------------------------------------- forms */

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; font-weight: 550; margin-bottom: 5px; }
label.field > small { display: block; color: var(--muted); font-size: 12px; margin-top: 4px; }

input[type=text], input[type=password], input[type=tel], input[type=number], input[type=url], select, textarea {
  font: inherit; font-size: 14px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--rule); border-radius: 7px; padding: 8px 10px; width: 100%;
}
textarea { resize: vertical; min-height: 74px; }
input[type=file] { font-size: 13px; }
select { cursor: pointer; }
input:disabled, textarea:disabled { background: var(--sunken); color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.check { display: inline-flex; align-items: center; gap: 7px; font-size: 13.5px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 15px; height: 15px; }

.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 160px; }
.row > .tight { flex: 0 0 auto; }

button.btn {
  font: inherit; font-size: 14px; font-weight: 550; cursor: pointer;
  border-radius: 7px; padding: 9px 16px;
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
}
button.btn:hover { filter: brightness(1.08); }
button.btn.quiet { background: var(--surface); color: var(--ink); border-color: var(--rule); }
button.btn.quiet:hover { background: var(--sunken); filter: none; }
button.btn.danger { background: var(--surface); color: var(--down); border-color: var(--down); }
button.btn:disabled { opacity: .5; cursor: not-allowed; filter: none; }
button.plain { background: none; border: none; color: var(--muted); cursor: pointer; font: inherit; font-size: 13px; padding: 4px; text-decoration: underline; }

.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 14px; }
.ok { color: var(--live); font-size: 13px; }
.bad { color: var(--down); font-size: 13px; }
.note { color: var(--muted); font-size: 13px; }

/* ---------------------------------------------------------------- segmented */

.seg { display: inline-flex; background: var(--sunken); border: 1px solid var(--rule); border-radius: 8px; padding: 3px; gap: 3px; flex-wrap: wrap; }
.seg button {
  font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
  border: none; background: none; color: var(--muted); padding: 6px 12px; border-radius: 6px;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* ---------------------------------------------------------------- qr */

.qr { text-align: center; }
.qr img { width: 100%; max-width: 280px; border-radius: 8px; border: 1px solid var(--rule); image-rendering: pixelated; }
.qr ol { text-align: left; color: var(--muted); font-size: 13px; padding-left: 20px; margin: 14px 0 0; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 7px 16px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--mono); font-size: 13px; word-break: break-all; }

/* ---------------------------------------------------------------- rules */

.rule { border: 1px solid var(--rule); border-radius: 9px; padding: 14px; margin-bottom: 12px; background: var(--surface); }
.rule.off { opacity: .6; }
.rule-top { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.rule-top input[type=text].label { flex: 1 1 150px; font-weight: 550; }
.rule-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin-bottom: 10px; }
.rule-body label.field { margin-bottom: 0; }
.tag { font-family: var(--mono); font-size: 11px; background: var(--sunken); border: 1px solid var(--rule); color: var(--muted); padding: 2px 7px; border-radius: 20px; }

/* ---------------------------------------------------------------- lists */

.list { border: 1px solid var(--rule); border-radius: 9px; overflow: hidden; }
.list button.item {
  display: block; width: 100%; text-align: left; background: var(--surface);
  border: none; border-bottom: 1px solid var(--rule); padding: 11px 14px; cursor: pointer; font: inherit;
  color: var(--ink);
}
.list button.item:last-child { border-bottom: none; }
.list button.item:hover { background: var(--sunken); }
.list button.item[aria-current="true"] { background: var(--accent-soft); }
.item-top { display: flex; align-items: center; gap: 8px; }
.item-top b { font-size: 14px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-top .tag { flex: none; }
.item p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.thread { max-height: 460px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding-right: 4px; }
.bubble { max-width: 78%; padding: 8px 11px; border-radius: 11px; font-size: 13.5px; background: var(--sunken); border: 1px solid var(--rule); }
.bubble.mine { align-self: flex-end; background: var(--accent-soft); border-color: transparent; }
.bubble small { display: block; color: var(--muted); font-size: 11px; font-family: var(--mono); margin-top: 3px; }

/* ---------------------------------------------------------------- activity */

.feed { font-size: 13px; max-height: 560px; overflow-y: auto; }
.feed div.entry { display: grid; grid-template-columns: 62px 78px 1fr; gap: 10px; padding: 6px 2px; border-bottom: 1px solid var(--rule); align-items: baseline; }
.feed time { color: var(--muted); font-family: var(--mono); font-size: 12px; font-variant-numeric: tabular-nums; }
.feed .kind { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.feed .entry.k-error .kind, .feed .entry.k-error .txt { color: var(--down); }
.feed .entry.k-reply .kind, .feed .entry.k-sent .kind { color: var(--live); }
.feed .txt { word-break: break-word; }
.feed .txt em { font-style: normal; color: var(--muted); }

.poll { border: 1px solid var(--rule); border-radius: 9px; padding: 13px; margin-bottom: 10px; }
.poll b { font-size: 14px; }
.bar { height: 6px; border-radius: 4px; background: var(--rule); overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; background: var(--accent); }
.poll .opt { font-size: 13px; margin-top: 8px; }
.poll .opt span { display: flex; justify-content: space-between; gap: 10px; }

/* ---------------------------------------------------------------- signin */

.signin { max-width: 340px; margin: 14vh auto; }
.signin .card { padding: 24px; }
.signin .btn { width: 100%; margin-top: 4px; }

.empty { color: var(--muted); font-size: 13.5px; padding: 18px 0; text-align: center; }

[hidden] { display: none !important; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
