/* ═══ CSS Variables ═══ */
:root {
  --bg-primary: #f0f2f5;
  --bg-secondary: #ffffff;
  --bg-sidebar: #ffffff;
  --bg-chat: #f0f2f5;
  --bg-hover: #f2f2f2;
  --bg-active: #e8f0fe;
  --border: #e4e6eb;
  --border-light: #f0f0f0;
  --text-primary: #050505;
  --text-secondary: #65676b;
  --text-tertiary: #8a8d91;
  --accent: #0084ff;
  --accent-hover: #0073e6;
  --accent-light: #e7f3ff;
  --danger: #dc3545;
  --danger-light: #fde7e9;
  --success: #00a884;
  --success-light: #dff6dd;
  --warning: #f59e0b;
  --warning-light: #fff3cd;
  --radius-sm: 6px;
  --radius: 12px;
  --radius-lg: 16px;
  --radius-full: 50%;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.08);
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-md: 0 2px 8px rgba(0,0,0,0.12);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.16);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  --sidebar-width: 340px;
  --header-height: 56px;
  --bottom-nav-height: 56px;
  --transition: 0.15s ease;
}

/* ═══ Reset ═══ */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; font-family: var(--font); font-size: 14px; color: var(--text-primary); background: var(--bg-primary); -webkit-font-smoothing: antialiased; overflow: hidden; }
a { color: var(--accent); text-decoration: none; cursor: pointer; }
a:hover { text-decoration: underline; }
button { cursor: pointer; border: none; background: none; font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: inherit; outline: none; }
ul, ol { list-style: none; }
img { max-width: 100%; display: block; }
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #aaa; }

/* ═══ Layout ═══ */
#app { display: flex; height: 100vh; overflow: hidden; }

#sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

#sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: var(--header-height);
}

#sidebar-header h1 {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
}

#sidebar-search {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
}

#sidebar-search .search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-primary);
  border-radius: 20px;
  padding: 8px 14px;
}

#sidebar-search .search-box .material-symbols-outlined {
  font-size: 18px;
  color: var(--text-tertiary);
}

#sidebar-search input {
  flex: 1;
  border: none;
  background: none;
  font-size: 13px;
  color: var(--text-primary);
}

#sidebar-search input::placeholder { color: var(--text-tertiary); }

#room-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

#sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 8px;
}

#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

#content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ═══ Buttons ═══ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  transition: background var(--transition), opacity var(--transition);
  white-space: nowrap;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #c82333; }

.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg-hover); }

.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-lg { padding: 10px 24px; font-size: 15px; }

.btn-block { width: 100%; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  transition: background var(--transition);
}

.icon-btn:hover { background: var(--bg-hover); }
.icon-btn .material-symbols-outlined { font-size: 20px; }

/* ═══ Card ═══ */
.card {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.card-header {
  padding: 16px;
  border-bottom: 1px solid var(--border-light);
  font-size: 15px;
  font-weight: 600;
}

/* ═══ Input ═══ */
.input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--bg-secondary);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(0,132,255,0.15);
}

.input::placeholder { color: var(--text-tertiary); }

.input-group { margin-bottom: 12px; }
.input-group label { display: block; font-size: 12px; font-weight: 500; color: var(--text-secondary); margin-bottom: 4px; }

/* ═══ Badge ═══ */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
}

.badge-admin { background: var(--warning-light); color: #92400e; }
.badge-user { background: var(--accent-light); color: var(--accent); }
.badge-ok { background: var(--success-light); color: #065f46; }
.badge-err { background: var(--danger-light); color: var(--danger); }
.badge-online { background: var(--success); color: #fff; }

/* ═══ Toast ═══ */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  box-shadow: var(--shadow-md);
  animation: toastIn 0.2s ease;
  max-width: 360px;
}

.toast-success { background: var(--success); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
.toast-info { background: var(--text-primary); color: #fff; }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ═══ Modal ═══ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9000;
  animation: fadeIn 0.15s ease;
}

.modal {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 90%;
  max-width: 420px;
  max-height: 80vh;
  overflow: hidden;
  animation: modalIn 0.2s ease;
}

.modal-header {
  padding: 16px;
  border-bottom: 1px solid var(--border-light);
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-body { padding: 16px; }
.modal-footer { padding: 12px 16px; border-top: 1px solid var(--border-light); display: flex; justify-content: flex-end; gap: 8px; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes modalIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }

/* ═══ Avatar ═══ */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-full);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  flex-shrink: 0;
  user-select: none;
}

.avatar-sm { width: 32px; height: 32px; font-size: 12px; }
.avatar-md { width: 40px; height: 40px; font-size: 14px; }
.avatar-lg { width: 64px; height: 64px; font-size: 22px; }

.avatar-online { position: relative; }
.avatar-online::after {
  content: '';
  position: absolute;
  bottom: 0;
  right: 0;
  width: 10px;
  height: 10px;
  background: var(--success);
  border: 2px solid var(--bg-secondary);
  border-radius: var(--radius-full);
}

/* ═══ Empty / Loading ═══ */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  color: var(--text-tertiary);
  text-align: center;
  gap: 12px;
}

.empty-state .material-symbols-outlined { font-size: 48px; opacity: 0.4; }

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  color: var(--text-tertiary);
  font-size: 13px;
  gap: 8px;
}

.loading .material-symbols-outlined { font-size: 20px; animation: spin 1s linear infinite; }

@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* ═══ Auth Page ═══ */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
  background: var(--bg-primary);
}

.auth-card {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.auth-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 8px;
}

.auth-logo .material-symbols-outlined { font-size: 36px; color: var(--accent); }
.auth-logo span { font-size: 28px; font-weight: 700; color: var(--accent); }

.auth-title {
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 24px;
  color: var(--text-primary);
}

.auth-error {
  color: var(--danger);
  font-size: 12px;
  text-align: center;
  margin-top: 12px;
  min-height: 18px;
}

.auth-link {
  text-align: center;
  margin-top: 16px;
  font-size: 13px;
  color: var(--text-secondary);
}

/* ═══ Room Item ═══ */
.room-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background var(--transition);
  border-left: 3px solid transparent;
}

.room-item:hover { background: var(--bg-hover); }
.room-item.active { background: var(--accent-light); border-left-color: var(--accent); }

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

.room-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-last {
  font-size: 12px;
  color: var(--text-tertiary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.room-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
}

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

.room-unread {
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
  min-width: 18px;
  text-align: center;
}

.room-create-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 12px;
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  transition: background var(--transition);
}

.room-create-btn:hover { background: var(--accent-light); }

/* ═══ Chat Header ═══ */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  min-height: var(--header-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-header-back { display: none; }

.chat-header-title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header-sub {
  font-size: 12px;
  color: var(--text-tertiary);
  font-weight: 400;
}

.chat-header-actions { display: flex; gap: 4px; }

/* ═══ Messages ═══ */
.messages-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--bg-chat);
}

.msg {
  display: flex;
  gap: 8px;
  max-width: 70%;
  animation: msgIn 0.15s ease;
}

.msg-other { align-self: flex-start; }
.msg-mine { align-self: flex-end; flex-direction: row-reverse; }

.msg-avatar { flex-shrink: 0; align-self: flex-end; }

.msg-body { display: flex; flex-direction: column; gap: 2px; }

.msg-sender {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 0 4px;
}

.msg-bubble {
  padding: 8px 12px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
  position: relative;
}

.msg-other .msg-bubble {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.msg-mine .msg-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg-time {
  font-size: 10px;
  color: var(--text-tertiary);
  padding: 0 4px;
}

.msg-mine .msg-time { text-align: right; }

.msg-system {
  align-self: center;
  font-size: 12px;
  color: var(--text-tertiary);
  padding: 4px 12px;
  background: rgba(0,0,0,0.05);
  border-radius: 12px;
  margin: 8px 0;
}

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

/* ═══ Chat Input ═══ */
.chat-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input-actions {
  display: flex;
  gap: 2px;
}

.chat-input-field {
  flex: 1;
  min-height: 36px;
  max-height: 120px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  resize: none;
  font-size: 14px;
  line-height: 1.4;
  background: var(--bg-primary);
  overflow-y: auto;
}

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

.chat-input-field::placeholder { color: var(--text-tertiary); }

/* ═══ Welcome ═══ */
.welcome-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-tertiary);
  gap: 12px;
}

.welcome-page .material-symbols-outlined { font-size: 64px; opacity: 0.3; }
.welcome-page p { font-size: 15px; }

/* ═══ Create Room ═══ */
.page-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  min-height: var(--header-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.page-header h2 { flex: 1; font-size: 16px; font-weight: 600; }

.create-body { padding: 16px; overflow-y: auto; flex: 1; }

.create-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-primary);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 20px;
}

.create-tab {
  flex: 1;
  padding: 8px;
  text-align: center;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all var(--transition);
}

.create-tab.active { background: var(--bg-secondary); color: var(--accent); box-shadow: var(--shadow-sm); }

.member-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.member-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  background: var(--accent-light);
  color: var(--accent);
  border-radius: 16px;
  font-size: 12px;
}

.member-tag .remove {
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  opacity: 0.7;
}

.member-tag .remove:hover { opacity: 1; }

/* ═══ Profile ═══ */
.profile-body { padding: 24px 16px; overflow-y: auto; flex: 1; }

.profile-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px 0;
}

.profile-name { font-size: 20px; font-weight: 600; }
.profile-id { font-size: 12px; color: var(--text-tertiary); font-family: var(--font-mono); }

.profile-table {
  width: 100%;
  border-collapse: collapse;
}

.profile-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-light);
  font-size: 13px;
}

.profile-table td:first-child { color: var(--text-secondary); width: 100px; }

.profile-actions {
  display: flex;
  gap: 8px;
  margin-top: 24px;
  justify-content: center;
}

/* ═══ Bottom Nav (Mobile) ═══ */
#bottom-nav {
  display: none;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
}

.bottom-nav-items {
  display: flex;
  height: var(--bottom-nav-height);
}

.bottom-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--text-tertiary);
  font-size: 10px;
  transition: color var(--transition);
}

.bottom-nav-item.active { color: var(--accent); }
.bottom-nav-item .material-symbols-outlined { font-size: 22px; }

/* ═══ Mobile Auth ═══ */
@media (max-width: 768px) {
  .auth-card { padding: 32px 24px; }

  #sidebar { display: none; }

  #bottom-nav { display: block; }

  .chat-header-back { display: flex; }

  .msg { max-width: 85%; }

  .chat-input { padding: 8px 12px; }

  .messages-container { padding: 12px; }

  .welcome-page { display: none; }
}
