/* ========== 全局 ========== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #fafafa;
  --bg-card: #ffffff;
  --bg-input: #f3f4f6;
  --bg-hover: #f3f4f6;
  --border: #e5e7eb;
  --border-focus: #c7d2fe;
  --text: #1a1a2e;
  --text-dim: #6b7280;
  --text-own: #ffffff;
  --text-own-dim: rgba(255, 255, 255, 0.7);
  --accent: #6366f1;
  --accent-light: #8b5cf6;
  --accent-glow: rgba(99, 102, 241, 0.15);
  --danger: #ef4444;
  --success: #22c55e;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 40px rgba(0, 0, 0, 0.1);
}

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

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 900px;
  margin: 0 auto;
  background: var(--bg);
}

.hidden {
  display: none !important;
}

/* ========== 顶栏 ========== */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  font-weight: 700;
  font-size: 18px;
  background: linear-gradient(135deg, var(--accent), var(--accent-light));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.room-badge {
  font-family: "JetBrains Mono", "Fira Code", monospace;
  font-size: 13px;
  color: var(--text-dim);
  background: var(--bg-input);
  padding: 3px 10px;
  border-radius: 20px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.online-count {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 6px rgba(34, 197, 94, 0.4);
  transition: background 0.3s, box-shadow 0.3s;
}

.nickname {
  font-size: 13px;
  color: var(--text-dim);
}

.btn-icon {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  color: var(--text-dim);
  transition: all 0.15s;
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text);
}

/* ========== 消息区 ========== */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
  background: var(--bg);
}

.messages::-webkit-scrollbar {
  width: 4px;
}
.messages::-webkit-scrollbar-track {
  background: transparent;
}
.messages::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 4px;
}
.messages::-webkit-scrollbar-thumb:hover {
  background: #9ca3af;
}

.messages-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  gap: 8px;
  font-size: 14px;
}

.empty-icon {
  font-size: 48px;
  opacity: 0.4;
}

/* 消息气泡 */
.msg-row {
  display: flex;
  gap: 10px;
  max-width: 85%;
  animation: fadeIn 0.2s ease-out;
}

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

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

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

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

.msg-nick {
  font-size: 12px;
  color: var(--text-dim);
  padding: 0 4px;
}

.msg-row.own .msg-nick {
  text-align: right;
  color: var(--text-dim);
}

.msg-bubble {
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}

.msg-row.own .msg-bubble {
  background: linear-gradient(135deg, var(--accent), var(--accent-light));
  border-color: transparent;
  color: var(--text-own);
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.2);
}

.msg-bubble img,
.msg-bubble video {
  max-width: 280px;
  max-height: 240px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: transform 0.15s;
}

.msg-bubble img:hover,
.msg-bubble video:hover {
  transform: scale(1.02);
}

.msg-bubble audio {
  max-width: 240px;
  outline: none;
}

.msg-time {
  font-size: 11px;
  color: var(--text-dim);
  padding: 0 4px;
  margin-top: 2px;
}

.msg-row.own .msg-time {
  text-align: right;
  color: var(--text-dim);
}

.msg-divider {
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
  padding: 8px 0;
}

/* ========== 输入区 ========== */
.input-area {
  flex-shrink: 0;
  padding: 12px 20px;
  background: var(--bg-card);
  border-top: 1px solid var(--border);
}

.upload-preview {
  margin-bottom: 8px;
  position: relative;
  display: inline-block;
}

.upload-preview img,
.upload-preview video {
  max-width: 160px;
  max-height: 100px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.btn-clear {
  position: absolute;
  top: -6px;
  right: -6px;
  background: var(--danger);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#msg-input {
  flex: 1;
  resize: none;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.4;
  max-height: 120px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

#msg-input::placeholder {
  color: #9ca3af;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#msg-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.btn-send {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-light));
  color: #fff;
  border: none;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.25);
}

.btn-send:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.35);
}

.btn-send:active {
  transform: scale(0.95);
}

/* ========== 弹窗 ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  min-width: 300px;
  box-shadow: var(--shadow-lg);
}

.modal h3 {
  margin-bottom: 16px;
  font-size: 16px;
  color: var(--text);
}

.modal input {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}

.modal input::placeholder {
  color: #9ca3af;
}

.modal input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.modal-actions button {
  flex: 1;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}

.modal-actions button:hover {
  background: var(--bg-hover);
  border-color: #d1d5db;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-light)) !important;
  border-color: transparent !important;
  color: #fff !important;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.2);
}

.btn-primary:hover {
  background: linear-gradient(135deg, var(--accent), var(--accent-light)) !important;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3) !important;
}

/* ========== 媒体预览弹窗 (保持暗色) ========== */
.media-preview {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.media-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
}

.media-content {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
}

.media-content img,
.media-content video {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}

/* ========== Emoji 选择器 ========== */
.emoji-picker {
  position: absolute;
  bottom: 130px;
  left: 20px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow-lg);
  z-index: 50;
  max-height: 260px;
  overflow-y: auto;
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
}

.emoji-grid button {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  transition: background 0.1s;
}

.emoji-grid button:hover {
  background: var(--bg-hover);
}

/* ========== 响应式 ========== */
@media (max-width: 640px) {
  .topbar {
    padding: 10px 14px;
  }
  .logo {
    font-size: 16px;
  }
  .messages {
    padding: 12px 14px;
  }
  .msg-row {
    max-width: 92%;
  }
  .input-area {
    padding: 10px 14px;
  }
  .msg-bubble img,
  .msg-bubble video {
    max-width: 200px;
    max-height: 180px;
  }
}
