/* ============================================================================
   CSS RESET & GLOBAL VARIABLES
   ============================================================================ */

:root {
    /* Improved Color Variables with better contrast */
    --primary: #2563eb;
    /* Brighter blue for better visibility */
    --primary-light: #60a5fa;
    --primary-dark: #1d4ed8;
    --bg: #ffffff;
    --text: #1a1a1a;
    /* Darker text for better contrast */
    --card: #f8fafc;
    --border: #e2e8f0;
    --success: #059669;
    --warning: #d97706;
    --error: #dc2626;

    /* Language-specific colors with better contrast */
    --thai-color: #1d4ed8;
    /* Thai text - brighter blue */
    --english-color: #059669;
    /* English text - green */
    --farsi-color: #b91c1c;
    /* Farsi text - red */
}

.dark-theme {
    --bg: #0f172a;
    /* Darker blue-gray background */
    --text: #f1f5f9;
    /* Lighter text for contrast */
    --card: #1e293b;
    --border: #334155;
    --primary: #3b82f6;
    /* Brighter blue in dark mode */
    --primary-light: #60a5fa;
    --primary-dark: #1d4ed8;
    --success: #10b981;
    --warning: #f59e0b;
    --error: #ef4444;

    /* Language colors for dark theme */
    --thai-color: #60a5fa;
    /* Light blue for Thai */
    --english-color: #34d399;
    /* Light green for English */
    --farsi-color: #f87171;
    /* Light red for Farsi */
}

/* Material Icons Fix */
.material-icons {
    font-family: 'Material Icons';
}

/* ============================================================================
   BASE STYLES & TYPOGRAPHY - ENHANCED CONTRAST
   ============================================================================ */

body {
    margin: 0;
    padding-top: 0;
    background: var(--bg);
    color: var(--text);
    transition: background 0.3s, color 0.3s;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Font Family Definitions */
body,
body.font-standard {
    font-family: 'Noto Sans Thai', sans-serif;
}

body.font-thai-modern {
    font-family: 'Kanit', sans-serif;
}

body.font-serif {
    font-family: 'Noto Serif Thai', serif;
}

body.font-fa-vazir {
    font-family: 'Vazirmatn', sans-serif;
}

/* Language-specific text direction */
:lang("fa") {
    direction: rtl;
    text-align: right;
}

:lang(th),
:lang(en) {
    direction: ltr;
    text-align: left;
}

/* Enhanced text contrast for all languages */
h1,
h2,
h3,
h4,
h5,
h6 {
    color: var(--text);
    font-weight: 700;
}

/* ============================================================================
   LAYOUT & STRUCTURE
   ============================================================================ */

/* Toolbar */
#app-toolbar {
    position: sticky;
    top: 0;
    width: 100%;
    height: 56px;
    background: var(--primary);
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 1000;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toolbar-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: background 0.2s;
}

.toolbar-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Main Content Areas */
#main-content {
    position: relative;
    z-index: 1;
}

/* Cards & Containers */
.card,
.card-help {
    background: var(--card);
    margin: 12px;
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    border: 1px solid var(--border);
}

.card-help {
    margin: 0;
    padding: 5px;
}

.dark-theme .card,
.dark-theme .card-help {
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
}

/* ============================================================================
   MEDIA PLAYER COMPONENT
   ============================================================================ */

#media-player-container:empty {
    height: 0;
    overflow: hidden;
    display: none;
}

#media-player-container:not(:empty) {
    display: block;
    position: sticky;
    top: 56px;
    z-index: 950;
    animation: slideDown 0.3s ease;
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.media-row {
    position: sticky;
    top: 56px;
    width: 100%;
    height: 50px;
    background: var(--card);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 4px;
    box-sizing: border-box;
    border-bottom: 2px solid var(--primary);
    z-index: 950;
    margin-bottom: 10px;
    transition: transform 0.3s ease, opacity 0.3s ease;
}

.media-col {
    display: flex;
    align-items: center;
    gap: 4px;
}

.media-col.middle {
    flex: 1;
    min-width: 0;
}

/* Media Controls */
.player-ctrl {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    font-size: 24px;
    padding: 4px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.player-ctrl:hover {
    background: rgba(0, 0, 0, 0.05);
}

.media-badge {
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 0.75rem;
    font-weight: bold;
    min-width: 48px;
    height: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.media-badge:hover {
    background: var(--primary-dark);
    transform: translateY(-1px);
}

.media-text-display {
    color: var(--text);
    font-size: 0.85rem;
    font-weight: 500;
    opacity: 0.9;
    padding: 0 8px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

/* ============================================================================
   DOCUMENT VIEW STYLES - ENHANCED THAI TEXT CONTRAST
   ============================================================================ */

.document-content {
    position: relative;
    z-index: 1;
}

.document-content>.doc-title {
    margin: 0 12px 15px;
    padding: 15px;
    background: var(--card);
    border-radius: 8px;
    font-size: 1.4rem;
    /*    text-align: center; */
}

/* Section Details */
.section-details {
    border-bottom: 1px solid #ddd;
    background: var(--card);
    clear: both;
}

.section-details summary {
    padding: 15px;
    font-weight: bold;
    cursor: pointer;
    outline: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
}

.section-title-text {
    flex-grow: 1;
}

.section-card {
    margin: 0;
    padding: 15px;
    contain: layout style paint;
}

/* Activity Buttons */
.doc-activity-row {
    display: flex;
    gap: 0.5rem;
    margin: 1rem 0;
    padding: 0 10px 15px;
    flex-wrap: wrap;
}

.doc-quiz-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--primary);
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: opacity 0.2s;
    flex: 1;
    min-width: 0;
    justify-content: center;
}

.doc-quiz-btn:hover {
    opacity: 0.9;
}

.doc-quiz-icon {
    font-size: 16px !important;
}

.doc-quiz-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.section-activity-inline {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    vertical-align: middle;
    margin-left: auto;
}

.section-quiz-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    background: var(--primary);
    color: white;
    border: none;
    padding: 4px 8px;
    border-radius: 12px;
    cursor: pointer;
    font-size: 0.75rem;
    transition: opacity 0.2s;
}

.doc-quiz-btn,
.section-quiz-btn {
    background: var(--primary);
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.2s;
    font-weight: 600;
}

.doc-quiz-btn:hover,
.section-quiz-btn:hover {
    background: var(--primary-dark);
    transform: translateY(-1px);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.section-quiz-btn:hover {
    opacity: 0.9;
}

.section-quiz-icon {
    font-size: 14px !important;
}

/* ============================================================================
   LIBRARY COMPONENT
   ============================================================================ */

.library-container {
    padding: 0 12px;
    margin-top: 10px;
}

.library-section {
    margin-bottom: 8px;
    border-radius: 8px;
    background: var(--card);
    overflow: hidden;
    border: 1px solid var(--border);
}

.library-summary {
    padding: 14px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    list-style: none;
    font-weight: 500;
    font-size: 1rem;
    color: var(--text);
    transition: background-color 0.2s;
}

.library-summary:hover {
    background-color: rgba(0, 0, 0, 0.03);
}

.dark-theme .library-summary:hover {
    background-color: rgba(255, 255, 255, 0.05);
}

.library-summary::-webkit-details-marker {
    display: none;
}

.library-summary .material-icons {
    font-size: 20px;
    transition: transform 0.2s;
    color: var(--primary);
    flex-shrink: 0;
}

.library-section[open] .library-summary .material-icons {
    transform: rotate(180deg);
}

.library-title {
    flex-grow: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.library-count {
    font-size: 0.85rem;
    opacity: 0.7;
    flex-shrink: 0;
}

.library-docs {
    padding: 0 0 8px 0;
    border-top: 1px solid rgba(0, 0, 0, 0.08);
}

.dark-theme .library-docs {
    border-top-color: rgba(255, 255, 255, 0.1);
}

.doc-btn {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 12px 16px;
    background: none;
    border: none;
    text-align: left;
    color: var(--text);
    cursor: pointer;
    transition: background-color 0.2s;
    gap: 12px;
    border-radius: 0;
}

.doc-btn:hover {
    background-color: rgba(0, 0, 0, 0.03);
}

.dark-theme .doc-btn:hover {
    background-color: rgba(255, 255, 255, 0.05);
}

.doc-btn-icon {
    font-size: 20px;
    color: var(--primary);
    opacity: 0.8;
    flex-shrink: 0;
}

.doc-btn-text {
    flex-grow: 1;
    font-size: 0.95rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.doc-btn-arrow {
    font-size: 20px;
    opacity: 0.5;
    flex-shrink: 0;
}

/* ============================================================================
   SENTENCE & WORD COMPONENTS
   ============================================================================ */

.sentence-group {
    display: inline-block !important;
    vertical-align: top;
    margin: 0 1rem 1rem 0;
}

.stack-column {
    display: inline-flex;
    flex-direction: column;
    vertical-align: top;
}

.stack-item {
    width: 100%;
    display: block;
}

/* Source sentence (Thai) - Now using the Thai-specific color */
.stack-item.source {
    font-size: 1.2rem;
    color: var(--thai-color);
    font-weight: 600;
}

.stack-item.trans {
    font-size: 0.9rem;
    opacity: 0.8;
}

/* Word blocks */
.sent-word-block,
.words-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 1.25rem;
    margin: 0 0 1rem 0;
}

.sent-word-item,
.word-card {
    display: inline-flex;
    flex-direction: column;
    vertical-align: top;
    border-radius: 6px;
    transition: background-color 0.2s;
    cursor: pointer;
}

.sent-word-item:hover {
    border-color: var(--primary);
}

/* Word blocks - Enhanced Thai source */
.sent-word-source {
    color: var(--thai-color);
    font-weight: 600;
}

/* Word source in word cards */
.word-source {
    color: var(--thai-color);
    font-weight: 600;
    font-size: 1.1rem;
}

.sent-word-trans {
    font-size: 0.85em;
    opacity: 0.8;
}

/* Thai spans in source text */
.word-span {
    display: inline-block;
    border-radius: 3px;
    transition: background-color 0.1s ease;
    cursor: pointer;
    border-bottom: 1px solid transparent;
    transition: all 0.2s;
    will-change: background-color, color;
    color: var(--thai-color);
}

.word-span:hover {
    color: var(--thai-color);
    border-bottom-color: var(--thai-color);
    background-color: rgba(37, 99, 235, 0.1);
}

.dark-theme .word-span:hover {
    background-color: rgba(96, 165, 250, 0.15);
}

/* Active highlighting - Enhanced for better visibility */
.active-highlight {
    background-color: #fef3c7;
    /* Amber 100 */
    color: #92400e;
    /* Amber 800 */
    border-radius: 4px;
    padding: 2px 4px;
    margin: -2px -4px;
}

.dark-theme .active-highlight {
    background-color: #fbbf24;
    /* Amber 400 */
    color: #78350f;
    /* Amber 900 */
}

/* Language-specific text colors - Enhanced contrast */
.lang-fa {
    color: var(--farsi-color);
    font-weight: 500;
}

.lang-en {
    color: var(--english-color);
    font-weight: 500;
}

/* ============================================================================
   SETTINGS OVERLAY
   ============================================================================ */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.overlay-full {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    max-width: 400px;
    max-height: 80vh;
    overflow-y: auto;
    z-index: 2500;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    padding: 20px;
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #ddd;
    margin-bottom: 10px;
}

.settings-header h2 {
    font-size: 1.5rem;
    /* Same as previous h3 size */
    margin: 0;
    flex-grow: 1;
}

.settings-sliders {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 20px;
    padding: 10px;
    background: rgba(0, 0, 0, 0.02);
    border-radius: 8px;
}

.control-row-inline {
    display: grid;
    grid-template-columns: 60px 1fr 40px;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.control-row-inline label {
    font-size: 0.85rem;
    font-weight: bold;
}

.val-label {
    font-size: 0.8rem;
    text-align: right;
    color: var(--primary);
}

.overlay-menu {
    position: fixed;
    top: 60px;
    right: 10px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    min-width: 150px;
}

.overlay-menu button {
    padding: 12px;
    border: none;
    background: none;
    color: var(--text);
    text-align: left;
    cursor: pointer;
    border-bottom: 1px solid #ddd;
}

/* Voice selector */
#voice-select {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    padding: 8px;
    margin-bottom: 15px;
    border: 1px solid var(--primary);
    border-radius: 4px;
    background: var(--card);
    color: var(--text);
}

/* Table styles */
table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
}

th,
td {
    text-align: left;
    padding: 8px;
    border-bottom: 1px solid #ddd;
}

[dir="rtl"] th,
[dir="rtl"] td {
    text-align: right;
}

/* ============================================================================
   QUIZ SYSTEM - ENHANCED VISIBILITY
   ============================================================================ */

.quiz-container {
    position: relative;
    z-index: 1;
    max-width: 500px;
    margin: 10px auto;
    padding: 0 1rem;
    /*    text-align: center; */
}

.quiz-header {
    margin-bottom: 1rem;
}

.quiz-header h2 {
    margin: 0 0 10px 0;
    font-size: 1.2rem;
}

.quiz-progress-container {
    width: 100%;
    height: 6px;
    background: #e0e0e0;
    border-radius: 3px;
    margin: 8px 0;
    overflow: hidden;
}

.quiz-progress-fill {
    height: 100%;
    background: var(--primary);
    transition: width 0.3s ease;
}

.quiz-lang-selectors {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

.quiz-lang-selectors select {
    padding: 4px 8px;
    font-size: 0.9rem;
    border-radius: 4px;
    border: 1px solid var(--primary);
    background: var(--card);
    color: var(--text);
}

/* Quiz Cards */
.quiz-question-card,
.quiz-option-btn {
    position: relative;
    display: flex;
    align-items: center;
    background: var(--card);
    color: var(--text);
    border: 2px solid #ddd;
    border-radius: 8px;
    min-height: 70px;
    padding: 1rem 1rem 1rem 3.5rem;
    transition: all 0.2s;
    /*    text-align: center; */
}

.quiz-question-card {
    border-color: var(--primary);
    margin-bottom: 1.5rem;
    background: var(--card);
    border-width: 2px;
}

.quiz-question-card:active {
    transform: scale(0.98);
}

.quiz-question-text {
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--thai-color);
    /* Using Thai color for questions */
    line-height: 1.2;
}

.quiz-options-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
}

.quiz-option-btn {
    position: relative;
    padding: 1rem 0.5rem 1rem 2.5rem;
    font-size: 1rem;
    font-weight: 500;
    min-height: 60px;
    display: flex;
    align-items: center;
    /*    justify-content: center; */
    background: var(--card);
    color: var(--text);
    border: 2px solid var(--border);
    cursor: pointer;
    line-height: 1.1;
    transition: all 0.2s;
}

.quiz-option-btn:hover {
    border-color: var(--primary);
    background: rgba(37, 99, 235, 0.05);
}

.dark-theme .quiz-option-btn:hover {
    background: rgba(59, 130, 246, 0.1);
}

.quiz-option-btn.correct {
    background-color: #4caf50 !important;
    color: white;
}

.quiz-option-btn.incorrect {
    background-color: #f44336 !important;
    color: white;
}

/* Quiz audio icons */
.audio-preview-icon {
    position: absolute;
    left: 3px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 24px !important;
    color: var(--primary);
    cursor: pointer;
    padding: 8px;
    z-index: 2;
}

.audio-preview-icon:hover {
    background: rgba(37, 99, 235, 0.15);
    border-radius: 50%;
}

.dark-theme .audio-preview-icon:hover {
    background: rgba(59, 130, 246, 0.2);
}

/* Review section */
.review-list {
    /*    text-align: left; */
    margin-top: 1.5rem;
}

.review-item {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    margin-bottom: 8px;
}

.quiz-footer-btns {
    display: flex;
    gap: 10px;
    margin-top: 15px;
}

/* ============================================================================
   HELP & JSON GENERATOR
   ============================================================================ */

.btn-activity {
    background: var(--primary);
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 4px;
    cursor: pointer;
    text-transform: capitalize;
}

.btn-flex {
    flex: 1;
}

.gen-options {
    margin-bottom: 12px;
    display: flex;
    gap: 15px;
    align-items: center;
}

.gen-input {
    width: 100%;
    padding: 8px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

.json-output-box {
    margin-top: 10px;
    display: none;
    background: #222;
    color: #0f0;
    padding: 10px;
    overflow-x: auto;
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9rem;
}

/* ============================================================================
   RESPONSIVE DESIGN
   ============================================================================ */

@media (max-width: 768px) {
    .document-content>.doc-title {
        margin: 0 8px 12px;
        padding: 12px;
        font-size: 1.2rem;
    }

    .section-details summary {
        padding: 12px;
    }

    .section-activity-inline {
        margin-left: 10px;
    }

    .section-quiz-btn {
        padding: 3px 6px;
        font-size: 0.7rem;
    }
}

@media (max-width: 480px) {
    .document-content>.doc-title {
        margin: 0 6px 10px;
        padding: 10px;
        font-size: 1.1rem;
    }

    .section-details summary {
        padding: 10px;
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .section-activity-inline {
        margin-left: 0;
        width: 100%;
        justify-content: flex-start;
    }

    .library-container {
        padding: 0 8px;
    }

    .library-summary {
        padding: 12px 14px;
        font-size: 0.95rem;
    }

    .doc-btn {
        padding: 10px 14px;
    }

    .doc-activity-row {
        padding: 0 8px 12px;
    }

    .doc-quiz-btn {
        padding: 6px 10px;
        font-size: 0.8rem;
    }
}

@media (max-width: 360px) {
    .library-container {
        padding: 0 6px;
    }

    .library-summary {
        padding: 10px 12px;
        font-size: 0.9rem;
        gap: 10px;
    }

    .doc-btn {
        padding: 8px 12px;
        gap: 10px;
    }

    .doc-btn-text {
        font-size: 0.9rem;
    }
}

/* Special cases for activity buttons */
.doc-activity-row:has(button:only-child) {
    justify-content: center;
}

.doc-activity-row:has(button:nth-child(2):last-child) button {
    flex: 0 1 calc(50% - 4px);
}

/* ============================================================================
   ACCESSIBILITY ENHANCEMENTS
   ============================================================================ */

/* Focus states for better keyboard navigation */
button:focus-visible,
input:focus-visible,
select:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --primary: #0056b3;
        --text: #000000;
        --thai-color: #0000ff;
        --english-color: #006400;
        --farsi-color: #8b0000;
    }

    .dark-theme {
        --primary: #4da6ff;
        --text: #ffffff;
        --thai-color: #add8e6;
        --english-color: #90ee90;
        --farsi-color: #ffb6c1;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}