/* AI Chat Widget - 魔方财务智能客服 */

:root {
  --ai-primary: #4A90D9;
  --ai-primary-dark: #3a7bc8;
  --ai-bg: #ffffff;
  --ai-bg-light: #f5f7fa;
  --ai-text: #333333;
  --ai-text-light: #888888;
  --ai-border: #e8e8e8;
  --ai-shadow: 0 8px 32px rgba(0,0,0,0.12);
  --ai-radius: 12px;
  --ai-bot-bubble: #f0f4f8;
  --ai-user-bubble: var(--ai-primary);
}

/* 触发按钮 */
/* ====== 机器人风格触发按钮 ====== */
.ai-trigger-robot {
  position: fixed;
  bottom: 250px;
  right: 24px;
  cursor: pointer;
  z-index: 99999;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  animation: aiRobotBounce 2s ease-in-out infinite;
  background: none !important;
  border: none !important;
  box-shadow: none !important;
  padding: 0 !important;
  border-radius: 0 !important;
  height: auto !important;
}

.ai-trigger-robot:hover { animation-play-state: paused; }

.ai-robot-img {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  box-shadow: 0 4px 16px rgba(74,144,217,0.4);
  transition: transform 0.2s;
  object-fit: cover;
}

.ai-trigger-robot:hover .ai-robot-img {
  transform: scale(1.08);
}

.ai-trigger-robot .ai-trigger-text {
  font-size: 12px;
  color: #555;
  font-weight: 600;
  background: #fff;
  padding: 2px 10px;
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

.ai-trigger-robot.active .ai-robot-img {
  border: 2px solid #ff6b6b;
}

@keyframes aiRobotBounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

#ai-chat-trigger {
  position: fixed;
  bottom: 250px;
  right: 24px;
  height: 48px;
  padding: 0 18px 0 14px;
  border-radius: 24px;
  background: var(--ai-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  z-index: 99999;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 16px rgba(74,144,217,0.4);
  transition: all 0.3s ease;
  font-size: 14px;
  font-weight: 600;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', sans-serif;
}

#ai-chat-trigger:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 24px rgba(74,144,217,0.5);
}

#ai-chat-close {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ff4d4f;
  color: #fff;
  border: 2px solid #fff;
  font-size: 14px;
  line-height: 16px;
  text-align: center;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  opacity: 0;
  transition: opacity 0.2s;
}

#ai-chat-trigger:hover #ai-chat-close {
  opacity: 1;
}

@media (max-width: 768px) {
  #ai-chat-trigger,
  .ai-trigger-robot {
    transition: opacity 0.2s;
  }
  #ai-chat-trigger.active,
  .ai-trigger-robot.active {
    opacity: 0;
    pointer-events: none;
  }
  #ai-chat-close {
    opacity: 1;
    width: 26px;
    height: 26px;
    font-size: 16px;
    top: -8px;
    right: -8px;
  }
}

#ai-chat-trigger svg {
  width: 28px;
  height: 28px;
  fill: #fff;
  transition: transform 0.3s;
}

#ai-chat-trigger.active svg.icon-chat { display: none; }
#ai-chat-trigger.active .ai-trigger-text { display: none; }
#ai-chat-trigger:not(.active) svg.icon-close { display: none; }
#ai-chat-trigger.active svg.icon-close { display: block; }

/* 未读小红点 */
#ai-chat-trigger .ai-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 16px;
  height: 16px;
  background: #ff4d4f;
  border-radius: 50%;
  border: 2px solid #fff;
  display: none;
}

/* 聊天窗口 */
#ai-chat-window {
  position: fixed;
  bottom: 320px;
  right: 24px;
  width: 400px;
  max-width: calc(100vw - 48px);
  height: 560px;
  max-height: calc(100vh - 140px);
  background: var(--ai-bg);
  border-radius: var(--ai-radius);
  box-shadow: var(--ai-shadow);
  z-index: 99998;
  display: none;
  flex-direction: column;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: var(--ai-text);
  animation: aiSlideUp 0.3s ease;
}

#ai-chat-window.open {
  display: flex;
  resize: both;
  overflow: hidden;
  min-width: 300px;
  min-height: 350px;
}

/* 拖拽手柄 */
.ai-chat-header {
  cursor: move;
}

/* 拉伸手柄（右下角） */
.ai-resize-handle {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 18px;
  height: 18px;
  cursor: nwse-resize;
  z-index: 10;
  background: linear-gradient(135deg, transparent 50%, rgba(0,0,0,0.15) 50%);
  border-radius: 0 0 12px 0;
}

/* 右边框拉伸 */
.ai-resize-right {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: ew-resize;
  z-index: 10;
}

/* 拖拽中禁用文字选择 */
.ai-dragging {
  user-select: none !important;
}
.ai-dragging * {
  user-select: none !important;
}

@keyframes aiSlideUp {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* 头部 */
.ai-chat-header {
  background: var(--ai-primary);
  color: #fff;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.ai-chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.ai-chat-header .ai-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.ai-chat-header .ai-title {
  font-size: 15px;
  font-weight: 600;
}

.ai-chat-header .ai-status {
  font-size: 12px;
  opacity: 0.85;
}

.ai-chat-header-actions {
  display: flex;
  gap: 8px;
}

.ai-chat-header-actions button {
  background: rgba(255,255,255,0.15);
  border: none;
  color: #fff;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: background 0.2s;
}

.ai-chat-header-actions button:hover {
  background: rgba(255,255,255,0.25);
}

/* 消息区域 */
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--ai-bg-light);
  scroll-behavior: smooth;
  /* Firefox */
  scrollbar-width: auto;
  scrollbar-color: #c1c1c1 transparent;
}

.ai-chat-messages::-webkit-scrollbar {
  width: 20px;
}

.ai-chat-messages::-webkit-scrollbar-track {
  background: transparent;
  border-radius: 6px;
}

.ai-chat-messages::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 6px;
  border: 3px solid transparent;
  background-clip: content-box;
  min-height: 40px;
}

.ai-chat-messages::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
  background-clip: content-box;
  border: 3px solid transparent;
}

.ai-chat-messages::-webkit-scrollbar-thumb:active {
  background: #909090;
  background-clip: content-box;
  border: 3px solid transparent;
}

/* 消息气泡 */
.ai-msg {
  display: flex;
  margin-bottom: 16px;
  gap: 8px;
}

.ai-msg.user {
  flex-direction: row-reverse;
}

.ai-msg .ai-msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

.ai-msg.bot .ai-msg-avatar {
  background: var(--ai-primary);
  color: #fff;
}

.ai-msg.user .ai-msg-avatar {
  background: #e0e0e0;
  color: #666;
}

.ai-msg .ai-msg-body {
  max-width: 80%;
}

.ai-msg .ai-msg-content {
  padding: 10px 14px;
  border-radius: 12px;
  word-break: break-word;
  white-space: pre-wrap;
}

.ai-msg.bot .ai-msg-content {
  background: var(--ai-bot-bubble);
  border-top-left-radius: 4px;
}

.ai-msg.user .ai-msg-content {
  background: var(--ai-user-bubble);
  color: #fff;
  border-top-right-radius: 4px;
}

.ai-msg .ai-msg-content p {
  margin: 0 0 8px;
}

.ai-msg .ai-msg-content p:last-child {
  margin-bottom: 0;
}

.ai-msg .ai-msg-content code {
  background: rgba(0,0,0,0.06);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: 'Consolas', 'Monaco', monospace;
}

.ai-msg.user .ai-msg-content code {
  background: rgba(255,255,255,0.2);
}

.ai-msg .ai-msg-content pre {
  background: #1e1e1e;
  color: #d4d4d4;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  margin: 8px 0;
  scrollbar-width: auto;
  scrollbar-color: #666 transparent;
}

.ai-msg .ai-msg-content pre::-webkit-scrollbar {
  height: 20px;
}

.ai-msg .ai-msg-content pre::-webkit-scrollbar-track {
  background: transparent;
  border-radius: 6px;
}

.ai-msg .ai-msg-content pre::-webkit-scrollbar-thumb {
  background: #666;
  border-radius: 6px;
  border: 3px solid transparent;
  background-clip: content-box;
}

.ai-msg .ai-msg-content pre::-webkit-scrollbar-thumb:hover {
  background: #888;
  background-clip: content-box;
  border: 3px solid transparent;
}

.ai-msg .ai-msg-content pre code {
  background: none;
  padding: 0;
  color: inherit;
}

/* 代码块容器 */
.ai-code-block {
  margin: 8px 0;
  border-radius: 8px;
  overflow: hidden;
  background: #1e1e1e;
}

.ai-code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: #2d2d2d;
  border-bottom: 1px solid #3e3e3e;
}

.ai-code-lang {
  font-size: 12px;
  color: #888;
  font-family: 'Consolas', 'Monaco', monospace;
}

.ai-copy-btn {
  background: transparent;
  border: 1px solid #555;
  color: #ccc;
  padding: 2px 10px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.ai-copy-btn:hover {
  background: #3e3e3e;
  border-color: #888;
  color: #fff;
}

.ai-code-block pre {
  margin: 0;
  padding: 12px;
  background: #1e1e1e;
  border-radius: 0;
}

.ai-msg .ai-msg-content a {
  color: var(--ai-primary);
  text-decoration: underline;
}

.ai-msg.user .ai-msg-content a {
  color: #fff;
}

.ai-msg .ai-msg-content ul, .ai-msg .ai-msg-content ol {
  margin: 4px 0;
  padding-left: 20px;
}

.ai-msg .ai-msg-time {
  font-size: 11px;
  color: var(--ai-text-light);
  margin-top: 4px;
}

.ai-msg.user .ai-msg-time {
  text-align: right;
}

/* 打字指示器 */
.ai-typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 14px;
}

.ai-typing span {
  width: 6px;
  height: 6px;
  background: var(--ai-text-light);
  border-radius: 50%;
  animation: aiBounce 1.4s ease-in-out infinite;
}

.ai-typing span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes aiBounce {
  0%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-6px); }
}

/* 产品卡片 */
.ai-product-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.ai-product-card {
  background: #fff;
  border: 1px solid var(--ai-border);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  transition: all 0.2s;
  flex: 1;
  min-width: 140px;
  max-width: 180px;
}

.ai-product-card:hover {
  border-color: var(--ai-primary);
  box-shadow: 0 2px 8px rgba(74,144,217,0.15);
}

.ai-product-card .ai-pc-name {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 4px;
  color: var(--ai-text);
}

.ai-product-card .ai-pc-price {
  color: var(--ai-primary);
  font-weight: 600;
  font-size: 14px;
}

.ai-product-card .ai-pc-cycle {
  color: var(--ai-text-light);
  font-size: 12px;
}

.ai-product-card .ai-pc-btn {
  display: inline-block;
  margin-top: 6px;
  font-size: 12px;
  color: var(--ai-primary);
  text-decoration: none;
  font-weight: 500;
}

.ai-product-card .ai-pc-btn:hover {
  text-decoration: underline;
}

/* 快捷问题 */
.ai-quick-questions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 16px;
  background: var(--ai-bg-light);
  border-top: 1px solid var(--ai-border);
}

.ai-quick-btn {
  background: #fff;
  border: 1px solid var(--ai-border);
  border-radius: 16px;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--ai-primary);
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.ai-quick-btn:hover {
  background: var(--ai-primary);
  color: #fff;
  border-color: var(--ai-primary);
}

/* 输入区域 */
.ai-chat-input-area {
  padding: 12px 16px;
  background: #fff;
  border-top: 1px solid var(--ai-border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
}

.ai-chat-input-area textarea {
  flex: 1;
  border: 1px solid var(--ai-border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  max-height: 100px;
  outline: none;
  transition: border-color 0.2s;
  line-height: 1.5;
}

.ai-chat-input-area textarea:focus {
  border-color: var(--ai-primary);
}

.ai-chat-input-area textarea::placeholder {
  color: #bbb;
}

.ai-send-btn {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--ai-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  flex-shrink: 0;
}

.ai-send-btn:hover {
  background: var(--ai-primary-dark);
}

.ai-send-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.ai-send-btn svg {
  width: 18px;
  height: 18px;
  fill: #fff;
}

/* 加载动画 */
.ai-loading {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--ai-primary);
  border-top-color: transparent;
  border-radius: 50%;
  animation: aiSpin 0.8s linear infinite;
}

@keyframes aiSpin {
  to { transform: rotate(360deg); }
}

/* 移动端适配 */
@media (max-width: 480px) {
  #ai-chat-window {
    bottom: 0;
    right: 0;
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
  }

  #ai-chat-trigger {
    bottom: 20px;
    right: 16px;
  }
}

/* 确认弹窗 */
.ai-chat-confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.45);
  z-index: 100000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: aiConfirmFadeIn 0.2s;
}

.ai-chat-confirm-box {
  background: #fff;
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 360px;
  width: 90%;
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
  text-align: center;
  animation: aiConfirmSlideIn 0.25s;
}

.ai-chat-confirm-msg {
  font-size: 15px;
  color: #333;
  line-height: 1.6;
  margin-bottom: 20px;
}

.ai-chat-confirm-btns {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.ai-chat-confirm-btns button {
  padding: 8px 28px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.ai-chat-confirm-btns .ai-cc-cancel {
  background: #e8e8e8;
  color: #666;
}

.ai-chat-confirm-btns .ai-cc-cancel:hover {
  background: #d8d8d8;
}

.ai-chat-confirm-btns .ai-cc-ok {
  background: var(--ai-primary);
  color: #fff;
}

.ai-chat-confirm-btns .ai-cc-ok:hover {
  opacity: 0.9;
}

@keyframes aiConfirmFadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes aiConfirmSlideIn {
  from { opacity: 0; transform: translateY(-16px) scale(0.95); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* 客服渠道卡片 */
.ai-livechat-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.ai-lc-card {
  background: #fff;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  padding: 10px 14px;
  transition: all 0.2s;
}

.ai-lc-card:hover {
  border-color: var(--ai-primary);
  box-shadow: 0 2px 8px rgba(74,144,217,0.12);
}

.ai-lc-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--ai-primary);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
}

.ai-lc-link:hover {
  text-decoration: none !important;
}

.ai-lc-qr img {
  max-width: 120px;
  max-height: 120px;
  border-radius: 4px;
}

.ai-lc-qr + .ai-lc-link,
.ai-lc-link + .ai-lc-qr {
  margin-top: 6px;
}

/* 转人工快捷按钮 */
.ai-quick-human {
  background: #fff3e0 !important;
  border-color: #ff9800 !important;
  color: #e65100 !important;
}

.ai-quick-human:hover {
  background: #ff9800 !important;
  color: #fff !important;
}