/* ============================================================================
   SVG RENDERING - Optimized for LaTeX-generated SVGs
   ============================================================================

   Purpose: Ensures high-quality rendering of mathematical SVGs from dvisvgm

   Problems solved:
   - Blurry SVGs (shape-rendering, text-rendering)
   - Incorrect scaling (width/height overrides)
   - Missing content (overflow, clipping)
   - Poor responsive behavior (max-width, auto sizing)

   Used in:
   - Games (question modals, answer buttons)
   - Documents (task preview, PDF generation)
   - Creator (task viewer, advanced search)
   - Admin (task management)
*/

/* ===== GLOBAL SVG RULES ===== */
/* Applied to all LaTeX-generated SVGs */

svg.latex-svg {
    /* High-quality rendering for mathematical content */
    shape-rendering: crispEdges;       /* Better for text/path rendering */
    text-rendering: optimizeLegibility; /* Better text rendering */

    /* Prevent blurring on browser zoom */
    image-rendering: -webkit-optimize-contrast;
    image-rendering: high-quality;

    /* Responsive: Fits container width */
    max-width: 100%;

    /* Prevent content clipping */
    overflow: visible;

    /* Center alignment */
    display: block;
    margin: 0 auto;
}

/* Fix for nested SVG elements (brackets, fractions, matrices) */
svg.latex-svg text,
svg.latex-svg path,
svg.latex-svg use {
    shape-rendering: geometricPrecision;
}

/* ===== CONTEXT-SPECIFIC RULES ===== */

/* Question Modal (Games) - Large, readable formulas */
.question-text svg,
.question-text svg.latex-svg {
    /* Use natural size with constraints */
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 400px;  /* Increased for better readability */

    /* Spacing */
    margin: 20px auto;
}

/* Answer Buttons (Games) - Compact, inline formulas */
.answer-btn svg,
.answer-btn svg.latex-svg {
    /* Use natural size with constraints */
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100px;  /* Increased slightly */

    /* Inline with text */
    display: inline-block;
    vertical-align: middle;
    margin: 5px auto;
}

/* Task Preview (Creator) - Use natural size, allow scrolling */
.task-card__preview svg,
.task-card__preview svg.latex-svg {
    /* Let SVG use its natural size (from post-processing) */
    width: auto;
    height: auto;
    max-width: 100%;
    margin: 0 auto;
}

/* Document/PDF Preview - Print-optimized */
.document-preview svg,
.document-preview svg.latex-svg {
    /* Preserve exact dimensions for PDF */
    width: auto !important;
    height: auto !important;
    max-width: 100%;
}

/* Cart Item Preview - Compact */
.cart-item svg,
.cart-item svg.latex-svg {
    max-height: 60px;
    margin: 5px auto;
}

/* Advanced Search Results - Small thumbnails */
.search-result svg,
.search-result svg.latex-svg {
    max-height: 50px;
    max-width: 100%;
}

/* SVG Modal (Zoom/Magnifier) - Large view */
.svg-modal-content svg,
.svg-modal-content svg.latex-svg {
    width: 90vw !important;      /* Force large display in modal */
    height: auto !important;
    max-height: 85vh !important; /* Don't exceed viewport */
    display: block !important;
    margin: 0 auto !important;
}

/* SVG Preview (Small) - Task Creator, My Tasks, Games Creator */
.task-svg-preview svg,
.task-svg-preview svg.latex-svg,
.preview-latex-rendered svg,
.preview-latex-rendered svg.latex-svg {
    width: auto !important;
    height: auto !important;
    max-width: 100% !important;
    max-height: 100px !important;
    display: block !important;
    margin: 0 auto !important;
}

/* ===== DARK MODE SUPPORT ===== */

/* Invert SVG colors in dark mode (if needed) */
@media (prefers-color-scheme: dark) {
    svg.latex-svg {
        /* Optional: Invert for dark backgrounds */
        /* filter: invert(1); */
    }
}

/* ===== PRINT STYLES ===== */

@media print {
    svg.latex-svg {
        /* Preserve exact dimensions for printing */
        max-width: none !important;
        width: auto !important;
        height: auto !important;

        /* Ensure crisp printing */
        shape-rendering: crispEdges;
        text-rendering: geometricPrecision;
    }
}

/* ===== LOADING STATE ===== */

/* Placeholder while SVG loads */
svg.latex-svg[data-loading="true"] {
    min-height: 40px;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}

@keyframes loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ===== ERROR STATE ===== */

/* Fallback styling for error SVGs */
svg.latex-svg-error {
    fill: #dc3545;
    stroke: #dc3545;
}

svg.latex-svg-error text {
    font-family: monospace;
    font-size: 12px;
}

/* ===== SVG ZOOM BUTTON ===== */

/* Zoom/Magnifier button for SVG previews */
.svg-zoom-btn,
.svg-zoom-btn-cart,
.svg-zoom-btn-finalize {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid #d1d5db;
    border-radius: 0.375rem;
    padding: 0.5rem;
    font-size: 1.25rem;
    cursor: pointer;
    z-index: 10;
    transition: all 0.2s ease;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
}

.svg-zoom-btn:hover,
.svg-zoom-btn-cart:hover,
.svg-zoom-btn-finalize:hover {
    background: white;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    transform: scale(1.1);
}

/* ===== SVG MODAL (ZOOM VIEW) ===== */

/* Full-screen modal overlay for zoomed SVG view */
.svg-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
    z-index: 10000;
    cursor: pointer;
    padding: 2rem;
}

.svg-modal-content {
    background: white;
    padding: 3rem;
    border-radius: 0.75rem;
    max-width: 95vw;
    max-height: 95vh;
    width: 95vw;
    height: 95vh;
    overflow: auto;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    cursor: default;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* SVG inside modal - scale to fill available space */
.svg-modal-content svg {
    display: block !important;
    width: 90vw !important;
    height: auto !important;
    max-height: 85vh !important;
    margin: 0 auto !important;
    object-fit: contain !important;
}

/* ===== SVG PREVIEW CONTAINERS ===== */

/* Container for SVG previews with zoom button */
.task-svg-preview-container,
.explanation-svg-preview-container {
    position: relative;
    display: inline-block;
    width: 100%;
}

/* Small preview SVGs in various contexts */
.task-svg-preview,
.explanation-svg-preview,
.task-svg-preview-small,
.explanation-svg-preview-small,
.task-svg-preview-finalize,
.explanation-svg-preview-finalize {
    cursor: pointer;
    transition: opacity 0.2s ease;
}

.task-svg-preview:hover,
.explanation-svg-preview:hover,
.task-svg-preview-small:hover,
.explanation-svg-preview-small:hover,
.task-svg-preview-finalize:hover,
.explanation-svg-preview-finalize:hover {
    opacity: 0.8;
}
