:root {
  --bg-0: #18191c;
  --bg-1: #1f2024;
  --bg-2: #26282d;
  --bg-3: #2f3137;
  --bg-4: #3a3c43;
  --hover: rgba(255, 255, 255, 0.05);
  --active: rgba(255, 255, 255, 0.09);
  --line: rgba(255, 255, 255, 0.07);
  --text: #e3e4e8;
  --text-2: #b5b8c0;
  --muted: #8b8f99;
  --accent: #e8875b;
  --accent-ink: #1a1007;
  --accent-soft: rgba(232, 135, 91, 0.16);
  --danger: #e5484d;
  --ok: #3fb26b;
  --speaking: #3fb26b;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, monospace;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg-0: #e4e5e9;
    --bg-1: #eceef1;
    --bg-2: #f4f5f7;
    --bg-3: #ffffff;
    --bg-4: #e1e3e8;
    --hover: rgba(0, 0, 0, 0.04);
    --active: rgba(0, 0, 0, 0.08);
    --line: rgba(0, 0, 0, 0.08);
    --text: #1d1f24;
    --text-2: #3d4049;
    --muted: #6b707b;
    --accent: #c9612f;
    --accent-ink: #fff;
    --accent-soft: rgba(201, 97, 47, 0.12);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg-2);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.spacer { flex: 1; }

svg.ic { display: block; flex-shrink: 0; }

/* ---------------------------------------------------------------- buttons & forms */
.btn {
  border: 0; border-radius: 8px; padding: 9px 16px;
  background: var(--bg-4); font-weight: 600;
}
.btn:hover { filter: brightness(1.1); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger-outline { background: transparent; color: var(--danger); box-shadow: inset 0 0 0 1px var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }
.link { background: none; border: 0; color: var(--muted); padding: 0; font-size: 13px; }
.link:hover { color: var(--text); text-decoration: underline; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; border-radius: 6px;
  background: none; border: 0; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.on { color: var(--accent); }
.icon-btn.off { color: var(--danger); }
.icon-btn.danger:hover { color: var(--danger); }

label { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--text-2); }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
input:not([type=checkbox]):not([type=radio]), select, textarea {
  background: var(--bg-0); border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 11px; outline: none; font-weight: 400; color: var(--text);
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.form-error { color: var(--danger); font-size: 13px; min-height: 1em; }

/* ---------------------------------------------------------------- auth */
.auth { height: 100%; display: grid; place-items: center; padding: 16px; background: var(--bg-1); overflow: auto; }
.auth-card {
  width: min(380px, 100%); display: grid; gap: 14px;
  background: var(--bg-2); padding: 28px; border-radius: 16px; border: 1px solid var(--line);
}
.auth-logo { display: flex; align-items: center; gap: 12px; font-size: 24px; font-weight: 700; }
.auth-card p { margin: -6px 0 4px; }
.auth-card .btn { padding: 11px; }

/* ---------------------------------------------------------------- layout */
.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr) 232px;
}
.app.no-members { grid-template-columns: 248px minmax(0, 1fr) 0; }
.app.no-members .members { display: none; }

.sidebar { background: var(--bg-1); display: flex; flex-direction: column; min-height: 0; }
.side-head {
  height: 52px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 10px 0 16px; border-bottom: 1px solid var(--line); font-weight: 700;
}
.channels { flex: 1; overflow-y: auto; padding: 8px 8px 16px; }
.cat {
  font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase;
  color: var(--muted); padding: 14px 8px 4px;
}
.channels ul { list-style: none; margin: 0; padding: 0; }
.chan {
  display: flex; align-items: center; gap: 6px; width: 100%;
  padding: 6px 8px; border-radius: 6px; border: 0; background: none;
  color: var(--muted); text-align: left; position: relative;
}
.chan:hover { background: var(--hover); color: var(--text-2); }
.chan.active { background: var(--active); color: var(--text); }
.chan.unread { color: var(--text); font-weight: 600; }
.chan.unread::before {
  content: ""; position: absolute; left: -8px; width: 4px; height: 8px;
  border-radius: 0 4px 4px 0; background: var(--text);
}
.chan .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chan .del { opacity: 0; width: 22px; height: 22px; }
.chan:hover .del { opacity: 1; }
.chan.connected { color: var(--ok); }

.vmembers { list-style: none; padding: 2px 0 6px 28px; margin: 0; }
.vmember { display: flex; align-items: center; gap: 8px; padding: 3px 6px; font-size: 14px; color: var(--text-2); border-radius: 6px; }
.vmember .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vmember .flags { display: flex; gap: 3px; color: var(--muted); }
.badge-live {
  font-size: 10px; font-weight: 800; background: var(--danger); color: #fff;
  border-radius: 4px; padding: 0 4px; line-height: 16px;
}

.voice-bar {
  display: flex; align-items: center; gap: 2px;
  padding: 8px 8px; border-top: 1px solid var(--line); background: var(--bg-1);
}
.vb-info { flex: 1; min-width: 0; background: none; border: 0; text-align: left; padding: 2px 6px; border-radius: 6px; }
.vb-info:hover { background: var(--hover); }
.vb-status { display: block; color: var(--ok); font-weight: 700; font-size: 13px; }
.vb-status.connecting { color: #e0b341; }
.vb-chan { display: block; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.user-panel {
  display: flex; align-items: center; gap: 4px; padding: 8px;
  background: var(--bg-0);
}
.me-name { flex: 1; min-width: 0; font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-left: 4px; }

.avatar {
  --size: 32px;
  width: var(--size); height: var(--size); border-radius: 50%;
  display: inline-grid; place-items: center; flex-shrink: 0;
  font-weight: 700; font-size: calc(var(--size) * 0.42); color: #fff;
  background: var(--c, #666); position: relative; user-select: none;
  transition: box-shadow .12s;
}
.avatar.sm { --size: 22px; }
.avatar.lg { --size: 40px; }
.avatar.xl { --size: 84px; }
.speaking > .avatar, .avatar.speaking { box-shadow: 0 0 0 2px var(--bg-1), 0 0 0 4px var(--speaking); }
.avatar .dot {
  position: absolute; right: -2px; bottom: -2px; width: 12px; height: 12px; border-radius: 50%;
  background: var(--muted); box-shadow: 0 0 0 3px var(--bg-1);
}
.avatar .dot.on { background: var(--ok); }

/* ---------------------------------------------------------------- main */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg-2); }
.main-head {
  height: 52px; flex-shrink: 0; display: flex; align-items: center; gap: 8px;
  padding: 0 12px 0 16px; border-bottom: 1px solid var(--line);
}
.main-head h1 { font-size: 16px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-icon { color: var(--muted); }
.only-narrow { display: none; }

.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; position: relative; }
.messages { flex: 1; overflow-y: auto; padding: 16px 0 8px; overflow-anchor: none; }
.chat-start { padding: 24px 16px 16px; }
.chat-start h2 { margin: 0 0 4px; font-size: 26px; }
.loading-more { text-align: center; color: var(--muted); font-size: 13px; padding: 8px; }
.day-sep {
  display: flex; align-items: center; gap: 8px; margin: 16px 16px 4px;
  font-size: 12px; font-weight: 600; color: var(--muted);
}
.day-sep::before, .day-sep::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.msg {
  display: grid; grid-template-columns: 56px minmax(0, 1fr);
  padding: 2px 48px 2px 0; position: relative; margin-top: 14px;
}
.msg.compact { margin-top: 0; }
.msg:hover { background: var(--hover); }
.msg-gutter { display: flex; justify-content: center; padding-top: 2px; }
.msg.compact .msg-gutter time { visibility: hidden; font-size: 10px; color: var(--muted); line-height: 22px; }
.msg.compact:hover .msg-gutter time { visibility: visible; }
.msg-head { display: flex; align-items: baseline; gap: 8px; }
.author { font-weight: 600; }
.msg-head time { font-size: 12px; color: var(--muted); }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text); }
.msg-text .edited { font-size: 11px; color: var(--muted); margin-left: 4px; }
.msg-text code { font-family: var(--mono); font-size: 13px; background: var(--bg-0); padding: 1px 5px; border-radius: 4px; }
.msg-text pre { font-family: var(--mono); font-size: 13px; background: var(--bg-0); border: 1px solid var(--line); padding: 10px 12px; border-radius: 8px; margin: 4px 0; white-space: pre-wrap; }
.msg-text pre code { background: none; padding: 0; }
.msg-text blockquote { margin: 2px 0; padding-left: 10px; border-left: 3px solid var(--bg-4); color: var(--text-2); }
.msg-actions {
  position: absolute; right: 12px; top: -14px; display: none;
  background: var(--bg-3); border: 1px solid var(--line); border-radius: 8px; padding: 2px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .2);
}
.msg:hover .msg-actions { display: flex; }
.msg-actions .icon-btn { width: 28px; height: 28px; }
.msg-edit textarea { width: 100%; resize: none; }
.msg-edit .hint { font-size: 12px; color: var(--muted); }

.file { margin-top: 6px; }
.file img, .file video {
  display: block; max-width: min(420px, 100%); max-height: 340px; border-radius: 8px;
  background: var(--bg-0); cursor: zoom-in;
}
.file video { cursor: auto; }
.file audio { width: min(420px, 100%); }
.file-card {
  display: inline-flex; align-items: center; gap: 12px; max-width: min(420px, 100%);
  background: var(--bg-1); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px;
}
.file-card .meta { min-width: 0; }
.file-card a { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.file-card .size { font-size: 12px; color: var(--muted); }

.composer { padding: 0 16px 6px; flex-shrink: 0; }
.composer-row {
  display: flex; align-items: flex-end; gap: 4px;
  background: var(--bg-3); border-radius: var(--radius); padding: 6px 8px;
}
.composer textarea {
  flex: 1; resize: none; border: 0; background: none; padding: 6px 4px;
  max-height: 40vh; line-height: 1.4;
}
.composer textarea:focus { border: 0; }
.typing { height: 20px; font-size: 12px; color: var(--text-2); padding: 2px 4px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.pending {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg-3); border-radius: var(--radius) var(--radius) 0 0; padding: 10px 12px;
  border-bottom: 1px solid var(--line); margin-bottom: -4px; position: relative;
}
.pending + .composer-row { border-radius: 0 0 var(--radius) var(--radius); }
.pending img { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; }
.pending .meta { flex: 1; min-width: 0; font-size: 13px; }
.pending .meta b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pending progress { width: 100%; height: 4px; accent-color: var(--accent); }
.drop-hint {
  position: absolute; inset: 8px; border: 2px dashed var(--accent); border-radius: 14px;
  display: grid; place-items: center; font-size: 20px; font-weight: 700; color: var(--accent);
  background: color-mix(in srgb, var(--bg-2) 85%, transparent); pointer-events: none; z-index: 5;
}

/* ---------------------------------------------------------------- voice */
.voice-view { flex: 1; display: flex; flex-direction: column; min-height: 0; background: var(--bg-0); }
.tiles {
  flex: 1; min-height: 0; padding: 16px; display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr));
  grid-auto-rows: minmax(160px, 1fr); align-content: center; overflow-y: auto;
}
.tiles[data-count="1"] { grid-template-columns: minmax(0, 900px); justify-content: center; }
.tile {
  position: relative; border-radius: 14px; background: var(--bg-2); overflow: hidden;
  display: grid; place-items: center; min-height: 160px; aspect-ratio: 16 / 9; max-height: 100%;
  margin: auto; width: 100%;
}
.tile::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; z-index: 2;
  box-shadow: inset 0 0 0 3px transparent; transition: box-shadow .12s;
}
.tile.speaking::after { box-shadow: inset 0 0 0 3px var(--speaking); }
.tile .label, .tile .state { z-index: 3; }
.tile video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; display: none; }
.tile.has-video video { display: block; }
.tile.has-video .avatar { display: none; }
.tile video.mirror { transform: scaleX(-1); }
.tile .label {
  position: absolute; left: 10px; bottom: 10px; display: flex; align-items: center; gap: 6px;
  background: rgba(0, 0, 0, .55); color: #fff; font-size: 13px; font-weight: 600;
  padding: 3px 8px; border-radius: 6px; max-width: calc(100% - 20px);
}
.tile .label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .state { position: absolute; top: 10px; right: 10px; font-size: 12px; color: #fff; background: rgba(0,0,0,.55); padding: 2px 8px; border-radius: 6px; }
.voice-controls { display: flex; justify-content: center; gap: 14px; padding: 14px 16px 20px; }
.round {
  width: 52px; height: 52px; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--bg-3); color: var(--text);
}
.round:hover { filter: brightness(1.15); }
.round.on { background: var(--text); color: var(--bg-0); }
.round.off { background: var(--danger); color: #fff; }
.round.danger { background: var(--danger); color: #fff; }

/* ---------------------------------------------------------------- members */
.members { background: var(--bg-1); overflow-y: auto; padding: 8px; }
.member { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 6px; }
.member:hover { background: var(--hover); }
.member.offline { opacity: .45; }
.member .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.member .crown { font-size: 11px; color: var(--accent); margin-left: auto; }

/* ---------------------------------------------------------------- dialogs, toasts */
dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0;
  background: var(--bg-2); color: var(--text); width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
dialog form { display: grid; gap: 14px; padding: 22px; }
dialog h2 { margin: 0; font-size: 18px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
.seg { display: flex; gap: 8px; }
.seg label { flex: 1; display: flex; align-items: center; gap: 8px; background: var(--bg-1); padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line); cursor: pointer; }
.seg label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.invite-box { display: grid; gap: 6px; font-size: 13px; background: var(--bg-1); padding: 12px; border-radius: 10px; }
.invite-row { display: flex; align-items: center; gap: 10px; }
.invite-row code { flex: 1; font-family: var(--mono); font-size: 15px; user-select: all; }

.toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 100; width: max-content; max-width: calc(100vw - 32px); }
.toast {
  background: var(--bg-4); color: var(--text); padding: 10px 16px; border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .35); font-size: 14px; animation: toast-in .18s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

.lightbox {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .85); display: grid; place-items: center;
  z-index: 90; cursor: zoom-out; padding: 24px;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* ---------------------------------------------------------------- narrow screens */
.scrim { display: none; }
@media (max-width: 1000px) {
  .app, .app.no-members { grid-template-columns: 248px minmax(0, 1fr); }
  .members { position: fixed; right: 0; top: 52px; bottom: 0; width: 240px; z-index: 20; box-shadow: -8px 0 24px rgba(0,0,0,.3); }
  .app:not(.show-members) .members { display: none; }
}
@media (max-width: 720px) {
  .app, .app.no-members { grid-template-columns: minmax(0, 1fr); }
  .only-narrow { display: inline-grid; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: min(300px, 85vw); z-index: 30;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .app.show-sidebar .sidebar { transform: none; }
  .app.show-sidebar .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 25; }
  .msg { padding-right: 12px; grid-template-columns: 52px minmax(0, 1fr); }
  .msg-actions { right: 8px; }
  .composer { padding: 0 8px 4px; }
  .tiles { padding: 8px; }
}
