/* ============================================================================
   FILTER MODAL COMPONENT
   ============================================================================
   
   Modal overlay for filtering matches and documents.
   Includes tabbed interface for Death Year, Authors, Titles, and Genres filters.
   Designed for RTL (Arabic) layout with responsive behavior.
   
   ============================================================================ */

/* ============================================================================
   MODAL OVERLAY & BACKDROP
   ============================================================================ */

.filter-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* ============================================================================
   MODAL CONTENT CONTAINER
   ============================================================================ */

.filter-modal-content {
    background: var(--color-white);
    border-radius: var(--radius-lg, 12px);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    width: 90%;
    max-width: 900px;
    max-height: 85vh;
    /* Clamp hard: tall tab content must scroll inside .filter-content,
       never grow the modal past the viewport (which scrolled the header
       and tab bar out of view) */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    position: relative;
    animation: slideUp 0.3s ease-out;
    direction: rtl; /* RTL for Arabic */
}

/* Header, tab bar, and footer stay fixed; only the content pane scrolls */
.filter-modal-header,
.filter-tabs,
.filter-modal-footer {
    flex-shrink: 0;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* ============================================================================
   MODAL HEADER
   ============================================================================ */

.filter-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid #e0e0e0;
    background: var(--color-white);
    border-radius: var(--radius-lg, 12px) var(--radius-lg, 12px) 0 0;
}

.filter-modal-header h2 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--color-text-primary, #333);
    font-family: var(--font-arabic, 'Noto Sans Arabic', sans-serif);
}

.close-button {
    background: none;
    border: none;
    font-size: 1.75rem;
    line-height: 1;
    color: #666;
    cursor: pointer;
    padding: 4px 8px;
    transition: all 0.2s ease;
    border-radius: var(--radius-sm, 4px);
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    min-height: 36px;
}

.close-button:hover {
    background: rgba(0, 0, 0, 0.05);
    color: #333;
}

.close-button:focus {
    outline: 2px solid var(--color-primary, #8B4513);
    outline-offset: 2px;
}

/* ============================================================================
   TAB NAVIGATION (Critical for switching between filters)
   ============================================================================ */

.filter-tabs {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 24px;
    background: #f8f9fa;
    border-bottom: 2px solid #e0e0e0;
    /* Wrap onto a second line instead of scrolling. There are only a handful
       of short tabs, so `overflow-x: auto` here just produced a stray 4px
       scrollbar whenever they were a pixel too wide — it never actually
       needed to scroll. */
    overflow: visible;
}

.filter-tab {
    position: relative;
    padding: 14px 20px;
    background: none;
    border: none;
    font-size: 0.9375rem;
    font-family: var(--font-arabic, 'Noto Sans Arabic', sans-serif);
    color: #666;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    border-bottom: 3px solid transparent;
    margin-bottom: -2px; /* Overlap with container border */
}

.filter-tab:hover:not(.active) {
    background: rgba(0, 0, 0, 0.03);
    color: #333;
}

.filter-tab.active {
    color: var(--color-primary, #8B4513);
    font-weight: 600;
    background: var(--color-white);
    border-bottom-color: var(--color-primary, #8B4513);
}

.filter-tab:focus {
    outline: none;
    background: rgba(139, 69, 19, 0.1);
}

/* ============================================================================
   FILTER CONTENT AREA
   ============================================================================ */

.filter-content {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    background: var(--color-white);
    /* min-height: 0 (not a fixed px) is what lets a flex child actually
       shrink and hand overflow to its own scrollbar */
    min-height: 0;
}

.filter-section {
    display: flex;
    flex-direction: column;
    gap: 16px;
    animation: fadeInContent 0.2s ease-out;
}

/* ============================================================================
   MATCH SCOPE (نطاق المطابقات)

   These `.scope-mode-*` classes had NO styles, so each option rendered as an
   inline <label> and the three flowed together in one line, squishing the
   Arabic. Stack them: one option per row, radio beside its label, helper text
   beneath.
   ============================================================================ */

.scope-mode-filter {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.scope-mode-option {
    display: grid;
    /* radio in a fixed first column; label + helper share the second so the
       helper lines up under the label, not under the radio */
    grid-template-columns: auto 1fr;
    align-items: baseline;
    column-gap: 10px;
    row-gap: 2px;
    padding: 10px 12px;
    border: 1px solid var(--color-border-primary, #d4c4a8);
    border-radius: var(--radius-md, 6px);
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.scope-mode-option:hover:not(.disabled) {
    background: var(--color-sand-light, #f5f0e8);
    border-color: var(--color-primary);
}

.scope-mode-option input[type="radio"] {
    grid-row: 1;
    width: 18px;
    height: 18px;
    margin: 0;
    cursor: pointer;
    accent-color: var(--color-primary);
}

.scope-mode-label {
    grid-column: 2;
    font-family: var(--font-primary);
    font-size: 0.95rem;
    color: var(--color-text-primary);
    line-height: 1.4;
}

.scope-mode-helper {
    grid-column: 2;
    font-family: var(--font-primary);
    font-size: 0.8rem;
    color: var(--color-text-secondary);
    line-height: 1.4;
}

.scope-mode-option.disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.scope-mode-option.disabled input[type="radio"] {
    cursor: not-allowed;
}

@keyframes fadeInContent {
    from {
        opacity: 0;
        transform: translateX(-10px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* Loading state for filter content */
.filter-section .loading {
    text-align: center;
    padding: 40px;
    color: #666;
    font-size: 1rem;
}

/* Empty state for filter content */
.filter-section .empty-state {
    text-align: center;
    padding: 40px;
    color: #999;
    font-size: 1rem;
}

/* ============================================================================
   ISNAD FILTER
   ============================================================================ */

.isnad-filter {
    margin-top: 24px;
    padding-top: 20px;
    border-top: 1px solid #e0e0e0;
}

.isnad-filter .filter-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--color-text-primary, #333);
    margin-bottom: 8px;
    font-family: var(--font-arabic, 'Noto Sans Arabic', sans-serif);
}

.isnad-filter .isnad-select {
    width: 100%;
    padding: 10px 12px;
    font-size: 0.875rem;
    font-family: var(--font-arabic, 'Noto Sans Arabic', sans-serif);
    border: 1px solid #ddd;
    border-radius: var(--radius-sm, 6px);
    background: var(--color-white);
    color: var(--color-text-primary, #333);
    cursor: pointer;
    transition: border-color 0.2s ease;
}

.isnad-filter .isnad-select:hover {
    border-color: var(--color-primary, #8B4513);
}

.isnad-filter .isnad-select:focus {
    outline: none;
    border-color: var(--color-primary, #8B4513);
    box-shadow: 0 0 0 3px rgba(139, 69, 19, 0.1);
}

/* ============================================================================
   MODAL FOOTER
   ============================================================================ */

.filter-modal-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border-top: 1px solid #e0e0e0;
    background: #f8f9fa;
    border-radius: 0 0 var(--radius-lg, 12px) var(--radius-lg, 12px);
}

.filter-count {
    font-size: 0.875rem;
    color: #666;
    font-family: var(--font-arabic, 'Noto Sans Arabic', sans-serif);
}

.filter-actions {
    display: flex;
    gap: 12px;
}

/* Clear button - secondary style */
.btn-clear {
    padding: 8px 20px;
    font-size: 0.875rem;
    font-family: var(--font-arabic, 'Noto Sans Arabic', sans-serif);
    background: var(--color-white);
    color: #666;
    border: 1px solid #ddd;
    border-radius: var(--radius-sm, 6px);
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-clear:hover:not(:disabled) {
    background: #f8f9fa;
    border-color: #999;
    color: #333;
}

.btn-clear:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Apply button - primary style */
.btn-apply {
    padding: 8px 24px;
    font-size: 0.875rem;
    font-family: var(--font-arabic, 'Noto Sans Arabic', sans-serif);
    background: var(--color-primary, #8B4513);
    color: white;
    border: none;
    border-radius: var(--radius-sm, 6px);
    cursor: pointer;
    transition: all 0.2s ease;
    font-weight: 500;
}

.btn-apply:hover:not(:disabled) {
    background: var(--color-primary-dark, #6B3410);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(139, 69, 19, 0.3);
}

.btn-apply:active {
    transform: translateY(0);
}

.btn-apply:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-apply:focus,
.btn-clear:focus {
    outline: 2px solid var(--color-primary, #8B4513);
    outline-offset: 2px;
}

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

/* Tablet and smaller */
@media (max-width: 768px) {
    .filter-modal-overlay {
        padding: 0;
    }

    .filter-modal-content {
        width: 100%;
        max-width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
    }

    .filter-modal-header {
        border-radius: 0;
        padding: 16px 20px;
    }

    .filter-tabs {
        padding: 0 20px;
    }

    .filter-tab {
        padding: 12px 16px;
        font-size: 0.875rem;
    }

    .filter-content {
        padding: 20px;
    }

    .filter-modal-footer {
        border-radius: 0;
        padding: 12px 20px;
    }
}

/* Mobile - very small screens */
@media (max-width: 480px) {
    .filter-modal-header h2 {
        font-size: 1.125rem;
    }

    .filter-tabs {
        gap: 4px;
        padding: 0 16px;
    }

    .filter-tab {
        padding: 10px 12px;
        font-size: 0.8125rem;
    }

    .filter-content {
        padding: 16px;
    }

    .filter-modal-footer {
        flex-direction: column;
        gap: 12px;
        align-items: stretch;
    }

    .filter-actions {
        width: 100%;
        justify-content: space-between;
    }

    .btn-clear,
    .btn-apply {
        flex: 1;
    }
}

/* ============================================================================
   ACCESSIBILITY & INTERACTIONS
   ============================================================================ */

/* Focus visible for keyboard navigation */
.filter-tab:focus-visible,
.close-button:focus-visible,
.btn-clear:focus-visible,
.btn-apply:focus-visible {
    outline: 2px solid var(--color-primary, #8B4513);
    outline-offset: 2px;
}

/* Prevent body scroll when modal is open */
body:has(.filter-modal-overlay) {
    overflow: hidden;
}

/* Smooth scrolling in filter content */
.filter-content {
    scroll-behavior: smooth;
}

/* ============================================================================
   UTILITIES
   ============================================================================ */

/* Hide scrollbar but keep functionality */
.filter-content.hide-scrollbar {
    scrollbar-width: none;
}

.filter-content.hide-scrollbar::-webkit-scrollbar {
    display: none;
}

/* Loading spinner (if needed) */
.filter-loading-spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(0, 0, 0, 0.1);
    border-radius: 50%;
    border-top-color: var(--color-primary, #8B4513);
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ============================================================================
   Z-INDEX HIERARCHY
   ============================================================================ */

/*
   Z-index scale for modal system:
   - .filter-modal-overlay: 1000
   - .filter-modal-content: auto (child of overlay)
   - Other modals should use 1100+
*/