:root {
  --bg: #F2F2F7;
  --surface: #FFFFFF;
  --text: #000000;
  --text-2: #8E8E93;
  --sep: rgba(60, 60, 67, 0.12);
  --accent: #0A84FF;
  --accent-2: #5E5CE6;
  --bubble-in: #E9E9EB;
  --bubble-in-text: #000000;
  --topbar: rgba(249, 249, 249, 0.85);
  --danger: #FF3B30;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1C1C1E;
    --text: #FFFFFF;
    --text-2: #8D8D93;
    --sep: rgba(84, 84, 88, 0.5);
    --bubble-in: #26262A;
    --bubble-in-text: #FFFFFF;
    --topbar: rgba(18, 18, 20, 0.8);
  }
}

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

html, body {
  height: 100%;
  overscroll-behavior: none;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  position: fixed;
  inset: 0;
  overflow: hidden;
}

.hidden { display: none !important; }

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.screen.slide {
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  transform: translateX(100%);
  z-index: 5;
}
.screen.slide.open { transform: translateX(0); }
.screen.slide.hidden { display: flex !important; visibility: hidden; }
.screen.slide.open.hidden { visibility: visible; }

/* ---------- Шапка ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px 10px;
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-bottom: 0.5px solid var(--sep);
  z-index: 2;
  flex-shrink: 0;
}
.topbar h1 { font-size: 17px; font-weight: 600; }
.icon-btn {
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  border: none; background: none; color: var(--accent);
  border-radius: 50%; cursor: pointer;
}
.icon-btn:active { background: var(--sep); }
button svg { pointer-events: none; }

/* ---------- Экран входа ---------- */
#screen-auth {
  background: linear-gradient(160deg, #0A84FF 0%, #5E5CE6 100%);
  justify-content: center;
  align-items: center;
  padding: 24px;
}
.auth-wrap {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border-radius: 24px;
  padding: 32px 24px calc(24px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
.auth-logo { width: 76px; height: 76px; border-radius: 18px; margin-bottom: 14px; }
.auth-title { font-size: 28px; font-weight: 700; letter-spacing: -0.5px; }
.auth-sub { color: var(--text-2); font-size: 15px; margin: 4px 0 24px; }
.auth-form { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.auth-form .field { display: flex; flex-direction: column; gap: 4px; }
.auth-form .field.hidden { display: none; }
.field-hint { font-size: 13px; padding-left: 6px; }
.field-hint.ok { color: #30D158; }
.field-hint.bad { color: var(--danger); }
.code-input { text-align: center; font-size: 24px !important; letter-spacing: 10px; font-weight: 600; }
.auth-links { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.btn-link.muted { color: var(--text-2); font-size: 14px; }
.auth-form input {
  width: 100%;
  padding: 14px 16px;
  font-size: 16px;
  border: 1px solid var(--sep);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.auth-form input:focus { border-color: var(--accent); }
.auth-error {
  color: var(--danger);
  font-size: 14px;
  text-align: center;
}
.btn-primary {
  padding: 14px;
  font-size: 17px;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none;
  border-radius: 14px;
  cursor: pointer;
}
.btn-primary:active { opacity: 0.85; }
.btn-primary.small { padding: 10px 22px; font-size: 15px; }
.btn-link {
  margin-top: 16px;
  border: none; background: none;
  color: var(--accent);
  font-size: 15px;
  cursor: pointer;
}

/* ---------- Баннер уведомлений ---------- */
.push-banner {
  margin: 10px 14px 0;
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--sep);
  font-size: 14px;
  line-height: 1.45;
  flex-shrink: 0;
}
.push-banner b { font-weight: 600; }
.push-banner .btn-primary {
  margin-top: 10px;
  width: 100%;
  padding: 11px;
  font-size: 15px;
}

/* ---------- Список чатов ---------- */
.chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.chat-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  cursor: pointer;
}
.chat-row:active { background: var(--surface); }
.avatar {
  width: 52px; height: 52px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; font-size: 20px;
  position: relative;
}
.avatar.small { width: 36px; height: 36px; font-size: 15px; }
.avatar .dot {
  position: absolute;
  right: 0; bottom: 1px;
  width: 13px; height: 13px;
  border-radius: 50%;
  background: #30D158;
  border: 2.5px solid var(--bg);
}
.chat-row-body {
  flex: 1;
  min-width: 0;
  border-bottom: 0.5px solid var(--sep);
  padding: 8px 0;
}
.chat-row:last-child .chat-row-body { border-bottom: none; }
.chat-row-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-name { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 13px; color: var(--text-2); flex-shrink: 0; }
.chat-row-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.chat-last {
  font-size: 15px; color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-username { font-size: 14px; color: var(--text-2); }
.badge {
  min-width: 21px; height: 21px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--accent);
  color: #fff;
  font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-2);
  font-size: 16px;
  padding: 24px;
  text-align: center;
}
.empty-icon { font-size: 44px; }

/* ---------- Диалог ---------- */
.chat-topbar { justify-content: flex-start; }
.back-btn { margin-left: -6px; }
.chat-peer-info { min-width: 0; }
.chat-peer-name { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-peer-status { font-size: 12.5px; color: var(--text-2); }
.chat-peer-status.online { color: #30D158; }
.chat-peer-status.typing { color: var(--accent); }

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.day-sep {
  align-self: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  margin: 12px 0 6px;
}
.msg {
  max-width: 76%;
  padding: 8px 13px;
  font-size: 16px;
  line-height: 1.35;
  border-radius: 18px;
  word-wrap: break-word;
  position: relative;
  animation: msg-in 0.18s ease;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
.msg-text { white-space: pre-wrap; }
.msg.mine {
  align-self: flex-end;
  background: linear-gradient(135deg, #0A84FF, #3E7BFA);
  color: #fff;
}
.msg.mine.tail { border-bottom-right-radius: 5px; }
.msg.theirs {
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--bubble-in-text);
}
.msg.theirs.tail { border-bottom-left-radius: 5px; }
.msg .meta {
  display: block;
  font-size: 11px;
  margin-top: 2px;
  text-align: right;
  opacity: 0.7;
}
.typing-bubble {
  align-self: flex-start;
  background: var(--bubble-in);
  border-radius: 18px;
  padding: 12px 16px;
  display: flex;
  gap: 4px;
}
.typing-bubble i {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-2);
  animation: blink 1.2s infinite;
}
.typing-bubble i:nth-child(2) { animation-delay: 0.2s; }
.typing-bubble i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.3; } 30% { opacity: 1; } }

/* ---------- Поле ввода ---------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px));
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--sep);
  flex-shrink: 0;
}
#composer-input {
  flex: 1;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.35;
  padding: 9px 14px;
  border: 1px solid var(--sep);
  border-radius: 19px;
  background: var(--surface);
  color: var(--text);
  resize: none;
  outline: none;
  max-height: 120px;
}
.send-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s, transform 0.15s;
}
.send-btn:disabled { opacity: 0.35; transform: scale(0.92); }

.attach-btn { color: var(--text-2); flex-shrink: 0; margin-bottom: -1px; }
.send-btn.mic { background: var(--text-2); }

/* ---------- Запись голосового ---------- */
.rec-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--topbar);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--sep);
  flex-shrink: 0;
}
.rec-bar.hidden { display: none; }
.rec-cancel { color: var(--danger); }
.rec-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--danger);
  animation: blink 1.2s infinite;
}
#rec-time { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 16px; }
.rec-hint { flex: 1; color: var(--text-2); font-size: 14px; }

/* ---------- Медиа-сообщения ---------- */
.msg.media {
  padding: 3px;
  background: none;
}
.msg.media .msg-img {
  display: block;
  max-width: min(66vw, 300px);
  max-height: 340px;
  border-radius: 16px;
  cursor: pointer;
}
.msg.media .meta {
  position: absolute;
  right: 10px; bottom: 8px;
  color: #fff;
  text-shadow: 0 0 4px rgba(0,0,0,0.8);
  margin: 0;
}
.voice {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 170px;
  padding: 2px 0;
}
.vp-btn {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.25);
  color: inherit;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.msg.theirs .vp-btn { background: rgba(127,127,127,0.22); }
.vp-bar {
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: rgba(127,127,127,0.35);
  overflow: hidden;
}
.msg.mine .vp-bar { background: rgba(255,255,255,0.35); }
.vp-bar i {
  display: block;
  height: 100%;
  width: 0;
  background: currentColor;
  border-radius: 2px;
}
.vp-time { font-size: 12.5px; font-variant-numeric: tabular-nums; opacity: 0.85; }

/* ---------- Просмотр фото ---------- */
.img-viewer {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.img-viewer img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}

/* ---------- Поиск ---------- */
.search-box { padding: 10px 14px; flex-shrink: 0; }
.search-box input {
  width: 100%;
  padding: 11px 16px;
  font-size: 16px;
  border: none;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
