/* ============================================
   知识库问答聊天组件样式
   兼容 Butterfly 主题的亮色/暗色模式
   ============================================ */

/* ============ CSS 变量 ============ */
:root {
  --kb-primary: #4f6ef7;
  --kb-primary-hover: #3b5de7;
  --kb-primary-light: #eef1ff;
  --kb-bg-panel: #ffffff;
  --kb-bg-input: #f5f6fa;
  --kb-bg-bubble-user: #4f6ef7;
  --kb-bg-bubble-ai: #f0f2f5;
  --kb-text: #1a1a2e;
  --kb-text-secondary: #6b7280;
  --kb-text-user: #ffffff;
  --kb-border: #e5e7eb;
  --kb-shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --kb-shadow-md: 0 4px 24px rgba(0,0,0,0.12);
  --kb-shadow-lg: 0 10px 40px rgba(0,0,0,0.16);
  --kb-radius: 16px;
  --kb-radius-sm: 12px;
  --kb-radius-xs: 8px;
  --kb-transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 暗色模式适配 */
[data-theme="dark"] {
  --kb-primary: #6b8aff;
  --kb-primary-hover: #8aa4ff;
  --kb-primary-light: #1e2545;
  --kb-bg-panel: #1e1e2e;
  --kb-bg-input: #282840;
  --kb-bg-bubble-user: #4f6ef7;
  --kb-bg-bubble-ai: #282840;
  --kb-text: #e0e0e8;
  --kb-text-secondary: #9090a0;
  --kb-border: #383850;
  --kb-shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --kb-shadow-md: 0 4px 24px rgba(0,0,0,0.4);
  --kb-shadow-lg: 0 10px 40px rgba(0,0,0,0.5);
}

/* ============ 浮动按钮 ============ */
#kb-chat-trigger {
  position: fixed;
  bottom: 28px;
  right: 28px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--kb-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  box-shadow: var(--kb-shadow-md);
  transition: all var(--kb-transition);
  z-index: 9998;
}

#kb-chat-trigger:hover {
  transform: scale(1.08);
  background: var(--kb-primary-hover);
  box-shadow: var(--kb-shadow-lg);
}

#kb-chat-trigger:active {
  transform: scale(0.95);
}

#kb-chat-trigger.kb-chat-trigger--open {
  background: var(--kb-bg-panel);
  color: var(--kb-text);
  box-shadow: var(--kb-shadow-lg);
}

/* ============ 面板 ============ */
#kb-chat-panel {
  position: fixed;
  bottom: 96px;
  right: 28px;
  width: 420px;
  max-width: calc(100vw - 40px);
  height: 580px;
  max-height: calc(100vh - 140px);
  background: var(--kb-bg-panel);
  border-radius: var(--kb-radius);
  box-shadow: var(--kb-shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 9999;
  border: 1px solid var(--kb-border);
  animation: kbSlideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

#kb-chat-panel.kb-chat-hidden {
  display: none !important;
}

@keyframes kbSlideUp {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ============ 头部 ============ */
#kb-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--kb-border);
  flex-shrink: 0;
  background: var(--kb-bg-panel);
}

#kb-chat-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
  color: var(--kb-text);
}

#kb-chat-header-left svg {
  color: var(--kb-primary);
}

#kb-chat-header-right {
  display: flex;
  gap: 4px;
}

#kb-chat-clear {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 6px;
  opacity: 0.5;
  transition: all var(--kb-transition);
}

#kb-chat-clear:hover {
  opacity: 1;
  background: var(--kb-bg-input);
}

/* ============ 消息区 ============ */
#kb-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

#kb-chat-messages::-webkit-scrollbar {
  width: 4px;
}

#kb-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

#kb-chat-messages::-webkit-scrollbar-thumb {
  background: var(--kb-border);
  border-radius: 4px;
}

/* ============ 消息气泡 ============ */
.kb-chat-msg {
  display: flex;
  gap: 10px;
  max-width: 92%;
  animation: kbMsgIn 0.25s ease;
}

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

.kb-chat-msg--user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.kb-chat-msg--assistant {
  align-self: flex-start;
}

.kb-chat-msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  background: var(--kb-bg-input);
  margin-top: 2px;
}

.kb-chat-msg--user .kb-chat-msg-avatar {
  background: var(--kb-primary-light);
}

.kb-chat-msg-bubble {
  padding: 12px 16px;
  border-radius: var(--kb-radius-sm);
  font-size: 14px;
  line-height: 1.65;
  word-break: break-word;
  position: relative;
}

.kb-chat-msg--user .kb-chat-msg-bubble {
  background: var(--kb-bg-bubble-user);
  color: var(--kb-text-user);
  border-bottom-right-radius: 4px;
}

.kb-chat-msg--assistant .kb-chat-msg-bubble {
  background: var(--kb-bg-bubble-ai);
  color: var(--kb-text);
  border-bottom-left-radius: 4px;
}

/* 消息中的 Markdown 样式 */
.kb-chat-msg-bubble p {
  margin: 0 0 8px;
}

.kb-chat-msg-bubble p:last-child {
  margin-bottom: 0;
}

.kb-chat-msg-bubble h1, 
.kb-chat-msg-bubble h2, 
.kb-chat-msg-bubble h3, 
.kb-chat-msg-bubble h4 {
  margin: 10px 0 6px;
  font-weight: 600;
  line-height: 1.4;
}

.kb-chat-msg-bubble h2 { font-size: 1.1em; }
.kb-chat-msg-bubble h3 { font-size: 1.05em; }
.kb-chat-msg-bubble h4 { font-size: 1em; }

.kb-chat-msg-bubble ul, 
.kb-chat-msg-bubble ol {
  margin: 6px 0;
  padding-left: 20px;
}

.kb-chat-msg-bubble li {
  margin: 3px 0;
}

.kb-chat-msg-bubble code {
  background: rgba(0,0,0,0.08);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9em;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
}

[data-theme="dark"] .kb-chat-msg-bubble code {
  background: rgba(255,255,255,0.1);
}

.kb-chat-msg-bubble pre {
  background: #1e1e2e;
  color: #e0e0e8;
  padding: 12px 14px;
  border-radius: var(--kb-radius-xs);
  overflow-x: auto;
  margin: 8px 0;
  font-size: 13px;
  line-height: 1.5;
}

.kb-chat-msg-bubble pre code {
  background: none;
  padding: 0;
  font-size: inherit;
}

.kb-chat-msg--user .kb-chat-msg-bubble pre {
  background: rgba(0,0,0,0.15);
  color: rgba(255,255,255,0.9);
}

.kb-chat-msg--user .kb-chat-msg-bubble code {
  background: rgba(0,0,0,0.12);
  color: rgba(255,255,255,0.95);
}

.kb-chat-msg-bubble a {
  color: var(--kb-primary);
  text-decoration: underline;
}

.kb-chat-msg--user .kb-chat-msg-bubble a {
  color: #fff;
}

.kb-chat-msg-bubble strong {
  font-weight: 600;
}

/* ============ 来源引用（可折叠） ============ */
	#kb-chat-sources {
	  padding: 0 18px 8px;
	  border-top: 1px solid var(--kb-border);
	  flex-shrink: 0;
	  max-height: 30vh;
	  overflow-y: auto;
	}
	
	.kb-chat-sources-title {
	  font-size: 12px;
	  color: var(--kb-text-secondary);
	  font-weight: 600;
	  padding: 8px 0 6px;
	  cursor: pointer;
	  user-select: none;
	  display: flex;
	  align-items: center;
	  gap: 4px;
	}
	
	.kb-chat-sources-title:hover {
	  color: var(--kb-primary);
	}
	
	.kb-chat-sources-title::after {
	  content: '▾';
	  font-size: 10px;
	  margin-left: auto;
	  transition: transform 0.2s;
	}
	
	.kb-chat-sources-title.kb-chat-sources-collapsed::after {
	  transform: rotate(-90deg);
	}
	
	.kb-chat-sources-list {
	  display: flex;
	  flex-wrap: wrap;
	  gap: 6px;
	}
	
	.kb-chat-sources-list.kb-chat-sources-collapsed {
	  display: none;
	}
	
	.kb-chat-source-item {
	  display: inline-flex;
	  align-items: center;
	  gap: 4px;
	  font-size: 11px;
	  padding: 3px 8px;
	  background: var(--kb-primary-light);
	  color: var(--kb-primary);
	  border-radius: 12px;
	  max-width: 200px;
	  text-decoration: none;
	  white-space: nowrap;
	  overflow: hidden;
	  text-overflow: ellipsis;
	  transition: background 0.15s;
	}
	
	.kb-chat-source-item:hover {
	  background: var(--kb-primary);
	  color: #fff;
	}
	
	.kb-chat-source-idx {
	  display: inline-flex;
	  align-items: center;
	  justify-content: center;
	  width: 16px;
	  height: 16px;
	  font-size: 10px;
	  font-weight: 700;
	  border-radius: 50%;
	  background: var(--kb-primary);
	  color: #fff;
	  flex-shrink: 0;
	}

/* ============ 输入区 ============ */
#kb-chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--kb-border);
  background: var(--kb-bg-panel);
  flex-shrink: 0;
}

#kb-chat-input {
  flex: 1;
  border: 1px solid var(--kb-border);
  border-radius: var(--kb-radius-xs);
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
  outline: none;
  background: var(--kb-bg-input);
  color: var(--kb-text);
  transition: border-color var(--kb-transition);
  max-height: 120px;
}

#kb-chat-input:focus {
  border-color: var(--kb-primary);
  box-shadow: 0 0 0 3px rgba(79, 110, 247, 0.12);
}

#kb-chat-input::placeholder {
  color: var(--kb-text-secondary);
}

#kb-chat-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--kb-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
  transition: all var(--kb-transition);
}

#kb-chat-send:hover {
  background: var(--kb-primary-hover);
  transform: scale(1.05);
}

#kb-chat-send:active {
  transform: scale(0.95);
}

#kb-chat-send.kb-chat-send--disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

#kb-chat-send svg {
  width: 18px;
  height: 18px;
}

/* ============ 加载动画 ============ */
.kb-chat-loading-dots {
  display: inline-flex;
  gap: 3px;
  align-items: center;
  margin-right: 8px;
}

.kb-chat-loading-dots span {
  width: 6px;
  height: 6px;
  background: var(--kb-text-secondary);
  border-radius: 50%;
  animation: kbDotBounce 1.4s infinite ease-in-out both;
}

.kb-chat-loading-dots span:nth-child(1) { animation-delay: 0s; }
.kb-chat-loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.kb-chat-loading-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes kbDotBounce {
  0%, 80%, 100% { transform: scale(0.4); opacity: 0.3; }
  40% { transform: scale(1); opacity: 1; }
}

.kb-chat-loading-text {
  font-size: 13px;
  color: var(--kb-text-secondary);
}

.kb-chat-msg--loading .kb-chat-msg-bubble {
  display: flex;
  align-items: center;
}

/* ============ 响应式 ============ */
@media (max-width: 520px) {
  #kb-chat-panel {
    right: 8px;
    bottom: 84px;
    width: calc(100vw - 16px);
    height: 480px;
    max-height: calc(100vh - 120px);
    border-radius: var(--kb-radius) var(--kb-radius) 8px 8px;
  }
  
  #kb-chat-trigger {
    right: 12px;
    bottom: 20px;
    width: 46px;
    height: 46px;
  }
  
  .kb-chat-msg {
    max-width: 95%;
  }
}
