:root {
  --bg: #0e1420;
  --panel: #1a2b45;
  --panel-2: #223553;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --text: #e8eef7;
  --muted: #8fa3bf;
  --mine: #2563eb;
  --theirs: #243b5c;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 18px;
  --header-h: 56px;
}

* { 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, sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  justify-content: center;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.hidden { display: none !important; }

.screen {
  width: 100%;
  max-width: 640px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* ---------- Вход ---------- */
#screen-login { align-items: center; justify-content: center; padding: 24px; }
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border-radius: 22px;
  padding: 28px 22px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .4);
}
.logo { font-size: 48px; text-align: center; }
.logo-img {
  display: block;
  width: 72px;
  height: 72px;
  margin: 0 auto;
  border-radius: 18px;
}
.login-card h1 { font-size: 22px; text-align: center; margin: 10px 0 4px; }
.hint { text-align: center; color: var(--muted); margin: 0 0 20px; font-size: 14px; }
.login-card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.login-card input {
  width: 100%;
  padding: 14px;
  font-size: 17px;
  border-radius: 12px;
  border: 1px solid var(--panel-2);
  background: var(--bg);
  color: var(--text);
  margin-bottom: 14px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
#code { letter-spacing: 10px; text-align: center; font-size: 24px; }
button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  border-radius: 12px;
  font-size: 16px;
}
.login-card button[type="submit"] {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.login-card button[type="submit"]:active { background: var(--accent-2); }
.linklike {
  background: none;
  color: var(--muted);
  margin-top: 10px;
  width: 100%;
  padding: 8px;
}
.msg { text-align: center; font-size: 14px; min-height: 18px; margin-top: 12px; }
.msg.error { color: var(--danger); }
.msg.ok { color: var(--ok); }

/* ---------- Чат ---------- */
.chat-header {
  height: var(--header-h);
  min-height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
  background: var(--panel);
  border-bottom: 1px solid rgba(255, 255, 255, .06);
  z-index: 5;
}
.chat-header .back {
  background: none;
  color: var(--text);
  padding: 6px;
  margin-right: 4px;
  border-radius: 10px;
  display: flex;
}
.chat-header .back:active { background: rgba(255, 255, 255, .08); }
.chat-title { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.chat-title span:first-child { font-weight: 600; font-size: 17px; }
.status { font-size: 12px; color: var(--muted); }
.status.online { color: var(--ok); }
.chat-actions { display: flex; gap: 6px; }
.chat-actions button {
  background: none;
  font-size: 20px;
  padding: 8px;
  border-radius: 10px;
  color: var(--text);
  display: flex;
  align-items: center;
}
.chat-actions button:active { background: rgba(255, 255, 255, .08); }
.chat-actions svg, .attach svg, #btn-send svg { display: block; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  -webkit-overflow-scrolling: touch;
}
.day-sep { text-align: center; font-size: 12px; color: var(--muted); margin: 10px 0; }
.bubble {
  max-width: 78%;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--theirs);
  align-self: flex-start;
  line-height: 1.35;
  word-wrap: break-word;
  animation: pop .12s ease-out;
}
.bubble.mine { background: var(--mine); align-self: flex-end; border-bottom-right-radius: 6px; }
.bubble .who { font-size: 12px; color: #bcd0f0; font-weight: 600; margin-bottom: 2px; }
.bubble img { max-width: 100%; border-radius: 12px; display: block; margin: 2px 0; }
.bubble .time { font-size: 11px; color: rgba(255, 255, 255, .6); text-align: right; margin-top: 2px; }
.typing { align-self: flex-start; color: var(--muted); font-size: 13px; padding-left: 6px; }
@keyframes pop { from { transform: scale(.96); opacity: .5; } }

/* ---------- Поле ввода ---------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px;
  padding-left: max(8px, env(safe-area-inset-left));
  padding-right: max(8px, env(safe-area-inset-right));
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid rgba(255, 255, 255, .06);
}
.composer textarea {
  flex: 1;
  resize: none;
  border: none;
  background: var(--bg);
  color: var(--text);
  border-radius: 20px;
  padding: 11px 14px;
  font-size: 16px; /* 16px предотвращает автозум на iOS */
  font-family: inherit;
  max-height: 120px;
}
.composer textarea:focus { outline: none; }
.attach { font-size: 22px; padding: 8px; cursor: pointer; color: var(--muted); display: flex; align-items: center; }

/* Кнопка записи голосового */
#btn-mic {
  background: none;
  color: var(--muted);
  padding: 8px;
  display: flex;
  align-items: center;
  flex-shrink: 0;
}
#btn-mic.rec { color: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* Голосовое сообщение в ленте */
.bubble audio { display: block; width: min(70vw, 260px); margin: 4px 0 2px; }
#voice-audio { width: min(100%, 380px); }
#btn-send {
  background: var(--accent);
  color: #fff;
  width: 42px; height: 42px;
  border-radius: 50%;
  font-size: 18px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#btn-send:active { background: var(--accent-2); }

/* ---------- Предпросмотр фото ---------- */
.photo-preview {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, .85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 20px;
  z-index: 20;
}
.photo-preview img { max-width: 100%; max-height: 60vh; border-radius: 14px; }
.preview-actions { display: flex; gap: 12px; }
.preview-actions button { padding: 12px 20px; background: var(--panel-2); color: var(--text); }
.preview-actions .primary { background: var(--accent); color: #fff; }

/* ---------- Модальное окно ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 20px;
}
.modal-box {
  background: var(--panel);
  border-radius: 18px;
  padding: 22px;
  width: 100%;
  max-width: 420px;
  max-height: 80vh;
  overflow-y: auto;
}
.modal-box h2 { margin: 0 0 14px; font-size: 18px; }
.modal-box h3 { margin: 18px 0 8px; font-size: 15px; color: var(--muted); }
#members-list { list-style: none; margin: 0; padding: 0; }
#members-list li {
  padding: 10px 6px;
  border-bottom: 1px solid rgba(255, 255, 255, .06);
  display: flex;
  align-items: center;
  gap: 10px;
}
#members-list li.clickable { cursor: pointer; border-radius: 10px; }
#members-list li.clickable:active { background: rgba(255, 255, 255, .06); }
#members-list li .chev { margin-left: auto; color: var(--muted); font-size: 18px; }
.avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
}
.invite-row { display: flex; gap: 8px; }

/* Ваш ник в списке участников */
.profile-row { display: flex; gap: 8px; }
#profile-name {
  flex: 1;
  min-width: 0;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--panel-2);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
}
#profile-name:focus { outline: none; border-color: var(--accent); }
#btn-save-profile { padding: 10px 14px; background: var(--accent); color: #fff; white-space: nowrap; }

/* ---------- Видеозвонок ---------- */
.call-overlay {
  position: fixed;
  inset: 0;
  background: #05080f;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
}
#video-remote {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}
#video-local {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 14px);
  right: 12px;
  width: 30vw;
  max-width: 140px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  background: #000;
  border-radius: 14px;
  border: 2px solid rgba(255, 255, 255, .25);
  z-index: 2;
  transform: scaleX(-1); /* зеркало — как в привычных мессенджерах */
}
.call-status-line {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 18px);
  left: 0;
  right: 0;
  text-align: center;
  color: #fff;
  text-shadow: 0 1px 5px rgba(0, 0, 0, .9);
  font-size: 16px;
  z-index: 3;
  pointer-events: none;
}
.call-actions {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom) + 28px);
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 18px;
  z-index: 3;
}
.call-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .16);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.call-btn:active { transform: scale(.94); }
.call-btn.accept { background: var(--ok); }
.call-btn.decline { background: var(--danger); }
.call-btn.off { background: #fff; color: #111; }
.call-incoming {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 22px;
  background: rgba(5, 8, 15, .6);
  z-index: 4;
}
.call-from { color: #fff; font-size: 21px; font-weight: 600; text-shadow: 0 1px 5px rgba(0, 0, 0, .9); }
.call-incoming-actions { display: flex; gap: 30px; }
.call-incoming-actions .call-btn { width: 66px; height: 66px; }
@keyframes ring { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.08); } }
#call-incoming .accept { animation: ring 1.1s infinite; }
#profile-msg { text-align: left; font-size: 13px; min-height: 0; margin: 6px 0 10px; }
#invite-link {
  flex: 1;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--panel-2);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
#btn-copy { padding: 10px 14px; background: var(--accent); color: #fff; white-space: nowrap; }

@media (min-width: 641px) {
  .messages, .chat-header, .composer { padding-left: 24px; padding-right: 24px; }
}
