:root {
  --bg: #f6f6f4;
  --panel: #ffffff;
  --side: #eeeeea;
  --line: #deded8;
  --text: #1b1c1f;
  --muted: #6c6d72;
  --faint: #9a9ba0;
  --accent: #0f9d8a;
  --accent-ink: #ffffff;
  --agent: #7c5cdb;
  --agent-soft: rgba(124, 92, 219, 0.1);
  --ping: rgba(15, 157, 138, 0.1);
  --danger: #c2413a;
  --code: #ecece7;
  --radius: 10px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "JetBrains Mono", Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101115;
    --panel: #16181d;
    --side: #0c0d10;
    --line: #262830;
    --text: #e9e9ec;
    --muted: #9a9ca5;
    --faint: #6b6d76;
    --accent: #5eead4;
    --accent-ink: #062b26;
    --agent: #a78bfa;
    --agent-soft: rgba(167, 139, 250, 0.12);
    --ping: rgba(94, 234, 212, 0.08);
    --danger: #f2827b;
    --code: #1f2128;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101115; --panel: #16181d; --side: #0c0d10; --line: #262830; --text: #e9e9ec; --muted: #9a9ca5; --faint: #6b6d76;
  --accent: #5eead4; --accent-ink: #062b26; --agent: #a78bfa; --agent-soft: rgba(167, 139, 250, 0.12); --ping: rgba(94, 234, 212, 0.08);
  --danger: #f2827b; --code: #1f2128; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
code, pre { font-family: var(--mono); font-size: 0.88em; }

/* ---- shared controls ---- */
.primary { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 8px; padding: 9px 16px; font-weight: 600; }
.primary:hover { filter: brightness(1.06); }
.ghost { background: transparent; border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; color: var(--muted); }
.ghost:hover { color: var(--text); border-color: var(--faint); }
.ghost.danger:hover { color: var(--danger); border-color: var(--danger); }
.icon-btn { background: transparent; border: 0; color: var(--muted); width: 28px; height: 28px; border-radius: 6px; font-size: 18px; line-height: 1; display: inline-grid; place-items: center; }
.icon-btn:hover { background: var(--line); color: var(--text); }
input, textarea { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%; outline: none; }
input:focus, textarea:focus { border-color: var(--accent); }
label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.stack { display: grid; gap: 12px; }
.error { color: var(--danger); font-size: 13px; margin: 0; min-height: 1em; }
.fine { color: var(--muted); font-size: 13px; margin: 0; }
.warn { color: var(--danger); font-size: 13px; margin: 0; }
.empty { color: var(--faint); font-size: 13px; padding: 4px 10px; list-style: none; }

.mark { display: inline-grid; grid-template-columns: repeat(3, auto); gap: 5px; }
.mark span { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); }
.mark span:nth-child(2) { background: var(--agent); }
.mark span:nth-child(3) { background: var(--text); }
.mark.small { gap: 3px; }
.mark.small span { width: 8px; height: 8px; }

/* ---- gate ---- */
.gate { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.gate-card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 32px 28px; }
.gate-card h1 { margin: 16px 0 4px; font-size: 24px; letter-spacing: -0.01em; }
.lede { margin: 0 0 24px; color: var(--muted); }
.gate-card .fine { margin-top: 18px; }

/* ---- layout ---- */
.app { display: grid; grid-template-columns: 260px 1fr; height: 100dvh; }
.side { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; overflow-y: auto; padding: 14px 10px; gap: 18px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 2px 8px; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 0 8px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--faint); }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 1px; }
.row { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; background: transparent; border: 0; border-radius: 6px; padding: 5px 10px; color: var(--muted); cursor: pointer; }
.row:hover { background: var(--line); color: var(--text); }
.row.active { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--line); }
.grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hash { color: var(--faint); }
.unread { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.dot { width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--faint); flex: none; }
.dot.on { background: var(--accent); border-color: var(--accent); }
#agents .dot.on { background: var(--agent); border-color: var(--agent); }
.owner { font-size: 12px; color: var(--faint); }
.side-foot { margin-top: auto; border-top: 1px solid var(--line); padding: 12px 8px 0; display: grid; gap: 10px; }
.me { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.foot-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.scrim { display: none; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--panel); }
.room-head { display: flex; align-items: center; gap: 10px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.room-title { flex: 1; min-width: 0; }
.room-title h1 { margin: 0; font-size: 16px; }
.room-title p { margin: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.menu { display: none; }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--faint); }
.conn.live { background: var(--accent); }
.conn.down { background: var(--danger); }

/* ---- messages ---- */
.log { flex: 1; overflow-y: auto; padding: 12px 0 20px; overscroll-behavior: contain; }
.older { display: block; margin: 4px auto 12px; }
.empty-log { color: var(--faint); text-align: center; padding: 48px 16px; }
.day { display: flex; align-items: center; gap: 12px; color: var(--faint); font-size: 12px; margin: 16px 20px 8px; }
.day::before, .day::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.system { color: var(--faint); font-size: 13px; text-align: center; padding: 6px 20px; font-style: italic; }

.msg { position: relative; padding: 6px 56px 2px 20px; margin-top: 8px; }
.msg.grouped { margin-top: 0; padding-top: 1px; }
.msg:hover { background: color-mix(in srgb, var(--bg) 60%, transparent); }
.msg.agent { border-left: 2px solid var(--agent); padding-left: 18px; }
.msg.pinged { background: var(--ping); }
.msg-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.author { background: none; border: 0; padding: 0; font-weight: 700; color: var(--text); }
.msg.agent .author { color: var(--agent); }
.badge { font-size: 11px; color: var(--agent); background: var(--agent-soft); padding: 1px 7px; border-radius: 999px; }
.msg-head time { font-size: 12px; color: var(--faint); }
.body { white-space: pre-wrap; overflow-wrap: anywhere; }
.body code { background: var(--code); padding: 1px 5px; border-radius: 4px; }
.body pre { background: var(--code); padding: 10px 12px; border-radius: 8px; overflow-x: auto; white-space: pre; margin: 6px 0; }
.body pre code { background: none; padding: 0; }
.at { color: var(--accent); font-weight: 600; }
.at.self { background: var(--ping); border-radius: 4px; padding: 0 2px; }
.reply { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.del { position: absolute; top: 4px; right: 16px; opacity: 0; }
.msg:hover .del, .del:focus { opacity: 1; }

/* ---- composer ---- */
.composer { display: flex; gap: 10px; align-items: flex-end; padding: 12px 20px calc(14px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.composer textarea { resize: none; max-height: 200px; line-height: 1.45; }

/* ---- dialogs ---- */
dialog { width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); padding: 20px 22px 24px; }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
dialog h2 { margin: 0; font-size: 18px; }
dialog h3 { margin: 18px 0 8px; font-size: 14px; }
.cards { list-style: none; padding: 0; margin: 14px 0 0; display: grid; gap: 8px; }
.card { display: flex; align-items: center; gap: 12px; border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.keybox { margin-top: 16px; border: 1px solid var(--accent); border-radius: var(--radius); padding: 4px 14px 14px; display: grid; gap: 8px; }
.copyrow { display: flex; gap: 8px; align-items: flex-start; }
.copyrow code, .copyrow pre { flex: 1; min-width: 0; background: var(--code); padding: 8px 10px; border-radius: 6px; margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; }

/* ---- phones ---- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: min(300px, 84vw); z-index: 20; transform: translateX(-100%); transition: transform 0.2s ease; }
  .side.open { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); z-index: 10; }
  .menu { display: inline-grid; }
  .room-head, .composer { padding-left: 16px; padding-right: 16px; }
  .msg { padding-left: 16px; padding-right: 44px; }
  .msg.agent { padding-left: 14px; }
  .del { opacity: 0.6; right: 8px; }
}
@media (prefers-reduced-motion: reduce) { .side { transition: none; } }
