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

:root {
  --bg: #0f0f13;
  --bg2: #16161d;
  --bg3: #1e1e28;
  --surface: #23232f;
  --surface2: #2a2a38;
  --border: #2e2e3e;
  --primary: #6C63FF;
  --primary-dark: #5a52e0;
  --accent: #4FACFE;
  --text: #e8e8f0;
  --text2: #9999b3;
  --text3: #6666888;
  --system: #f0b429;
  --success: #3dd68c;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --transition: 0.2s ease;
}

html, body {
  height: 100%;
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ===== Screen ===== */
.screen { position: fixed; inset: 0; display: none; }
.screen.active { display: flex; }

/* ===== Login ===== */
#loginScreen {
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 30% 20%, rgba(108,99,255,0.2) 0%, transparent 60%),
              radial-gradient(ellipse at 70% 80%, rgba(79,172,254,0.15) 0%, transparent 60%),
              var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow), 0 0 0 1px rgba(108,99,255,0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  animation: slideUp 0.4s ease;
}

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

.login-logo { margin-bottom: 4px; filter: drop-shadow(0 4px 16px rgba(108,99,255,0.4)); }
.login-card h1 { font-size: 1.8rem; font-weight: 700; letter-spacing: -0.5px; }
.login-sub { color: var(--text2); font-size: 0.9rem; margin-top: -8px; }

.input-group {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}
.input-group label { font-size: 0.85rem; color: var(--text2); font-weight: 500; }
.input-group input {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1rem;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.input-group input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(108,99,255,0.15);
}
.input-group input::placeholder { color: var(--text2); }

.btn-primary {
  width: 100%;
  padding: 13px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity var(--transition), transform var(--transition), box-shadow var(--transition);
  margin-top: 4px;
  box-shadow: 0 4px 16px rgba(108,99,255,0.3);
}
.btn-primary:hover { opacity: 0.92; transform: translateY(-1px); box-shadow: 0 6px 20px rgba(108,99,255,0.4); }
.btn-primary:active { transform: translateY(0); }

/* ===== Chat Layout ===== */
#chatScreen { background: var(--bg); }

.chat-layout {
  display: flex;
  width: 100%;
  height: 100%;
}

/* ===== Sidebar ===== */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform var(--transition), min-width var(--transition);
  overflow: hidden;
}

.sidebar.collapsed {
  min-width: 0;
  width: 0;
  transform: translateX(-260px);
  border-right: none;
}

.sidebar-header {
  padding: 20px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.room-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.room-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.room-name { font-weight: 600; font-size: 0.95rem; }
.online-count {
  font-size: 0.78rem;
  color: var(--text2);
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 2px;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--success);
  animation: pulse 2s infinite;
  flex-shrink: 0;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.sidebar-section-title {
  padding: 12px 16px 6px;
  font-size: 0.75rem;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}

.user-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 8px;
}

.user-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  transition: background var(--transition);
}
.user-item:hover { background: var(--surface); }

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
  text-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.user-item-name {
  font-size: 0.88rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-item-name .you-tag {
  font-size: 0.72rem;
  color: var(--text2);
  margin-left: 4px;
}

/* ===== Chat Main ===== */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

/* Header */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  height: 60px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-toggle {
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  transition: background var(--transition), color var(--transition);
}
.sidebar-toggle:hover { background: var(--surface); color: var(--text); }

.header-title h2 {
  font-size: 1rem;
  font-weight: 600;
}

.header-user {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text2);
}

.header-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: white;
  text-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

/* Message List */
.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

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

.welcome-tip {
  text-align: center;
  color: var(--text2);
  font-size: 0.85rem;
  padding: 24px 0 12px;
}

/* System Message */
.msg-system {
  text-align: center;
  padding: 6px 0;
}
.msg-system .sys-bubble {
  display: inline-block;
  padding: 4px 14px;
  background: rgba(240, 180, 41, 0.1);
  border: 1px solid rgba(240, 180, 41, 0.2);
  border-radius: 20px;
  color: var(--system);
  font-size: 0.8rem;
}

/* Chat Message */
.msg-row {
  display: flex;
  gap: 10px;
  padding: 3px 0;
  animation: fadeIn 0.2s ease;
}

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

.msg-row.self {
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
  align-self: flex-end;
  margin-bottom: 2px;
  text-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.msg-content {
  max-width: 70%;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.msg-row.self .msg-content {
  align-items: flex-end;
}

.msg-meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 0 4px;
}

.msg-row.self .msg-meta {
  flex-direction: row-reverse;
}

.msg-name {
  font-size: 0.8rem;
  font-weight: 600;
}

.msg-time {
  font-size: 0.72rem;
  color: var(--text2);
}

.msg-bubble {
  padding: 9px 14px;
  border-radius: 16px;
  font-size: 0.92rem;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
}

.msg-row:not(.self) .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  color: var(--text);
}

.msg-row.self .msg-bubble {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 12px rgba(108,99,255,0.25);
}

/* Consecutive messages from same user */
.msg-row.consecutive .msg-avatar { visibility: hidden; }
.msg-row.consecutive:not(.self) .msg-bubble { border-top-left-radius: 4px; border-bottom-left-radius: 4px; }
.msg-row.consecutive.self .msg-bubble { border-top-right-radius: 4px; border-bottom-right-radius: 4px; }
.msg-row.consecutive .msg-meta { display: none; }

/* Typing Bar */
.typing-bar {
  min-height: 24px;
  padding: 4px 20px;
  font-size: 0.8rem;
  color: var(--text2);
  font-style: italic;
  flex-shrink: 0;
}

/* Input Area */
.input-area {
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(108,99,255,0.1);
}

#messageInput {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 0.93rem;
  font-family: inherit;
  resize: none;
  max-height: 120px;
  min-height: 24px;
  line-height: 1.5;
  overflow-y: auto;
}
#messageInput::placeholder { color: var(--text2); }

.input-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.char-count {
  font-size: 0.75rem;
  color: var(--text2);
  min-width: 44px;
  text-align: right;
}

.char-count.warning { color: #f0b429; }
.char-count.danger { color: #ff5c5c; }

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--transition), transform var(--transition), box-shadow var(--transition);
  box-shadow: 0 2px 10px rgba(108,99,255,0.3);
}
.send-btn:hover { opacity: 0.9; transform: scale(1.05); box-shadow: 0 4px 14px rgba(108,99,255,0.4); }
.send-btn:active { transform: scale(0.96); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; box-shadow: none; }

/* ===== Scrollbar ===== */
.user-list::-webkit-scrollbar { width: 4px; }
.user-list::-webkit-scrollbar-track { background: transparent; }
.user-list::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 2px; }

/* ===== Date Divider ===== */
.date-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  color: var(--text2);
  font-size: 0.75rem;
}
.date-divider::before, .date-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ===== Responsive ===== */
@media (max-width: 640px) {
  .sidebar {
    position: fixed;
    z-index: 100;
    top: 0;
    left: 0;
    height: 100%;
    transform: translateX(-260px);
    box-shadow: var(--shadow);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .sidebar.collapsed {
    transform: translateX(-260px);
  }
  .msg-content { max-width: 85%; }
  .login-card { margin: 16px; padding: 32px 24px; }
}
