/**
 * SAM-MATH Exam Page - Optimized Styles
 * بهینه‌سازی کامل برای موبایل، Safari، و فرمول‌های ریاضی
 */

/* ==========================================
   1. فرمول‌های ریاضی - حذف ریشه‌ای اسکرول
   ========================================== */

/* حذف کامل و دائمی scrollbar از همه فرمول‌ها */
.MathJax, .MJX-TEX, .MathJax_Display,
.katex, .katex-display,
.mjx-chtml, mjx-container, mjx-math,
.math-inline, .math-block, .math-display,
.MJX-display, .mjx-full-width {
    overflow: visible !important;
    overflow-x: visible !important;
    overflow-y: visible !important;
    max-width: 100% !important;
}

/* حذف scrollbar از داخل فرمول‌ها */
.MathJax *, .mjx-chtml *, .katex *,
mjx-container *, mjx-math * {
    overflow: visible !important;
}

/* مستقل از RTL/LTR */
.math-inline, .math-block {
    direction: ltr;
    unicode-bidi: isolate;
}

.mjx-chtml, .mjx-chtml *,
.MathJax, .MathJax * {
    direction: ltr !important;
    unicode-bidi: isolate;
    text-align: left;
}

/* جلوگیری از text-decoration روی فرمول‌ها */
.math-inline, .math-inline *,
.math-block, .math-block *,
.MathJax, .MathJax *,
.katex, .katex *,
.mjx-chtml, .mjx-chtml *,
mjx-container, mjx-container * {
    text-decoration: none !important;
    text-decoration-line: none !important;
}

/* فرمول‌ها بالای border/underline قرار گیرند */
.math-inline, .math-block,
.MathJax, .katex,
.mjx-chtml, mjx-container {
    position: relative;
    z-index: 2 !important;
}

/* فرمول‌های inline: baseline */
.mjx-chtml:not(.MJXc-display),
.MathJax:not(.MJXc-display),
.math-inline .MathJax,
.math-inline .katex,
mjx-container:not([display="true"]) {
    display: inline-block !important;
    vertical-align: baseline !important;
    margin: 0 0.15em;
}

/* فرمول‌های display: block و وسط‌چین */
.mjx-chtml.MJXc-display,
.MathJax.MJXc-display,
.MathJax_Display,
.katex-display,
.math-display,
.math-block,
mjx-container[display="true"],
.MJX-display,
.mjx-full-width {
    display: block !important;
    text-align: center !important;
    margin: 1rem auto !important;
    padding: 0.5rem 0;
}

/* ==========================================
   2. متن سوال و گزینه‌ها
   ========================================== */

.question-text,
.choice-text {
    line-height: 1.85 !important; /* ارتفاع بیشتر برای جلوگیری از رفتن فرمول زیر خط */
    font-size: 16px;
    word-spacing: 0.02em;
}

/* جای خالی با زیرخط - wrapper */
.blank-underline {
    border-bottom: 2px solid currentColor;
    line-height: 2 !important; /* فضای بیشتر برای فرمول */
    display: inline-block;
    min-height: 2em;
}

/* لینک‌ها: زیرخط به فرمول نخورد */
a, .underlined {
    text-decoration-skip-ink: auto;
    text-underline-offset: 0.2em;
}

/* اگر flex است: شکست خط مجاز */
.question-row {
    display: flex;
    flex-wrap: wrap;
    min-width: 0;
}

/* موبایل: فونت کمی بزرگ‌تر */
@media (max-width: 420px) {
    .question-text,
    .choice-text {
        font-size: 17px;
        line-height: 1.8 !important;
    }
}

/* دسکتاپ */
@media (min-width: 992px) {
    .question-text,
    .choice-text {
        font-size: 18px;
    }
}

/* فرمول‌های inline طولانی در موبایل → block فقط اگر بیش از 50% عرض باشد */
@media (max-width: 640px) {
    .question-text .mjx-chtml:not(.MJXc-display),
    .choice-text .mjx-chtml:not(.MJXc-display) {
        max-width: 100%;
    }
    
    /* اگر فرمول inline خیلی طولانی شد، block بشه */
    .question-text .mjx-chtml[style*="width"],
    .choice-text .mjx-chtml[style*="width"] {
        display: inline-block !important;
        max-width: 100%;
    }
}

/* ==========================================
   3. تصاویر سوال
   ========================================== */

.q-figure,
.question-figure {
    margin: 1rem 0;
    text-align: center;
    max-width: 100%;
}

.q-figure img,
.question-figure img,
.question-text img,
.question-image {
    max-width: 70%; /* کوچک‌تر از قبل */
    height: auto;
    display: block;
    margin: 0.75rem auto;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    object-fit: contain;
    cursor: zoom-in; /* نشان می‌دهد قابل بزرگنمایی است */
    transition: all 0.3s ease;
}

.q-figure img:hover,
.question-image:hover {
    transform: scale(1.03);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

/* Clickable image indicator */
.clickable-image {
    position: relative;
}

.clickable-image::after {
    content: '🔍';
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.clickable-image:hover::after {
    opacity: 1;
}

/* تصویر در موبایل */
@media (max-width: 640px) {
    .q-figure img,
    .question-figure img,
    .question-text img {
        max-width: 85%;
        border-radius: 6px;
    }
}

.q-figure figcaption,
.question-figure figcaption {
    font-size: 0.85em;
    color: #667085;
    margin-top: 0.5rem;
    font-style: italic;
}

/* جلوگیری از float */
img[style*="float:"],
.question-text img {
    float: none !important;
}

/* ==========================================
   4. گزینه‌ها - Mobile First
   ========================================== */

.q-choices {
    display: grid;
    gap: 0.75rem;
    margin-top: 1rem;
    margin-bottom: 3rem; /* فاصله بیشتر با دکمه‌های پایین (floating box) */
    grid-auto-rows: 1fr; /* همه ردیف‌ها هم‌اندازه */
}

/* در موبایل فاصله بیشتر */
@media (max-width: 640px) {
    .q-choices {
        margin-bottom: 4rem;
    }
}

/* دسکتاپ: دو ستون فقط اگر options_per_row > 1 */
@media (min-width: 992px) {
    .q-choices.two-cols {
        grid-template-columns: 1fr 1fr;
    }
}

.choice {
    position: relative;
    display: flex;
    flex-direction: column;
}

.choice label {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 1rem;
    border: 2px solid #E4E7EC;
    border-radius: 12px;
    background: #fff;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 44px; /* Touch target */
    height: 100%; /* هم‌اندازه کردن همه گزینه‌ها */
    flex: 1; /* stretch کردن */
}

.choice label:hover {
    border-color: #D0D5DD;
    background: #F9FAFB;
}

.choice:has(input:checked) label {
    border-color: #155EEF;
    background: #EFF6FF;
    box-shadow: 0 0 0 3px rgba(21, 94, 239, 0.08);
}

.choice input[type="radio"] {
    margin-top: 0.25rem;
    min-width: 20px;
    min-height: 20px;
    flex-shrink: 0;
}

.choice:focus-within label {
    outline: 2px solid #84CAFF;
    outline-offset: 2px;
}

/* ==========================================
   17. MathJax – Fix stray middle lines on stretchy braces/arrows
   - Do NOT change normal glyphs; only hide repeating ext/mid pieces
   ========================================== */
/* vertical and horizontal stretchy ext/mid pieces (cause long hairlines) */
mjx-stretchy-v mjx-ext mjx-c::before,
mjx-stretchy-h mjx-ext mjx-c::before,
mjx-stretchy-v mjx-mid mjx-c::before,
mjx-stretchy-h mjx-mid mjx-c::before {
    content: none !important;
}

/* keep caps and ends visible so overall shape remains correct */
mjx-stretchy-v mjx-beg mjx-c::before,
mjx-stretchy-v mjx-end mjx-c::before,
mjx-stretchy-h mjx-beg mjx-c::before,
mjx-stretchy-h mjx-end mjx-c::before {
    display: inline-block !important;
}

/* ==========================================
   5. Header ثابت (sticky) بدون overlap
   ========================================== */

.exam-header {
    position: sticky;
    top: 0;
    z-index: 100;
    background: #0B3B7A;
    color: #fff;
}

body {
    scroll-padding-top: 80px; /* فاصله برای header */
}

/* ==========================================
   6. Autoscale فرمول‌های عریض
   ========================================== */

.math-autoscale {
    transform-origin: left top;
}

[dir="rtl"] .math-autoscale {
    transform-origin: right top;
}

/* ==========================================
   7. کارت سوال
   ========================================== */

.question-card {
    max-width: 920px;
    margin: 0 auto;
    background: #fff;
    border-radius: 14px;
    border: 1px solid #EEE;
    padding: 1.5rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

@media (max-width: 640px) {
    .question-card {
        padding: 1rem;
        border-radius: 10px;
    }
}

/* ==========================================
   8. Navigation Footer
   ========================================== */

.exam-nav-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #fff;
    border-top: 2px solid #E4E7EC;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
    z-index: 90;
}

/* فاصله برای جلوگیری از پوشش محتوا */
.exam-content {
    padding-bottom: 200px; /* فضا برای footer */
}

@media (max-width: 640px) {
    .exam-content {
        padding-bottom: 180px;
    }
}

/* ==========================================
   9. بهینه‌سازی موبایل (iOS Safari)
   ========================================== */

/* جلوگیری از zoom خودکار input در iOS */
@media (max-width: 640px) {
    input[type="radio"] {
        font-size: 16px;
    }
}

/* اسکرول صاف در Safari */
@supports (-webkit-overflow-scrolling: touch) {
    .math-display,
    .mjx-chtml.MJXc-display {
        -webkit-overflow-scrolling: touch;
    }
}

/* ==========================================
   10. Typography بهتر برای فارسی
   ========================================== */

[dir="rtl"] .question-text,
[dir="rtl"] .choice-text {
    word-spacing: 0.03em;
    letter-spacing: -0.01em;
}

/* ==========================================
   11. Badge Difficulty
   ========================================== */

.difficulty-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.375rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
}

.difficulty-easy {
    background: #DCFCE7;
    color: #166534;
}

.difficulty-medium {
    background: #FEF3C7;
    color: #92400E;
}

.difficulty-hard {
    background: #FEE2E2;
    color: #991B1B;
}

/* ==========================================
   12. Loading State
   ========================================== */

.math-loading {
    opacity: 0.5;
    pointer-events: none;
}

/* ==========================================
   13. Print Styles
   ========================================== */

@media print {
    .exam-header,
    .exam-nav-footer {
        display: none !important;
    }
    
    .question-card {
        page-break-inside: avoid;
        box-shadow: none;
        border: 1px solid #ccc;
    }
    
    .mjx-chtml.MJXc-display,
    .MathJax.MJXc-display {
        overflow: visible !important;
    }
}

/* ==========================================
   14. مودال بزرگنمایی عکس
   ========================================== */

.image-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.9);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: zoom-out;
    padding: 2rem;
}

.image-modal img {
    max-width: 95%;
    max-height: 95vh;
    object-fit: contain;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    cursor: default;
}

.image-modal-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    z-index: 10000;
}

.image-modal-close:hover {
    background: white;
    transform: scale(1.1);
}

.image-modal-close svg {
    width: 24px;
    height: 24px;
}

/* ==========================================
   15. دکمه‌های کنترل فونت
   ========================================== */

.font-controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    background: white;
    padding: 0.5rem;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

@media (max-width: 640px) {
    .font-controls {
        padding: 0.375rem;
        gap: 0.375rem;
    }
}

.font-btn {
    width: 40px;
    height: 40px;
    border: 2px solid #E4E7EC;
    border-radius: 8px;
    background: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 14px;
    color: #344054;
    transition: all 0.2s;
    user-select: none;
}

@media (max-width: 640px) {
    .font-btn {
        width: 36px;
        height: 36px;
        font-size: 12px;
    }
}

.font-btn:hover {
    border-color: #155EEF;
    background: #F0F6FF;
    color: #155EEF;
}

.font-btn:active {
    transform: scale(0.95);
}

.font-btn.reset {
    font-size: 16px;
}

/* سایزهای فونت (4 سطح) */
body.font-size-xs .question-text,
body.font-size-xs .choice-text {
    font-size: 14px !important;
    line-height: 1.6 !important;
}

body.font-size-sm .question-text,
body.font-size-sm .choice-text {
    font-size: 16px !important;
    line-height: 1.7 !important;
}

/* پیش‌فرض (md) */
body.font-size-md .question-text,
body.font-size-md .choice-text {
    font-size: 17px !important;
    line-height: 1.75 !important;
}

body.font-size-lg .question-text,
body.font-size-lg .choice-text {
    font-size: 19px !important;
    line-height: 1.8 !important;
}

body.font-size-xl .question-text,
body.font-size-xl .choice-text {
    font-size: 21px !important;
    line-height: 1.85 !important;
}

/* ==========================================
   16. Accessibility
   ========================================== */

/* Focus visible برای keyboard navigation */
.choice:focus-visible {
    outline: 3px solid #84CAFF;
    outline-offset: 2px;
}

/* High contrast mode */
@media (prefers-contrast: high) {
    .choice label {
        border-width: 3px;
    }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
