/*
 * Desktop mega-menu — namespace .dm-*
 * 4-column grid dropdown, white card with shadow.
 * Hover-triggered (CSS-only). JS only for touch + ESC.
 * Hidden < 992px.
 */

/* Hide on mobile */
@media (max-width: 991px) {
    .dm { display: none !important; }
}

/* ───── DESKTOP MEGA MENU (≥992px) ───── */
@media (min-width: 992px) {
    :root {
        --dm-font:        "Roboto Condensed", "Inter", sans-serif;
        --dm-font-2:      "Inter", "Helvetica Neue", Arial, sans-serif;
        --dm-color:       #0B2B3B;
        --dm-color-hover: #d90429;
        --dm-bg:          #fff;
        --dm-border:      #e5e7eb;
        --dm-shadow:      0 8px 28px rgba(0,0,0,.08);
        --dm-radius:      12px;
        --dm-l1-size:     15px;   /* sweet spot: đọc được nhưng vẫn fit 12 items */
        --dm-l2-size:     20px;
        --dm-l3-size:     16px;
        --dm-l3-line:     1.6rem;
    }

    .dm {
        display: block;
        background: var(--dm-bg);
        border-top: 1px solid var(--dm-border);
        border-bottom: 1px solid var(--dm-border);
    }
    .dm__inner {
        max-width: 1440px;
        margin: 0 auto;
        padding: 0 15px;
    }

    /* Top-level list — single row, no wrap.
       KHÔNG dùng overflow-x:auto vì sẽ clip dropdown absolute. */
    .dm__list {
        display: flex;
        flex-wrap: nowrap;
        list-style: none;
        margin: 0;
        padding: 0;
        /* overflow visible mặc định để dropdown thoát ra ngoài được */
    }
    .dm__list > li {
        position: relative;
        flex-shrink: 0;
    }
    .dm__list > li > a {
        display: flex;
        align-items: center;
        gap: 5px;
        padding: 12px 13px;
        font-family: var(--dm-font);
        font-size: var(--dm-l1-size);
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: .3px;
        color: var(--dm-color);
        text-decoration: none;
        white-space: nowrap;
        transition: color .15s;
    }
    .dm__list > li:hover > a,
    .dm__list > li.is-open > a,
    .dm__list > li.current-menu-item > a {
        color: var(--dm-color-hover);
    }

    /* ─── Chevron handling ───
       LEVEL 1 (top-level menu): SVG chevron — giữ lại
       LEVEL 2+ (inside mega): KHÔNG cần chevron — ẩn */

    /* LEVEL 1: SVG chevron (đè lên FontAwesome legacy của style.css:1400) */
    .dm__list > li.menu-item-has-children > a::after,
    .dm__list > li.menu-item-has-children > a:after {
        content: '' !important;
        display: inline-block !important;
        width: 11px !important;
        height: 11px !important;
        margin-left: 4px !important;
        background-color: currentColor;
        font: 0/0 a !important;            /* nuke FontAwesome font from legacy */
        -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>") center/contain no-repeat;
                mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>") center/contain no-repeat;
        opacity: .8;
        transition: transform .25s, opacity .25s;
    }
    .dm__list > li:hover.menu-item-has-children > a::after {
        transform: rotate(180deg);
        opacity: 1;
    }

    /* LEVEL 2+ (inside mega): bỏ chevron — labels của column không cần arrow */
    .dm__list > li > .sub-menu .menu-item-has-children > a::after,
    .dm__list > li > .sub-menu .menu-item-has-children > a:after {
        content: none !important;
        display: none !important;
        margin: 0 !important;
    }

    /* ─── Mega menu (level 2) — max 4 cột/hàng, wrap xuống hàng tiếp theo ─── */
    .dm__list > li > .sub-menu {
        position: absolute;
        top: 100%;
        left: 0;                              /* mặc định mở SANG PHẢI */
        z-index: 9999;
        width: max-content;                   /* fluid theo content */
        /* Cap ở 4 cột × 250 + 3 gap × 36 + padding 44 ≈ 1152px */
        max-width: min(calc(100vw - 32px), 1152px);
        margin: 0;
        padding: 18px 22px;
        list-style: none;
        background: var(--dm-bg);
        border: 1px solid var(--dm-border);
        border-radius: var(--dm-radius);
        box-shadow: var(--dm-shadow);
        font-family: var(--dm-font-2);
        /* Auto-fit grid: tự fill columns, max 4 vì max-width capped */
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
        gap: 20px 36px;
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transform: scale(.98);
        transform-origin: top left;
        transition: opacity .2s, visibility .2s, transform .2s;
    }
    .dm__list > li:hover > .sub-menu,
    .dm__list > li.is-open > .sub-menu,
    .dm__list > li > .sub-menu:hover {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
        transform: scale(1);
    }

    /* ─── Hover bridge: pseudo-element 14px transparent dưới menu item
           giúp chuột di chuyển từ menu xuống mega không bị mất hover. ─── */
    .dm__list > li.menu-item-has-children::after {
        content: '';
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        height: 14px;
        z-index: 9998;
        pointer-events: none;
    }
    .dm__list > li.menu-item-has-children:hover::after {
        pointer-events: auto;     /* Active khi hover để bắt chuột */
    }

    /* Edge case: items cuối hàng → flip sang phải */
    .dm__list > li.is-edge > .sub-menu {
        left: auto;
        right: 0;
        transform-origin: top right;
    }

    /* Level 2 — column heading */
    .dm__list > li > .sub-menu > li {
        list-style: none;
    }
    .dm__list > li > .sub-menu > li > a {
        display: block;
        padding: 0 0 8px 10px;
        margin-bottom: 8px;
        font-family: var(--dm-font);
        font-size: var(--dm-l2-size);
        font-weight: 600;
        color: #111827;
        text-decoration: none;
        text-transform: none;
        white-space: normal;
        border-left: 3px solid transparent;
        transition: color .25s, border-left-color .25s;
    }
    .dm__list > li > .sub-menu > li > a:hover {
        color: var(--dm-color-hover);
        border-left-color: var(--dm-color-hover);
    }

    /* Level 3 — items under each column */
    .dm__list > li > .sub-menu > li > .sub-menu {
        display: block;
        list-style: none;
        margin: 0;
        padding: 0 16px 0 14px;
        border-left: 1px dashed var(--dm-border);
        max-height: calc(var(--dm-l3-line) * 20);
        overflow-y: auto;
        overflow-x: hidden;
        min-width: 230px;
        max-width: 340px;          /* rộng hơn cho text dài 1 dòng */
        white-space: nowrap;       /* text 1 dòng */
        scrollbar-gutter: stable;
    }
    .dm__list > li > .sub-menu > li > .sub-menu li a {
        display: block;
        padding: 4px 8px;
        font-family: var(--dm-font-2);
        font-size: var(--dm-l3-size);
        font-weight: 400;
        color: #4b5563;
        line-height: var(--dm-l3-line);
        text-decoration: none;
        text-transform: none;
        border-radius: 4px;
        transition: color .2s, background .2s;
    }
    .dm__list > li > .sub-menu > li > .sub-menu li a:hover {
        color: var(--dm-color-hover);
        background: rgba(217,4,41,.05);
    }

    /* Custom scrollbar trong level-3 */
    .dm__list > li > .sub-menu > li > .sub-menu::-webkit-scrollbar {
        width: 6px;
    }
    .dm__list > li > .sub-menu > li > .sub-menu::-webkit-scrollbar-thumb {
        background: rgba(11,43,59,.25);
        border-radius: 6px;
    }
    .dm__list > li > .sub-menu > li > .sub-menu::-webkit-scrollbar-track {
        background: transparent;
    }
}
