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

:root {
  --bg: #f4f4f5;
  --panel: #ffffff;
  --border: #e4e4e7;
  --text: #18181b;
  --muted: #71717a;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --own: #dbeafe;
  --own-border: #bfdbfe;
  --active: #eff6ff;
  --ok: #16a34a;
  --connecting: #d97706;
  --focus-ring: rgba(37,99,235,.15);
  --shadow: rgba(0,0,0,.08);
  --toast-bg: #18181b;
  --toast-fg: #ffffff;
}

:root[data-theme="dark"] {
  --bg: #18181b;
  --panel: #232328;
  --border: #3f3f46;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --danger: #ef4444;
  --danger-hover: #f87171;
  --own: #1e3a8a;
  --own-border: #1d4ed8;
  --active: #1e293b;
  --ok: #22c55e;
  --connecting: #f59e0b;
  --focus-ring: rgba(59,130,246,.3);
  --shadow: rgba(0,0,0,.4);
  --toast-bg: #f4f4f5;
  --toast-fg: #18181b;
}

html, body { height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; margin-top: 4px; }

/* ---------- Экраны-карточки (авторизация) ---------- */
.screen {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 28px;
}
.card h1 { font-size: 24px; margin-bottom: 4px; }
.card > .muted { margin-bottom: 20px; }

/* ---------- Табы ---------- */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  background: var(--bg);
  border-radius: 8px;
  padding: 4px;
}
.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}
.tab.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 2px var(--shadow); }

/* ---------- Формы ---------- */
.form { display: flex; flex-direction: column; gap: 14px; }
.form label { display: flex; flex-direction: column; gap: 4px; }
.form label > span { font-size: 13px; font-weight: 600; }

input, textarea {
  font: inherit;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--panel);
  color: var(--text);
  width: 100%;
}
input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus-ring); }

/* ---------- Кнопки ---------- */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 9px 14px;
  cursor: pointer;
  text-align: center;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn.ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn.ghost:hover:not(:disabled) { color: var(--text); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn.sm { padding: 6px 10px; font-size: 13px; }

/* ---------- Вход через Google ---------- */
.auth-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 0 14px;
  color: var(--muted);
  font-size: 13px;
}
.auth-divider::before,
.auth-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}
.btn.google {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  background: #fff;
  color: #1f1f1f;
  border-color: var(--border);
}
.btn.google:hover:not(:disabled) { background: #f8f9fa; color: #1f1f1f; }
/* Фирменная кнопка Google остаётся белой и в тёмной теме. */
:root[data-theme="dark"] .btn.google { background: #fff; color: #1f1f1f; }
:root[data-theme="dark"] .btn.google:hover:not(:disabled) { background: #f1f3f4; }
.btn.google svg { flex-shrink: 0; }

/* ---------- Чат: каркас ---------- */
#app {
  height: 100%;
  height: 100dvh;
  display: flex;
  overflow: hidden;
}
.sidebar {
  width: 300px;
  min-width: 300px;
  border-right: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}
.sidebar-head {
  padding: 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sidebar-head .brand { font-weight: 800; font-size: 17px; }
.sidebar-search { padding: 10px 12px; border-bottom: 1px solid var(--border); display: flex; gap: 8px; }
.sidebar-search input { flex: 1; }
.rooms-list { flex: 1; overflow-y: auto; }
.room-item {
  padding: 11px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.room-item:hover { background: var(--bg); }
.room-item.active { background: var(--active); border-left: 3px solid var(--primary); padding-left: 11px; }
.room-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rooms-empty { padding: 24px 14px; color: var(--muted); text-align: center; font-size: 13px; }

.sidebar-foot {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.sidebar-foot .who { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Чат: основная область ---------- */
.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.chat-head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}
.chat-head .back-btn {
  display: none;
  border: none;
  background: transparent;
  font-size: 22px;
  color: var(--text);
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
  flex-shrink: 0;
}
.chat-head .title { font-weight: 700; font-size: 16px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .status { font-size: 12px; color: var(--muted); }
.chat-head .status.online { color: var(--ok); }
.chat-head .status.connecting { color: var(--connecting); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.msg {
  max-width: 70%;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  word-wrap: break-word;
}
.msg .author { font-size: 12px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.msg .time { font-size: 11px; color: var(--muted); margin-top: 4px; }
.msg.own { align-self: flex-end; background: var(--own); border-color: var(--own-border); }
.msg.own .author { color: var(--primary-hover); }
.msg.sys { align-self: center; background: transparent; border: none; color: var(--muted); font-size: 13px; }

.load-more { text-align: center; padding: 8px; }
.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  gap: 8px;
}

.composer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  gap: 8px;
  flex-shrink: 0;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}
.composer input { flex: 1; }

/* ---------- Тосты ---------- */
#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}
.toast {
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  max-width: 90vw;
}
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ---------- Модалки ---------- */
.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 16px;
}
.modal {
  background: var(--panel);
  border-radius: 10px;
  padding: 20px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal h3 { font-size: 18px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; }

@media (max-width: 640px) {
  .card { padding: 20px 16px; border-radius: 8px; }
  .card h1 { font-size: 22px; }

  #app { flex-direction: column; }

  .sidebar {
    width: 100%;
    min-width: 0;
    height: 100%;
    height: 100dvh;
  }
  #app.mobile-chat .sidebar { display: none; }

  .chat { height: 100%; height: 100dvh; }
  #app:not(.mobile-chat) .chat { display: none; }

  .chat-head .back-btn { display: block; }

  .msg { max-width: 85%; }

  .sidebar-search { padding: 8px 10px; }
  .sidebar-head { padding: 10px 12px; }
  .sidebar-head .brand { font-size: 16px; }

  .messages { padding: 10px; gap: 6px; }

  .composer { padding: 8px 10px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }

  .room-item { padding: 13px 12px; }

  .btn { min-height: 44px; }
  .btn.sm { min-height: 36px; }

  .modal-bg { padding: 0; align-items: flex-end; }
  .modal {
    max-width: 100%;
    border-radius: 12px 12px 0 0;
    padding: 20px 16px 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
}
