:root {
  --bg: #0e1621;
  --bg-panel: #17212b;
  --bg-hover: #202b36;
  --bg-input: #242f3d;
  --bg-own: #2b5278;
  --text: #f5f5f5;
  --text-dim: #7d8b99;
  --accent: #64b5f6;
  --accent-strong: #2196f3;
  --red: #e53935;
  --green: #4caf50;
  --radius: 14px;
  --radius-sm: 8px;
}

/* ===== ТЕМЫ (ОФОРМЛЕНИЕ) ===== */
body.theme-dark, body.theme-night {
  --bg: #0e1621; --bg-panel: #17212b; --bg-hover: #202b36; --bg-input: #242f3d;
  --bg-own: #2b5278; --text: #f5f5f5; --text-dim: #7d8b99;
  --accent: #64b5f6; --accent-strong: #2196f3;
}
body.theme-light {
  --bg: #f5f5f5; --bg-panel: #ffffff; --bg-hover: #e4e4e5; --bg-input: #ebebed;
  --bg-own: #d3efff; --text: #1c2733; --text-dim: #6b7a88;
  --accent: #168acd; --accent-strong: #0f6fc2;
}
body.theme-blue {
  --bg: #0a2740; --bg-panel: #0f3352; --bg-hover: #174264; --bg-input: #1d4b6f;
  --bg-own: #105a8f; --text: #eaf6ff; --text-dim: #7fb2d6;
  --accent: #4fc3f7; --accent-strong: #0288d1;
}
body.theme-green {
  --bg: #0c2417; --bg-panel: #123620; --bg-hover: #1a4529; --bg-input: #1f5231;
  --bg-own: #1b6b3a; --text: #eafff0; --text-dim: #7fc99a;
  --accent: #81c784; --accent-strong: #43a047;
}
body.theme-purple {
  --bg: #1d132e; --bg-panel: #2a1b40; --bg-hover: #37234f; --bg-input: #3f2a5a;
  --bg-own: #5c3a8f; --text: #f4ecff; --text-dim: #a98ad1;
  --accent: #b388ff; --accent-strong: #7c4dff;
}
body.theme-oled {
  --bg: #000000; --bg-panel: #0b0b0d; --bg-hover: #151519; --bg-input: #1c1c21;
  --bg-own: #1d3d55; --text: #f0f0f0; --text-dim: #6d7a88;
  --accent: #55a7ee; --accent-strong: #1e88e5;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  transition: background-color .25s ease;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

.hidden { display: none !important; }

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea { font-family: inherit; }

/* ===== Auth ===== */
.auth-view {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1b3447 0%, var(--bg) 60%);
  padding: 16px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-panel);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.auth-logo { font-size: 56px; text-align: center; }
.auth-card h1 { text-align: center; font-size: 28px; margin-top: 8px; }
.auth-sub { text-align: center; color: var(--text-dim); margin-bottom: 24px; }
.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab {
  flex: 1; padding: 10px; border-radius: var(--radius-sm);
  background: var(--bg-input); color: var(--text-dim); font-weight: 600;
}
.tab.active { background: var(--accent-strong); color: #fff; }
.auth-card input {
  width: 100%; padding: 12px 14px; margin-bottom: 12px;
  background: var(--bg-input); border: 1px solid transparent; border-radius: var(--radius-sm);
  color: var(--text); font-size: 15px; outline: none;
}
.auth-card input:focus { border-color: var(--accent); }
.btn {
  width: 100%; padding: 12px; border-radius: var(--radius-sm);
  font-size: 15px; font-weight: 700;
}
.btn.primary { background: var(--accent-strong); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.cancel { background: var(--bg-input); color: var(--text-dim); }
.auth-error { margin-top: 12px; padding: 10px; background: rgba(229,57,53,.15); color: #ff8a80; border-radius: var(--radius-sm); font-size: 14px; }

/* ===== Layout ===== */
.app-view { display: flex; height: 100dvh; }
.sidebar {
  width: 320px; min-width: 280px;
  background: var(--bg-panel);
  display: flex; flex-direction: column;
  border-right: 1px solid rgba(255,255,255,.06);
}
.sidebar-header { display: flex; align-items: center; gap: 8px; padding: 12px; border-bottom: 1px solid rgba(255,255,255,.06); }
.profile-btn {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px; color: #fff; text-transform: uppercase;
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-input); font-size: 18px; color: var(--accent);
}
.icon-btn:hover { background: var(--bg-hover); }
.search-wrap { padding: 10px 12px; }
.search-wrap input {
  width: 100%; padding: 10px 14px; background: var(--bg-input);
  border: 1px solid transparent; border-radius: 20px; color: var(--text); outline: none; font-size: 14px;
}
.search-wrap input:focus { border-color: var(--accent); }

.chats-list { flex: 1; overflow-y: auto; }
.chats-list::-webkit-scrollbar, .messages::-webkit-scrollbar { width: 5px; }
.chats-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 3px; }

.chat-item {
  display: flex; gap: 12px; padding: 10px 12px; align-items: center;
  border-bottom: 1px solid rgba(255,255,255,.04); cursor: pointer;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-hover); }
.chat-item .avatar { width: 46px; height: 46px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; position: relative; }
.chat-item .avatar .dot {
  position: absolute; bottom: 1px; right: 1px; width: 12px; height: 12px;
  border-radius: 50%; border: 2px solid var(--bg-panel); background: #666;
}
.chat-item .avatar .dot.online { background: var(--green); }
.chat-item-body { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-item-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { font-size: 12px; color: var(--text-dim); flex-shrink: 0; }
.chat-item-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.chat-item-preview { font-size: 13px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-preview.draft { color: var(--accent); }
.unread-badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent-strong); color: #fff; font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.chat-item .tick { color: var(--text-dim); font-size: 11px; }

/* ===== Chat area ===== */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.chat-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-dim); gap: 8px; }
.chat-empty-icon { font-size: 64px; opacity: .5; }
.chat-empty-title { font-size: 20px; color: var(--text); }
.chat-empty-sub { font-size: 14px; }

.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; height: 100dvh; }
.chat-header {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid rgba(255,255,255,.06); background: var(--bg-panel);
}
.chat-header-info { display: flex; align-items: center; gap: 10px; cursor: pointer; min-width: 0; }
.chat-header .avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex-shrink: 0; }
.chat-header-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-header-status { font-size: 12px; color: var(--text-dim); }
.chat-members-count { margin-left: auto; font-size: 13px; color: var(--text-dim); flex-shrink: 0; }

.messages { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 4px; }
.date-divider {
  align-self: center; text-align: center; background: var(--bg-panel);
  color: var(--text-dim); font-size: 12px; padding: 5px 14px; border-radius: 12px; margin: 10px 0;
}
.msg-row { display: flex; }
.msg-row.own { justify-content: flex-end; }
.msg-bubble {
  max-width: 70%; padding: 7px 12px 6px; border-radius: var(--radius);
  background: var(--bg-panel); position: relative; word-wrap: break-word; overflow-wrap: break-word;
  display: flex; flex-direction: column;
}
.msg-row.own .msg-bubble { background: var(--bg-own); border-top-right-radius: 3px; }
.msg-row.peer .msg-bubble { border-top-left-radius: 3px; }
.msg-row.grouped .msg-bubble { border-radius: var(--radius); margin-top: 2px; }
.msg-row.grouped.own .msg-bubble { border-top-right-radius: 3px; }
.msg-row.grouped.peer .msg-bubble { border-top-left-radius: 3px; }
.msg-row.first.own .msg-bubble { margin-top: 8px; }
.msg-row.first.peer .msg-bubble { margin-top: 8px; }
.msg-sender { font-size: 13px; font-weight: 600; color: var(--accent); margin-bottom: 2px; cursor: pointer; }
.msg-text { font-size: 15px; line-height: 1.35; white-space: pre-wrap; }
.msg-meta {
  align-self: flex-end; display: flex; align-items: center; gap: 4px;
  color: var(--text-dim); font-size: 11px; margin-top: 2px; padding-top: 1px;
}
.msg-row.own .msg-meta { color: rgba(255,255,255,.55); }
.msg-meta .tick { font-size: 14px; line-height: 1; }
.msg-meta .tick.read { color: var(--accent); }
.msg-file { display: flex; align-items: center; gap: 10px; padding: 8px 10px; margin-bottom: 4px; background: rgba(255,255,255,.06); border-radius: var(--radius-sm); cursor: pointer; min-width: 210px; }
.msg-file .file-icon { font-size: 26px; }
.msg-file .file-name { font-size: 14px; font-weight: 600; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-file .file-size { font-size: 12px; color: var(--text-dim); }
.msg-image { max-width: 320px; width: 100%; border-radius: var(--radius-sm); display: block; margin-bottom: 4px; cursor: zoom-in; }
.msg-image-full {
  position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center; cursor: zoom-out;
}
.msg-image-full img { max-width: 95vw; max-height: 95vh; border-radius: 6px; }

.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px;
  background: var(--bg-panel); border-top: 1px solid rgba(255,255,255,.06);
}
.composer-input-wrap { flex: 1; background: var(--bg-input); border-radius: 20px; padding: 8px 14px; }
#msg-input { width: 100%; background: none; border: none; outline: none; color: var(--text); font-size: 15px; resize: none; max-height: 120px; line-height: 1.4; }
.send-btn { background: var(--accent-strong); color: #fff; border-radius: 50%; font-size: 16px; }
.send-btn:hover { filter: brightness(1.15); }

.typing-indicator { padding: 4px 20px; font-size: 13px; color: var(--accent); font-style: italic; }

/* ===== Смайлики и стикеры ===== */
.msg-shell { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.emoji-panel {
  background: var(--bg-panel); border-top: 1px solid rgba(255,255,255,.08);
  padding: 8px; max-height: 240px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px;
}
.emoji-tabs { display: flex; gap: 6px; border-bottom: 1px solid rgba(255,255,255,.08); padding-bottom: 6px; position: sticky; top: 0; background: var(--bg-panel); }
.etab { padding: 6px 14px; border-radius: 12px; font-size: 13px; color: var(--text-dim); background: var(--bg-input); }
.etab.active { background: var(--accent-strong); color: #fff; }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); gap: 2px; }
.emoji-cell { font-size: 24px; padding: 4px; border-radius: 6px; text-align: center; line-height: 1.2; }
.emoji-cell:hover { background: var(--bg-hover); }
.sticker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 6px; }
.sticker-cell { font-size: 44px; padding: 8px; border-radius: 10px; background: var(--bg-input); }
.sticker-cell:hover { background: var(--bg-hover); transform: scale(1.08); }
.msg-sticker { font-size: 64px; line-height: 1.1; padding: 2px 6px; user-select: none; }

/* ===== Modal ===== */
.modal-overlay { position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: var(--bg-panel); border-radius: 16px; width: 100%; max-width: 420px; max-height: 85vh; display: flex; flex-direction: column; overflow: hidden; }
.modal-header { padding: 16px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid rgba(255,255,255,.08); }
.modal-header h3 { font-size: 17px; }
.modal-body { padding: 16px; overflow-y: auto; }
.modal-body input, .modal-body textarea {
  width: 100%; padding: 10px 12px; margin-bottom: 12px; background: var(--bg-input);
  border: 1px solid transparent; border-radius: var(--radius-sm); color: var(--text); outline: none; font-size: 14px; resize: vertical;
}
.modal-body input:focus, .modal-body textarea:focus { border-color: var(--accent); }
.modal-actions { padding: 12px 16px; display: flex; gap: 8px; border-top: 1px solid rgba(255,255,255,.08); }
.modal-actions .btn { width: auto; flex: 1; }
.user-list-item { display: flex; align-items: center; gap: 12px; padding: 9px 8px; border-radius: var(--radius-sm); cursor: pointer; }
.user-list-item:hover { background: var(--bg-hover); }
.user-list-item .avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex-shrink: 0; }
.user-list-item .name { font-weight: 600; font-size: 15px; }
.user-list-item .sub { font-size: 12px; color: var(--text-dim); }
.checkbox { margin-left: auto; }
.group-badge { width: 46px; height: 46px; border-radius: 50%; background: var(--bg-input); display: flex; align-items: center; justify-content: center; font-size: 22px; flex-shrink: 0; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-panel); color: var(--text); padding: 12px 18px; border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,.5); z-index: 300; font-size: 14px;
  border-left: 3px solid var(--accent);
}
.toast.error { border-left-color: var(--red); }

/* ===== Папки чатов ===== */
.folders-bar {
  display: flex; gap: 6px; padding: 6px 12px; overflow-x: auto; flex-shrink: 0;
  border-bottom: 1px solid rgba(255,255,255,.06);
}
.folders-bar::-webkit-scrollbar { height: 3px; }
.folder-chip {
  flex-shrink: 0; padding: 6px 14px; border-radius: 16px; background: var(--bg-input);
  color: var(--text-dim); font-size: 13px; font-weight: 600; white-space: nowrap;
}
.folder-chip.active { background: var(--accent-strong); color: #fff; }
.folder-chip .fcount { opacity: .75; font-size: 11px; margin-left: 5px; }
.folder-manage {
  flex-shrink: 0; width: 28px; height: 28px; border-radius: 50%; background: var(--bg-input);
  display: flex; align-items: center; justify-content: center; font-size: 14px; color: var(--accent);
}

/* ===== Кнопки чата ===== */
#chat-back { flex-shrink: 0; }
.chat-header { gap: 8px; }
.chat-more-btn { flex-shrink: 0; }

/* темы в модалке */
.theme-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.theme-card {
  border-radius: 12px; overflow: hidden; cursor: pointer; border: 2px solid transparent;
  background: var(--bg-panel); text-align: center; padding: 0 0 10px;
}
.theme-card.active { border-color: var(--accent-strong); }
.theme-swatches { height: 44px; display: flex; flex-direction: column; }
.theme-card-name { font-size: 12px; color: var(--text-dim); padding-top: 6px; }

/* ===== Тайпинг анимация (как Telegram) ===== */
@keyframes td {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-3px); opacity: 1; }
}
.typing-dots { display: inline-flex; gap: 3px; align-items: flex-end; margin-left: 4px; }
.typing-dots i {
  width: 5px; height: 5px; border-radius: 50%; background: var(--accent); display: inline-block;
  animation: td 1.2s infinite;
}
.typing-dots i:nth-child(2) { animation-delay: .15s; }
.typing-dots i:nth-child(3) { animation-delay: .3s; }
.typing-title { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--accent); font-style: normal; }
.typing-inline { text-align: center; padding: 8px 0; color: var(--accent); font-size: 13px; display: flex; justify-content: center; align-items: center; }

/* ===== Аватары в сообщениях ===== */
.msg-avatar { display: block; font-size: 26px; margin-right: 6px; width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0; align-self: flex-end; position: relative; overflow: visible; padding: 0; }
.msg-avatar .dot { width: 10px; height: 10px; bottom: -1px; right: -1px; border-width: 2px; border-color: var(--bg); }
.msg-row { align-items: flex-end; gap: 0; }
.msg-row.grouped { margin-top: 2px; }
.msg-row.first { margin-top: 8px; }

/* ===== Тумблеры ===== */
.mini {
  padding: 5px 12px; border-radius: 8px; font-size: 12px; background: var(--bg-input);
  color: var(--accent); cursor: pointer; border: none; white-space: nowrap;
}
.mini:hover { background: var(--bg-hover); }
.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid rgba(255,255,255,.06); }
.toggle-row:last-child { border-bottom: none; }
.toggle { position: relative; width: 46px; height: 26px; border-radius: 14px; background: var(--bg-input); transition: background .2s; }
.toggle::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: left .2s; }
.toggle.on { background: var(--accent-strong); }
.toggle.on::after { left: 23px; }

/* ===== Responsive ===== */
@media (max-width: 760px) {
  .sidebar { width: 100%; }
  .sidebar.closed { display: none; }
  .chat-view.mobile-open { position: fixed; inset: 0; z-index: 50; width: 100%; height: 100dvh; background: var(--bg); }
  .msg-bubble { max-width: 85%; }
  .msg-image { max-width: 240px; }
  .sidebar-header { padding: 10px 8px; }
  .composer { padding: 6px 8px; }
  .emoji-panel { max-height: 38vh; }
  .msg-avatar { display: none !important; }
  .chat-header-info .avatar { width: 36px !important; height: 36px !important; }
}
