* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --accent: #4C7EF5;
  --accent-hover: #3A68D8;
  --bubble-mine: #4C7EF5;
  --bubble-theirs: #343C49;
  --sidebar: #1B1F27;
  --bg: #232833;
  --card: #232833;
  --card-selected: #3F6FE0;
  --divider: #2B3140;
  --muted: #8A93A5;
  --online: #3ED47F;
  --text: #EDF0F7;
  --danger: #E57373;
  --text-mine: #ffffff;
  --reply-text: #B9C4E8;
  --fs: 1; /* множитель размера шрифта (только текст) */
}
html, body { height: 100%; }
body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

/* ---- Экран входа ---- */
#auth-view {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
}
.auth-card {
  width: 340px; text-align: center; padding: 30px;
  background: var(--card); border-radius: 18px;
}
.logo {
  width: 88px; height: 88px; margin: 0 auto 14px;
  border-radius: 50%; background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: calc(26px * var(--fs)); color: #fff;
}
.auth-card h1 { font-size: calc(30px * var(--fs)); }
.auth-card .sub { color: var(--muted); margin: 4px 0 20px; }
.auth-card input {
  width: 100%; padding: 12px; margin-bottom: 10px;
  border: none; border-radius: 12px; background: var(--bubble-theirs);
  color: var(--text); font-size: calc(14px * var(--fs));
}
.btn-primary {
  width: 100%; padding: 13px; border: none; border-radius: 12px;
  background: var(--accent); color: #fff; font-size: calc(15px * var(--fs)); font-weight: 700; cursor: pointer;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-link { margin-top: 10px; background: none; border: none; color: #A9B2C2; cursor: pointer; font-size: calc(13px * var(--fs)); }
.btn-link:hover { text-decoration: underline; }
.error { color: var(--danger); font-size: calc(13px * var(--fs)); min-height: 18px; margin-top: 10px; }

/* ---- Лэйаут ---- */
#app-view { display: flex; height: 100vh; }
.sidebar {
  width: 330px; min-width: 260px; background: var(--sidebar);
  display: flex; flex-direction: column; border-right: 1px solid var(--divider);
}
.chat-panel { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.sidebar-header {
  display: flex; align-items: center; gap: 10px; padding: 14px 14px 8px;
}
.brand { font-size: calc(18px * var(--fs)); font-weight: 700; flex: 1; }
.btn-ghost {
  background: var(--divider); color: var(--text); border: none;
  padding: 7px 12px; border-radius: 10px; font-size: calc(13px * var(--fs)); font-weight: 700; cursor: pointer;
}
.btn-ghost:hover { background: var(--accent); color: #fff; }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--accent-hover); color: #fff; }

.avatar {
  width: 44px; height: 44px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; background: var(--accent); font-size: calc(16px * var(--fs)); flex-shrink: 0;
}
.avatar.small { width: 36px; height: 36px; font-size: calc(14px * var(--fs)); }

#search-input {
  margin: 4px 14px 8px; padding: 10px 12px;
  border: none; border-radius: 12px; background: var(--bubble-theirs); color: var(--text); font-size: calc(13px * var(--fs));
}
#search-results { margin: 0 8px 6px; background: var(--card); border-radius: 12px; overflow: hidden; }
.search-item {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; cursor: pointer;
}
.search-item:hover { background: var(--divider); }

#chat-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.chat-item {
  display: flex; align-items: center; gap: 10px; padding: 10px;
  border-radius: 12px; cursor: pointer; margin-bottom: 4px; background: var(--card);
}
.chat-item.selected { background: var(--card-selected); }
.chat-item:hover { background: var(--card-selected); }
.chat-item.selected .chat-item-name,
.chat-item.selected .chat-item-last { color: #fff; }
.chat-item-info { flex: 1; min-width: 0; }
.chat-item-name { font-weight: 700; font-size: calc(14px * var(--fs)); }
.chat-item-last { color: var(--muted); font-size: calc(12px * var(--fs)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread-badge {
  background: var(--accent); color: #fff; border-radius: 11px;
  min-width: 22px; height: 22px; padding: 0 6px; font-size: calc(11px * var(--fs)); font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.online-dot { color: var(--online); font-size: calc(10px * var(--fs)); }

/* ---- Шапка чата ---- */
.chat-header {
  display: flex; align-items: center; gap: 12px; padding: 12px 18px;
  background: var(--sidebar); min-height: 74px;
}
.chat-header-info { flex: 1; }
#chat-title { font-size: calc(17px * var(--fs)); font-weight: 700; }
#chat-status { color: var(--muted); font-size: calc(12px * var(--fs)); }
#chat-status.online { color: var(--online); }
.btn-danger {
  background: none; border: none; font-size: calc(18px * var(--fs)); cursor: pointer;
  padding: 6px 10px; border-radius: 10px; color: var(--danger);
}
.btn-danger:hover { background: #3A2230; }
.btn-danger:disabled { opacity: .4; cursor: default; }

/* ---- Сообщения ---- */
#messages-area { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
#empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: calc(18px * var(--fs)); text-align: center; }
.msg { max-width: 72%; display: flex; flex-direction: column; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }
.msg-bubble {
  padding: 8px 12px; border-radius: 16px; font-size: calc(14px * var(--fs)); line-height: 1.4;
  word-wrap: break-word; white-space: pre-wrap;
}
.msg.mine .msg-bubble { background: var(--bubble-mine); color: var(--text-mine); border-bottom-right-radius: 4px; }
.msg.theirs .msg-bubble { background: var(--bubble-theirs); color: var(--text); border-bottom-left-radius: 4px; }
.msg-meta { font-size: calc(10px * var(--fs)); color: var(--muted); margin-top: 2px; padding: 0 4px; }
.msg .read { color: var(--online); }
.msg-image { max-width: 260px; max-height: 260px; border-radius: 12px; cursor: pointer; }
.reply-quote {
  border-left: 3px solid var(--accent); background: rgba(0,0,0,.05);
  border-radius: 6px; padding: 3px 8px; margin-bottom: 4px; font-size: calc(12px * var(--fs)); color: var(--reply-text);
}
.reactions-bar { display: flex; gap: 4px; margin-top: 4px; }
.reaction-chip {
  background: var(--divider); border-radius: 12px; padding: 2px 8px; font-size: calc(12px * var(--fs)); cursor: pointer;
}
.reaction-chip:hover { background: var(--card-selected); }
.edited { color: var(--muted); font-size: calc(11px * var(--fs)); }

/* ---- Композер ---- */
.composer { background: var(--sidebar); padding: 0; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px; }
.btn-attach {
  width: 44px; height: 44px; border-radius: 50%; border: none;
  background: var(--accent); color: #fff; font-size: calc(24px * var(--fs)); font-weight: 700; cursor: pointer;
}
.btn-attach:hover { background: var(--accent-hover); }
.btn-attach:disabled, .btn-send:disabled { opacity: .4; cursor: default; }
#message-input {
  flex: 1; resize: none; background: var(--bubble-theirs); color: var(--text);
  border: none; border-radius: 16px; padding: 12px; font-size: calc(13px * var(--fs)); font-family: inherit; max-height: 120px;
}
.btn-send {
  width: 44px; height: 44px; border-radius: 50%; border: none;
  background: var(--accent); color: #fff; font-size: calc(18px * var(--fs)); cursor: pointer;
}
.btn-send:hover { background: var(--accent-hover); }
#reply-bar {
  display: flex; align-items: center; gap: 8px; margin: 8px 14px 0;
  background: var(--bubble-theirs); border-radius: 10px; padding: 6px 10px; font-size: calc(12px * var(--fs)); color: #B9C4E8;
}
#reply-text { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#reply-cancel { background: none; border: none; color: var(--muted); cursor: pointer; font-size: calc(14px * var(--fs)); }

/* Контекстное меню */
.ctx-menu {
  position: fixed; z-index: 1000; background: var(--bubble-theirs);
  border-radius: 10px; padding: 4px; box-shadow: 0 4px 20px rgba(0,0,0,.5); min-width: 180px;
}
.ctx-menu button {
  display: block; width: 100%; text-align: left; padding: 8px 12px;
  background: none; border: none; color: var(--text); font-size: calc(13px * var(--fs)); cursor: pointer; border-radius: 6px;
}
.ctx-menu button:hover { background: var(--accent); }

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: var(--divider); border-radius: 4px; }

/* ---- Светлая тема (Telegram-тоны) ---- */
body.light {
  --accent: #3390EC;
  --accent-hover: #2F7AE5;
  --bubble-mine: #3390EC;
  --bubble-theirs: #EEF1F5;
  --sidebar: #EFF2F5;
  --bg: #FFFFFF;
  --card: #FFFFFF;
  --card-selected: #3390EC;
  --online: #3ED47F;
  --muted: #707579;
  --divider: #E6E9EC;
  --text: #111111;
  --danger: #E53935;
  --text-mine: #FFFFFF;
  --reply-text: #4F6AA8;
}
.msg.mine .reply-quote { color: rgba(255,255,255,.9); background: rgba(0,0,0,.15); }
.msg.mine .msg-meta { color: var(--muted); }
body.light .btn-danger:hover { background: #FDE7E7; }
body.light input, body.light textarea { color-scheme: light; }

/* ---- Окно настроек ---- */
.modal {
  position: fixed; inset: 0; z-index: 900;
  background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
}
.modal-card {
  width: 440px; max-width: 92vw; max-height: 88vh; overflow: auto;
  background: var(--card); border-radius: 16px; box-shadow: 0 8px 40px rgba(0,0,0,.3);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--divider);
}
.modal-head h2 { font-size: calc(20px * var(--fs)); }
.modal-body { padding: 16px 18px; }
.modal-body h3 { font-size: calc(14px * var(--fs)); font-weight: 700; margin: 10px 0 8px; color: var(--text); }
.modal-body hr { border: none; border-top: 1px solid var(--divider); margin: 14px 0; }
.theme-row { display: flex; gap: 10px; }
.btn-primary.mini { width: auto; flex: 1; }
.status { color: var(--online); font-size: calc(13px * var(--fs)); min-height: 18px; margin-top: 12px; }
.acc-line { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.acc-info { display: flex; flex-direction: column; }
.acc-name { font-weight: 700; }
.acc-sub { color: var(--muted); font-size: calc(12px * var(--fs)); }

/* ---- Настройки: подменю ---- */
.settings-card { width: 680px; }
.settings-body { display: flex; min-height: 380px; }
.settings-menu {
  width: 170px; border-right: 1px solid var(--divider);
  padding: 12px 8px; display: flex; flex-direction: column; gap: 4px;
}
.set-item {
  text-align: left; padding: 10px 12px; border: none; background: none;
  border-radius: 10px; color: var(--text); font-size: calc(14px * var(--fs)); font-weight: 600; cursor: pointer;
}
.set-item:hover { background: var(--divider); }
.set-item.active { background: var(--accent); color: #fff; }
.settings-content { flex: 1; padding: 14px 18px; overflow: auto; }
.set-panel h3 { margin-top: 0; }
.lbl { display: block; font-size: calc(13px * var(--fs)); color: var(--muted); margin: 12px 0 6px; }
.set-panel input[type="text"] {
  width: 100%; padding: 10px 12px; margin-bottom: 8px; border: none; border-radius: 10px;
  background: var(--bubble-theirs); color: var(--text); font-size: calc(14px * var(--fs));
}
.set-panel .btn-primary { margin-top: 4px; }
.danger-outline {
  background: none !important; color: var(--danger) !important;
  box-shadow: inset 0 0 0 1px var(--danger);
}
.danger-outline:hover { background: var(--danger) !important; color: #fff !important; }