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

:root {
  --primary: #6366F1;
  --primary-hover: #4F46E5;
  --bg: #ffffff;
  --bg-surface: #F8FAFC;
  --bg-msg-self: #EEF2FF;
  --bg-msg-other: #F1F5F9;
  --bg-system: #F1F5F9;
  --text: #1E293B;
  --text-secondary: #64748B;
  --text-muted: #94A3B8;
  --border: #E2E8F0;
  --radius: 12px;
  --radius-sm: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  font-size: 13px;
  line-height: 1.5;
}

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

/* ── Join Screen ───────────────────────────── */
#join-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #EEF2FF 0%, #E0E7FF 100%);
}

.join-card {
  background: var(--bg);
  border-radius: 20px;
  padding: 48px 40px;
  width: 400px;
  box-shadow: 0 20px 60px rgba(79,70,229,.12);
}

.logo { text-align: center; margin-bottom: 32px; }
.logo h1 { font-size: 24px; font-weight: 700; margin-top: 12px; color: var(--primary); }
.logo p { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }

.input-group { margin-bottom: 16px; }
.input-group label { display: block; font-size: 13px; font-weight: 600; color: var(--text-secondary); margin-bottom: 6px; }
.input-group input { width: 100%; padding: 10px 14px; border: 1.5px solid var(--border); border-radius: var(--radius-sm); font-size: 14px; outline: none; background: #F3F4F6; transition: border-color .15s; }
.input-group input:focus { border-color: var(--primary); }

.room-row { display: flex; gap: 8px; }
.room-row input { flex: 1; }

.btn-primary, .btn-secondary {
  border: none; border-radius: var(--radius-sm); font-size: 14px; font-weight: 600; cursor: pointer; transition: all .15s;
}
.btn-primary { width: 100%; padding: 12px; background: var(--primary); color: #fff; margin-top: 8px; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { padding: 10px 14px; background: #F3F4F6; color: var(--text); font-size: 20px; line-height: 1; }
.btn-secondary:hover { background: var(--border); }

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

.chat-layout {
  display: flex;
  flex: 1;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ────────────────────────────────── */
.sidebar {
  width: 160px;
  flex-shrink: 0;
  background: var(--bg-surface);
  border-right: 0.5px solid var(--border);
  display: flex;
  flex-direction: column;
  border-radius: var(--radius) 0 0 var(--radius);
  transition: width .25s ease, padding .25s ease;
  overflow: hidden;
}
.sidebar.collapsed {
  width: 0;
  border-right: none;
}

.sidebar-header {
  padding: 14px 12px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  border-bottom: 0.5px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  white-space: nowrap;
}

.sidebar-count {
  background: var(--primary);
  color: #fff;
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
}

.sidebar-toggle {
  position: absolute;
  left: 160px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 40px;
  border: 0.5px solid var(--border);
  border-left: none;
  background: var(--bg);
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
  z-index: 10;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: left .25s ease;
}
.sidebar.collapsed + .sidebar-toggle { left: 0; }
.sidebar-toggle:hover { background: var(--bg-surface); }

.member-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

.member-list::-webkit-scrollbar { width: 4px; }
.member-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.member-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  font-size: 12px;
  color: var(--text);
  transition: background .1s;
  white-space: nowrap;
}
.member-item:hover { background: #EEF2FF; }

.member-avatar {
  width: 24px; height: 24px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 9px; font-weight: 500; color: #fff;
  flex-shrink: 0;
}

/* ── Chat Area ──────────────────────────────── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-radius: 0 var(--radius) var(--radius) 0;
  border: 0.5px solid var(--border);
  border-left: none;
}

/* ── Header ─────────────────────────────────── */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  background: var(--bg-surface);
  border-bottom: 0.5px solid var(--border);
  flex-shrink: 0;
}

.header-left { display: flex; align-items: center; gap: 4px; }
.header-right { display: flex; align-items: center; gap: 8px; }

.room-label { font-size: 15px; font-weight: 500; color: var(--text); }
.room-label strong { font-weight: 500; }

.btn-copy {
  background: var(--bg);
  border: 0.5px solid var(--border);
  border-radius: 6px;
  width: 24px; height: 24px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  padding: 0; margin-left: 4px;
  transition: background .15s;
}
.btn-copy:hover { background: #EEF2FF; }

.btn-leave {
  border: 0.5px solid #FECACA;
  background: #FEF2F2;
  color: #EF4444;
  padding: 5px 14px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s;
}
.btn-leave:hover { background: #FEE2E2; }

.status-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #10B981;
  flex-shrink: 0;
}
.status-dot.disconnected { background: #EF4444; }

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

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

.empty-hint {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  flex: 1; color: var(--text-muted); font-size: 13px; gap: 12px; opacity: 0.6;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 70%;
  animation: fadeIn .2s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.msg-self { align-self: flex-end; }
.msg-other { align-self: flex-start; }
.msg-system { align-self: center; max-width: 85%; }

/* Message header: avatar + name + time */
.msg-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 2px;
}
.msg-other .msg-header { padding-left: 2px; }
.msg-self .msg-header { justify-content: flex-end; padding-right: 2px; }

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

.msg-sender-name {
  font-size: 11px; font-weight: 500; color: var(--text);
}
.msg-time {
  font-size: 10px; color: var(--text-muted);
}
.msg-self .msg-time { order: -1; margin-right: 0; }
.msg-other .msg-time { margin-left: 4px; }

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

.btn-copy-text {
  position: absolute;
  right: 4px;
  bottom: 2px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 11px;
  opacity: 0;
  padding: 2px 4px;
  border-radius: 4px;
  transition: opacity .15s;
}
.msg-bubble:hover .btn-copy-text { opacity: 0.6; }
.btn-copy-text:hover { opacity: 1 !important; background: rgba(0,0,0,0.06); }
.msg-self .msg-bubble { background: var(--bg-msg-self); color: #4338CA; }
.msg-other .msg-bubble { background: var(--bg-msg-other); color: var(--text); }
.msg-system .msg-bubble {
  background: var(--bg-system); font-size: 11px; color: var(--text-muted);
  border-radius: 13px; text-align: center; padding: 5px 14px;
}

/* ── File Card ──────────────────────────────── */
.file-card {
  display: flex; align-items: center; gap: 10px;
  padding: 10px; background: var(--bg);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  margin-top: 4px;
}
.file-icon { font-size: 24px; flex-shrink: 0; }
.file-info { flex: 1; min-width: 0; }
.file-name { font-size: 12px; font-weight: 500; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-size { font-size: 10px; color: var(--text-muted); }

.file-progress { height: 3px; background: var(--border); border-radius: 2px; margin-top: 5px; overflow: hidden; }
.file-progress-bar { height: 100%; background: var(--primary); border-radius: 2px; transition: width .15s; }

.btn-dl {
  background: none; border: none; cursor: pointer; padding: 0;
  flex-shrink: 0; width: 24px; height: 24px;
  opacity: 0.6; transition: opacity .15s;
}
.btn-dl:hover { opacity: 1; }
.btn-dl svg { width: 24px; height: 24px; }

/* ── Image / Video ──────────────────────────── */
.image-actions {
  display: flex; align-items: center; justify-content: space-between;
  padding: 5px 8px; gap: 6px;
}

.msg-image {
  display: block; width: 100%; max-width: 200px; max-height: 200px;
  object-fit: cover; cursor: pointer; border-radius: 6px;
}

/* ── Typing ─────────────────────────────────── */
.typing-indicator {
  padding: 4px 16px 6px; font-size: 11px; color: var(--text-muted);
  display: flex; align-items: center; gap: 4px; flex-shrink: 0;
}
.typing-indicator.hidden { display: none; }
.typing-indicator span {
  width: 5px; height: 5px; background: var(--text-muted);
  border-radius: 50%; animation: bounce 1.4s infinite ease-in-out;
}
.typing-indicator span:nth-child(1) { animation-delay: 0s; }
.typing-indicator span:nth-child(2) { animation-delay: .16s; }
.typing-indicator span:nth-child(3) { animation-delay: .32s; }
@keyframes bounce { 0%, 80%, 100% { transform: scale(0); } 40% { transform: scale(1); } }

/* ── Input Area ─────────────────────────────── */
.input-area {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px;
  background: var(--bg-surface);
  border-top: 0.5px solid var(--border);
  flex-shrink: 0;
}

.btn-attach {
  width: 34px; height: 34px;
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background .15s;
}
.btn-attach:hover { background: #EEF2FF; }

#msg-input {
  flex: 1;
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 100px;
  background: var(--bg);
  transition: border-color .15s;
}
#msg-input:focus { border-color: var(--primary); }

.btn-send {
  padding: 8px 20px;
  border: none; border-radius: var(--radius-sm);
  background: var(--primary);
  color: #fff;
  font-size: 13px; font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
  font-family: inherit;
  transition: background .15s;
}
.btn-send:hover { background: var(--primary-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-voice {
  width: 34px; height: 34px;
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: all .15s;
}
.btn-voice:hover { background: #EEF2FF; }
.btn-voice.recording {
  background: #FEF2F2;
  border-color: #FECACA;
  animation: pulse-record 0.8s infinite;
}
.btn-voice.recording svg rect { fill: #EF4444; stroke: #EF4444; }
.btn-voice.recording svg path { stroke: #EF4444; }
@keyframes pulse-record { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }

.voice-msg { display: flex; align-items: center; gap: 8px; padding: 4px 8px; }
.voice-msg audio { height: 32px; width: 180px; }
.voice-duration { font-size: 11px; color: var(--text-muted); }

.hidden { display: none !important; }

/* ── Drop overlay ───────────────────────────── */
.drop-overlay {
  position: fixed; inset: 0;
  background: rgba(99,102,241,.06);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.drop-hint { text-align: center; color: var(--primary); }
.drop-hint p { font-size: 18px; font-weight: 600; margin-top: 12px; }

/* ── Lightbox ───────────────────────────────── */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.85);
  backdrop-filter: blur(8px); z-index: 999;
  display: none; align-items: center; justify-content: center; cursor: pointer;
}
.lightbox.active { display: flex; }
.lightbox img { max-width: 90vw; max-height: 90vh; object-fit: contain; border-radius: 8px; cursor: default; }
.lightbox-close { position: absolute; top: 20px; right: 28px; font-size: 36px; color: #fff; cursor: pointer; opacity: 0.7; }
.lightbox-close:hover { opacity: 1; }
