:root {
  --bg: #eef1f5;
  --panel: #ffffff;
  --panel-2: #f5f7fa;
  --border: #e3e7ed;
  --text: #17202b;
  --muted: #6b7684;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --accent-soft: #e6efff;
  --bubble-in: #ffffff;
  --bubble-out: #dcf3d8;
  --bubble-out-text: #17202b;
  --chat-bg: #dfe7ee;
  --danger: #e5484d;
  --online: #22c55e;
  --shadow: 0 8px 30px rgba(15, 23, 42, .14);
  --radius: 14px;
  color-scheme: light;
}
/* Тёмная тема: data-theme ставит скрипт в <head> (вручную или по настройке системы) */
:root[data-theme="dark"] {
    --bg: #0e1621;
    --panel: #17212b;
    --panel-2: #1f2b38;
    --border: #253241;
    --text: #e6edf3;
    --muted: #8a99a8;
    --accent: #5b9cf8;
    --accent-2: #3b82f6;
    --accent-soft: #1e3350;
    --bubble-in: #1f2b38;
    --bubble-out: #2b5278;
    --bubble-out-text: #eef5ff;
    --chat-bg: #0e1621;
    --shadow: 0 8px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
svg { width: 22px; height: 22px; fill: currentColor; }

/* ---------- Вход ---------- */
.auth { height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 360px; background: var(--panel); border-radius: 20px;
  padding: 28px 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.auth-card h1 { margin: 0; text-align: center; font-size: 22px; }
.logo { font-size: 44px; text-align: center; }
.logo.big { font-size: 72px; }
.tabs { display: flex; background: var(--panel-2); border-radius: 10px; padding: 4px; }
.tabs button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 8px; color: var(--muted); }
.tabs button.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.field, .auth-card input {
  border: 1px solid var(--border); background: var(--panel-2); border-radius: 10px; padding: 10px 12px; outline: none;
}
.field { width: 100%; }
.field:focus, .auth-card input:focus { border-color: var(--accent); }
.error { color: var(--danger); margin: 0; min-height: 1em; font-size: 13px; }
.primary {
  border: 0; background: var(--accent); color: #fff; border-radius: 10px; padding: 11px 16px; font-weight: 600;
}
.primary:hover { background: var(--accent-2); }
.primary:disabled { opacity: .5; cursor: default; }
.ghost { border: 1px solid var(--border); background: none; border-radius: 10px; padding: 10px 14px; }
.danger-btn { border: 0; background: none; color: var(--danger); padding: 10px 14px; border-radius: 10px; }
.danger-btn:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }

/* ---------- Раскладка ---------- */
.app { display: flex; height: 100%; }
.sidebar {
  width: 340px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-width: 0;
}
.side-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.me-btn { border: 0; background: none; padding: 0; border-radius: 50%; }
.search { flex: 1; min-width: 0; }
.search input {
  width: 100%; border: 0; background: var(--panel-2); border-radius: 20px; padding: 8px 14px; outline: none;
}
.icon-btn {
  border: 0; background: none; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  color: var(--muted); flex-shrink: 0; cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.chat-list { flex: 1; overflow-y: auto; padding: 6px; }
.list-label { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 10px 4px; }
.chat-item {
  display: flex; gap: 12px; align-items: center; padding: 8px 10px; border-radius: 12px; cursor: pointer; width: 100%;
  border: 0; background: none; text-align: left;
}
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent); color: #fff; }
.chat-item.active .muted, .chat-item.active .time { color: rgba(255,255,255,.8); }
.chat-item .body { flex: 1; min-width: 0; }
.chat-item .row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.chat-item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .preview { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.muted { color: var(--muted); }
.badge {
  background: var(--accent); color: #fff; border-radius: 11px; min-width: 22px; height: 22px; padding: 0 7px;
  font-size: 12px; font-weight: 600; display: grid; place-items: center; flex-shrink: 0;
}
.chat-item.active .badge { background: #fff; color: var(--accent); }
.empty-list { text-align: center; color: var(--muted); padding: 30px 16px; }

/* ---------- Аватарки ---------- */
.avatar {
  position: relative; border-radius: 50%; flex-shrink: 0; display: grid; place-items: center;
  color: #fff; font-weight: 600; user-select: none; background-size: cover; background-position: center;
}
.avatar img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.avatar .dot {
  position: absolute; right: 0; bottom: 0; width: 28%; height: 28%; min-width: 10px; min-height: 10px;
  border-radius: 50%; background: var(--online); border: 2px solid var(--panel);
}

/* ---------- Чат ---------- */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chat-bg); }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 20px; }
.chat-inner { display: flex; flex-direction: column; height: 100%; }
.chat-head {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px; background: var(--panel);
  border-bottom: 1px solid var(--border); min-height: 58px;
}
.chat-head .back { display: none; }
.chat-head-info { display: flex; align-items: center; gap: 12px; border: 0; background: none; padding: 4px 6px; border-radius: 10px; min-width: 0; text-align: left; }
.chat-head-info:hover { background: var(--panel-2); }
.chat-head-text { display: flex; flex-direction: column; min-width: 0; }
.chat-head-text b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head-text small { color: var(--muted); }
.chat-head-text small.accent { color: var(--accent); }

.messages { flex: 1; overflow-y: auto; padding: 12px 16px 6px; display: flex; flex-direction: column; gap: 2px; }
.day-sep { align-self: center; background: rgba(0,0,0,.18); color: #fff; font-size: 12px; padding: 3px 10px; border-radius: 12px; margin: 10px 0; }
.sys-msg { align-self: center; background: rgba(0,0,0,.14); color: #fff; font-size: 13px; padding: 4px 12px; border-radius: 12px; margin: 6px 0; text-align: center; max-width: 80%; }
:root[data-theme="dark"] .day-sep, :root[data-theme="dark"] .sys-msg { background: rgba(255,255,255,.08); color: var(--muted); }

.msg-row { display: flex; align-items: flex-end; gap: 8px; max-width: min(620px, 85%); }
.msg-row.out { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.grouped { margin-top: 0; }
.msg-row:not(.grouped) { margin-top: 6px; }
.msg-row .avatar-slot { width: 34px; flex-shrink: 0; }
.bubble {
  position: relative; background: var(--bubble-in); padding: 6px 10px 6px; border-radius: var(--radius);
  box-shadow: 0 1px 1px rgba(0,0,0,.08); min-width: 70px; word-wrap: break-word; overflow-wrap: anywhere;
}
.out .bubble { background: var(--bubble-out); color: var(--bubble-out-text); }
.bubble .author { font-size: 13px; font-weight: 600; margin-bottom: 2px; cursor: pointer; }
.bubble .text { white-space: pre-wrap; }
.bubble .text a { color: var(--accent); }
.bubble .meta { float: right; margin: 6px 0 -4px 10px; font-size: 11px; color: var(--muted); display: flex; gap: 4px; align-items: center; user-select: none; }
.out .bubble .meta { color: color-mix(in srgb, var(--bubble-out-text) 60%, transparent); }
.meta .ticks { font-size: 12px; letter-spacing: -3px; padding-right: 3px; }
.meta .ticks.read { color: var(--accent); }
:root[data-theme="dark"] .out .meta .ticks.read { color: #8fd3ff; }
.bubble.deleted .text { font-style: italic; color: var(--muted); }
.bubble img.photo { display: block; max-width: 100%; max-height: 340px; border-radius: 10px; margin: 2px 0 4px; cursor: zoom-in; }
.reply-quote {
  border-left: 3px solid var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent);
  padding: 3px 8px; border-radius: 6px; margin: 2px 0 4px; font-size: 13px; cursor: pointer;
}
.reply-quote b { display: block; color: var(--accent); font-size: 12px; }
.reply-quote span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 360px; }
.msg-row.flash .bubble { animation: flash 1.2s; }
@keyframes flash { 0%, 60% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 1px 1px rgba(0,0,0,.08); } }

.typing-row { font-size: 13px; color: var(--muted); padding: 2px 4px 4px; min-height: 22px; }

/* ---------- Поле ввода ---------- */
.composer-wrap { background: var(--panel); border-top: 1px solid var(--border); }
.context-bar { display: flex; align-items: center; gap: 10px; padding: 8px 12px 0 16px; }
.context-bar > div { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 10px; }
.context-bar b { display: block; color: var(--accent); font-size: 13px; }
.context-bar span { display: block; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; }
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none; background: var(--panel-2); border-radius: 20px;
  padding: 9px 14px; max-height: 160px; line-height: 1.4;
}
.send {
  border: 0; width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; flex-shrink: 0;
}
.send:hover { background: var(--accent-2); }
.send svg { width: 20px; height: 20px; }

/* ---------- Меню и модалки ---------- */
.menu {
  position: fixed; z-index: 50; background: var(--panel); border-radius: 12px; box-shadow: var(--shadow);
  padding: 6px; min-width: 170px; border: 1px solid var(--border);
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 12px; border-radius: 8px; }
.menu button:hover { background: var(--panel-2); }
.menu button.danger { color: var(--danger); }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 40; padding: 16px;
  animation: fade .15s;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: 100%; max-width: 420px; max-height: calc(100vh - 32px); overflow-y: auto; background: var(--panel);
  border-radius: 18px; box-shadow: var(--shadow); padding: 20px; display: flex; flex-direction: column; gap: 14px;
}
.modal h2 { margin: 0; font-size: 18px; }
.modal .center { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.avatar-edit { position: relative; cursor: pointer; border-radius: 50%; }
.avatar-edit::after {
  content: "📷"; position: absolute; inset: 0; border-radius: 50%; background: rgba(0,0,0,.45);
  display: grid; place-items: center; font-size: 26px; opacity: 0; transition: opacity .15s;
}
.avatar-edit:hover::after { opacity: 1; }
.info-row { display: flex; flex-direction: column; padding: 8px 0; border-bottom: 1px solid var(--border); }
.info-row small { color: var(--muted); }
.member { display: flex; flex-direction: row; font-size: inherit; color: inherit; align-items: center; gap: 10px; padding: 6px 4px; border-radius: 10px; cursor: pointer; }
.member:hover { background: var(--panel-2); }
.member .body { flex: 1; min-width: 0; }
.member .body small { display: block; color: var(--muted); }
.member input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.pick-list { max-height: 280px; overflow-y: auto; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { background: var(--accent-soft); color: var(--accent); border-radius: 14px; padding: 3px 10px; font-size: 13px; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); z-index: 60; display: grid; place-items: center; cursor: zoom-out; }
.lightbox img { max-width: 95vw; max-height: 95vh; border-radius: 8px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #222c37; color: #fff;
  padding: 10px 16px; border-radius: 10px; z-index: 70; box-shadow: var(--shadow); max-width: calc(100% - 32px);
}

/* ---------- Мобильная версия ---------- */
@media (max-width: 760px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat { display: flex; }
  .chat-head .back { display: grid; }
  .messages { padding: 10px 10px 4px; }
  .msg-row { max-width: 92%; }
}
