:root {
  --bg: #0e1621;
  --panel: #17212b;
  --panel-2: #202b36;
  --panel-3: #2a3947;
  --text: #eaf1f7;
  --muted: #8195a8;
  --accent: #4a9eff;
  --accent-2: #2f7fe0;
  --out: #2b5278;
  --in: #182533;
  --danger: #ff6b6b;
  --border: rgba(255, 255, 255, .07);
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --radius: 14px;
}

[data-theme="light"] {
  --bg: #eef2f6;
  --panel: #ffffff;
  --panel-2: #f1f5f9;
  --panel-3: #e3eaf1;
  --text: #16212c;
  --muted: #6b7c8d;
  --accent: #2b7de9;
  --accent-2: #1d63c4;
  --out: #d6e9ff;
  --in: #ffffff;
  --border: rgba(16, 32, 48, .09);
  --shadow: 0 10px 30px rgba(20, 40, 60, .12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spacer { flex: 1; }

button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
  width: 100%;
}
input:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: none; }

/* ---------- Авторизация ---------- */

.auth {
  height: 100dvh;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(900px 500px at 80% -10%, rgba(74, 158, 255, .22), transparent 60%),
    radial-gradient(700px 500px at 0% 100%, rgba(124, 92, 255, .18), transparent 60%),
    var(--bg);
}

.auth-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 34px 28px 26px;
  box-shadow: var(--shadow);
}

.logo {
  font-size: 46px;
  font-weight: 800;
  letter-spacing: -1px;
  background: linear-gradient(135deg, #6fb6ff, #7c5cff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-align: center;
  line-height: 1;
}

.tagline {
  text-align: center;
  color: var(--muted);
  margin: 6px 0 24px;
  font-size: 14px;
}

.tabs {
  display: flex;
  background: var(--panel-2);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 18px;
}

.tab {
  flex: 1;
  border: 0;
  background: transparent;
  padding: 9px;
  border-radius: 9px;
  color: var(--muted);
  font-weight: 600;
}

.tab.active {
  background: var(--panel-3);
  color: var(--text);
}

.auth-form { display: grid; gap: 12px; }
.auth-form label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }

.primary {
  border: 0;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 11px;
  box-shadow: 0 6px 16px rgba(47, 127, 224, .28);
  transition: transform .08s ease, filter .15s ease;
}
.primary:hover { filter: brightness(1.06); }
.primary:active { transform: translateY(1px); }
.primary:disabled { opacity: .55; cursor: default; }

.ghost {
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 10px;
  padding: 9px 14px;
}
.ghost:hover { background: var(--panel-2); }
.ghost.danger { color: var(--danger); border-color: rgba(255, 107, 107, .35); }

.error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 8px 0 0; }

/* ---------- Каркас ---------- */

.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: 340px;
  flex: 0 0 340px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

.sidebar-head, .chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  min-height: 60px;
}

.brand { font-size: 20px; font-weight: 800; letter-spacing: -.5px; }
.head-actions { margin-left: auto; display: flex; gap: 4px; }

.icon-btn {
  border: 0;
  background: transparent;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--muted);
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.avatar, .avatar-btn, .avatar-xl {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 0;
  padding: 0;
  background: linear-gradient(135deg, #46688f, #2f7fe0);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
  overflow: hidden;
  flex: 0 0 auto;
  background-size: cover !important;
  background-position: center !important;
}
.avatar-xl { width: 74px; height: 74px; font-size: 26px; }

.search-box { padding: 10px 12px 6px; }
.search-box input { background: var(--panel-2); }

.search-results {
  position: absolute;
  top: 104px;
  left: 12px;
  right: 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-height: 55vh;
  overflow: auto;
  z-index: 20;
  padding: 6px;
}
.search-results.scroll { position: static; max-height: 42vh; margin-top: 10px; }

.user-item, .chat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
}
.user-item:hover, .chat-item:hover { background: var(--panel-3); }
.user-item .u-body { min-width: 0; }
.user-item .u-name { font-weight: 600; }
.chat-list { flex: 1; overflow-y: auto; padding: 6px; min-height: 0; }
.chat-list.compact { max-height: 50vh; }

.chat-item.active { background: var(--panel-3); }
.chat-body { flex: 1; min-width: 0; display: grid; gap: 2px; }
.chat-row { display: flex; align-items: center; gap: 8px; }
.chat-name { font-weight: 600; flex: 1; min-width: 0; }
.chat-time { font-size: 11.5px; color: var(--muted); flex: 0 0 auto; }
.chat-preview { font-size: 13px; color: var(--muted); }
.badge {
  background: var(--accent);
  color: #fff;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 700;
  padding: 2px 7px;
  min-width: 20px;
  text-align: center;
}
.online-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: #4cd964;
  border: 2px solid var(--panel);
  position: absolute;
  bottom: 0; right: 0;
}
.avatar-wrap { position: relative; flex: 0 0 auto; }

.conn-status {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-3);
  color: var(--muted);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 20px;
}

/* ---------- Чат ---------- */

.chat-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(1200px 700px at 70% 10%, rgba(74, 158, 255, .06), transparent 60%),
    var(--bg);
  position: relative;
}

.empty-chat {
  margin: auto;
  text-align: center;
  color: var(--muted);
  display: grid;
  gap: 14px;
  place-items: center;
  font-size: 14.5px;
}
.empty-logo { font-size: 56px; font-weight: 800; color: var(--panel-3); letter-spacing: -2px; }

.chat-view { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-title { min-width: 0; flex: 1; }
.chat-title > div:first-child { font-weight: 600; }
.back-btn { display: none; }
.chat-head .avatar { width: 40px; height: 40px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  scroll-behavior: smooth;
}

.msg-row { display: flex; gap: 8px; align-items: flex-end; max-width: 100%; }
.msg-row.out { justify-content: flex-end; }
.msg-avatar { width: 30px; height: 30px; font-size: 12px; margin-bottom: 2px; }

.msg {
  max-width: min(640px, 76%);
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.msg-row.out .msg { align-items: flex-end; }

.bubble {
  background: var(--in);
  border-radius: 14px;
  padding: 8px 11px 7px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .12);
  position: relative;
  min-width: 90px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
[data-theme="light"] .bubble { box-shadow: 0 1px 2px rgba(20, 40, 60, .1); }
.msg-row.out .bubble { background: var(--out); }

.msg-head { font-size: 12.5px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.msg-row.out .msg-head { display: none; }

.msg-text { white-space: pre-wrap; }
.msg-text a { color: var(--accent); }

.msg-foot {
  display: flex;
  align-items: center;
  gap: 5px;
  justify-content: flex-end;
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
  user-select: none;
}
.msg-row.out .msg-foot { color: rgba(255, 255, 255, .55); }
[data-theme="light"] .msg-row.out .msg-foot { color: #5883a8; }
.msg-status { letter-spacing: -2px; }

.reply-quote {
  border-left: 3px solid var(--accent);
  background: rgba(74, 158, 255, .1);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 3px;
  cursor: pointer;
}
.reply-quote b { color: var(--accent); font-weight: 600; }
.reply-quote .q-text { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.forwarded { font-size: 12px; color: var(--accent); margin-bottom: 2px; opacity: .9; }

.msg-media { display: grid; gap: 4px; margin: -2px -3px 2px; }
.media-item {
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel-3);
  max-width: 420px;
  position: relative;
}
.media-item img, .media-item video {
  display: block;
  max-width: 100%;
  max-height: 360px;
  width: auto;
  height: auto;
  cursor: pointer;
}
.media-item.video-wrap video { width: 100%; }

.reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
  align-self: flex-start;
}
.reaction {
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 20px;
  padding: 1px 7px;
  font-size: 12.5px;
  display: flex;
  gap: 4px;
  align-items: center;
  color: var(--text);
}
.reaction.mine { border-color: var(--accent); background: rgba(74, 158, 255, .16); }

.msg-menu-btn {
  position: absolute;
  top: -8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 24px; height: 24px;
  display: none;
  place-items: center;
  font-size: 14px;
  color: var(--muted);
  z-index: 3;
}
.msg-row:hover .msg-menu-btn { display: grid; }
.msg-row.in .msg-menu-btn { right: -28px; }
.msg-row.out .msg-menu-btn { left: -28px; }

.date-sep {
  display: flex;
  justify-content: center;
  margin: 10px 0 4px;
}
.date-sep span {
  background: var(--panel-3);
  color: var(--muted);
  font-size: 11.5px;
  padding: 3px 10px;
  border-radius: 20px;
}

.msg-menu {
  position: fixed;
  z-index: 60;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 6px;
  min-width: 190px;
  display: grid;
  gap: 2px;
}
.msg-menu button {
  border: 0;
  background: transparent;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 14px;
}
.msg-menu button:hover { background: var(--panel-3); }
.msg-menu .emoji-row { display: flex; gap: 2px; padding: 4px 3px 6px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.msg-menu .emoji-row button { font-size: 19px; padding: 2px 5px; }

.typing { height: 18px; padding: 0 22px 4px; }

.composer-wrap {
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
}

.composer { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  max-height: 160px;
  min-height: 42px;
  padding: 10px 12px;
  border-radius: 21px;
  background: var(--panel-2);
  overflow-y: auto;
}
.send-btn {
  width: 42px; height: 42px;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 18px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}
.send-btn:disabled { opacity: .5; }

.reply-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  border-left: 3px solid var(--accent);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 5px 10px;
  margin-bottom: 6px;
  font-size: 13px;
}
.reply-bar .rb-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-bar .rb-close { border: 0; background: transparent; color: var(--muted); font-size: 16px; }

.attachments {
  display: flex;
  gap: 8px;
  padding: 6px 2px;
  overflow-x: auto;
}
.att-item {
  position: relative;
  width: 74px; height: 74px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel-2);
  flex: 0 0 auto;
}
.att-item img, .att-item video { width: 100%; height: 100%; object-fit: cover; }
.att-item .att-remove {
  position: absolute; top: 2px; right: 2px;
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 0;
  background: rgba(0, 0, 0, .6);
  color: #fff;
  font-size: 13px;
  line-height: 1;
}
.att-badge {
  position: absolute; left: 4px; bottom: 4px;
  background: rgba(0, 0, 0, .6);
  color: #fff;
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 6px;
}

.upload-progress { padding: 4px 0 2px; }
.upload-progress .bar { height: 4px; background: var(--panel-3); border-radius: 3px; overflow: hidden; }
.upload-progress .bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .15s linear; }

/* ---------- Диалоги ---------- */

.dlg {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 16px;
  padding: 0;
  width: min(460px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.dlg::backdrop { background: rgba(3, 8, 14, .62); backdrop-filter: blur(2px); }
.dlg-body { padding: 18px; display: grid; gap: 12px; }
.dlg h3 { margin: 0; font-size: 18px; }
.dlg-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; flex-wrap: wrap; }

.profile-top { display: flex; gap: 14px; align-items: center; }
.profile-info { flex: 1; min-width: 0; display: grid; gap: 6px; }
.field { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 13px;
  display: flex;
  gap: 6px;
  align-items: center;
}
.chip button { border: 0; background: transparent; color: var(--muted); padding: 0; }

.chat-info { display: grid; gap: 12px; max-height: 55vh; overflow: auto; }
.member-row { display: flex; align-items: center; gap: 10px; }
.member-row .u-name { flex: 1; min-width: 0; }
.role-tag { font-size: 11px; color: var(--accent); }

/* ---------- Лайтбокс ---------- */

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .88);
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
}
.lightbox img, .lightbox video {
  max-width: 96vw;
  max-height: 92vh;
  border-radius: 8px;
}
.lightbox-close {
  position: absolute;
  top: 14px; right: 18px;
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, .12);
  color: #fff;
  font-size: 24px;
}

/* ---------- Скроллбары ---------- */

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--panel-3); border-radius: 4px; }

/* ---------- Мобильная версия ---------- */

@media (max-width: 820px) {
  .sidebar { width: 100%; flex: 1 0 100%; }
  .app.open-chat .sidebar { display: none; }
  .app:not(.open-chat) .chat-pane { display: none; }
  .back-btn { display: grid; }
  .msg { max-width: 84%; }
  .messages { padding: 12px 12px 6px; }
  .search-results { top: 112px; }
}

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