/**
 * 优化的 Mermaid 图表样式
 * 支持 rehype-mermaid 风格的响应式显示和交互
 */

/* 基础容器样式 */
.mermaid-dynamic, .mermaid-static {
  margin: 2rem 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--entry);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  position: relative;
}

.mermaid-dynamic:hover, .mermaid-static:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

/* 标题栏样式 */
.mermaid-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background: linear-gradient(135deg, var(--secondary), var(--tertiary));
  color: var(--primary);
  font-weight: 600;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
}

.mermaid-title-text {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.mermaid-title-text::before {
  content: "📊";
  font-size: 1rem;
}

.mermaid-actions {
  display: flex;
  gap: 0.5rem;
}

.mermaid-fullscreen-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--entry);
  color: var(--secondary);
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 0;
}

.mermaid-fullscreen-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--theme);
}

.mermaid-fullscreen-btn svg {
  width: 14px;
  height: 14px;
}

/* 容器样式 */
.mermaid-container {
  padding: 1.5rem;
  text-align: center;
  overflow-x: auto;
  overflow-y: visible;
  min-height: 200px;
  width: 100%;
  position: relative;
  /* 确保容器能够容纳大型图表 */
  max-width: none;
  /* 改善滚动条样式 */
  scrollbar-width: thin;
  scrollbar-color: var(--secondary) var(--tertiary);
}

/* 图片样式 */
.mermaid-image {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  transition: all 0.3s ease;
  cursor: pointer;
}

.mermaid-image:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 响应式图片 */
.mermaid-responsive-image {
  width: 100%;
  max-width: 100%;
  height: auto;
  object-fit: contain;
}

/* 响应式容器 */
.mermaid-responsive .mermaid-container {
  max-width: 100%;
  overflow-x: auto;
  /* 确保在响应式模式下图表能够完整显示 */
  overflow-y: visible;
  /* 允许水平滚动查看完整图表 */
  white-space: nowrap;
}

.mermaid-responsive .mermaid-container::-webkit-scrollbar {
  height: 6px;
}

.mermaid-responsive .mermaid-container::-webkit-scrollbar-track {
  background: var(--tertiary);
  border-radius: 3px;
}

.mermaid-responsive .mermaid-container::-webkit-scrollbar-thumb {
  background: var(--secondary);
  border-radius: 3px;
}

.mermaid-responsive .mermaid-container::-webkit-scrollbar-thumb:hover {
  background: var(--primary);
}

/* Mermaid SVG 样式优化 */
.mermaid svg {
  /* 移除最大宽度限制，让图表自然显示 */
  max-width: none;
  min-width: 100%;
  height: auto !important;
  width: auto !important;
  display: block;
  margin: 0 auto;
  border-radius: 6px;
  transition: all 0.3s ease;
  /* 确保 SVG 能够正确缩放 */
  box-sizing: border-box;
}

.mermaid svg:hover {
  transform: scale(1.01);
}

.mermaid .node rect,
.mermaid .node circle,
.mermaid .node ellipse,
.mermaid .node polygon {
  fill: var(--entry);
  stroke: var(--border);
  stroke-width: 1.5px;
  transition: all 0.2s ease;
}

.mermaid .node:hover rect,
.mermaid .node:hover circle,
.mermaid .node:hover ellipse,
.mermaid .node:hover polygon {
  fill: var(--theme);
  stroke: var(--primary);
}

.mermaid .node .label {
  color: var(--primary);
  font-family: var(--font-sans);
  font-weight: 500;
}

.mermaid .edgePath .path {
  stroke: var(--secondary);
  stroke-width: 2px;
  transition: stroke 0.2s ease;
}

.mermaid .edgePath:hover .path {
  stroke: var(--primary);
  stroke-width: 3px;
}

.mermaid .edgeLabel {
  background-color: var(--entry);
  color: var(--primary);
  font-family: var(--font-sans);
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--border);
}

/* 错误回退样式 */
.mermaid-fallback {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 1rem 0;
  overflow: hidden;
}

.mermaid-fallback-header {
  padding: 0.5rem 1rem;
  background: var(--tertiary);
  color: var(--secondary);
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}

.mermaid-fallback pre {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  text-align: left;
  background: transparent;
  padding: 1rem;
  margin: 0;
  white-space: pre-wrap;
  overflow-x: auto;
  color: var(--primary);
}

/* 深色模式适配 */
.dark .mermaid-dynamic,
.dark .mermaid-static {
  border-color: var(--tertiary);
  box-shadow: 0 2px 8px rgba(255, 255, 255, 0.05);
}

.dark .mermaid-dynamic:hover,
.dark .mermaid-static:hover {
  box-shadow: 0 4px 16px rgba(255, 255, 255, 0.1);
}

.dark .mermaid-title {
  background: linear-gradient(135deg, var(--tertiary), var(--code-bg));
  color: var(--primary);
}

.dark .mermaid-fullscreen-btn {
  background: var(--code-bg);
  border-color: var(--tertiary);
}

.dark .mermaid-fullscreen-btn:hover {
  background: var(--entry);
  border-color: var(--primary);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .mermaid-container {
    padding: 1rem;
  }
  
  .mermaid-image {
    max-width: 100%;
  }
}

@media (max-width: 768px) {
  .mermaid-dynamic, .mermaid-static {
    margin: 1rem -1rem;
    border-left: none;
    border-right: none;
    border-radius: 0;
    box-shadow: none;
  }
  
  .mermaid-container {
    padding: 0.75rem;
  }
  
  .mermaid-title {
    padding: 0.5rem 0.75rem;
    font-size: 0.85rem;
  }
  
  .mermaid-title-text::before {
    font-size: 0.9rem;
  }
  
  .mermaid-fullscreen-btn {
    width: 24px;
    height: 24px;
  }
  
  .mermaid-fullscreen-btn svg {
    width: 12px;
    height: 12px;
  }
}

@media (max-width: 480px) {
  .mermaid-container {
    padding: 0.5rem;
    min-height: 150px;
  }
  
  .mermaid-title {
    padding: 0.5rem;
    font-size: 0.8rem;
  }
  
  .mermaid-actions {
    gap: 0.25rem;
  }
}

/* 打印样式 */
@media print {
  .mermaid-dynamic, .mermaid-static {
    border: 1px solid #ccc;
    break-inside: avoid;
    box-shadow: none;
    transform: none;
  }
  
  .mermaid-title {
    background: #f5f5f5 !important;
    color: #333 !important;
  }
  
  .mermaid-actions {
    display: none;
  }
  
  .mermaid-image:hover,
  .mermaid svg:hover {
    transform: none;
  }
}

/* 加载状态 */
.mermaid-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: var(--secondary);
  gap: 1rem;
}

.mermaid-loading::before {
  content: "";
  width: 40px;
  height: 40px;
  border: 3px solid var(--tertiary);
  border-top: 3px solid var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.mermaid-loading::after {
  content: "正在渲染图表...";
  font-size: 0.9rem;
  animation: pulse 1.5s ease-in-out infinite alternate;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes pulse {
  from { opacity: 0.6; }
  to { opacity: 1; }
}

/* 错误状态 */
.mermaid-error {
  background: linear-gradient(135deg, #fee, #fdd);
  border: 1px solid #fcc;
  color: #c33;
  padding: 1rem;
  text-align: center;
  font-size: 0.9rem;
  border-radius: 8px;
  margin: 1rem 0;
}

.dark .mermaid-error {
  background: linear-gradient(135deg, #422, #533);
  border-color: #633;
  color: #faa;
}

/* 全屏模态框样式 */
.mermaid-fullscreen-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
}

.mermaid-fullscreen-modal.active {
  opacity: 1;
  visibility: visible;
}

.mermaid-fullscreen-content {
  max-width: 95%;
  max-height: 95%;
  background: var(--entry);
  border-radius: 12px;
  padding: 2rem;
  position: relative;
  overflow: auto;
}

.mermaid-fullscreen-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 32px;
  height: 32px;
  border: none;
  background: var(--secondary);
  color: var(--entry);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: all 0.2s ease;
}

.mermaid-fullscreen-close:hover {
  background: var(--primary);
  transform: scale(1.1);
}

/* 导出按钮样式 */
.mermaid-export-actions {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: var(--secondary);
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}

.mermaid-export-btn {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--entry);
  color: var(--secondary);
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
}

.mermaid-export-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.mermaid-export-btn svg {
  width: 14px;
  height: 14px;
}

/* 图表类型特定样式 */
.mermaid .flowchart-node {
  cursor: pointer;
}

.mermaid .sequence-diagram .actor {
  cursor: pointer;
}

.mermaid .gantt-task {
  cursor: pointer;
}

/* 可访问性增强 */
.mermaid svg[role="img"] {
  outline: none;
}

.mermaid svg:focus {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* 性能优化 */
.mermaid svg {
  will-change: transform;
}

.mermaid-container {
  contain: layout style paint;
}

/* 大型图表优化 */
.mermaid-container .mermaid {
  /* 确保图表容器能够适应内容 */
  display: inline-block;
  min-width: 100%;
  white-space: normal;
}

/* 修复图表被截断的问题 */
.mermaid svg {
  /* 确保 SVG 视口正确设置 */
  overflow: visible;
}

/* 针对不同类型的图表进行优化 */
.mermaid .flowchart,
.mermaid .sequence-diagram,
.mermaid .gantt,
.mermaid .gitgraph,
.mermaid .mindmap {
  /* 确保图表元素不被裁剪 */
  overflow: visible;
}

/* 移动端大图表处理 */
@media (max-width: 768px) {
  .mermaid-container {
    /* 在移动端允许水平滚动 */
    overflow-x: scroll;
    -webkit-overflow-scrolling: touch;
  }
  
  .mermaid svg {
    /* 移动端保持原始尺寸，通过滚动查看 */
    min-width: auto;
    max-width: none;
  }
}

/* 主题变量覆盖 */
:root {
  --mermaid-primary-color: var(--primary);
  --mermaid-secondary-color: var(--secondary);
  --mermaid-background-color: var(--entry);
  --mermaid-border-color: var(--border);
  --mermaid-text-color: var(--primary);
}
