:root {
  --bg: #0b141a;
  --panel: #111b21;
  --panel-2: #202c33;
  --chat: #0b141a;
  --mine: #005c4b;
  --theirs: #202c33;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --accent-2: #128c7e;
  --danger: #ea4335;
  --border: #2a3942;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  /* overflow:hidden на WebView (WhatsApp) часто даёт белый пустой экран */
  overflow: auto;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
a { color: var(--accent); }

/* hidden должен реально прятать, даже если у класса есть display:flex/grid */
[hidden] { display: none !important; }

#app { height: 100%; display: flex; flex-direction: column; min-height: 0; }

.screen { display: none; flex: 1; min-height: 0; flex-direction: column; }
.screen.active { display: flex; }

/* Auth */
#auth {
  align-items: center; justify-content: flex-start; padding: 24px;
  padding-top: max(24px, var(--safe-top));
  background: radial-gradient(ellipse at top, #123028, var(--bg) 55%);
  overflow: auto; -webkit-overflow-scrolling: touch;
  min-height: 100%;
}
#main.screen.active { overflow: hidden; height: 100%; }
.auth-card {
  width: min(420px, 100%); background: var(--panel); border-radius: 16px;
  padding: 28px 22px; box-shadow: 0 12px 40px rgba(0,0,0,.35);
}
.logo {
  width: 72px; height: 72px; border-radius: 50%; margin: 0 auto 16px;
  background: var(--accent);
  display: grid; place-items: center; overflow: hidden;
}
.logo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.auth-card h1 { margin: 0 0 6px; text-align: center; font-size: 1.6rem; }
.auth-card .sub { text-align: center; color: var(--muted); margin: 0 0 22px; font-size: .95rem; }
.field { margin-bottom: 14px; }
.field label { display: block; color: var(--muted); font-size: .82rem; margin-bottom: 6px; }
.field input {
  width: 100%; padding: 14px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); outline: none;
}
.field input:focus { border-color: var(--accent); }
.btn {
  width: 100%; padding: 14px; border-radius: 10px; background: var(--accent);
  color: #04140f; font-weight: 700; margin-top: 6px;
}
.btn:disabled { opacity: .5; }
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--border); }
.hint { color: var(--muted); font-size: .85rem; margin-top: 14px; line-height: 1.4; }
.error { color: #ff8a80; font-size: .95rem; margin-top: 10px; min-height: 1.2em; font-weight: 600; }
.ok-hint {
  color: #7dffa8; font-weight: 700; font-size: .95rem; line-height: 1.4;
  margin: 0 0 14px; padding: 10px 12px; border-radius: 10px;
  background: rgba(0,168,132,.15); border: 1px solid rgba(0,168,132,.35);
}
.auth-step[hidden] { display: none !important; }

/* Shell */
#main { flex-direction: row; position: relative; overflow: hidden; min-height: 0; }
.sidebar {
  width: 100%; background: var(--panel); display: flex; flex-direction: column;
  border-right: 1px solid var(--border); min-height: 0; flex: 1;
}
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  padding-top: calc(10px + var(--safe-top)); background: var(--panel-2);
}
.topbar .brand {
  display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0;
}
.topbar .brand-icon {
  width: 36px; height: 36px; border-radius: 50%; overflow: hidden; flex-shrink: 0;
}
.topbar .brand-icon img { width: 100%; height: 100%; display: block; object-fit: cover; }
.topbar h2 { margin: 0; font-size: 1.15rem; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  color: var(--muted); font-size: 1.15rem; flex-shrink: 0;
}
.icon-btn:hover { background: rgba(255,255,255,.06); color: var(--text); }
.icon-btn svg { width: 22px; height: 22px; display: block; }

.chat-list { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
.chat-item {
  display: flex; gap: 12px; padding: 12px 14px; align-items: center;
  border-bottom: 1px solid rgba(255,255,255,.04); cursor: pointer;
}
.chat-item:hover, .chat-item.active { background: rgba(255,255,255,.04); }
.avatar {
  width: 49px; height: 49px; border-radius: 50%; background: #6a7175;
  display: grid; place-items: center; font-weight: 700; flex-shrink: 0;
  color: #fff; font-size: .95rem;
}
.chat-meta { flex: 1; min-width: 0; }
.chat-meta .row { display: flex; justify-content: space-between; gap: 8px; }
.chat-meta .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-meta .time { color: var(--muted); font-size: .75rem; flex-shrink: 0; }
.chat-meta .preview {
  color: var(--muted); font-size: .86rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px;
}
.badge {
  background: var(--accent); color: #04140f; border-radius: 999px; min-width: 20px;
  height: 20px; padding: 0 6px; font-size: .72rem; font-weight: 700;
  display: inline-grid; place-items: center;
}

.conversation {
  flex: 1; display: none; flex-direction: column; min-width: 0; min-height: 0;
  background:
    linear-gradient(rgba(11,20,26,.88), rgba(11,20,26,.92)),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 80 80'%3E%3Cg fill='%23ffffff' fill-opacity='0.03'%3E%3Ccircle cx='10' cy='10' r='2'/%3E%3Ccircle cx='50' cy='30' r='1.5'/%3E%3Ccircle cx='30' cy='60' r='2'/%3E%3Ccircle cx='70' cy='50' r='1.5'/%3E%3C/g%3E%3C/svg%3E");
}
.conv-active {
  display: flex; flex-direction: column; flex: 1; min-height: 0; height: 100%;
}
.conv-head {
  display: flex; align-items: center; gap: 4px; padding: 8px 8px;
  padding-top: calc(8px + var(--safe-top)); background: var(--panel-2);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.conv-head .info { flex: 1; min-width: 0; padding-left: 4px; }
.conv-head .info .name {
  font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-head .info .status { color: var(--muted); font-size: .78rem; }
.conv-head .avatar { width: 40px; height: 40px; font-size: .85rem; }

/* Круглые кнопки звонка как в WhatsApp, свой цвет */
.icon-btn.call {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--accent); color: #04140f;
}
.icon-btn.call:hover { background: #06c097; color: #04140f; }
.icon-btn.call.video { background: var(--accent-2); }
.icon-btn.call.video:hover { background: #16a085; }
.icon-btn.wa {
  width: 40px; height: 40px; border-radius: 50%;
  background: #25d366; color: #04140f; font-weight: 800; font-size: .72rem;
  letter-spacing: 0;
}
.icon-btn.wa:hover { background: #2fe574; color: #04140f; }
.icon-btn.wa svg { width: 20px; height: 20px; }
.icon-btn.tg {
  width: 40px; height: 40px; border-radius: 50%;
  background: #2aabee; color: #041018;
}
.icon-btn.tg:hover { background: #4cbcf5; color: #041018; }
.icon-btn.tg svg { width: 20px; height: 20px; }

.select-bar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: #1a2a32; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.select-bar[hidden] { display: none !important; }
.select-count { flex: 1; font-weight: 700; }
.btn-danger {
  padding: 10px 16px; border-radius: 10px; background: var(--danger);
  color: #fff; font-weight: 700;
}
.bubble.selected {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  background: rgba(0, 168, 132, .28) !important;
}
.bubble .pick {
  position: absolute; left: -2px; top: -2px; width: 22px; height: 22px;
  border-radius: 50%; border: 2px solid var(--accent); background: var(--panel);
  display: none; place-items: center; font-size: .7rem; color: var(--accent); z-index: 2;
}
.messages.selecting .bubble .pick { display: grid; }
.bubble.selected .pick {
  background: var(--accent); color: #04140f; content: '';
}
.bubble.selected .pick::after { content: '✓'; font-weight: 800; }
.bubble { cursor: pointer; }

.messages {
  flex: 1; overflow: auto; padding: 12px 10px 8px; display: flex; flex-direction: column; gap: 4px;
  min-height: 0; -webkit-overflow-scrolling: touch;
}
.bubble {
  max-width: min(82%, 520px); padding: 7px 9px 5px; border-radius: 10px;
  position: relative; word-wrap: break-word; white-space: pre-wrap;
  box-shadow: 0 1px 0.5px rgba(0,0,0,.13);
}
.bubble.mine { align-self: flex-end; background: var(--mine); border-top-right-radius: 2px; }
.bubble.theirs { align-self: flex-start; background: var(--theirs); border-top-left-radius: 2px; }
.bubble .meta {
  display: flex; justify-content: flex-end; gap: 4px; margin-top: 3px;
  color: rgba(233,237,239,.55); font-size: .68rem;
}
.bubble img, .bubble audio, .bubble video {
  max-width: 100%; border-radius: 8px; display: block; margin-top: 4px;
}
.bubble audio { width: min(260px, 100%); height: 40px; }

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 8px 10px;
  padding-bottom: calc(8px + var(--safe-bottom)); background: var(--panel); flex-shrink: 0;
}
.composer textarea {
  flex: 1; resize: none; min-height: 44px; max-height: 120px; padding: 12px 14px;
  border-radius: 22px; border: 0; background: var(--panel-2); outline: none;
}
.send-btn, .attach-btn, .mic-btn {
  width: 46px; height: 46px; border-radius: 50%; background: var(--accent); color: #04140f;
  display: grid; place-items: center; font-size: 1.15rem; flex-shrink: 0;
}
.send-btn svg, .attach-btn svg, .mic-btn svg { width: 22px; height: 22px; display: block; }
.attach-btn, .mic-btn { background: var(--panel-2); color: var(--muted); }
.mic-btn.recording {
  background: var(--danger); color: #fff;
  animation: micPulse 1s ease-in-out infinite;
}
@keyframes micPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}
.composer.has-text .mic-btn { display: none; }
.composer:not(.has-text) .send-btn { display: none; }

/* Sound unlock gate (iPhone) */
.sound-gate {
  position: fixed; inset: 0; z-index: 60; background: rgba(5, 12, 16, .92);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.sound-gate[hidden] { display: none !important; }
.sound-gate-card {
  width: min(400px, 100%); background: var(--panel); border-radius: 18px;
  padding: 28px 22px; text-align: center; box-shadow: 0 16px 50px rgba(0,0,0,.45);
}
.sound-gate-card h2 { margin: 0 0 10px; font-size: 1.35rem; }
.sound-gate-card p { color: var(--muted); margin: 0 0 18px; line-height: 1.45; font-size: .95rem; }
.sound-gate-card .btn { width: 100%; }

/* Call overlay */
#call {
  position: fixed; inset: 0; background: #0a1014; z-index: 50; display: none;
  flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  padding: 24px; padding-bottom: calc(24px + var(--safe-bottom));
}
#call.active { display: flex; }
#call.ringing { animation: callPulse 1s ease-in-out infinite; }
@keyframes callPulse {
  0%, 100% { background: #0a1014; }
  50% { background: #143d2e; }
}
#call video#remoteVideo {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000;
}
#call video#localVideo {
  position: absolute; right: 16px; top: calc(16px + var(--safe-top)); width: 112px; height: 160px;
  object-fit: cover; border-radius: 12px; border: 2px solid rgba(255,255,255,.2); z-index: 2;
}
.call-ui { position: relative; z-index: 3; text-align: center; }
.call-ui .name { font-size: 1.5rem; font-weight: 700; }
.call-ui .state { color: var(--muted); margin: 8px 0 16px; }
.btn-sound {
  display: inline-block; margin: 0 auto 18px; padding: 12px 18px; border-radius: 12px;
  background: #f5c542; color: #1a1200; font-weight: 700; font-size: 1rem;
}
.btn-sound[hidden] { display: none !important; }
.call-actions { display: flex; gap: 18px; justify-content: center; }
.call-actions button {
  width: 72px; height: 72px; border-radius: 50%; background: var(--panel-2); font-size: 1.15rem;
  font-weight: 700;
}
.call-actions .hang { background: var(--danger); color: #fff; }
.call-actions .ok { background: var(--accent); color: #04140f; }

.sheet {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40; display: none;
  align-items: flex-end; justify-content: center;
}
.sheet.open { display: flex; }
.sheet .box {
  width: min(520px, 100%); background: var(--panel); border-radius: 16px 16px 0 0;
  padding: 18px 16px calc(18px + var(--safe-bottom)); max-height: 80vh; overflow: auto;
}
.sheet .box h3 { margin: 0 0 12px; }
.user-pick {
  display: flex; gap: 12px; align-items: center; padding: 10px 4px;
  border-bottom: 1px solid var(--border); cursor: pointer;
}

.empty {
  flex: 1; display: grid; place-items: center; color: var(--muted); padding: 24px; text-align: center;
  font-size: 1rem; line-height: 1.45;
}
.empty strong { color: var(--text); display: block; margin-bottom: 8px; font-size: 1.15rem; }
.conversation.open > .empty { display: none !important; }

/* Телефон: либо список на весь экран, либо чат на весь экран */
@media (max-width: 859px) {
  #main { flex-direction: column; }
  .sidebar {
    width: 100%; max-width: none; border-right: 0; flex: 1; min-height: 0;
  }
  .conversation {
    display: none !important;
    position: absolute; inset: 0; width: 100%; height: 100%;
    z-index: 5; background: var(--bg);
  }
  #main.has-chat .sidebar { display: none !important; visibility: hidden; }
  #main.has-chat .conversation {
    display: flex !important;
  }
  .conversation.open .conv-active {
    display: flex !important;
    flex: 1;
    min-height: 0;
    height: 100%;
  }
}

/* Широкий экран: список + чат рядом */
@media (min-width: 860px) {
  .sidebar { width: 380px; max-width: 380px; flex: 0 0 380px; }
  .conversation { display: flex; }
  .conversation .back { display: none; }
  #main.has-chat .sidebar { display: flex; }
}
