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

:root {
  --bg-dark: #0f1117;
  --bg-sidebar: #1a1d27;
  --bg-chat: #13151e;
  --bg-input: #1e2130;
  --bg-message-hover: #1e2130;
  --accent: #5865f2;
  --accent-hover: #4752c4;
  --text-primary: #e8eaf0;
  --text-secondary: #8b8fa8;
  --text-muted: #5c5f73;
  --border: #2a2d3e;
  --system-msg: #5c6bc0;
  --radius: 8px;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

body {
  font-family: var(--font);
  background: var(--bg-dark);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
}

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

/* ── Login ── */
#login-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f1117 0%, #1a1d27 100%);
}

.login-box {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

/* ── Login page: user avatar ── */
.user-avatar-login {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  margin: 0 auto 12px;
  overflow: hidden;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 700;
  color: #fff;
}
.user-avatar-login img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.btn-signout {
  display: block;
  text-align: center;
  margin-top: 16px;
  font-size: 13px;
  color: var(--text-muted);
  text-decoration: none;
  transition: color 0.2s;
}
.btn-signout:hover { color: var(--text-secondary); }

.logo {
  text-align: center;
  margin-bottom: 32px;
}

.logo-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 12px;
}

.logo h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.logo p {
  color: var(--text-secondary);
  font-size: 14px;
}

.input-group {
  margin-bottom: 20px;
}

.input-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.input-group input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text-primary);
  font-size: 15px;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.2s;
}

.input-group input:focus {
  border-color: var(--accent);
}

.room-select-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.room-option {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 16px;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
}

.room-option:hover {
  border-color: var(--accent);
  color: var(--text-primary);
}

.room-option.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn-join {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 13px;
  font-size: 15px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.2s;
  margin-top: 4px;
}

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

/* ── Chat Layout ── */
#chat-screen {
  flex-direction: row;
}

/* ── Sidebar ── */
.sidebar {
  width: 240px;
  min-width: 240px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 18px 16px;
  font-weight: 700;
  font-size: 15px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-icon-small { font-size: 20px; }

.sidebar-section {
  padding: 16px 0 8px;
  flex: 1;
  overflow-y: auto;
}

.sidebar-label {
  padding: 0 16px 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.sidebar-section ul {
  list-style: none;
}

.sidebar-section ul li {
  padding: 6px 16px;
  border-radius: 6px;
  margin: 1px 8px;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  gap: 6px;
}

.sidebar-section ul li:hover {
  background: var(--bg-input);
  color: var(--text-primary);
}

.sidebar-section ul li.active {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

#room-list li::before { content: '# '; opacity: 0.6; }

.user-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.current-user-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  flex: 1;
}

.current-user-badge .badge-pic {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}
.current-user-badge .badge-initial {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}
.current-user-badge .badge-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-footer-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.admin-link {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  text-decoration: none;
  transition: all 0.15s;
}
.admin-link:hover {
  background: var(--bg-input);
  color: var(--text-primary);
}

#logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  width: 28px;
  height: 28px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.15s;
}

#logout-btn:hover {
  background: #e74c3c22;
  border-color: #e74c3c;
  color: #e74c3c;
}

/* ── Chat Area ── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-chat);
}

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

#user-count-badge {
  font-size: 12px;
  font-weight: 400;
  color: var(--text-secondary);
  background: var(--bg-input);
  padding: 3px 10px;
  border-radius: 20px;
}

/* ── Messages ── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scroll-behavior: smooth;
}

.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.message {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 5px 8px;
  border-radius: var(--radius);
  transition: background 0.1s;
  animation: fadeIn 0.2s ease;
}

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

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  color: #fff;
  flex-shrink: 0;
  margin-top: 2px;
  overflow: hidden;
}

.msg-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.msg-content { flex: 1; min-width: 0; }

.msg-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 3px;
}

.msg-username {
  font-weight: 700;
  font-size: 14px;
}

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

.msg-text {
  font-size: 15px;
  line-height: 1.5;
  color: var(--text-primary);
  word-break: break-word;
  white-space: pre-wrap;
}

/* System messages */
.system-message {
  text-align: center;
  font-size: 12px;
  color: var(--system-msg);
  padding: 4px 0;
  font-style: italic;
}

/* ── Typing Indicator ── */
.typing-indicator {
  min-height: 20px;
  padding: 0 32px 4px;
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
}

/* ── Message Input ── */
.message-form {
  display: flex;
  gap: 10px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  background: var(--bg-chat);
}

.message-form input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text-primary);
  font-size: 15px;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.2s;
}

.message-form input:focus {
  border-color: var(--accent);
}

.message-form input::placeholder { color: var(--text-muted); }

/* ── File messages ── */
.msg-image {
  max-width: min(320px, 100%);
  max-height: 280px;
  border-radius: 8px;
  cursor: pointer;
  object-fit: cover;
  display: block;
  margin-top: 6px;
  border: 1px solid var(--border);
  transition: opacity 0.15s;
}
.msg-image:hover { opacity: 0.9; }

.msg-file-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  color: var(--text-primary);
  text-decoration: none;
  font-size: 14px;
  margin-top: 6px;
  transition: border-color 0.15s;
  max-width: 320px;
}
.msg-file-link:hover { border-color: var(--accent); }
.msg-file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-file-size { color: var(--text-muted); font-size: 12px; flex-shrink: 0; }

/* ── Attach button ── */
.btn-attach {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  width: 44px;
  height: 44px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  transition: all 0.15s;
}
.btn-attach:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ── Upload progress ── */
.upload-progress {
  position: absolute;
  bottom: 76px;
  left: 24px;
  right: 24px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text-secondary);
  display: none;
  align-items: center;
  gap: 10px;
  z-index: 5;
}
.upload-progress.visible { display: flex; }
.upload-progress-bar {
  flex: 1;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
.upload-progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.3s;
}

.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 12px 20px;
  font-size: 14px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s;
}

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

/* ── Message grouping ── */
.message.continuation {
  padding-top: 1px;
  margin-top: -2px;
}

.msg-avatar-placeholder {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

/* ── Links in messages ── */
.msg-link {
  color: #7289da;
  text-decoration: none;
}
.msg-link:hover { text-decoration: underline; }

/* ── Scroll-to-bottom button ── */
.scroll-btn {
  position: absolute;
  bottom: 76px;
  right: 20px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
  z-index: 10;
  transition: background 0.2s;
}
.scroll-btn.visible { display: flex; }
.scroll-btn:hover { background: var(--accent-hover); }

/* ── Chat area needs relative positioning for scroll btn ── */
.chat-area { position: relative; }

/* ── Sidebar toggle (hidden on desktop) ── */
.sidebar-toggle {
  display: none;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 20px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
  margin-right: 4px;
  transition: background 0.15s;
}
.sidebar-toggle:hover { background: var(--bg-input); }

/* ── Mobile ── */
@media (max-width: 640px) {
  .sidebar { width: 200px; min-width: 200px; }
  .login-box { padding: 28px 20px; margin: 16px; }
  .messages { padding: 12px; }
  .message-form { padding: 12px; }
  .chat-header { padding: 12px 16px; }
}

@media (max-width: 480px) {
  .sidebar-toggle { display: block; }
  .sidebar {
    position: fixed;
    left: -240px;
    top: 0;
    height: 100vh;
    z-index: 100;
    transition: left 0.25s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,0.5);
  }
  .sidebar.open { left: 0; }
}

/* ── Message actions ── */
.message { position: relative; }

.msg-actions {
  position: absolute;
  top: 4px;
  right: 8px;
  display: none;
  align-items: center;
  gap: 2px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 4px;
  z-index: 2;
}
.message:hover .msg-actions { display: flex; }

.msg-action-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  padding: 3px 6px;
  border-radius: 4px;
  transition: all 0.15s;
  line-height: 1;
}
.msg-action-btn:hover { background: var(--bg-input); color: var(--text-primary); }
.msg-action-btn.danger:hover { color: #e74c3c; }

/* ── Edit input ── */
.msg-edit-input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 6px 10px;
  color: var(--text-primary);
  font-size: 15px;
  font-family: var(--font);
  outline: none;
}

/* ── Edited tag ── */
.msg-edited {
  font-size: 11px;
  color: var(--text-muted);
  font-style: italic;
}

/* ── Reply quote in message ── */
.msg-reply-quote {
  display: flex;
  gap: 8px;
  align-items: baseline;
  background: var(--bg-input);
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
  padding: 5px 10px;
  margin-bottom: 4px;
  cursor: pointer;
  transition: background 0.15s;
  max-width: 100%;
  overflow: hidden;
}
.msg-reply-quote:hover { background: var(--border); }
.msg-reply-quote-name { font-size: 12px; font-weight: 700; color: var(--accent); flex-shrink: 0; }
.msg-reply-quote-text { font-size: 12px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── Reply bar (above input) ── */
.reply-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 24px;
  background: var(--bg-input);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
.reply-bar-icon { color: var(--accent); font-size: 16px; flex-shrink: 0; }
.reply-bar-info { flex: 1; overflow: hidden; }
.reply-bar-name { font-size: 12px; font-weight: 700; color: var(--accent); margin-right: 6px; }
.reply-bar-text { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reply-bar-cancel {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 4px;
  flex-shrink: 0;
  transition: color 0.15s;
}
.reply-bar-cancel:hover { color: var(--text-primary); }

/* ── Video messages ── */
.msg-video {
  max-width: min(400px, 100%);
  max-height: 300px;
  border-radius: 8px;
  display: block;
  margin-top: 6px;
  border: 1px solid var(--border);
}

/* ── Highlight on reply scroll ── */
@keyframes highlightFade {
  0%   { background: var(--accent)22; }
  100% { background: transparent; }
}
.message.highlight { animation: highlightFade 1.5s ease; }
