/*
 * Темы. Три состояния: «авто» (ничего не проставлено, решает система),
 * data-theme="light" и data-theme="dark" — выбор пользователя, он же в localStorage.
 * Светлая палитра живёт на голом :root, чтобы при любом сбое страница осталась читаемой.
 */
:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6ea;
  --text: #1c2430;
  --text-dim: #6b7683;
  --accent: #2f6fed;
  --accent-soft: #e8f0ff;
  --danger: #d64545;
  --danger-soft: #fdeaea;
  --ok: #2e9e5b;
  --warn: #c98a14;
  --bubble-in: #ffffff;
  --bubble-out: #e8f0ff;
  --backdrop: rgba(15, 20, 28, .45);
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 45, .06), 0 6px 20px rgba(20, 30, 45, .05);
  color-scheme: light;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* системная тёмная — пока пользователь не выбрал светлую явно */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181d;
    --surface: #1c2026;
    --surface-2: #22272e;
    --border: #2c333c;
    --text: #e6eaf0;
    --text-dim: #9aa5b1;
    --accent: #5b8dff;
    --accent-soft: #24314d;
    --danger: #ff6b6b;
    --danger-soft: #3a2226;
    --ok: #4ec27c;
    --warn: #e0a83a;
    --bubble-in: #232931;
    --bubble-out: #2a3a57;
    --backdrop: rgba(0, 0, 0, .6);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

/* явно выбранная тёмная — перекрывает системную светлую */
:root[data-theme="dark"] {
  --bg: #15181d;
  --surface: #1c2026;
  --surface-2: #22272e;
  --border: #2c333c;
  --text: #e6eaf0;
  --text-dim: #9aa5b1;
  --accent: #5b8dff;
  --accent-soft: #24314d;
  --danger: #ff6b6b;
  --danger-soft: #3a2226;
  --ok: #4ec27c;
  --warn: #e0a83a;
  --bubble-in: #232931;
  --bubble-out: #2a3a57;
  --backdrop: rgba(0, 0, 0, .6);
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

button, input, textarea, select { font: inherit; color: inherit; }

/* ── Кнопки и поля ───────────────────────────────────────────────────── */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-2); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--primary:hover { filter: brightness(1.06); background: var(--accent); }
.btn--ghost { border-color: transparent; background: transparent; color: var(--text-dim); padding: 5px 8px; }
.btn--ghost:hover { background: var(--surface-2); color: var(--text); }
.btn--danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

/* ── Иконки ──────────────────────────────────────────────────────────────
   Один штриховой набор вместо эмодзи: размер задаётся в em, поэтому иконка
   растёт вместе с текстом рядом, а цвет наследуется через currentColor. */
.icon {
  width: 1.25em; height: 1.25em; flex: none;
  fill: none; stroke: currentColor;
  stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round;
  vertical-align: -0.22em;
}
.icon--inline { width: 1em; height: 1em; opacity: .75; }
/* клик по значку должен считаться кликом по кнопке, а не по самому <svg> */
button .icon, a .icon { pointer-events: none; }
.icon--lg { width: 1.7em; height: 1.7em; }

/* фирменные цвета мессенджеров — по ним канал узнаётся быстрее, чем по подписи */
.icon--telegram { color: #2ea6da; }
.icon--whatsapp { color: #3fae4a; }
.icon--max { color: #7c5cf5; }
.icon--avito { color: #d4a017; }
/* на акцентной подложке фирменный цвет читался бы плохо */
.nav-item--active .icon { color: inherit; }

.icon-btn {
  border: 0;
  background: transparent;
  cursor: pointer;
  border-radius: 7px;
  padding: 5px;
  color: var(--text-dim);
  font-size: 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn .icon { width: 17px; height: 17px; vertical-align: 0; }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field > span { font-size: 12px; color: var(--text-dim); }
.field input, .field select, .field textarea {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  background: var(--surface);
  outline: none;
}
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }

/* ── Вход ────────────────────────────────────────────────────────────── */
.login { display: grid; place-items: center; height: 100vh; background: var(--bg); }
.login__card {
  background: var(--surface);
  padding: 32px;
  border-radius: 14px;
  box-shadow: var(--shadow);
  width: 340px;
  border: 1px solid var(--border);
}
.login__logo { color: var(--accent); margin-bottom: 10px; }
.login__logo .icon { width: 34px; height: 34px; stroke-width: 1.7; }
.login__title { margin: 0 0 4px; font-size: 22px; }
.login__hint { margin: 0 0 22px; color: var(--text-dim); font-size: 13px; }
.login__card .btn { width: 100%; }
.login__error {
  background: var(--danger-soft); color: var(--danger); border-radius: 8px;
  padding: 8px 11px; font-size: 13px; margin-bottom: 12px;
}

/* ── Каркас ──────────────────────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: 224px 320px minmax(0, 1fr) 0;
  height: 100vh;
}
.app.app--contact { grid-template-columns: 224px 320px minmax(0, 1fr) 300px; }

/* ── Левая колонка ───────────────────────────────────────────────────── */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar__brand {
  display: flex; align-items: center; gap: 9px;
  padding: 16px 16px 12px; font-weight: 600; font-size: 15px;
}
.sidebar__logo { display: inline-flex; color: var(--accent); }
.sidebar__logo .icon { width: 22px; height: 22px; stroke-width: 1.8; }
.sidebar__nav, .sidebar__section { padding: 0 8px; }
.sidebar__section { margin-top: 16px; overflow-y: auto; }
.sidebar__caption {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-dim); padding: 6px 8px;
}
.nav-item {
  display: flex; align-items: center; gap: 9px;
  padding: 7px 9px; border-radius: 8px; cursor: pointer;
  color: var(--text); text-decoration: none; font-size: 13.5px;
}
.nav-item__icon { display: inline-flex; color: var(--text-dim); }
.nav-item__icon .icon { width: 17px; height: 17px; vertical-align: 0; }
.nav-item:hover { background: var(--surface-2); }
.nav-item--active { background: var(--accent-soft); color: var(--accent); font-weight: 500; }
.nav-item__count {
  margin-left: auto; font-size: 11.5px; color: var(--text-dim);
  background: var(--surface-2); border-radius: 20px; padding: 1px 7px;
}
.nav-item--active .nav-item__count { background: var(--surface); color: var(--accent); }
.nav-item__dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }

.sidebar__footer { margin-top: auto; border-top: 1px solid var(--border); padding: 10px 12px; }
.sidebar__user { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 6px; }
.sidebar__actions { display: flex; gap: 4px; }

/* ── Список диалогов ─────────────────────────────────────────────────── */
.list {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}
.list__header { padding: 12px; border-bottom: 1px solid var(--border); }
.list__header input {
  width: 100%; border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 11px; background: var(--surface-2); outline: none;
}
.list__header input:focus { border-color: var(--accent); background: var(--surface); }
.list__title { font-size: 12px; color: var(--text-dim); margin-top: 9px; }
.list__items { overflow-y: auto; flex: 1; }

.conv {
  display: grid; grid-template-columns: 36px minmax(0, 1fr); gap: 10px;
  padding: 11px 12px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.conv:hover { background: var(--surface-2); }
.conv--active { background: var(--accent-soft); }
.conv--active:hover { background: var(--accent-soft); }
.conv__top { display: flex; align-items: baseline; gap: 8px; }
.conv__name { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv__time { margin-left: auto; font-size: 11px; color: var(--text-dim); flex: none; }
.conv__preview {
  color: var(--text-dim); font-size: 12.5px; margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv__meta { display: flex; align-items: center; gap: 6px; margin-top: 5px; }
.conv__badge {
  margin-left: auto; background: var(--accent); color: #fff;
  border-radius: 20px; font-size: 11px; padding: 0 6px; min-width: 18px; text-align: center;
}

.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--border);
  display: grid; place-items: center; font-size: 13px; font-weight: 600;
  color: var(--text-dim); flex: none;
}
.avatar--sm { width: 22px; height: 22px; font-size: 10px; }

.chip {
  font-size: 11px; padding: 1px 7px; border-radius: 20px;
  background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border);
  display: inline-flex; align-items: center; gap: 4px; white-space: nowrap;
}
.chip--accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.chip--ok { color: var(--ok); }
.chip--warn { color: var(--warn); }
.chip--danger { color: var(--danger); }

/* ── Переписка ───────────────────────────────────────────────────────── */
.chat { display: flex; flex-direction: column; overflow: hidden; background: var(--bg); }
.chat__empty { margin: auto; color: var(--text-dim); }
.chat__inner { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.chat__header {
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 10px 16px; display: flex; align-items: center; gap: 12px;
}
.chat__title { font-weight: 600; }
.chat__sub { font-size: 12px; color: var(--text-dim); }
.chat__tools { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.chat__tools select {
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px;
  background: var(--surface); max-width: 150px;
}
.chat__messages { flex: 1; overflow-y: auto; padding: 18px 16px; display: flex; flex-direction: column; gap: 3px; }
.chat__presence { padding: 0 16px 6px; font-size: 12px; color: var(--warn); }

.day { align-self: center; margin: 14px 0 8px; font-size: 11.5px; color: var(--text-dim); }

.msg { max-width: min(640px, 78%); align-self: flex-start; }
.msg--out { align-self: flex-end; }
.msg__bubble {
  background: var(--bubble-in); border: 1px solid var(--border);
  border-radius: 12px; padding: 8px 11px; box-shadow: var(--shadow);
  white-space: pre-wrap; word-break: break-word;
}
.msg--out .msg__bubble { background: var(--bubble-out); border-color: transparent; }
.msg__reply {
  border-left: 2px solid var(--accent); padding-left: 7px; margin-bottom: 5px;
  font-size: 12px; color: var(--text-dim); max-height: 38px; overflow: hidden;
}
.msg__meta {
  display: flex; align-items: center; gap: 6px; margin-top: 3px;
  font-size: 11px; color: var(--text-dim);
}
.msg--out .msg__meta { justify-content: flex-end; }
.msg__status { display: inline-flex; }
.msg__status .icon { width: 15px; height: 15px; }
.msg__status--read { color: var(--accent); }
.msg__status--failed { color: var(--danger); }
.msg__status--queued { opacity: .7; }
.msg__actions { opacity: 0; transition: opacity .1s; display: flex; gap: 2px; }
.msg:hover .msg__actions { opacity: 1; }
.msg__reactions { display: flex; gap: 4px; margin-top: 4px; }
.msg__reaction {
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  padding: 1px 7px; font-size: 12px; cursor: default;
}
.msg__reaction--mine { border-color: var(--accent); background: var(--accent-soft); }
.msg__system { align-self: center; font-size: 12px; color: var(--text-dim); }

.att { margin-top: 6px; }
.att img, .att video { max-width: 100%; max-height: 320px; border-radius: 8px; display: block; }
.att audio { width: 260px; }
.att__file {
  display: flex; align-items: center; gap: 9px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  text-decoration: none; color: var(--text);
}
.att__file:hover { background: var(--surface-2); }
.att__name { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att__size { font-size: 11px; color: var(--text-dim); }
.att__state { font-size: 12px; color: var(--text-dim); font-style: italic; }

/* ── Поле ответа ─────────────────────────────────────────────────────── */
.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 10px 12px; }
.composer__row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; resize: none; max-height: 180px; border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 11px; outline: none; background: var(--surface-2);
}
.composer textarea:focus { border-color: var(--accent); background: var(--surface); }
.composer__note { margin-top: 7px; font-size: 12px; color: var(--warn); }
.composer__files { display: flex; flex-wrap: wrap; gap: 6px; }
.composer__files:not(:empty) { margin-bottom: 8px; }
.composer__file {
  display: flex; align-items: center; gap: 6px; font-size: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px 8px;
}
.composer__reply {
  border-left: 2px solid var(--accent); padding: 4px 8px; margin-bottom: 8px;
  font-size: 12px; color: var(--text-dim); display: flex; align-items: center; gap: 8px;
}

/* ── Карточка контакта ───────────────────────────────────────────────── */
.contact {
  background: var(--surface); border-left: 1px solid var(--border);
  overflow-y: auto; padding: 16px;
}
.contact__name { font-weight: 600; font-size: 15px; }
.contact__block { margin-top: 18px; }
.contact__caption {
  font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-dim); margin-bottom: 7px;
}
.contact textarea {
  width: 100%; min-height: 90px; resize: vertical;
  border: 1px solid var(--border); border-radius: 8px; padding: 9px; outline: none;
  background: var(--surface-2);
}
.contact textarea:focus { border-color: var(--accent); background: var(--surface); }
.contact__row { display: flex; align-items: center; gap: 8px; padding: 5px 0; font-size: 13px; }
.contact__history { font-size: 12px; color: var(--text-dim); }
.contact__history div { padding: 5px 0; border-bottom: 1px solid var(--border); }

/* ── Модальное окно настроек ─────────────────────────────────────────── */
.modal { position: fixed; inset: 0; display: grid; place-items: center; z-index: 40; }
.modal__backdrop { position: absolute; inset: 0; background: var(--backdrop); }
.modal__window {
  position: relative; background: var(--surface); border-radius: 14px;
  width: min(880px, 92vw); max-height: 86vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow); border: 1px solid var(--border);
}
.modal__header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.modal__body { padding: 18px; overflow-y: auto; }
.tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.tab {
  border: 0; background: transparent; padding: 6px 11px; border-radius: 8px;
  cursor: pointer; color: var(--text-dim); font-size: 13px;
}
.tab:hover { background: var(--surface-2); }
.tab--active { background: var(--accent-soft); color: var(--accent); }

.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); }
.table th { font-size: 11px; text-transform: uppercase; color: var(--text-dim); letter-spacing: .04em; }
.table tr:last-child td { border-bottom: 0; }

.card {
  border: 1px solid var(--border); border-radius: 12px;
  padding: 14px; margin-bottom: 12px; background: var(--surface-2);
}
.card__head { display: flex; align-items: center; gap: 9px; margin-bottom: 9px; }
.card__title { font-weight: 600; }
.mono {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 2px 6px; word-break: break-all;
}
.muted { color: var(--text-dim); }
.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.row > .field { flex: 1; min-width: 160px; margin-bottom: 0; }

/* ── Тосты ───────────────────────────────────────────────────────────── */
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: 9px; padding: 10px 14px; box-shadow: var(--shadow); max-width: 340px; font-size: 13px;
}
.toast--error { border-left-color: var(--danger); }
.toast--ok { border-left-color: var(--ok); }

/* ── Узкий экран ─────────────────────────────────────────────────────── */
@media (max-width: 1100px) {
  .app, .app.app--contact { grid-template-columns: 60px 280px minmax(0, 1fr); }
  .contact { display: none; }
  .sidebar__brand span:last-child, .nav-item span:not(.nav-item__count), .sidebar__caption span { display: none; }
}
