:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --side: #161b24;
  --side-2: #1e2531;
  --side-text: #aab3c2;
  --side-active: #2a3444;
  --text: #171a1f;
  --muted: #6b7280;
  --line: #e4e7ec;
  --accent: #2f6df6;
  --accent-soft: #eaf0fe;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --ok: #17845a;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --panel: #161b24;
    --side: #0b0e13;
    --side-2: #141922;
    --text: #e6e9ee;
    --muted: #8b95a5;
    --line: #262d38;
    --accent: #5b8cff;
    --accent-soft: #1b2436;
    --danger: #ef6b5e;
    --danger-soft: #2a1a18;
    --ok: #3fbd8b;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; font-family: var(--font); background: var(--bg); color: var(--text);
  font-size: 15px; line-height: 1.5; -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ---------- вход и приглашение ---------- */
.gate {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px;
}
.gate-card {
  width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 32px; box-shadow: 0 10px 40px rgba(0,0,0,.06);
}
.gate-card h1 { margin: 0 0 6px; font-size: 21px; letter-spacing: -.01em; }
.gate-card p.sub { margin: 0 0 24px; color: var(--muted); font-size: 14px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg);
}
.field input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.btn {
  border: 0; border-radius: var(--radius); background: var(--accent); color: #fff;
  padding: 11px 16px; cursor: pointer; font-weight: 600; width: 100%;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
.btn.slim { width: auto; padding: 7px 12px; font-weight: 500; font-size: 14px; }
.btn.danger { background: var(--danger); }
.note { margin-top: 14px; font-size: 13px; color: var(--muted); }
.error {
  background: var(--danger-soft); color: var(--danger); padding: 9px 12px;
  border-radius: var(--radius); font-size: 14px; margin-bottom: 14px;
}
.hidden { display: none !important; }

/* ---------- каркас ---------- */
.app { display: grid; grid-template-columns: 268px 1fr; height: 100dvh; }

.sidebar {
  background: var(--side); color: var(--side-text); display: flex; flex-direction: column;
  min-width: 0;
}
.brand {
  padding: 18px 18px 14px; color: #fff; font-weight: 650; font-size: 15px;
  letter-spacing: -.01em; display: flex; align-items: center; gap: 9px;
}
.brand .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.side-label {
  padding: 10px 18px 6px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .07em; color: #5d6779;
}
.bots { flex: 1; min-height: 0; overflow-y: auto; padding: 0 8px 8px; }
/* Кнопка комнаты в боковой панели. Отдельный класс: «.bot» занят репликами бота. */
.room {
  display: flex; gap: 10px; align-items: center; width: 100%; text-align: left;
  padding: 9px 10px; border: 0; background: transparent; color: var(--side-text);
  border-radius: 8px; cursor: pointer; margin-bottom: 2px;
}
.room:hover { background: var(--side-2); }
.room.active { background: var(--side-active); color: #fff; }
.room .ava {
  width: 30px; height: 30px; border-radius: 8px; background: var(--side-2);
  display: grid; place-items: center; font-size: 15px; flex: none;
}
.room.active .ava { background: #35415a; }
.room .meta { min-width: 0; flex: 1; }
.room .nm { font-size: 14px; font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room .sb { font-size: 12px; color: #6a7488; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room .live { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); flex: none; animation: pulse 1.4s infinite; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .25 } }
.empty-side { padding: 16px 18px; font-size: 13px; color: #5d6779; }

.me { border-top: 1px solid #222a37; padding: 12px; display: flex; gap: 10px; align-items: center; }
.me .ava { width: 32px; height: 32px; border-radius: 50%; background: var(--side-2); display: grid; place-items: center; color: #fff; font-size: 13px; font-weight: 600; flex: none; }
.me .meta { min-width: 0; flex: 1; }
.me .nm { color: #fff; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me .rl { font-size: 12px; color: #5d6779; }
.icon-btn { background: transparent; border: 0; color: var(--side-text); cursor: pointer; padding: 6px; border-radius: 6px; font-size: 15px; }
.icon-btn:hover { background: var(--side-2); color: #fff; }

.me { position: relative; }
.popup {
  position: absolute; bottom: 52px; right: 10px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; padding: 5px;
  box-shadow: 0 10px 30px rgba(0,0,0,.22); min-width: 180px; z-index: 30;
}
.popup button {
  display: block; width: 100%; text-align: left; border: 0; background: transparent;
  padding: 9px 12px; border-radius: 7px; cursor: pointer; font-size: 14px; color: var(--text);
}
.popup button:hover { background: var(--bg); }

/* ---------- чат ---------- */
.main { display: flex; flex-direction: column; min-width: 0; background: var(--panel); }
.chat-head {
  padding: 14px 22px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px;
}
.chat-head .ava { width: 34px; height: 34px; border-radius: 9px; background: var(--accent-soft); display: grid; place-items: center; font-size: 17px; }
.chat-head h2 { margin: 0; font-size: 15.5px; font-weight: 620; }
.chat-head .sub { font-size: 13px; color: var(--muted); }
.badge {
  font-size: 11.5px; padding: 3px 8px; border-radius: 20px; background: var(--bg);
  border: 1px solid var(--line); color: var(--muted);
}

.stream { flex: 1; overflow-y: auto; padding: 20px 22px; }
.stream-inner { max-width: 820px; margin: 0 auto; }
.msg { display: flex; gap: 11px; padding: 9px 0; }
.msg .ava {
  width: 30px; height: 30px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-size: 12.5px; font-weight: 600; background: var(--bg); border: 1px solid var(--line); color: var(--muted);
}
.msg.bot .ava { background: var(--accent-soft); border-color: transparent; }
.msg .body { min-width: 0; flex: 1; }
.msg .who { font-size: 13px; font-weight: 620; margin-bottom: 2px; }
.msg .who span { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 7px; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .text code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: 13.5px; }
.msg .text pre { background: var(--bg); padding: 12px 14px; border-radius: var(--radius); overflow-x: auto; border: 1px solid var(--line); }
.msg.mine .who { color: var(--accent); }
.msg.err .text { color: var(--danger); background: var(--danger-soft); padding: 9px 12px; border-radius: var(--radius); }
.day { text-align: center; font-size: 12px; color: var(--muted); margin: 16px 0 8px; }

.typing { display: flex; gap: 11px; padding: 9px 0; align-items: center; color: var(--muted); font-size: 14px; }
.typing .dots span {
  display: inline-block; width: 5px; height: 5px; border-radius: 50%; background: var(--muted);
  margin-right: 3px; animation: bounce 1.2s infinite;
}
.typing .dots span:nth-child(2) { animation-delay: .15s; }
.typing .dots span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: .4 } 30% { transform: translateY(-4px); opacity: 1 } }

.composer { border-top: 1px solid var(--line); padding: 14px 22px 18px; }
.composer-inner { max-width: 820px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 11px 14px;
  background: var(--bg); max-height: 200px; min-height: 44px;
}
.composer textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.composer .send { border: 0; border-radius: 12px; background: var(--accent); color: #fff; height: 44px; padding: 0 18px; cursor: pointer; font-weight: 600; }
.composer .send:disabled { opacity: .5; cursor: default; }
.composer .hint { max-width: 820px; margin: 7px auto 0; font-size: 12px; color: var(--muted); }
.readonly-bar { max-width: 820px; margin: 0 auto; font-size: 13.5px; color: var(--muted); text-align: center; padding: 10px; background: var(--bg); border-radius: var(--radius); }

.blank { flex: 1; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 40px; }
.blank h3 { margin: 0 0 6px; color: var(--text); font-size: 16px; }

/* ---------- окна управления ---------- */
.overlay { position: fixed; inset: 0; background: rgba(8,10,14,.5); display: grid; place-items: center; padding: 24px; z-index: 40; }
.modal { background: var(--panel); border-radius: 14px; width: 100%; max-width: 860px; max-height: 88dvh; display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--line); }
.modal-head { padding: 16px 22px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 14px; }
.modal-head h2 { margin: 0; font-size: 16px; flex: 1; }
.tabs { display: flex; gap: 4px; padding: 10px 16px 0; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: transparent; padding: 9px 14px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; font-size: 14px; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.modal-body { padding: 18px 22px 24px; overflow-y: auto; }
.modal-body h4 { margin: 20px 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.modal-body h4:first-child { margin-top: 0; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); }
td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.pill { font-size: 11.5px; padding: 2px 8px; border-radius: 20px; background: var(--accent-soft); color: var(--accent); }
.pill.grey { background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.pill.off { background: var(--danger-soft); color: var(--danger); }

.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.form-grid .wide { grid-column: 1 / -1; }
.form-grid input, .form-grid select, .form-grid textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}
.access-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.access-chip { display: flex; align-items: center; gap: 7px; border: 1px solid var(--line); border-radius: 20px; padding: 5px 12px; font-size: 13.5px; background: var(--bg); }
.access-chip select { border: 0; background: transparent; font-size: 12.5px; color: var(--muted); }
.copy-box { display: flex; gap: 8px; margin-top: 10px; }
.copy-box input { flex: 1; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); font-size: 13px; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .app.show-list .sidebar { display: flex; }
  .app.show-list .main { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .stream, .composer { padding-left: 14px; padding-right: 14px; }
}
