:root {
  --bg: #0e1621;
  --bg-2: #16212e;
  --panel: #18242f;
  --header-1: #6a5cff;
  --header-2: #4f9dff;
  --accent: #4f9dff;
  --text: #eef2f7;
  --muted: #8b97a6;
  --bubble-me-1: #3d7eff;
  --bubble-me-2: #5b8def;
  --bubble-other: #1f2b38;
  --danger: #e0484d;
  --green: #2fbf71;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* Высота с учётом мобильных браузеров */
.login, .app { height: 100vh; height: 100dvh; }
.hidden { display: none !important; }

button { font-family: inherit; cursor: pointer; border: none; transition: transform .08s, background .15s, opacity .15s; }
button:active { transform: scale(0.95); }
input { font-family: inherit; }

/* ===================== ВХОД ===================== */
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(120% 80% at 50% 0%, #1d2740 0%, var(--bg) 60%);
}
.login-card {
  background: var(--panel);
  padding: 34px 26px;
  border-radius: 22px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 24px 70px rgba(0,0,0,0.5);
}
.login-logo {
  width: 76px; height: 76px;
  margin: 0 auto 4px;
  border-radius: 22px;
  box-shadow: 0 10px 30px rgba(79,157,255,0.35);
}
.login-card h1 { font-size: 30px; text-align: center; font-weight: 800; letter-spacing: -0.5px; }
.subtitle { text-align: center; color: var(--muted); margin-bottom: 6px; font-size: 14px; }
.login-card input, .composer input {
  background: #0c141d;
  border: 1.5px solid #28333f;
  color: var(--text);
  padding: 14px 16px;
  border-radius: 14px;
  font-size: 16px; /* >=16px чтобы iOS не зумил */
  outline: none;
  transition: border-color .15s;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  background: linear-gradient(135deg, var(--header-1), var(--header-2));
  color: #fff; padding: 15px; border-radius: 14px; font-size: 16px; font-weight: 700;
}
.error { color: var(--danger); font-size: 13px; text-align: center; min-height: 16px; line-height: 1.4; }
.switch-link { text-align: center; font-size: 14px; color: var(--muted); margin-top: 4px; }
.switch-link a { color: var(--accent); cursor: pointer; text-decoration: none; }
.switch-link a:hover { text-decoration: underline; }
.empty-note { text-align: center; color: var(--muted); padding: 40px 20px; font-size: 15px; }
.req-ok, .req-no {
  width: 42px; height: 42px; border-radius: 50%; font-size: 18px; color: #fff; flex-shrink: 0; margin-left: 6px;
}
.req-ok { background: var(--green); }
.req-no { background: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%) translateY(10px);
  background: rgba(0,0,0,0.85); color: #fff; padding: 10px 18px; border-radius: 20px;
  font-size: 14px; opacity: 0; transition: opacity .25s, transform .25s; z-index: 1000; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Окошко «добавить в звонок» */
.add-picker {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 1100; padding: 20px;
}
.add-picker-inner {
  background: var(--panel); border-radius: 18px; width: 100%; max-width: 340px;
  max-height: 70vh; display: flex; flex-direction: column; overflow: hidden;
}
.add-picker-title { padding: 16px; font-weight: 700; font-size: 16px; text-align: center; border-bottom: 1px solid #232b36; }
.add-picker-list { overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 4px; }
.add-pick-item {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px;
  background: transparent; color: var(--text); border-radius: 12px; font-size: 15px; text-align: left;
}
.add-pick-item:hover { background: rgba(255,255,255,0.06); }
.add-pick-av { width: 36px; height: 36px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex-shrink: 0; }
.add-picker-close { margin: 8px; padding: 12px; background: #2a3744; color: #fff; border-radius: 12px; font-weight: 600; }

/* ===================== ОСНОВНОЙ ЭКРАН ===================== */
.app { display: flex; flex-direction: column; background: var(--bg); }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: linear-gradient(135deg, var(--header-1), var(--header-2));
  box-shadow: 0 2px 14px rgba(0,0,0,0.3);
  z-index: 10;
}
.topbar .logo { width: 34px; height: 34px; border-radius: 10px; flex-shrink: 0; }
.brand-wrap { flex: 1; min-width: 0; }
.brand { font-weight: 800; font-size: 17px; line-height: 1.1; color: #fff; }
.online { color: rgba(255,255,255,0.82); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.online .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #b7ffce; margin-right: 5px; vertical-align: middle; }

.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  background: rgba(255,255,255,0.18);
  font-size: 19px; color: #fff;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.icon-btn:hover { background: rgba(255,255,255,0.28); }
.icon-btn.on { background: var(--green); }
.icon-btn.call { background: var(--green); }
.icon-btn.ghost { background: transparent; font-size: 30px; font-weight: 300; width: 36px; }
.icon-btn.ghost:hover { background: rgba(255,255,255,0.15); }

/* ----- Список чатов ----- */
.list-view { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; background: var(--bg); }
.conv-list { display: flex; flex-direction: column; }
.conv-row {
  display: flex; align-items: center; gap: 13px;
  padding: 13px 16px; cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}
.conv-row:active { background: rgba(255,255,255,0.06); }
.conv-avatar {
  position: relative; width: 50px; height: 50px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 19px; font-weight: 700; color: #fff;
}
.presence-dot {
  position: absolute; right: -1px; bottom: -1px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--green); border: 2.5px solid var(--bg);
}
.conv-mid { flex: 1; min-width: 0; }
.conv-title { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-sub { font-size: 13px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.conv-right { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; flex-shrink: 0; }
.conv-time { font-size: 12px; color: var(--muted); }
.badge {
  min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px;
  background: var(--green); color: #fff; font-size: 13px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

/* ----- Один чат ----- */
.chat-view { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ----- Баннер входящего звонка ----- */
.incoming {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; background: #1f6e3f; color: #fff; font-size: 14px;
}
.incoming span { flex: 1; }
.incoming .answer { background: #fff; color: #1f6e3f; padding: 9px 16px; border-radius: 20px; font-weight: 700; }
.incoming .decline { background: rgba(255,255,255,0.2); color: #fff; width: 38px; height: 38px; border-radius: 50%; }

/* Плашка «идёт звонок» */
.call-bar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: #1f6e3f; color: #fff; font-size: 14px; }
.call-bar-text { flex: 1; font-weight: 600; }
.call-bar-return { background: #fff; color: #1f6e3f; padding: 8px 16px; border-radius: 18px; font-weight: 700; font-size: 13px; }
.call-bar-hangup { background: var(--danger); color: #fff; width: 40px; height: 40px; border-radius: 50%; font-size: 17px; }

/* Чёрный слой «затемнить экран» во время звонка */
.dark-overlay { position: fixed; inset: 0; background: #000; z-index: 3000; }

/* Подсказка для iPhone */
.ios-hint {
  background: #20344a; color: #cfe0f5; font-size: 13px; line-height: 1.45;
  padding: 11px 38px 11px 14px; position: relative;
}
.hint-close { position: absolute; right: 10px; top: 8px; font-size: 16px; padding: 4px; cursor: pointer; color: var(--muted); }

/* ----- Звонок ----- */
.call-area { background: #05080c; padding: 10px; }
.videos {
  display: grid; gap: 8px;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  margin-bottom: 10px;
}
.video-wrap { position: relative; aspect-ratio: 3/4; }
@media (min-width: 600px) { .video-wrap { aspect-ratio: 4/3; } }
.videos video { width: 100%; height: 100%; border-radius: 14px; background: #000; object-fit: cover; display: block; }
.call-avatar-big {
  position: absolute; inset: 0; display: none;
  align-items: center; justify-content: center;
  font-size: 52px; font-weight: 700; color: #fff; border-radius: 14px;
}
.video-wrap.no-video video { display: none; }
.video-wrap.no-video .call-avatar-big { display: flex; }

/* Компактный вид аудиозвонка: маленькие круглые аватарки с именами по центру. */
.call-area.audio .videos { display: flex; flex-wrap: wrap; justify-content: center; gap: 20px; padding: 14px 0 6px; }
.call-area.audio .video-wrap { width: 96px; aspect-ratio: auto; }
.call-area.audio video { display: none; }
.call-area.audio .call-avatar-big {
  position: static; display: flex; width: 96px; height: 96px;
  border-radius: 50%; font-size: 38px;
}
.call-area.audio .label {
  position: static; background: none; padding: 0; margin-top: 8px;
  text-align: center; display: block; font-size: 14px; backdrop-filter: none;
}
.video-wrap .label {
  position: absolute; bottom: 8px; left: 8px;
  background: rgba(0,0,0,0.55); padding: 3px 9px; border-radius: 8px; font-size: 12px; backdrop-filter: blur(4px);
}
.call-status { text-align: center; color: #cfe0f5; font-size: 14px; padding: 4px 0 8px; animation: pulse 1.4s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .45; } }
.call-controls { display: flex; gap: 12px; justify-content: center; padding-bottom: 4px; }
.call-controls button {
  width: 56px; height: 56px; border-radius: 50%;
  background: #2a3744; font-size: 22px; color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.call-controls button.off { background: var(--danger); }
.call-controls button.on { background: #2e9e5b; }
.call-controls .hangup { background: var(--danger); width: 64px; height: 64px; }

/* ----- Сообщения ----- */
.messages {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 16px 12px 8px;
  display: flex; flex-direction: column; gap: 3px;
  background: radial-gradient(140% 60% at 50% 0%, #14202c 0%, var(--bg) 50%);
  -webkit-overflow-scrolling: touch;
}
.row { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; margin-top: 7px; }
.row.me { justify-content: flex-end; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; color: #fff;
}
.bubble {
  max-width: 78%; padding: 9px 13px; border-radius: 18px; font-size: 15.5px; line-height: 1.35;
  word-wrap: break-word; overflow-wrap: anywhere;
  animation: pop .18s ease-out;
  box-shadow: 0 1px 2px rgba(0,0,0,0.25);
}
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }
.row.other .bubble { background: var(--bubble-other); border-bottom-left-radius: 6px; }
.row.me .bubble {
  background: linear-gradient(135deg, var(--bubble-me-1), var(--bubble-me-2));
  border-bottom-right-radius: 6px;
}
.bubble .who { font-size: 12.5px; font-weight: 700; margin-bottom: 2px; }
.bubble .time { font-size: 10.5px; color: rgba(255,255,255,0.6); margin-top: 3px; text-align: right; }
.system {
  align-self: center; color: var(--muted); font-size: 12px;
  padding: 4px 12px; margin: 6px 0; background: rgba(255,255,255,0.05); border-radius: 12px;
}

/* Галочки прочтения, изменено/удалено, вложения */
.bubble .ticks { margin-left: 4px; font-size: 11px; color: rgba(255,255,255,0.6); }
.bubble .ticks.read { color: #8ed8ff; }
.bubble .edited { font-size: 10px; color: rgba(255,255,255,0.5); }
.bubble .deleted { font-style: italic; opacity: 0.6; }
.bubble .att-img { max-width: 220px; max-height: 300px; border-radius: 12px; display: block; cursor: pointer; margin-bottom: 2px; }
.bubble .att-file { display: inline-block; color: #fff; text-decoration: underline; word-break: break-all; }
.bubble .msg-text { margin-top: 2px; }

/* Меню «изменить/удалить» */
.msg-menu { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: flex-end; justify-content: center; z-index: 2500; padding: 16px; }
.msg-menu-card { background: var(--panel); border-radius: 16px; width: 100%; max-width: 340px; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 10px 40px rgba(0,0,0,.5); }
.msg-menu-card button { background: transparent; color: var(--text); padding: 15px; font-size: 16px; border-bottom: 1px solid #232b36; text-align: center; }
.msg-menu-card button.mm-del { color: var(--danger); }
.msg-menu-card button.mm-cancel { border-bottom: none; color: var(--muted); }

/* ----- Поле ввода ----- */
.composer .attach { width: 46px; height: 46px; border-radius: 50%; flex-shrink: 0; background: #2a3744; color: #fff; font-size: 20px; }
.composer {
  display: flex; gap: 10px; align-items: center;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: var(--panel); border-top: 1px solid #222e3a;
}
.composer input { flex: 1; border-radius: 22px; padding: 12px 18px; }
.composer .send {
  width: 48px; height: 48px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, var(--header-1), var(--header-2));
  color: #fff; font-size: 20px; display: flex; align-items: center; justify-content: center;
}
