/**
 * Markdown & RAG Styles - Markdown渲染和RAG相关样式
 * 提取自原项目中所有Markdown和知识库相关的样式
 */

/* ============================================================================
   Markdown基础样式 - Markdown Base Styles
   ============================================================================ */

.markdown-content {
  line-height: 1.6;
  color: #333;
  font-size: 0.95rem;
}

.markdown-content > *:first-child {
  margin-top: 0;
}

.markdown-content > *:last-child {
  margin-bottom: 0;
}

/* 标题样式 */
.markdown-content h1,
.markdown-content h2,
.markdown-content h3,
.markdown-content h4,
.markdown-content h5,
.markdown-content h6 {
  margin: 1.2em 0 0.6em 0;
  font-weight: 600;
  line-height: 1.3;
  color: #2c3e50;
}

.markdown-content h1 { font-size: 1.5em; border-bottom: 2px solid #3498db; padding-bottom: 0.3em; }
.markdown-content h2 { font-size: 1.3em; border-bottom: 1px solid #bdc3c7; padding-bottom: 0.2em; }
.markdown-content h3 { font-size: 1.15em; }
.markdown-content h4 { font-size: 1.05em; }
.markdown-content h5 { font-size: 1em; }
.markdown-content h6 { font-size: 0.95em; color: #666; }

/* 段落和文本 */
.markdown-content p {
  margin: 0.8em 0;
  text-align: justify;
}

/* 空行优化 - 使空行变得非常窄 */
.markdown-content p:empty,
.markdown-content p:blank {
  margin: 0.1em 0;
  height: 0.1em;
  line-height: 0.1;
}

/* 处理只包含空白字符的段落 */
.markdown-content p:has(br:only-child),
.markdown-content p:not(:has(*)):not(:has(text)):empty {
  margin: 0.1em 0;
  height: 0.1em;
  line-height: 0.1;
}

/* 使用CSS选择器匹配只包含空白字符的段落 */
.markdown-content p:is(:empty, :not(:has(*)):has(br:only-child)) {
  margin: 0.1em 0;
  height: 0.1em;
  line-height: 0.1;
}

/* 全局的空行处理 - 适用于所有可能的空行情况 */
.markdown-content br + br,
.markdown-content br:only-child {
  line-height: 0.2;
}

/* 连续的空行处理 */
.markdown-content p:empty + p:empty {
  display: none;
}

/* 更强的空行控制 */
.markdown-content p:empty,
.markdown-content p:has(br:only-child) {
  margin: 0.2em 0 !important;
  height: 0.2em !important;
  line-height: 0.2 !important;
  padding: 0 !important;
}

/* 对于包含只有空白字符的段落 */
.markdown-content p:not(:has(*)):is([children=" "], [children="\n"], [children="\t"]) {
  margin: 0.1em 0 !important;
  height: 0.1em !important;
  line-height: 0.1 !important;
  display: block !important;
}

/* ReactMarkdown生成的空段落 */
.markdown-content > p:empty {
  margin: 0.15em 0 !important;
  height: 0.15em !important;
  font-size: 0.5em !important;
}

/* 全局减少段落间距 */
.markdown-content p + p {
  margin-top: 0.6em !important;
}

.markdown-content strong {
  font-weight: 600;
  color: #2c3e50;
}

.markdown-content em {
  font-style: italic;
  color: #7f8c8d;
}

.markdown-content del {
  text-decoration: line-through;
  color: #95a5a6;
}

/* 链接样式 */
.markdown-content a {
  color: #3498db;
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: all 0.2s ease;
}

.markdown-content a:hover {
  color: #2980b9;
  border-bottom-color: #2980b9;
}

/* 列表样式 */
.markdown-content ul,
.markdown-content ol {
  margin: 0.8em 0;
  padding-left: 2em;
}

.markdown-content li {
  margin: 0.3em 0;
  line-height: 1.5;
}

/* 空列表项优化 */
.markdown-content li:empty {
  margin: 0.1em 0 !important;
  height: 0.1em !important;
  line-height: 0.1 !important;
  display: list-item !important;
}

/* 只包含少量内容的列表项 */
.markdown-content li:has(p:empty),
.markdown-content li:has(br:only-child) {
  margin: 0.15em 0 !important;
  line-height: 1.2 !important;
}

/* 连续的空列表项 */
.markdown-content li:empty + li:empty {
  display: none !important;
}

/* 减少列表项中段落的间距 */
.markdown-content li p {
  margin: 0.2em 0 !important;
}

.markdown-content li p:only-child {
  margin: 0 !important;
}

.markdown-content ul li {
  list-style-type: disc;
}

.markdown-content ol li {
  list-style-type: decimal;
}

.markdown-content ul ul li {
  list-style-type: circle;
}

.markdown-content ul ul ul li {
  list-style-type: square;
}

/* 引用块样式 */
.markdown-content blockquote {
  margin: 1em 0;
  padding: 0.8em 1.2em;
  border-left: 4px solid #3498db;
  background: #f8f9fa;
  border-radius: 0 6px 6px 0;
  color: #5a6c7d;
  font-style: italic;
}

.markdown-content blockquote p {
  margin: 0.4em 0;
}

.markdown-content blockquote p:first-child {
  margin-top: 0;
}

.markdown-content blockquote p:last-child {
  margin-bottom: 0;
}

/* 水平分割线 */
.markdown-content hr {
  border: none;
  height: 2px;
  background: linear-gradient(to right, transparent, #bdc3c7, transparent);
  margin: 2em 0;
}

/* ============================================================================
   代码样式 - Code Styles
   ============================================================================ */

.markdown-content code {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace;
  font-size: 0.9em;
}

.inline-code {
  background: #f1f3f4;
  color: #e74c3c;
  padding: 0.15em 0.4em;
  border-radius: 3px;
  font-weight: 500;
  border: 1px solid #ecf0f1;
}

.code-block {
  background: #f8f9fa;
  color: #333;
  padding: 1.2em;
  border-radius: 8px;
  margin: 1em 0;
  overflow-x: auto;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border: 1px solid #e9ecef;
}

.code-block code {
  background: none;
  color: inherit;
  padding: 0;
  border-radius: 0;
  border: none;
  font-size: 0.85em;
  line-height: 1.5;
}

/* 语法高亮增强 */
.code-block .token.comment {
  color: #a0aec0;
  font-style: italic;
}

.code-block .token.keyword {
  color: #63b3ed;
  font-weight: bold;
}

.code-block .token.string {
  color: #68d391;
}

.code-block .token.number {
  color: #fbb6ce;
}

/* ============================================================================
   表格样式 - Table Styles
   ============================================================================ */

.table-wrapper {
  overflow-x: auto;
  margin: 1em 0;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.markdown-table {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border-radius: 8px;
  overflow: hidden;
}

.markdown-table th,
.markdown-table td {
  padding: 0.8em 1em;
  text-align: left;
  border-bottom: 1px solid #e2e8f0;
}

.markdown-table th {
  background: #f7fafc;
  font-weight: 600;
  color: #2d3748;
  border-bottom: 2px solid #cbd5e0;
}

.markdown-table tr:hover {
  background: #f8f9fa;
}

.markdown-table tr:last-child td {
  border-bottom: none;
}

/* ============================================================================
   流式Markdown样式 - Streaming Markdown Styles
   ============================================================================ */

.streaming-markdown {
  position: relative;
}

.normal-content,
.main-content {
  position: relative;
}

.chain-of-thought {
  margin-bottom: 1rem;
  border: 1px solid #e0e7ff;
  border-radius: 8px;
  background: #f8faff;
  overflow: hidden;
}

.chain-header {
  padding: 0.75rem 1rem;
  background: #f0f8ff;
  border-bottom: 1px solid #e0e7ff;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  transition: background-color 0.2s;
}

.chain-header:hover {
  background: #e6f3ff;
}

.chain-icon {
  font-size: 1.1rem;
}

.chain-title {
  font-weight: 500;
  color: #4338ca;
  font-size: 0.9rem;
}

.thinking-content {
  padding: 1rem;
  background: white;
}

/* ============================================================================
   RAG来源样式 - RAG Sources Styles  
   ============================================================================ */

.rag-sources-container {
  margin-top: 12px;
  border: 1px solid #e0e7ff;
  border-radius: 8px;
  background: #f8faff;
  overflow: hidden;
}

.rag-sources-header {
  padding: 8px 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: #4338ca;
  background: #f0f8ff;
  border-bottom: 1px solid #e0e7ff;
  transition: background-color 0.2s;
  user-select: none;
}

.rag-sources-header:hover {
  background: #e6f3ff;
}

.rag-sources-content {
  padding: 8px 12px;
  background: white;
}

.rag-source-item {
  margin-bottom: 12px;
  padding: 10px;
  background: white;
  border-radius: 6px;
  border: 1px solid #e5e7eb;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.rag-source-item:last-child {
  margin-bottom: 0;
}

.rag-source-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.rag-source-title {
  font-size: 12px;
  font-weight: 600;
  color: #374151;
  display: flex;
  align-items: center;
  gap: 4px;
}

.rag-source-title span:first-child {
  font-size: 14px;
}

.rag-source-meta {
  color: #6b7280;
  font-weight: normal;
  font-size: 11px;
}

.rag-source-score {
  font-size: 11px;
  color: #6b7280;
  background: #f3f4f6;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 500;
}

.rag-source-content {
  font-size: 12px;
  color: #4b5563;
  line-height: 1.4;
  font-style: italic;
  background: #f9fafb;
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #f3f4f6;
}

/* ============================================================================
   数学公式样式 - Math Formula Styles
   ============================================================================ */

.markdown-content .katex {
  font-size: 1.05em;
}

.markdown-content .katex-display {
  margin: 1em 0;
  text-align: center;
}

.markdown-content .katex-display .katex {
  display: inline-block;
  white-space: nowrap;
}

.markdown-content .katex-error {
  color: #e74c3c;
  background: #fdf2f2;
  padding: 0.2em 0.4em;
  border-radius: 3px;
  border: 1px solid #fed7d7;
}

/* 数学公式容器样式增强 */
.markdown-content .katex-html {
  white-space: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
}

/* ============================================================================
   图片样式 - Image Styles
   ============================================================================ */

.markdown-content img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  margin: 0.5em 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e2e8f0;
}

.markdown-content img[alt*="icon"],
.markdown-content img[alt*="emoji"] {
  display: inline;
  margin: 0 0.2em;
  box-shadow: none;
  border: none;
  border-radius: 0;
  vertical-align: middle;
}

/* ============================================================================
   响应式设计 - Responsive Design
   ============================================================================ */

@media (max-width: 768px) {
  .markdown-content {
    font-size: 0.9rem;
  }
  
  .markdown-content h1 { font-size: 1.3em; }
  .markdown-content h2 { font-size: 1.2em; }
  .markdown-content h3 { font-size: 1.1em; }
  
  .code-block {
    padding: 1em;
    font-size: 0.8em;
  }
  
  .markdown-table th,
  .markdown-table td {
    padding: 0.6em 0.8em;
    font-size: 0.85em;
  }
  
  .table-wrapper {
    box-shadow: none;
    border: 1px solid #e2e8f0;
  }
  
  .rag-sources-header {
    padding: 6px 10px;
    font-size: 12px;
  }
  
  .rag-sources-content {
    padding: 6px 10px;
  }
  
  .rag-source-item {
    padding: 8px;
    margin-bottom: 8px;
  }
  
  .rag-source-title {
    font-size: 11px;
  }
  
  .rag-source-content {
    font-size: 11px;
    padding: 6px;
  }
}

@media (max-width: 480px) {
  .markdown-content {
    font-size: 0.85rem;
  }
  
  .markdown-content ul,
  .markdown-content ol {
    padding-left: 1.5em;
  }
  
  .markdown-content blockquote {
    padding: 0.6em 1em;
    margin: 0.8em 0;
  }
  
  .code-block {
    padding: 0.8em;
    margin: 0.8em 0;
  }
  
  .chain-header {
    padding: 0.6rem 0.8rem;
    font-size: 0.85rem;
  }
  
  .thinking-content {
    padding: 0.8rem;
  }
}

/* ============================================================================
   打印样式 - Print Styles
   ============================================================================ */

@media print {
  .markdown-content {
    color: black;
    background: white;
  }
  
  .code-block {
    background: #f5f5f5;
    color: black;
    border: 1px solid #ccc;
  }
  
  .rag-sources-container {
    border: 1px solid #ccc;
    background: white;
  }
  
  .rag-sources-header {
    background: #f5f5f5;
    border-bottom: 1px solid #ccc;
  }
  
  .chain-of-thought {
    border: 1px solid #ccc;
    background: white;
  }
  
  .chain-header {
    background: #f5f5f5;
    border-bottom: 1px solid #ccc;
  }
}