/* Enhanced styles for robust markdown rendering */
/* File: css/markdown-enhanced.css */

/* KaTeX error handling styles */
.katex-error {
    background-color: #ffeaea;
    border: 1px solid #ff6b6b;
    border-radius: 4px;
    padding: 2px 6px;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.9em;
    color: #d63031;
    position: relative;
    cursor: help;
}

.katex-error.katex-block {
    display: block;
    margin: 8px 0;
    padding: 8px 12px;
    background-color: #fff5f5;
    border-left: 4px solid #ff6b6b;
    border-radius: 0 4px 4px 0;
}

.katex-error.katex-inline {
    display: inline;
    margin: 0 2px;
}

.katex-error:hover::after {
    content: attr(title);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background-color: #2d3748;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    z-index: 1000;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.katex-error:hover::before {
    content: '';
    position: absolute;
    bottom: calc(100% - 4px);
    left: 50%;
    transform: translateX(-50%);
    border: 4px solid transparent;
    border-top-color: #2d3748;
    z-index: 1000;
}

/* Missing image styles */
.missing-image {
    display: inline-block;
    background-color: #f7fafc;
    border: 2px dashed #cbd5e0;
    border-radius: 4px;
    padding: 8px 12px;
    color: #718096;
    font-style: italic;
    font-size: 0.9em;
    cursor: help;
    position: relative;
}

.missing-image:hover {
    background-color: #edf2f7;
    border-color: #a0aec0;
    color: #4a5568;
}

.missing-image::before {
    content: '🖼️';
    margin-right: 6px;
}

/* Markdown error container */
.markdown-error {
    background-color: #fed7d7;
    border: 1px solid #fc8181;
    border-radius: 6px;
    padding: 12px 16px;
    margin: 8px 0;
    color: #c53030;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

.markdown-error::before {
    content: '⚠️ ';
    font-weight: bold;
}

/* Enhanced KaTeX display improvements */
.katex-block {
    margin: 16px 0;
    text-align: center;
    overflow-x: auto;
    padding: 12px 16px;
    box-sizing: border-box;
    position: relative;
}

.katex-block .katex-display {
    display: block;          /* 使用块级，保持 100% 宽度，便于右标绝对定位到最右侧 */
    width: 100%;
    margin: 0 auto;
    text-align: center;      /* 保持 KaTeX 默认：使内部 .katex 居中 */
    position: relative;
    /* 预留更充足的右侧空间给公式右标（如 \tag 或编号） */
    padding-right: 4.25em;
}

.katex-block .katex-display .katex-tag {
    right: 0;
}

/* 在未包裹 .katex-block 的情况下，兜底为 .katex-display 也预留右侧空间，避免右标与公式内容挤在一起 */
.katex-display {
    display: block !important; /* 确保占满一行，右标定位到最右侧 */
    width: 100%;
    position: relative;
    padding-right: 4.25em;
    text-align: center; /* 与 KaTeX 默认一致，内部 .katex 居中 */
}

/* 兼容 KaTeX 默认的 ".tag" 类与本项目历史使用的 ".katex-tag" 类 */
.katex-display .katex-tag,
.katex-display .tag,
.katex .katex-tag,
.katex .tag {
    position: absolute;
    right: 0;
    top: 0;
    height: 100%;
    display: inline-flex;
    align-items: center;
    white-space: nowrap;
    /* 给公式主体与右标之间留出一点视觉间距 */
    margin-left: 0.5em;
}

/* 防止公式主体撑到与右标重叠：受限其最大宽度，给右侧预留的 4.25em 空间 */
.katex-display > .katex {
    max-width: calc(100% - 4.25em);
}

.katex-inline {
    margin: 0 1px;
}

.katex-fallback {
    background-color: #fff5f5;
    border: 1px dashed #f56565;
    border-radius: 6px;
    color: #c53030;
    font-family: 'Courier New', Consolas, monospace;
}

.katex-fallback.katex-block {
    text-align: left;
    padding: 12px 16px;
    white-space: normal;
}

.katex-fallback.katex-inline {
    display: inline-flex;
    align-items: center;
    padding: 0 6px;
}

.katex-fallback-source {
    white-space: pre-wrap;
    word-break: break-word;
}

/* 防止数学公式被段内换行算法拆分，尤其是翻译页/分块对比页的 markdown-body 全局换行规则 */
.markdown-body .katex,
.markdown-body .katex * {
    word-break: normal !important;
    overflow-wrap: normal !important;
}

/* 追加：确保 .katex-display 的 padding 不会导致容器溢出 */
.katex-block .katex-display,
.katex-display {
    box-sizing: border-box;
}

/* Performance indicator styles */
.markdown-metrics {
    position: fixed;
    top: 10px;
    right: 10px;
    background-color: rgba(0,0,0,0.8);
    color: white;
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 11px;
    font-family: monospace;
    z-index: 10000;
    display: none;
}

.markdown-metrics.show {
    display: block;
}

.markdown-metrics .metric {
    display: block;
    margin: 2px 0;
}

.markdown-metrics .metric.error {
    color: #ff6b6b;
}

.markdown-metrics .metric.success {
    color: #51cf66;
}

/* Responsive improvements for formulas */
@media (max-width: 768px) {
    .katex-block {
        font-size: 0.9em;
        padding: 8px 12px;
        margin: 12px 0;
    }
    
    .katex-fallback.katex-block {
        padding: 8px 12px;
    }
    
    .katex-fallback.katex-inline {
        max-width: 100%;
        padding: 0 4px;
        white-space: pre-wrap;
    }
    
    .katex-error.katex-block {
        padding: 6px 8px;
    }

    .katex-error:hover::after {
        font-size: 10px;
        max-width: 200px;
        white-space: normal;
        text-align: center;
    }

    /* 移动端进一步缩小公式并减少右侧留白 */
    .katex-display {
        padding-right: 3.25em !important;
    }
    .katex-display > .katex {
        max-width: calc(100% - 3.25em) !important;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .katex-error {
        background-color: #4a1c1c;
        border-color: #e53e3e;
        color: #feb2b2;
    }

    .katex-error.katex-block {
        background-color: #2d1212;
        border-left-color: #e53e3e;
    }

    .katex-fallback {
        background-color: #3c1a1a;
        border-color: #e53e3e;
        color: #fed7d7;
    }

    .missing-image {
        background-color: #2d3748;
        border-color: #4a5568;
        color: #a0aec0;
    }
    
    .missing-image:hover {
        background-color: #4a5568;
        border-color: #718096;
        color: #e2e8f0;
    }
    
    .markdown-error {
        background-color: #742a2a;
        border-color: #e53e3e;
        color: #feb2b2;
    }
}

/* Accessibility improvements */
.katex-error:focus,
.missing-image:focus {
    outline: 2px solid #4299e1;
    outline-offset: 2px;
}

/* Animation for error states */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-2px); }
    75% { transform: translateX(2px); }
}

.katex-error.katex-inline:hover {
    animation: shake 0.3s ease-in-out;
}

/* Loading states */
.markdown-processing {
    position: relative;
    opacity: 0.7;
}

.markdown-processing::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}
