/**
 * TrustWeb Builder — Image Accordion Widget CSS
 *
 * Kỹ thuật:
 *   - CSS custom properties cho easy theming & per-panel override
 *   - Horizontal: flex-grow transition (GPU-accelerated)
 *   - Vertical: max-height transition
 *   - Overlay: solid color hoặc gradient (bottom → transparent)
 *   - Remote Counter: CSS grid 2-col, image crossfade
 *   - Responsive: media query stack vertical trên mobile
 *   - Accessibility: focus-visible, reduced-motion
 *   - All selectors scoped dưới .twb-ia-wrapper — không leak
 *
 * @package TrustWeb_Builder
 * @since   3.3.5
 */

/* ─────────────────────────────────────────────────────────────────────────────
   CSS CUSTOM PROPERTIES (defaults — bị ghi đè bởi Elementor selectors)
   ───────────────────────────────────────────────────────────────────────────── */

.twb-ia-panel {
    --ia-speed:              500ms;
    --ia-overlay-color:      #1a1a2e;
    --ia-overlay-opacity:    0.5;
    --ia-overlay-opacity-active: 0.65;
    --ia-collapsed-w:        80px;
    --ia-collapsed-h:        80px;
    --ia-title-ease:         cubic-bezier(0.22, 0.61, 0.36, 1);
    --ia-closed-title-offset-x: 0px;
    --ia-closed-title-offset-y: 0px;
}

/* ─────────────────────────────────────────────────────────────────────────────
   WRAPPER
   ───────────────────────────────────────────────────────────────────────────── */

.twb-ia-wrapper {
    --ia-active-h: 300px;
    position: relative;
    width: 100%;
    box-sizing: border-box;
}

/* ─────────────────────────────────────────────────────────────────────────────
   TRACK — HORIZONTAL
   Layout: row, panels expand flex-grow từ var(--ia-collapsed-w)
   ───────────────────────────────────────────────────────────────────────────── */

.twb-ia-track--horizontal {
    display: flex;
    flex-direction: row;
    gap: 8px;
    height: 480px;
    overflow: hidden;
}

.twb-ia--horizontal .twb-ia-panel {
    flex: 0 0 var(--ia-collapsed-w, 80px);
    height: 100%;
    cursor: pointer;
    transition: flex var(--ia-speed, 500ms) ease;
    will-change: flex;
}

.twb-ia--horizontal .twb-ia-panel.is-active {
    flex: 1 1 0%;
}

/* ─────────────────────────────────────────────────────────────────────────────
   TRACK — VERTICAL (TỐI ƯU CHỐNG GIỰT)
   Sử dụng flex-grow độc lập để transition mượt mà, không thay đổi flex-basis.
   ───────────────────────────────────────────────────────────────────────────── */
.twb-ia-track--vertical {
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow: hidden;
    /* Dự phòng an toàn 480px trong trường hợp Elementor cache chưa cập nhật */
    height: 480px; 
}

.twb-ia--vertical .twb-ia-panel {
    /* Tách rời hoàn toàn các thuộc tính để trình duyệt (GPU) không bị lỗi nhịp */
    flex-grow: 0;
    flex-shrink: 0;
    flex-basis: var(--ia-collapsed-h, 80px);
    width: 100%;
    cursor: pointer;
    overflow: hidden;
    /* Chỉ animate mỗi flex-grow, loại bỏ hoàn toàn hiện tượng giựt/nhảy khung */
    transition: flex-grow var(--ia-speed, 500ms) cubic-bezier(0.4, 0, 0.2, 1);
}

.twb-ia--vertical .twb-ia-panel.is-active {
    flex-grow: 1; /* Nở ra chiếm phần không gian trống */
}

/* ── Hiển thị Tiêu đề + Icon khi đóng ── */
.twb-ia--vertical .twb-ia-panel:not(.is-active) .twb-ia-content {
    opacity: 1;
    pointer-events: none;
    /* LƯU Ý: Tuyệt đối không dùng justify-content: center ở đây, 
       vì khi hover nó trả về flex-end sẽ làm chữ bị dịch chuyển (giựt) tức thì */
}

.twb-ia--vertical .twb-ia-panel:not(.is-active) .twb-ia-subtitle {
    display: none;
}

.twb-ia--vertical .twb-ia-panel:not(.is-active) .twb-ia-icon {
    margin-bottom: 4px;
    font-size: clamp(20px, 2vw, 28px);
}

/* ─────────────────────────────────────────────────────────────────────────────
   PANEL — CHUNG
   ───────────────────────────────────────────────────────────────────────────── */

.twb-ia-panel {
    position: relative;
    overflow: hidden;
    border-radius: 16px;
    background-color: #1a1a2e;
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    box-sizing: border-box;
    outline: none;
}

/* Focus visible — accessibility */
.twb-ia-panel:focus-visible {
    outline: 3px solid rgba(255, 255, 255, 0.8);
    outline-offset: 2px;
}

/* ─────────────────────────────────────────────────────────────────────────────
   OVERLAY — Solid & Gradient
   Dùng ::before pseudo để tách overlay ra, không ảnh hưởng content
   ───────────────────────────────────────────────────────────────────────────── */

.twb-ia-panel::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 1;
    pointer-events: none;
    transition: opacity var(--ia-speed, 500ms) ease;
}

/* Solid overlay */
.twb-ia-panel.twb-ia-overlay--solid::before {
    background-color: var(--ia-overlay-color, #1a1a2e);
    opacity: var(--ia-overlay-opacity, 0.5);
}

.twb-ia-panel.twb-ia-overlay--solid.is-active::before {
    opacity: var(--ia-overlay-opacity-active, 0.65);
}

/* Gradient overlay — bottom (content area) đậm hơn, top trong hơn */
.twb-ia-panel.twb-ia-overlay--gradient::before {
    background: linear-gradient(
        to top,
        color-mix(in srgb, var(--ia-overlay-color, #1a1a2e) calc(var(--ia-overlay-opacity, 0.5) * 100%), transparent) 40%,
        transparent 80%
    );
    opacity: 1;
}

/* Fallback cho browser không hỗ trợ color-mix */
@supports not (color: color-mix(in srgb, red, blue)) {
    .twb-ia-panel.twb-ia-overlay--gradient::before {
        background: linear-gradient(
            to top,
            rgba(26, 26, 46, 0.85) 0%,
            rgba(26, 26, 46, 0.4) 50%,
            transparent 80%
        );
        opacity: 1;
    }
}

.twb-ia-panel.twb-ia-overlay--gradient.is-active::before {
    background: linear-gradient(
        to top,
        color-mix(in srgb, var(--ia-overlay-color, #1a1a2e) calc(var(--ia-overlay-opacity-active, 0.65) * 100%), transparent) 50%,
        transparent 85%
    );
}

@supports not (color: color-mix(in srgb, red, blue)) {
    .twb-ia-panel.twb-ia-overlay--gradient.is-active::before {
        background: linear-gradient(
            to top,
            rgba(26, 26, 46, 0.9) 0%,
            rgba(26, 26, 46, 0.5) 55%,
            transparent 85%
        );
    }
}

/* ─────────────────────────────────────────────────────────────────────────────
   CONTENT — nằm phía trên overlay (z-index > 1)
   Horizontal: xoay dọc khi đóng (writing-mode), nằm dưới khi mở
   ───────────────────────────────────────────────────────────────────────────── */

.twb-ia-content {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding: 24px;
    box-sizing: border-box;
    pointer-events: none; /* Panel nhận click, không phải content */
}

/* Khi active: pointer-events on để CTA button clickable */
.twb-ia-panel.is-active .twb-ia-content {
    pointer-events: auto;
}

/* ── Icon ── */

.twb-ia-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 36px;
    margin-bottom: 12px;
    transition: opacity var(--ia-speed, 500ms) ease, transform var(--ia-speed, 500ms) ease;
}

/* Horizontal: icon luôn hiện (kể cả khi đóng) */
.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-icon {
    /* Giữ icon nhỏ lại khi đóng */
    font-size: clamp(16px, 1.5rem, 28px);
}

/* ── Title ── */

.twb-ia-title {
    color: #fff;
    margin: 0 0 6px;
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1.3;
    transform: translate3d(0, 0, 0) rotate(0deg);
    transform-origin: left bottom;
    transition:
        transform var(--ia-speed, 500ms) var(--ia-title-ease, ease),
        color calc(var(--ia-speed, 500ms) * 0.45) ease;
    will-change: transform;
}

/* Horizontal closed: giữ title sáng rõ, xoay 90° từ cùng một node để animation nhẹ và sạch. */
.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-content {
    justify-content: flex-end;
    align-items: flex-start;
}

.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-title {
    position: relative;
    top: var(--ia-closed-title-offset-y, 0px);
    left: var(--ia-closed-title-offset-x, 0px);
    width: max-content;
    max-width: none;
    margin: 0;
    white-space: nowrap;
    opacity: 1;
    transform: translate3d(0, 0, 0) rotate(-90deg);
    transform-origin: left bottom;
}

.twb-ia--horizontal .twb-ia-panel.is-active .twb-ia-title {
    position: relative;
    top: auto;
    left: auto;
    width: 100%;
    max-width: 100%;
    white-space: normal;
    opacity: 1;
    transform: translate3d(0, 0, 0) rotate(0deg);
}

/* ── Subtitle ── */

.twb-ia-subtitle {
    color: rgba(255,255,255,0.8);
    margin: 0;
    font-size: 0.9rem;
    line-height: 1.4;
    opacity: 1;
    transform: translate3d(0, 0, 0);
    transition:
        opacity calc(var(--ia-speed, 500ms) * 0.55) ease,
        transform calc(var(--ia-speed, 500ms) * 0.75) var(--ia-title-ease, ease);
}

.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-icon {
    display: none;
}

.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-subtitle {
    position: absolute;
    opacity: 0;
    visibility: hidden;
    transform: translate3d(0, 12px, 0);
    pointer-events: none;
}

.twb-ia--horizontal .twb-ia-panel.is-active .twb-ia-icon,
.twb-ia--horizontal .twb-ia-panel.is-active .twb-ia-subtitle {
    opacity: 1;
    visibility: visible;
    transform: translate3d(0, 0, 0);
}

/* ── Expand area (description + CTA) — ẩn khi đóng ── */

.twb-ia-expand-area {
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    transform: translate3d(0, 16px, 0);
    transition:
        max-height var(--ia-speed, 500ms) var(--ia-title-ease, ease),
        opacity calc(var(--ia-speed, 500ms) * 0.7) ease,
        transform calc(var(--ia-speed, 500ms) * 0.85) var(--ia-title-ease, ease);
    will-change: max-height, opacity, transform;
}

.twb-ia-panel.is-active .twb-ia-expand-area {
    max-height: 300px;
    opacity: 1;
    transform: translate3d(0, 0, 0);
}

/* ── Description ── */

.twb-ia-desc {
    color: rgba(255,255,255,0.85);
    margin: 10px 0 0;
    font-size: 0.9rem;
    line-height: 1.6;
}

/* ── CTA Button ── */

.twb-ia-cta {
    display: inline-block;
    margin-top: 16px;
    padding: 10px 20px;
    background-color: rgba(255,255,255,0.2);
    color: #fff;
    border: 1px solid rgba(255,255,255,0.5);
    border-radius: 6px;
    text-decoration: none;
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1;
    transition: background-color 200ms ease, color 200ms ease;
    pointer-events: auto;
}

.twb-ia-cta:hover {
    background-color: #fff;
    color: #1a1a2e;
    text-decoration: none;
}

/* Ẩn phần phụ khi panel đóng — pointer-events: none ở .twb-ia-content đã xử lý */
.twb-ia--vertical .twb-ia-panel:not(.is-active) .twb-ia-icon {
    font-size: 0.8rem;
}

.twb-ia--vertical .twb-ia-panel.is-active .twb-ia-content {
    opacity: 1;
    pointer-events: auto;
    transition: opacity var(--ia-speed, 500ms) ease;
}

/* ─────────────────────────────────────────────────────────────────────────────
   LAYOUT: REMOTE COUNTER
   ───────────────────────────────────────────────────────────────────────────── */

.twb-ia--remote {
    display: flex;
    flex-direction: row;
    align-items: stretch;
    gap: 40px;
}

/* Vị trí counter: trái = content trước ảnh */
.twb-ia--remote-pos-left {
    flex-direction: row;
}

.twb-ia--remote-pos-right {
    flex-direction: row-reverse;
}

/* ── Image area ── */

.twb-ia-remote-image {
    position: relative;
    flex: 1 1 55%;
    border-radius: 16px;
    overflow: hidden;
    background-color: #1a1a2e;
    /* BUG-9 FIX: min-height fallback — parent có thể chưa có height explicit
       (đặc biệt trong Elementor editor trước khi selector CSS được inject).
       Elementor remote_image_height control ghi đè height: khi user set. */
    min-height: 320px;
}

.twb-ia-remote-slide {
    position: absolute;
    inset: 0;
    background-size: cover;
    background-position: center center;
    opacity: 0;
    transition: opacity 600ms ease;
}

.twb-ia-remote-slide.is-active {
    opacity: 1;
}

/* ── Content area ── */

.twb-ia-remote-content {
    flex: 1 1 45%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 0;
    padding: 8px 0;
    box-sizing: border-box;
}

/* ── Counter ── */

.twb-ia-remote-counter {
    margin-bottom: 16px;
}

.twb-ia-counter-num {
    font-size: 2.5rem;
    font-weight: 700;
    color: #111827;
    line-height: 1;
}

.twb-ia-counter-current {
    /* Phần số active — có thể style riêng qua CSS nếu cần */
}

/* ── Remote panel text switching ── */

.twb-ia-remote-panel-text {
    display: none;
    flex-direction: column;
}

.twb-ia-remote-panel-text.is-active {
    display: flex;
}

.twb-ia-remote-content .twb-ia-title {
    color: #111827;
    font-size: 1.75rem;
    font-weight: 700;
    margin: 0 0 8px;
    line-height: 1.3;
}

.twb-ia-remote-content .twb-ia-subtitle {
    color: #6b7280;
    font-size: 0.95rem;
    margin: 0 0 4px;
}

.twb-ia-remote-content .twb-ia-desc {
    color: #6b7280;
    margin-top: 8px;
    font-size: 0.9rem;
    line-height: 1.6;
}

.twb-ia-remote-content .twb-ia-cta {
    background-color: rgba(26,79,216,0.1);
    color: #1a4fd8;
    border-color: rgba(26,79,216,0.3);
    align-self: flex-start;
    margin-top: 20px;
}

.twb-ia-remote-content .twb-ia-cta:hover {
    background-color: #1a4fd8;
    color: #fff;
}

/* ── Remote Navigation (Dots & Arrows) ── */

.twb-ia-remote-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 24px;
    gap: 16px;
    flex-wrap: wrap;
}

.twb-ia-remote-dots {
    display: flex;
    gap: 10px;
    align-items: center;
}

/* ── Remote Arrows ── */

.twb-ia-remote-arrows {
    display: flex;
    gap: 8px;
    align-items: center;
}

.twb-ia-remote-arrow {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background-color: #f3f4f6;
    cursor: pointer;
    transition: background-color 300ms ease;
    padding: 0;
    outline: none;
}

.twb-ia-remote-arrow:hover {
    background-color: #e5e7eb;
}

.twb-ia-remote-arrow svg {
    fill: #111827;
    transition: fill 300ms ease, transform 200ms ease;
}

.twb-ia-remote-arrow:active svg {
    transform: scale(0.85);
}

.twb-ia-remote-arrow:focus-visible {
    outline: 2px solid #1a4fd8;
    outline-offset: 2px;
}

.twb-ia-remote-dot {
    width: 32px;
    height: 4px;
    border-radius: 2px;
    background-color: #d1d5db;
    border: none;
    padding: 0;
    cursor: pointer;
    transition: background-color 300ms ease, width 300ms ease;
    outline: none;
}

.twb-ia-remote-dot:focus-visible {
    outline: 2px solid #1a4fd8;
    outline-offset: 2px;
}

.twb-ia-remote-dot.is-active {
    background-color: #1a4fd8;
    width: 48px;
}

/* ─────────────────────────────────────────────────────────────────────────────
   RESPONSIVE — Tablet/Mobile stack
   ───────────────────────────────────────────────────────────────────────────── */

@media (max-width: 1024px) {
    /* Tablet: horizontal accordion stack dọc khi bật Tablet Stack. */
    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-track--horizontal {
        flex-direction: column;
        height: auto !important;
        min-height: 0;
        overflow: visible;
    }

    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel {
        flex: 0 0 auto;
        width: 100%;
        height: var(--ia-collapsed-h, 80px);
        max-height: none;
        min-height: var(--ia-collapsed-h, 80px);
        transition: height var(--ia-speed, 500ms) ease;
        will-change: height;
    }

    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel.is-active {
        height: clamp(260px, var(--ia-active-h, 360px), 70vh);
    }

    /* Tablet/mobile stack: title nằm ngang để dễ đọc, tránh xoay trong panel thấp. */
    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-content {
        justify-content: center;
        align-items: stretch;
        padding-top: 16px;
        padding-bottom: 16px;
    }

    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-title {
        position: relative;
        top: auto;
        left: auto;
        width: 100%;
        max-width: 100%;
        margin: 0;
        transform: none;
        transform-origin: left center;
        white-space: normal;
        writing-mode: horizontal-tb;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-icon,
    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-subtitle {
        display: none;
    }

    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel.is-active .twb-ia-content {
        justify-content: flex-end;
        overflow: hidden;
    }

    .twb-ia--tablet-stack.twb-ia--horizontal .twb-ia-panel.is-active .twb-ia-title {
        position: relative;
        width: 100%;
        max-width: 100%;
        transform: none;
    }

    .twb-ia--remote {
        gap: 24px;
    }
}

@media (max-width: 767px) {
    /* Mobile: horizontal accordion stack dọc khi bật Mobile Stack. */
    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-track--horizontal {
        flex-direction: column;
        height: auto !important;
        min-height: 0;
        overflow: visible;
    }

    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel {
        flex: 0 0 auto;
        width: 100%;
        height: var(--ia-collapsed-h, 76px);
        max-height: none;
        min-height: var(--ia-collapsed-h, 76px);
        transition: height var(--ia-speed, 500ms) ease;
        will-change: height;
    }

    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel.is-active {
        height: clamp(220px, var(--ia-active-h, 300px), 68vh);
    }

    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-content {
        justify-content: center;
        align-items: stretch;
        padding-top: 14px;
        padding-bottom: 14px;
    }

    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-title {
        position: relative;
        top: auto;
        left: auto;
        width: 100%;
        max-width: 100%;
        margin: 0;
        transform: none;
        white-space: normal;
        writing-mode: horizontal-tb;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-icon,
    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel:not(.is-active) .twb-ia-subtitle {
        display: none;
    }

    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel.is-active .twb-ia-content {
        justify-content: flex-end;
        overflow: hidden;
        padding: 18px;
    }

    .twb-ia--mobile-stack.twb-ia--horizontal .twb-ia-panel.is-active .twb-ia-title {
        position: relative;
        width: 100%;
        max-width: 100%;
        transform: none;
    }

    .twb-ia--mobile-stack .twb-ia-desc {
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    /* Remote: stack dọc trên mobile. */
    .twb-ia--remote {
        flex-direction: column !important;
        gap: 18px;
    }

    .twb-ia-remote-image {
        flex: none;
        width: 100%;
        min-height: 220px;
    }

    .twb-ia-remote-content {
        flex: none;
        padding: 0;
    }

    .twb-ia-remote-nav {
        margin-top: 18px;
    }
}

/* ─────────────────────────────────────────────────────────────────────────────
   REDUCED MOTION — tắt animation cho user đã set prefer-reduced-motion
   ───────────────────────────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    .twb-ia-panel,
    .twb-ia-panel::before,
    .twb-ia-title,
    .twb-ia-icon,
    .twb-ia-subtitle,
    .twb-ia-expand-area,
    .twb-ia-remote-slide,
    .twb-ia-remote-dot {
        transition-duration: 0ms !important;
    }
}

/* ─────────────────────────────────────────────────────────────────────────────
   EDITOR MODE
   JS inject .twb-ia--editor để disable hover trigger trong Elementor editor
   ───────────────────────────────────────────────────────────────────────────── */

/* BUG-15 FIX: Thêm selector backup dùng body.elementor-edit-mode phòng khi JS
   timing fail (elementorFrontend chưa load trước DOMContentLoaded).
   Rule gốc dùng .twb-ia--editor (add bởi JS) vẫn giữ nguyên. */
body.elementor-edit-mode .twb-ia--trigger-hover .twb-ia-panel:hover:not(.is-active) {
    /* CSS-level backup: tắt hover effect trong editor dù JS chưa chạy */
    cursor: default;
}

