:root {
  --bg: #12151c;
  --bg-2: #171b24;
  --bg-3: #1e2430;
  --line: #262d3a;
  --text: #e8ecf4;
  --muted: #8b95a8;
  --accent: #4f7cf7;
  --accent-2: #3d68e0;
  --danger: #e0503f;
  --ok: #35b06a;
  --bubble-me: #2f5bd0;
  --bubble-you: #222937;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100vh; overflow: hidden; }
body {
  margin: 0;
  /* Фиксируем страницу: скроллятся только списки внутри. Иначе на телефоне
     адресная строка и клавиатура утаскивают композер за нижний край. */
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--bg-2);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
h1 { font-size: 22px; margin: 0 0 6px; }
h2 { font-size: 17px; margin: 0; }
p { margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); font-size: 14px; }
[hidden] { display: none !important; }

/* ---------- splash ---------- */
.splash {
  position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 100;
}
.spinner {
  width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.screen { height: 100vh; height: var(--app-h, 100vh); }

/* ---------- auth ---------- */
.auth { display: flex; padding: 24px; overflow-y: auto; background: var(--bg); }
.auth-card {
  width: 100%; max-width: 380px; margin: auto; background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px 24px; text-align: center;
}
.auth-card .logo img { width: 64px; height: 64px; border-radius: 16px; }
.auth-card form { margin-top: 18px; text-align: left; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
input, textarea {
  width: 100%; margin-top: 5px; padding: 11px 12px; background: var(--bg-3);
  border: 1px solid var(--line); border-radius: 10px; outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }
button { cursor: pointer; border: none; background: none; }
.primary {
  width: 100%; padding: 12px; background: var(--accent); color: #fff; border-radius: 10px;
  font-weight: 600; margin-top: 4px;
}
.primary:active { background: var(--accent-2); }
.primary:disabled { opacity: .55; cursor: default; }
.ghost {
  width: 100%; padding: 11px; border: 1px solid var(--line); border-radius: 10px;
  color: var(--muted); margin-top: 12px;
}

/* ---------- app shell ---------- */
.app { display: grid; grid-template-columns: 340px minmax(0, 1fr); height: 100%; }
.sidebar {
  display: flex; flex-direction: column; background: var(--bg-2);
  border-right: 1px solid var(--line); min-height: 0; min-width: 0;
  max-width: 100%; overflow-x: hidden;
  padding-top: var(--safe-top);
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 14px 14px 10px;
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me-text { display: flex; flex-direction: column; min-width: 0; }
.me-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-actions { display: flex; gap: 4px; flex-shrink: 0; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; color: var(--muted); font-size: 16px;
  display: grid; place-items: center;
}
.icon-btn:hover { background: var(--bg-3); color: var(--text); }
.icon-btn.call { font-size: 17px; }
.icon-btn.back { display: none; font-size: 26px; }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 15px;
  background: var(--accent);
}
.avatar.big { width: 84px; height: 84px; font-size: 30px; }
.avatar-wrap { position: relative; flex-shrink: 0; }
.dot {
  position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%;
  background: var(--muted); border: 2px solid var(--bg-2);
}
.dot.on { background: var(--ok); }

.setup-bar {
  margin: 0 14px 10px; padding: 12px 13px; border-radius: 12px;
  background: var(--bg-3); border: 1px solid var(--accent);
  display: flex; flex-direction: column; gap: 4px;
}
.setup-bar { position: relative; }
.setup-bar strong { font-size: 14px; padding-right: 24px; }
.setup-close { position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; font-size: 13px; }
.setup-bar .setup-text { font-size: 12.5px; line-height: 1.35; }
.setup-actions { display: flex; gap: 8px; margin-top: 6px; }
.setup-actions .primary,
.setup-actions .ghost { margin: 0; padding: 9px 12px; font-size: 13.5px; }
.setup-actions .ghost { flex: 0 0 auto; }
.setup-bar .hint { margin: 6px 0 0; font-size: 12px; }

.invite-btn {
  margin: 0 14px 10px; padding: 10px; border: 1px dashed var(--line); border-radius: 10px;
  color: var(--accent); font-weight: 500;
}
.invite-btn:hover { background: var(--bg-3); }

.contact-list { flex: 1; min-width: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.contact {
  display: flex; align-items: center; gap: 11px; padding: 10px 14px; width: 100%;
  max-width: 100%; min-width: 0; text-align: left;
}
.contact:hover { background: var(--bg-3); }
.contact.active { background: var(--bg-3); }
.contact-text { flex: 1; min-width: 0; }
.contact-name { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; min-width: 0; }
.contact-name strong { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.contact-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
/* Обрезать надо сам текст, а не строку: у флекс-контейнера text-overflow не работает. */
.contact-last {
  display: flex; align-items: center; gap: 8px; min-width: 0; margin-top: 1px;
  font-size: 13px; color: var(--muted);
}
.contact-preview {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.badge {
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 600; border-radius: 10px;
  padding: 1px 7px; flex-shrink: 0; min-width: 19px; text-align: center;
}
.icon-btn svg, .round svg, .send svg { display: block; }
.sys { display: inline-flex; align-items: center; gap: 6px; }
.sys-icon { display: inline-flex; }
.empty-hint { padding: 20px 18px; color: var(--muted); font-size: 14px; text-align: center; }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-placeholder { flex: 1; display: grid; place-items: center; color: var(--muted); }
.chat-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 11px; padding: 11px 14px;
  padding-top: calc(11px + var(--safe-top));
  border-bottom: 1px solid var(--line); background: var(--bg-2);
}
.peer-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.peer-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#peer-status { color: var(--muted); font-size: 12px; }
#peer-status.online { color: var(--ok); }

.messages {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 16px 14px; display: flex; flex-direction: column; gap: 3px;
}
.bubble {
  max-width: min(76%, 560px); padding: 7px 11px 5px; border-radius: 15px;
  background: var(--bubble-you); align-self: flex-start; word-wrap: break-word;
  overflow-wrap: anywhere; white-space: pre-wrap;
}
.bubble.me { background: var(--bubble-me); align-self: flex-end; }
.bubble + .bubble { margin-top: 2px; }
.bubble .meta {
  font-size: 10px; color: rgba(255,255,255,.55); float: right; margin: 6px 0 0 8px; user-select: none;
}
.bubble.pending { opacity: .6; }
.sys {
  align-self: center; font-size: 12px; color: var(--muted); background: var(--bg-3);
  padding: 4px 11px; border-radius: 12px; margin: 6px 0;
}
.sys.missed { color: var(--danger); }
.typing { align-self: flex-start; color: var(--muted); font-size: 12px; padding: 2px 4px; }
.load-more { align-self: center; color: var(--accent); font-size: 13px; padding: 8px; }

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--line); background: var(--bg-2);
}
.composer textarea {
  flex: 1; min-width: 0; margin: 0; resize: none; max-height: 140px;
  border-radius: 18px; padding: 10px 14px; line-height: 1.35;
}
.send {
  width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff;
  flex-shrink: 0; display: grid; place-items: center;
}
.send:active { background: var(--accent-2); }

/* ---------- call ---------- */
.call-overlay {
  position: fixed; inset: 0; z-index: 60; background: #0a0c11;
  height: var(--app-h, 100vh);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding-bottom: calc(28px + var(--safe-bottom));
}
.remote-video {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #0a0c11;
}
.remote-video.hidden { opacity: 0; }
.local-video {
  position: absolute; right: 14px; top: calc(14px + var(--safe-top)); width: 108px; height: 152px;
  object-fit: cover; border-radius: 12px; background: #000; border: 1px solid var(--line); z-index: 2;
}
/* Зеркалим только фронтальную камеру: основную зеркалить неправильно. */
.local-video.mirrored { transform: scaleX(-1); }
.round:disabled { opacity: .5; }
.call-info {
  position: relative; z-index: 1; display: flex; flex-direction: column; align-items: center;
  gap: 8px; text-align: center; margin-bottom: auto; margin-top: auto;
}
/* На видеозвонке прячем аватар и имя, но подсказка про звук должна остаться видимой. */
.call-info.faded .avatar,
.call-info.faded strong,
.call-info.faded #call-status { opacity: 0; }
.call-hint {
  font-size: 13px; color: #f0b429; max-width: 280px; text-align: center;
}
.unmute {
  background: #fff; color: #12151c; border-radius: 20px; padding: 10px 18px;
  font-weight: 600; font-size: 14px; margin-top: 4px;
}
.call-controls {
  position: relative; z-index: 3; display: flex; gap: 18px; margin-top: auto;
}
.round {
  width: 60px; height: 60px; border-radius: 50%; background: rgba(255,255,255,.14); color: #fff;
  font-size: 22px; display: grid; place-items: center; backdrop-filter: blur(6px);
}
.round.off { background: #fff; color: #12151c; }
.round.danger { background: var(--danger); }
.round.accept { background: var(--ok); }

/* ---------- modals ---------- */
.modal {
  position: fixed; inset: 0; z-index: 70; background: rgba(6,8,12,.72);
  height: var(--app-h, 100vh);
  display: flex; overflow-y: auto;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
}
.modal-card {
  width: 100%; max-width: 460px; margin: auto; background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 16px; padding: 0 18px 18px; max-height: 100%; overflow-y: auto; overflow-anchor: none;
}
.modal-card header {
  position: sticky; top: 0; z-index: 1; background: var(--bg-2);
  display: flex; align-items: center; justify-content: space-between;
  margin: 0 -18px 12px; padding: 18px 18px 10px;
}
.invite-list { margin-top: 14px; display: flex; flex-direction: column; gap: 8px; }
.invite-row {
  background: var(--bg-3); border-radius: 10px; padding: 10px 12px; font-size: 13px;
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
}
.invite-row code { color: var(--accent); word-break: break-all; font-size: 12px; }
.invite-row.used code { color: var(--muted); text-decoration: line-through; }
.invite-actions { display: flex; gap: 6px; flex-shrink: 0; }
.mini {
  padding: 6px 10px; border-radius: 8px; background: var(--accent); color: #fff; font-size: 12px;
}
.mini.grey { background: var(--line); color: var(--text); }

/* ---------- onboarding ---------- */
.steps { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.step { background: var(--bg-3); border-radius: 12px; padding: 12px 14px; }
.step-head { display: flex; align-items: center; gap: 10px; }
.step-mark {
  width: 22px; height: 22px; border-radius: 50%; background: var(--line); color: var(--text);
  display: grid; place-items: center; font-size: 12px; flex-shrink: 0;
}
.step.done .step-mark { background: var(--ok); }
.step .tick { color: var(--ok); margin-left: auto; font-weight: 700; }
.step-body { font-size: 13.5px; color: var(--muted); margin-top: 8px; }
.step-body p { margin: 0 0 8px; }
.step-body ol { margin: 0 0 8px; padding-left: 20px; }
.step-body .primary { width: auto; padding: 9px 14px; font-size: 14px; }
.os-tabs { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.os-tab {
  padding: 6px 13px; border-radius: 999px; background: var(--bg-2); color: var(--muted);
  font-size: 13px; border: 1px solid var(--line);
}
.os-tab.on { background: var(--accent); color: #fff; border-color: transparent; }
.hint { font-size: 12.5px; margin-top: 8px; }
.hint.ok { color: var(--ok); }
.hint.bad { color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%);
  background: var(--bg-3); border: 1px solid var(--line); color: var(--text);
  padding: 10px 16px; border-radius: 20px; z-index: 90; font-size: 14px; max-width: 90vw;
}

/* ---------- mobile ---------- */
@media (max-width: 780px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { border-right: none; }
  .chat { display: none; }
  .icon-btn.back { display: grid; }
  body.chat-open .sidebar { display: none; }
  body.chat-open .chat { display: flex; }
  .chat-placeholder { display: none; }
  .local-video { width: 92px; height: 128px; }
}
