/* EdgeFT · 消息内容组件（message-list / message-bubble / file-card / file.ts 渲染）
   iOS 27 Messages 风格：气泡连续圆角、收发双方语义色、发送出动画、
   文件卡玻璃化、上传进度内联反馈。
   容器与滚动（.chat-box / #view-chat）见 layout.css。 */

/* ---------- 日期分隔 / 历史加载 ---------- */
.date-divider {
  text-align: center;
  color: var(--muted);
  font-size: var(--font-micro);
  font-weight: 600;
  letter-spacing: 0.5px;
  margin: 16px 0 12px;
}
.hist-loading,
.hist-end {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
  font-size: var(--font-caption);
  padding: 12px 0;
}
.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--ios-separator);
  border-top-color: var(--ios-tertiary-label);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- 消息行 ---------- */
.chat-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 12px;
  animation: msg-in 0.3s var(--ease-spring) both;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: none; }
}
/* 历史回填消息不逐条弹入（仅最新一条有动画意义，重渲染会全部重放；
   用 :last-child 限定最新消息） */
.chat-row:not(:last-child) {
  animation: none;
}
.chat-box.restoring .chat-row {
  animation: none !important;
}
.chat-box .chat-row.restored {
  animation: none;
}

/* ---------- 头像：40px 圆形色块 + 设备名首字母（JS 生成色值，见 util.ts） ---------- */
.chat-avatar {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-on-primary);
  font-size: var(--font-title);
  font-weight: 600;
  user-select: none;
  -webkit-user-select: none;
}

/* ---------- 气泡主体列（头像之外的 meta + 内容，宽度上限 70% 落在这层） ---------- */
.chat-body {
  display: flex;
  flex-direction: column;
  flex: 0 1 auto;
  min-width: 0;
  max-width: 70%;
  align-items: flex-start;
}
.chat-row.mine .chat-body {
  align-items: flex-end;
}

.chat-meta {
  font-size: var(--font-micro);
  color: var(--timestamp);
  margin: 0 4px 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}
/* 系统行（日期/提示类）居中弱色 */
.chat-row.system {
  display: flex;
  align-items: center;
  justify-content: center;
}
.chat-row.system .chat-text {
  background: none;
  color: var(--muted);
  font-size: var(--font-caption-2);
}

/* 发送方 / 接收方 —— 对齐只写在这一层（Message 层），所有内容子元素继承 */
.chat-row.mine {
  flex-direction: row-reverse;
}
.chat-row.theirs {
  flex-direction: row;
}

/* ---------- 气泡（微信风格：己方绿底深字、对方白底发丝线，真三角发言角） ---------- */
.chat-text {
  position: relative; /* 三角伪元素锚点 */
  padding: 12px 16px;
  border-radius: var(--radius-bubble);
  font-size: var(--font-body);
  line-height: 1.4;
  word-break: break-word;
  white-space: pre-wrap;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.chat-row.theirs .chat-text {
  background: var(--panel);
  color: var(--text);
  border: 0.5px solid var(--ios-separator);
  border-bottom-left-radius: var(--radius-bubble-tail); /* 发言角 */
}
.chat-row.mine .chat-text {
  background: var(--wechat-green);
  color: var(--text);
  border-bottom-right-radius: var(--radius-bubble-tail);
}
/* 真三角（微信）：己方右下、对方左下 */
.chat-row.mine .chat-text::after {
  content: "";
  position: absolute;
  right: -8px;
  top: 14px;
  border: 8px solid transparent;
  border-left-color: var(--wechat-green);
}
.chat-row.theirs .chat-text::after {
  content: "";
  position: absolute;
  left: -8px;
  top: 14px;
  border: 8px solid transparent;
  border-right-color: var(--panel);
}
/* 连续多条同一方：尾条收发言角、前条圆角收敛（简化：仅最后一条带角） */
.chat-row.mine + .chat-row.mine .chat-text {
  border-top-right-radius: var(--radius-bubble-group);
}
.chat-row.theirs + .chat-row.theirs .chat-text {
  border-top-left-radius: var(--radius-bubble-group);
}

/* ---------- AI 成员气泡（@AI 触发，device="ai"；淡紫底区别于真人 theirs 白底） ----------
   AI 专属色未收录进 tokens.css（功能新增，独立语义色） */
.chat-row.ai .chat-avatar {
  background: #7c6ff0; /* AI 紫色头像块 */
}
.chat-row.ai .chat-text {
  background: #eae5ff;
  color: var(--text);
  border-color: #d5ccf5;
}
.chat-row.ai .chat-text::after {
  border-right-color: #eae5ff;
}

.msg-link {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ---------- 发送状态（紧贴气泡：发送中 / 已发送 / 点击重试） ---------- */
.msg-state {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  font-size: var(--font-micro);
  color: var(--timestamp);
  margin: 0 4px;
  background: transparent;
  cursor: default;
  -webkit-appearance: none;
  appearance: none;
}
.msg-state.failed {
  color: var(--ios-red);
  background: var(--ios-red-soft);
  font-size: var(--font-body);
  font-weight: 800;
  cursor: pointer;
  animation: msg-failed-pulse 1.25s ease-in-out infinite;
}
.msg-state.pending {
  border: 2px solid rgba(120, 120, 128, 0.22);
  border-top-color: var(--ios-tertiary-label);
  animation: spin 0.8s linear infinite;
}
.msg-state.sent {
  /* 指令：已发送不显示（微信无发送状态角标） */
  display: none;
}
.msg-state:focus-visible {
  outline: 2px solid var(--ios-blue);
  outline-offset: 2px;
}
.chat-content {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  width: 100%;
}
.chat-content .chat-text {
  flex: 0 1 auto;
  min-width: 0;
}
.chat-row.theirs .chat-content {
  flex-direction: row-reverse;
}
@keyframes msg-failed-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.14); }
}

/* ---------- 文件卡（接收方：白卡 + 柔影；己方：气泡蓝内白卡） ----------
   行层占满全宽，左右对齐由 .chat-row.mine/.theirs 的 align-items 统一控制；
   宽度上限落在内容层 .file-body，不限制行宽 */
.file-body {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-width: 0;
  padding: 12px;
  border: 0.5px solid var(--ios-separator);
  border-radius: var(--radius-card);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: transform var(--dur-fast) var(--ease-spring), opacity var(--dur-fast) var(--ease-out);
}
.file-download-card:active:not([aria-disabled="true"]) {
  transform: scale(0.985);
}
.file-download-card:focus-visible,
.file-image-preview:focus-visible {
  outline: 2px solid var(--ios-blue);
  outline-offset: 3px;
}
.file-image-preview {
  display: block;
  width: min(70vw, 420px);
  max-height: min(50vh, 500px);
  overflow: hidden;
  padding: 0;
  border: 0;
  border-radius: var(--radius-card);
  background: var(--ios-fill);
  color: var(--muted);
  font-size: var(--font-caption);
  cursor: zoom-in;
  -webkit-appearance: none;
  appearance: none;
}
.file-image-preview img {
  display: block;
  width: 100%;
  max-height: min(50vh, 500px);
  /* 保持原图比例、不裁剪（宽高比由元数据在 JS 侧设置，缺失时自然 contain） */
  object-fit: contain;
}
.file-image-preview.expired {
  min-height: 120px;
  display: grid;
  place-items: center;
  cursor: default;
}
/* 图片卡行占满全宽，内容由 .chat-row.mine/.theirs 的 align-items 统一对齐（与文本/文件卡一致） */

/* ---------- 图片上传消息（独立渲染类型：缩略图 + 中央环形进度，非文件卡片） ---------- */
.image-bubble {
  position: relative;
  width: min(70vw, 420px);
  max-height: min(50vh, 500px);
  border-radius: var(--radius-card);
  overflow: hidden;
  background: var(--ios-fill);
}
.image-bubble img {
  display: block;
  width: 100%;
  max-height: min(50vh, 500px);
  object-fit: contain;
}
/* 中央上传指示层：半透明遮罩 + 居中半透明圆环 + 百分比，淡入出现 */
.upload-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.22);
  pointer-events: none;
  z-index: 1;
  animation: upload-fade-in 0.25s ease-out both;
}
@keyframes upload-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.upload-ring {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55); /* 半透明圆底（微信风格纯色，无模糊） */
  transform: rotate(-90deg); /* 进度从顶部顺时针开始 */
}
.ring-track {
  fill: none;
  stroke: rgba(255, 255, 255, 0.25);
  stroke-width: 5;
}
.ring-bar {
  fill: none;
  stroke: var(--text-on-primary);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-dasharray: 263.9; /* 2π × r42 */
  stroke-dashoffset: 263.9;
  transition: stroke-dashoffset 0.3s cubic-bezier(0.22, 1, 0.36, 1);
  filter: drop-shadow(0 0 4px rgba(255, 255, 255, 0.55));
}
.upload-pct {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: var(--text-on-primary);
  font-size: 16px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}
/* 失败态：遮罩加深、环变红、感叹号 + 抖动 */
.upload-overlay.fail {
  background: rgba(0, 0, 0, 0.35);
  animation: upload-shake 0.3s ease both;
}
.upload-overlay.fail .ring-bar {
  stroke: var(--ios-red);
  filter: drop-shadow(0 0 5px rgba(255, 59, 48, 0.6));
}
.upload-overlay.fail .upload-pct {
  font-size: 17px;
  font-weight: 700;
}
@keyframes upload-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}
.chat-row.mine .file-body {
  border-color: var(--ios-separator);
}
.file-icon {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: var(--radius-control);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  background: var(--ios-fill);
  color: var(--ios-secondary-label);
  position: relative;
}
/* 类型彩色变体（与文件区/收藏页 .list-icon 同套软底 + 类型色） */
.file-icon.orange { background: var(--ios-orange-soft); color: var(--ios-orange); }
.file-icon.green  { background: var(--ios-green-soft);  color: var(--ios-green); }
.file-icon.red    { background: var(--ios-red-soft);    color: var(--ios-red); }
/* 已下载：半透明白层 + SVG 绿色对勾（与文件区/收藏页 .dl-mark 视觉统一，不依赖文字/额外图标） */
.file-icon.dl-done::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--ios-white-soft);
  pointer-events: none;
}
.file-icon.dl-done::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--ios-green);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='20 6 9 17 4 12'/%3E%3C/svg%3E") center / 32% no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23fff' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='20 6 9 17 4 12'/%3E%3C/svg%3E") center / 32% no-repeat;
  pointer-events: none;
}
.file-info {
  flex: 1;
  min-width: 0;
}
.file-name {
  font-size: var(--font-body);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-meta {
  font-size: var(--font-sub);
  color: var(--muted);
  margin-top: 4px;
}
/* 状态文字三色（微信语义）：未下载灰 / 已下载绿 / 已过期红 */
.file-status.idle { color: var(--ios-secondary-label); }
.file-status.downloaded { color: var(--ios-green); }
.file-status.expired { color: var(--ios-red); }
.file-meta.err {
  color: var(--ios-red);
}
.file-expired-tag {
  display: block;
  margin-top: 8px;
  color: var(--ios-red);
  font-size: var(--font-caption-2);
  font-weight: 600;
}
/* 过期文件卡整体降透明（与文件区/收藏页 .list-row.expired 一致） */
.file-card.expired .file-body,
.file-card.expired .file-image-preview {
  opacity: 0.5;
}
.file-download-card[aria-disabled="true"],
.file-image-preview[aria-disabled="true"] {
  cursor: default;
}

/* ---------- 上传进度卡（同一张卡走完：准备 → 上传中 → 完成/失败） ---------- */
/* 己方进度卡是浅蓝底（继承气泡蓝被覆盖为玻璃浅底），文字需深色系 */
.chat-row.mine .file-body.up {
  background: var(--panel);
  color: var(--text);
  border: 0.5px solid var(--ios-separator);
}
.upload-progress {
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--ios-fill);
  overflow: hidden;
  margin-top: 8px;
}
.upload-fill {
  height: 100%;
  border-radius: inherit;
  background: var(--ios-blue);
  transition: width 0.25s var(--ease-out);
}
.up-state {
  font-size: var(--font-micro);
  color: var(--muted);
  margin-top: 4px;
}
.up-state.err,
.file-body.err .up-state {
  color: var(--ios-red);
}
.up-percent {
  font-size: var(--font-micro);
  color: var(--ios-blue);
  font-variant-numeric: tabular-nums;
}

/* ---------- 传输记录卡（tx log） ---------- */
.tx-log-body {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-radius: var(--radius-group);
  background: var(--panel);
  border: 0.5px solid var(--ios-separator);
  font-size: var(--font-caption);
}
.tx-log-info {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tx-log-state.ok {
  color: var(--ios-green);
}
.tx-log-state.err {
  color: var(--ios-red);
}

/* ---------- 新消息浮标（iOS Messages "New Messages" 胶囊） ---------- */
.new-msg {
  position: sticky;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 8;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-pill);
  background: var(--panel);
  border: 0.5px solid var(--ios-separator);
  color: var(--ios-blue);
  font-size: var(--font-caption);
  font-weight: 600;
  cursor: pointer;
  animation: pill-in 0.3s var(--ease-spring) both;
}
.new-msg .nm-arrow,
.new-msg .nm-count {
  display: inline-flex;
  align-items: center;
}
@keyframes pill-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* 移动端：chatBox 是 fixed 全屏滚动层，而按钮是其兄弟节点——sticky 会粘附在不滚动的
   view-pane 上而失效。改 fixed 相对视口，浮在输入区与底部导航之上，
   并再抬高约 30px 留出舒适间隙（用户反馈原位置太靠下） */
@media (max-width: 720px) {
  .new-msg {
    position: fixed;
    bottom: calc(154px + var(--sab));
  }
}

/* ---------- 移动端微调：气泡更贴边、字号 iOS 化 ---------- */
@media (max-width: 720px) {
  .chat-text {
    font-size: 16px;
  }
  /* 行层不设宽限（对齐归 .chat-row.mine/.theirs）；文件卡内容宽跟随桌面规则，
     仅放开最小宽限制避免窄屏溢出 */
  .file-body {
    min-width: 0;
  }
}
