/* ─── Reset & Base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0f0f13;
  --surface:  #1a1a23;
  --surface2: #22222e;
  --border:   #2e2e3e;
  --accent:   #6C63FF;
  --accent2:  #9b94ff;
  --text:     #e8e8f0;
  --muted:    #7c7c9a;
  --danger:   #ff4d6d;
  --online:   #3ecf8e;
  --radius:   14px;
  --radius-sm:8px;
}

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

/* ─── Screens ───────────────────────────────────────────────────── */
.screen { display: none; width: 100%; height: 100vh; }
.screen.active { display: flex; }

/* ─── Auth ──────────────────────────────────────────────────────── */
#auth-screen {
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 60% 40%, #1e1a3a 0%, var(--bg) 70%);
}

.auth-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 36px;
  width: 380px;
  box-shadow: 0 24px 64px rgba(0,0,0,.5);
}

.auth-logo {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 28px; font-size: 22px; font-weight: 700; letter-spacing: .5px;
}

.auth-tabs {
  display: flex; border-radius: var(--radius-sm);
  background: var(--surface2); padding: 4px; margin-bottom: 28px;
}

.tab-btn {
  flex: 1; padding: 9px; border: none; border-radius: 10px;
  background: transparent; color: var(--muted); font-size: 14px; cursor: pointer; transition: all .2s;
}
.tab-btn.active { background: var(--accent); color: #fff; font-weight: 600; }

.auth-form { display: none; flex-direction: column; gap: 18px; }
.auth-form.active { display: flex; }

.field { display: flex; flex-direction: column; gap: 7px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 500; }
.field input {
  background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 11px 14px; color: var(--text); font-size: 15px; outline: none; transition: border-color .2s;
}
.field input:focus { border-color: var(--accent); }
.field-hint { font-size: 11px; color: var(--muted); margin-top: -4px; }

.btn-primary {
  padding: 12px; background: var(--accent); color: #fff; border: none; border-radius: var(--radius-sm);
  font-size: 15px; font-weight: 600; cursor: pointer; transition: opacity .2s, transform .1s;
}
.btn-primary:hover { opacity: .9; }
.btn-primary:active { transform: scale(.98); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.error-msg { font-size: 13px; color: var(--danger); min-height: 18px; display: flex; align-items: center; gap: 6px; }
.error-msg:not(:empty)::before { content: '⚠'; font-size: 14px; }

/* ─── App Layout ────────────────────────────────────────────────── */
#app-screen { flex-direction: row; }

/* ─── Sidebar ───────────────────────────────────────────────────── */
.sidebar {
  width: 320px; min-width: 320px; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; height: 100vh;
}

.sidebar-header {
  display: flex; align-items: center; gap: 10px; padding: 18px 16px;
  border-bottom: 1px solid var(--border);
}

.my-avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--accent);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 15px; flex-shrink: 0;
}

.my-nickname { flex: 1; font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  padding: 6px; border-radius: 8px; display: flex; align-items: center; transition: color .2s, background .2s;
}
.icon-btn:hover { color: var(--text); background: var(--surface2); }

/* connection status */
.conn-status { font-size: 11px; padding: 0 16px 4px; min-height: 16px; }
.conn-warn { color: #f59e0b; }
.conn-err { color: var(--danger); }

.search-wrap { padding: 12px 16px; position: relative; border-bottom: 1px solid var(--border); }

#user-search {
  width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 14px; color: var(--text); font-size: 14px; outline: none; transition: border-color .2s;
}
#user-search:focus { border-color: var(--accent); }

.search-results {
  position: absolute; top: calc(100% - 4px); left: 16px; right: 16px;
  background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; z-index: 99;
  max-height: 240px; overflow-y: auto; box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.search-results.hidden { display: none; }
.search-empty { padding: 12px 14px; color: var(--muted); font-size: 13px; }

.search-user-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer; transition: background .15s;
}
.search-user-item:hover { background: var(--border); }
.online-label { color: var(--online); font-size: 11px; }

.chats-label {
  padding: 14px 16px 8px; font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .8px; color: var(--muted);
}

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

.chat-item {
  display: flex; align-items: center; gap: 11px; padding: 12px 16px;
  cursor: pointer; transition: background .15s; border-radius: 10px; margin: 2px 8px;
}
.chat-item:hover { background: var(--surface2); }
.chat-item.active { background: rgba(108, 99, 255, .18); }

.chat-item-avatar {
  width: 44px; height: 44px; border-radius: 50%; background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px; flex-shrink: 0; position: relative;
}

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

.chat-item-info { flex: 1; min-width: 0; }

.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.chat-item-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }

.chat-item-bottom { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.chat-item-last { flex: 1; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.typing-label { color: var(--accent2); font-style: italic; }

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

.empty-state { text-align: center; color: var(--muted); font-size: 14px; padding: 40px 20px; }

/* ─── Chat Area ─────────────────────────────────────────────────── */
.chat-area {
  flex: 1; display: flex; align-items: center; justify-content: center;
  background: var(--bg); height: 100vh;
}

.no-chat-selected {
  display: flex; flex-direction: column; align-items: center; gap: 14px; color: var(--muted); font-size: 15px;
}

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

.chat-header {
  display: flex; align-items: center; gap: 12px; padding: 16px 24px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}

.chat-avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--accent);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 16px; flex-shrink: 0;
}

.chat-name { font-size: 15px; font-weight: 700; }
.chat-status { font-size: 12px; color: var(--muted); margin-top: 2px; transition: color .3s; }
.chat-status.online { color: var(--online); }

/* ─── Messages ───────────────────────────────────────────────────── */
.messages {
  flex: 1; overflow-y: auto; padding: 20px 24px; display: flex; flex-direction: column; gap: 4px;
}

.msg-group { display: flex; flex-direction: column; max-width: 68%; animation: msg-in .2s ease-out; }
.msg-group.mine { align-self: flex-end; align-items: flex-end; }
.msg-group.theirs { align-self: flex-start; align-items: flex-start; }

@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.msg-sender-name { font-size: 11px; color: var(--accent2); margin-bottom: 4px; font-weight: 600; }

.msg-bubble {
  padding: 10px 14px; border-radius: 16px; font-size: 14px; line-height: 1.5;
  max-width: 100%; word-break: break-word;
}
.msg-group.mine .msg-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg-group.theirs .msg-bubble { background: var(--surface2); color: var(--text); border-bottom-left-radius: 4px; }

.msg-meta {
  display: flex; align-items: center; gap: 4px; padding: 0 4px; margin-top: 3px;
}

.msg-time { font-size: 10px; color: var(--muted); }

/* ✓ = sent (серая 1 галочка), ✓✓ delivered (серые), ✓✓ read (фиолетовые) */
.msg-check {
  font-size: 12px; letter-spacing: -2px; transition: color .3s;
  font-weight: 600; margin-left: 2px;
}
.msg-check.sent { color: rgba(255,255,255,.45); }
.msg-check.delivered { color: rgba(255,255,255,.7); }
.msg-check.read { color: var(--accent2); }

/* ─── Typing bar ─────────────────────────────────────────────────── */
.typing-bar { height: 22px; padding: 0 24px; font-size: 12px; color: var(--accent2); }

.typing-dots span { animation: dot-blink 1.4s infinite; }
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }

@keyframes dot-blink { 0%, 60%, 100% { opacity: 0; } 30% { opacity: 1; } }

/* ─── Message Form ───────────────────────────────────────────────── */
.message-form {
  display: flex; align-items: center; gap: 10px; padding: 16px 24px;
  border-top: 1px solid var(--border); background: var(--surface);
}

.message-form input {
  flex: 1; background: var(--surface2); border: 1px solid var(--border); border-radius: 12px;
  padding: 11px 16px; color: var(--text); font-size: 14px; outline: none; transition: border-color .2s;
}
.message-form input:focus { border-color: var(--accent); }

.send-btn {
  width: 42px; height: 42px; border-radius: 50%; background: var(--accent); border: none;
  color: #fff; display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0; transition: opacity .2s, transform .1s;
}
.send-btn:hover { opacity: .85; }
.send-btn:active { transform: scale(.93); }

/* ─── Scrollbar ───────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ─── Avatar colors ──────────────────────────────────────────────── */
.av-0 { background: #6C63FF; }
.av-1 { background: #e84393; }
.av-2 { background: #f59e0b; }
.av-3 { background: #10b981; }
.av-4 { background: #ef4444; }
.av-5 { background: #3b82f6; }

/* ─── E2E Lock indicator ─────────────────────────────────────────── */
.msg-lock {
  font-size: 10px;
  margin-right: 3px;
  opacity: .7;
}

.e2e-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  color: #10b981;
  margin-left: 6px;
  opacity: .85;
}

.chat-item-name .e2e-icon {
  font-size: 11px;
  margin-left: 4px;
  opacity: .6;
}

/* ─── Header search button ───────────────────────────────────────── */
.header-icon-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  margin-left: auto;
  display: flex;
  align-items: center;
  transition: background .15s, color .15s;
}
.header-icon-btn:hover { background: var(--hover); color: var(--text); }

/* ─── Attach button ───────────────────────────────────────────────── */
.attach-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  transition: background .15s, color .15s;
}
.attach-btn:hover { background: var(--hover); color: var(--accent); }

/* ─── Reply bar in form ───────────────────────────────────────────── */
.reply-bar, .edit-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 10px;
  background: var(--hover);
  border-left: 3px solid var(--accent);
  font-size: 12px;
  color: var(--text-secondary);
  border-radius: 4px;
  margin-bottom: 4px;
}
.reply-bar button, .edit-bar button {
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary); font-size: 14px; padding: 0 4px;
}

/* ─── Reply preview inside bubble ────────────────────────────────── */
.msg-reply-preview {
  background: rgba(0,0,0,.12);
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  padding: 3px 7px;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reply-nick { font-weight: 600; color: var(--accent); margin-right: 4px; }

/* ─── Media in messages ───────────────────────────────────────────── */
.msg-media-img {
  max-width: 240px; max-height: 240px;
  border-radius: 8px; display: block;
  margin-top: 6px; cursor: pointer;
  object-fit: cover;
}
.msg-media-video {
  max-width: 240px; border-radius: 8px;
  display: block; margin-top: 6px;
}
.msg-media-audio {
  display: block; margin-top: 6px;
  max-width: 240px; width: 100%;
}
.msg-media-file {
  display: inline-flex; align-items: center; gap: 6px;
  margin-top: 6px; padding: 6px 10px;
  background: rgba(0,0,0,.15); border-radius: 8px;
  color: var(--text); text-decoration: none; font-size: 13px;
}
.msg-media-file:hover { background: rgba(0,0,0,.25); }

/* ─── Reactions bar ───────────────────────────────────────────────── */
.msg-react-bar {
  display: flex; flex-wrap: wrap; gap: 4px;
  margin: 2px 0 0 0; min-height: 0;
}
.react-chip {
  background: var(--hover);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1px 7px;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s;
}
.react-chip.active {
  background: rgba(108,99,255,.2);
  border-color: var(--accent);
}
.react-chip:hover { background: var(--border); }
.react-add-btn {
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 1px 8px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color .15s;
}
.react-add-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ─── Emoji picker popup ──────────────────────────────────────────── */
.emoji-picker-popup {
  position: fixed;
  z-index: 999;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  display: flex; gap: 4px;
  box-shadow: 0 4px 20px rgba(0,0,0,.3);
}
.emoji-picker-popup button {
  background: none; border: none;
  font-size: 22px; cursor: pointer;
  border-radius: 8px; padding: 4px;
  transition: background .1s;
}
.emoji-picker-popup button:hover { background: var(--hover); }

/* ─── Context menu ────────────────────────────────────────────────── */
.msg-ctx-menu {
  position: fixed;
  z-index: 999;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0,0,0,.3);
  display: flex; flex-direction: column;
  min-width: 150px; overflow: hidden;
}
.msg-ctx-menu button {
  background: none; border: none;
  text-align: left; padding: 10px 14px;
  font-size: 13px; color: var(--text);
  cursor: pointer; transition: background .1s;
}
.msg-ctx-menu button:hover { background: var(--hover); }

/* ─── Message search bar ──────────────────────────────────────────── */
.msg-search-bar {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.msg-search-bar input {
  flex: 1; background: var(--hover);
  border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px;
  color: var(--text); font-size: 13px;
}
.msg-search-bar button {
  background: none; border: none;
  color: var(--text-secondary); cursor: pointer;
  font-size: 16px; padding: 4px;
  border-radius: 6px; transition: background .1s;
}
.msg-search-bar button:hover { background: var(--hover); }
.search-results-header {
  padding: 8px 16px;
  font-size: 12px;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
}

/* ─── Load more chats button ─────────────────────────────────────── */
.load-more-wrap {
  padding: 8px 12px;
  text-align: center;
}
.load-more-btn {
  width: 100%;
  padding: 8px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: background .15s;
}
.load-more-btn:hover {
  background: var(--hover);
  color: var(--text);
}
