// Admin Filter Panel Styles
// Sliding panel from the right side for admin changelist filters

// Animation for active filter arrow
@keyframes pulse-scale {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
}

// Filter panel toggle button
#filter-panel-toggle {
    width: 100%;
}

// Loading Indicator for HTMX requests with enhanced animations
.filter-loading-indicator {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.85);
    color: #fff;
    padding: 24px 32px;
    border-radius: 8px;
    z-index: 100000;  // Higher to cover filter panel
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    text-align: center;
    font-size: 1.125rem;
    font-weight: 500;
    opacity: 0;
    transition: opacity 200ms ease-in-out;

    &.htmx-request {
        display: block;
        opacity: 1;
        animation: fadeInScale 300ms cubic-bezier(0.4, 0, 0.2, 1) forwards;
    }

    // Fade out animation when request completes
    &.htmx-request-complete {
        animation: fadeOutScale 200ms cubic-bezier(0.4, 0, 1, 1) forwards;
    }

    .filter-loading-spinner {
        width: 40px;
        height: 40px;
        margin: 0 auto 12px;
        border: 4px solid rgba(255, 255, 255, 0.3);
        border-top-color: #fff;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
    }

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

    @keyframes fadeInScale {
        0% {
            opacity: 0;
            transform: translate(-50%, -50%) scale(0.8);
        }
        100% {
            opacity: 1;
            transform: translate(-50%, -50%) scale(1);
        }
    }

    @keyframes fadeOutScale {
        0% {
            opacity: 1;
            transform: translate(-50%, -50%) scale(1);
        }
        100% {
            opacity: 0;
            transform: translate(-50%, -50%) scale(0.8);
        }
    }
}

// Loading Overlay - blocks all clicks during HTMX requests
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.05);  // Very subtle background
    z-index: 99999;  // Higher than filter panel overlay, lower than loading indicator
    cursor: wait;  // Show waiting cursor
    opacity: 0;
    visibility: hidden;
    transition: opacity 300ms ease-in-out, visibility 300ms;

    &.htmx-request {
        visibility: visible;
        opacity: 1;
    }

    &.htmx-request-complete {
        opacity: 0;
        // Keep visible during fade-out, then hide after transition
        transition: opacity 300ms ease-in-out, visibility 0s 300ms;
    }
}

// HTMX Error Toast Notification
.htmx-error-toast {
    position: fixed;
    top: 20px;
    right: 20px;
    max-width: 400px;
    z-index: 100001;  // Above everything else
    background: #fff;
    border-left: 4px solid #d32f2f;  // Red accent
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15), 0 2px 4px rgba(0, 0, 0, 0.1);
    transform: translateY(0);
    opacity: 1;
    transition: transform 300ms cubic-bezier(0.4, 0, 0.2, 1),
                opacity 300ms cubic-bezier(0.4, 0, 0.2, 1);

    &.hidden {
        transform: translateY(-20px);
        opacity: 0;
        pointer-events: none;
    }

    .error-toast-content {
        display: flex;
        align-items: flex-start;
        padding: 16px;
        gap: 12px;
    }

    .error-icon {
        font-size: 24px;
        flex-shrink: 0;
        line-height: 1;
    }

    .error-message {
        flex: 1;
        min-width: 0;

        strong {
            display: block;
            font-size: 14px;
            font-weight: 600;
            color: #d32f2f;
            margin-bottom: 4px;
        }

        p {
            margin: 0;
            font-size: 13px;
            color: #666;
            line-height: 1.4;
        }
    }

    .error-actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
        flex-shrink: 0;
    }

    .error-btn {
        padding: 6px 12px;
        border: 1px solid #ddd;
        background: #fff;
        border-radius: 3px;
        cursor: pointer;
        font-size: 12px;
        font-weight: 500;
        transition: all 150ms ease-in-out;

        &:hover {
            background: #f5f5f5;
        }

        &.error-retry {
            color: #1976d2;
            border-color: #1976d2;
            white-space: nowrap;

            &:hover {
                background: #e3f2fd;
            }
        }

        &.error-close {
            color: #666;
            font-size: 20px;
            line-height: 1;
            padding: 4px 8px;
            border: none;

            &:hover {
                color: #d32f2f;
                background: #ffebee;
            }
        }
    }

    // Animation for entrance
    @keyframes slideInFromTop {
        from {
            transform: translateY(-20px);
            opacity: 0;
        }
        to {
            transform: translateY(0);
            opacity: 1;
        }
    }
}

// Overlay background - lighter shade
.filter-panel-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.2);  // Lighter overlay
    z-index: 9999;  // Increased to cover support button (z-index: 9999)
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease-in-out, visibility 0.3s;

    &.active {
        visibility: visible;
        opacity: 1;
    }
}

// Filter panel container
.filter-panel {
    position: fixed;
    top: 0;
    right: -900px;  // Hidden off-screen initially (wider panel)
    bottom: 0;
    width: 900px;
    max-width: 95vw;
    background-color: #fff;
    z-index: 10000;  // Increased to cover support button (z-index: 9999) when active
    box-shadow: -2px 0 8px rgba(0, 0, 0, 0.15);
    transition: right 0.3s ease-in-out;
    overflow-y: auto;
    overflow-x: hidden;

    &.active {
        right: 0;  // Slide in
    }

    // No animation when pinned on page load
    &.pinned-no-animation {
        transition: none;
    }

    // Narrow panel for 6 or fewer filters
    &.filter-panel-narrow {
        width: 450px;
        right: -450px;

        &.active {
            right: 0;
        }

        // Single column layout for narrow panel
        .filter-panel-content {
            grid-template-columns: 1fr !important;
        }
    }

    // Panel header
    .filter-panel-header {
        position: sticky;
        top: 0;
        background-color: #f8f9fa;
        border-bottom: 2px solid #dee2e6;
        padding: 16px 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        z-index: 10;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);

        h2 {
            margin: 0;
            font-size: 1.375rem;
            font-weight: 700;
            color: #2c3e50;
            letter-spacing: -0.02em;
        }

        .filter-panel-actions {
            display: flex;
            gap: 8px;
            align-items: center;
        }

        .filter-panel-reset,
        .filter-panel-pin,
        .filter-panel-close {
            background: transparent;
            border: 1px solid transparent;
            font-size: 1.25rem;
            line-height: 1;
            color: #666;
            cursor: pointer;
            padding: 6px 10px;
            margin: 0;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            border-radius: 3px;
            transition: all 0.15s ease;

            &:hover {
                background-color: #f5f5f5;
                border-color: #ddd;
                color: #333;
            }

            &:active {
                background-color: #e9e9e9;
                border-color: #ccc;
            }

            &:focus {
                outline: none;
                box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
            }
        }

        .filter-panel-reset {
            font-size: 1.25rem;

            &:hover {
                background-color: #fff3cd;
                border-color: #ffeaa7;
                color: #856404;
            }

            &:active {
                background-color: #ffeaa7;
                border-color: #ffd700;
            }
        }

        .filter-panel-pin {
            font-size: 1.125rem;

            &.pinned {
                background-color: #28a745;  // Stronger green when pinned
                border-color: #218838;
                color: #ffffff;
                box-shadow: 0 0 0 2px rgba(40, 167, 69, 0.3);  // Glow effect

                &:hover {
                    background-color: #218838;
                    border-color: #1e7e34;
                    color: #ffffff;
                    box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.4);
                }
            }
        }

        .filter-panel-close {
            font-size: 1.5rem;
            font-weight: 300;
            padding: 4px 10px;
        }
    }

    // Panel content - Force grid layout with !important to override Grappelli
    .filter-panel-content {
        padding: 24px !important;
        display: grid !important;
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 24px !important;
        align-items: start !important;
        grid-auto-flow: row !important;

        .filter-group {
            margin-bottom: 0 !important;
            break-inside: avoid;
            width: auto !important;
            max-width: 100% !important;
            min-width: 0 !important;
            box-sizing: border-box !important;

            // Whoosh animation for empty filter groups
            &.whoosh-out {
                animation: groupWhooshOut 0.4s ease-out forwards;
                pointer-events: none;
            }

            @keyframes groupWhooshOut {
                0% {
                    opacity: 1;
                    transform: translateY(0) scale(1);
                }
                50% {
                    opacity: 0.3;
                    transform: translateY(-10px) scale(0.95);
                }
                100% {
                    opacity: 0;
                    transform: translateY(-20px) scale(0.8);
                    height: 0;
                    margin: 0;
                    padding: 0;
                }
            }

            // Hidden state (after fade complete)
            &.hidden {
                display: none;
            }

            // Filter title - collapsible styling
            .filter-title {
                font-size: 0.8125rem;
                font-weight: 700;
                text-transform: none;
                color: #34495e;
                margin: 0 0 12px 0 !important;
                padding: 8px 12px;
                background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
                border-left: 4px solid #4a90e2;
                border-radius: 4px;
                letter-spacing: -0.01em;
                box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
                word-wrap: break-word !important;
                overflow-wrap: break-word !important;
                width: 100% !important;
                box-sizing: border-box !important;

                // Collapsible styling
                &.filter-collapsible {
                    cursor: pointer;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    user-select: none;
                    transition: all 0.2s ease;

                    &:hover {
                        background: linear-gradient(135deg, #e9ecef 0%, #dee2e6 100%);
                        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
                    }

                    &:active {
                        transform: translateY(1px);
                    }

                    .filter-title-text {
                        flex: 1;
                        word-wrap: break-word;
                        overflow-wrap: break-word;

                        // Selected filter value styling
                        .selected-filter-value {
                            font-weight: 700;
                            color: #0275d8;
                            margin-left: 4px;

                            // Count in header for selected filters
                            .filter-header-count {
                                display: inline-block;
                                margin-left: 6px;
                                padding: 2px 6px;
                                background-color: #0275d8;
                                color: #ffffff;
                                font-size: 0.8125rem;
                                font-weight: 700;
                                border-radius: 10px;
                                min-width: 20px;
                                text-align: center;
                                vertical-align: baseline;
                            }
                        }
                    }

                    .filter-toggle-icon {
                        font-size: 1rem;
                        color: #6c757d;
                        transition: transform 0.2s ease;
                        margin-left: 8px;
                        flex-shrink: 0;
                    }

                    // Filter with active selection gets highlighted
                    &.has-selection {
                        // Thick colored left border (like VS Code)
                        border-left-color: #dc3545;  // Bootstrap danger red
                        border-left-width: 4px;

                        // Subtle background gradient
                        background: linear-gradient(135deg, #fff5f5 0%, #ffe8e8 100%);

                        &:hover {
                            background: linear-gradient(135deg, #ffe8e8 0%, #ffd6d6 100%);
                        }

                        // Make the arrow red and slightly animated
                        .filter-toggle-icon {
                            color: #dc3545;
                            font-weight: bold;
                            animation: pulse-scale 2s infinite;
                        }

                        // Make the filter title text bold
                        .filter-title-text {
                            font-weight: 600;
                        }
                    }

                    // Expanded state (default animation)
                    .filter-toggle-icon {
                        transform: rotate(0deg);
                    }

                    // Collapsed state - no rotation needed as plus icon is default
                    &.collapsed {
                        .filter-toggle-icon {
                            transform: rotate(0deg);
                        }
                    }
                }
            }

            // Filter options list
            .filter-options {
                list-style: none;
                margin: 0 !important;
                padding: 0 !important;
                overflow: hidden;
                transition: max-height 0.3s ease, opacity 0.3s ease;
                max-height: 2000px;
                opacity: 1;
                width: 100% !important;
                box-sizing: border-box !important;

                // Collapsed state
                &.collapsed {
                    max-height: 0;
                    opacity: 0;
                    margin-bottom: 0;
                }

                .filter-option {
                    margin: 0;
                    padding: 0;
                    display: flex;
                    align-items: center;
                    border-radius: 4px;
                    transition: all 0.2s ease;
                    position: relative;
                    overflow: hidden;
                    max-height: 50px;

                    // Check icon for selected items
                    .filter-check-icon {
                        font-size: 0.875rem;
                        color: #5cb85c;
                        font-weight: bold;
                        padding: 8px 4px 8px 12px;
                        flex-shrink: 0;
                    }

                    // Link styling
                    .filter-option-link {
                        flex: 1;
                        padding: 8px 4px;
                        color: #333;
                        text-decoration: none;
                        transition: all 0.2s ease;
                        font-size: 0.9375rem;

                        &:focus {
                            outline: 2px solid #4a90e2;
                            outline-offset: -2px;
                            border-radius: 2px;
                        }
                    }

                    // Count badge for filter options
                    .filter-count {
                        display: inline-block;
                        margin-left: auto;
                        margin-right: 12px;
                        padding: 2px 7px;
                        background-color: #e9ecef;
                        color: #6c757d;
                        font-size: 0.75rem;
                        font-weight: 600;
                        border-radius: 10px;
                        white-space: nowrap;
                        min-width: 20px;
                        text-align: center;
                        transition: all 0.2s ease;
                        flex-shrink: 0;
                    }

                    // Hover state for entire option
                    &:hover {
                        background-color: #f0f0f0;

                        .filter-option-link {
                            color: #000;
                        }

                        .filter-count {
                            background-color: #dee2e6;
                            color: #495057;
                        }
                    }

                    // Selected state with background accent
                    &.selected {
                        background-color: #e8f4f8;
                        position: relative;

                        // Add a left border accent
                        &:before {
                            content: "";
                            position: absolute;
                            left: 0;
                            top: 0;
                            bottom: 0;
                            width: 3px;
                            background-color: #0275d8;
                            border-radius: 4px 0 0 4px;
                        }

                        .filter-option-link {
                            color: #0275d8;
                            font-weight: 600;
                        }

                        .filter-count {
                            background-color: #b8dff0;
                            color: #0275d8;
                            font-weight: 700;
                        }

                        &:hover {
                            background-color: #d1e9f3;

                            .filter-count {
                                background-color: #a3d5eb;
                                color: #025aa5;
                            }
                        }
                    }

                    // When there's no check icon, add padding to the link
                    &:not(.selected) .filter-option-link {
                        padding-left: 12px;
                    }

                    // Whoosh animation for zero-count items
                    &.whoosh-out {
                        animation: whooshOut 0.3s ease-out forwards;
                        pointer-events: none;
                    }

                    @keyframes whooshOut {
                        0% {
                            opacity: 1;
                            transform: translateX(0) scale(1);
                            max-height: 50px;
                        }
                        50% {
                            opacity: 0.5;
                            transform: translateX(-20px) scale(0.9);
                        }
                        100% {
                            opacity: 0;
                            transform: translateX(-50px) scale(0.7);
                            max-height: 0;
                            padding: 0;
                            margin: 0;
                        }
                    }

                    // Hidden state (after fade complete)
                    &.hidden {
                        display: none;
                    }
                }
            }

            // Select dropdown for filters with many options
            .filter-select {
                width: 100% !important;
                max-width: 100% !important;
                box-sizing: border-box !important;
                padding: 10px 12px;
                font-size: 0.9375rem;
                color: #333;
                background-color: #fff;
                border: 2px solid #dee2e6;
                border-radius: 6px;
                cursor: pointer;
                transition: all 0.2s ease;
                appearance: none;
                background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23333' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
                background-repeat: no-repeat;
                background-position: right 12px center;
                background-size: 12px;
                padding-right: 36px;

                &:hover {
                    border-color: #4a90e2;
                    background-color: #f8f9fa;
                }

                &:focus {
                    outline: none;
                    border-color: #4a90e2;
                    box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
                }

                option {
                    padding: 8px;

                    &:checked {
                        background-color: #e8f4f8;
                        font-weight: 600;
                    }
                }
            }
        }
    }
}

// Transitioning panel (old panel during crossfade to prevent flickering)
.filter-panel-transitioning {
    // Force fixed positioning and high z-index to stay on top during transition
    position: fixed !important;
    z-index: 10001 !important;

    // Fade-out animation class applied after swap
    &.fade-out {
        opacity: 0;
        transition: opacity 300ms ease-out;
        pointer-events: none; // Prevent interaction during fade
    }
}

// Body class when panel is open
// Note: overflow is NOT hidden to allow background scrolling
body.filter-panel-open {
    // overflow: hidden;  // Commented out to allow scrolling of the table in background
}

// Fix footer to bottom of screen with full width
// Target the specific footer element with all three classes
footer.grp-module.grp-submit-row.grp-fixed-footer {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    z-index: 100 !important;  // Below filter panel (10000) but above content
}

// Add padding to body to prevent content from being hidden behind fixed footer
body.grp-change-list #grp-changelist-form {
    padding-bottom: 80px;  // Height of footer plus margin
}

// Responsive adjustments
@media (max-width: 1400px) {
    .filter-panel {
        width: 750px;
        right: -750px;

        &.active {
            right: 0;
        }
    }
}

@media (max-width: 1200px) {
    .filter-panel {
        width: 650px;
        right: -650px;

        &.active {
            right: 0;
        }
    }
}

@media (max-width: 1024px) {
    .filter-panel {
        width: 550px;
        right: -550px;

        &.active {
            right: 0;
        }

        .filter-panel-content {
            grid-template-columns: 1fr !important;  // Single column on tablets
        }
    }
}

@media (max-width: 768px) {
    .filter-panel {
        width: 450px;
        right: -450px;

        &.active {
            right: 0;
        }

        .filter-panel-content {
            grid-template-columns: 1fr !important;  // Single column on mobile
        }
    }
}

@media (max-width: 480px) {
    .filter-panel {
        width: 100%;
        right: -100%;
        max-width: none;

        &.active {
            right: 0;
        }

        .filter-panel-content {
            grid-template-columns: 1fr !important;  // Single column on small mobile
        }
    }
}

// Animation for smooth appearance
@keyframes slideInRight {
    from {
        transform: translateX(100%);
        opacity: 0.8;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

// Dark theme support (if applicable)
@media (prefers-color-scheme: dark) {
    .filter-panel {
        background-color: #2b2b2b;
        color: #e0e0e0;

        .filter-panel-header {
            background-color: #1f1f1f;
            border-bottom-color: #444;

            h2 {
                color: #e0e0e0;
            }

            .filter-panel-close {
                color: #999;

                &:hover {
                    background-color: #3a3a3a;
                    color: #fff;
                }
            }
        }

        .filter-panel-content {
            .filter-group {
                .filter-title {
                    color: #999;
                    border-bottom-color: #444;

                    &.filter-collapsible {
                        .filter-title-text {
                            .selected-filter-value {
                                color: #4a9eff;

                                // Count in header for selected filters (dark theme)
                                .filter-header-count {
                                    background-color: #4a9eff;
                                    color: #1f1f1f;
                                }
                            }
                        }

                        &.has-selection {
                            // Red border for active filters in dark theme
                            border-left-color: #ff4444;
                            border-left-width: 4px;
                            background: linear-gradient(135deg, #3a1a1a 0%, #462222 100%);

                            &:hover {
                                background: linear-gradient(135deg, #462222 0%, #5a2a2a 100%);
                            }

                            .filter-toggle-icon {
                                color: #ff4444;
                                font-weight: bold;
                                animation: pulse-scale 2s infinite;
                            }

                            .filter-title-text {
                                font-weight: 600;
                            }
                        }
                    }
                }

                .filter-options {
                    .filter-option {
                        .filter-option-link {
                            color: #e0e0e0;
                        }

                        .filter-count {
                            background-color: #3a3a3a;
                            color: #999;
                        }

                        &:hover {
                            background-color: #3a3a3a;

                            .filter-option-link {
                                color: #fff;
                            }

                            .filter-count {
                                background-color: #4a4a4a;
                                color: #ccc;
                            }
                        }

                        &.selected {
                            background-color: #1a3a52;

                            &:before {
                                background-color: #4a9eff;
                            }

                            .filter-option-link {
                                color: #4a9eff;
                            }

                            .filter-count {
                                background-color: #224660;
                                color: #4a9eff;
                            }

                            &:hover {
                                background-color: #224660;

                                .filter-count {
                                    background-color: #2a5370;
                                    color: #6ab0ff;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    .filter-panel-overlay {
        background-color: rgba(0, 0, 0, 0.3);  // Lighter overlay for dark theme
    }
}
