/* 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: inline-block;
    margin: 0 auto;
    text-align: left;
    position: relative;
    padding-right: 2.75em;
}

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

.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;
}

/* 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;
    }
}

/* 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%); }
}
