/* ============================================
   JustClaw 客服组件（优化版）
   轮询实时通信 + 状态指示器
   ============================================ */

/* ---------- 浮动按钮 ---------- */
.chat-float-btn {
  position: fixed; bottom: 24px; right: 24px; z-index: 900;
  display: flex; flex-direction: column; align-items: flex-end; gap: 12px;
}

.chat-status-bar {
  display: flex; align-items: center; gap: 8px;
  background: var(--card-bg, rgba(18,18,18,0.85));
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border, rgba(255,255,255,0.1));
  border-radius: 999px; padding: 6px 14px;
  font-size: 0.8rem; color: var(--text-muted, #a1a1aa);
  box-shadow: 0 4px 15px rgba(0,0,0,0.15); transition: all 0.3s ease;
  white-space: nowrap;
}

.chat-status-dot {
  width: 8px; height: 8px; border-radius: 50%; background: #888;
  transition: background 0.3s ease; flex-shrink: 0;
}
.chat-status-bar.online .chat-status-dot   { background: #10b981; box-shadow: 0 0 6px #10b981; animation: pulse-dot 2s ease-in-out infinite; }
.chat-status-bar.replying .chat-status-dot  { background: #f59e0b; box-shadow: 0 0 6px #f59e0b; animation: pulse-dot 1s ease-in-out infinite; }
.chat-status-bar.offline .chat-status-dot  { background: #ef4444; }

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

/* ---------- 主聊天按钮 ---------- */
.chat-main-btn {
  width: 56px; height: 56px; border-radius: 50%;
  background: linear-gradient(135deg, #2d7eff, #8b5cf6); color: #fff;
  font-size: 1.4rem; display: flex; align-items: center; justify-content: center;
  cursor: pointer; box-shadow: 0 6px 24px rgba(45,126,255,0.35);
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1), box-shadow 0.3s ease;
  border: none; position: relative;
}
.chat-main-btn:hover { transform: scale(1.08); box-shadow: 0 8px 32px rgba(45,126,255,0.5); }
.chat-main-btn.open { transform: rotate(45deg); }
.chat-main-btn:focus-visible { outline: 2px solid #2d7eff; outline-offset: 3px; }

.chat-badge {
  position: absolute; top: -2px; right: -2px; width: 18px; height: 18px;
  background: #ef4444; border-radius: 50%; font-size: 0.65rem; font-weight: 700;
  color: #fff; display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--bg, #0a0a0a); animation: badge-pop 0.3s ease;
}
@keyframes badge-pop { from{transform:scale(0)} to{transform:scale(1)} }

/* ---------- 聊天窗口 ---------- */
.chat-window {
  position: fixed; bottom: 90px; right: 24px; width: 360px; height: 520px;
  background: var(--bg-light, #121212);
  border: 1px solid var(--border, rgba(255,255,255,0.1));
  border-radius: 20px; box-shadow: 0 16px 60px rgba(0,0,0,0.4);
  display: flex; flex-direction: column; overflow: hidden;
  z-index: 899; transform: scale(0.9) translateY(20px); opacity: 0;
  pointer-events: none;
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1), opacity 0.3s ease;
}
.chat-window.open { transform: scale(1) translateY(0); opacity: 1; pointer-events: all; }

/* ---------- 聊天头部 ---------- */
.chat-head {
  padding: 16px 18px;
  background: linear-gradient(135deg, #2d7eff, #8b5cf6); color: #fff;
  display: flex; align-items: center; gap: 12px; flex-shrink: 0;
}
.chat-head-avatar {
  width: 40px; height: 40px; border-radius: 50%; background: rgba(255,255,255,0.2);
  display: flex; align-items: center; justify-content: center; font-size: 1.2rem; flex-shrink: 0;
}
.chat-head-info { flex: 1; min-width: 0; }
.chat-head-name { font-weight: 700; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head-sub { font-size: 0.75rem; opacity: 0.8; display: flex; align-items: center; gap: 5px; }
.chat-head-sub-dot { width: 6px; height: 6px; border-radius: 50%; background: #10b981; animation: pulse-dot 2s ease-in-out infinite; }
.chat-head-actions { display: flex; gap: 8px; }
.chat-head-btn {
  background: rgba(255,255,255,0.15); border: none; color: #fff;
  width: 32px; height: 32px; border-radius: 8px; cursor: pointer;
  font-size: 0.9rem; display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.chat-head-btn:hover { background: rgba(255,255,255,0.25); }
.chat-head-btn:focus-visible { outline: 2px solid rgba(255,255,255,0.5); outline-offset: 2px; }

/* ---------- 消息区域 ---------- */
.chat-body {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.chat-body::-webkit-scrollbar { width: 4px; }
.chat-body::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; }

/* ---------- 消息气泡 ---------- */
.chat-msg {
  display: flex; gap: 8px; align-items: flex-end;
  animation: msg-in 0.3s ease; max-width: 85%;
}
@keyframes msg-in { from{opacity:0;transform:translateY(8px)} to{opacity:1;transform:translateY(0)} }
.chat-msg.user { align-self: flex-end; flex-direction: row-reverse; }
.chat-msg.bot  { align-self: flex-start; }

.chat-msg-avatar {
  width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.75rem; font-weight: 700;
}
.chat-msg.user .chat-msg-avatar { background: linear-gradient(135deg, #2d7eff, #8b5cf6); color: #fff; }
.chat-msg.bot .chat-msg-avatar {
  background: var(--bg-lighter, #1a1a1a); color: var(--text-muted, #a1a1aa);
  border: 1px solid var(--border, rgba(255,255,255,0.1)); font-size: 0.65rem;
}

.chat-msg-content {
  padding: 10px 14px; border-radius: 16px; font-size: 0.9rem;
  line-height: 1.5; word-break: break-word;
}
.chat-msg.user .chat-msg-content {
  background: linear-gradient(135deg, #2d7eff, #8b5cf6); color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.bot .chat-msg-content {
  background: var(--bg-lighter, #1a1a1a); color: var(--text, #f9fafb);
  border: 1px solid var(--border, rgba(255,255,255,0.08)); border-bottom-left-radius: 4px;
}
.chat-msg-time { font-size: 0.65rem; color: var(--text-dimmer, #777); margin-top: 3px; padding: 0 4px; }
.chat-msg.user .chat-msg-time { text-align: right; }

/* ---------- 打字指示器 ---------- */
.typing-indicator {
  display: flex; gap: 4px; align-items: center;
  padding: 10px 14px;
  background: var(--bg-lighter, #1a1a1a);
  border: 1px solid var(--border, rgba(255,255,255,0.08));
  border-radius: 16px; border-bottom-left-radius: 4px;
  align-self: flex-start; max-width: 70px;
}
.typing-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-dimmer, #777); animation: typing-bounce 1.4s ease-in-out infinite;
}
.typing-dot:nth-child(2){animation-delay:0.2s}
.typing-dot:nth-child(3){animation-delay:0.4s}
@keyframes typing-bounce {
  0%,60%,100%{transform:translateY(0)}
  30%{transform:translateY(-6px)}
}

/* ---------- 系统消息 ---------- */
.chat-msg.system { align-self: center; max-width: 90%; }
.chat-msg.system .chat-msg-content {
  background: transparent; border: 1px dashed var(--border, rgba(255,255,255,0.15));
  color: var(--text-dimmer, #777); font-size: 0.8rem; text-align: center;
  border-radius: 8px; padding: 6px 12px;
}

/* ---------- 底部输入区 ---------- */
.chat-foot {
  padding: 12px 14px; border-top: 1px solid var(--border, rgba(255,255,255,0.08));
  display: flex; gap: 8px; align-items: flex-end; flex-shrink: 0;
  background: var(--bg, #0a0a0a);
}

.chat-textarea {
  flex: 1; min-height: 40px; max-height: 100px; padding: 10px 14px;
  border-radius: 20px; border: 1px solid var(--border, rgba(255,255,255,0.1));
  background: var(--bg-lighter, #1a1a1a); color: var(--text, #f9fafb);
  font-family: inherit; font-size: 0.9rem; resize: none; outline: none;
  transition: border-color 0.2s; line-height: 1.4;
}
.chat-textarea:focus { border-color: #2d7eff; box-shadow: 0 0 0 3px rgba(45,126,255,0.15); }
.chat-textarea:focus-visible { border-color: #2d7eff; }
.chat-textarea::placeholder { color: var(--text-dimmer, #777); }
.chat-textarea::-webkit-scrollbar { width: 3px; }
.chat-textarea::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; }

.chat-send-btn {
  width: 40px; height: 40px; border-radius: 50%;
  background: linear-gradient(135deg, #2d7eff, #8b5cf6); border: none; color: #fff;
  font-size: 1rem; cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: transform 0.2s ease, box-shadow 0.2s ease; flex-shrink: 0;
}
.chat-send-btn:hover:not(:disabled) { transform: scale(1.08); box-shadow: 0 4px 12px rgba(45,126,255,0.3); }
.chat-send-btn:focus-visible { outline: 2px solid #2d7eff; outline-offset: 2px; }
.chat-send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- 会话信息条 ---------- */
.session-info-bar {
  padding: 8px 16px; background: var(--bg-lighter, #1a1a1a);
  border-bottom: 1px solid var(--border, rgba(255,255,255,0.06));
  font-size: 0.75rem; color: var(--text-dimmer, #777);
  display: flex; justify-content: space-between; align-items: center; flex-shrink: 0;
}
.session-id-label {
  font-family: monospace; background: var(--bg-light, #121212);
  padding: 2px 8px; border-radius: 4px;
  border: 1px solid var(--border, rgba(255,255,255,0.08));
}
.clear-session-btn {
  background: transparent; border: 1px solid var(--border, rgba(255,255,255,0.1));
  color: var(--text-dimmer, #777); padding: 2px 8px; border-radius: 4px;
  cursor: pointer; font-size: 0.72rem; transition: all 0.2s; font-family: inherit;
}
.clear-session-btn:hover { color: #ef4444; border-color: #ef4444; }
.clear-session-btn:focus-visible { outline: 2px solid #ef4444; outline-offset: 2px; }

/* ---------- 无障碍：减少动画 ---------- */
@media (prefers-reduced-motion: reduce) {
  .chat-msg { animation: none; }
  .typing-dot { animation: none; }
  .chat-status-dot { animation: none; }
  .chat-badge { animation: none; }
}

/* ---------- 响应式：移动端 ---------- */
@media (max-width: 480px) {
  .chat-window { width: calc(100vw - 32px); height: 70vh; bottom: 84px; right: 16px; left: 16px; }
  .chat-float-btn { right: 16px; bottom: 16px; }
  .chat-status-bar { display: none; }
}

@media (max-width: 360px) {
  .chat-window { border-radius: 12px; }
}
