/* ============================================================
   SecureChat Web - 样式表
   深色/浅色主题跟随系统，移动端优先响应式设计
   ============================================================ */

:root,
[data-theme="dark"] {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #21262d;
  --bg-hover: #30363d;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #484f58;
  --accent: #238636;
  --accent-hover: #2ea043;
  --accent-blue: #58a6ff;
  --danger: #da3633;
  --border: #30363d;
  --shadow: rgba(0, 0, 0, 0.4);
  --radius: 8px;
  --radius-lg: 12px;
  --transition: 0.2s ease;
  --sidebar-bg: #161b22;
  --chat-bg: #0d1117;
  --input-bg: #21262d;
  --modal-bg: #21262d;

  /* 安全区（iPhone 刘海/小白条） */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  /* iOS 100vh 修复（由 inline script 动态赋值） */
  --vh: 1vh;

  /* 触摸目标（Apple HIG 推荐 44px） */
  --touch-target: 44px;
}

[data-theme="light"] {
  --bg-primary: #ffffff;
  --bg-secondary: #f6f8fa;
  --bg-tertiary: #eaeef2;
  --bg-hover: #d0d7de;
  --text-primary: #1f2328;
  --text-secondary: #656d76;
  --text-muted: #8b949e;
  --accent: #1a7f37;
  --accent-hover: #2da44e;
  --accent-blue: #0969da;
  --danger: #cf222e;
  --border: #d0d7de;
  --shadow: rgba(0, 0, 0, 0.12);
  --sidebar-bg: #f6f8fa;
  --chat-bg: #ffffff;
  --input-bg: #f6f8fa;
  --modal-bg: #ffffff;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  /* 禁止 iOS Safari 弹性滚动/双击缩放干扰 */
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  min-height: calc(var(--vh) * 100);
  /* iOS 横屏时增加左侧安全区（横屏刘海） */
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.hidden, .auth-form.hidden { display: none !important; }

/* ── 通用组件 ─────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  text-decoration: none;
}

.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--bg-hover); }

.btn-danger {
  background: var(--danger);
  color: white;
}

.btn-icon {
  padding: 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-primary);
  transition: all var(--transition);
  /* 最小触摸目标 */
  min-width: var(--touch-target);
  min-height: var(--touch-target);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn-icon:hover { background: var(--bg-hover); }

input[type="text"],
input[type="password"],
input[type="search"] {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: border-color var(--transition);
}

input:focus {
  border-color: var(--accent-blue);
}

input::placeholder {
  color: var(--text-muted);
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.6;
}

/* ── Auth View ────────────────────────────────────────────── */

#auth-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 40px 32px;
}

.auth-logo {
  text-align: center;
  margin-bottom: 32px;
}

.auth-logo .emoji {
  font-size: 48px;
  display: block;
  margin-bottom: 8px;
}

.auth-logo h1 {
  font-size: 24px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.auth-logo p {
  font-size: 13px;
  color: var(--text-secondary);
}

.auth-tabs {
  display: flex;
  margin-bottom: 24px;
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  padding: 4px;
}

.auth-tab {
  flex: 1;
  padding: 8px;
  text-align: center;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 6px;
  transition: all var(--transition);
}

.auth-tab.active {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.auth-form {
  display: flex;
  flex-direction: column;
}

.auth-form .btn {
  width: 100%;
  margin-top: 8px;
  padding: 12px;
  font-size: 15px;
}

.auth-form .form-group {
  display: flex;
  width: 100%;
  flex-direction: column;
  gap: 6px;
}

.auth-form label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.auth-error {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  padding: 8px;
  background: rgba(218, 54, 51, 0.1);
  border-radius: var(--radius);
}

/* 📦 安卓客户端下载入口（SPEC-P1-APK）
   放在登录卡片内、注册表单下方：未登录用户也能看到下载入口。
   整块可点（<a>），不依赖 JS。 */
.android-download {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin-top: 16px;
  padding: 12px 14px;
  text-decoration: none;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text-primary);
  transition: all var(--transition);
}

.android-download:hover {
  background: var(--bg-secondary);
  border-color: var(--accent);
  transform: translateY(-1px);
}

.android-download:active {
  transform: translateY(0);
}

.android-download-icon {
  font-size: 22px;
  line-height: 1;
  flex-shrink: 0;
}

.android-download-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
}

.android-download-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.android-download-meta {
  font-size: 12px;
  color: var(--text-secondary);
}

.android-download-arrow {
  font-size: 16px;
  color: var(--accent);
  flex-shrink: 0;
}

/* 移动端：确保入口在窄屏下不被压缩、依然完整可点 */
@media (max-width: 480px) {
  .android-download {
    padding: 10px 12px;
    gap: 8px;
  }
  .android-download-title { font-size: 13px; }
  .android-download-meta { font-size: 11px; }
  .android-download-icon { font-size: 20px; }
}

/* ── App View ─────────────────────────────────────────────── */

#app-view {
  flex: 1;
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ⚡ 锁屏遮罩：隐藏聊天内容，防肩窥
 * 解锁弹窗（z-index: 1001）仍然可见，遮罩 z-index 设为 1000 以让其压在锁屏层上
 */
#app-view.locked .sidebar { filter: blur(8px); pointer-events: none; user-select: none; }
#app-view.locked .main-content { filter: blur(8px); pointer-events: none; user-select: none; }
#app-view.locked::after {
  content: '🔒 已锁定\A请输入密码解锁';
  white-space: pre;
  text-align: center;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(10, 10, 15, 0.85);
  color: #fff;
  font-size: 24px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding-top: 80px;
  line-height: 1.8;
}

/* Sidebar */
.sidebar {
  width: 320px;
  min-width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  /* ⚠️ BUG-20261027-01 修复：侧栏是 #app-view 的 flex 子项，不加 flex-shrink:0
     会被 .main-content 的固有宽度挤压（实测 375px 视口下 320px → 295px），
     导致 header 里 6 个 44px 按钮放不下、末位「登出」被挤出栏外。 */
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* ⚠️ BUG-20261027-01 修复：改为可换行两行布局。
     侧栏固定 320px，padding 16px → 可用宽 288px。
     6 个 44px 触摸目标按钮 + gap 至少需要 6*44+5*4 = 284px，
     几乎占满整行，标题/用户名被压成 0px（实测 clientWidth=0px，
     scrollWidth=61px，内容被裁剪）→ 登录后看不到当前用户名。
     同行物理上放不下「标题+用户名+6 按钮」，故：
       第 1 行 = 标题 + 用户名（独占整行）
       第 2 行 = 6 个按钮（右对齐） */
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
  font-size: 16px;
  font-weight: 700;
  /* ⚠️ 修复：用户名变长时 h2 原本不收缩，会把右侧 5 个 header 按钮
     挤出 320px 侧栏（实测溢出 83px），按钮随即被 .main-content 覆盖，
     导致「登出」按钮在所有视口宽度下都点不到。
     这里允许收缩 + 省略号截断用户名，保证按钮组始终完整可见可点。 */
  min-width: 0;
  /* 独占第一行：flex-basis 100% + 可换行 → 按钮组自动落到第二行 */
  flex: 1 1 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 用户名是可选信息，最多占 h2 的一半宽度，避免挤掉标题 */
#current-username {
  display: inline-block;
  max-width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: bottom;
}

.header-actions {
  display: flex;
  gap: 4px;
  /* 按钮组不可压缩：min-width 44px 是最小触摸目标，不能被挤扁 */
  flex-shrink: 0;
  /* 第二行：右对齐，6*44 + 5*4 = 284px ≤ 288px 可用宽 */
  flex: 1 1 100%;
  justify-content: flex-end;
  /* 兜底：极窄屏下允许按钮换行，绝不溢出栏外 */
  flex-wrap: wrap;
}

/* Search Bar */
/* ⚡ P2-2：搜索行改为 flex，右侧放「已归档会话」入口按钮。
   原因：header 里放 7 个 44px 触摸目标按钮需要 7*44+6*8=356px，
   超出 320px 侧栏可用宽（304px），末位「登出」按钮被挤出栏外无法点击。 */
.search-bar {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.search-bar input {
  background: var(--bg-tertiary);
  border-radius: var(--radius);
  /* width:auto 覆盖通用 input[type=search]{width:100%}：
     flex item 的 basis 若取 100% 会把右侧按钮挤出容器 */
  width: auto;
  flex: 1 1 0;
  min-width: 0;
}

/* Conversation List */
.conversation-list {
  flex: 1;
  overflow-y: auto;
}

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

.conversation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background var(--transition);
  border-bottom: 1px solid var(--border);
}

.conversation-item:hover {
  background: var(--bg-hover);
}

.conversation-item.online .conv-name::after {
  content: ' ●';
  color: var(--accent);
  font-size: 10px;
}

.conv-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.conv-info {
  flex: 1;
  min-width: 0;
}

.conv-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-preview {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.conv-meta {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Main Content */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
}

.main-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-secondary);
}

.main-placeholder .icon {
  font-size: 64px;
  opacity: 0.3;
}

.main-placeholder p {
  font-size: 14px;
}

/* Search Results */
#search-results {
  padding: 8px 0;
  border-top: 1px solid var(--border);
  max-height: 300px;
  overflow-y: auto;
}

.search-result-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background var(--transition);
}

.search-result-item:hover {
  background: var(--bg-hover);
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.user-info {
  flex: 1;
}

.user-name {
  font-size: 14px;
  font-weight: 600;
}

/* ── Chat View ─────────────────────────────────────────────── */

#chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

.chat-header .back-btn {
  background: none;
  border: none;
  color: var(--accent-blue);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius);
  transition: background var(--transition);
}
.chat-header .back-btn:hover { background: var(--bg-tertiary); }

.chat-header h3 {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.messages-container::-webkit-scrollbar {
  width: 4px;
}
.messages-container::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 2px;
}

.message-bubble {
  display: flex;
  max-width: 75%;
}

.message-bubble.mine {
  align-self: flex-end;
}

.message-bubble.theirs {
  align-self: flex-start;
}

.bubble-content {
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  position: relative;
}

.message-bubble.mine .bubble-content {
  background: var(--accent);
  color: white;
  border-bottom-right-radius: 4px;
}

.message-bubble.theirs .bubble-content {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.bubble-text {
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
}

.bubble-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  justify-content: flex-end;
}

.bubble-time {
  font-size: 10px;
  opacity: 0.7;
}

.bubble-status {
  font-size: 10px;
  opacity: 0.7;
}

/* Typing indicator */
#typing-indicator {
  padding: 8px 16px;
  color: var(--text-secondary);
  font-size: 13px;
  font-style: italic;
}

/* Chat Input */
.chat-input-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

.chat-input {
  flex: 1;
  resize: none;
  min-height: 40px;
  max-height: 120px;
  padding: 10px 14px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color var(--transition);
}

.chat-input:focus { border-color: var(--accent-blue); }

.send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
  flex-shrink: 0;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Error Toast ───────────────────────────────────────────── */

#error-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--danger);
  color: white;
  padding: 12px 24px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  z-index: 9999;
  opacity: 0;
  transition: all 0.3s ease;
  max-width: 90%;
}

#error-toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ── Responsive ───────────────────────────────────────────── */

/* 全局：用 --vh 替代 100vh（iOS Safari URL 栏展开/收起时正确） */
html, body {
  height: calc(var(--vh, 1vh) * 100);
}

/* ── A. 小屏手机 (≤ 480px) ───────────────────────────────── */
@media (max-width: 480px) {
  /* ⚠️ BUG-20261027-01 修复：侧栏占满整宽（与下方 481-768px 段一致）。
     小屏下 .sidebar 若只占 320px，聊天列表会被挤成窄条、
     header 里 6 个 44px 按钮（需 284px）放不下。 */
  .sidebar {
    width: 100%;
    min-width: unset;
  }

  /* 聊天 header：紧凑布局，4 个按钮不再挤 */
  .chat-header {
    padding: 8px 10px;
    padding-left: calc(10px + var(--safe-left));
    padding-right: calc(10px + var(--safe-right));
    padding-top: calc(8px + var(--safe-top));
    gap: 6px;
  }

  .chat-header .back-btn {
    font-size: 22px;
    padding: 6px 10px;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
  }

  .chat-header h3 {
    font-size: 14px;
    /* 防止标题过长挤压按钮 */
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* 聊天 header 图标按钮组 */
  .chat-header > div[style*="margin-left:auto"] {
    gap: 4px !important;
  }

  .chat-header .btn-icon {
    font-size: 16px !important;
    padding: 6px;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
  }

  /* 消息列表：缩 padding */
  .messages-container {
    padding: 10px 8px;
    gap: 6px;
  }

  /* 消息气泡：小屏更宽（85%），padding/字号缩 */
  .message-bubble {
    max-width: 85%;
  }

  .bubble-content {
    padding: 8px 12px;
  }

  .bubble-text {
    font-size: 13px;
    line-height: 1.45;
  }

  .bubble-time, .bubble-status {
    font-size: 9px;
  }

  /* 输入区：紧凑布局，按钮达 44×44 */
  .input-area {
    padding: 8px 10px;
    padding-bottom: calc(8px + var(--safe-bottom));
    padding-left: calc(10px + var(--safe-left));
    padding-right: calc(10px + var(--safe-right));
    gap: 6px;
  }

  #attach-btn,
  .voice-record-btn {
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #message-input {
    font-size: 16px; /* iOS 16px 才不自动放大 */
    min-height: 38px;
    padding: 8px 12px;
  }

  .send-btn {
    min-width: var(--touch-target);
    min-height: var(--touch-target);
  }

  /* 录音覆盖层：小屏居中距输入区更近 */
  #voice-record-overlay {
    bottom: calc(60px + var(--safe-bottom)) !important;
  }

  /* 模态框：改为底部 sheet 风格 */
  .modal-overlay {
    align-items: flex-end !important;
  }

  .modal-content {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding: 20px 16px;
    padding-bottom: calc(20px + var(--safe-bottom));
    max-height: 85vh;
    animation: slideUp 0.25s ease-out;
  }

  @keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }

  /* 通话 overlay：小屏本地小窗缩小、控制按钮位置调整 */
  #local-video {
    width: 80px !important;
    height: 110px !important;
    top: calc(12px + var(--safe-top)) !important;
    right: 12px !important;
  }

  #call-overlay #call-peer-name {
    font-size: 18px !important;
    margin-top: calc(20px + var(--safe-top));
  }

  #call-overlay #call-status-text {
    font-size: 14px !important;
  }

  #call-overlay > div[style*="bottom: 48px"] {
    bottom: calc(24px + var(--safe-bottom)) !important;
    gap: 16px !important;
  }

  /* 登录卡片：缩 padding，留出安全区 */
  .auth-card {
    padding: 24px 20px;
    padding-top: calc(24px + var(--safe-top));
    padding-bottom: calc(24px + var(--safe-bottom));
    width: 100%;
    max-width: 100%;
    border-radius: 0;
    min-height: calc(var(--vh, 1vh) * 100);
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  /* 错误 toast：避开刘海
     ⚠️ BUG-20261027-02：必须同时重置 bottom！
     基础规则用 bottom: 24px 定位，这里只加了 top，fixed 元素同时带 top+bottom
     且 height:auto → 被拉伸填满两者之间（实测 375×667 下高 623px = 93.4% 屏高，
     1280×800 下正常 44px）。且 z-index:9999 + 不透明背景 → 整个屏幕点不动。
     修法：bottom: auto 解除另一边定位 + pointer-events: none 不阻断底层点击。 */
  #error-toast {
    top: calc(20px + var(--safe-top)) !important;
    bottom: auto !important;
    max-width: calc(100vw - 32px);
    pointer-events: none;
  }
}

/* ── B. 平板手机 (481-768px) ─────────────────────────────── */
@media (min-width: 481px) and (max-width: 768px) {
  .sidebar {
    width: 100%;
    min-width: unset;
  }

  .message-bubble {
    max-width: 78%;
  }

  .chat-header {
    padding-left: calc(12px + var(--safe-left));
    padding-right: calc(12px + var(--safe-right));
  }

  .input-area {
    padding-left: calc(12px + var(--safe-left));
    padding-right: calc(12px + var(--safe-right));
    padding-bottom: calc(8px + var(--safe-bottom));
  }
}

/* ── C. iPad (769-1024px) ────────────────────────────────── */
@media (min-width: 769px) and (max-width: 1024px) {
  /* iPad 保持双栏布局（sidebar 280-320），但间距优化 */
  .sidebar {
    min-width: 280px;
    max-width: 320px;
  }

  .message-bubble {
    max-width: 70%;
  }

  /* 通话 overlay 本地小窗略小 */
  #local-video {
    width: 140px !important;
    height: 180px !important;
  }
}

/* ── D. 横屏（手机）────────────────────────────────────── */
@media (max-height: 500px) and (orientation: landscape) {
  /* 通话 overlay 横屏：本地小窗挪左上，控制按钮放右边 */
  #local-video {
    width: 100px !important;
    height: 130px !important;
    top: 12px !important;
    right: 12px !important;
  }

  #call-overlay > div[style*="top: 40px"] {
    top: 12px !important;
    left: 12px !important;
    right: auto !important;
    text-align: left !important;
  }

  #call-overlay #call-peer-name {
    font-size: 16px !important;
    margin-bottom: 2px !important;
  }

  #call-overlay #call-status-text {
    font-size: 13px !important;
  }

  #call-overlay > div[style*="bottom: 48px"] {
    bottom: 12px !important;
  }

  /* 录音 overlay 横屏靠右下角 */
  #voice-record-overlay {
    bottom: 60px !important;
  }
}

/* ── Call UI ───────────────────────────────────────────────── */

.call-btn {
  min-width: 48px;
  height: 48px;
  border-radius: 50%;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 16px;
  transition: opacity 0.2s, transform 0.1s;
}

.call-btn:active {
  transform: scale(0.95);
}

.call-btn:hover {
  opacity: 0.9;
}

/* 通话中静音/摄像头按钮 */
.call-btn:not([style*="border-radius: 50%"]) {
  border-radius: 24px;
  color: #fff;
}

#call-overlay video {
  object-fit: cover;
}

/* ── 未读计数角标 ─────────────────────────────────────── */
.conv-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}

.unread-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background: #e74c3c;
  color: white;
  font-size: 11px;
  font-weight: bold;
  border-radius: 50%;
  min-width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  border: 2px solid var(--bg-secondary);
  line-height: 1;
}

.conversation-item.unread .conv-name {
  font-weight: bold;
}

.conversation-item.unread .conv-preview {
  color: var(--text-primary);
}

/* ── P1-4 图片缩略图预览 ─────────────────────────────── */
.image-preview {
  margin-bottom: 8px;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  background: rgba(255,255,255,0.06);
  line-height: 0;
}

.image-thumb {
  display: block;
  width: 100%;
  max-width: 280px;
  height: auto;
  max-height: 320px;
  object-fit: cover;
  transition: transform 0.15s;
}

.image-preview:hover .image-thumb {
  transform: scale(1.02);
}

/* ── 文件消息 ─────────────────────────────────────────── */
.file-attachment {
  padding: 10px 14px;
  background: rgba(255,255,255,0.08);
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 180px;
  max-width: 300px;
  cursor: pointer;
  transition: background 0.2s;
}

.file-attachment:hover {
  background: rgba(255,255,255,0.15);
}

.file-message.mine .file-attachment {
  background: rgba(255,255,255,0.15);
}

.file-info {
  flex: 1;
  min-width: 0;
}

.file-name {
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-primary);
}

.file-size {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.file-download-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 18px;
  padding: 4px;
  opacity: 0.7;
  transition: opacity 0.2s;
}

.file-download-btn:hover {
  opacity: 1;
}

/* ── Modal Overlay ────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  z-index: 1002;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: 360px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-content h3 {
  margin-bottom: 12px;
  font-size: 16px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 16px;
}

/* ── 群组信息 ─────────────────────────────────────────── */
.group-info-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 4px;
}

.group-info-meta {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.group-members-list {
  max-height: 300px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.group-member-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.group-member-item:last-child {
  border-bottom: none;
}

.member-avatar {
  font-size: 24px;
}

.member-info {
  flex: 1;
}

.member-name {
  font-weight: 500;
}

.member-role {
  font-size: 12px;
  color: var(--text-secondary);
}

.btn-remove-member {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  padding: 4px;
  opacity: 0.6;
  transition: opacity 0.2s;
}

.btn-remove-member:hover {
  opacity: 1;
}

.btn-danger {
  background: #e74c3c;
  color: white;
  border: none;
  border-radius: var(--radius);
  padding: 6px 14px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.2s;
}

.btn-danger:hover {
  background: #c0392b;
}

/* ── 聊天输入区附件按钮 ─────────────────────────────── */
.input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
}

#attach-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 20px;
  padding: 4px 8px;
  opacity: 0.7;
  transition: opacity 0.2s;
  flex-shrink: 0;
}

#attach-btn:hover {
  opacity: 1;
}

#message-input {
  flex: 1;
  resize: none;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  min-height: 38px;
  max-height: 120px;
  line-height: 1.4;
}

#message-input:focus {
  outline: none;
  border-color: var(--accent);
}

/* BUG-20261027-03: 不用 display:none。
   iOS Safari 对 display:none 的文件输入框存在长期行为差异——选完文件后 change
   事件可能不触发，导致「选了图但没反应」。
   改为「仍在渲染树内、但视觉上不可见」：iOS 能正常触发 change，用户也看不到它。 */
#file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: 0;
  border: 0;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ── 语音消息按钮 ─────────────────────────────── */
.voice-record-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 20px;
  padding: 4px 8px;
  opacity: 0.7;
  transition: opacity 0.2s, color 0.2s, transform 0.1s;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: none;
}

.voice-record-btn:hover {
  opacity: 1;
}

.voice-record-btn:active,
.voice-record-btn.recording {
  color: #ef4444;
  transform: scale(1.15);
  opacity: 1;
}

.voice-record-btn.cancel {
  color: #f59e0b;
  animation: voice-shake 0.3s ease-in-out infinite;
}

@keyframes voice-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(1.3); }
}

@keyframes voice-shake {
  0%, 100% { transform: translateX(0) scale(1.15); }
  25% { transform: translateX(-2px) scale(1.15); }
  75% { transform: translateX(2px) scale(1.15); }
}

.voice-record-overlay {
  transition: opacity 0.2s;
}

.voice-record-overlay.hidden {
  opacity: 0;
  pointer-events: none !important;
  display: none !important;
}

/* ── 语音消息气泡 ─────────────────────────────── */
/* ── P2-2 通话记录气泡 ─────────────────────────────────── */
.message-bubble.call-record {
  max-width: 260px;
}

.message-bubble.call-record .bubble-content {
  background: rgba(128,128,128,0.16);
  border: 1px solid rgba(128,128,128,0.22);
}

.call-record-body {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
}

.call-record-icon {
  font-size: 16px;
  line-height: 1;
  flex-shrink: 0;
}

.call-record-label {
  font-size: 13px;
  opacity: 0.85;
  white-space: nowrap;
}

/* 未接通 / 已拒绝：加一点警示色，和普通消息区分开 */
.message-bubble.call-record.missed .call-record-label,
.message-bubble.call-record.rejected .call-record-label {
  color: #e5534b;
}

.voice-bubble {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 160px;
  padding: 4px 0;
}

.voice-play-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: inherit;
  transition: background 0.2s, transform 0.1s;
  flex-shrink: 0;
}

.voice-play-btn:hover {
  background: rgba(255,255,255,0.25);
}

.voice-play-btn:active {
  transform: scale(0.95);
}

.voice-play-btn.playing {
  background: var(--accent);
  color: #fff;
}

.voice-wave {
  flex: 1;
  height: 24px;
  display: flex;
  align-items: center;
  gap: 2px;
}

.voice-wave-bar {
  width: 2px;
  background: currentColor;
  opacity: 0.4;
  border-radius: 1px;
  transition: opacity 0.2s;
}

.voice-wave-bar.active {
  opacity: 1;
}

.voice-duration {
  font-size: 12px;
  opacity: 0.7;
  flex-shrink: 0;
}

/* ── 会话列表在线状态点 ─────────────────────────────── */
.avatar-wrap {
  position: relative;
  display: inline-block;
  flex-shrink: 0;
}

.status-dot {
  position: absolute;
  bottom: -1px;
  right: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--bg-secondary);
  background: #6b7280;  /* 灰 - 离线默认 */
  transition: background 0.3s;
}

.status-dot.online {
  background: #22c55e;
  box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.3);
}

.status-dot.offline {
  background: #6b7280;
}

/* ── 聊天头部状态文字 ─────────────────────────────── */
.chat-header-status {
  font-size: 12px;
  opacity: 0.7;
  margin-top: 2px;
}

.chat-header-status.online {
  color: #22c55e;
  opacity: 1;
}

/* ── P2-2 双方归档：会话项右键菜单 ─────────────────── */
.conv-context-menu {
  position: fixed;
  z-index: 3000;
  min-width: 160px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  padding: 4px 0;
}
.conv-context-menu .ctx-menu-item {
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  color: var(--text-primary, inherit);
}
.conv-context-menu .ctx-menu-item:hover {
  background: var(--bg-hover);
}

/* ── P2-2 双方归档：已归档会话列表 ─────────────────── */
#archived-list { max-height: 260px; overflow-y: auto; margin-bottom: 12px; }
.archived-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
}
.archived-item:last-child { border-bottom: none; }
.archived-name { font-size: 14px; }

/* 注册表单：强制所有字段可见（去掉 display:flex，避免与 .hidden 隐藏规则冲突） */
#register-form .form-group label,
#register-form input {
  visibility: visible !important;
}
#register-form input {
  width: 100%;
  box-sizing: border-box;
  min-height: 40px;
}
#register-form { flex-direction: column; }
