:root {
  --bg: #f0f2f5;
  --surface: #fff;
  --border: #e3e6ea;
  --text: #1a202c;
  --muted: #7a8290;
  --primary: #2b6cb0;
  --primary-hover: #245a99;
  --danger: #e53e3e;
  --bubble-me: #cfe3fa;
  --bubble-peer: #fff;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.05);
  --chat-bg: #e9edf2;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

.hidden { display: none !important; }

.screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
}

/* ===== АВТОРИЗАЦИЯ ===== */
.auth-screen {
  background: linear-gradient(140deg, #4c8fd8, #2b6cb0);
  justify-content: center; align-items: center;
  padding: 20px;
}
.auth-box {
  width: 100%; max-width: 400px;
  background: var(--surface);
  border-radius: 20px;
  padding: 28px 24px;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.logo { margin: 0 0 6px; font-size: 26px; text-align: center; color: var(--primary); }
.auth-sub { margin: 0 0 24px; text-align: center; color: var(--muted); font-size: 13px; }
.auth-tabs {
  display: flex; background: var(--bg);
  border-radius: 12px; padding: 4px; margin-bottom: 20px;
}
.auth-tab {
  flex: 1; border: 0; background: transparent;
  padding: 10px; border-radius: 8px;
  cursor: pointer; font-size: 15px;
  color: var(--muted); font-weight: 500;
}
.auth-tab.active {
  background: var(--surface); color: var(--text);
  box-shadow: var(--shadow);
}
.auth-pane input { width: 100%; margin-bottom: 12px; font-size: 16px; }
.auth-pane button { width: 100%; margin-bottom: 8px; font-size: 15px; }

input[type="text"], input[type="tel"], input[type="password"], input[type="search"] {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  background: var(--surface);
  outline: none;
  color: var(--text);
  width: 100%;
  font-family: inherit;
}
input:focus { border-color: var(--primary); }

button {
  font-family: inherit; cursor: pointer;
  border: 0; border-radius: 10px;
  padding: 12px 16px; font-weight: 500;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: .6; cursor: not-allowed; }
.btn-secondary {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border);
  width: 100%; margin-bottom: 10px;
}
.btn-danger {
  background: transparent; color: var(--danger);
  border: 1px solid var(--danger); width: 100%;
}
.auth-hint { text-align: center; color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.auth-hint a { color: var(--primary); text-decoration: none; }
.auth-error {
  background: #fee; color: var(--danger);
  padding: 10px 12px; border-radius: 10px;
  margin-top: 12px; font-size: 14px;
}

/* ===== ОСНОВНОЙ LAYOUT ===== */
.app-layout {
  display: flex;
  height: 100%;
  overflow: hidden;
}

/* === ЛЕВАЯ КОЛОНКА === */
.sidebar {
  width: 380px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  height: 100%;
  position: relative;
}
.sidebar-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  padding-top: max(12px, env(safe-area-inset-top));
}
.sidebar-user {
  display: flex; align-items: center; gap: 10px;
  cursor: pointer; border-radius: 10px;
  padding: 4px 8px; margin: -4px -8px;
}
.sidebar-user:active { background: var(--bg); }
.sidebar-user b { font-size: 16px; }
.icon-btn {
  background: transparent;
  font-size: 20px;
  padding: 6px 10px;
  color: var(--primary);
  border-radius: 8px;
}
.icon-btn:active { background: var(--bg); }

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.stab {
  flex: 1;
  background: transparent;
  border: 0;
  padding: 12px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 500;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  font-size: 15px;
}
.stab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* === СПИСОК ЧАТОВ === */
.chats-list { padding: 6px 0; }
.chat-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; cursor: pointer;
}
.chat-item:hover { background: #f7f8fa; }
.chat-item.active { background: #eaf1fa; }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .name {
  font-weight: 600; margin-bottom: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item .preview {
  color: var(--muted); font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item .right { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.chat-item .time { color: var(--muted); font-size: 12px; }
.chat-item .unread {
  background: var(--primary); color: #fff;
  font-size: 11px; font-weight: 600;
  padding: 2px 7px; border-radius: 10px;
  min-width: 20px; text-align: center;
}

.avatar {
  width: 48px; height: 48px; border-radius: 50%;
  background: linear-gradient(135deg, #7aa8dd, #4c8fd8);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; flex-shrink: 0;
  overflow: hidden; background-size: cover; background-position: center;
  font-size: 18px;
}
.avatar-sm { width: 38px; height: 38px; font-size: 14px; }
.avatar-lg { width: 80px; height: 80px; font-size: 32px; }

/* === КОНТАКТЫ === */
.search-bar {
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 1;
}
.list { padding: 6px 0; }
.list-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
}
.list-item .name {
  flex: 1; min-width: 0;
  font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.list-item .sub { color: var(--muted); font-size: 13px; }
.btn-mini {
  padding: 6px 12px; font-size: 13px;
  border-radius: 8px;
  background: var(--primary); color: #fff;
}
.empty-state {
  text-align: center; padding: 40px 30px;
  color: var(--muted);
}
.empty-state .hint { font-size: 13px; margin-top: 8px; }

/* === ПРАВАЯ КОЛОНКА === */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  height: 100%;
  position: relative;
}
.chat-placeholder {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--muted);
  text-align: center;
  padding: 20px;
}
.chat-placeholder-icon { font-size: 64px; margin-bottom: 16px; opacity: .5; }
.chat-placeholder h3 { margin: 0 0 8px; color: var(--text); }
.chat-placeholder p { margin: 0; font-size: 14px; }

.chat-content {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

.chat-topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: max(10px, env(safe-area-inset-top));
  flex-shrink: 0;
}
.chat-back { display: none; }
.chat-peer-info { display: flex; flex-direction: column; min-width: 0; }
.chat-peer-info b {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-size: 15px;
}

.messages-view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0;
}
.messages-inner {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px 12px 8px;
  min-height: 100%;
  width: 100%;
}
.msg {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 85%;
  align-self: flex-start;
  margin-bottom: 2px;
}
.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  background: linear-gradient(135deg, #7aa8dd, #4c8fd8);
  background-size: cover;
  background-position: center;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
}
.msg-avatar.placeholder {
  background: transparent;
}
.msg-bubble {
  padding: 8px 12px;
  border-radius: 14px;
  background: var(--bubble-peer);
  color: #1a202c;
  box-shadow: 0 1px 1px rgba(0,0,0,.06);
  word-wrap: break-word;
  white-space: pre-wrap;
  line-height: 1.35;
}
.msg.me .msg-bubble {
  background: var(--bubble-me);
}
.msg .time {
  display: inline;
  font-size: 11px;
  color: #7a8290;
  margin-left: 8px;
  white-space: nowrap;
  vertical-align: bottom;
}

.message-input {
  display: flex; gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: #f5f6f8;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.message-input input {
  flex: 1;
  border-radius: 22px;
  background: #ffffff;
  border: 1px solid var(--border);
  padding: 10px 16px;
}
.message-input input:focus {
  border-color: var(--primary);
}
.send-btn {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 16px;
  padding: 0; flex-shrink: 0;
}

/* ===== ЭКРАН ПРОФИЛЯ ===== */
.profile-screen {
  background: var(--bg);
}
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: max(12px, env(safe-area-inset-top));
}
.topbar h2 { margin: 0; font-size: 18px; }
.profile-body {
  flex: 1;
  overflow-y: auto;
}
.profile-head {
  display: flex; align-items: center; gap: 16px;
  padding: 24px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.profile-name { display: flex; flex-direction: column; }
.profile-name b { font-size: 19px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.profile-fields {
  padding: 8px 20px;
  background: var(--surface);
  margin-bottom: 16px;
}
.field-row {
  display: flex; justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.field-row:last-child { border-bottom: 0; }
.field-row span { color: var(--muted); }
#editProfileBtn, #logoutBtn {
  margin: 8px 20px;
  width: calc(100% - 40px);
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 30px; left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.85);
  color: #fff;
  padding: 10px 18px;
  border-radius: 22px;
  font-size: 14px;
  z-index: 1000;
  animation: toast-in .2s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

/* ===== МОБИЛЬНЫЙ РЕЖИМ ===== */
@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    border-right: 0;
  }
  .chat-area {
    position: fixed;
    inset: 0;
    z-index: 100;
    transform: translateX(100%);
    transition: transform .22s ease;
  }
  body.chat-open .chat-area {
    transform: translateX(0);
  }
  .chat-back { display: inline-block; }
  .chat-placeholder { display: none; }
  .chat-content { display: flex !important; }
}

.loading-screen {
  justify-content: center;
  align-items: center;
  background: var(--bg);
  text-align: center;
  color: var(--muted);
}
.spinner {
  width: 40px; height: 40px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 16px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Меню в шапке */
.sidebar-top { position: relative; }

.dropdown-menu {
  position: absolute;
  top: 58px;
  right: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,.12);
  min-width: 200px;
  padding: 6px;
  z-index: 200;
  animation: menu-pop .15s ease;
}
@keyframes menu-pop {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.menu-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; text-align: left;
  padding: 10px 12px;
  background: transparent;
  border-radius: 8px;
  font-size: 15px;
  color: var(--text);
  cursor: pointer;
}
.menu-item:hover { background: var(--bg); }
.menu-item.danger { color: var(--danger); }

.date-separator {
  display: flex;
  justify-content: center;
  margin: 12px 0 6px;
}
.date-separator span {
  background: rgba(43, 108, 176, 0.85);
  color: #fff;
  font-size: 12px;
  font-weight: 500;
  padding: 5px 14px;
  border-radius: 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,.08);
}

/* Кнопка прикрепления */
.attach-btn {
  background: transparent;
  font-size: 20px;
  padding: 0 10px;
  color: var(--muted);
  border-radius: 50%;
  width: 42px;
  height: 42px;
  flex-shrink: 0;
}
.attach-btn:active { background: var(--bg); }

/* Картинка в сообщении */
.msg-image {
  max-width: 260px;
  max-height: 340px;
  border-radius: 10px;
  display: block;
  cursor: pointer;
  margin-bottom: 4px;
}

/* Видео в сообщении */
.msg-video {
  max-width: 300px;
  max-height: 340px;
  border-radius: 10px;
  display: block;
  margin-bottom: 4px;
  background: #000;
}

/* Файл */
.msg-file {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
}
.msg-file-icon { font-size: 28px; }
.msg-file-info { display: flex; flex-direction: column; }
.msg-file-name { font-size: 14px; color: var(--text); }
.msg-file-size { font-size: 11px; color: var(--muted); }

/* Модалка просмотра фото */
.image-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.9);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.image-modal img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}
.image-modal-close {
  position: absolute;
  top: 16px;
  right: 22px;
  color: #fff;
  font-size: 36px;
  cursor: pointer;
  line-height: 1;
  user-select: none;
}