:root {
  --bg: #0f172a;
  --bg-soft: #16213c;
  --panel: #1b2647;
  --panel-2: #223056;
  --text: #e8ecf6;
  --muted: #96a2c0;
  --accent: #4f8cff;
  --accent-soft: #2b4a8a;
  --mine: #2f6bd8;
  --other: #223056;
  --danger: #ff6b6b;
  --ok: #46d38a;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); font-size: 13px; margin: 6px 0 0; }

button { font: inherit; cursor: pointer; }
input, textarea, button { font-family: inherit; }

/* ── Экран входа ─────────────────────────────────────────────── */
.auth-screen { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.auth-card {
  width: min(360px, 100%);
  background: var(--panel);
  border-radius: 18px;
  padding: 24px;
  display: grid;
  gap: 14px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}
.auth-card h1 { margin: 0; font-size: 22px; }
.auth-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.auth-card input {
  background: var(--bg-soft);
  border: 1px solid #2c3a63;
  border-radius: 10px;
  padding: 11px 12px;
  color: var(--text);
  font-size: 16px; /* 16px — iOS не зумит при фокусе */
}
.primary {
  background: var(--accent);
  border: 0;
  border-radius: 10px;
  padding: 12px;
  color: #fff;
  font-weight: 600;
}
.primary:disabled { opacity: 0.5; cursor: default; }
.link { background: none; border: 0; color: var(--muted); padding: 4px; text-decoration: underline; }

/* ── Основной каркас ─────────────────────────────────────────── */
.app-screen { height: 100dvh; display: grid; grid-template-columns: 320px 1fr; }
.sidebar {
  background: var(--panel);
  border-right: 1px solid #24304f;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 12px 14px; border-bottom: 1px solid #24304f;
}
.me { display: flex; align-items: center; gap: 8px; min-width: 0; }
.me-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn { color: var(--danger); font-size: 12px; }
.conn.online { color: var(--ok); }
.sidebar-actions { display: flex; gap: 4px; }
.icon-btn {
  background: var(--panel-2); border: 0; color: var(--text);
  width: 36px; height: 36px; border-radius: 10px; font-size: 17px; line-height: 1;
}
.icon-btn:active { transform: scale(0.96); }

.chat-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.chat-item { display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; align-items: center;
  padding: 10px; border-radius: 12px; cursor: pointer; }
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--accent-soft);
  display: grid; place-items: center; font-weight: 700; font-size: 15px; color: #dce6ff;
  position: relative;
}
.avatar .dot { position: absolute; right: 0; bottom: 0; width: 11px; height: 11px; border-radius: 50%;
  background: var(--muted); border: 2px solid var(--panel); }
.avatar .dot.online { background: var(--ok); }
.chat-item-body { min-width: 0; }
.chat-item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item-last { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item-meta { display: grid; justify-items: end; gap: 4px; }
.chat-item-time { color: var(--muted); font-size: 11px; }
.badge { background: var(--accent); color: #fff; border-radius: 10px; font-size: 11px;
  min-width: 20px; text-align: center; padding: 1px 6px; font-weight: 700; }
.empty { padding: 16px; }

/* ── Панель переписки ────────────────────────────────────────── */
.chat-panel { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-bottom: 1px solid #24304f; background: var(--bg-soft);
}
.chat-title-box { min-width: 0; }
.chat-title-box h2 { margin: 0; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: min(72%, 560px); padding: 8px 12px; border-radius: var(--radius);
  background: var(--other); align-self: flex-start; word-wrap: break-word; }
.msg.mine { background: var(--mine); align-self: flex-end; }
.msg.pending { opacity: 0.65; }
.msg-author { font-size: 11.5px; color: #b9c6e8; margin-bottom: 2px; font-weight: 600; }
.msg-text { white-space: pre-wrap; }
.msg-meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center;
  font-size: 10.5px; color: #cdd8f3; margin-top: 3px; }
.msg.mine .msg-meta .ticks { font-weight: 700; }
.msg-file { display: block; margin-top: 4px; }
.msg-file img, .msg-file video { max-width: 100%; border-radius: 10px; display: block; }
.msg-file a { color: #eaf1ff; }
.day-sep { align-self: center; color: var(--muted); font-size: 11.5px; margin: 10px 0 2px; }

.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
  border-top: 1px solid #24304f; background: var(--bg-soft); }
.composer textarea {
  flex: 1; resize: none; max-height: 140px; background: var(--panel);
  border: 1px solid #2c3a63; border-radius: 12px; padding: 10px 12px; color: var(--text); font-size: 16px;
}
.attach { background: var(--panel-2); border-radius: 10px; width: 40px; height: 40px;
  display: grid; place-items: center; font-size: 18px; cursor: pointer; }
.send { width: 46px; height: 40px; padding: 0; }
.jump-latest { position: absolute; right: 18px; bottom: 82px; z-index: 5;
  background: var(--panel-2); color: var(--text); border: 1px solid #33436e;
  border-radius: 20px; padding: 6px 12px; font-size: 12px; }

/* ── Модальное окно ──────────────────────────────────────────── */
.modal { position: fixed; inset: 0; background: rgba(6, 10, 22, 0.72); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--panel); border-radius: 18px; padding: 20px; width: min(420px, 100%);
  display: grid; gap: 12px; max-height: 86dvh; }
.modal-card h2 { margin: 0; font-size: 18px; }
.modal-card input { background: var(--bg-soft); border: 1px solid #2c3a63; border-radius: 10px;
  padding: 11px 12px; color: var(--text); font-size: 16px; }
.user-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; max-height: 36dvh; display: grid; gap: 6px; }
.user-list li { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px; background: var(--bg-soft); }
.user-list input { width: 18px; height: 18px; accent-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; align-items: center; }
.modal-actions .primary { padding: 10px 18px; }

/* ── Уведомления ─────────────────────────────────────────────── */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #2b1c1c; color: #ffdede; border: 1px solid #5a2b2b; padding: 10px 16px;
  border-radius: 12px; font-size: 13px; z-index: 100; max-width: 90vw; }
.typing { color: var(--ok); font-size: 12.5px; font-style: italic; }

/* ── Мобильная раскладка ─────────────────────────────────────── */
@media (max-width: 760px) {
  .app-screen { grid-template-columns: 1fr; }
  .sidebar { grid-column: 1; grid-row: 1; }
  .chat-panel { display: none; }
  .app-screen.chat-open .sidebar { display: none; }
  .app-screen.chat-open .chat-panel { display: flex; }
  .icon-btn.back { display: block; }
  .msg { max-width: 86%; }
}

/* Уровень 1: цитаты, реакции, удалённые сообщения */
.msg-reply {
  display: block; margin-bottom: 6px; padding: 5px 9px; border-radius: 8px;
  border-left: 3px solid currentColor; background: rgba(255, 255, 255, 0.1); font-size: 12.5px;
}
.msg-reply-who { display: block; font-weight: 700; font-size: 11px; }
.msg-reply-text { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.reaction { padding: 1px 7px; border-radius: 999px; background: rgba(255, 255, 255, 0.12); font-size: 12.5px; }
.reaction.mine { background: rgba(109, 139, 255, 0.35); box-shadow: inset 0 0 0 1px #6d8bff; }
.msg.deleted .msg-text { font-style: italic; opacity: 0.7; }
.edited { font-size: 10.5px; opacity: 0.75; }
