/**
 * Product Selector Modal - Responsive Styles
 * All @media queries for tablet and mobile
 */

/* ==========================================================================
   Tablet (max-width: 1024px)
   ========================================================================== */

@media (max-width: 1024px) {
    .product-selector-modal__content {
        max-width: 95vw;
        max-height: 90vh;
    }

    .product-selector-modal__layout {
        grid-template-columns: 1fr;
    }

    /* Filters sidebar becomes a side panel on tablet/mobile */
    .filters-sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 280px;
        max-width: 85vw;
        height: 100vh;
        z-index: 1000;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.2);
        display: flex;
        flex-direction: column;
        padding-top: 48px; /* Make room for close button */
    }

    /* Show filters when is-open class is added */
    .filters-sidebar.is-open {
        transform: translateX(0);
    }

    /* Close button in sidebar - show on mobile when open */
    .filters-sidebar.is-open .filters-sidebar__close {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        position: absolute;
        top: 12px;
        right: 12px;
        background: transparent;
        border: none;
        color: var(--text-secondary);
        cursor: pointer;
        border-radius: 6px;
        transition: background-color 0.2s, color 0.2s;
        z-index: 10;
    }

    .filters-sidebar.is-open .filters-sidebar__close:hover {
        background-color: var(--bg-hover);
        color: var(--text-primary);
    }

    .filters-sidebar.is-open .filters-sidebar__close svg {
        width: 18px;
        height: 18px;
    }

    /* Clickable backdrop for mobile filters */
    .filters-backdrop {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 999;
        cursor: pointer;
    }

    .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    }

    /* === MOBILE TOOLBAR RESTRUCTURE === */

    /* Show mobile search in toolbar - full width */
    .toolbar__search {
        display: flex;
        width: 100%;
    }

    .toolbar__search-input {
        flex: 1;
        width: 100%;
        padding: 10px 14px;
        background: var(--bg-tertiary);
        border: 1px solid var(--border);
        border-radius: 8px;
        color: var(--text-primary);
        font-size: 0.9375rem;
    }

    .toolbar__search-input::placeholder {
        color: var(--text-muted);
    }

    .toolbar__search-input:focus {
        outline: none;
        border-color: var(--accent);
    }

    /* Toolbar becomes column layout */
    .product-selector-modal__toolbar {
        flex-direction: column;
        padding: 12px 16px;
        gap: 10px;
        flex-shrink: 0;
        background: var(--bg-secondary);
    }

    /* Controls row - MUST be horizontal */
    .toolbar__controls {
        display: flex !important;
        flex-direction: row !important;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        width: 100%;
    }

    .toolbar__left {
        display: flex !important;
        flex-direction: row !important;
        align-items: center;
        gap: 6px;
    }

    /* Compact toggles - inline, no wrap */
    .compatibility-toggle {
        display: inline-flex !important;
        flex-direction: row !important;
        align-items: center;
        gap: 4px;
        padding: 7px 10px;
        background: var(--bg-tertiary);
        border-radius: 6px;
        border: 1px solid var(--border);
        flex-shrink: 0;
        white-space: nowrap;
    }

    .compatibility-toggle__label {
        font-size: 0.8125rem;
    }

    .compatibility-toggle input[type="checkbox"] {
        width: 16px;
        height: 16px;
    }

    /* Hide product count */
    .product-count {
        display: none;
    }

    .toolbar__right {
        flex-shrink: 0;
    }

    /* Compact sort */
    .sort-dropdown__trigger {
        padding: 7px 28px 7px 10px;
        font-size: 0.8125rem;
    }

    /* Mobile filter toggle button */
    .mobile-filter-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
        background-color: var(--bg-tertiary);
        color: var(--text-primary);
        border: 1px solid var(--border);
        padding: 7px 12px;
        border-radius: 6px;
        font-size: 0.8125rem;
        cursor: pointer;
        transition: all 0.2s;
        white-space: nowrap;
        flex-shrink: 0;
    }

    .mobile-filter-toggle:hover {
        background-color: var(--bg-hover);
        border-color: var(--border-light);
    }

    .mobile-filter-toggle svg {
        width: 16px;
        height: 16px;
        flex-shrink: 0;
    }

    /* Table view: hide spec columns on tablet */
    .product-table .col-spec {
        display: none;
    }

    .product-table th.col-spec {
        display: none;
    }
}

/* ==========================================================================
   Mobile - FULLSCREEN (max-width: 768px)
   ========================================================================== */

@media (max-width: 768px) {
    .product-selector-modal {
        padding: 0;
        align-items: stretch;
    }

    .product-selector-modal__content {
        position: fixed;
        inset: 0;
        width: 100vw;
        height: 100vh;
        max-width: 100vw;
        max-height: 100vh;
        border-radius: 0;
        display: block;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        animation: modal-slide-up 0.3s ease-out;
    }

    /* Sticky header with close button always visible */
    .product-selector-modal__header {
        position: sticky;
        top: 0;
        z-index: 20;
        background-color: var(--bg-modal);
        border-radius: 0;
        padding: 16px;
    }

    /* Sticky toolbar with search */
    .product-selector-modal__toolbar {
        position: sticky;
        top: 76px; /* Below header: 16px padding + 44px close btn + 16px padding = 76px */
        z-index: 19;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border);
    }

    /* Close button larger for touch - always visible top right */
    .product-selector-modal__close {
        width: 44px;
        height: 44px;
        min-width: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .product-selector-modal__layout {
        min-height: 50vh;
    }

    .product-grid-container {
        padding: 16px;
    }

    .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        gap: 12px;
    }

    /* Product cards more compact on mobile */
    .product-card__info {
        padding: 10px;
    }

    .product-card__actions {
        padding: 10px;
        padding-top: 0;
    }

    .btn-add-to-build {
        padding: 12px 16px; /* Larger touch target */
    }

    /* Hide "Toevoegen" text on mobile, show only "+" */
    .btn-add-to-build .btn-add-text {
        display: none;
    }

    .btn-add-to-build .btn-add-icon {
        font-size: 1.25rem;
        font-weight: 700;
    }

    /* Active filters bar compact and sticky */
    .active-filters-bar {
        padding: 8px 16px;
        position: sticky;
        top: 180px; /* Below header (76px) + toolbar (~104px) */
        z-index: 18;
        background: var(--bg-secondary);
    }

    /* Force grid on mobile, hide table and toggle */
    .product-table-container {
        display: none !important;
    }

    .view-toggle {
        display: none !important;
    }

    /* ===== Stacked mobile toolbar (same opmaak als de categorie-pagina's) =====
       Row 1: search (apart element, volle breedte)
       Row 2: Compatibel | Op voorraad
       Row 3: Filter | Sorteer */

    /* Flatten both groups so all controls share one wrapping flex row */
    .toolbar__left,
    .toolbar__right {
        display: contents !important;
    }

    .toolbar__controls {
        flex-wrap: wrap;
        column-gap: 8px;
        /* row-gap gelijk aan toolbar padding-bottom -> symmetrische witruimte rond Filter|Sorteer */
        row-gap: 12px;
        justify-content: space-between;
    }

    /* Hide the inline desktop sort dropdown on phones */
    .sort-dropdown {
        display: none;
    }

    /* Row 2 — compatibility + stock toggles, side by side */
    .compatibility-toggle {
        order: 1;
        flex: 1 1 calc(50% - 4px);
        justify-content: center;
    }

    /* Row 3 — Filter + Sorteer as equal-width pill buttons (catalog style) */
    .mobile-filter-toggle,
    .mobile-sort-btn {
        display: flex;
        order: 2;
        flex: 1 1 calc(50% - 4px);
        align-items: center;
        justify-content: center;
        gap: 8px;
        min-height: 46px;
        padding: clamp(0.7rem, 2vw, 0.875rem) 1rem;
        border-radius: 50px;
        background-color: var(--bg-tertiary);
        border: 1px solid var(--border);
        color: var(--text-primary);
        font-size: 0.875rem;
        font-weight: 500;
        white-space: nowrap;
    }

    .mobile-filter-toggle svg,
    .mobile-sort-btn svg {
        width: 18px;
        height: 18px;
        opacity: 0.7;
    }

    /* ----- Mobile sort bottom-sheet ----- */
    .mobile-sort-overlay {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.25s ease, visibility 0.25s ease;
        z-index: 1000;
    }

    .mobile-sort-overlay.is-active {
        opacity: 1;
        visibility: visible;
    }

    .mobile-sort-panel {
        display: block;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        background: var(--bg-modal, var(--bg-secondary));
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.3);
        transform: translateY(100%);
        transition: transform 0.3s ease;
        z-index: 1001;
        max-height: 80vh;
        overflow-y: auto;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .mobile-sort-panel.is-active {
        transform: translateY(0);
    }

    .mobile-sort-panel__header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 16px 20px;
        border-bottom: 1px solid var(--border);
        position: sticky;
        top: 0;
        background: inherit;
    }

    .mobile-sort-panel__title {
        font-size: 1.0625rem;
        font-weight: 700;
        color: var(--text-primary);
        margin: 0;
    }

    .mobile-sort-panel__close {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        border: none;
        background: var(--bg-tertiary);
        color: var(--text-primary);
        border-radius: 8px;
        cursor: pointer;
    }

    .mobile-sort-panel__options {
        display: flex;
        flex-direction: column;
        padding: 8px;
    }

    .mobile-sort-option {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        width: 100%;
        text-align: left;
        padding: 14px 16px;
        background: transparent;
        border: none;
        border-radius: 8px;
        color: var(--text-primary);
        font-size: 0.9375rem;
        cursor: pointer;
        transition: background-color 0.15s;
    }

    .mobile-sort-option:hover {
        background: var(--bg-hover, var(--bg-tertiary));
    }

    .mobile-sort-option.is-active {
        background: var(--bg-tertiary);
        color: var(--accent);
        font-weight: 600;
    }

    .mobile-sort-option.is-active::after {
        content: "";
        width: 18px;
        height: 18px;
        flex-shrink: 0;
        background-color: currentColor;
        -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='black'%3E%3Cpath fill-rule='evenodd' d='M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z' clip-rule='evenodd'/%3E%3C/svg%3E") center / contain no-repeat;
        mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='black'%3E%3Cpath fill-rule='evenodd' d='M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z' clip-rule='evenodd'/%3E%3C/svg%3E") center / contain no-repeat;
    }
}

/* Mobile slide up animation */
@keyframes modal-slide-up {
    from {
        transform: translateY(100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* ==========================================================================
   Small Mobile (max-width: 480px)
   ========================================================================== */

@media (max-width: 480px) {
    .product-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .product-grid-container {
        padding: 12px;
    }
}

/* ==========================================================================
   Compatibility Filter Notice - Responsive
   ========================================================================== */

@media (max-width: 640px) {
    .compat-filter-notice {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .compat-filter-notice__btn {
        align-self: flex-end;
    }
}
