:root {
  --bg: #0b1116;
  --bg-2: #111a22;
  --panel: #17222c;
  --line: #243241;
  --text: #e8eef4;
  --muted: #8b9aab;
  --accent: #2dd4bf;
  --accent-2: #7c6bff;
  --mine: #1c4f4a;
  --theirs: #1b2732;
  --system: #8b9aab;
  --danger: #ff6b7a;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.4);
  --radius: 16px;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  max-height: 100%;
  margin: 0;
  overflow: hidden;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  position: relative;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.mobile-only { display: none; }

#app {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  height: 100%;
  max-height: 100%;
  overflow: hidden;
}

#sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  height: 100%;
  overflow: hidden;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 14px 12px;
}
.brand-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
}
.brand-name { font-weight: 700; letter-spacing: -0.02em; }
.brand-host { font-size: 12px; color: var(--muted); }
.brand .icon-btn { margin-left: auto; }

.side-body {
  --rooms-pct: 38%;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.side-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
#rooms-pane {
  flex: 0 0 var(--rooms-pct);
  min-height: 96px;
  max-height: calc(100% - 104px);
  height: var(--rooms-pct);
}
#people-pane {
  flex: 1 1 0;
  min-height: 96px;
}

.side-label {
  padding: 16px 16px 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  flex-shrink: 0;
}

.room-list, .people-list {
  overflow: auto;
  padding: 0 8px;
  min-height: 0;
  flex: 1 1 auto;
}
.people-list { padding-bottom: 16px; }

.side-split {
  flex: 0 0 8px;
  position: relative;
  cursor: row-resize;
  touch-action: none;
  z-index: 1;
}
.side-split::before {
  content: "";
  position: absolute;
  left: 14px;
  right: 14px;
  top: 3px;
  height: 2px;
  border-radius: 2px;
  background: var(--line);
}
.side-split:hover::before,
.side-split.dragging::before,
.side-split:focus-visible::before {
  background: var(--accent);
}
.side-split:focus-visible { outline: none; }
body.resizing-sidebar {
  cursor: row-resize;
  user-select: none;
}

.room, .person {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 9px 10px;
  border-radius: 12px;
  cursor: pointer;
}
.room:hover, .person:hover { background: rgba(255,255,255,0.04); }
.room.active { background: rgba(45, 212, 191, 0.12); }
.room-copy { min-width: 0; flex: 1; }
.room-name { font-weight: 600; font-size: 14px; }
.room-preview {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
  color: #0b1116;
  position: relative;
  flex-shrink: 0;
}
.avatar.sm { width: 22px; height: 22px; font-size: 10px; }
.avatar.agent { box-shadow: 0 0 0 2px var(--accent-2); }
.dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #3dd68c;
  border: 2px solid var(--bg-2);
}
.dot.off { background: #5b6b78; }

.person-name { font-size: 13px; }
.person-meta { font-size: 11px; color: var(--muted); }

#main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(1200px 500px at 80% -10%, rgba(124, 107, 255, 0.08), transparent 50%),
    radial-gradient(900px 400px at 0% 100%, rgba(45, 212, 191, 0.07), transparent 45%),
    var(--bg);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(11, 17, 22, 0.72);
  backdrop-filter: blur(12px);
  flex-shrink: 0;
}
.chat-title { min-width: 0; flex: 1; }
#room-name { font-weight: 700; }
#room-topic {
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-members { display: flex; }
.header-members .avatar { margin-left: -8px; box-shadow: 0 0 0 2px var(--bg); }
#btn-voice.on { color: #86efac; }
.active-agents { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.agent-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text); background: rgba(255,255,255,0.04); border: 1px solid var(--line); border-radius: 999px; padding: 3px 9px 3px 6px; }
.agent-pill .pdot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.mini-del { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 13px; padding: 2px 5px; border-radius: 8px; }
.mini-del:hover { color: var(--danger); background: rgba(255,107,122,0.1); }
.room-del, .person-del { flex-shrink: 0; }

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 18px 22px 8px;
  -webkit-overflow-scrolling: touch;
}
.day-sep {
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  margin: 12px 0;
}
.bubble-row {
  display: flex;
  gap: 8px;
  margin: 2px 0 10px;
  max-width: 78%;
}
.bubble-row.mine {
  margin-left: auto;
  flex-direction: row-reverse;
}
.bubble {
  background: var(--theirs);
  padding: 8px 12px;
  border-radius: 14px 14px 14px 5px;
  line-height: 1.4;
  font-size: 14.5px;
  word-wrap: break-word;
}
.bubble-row.mine .bubble {
  background: var(--mine);
  border-radius: 14px 14px 5px 14px;
}
.bubble.agent { outline: 1px solid rgba(124, 107, 255, 0.35); }
.who { font-size: 12px; font-weight: 700; margin-bottom: 2px; }
.time { font-size: 10px; color: var(--muted); margin-top: 4px; }
.sys {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  margin: 10px 0;
}
.mention {
  color: var(--accent);
  font-weight: 600;
}
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: rgba(0,0,0,0.25);
  padding: 1px 5px;
  border-radius: 6px;
}
.bubble a { color: #9cdcff; }

.typing {
  flex-shrink: 0;
  min-height: 0;
  padding: 0 22px 6px;
  font-size: 12px;
  color: var(--muted);
}

.composer {
  display: flex;
  gap: 10px;
  padding: 10px 16px 16px;
  align-items: flex-end;
  flex-shrink: 0;
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  resize: none;
  max-height: 140px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 16px;
  padding: 12px 14px;
  font: inherit;
  outline: none;
}
.composer textarea:focus { border-color: #355064; }
.send {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(180deg, #3ee0c8, #1aa58f);
  color: #06231e;
  font-size: 18px;
  cursor: pointer;
}
.send:disabled { opacity: 0.4; cursor: default; }

button.icon-btn, button.ghost-btn, button.text-btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  padding: 6px 10px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
}
button.text-btn {
  margin: 4px 12px 8px;
  width: calc(100% - 24px);
  text-align: left;
  color: var(--accent);
  border: 0;
}
button.ghost-btn:hover, button.icon-btn:hover { background: rgba(255,255,255,0.05); }

.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background:
    radial-gradient(800px 400px at 50% 0%, rgba(45, 212, 191, 0.12), transparent),
    var(--bg);
  z-index: 20;
}
.gate-card, .sheet {
  width: min(420px, calc(100vw - 32px));
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.gate-icon { width: 72px; height: 72px; border-radius: 18px; }
.gate-card h1, .sheet h2 { margin: 12px 0 6px; letter-spacing: -0.03em; }
.gate-card p, .sheet p { color: var(--muted); line-height: 1.45; }
label { display: block; font-size: 13px; margin: 14px 0; color: var(--muted); }
input {
  width: 100%;
  margin-top: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  padding: 11px 12px;
  font: inherit;
  outline: none;
}
.gate-card form button[type="submit"],
.sheet form button[type="submit"] {
  width: 100%;
  border: 0;
  background: var(--accent);
  color: #06231e;
  font-weight: 700;
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
  font: inherit;
}
.fine { font-size: 12px; color: var(--muted); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: grid;
  place-items: center;
  z-index: 30;
  padding: 20px;
}
.share-links { display: grid; gap: 8px; margin: 12px 0; }
.link-row {
  display: flex;
  gap: 8px;
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 10px;
}
.link-row span { flex: 1; font-size: 13px; word-break: break-all; }
.link-row button { flex-shrink: 0; }
.code {
  background: #0a0f14;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  font-size: 12px;
  overflow: auto;
  white-space: pre-wrap;
}

.agent-grid { display: grid; gap: 8px; margin: 14px 0; }
.agent-card {
  display: flex;
  gap: 10px;
  align-items: center;
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--panel);
  color: inherit;
  border-radius: 14px;
  padding: 10px;
  cursor: pointer;
}
.agent-card:hover { border-color: #3d5164; }
.agent-card b { display: block; }
.agent-card small { color: var(--muted); }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 10px 14px;
  border-radius: 12px;
  z-index: 50;
}

@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(320px, 86vw);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    z-index: 15;
  }
  #sidebar.open { transform: none; }
  .mobile-only { display: inline-flex; }
  .bubble-row { max-width: 92%; }
}
