:root {
  --bg: #0f1117;
  --bg-panel: #171a23;
  --bg-hover: #1f2330;
  --bg-active: #2a2f42;
  --bg-input: #1c2029;
  --border: #262b3a;
  --text: #e8eaf0;
  --text-dim: #8a91a5;
  --accent: #6c7bff;
  --accent-hover: #5a68e8;
  --bubble-in: #232837;
  --bubble-out: #3d4a8f;
  --online: #3ecf6a;
  --danger: #e05555;
}

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

[hidden] { display: none !important; }

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

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

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

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

.auth-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 32px;
  width: 380px;
  max-width: calc(100vw - 32px);
}

.auth-card h1 { text-align: center; font-size: 28px; }
.auth-subtitle { text-align: center; color: var(--text-dim); margin: 4px 0 20px; }

.tabs { display: flex; gap: 8px; margin-bottom: 20px; }

.tab {
  flex: 1;
  padding: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-dim);
}

.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }

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

.auth-form input, #profile-form input {
  padding: 11px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}

.auth-form input:focus, #profile-form input:focus { border-color: var(--accent); }

.hint { color: var(--text-dim); font-size: 14px; text-align: center; line-height: 1.5; }

.error {
  margin-top: 12px;
  padding: 10px;
  background: rgba(224, 85, 85, .12);
  border: 1px solid var(--danger);
  border-radius: 8px;
  color: #ff9b9b;
  font-size: 14px;
  text-align: center;
}

/* ---------- Кнопки ---------- */

.btn-primary {
  padding: 11px 18px;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  color: #fff;
  cursor: pointer;
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  padding: 10px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  text-align: center;
}

.btn-secondary:hover { background: var(--bg-hover); }

.btn-danger {
  padding: 10px 16px;
  background: transparent;
  border: 1px solid var(--danger);
  border-radius: 8px;
  color: var(--danger);
  cursor: pointer;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
}

.icon-btn:hover { background: var(--bg-hover); }

/* ---------- Основной экран ---------- */

#main-screen { display: flex; height: 100vh; }
#main-screen[hidden] { display: none; }

#sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

#me-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.me-info { flex: 1; min-width: 0; }
.name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nickname { color: var(--text-dim); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.search-box { padding: 10px 12px; }

#search-input {
  width: 100%;
  padding: 9px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 20px;
  outline: none;
}

#search-input:focus { border-color: var(--accent); }

#search-results { border-bottom: 1px solid var(--border); }

.search-title { padding: 6px 14px; font-size: 12px; color: var(--text-dim); text-transform: uppercase; }

#chat-list { flex: 1; overflow-y: auto; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
}

.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); }

.chat-item .preview { color: var(--text-dim); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-item-right { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.chat-time { font-size: 12px; color: var(--text-dim); }

.unread-badge {
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}

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

.avatar {
  width: 42px;
  height: 42px;
  min-width: 42px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
  color: #fff;
  overflow: hidden;
  position: relative;
}

.avatar img { width: 100%; height: 100%; object-fit: cover; }

.avatar-big { width: 80px; height: 80px; min-width: 80px; font-size: 32px; }

.avatar .online-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--online);
  border: 2px solid var(--bg-panel);
}

/* ---------- Окно чата ---------- */

#chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-dim);
}

#chat-empty[hidden], #chat-view[hidden] { display: none; }

.empty-icon { font-size: 48px; }

#chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }

#chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

#back-btn { display: none; }

#chat-status.online { color: var(--online); }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg {
  max-width: 65%;
  padding: 8px 12px;
  border-radius: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.msg.in { background: var(--bubble-in); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.out { background: var(--bubble-out); align-self: flex-end; border-bottom-right-radius: 4px; }

.msg .msg-time {
  display: inline-block;
  font-size: 11px;
  color: rgba(255, 255, 255, .55);
  margin-left: 8px;
  vertical-align: bottom;
}

#send-form {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
}

#msg-input {
  flex: 1;
  padding: 11px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 22px;
  outline: none;
}

#msg-input:focus { border-color: var(--accent); }

#send-form .btn-primary { border-radius: 50%; width: 44px; height: 44px; padding: 0; }

/* ---------- Модалка профиля ---------- */

#profile-modal {
  margin: auto;
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  width: 400px;
  max-width: calc(100vw - 32px);
}

#profile-modal::backdrop { background: rgba(0, 0, 0, .6); }

#profile-modal h2 { margin-bottom: 20px; }

.profile-avatar-row { display: flex; align-items: center; gap: 16px; margin-bottom: 20px; }

#profile-form { display: flex; flex-direction: column; gap: 14px; }
#profile-form label { display: flex; flex-direction: column; gap: 6px; color: var(--text-dim); font-size: 14px; }

.modal-buttons { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.modal-buttons .btn-danger { margin-right: auto; }

/* ---------- Мобильная версия ---------- */

@media (max-width: 700px) {
  #sidebar { width: 100%; min-width: 0; }
  #chat-area { display: none; }
  #main-screen.chat-open #sidebar { display: none; }
  #main-screen.chat-open #chat-area { display: flex; }
  #back-btn { display: block; }
  .msg { max-width: 85%; }
}
