/**
 * TWB Smart Search Widget – Frontend & Editor Styles
 *
 * Cấu trúc:
 *   1. Base / Reset
 *   2. Layout: Inline
 *   3. Layout: Full Width
 *   4. Layout: Overlay (trigger + modal)
 *   5. Style Presets: classic / rounded / minimal / boxed
 *   6. Ô input & icon & nút xoá
 *   7. Nút submit
 *   8. Live Search Dropdown & kết quả
 *   9. Trạng thái: loading / empty / keyboard-active
 *  10. Responsive
 *  11. Elementor editor preview fixes
 *
 * Nguyên tắc:
 *   - Không dùng !important trừ khi bắt buộc override
 *   - Không đặt màu sắc cứng – dùng CSS custom properties kết hợp selectors
 *   - Tất cả animation dùng transform/opacity (GPU-friendly)
 *   - Dropdown dùng visibility+opacity thay vì display:none để transition mượt
 *
 * @package TrustWeb_Builder
 * @since   3.1.0
 */

/* ─── 1. BASE / RESET ─────────────────────────────────────────────────────── */

.twb-search-wrapper {
    position: relative;
    font-size: inherit;
    line-height: 1;
    width: 100%;
}

.twb-search-form-wrap {
    position: relative;
    display: flex;
    align-items: stretch;
    width: 100%;
    border: 1px solid #d1d5db;
    background-color: #fff;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.twb-search-form-wrap:focus-within {
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
    outline: none;
}

.twb-search-form {
    display: flex;
    align-items: center;
    width: 100%;
    margin: 0;
    padding: 0;
    gap: 0;
}

/* ─── 2. LAYOUT: INLINE ───────────────────────────────────────────────────── */

.twb-search-wrapper.layout-inline {
    display: inline-flex;
    width: auto;
}

.twb-search-wrapper.layout-inline .twb-search-form-wrap {
    flex-direction: row;
}

/* ─── 3. LAYOUT: FULL WIDTH ───────────────────────────────────────────────── */

.twb-search-wrapper.layout-fullwidth {
    display: block;
    width: 100%;
    /* Fix: Elementor widget wrapper là flex container.
       min-width: 0 để block child không overflow ra ngoài flex item. */
    min-width: 0;
    box-sizing: border-box;
}

.twb-search-wrapper.layout-fullwidth .twb-search-form-wrap {
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
}

.twb-search-wrapper.layout-fullwidth .twb-search-form {
    width: 100%;
    min-width: 0;
}

/* ─── 4. LAYOUT: OVERLAY ──────────────────────────────────────────────────── */

.twb-search-wrapper.layout-overlay {
    display: inline-flex;
    align-items: center;
    width: auto;
}

/* Nút trigger */
.twb-overlay-trigger {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 6px;
    color: inherit;
    line-height: 1;
    font-family: inherit;
    font-size: inherit;
    transition: color 0.2s ease, opacity 0.2s ease;
}

.twb-overlay-trigger:hover {
    opacity: 0.75;
}

.twb-overlay-trigger svg {
    display: block;
    width: 22px;
    height: 22px;
    flex-shrink: 0;
}

.twb-overlay-trigger-label {
    font-size: 0.9em;
}

/* Modal backdrop */
.twb-overlay-modal {
    position: fixed;
    inset: 0;
    z-index: 99999;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 60px 16px 16px;
    background-color: rgba(0, 0, 0, 0.6);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.25s ease, visibility 0.25s ease;
    overflow-y: auto;
}

.twb-overlay-modal.is-open {
    opacity: 1;
    visibility: visible;
}

/* Inner content area */
.twb-overlay-inner {
    position: relative;
    width: 100%;
    max-width: 700px;
    background: #fff;
    border-radius: 12px;
    padding: 24px;
    transform: translateY(-20px);
    transition: transform 0.25s ease;
}

.twb-overlay-modal.is-open .twb-overlay-inner {
    transform: translateY(0);
}

/* Close button trong overlay */
.twb-overlay-close {
    position: absolute;
    top: 0;
    right: 0;
    background: none;
    border: none;
    cursor: pointer;
    color: #6b7280;
    padding: 4px;
    line-height: 1;
    border-radius: 4px;
    transition: color 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.twb-overlay-close:hover {
    color: #111;
}

.twb-overlay-close svg {
    width: 20px;
    height: 20px;
    display: block;
}

/* ─── 5. STYLE PRESETS ────────────────────────────────────────────────────── */

/* Classic: border vuông, nền trắng (default) */
.twb-search-form-wrap.preset-classic {
    border-radius: 4px;
}

/* Rounded: bo tròn hoàn toàn */
/* BUG FIX: Không dùng overflow:hidden ở đây — sẽ clip dropdown.
   Thay vào đó bo tròn từng phần tử con trực tiếp. */
.twb-search-form-wrap.preset-rounded {
    border-radius: 9999px;
}

.twb-search-form-wrap.preset-rounded .twb-search-input {
    border-radius: 9999px 0 0 9999px;
}

.twb-search-form-wrap.preset-rounded .twb-search-icon-inner {
    border-radius: 9999px 0 0 9999px;
}

.twb-search-form-wrap.preset-rounded .twb-search-btn {
    border-radius: 0 9999px 9999px 0;
}

/* Khi không có button, input bo cả bên phải */
.twb-search-form-wrap.preset-rounded .twb-search-input:last-child,
.twb-search-form-wrap.preset-rounded .twb-search-clear:last-child {
    border-radius: 0 9999px 9999px 0;
}

/* Minimal: chỉ viền dưới, nền trong suốt */
.twb-search-form-wrap.preset-minimal {
    border: none;
    border-bottom: 2px solid #d1d5db;
    border-radius: 0;
    background-color: transparent;
    box-shadow: none;
}

.twb-search-form-wrap.preset-minimal:focus-within {
    box-shadow: none;
    border-bottom-color: #2563eb;
}

.twb-search-form-wrap.preset-minimal .twb-search-input {
    background: transparent;
}

.twb-search-form-wrap.preset-minimal .twb-search-btn {
    background: transparent;
    color: #6b7280;
    border-radius: 0;
}

/* Boxed: nền xám nhạt */
.twb-search-form-wrap.preset-boxed {
    border-radius: 8px;
    border-color: transparent;
    background-color: #f3f4f6;
}

.twb-search-form-wrap.preset-boxed:focus-within {
    background-color: #fff;
    border-color: #2563eb;
}

.twb-search-form-wrap.preset-boxed .twb-search-input {
    background: transparent;
}

.twb-search-form-wrap.preset-boxed .twb-search-btn {
    background: transparent;
    color: #6b7280;
}

/* ─── 6. INPUT & ICON & NÚT XOÁ ──────────────────────────────────────────── */

.twb-search-input {
    flex: 1 1 auto;
    min-width: 0;
    height: 46px;
    padding: 0 12px;
    border: none !important;
    outline: none;
    background: transparent;
    color: inherit;
    font-family: inherit;
    font-size: 1em;
    line-height: 46px;
    /* Reset browser appearance */
    -webkit-appearance: none;
    appearance: none;
}

/* Xoá icon X mặc định của Chrome/Edge trên input[type=search] */
.twb-search-input::-webkit-search-decoration,
.twb-search-input::-webkit-search-cancel-button,
.twb-search-input::-webkit-search-results-button,
.twb-search-input::-webkit-search-results-decoration {
    display: none;
}

/* Icon kính lúp bên trái */
.twb-search-icon-inner {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 0 0 12px;
    color: #9ca3af;
    flex-shrink: 0;
    pointer-events: none;
    line-height: 1;
}

.twb-search-icon-inner svg {
    display: block;
    width: 17px;
    height: 17px;
}

/* Input có icon → giảm padding trái.
   BUG FIX: thêm class .has-icon để fallback cho browser không support :has() */
.twb-search-form.has-icon .twb-search-input {
    padding-left: 6px;
}
/* Progressive enhancement với :has() cho browser hiện đại */
.twb-search-form:has(.twb-search-icon-inner) .twb-search-input {
    padding-left: 6px;
}

/* Nút xoá */
.twb-search-clear {
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0 8px;
    color: #9ca3af;
    flex-shrink: 0;
    line-height: 1;
    transition: color 0.15s ease;
}

.twb-search-clear:hover {
    color: #374151;
}

.twb-search-clear svg {
    display: block;
    width: 16px;
    height: 16px;
}

.twb-search-clear[hidden] {
    display: none;
}

/* ─── 7. NÚT SUBMIT ───────────────────────────────────────────────────────── */

.twb-search-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex-shrink: 0;
    min-width: 48px;
    height: 46px;
    padding: 0 14px;
    border: none;
    cursor: pointer;
    background-color: #2563eb;
    color: #fff;
    font-family: inherit;
    font-size: inherit;
    line-height: 1;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.twb-search-btn:hover {
    background-color: #1d4ed8;
}

.twb-search-btn svg {
    display: block;
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

/* ─── 8. LIVE SEARCH DROPDOWN ─────────────────────────────────────────────── */

/* Container dropdown */
.twb-search-results {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    /* BUG FIX: z-index 99999 để vượt qua Elementor sticky header (9999) */
    z-index: 99999;
    background-color: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1), 0 2px 8px rgba(0, 0, 0, 0.06);
    overflow: hidden;
    max-height: 420px;
    overflow-y: auto;
    /* Smooth show/hide */
    opacity: 0;
    visibility: hidden;
    transform: translateY(-6px);
    transition: opacity 0.18s ease, transform 0.18s ease, visibility 0.18s ease;
}

.twb-search-results.is-visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.twb-search-results[hidden] {
    display: none !important; /* reset trạng thái ban đầu */
}

/* Scroll bar gọn */
.twb-search-results::-webkit-scrollbar {
    width: 5px;
}
.twb-search-results::-webkit-scrollbar-track {
    background: #f9fafb;
}
.twb-search-results::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 99px;
}

/* Item kết quả */
.twb-result-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    text-decoration: none;
    color: inherit;
    transition: background-color 0.15s ease;
    cursor: pointer;
    border-bottom: 1px solid #f3f4f6;
}

.twb-result-item:last-of-type {
    border-bottom: none;
}

.twb-result-item:hover,
.twb-result-item.is-active {
    background-color: #f3f4f6;
    text-decoration: none;
}

/* Ảnh đại diện */
.twb-result-thumb {
    flex-shrink: 0;
    width: 56px;
    height: 56px;
    overflow: hidden;
    border-radius: 6px;
    background: #f3f4f6;
    display: flex;
    align-items: center;
    justify-content: center;
}

.twb-result-thumb img {
    display: block;
    width: 56px;
    height: 56px;
    object-fit: cover;
}

/* Placeholder khi không có ảnh */
.twb-result-thumb-placeholder {
    color: #d1d5db;
}

.twb-result-thumb-placeholder svg {
    width: 24px;
    height: 24px;
    display: block;
}

/* Nội dung text */
.twb-result-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.twb-result-title {
    font-size: 0.9em;
    font-weight: 600;
    color: #111827;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0;
    line-height: 1.4;
}

/* Highlight từ khoá tìm kiếm */
.twb-result-title mark,
.twb-result-excerpt mark {
    background: #fef08a;
    color: inherit;
    border-radius: 2px;
    padding: 0 1px;
}

.twb-result-excerpt {
    font-size: 0.78em;
    color: #6b7280;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0;
    line-height: 1.4;
}

.twb-result-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.72em;
    color: #9ca3af;
    margin: 0;
    line-height: 1;
}

/* Badge loại bài */
.twb-result-badge {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 4px;
    background: #eff6ff;
    color: #3b82f6;
    font-size: 0.85em;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    white-space: nowrap;
}

/* Link "Xem tất cả kết quả" */
.twb-result-view-all {
    display: block;
    text-align: center;
    padding: 10px 14px;
    font-size: 0.85em;
    font-weight: 500;
    color: #2563eb;
    background-color: #f0f9ff;
    text-decoration: none;
    border-top: 1px solid #e5e7eb;
    transition: background-color 0.15s ease;
}

.twb-result-view-all:hover {
    background-color: #dbeafe;
    text-decoration: none;
}

/* Trạng thái "Không có kết quả" */
.twb-result-empty {
    padding: 20px 14px;
    text-align: center;
    font-size: 0.875em;
    color: #9ca3af;
}

/* Skeleton loading */
.twb-result-skeleton {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
}

.twb-skeleton-thumb,
.twb-skeleton-line {
    background: linear-gradient(90deg, #f3f4f6 25%, #e9eaeb 50%, #f3f4f6 75%);
    background-size: 200% 100%;
    animation: twb-shimmer 1.4s ease-in-out infinite;
    border-radius: 4px;
}

.twb-skeleton-thumb {
    width: 56px;
    height: 56px;
    flex-shrink: 0;
    border-radius: 6px;
}

.twb-skeleton-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.twb-skeleton-line {
    height: 12px;
}

.twb-skeleton-line:first-child {
    width: 70%;
}

.twb-skeleton-line:last-child {
    width: 45%;
    height: 10px;
}

@keyframes twb-shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ─── 9. TRẠNG THÁI LOADING ──────────────────────────────────────────────── */

/* Spinner trong nút khi đang search */
.twb-search-btn.is-loading svg {
    animation: twb-spin 0.7s linear infinite;
}

@keyframes twb-spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

/* ─── 10. RESPONSIVE ─────────────────────────────────────────────────────── */

@media (max-width: 767px) {
    .twb-search-wrapper.layout-inline {
        width: 100%;
    }

    .twb-search-wrapper.layout-inline .twb-search-form-wrap {
        width: 100%;
    }

    .twb-overlay-inner {
        border-radius: 0;
        padding: 20px 16px;
    }

    .twb-overlay-modal {
        padding: 0;
        align-items: flex-start;
    }


    .twb-result-thumb {
        width: 44px;
        height: 44px;
    }

    .twb-result-thumb img {
        width: 44px;
        height: 44px;
    }

    .twb-skeleton-thumb {
        width: 44px;
        height: 44px;
    }
}

/* ─── 11. ELEMENTOR EDITOR FIXES ─────────────────────────────────────────── */

/* BUG FIX: form-wrap phải overflow:visible để dropdown không bị clip.
   Áp dụng cả frontend lẫn editor. Mỗi preset tự bo tròn từng element con. */
.twb-search-form-wrap {
    overflow: visible;
}

/* Elementor widget container có thể set width auto hoặc overflow hidden —
   buộc container của search widget chiếm đủ không gian */
.elementor-widget-twb-search {
    width: 100%;
}

.elementor-widget-twb-search .elementor-widget-container {
    overflow: visible;
}

/* Ngăn dropdown bị clip bởi Elementor editor column overflow:hidden */
.elementor-editor-active .twb-search-form-wrap {
    overflow: visible;
}

/* Preview note cho overlay layout */
.elementor-editor-active .twb-overlay-hint {
    display: block;
    font-size: 11px;
    color: #9ca3af;
    margin-top: 6px;
    font-style: italic;
}