:root {
  --bg: #edeef0;
  --surface: #ffffff;
  --text: #000000;
  --muted: #818c99;
  --line: #e7e8ec;
  --accent: #0077ff;
  --accent-text: #ffffff;
  --in: #ffffff;          /* входящие — белые */
  --out: #cce4ff;         /* свои — голубые, как в ВК */
  --wave: #7ab4ff;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #19191a;
    --surface: #232324;
    --text: #e1e3e6;
    --muted: #76787a;
    --line: #2c2d2e;
    --accent: #4a9dff;
    --accent-text: #0d1620;
    --in: #232324;
    --out: #2b4d78;
    --wave: #6ba6f5;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.screen { position: absolute; inset: 0; display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* Шапка */
.bar {
  display: flex; align-items: center; gap: 4px;
  padding: calc(var(--safe-top) + 10px) 12px 10px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.bar h1 { margin: 0; font-size: 19px; font-weight: 600; flex: 1; }
.icon-btn {
  border: 0; background: none; color: var(--accent);
  font-size: 26px; line-height: 1; padding: 6px 10px; cursor: pointer;
}

/* Список диалогов */
.dialogs { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.dialog {
  display: flex; gap: 12px; align-items: center;
  padding: 12px 14px; border-bottom: 1px solid var(--line);
  background: var(--surface); cursor: pointer;
}
.dialog:active { background: var(--bg); }
.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: 0 0 auto;
  object-fit: cover; background: var(--line);
}
.dialog-body { flex: 1; min-width: 0; }
.dialog-name { font-weight: 600; margin-bottom: 2px; }
.dialog-last {
  color: var(--muted); font-size: 15px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.badge {
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: var(--accent); color: var(--accent-text);
  font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
}

/* Переписка */
.messages {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 14px 12px; display: flex; flex-direction: column; gap: 8px;
}

/* Каждое сообщение — колонка: строка с пузырём и, при необходимости, ряд действий */
.msg-block { display: flex; flex-direction: column; max-width: 100%; }
.msg-block.in { align-items: flex-start; }
.msg-block.out { align-items: flex-end; }

/* Время стоит рядом с пузырём, а не внутри него */
.msg-line { display: flex; align-items: flex-end; gap: 7px; max-width: 100%; }
.msg-block.out .msg-line { flex-direction: row; }
.msg-time { flex: 0 0 auto; font-size: 12px; color: var(--muted); padding-bottom: 3px; }

.msg {
  max-width: 76vw; padding: 9px 13px; border-radius: 16px;
  word-wrap: break-word; white-space: pre-wrap;
}
.msg.in { background: var(--in); border-bottom-left-radius: 4px; }
.msg.out { background: var(--out); border-bottom-right-radius: 4px; }
.msg-att { font-size: 14px; color: var(--muted); font-style: italic; }

/* Шаблоны */
.templates {
  flex: 0 0 auto; max-height: 42vh; overflow-y: auto;
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 8px;
}
.tpl {
  padding: 11px 13px; border-radius: 12px; background: var(--bg);
  margin-bottom: 7px; cursor: pointer; font-size: 15px;
}
.tpl:active { background: var(--out); }
.tpl-title { font-weight: 600; margin-bottom: 3px; }
.tpl-text { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Поле ввода */
.composer {
  flex: 0 0 auto; display: flex; align-items: flex-end; gap: 6px;
  padding: 8px 10px calc(var(--safe-bottom) + 8px);
  background: var(--surface); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; max-height: 120px;
  border: 1px solid var(--line); border-radius: 20px;
  padding: 9px 14px; font: inherit; font-size: 16px;
  background: var(--bg); color: var(--text);
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.send-btn {
  width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  font-size: 22px; cursor: pointer; flex: 0 0 auto;
}
.send-btn:disabled { opacity: .4; }

/* Вход */
.login-box {
  margin: auto; padding: 24px; width: min(340px, 88vw);
  display: flex; flex-direction: column; gap: 12px;
}
.login-box h1 { margin: 0 0 4px; font-size: 24px; }
.login-box input {
  padding: 13px 15px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); color: var(--text);
}
.login-box button {
  padding: 13px; font-size: 17px; font-weight: 600;
  border: 0; border-radius: 12px;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
}
.error { color: #c0392b; font-size: 15px; margin: 0; min-height: 20px; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

/* Всплывающее сообщение */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + 80px);
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 20px; font-size: 15px;
  z-index: 10; max-width: 86vw;
}

/* Панель шаблонов: шапка */
.templates-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 2px 6px 8px; font-size: 14px; color: var(--muted);
}
.link-btn {
  border: 0; background: none; color: var(--accent);
  font: inherit; font-size: 15px; padding: 4px 6px; cursor: pointer;
}
.link-btn.strong { font-weight: 600; }

/* Редактор шаблонов */
.editor-list { flex: 1; overflow-y: auto; padding: 12px; }
.tpl-card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px; margin-bottom: 12px;
}
.tpl-card input, .tpl-card textarea {
  width: 100%; border: 0; background: none; color: var(--text);
  font: inherit; font-size: 16px; padding: 4px 0; resize: none;
}
.tpl-card input { font-weight: 600; border-bottom: 1px solid var(--line); margin-bottom: 6px; }
.tpl-card input:focus, .tpl-card textarea:focus { outline: none; }
.tpl-card textarea { min-height: 62px; }
.tpl-card-foot {
  display: flex; gap: 4px; align-items: center;
  border-top: 1px solid var(--line); margin-top: 8px; padding-top: 6px;
}
.tpl-card-foot .spacer { flex: 1; }
.tpl-card-foot button {
  border: 0; background: none; color: var(--muted);
  font-size: 19px; padding: 4px 10px; cursor: pointer;
}
.tpl-card-foot .danger { color: #c0392b; font-size: 15px; }

.editor-foot {
  flex: 0 0 auto; padding: 10px 12px calc(var(--safe-bottom) + 12px);
  background: var(--surface); border-top: 1px solid var(--line);
}
.wide-btn {
  width: 100%; padding: 13px; border: 0; border-radius: 12px;
  background: var(--accent); color: var(--accent-text);
  font: inherit; font-size: 17px; font-weight: 600; cursor: pointer;
}

/* Подгрузка списка диалогов */
.more-wrap { padding: 14px 14px calc(var(--safe-bottom) + 20px); }
.more-btn {
  width: 100%; padding: 13px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); color: var(--accent);
  font: inherit; font-size: 16px; cursor: pointer;
}

/* Строка поиска */
.search-row {
  flex: 0 0 auto; padding: 8px 12px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.search-row input {
  width: 100%; padding: 9px 14px; font: inherit; font-size: 16px;
  border: 1px solid var(--line); border-radius: 18px;
  background: var(--bg); color: var(--text);
  -webkit-appearance: none;
}
.search-row input:focus { outline: none; border-color: var(--accent); }

/* Запись голосового */
.mic { font-size: 21px; }
.recorder {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px calc(var(--safe-bottom) + 10px);
  background: var(--surface); border-top: 1px solid var(--line);
  box-shadow: 0 -6px 20px rgba(0, 0, 0, .12);
}
.recorder .spacer { flex: 1; }
.danger-btn { color: #c0392b; }
.rec-dot {
  width: 11px; height: 11px; border-radius: 50%; background: #c0392b;
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .25; } }
.rec-time { font-variant-numeric: tabular-nums; font-size: 17px; }

/* Голосовые шаблоны */
.tpl.voice { display: flex; align-items: center; gap: 10px; }
.tpl.voice .tpl-body { flex: 1; min-width: 0; }
.tpl-actions { display: flex; gap: 2px; flex: 0 0 auto; }
.tpl-actions button {
  border: 0; background: none; color: var(--accent);
  font-size: 20px; padding: 6px 9px; cursor: pointer;
}
.wide-btn.ghost {
  margin-top: 8px; background: none; color: var(--accent);
  border: 1px solid var(--line);
}
.voice-note { color: var(--muted); font-size: 14px; }

/* Голосовое внутри сообщения */
.msg-voice { display: flex; align-items: center; gap: 9px; min-width: 190px; }
.voice-play {
  width: 36px; height: 36px; flex: 0 0 auto;
  border: 0; border-radius: 50%; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-size: 15px;
}
.voice-wave {
  flex: 1; display: flex; align-items: center; gap: 2px; height: 26px; min-width: 70px;
}
.voice-wave i { flex: 1; background: var(--wave); border-radius: 1px; min-width: 2px; }
.voice-dur { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; flex: 0 0 auto; }

/* Вложения в сообщении */
.msg.has-media { padding: 5px 5px 8px; }
.msg-photo {
  display: block; max-width: 100%; border-radius: 14px;
  margin-bottom: 3px; cursor: pointer; background: var(--line);
}
.msg-sticker { display: block; width: 130px; height: auto; }
.msg-video { position: relative; cursor: pointer; }
.msg-video img { display: block; max-width: 100%; border-radius: 14px; background: var(--line); }
.msg-video .play-badge {
  position: absolute; inset: 0; margin: auto;
  width: 54px; height: 54px; border-radius: 50%;
  background: rgba(0, 0, 0, .55); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 22px;
}
.msg-video .vid-time {
  position: absolute; right: 8px; bottom: 8px;
  background: rgba(0, 0, 0, .65); color: #fff;
  padding: 2px 7px; border-radius: 9px; font-size: 13px;
}
.msg-doc { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.msg-doc a { color: var(--accent); }

/* Цитата внутри сообщения */
.msg-quote {
  border-left: 3px solid var(--accent);
  padding: 2px 0 2px 8px; margin-bottom: 6px;
  font-size: 14px; color: var(--muted);
  max-height: 46px; overflow: hidden;
}

/* Действия по нажатию на сообщение */
.msg-actions { display: flex; gap: 6px; margin-top: 6px; }
.msg-actions button {
  border: 0; border-radius: 9px; padding: 5px 11px; cursor: pointer;
  background: var(--bg); color: var(--accent); font: inherit; font-size: 14px;
}

/* Полоса цитаты над полем ввода */
.reply-bar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; background: var(--surface);
  border-top: 1px solid var(--line);
}
.reply-bar-body { flex: 1; min-width: 0; border-left: 3px solid var(--accent); padding-left: 8px; }
.reply-bar-who { font-size: 13px; color: var(--accent); font-weight: 600; }
.reply-bar-text {
  font-size: 14px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Просмотр во весь экран */
.viewer {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, .93);
  display: flex; align-items: center; justify-content: center;
}
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer iframe { width: 100%; height: 70vh; border: 0; }
.viewer-close {
  position: absolute; top: calc(var(--safe-top) + 12px); right: 14px;
  width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: rgba(255, 255, 255, .18); color: #fff; font-size: 19px; cursor: pointer;
}

/* Скорость проигрывания голосового */
.voice-rate {
  border: 1px solid var(--line); border-radius: 11px;
  background: none; color: var(--muted);
  font: inherit; font-size: 13px; padding: 2px 8px; cursor: pointer;
  min-width: 40px;
}

.msg-actions .danger { color: #c0392b; }
.msg-actions .ask-label { align-self: center; font-size: 14px; color: var(--muted); }

/* Разделитель дня */
.day-sep {
  align-self: center; margin: 6px 0 2px;
  padding: 3px 12px; border-radius: 12px;
  background: rgba(128, 128, 128, .18);
  color: var(--muted); font-size: 13px;
}
