:root {
  --bg: #0f1419;
  --panel: #1a2230;
  --panel2: #232e40;
  --accent: #4f8cff;
  --accent2: #36c98d;
  --text: #e6edf3;
  --muted: #8b98a9;
  --danger: #ff6b6b;
  --border: #2a3548;
}
/* 浅色主题（对眼睛友好） */
[data-theme="light"] {
  --bg: #eef1f6;
  --panel: #ffffff;
  --panel2: #e6ebf2;
  --accent: #2f6fed;
  --accent2: #18a673;
  --text: #1b2330;
  --muted: #6a7686;
  --danger: #d84545;
  --border: #d4dbe6;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text);
  display: flex; align-items: center; justify-content: center;
}
#app { width: 100%; max-width: 760px; height: 100vh; max-height: 100vh; display: flex; flex-direction: column; }
.screen { display: none; flex: 1; min-height: 0; flex-direction: column; }
.screen.active { display: flex; }

/* 首页 */
#home { overflow-y: auto; }
.brand { text-align: center; padding: 28px 16px 8px; }
.brand h1 { margin: 0; font-size: 34px; letter-spacing: 1px;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text; background-clip: text; color: transparent; }
.brand .sub { color: var(--muted); font-size: 13px; margin-top: 6px; }
.tabs { display: flex; gap: 8px; padding: 12px 16px; }
.tab { flex: 1; padding: 10px; background: var(--panel); border: 1px solid var(--border); color: var(--muted);
  border-radius: 10px; cursor: pointer; font-size: 14px; }
.tab.active { color: var(--text); border-color: var(--accent); background: var(--panel2); }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 18px; margin: 0 16px 16px; display: flex; flex-direction: column; gap: 12px; }
.card.hidden, .hidden { display: none !important; }
.recent { padding: 0 16px 8px; display: flex; flex-direction: column; gap: 6px; }
.recent-title { font-size: 12px; color: var(--muted); }
.recent-item { text-align: left; background: var(--panel); border: 1px solid var(--border); color: var(--text); padding: 9px 12px; border-radius: 10px; font-size: 13px; }
.recent-item:hover { border-color: var(--accent); }
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
input[type=text] { background: var(--bg); border: 1px solid var(--border); color: var(--text); padding: 11px 12px; border-radius: 10px; font-size: 14px; outline: none; }
input[type=text]:focus { border-color: var(--accent); }
button { font-family: inherit; cursor: pointer; border: none; border-radius: 10px; font-size: 14px; }
button.primary { background: linear-gradient(90deg, var(--accent), #6aa0ff); color: #fff; padding: 12px; font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; color: var(--accent); border: 1px solid var(--border); padding: 6px 10px; }
button.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); padding: 12px; font-weight: 600; }
button.danger:hover { background: rgba(224, 108, 117, 0.12); }
.hint { color: var(--muted); font-size: 12px; margin: 0; line-height: 1.5; }
.join-status { font-size: 13px; color: var(--muted); min-height: 18px; }
.join-status .err { color: var(--danger); }
.join-status .ok { color: var(--accent2); }
.countdown { color: var(--danger); font-weight: 600; }

.site-record {
  flex: 0 0 auto;
  padding: 10px 16px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  text-align: center;
}
.site-record a { color: inherit; text-decoration: none; }
.site-record a:hover,
.site-record a:focus-visible { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
.home-record { margin-top: auto; padding-bottom: 18px; }
.chat-record { padding-block: 5px; border-top: 1px solid var(--border); background: var(--panel); }

/* 聊天 */
.chat-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.h-left strong { font-size: 16px; }
.role-tag { font-size: 11px; color: var(--accent2); border: 1px solid var(--accent2); border-radius: 6px; padding: 1px 6px; margin-left: 8px; }
.conn { font-size: 12px; color: var(--muted); margin-right: 8px; }
.conn.on { color: var(--accent2); }

.owner-panel { display: flex; flex-wrap: wrap; gap: 10px; padding: 10px 16px; background: var(--panel2); border-bottom: 1px solid var(--border); }
.op-block { min-width: 200px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel); }
.op-head { font-size: 13px; margin: 0; color: var(--muted); cursor: pointer; user-select: none;
  display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 9px 11px; }
.op-head .chev { transition: transform .15s ease; font-style: normal; font-size: 11px; }
.op-block.collapsed .chev { transform: rotate(-90deg); }
.op-body { padding: 0 11px 10px; }
.op-block.collapsed .op-body { display: none; }
.member-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.member-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; font-size: 13px; }
.member-list .m-actions { display: flex; gap: 4px; }
.member-list .m-actions button { padding: 4px 8px; font-size: 12px; border-radius: 6px; }
.btn-ok { background: var(--accent2); color: #06281c; }
.btn-no { background: var(--danger); color: #2a0c0c; }
.btn-rm { background: var(--panel2); color: var(--muted); border: 1px solid var(--border); }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 72%; padding: 9px 12px; border-radius: 12px; font-size: 14px; line-height: 1.45; word-break: break-word; position: relative; }
.msg.other { padding-left: 44px; }
.msg.me { padding-right: 44px; }
.msg-avatar { position: absolute; top: 2px; width: 32px; height: 32px; border-radius: 50%; object-fit: cover; background: var(--panel2); border: 1px solid var(--border); display: flex; align-items: center; justify-content: center; font-size: 14px; color: var(--muted); }
.msg.other .msg-avatar { left: 4px; }
.msg.me .msg-avatar { right: 4px; }
.msg.me { align-self: flex-end; background: linear-gradient(90deg, var(--accent), #6aa0ff); color: #fff; border-bottom-right-radius: 3px; }
.msg.other { align-self: flex-start; background: var(--panel2); border-bottom-left-radius: 3px; }
.msg .who { font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.msg.me .who { color: rgba(255,255,255,0.8); }
/* 文本消息：保留用户写入/粘贴的原格式（换行、连续空格），长单词换行避免溢出 */
.msg-text { white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; }
/* 精确时间：默认隐藏，点击该条消息时显示 */
.msg .msg-time { display: none; font-size: 10px; color: var(--muted); margin-top: 4px; text-align: right; user-select: none; }
.msg.me .msg-time { color: rgba(255,255,255,0.75); }
.msg.show-time .msg-time { display: block; }
.msg img { max-width: 100%; border-radius: 8px; display: block; }
.msg audio { width: 220px; max-width: 100%; }
.sys { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 3px 10px; border-radius: 10px; }

.emoji-panel { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 16px; background: var(--panel); border-top: 1px solid var(--border); max-height: 160px; overflow-y: auto; }
.emoji-panel span { font-size: 22px; cursor: pointer; padding: 2px 4px; border-radius: 6px; }
.emoji-panel span:hover { background: var(--panel2); }

.composer { display: flex; gap: 8px; align-items: center; padding: 12px 16px; background: var(--panel); border-top: 1px solid var(--border); }
.icon-btn { background: var(--panel2); color: var(--text); font-size: 18px; width: 40px; height: 40px; border-radius: 10px; }
.icon-btn:active { transform: scale(0.94); }
.voice-rec { flex: 1; min-height: 44px; padding: 0 16px; border: none; border-radius: 22px; background: var(--accent); color: #fff; font-size: 17px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 8px; cursor: pointer; -webkit-user-select: none; user-select: none; touch-action: none; }
.voice-rec:active { opacity: 0.85; }
#input-text { flex: 1; background: var(--bg); border: 1px solid var(--border); color: var(--text); padding: 11px 12px; border-radius: 10px; font-size: 14px; outline: none; resize: none; max-height: 160px; line-height: 1.5; font-family: inherit; overflow-y: auto; }
#input-text:focus { border-color: var(--accent); }
.voice-hint { text-align: center; color: var(--danger); font-size: 12px; margin: 0; padding-bottom: 8px; }

/* 会话栏（群聊 + 私聊列表） */
.conv-bar { display: flex; gap: 8px; overflow-x: auto; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.conv-chip { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; padding: 6px 12px; background: var(--panel2); border: 1px solid var(--border); border-radius: 999px; font-size: 13px; cursor: pointer; color: var(--text); }
.conv-avatar { width: 20px; height: 20px; border-radius: 50%; object-fit: cover; background: var(--panel); border: 1px solid var(--border); display: inline-flex; align-items: center; justify-content: center; font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.conv-label { overflow: hidden; text-overflow: ellipsis; }
.conv-chip.active { border-color: var(--accent); background: var(--accent); color: #fff; }
.conv-chip .badge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; min-width: 18px; text-align: center; }
.conv-chip.active .badge { background: #fff; color: var(--accent); }

/* 文件消息 */
.msg .file-chip { display: flex; align-items: center; gap: 8px; background: rgba(255,255,255,0.08); border-radius: 8px; padding: 8px 10px; cursor: pointer; max-width: 240px; }
.msg .file-chip .ficon { font-size: 20px; }
.msg .file-chip .fmeta { overflow: hidden; }
.msg .file-chip .fname { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg .file-chip .fsize { font-size: 11px; opacity: 0.7; }
.msg .live-badge { position: absolute; top: 4px; left: 4px; background: rgba(0,0,0,0.6); color: #fff; font-size: 11px; padding: 1px 6px; border-radius: 6px; }
.msg.media { position: relative; padding: 4px; }
.msg.media img { cursor: zoom-in; border-radius: 8px; max-width: 220px; display: block; }
.msg audio { width: 220px; max-width: 100%; }

/* 懒加载缩略图占位：不解密原图，点击进入画廊按需加载，节省带宽 */
.msg .thumb { width: 168px; height: 168px; border-radius: 10px; background: linear-gradient(135deg, var(--panel2), var(--panel)); border: 1px solid var(--border); cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--muted); transition: transform .12s ease, box-shadow .12s ease; }
.msg .thumb:hover { transform: translateY(-1px); box-shadow: 0 4px 14px rgba(0,0,0,0.25); }
.msg .thumb .thumb-ico { font-size: 34px; line-height: 1; }
.msg .thumb .thumb-label { font-size: 12px; }
.msg .thumb.livephoto { border-color: var(--accent); }
.msg .thumb.livephoto .thumb-ico { filter: drop-shadow(0 0 4px var(--accent)); }

/* 灯箱 */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,0.92); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.lightbox.hidden { display: none !important; }
.lightbox img, .lightbox video { max-width: 100%; max-height: 100%; border-radius: 10px; }
.lightbox .lb-close { position: absolute; top: 16px; right: 20px; font-size: 26px; color: #fff; cursor: pointer; background: rgba(255,255,255,0.12); width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; }
/* 画廊：在灯箱基础上叠加导航/计数 */
.lightbox.gallery { flex-direction: column; }
.lightbox.gallery .gbar { position: absolute; top: 14px; left: 0; right: 0; display: flex; align-items: center; justify-content: center; gap: 18px; z-index: 2; }
.lightbox.gallery .gcnt { color: #fff; font-size: 14px; background: rgba(0,0,0,0.4); padding: 4px 12px; border-radius: 20px; }
.lightbox.gallery .gclose { position: absolute; top: 14px; right: 18px; font-size: 24px; color: #fff; cursor: pointer; background: rgba(255,255,255,0.12); width: 38px; height: 38px; border-radius: 50%; display: flex; align-items: center; justify-content: center; z-index: 2; }
.lightbox.gallery .gnav { position: absolute; top: 50%; transform: translateY(-50%); font-size: 40px; color: #fff; cursor: pointer; background: rgba(255,255,255,0.1); width: 52px; height: 52px; border-radius: 50%; display: flex; align-items: center; justify-content: center; user-select: none; z-index: 2; }
.lightbox.gallery .gnav.gprev { left: 16px; }
.lightbox.gallery .gnav.gnext { right: 16px; }
.lightbox.gallery .gmedia { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; position: relative; }
.lightbox.gallery .gimg { max-width: 100%; max-height: 100%; border-radius: 10px; }
.lightbox.gallery .gplay { position: absolute; left: 50%; bottom: 24px; transform: translateX(-50%); color: #fff; background: rgba(0,0,0,0.55); padding: 10px 18px; border-radius: 24px; cursor: pointer; font-size: 15px; }
.lightbox.gallery .gloading { color: rgba(255,255,255,0.7); font-size: 15px; }

/* 设置弹窗 */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 60; padding: 20px; }
.modal.hidden { display: none !important; }
.modal-card { background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 22px; width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }
.modal-card h3 { margin: 0; color: var(--text); }
.set-row { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--text); }
.set-row > span { width: 56px; color: var(--muted); }
.set-row input[type=color] { width: 44px; height: 32px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); padding: 2px; cursor: pointer; }
.set-theme { background: var(--panel2); color: var(--text); border: 1px solid var(--border); padding: 7px 14px; }
.set-theme.active { border-color: var(--accent); background: var(--accent); color: #fff; }
/* 资料：头像与昵称 */
.profile-row { align-items: flex-start; }
.avatar-preview { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; background: var(--panel2); border: 1px solid var(--border); flex: 0 0 auto; }
.profile-fields { display: flex; flex-direction: column; gap: 6px; flex: 1; }
.set-name { background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 14px; }
.avatar-actions { display: flex; gap: 12px; }
.link-btn { color: var(--accent); cursor: pointer; font-size: 13px; }
.link-btn:hover { text-decoration: underline; }
.version-row { justify-content: space-between; }
.version-row .version-val { color: var(--accent); font-size: 13px; }
/* 在线状态点 */
.online-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent2); margin-left: 6px; vertical-align: middle; }
.online-dot.off { background: var(--muted); }

/* ============ 自适应：单栏流式，桌面/手机通用 ============ */
#app { max-width: 960px; width: 100%; height: 94vh; }
@media (max-width: 760px) {
  body { align-items: stretch; padding: 0; }
  #app { max-width: 100%; height: 100vh; height: 100dvh; border-radius: 0;
    padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
  .composer { flex-wrap: wrap; padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  #input-text { flex: 1 1 120px; min-width: 0; }
  .brand h1 { font-size: 28px; }
  .msg { max-width: 82%; }
  .icon-btn { width: 32px; height: 32px; font-size: 15px; }
}

/* 离线期间新消息提示条 */
.offline-banner { background: var(--accent); color: #fff; font-size: 13px; text-align: center;
  padding: 7px 12px; cursor: pointer; }
.offline-banner.hidden { display: none !important; }

/* 自己消息下方的送达/已读状态 */
.msg-meta { font-size: 11px; color: var(--muted); margin-top: 3px; text-align: right; opacity: 0.85; }
/* 手机端输入框字号≥16px，避免聚焦时 Safari 整体放大 */
#input-text { font-size: 16px; }

/* ============ @提及 ============ */
.mention { color: var(--accent); font-weight: 600; }
.msg.me .mention { color: #fff; text-decoration: underline; }
.msg.mentioned { outline: 1px solid var(--accent); box-shadow: 0 0 0 2px rgba(79,140,255,0.18); }
.mention-pop { position: absolute; left: 16px; right: 16px; bottom: 66px; max-height: 200px; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; z-index: 8; box-shadow: 0 8px 24px rgba(0,0,0,0.35); }
.mention-pop.hidden { display: none !important; }
.mp-item { padding: 9px 12px; font-size: 14px; cursor: pointer; color: var(--text); }
.mp-item:hover, .mp-item.active { background: var(--panel2); }

/* ============ 引用回复 ============ */
.reply-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; margin: 0 16px;
  background: var(--panel2); border-left: 3px solid var(--accent); border-radius: 8px; }
.reply-bar.hidden { display: none !important; }
.reply-info { flex: 1; overflow: hidden; font-size: 13px; color: var(--muted); }
.reply-info .reply-label { color: var(--accent); font-weight: 600; margin-right: 6px; }
.reply-info .reply-snippet { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reply-cancel { background: transparent; color: var(--muted); font-size: 16px; width: 28px; height: 28px; border-radius: 50%; }
.reply-cancel:hover { color: var(--danger); }

.msg .quote { display: flex; flex-direction: column; gap: 2px; margin-bottom: 5px; padding: 5px 8px;
  background: rgba(255,255,255,0.06); border-left: 3px solid var(--accent); border-radius: 6px; cursor: pointer; font-size: 12px; }
.msg .quote .q-name { color: var(--accent); font-weight: 600; }
.msg .quote .q-text { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.msg.me .quote .q-text { color: rgba(255,255,255,0.85); }
/* 引用中的图片/实况缩略图：解密后以 Blob URL 内联显示，避免裸显 [image] */
.msg .quote .q-thumb { display: block; width: 64px; height: 64px; object-fit: cover; border-radius: 6px; margin-top: 3px; background: rgba(255,255,255,0.08); }
.msg .quote.q-unavailable { opacity: 0.7; }
.msg.flash { animation: flash 1s ease; }
@keyframes flash { 0% { background: var(--accent); } 100% { background: transparent; } }

/* 消息悬浮引用按钮 */
.msg-reply { position: absolute; top: -8px; right: -6px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--panel2); color: var(--muted); border: 1px solid var(--border); font-size: 12px; line-height: 1;
  display: none; align-items: center; justify-content: center; padding: 0; }
.msg:hover .msg-reply { display: flex; }
.msg { position: relative; }

/* ============ 移动端 / Capacitor App 适配 ============ */
/* 在原生 WebView 中禁用文本选择高亮与长按菜单干扰 */
* { -webkit-touch-callout: none; }
button, input, textarea, .icon-btn, .msg-reply, .mp-item {
  -webkit-tap-highlight-color: transparent;
}
/* iOS Safari / WebView 软键盘弹起时，用 dvh 让聊天区正确收缩 */
@media (max-width: 760px) {
  html, body { height: 100%; overflow: hidden; }
  #app { height: 100vh; height: 100dvh; }
  /* 底部输入框区域给手势条留白（Capacitor 加载远程页时用 safe-area） */
  .composer { padding-left: max(12px, env(safe-area-inset-left));
              padding-right: max(12px, env(safe-area-inset-right)); }
  .topbar { padding-left: max(12px, env(safe-area-inset-left));
            padding-right: max(12px, env(safe-area-inset-right)); }
  /* 可点击元素最小触控尺寸 */
  .icon-btn, .msg-reply { min-width: 36px; min-height: 36px; }
  /* 移动端始终显示消息引用按钮（无 hover） */
  .msg .msg-reply { display: flex; }
}


