/* =====================================================
   ЦВЕТА. Поменяй значения тут — изменится весь мессенджер
   ===================================================== */
:root {
  --bg: #ECEBF3;          /* фон переписки */
  --surface: #FFFFFF;     /* панели, чужие сообщения */
  --surface-2: #F5F4FA;   /* наведение, выделение */
  --ink: #1E1B2E;         /* основной текст */
  --muted: #6F6A85;       /* второстепенный текст */
  --accent: #5A3FD6;      /* мои сообщения, кнопки */
  --on-accent: #FFFFFF;   /* текст на акцентном цвете */
  --line: #DAD8E6;        /* линии */
  --online: #2FB36B;      /* зелёная точка «в сети» */
  --danger: #D04A4A;      /* ошибки */
}

/* Тёмная тема включается сама, если она включена на устройстве */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15131F;
    --surface: #211E2F;
    --surface-2: #2A2640;
    --ink: #EDEBF5;
    --muted: #9A95B0;
    --accent: #7B63F0;
    --on-accent: #FFFFFF;
    --line: #322E45;
    --online: #3FCB7E;
    --danger: #F06A6A;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: "Onest", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 14px; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input {
  width: 100%;
  font-size: 16px;               /* 16px, чтобы iPhone не увеличивал страницу при вводе */
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}
input:focus { border-color: var(--accent); outline: none; }

.primary {
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 500;
  padding: 11px 18px;
}
.text-btn {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 14px;
  padding: 6px 8px;
  border-radius: 8px;
}
.text-btn:hover { color: var(--ink); background: var(--surface-2); }

.error { color: var(--danger); font-size: 14px; min-height: 1em; }
.error:empty { display: none; }
.hint { color: var(--muted); font-size: 13px; }

/* =====================================================
   АВАТАРКИ — кружок с первыми буквами имени
   ===================================================== */
.avatar {
  position: relative;
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #FFFFFF;
  font-weight: 700;
  font-size: 17px;
  user-select: none;
}
.avatar.small { width: 40px; height: 40px; font-size: 15px; }
/* Зелёная точка, если человек в сети */
.avatar.online::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: 0;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--online);
  border: 2px solid var(--surface);
}

/* =====================================================
   ВХОД И РЕГИСТРАЦИЯ
   ===================================================== */
.auth {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.auth-card { width: 100%; max-width: 360px; }
.auth-card h1 {
  font-size: 34px;
  line-height: 1.1;
  letter-spacing: -0.02em;
  margin-bottom: 24px;
}
.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border-radius: 12px;
  margin-bottom: 16px;
}
.tab {
  flex: 1;
  border: none;
  background: none;
  padding: 8px;
  border-radius: 9px;
  color: var(--muted);
  font-weight: 500;
}
.tab.active { background: var(--accent); color: var(--on-accent); }
#auth-form { display: flex; flex-direction: column; gap: 10px; }

/* =====================================================
   ОСНОВНОЙ ЭКРАН: слева чаты, справа переписка
   ===================================================== */
.app {
  height: 100vh;
  height: 100dvh;                /* точная высота экрана на телефонах */
  display: grid;
  grid-template-columns: 340px 1fr;
}

/* ----- Левая колонка ----- */
.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;                  /* без этого длинный текст растягивает колонку */
  min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--line);
}
.profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
}
.profile .avatar { width: 40px; height: 40px; font-size: 15px; }
.profile-text { flex: 1; min-width: 0; }
.profile-name { font-weight: 700; }

.search-row { display: flex; gap: 8px; padding: 0 16px 12px; }
.search-row input { background: var(--surface-2); border-color: transparent; padding: 9px 14px; }
.square-btn {
  flex-shrink: 0;
  width: 42px;
  border: none;
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 22px;
  line-height: 1;
}
.square-btn:hover { background: var(--line); }

.connection {
  margin: 0 16px 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 14px;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  padding: 0 8px 8px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
}
.list-label { color: var(--muted); font-size: 14px; padding: 8px 10px 4px; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 12px;
  cursor: pointer;
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--accent); color: var(--on-accent); }
.chat-item.active .muted,
.chat-item.active .chat-time,
.chat-item.active .chat-preview { color: var(--on-accent); opacity: 0.85; }

.chat-item-body { flex: 1; min-width: 0; }
.chat-item-row { display: flex; align-items: baseline; gap: 8px; }
.chat-item-title {
  flex: 1;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.chat-preview {
  flex: 1;
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-preview .typing { color: var(--accent); }
.chat-item.active .chat-preview .typing { color: var(--on-accent); }
.badge {
  flex-shrink: 0;
  min-width: 22px;
  padding: 1px 7px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}
.chat-item.active .badge { background: var(--on-accent); color: var(--accent); }
.list-empty { color: var(--muted); font-size: 14px; padding: 12px 10px; }

/* ----- Правая часть ----- */
.conversation { min-width: 0; min-height: 0; display: flex; flex-direction: column; }

.empty {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}
.empty p { max-width: 300px; }

.chat-view { flex: 1; min-height: 0; display: flex; flex-direction: column; }

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.chat-header .avatar { width: 40px; height: 40px; font-size: 15px; }
.chat-header-text { min-width: 0; }
.chat-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-status.typing { color: var(--accent); }
.back-btn {
  display: none;                 /* кнопка «назад» нужна только на телефоне */
  border: none;
  background: none;
  font-size: 22px;
  padding: 4px 8px 4px 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  padding: 16px max(16px, calc((100% - 760px) / 2));   /* на широком экране переписка по центру */
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.day {
  align-self: center;
  margin: 10px 0 6px;
  padding: 3px 12px;
  border-radius: 12px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
}

.msg {
  max-width: min(78%, 520px);
  align-self: flex-start;
  background: var(--surface);
  padding: 7px 12px 5px;
  border-radius: 18px 18px 18px 6px;
  overflow-wrap: anywhere;       /* длинные слова переносятся, а не ломают вёрстку */
}
.msg-name { font-size: 14px; font-weight: 700; }
.msg-text { white-space: pre-wrap; }
.msg-time { font-size: 12px; color: var(--muted); text-align: right; margin-top: 1px; }

.msg.mine {
  align-self: flex-end;
  background: var(--accent);
  color: var(--on-accent);
  border-radius: 18px 18px 6px 18px;
}
.msg.mine .msg-time { color: var(--on-accent); opacity: 0.75; }

.composer {
  display: flex;
  gap: 8px;
  padding: 10px max(16px, calc((100% - 760px) / 2));
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.composer input { flex: 1; background: var(--surface-2); border-color: transparent; }

/* =====================================================
   ОКНО «НОВАЯ ГРУППА»
   ===================================================== */
dialog {
  margin: auto;
  width: min(400px, calc(100% - 32px));
  border: none;
  border-radius: 16px;
  padding: 20px;
  background: var(--surface);
  color: var(--ink);
}
dialog::backdrop { background: rgba(20, 16, 40, 0.45); }
#group-form { display: flex; flex-direction: column; gap: 10px; }
#group-form h2 { font-size: 20px; margin-bottom: 4px; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* =====================================================
   ТЕЛЕФОН: показываем либо список чатов, либо открытый чат
   ===================================================== */
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { border-right: none; }
  .conversation { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .conversation { display: flex; }
  .back-btn { display: block; }
  .msg { max-width: 85%; }
}
