:root {
  --bg: #ffffff;
  --screen-bg: #f6f6f6;
  --card: #ffffff;
  --chat-bg: #efeae2;
  --header-bg: #f6f6f6;
  --text: #111b21;
  --muted: #667781;
  --accent: #00a884;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --composer: #f0f2f5;
  --input-bg: #ffffff;
  --day-chip: #ffffff;
  --border: #e9edef;
  --danger: #c0392b;
}
[data-theme="dark"] {
  --bg: #111b21;
  --screen-bg: #0b141a;
  --card: #1f2c34;
  --chat-bg: #0b141a;
  --header-bg: #202c33;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --bubble-out: #005c4b;
  --bubble-in: #202c33;
  --composer: #202c33;
  --input-bg: #2a3942;
  --day-chip: #182229;
  --border: #222d34;
  --danger: #f28b82;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}
#root { height: 100dvh; display: flex; flex-direction: column; }
button { font: inherit; border: 0; background: none; color: inherit; cursor: pointer; }
input, textarea { font: inherit; border: 0; outline: none; background: var(--input-bg); color: var(--text); }

/* высота от #root: его поджимает JS при открытии клавиатуры */
.screen { display: flex; flex-direction: column; height: 100%; background: var(--bg); }
.screen.grouped { background: var(--screen-bg); }

.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--header-bg);
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
}
.topbar .back { font-size: 30px; color: var(--accent); line-height: 1; padding: 0 4px 4px; }
.topbar .tb-title { font-size: 17px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .tb-sub { font-size: 12px; color: var(--muted); }
.topbar .tb-body { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; }

.bigheader {
  display: flex; justify-content: space-between; align-items: center;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 8px;
  background: var(--bg);
}
.bigheader h1 { font-size: 30px; font-weight: 700; }
.bigheader .actions { display: flex; gap: 16px; font-size: 21px; }

.warn { background: #fff3cd; color: #856404; font-size: 13px; padding: 8px 16px; }
[data-theme="dark"] .warn { background: #33290a; color: #ffd970; }

.list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.chat-row {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 14px; width: 100%; text-align: left;
}
.chat-row:active { background: var(--screen-bg); }
.chat-row + .chat-row .row-inner { border-top: 0.5px solid var(--border); }
.chat-row .body { flex: 1; min-width: 0; }
.chat-row .top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.chat-row .name { font-size: 17px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .time { font-size: 13px; color: var(--muted); flex-shrink: 0; }
.chat-row .bottom { display: flex; align-items: center; gap: 6px; margin-top: 2px; }
.chat-row .preview { font-size: 15px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.sep { height: 0.5px; background: var(--border); margin-left: 80px; }

.avatar {
  border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; background-size: cover; background-position: center;
}
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

.empty { text-align: center; color: var(--muted); margin-top: 80px; padding: 0 32px; line-height: 1.5; }

/* переписка */
.convo { background: var(--chat-bg); }
.msgs { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 10px; display: flex; flex-direction: column; }
.day-wrap { display: flex; justify-content: center; margin: 6px 0; }
.day-chip {
  background: var(--day-chip); border-radius: 8px; padding: 4px 12px;
  font-size: 12px; color: var(--muted); font-weight: 500;
  box-shadow: 0 1px 1px rgba(0,0,0,.06);
}
.brow { display: flex; margin: 1.5px 0; }
.brow.out { justify-content: flex-end; }
.bubble {
  max-width: 80%; border-radius: 10px; padding: 6px 9px 5px;
  box-shadow: 0 1px 1px rgba(0,0,0,.06);
  word-wrap: break-word;
}
.brow.out .bubble { background: var(--bubble-out); border-top-right-radius: 4px; }
.brow.in .bubble { background: var(--bubble-in); border-top-left-radius: 4px; }
.bubble .sender { font-size: 13px; font-weight: 600; margin-bottom: 1px; }
/* переносы строк сохраняем ТОЛЬКО в тексте сообщения — не в служебной разметке пузыря */
.bubble .btext { font-size: 16px; line-height: 1.35; white-space: pre-wrap; }
.bubble .meta { display: flex; align-items: center; gap: 4px; justify-content: flex-end; margin-top: 2px; }
.bubble .meta .mtime { font-size: 11px; color: var(--muted); }
.bubble .meta .check { font-size: 12px; color: var(--muted); }
.bubble .meta .dot { width: 7px; height: 7px; }

.media-img {
  max-width: min(240px, 62vw); max-height: 300px; border-radius: 8px; display: block;
  margin-bottom: 3px; background: var(--screen-bg);
}
.media-audio { width: 220px; max-width: 60vw; margin-bottom: 3px; }
.doc {
  display: flex; align-items: center; gap: 6px; font-size: 15px; color: var(--accent);
  text-decoration: none; padding: 6px 2px; word-break: break-all;
}
.composer .attach { font-size: 22px; padding: 6px 2px 8px; flex-shrink: 0; }

.route-hint {
  align-self: flex-end; margin: 0 12px 4px; display: flex; align-items: center; gap: 6px;
  background: var(--day-chip); border-radius: 10px; padding: 3px 10px;
  font-size: 12px; color: var(--muted);
}
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
  background: var(--composer); flex-shrink: 0;
}
.composer textarea {
  flex: 1; border-radius: 20px; padding: 9px 14px; font-size: 16px;
  max-height: 120px; resize: none; line-height: 1.3;
}
.composer .send {
  width: 38px; height: 38px; border-radius: 50%; background: var(--accent);
  color: #fff; font-size: 16px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.composer .send:disabled { opacity: .4; }

/* карточки настроек */
.cards { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.card { background: var(--card); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.card .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
.card input { background: var(--screen-bg); border-radius: 10px; padding: 10px 12px; font-size: 16px; width: 100%; }
.btn {
  background: var(--accent); color: #fff; border-radius: 12px;
  padding: 13px; font-size: 16px; font-weight: 600; text-align: center;
}
.btn:disabled { opacity: .4; }
.btn.small { padding: 9px 14px; font-size: 14px; border-radius: 10px; }
.choice { display: flex; align-items: center; gap: 10px; padding: 7px 0; width: 100%; text-align: left; }
.choice .radio { font-size: 18px; color: var(--border); }
.choice.sel .radio { color: var(--accent); }
.choice .c-title { font-size: 15px; }
.choice .c-sub { font-size: 12px; color: var(--muted); }
.src-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.src-row .detach { color: var(--danger); font-size: 13px; }
.center-profile { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.center-profile .pname { font-size: 20px; font-weight: 600; }
.error { color: var(--danger); font-size: 13px; text-align: center; }
.hint { font-size: 12px; color: var(--muted); line-height: 1.4; }

.modal-back {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; z-index: 10;
}
.modal {
  background: var(--screen-bg); width: 100%; max-height: 80dvh; border-radius: 16px 16px 0 0;
  display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom);
}
.modal .m-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; }
.modal .m-list { overflow-y: auto; }
.merge-row { display: flex; align-items: center; gap: 10px; padding: 10px 16px; width: 100%; text-align: left; background: var(--card); border-bottom: 0.5px solid var(--border); }

.toast {
  position: fixed; bottom: calc(env(safe-area-inset-bottom) + 80px); left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.75); color: #fff; border-radius: 10px; padding: 8px 16px; font-size: 14px; z-index: 20;
}
