:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1c1e21;
  --muted: #6b7280;
  --border: #e2e5ea;
  --accent: #2b6cff;
  --accent-text: #ffffff;
  --mine: #dbe8ff;
  --theirs: #eceef2;
  --danger: #c62828;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141a;
    --panel: #1a1d25;
    --text: #e8eaee;
    --muted: #9aa1ae;
    --border: #2a2e38;
    --accent: #4c8bff;
    --mine: #23406e;
    --theirs: #262a34;
    --danger: #ef6b6b;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button, input, textarea { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 14px;
}
button:disabled { opacity: 0.5; cursor: default; }

.error { color: var(--danger); min-height: 1.4em; margin: 4px 0; }

/* ---------- Экран входа ---------- */
#login-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 16px;
}
#login-form {
  width: 100%;
  max-width: 340px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
#login-form h1 { margin: 0; font-size: 1.4rem; }
#login-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--muted); }
#login-form input {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
}
#login-form button[type="submit"] { background: var(--accent); color: var(--accent-text); border: none; padding: 10px; }

/* ---------- Основной экран ---------- */
#chat-view { display: flex; height: 100vh; height: 100dvh; }

#sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
#sidebar header, #conversation header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  min-height: 56px;
}
#sidebar header strong { flex: 1; overflow: hidden; text-overflow: ellipsis; }

#user-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.user-btn {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: none;
  border-radius: 0;
  border-bottom: 1px solid var(--border);
  padding: 14px;
  text-align: left;
}
.user-btn.active { background: var(--theirs); }
.badge {
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 999px;
  padding: 0 8px;
  font-size: 0.8rem;
}

#conversation { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#conversation header strong { flex: 1; }
#back-btn { display: none; }
#conn-status { font-size: 0.8rem; }
#conn-status.ok { color: var(--muted); }
#conn-status.bad { color: var(--danger); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
}
#older-btn { align-self: center; margin-bottom: 8px; font-size: 0.85rem; }

.message {
  max-width: 75%;
  margin: 3px 0;
  padding: 8px 12px;
  border-radius: 14px;
  overflow-wrap: anywhere;
}
.message .text { white-space: pre-wrap; }
.message time { display: block; margin-top: 2px; font-size: 0.7rem; color: var(--muted); }
.message.mine { align-self: flex-end; background: var(--mine); }
.message.theirs { align-self: flex-start; background: var(--theirs); }
.message.failed .text { font-style: italic; color: var(--danger); }

#notice { padding: 0 14px; }

#composer {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
#composer-input {
  flex: 1;
  resize: none;
  max-height: 140px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
#composer button { background: var(--accent); color: var(--accent-text); border: none; }

/* ---------- Телефон: либо список, либо переписка ---------- */
@media (max-width: 700px) {
  #sidebar { width: 100%; border-right: none; }
  #conversation { display: none; }
  #chat-view.chat-open #sidebar { display: none; }
  #chat-view.chat-open #conversation { display: flex; }
  #back-btn { display: inline-block; }
  .message { max-width: 88%; }
}