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

:root {
  --bg-base:       #0f1117;
  --bg-sidebar:    #161b27;
  --bg-card:       #1e2535;
  --bg-hover:      #252d40;
  --bg-active:     #2a3450;
  --bg-input:      #1e2535;
  --accent:        #4ade80;
  --accent-dim:    #22c55e55;
  --accent-danger: #f87171;
  --msg-me:        #1a4731;
  --msg-me-border: #22c55e40;
  --msg-other:     #1e2535;
  --text-primary:  #e8eaf0;
  --text-secondary:#8892a4;
  --text-muted:    #4a5568;
  --border:        #ffffff0d;
  --border-strong: #ffffff18;
  --shadow:        0 8px 32px #00000066;
  --shadow-lg:     0 16px 48px #00000088;
  --radius-sm:     8px;
  --radius-md:     14px;
  --radius-lg:     20px;
  --sidebar-w:     280px;
  --t:             0.18s ease;
}

html, body { height: 100%; overflow: hidden; }
body {
  font-family: 'Noto Sans SC', 'DM Sans', sans-serif;
  background: var(--bg-base);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ─── Loading overlay ──────────────────────────────────────── */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-base);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 9999;
}
.loading-spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--text-secondary); font-size: 14px; }

/* ─── Layout ────────────────────────────────────────────────── */
.app { display: flex; height: 100vh; width: 100vw; overflow: hidden; }

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

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.me-avatar-wrap { position: relative; flex-shrink: 0; }
.me-avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 2px solid var(--accent-dim);
  object-fit: cover;
  background: var(--bg-card);
}
.me-name {
  flex: 1;
  font-weight: 600;
  font-size: 14.5px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-actions { display: flex; gap: 4px; flex-shrink: 0; }

/* Status dot */
.status-dot {
  position: absolute;
  bottom: 1px; right: 1px;
  width: 10px; height: 10px;
  border-radius: 50%;
  border: 2px solid var(--bg-sidebar);
}
.status-dot.online  { background: var(--accent); }
.status-dot.offline { background: var(--text-muted); }

/* Icon button */
.icon-btn {
  background: none; border: none;
  color: var(--text-secondary);
  cursor: pointer;
  width: 32px; height: 32px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  transition: background var(--t), color var(--t);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

/* Search */
.search-wrap { position: relative; padding: 12px 14px 10px; flex-shrink: 0; }
.search-icon {
  position: absolute; left: 26px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted); font-size: 12px; pointer-events: none;
}
.search-input {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  padding: 8px 12px 8px 34px;
  font-size: 13px; font-family: inherit;
  outline: none;
  transition: border-color var(--t);
}
.search-input::placeholder { color: var(--text-muted); }
.search-input:focus { border-color: var(--accent-dim); }

/* Contact list */
.contact-list { flex: 1; overflow-y: auto; padding-bottom: 12px; }
.contact-list::-webkit-scrollbar { width: 3px; }
.contact-list::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 9px; }

/* Group header */
.group-header {
  display: flex; align-items: center; gap: 7px;
  padding: 10px 16px 5px;
  font-size: 11.5px; font-weight: 600;
  letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--text-muted);
  cursor: pointer; user-select: none;
  transition: color var(--t);
}
.group-header:hover { color: var(--text-secondary); }

.group-arrow { font-size: 10px; transition: transform 0.2s ease; }
.group-arrow.collapsed { transform: rotate(-90deg); }

.group-count {
  margin-left: auto;
  background: var(--bg-card);
  border-radius: 99px;
  padding: 1px 7px;
  font-size: 10.5px;
}

.group-body { overflow: hidden; transition: max-height 0.25s ease; max-height: 9999px; }
.group-body.collapsed { max-height: 0; }

/* Contact item */
.contact-item {
  display: flex; align-items: center; gap: 11px;
  padding: 8px 14px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  margin: 1px 6px;
  position: relative;
  transition: background var(--t);
}
.contact-item:hover  { background: var(--bg-hover); }
.contact-item.active { background: var(--bg-active); }
.contact-item.active .contact-name { color: var(--accent); }

.contact-avatar-wrap { position: relative; flex-shrink: 0; }
.contact-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg-card);
}
.contact-info { flex: 1; overflow: hidden; }
.contact-name {
  font-size: 13.5px; font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.contact-preview {
  font-size: 12px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.contact-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.contact-time { font-size: 11px; color: var(--text-muted); }
.unread-badge {
  background: var(--accent); color: #0f1117;
  font-size: 10px; font-weight: 700;
  border-radius: 99px; min-width: 17px; height: 17px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 4px;
}

/* ─── Tooltip ────────────────────────────────────────────────── */
.tooltip {
  position: fixed; z-index: 9990;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  display: flex; align-items: center; gap: 12px;
  pointer-events: none;
  opacity: 0; transform: translateX(6px);
  transition: opacity 0.15s ease, transform 0.15s ease;
  min-width: 170px;
}
.tooltip.show { opacity: 1; transform: translateX(0); }
.tooltip-avatar-wrap { position: relative; flex-shrink: 0; }
.tooltip-avatar {
  width: 44px; height: 44px;
  border-radius: 50%; object-fit: cover;
  border: 2px solid var(--border-strong);
  background: var(--bg-base);
}
.tooltip-info { display: flex; flex-direction: column; gap: 4px; }
.tooltip-name { font-size: 13.5px; font-weight: 600; color: var(--text-primary); margin-bottom: 2px; }
.tooltip-row  { display: flex; align-items: baseline; gap: 5px; font-size: 11.5px; }
.tooltip-label { color: var(--text-muted); flex-shrink: 0; }
.tooltip-row span:last-child { color: var(--text-secondary); }

/* ─── Plus menu ─────────────────────────────────────────────── */
.plus-menu {
  position: fixed; z-index: 9990;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  padding: 6px; min-width: 155px;
  opacity: 0; pointer-events: none;
  transform: scale(0.95) translateY(-4px);
  transform-origin: top right;
  transition: opacity 0.15s ease, transform 0.15s ease;
}
.plus-menu.show { opacity: 1; pointer-events: all; transform: scale(1) translateY(0); }
.plus-menu-item {
  padding: 9px 12px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 13.5px; color: var(--text-secondary);
  display: flex; align-items: center; gap: 9px;
  transition: background var(--t), color var(--t);
}
.plus-menu-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.plus-menu-item i { width: 16px; text-align: center; color: var(--accent); }

/* ─── Modal ──────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 9900;
  background: #00000077;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease;
  backdrop-filter: blur(2px);
}
.modal-backdrop.show { opacity: 1; pointer-events: all; }

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 380px; max-width: calc(100vw - 40px);
  transform: scale(0.96) translateY(8px);
  transition: transform 0.2s ease;
}
.modal-backdrop.show .modal { transform: scale(1) translateY(0); }

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}
.modal-title { font-size: 15px; font-weight: 600; color: var(--text-primary); display: flex; align-items: center; gap: 8px; }
.modal-title i { color: var(--accent); }
.modal-close {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 16px;
  width: 28px; height: 28px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  transition: background var(--t), color var(--t);
}
.modal-close:hover { background: var(--bg-hover); color: var(--text-primary); }

.modal-body { padding: 18px 20px; }
.modal-label { display: block; font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 8px; }
.modal-input {
  width: 100%;
  background: var(--bg-base);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 10px 12px;
  font-size: 14px; font-family: inherit;
  outline: none;
  transition: border-color var(--t);
}
.modal-input::placeholder { color: var(--text-muted); }
.modal-input:focus { border-color: var(--accent-dim); }
.modal-hint { font-size: 12px; color: var(--text-muted); margin-top: 8px; }

.modal-copyrow {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-base);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.modal-copyval { flex: 1; font-size: 15px; font-weight: 700; letter-spacing: 0.12em; color: var(--accent); }
.modal-copybtn {
  background: var(--bg-card); border: 1px solid var(--border-strong);
  color: var(--text-secondary); cursor: pointer;
  padding: 4px 10px; border-radius: var(--radius-sm);
  font-size: 12px; font-family: inherit;
  display: flex; align-items: center; gap: 5px;
  transition: background var(--t), color var(--t);
}
.modal-copybtn:hover { background: var(--bg-hover); color: var(--text-primary); }

.modal-footer {
  display: flex; gap: 8px; justify-content: flex-end;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border);
}
.modal-btn {
  padding: 9px 18px; border-radius: var(--radius-sm);
  font-size: 13.5px; font-family: inherit;
  cursor: pointer; border: none;
  transition: background var(--t), opacity var(--t);
}
.modal-btn.primary   { background: var(--accent); color: #0f1117; font-weight: 600; }
.modal-btn.primary:hover { background: #22c55e; }
.modal-btn.secondary { background: var(--bg-hover); color: var(--text-secondary); }
.modal-btn.secondary:hover { background: var(--bg-active); color: var(--text-primary); }
.modal-btn.danger    { background: #3a1a1a; color: var(--accent-danger); margin-right: auto; }
.modal-btn.danger:hover { background: #4a2020; }

/* ─── Main chat ─────────────────────────────────────────────── */
.chat-main { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: var(--bg-base); }

.empty-state {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px;
  opacity: 0.35;
}
.empty-logo { width: 100px; height: 100px; object-fit: contain; }
.empty-hint { font-size: 15px; color: var(--text-secondary); }

.chat-view { display: flex; flex-direction: column; height: 100%; }

/* Chat header */
.chat-header {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
  flex-shrink: 0;
}
.chat-header-avatar-wrap { position: relative; flex-shrink: 0; }
.chat-header-avatar {
  width: 40px; height: 40px;
  border-radius: 50%; object-fit: cover;
  border: 2px solid var(--border-strong);
  background: var(--bg-card);
}
.chat-header-info { flex: 1; overflow: hidden; }
.chat-header-name {
  display: block; font-size: 15px; font-weight: 600; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-header-sub { display: block; font-size: 12px; color: var(--text-muted); }
.chat-header-actions { display: flex; gap: 4px; flex-shrink: 0; }

/* Load more */
.load-more-wrap {
  text-align: center;
  padding: 8px 0 0;
  flex-shrink: 0;
}
.load-more-btn {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: 99px;
  color: var(--text-secondary);
  padding: 5px 16px;
  font-size: 12px; font-family: inherit;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  transition: background var(--t), color var(--t);
}
.load-more-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

/* Messages area */
.messages-area {
  flex: 1; overflow-y: auto;
  padding: 16px 24px; display: flex; flex-direction: column; gap: 3px;
}
.messages-area::-webkit-scrollbar { width: 3px; }
.messages-area::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 9px; }

/* Message bubbles */
.msg-row {
  display: flex; align-items: flex-end; gap: 8px;
  max-width: 72%;
  animation: msgIn 0.18s ease;
}
@keyframes msgIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.msg-row.me    { align-self: flex-end;   flex-direction: row-reverse; }
.msg-row.other { align-self: flex-start; }
.msg-row.me .msg-avatar { display: none; }

.msg-avatar {
  width: 28px; height: 28px;
  border-radius: 50%; object-fit: cover; flex-shrink: 0;
  background: var(--bg-card);
}

.msg-sender-name {
  font-size: 11px; color: #4ade8088; font-weight: 600;
  margin-bottom: 3px; padding-left: 2px;
}

.msg-bubble {
  padding: 9px 14px;
  border-radius: var(--radius-lg);
  font-size: 14px; line-height: 1.55;
  word-break: break-word; position: relative; max-width: 100%;
}
.msg-row.me .msg-bubble {
  background: var(--msg-me);
  border: 1px solid var(--msg-me-border);
  border-bottom-right-radius: 5px;
  color: #d4fce5;
}
.msg-row.other .msg-bubble {
  background: var(--msg-other);
  border: 1px solid var(--border-strong);
  border-bottom-left-radius: 5px;
  color: var(--text-primary);
}
.msg-bubble a { color: #60a5fa; text-decoration: none; }
.msg-bubble a:hover { text-decoration: underline; }

.msg-time { font-size: 10.5px; color: var(--text-muted); margin-top: 3px; padding: 0 4px; display: block; }
.msg-row.me    .msg-time { text-align: right; }
.msg-row.other .msg-time { text-align: left; }

/* Date divider */
.date-divider {
  display: flex; align-items: center; gap: 12px;
  color: var(--text-muted); font-size: 11.5px;
  padding: 8px 0; text-transform: uppercase; letter-spacing: 0.06em;
}
.date-divider::before, .date-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}

/* Input area */
.input-area {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  background: var(--bg-sidebar);
  flex-shrink: 0;
}

.input-box {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  font-size: 14px; font-family: inherit; color: var(--text-primary);
  outline: none;
  max-height: 120px; overflow-y: auto; line-height: 1.5;
  transition: border-color var(--t);
}
.input-box:focus { border-color: var(--accent-dim); }
.input-box:empty::before { content: attr(data-placeholder); color: var(--text-muted); pointer-events: none; }

.send-btn {
  background: var(--accent); color: #0f1117;
  border: none; border-radius: var(--radius-md);
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 14px; flex-shrink: 0;
  transition: background var(--t), transform var(--t);
}
.send-btn:hover  { background: #22c55e; transform: scale(1.05); }
.send-btn:active { transform: scale(0.95); }

/* ═══════════════════════════════════════════════════════════════
   移动端响应式（≤ 640px）
   策略：侧边栏和聊天区各占 100vw，水平滑动切换
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 640px) {

  /* 整个 app 水平排列但不换行，宽度 200vw，通过 translateX 切换视图 */
  .app {
    width: 200vw;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }
  /* 默认显示侧边栏 */
  .app.show-chat {
    transform: translateX(-50%);
  }

  .sidebar {
    width: 100vw;
    min-width: 100vw;
    border-right: none;
  }

  .chat-main {
    width: 100vw;
    min-width: 100vw;
    flex-shrink: 0;
  }

  /* 聊天头部加返回按钮 */
  .back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    font-size: 16px;
    flex-shrink: 0;
    transition: background var(--t), color var(--t);
  }
  .back-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

  /* 聊天头部稍微紧凑一点 */
  .chat-header {
    padding: 10px 14px;
    gap: 10px;
  }

  /* 消息区域左右padding缩小 */
  .messages-area {
    padding: 12px 14px;
  }

  /* 气泡最大宽度放宽 */
  .msg-row {
    max-width: 88%;
  }

  /* 输入区域 */
  .input-area {
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }

  /* tooltip 在移动端禁用（没有 hover） */
  .tooltip { display: none !important; }

  /* plus menu 从底部弹出效果更好，但保持右上角位置也可接受 */
}

/* 桌面端隐藏返回按钮 */
@media (min-width: 641px) {
  .back-btn { display: none; }
}