/**
 * SV Product Filter - Frontend Styles
 * Matches Plugin68 WooCommerce Ajax Product Filter design
 */

/* ========= VARIABLES ========= */
.svpf-filter-wrap {
    --svpf-primary: #e53935;
    --svpf-secondary: #333;
    max-width: 100%;
    position: relative;
    text-align: left;
    margin-bottom: 15px;
}

.svpf-filter-wrap * {
    box-sizing: border-box;
}

.svpf-filter-wrap a {
    text-decoration: none !important;
    color: #333;
    outline: 0;
}

/* ========= DROPDOWN BAR ========= */
.svpf-dropdown-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 15px;
    /* Không được overflow:hidden — sẽ cắt dropdown panel */
    overflow: visible;
}

.svpf-dropdown {
    position: relative;
    display: inline-block;
    /* Quan trọng: overflow phải visible để panel hiện ra ngoài */
    overflow: visible;
}

.svpf-dd-toggle {
    position: relative;
    padding: 8px 28px 8px 20px;
    border: 1px solid #e0e0e0;
    border-radius: 5px;
    font-size: 14px;
    font-weight: 400;
    text-transform: capitalize;
    line-height: 1.3;
    cursor: pointer;
    display: inline-block;
    background: #fff;
    -webkit-transition: border-color .2s ease;
    transition: border-color .2s ease;
    -webkit-user-select: none;
    user-select: none;
    /* Safari iOS: cần -webkit-tap-highlight để nhận touch */
    -webkit-tap-highlight-color: rgba(0,0,0,0.05);
    /* Quan trọng: Safari không nhận tap nếu không có cursor:pointer trên non-button */
    cursor: pointer !important;
}

/* Dropdown arrow */
.svpf-dd-toggle::after {
    content: '';
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 4px solid #333;
    height: 0;
    width: 0;
    position: absolute;
    top: calc(50% - 2px);
    right: 13px;
}

.svpf-dropdown.open .svpf-dd-toggle,
.svpf-dd-toggle:hover,
.svpf-dd--active .svpf-dd-toggle {
    border-color: var(--svpf-primary) !important;
}

/* Active badge count */
.svpf-dd--active .svpf-dd-toggle::before {
    content: attr(data-count);
    position: absolute;
    top: 7px;
    left: 7px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    background: var(--svpf-primary);
    border-radius: 99px;
    z-index: 2;
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
}

.svpf-dd--active .svpf-dd-toggle {
    padding-left: 34px;
}

/* Dropdown panel */
.svpf-dd-panel {
    position: absolute;
    top: calc(100% + 10px);
    left: 0;
    min-width: 382px;
    max-width: 550px;
    max-height: 300px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    z-index: 9990;
    background: #fff;
    -webkit-box-shadow: 0 2px 20px rgba(0,0,0,.4);
    box-shadow: 0 2px 20px rgba(0,0,0,.4);
    padding: 15px;
    border-radius: 8px;
    /* KHÔNG dùng transform:scale — gây bug tap trên Safari iOS */
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    /* Chỉ animate opacity để nhẹ nhàng hơn */
    -webkit-transition: opacity .18s ease, visibility .18s ease;
    transition: opacity .18s ease, visibility .18s ease;
    /* Tạo stacking context riêng — quan trọng trên Safari */
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    will-change: opacity;
}

.svpf-dropdown.open .svpf-dd-panel {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* Arrow — ẩn trên mobile để đơn giản hóa */
.svpf-dropdown.open::before,
.svpf-dropdown.open::after {
    content: '';
    position: absolute;
    left: 20px;
    bottom: -10px;
    z-index: 30;
}

.svpf-dropdown.open::after {
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-bottom: 8px solid #fff;
    z-index: 30;
}

.svpf-dropdown.open::before {
    border-left: 9px solid transparent;
    border-right: 9px solid transparent;
    border-bottom: 9px solid #ddd;
    z-index: 29;
    left: 19px;
}

/* ========= FILTER ITEMS (shared) ========= */
.svpf-filter-item {
    display: inline-block;
    margin-right: 5px;
    margin-top: 5px;
    line-height: 1;
}

.svpf-filter-item a {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    justify-content: center;
    border-radius: 5px;
    font-size: 14px;
    overflow: hidden;
    border: 1px solid #e0e0e0;
    padding: 5px 8px;
    text-transform: capitalize;
    font-weight: 400 !important;
    outline: 0 !important;
    transition: all .2s ease-in-out;
    min-height: 36px;
    min-width: 36px;
    position: relative;
}

.svpf-filter-item a:hover {
    border-color: var(--svpf-primary);
    color: var(--svpf-primary);
}

.svpf-filter-item.active > a {
    border: 1px solid var(--svpf-primary);
}


.svpf-item-count {
    color: #555;
    font-weight: 400;
}

.svpf-active-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin: 0 0 15px;
}

.svpf-active-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 2px solid #111;
    border-radius: 999px;
    background: #fff;
    padding: 8px 14px;
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
}

.svpf-active-chip__remove {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--svpf-primary);
    color: #fff;
    font-size: 13px;
    font-weight: 700;
}

/* ========= INLINE IMAGE FILTER (Lọc theo nhu cầu) ========= */
.svpf-inline-filter {
    margin-bottom: 15px;
}

.svpf-inline-title {
    font-size: 14px;
    text-transform: uppercase;
    font-weight: 700;
    color: #333;
    margin-bottom: 8px;
}

.svpf-inline-items {
    white-space: normal;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.svpf-inline-items .svpf-filter-item {
    width: 110px;
    max-width: 110px;
    margin: 0;
    vertical-align: top;
}

.svpf-inline-items .svpf-filter-item a {
    padding: 8px;
    flex-direction: column;
    justify-content: flex-start;
    gap: 6px;
    min-height: var(--svpf-img-height, 135px);
    height: auto;
    background: rgba(242,244,247,1);
}

.svpf-item-media {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: calc(var(--svpf-img-height, 135px) - 48px);
}

.svpf-inline-items .svpf-filter-item a img {
    width: auto;
    max-width: 98%;
    max-height: calc(var(--svpf-img-height, 135px) - 48px);
    height: auto;
    object-fit: contain;
    mix-blend-mode: multiply;
    margin: 0 auto;
}

.svpf-item-label {
    font-size: 12px;
    font-weight: 700;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    white-space: normal;
    margin: 0;
    text-align: center;
    line-height: 1.35;
    min-height: 32px;
}

.svpf-inline-items .svpf-item-count,
.svpf-filter-item--image .svpf-item-count {
    display: block;
    margin-top: auto;
    font-size: 11px;
    line-height: 1.2;
    text-align: center;
}

/* ========= PRICE FILTER ========= */
.svpf-price-ranges {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 10px;
}

.svpf-price-btn {
    display: inline-flex;
    align-items: center;
    padding: 5px 8px;
    border: 1px solid #e0e0e0;
    border-radius: 5px;
    font-size: 14px;
    cursor: pointer;
    transition: all .2s ease-in-out;
    min-height: 36px;
    background: #fff;
    color: #333;
    text-decoration: none !important;
}

.svpf-price-btn:hover,
.svpf-price-btn.active {
    border-color: var(--svpf-primary);
    color: var(--svpf-primary);
}

/* Price slider */
.svpf-price-slider-wrap {
    margin-top: 10px;
}

.svpf-price-hint {
    font-size: 14px;
    color: var(--svpf-secondary);
    margin: 6px 0 10px;
}

.svpf-price-inputs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    gap: 8px;
}

.svpf-price-inputs input {
    padding: 7px !important;
    border: 1px solid #bdbdbd !important;
    border-radius: 4px !important;
    background: #fff;
    box-shadow: none !important;
    width: 45%;
    font-size: 14px;
}

.svpf-price-inputs input::-webkit-outer-spin-button,
.svpf-price-inputs input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.svpf-price-divider {
    width: 30px;
    height: 1px;
    background: #ddd;
    display: inline-block;
}

/* Range slider */
.svpf-range-slider {
    position: relative;
    height: 30px;
    width: 100%;
}

.svpf-range-slider input[type='range'] {
    width: 100%;
    height: 30px;
    overflow: hidden;
    cursor: pointer;
    outline: none;
    padding: 0 !important;
    margin: 0 !important;
    pointer-events: none;
    position: absolute;
    -webkit-appearance: none;
    background: none;
}

.svpf-range-slider input[type='range']::-webkit-slider-runnable-track {
    width: 100%;
    height: 5px;
    background: var(--svpf-secondary);
    -webkit-appearance: none;
}

.svpf-range-slider input[type='range']:nth-child(2)::-webkit-slider-runnable-track {
    background: none;
}

.svpf-range-slider input[type='range']::-webkit-slider-thumb {
    -webkit-appearance: none;
    position: relative;
    height: 20px;
    width: 20px;
    margin-top: -7px;
    background: #fff;
    border: 1px solid var(--svpf-secondary);
    border-radius: 25px;
    z-index: 1;
    pointer-events: auto;
    cursor: grab;
}

.svpf-range-slider input[type='range']::-moz-range-thumb {
    height: 20px;
    width: 20px;
    background: #fff;
    border: 1px solid var(--svpf-secondary);
    border-radius: 25px;
    pointer-events: auto;
    cursor: grab;
}

.svpf-range-slider input[type='range']:active::-webkit-slider-thumb {
    cursor: grabbing;
    box-shadow: 0 0 0 5px rgba(53, 93, 187, .38);
}

.svpf-range-slider input.svpf-slider-min::-webkit-slider-thumb {
    z-index: 2;
}

.svpf-price-labels {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    font-size: 14px;
    color: var(--svpf-primary);
}

.svpf-price-apply {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
}

.svpf-btn-filter-price {
    display: inline-block;
    border-radius: 4px;
    font-size: 14px;
    line-height: 16px;
    padding: 8px 20px;
    text-align: center;
    background-color: var(--svpf-secondary);
    color: #fff !important;
    text-transform: uppercase;
    cursor: pointer;
    transition: all .2s;
}

.svpf-btn-filter-price:hover {
    opacity: .85;
}

/* ========= SORT BAR ========= */
.svpf-sort-bar {
    display: flex;
    align-items: center;
    gap: 0;
    padding: 0;
    margin-bottom: 8px;
    flex-wrap: wrap;
    font-size: 14px;
    line-height: 1.5;
}

.svpf-sort-label {
    margin-right: 10px;
    color: #555;
}

.svpf-sort-item {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 4px 0;
    color: #555;
    font-weight: 400;
    cursor: pointer;
    transition: color .15s ease;
    white-space: nowrap;
    border: none;
    background: none;
    text-decoration: none !important;
}

.svpf-sort-item + .svpf-sort-item::before,
.svpf-sort-item + .svpf-sort-price::before,
.svpf-sort-price + .svpf-sort-item::before {
    content: '·';
    margin-right: 8px;
    color: rgba(234,236,240,1);
    font-weight: 700;
    font-size: 24px;
    line-height: 24px;
}

.svpf-sort-item + .svpf-sort-item,
.svpf-sort-item + .svpf-sort-price,
.svpf-sort-price + .svpf-sort-item {
    margin-left: 8px;
}

.svpf-sort-item:hover,
.svpf-sort-item.active {
    color: var(--svpf-primary);
}

.svpf-sort-item svg {
    flex-shrink: 0;
}

/* Price sort dropdown */
.svpf-sort-price {
    position: relative;
    display: inline-flex;
    align-items: center;
}

.svpf-sort-price + .svpf-sort-item::before {
    content: '·';
    margin-right: 8px;
    color: rgba(234,236,240,1);
    font-weight: 700;
    font-size: 24px;
    line-height: 24px;
}

.svpf-sort-price-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,.1);
    z-index: 100;
    min-width: 140px;
    padding: 4px 0;
}

.svpf-sort-price.open .svpf-sort-price-dropdown {
    display: block;
}

.svpf-sort-dd-item {
    display: block;
    padding: 6px 14px;
    color: #333;
    font-size: 13px;
    white-space: nowrap;
    cursor: pointer;
    text-decoration: none !important;
}

.svpf-sort-dd-item:hover {
    background: #f5f5f5;
    color: var(--svpf-primary);
}

.svpf-sort-dd-item.active {
    color: var(--svpf-primary);
    font-weight: 600;
}

/* ========= LOADING OVERLAY ========= */
/* ========= TOP PROGRESS BAR ========= */
/* Thanh tiến trình mỏng ở đầu trang — không che nội dung */
#svpf-topbar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 9999;
    width: 100%;
    height: 3px;
    pointer-events: none;
}

#svpf-topbar-fill {
    height: 3px;
    width: 0%;
    background: var(--svpf-primary, #e53935);
    border-radius: 0 2px 2px 0;
    transition: width 0.1s linear;
    box-shadow: 0 0 6px rgba(229,57,53,.5);
}

/* Pulse animation khi đang loading */
@keyframes svpf-topbar-pulse {
    0%   { width: 0%; }
    20%  { width: 40%; }
    50%  { width: 65%; }
    80%  { width: 80%; }
    100% { width: 90%; }
}

#svpf-topbar.active #svpf-topbar-fill {
    animation: svpf-topbar-pulse 1.8s ease-out forwards;
}

#svpf-topbar.done #svpf-topbar-fill {
    width: 100% !important;
    animation: none;
    transition: width 0.15s ease-out;
}

/* ========= PRODUCT AREA SKELETON ========= */
/* Mờ nhẹ vùng sản phẩm — dùng will-change để tránh Safari repaint bug */
body.svpf-loading ul.products,
body.svpf-loading .products,
body.svpf-loading .woocommerce-loop-products,
body.svpf-loading .products-loop,
body.svpf-loading .product-grid {
    opacity: 0.45;
    pointer-events: none;
    -webkit-transition: opacity 0.15s ease;
    transition: opacity 0.15s ease;
    position: relative;
    will-change: opacity;
}

body.svpf-loading .woocommerce-result-count,
body.svpf-loading .woocommerce-pagination {
    opacity: 0.45;
    pointer-events: none;
    -webkit-transition: opacity 0.15s ease;
    transition: opacity 0.15s ease;
}

/* Fade in sau khi load xong */
ul.products,
.products,
.woocommerce-loop-products {
    -webkit-transition: opacity 0.2s ease;
    transition: opacity 0.2s ease;
}

/* ========= FILTER ITEMS WITH IMAGES (Auto-detect) ========= */
/* Dùng trong cả dropdown panel và inline section */
.svpf-filter-item--image {
    display: inline-block;
    margin-right: 6px;
    margin-top: 6px;
}

.svpf-filter-item--image a {
    display: flex !important;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 8px 6px;
    min-height: 90px;
    min-width: 80px;
    width: 90px;
    height: auto;
    background: rgba(242,244,247,1);
    border: 1px solid #e0e0e0;
    border-radius: 5px;
    text-align: center;
    gap: 5px;
    transition: border-color .2s, box-shadow .2s;
}

.svpf-filter-item--image a img {
    width: auto;
    max-width: 60px;
    max-height: 55px;
    height: auto;
    object-fit: contain;
    mix-blend-mode: multiply;
    display: block;
    transition: transform 0.15s ease;
}

.svpf-filter-item--image a:hover {
    border-color: var(--svpf-primary);
}

.svpf-filter-item--image a:hover img {
    transform: scale(1.05);
}

.svpf-filter-item--image.active a {
    background: rgba(242,244,247,1);
    border-color: var(--svpf-primary);
    box-shadow: 0 0 0 2px var(--svpf-primary);
}

.svpf-filter-item--image a .svpf-item-label {
    font-size: 11px;
    line-height: 1.2;
    margin: 0;
    word-break: break-word;
    white-space: normal;
}

/* Trong dropdown panel: hiện dạng grid ngang */
.svpf-dd-panel .svpf-filter-item--image {
    margin: 0;
}

.svpf-dd-panel .svpf-filter-items-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

/* ========= NO PRODUCTS ========= */
.svpf-no-products {
    padding: 40px 20px;
    text-align: center;
    font-size: 16px;
    color: #666;
}

/* ========= MOBILE ========= */
@media screen and (max-width: 768px) {
    /* Filter bar ngang, cuộn được */
    .svpf-dropdown-bar {
        display: flex;
        overflow-x: auto;
        overflow-y: visible;
        flex-wrap: nowrap;
        gap: 8px;
        padding-bottom: 4px;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }

    .svpf-dropdown-bar::-webkit-scrollbar {
        display: none;
    }

    /* Dropdown toggle: không bị wrap text */
    .svpf-dropdown {
        flex-shrink: 0;
    }

    .svpf-dd-toggle {
        white-space: nowrap;
    }

    /* Mobile panel: width tự căn theo viewport */
    .svpf-dd-panel {
        width: calc(100vw - 24px) !important;
        min-width: unset !important;
        max-width: calc(100vw - 24px) !important;
        max-height: 65vh !important;
        left: 0 !important;
        right: auto !important;
        z-index: 9991;
        /* Không thêm transform gì thêm — Safari đã có translateZ(0) từ base style */
    }

    /* Dropdown quá gần phải: mở về bên phải */
    .svpf-dropdown:last-child .svpf-dd-panel,
    .svpf-dropdown:nth-last-child(2) .svpf-dd-panel {
        left: auto !important;
        right: 0 !important;
    }

    /* Backdrop được tạo qua JS (#svpf-backdrop) — không dùng pseudo-element */

    /* Sort bar cuộn ngang */
    .svpf-sort-bar {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
        font-size: 13px;
    }

    .svpf-sort-bar::-webkit-scrollbar,
    .svpf-dropdown-bar::-webkit-scrollbar {
        display: none;
    }

    .svpf-sort-label {
        display: none;
    }

    /* Inline image items nhỏ hơn trên mobile */
    .svpf-inline-items .svpf-filter-item {
        width: 80px;
        max-width: 80px;
    }

    .svpf-filter-item--image a {
        min-width: 72px;
        width: 80px;
    }
}

/* ========= DROPDOWN BACKDROP (mobile) ========= */
#svpf-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.25);
    z-index: 9980;           /* DƯỚI panel (9990) */
    -webkit-tap-highlight-color: transparent;
}

#svpf-backdrop.active {
    display: block;
}


/* ========= 2.3.4 BRAND + IOS POPUP FIX ========= */
.svpf-dd-panel.svpf-dd-panel--active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

.svpf-inline-filter--brand .svpf-inline-items {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.svpf-inline-filter--brand .svpf-filter-item--image {
    margin: 0;
}

.svpf-inline-filter--brand .svpf-filter-item--image a {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 132px;
    min-width: 132px;
    min-height: 68px;
    padding: 8px 12px;
    border-radius: 8px;
    gap: 0;
}

.svpf-inline-filter--brand .svpf-filter-item--image a img {
    max-width: 94px;
    max-height: 34px;
}

.svpf-inline-filter--brand .svpf-filter-item--image a .svpf-item-label,
.svpf-inline-filter--brand .svpf-filter-item--image .svpf-item-count {
    display: none !important;
}

@media screen and (max-width: 768px) {
    .svpf-inline-filter--brand .svpf-inline-items {
        gap: 8px;
        overflow-x: auto;
        flex-wrap: nowrap;
        padding-bottom: 4px;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }

    .svpf-inline-filter--brand .svpf-inline-items::-webkit-scrollbar {
        display: none;
    }

    .svpf-inline-filter--brand .svpf-filter-item--image {
        flex: 0 0 auto;
    }

    .svpf-inline-filter--brand .svpf-filter-item--image a {
        width: 116px;
        min-width: 116px;
        min-height: 58px;
        padding: 6px 10px;
        border-radius: 8px;
    }

    .svpf-inline-filter--brand .svpf-filter-item--image a img {
        max-width: 82px;
        max-height: 28px;
    }

    .svpf-dropdown.open {
        z-index: 100002;
    }

    .svpf-dd-panel,
    .svpf-dd-panel.svpf-dd-panel--teleported {
        width: calc(100vw - 24px) !important;
        max-width: calc(100vw - 24px) !important;
        max-height: min(62vh, 420px) !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        z-index: 100003 !important;
    }
}

#svpf-backdrop {
    z-index: 100001;
}

/* ========= 2.3.5 DROPDOWN + BRAND STACKING FIX ========= */
/* Backdrop chỉ nằm dưới panel, không chặn click menu xổ */
#svpf-backdrop {
    z-index: 9998 !important;
}

.svpf-dd-panel,
.svpf-dd-panel.svpf-dd-panel--active,
.svpf-dd-panel.svpf-dd-panel--teleported {
    z-index: 100005 !important;
}

.svpf-dropdown.open {
    z-index: 100004;
}

/* Brand item không bị ép width cũ 110px gây lệch/đè */
.svpf-inline-filter--brand .svpf-inline-items .svpf-filter-item,
.svpf-inline-filter--brand .svpf-filter-item--image {
    width: auto !important;
    max-width: none !important;
    flex: 0 0 auto;
}

.svpf-inline-filter--brand .svpf-filter-item--image a {
    box-sizing: border-box;
    overflow: hidden;
}

.svpf-inline-filter--brand .svpf-filter-item--image a img {
    display: block;
    margin: 0 auto;
}

/* Desktop panel click ổn, mobile panel full width và nằm trên mọi layer */
@media screen and (min-width: 769px) {
    #svpf-backdrop.active {
        display: none !important;
    }
}

@media screen and (max-width: 768px) {
    #svpf-backdrop.active {
        display: block !important;
    }

    .svpf-dd-panel,
    .svpf-dd-panel.svpf-dd-panel--active,
    .svpf-dd-panel.svpf-dd-panel--teleported {
        z-index: 100005 !important;
        top: auto;
    }

    .svpf-inline-filter--brand .svpf-inline-items {
        align-items: stretch;
    }
}
