/* =========================================================
   im-h5 移动端聊天页样式
   设计语言：精致现代，柔和留白，轻量阴影，圆角气泡
   主色：#4F46E5（靛蓝）
   字体：Manrope（具几何感，可读性高，避免泛 AI 风格）
   ========================================================= */

/* ---------- 设计变量 ---------- */
:root {
  /* 品牌色 */
  --color-primary: #4F46E5;
  --color-primary-700: #4338CA;
  --color-primary-50: #EEF2FF;

  /* 中性色 */
  --color-bg: #FFFFFF;
  --color-bg-soft: #FAFAFB;
  --color-surface: #FFFFFF;
  --color-bubble-agent: #F3F4F6;
  --color-text: #0F172A;
  --color-text-muted: #64748B;
  --color-text-faint: #94A3B8;
  --color-border: #E5E7EB;
  --color-border-soft: #F1F5F9;

  /* 状态色 */
  --color-online: #10B981;
  --color-offline: #94A3B8;
  --color-error: #EF4444;
  --color-warning: #F59E0B;

  /* 排版 */
  --font-sans: "Manrope", -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  --fs-xs: 11px;
  --fs-sm: 13px;
  --fs-base: 15px;
  --fs-md: 16px;
  --fs-lg: 17px;

  /* 间距：基于 8dp 网格 */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;

  /* 圆角 */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 22px;
  --radius-full: 9999px;

  /* 阴影 */
  --shadow-topbar: 0 1px 0 rgba(15, 23, 42, 0.04), 0 8px 24px -16px rgba(15, 23, 42, 0.08);
  --shadow-bubble: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-card: 0 24px 64px -24px rgba(15, 23, 42, 0.18);

  /* 触控热区最小 44pt（移动端可用性） */
  --hit-min: 44px;
}

/* ---------- 基础重置 ---------- */
* {
  box-sizing: border-box;
}
*::before,
*::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  text-size-adjust: 100%;
  overscroll-behavior: none;
}

body {
  /* 防止 iOS 输入框滚动整页 */
  position: fixed;
  inset: 0;
  overflow: hidden;
}

button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

textarea, input {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  outline: none;
  resize: none;
  -webkit-appearance: none;
  appearance: none;
}

/* ---------- 应用根容器 ---------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background:
    radial-gradient(1200px 600px at 110% -20%, rgba(79, 70, 229, 0.06), transparent 60%),
    radial-gradient(900px 500px at -20% 110%, rgba(79, 70, 229, 0.04), transparent 60%),
    var(--color-bg);
}

/* ---------- 顶部导航栏 ---------- */
.topbar {
  position: relative;
  z-index: 5;
  flex-shrink: 0;
  background: var(--color-surface);
  box-shadow: var(--shadow-topbar);
  /* 适配 iOS 刘海屏 */
  padding-top: env(safe-area-inset-top);
}
.topbar__inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-2) var(--sp-4);
  min-height: 56px;
  gap: var(--sp-3);
}
.topbar__left {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-width: 0;
}
.topbar__title {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.topbar__name {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--color-text);
  line-height: 1.2;
}
.topbar__status {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-top: 2px;
}
.topbar__status-text {
  font-size: var(--fs-xs);
  color: var(--color-text-muted);
  letter-spacing: 0.01em;
}

/* 头像 */
.avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 20px;
  color: #fff;
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-700));
  box-shadow: 0 4px 12px -2px rgba(79, 70, 229, 0.35);
}
.avatar--agent-sm {
  width: 28px;
  height: 28px;
  font-size: 14px;
  box-shadow: none;
}

/* 状态指示灯 */
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  display: inline-block;
  flex-shrink: 0;
  position: relative;
}
.status-dot--online {
  background: var(--color-online);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.18);
}
.status-dot--online::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-online);
  opacity: 0.6;
  animation: pulse 1.8s ease-out infinite;
}
.status-dot--connecting {
  background: var(--color-warning);
  animation: blink 1.2s ease-in-out infinite;
}
.status-dot--offline {
  background: var(--color-offline);
}
.status-dot--error {
  background: var(--color-error);
}

@keyframes pulse {
  0%   { transform: scale(1);   opacity: 0.6; }
  100% { transform: scale(1.8); opacity: 0;   }
}
@keyframes blink {
  0%, 100% { opacity: 1;   }
  50%      { opacity: 0.3; }
}

/* 顶部图标按钮 */
.iconbtn {
  width: var(--hit-min);
  height: var(--hit-min);
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  color: var(--color-text-muted);
  transition: background-color 0.15s ease;
}
.iconbtn:active:not(:disabled) {
  background: var(--color-border-soft);
}
.iconbtn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* ---------- 消息区域 ---------- */
.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: var(--sp-4);
  scroll-behavior: smooth;
  /* 滚动条美化（iOS / 移动端默认隐藏） */
  scrollbar-width: thin;
  scrollbar-color: rgba(15, 23, 42, 0.15) transparent;
}
.messages::-webkit-scrollbar {
  width: 4px;
}
.messages::-webkit-scrollbar-thumb {
  background: rgba(15, 23, 42, 0.15);
  border-radius: var(--radius-full);
}
.messages__list {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

/* "加载历史"占位 */
.load-more {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: var(--sp-3) 0;
  font-size: var(--fs-xs);
  color: var(--color-text-muted);
}

/* 时间分隔标签 */
.time-divider {
  display: flex;
  justify-content: center;
  margin: var(--sp-4) 0 var(--sp-2);
}
.time-divider span {
  font-size: var(--fs-xs);
  color: var(--color-text-faint);
  background: rgba(241, 245, 249, 0.7);
  padding: 4px 10px;
  border-radius: var(--radius-full);
  letter-spacing: 0.02em;
}

/* 系统消息 */
.sysmsg {
  display: flex;
  justify-content: center;
  margin: var(--sp-2) 0;
}
.sysmsg span {
  font-size: var(--fs-xs);
  color: var(--color-text-muted);
  background: var(--color-border-soft);
  padding: 6px 12px;
  border-radius: var(--radius-full);
  max-width: 80%;
  text-align: center;
  line-height: 1.5;
}

/* ---------- 消息行 ---------- */
.msg {
  display: flex;
  align-items: flex-end;
  gap: var(--sp-2);
  max-width: 100%;
  animation: msg-in 0.28s cubic-bezier(0.2, 0.7, 0.3, 1.2);
}
.msg--agent {
  justify-content: flex-start;
}
.msg--visitor {
  justify-content: flex-end;
}
.msg__avatar {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, #E0E7FF, #C7D2FE);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--color-primary);
}
.msg__content {
  display: flex;
  flex-direction: column;
  max-width: 78%;
  min-width: 0;
}
.msg--visitor .msg__content {
  align-items: flex-end;
}
.msg--agent .msg__content {
  align-items: flex-start;
}

/* 气泡 */
.bubble {
  position: relative;
  padding: 10px 14px;
  font-size: var(--fs-base);
  line-height: 1.55;
  border-radius: var(--radius-lg);
  word-wrap: break-word;
  word-break: break-word;
  white-space: pre-wrap;
  box-shadow: var(--shadow-bubble);
  letter-spacing: -0.005em;
}
.bubble--agent {
  background: var(--color-bubble-agent);
  color: var(--color-text);
  border-bottom-left-radius: 6px;
}
.bubble--visitor {
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-700));
  color: #fff;
  border-bottom-right-radius: 6px;
}

/* 状态行（时间 + ACK） */
.msg__meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  padding: 0 4px;
  font-size: var(--fs-xs);
  color: var(--color-text-faint);
  line-height: 1;
}
.msg__time {
  white-space: nowrap;
}
.msg__ack {
  display: inline-flex;
  align-items: center;
  font-size: 13px;
  line-height: 1;
}
.msg__ack--sending {
  color: var(--color-text-faint);
  animation: spin 0.9s linear infinite;
}
.msg__ack--sent {
  color: var(--color-text-muted);
}
.msg__ack--read {
  color: var(--color-primary);
}
.msg__ack--failed {
  color: var(--color-error);
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0);   }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- 图片消息气泡 ---------- */
.bubble--image {
  padding: 4px;
  background: transparent !important;
  background-image: none !important;
  box-shadow: 0 2px 8px -2px rgba(15, 23, 42, 0.12);
  border-radius: var(--radius-md);
  border-bottom-left-radius: var(--radius-md) !important;
  border-bottom-right-radius: var(--radius-md) !important;
  position: relative;
  overflow: hidden;
  line-height: 0;
  max-width: 100%;
}
.bubble--image img {
  display: block;
  max-width: 220px;
  max-height: 220px;
  width: auto;
  height: auto;
  border-radius: 10px;
  cursor: zoom-in;
  background: var(--color-border-soft);
  object-fit: cover;
  transition: transform 0.18s ease;
}
.bubble--image img:active {
  transform: scale(0.98);
}
.bubble__img-error {
  display: inline-block;
  padding: 12px 16px;
  font-size: var(--fs-xs);
  color: var(--color-text-muted);
  background: var(--color-border-soft);
  border-radius: 10px;
  line-height: 1.5;
}
.bubble__upload-overlay {
  position: absolute;
  inset: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.42);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  border-radius: 10px;
  color: #fff;
  font-size: 26px;
  pointer-events: none;
}
.bubble__upload-overlay--failed {
  background: rgba(239, 68, 68, 0.55);
  color: #fff;
}
.loader--on-dark {
  border-color: rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
}

/* ---------- typing 指示器 ---------- */
.typing {
  display: flex;
  margin-top: var(--sp-2);
  padding-left: 36px; /* 与消息头像对齐 */
}
.typing__bubble {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 12px 16px;
}
.typing__dot {
  width: 6px;
  height: 6px;
  border-radius: var(--radius-full);
  background: var(--color-text-faint);
  animation: typing-bounce 1.2s ease-in-out infinite;
}
.typing__dot:nth-child(2) { animation-delay: 0.15s; }
.typing__dot:nth-child(3) { animation-delay: 0.30s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0);    opacity: 0.4; }
  30%           { transform: translateY(-4px); opacity: 1;   }
}

/* ---------- 输入区域 ---------- */
.composer {
  flex-shrink: 0;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border-soft);
  /* 适配 iOS 底部安全区 */
  padding-bottom: env(safe-area-inset-bottom);
}
.composer__inner {
  display: flex;
  align-items: flex-end;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
}
.composer__field {
  flex: 1;
  display: flex;
  align-items: center;
  background: var(--color-bg-soft);
  border: 1px solid var(--color-border-soft);
  border-radius: var(--radius-xl);
  padding: 4px 14px;
  min-height: var(--hit-min);
  transition: border-color 0.15s ease, background-color 0.15s ease;
}
.composer__field:focus-within {
  border-color: var(--color-primary);
  background: #fff;
}
.composer__textarea {
  flex: 1;
  width: 100%;
  font-size: var(--fs-md);
  line-height: 1.45;
  padding: 8px 0;
  max-height: 88px; /* 大约 3 行 */
  min-height: 24px;
  display: block;
  background: transparent;
  color: var(--color-text);
}
.composer__textarea::placeholder {
  color: var(--color-text-faint);
}

/* 附件按钮（左侧：发送图片入口） */
.btn-attach {
  flex-shrink: 0;
  width: var(--hit-min);
  height: var(--hit-min);
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  color: var(--color-text-muted);
  background: transparent;
  transition: background-color 0.15s ease, color 0.15s ease, transform 0.12s ease;
}
.btn-attach:hover {
  color: var(--color-primary);
  background: var(--color-primary-50);
}
.btn-attach:active {
  transform: scale(0.92);
  background: var(--color-primary-50);
  color: var(--color-primary);
}
.btn-attach:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* 发送按钮 */
.sendbtn {
  flex-shrink: 0;
  width: var(--hit-min);
  height: var(--hit-min);
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  color: #fff;
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-700));
  box-shadow: 0 6px 16px -4px rgba(79, 70, 229, 0.45);
  transition: transform 0.12s ease, opacity 0.15s ease, box-shadow 0.15s ease;
}
.sendbtn:active:not(:disabled) {
  transform: scale(0.92);
}
.sendbtn:disabled {
  background: #E5E7EB;
  color: #94A3B8;
  box-shadow: none;
  cursor: not-allowed;
}

/* ---------- 浮层（Loading / Error） ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(15, 23, 42, 0.32);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-6);
  animation: overlay-in 0.2s ease;
}
@keyframes overlay-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.overlay__card {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  padding: var(--sp-6) var(--sp-8);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-3);
  box-shadow: var(--shadow-card);
  min-width: 200px;
  text-align: center;
}
.overlay__text {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--color-text-muted);
}
.overlay__icon {
  font-size: 40px;
  color: var(--color-error);
  line-height: 1;
}
.overlay--error .overlay__card {
  gap: var(--sp-4);
}

/* Loading 旋转器 */
.loader {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  border: 3px solid var(--color-border-soft);
  border-top-color: var(--color-primary);
  animation: spin 0.8s linear infinite;
}
.loader--sm {
  width: 16px;
  height: 16px;
  border-width: 2px;
}

/* 通用按钮 */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  height: var(--hit-min);
  padding: 0 var(--sp-5);
  border-radius: var(--radius-full);
  font-size: var(--fs-sm);
  font-weight: 600;
  letter-spacing: -0.005em;
  transition: transform 0.12s ease, opacity 0.15s ease;
}
.btn:active {
  transform: scale(0.96);
}
.btn--primary {
  background: var(--color-primary);
  color: #fff;
  box-shadow: 0 6px 16px -4px rgba(79, 70, 229, 0.45);
}
.btn i {
  font-size: 16px;
}

/* ---------- 工具类 ---------- */
[hidden] {
  display: none !important;
}

/* ---------- 断线重连横幅 ---------- */
.bm-reconnect-bar {
  position: fixed; top: 0; left: 0; right: 0;
  padding: 8px 16px; text-align: center; font-size: 13px;
  z-index: 9999; transition: all 0.3s;
  font-weight: 500;
}
.bm-reconnect-bar.disconnected { background: #FEE2E2; color: #DC2626; }
.bm-reconnect-bar.reconnected { background: #D1FAE5; color: #059669; }

/* 大屏适配（在桌面浏览器查看时仍居中且不超宽） */
@media (min-width: 720px) {
  .app {
    max-width: 480px;
    margin: 0 auto;
    box-shadow: 0 24px 80px -32px rgba(15, 23, 42, 0.25);
  }
  body {
    background: #F1F5F9;
    position: static;
    overflow: auto;
    height: 100vh;
    display: flex;
    align-items: stretch;
    justify-content: center;
  }
}
