:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --bg: #f5f6fa;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --danger: #ef4444;
  --success: #10b981;
  --radius: 12px;
}

* { box-sizing: border-box; }

/* 确保 hidden 属性始终生效：类选择器的 display 规则会覆盖 UA 的 [hidden]{display:none}，
   这里用 !important 兜底，避免"隐藏"的弹窗/遮罩层仍然拦截点击。 */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ================= 登录/注册 ================= */
.auth-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #ec4899 100%);
}

.auth-card {
  background: var(--surface);
  border-radius: 20px;
  padding: 36px 32px;
  width: 380px;
  max-width: 90vw;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}

.brand { text-align: center; margin-bottom: 24px; }
.brand-logo { font-size: 40px; }
.brand h1 { font-size: 22px; margin: 8px 0 4px; }
.brand-sub { color: var(--text-muted); font-size: 13px; margin: 0; }

.auth-tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.auth-tab {
  flex: 1; padding: 10px; border: none; background: none;
  font-size: 15px; color: var(--text-muted); border-bottom: 2px solid transparent;
}
.auth-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

.auth-form { display: flex; flex-direction: column; gap: 6px; }
.auth-form label { font-size: 13px; color: var(--text-muted); margin-top: 8px; }
.auth-form input {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px;
  outline: none;
}
.auth-form input:focus { border-color: var(--primary); }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; }

.btn-primary {
  background: var(--primary); color: white; border: none; border-radius: 10px;
  padding: 11px; font-size: 15px; font-weight: 600; margin-top: 10px;
  transition: background .15s;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-outline {
  background: var(--surface); color: var(--primary); border: 1px solid var(--primary);
  border-radius: 8px; padding: 6px 10px; font-size: 13px; font-weight: 500;
  display: inline-flex; align-items: center; gap: 4px; position: relative;
}
.btn-outline:hover { background: #eef2ff; }

/* ================= 应用主体布局 ================= */
.app-screen { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: 300px; min-width: 260px; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
}

.my-profile { display: flex; align-items: center; gap: 10px; padding: 16px; border-bottom: 1px solid var(--border); }
.my-info { flex: 1; min-width: 0; }
.my-nickname { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.my-status { font-size: 12px; color: var(--success); }

.avatar {
  width: 42px; height: 42px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  color: white; font-weight: 700; font-size: 16px; flex-shrink: 0; background: var(--primary);
}
.avatar.small { width: 36px; height: 36px; font-size: 14px; }

.icon-btn {
  border: none; background: none; font-size: 18px; padding: 6px; border-radius: 8px; color: var(--text-muted);
}
.icon-btn:hover { background: var(--bg); }

.sidebar-tabs { display: flex; padding: 8px 12px 0; gap: 4px; }
.sidebar-tab {
  flex: 1; padding: 8px; border: none; background: none; border-radius: 8px; font-size: 14px; color: var(--text-muted);
}
.sidebar-tab.active { background: #eef2ff; color: var(--primary); font-weight: 600; }

.sidebar-toolbar { display: flex; gap: 6px; padding: 10px 12px; flex-wrap: wrap; }

.badge {
  background: var(--danger); color: white; font-size: 10px; border-radius: 999px;
  padding: 1px 6px; margin-left: 4px; position: relative; top: -1px;
}

.list-panel { flex: 1; overflow-y: auto; padding: 4px 8px; }

.list-item {
  display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 10px; cursor: pointer;
  position: relative;
}
.list-item:hover { background: var(--bg); }
.list-item.active { background: #eef2ff; }
.list-item-info { flex: 1; min-width: 0; }
.list-item-name { font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item-sub { font-size: 12px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.online-dot {
  width: 9px; height: 9px; border-radius: 50%; background: #d1d5db; border: 2px solid var(--surface);
  position: absolute; left: 32px; bottom: 8px;
}
.online-dot.online { background: var(--success); }
.empty-hint { color: var(--text-muted); font-size: 13px; text-align: center; padding: 24px 12px; }

/* ================= 聊天区域 ================= */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-muted);
}
.chat-empty-icon { font-size: 48px; margin-bottom: 8px; }

.chat-window { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.chat-header {
  display: flex; align-items: center; justify-content: space-between; padding: 12px 20px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.chat-header-info { display: flex; align-items: center; gap: 10px; }
.chat-title { font-weight: 600; font-size: 15px; }
.chat-subtitle { font-size: 12px; color: var(--text-muted); }
.chat-actions { display: flex; gap: 4px; }

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

.msg-row { display: flex; gap: 8px; max-width: 70%; }
.msg-row.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar { width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0; }
.msg-body { display: flex; flex-direction: column; gap: 2px; }
.msg-sender-name { font-size: 11px; color: var(--text-muted); margin-left: 4px; }
.msg-bubble {
  background: var(--surface); border: 1px solid var(--border); padding: 9px 13px; border-radius: 14px;
  font-size: 14px; line-height: 1.5; word-break: break-word; white-space: pre-wrap;
}
.msg-row.mine .msg-bubble { background: var(--primary); color: white; border-color: var(--primary); }
.msg-row.mine .msg-sender-name { text-align: right; margin-right: 4px; }
.msg-time { font-size: 10px; color: var(--text-muted); margin: 0 4px; }
.msg-row.mine .msg-time { text-align: right; }

.msg-image { max-width: 240px; border-radius: 10px; display: block; cursor: pointer; }
.msg-file {
  display: flex; align-items: center; gap: 8px; text-decoration: none; color: inherit;
  background: var(--bg); padding: 8px 12px; border-radius: 10px; font-size: 13px;
}
.msg-row.mine .msg-file { background: rgba(255,255,255,0.18); }
.msg-file-icon { font-size: 20px; }

.system-msg { align-self: center; font-size: 12px; color: var(--text-muted); background: var(--bg); padding: 4px 12px; border-radius: 999px; }

.typing-indicator { min-height: 20px; padding: 0 20px; font-size: 12px; color: var(--text-muted); }

.composer {
  display: flex; align-items: center; gap: 6px; padding: 10px 16px; border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer #message-input {
  flex: 1; border: 1px solid var(--border); border-radius: 20px; padding: 10px 16px; font-size: 14px; outline: none;
}
.composer #message-input:focus { border-color: var(--primary); }
.btn-send { border-radius: 20px; padding: 10px 18px; margin: 0; }

/* ================= 弹窗 ================= */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal { background: var(--surface); border-radius: 16px; width: 420px; max-width: 90vw; max-height: 80vh; display: flex; flex-direction: column; overflow: hidden; }
.modal-header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); font-weight: 600; }
.modal-body { padding: 16px 20px; overflow-y: auto; }
.modal-body input[type="text"] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px; outline: none; margin-bottom: 8px;
}
.modal-body h4 { font-size: 13px; color: var(--text-muted); margin: 16px 0 6px; }

.search-results { display: flex; flex-direction: column; gap: 4px; min-height: 20px; }
.search-result-item {
  display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px;
}
.search-result-item:hover { background: var(--bg); }
.search-result-item .list-item-info { flex: 1; }
.search-result-item button {
  border: none; background: var(--primary); color: white; border-radius: 8px; padding: 5px 10px; font-size: 12px;
}
.search-result-item button.secondary { background: var(--bg); color: var(--text); }
.search-result-item button:disabled { background: var(--border); color: var(--text-muted); }
.picker-item { cursor: pointer; }
.picker-item.selected { background: #eef2ff; }

/* ================= 来电 / 通话 ================= */
.call-toast {
  position: fixed; top: 20px; right: 20px; background: var(--surface); border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2); padding: 14px 16px; display: flex; align-items: center; gap: 10px;
  z-index: 200; animation: slideIn .2s ease;
}
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.call-toast-avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--primary); }
.call-toast-info { margin-right: 6px; }
.call-toast-sub { font-size: 12px; color: var(--text-muted); }
.call-btn { border: none; border-radius: 50%; width: 40px; height: 40px; font-size: 16px; }
.call-btn.accept { background: var(--success); color: white; }
.call-btn.reject { background: var(--danger); color: white; }

.call-overlay {
  position: fixed; inset: 0; background: #111827; display: flex; flex-direction: column;
  align-items: center; justify-content: center; z-index: 300; color: white;
}
.call-status { position: absolute; top: 24px; font-size: 15px; color: #d1d5db; }
.video-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; width: 90%; max-width: 900px;
}
.video-tile { position: relative; background: #1f2937; border-radius: 14px; overflow: hidden; aspect-ratio: 4/3; }
.video-tile video { width: 100%; height: 100%; object-fit: cover; }
.video-tile .video-name { position: absolute; bottom: 8px; left: 10px; font-size: 12px; background: rgba(0,0,0,0.5); padding: 2px 8px; border-radius: 999px; }
.video-tile.audio-only { display: flex; align-items: center; justify-content: center; }
.video-tile.audio-only .avatar { width: 64px; height: 64px; font-size: 24px; }

.call-controls { position: absolute; bottom: 32px; display: flex; gap: 16px; }
.call-control-btn {
  width: 52px; height: 52px; border-radius: 50%; border: none; background: #374151; color: white; font-size: 20px;
}
.call-control-btn.muted { background: var(--danger); }
.call-control-btn.hangup { background: var(--danger); }

/* ================= Toast 提示 ================= */
.toast-container { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 400; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #111827; color: white; padding: 10px 18px; border-radius: 10px; font-size: 13px;
  animation: fadeUp .2s ease;
}
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.btn-back { display: none; }

/* ================= 响应式：移动端 ================= */
/* 用动态视口高度修复移动端浏览器地址栏伸缩导致的 100vh 溢出问题（不支持 dvh 的旧机型退回 vh）。 */
.auth-screen, .app-screen, .call-overlay { height: 100vh; height: 100dvh; }

@media (max-width: 760px) {
  .app-screen { position: relative; }
  .sidebar { width: 100%; position: absolute; inset: 0; z-index: 5; }
  .chat-area { width: 100%; }
  .app-screen.chat-open .sidebar { display: none; }
  .btn-back { display: inline-flex; }
  .msg-row { max-width: 82%; }
  .video-grid { grid-template-columns: 1fr; width: 96%; }

  /* 顶部资料区留出刘海/状态栏安全区 */
  .my-profile { padding-top: calc(14px + env(safe-area-inset-top)); }
  .chat-header { padding-top: calc(12px + env(safe-area-inset-top)); }

  /* 更大的点按目标，符合移动端触控 */
  .icon-btn { font-size: 20px; padding: 8px; }
  .sidebar-tab { padding: 11px 8px; font-size: 15px; }
  .list-item { padding: 12px 10px; }
  .avatar { width: 46px; height: 46px; }
  .online-dot { left: 36px; bottom: 10px; }

  /* 工具栏按钮在窄屏平铺换行更整齐 */
  .sidebar-toolbar { gap: 8px; }
  .sidebar-toolbar .btn-outline { flex: 1; justify-content: center; padding: 9px 8px; font-size: 14px; }

  /* 输入区与通话控制条避开底部手势条安全区 */
  .composer { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .btn-send { padding: 10px 14px; white-space: nowrap; flex-shrink: 0; }
  .composer #message-input { min-width: 0; }
  .call-controls { bottom: calc(24px + env(safe-area-inset-bottom)); }
  .call-status { top: calc(16px + env(safe-area-inset-top)); }

  /* 弹窗在手机上贴到底部，更像原生 sheet，且不超出屏幕 */
  .modal-backdrop { align-items: flex-end; }
  .modal { width: 100%; max-width: 100%; max-height: 88vh; border-radius: 18px 18px 0 0; }
  .modal-body { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

  /* 来电提示条改为顶部横幅，避免遮挡且更醒目 */
  .call-toast { top: calc(10px + env(safe-area-inset-top)); left: 10px; right: 10px; }

  .vault-body { padding: 16px; }
}

/* 触屏设备：输入框字号 ≥16px，防止 iOS Safari 聚焦时自动放大页面 */
@media (max-width: 760px), (pointer: coarse) {
  input, textarea, .composer #message-input,
  .auth-form input, .modal-body input[type="text"],
  .vault-input, .vault-textarea { font-size: 16px; }
}

/* ================= 端到端加密版新增样式 ================= */
.btn-text { background: none; border: none; color: var(--text-muted); font-size: 12px; margin-top: 8px; text-decoration: underline; }
.auth-hint { font-size: 13px; color: var(--text-muted); background: var(--bg); padding: 10px 12px; border-radius: 10px; line-height: 1.6; margin-bottom: 8px; }
.auth-hint b { color: var(--danger); }

.chat-empty-sub { font-size: 12px; color: var(--text-muted); max-width: 320px; text-align: center; margin-top: 4px; }
.modal-desc { font-size: 13px; color: var(--text-muted); line-height: 1.6; margin: 0 0 10px; }

.invite-address { font-size: 12px; color: var(--text-muted); margin-bottom: 8px; word-break: break-all; }
.invite-address code, .modal-body code { background: var(--bg); padding: 2px 6px; border-radius: 6px; font-size: 12px; }

.add-preview { background: #eef2ff; border-radius: 10px; padding: 10px 12px; font-size: 13px; margin: 8px 0; }

/* 加解密工具 */
.vault-window { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.vault-body { flex: 1; overflow-y: auto; padding: 20px; max-width: 720px; width: 100%; margin: 0 auto; }
.vault-note { font-size: 13px; color: var(--text-muted); background: var(--bg); border-radius: 10px; padding: 12px 14px; line-height: 1.7; margin-bottom: 16px; }
.vault-note code { background: #fff; padding: 1px 5px; border-radius: 5px; }
.vault-label { display: block; font-size: 13px; color: var(--text-muted); margin: 12px 0 4px; }
.vault-input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px; outline: none; }
.vault-input:focus { border-color: var(--primary); }
.vault-textarea { width: 100%; min-height: 110px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 13px; outline: none; resize: vertical; font-family: ui-monospace, Menlo, Consolas, monospace; line-height: 1.5; }
.vault-textarea:focus { border-color: var(--primary); }
.vault-tabs { display: flex; gap: 6px; margin: 16px 0 8px; }
.vault-tab { padding: 7px 16px; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; font-size: 13px; color: var(--text-muted); }
.vault-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.vault-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.vault-actions button { margin: 0; }
.vault-file-status { font-size: 13px; color: var(--success); margin-top: 10px; min-height: 18px; }
.vault-side { display: flex; align-items: center; justify-content: center; text-align: center; }

.call-toast-avatar { display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 700; }

/* ================= 每条消息的口令锁 ================= */
.lock-toggle.active { background: #fef3c7; color: #d97706; }
.lock-bar {
  display: flex; align-items: center; gap: 8px; padding: 8px 16px;
  background: #fffbeb; border-top: 1px solid #fde68a;
}
.lock-bar-icon { font-size: 15px; }
.lock-bar-input {
  flex: 1; min-width: 0; border: 1px solid #fcd34d; border-radius: 8px; padding: 7px 10px;
  font-size: 14px; outline: none; background: #fff;
}
.lock-bar-input:focus { border-color: #f59e0b; }
.lock-bar-hint { font-size: 11px; color: #b45309; white-space: nowrap; }

/* 加锁消息气泡 */
.locked-msg {
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  background: #fffbeb; border: 1px dashed #f59e0b; color: #92400e;
  padding: 9px 13px; border-radius: 14px; font-size: 14px;
}
.msg-row.mine .locked-msg { background: rgba(255,255,255,0.16); border-color: rgba(255,255,255,0.6); color: #fff; }
.locked-msg .lock-emoji { font-size: 16px; }
.locked-badge { font-size: 11px; opacity: .75; margin: 0 4px; }
.msg-row.mine .locked-badge { text-align: right; }

@media (max-width: 760px) {
  .lock-bar-hint { display: none; }  /* 手机窄屏隐藏长提示，保留输入框 */
}

/* ================= 定时销毁 ================= */
.burn-toggle.active { background: #fee2e2; color: #dc2626; }
.burn-bar {
  display: flex; align-items: center; gap: 14px; padding: 8px 16px; flex-wrap: wrap;
  background: #fef2f2; border-top: 1px solid #fecaca;
}
.burn-field { font-size: 13px; color: #b91c1c; display: flex; align-items: center; gap: 6px; }
.burn-field select {
  border: 1px solid #fca5a5; border-radius: 8px; padding: 5px 8px; font-size: 13px; background: #fff;
  color: var(--text); outline: none;
}
.burn-field select:focus { border-color: #ef4444; }
.burn-badge { font-size: 11px; color: #dc2626; opacity: .85; margin: 0 4px; display: flex; align-items: center; gap: 3px; }
.msg-row.mine .burn-badge { justify-content: flex-end; color: #fecaca; }
.burned-tombstone {
  align-self: center; font-size: 12px; color: var(--text-muted); background: #f3f4f6;
  padding: 5px 14px; border-radius: 999px; display: flex; align-items: center; gap: 5px;
}

/* ================= 我的资料 ================= */
#open-profile { cursor: pointer; }
#open-profile:hover .my-nickname { text-decoration: underline; }
.profile-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.profile-label { font-size: 13px; color: var(--text-muted); flex-shrink: 0; }
.profile-val { font-size: 14px; font-weight: 600; text-align: right; word-break: break-all; }
.profile-addr { font-weight: 400; font-size: 12px; font-family: monospace; color: var(--text-muted); }

/* ================= 密码显示/隐藏 眼睛 ================= */
.pw-wrap { position: relative; display: block; width: 100%; }
.lock-bar .pw-wrap { flex: 1; min-width: 0; }
.pw-wrap input { width: 100%; padding-right: 40px; }
.pw-eye {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 16px; line-height: 1;
  padding: 4px; opacity: .55; border-radius: 6px;
}
.pw-eye:hover { opacity: 1; background: rgba(0,0,0,0.05); }
