/**
 * 代码美化增强样式
 * 优化代码字体大小、间距和阅读体验
 */

/* ===== 代码块基础优化 ===== */
.highlight {
  position: relative;
  margin: 2rem 0;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow: 
    0 4px 16px rgba(0, 0, 0, 0.06),
    0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  background: #ffffff;
}

.highlight:hover {
  box-shadow: 
    0 8px 24px rgba(0, 0, 0, 0.1),
    0 4px 8px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

/* ===== 代码容器优化 ===== */
.highlight pre {
  margin: 0;
  padding: 2rem 2.5rem;
  background: #ffffff !important;
  overflow-x: auto;
  line-height: 1.6; /* 适合代码的行高 */
  font-size: 14px; /* 适合代码阅读的字体大小 */
  font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace; /* 等宽字体 */
  font-weight: 400;
  letter-spacing: 0; /* 等宽字体不需要额外间距 */
  color: #24292f; /* 深灰色，易于阅读 */
  border-radius: 12px;
  position: relative;
  white-space: pre !important; /* 强制保持空白字符和换行 */
  word-wrap: normal !important; /* 不自动换行 */
  word-break: normal !important; /* 不强制断词 */
  tab-size: 4 !important; /* 设置制表符宽度 */
  -moz-tab-size: 4 !important; /* Firefox支持 */
}

/* ===== 代码内容优化 ===== */
.highlight code {
  font-family: inherit; /* 继承容器的等宽字体 */
  font-size: inherit;
  line-height: inherit;
  background: none !important;
  padding: 0 !important;
  border: none !important;
  border-radius: 0 !important;
  font-weight: inherit;
  letter-spacing: inherit;
  white-space: pre !important; /* 强制保持空白字符 */
  word-wrap: normal !important; /* 不自动换行 */
  word-break: normal !important; /* 不强制断词 */
}

/* ===== 行号优化 ===== */
.highlight .lntable {
  border-spacing: 0;
  padding: 0;
  margin: 0;
  border: 0;
  width: 100%;
  table-layout: fixed;
}

.highlight .lntd {
  vertical-align: top;
  padding: 0;
  margin: 0;
  border: 0;
}

.highlight .lnt,
.highlight .ln {
  color: rgba(36, 41, 47, 0.4); /* 与新主题颜色一致 */
  user-select: none;
  margin-right: 1.5rem; /* 适中的行号与代码间距 */
  padding-right: 1rem;
  width: 3rem; /* 合适的行号宽度 */
  text-align: right;
  font-size: 12px; /* 行号字体大小 */
  font-weight: 400;
  border-right: 1px solid rgba(36, 41, 47, 0.1);
  line-height: 1.6; /* 与代码行高保持一致 */
  font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace;
}

/* ===== 代码行优化 ===== */
.highlight .line {
  display: block;
  min-height: 1.6em; /* 与行高保持一致 */
  padding: 0; /* 移除额外的行间距 */
  white-space: pre !important; /* 强制保持空白字符 */
  word-wrap: normal !important; /* 不自动换行 */
  word-break: normal !important; /* 不强制断词 */
}

/* ===== 内联代码优化 ===== */
.post-content code:not(.highlight code) {
  background: #f6f8fa !important;
  color: #d73a49 !important;
  padding: 0.2em 0.4em;
  border: 1px solid #e1e4e8 !important;
  border-radius: 4px;
  font-family: 'JetBrains Mono', 'Fira Code', 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace; /* 等宽字体 */
  font-size: 0.85em;
  font-weight: 400;
  letter-spacing: 0;
  white-space: nowrap;
}

.post-content pre code {
  background: transparent !important;
  border: none !important;
  padding: 0;
  border-radius: 0;
  white-space: pre;
}

/* ===== 语言标签优化 ===== */
.highlight::before {
  content: attr(data-lang);
  position: absolute;
  top: 16px;
  right: 20px;
  background: linear-gradient(135deg, #007acc, #0056b3);
  color: white;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  opacity: 0.9;
  z-index: 5;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  box-shadow: 0 2px 8px rgba(0, 122, 204, 0.3);
}

/* ===== 复制按钮样式 ===== */
.highlight .copy-button {
  position: absolute;
  top: 16px;
  right: 80px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 10px;
  font-weight: 600;
  color: #666;
  cursor: pointer;
  transition: all 0.2s ease;
  backdrop-filter: blur(10px);
  z-index: 6;
}

.highlight .copy-button:hover {
  background: #007acc;
  color: white;
  border-color: #007acc;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 122, 204, 0.3);
}

.highlight .copy-button.copied {
  background: #28a745;
  color: white;
  border-color: #28a745;
}

/* ===== 代码高亮主题优化 ===== */
/* 关键字 */
.highlight .k,
.highlight .kc,
.highlight .kd,
.highlight .kn,
.highlight .kp,
.highlight .kr,
.highlight .kt {
  color: #0066cc !important;
  font-weight: 600 !important;
}

/* 字符串 */
.highlight .s,
.highlight .s1,
.highlight .s2,
.highlight .sb,
.highlight .sc,
.highlight .sd,
.highlight .se,
.highlight .sh,
.highlight .si,
.highlight .sx {
  color: #22863a !important;
  font-weight: 500 !important;
}

/* 注释 */
.highlight .c,
.highlight .c1,
.highlight .cm,
.highlight .cp,
.highlight .cs {
  color: #6a737d !important;
  font-style: italic !important;
  font-weight: 400 !important;
}

/* 数字 */
.highlight .m,
.highlight .mf,
.highlight .mh,
.highlight .mi,
.highlight .mo {
  color: #005cc5 !important;
  font-weight: 600 !important;
}

/* 函数名 */
.highlight .n,
.highlight .na,
.highlight .nb,
.highlight .nc,
.highlight .nd,
.highlight .ne,
.highlight .nf,
.highlight .nl,
.highlight .nn,
.highlight .no,
.highlight .nt,
.highlight .nv {
  color: #6f42c1 !important;
  font-weight: 500 !important;
}

/* 操作符 */
.highlight .o,
.highlight .ow {
  color: #d73a49 !important;
  font-weight: 600 !important;
}

/* 标点符号 */
.highlight .p {
  color: #24292e !important;
}

/* ===== 深色主题适配 ===== */
@media (prefers-color-scheme: dark) {
  .highlight {
    border-color: rgba(255, 255, 255, 0.1);
    background: #1e1e1e;
  }
  
  .highlight pre {
    background: #1e1e1e !important;
    color: #d4d4d4;
  }
  
  .highlight .lnt,
  .highlight .ln {
    color: rgba(255, 255, 255, 0.4);
    border-right-color: rgba(255, 255, 255, 0.1);
  }
  
  .post-content code {
    background: #2d2d30 !important;
    color: #dcdcaa !important;
    border-color: rgba(255, 255, 255, 0.1) !important;
  }
  
  .highlight .copy-button {
    background: rgba(45, 45, 48, 0.9);
    color: #cccccc;
    border-color: rgba(255, 255, 255, 0.1);
  }
  
  .highlight .copy-button:hover {
    background: #007acc;
    color: white;
  }
}

/* ===== 主题切换时的过渡 ===== */
[data-theme="dark"] .highlight {
  border-color: rgba(255, 255, 255, 0.1);
  background: #1e1e1e;
}

[data-theme="dark"] .highlight pre {
  background: #1e1e1e !important;
  color: #d4d4d4;
}

[data-theme="dark"] .highlight .lnt,
[data-theme="dark"] .highlight .ln {
  color: rgba(255, 255, 255, 0.4);
  border-right-color: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .post-content code {
  background: #2d2d30 !important;
  color: #dcdcaa !important;
  border-color: rgba(255, 255, 255, 0.1) !important;
}

/* ===== 响应式设计 ===== */
@media (max-width: 1200px) {
  .highlight pre {
    padding: 2rem 2.5rem;
    font-size: 24px; /* 统一为24px */
    line-height: 1.85; /* 稍微调整行高 */
  }
  
  .highlight .lnt,
  .highlight .ln {
    margin-right: 1.8rem;
    padding-right: 1rem;
    width: 3rem;
    font-size: 13px;
    line-height: 1.85;
  }
}

@media (max-width: 768px) {
  .highlight {
    margin: 1.5rem -1rem;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
  
  .highlight pre {
    padding: 1.5rem 1.2rem;
    font-size: 24px; /* 统一为24px */
    line-height: 1.8; /* 保持较好的行高 */
    letter-spacing: 0.03em;
  }
  
  .highlight .lnt,
  .highlight .ln {
    margin-right: 1.5rem;
    padding-right: 0.8rem;
    width: 2.5rem;
    font-size: 12px;
    line-height: 1.8;
  }
  
  .highlight::before {
    top: 12px;
    right: 12px;
    padding: 4px 8px;
    font-size: 9px;
  }
  
  .highlight .copy-button {
    top: 12px;
    right: 60px;
    padding: 4px 6px;
    font-size: 9px;
  }
  
  .post-content code {
    font-size: 0.9em; /* 移动端内联代码也保持较大字体 */
    padding: 0.3em 0.6em;
    letter-spacing: 0.02em;
  }
}

@media (max-width: 480px) {
  .highlight pre {
    padding: 1.2rem 1rem;
    font-size: 24px; /* 统一为24px */
    line-height: 1.75;
    letter-spacing: 0.02em;
  }
  
  .highlight .lnt,
  .highlight .ln {
    margin-right: 1.2rem;
    padding-right: 0.6rem;
    width: 2rem;
    font-size: 11px;
    line-height: 1.75;
  }
}

/* ===== 滚动条优化 ===== */
.highlight pre::-webkit-scrollbar {
  height: 10px;
}

.highlight pre::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.05);
  border-radius: 5px;
}

.highlight pre::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 5px;
  border: 2px solid transparent;
  background-clip: content-box;
}

.highlight pre::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
  background-clip: content-box;
}

/* 深色主题滚动条 */
@media (prefers-color-scheme: dark) {
  .highlight pre::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
  }
  
  .highlight pre::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
  }
  
  .highlight pre::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
  }
}

/* ===== 打印样式 ===== */
@media print {
  .highlight {
    background: white !important;
    border: 1px solid #ccc;
    box-shadow: none;
    break-inside: avoid;
  }
  
  .highlight pre {
    background: white !important;
    color: black !important;
    font-size: 12px;
    line-height: 1.4;
    padding: 1rem;
  }
  
  .highlight::before,
  .highlight .copy-button {
    display: none;
  }
  
  .highlight .lnt,
  .highlight .ln {
    color: #666 !important;
    border-right-color: #ccc !important;
  }
}

/* ===== 代码块焦点状态 ===== */
.highlight:focus-within {
  outline: 2px solid #007acc;
  outline-offset: 2px;
}

/* ===== 代码块选择状态 ===== */
.highlight pre::selection {
  background: rgba(0, 122, 204, 0.2);
}

.highlight pre::-moz-selection {
  background: rgba(0, 122, 204, 0.2);
}

/* ===== 代码块加载动画 ===== */
.highlight.loading {
  opacity: 0.7;
  position: relative;
}

.highlight.loading::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
  animation: codeLoading 1.5s ease-in-out infinite;
}

@keyframes codeLoading {
  0% { left: -100%; }
  100% { left: 100%; }
}

/* ===== 代码块错误状态 ===== */
.highlight.error {
  border-color: #dc3545;
  box-shadow: 0 0 0 2px rgba(220, 53, 69, 0.2);
}

.highlight.error::before {
  background: #dc3545;
  content: 'ERROR';
}

/* ===== 代码块成功状态 ===== */
.highlight.success {
  border-color: #28a745;
  box-shadow: 0 0 0 2px rgba(40, 167, 69, 0.2);
}

.highlight.success::before {
  background: #28a745;
  content: 'SUCCESS';
}

/* ===== 特殊代码类型样式 ===== */
.highlight[data-lang="bash"]::before,
.highlight[data-lang="shell"]::before {
  background: linear-gradient(135deg, #4caf50, #388e3c);
}

.highlight[data-lang="javascript"]::before,
.highlight[data-lang="js"]::before {
  background: linear-gradient(135deg, #f7df1e, #e6c200);
  color: #000;
}

.highlight[data-lang="python"]::before {
  background: linear-gradient(135deg, #3776ab, #2d5aa0);
}

.highlight[data-lang="go"]::before {
  background: linear-gradient(135deg, #00add8, #007d9c);
}

.highlight[data-lang="rust"]::before {
  background: linear-gradient(135deg, #ce422b, #a33620);
}

.highlight[data-lang="typescript"]::before,
.highlight[data-lang="ts"]::before {
  background: linear-gradient(135deg, #3178c6, #2c5aa0);
}

.highlight[data-lang="css"]::before {
  background: linear-gradient(135deg, #1572b6, #0e4a6b);
}

.highlight[data-lang="html"]::before {
  background: linear-gradient(135deg, #e34f26, #c13818);
}

.highlight[data-lang="json"]::before {
  background: linear-gradient(135deg, #000000, #333333);
}

.highlight[data-lang="yaml"]::before,
.highlight[data-lang="yml"]::before {
  background: linear-gradient(135deg, #cb171e, #a01317);
}

.highlight[data-lang="dockerfile"]::before {
  background: linear-gradient(135deg, #2496ed, #1a7bb8);
}

.highlight[data-lang="sql"]::before {
  background: linear-gradient(135deg, #336791, #2a5373);
}

/* ===== 代码块内容增强 ===== */
.highlight .highlight-line {
  background: rgba(255, 255, 0, 0.1);
  display: block;
  margin: 0 -2.5rem;
  padding: 0 2.5rem;
  border-left: 4px solid #ffc107;
}

.highlight .highlight-line.error {
  background: rgba(220, 53, 69, 0.1);
  border-left-color: #dc3545;
}

.highlight .highlight-line.success {
  background: rgba(40, 167, 69, 0.1);
  border-left-color: #28a745;
}

.highlight .highlight-line.info {
  background: rgba(0, 123, 255, 0.1);
  border-left-color: #007bff;
}

.highlight .highlight-line.warning {
  background: rgba(255, 193, 7, 0.1);
  border-left-color: #ffc107;
}

/* ===== 代码折叠功能 ===== */
.code-fold {
  position: relative;
}

.code-fold-toggle {
  position: absolute;
  top: 16px;
  left: 16px;
  background: rgba(0, 0, 0, 0.1);
  border: none;
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 10px;
  cursor: pointer;
  color: #666;
  transition: all 0.2s ease;
  z-index: 5;
}

.code-fold-toggle:hover {
  background: rgba(0, 0, 0, 0.2);
  color: #333;
}

.code-fold.collapsed .highlight pre {
  max-height: 200px;
  overflow: hidden;
  position: relative;
}

.code-fold.collapsed .highlight pre::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: linear-gradient(transparent, #ffffff);
  pointer-events: none;
}

/* ===== 代码差异显示 ===== */
.highlight .line.added {
  background: rgba(40, 167, 69, 0.1);
  border-left: 4px solid #28a745;
  margin: 0 -2.5rem;
  padding: 0 2.5rem;
}

.highlight .line.removed {
  background: rgba(220, 53, 69, 0.1);
  border-left: 4px solid #dc3545;
  margin: 0 -2.5rem;
  padding: 0 2.5rem;
}

.highlight .line.modified {
  background: rgba(255, 193, 7, 0.1);
  border-left: 4px solid #ffc107;
  margin: 0 -2.5rem;
  padding: 0 2.5rem;
}

/* ===== 代码性能优化 ===== */
.highlight {
  contain: layout style paint;
  will-change: transform;
}

.highlight pre {
  contain: layout style;
}

/* ===== 无障碍访问优化 ===== */
.highlight[role="region"] {
  outline: none;
}

.highlight[aria-label]::before {
  content: attr(aria-label) " - " attr(data-lang);
}

/* 高对比度模式支持 */
@media (prefers-contrast: high) {
  .highlight {
    border-width: 2px;
    border-color: #000;
  }
  
  .highlight pre {
    background: #fff !important;
    color: #000 !important;
  }
  
  .highlight .lnt,
  .highlight .ln {
    color: #666 !important;
    border-right-color: #000 !important;
  }
}

/* 减少动画模式支持 */
@media (prefers-reduced-motion: reduce) {
  .highlight,
  .highlight *,
  .highlight::before,
  .highlight::after {
    animation: none !important;
    transition: none !important;
  }
}
