:root {
  --bg: #0e1621;
  --bg-panel: #17212b;
  --bg-hover: #202b36;
  --bg-input: #242f3d;
  --bg-own: #2b5278;
  --text: #f5f5f5;
  --text-dim: #7d8b99;
  --accent: #64b5f6;
  --accent-strong: #2196f3;
  --red: #e53935;
  --green: #4caf50;
  --radius: 14px;
  --radius-sm: 8px;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

.hidden { display: none !important; }

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea { font-family: inherit; }

/* ===== Auth ===== */
.auth-view {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1b3447 0%, var(--bg) 60%);
  padding: 16px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-panel);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.auth-logo { font-size: 56px; text-align: center; }
.auth-card h1 { text-align: center; font-size: 28px; margin-top: 8px; }
.auth-sub { text-align: center; color: var(--text-dim); margin-bottom: 24px; }
.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tab {
  flex: 1; padding: 10px; border-radius: var(--radius-sm);
  background: var(--bg-input); color: var(--text-dim); font-weight: 600;
}
.tab.active { background: var(--accent-strong); color: #fff; }
.auth-card input {
  width: 100%; padding: 12px 14px; margin-bottom: 12px;
  background: var(--bg-input); border: 1px solid transparent; border-radius: var(--radius-sm);
  color: var(--text); font-size: 15px; outline: none;
}
.auth-card input:focus { border-color: var(--accent); }
.btn {
  width: 100%; padding: 12px; border-radius: var(--radius-sm);
  font-size: 15px; font-weight: 700;
}
.btn.primary { background: var(--accent-strong); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.cancel { background: var(--bg-input); color: var(--text-dim); }
.auth-error { margin-top: 12px; padding: 10px; background: rgba(229,57,53,.15); color: #ff8a80; border-radius: var(--radius-sm); font-size: 14px; }

/* ===== Layout ===== */
.app-view { display: flex; height: 100dvh; }
.sidebar {
  width: 320px; min-width: 280px;
  background: var(--bg-panel);
  display: flex; flex-direction: column;
  border-right: 1px solid rgba(255,255,255,.06);
}
.sidebar-header { display: flex; align-items: center; gap: 8px; padding: 12px; border-bottom: 1px solid rgba(255,255,255,.06); }
.profile-btn {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px; color: #fff; text-transform: uppercase;
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-input); font-size: 18px; color: var(--accent);
}
.icon-btn:hover { background: var(--bg-hover); }
.search-wrap { padding: 10px 12px; }
.search-wrap input {
  width: 100%; padding: 10px 14px; background: var(--bg-input);
  border: 1px solid transparent; border-radius: 20px; color: var(--text); outline: none; font-size: 14px;
}
.search-wrap input:focus { border-color: var(--accent); }

.chats-list { flex: 1; overflow-y: auto; }
.chats-list::-webkit-scrollbar, .messages::-webkit-scrollbar { width: 5px; }
.chats-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 3px; }

.chat-item {
  display: flex; gap: 12px; padding: 10px 12px; align-items: center;
  border-bottom: 1px solid rgba(255,255,255,.04); cursor: pointer;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-hover); }
.chat-item .avatar { width: 46px; height: 46px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; position: relative; }
.chat-item .avatar .dot {
  position: absolute; bottom: 1px; right: 1px; width: 12px; height: 12px;
  border-radius: 50%; border: 2px solid var(--bg-panel); background: #666;
}
.chat-item .avatar .dot.online { background: var(--green); }
.chat-item-body { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-item-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { font-size: 12px; color: var(--text-dim); flex-shrink: 0; }
.chat-item-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.chat-item-preview { font-size: 13px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-preview.draft { color: var(--accent); }
.unread-badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent-strong); color: #fff; font-size: 12px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.chat-item .tick { color: var(--text-dim); font-size: 11px; }

/* ===== Chat area ===== */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.chat-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-dim); gap: 8px; }
.chat-empty-icon { font-size: 64px; opacity: .5; }
.chat-empty-title { font-size: 20px; color: var(--text); }
.chat-empty-sub { font-size: 14px; }

.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; height: 100dvh; }
.chat-header {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid rgba(255,255,255,.06); background: var(--bg-panel);
}
.chat-header-info { display: flex; align-items: center; gap: 10px; cursor: pointer; min-width: 0; }
.chat-header .avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex-shrink: 0; }
.chat-header-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-header-status { font-size: 12px; color: var(--text-dim); }
.chat-members-count { margin-left: auto; font-size: 13px; color: var(--text-dim); flex-shrink: 0; }

.messages { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 4px; }
.date-divider {
  align-self: center; text-align: center; background: var(--bg-panel);
  color: var(--text-dim); font-size: 12px; padding: 5px 14px; border-radius: 12px; margin: 10px 0;
}
.msg-row { display: flex; }
.msg-row.own { justify-content: flex-end; }
.msg-bubble {
  max-width: 70%; padding: 7px 12px 6px; border-radius: var(--radius);
  background: var(--bg-panel); position: relative; word-wrap: break-word; overflow-wrap: break-word;
  display: flex; flex-direction: column;
}
.msg-row.own .msg-bubble { background: var(--bg-own); border-top-right-radius: 3px; }
.msg-row.peer .msg-bubble { border-top-left-radius: 3px; }
.msg-row.grouped .msg-bubble { border-radius: var(--radius); margin-top: 2px; }
.msg-row.grouped.own .msg-bubble { border-top-right-radius: 3px; }
.msg-row.grouped.peer .msg-bubble { border-top-left-radius: 3px; }
.msg-row.first.own .msg-bubble { margin-top: 8px; }
.msg-row.first.peer .msg-bubble { margin-top: 8px; }
.msg-sender { font-size: 13px; font-weight: 600; color: var(--accent); margin-bottom: 2px; cursor: pointer; }
.msg-text { font-size: 15px; line-height: 1.35; white-space: pre-wrap; }
.msg-meta {
  align-self: flex-end; display: flex; align-items: center; gap: 4px;
  color: var(--text-dim); font-size: 11px; margin-top: 2px; padding-top: 1px;
}
.msg-row.own .msg-meta { color: rgba(255,255,255,.55); }
.msg-meta .tick { font-size: 14px; line-height: 1; }
.msg-meta .tick.read { color: var(--accent); }
.msg-file { display: flex; align-items: center; gap: 10px; padding: 8px 10px; margin-bottom: 4px; background: rgba(255,255,255,.06); border-radius: var(--radius-sm); cursor: pointer; min-width: 210px; }
.msg-file .file-icon { font-size: 26px; }
.msg-file .file-name { font-size: 14px; font-weight: 600; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-file .file-size { font-size: 12px; color: var(--text-dim); }
.msg-image { max-width: 320px; width: 100%; border-radius: var(--radius-sm); display: block; margin-bottom: 4px; cursor: zoom-in; }
.msg-image-full {
  position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.92);
  display: flex; align-items: center; justify-content: center; cursor: zoom-out;
}
.msg-image-full img { max-width: 95vw; max-height: 95vh; border-radius: 6px; }

.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px;
  background: var(--bg-panel); border-top: 1px solid rgba(255,255,255,.06);
}
.composer-input-wrap { flex: 1; background: var(--bg-input); border-radius: 20px; padding: 8px 14px; }
#msg-input { width: 100%; background: none; border: none; outline: none; color: var(--text); font-size: 15px; resize: none; max-height: 120px; line-height: 1.4; }
.send-btn { background: var(--accent-strong); color: #fff; border-radius: 50%; font-size: 16px; }
.send-btn:hover { filter: brightness(1.15); }

.typing-indicator { padding: 4px 20px; font-size: 13px; color: var(--accent); font-style: italic; }

/* ===== Modal ===== */
.modal-overlay { position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: var(--bg-panel); border-radius: 16px; width: 100%; max-width: 420px; max-height: 85vh; display: flex; flex-direction: column; overflow: hidden; }
.modal-header { padding: 16px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid rgba(255,255,255,.08); }
.modal-header h3 { font-size: 17px; }
.modal-body { padding: 16px; overflow-y: auto; }
.modal-body input, .modal-body textarea {
  width: 100%; padding: 10px 12px; margin-bottom: 12px; background: var(--bg-input);
  border: 1px solid transparent; border-radius: var(--radius-sm); color: var(--text); outline: none; font-size: 14px; resize: vertical;
}
.modal-body input:focus, .modal-body textarea:focus { border-color: var(--accent); }
.modal-actions { padding: 12px 16px; display: flex; gap: 8px; border-top: 1px solid rgba(255,255,255,.08); }
.modal-actions .btn { width: auto; flex: 1; }
.user-list-item { display: flex; align-items: center; gap: 12px; padding: 9px 8px; border-radius: var(--radius-sm); cursor: pointer; }
.user-list-item:hover { background: var(--bg-hover); }
.user-list-item .avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff; flex-shrink: 0; }
.user-list-item .name { font-weight: 600; font-size: 15px; }
.user-list-item .sub { font-size: 12px; color: var(--text-dim); }
.checkbox { margin-left: auto; }
.group-badge { width: 46px; height: 46px; border-radius: 50%; background: var(--bg-input); display: flex; align-items: center; justify-content: center; font-size: 22px; flex-shrink: 0; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-panel); color: var(--text); padding: 12px 18px; border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,.5); z-index: 300; font-size: 14px;
  border-left: 3px solid var(--accent);
}
.toast.error { border-left-color: var(--red); }

/* ===== Responsive ===== */
@media (max-width: 760px) {
  .sidebar { width: 100%; }
  .sidebar.closed { display: none; }
  .chat-view.mobile-open { position: fixed; inset: 0; z-index: 50; width: 100%; height: 100dvh; background: var(--bg); }
  .msg-bubble { max-width: 85%; }
  .msg-image { max-width: 240px; }
}
