:root {
  --bg: #17212b;
  --bg-chat: #0e1621;
  --header: #17212b;
  --press: #232e3c;
  --accent: #3390ec;
  --out: #2b5278;
  --in: #213243;
  --search: #242f3d;
  --text: #ffffff;
  --muted: #7d8e9e;
  --divider: rgba(255, 255, 255, 0.06);
  --danger: #e0506a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
}
body { overflow: hidden; position: fixed; inset: 0; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); margin-top: 10px; min-height: 18px; font-size: 14px; }

.screen {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--vh, 100%);
  transform: translateY(var(--vtop, 0px));
  display: flex; flex-direction: column;
}

/* ── Топбар ── */
.topbar {
  display: flex; align-items: center; gap: 6px;
  padding: calc(var(--safe-top) + 4px) 10px 4px;
  background: var(--header);
  flex: 0 0 auto;
  position: relative; z-index: 5;
}
.topbar .title { font-size: 18px; font-weight: 600; flex: 1; }
.icon-btn {
  background: none; border: none; color: #aebfce;
  font-size: 21px; padding: 6px 8px; cursor: pointer; border-radius: 50%;
}
.icon-btn:active { background: var(--press); }
.chat-head { gap: 4px; }
.chat-head-info { flex: 1; min-width: 0; text-align: center; }
.chat-head .title { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subtitle { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { display: flex; align-items: center; gap: 1px; font-size: 26px; }
.back-count { font-size: 15px; color: var(--accent); font-weight: 400; }

/* ── Поиск ── */
.search-wrap { background: var(--header); padding: 0 8px 6px; flex: 0 0 auto; }
.search {
  display: flex; align-items: center; gap: 8px;
  background: var(--search); border-radius: 16px; padding: 6px 12px;
}
.search-ic { opacity: .6; font-size: 14px; }
.search input {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text); font-size: 15px;
}
.search input::placeholder { color: var(--muted); }

/* ── Логин ── */
.login { align-items: center; justify-content: center; background: var(--bg); }
.login-box { width: min(86%, 340px); text-align: center; }
.login-logo {
  width: 84px; height: 84px; margin: 0 auto; border-radius: 50%;
  background: linear-gradient(135deg, #72d5fd, #2a9ef1);
  display: flex; align-items: center; justify-content: center; font-size: 40px;
}
.login-box h1 { margin: 16px 0 4px; font-size: 22px; }
.login-box input {
  width: 100%; margin-top: 18px; padding: 14px;
  background: var(--search); border: none; border-radius: 12px;
  color: var(--text); font-size: 16px; outline: none; text-align: center;
}
.login-box button {
  width: 100%; margin-top: 12px; padding: 14px;
  background: var(--accent); border: none; border-radius: 12px;
  color: #fff; font-size: 16px; font-weight: 600; cursor: pointer;
}

/* ── Аватарки ── */
.avatar {
  width: 54px; height: 54px; border-radius: 50%; flex: 0 0 auto;
  background: var(--press) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center;
  font-size: 21px; font-weight: 600; color: #fff; overflow: hidden;
}
.avatar.sm { width: 38px; height: 38px; font-size: 16px; }

/* ── Список чатов ── */
.chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; background: var(--bg); }
.empty { padding: 40px; text-align: center; color: var(--muted); }
.chat-row {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px; cursor: pointer; position: relative;
}
.chat-row:active { background: var(--press); }
.chat-row::after {
  content: ''; position: absolute; left: 78px; right: 0; bottom: 0;
  border-bottom: 1px solid var(--divider);
}
.chat-main { flex: 1; min-width: 0; }
.chat-top { display: flex; align-items: center; gap: 8px; }
.chat-name { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.chat-time { color: var(--muted); font-size: 13px; flex: 0 0 auto; }
.chat-bottom { display: flex; align-items: center; gap: 8px; margin-top: 3px; }
.chat-preview { flex: 1; min-width: 0; }
.chat-item { color: var(--accent); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-last { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.3; }
.badge {
  background: var(--accent); color: #fff; border-radius: 12px;
  min-width: 22px; height: 22px; padding: 0 7px; font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;
}
.badge.dot { min-width: 12px; width: 12px; height: 12px; padding: 0; }

/* ── Переписка ── */
.messages {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 10px 10px 14px; display: flex; flex-direction: column; gap: 2px;
  background-color: #0e1621;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='150' height='150' viewBox='0 0 150 150'%3E%3Cg fill='none' stroke='%23ffffff' stroke-opacity='0.035' stroke-width='2'%3E%3Ccircle cx='22' cy='24' r='9'/%3E%3Cpath d='M62 30 q11 -12 22 0'/%3E%3Ccircle cx='120' cy='52' r='6'/%3E%3Cpath d='M30 96 l11 11 l-11 11'/%3E%3Ccircle cx='96' cy='118' r='11'/%3E%3Cpath d='M126 104 h16 M134 96 v16'/%3E%3Cpath d='M14 130 q10 8 20 0'/%3E%3C/g%3E%3C/svg%3E");
  background-size: 300px;
}
.date-sep { align-self: center; margin: 10px 0; }
.date-sep span {
  background: rgba(0, 0, 0, .35); color: #c9d6e3; font-size: 12px; font-weight: 600;
  padding: 4px 12px; border-radius: 14px;
}
.msg {
  max-width: 78%; padding: 6px 9px 6px 11px; border-radius: 14px;
  font-size: 15.5px; line-height: 1.35; word-wrap: break-word; white-space: pre-wrap;
  position: relative; margin-top: 2px;
}
.msg.in { align-self: flex-start; background: var(--in); border-bottom-left-radius: 5px; }
.msg.out { align-self: flex-end; background: var(--out); border-bottom-right-radius: 5px; }
/* «хвостик» как в Telegram */
.msg.in.tail { border-bottom-left-radius: 16px; }
.msg img { max-width: 100%; border-radius: 12px; display: block; margin: -2px -3px 3px -5px; }
.msg .meta { font-size: 11px; color: rgba(255, 255, 255, .45); text-align: right; margin-top: 1px; float: right; margin-left: 8px; position: relative; top: 6px; }
.msg.out .meta { color: #8bbce6; }
.msg.out .meta .read { color: #6fd0ff; }
.msg .sys { font-style: italic; opacity: .7; }
.voice-btn {
  background: var(--accent); color: #fff; border: none; border-radius: 16px;
  padding: 7px 16px; font-size: 14px; cursor: pointer;
}
.voice-audio { max-width: 230px; height: 38px; display: block; }

/* ── Композер ── */
.composer {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 3px 6px 5px;
  background: var(--header); flex: 0 0 auto;
}
.attach { font-size: 22px; padding: 8px; cursor: pointer; color: #8696a7; }
.composer textarea {
  flex: 1; resize: none; max-height: 120px;
  background: var(--search); border: none; border-radius: 20px;
  color: var(--text); font-size: 16px; padding: 10px 16px; outline: none; font-family: inherit;
  line-height: 1.3;
}
.send {
  background: var(--accent); border: none; color: #fff;
  width: 42px; height: 42px; border-radius: 50%; font-size: 19px; cursor: pointer; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
}
.send:disabled { opacity: .5; }
.mic {
  background: none; border: none; color: #8696a7;
  width: 42px; height: 42px; font-size: 22px; cursor: pointer; flex: 0 0 auto;
}

/* ── Тост ── */
.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom)); transform: translateX(-50%);
  background: #000b; color: #fff; padding: 10px 16px; border-radius: 20px;
  font-size: 14px; z-index: 100; max-width: 80%; text-align: center;
}
.spinner { text-align: center; color: var(--muted); padding: 20px; }
