/* ==========================================================================
   Component: Custom Select — progressive-enhancement dropdown
   Giữ native <select> hidden-but-accessible; JS render trigger + menu.
   ========================================================================== */

.custom-select {
    position: relative;
    display: inline-block;
    width: 100%;
}

/* Native <select> — visually hidden nhưng vẫn tab-focusable cho fallback nếu JS fail.
   Khi JS active, đã set aria-hidden + tabindex=-1 nên chỉ còn vai trò submit value. */
.custom-select__native {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
    pointer-events: none;
}

/* Pre-hydration state: style bare <select.js-custom-select> để match đúng
   kích thước + bề mặt của .custom-select__trigger. Nhờ vậy khi JS chạy xong
   wrap select lại, không có layout shift / flash native select.
   Sau hydration, class .custom-select__native (rule ở trên) ẩn native đi. */
select.js-custom-select:not(.custom-select__native) {
    display: block;
    width: 100%;
    height: 3rem;
    padding: 0 2.5rem 0 0.875rem;
    margin: 0;
    background-color: #ffffff;
    background-image: linear-gradient(45deg, transparent 50%, rgba(66, 66, 66, 0.5) 50%),
                      linear-gradient(135deg, rgba(66, 66, 66, 0.5) 50%, transparent 50%);
    background-position: calc(100% - 1.3125rem) 55%, calc(100% - 0.9375rem) 55%;
    background-size: 0.375rem 0.375rem, 0.375rem 0.375rem;
    background-repeat: no-repeat;
    border: 0.8px solid rgba(0, 0, 0, 0.1);
    border-radius: 0.75rem;
    font-family: "SVN-Gilroy", "Open Sans", sans-serif;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.2;
    color: rgba(26, 26, 26, 0.3);
    appearance: none;
    -webkit-appearance: none;
    outline: none;
    cursor: pointer;
}

/* Placeholder option dùng màu nhạt cho khớp với .custom-select.is-placeholder */
select.js-custom-select:not(.custom-select__native) option[disabled] {
    color: rgba(26, 26, 26, 0.3);
}

select.js-custom-select:not(.custom-select__native) option {
    color: #1a1a1a;
}

/* Native select (touch, không hydrate = iOS/Android picker): khi đã chọn 1 option
   THẬT (value khác rỗng) thì chữ đậm lên; còn placeholder (option value="") giữ
   xám như .is-placeholder. Dùng :has() — live theo selection, đúng cả khi prefill
   hoặc cascade reset (quận/xã về "Đang tải..."). iOS 15.4+ / Chrome 105+ hỗ trợ. */
select.js-custom-select:not(.custom-select__native):has(option:not([value=""]):checked) {
    color: #1a1a1a;
}

/* ===== Trigger button ===== */
.custom-select__trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0 2.5rem 0 0.875rem;
    height: 3rem;
    background: #ffffff;
    border: 0.8px solid rgba(0, 0, 0, 0.1);
    border-radius: 0.75rem;
    font-family: "SVN-Gilroy", "Open Sans", sans-serif;
    font-size: 0.875rem;
    font-weight: 500;
    color: #1a1a1a;
    text-align: left;
    cursor: pointer;
    outline: none;
    transition: border-color 400ms cubic-bezier(0.47, 0, 0.01, 1.01);
}

.custom-select__trigger:focus-visible,
.custom-select.is-open .custom-select__trigger {
    border-color: #09B8B3;
}

.custom-select__trigger[disabled] {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Placeholder state: native chưa chọn (value rỗng) */
.custom-select.is-placeholder .custom-select__value {
    color: rgba(26, 26, 26, 0.3);
}

.custom-select__value {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.2;
}

.custom-select__caret {
    position: absolute;
    right: 0.875rem;
    top: 50%;
    width: 1.25rem;
    height: 1.25rem;
    transform: translateY(-50%);
    pointer-events: none;
    transition: transform 200ms ease;
}

.custom-select.is-open .custom-select__caret {
    transform: translateY(-50%) rotate(180deg);
}

/* ===== Menu ===== */
.custom-select__menu {
    position: absolute;
    top: calc(100% + 0.375rem);
    left: 0;
    right: 0;
    z-index: 50;
    margin: 0;
    list-style: none;
    background: #ffffff;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 0.75rem;
    box-shadow: 0 0.75rem 1.5rem rgba(0, 0, 0, 0.08);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-0.25rem);

    /* Trạng thái ĐÓNG phải có chiều cao thật = 0. Absolute + visibility:hidden vẫn
       đóng góp vào scrollable overflow của container cha → sinh vùng cuộn trống
       ~13rem dưới ô Xã/Phường trong drawer. Collapse max-height/padding/border để
       menu đóng chiếm 0px. Các prop này snap sau 200ms (giống visibility) nên
       hiệu ứng fade-out vẫn mượt, không bị sập đột ngột. */
    max-height: 0;
    padding: 0 0;
    border-width: 0;
    overflow: hidden;
    transition: opacity 200ms ease, transform 200ms ease,
                visibility 0s linear 200ms, max-height 0s linear 200ms,
                padding 0s linear 200ms, border-width 0s linear 200ms;
}

.custom-select.is-open .custom-select__menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    max-height: 15rem;
    padding: 0.375rem 0;
    border-width: 1px;
    overflow-y: auto;
    transition: opacity 200ms ease, transform 200ms ease,
                visibility 0s, max-height 0s, padding 0s, border-width 0s;
}

.custom-select__search {
    position: sticky;
    top: -0.375rem;
    z-index: 1;
    padding: 0.5rem;
    background: #ffffff;
}

.custom-select__search-input {
    display: block;
    width: 100%;
    height: 2.375rem;
    padding: 0 0.75rem;
    background: #ffffff;
    border: 0.8px solid rgba(0, 0, 0, 0.1);
    border-radius: 0.625rem;
    font-family: "SVN-Gilroy", "Open Sans", sans-serif;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.2;
    color: #1a1a1a;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.custom-select__search-input::placeholder {
    color: rgba(26, 26, 26, 0.3);
}

.custom-select__search-input:focus-visible {
    border-color: #09B8B3;
}

.custom-select__option {
    padding: 0.625rem 0.875rem;
    font-family: "SVN-Gilroy", "Open Sans", sans-serif;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.2;
    color: #424242;
    cursor: pointer;
    transition: background-color 150ms ease, color 150ms ease;
}

.custom-select__option:hover,
.custom-select__option.is-highlight {
    background: rgba(9, 184, 179, 0.08);
    color: #09B8B3;
}

.custom-select__option.is-selected {
    background: rgba(9, 184, 179, 0.12);
    color: #09B8B3;
    font-weight: 600;
}

.custom-select__option[aria-disabled="true"] {
    color: rgba(66, 66, 66, 0.4);
    cursor: not-allowed;
    pointer-events: none;
}

.custom-select__empty {
    padding: 0.75rem 0.875rem;
    font-family: "SVN-Gilroy", "Open Sans", sans-serif;
    font-size: 0.8125rem;
    font-weight: 500;
    color: rgba(66, 66, 66, 0.5);
    text-align: center;
}

/* ===== Variant: inline (label + value cùng 1 dòng, nền xám nhạt) — single-product ===== */
.custom-select--inline {
    display: inline-flex;
    align-items: center;
    width: auto;
    background-color: rgba(164, 164, 164, 0.1);
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 0.5rem;
    padding: 0;
}

.custom-select--inline .custom-select__label {
    font-family: "SVN-Gilroy", sans-serif;
    font-weight: 500;
    font-size: 0.875rem;
    line-height: 1.2;
    color: #424242;
    white-space: nowrap;
    flex-shrink: 0;
    padding: 0.625rem 0 0.625rem 1rem;
    margin-right: 0.375rem;
    pointer-events: none;
}

.custom-select--inline .custom-select__trigger {
    height: auto;
    padding: 0.625rem 2rem 0.625rem 0;
    background: transparent;
    border: none;
    border-radius: 0;
    color: rgba(66, 66, 66, 0.6);
    font-size: 0.875rem;
    line-height: 1.2;
}

.custom-select--inline .custom-select__trigger:focus-visible,
.custom-select--inline.is-open .custom-select__trigger {
    border-color: transparent;
}

.custom-select--inline .custom-select__caret {
    right: 1rem;
    width: 1rem;
    height: 1rem;
}

.custom-select--inline.is-placeholder .custom-select__value {
    color: rgba(66, 66, 66, 0.6);
}

.custom-select--inline .custom-select__menu {
    left: 0;
    right: auto;
    min-width: 100%;
}

/* ===== Mobile ===== */
@media screen and (max-width: 639.98px) {
    .custom-select__trigger {
        font-size: 0.875rem;
    }

    /* Chỉ set max-height ở trạng thái MỞ — menu đóng phải giữ max-height:0 để
       không sinh vùng cuộn trống trong drawer (xem comment ở rule base). */
    .custom-select.is-open .custom-select__menu {
        max-height: 13rem;
    }

    /* Searchable (địa chỉ tỉnh/quận/xã): bàn phím ảo chiếm ~50% màn hình nên menu
       phải thấp hơn, và ô search phải dính đỉnh menu để luôn nhìn thấy khi gõ. */
    .custom-select--searchable.is-open .custom-select__menu {
        max-height: 40vh;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .custom-select--searchable .custom-select__search {
        box-shadow: 0 1px 0 rgba(0, 0, 0, 0.06);
    }

    /* Vùng chạm tối thiểu 44px theo HIG — option desktop 0.625rem quá mỏng cho ngón tay. */
    .custom-select--searchable .custom-select__option {
        padding-top: 0.875rem;
        padding-bottom: 0.875rem;
    }

    /* ==================================================================
       Bottom sheet — select có data-sheet-on-mobile
       Portal ra <body> (xem sheet.js). Khung neo theo visualViewport nên
       luôn nằm TRÊN bàn phím ảo iOS, không phụ thuộc vị trí trigger và
       không bị overflow:hidden của .checkout-drawer__sheet cắt.
       ================================================================== */

    /* --cs-vv-top / --cs-vv-height do JS ghi bằng px — là số ĐO RUNTIME từ
       visualViewport, không phải token thiết kế, nên không áp quy tắc rem. */
    .custom-select-sheet {
        position: fixed;
        left: 0;
        right: 0;
        top: var(--cs-vv-top, 0);
        height: var(--cs-vv-height, 100%);
        z-index: 1200;
        visibility: hidden;
        pointer-events: none;
    }

    .custom-select-sheet.is-open {
        visibility: visible;
        pointer-events: auto;
    }

    .custom-select-sheet__backdrop {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        transition: opacity 250ms ease;
    }

    .custom-select-sheet.is-open .custom-select-sheet__backdrop {
        opacity: 1;
    }

    .custom-select-sheet__panel {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        max-height: 100%;
        display: flex;
        flex-direction: column;
        background: #ffffff;
        border-radius: 1rem 1rem 0 0;
        transform: translateY(100%);
        transition: transform 300ms cubic-bezier(0.32, 0.72, 0, 1);
    }

    .custom-select-sheet.is-open .custom-select-sheet__panel {
        transform: translateY(0);
    }

    .custom-select-sheet__header {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0.875rem 1rem;
        border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    }

    .custom-select-sheet__title {
        font-family: "SVN-Gilroy", "Open Sans", sans-serif;
        font-size: 1rem;
        font-weight: 600;
        line-height: 1.2;
        color: #1a1a1a;
    }

    .custom-select-sheet__close {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 2rem;
        height: 2rem;
        padding: 0;
        background: rgba(164, 164, 164, 0.15);
        border: none;
        border-radius: 50%;
        color: #424242;
        cursor: pointer;
    }

    .custom-select-sheet__close:focus-visible {
        outline: 0.125rem solid #09b8b3;
        outline-offset: 0.125rem;
    }

    /* min-height:0 bắt buộc — thiếu nó flex child không co lại được và list
       tràn ra ngoài panel thay vì cuộn bên trong. */
    .custom-select-sheet__list {
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
    }

    /* Menu giữ nguyên class cũ nhưng đổi hoàn toàn ngữ cảnh: không còn absolute
       neo theo trigger, mà lấp đầy panel. Giữ position:relative để offsetTop của
       option vẫn tính theo menu (scrollOptionIntoView dựa vào đó). */
    .custom-select-sheet .custom-select__menu {
        position: relative;
        top: auto;
        right: auto;
        bottom: auto;
        left: auto;
        flex: 1;
        min-height: 0;
        max-height: none;
        padding: 0 0 0.5rem;
        border-width: 0;
        border-radius: 0;
        box-shadow: none;
        opacity: 1;
        visibility: visible;
        transform: none;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        transition: none;
    }

    /* Menu đã bị portal ra khỏi .custom-select--searchable nên các rule dạng
       `.custom-select--searchable .custom-select__option` KHÔNG còn match nữa —
       phải khai báo lại vùng chạm và style ô search ở đây. */
    .custom-select-sheet .custom-select__search {
        top: 0;
        padding: 0.75rem 1rem;
        box-shadow: 0 1px 0 rgba(0, 0, 0, 0.06);
    }

    .custom-select-sheet .custom-select__option {
        padding: 0.875rem 1rem;
    }

    .custom-select-sheet .custom-select__empty {
        padding: 1.25rem 1rem;
    }

    /* iOS Safari tự phóng to (zoom) khi focus input/select có font-size < 16px.
       Pin 16px chặn auto-zoom. Ngoại lệ px bắt buộc theo nền tảng iOS — không phải
       token. select.js-custom-select không-hydrate = native picker trên touch. */
    .custom-select__search-input,
    select.js-custom-select:not(.custom-select__native) {
        font-size: 16px;
    }
}
