:root {
  --bg: #12151a;
  --surface: #191d24;
  --surface-2: #20252d;
  --border: #2a3038;
  --text: #e7e8ec;
  --text-dim: #8b93a1;
  --accent: #3f8f6f;
  --accent-soft: rgba(63, 143, 111, 0.16);
  --danger: #c25450;
  --radius: 8px;
  font-size: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  height: 100vh;
  overflow: hidden;
}

h1, h3 { font-family: 'Space Grotesk', sans-serif; margin: 0; }

.hidden { display: none !important; }

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
}

input {
  font-family: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 0.95rem;
}
input:focus { outline: 1px solid var(--accent); }

/* ---------- Авторизация ---------- */

.auth-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-card {
  width: 360px;
  padding: 32px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.auth-mark {
  color: var(--accent);
  font-size: 1.4rem;
  margin-bottom: 8px;
}

.auth-card h1 {
  font-size: 1.6rem;
  margin-bottom: 6px;
}

.auth-sub {
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 0 0 20px;
}

.auth-tabs {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 18px;
}
.auth-tab {
  flex: 1;
  background: var(--surface-2);
  color: var(--text-dim);
  padding: 9px 0;
  font-size: 0.85rem;
}
.auth-tab.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.auth-form { display: flex; flex-direction: column; gap: 10px; }

.auth-form button[type="submit"] {
  background: var(--accent);
  color: #0e1512;
  font-weight: 600;
  padding: 11px;
  border-radius: var(--radius);
  margin-top: 4px;
}

.auth-hint { color: var(--text-dim); font-size: 0.78rem; margin: 0; }
.auth-error { color: var(--danger); font-size: 0.8rem; margin: 0; min-height: 1em; }

/* ---------- Приложение ---------- */

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.me-name { font-family: 'Space Grotesk', sans-serif; font-weight: 600; }

.icon-btn {
  background: transparent;
  color: var(--text-dim);
  font-size: 1rem;
  width: 30px;
  height: 30px;
  border-radius: var(--radius);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.new-chat {
  display: flex;
  gap: 6px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.new-chat input { flex: 1; }
.new-chat button {
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius);
  padding: 0 12px;
  font-size: 0.85rem;
}

.contact-list { flex: 1; overflow-y: auto; }

.contact {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.contact:hover { background: var(--surface-2); }
.contact.active { background: var(--accent-soft); border-left: 2px solid var(--accent); }
.contact-name { font-weight: 500; }
.contact-preview { color: var(--text-dim); font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Чат ---------- */

.chat-pane { display: flex; flex-direction: column; height: 100vh; }

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  gap: 8px;
}
.empty-mark { font-size: 1.6rem; color: var(--border); }

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

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}
.chat-header-info { display: flex; flex-direction: column; }
.chat-title { font-family: 'Space Grotesk', sans-serif; font-weight: 600; }
.chat-status { font-size: 0.75rem; color: var(--accent); }

.text-btn {
  background: transparent;
  color: var(--text-dim);
  font-size: 0.8rem;
  padding: 6px 10px;
  border-radius: var(--radius);
}
.text-btn:hover { background: var(--surface-2); color: var(--text); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 60%;
  padding: 10px 13px;
  border-radius: 10px;
  font-size: 0.92rem;
  line-height: 1.45;
  word-wrap: break-word;
}
.msg.mine {
  align-self: flex-end;
  background: var(--accent);
  color: #0e1512;
}
.msg.theirs {
  align-self: flex-start;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.msg-meta {
  font-size: 0.68rem;
  opacity: 0.6;
  margin-top: 4px;
  display: block;
}
.msg-file {
  display: flex;
  align-items: center;
  gap: 8px;
}
.msg-file a { color: inherit; font-weight: 600; }

.send-bar {
  display: flex;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
}
.send-bar input[type="text"] { flex: 1; }
.send-btn {
  background: var(--accent);
  color: #0e1512;
  font-weight: 600;
  padding: 0 18px;
  border-radius: var(--radius);
}

/* ---------- Модалка ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-card {
  width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
}
.modal-sub { color: var(--text-dim); font-size: 0.85rem; line-height: 1.5; }
.safety-number {
  font-family: 'IBM Plex Mono', monospace;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  font-size: 0.85rem;
  line-height: 1.6;
  white-space: pre-wrap;
}

/* ---------- Доп. компоненты: поиск, автодополнение, группы, статус ---------- */

textarea {
  font-family: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 0.9rem;
  resize: vertical;
}

.header-actions { display: flex; gap: 4px; }

.search-bar { padding: 10px 12px 0; }
.search-bar input { width: 100%; }

.search-results { flex: 1; overflow-y: auto; }
.search-result { padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer; }
.search-result:hover { background: var(--surface-2); }
.search-result-who { font-size: 0.75rem; color: var(--accent); margin-bottom: 2px; }
.search-result-text { font-size: 0.85rem; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-empty { padding: 16px; color: var(--text-dim); font-size: 0.85rem; text-align: center; }

.full-width { width: 100%; text-align: left; padding: 8px 16px; border-bottom: 1px solid var(--border); border-radius: 0; }

.new-chat { position: relative; }
.autocomplete {
  position: absolute;
  top: 100%;
  left: 12px;
  right: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  z-index: 10;
  max-height: 200px;
  overflow-y: auto;
}
.autocomplete-item { padding: 8px 12px; font-size: 0.85rem; cursor: pointer; }
.autocomplete-item:hover { background: var(--accent-soft); }

.presence-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border);
  margin-right: 8px;
  flex-shrink: 0;
}
.presence-dot.online { background: var(--accent); }
.contact { flex-direction: row; align-items: center; }
.group-item .contact-name { display: flex; align-items: center; }

.msg-author { font-size: 0.72rem; color: var(--accent); font-weight: 600; margin-bottom: 3px; }

.msg-meta-line { display: flex; align-items: center; gap: 5px; margin-top: 4px; }
.msg-meta { font-size: 0.68rem; opacity: 0.6; }
.msg-status { font-size: 0.68rem; opacity: 0.6; }
.msg-status.status-read { opacity: 1; color: #1e6fd9; }

/* ---------- Настройки ---------- */

.settings-card { width: 420px; max-height: 85vh; overflow-y: auto; }
.settings-section {
  border-top: 1px solid var(--border);
  padding-top: 14px;
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.settings-section h4 { margin: 0 0 2px; font-family: 'Space Grotesk', sans-serif; }
.settings-hint { color: var(--text-dim); font-size: 0.78rem; line-height: 1.5; margin: 0; }
.settings-msg { font-size: 0.8rem; color: var(--accent); margin: 0; min-height: 1em; }
.checkbox-line { display: flex; align-items: center; gap: 6px; font-size: 0.85rem; color: var(--text-dim); }
.danger-section .danger-btn { color: var(--danger); }
.danger-section .danger-btn:hover { background: rgba(194, 84, 80, 0.15); }

.modal-actions { display: flex; gap: 8px; margin-top: 10px; }
.modal-actions button:first-child {
  flex: 1;
  background: var(--accent);
  color: #0e1512;
  font-weight: 600;
  padding: 10px;
  border-radius: var(--radius);
}
#groupModal .modal-card { display: flex; flex-direction: column; gap: 10px; }

/* ---------- Мобильная адаптация ---------- */

.mobile-only { display: none; }

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0; z-index: 5; }
  .chat-pane { position: fixed; inset: 0; z-index: 6; transform: translateX(100%); transition: transform 0.2s ease; }
  body.chat-active .chat-pane { transform: translateX(0); }
  body.chat-active .sidebar { display: none; }
  .mobile-only { display: inline-flex; align-items: center; justify-content: center; }
  .msg { max-width: 82%; }
  .auth-card { width: 90vw; max-width: 360px; padding: 24px; }
  .modal-card, .settings-card { width: 92vw; max-width: 420px; padding: 18px; }
}

@supports (padding: max(0px)) {
  .sidebar-header, .chat-header, .send-bar { padding-top: max(14px, env(safe-area-inset-top)); }
  .send-bar { padding-bottom: max(14px, env(safe-area-inset-bottom)); }
}
