:root {
  --green: #07c160;
  --green-dark: #06ad56;
  --chat-bg: #ededed;
  --text: #191919;
  --muted: #b2b2b2;
  --border: #d9d9d9;
  --bubble-peer: #ffffff;
  --bubble-me: #95ec69;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--chat-bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.view { display: none; height: 100%; }
.view.active { display: flex; }

/* ========== 计算器（伪装外壳，保持 iOS 原生感，勿动）========== */
#calcView {
  background: #000; align-items: flex-end; justify-content: center;
}
.calc {
  width: 100%; max-width: 440px; height: 100%;
  display: flex; flex-direction: column; justify-content: flex-end;
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
}
.calc-display {
  color: #fff; font-size: 76px; font-weight: 200; text-align: right;
  padding: 0 24px 20px; line-height: 1.1;
  overflow: hidden; white-space: nowrap;
  min-height: 110px; display: flex; align-items: flex-end; justify-content: flex-end;
}
.calc-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; padding: 0 16px;
}
.calc-key {
  aspect-ratio: 1; border-radius: 50%; border: none;
  font-size: 32px; font-weight: 400; cursor: pointer;
  background: #333; color: #fff; transition: filter 0.1s, background 0.1s;
  display: flex; align-items: center; justify-content: center;
  user-select: none;
}
.calc-key:active { filter: brightness(1.5); }
.calc-key.zero { grid-column: span 2; aspect-ratio: auto; border-radius: 60px; justify-content: flex-start; padding-left: 32px; }
.calc-key.fn { background: #a5a5a5; color: #000; }
.calc-key.op { background: #ff9f0a; color: #fff; font-size: 36px; }
.calc-key.op:active { background: #ffb340; }

/* ========== 弹窗（微信风格）========== */
.modal {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  align-items: center; justify-content: center; z-index: 500; padding: 24px;
}
.modal.show { display: flex; }
.modal-box {
  background: #fff; border-radius: 12px; padding: 26px 24px 20px;
  width: 100%; max-width: 320px;
  box-shadow: 0 18px 50px rgba(0,0,0,0.22);
  animation: popUp 0.22s cubic-bezier(0.2, 1.1, 0.4, 1);
}
@keyframes popUp {
  from { opacity: 0; transform: scale(0.92); }
  to   { opacity: 1; transform: none; }
}
.modal-box h3 { font-size: 17px; margin-bottom: 18px; font-weight: 600; text-align: center; }
/* 头像编辑 */
.avatar-edit-row { display: flex; flex-direction: column; align-items: center; gap: 7px; margin-bottom: 18px; }
.avatar-edit {
  width: 66px; height: 66px; border-radius: 14px; overflow: hidden;
  background: #7d8bff; color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 30px; font-weight: 600;
  cursor: pointer; user-select: none;
  transition: opacity 0.15s;
}
.avatar-edit:active { opacity: 0.7; }
.avatar-edit img { width: 100%; height: 100%; object-fit: cover; display: block; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions > * { flex: 1; }
.btn-ghost {
  background: #f2f2f2; color: #191919; border: none; border-radius: 8px;
  padding: 12px; font-size: 15px; cursor: pointer; font-weight: 500;
  transition: background 0.15s;
}
.btn-ghost:active { background: #e6e6e6; }

/* ========== 登录/注册页（微信风格：白底 + 绿色主按钮）========== */
#authView {
  align-items: center;
  justify-content: center;
  background: #f7f7f7;
  overflow-y: auto;
  padding: 20px;
}
.auth-container { width: 100%; max-width: 400px; }
.brand { text-align: center; margin-bottom: 34px; }
.brand-icon {
  width: 76px; height: 76px; margin: 0 auto 16px;
  background: var(--green);
  border-radius: 18px;
  display: flex; align-items: center; justify-content: center;
  font-size: 38px;
  box-shadow: 0 6px 18px rgba(7,193,96,0.28);
}
.brand h1 { font-size: 26px; letter-spacing: 2px; margin-bottom: 6px; font-weight: 600; }
.brand p { color: var(--muted); font-size: 13px; }

.auth-box {
  background: #fff;
  border-radius: 12px; padding: 24px 22px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.05);
}
.tabs { display: flex; margin-bottom: 22px; border-bottom: 1px solid #ebebeb; }
.tab {
  flex: 1; padding: 12px 0; border: none; background: none;
  font-size: 15px; color: #9b9b9b; cursor: pointer;
  position: relative; font-weight: 500;
  transition: color 0.2s;
}
.tab::after {
  content: ''; position: absolute; left: 50%; bottom: -1px;
  width: 0; height: 2.5px; background: var(--green);
  transform: translateX(-50%); transition: width 0.22s; border-radius: 2px;
}
.tab.active { color: var(--text); }
.tab.active::after { width: 32px; }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form.hidden { display: none; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; color: #888; font-weight: 500; }
.field input {
  border: 1px solid #e2e2e2; border-radius: 8px;
  padding: 12px 14px; font-size: 15px; outline: none;
  background: #fafafa;
  transition: border-color 0.2s, background 0.2s;
}
.field input:focus {
  border-color: var(--green);
  background: #fff;
}
.hint { color: #b8b8b8; font-size: 11px; line-height: 1.4; }
.btn-primary {
  background: var(--green); color: #fff; border: none;
  border-radius: 8px; padding: 13px; font-size: 16px;
  font-weight: 600; cursor: pointer; margin-top: 4px;
  transition: opacity 0.15s, transform 0.15s;
}
.btn-primary:active { opacity: 0.85; transform: scale(0.99); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.auth-error { color: #e64340; font-size: 13px; text-align: center; min-height: 18px; margin-top: 8px; font-weight: 500; }
.auth-notice { color: var(--muted); font-size: 12px; text-align: center; line-height: 1.5; }

.security-note {
  display: flex; gap: 12px; margin-top: 20px; padding: 14px 16px;
  background: #fff;
  border-radius: 10px;
}
.security-note > span { font-size: 22px; flex-shrink: 0; }
.security-note strong { font-size: 13px; color: var(--green-dark); }
.security-note p { font-size: 11px; color: #999; margin-top: 4px; line-height: 1.55; }

/* ========== 聊天页（微信风格）========== */
#chatView {
  flex-direction: column;
  position: relative;
  background: var(--chat-bg);
}

/* --- 顶栏：微信样式（同色背景 + 底部细线 + 居中标题）--- */
.chat-header {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  height: calc(54px + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  background: var(--chat-bg);
  border-bottom: 1px solid var(--border);
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding-left: 14px; padding-right: 8px;
}
.header-side { display: flex; align-items: center; gap: 4px; }
.header-side.right { justify-content: flex-end; }
.header-title-wrap {
  display: flex; flex-direction: column; align-items: center;
  min-width: 0; padding: 0 8px;
}
.header-title-wrap h2 {
  font-size: 17px; font-weight: 600; color: var(--text);
  max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.status { font-size: 10px; color: var(--muted); font-weight: 400; line-height: 1.3; }
.status.online { color: var(--green); }
.lock-badge { font-size: 15px; opacity: 0.75; }
.icon-btn {
  border: none; background: none; color: #191919;
  font-size: 20px; cursor: pointer; padding: 8px 10px;
  border-radius: 8px;
  transition: background 0.15s, opacity 0.15s;
}
.icon-btn:active { background: rgba(0,0,0,0.06); }

/* --- 消息区 --- */
.messages {
  position: absolute; inset: 0;
  overflow-y: auto;
  padding: calc(64px + env(safe-area-inset-top)) 12px calc(72px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.messages::-webkit-scrollbar { display: none; }
.empty-state {
  margin: auto; text-align: center; padding: 40px 24px;
}
.empty-icon { font-size: 48px; margin-bottom: 14px; }
.empty-state p { font-size: 14px; margin-bottom: 6px; color: #999; font-weight: 500; }
.empty-state span { font-size: 12px; color: var(--muted); }

/* --- 消息结构：.msg > 时间(居中) + .msg-row(头像+气泡) --- */
.msg { display: flex; flex-direction: column; animation: msgIn 0.25s ease-out; }
.msg.me { align-items: flex-end; }
@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}
.msg-time {
  align-self: center;
  font-size: 12px; color: var(--muted);
  margin: 14px 0 2px;
  font-weight: 400;
}
.msg-row {
  display: flex; align-items: flex-start; gap: 10px;
  max-width: 80%;
  margin-top: 10px;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
.msg.me .msg-row { flex-direction: row-reverse; }
.msg-avatar {
  width: 40px; height: 40px; border-radius: 5px; flex-shrink: 0;
  background: #7d8bff;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 600; color: #fff;
}
img.msg-avatar { object-fit: cover; display: block; }
.msg.me .msg-avatar { background: var(--green); }

/* --- 气泡（微信样式：白色 / 微信绿 + 小三角）--- */
.bubble {
  position: relative;
  margin-top: 3px;
  padding: 10px 13px;
  border-radius: 8px;
  background: var(--bubble-peer);
  font-size: 16px; line-height: 1.5;
  word-break: break-word; max-width: 100%;
  color: var(--text);
}
.bubble::before {
  content: '';
  position: absolute; top: 13px; left: -5px;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-right: 5px solid var(--bubble-peer);
}
.msg.me .bubble { background: var(--bubble-me); }
.msg.me .bubble::before {
  left: auto; right: -5px;
  border-right-color: transparent;
  border-left: 5px solid var(--bubble-me);
}
/* 图片消息：无气泡底色 */
.bubble.pic { background: none; padding: 0; margin-top: 0; border-radius: 6px; overflow: hidden; }
.bubble.pic::before { display: none; }
.bubble.pic img { max-width: 200px; border-radius: 6px; display: block; }

/* --- 输入区（微信样式）--- */
.composer {
  position: absolute; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  background: #f7f7f7;
  border-top: 1px solid var(--border);
}
.attach-btn {
  font-size: 26px; padding: 5px 7px; flex-shrink: 0;
  color: #191919; font-weight: 300;
  line-height: 38px;
}
#msgInput {
  flex: 1; border: none; border-radius: 6px;
  padding: 10px 12px; font-size: 16px; resize: none; outline: none;
  max-height: 110px; line-height: 1.4; font-family: inherit;
  background: #fff; color: var(--text);
}
.send-btn {
  height: 38px; padding: 0 16px;
  background: var(--green);
  color: #fff; border: none;
  border-radius: 6px;
  font-size: 15px; font-weight: 500;
  cursor: pointer; flex-shrink: 0;
  transition: opacity 0.15s, transform 0.1s;
}
.send-btn:active { opacity: 0.8; transform: scale(0.97); }
.emoji-btn { font-size: 24px; padding: 6px 4px; line-height: 38px; }
.voice-toggle { font-size: 22px; padding: 5px 6px; line-height: 38px; }
/* 按住说话按钮 */
.hold-talk {
  flex: 1; height: 40px; border: none; border-radius: 6px;
  background: #fff; color: #191919; font-size: 15px; font-weight: 500;
  cursor: pointer; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none; touch-action: none;
  display: none; align-items: center; justify-content: center;
}
.composer.voice-mode #msgInput { display: none; }
.composer.voice-mode .hold-talk { display: flex; }
.composer.voice-mode .emoji-btn { display: none; }
.hold-talk.recording { background: #d9d9d9; color: #666; }

/* 录音浮层 */
.voice-overlay {
  display: none; position: fixed; inset: 0; z-index: 1000;
  align-items: center; justify-content: center; flex-direction: column;
  background: rgba(0,0,0,0.35);
}
.voice-overlay.show { display: flex; }
.voice-rec-box {
  background: rgba(40,40,40,0.92); border-radius: 16px;
  width: 160px; height: 160px; color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
  transition: background 0.15s;
}
.voice-overlay.cancelling .voice-rec-box { background: rgba(200,60,60,0.94); }
.voice-mic { font-size: 46px; line-height: 1; }
.voice-tip { font-size: 13px; margin-top: 14px; opacity: 0.9; }
.voice-wave { display: flex; gap: 4px; align-items: center; height: 24px; margin-top: 10px; }
.voice-wave span {
  display: block; width: 3px; background: #fff; border-radius: 2px;
  height: 6px; animation: wave 0.6s ease-in-out infinite alternate;
}
.voice-wave span:nth-child(2) { animation-delay: 0.1s; }
.voice-wave span:nth-child(3) { animation-delay: 0.2s; }
.voice-wave span:nth-child(4) { animation-delay: 0.3s; }
.voice-wave span:nth-child(5) { animation-delay: 0.4s; }
.voice-wave span:nth-child(6) { animation-delay: 0.2s; }
.voice-wave span:nth-child(7) { animation-delay: 0.1s; }
@keyframes wave { from { height: 5px; } to { height: 20px; } }
.voice-cancel-tip {
  display: none; margin-top: 20px; color: #fff; font-size: 14px;
  background: rgba(200,60,60,0.94); padding: 10px 20px; border-radius: 8px;
}
.voice-overlay.cancelling .voice-cancel-tip { display: block; }
.voice-overlay.cancelling .voice-rec-box { opacity: 0.5; }

/* 语音消息气泡 */
.voice-bubble {
  display: flex; align-items: center; gap: 8px;
  min-width: 80px; cursor: pointer; user-select: none;
}
.voice-play { font-size: 16px; flex-shrink: 0; }
.voice-bars { display: flex; align-items: center; gap: 2px; height: 18px; flex: 1; }
.voice-bars i {
  display: block; width: 2px; border-radius: 2px; background: currentColor; opacity: 0.55;
}
.voice-bars i:nth-child(1) { height: 6px; }
.voice-bars i:nth-child(2) { height: 10px; }
.voice-bars i:nth-child(3) { height: 14px; }
.voice-bars i:nth-child(4) { height: 9px; }
.voice-bars i:nth-child(5) { height: 16px; }
.voice-bars i:nth-child(6) { height: 11px; }
.voice-bars i:nth-child(7) { height: 7px; }
.voice-bubble.playing .voice-bars i { animation: wave 0.6s ease-in-out infinite alternate; }
.voice-bubble.playing .voice-bars i:nth-child(2n) { animation-delay: 0.2s; }
.voice-dur { font-size: 12px; opacity: 0.7; flex-shrink: 0; }

/* --- 表情面板 --- */
.emoji-panel {
  display: none;
  position: absolute; left: 0; right: 0;
  bottom: calc(56px + env(safe-area-inset-bottom));
  height: 210px;
  overflow-y: auto;
  background: #f7f7f7;
  border-top: 1px solid var(--border);
  z-index: 20;
  padding: 10px 8px;
}
.emoji-panel.show { display: block; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; }
.emoji-item {
  border: none; background: none;
  font-size: 27px; padding: 7px 0;
  cursor: pointer; line-height: 1;
  border-radius: 8px;
  transition: background 0.1s;
}
.emoji-item:active { background: rgba(0,0,0,0.07); }

/* 消息长按菜单 */
.msg-menu-box {
  background: #fff; border-radius: 14px;
  width: 100%; max-width: 280px; overflow: hidden;
  animation: popUp 0.22s cubic-bezier(0.2, 1.1, 0.4, 1);
}
/* 提示音开关 */
.sound-field { flex-direction: row; align-items: center; justify-content: space-between; gap: 10px; }
.toggle-btn {
  min-width: 76px; padding: 8px 14px;
  border-radius: 16px; border: none;
  font-size: 14px; font-weight: 600; cursor: pointer;
  font-family: inherit;
}
.toggle-btn.on { background: rgba(7,193,96,0.12); color: #06ad56; }
.toggle-btn.off { background: #f0f0f0; color: #999; }
.menu-item {
  display: block; width: 100%; padding: 16px;
  border: none; background: #fff;
  font-size: 16px; text-align: center; color: #191919;
  border-bottom: 1px solid #ebebeb;
  cursor: pointer; font-family: inherit;
}
.menu-item:last-child { border-bottom: none; }
.menu-item.danger { color: #e64340; }
.menu-item.cancel { color: #888; }
.menu-item:active { background: #f5f5f5; }
.menu-item:disabled { color: #c2c2c2; }

/* Toast（微信黑底白字居中提示）*/
.toast {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(0.9);
  background: rgba(0,0,0,0.78);
  color: #fff; padding: 13px 22px;
  border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none;
  transition: all 0.22s; z-index: 999; max-width: 80%; text-align: center;
  font-weight: 400;
}
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }

@media (min-width: 600px) {
  #chatView { max-width: 520px; margin: 0 auto; }
  #authView { padding: 20px; }
}
