:root {
  --accent: #d93a3f;
  --link: #2a73c9;
  --bg: #e8ecef;
  --panel: #ffffff;
  --text: #16191d;
  --muted: #667079;
  --line: #e1e5e9;
  --hover: #f2f4f6;
  --active: #fbe9e9;
  --in: #ffffff;
  --out: #fadede;
  --rail: #1c1f24;
  --rail-text: #b9c0c8;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --link: #6aaaf0;
    --bg: #0e1216;
    --panel: #161a1f;
    --text: #e8ebee;
    --muted: #8a949e;
    --line: #242a31;
    --hover: #1d2228;
    --active: #2a1a1c;
    --in: #1f252c;
    --out: #4a2427;
    --rail: #0f1215;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.4; }
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- каркас ---------- */
.app { display: grid; grid-template-columns: 84px 340px 1fr; height: 100dvh; }
.chats, .view { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.chats { background: var(--panel); border-right: 1px solid var(--line); }
.view { background: var(--bg); position: relative; }

.bar {
  display: flex; align-items: center; gap: 10px; flex: none;
  height: 56px; padding: 0 14px; background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.bar h1 { flex: 1; min-width: 0; margin: 0; font-size: 17px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar form { margin: 0; }
.ghost { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; font-size: 13px; color: var(--muted); }
.ghost:hover { background: var(--hover); color: var(--text); }
.back { display: none; }

/* ---------- аккаунты ---------- */
.accounts { background: var(--rail); display: flex; flex-direction: column; gap: 4px; padding: 10px 6px; overflow-y: auto; }
.acc {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: none; border: 0; border-radius: 12px; padding: 8px 2px;
  color: var(--rail-text); font-size: 11px;
}
.acc:hover { background: rgba(255, 255, 255, .06); }
.acc.active { background: rgba(255, 255, 255, .13); color: #fff; }
.acc-label { max-width: 72px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acc .badge { position: absolute; top: 2px; right: 6px; }
.acc.offline .avatar { opacity: .55; }

.avatar { flex: none; width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 16px; }
.avatar.sm { width: 40px; height: 40px; font-size: 14px; }
.badge { background: var(--accent); color: #fff; border-radius: 10px; min-width: 20px; padding: 0 6px; font-size: 12px; font-weight: 600; line-height: 20px; text-align: center; }

/* ---------- список чатов ---------- */
.search { padding: 8px 12px; flex: none; }
.search input { width: 100%; border: 1px solid var(--line); background: var(--hover); border-radius: 10px; padding: 8px 12px; outline: none; }
.search input:focus { border-color: var(--accent); }
.chat-list { flex: 1; overflow-y: auto; }
.chat { display: flex; gap: 12px; width: 100%; text-align: left; background: none; border: 0; padding: 9px 14px; align-items: center; }
.chat:hover { background: var(--hover); }
.chat.active { background: var(--active); }
.chat-body { flex: 1; min-width: 0; }
.chat-top, .chat-bot { display: flex; align-items: center; gap: 8px; }
.chat-title { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { font-size: 12px; color: var(--muted); flex: none; }
.chat-preview { flex: 1; min-width: 0; color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-preview.del { text-decoration: line-through; }
.hint { padding: 24px 20px; color: var(--muted); }

/* ---------- переписка ---------- */
.head-text { min-width: 0; }
.head-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-sub { font-size: 12.5px; color: var(--muted); }
.empty { margin: auto; color: var(--muted); }
.messages { flex: 1; overflow-y: auto; padding: 12px 16px 18px; display: flex; flex-direction: column; gap: 3px; }
.day { align-self: center; margin: 10px 0 6px; padding: 2px 12px; border-radius: 12px; background: rgba(0, 0, 0, .38); color: #fff; font-size: 12.5px; }
.msg { display: flex; }
.msg.out { justify-content: flex-end; }
.bubble { max-width: min(540px, 84%); padding: 7px 10px 5px; border-radius: 14px; background: var(--in); box-shadow: 0 1px 1px rgba(0, 0, 0, .07); overflow-wrap: anywhere; }
.msg.in .bubble { border-bottom-left-radius: 4px; }
.msg.out .bubble { background: var(--out); border-bottom-right-radius: 4px; }
.msg.deleted .bubble { outline: 1.5px dashed var(--accent); outline-offset: -1.5px; }
.text { white-space: pre-wrap; }
.text a, .history a { color: var(--link); }
.bubble > * + .text { margin-top: 6px; }

.meta { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 2px; font-size: 11.5px; color: var(--muted); }
.tag { background: none; border: 0; padding: 0; font-size: 11.5px; color: var(--muted); }
.tag.del { color: var(--accent); font-weight: 600; }
button.tag { text-decoration: underline dotted; }
.history { margin-top: 6px; padding: 6px 8px; border-left: 3px solid var(--accent); background: rgba(0, 0, 0, .05); font-size: 13.5px; white-space: pre-wrap; }
.history .old + .old { margin-top: 4px; }

.photo, .video { display: block; max-width: 100%; max-height: 420px; border-radius: 10px; }
.round { display: block; width: 240px; height: 240px; max-width: 100%; border-radius: 50%; object-fit: cover; background: #000; }
.sticker { display: block; width: 160px; max-width: 100%; }
.sticker-emoji { font-size: 64px; line-height: 1.1; }
.voice { display: flex; align-items: center; gap: 8px; min-width: 260px; }
.voice audio { flex: 1; min-width: 0; height: 36px; }
.dur { font-size: 12.5px; color: var(--muted); }
.audio-title { font-size: 13.5px; font-weight: 600; margin-bottom: 4px; }
.doc { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; min-width: 220px; }
.doc-icon { flex: none; width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 11px; font-weight: 700; }
.doc-name { font-weight: 600; overflow-wrap: anywhere; }
.doc-size { font-size: 12.5px; color: var(--muted); }
.note { color: var(--muted); font-size: 13.5px; }

/* ---------- вход ---------- */
.login-page { display: grid; place-items: center; min-height: 100dvh; }
.login { width: min(360px, 92vw); background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px 24px; }
.login h1 { margin: 0 0 6px; font-size: 22px; }
.login .lead { margin: 0 0 20px; color: var(--muted); }
.login label { display: block; margin-bottom: 6px; font-size: 13.5px; color: var(--muted); }
.login input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--hover); }
.login .err { min-height: 1.4em; margin: 8px 0 12px; color: var(--accent); font-size: 13.5px; }
.primary { width: 100%; padding: 10px; border: 0; border-radius: 10px; background: var(--accent); color: #fff; font-weight: 600; }
.primary:hover { filter: brightness(1.08); }

/* ---------- телефон ---------- */
@media (max-width: 820px) {
  .app { display: flex; flex-direction: column; }
  .accounts { flex: none; flex-direction: row; overflow-x: auto; overflow-y: hidden; padding: 6px; padding-top: max(6px, env(safe-area-inset-top)); }
  .acc { flex: none; min-width: 72px; }
  .chats, .view { flex: 1; border-right: 0; }
  .view { display: none; }
  body.show-chat .view { display: flex; }
  body.show-chat .chats, body.show-chat .accounts { display: none; }
  body.show-chat .bar { padding-top: env(safe-area-inset-top); height: calc(56px + env(safe-area-inset-top)); }
  .back { display: inline-block; }
}
