:root {
  --bg: #F4F1EA;
  --surface: #FFFFFF;
  --ink: #1E2230;
  --muted: #73768A;
  --line: #E4DED2;
  --accent: #2F6F62;
  --accent-soft: #E3EFEA;
  --accent-ink: #FFFFFF;
  --out: #D7EEE6;
  --in: #FFFFFF;
  --badge: #E0644B;
  --shadow: 0 1px 2px rgba(30, 34, 48, 0.08);
  --radius: 18px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171C;
    --surface: #1D2128;
    --ink: #ECEDEF;
    --muted: #9A9EAB;
    --line: #2A2F38;
    --accent: #63B9A5;
    --accent-soft: #1F3530;
    --accent-ink: #0E1A17;
    --out: #234A40;
    --in: #232830;
    --badge: #E57A63;
    --shadow: none;
  }
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

#app {
  height: var(--vh, 100dvh);
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }

.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 14px;
  padding: 14px 20px;
  font-weight: 600;
  font-size: 17px;
}
.primary:disabled { opacity: 0.5; }
.primary.small { padding: 8px 14px; font-size: 15px; border-radius: 12px; }

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

/* ---------- Вход ---------- */

.login {
  margin: auto;
  width: min(100%, 360px);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-icon { width: 76px; height: 76px; border-radius: 20px; margin: 0 auto 4px; }
.login-title { margin: 0; font-size: 28px; }
.login-hint { margin: 0 0 8px; color: var(--muted); }
.login input {
  font: inherit;
  font-size: 17px;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  text-align: center;
}
.error { color: var(--badge); min-height: 1.4em; margin: 0; }

/* ---------- Шапки ---------- */

.bar {
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.list-bar { border-bottom: 0; padding-bottom: 4px; }
.list-bar h1 { margin: 0; font-size: 32px; letter-spacing: -0.02em; }

.chat-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-left: 6px;
}
.chat-bar h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  border-radius: 50%;
  flex-shrink: 0;
}

.net {
  display: none;
  position: fixed;
  top: calc(env(safe-area-inset-top) + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  font-size: 13px;
  padding: 4px 12px;
  border-radius: 20px;
  z-index: 20;
}
body.offline .net { display: block; }

/* ---------- Список чатов ---------- */

.list-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 16px);
}

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 10px;
  font-size: 15px;
}
.banner span { flex: 1; }

.chat-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.chat-item:active { opacity: 0.7; }

.chat-body { flex: 1; min-width: 0; }
.chat-top, .chat-bottom { display: flex; align-items: center; gap: 8px; }
.chat-name { flex: 1; font-weight: 600; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { color: var(--muted); font-size: 13px; flex-shrink: 0; }
.chat-preview { flex: 1; color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  background: var(--badge);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  min-width: 22px;
  height: 22px;
  border-radius: 11px;
  padding: 0 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.empty { color: var(--muted); text-align: center; padding: 32px 16px; }

.list-error { text-align: center; color: var(--muted); padding: 24px 16px; }

/* ---------- Аватарки ---------- */

.avatar {
  position: relative;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  flex-shrink: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 19px;
}
.avatar.small { width: 38px; height: 38px; font-size: 15px; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

/* ---------- Переписка ---------- */

.messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 8px 10px 12px;
  display: flex;
  flex-direction: column;
}
/* Короткая переписка прижимается к низу, как в Telegram. */
#msg-list { margin-top: auto; }

.older {
  display: block;
  margin: 4px auto 10px;
  color: var(--accent);
  font-size: 15px;
  padding: 8px 14px;
}

.day {
  text-align: center;
  margin: 14px 0 8px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
}

.msg { display: flex; margin: 3px 0; }
.msg.out { justify-content: flex-end; }

.bubble {
  max-width: 80%;
  padding: 8px 12px 6px;
  border-radius: var(--radius);
  background: var(--in);
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}
.msg.in .bubble { border-bottom-left-radius: 6px; }
.msg.out .bubble { background: var(--out); border-bottom-right-radius: 6px; }

.sender { font-size: 13px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }

.text { white-space: pre-wrap; }
.text a { color: var(--accent); }

.photo {
  display: block;
  width: min(64vw, 280px);
  height: auto;
  max-height: 420px;
  object-fit: cover;
  border-radius: 12px;
  margin: 2px 0 4px;
  background: var(--line);
  cursor: zoom-in;
}

.sticker-row .bubble { background: transparent; box-shadow: none; padding: 0; }
.sticker { display: block; width: 140px; height: auto; }

.attach {
  font-size: 15px;
  color: var(--muted);
  padding: 2px 0;
}

/* Видео, голосовые, файлы */

.video-wrap { margin: 2px 0 4px; }
.video {
  display: block;
  width: min(64vw, 280px);
  height: auto;
  max-height: 420px;
  border-radius: 12px;
  background: #000;
}
.video-wrap.round .video {
  width: 200px;
  height: 200px;
  border-radius: 50%;
  object-fit: cover;
}

.audio-box { margin: 2px 0 2px; }
.audio-title { font-size: 15px; font-weight: 600; margin-bottom: 6px; }
.audio { display: block; width: min(64vw, 260px); height: 40px; }

.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 200px;
  padding: 4px 0 2px;
  color: inherit;
  text-decoration: none;
}
.file-icon {
  width: 46px;
  height: 46px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 12px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  letter-spacing: 0.02em;
}
.file-info { display: flex; flex-direction: column; min-width: 0; }
.file-name { font-weight: 600; overflow-wrap: break-word; }
.file-size { font-size: 13px; color: var(--accent); }

.time {
  font-size: 11px;
  color: var(--muted);
  text-align: right;
  margin-top: 2px;
}
.sticker-row .time { padding-right: 4px; }

/* ---------- Поле ввода ---------- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 8px max(8px, env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

.attach-btn { cursor: pointer; }

#text-input {
  flex: 1;
  resize: none;
  font: inherit;
  font-size: 17px;
  line-height: 1.35;
  padding: 9px 14px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  max-height: 140px;
  min-height: 42px;
}

.send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-bottom: 1px;
}
.send-btn:disabled { opacity: 0.5; }

.readonly {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  padding: 12px 16px max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

/* ---------- Просмотр фото и подсказки ---------- */

.viewer {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 84px);
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 14px;
  font-size: 15px;
  max-width: calc(100% - 32px);
  text-align: center;
  z-index: 40;
}
