:root {
  --bg: #eef2f7;
  --panel: #ffffff;
  --border: #e4e9f0;
  --text: #1f2937;
  --text-dim: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #eaf1fe;
  --green: #16a34a;
  --red: #dc2626;
  --radius: 12px;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------- 按钮 ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 9px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  transition: 0.15s;
}
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn.primary {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-dim); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- 登录/注册 ---------- */
.auth-view {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(600px 400px at 15% 10%, rgba(59, 130, 246, 0.12), transparent 60%),
    radial-gradient(700px 500px at 85% 90%, rgba(99, 102, 241, 0.10), transparent 60%),
    var(--bg);
}
.auth-card {
  width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 32px 28px;
  box-shadow: 0 12px 40px rgba(15, 23, 42, 0.10);
}
.brand {
  font-size: 27px;
  background: linear-gradient(135deg, #2563eb, #6366f1);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 800;
}
.brand-sub { color: var(--text-dim); font-size: 13px; margin: 6px 0 18px; }
.tabs { display: flex; gap: 6px; margin-bottom: 18px; }
.tab {
  flex: 1; padding: 8px; border: 1px solid var(--border); border-radius: 9px;
  background: transparent; cursor: pointer; font-size: 14px; color: var(--text-dim);
  transition: 0.15s;
}
.tab.active {
  background: var(--primary-soft); border-color: var(--primary);
  color: var(--primary); font-weight: 600;
}
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.auth-form input, .search-bar input, #dialogInput {
  border: 1px solid var(--border); border-radius: 9px; padding: 10px 12px;
  font-size: 14px; outline: none; width: 100%; transition: border-color 0.15s, box-shadow 0.15s;
}
.auth-form input:focus, .search-bar input:focus, #dialogInput:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
.hint { font-size: 12px; color: var(--text-dim); line-height: 1.6; }
.auth-error { margin-top: 12px; color: var(--red); font-size: 13px; }

.register-success { margin-top: 8px; display: flex; flex-direction: column; gap: 10px; }
.success-title { font-size: 17px; font-weight: 600; }
.account-box {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--primary-soft); border: 1px dashed var(--primary);
  border-radius: 9px; padding: 12px 14px;
}
.account-box span { font-size: 24px; font-weight: 700; letter-spacing: 3px; color: var(--primary-dark); }

/* ---------- 主界面 ---------- */
.main-view { display: flex; height: 100vh; }
.sidebar {
  width: 300px; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.me {
  display: flex; align-items: center; gap: 10px; padding: 14px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.06), rgba(99, 102, 241, 0.05));
}
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px; flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(37, 99, 235, 0.3);
}
.me-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.me-info strong { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account-no { font-size: 12px; color: var(--text-dim); }

.search-bar { display: flex; gap: 6px; padding: 10px 12px; }
.search-result { padding: 0 12px 10px; }
.search-result .user-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--primary-soft); border-radius: 9px; padding: 8px 10px; margin-bottom: 6px;
}
.user-row .u-name { font-size: 13px; }
.user-row .u-name small { color: var(--text-dim); margin-left: 6px; }

.list-actions { display: flex; gap: 6px; padding: 0 12px 10px; }
.list-actions .btn { flex: 1; }

.conv-list { flex: 1; overflow-y: auto; }
.conv-section-title {
  font-size: 12px; color: var(--text-dim); padding: 10px 14px 4px;
  font-weight: 600; letter-spacing: 1px;
}
.conv-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.12s;
}
.conv-item:hover { background: var(--bg); }
.conv-item.active {
  background: var(--primary-soft);
  border-left-color: var(--primary);
}
.conv-item .avatar { width: 36px; height: 36px; font-size: 14px; border-radius: 10px; box-shadow: none; }
.conv-item .ci-info { flex: 1; min-width: 0; }
.conv-item .ci-name { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .ci-last { font-size: 12px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item.has-unread .ci-name { font-weight: 700; }
.conv-item.has-unread .ci-last { color: var(--text); }

/* 未读角标：红色渐变 + 微脉冲动画 */
.badge {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  color: #fff; font-size: 11px; font-weight: 600; min-width: 19px; height: 19px;
  border-radius: 10px; display: flex; align-items: center; justify-content: center;
  padding: 0 6px; flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(220, 38, 38, 0.4);
  animation: badge-pop 0.3s ease;
}
@keyframes badge-pop {
  0% { transform: scale(0.4); opacity: 0; }
  60% { transform: scale(1.18); }
  100% { transform: scale(1); opacity: 1; }
}
.group-tag {
  font-size: 10px; color: var(--primary); background: var(--primary-soft);
  border-radius: 4px; padding: 1px 4px; margin-left: 4px; font-weight: 600;
}

/* 好友在线绿点 */
.online-dot {
  display: inline-block;
  width: 8px; height: 8px;
  background: #22c55e;
  border-radius: 50%;
  margin-left: 6px;
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18);
  vertical-align: middle;
  animation: dotIn 0.25s ease-out;
}
@keyframes dotIn { from { transform: scale(0); } to { transform: scale(1); } }

/* ---------- 聊天区 ---------- */
.chat-panel {
  flex: 1; display: flex; flex-direction: column; min-width: 0;
  background: var(--bg);
}
.empty-chat {
  flex: 1; display: flex; flex-direction: column; gap: 12px;
  align-items: center; justify-content: center; color: var(--text-dim);
}
.empty-chat::before {
  content: '💬';
  font-size: 44px;
  display: block;
  width: 92px; height: 92px; line-height: 92px; text-align: center;
  background: var(--panel); border-radius: 50%;
  box-shadow: var(--shadow-md);
}
.chat-area { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-header {
  display: flex; align-items: baseline; gap: 10px;
  padding: 13px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow-sm);
  z-index: 2;
}
.chat-header h2 { font-size: 16px; }
.chat-meta { font-size: 12px; color: var(--text-dim); }

.messages {
  flex: 1; overflow-y: auto;
  padding: 18px 20px;
  display: flex; flex-direction: column; gap: 12px;
  background:
    radial-gradient(500px 300px at 90% 0%, rgba(59, 130, 246, 0.04), transparent 60%),
    radial-gradient(500px 300px at 0% 100%, rgba(99, 102, 241, 0.04), transparent 60%);
}

/* 消息气泡：入场动画 + 立体感 */
.msg {
  display: flex; gap: 10px; max-width: 72%;
  animation: msg-in 0.22s ease;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg .avatar { width: 32px; height: 32px; font-size: 12px; box-shadow: var(--shadow-sm); }
.msg .bubble-wrap { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.msg .sender { font-size: 11px; color: var(--text-dim); padding-left: 4px; }
.msg .bubble {
  padding: 9px 13px;
  border-radius: 14px;
  border-bottom-left-radius: 5px;
  font-size: 14px; line-height: 1.55;
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  word-break: break-word; white-space: pre-wrap;
}
.msg .time { font-size: 10px; color: var(--text-dim); align-self: flex-end; }
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg.mine .bubble {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  border: none;
  border-radius: 14px;
  border-bottom-right-radius: 5px;
  color: #fff;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
}
.msg.mine .sender, .msg.mine .time { align-self: flex-end; color: var(--text-dim); }
.msg.pending { opacity: 0.55; }
.msg .sys {
  font-size: 12px; color: var(--text-dim);
  background: rgba(15, 23, 42, 0.05);
  border-radius: 999px; padding: 5px 14px;
  align-self: center;
}

.composer {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(8px);
}
.composer textarea {
  flex: 1; resize: none;
  border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 14px; font-size: 14px; font-family: inherit; outline: none;
  max-height: 120px; background: var(--bg);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer textarea:focus {
  border-color: var(--primary); background: var(--panel);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.10);
}
.composer .btn.primary { border-radius: 12px; padding: 10px 20px; }
.attach-btn { font-size: 18px; padding: 8px 10px; line-height: 1; }

/* 语音输入：模式切换按钮激活态 + 微信式「按住 说话」大按钮 */
.mic-btn.active {
  color: var(--primary);
  background: rgba(37, 99, 235, 0.10);
}
.hold-btn {
  flex: 1; align-self: flex-end; height: 40px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--panel); color: var(--text);
  font-size: 14px; font-family: inherit;
  user-select: none; -webkit-user-select: none;
  touch-action: none; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: background 0.12s, border-color 0.12s;
}
.hold-btn.holding {
  background: #dbe5f4;
  border-color: rgba(37, 99, 235, 0.35);
  color: var(--primary);
}
.hold-btn.cancel {
  background: rgba(225, 29, 72, 0.10);
  border-color: rgba(225, 29, 72, 0.40);
  color: #e11d48;
}

/* ---------- 图片 / 文件消息 ---------- */
.msg .bubble.media { padding: 4px; background: var(--panel); }
.msg.mine .bubble.media { background: rgba(255, 255, 255, 0.92); }
.msg-img {
  display: block; max-width: 220px; max-height: 220px;
  border-radius: 10px; cursor: zoom-in; object-fit: cover;
}
.img-caption { font-size: 12px; color: var(--text-dim); padding: 4px 6px 2px; word-break: break-all; }
.msg.mine .img-caption { color: var(--text-dim); }
.file-row {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; text-decoration: none; color: var(--text);
  min-width: 160px;
}
.msg.mine .file-row { color: #1d4ed8; }
.file-icon { font-size: 22px; }
.file-name { font-size: 13px; word-break: break-all; text-decoration: underline; }

/* 撤回占位 */
.msg.recalled { max-width: 100%; }
/* 自己的气泡：长按撤回时不弹系统选择菜单 */
.msg.mine .bubble { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }

/* ---------- 群成员管理弹窗 ---------- */
.members-list {
  max-height: 50vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
  margin: 12px -6px 10px; padding: 2px 6px;
}
.member-row {
  display: flex; align-items: center; gap: 12px;
  padding: 9px 10px; border-radius: 10px;
  transition: background 0.15s;
}
.member-row:hover { background: var(--bg); }
.member-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.member-name { font-size: 14px; font-weight: 500; display: flex; align-items: center; gap: 6px; }
.member-acc { color: var(--text-dim); font-size: 12px; }
.member-ops { display: flex; gap: 6px; flex-shrink: 0; }
.owner-tag {
  font-size: 10px; color: #b45309; background: linear-gradient(135deg, #fef3c7, #fde68a);
  border-radius: 999px; padding: 1px 8px; font-weight: 600; flex-shrink: 0;
}
.me-tag {
  font-size: 10px; color: var(--primary); background: var(--primary-soft);
  border-radius: 999px; padding: 1px 8px; flex-shrink: 0;
}
.btn.danger {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  border-color: transparent; color: #fff;
  box-shadow: 0 2px 8px rgba(220, 38, 38, 0.35);
}
.btn.danger:hover { filter: brightness(1.06); color: #fff; }
.members-actions { justify-content: space-between; }

/* ---------- 弹窗与提示 ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 22px;
  width: 320px; box-shadow: 0 16px 48px rgba(15, 23, 42, 0.2);
  /* 全局 reset 把 margin 清零后原生 dialog 会贴在左上角，这里恢复居中 */
  position: fixed; inset: 0; margin: auto; height: fit-content;
}
dialog::backdrop { background: rgba(15, 23, 42, 0.4); backdrop-filter: blur(2px); }
dialog h3 { font-size: 16px; margin-bottom: 6px; }
dialog form { display: flex; flex-direction: column; gap: 12px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }

.toast {
  position: fixed; top: 24px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 18px; border-radius: 10px;
  font-size: 13px; z-index: 100; animation: fadein 0.2s;
  box-shadow: var(--shadow-md);
}
.toast.err { background: var(--red); }
@keyframes fadein { from { opacity: 0; transform: translate(-50%, -6px); } to { opacity: 1; } }

.conn-dot {
  width: 10px; height: 10px; border-radius: 50%;
  flex-shrink: 0; margin-left: 8px;
  transition: background 0.2s;
}
.conn-dot.online { background: #22c55e; box-shadow: 0 0 4px #22c55e; }
.conn-dot.connecting { background: #9ca3af; }

/* ---------- 邀请码弹窗 ---------- */
.code-dialog { text-align: center; }
.code-dialog-icon {
  width: 52px; height: 52px; margin: 0 auto 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 26px; border-radius: 16px;
  background: linear-gradient(135deg, #dbeafe, #e0e7ff);
  box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.15);
}
.code-dialog h3 { text-align: center; }
.code-dialog .hint { margin-bottom: 14px; }
.code-box {
  display: flex; align-items: center; justify-content: center;
  background:
    repeating-linear-gradient(45deg, rgba(37, 99, 235, 0.04) 0 8px, transparent 8px 16px),
    var(--primary-soft);
  border: 1.5px dashed var(--primary);
  border-radius: 12px; padding: 16px 14px;
  cursor: pointer; transition: 0.15s;
  -webkit-user-select: all; user-select: all;
}
.code-box:hover { transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.code-box:active { transform: scale(0.98); }
.code-box span {
  font-family: "SF Mono", "Cascadia Code", Consolas, monospace;
  font-size: 26px; font-weight: 700; letter-spacing: 6px;
  color: var(--primary-dark);
}
.code-copy-btn { width: 100%; margin-top: 12px; justify-content: center; }
.code-dialog .dialog-actions { margin-top: 6px; }
.header-actions { margin-left: auto; display: flex; gap: 6px; }

/* 返回按钮：桌面端隐藏 */
.back-btn { display: none; }

/* ---------- 移动端适配 ---------- */
/* 防止 iOS 键盘弹出/自动缩放导致页面被撑宽 */
html, body { overflow-x: hidden; max-width: 100vw; }

@media (max-width: 768px) {
  body { height: 100vh; height: 100dvh; }

  /* 关键：iOS Safari 会对字号 <16px 的输入框自动放大页面（表现为界面突然宽出/放大）。
     移动端把所有输入控件字号提到 16px 即可禁用该行为 */
  .composer textarea,
  .auth-form input,
  .search-bar input,
  #dialogInput,
  dialog input {
    font-size: 16px !important;
  }

  .auth-card { width: 92vw; max-width: 380px; padding: 24px 20px; }

  /* 单栏切换：默认显示会话列表，点开会话后整屏切到聊天页 */
  .main-view { height: 100vh; height: 100dvh; }
  .sidebar { width: 100%; border-right: none; }
  .chat-panel { display: none; }
  .main-view.show-chat .chat-panel { display: flex; flex: 1; }
  .main-view.show-chat .sidebar { display: none; }
  .back-btn { display: inline-block; }

  .chat-header {
    padding: 10px 12px; gap: 8px;
    align-items: center; flex-wrap: nowrap;
  }
  .chat-header h2 { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex-shrink: 1; }
  .chat-meta { display: none; } /* 空间有限，成员信息挪到邀请码弹窗 */

  .messages { padding: 12px; gap: 10px; }
  .msg { max-width: 85%; }
  .composer { padding: 8px 10px; gap: 6px; }
  .composer .btn { padding: 9px 14px; }

  dialog { width: 88vw; max-width: 320px; }

  /* 触控目标放大 */
  .conv-item { padding: 13px 14px; }
  .btn.small { padding: 8px 12px; font-size: 13px; }
}
