/* ============================================================
   CF7 Phone Field + Validation Styles
   Matches trading-account-form plugin visual style
   ============================================================ */

/* ── Flag sprite fix ────────────────────────────────────────── */
/* Force intl-tel-input flags to load from CDN sprite correctly */
.iti__flag {
    background-image: url("https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.13/img/flags.png") !important;
    background-repeat: no-repeat;
    background-size: 5652px 15px;
    width: 20px;
    height: 15px;
    box-shadow: 0 0 1px 0 #888;
    display: inline-block;
    vertical-align: middle;
}

@media (min-resolution: 2dppx) {
    .iti__flag {
        background-image: url("https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.13/img/flags@2x.png") !important;
    }
}

/* ── CF7 form-control-wrap fix ───────────────────────────────── */
/* CF7 wraps inputs in a <span> — force it to block for proper sizing */
.wpcf7-form-control-wrap[data-name*="phone"],
.wpcf7-form-control-wrap[data-name*="tel"] {
    display: block;
    width: 100%;
}

/* Ensure CF7 label containing phone is also block */
.wpcf7-form label:has(input[type="tel"]) {
    display: block;
    width: 100%;
}

/* ── Phone wrapper ──────────────────────────────────────────── */
.cf7-phone-wrapper {
    position: relative;
    display: block;
    width: 100%;
}

.cf7-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

/* ── intl-tel-input container inside CF7 ────────────────────── */
.cf7-phone-wrapper .iti,
.wpcf7-form .iti {
    width: 100%;
    display: block;
}

.cf7-phone-wrapper .iti--allow-dropdown input[type="tel"],
.wpcf7-form .iti--allow-dropdown input[type="tel"] {
    padding-left: 95px;
    padding-right: 35px !important;
    width: 100% !important;
    box-sizing: border-box;
    height: 44px;
    font-size: 16px;
}

/* ── Selected flag area ─────────────────────────────────────── */
.iti__selected-flag {
    padding: 0 6px 0 8px;
    display: flex;
    align-items: center;
    background-color: white;
    gap: 4px;
    cursor: pointer;
}

.iti__flag-container {
    z-index: 6;
}

.iti__selected-dial-code {
    font-size: 0.9rem;
    color: #333;
    margin-left: 4px;
    direction: ltr;
}

.iti__arrow {
    margin-left: 6px;
    border-left: 3px solid transparent;
    border-right: 3px solid transparent;
    border-top: 4px solid #555;
    width: 0;
    height: 0;
}

.iti__arrow--up {
    border-top: none;
    border-bottom: 4px solid #555;
}

/* ── Country dropdown ───────────────────────────────────────── */
.iti__country-list {
    position: absolute;
    z-index: 1000;
    max-height: 220px;
    overflow-y: auto;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    width: 320px;
    top: 100%;
    margin: 0;
    padding: 0;
    list-style: none;
}

.iti__country {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 0.9rem;
    color: #333;
    direction: ltr;
    text-align: left;
    gap: 8px;
    transition: background-color 0.15s;
}

.iti__country:hover {
    background-color: #f5f5f5;
}

.iti__country.iti__highlight {
    background-color: #e8f4fd;
}

.iti__flag-box {
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;
    width: 20px;
    margin-right: 8px;
}

.iti__country-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-right: 6px;
}

.iti__dial-code {
    color: #888;
    font-size: 0.85rem;
    flex-shrink: 0;
}

/* Preferred countries separator */
.iti__divider {
    border-bottom: 1px solid #eee;
    padding: 0;
    margin: 4px 0;
}

/* ── Validation icon ────────────────────────────────────────── */
.cf7-validation-icon {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    display: none;
    width: 20px;
    height: 20px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    z-index: 5;
    pointer-events: none;
}

/* ── Validation message ─────────────────────────────────────── */
.cf7-validation-message {
    display: none;
    font-size: 0.8rem;
    margin-top: 4px;
    min-height: 20px;
}

/* ── Valid state ─────────────────────────────────────────────── */
.wpcf7-form input.cf7-valid {
    border-color: #4CAF50 !important;
}

/* ── Invalid state ──────────────────────────────────────────── */
.wpcf7-form input.cf7-invalid {
    border-color: #F44336 !important;
}

/* ── LTR forms (Spanish, English, etc.) ────────────────────── */
/* Force the entire CF7 form to LTR on non-RTL pages */
html[lang="es"] .wpcf7-form,
html[lang="en"] .wpcf7-form,
html[lang="pt"] .wpcf7-form,
html[lang="de"] .wpcf7-form,
html[lang="it"] .wpcf7-form,
html[lang="vi"] .wpcf7-form,
html[lang="th"] .wpcf7-form {
    direction: ltr;
    text-align: left;
}

html[lang="es"] .wpcf7-form label,
html[lang="en"] .wpcf7-form label,
html[lang="pt"] .wpcf7-form label,
html[lang="de"] .wpcf7-form label,
html[lang="it"] .wpcf7-form label,
html[lang="vi"] .wpcf7-form label,
html[lang="th"] .wpcf7-form label {
    text-align: left;
    direction: ltr;
}

html[lang="es"] .wpcf7-form input,
html[lang="en"] .wpcf7-form input,
html[lang="pt"] .wpcf7-form input,
html[lang="de"] .wpcf7-form input,
html[lang="it"] .wpcf7-form input,
html[lang="vi"] .wpcf7-form input,
html[lang="th"] .wpcf7-form input {
    direction: ltr;
    text-align: left;
}

/* Phone input is always LTR for number entry */
.wpcf7-form .iti--allow-dropdown input[type="tel"],
.cf7-phone-wrapper .iti--allow-dropdown input[type="tel"] {
    direction: ltr;
    text-align: left;
}

/* Flag container on LTR: left side */
.iti__flag-container {
    left: 0;
    right: auto;
}

/* ── RTL support (Arabic, Hebrew) ──────────────────────────── */
/* Phone field stays LTR layout even on RTL pages (flag LEFT, number RIGHT) */
html[dir="rtl"] .cf7-validation-icon {
    right: auto;
    left: 10px;
}

html[dir="rtl"] .wpcf7-form .iti,
html[dir="rtl"] .cf7-phone-wrapper .iti {
    direction: ltr;
}

html[dir="rtl"] .wpcf7-form .iti--allow-dropdown input[type="tel"],
html[dir="rtl"] .cf7-phone-wrapper .iti--allow-dropdown input[type="tel"] {
    direction: ltr;
    text-align: left;
    padding-left: 95px !important;
    padding-right: 35px !important;
}

html[dir="rtl"] .iti__flag-container {
    left: 0 !important;
    right: auto !important;
}

html[dir="rtl"] .iti__selected-dial-code {
    margin-left: 4px;
    margin-right: 0;
}

html[dir="rtl"] .iti__arrow {
    margin-left: 6px;
    margin-right: 0;
}

/* Keep dropdown LTR even on RTL pages for proper flag/name layout */
html[dir="rtl"] .iti__country-list {
    left: 0 !important;
    right: auto !important;
}

html[dir="rtl"] .iti__country {
    direction: ltr;
    text-align: left;
}

/* ── Scrollbar styling ──────────────────────────────────────── */
.iti__country-list::-webkit-scrollbar {
    width: 6px;
}

.iti__country-list::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
}

.iti__country-list::-webkit-scrollbar-thumb {
    background: #bbb;
    border-radius: 3px;
}

.iti__country-list::-webkit-scrollbar-thumb:hover {
    background: #999;
}

/* ── Responsive ─────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
    /* Ensure phone wrapper is visible and full-width on mobile */
    .cf7-phone-wrapper,
    .wpcf7-form .iti,
    .wpcf7-form-control-wrap[data-name*="phone"],
    .wpcf7-form-control-wrap[data-name*="tel"] {
        display: block !important;
        width: 100% !important;
        overflow: visible !important;
    }

    /* Make phone input full width and properly sized for mobile */
    .wpcf7-form .iti--allow-dropdown input[type="tel"],
    .cf7-phone-wrapper .iti--allow-dropdown input[type="tel"] {
        width: 100% !important;
        height: 48px;
        font-size: 16px !important;
        padding-right: 30px !important;
        border-radius: 8px;
    }

    /* Country dropdown on mobile — full width of form */
    .iti__country-list {
        width: calc(100vw - 2rem) !important;
        max-width: 320px;
        max-height: 250px;
    }

    .cf7-validation-icon {
        right: 8px;
        width: 18px;
        height: 18px;
    }

    /* Prevent mobile safari zoom on focus */
    .wpcf7-form input[type="tel"],
    .wpcf7-form input[type="text"],
    .wpcf7-form input[type="email"] {
        font-size: 16px !important;
    }

    .iti__country {
        padding: 10px 12px;
        min-height: 44px;
    }

    /* Ensure parent containers don't clip the phone field */
    .form-section,
    .wpcf7,
    .wpcf7-form {
        overflow: visible !important;
    }
}

/* ── Body-attached dropdown (dropdownContainer: body) ── */
/* intl-tel-input creates .iti--container on document.body */
.iti--container {
    z-index: 99999;
}

/* Desktop: normal absolute dropdown near the input */
@media screen and (min-width: 769px) {
    .iti--container .iti__country-list {
        max-height: 250px;
        width: 320px;
        border-radius: 4px;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
        overflow-y: auto;
        background: #fff;
    }

    .iti--container .iti__country {
        direction: ltr;
        text-align: left;
        padding: 8px 12px;
        display: flex;
        align-items: center;
        gap: 8px;
    }
}

/* Mobile: fullscreen modal overlay so dropdown is always reachable */
@media screen and (max-width: 768px) {
    .iti--container {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        background: rgba(0, 0, 0, 0.5) !important;
    }

    .iti--container .iti__country-list {
        position: relative !important;
        top: auto !important;
        bottom: auto !important;
        left: auto !important;
        max-height: 70vh;
        width: 90vw !important;
        max-width: 360px;
        margin: 0 auto;
        border-radius: 12px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        background: #fff;
    }

    .iti--container .iti__country {
        direction: ltr;
        text-align: left;
        padding: 14px 16px;
        min-height: 48px;
        display: flex;
        align-items: center;
        gap: 10px;
        border-bottom: 1px solid #f0f0f0;
    }

    .iti--container .iti__country:last-child {
        border-bottom: none;
    }

    .iti--container .iti__country:active,
    .iti--container .iti__country.iti__highlight {
        background-color: #e8f4fd;
    }
}
