:root {
  --bg: #f5f5f5;
  --card: #fff;
  --text: #333;
  --text-secondary: #999;
  --border: #e8e8e8;
  --input-bg: #f5f5f5;
}
body.dark {
  --bg: #1a1a2e;
  --card: #16213e;
  --text: #e0e0e0;
  --text-secondary: #888;
  --border: #2a2a4e;
  --input-bg: #1e1e3a;
}
body.dark { background: var(--bg); color: var(--text); }
body.dark #page-auth { background: var(--bg); }
body.dark .login-box, body.dark .register-box { background: var(--card); }
body.dark .login-box h1 { color: #7B61FF; }
body.dark .chat-item, body.dark .friend-item { background: var(--card); border-color: var(--border); }
body.dark .chat-item.active, body.dark .friend-item:hover { background: #1e2a4a; }
body.dark .chat-preview, body.dark .chat-time, body.dark #chat-placeholder p { color: var(--text-secondary); }
body.dark #chat-header { background: var(--card); border-color: var(--border); }
#chat-messages { background: #1A0F2E; }
body.dark .msg-bubble { background: var(--card); color: var(--text); }
body.dark .msg-bubble.me { background: #7B61FF; color: #fff; }
body.dark .msg-time { color: var(--text-secondary); }
body.dark #chat-input-area { background: var(--card); border-color: var(--border); }
body.dark #chat-input { background: var(--input-bg); color: var(--text); }
body.dark .topbar { border-color: #7B61FF; }
body.dark .tab-content, body.dark .modal-content { background: var(--card); color: var(--text); }
body.dark #emoji-panel { background: var(--card); border-color: var(--border); }
body.dark .sticker-subtab { color: var(--text-secondary) !important; }
body.dark .sticker-subtab.active { color: #7B61FF !important; }
body.dark .sticker-subtabs { border-color: var(--border); }
body.dark #friends-list .friend-item { border-color: var(--border); }
body.dark .friend-name { color: var(--text); }
body.dark .btn-remark { color: #7B61FF; }
body.dark .friend-profile { background: var(--card); }
/* ====== 全局 ====== */
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: "Microsoft YaHei", "Segoe UI", sans-serif; background: #f0f0f0; color: #333; overflow: hidden; }
.page { display: none; width: 100%; }
#page-main { padding-top: env(safe-area-inset-top); }
#page-auth { padding-top: env(safe-area-inset-top); }
.page.active { display: flex; flex-direction: column; }

/* ====== 登录 ====== */
#page-auth { align-items: center; justify-content: center; background: linear-gradient(135deg, #7B61FF 0%, #6A4FD9 100%); }
.auth-container { background: #fff; border-radius: 16px; padding: 40px; width: 380px; max-width: 90vw; box-shadow: 0 8px 32px rgba(0,0,0,0.15); }
.auth-logo { text-align: center; margin-bottom: 28px; }
.logo-icon { font-size: 48px; margin-bottom: 8px; }
.auth-logo h1 { font-size: 28px; color: #7B61FF; }
.subtitle { font-size: 18px; color: #999; margin-top: 4px; }
.auth-container input { display: block; width: 100%; padding: 12px 16px; margin-bottom: 14px; border: 1px solid #ddd; border-radius: 8px; font-size: 18px; transition: border .2s; }
.auth-container input:focus { outline: none; border-color: #7B61FF; }
.btn-primary { width: 100%; padding: 12px; background: #7B61FF; color: #fff; border: none; border-radius: 8px; font-size: 18px; cursor: pointer; transition: background .2s; }
.btn-primary:hover { background: #06a956; }
.btn-primary:disabled { opacity: .6; cursor: not-allowed; }
.auth-switch { text-align: center; margin-top: 16px; font-size: 17px; color: #999; }
.auth-switch a { color: #7B61FF; text-decoration: none; }
.error-msg { text-align: center; color: #e74c3c; font-size: 17px; margin-top: 12px; min-height: 20px; }

/* ====== 主界面布局 ====== */
.topbar { height: 56px; background: #7B61FF; color: #fff; display: flex; align-items: center; padding: 0 20px; justify-content: space-between; flex-shrink: 0; }
.topbar-title { font-size: 20px; font-weight: 600; }
.topbar-actions { display: flex; gap: 8px; }
.btn-icon { background: none; border: none; color: #fff; font-size: 20px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.btn-icon:hover { background: rgba(255,255,255,0.15); }

.main-body { display: flex; flex: 1; overflow: hidden; }

/* 侧边栏 */
.sidebar { width: 320px; min-width: 320px; background: #fff; border-right: 1px solid #e0e0e0; display: flex; flex-direction: column; }
.sidebar-tabs { display: flex; border-bottom: 1px solid #e0e0e0; }
.tab { flex: 1; padding: 12px; background: none; border: none; font-size: 18px; cursor: pointer; color: #999; transition: all .2s; }
.tab.active { color: #7B61FF; border-bottom: 2px solid #7B61FF; font-weight: 600; }
.tab-content { display: none; flex: 1; overflow-y: auto; }
.tab-content.active { display: block; }

/* 聊天列表项 */
.chat-item { display: flex; padding: 14px 16px; border-bottom: 1px solid #f0f0f0; cursor: pointer; transition: background .15s; }
.chat-item:hover { background: #f5f5f5; }
.chat-item.active { background: #e8f5e9; }
.chat-avatar { width: 57px; height: 57px; border-radius: 6px; background: #7B61FF; color: #fff; display: flex; align-items: center; justify-content: center; font-size: 23px; font-weight: 600; flex-shrink: 0; }
.chat-info { flex: 1; margin-left: 12px; min-width: 0; }
.chat-name { font-size: 17px; font-weight: 500; }
.chat-preview { font-size: 15px; color: #999; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-meta { display: flex; flex-direction: column; align-items: flex-end; margin-left: 8px; flex-shrink: 0; }
.chat-time { font-size: 13px; color: #bbb; }
.chat-badge { background: #e74c3c; color: #fff; font-size: 13px; min-width: 20px; height: 20px; border-radius: 10px; display: flex; align-items: center; justify-content: center; padding: 0 5px; margin-top: 4px; }

/* 通讯录 */
.friend-item { display: flex; align-items: center; padding: 12px 16px; border-bottom: 1px solid #f0f0f0; cursor: pointer; }
.friend-item:hover { background: #f5f5f5; }
.friend-avatar { width: 52px; height: 52px; border-radius: 6px; background: #7B61FF; color: #fff; display: flex; align-items: center; justify-content: center; font-size: 21px; font-weight: 600; flex-shrink: 0; }
.friend-name { margin-left: 12px; font-size: 17px; }
.request-badge { background: #ff9800; color: #fff; font-size: 14px; padding: 2px 10px; border-radius: 12px; cursor: pointer; margin: 8px 12px; display: inline-block; }

/* 聊天区域 */
.chat-area { flex: 1; display: flex; flex-direction: column; background: #f7f7f7; min-height: 0; }
.chat-placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #bbb; }
.placeholder-icon { font-size: 64px; margin-bottom: 12px; }
.chat-header { padding: 14px 20px; background: #f0f0f0; border-bottom: 1px solid #e0e0e0; font-size: 18px; font-weight: 600; flex-shrink: 0; display: flex; align-items: center; justify-content: center; position: relative; }
.chat-header-back { display: none; background: none; border: none; font-size: 22px; cursor: pointer; padding: 4px 8px; border-radius: 6px; color: #333; position: absolute; left: 12px; }
.chat-header-back:hover { background: rgba(0,0,0,0.05); }
#chat-view { min-height: 0; flex: 1; display: flex; flex-direction: column; position: relative; }
.chat-messages { flex: 1; overflow-y: auto; padding: 16px 20px; min-height: 0; }
.scroll-bottom-btn {
  display: none;
  position: absolute;
  bottom: 68px;
  left: 50%;
  transform: translateX(-50%);
  background: #7B61FF;
  color: #fff;
  border: none;
  border-radius: 20px;
  padding: 8px 20px;
  font-size: 17px;
  cursor: pointer;
  z-index: 10;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  transition: opacity .3s;
}
.scroll-bottom-btn:hover { background: #06a956; }
.scroll-bottom-btn.show { display: block; }
.chat-input-area { background: #fff; border-top: 1px solid #e0e0e0; padding: 8px 16px max(12px, env(safe-area-inset-bottom)); flex-shrink: 0; }
/* 上传进度条 — 居中浮在聊天区底部 */
.upload-progress { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 8px 16px; margin: 0 16px 4px; background: #fff; border-radius: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); flex-shrink: 0; width: fit-content; align-self: center; }
.upload-progress-bar { width: 120px; height: 5px; border-radius: 3px; background: #e0e0e0; overflow: hidden; }
#upload-progress-fill { height: 100%; width: 0%; background: #7B61FF; border-radius: 3px; transition: width .2s; }
#upload-progress-text { font-size: 14px; color: #7B61FF; font-weight: 500; white-space: nowrap; }
.chat-tools { display: flex; gap: 4px; margin-bottom: 6px; }
.tool-btn { background: none; border: none; font-size: 22px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.tool-btn:hover { background: #f0f0f0; }
.chat-input-row { display: flex; gap: 8px; }
.chat-input-row textarea { flex: 1; padding: 10px 14px; border: 1px solid #ddd; border-radius: 20px; font-size: 18px; resize: none; outline: none; font-family: inherit; min-height: 40px; max-height: 120px; }
.chat-input-row textarea:focus { border-color: #7B61FF; }
.btn-send { background: #7B61FF; color: #fff; border: none; border-radius: 20px; padding: 10px 20px; font-size: 18px; cursor: pointer; white-space: nowrap; }
.btn-send:hover { background: #06a956; }

/* 聊天气泡（带头像） */
.msg-row { display: flex; margin-bottom: 16px; align-items: flex-start; position: relative; gap: 6px; }
.msg-row.me { flex-direction: row-reverse; }
.msg-row:hover .msg-delete-btn { opacity: 1; }
.msg-avatar { width: 47px; height: 47px; border-radius: 4px; display: flex; align-items: center; justify-content: center; font-size: 18px; font-weight: 600; flex-shrink: 0; }
.msg-avatar.other { background: #7B61FF; color: #fff; }
.msg-avatar.me { background: #B084CC; color: #333; }
.msg-content { display: flex; flex-direction: column; max-width: 60%; }
.msg-row.me .msg-content { align-items: flex-end; }
.msg-row.other .msg-content { align-items: flex-start; }
.msg-bubble { padding: 10px 14px; border-radius: 16px; font-size: 17px; line-height: 1.4; word-break: break-word; }
.msg-row .msg-bubble { background: #E8D5F0; border-radius: 16px; }
.msg-row.me .msg-bubble { background: #B084CC; border-bottom-right-radius: 4px; }
.msg-row.other .msg-bubble { background: #fff; border-bottom-left-radius: 4px; }
.msg-meta { display: flex; align-items: center; gap: 4px; margin-top: 2px; }
.msg-row.me .msg-meta { justify-content: flex-end; }
.msg-row.other .msg-meta { justify-content: flex-start; }
.msg-time { font-size: 13px; color: #bbb; }
.msg-read { font-size: 13px; color: #7B61FF; }
.msg-unread { font-size: 13px; color: #bbb; }
.msg-img { max-width: 100%; max-height: 300px; border-radius: 8px; cursor: pointer; display: block; }
.load-more-indicator { text-align: center; padding: 12px; font-size: 17px; color: #999; }
.msg-delete-btn {
  opacity: 0;
  transition: opacity .2s;
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  color: #999;
  flex-shrink: 0;
}
.msg-delete-btn:hover { color: #e74c3c; background: rgba(0,0,0,0.05); }

/* 表情/贴纸面板 */
.emoji-panel {
  height: 350px;
  border-top: 1px solid #eee;
  background: #f8f8f8;
  margin-top: 6px;
  display: flex;
  flex-direction: column;
}
.emoji-tabs {
  display: flex;
  border-bottom: 1px solid #e0e0e0;
  flex-shrink: 0;
}
.emoji-tab {
  flex: 1;
  padding: 8px;
  background: none;
  border: none;
  font-size: 15px;
  cursor: pointer;
  color: #999;
  transition: all .2s;
}
.emoji-tab.active {
  color: #7B61FF;
  border-bottom: 2px solid #7B61FF;
  font-weight: 600;
}
.emoji-content {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 8px;
  align-content: flex-start;
}
.emoji-content span {
  font-size: 24px;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.emoji-content span:hover { background: #e0e0e0; }

/* 贴纸消息气泡 */
.msg-bubble-emoji {
  background: transparent !important;
  padding: 0 !important;
}

/* 弹窗 */
.modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); align-items: center; justify-content: center; z-index: 100; }
.modal-content { background: #fff; border-radius: 12px; padding: 24px; width: 420px; max-width: 90vw; max-height: 80vh; overflow-y: auto; }
.modal-close { float: right; font-size: 24px; cursor: pointer; color: #999; }
.modal-content h3 { margin-bottom: 16px; }
#search-input { width: 100%; padding: 10px 14px; border: 1px solid #ddd; border-radius: 8px; font-size: 18px; margin-bottom: 12px; }
.search-results { max-height: 300px; overflow-y: auto; }
.search-item { display: flex; align-items: center; padding: 10px; border-bottom: 1px solid #f0f0f0; }
.search-item .username { flex: 1; font-size: 17px; margin-left: 10px; }
.search-item button { background: #7B61FF; color: #fff; border: none; border-radius: 16px; padding: 6px 16px; font-size: 17px; cursor: pointer; }

/* 滚动条 */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #aaa; }

/* ====== 移动端底部导航栏 ====== */
@media (max-width: 768px) {
  .topbar {
    height: 48px; padding: 0 16px max(0px, env(safe-area-inset-bottom));
    order: 1;
    flex-shrink: 0;
  }
  .topbar-hidden { display: none !important; }
  .main-body { flex: 1; order: 0; }
  .sidebar { width: 100%; min-width: unset; }
  .sidebar.hide-mobile { display: none; }
  .chat-area { display: none; flex: 1; width: 100%; }
  .chat-area.show-mobile { display: flex; }
  .main-body { position: relative; }
  .chat-avatar { width: 52px; height: 52px; font-size: 21px; }
  .auth-container { padding: 28px 24px; }
  .chat-header { padding: 12px 12px; }
  .chat-header-back { display: inline-flex !important; font-size: 22px; padding: 4px 8px; }
  .chat-messages { padding: 12px; }
  .scroll-bottom-btn { bottom: 60px; font-size: 18px; padding: 6px 16px; }
  .chat-input-area { padding: 6px 8px max(10px, env(safe-area-inset-bottom)); }
  .msg-avatar { width: 39px; height: 39px; font-size: 18px; }
  .msg-content { max-width: 72%; }
  .msg-bubble { padding: 8px 12px; font-size: 18px; }
  .emoji-content span { width: 32px; height: 32px; font-size: 20px; }
  .btn-send { padding: 8px 14px; font-size: 17px; }
  .friend-item { padding: 10px 12px; }
}

@media (max-width: 480px) {
  .topbar { height: 42px; padding: 0 12px max(0px, env(safe-area-inset-bottom)); }
  .topbar-title { font-size: 18px; }
  .btn-icon { font-size: 18px; padding: 2px 6px; }
  .auth-container { padding: 24px 20px; border-radius: 12px; }
  .auth-container input { padding: 10px 14px; font-size: 18px; }
  .btn-primary { padding: 10px; font-size: 17px; }
  .chat-avatar { width: 47px; height: 47px; font-size: 18px; }
  .msg-avatar { width: 37px; height: 37px; font-size: 18px; }
  .msg-bubble { padding: 7px 10px; font-size: 18px; }
  .chat-input-row textarea { font-size: 18px; padding: 8px 12px; }
  .btn-send { padding: 8px 12px; font-size: 17px; }
  .chat-header { padding: 10px 10px; }
  .chat-header-back { font-size: 20px !important; padding: 4px 6px !important; }
  .emoji-panel { height: 250px; }
  .modal-content { width: 95vw; padding: 16px; }
}

/* iOS 键盘适配 */
.chat-area { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }
.chat-view { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
#chat-input-area { position: sticky; bottom: 0; z-index: 10; }

/* 移动端输入框固定底部 */
#chat-view { flex: 1; overflow-y: auto; }
#chat-input-area { position: sticky; bottom: 0; background: var(--card, #fff); z-index: 10; }
.msg-no-bubble { background: transparent !important; padding: 0 !important; }
