/**
 * Module: Header & Navigation
 * STATUS: FIXED
 * - Submenus: Fixed giant icons. Now neat rows (Icon + Text).
 * - Desktop: Professional Dropdowns.
 * - Mobile: Clean list (Icons hidden for simplicity).
 */

/* --- 1. Structural Base --- */
header#masthead { 
    position: sticky; 
    top: 0; 
    width: 100%; 
    z-index: 9999;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
    background-color: var(--medical-header-bg);
}

/* --- 2. Top Bar --- */
.header-top-bar {
    height: var(--iatrics-top-bar-height);
    background-color: var(--medical-accent);
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    
    padding-left: max(var(--iatrics-safe-padding), calc((100% - var(--iatrics-max-width)) / 2 + var(--iatrics-safe-padding)));
    padding-right: max(var(--iatrics-safe-padding), calc((100% - var(--iatrics-max-width)) / 2 + var(--iatrics-safe-padding)));
    
    font-size: 0.85rem;
    font-weight: 500;
}

.top-contact {
    display: flex;
    align-items: center;
    gap: 25px;
    flex-wrap: wrap;
}

.header-top-bar a {
    color: #ffffff;
    text-decoration: none !important;
    border-bottom: none !important;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: opacity 0.2s ease;
}

.header-top-bar a:hover { opacity: 0.8; }
.header-top-bar .dashicons { font-size: 1.1em; width: auto; height: auto; text-decoration: none; }

/* --- 3. Main Header --- */
.header-main {
    height: var(--iatrics-main-header-height);
    background-color: #ffffff;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    justify-content: center; 
    width: 100%;
}

.header-inner {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    max-width: var(--iatrics-max-width); 
    padding-left: var(--iatrics-safe-padding);
    padding-right: var(--iatrics-safe-padding);
    box-sizing: border-box;
}

.site-branding {
    flex-shrink: 0;
    margin-right: 20px;
    display: flex;
    align-items: center;
}

.site-branding .custom-logo {
    max-height: 50px;
    width: auto;
    display: block;
}

/* --- 4. Desktop Navigation (1200px+) --- */
@media (min-width: 1200px) {
    .menu-toggle { display: none !important; }

    .header-right-actions {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        flex: 1;
    }

    .nav-menu {
        display: flex;
        gap: 32px;
        list-style: none;
        margin: 0;
        padding: 0;
        align-items: center;
    }

    /* Top-level links */
    .nav-menu > li > a {
        color: var(--medical-heading);
        text-decoration: none;
        font-weight: 700;
        font-size: var(--font-size-nav);
        text-transform: uppercase;
        letter-spacing: 0.06em;
        padding: 10px 0;
        position: relative;
        transition: color 0.25s ease;
        white-space: nowrap;
        display: flex;
        align-items: center;
        gap: 5px;
    }

    .nav-menu > li > a:hover { color: var(--medical-accent); }

    /* Chevron indicator on parent items */
    .nav-menu > .menu-item-has-children > a::after {
        content: '';
        display: inline-block;
        width: 6px;
        height: 6px;
        border-right: 2px solid currentColor;
        border-bottom: 2px solid currentColor;
        transform: rotate(45deg) translateY(-2px);
        transition: transform 0.25s ease, opacity 0.25s ease;
        opacity: 0.55;
        flex-shrink: 0;
    }

    .nav-menu > .menu-item-has-children:hover > a::after {
        transform: rotate(225deg) translateY(-2px);
        opacity: 1;
        color: var(--medical-accent);
    }

    /* Active underline on top-level */
    .nav-menu > li > a::before {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 0;
        height: 2px;
        background: var(--medical-accent);
        transition: width 0.25s ease;
    }
    .nav-menu > li:hover > a::before,
    .nav-menu > li.current-menu-item > a::before,
    .nav-menu > li.current-menu-ancestor > a::before {
        width: 100%;
    }
    /* Don't show underline on items with the chevron (::after already there) */
    .nav-menu > .menu-item-has-children > a::before { display: none; }

    /* ======================================
       MEGA MENU PANELS
       ====================================== */
    .nav-menu .menu-item-has-children { position: relative; }

    /* Hide standard sub-menu by default */
    .nav-menu .sub-menu {
        display: none;
        list-style: none;
        margin: 0;
        padding: 0;
    }

    /* Mega panel — all dropdowns in this theme have children (About, Procedures, Resources) */
    .mega-menu-item > .sub-menu {
        position: absolute;
        top: calc(100% + 14px);
        left: 50%;
        transform: translateX(-50%);
        width: max-content;
        min-width: 400px;
        background: #ffffff;
        border-top: 3px solid var(--medical-accent);
        border-radius: 0 0 14px 14px;
        box-shadow: 0 32px 72px rgba(0, 0, 0, 0.10), 0 6px 18px rgba(0, 0, 0, 0.05);
        padding: 0;
        z-index: 10000;
        /* Flex: items side by side */
        display: none;
        flex-direction: row;
        flex-wrap: nowrap;
        overflow: hidden;
    }

    /* Small caret pointing up to the nav item */
    .mega-menu-item > .sub-menu::before {
        content: '';
        position: absolute;
        top: -7px;
        left: 50%;
        transform: translateX(-50%) rotate(45deg);
        width: 12px;
        height: 12px;
        background: #ffffff;
        border-top: 3px solid var(--medical-accent);
        border-left: 3px solid var(--medical-accent);
    }

    /* Invisible bridge covering the gap between the trigger and panel.
       Keeps the hover state alive as the mouse travels downward. */
    .mega-menu-item::after {
        content: '';
        position: absolute;
        top: 100%;
        left: -30px;
        right: -30px;
        height: 18px; /* slightly larger than the 14px gap */
        z-index: 10001;
        display: none;
    }

    .mega-menu-item:hover::after,
    .mega-menu-item.is-hover::after {
        display: block;
    }

    .mega-menu-item:hover > .sub-menu,
    .mega-menu-item.is-hover > .sub-menu {
        display: flex;
        animation: fadeUp 0.22s ease-out;
    }

    /* ---- Cells (each menu item) ---- */
    .mega-menu-item > .sub-menu > li {
        flex: 1;
        min-width: 148px;
        display: flex;
        border-bottom: none;
        width: auto;
    }

    /* Vertical divider between cells */
    .mega-menu-item > .sub-menu > li + li {
        border-left: 1px solid #f0f4f8;
    }

    /* ---- Cell link: icon on top, title below ---- */
    .mega-menu-item > .sub-menu > li > a {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        text-align: center;
        padding: 32px 28px 28px;
        gap: 16px;
        border-radius: 0;
        border-bottom: none;
        text-transform: none;
        font-size: var(--font-size-nav);
        font-weight: 600;
        letter-spacing: 0.015em;
        color: var(--medical-heading);
        line-height: 1.4;
        white-space: normal;
        width: 100%;
        box-sizing: border-box;
        transition: background-color 0.22s ease, color 0.22s ease;
        position: relative;
    }

    /* Red accent bottom bar on hover */
    .mega-menu-item > .sub-menu > li > a::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 24px;
        right: 24px;
        height: 2px;
        background: var(--medical-accent);
        transform: scaleX(0);
        transition: transform 0.22s ease;
        transform-origin: center;
    }

    .mega-menu-item > .sub-menu > li > a:hover {
        background-color: #f8fafc;
        color: var(--medical-accent);
    }

    .mega-menu-item > .sub-menu > li > a:hover::after {
        transform: scaleX(1);
    }

    /* ---- Icons ---- */
    .mega-menu-item > .sub-menu img.menu-item-icon {
        width: 54px !important;
        height: 54px !important;
        object-fit: contain;
        flex-shrink: 0;
        display: block;
        transition: transform 0.22s ease, opacity 0.22s ease;
        padding: 8px;
        background: rgba(143, 0, 26, 0.05);
        border-radius: 50%;
    }

    .mega-menu-item > .sub-menu > li > a:hover img.menu-item-icon {
        transform: translateY(-3px);
        background: rgba(143, 0, 26, 0.09);
    }

    /* ---- Title text ---- */
    .mega-menu-item > .sub-menu .menu-item-title {
        display: block;
        font-size: var(--font-size-nav);
        font-weight: 700;
        color: inherit;
        line-height: 1.35;
    }

    /* ======================================
       CTA BUTTON — Contact & Referrals
       ====================================== */
    .header-cta-wrapper { margin-left: 32px; }

    .header-cta-btn {
        display: inline-flex;
        align-items: center;
        gap: 7px;
        color: var(--medical-accent);
        text-decoration: none;
        font-weight: 700;
        font-size: var(--font-size-nav);
        text-transform: uppercase;
        letter-spacing: 0.07em;
        padding: 8px 0;
        position: relative;
        transition: color 0.25s ease, gap 0.25s ease;
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Animated underline */
    .header-cta-btn::after {
        content: '';
        position: absolute;
        bottom: 2px;
        left: 0;
        width: 100%;
        height: 1.5px;
        background: var(--medical-accent);
        transform: scaleX(0);
        transform-origin: left center;
        transition: transform 0.25s ease;
    }

    .header-cta-btn:hover {
        color: var(--medical-accent-hover);
        gap: 11px;
    }

    .header-cta-btn:hover::after {
        transform: scaleX(1);
    }

    .header-cta-btn svg {
        flex-shrink: 0;
        transition: transform 0.25s ease;
    }

    .header-cta-btn:hover svg {
        transform: translateX(3px);
    }
}

/* --- 5. Mobile & Tablet Logic (Under 1200px) --- */
@media (max-width: 1199px) {
    
    .header-top-bar { justify-content: center; }
    .top-contact { gap: 15px; }

    .menu-toggle {
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 6px;
        background: none;
        border: none;
        cursor: pointer;
        padding: 10px;
        z-index: 10001;
    }

    .menu-toggle span {
        display: block;
        width: 30px;
        height: 3px;
        background: var(--medical-heading);
        border-radius: 3px;
        transition: 0.3s;
    }

    .nav-menu, .header-cta-wrapper { display: none; }

    .main-navigation.toggled .nav-menu {
        display: flex;
        flex-direction: column;
        position: absolute;
        top: 100%;
        left: 0;
        width: 100%;
        background: #ffffff;
        padding: 0;
        box-shadow: 0 10px 40px rgba(0,0,0,0.15);
        border-top: 1px solid #e2e8f0;
        max-height: 85vh;
        overflow-y: auto;
    }

    .main-navigation.toggled .nav-menu li {
        list-style: none !important;
        margin: 0 !important;
    }

    .main-navigation.toggled .nav-menu > li > a {
        display: block;
        padding: 15px 25px;
        font-size: 1rem;
        font-weight: 700;
        color: var(--medical-heading);
        border-bottom: 1px solid #f1f5f9;
        text-transform: uppercase;
        text-decoration: none;
    }

    .main-navigation.toggled .sub-menu {
        background-color: #fcfcfc;
        display: block;
    }

    .main-navigation.toggled .sub-menu a {
        display: block;
        padding: 12px 25px 12px 40px;
        font-size: 0.9rem;
        color: var(--medical-body-text);
        border-bottom: 1px solid #f8fafc;
        font-weight: 500;
        text-decoration: none;
    }

    /* Hide Images in Mobile Menu for Cleanliness */
    .main-navigation.toggled .nav-menu img,
    .main-navigation.toggled .nav-menu .menu-item-icon {
        display: none !important;
    }
    
    .main-navigation.toggled .nav-menu::after {
        content: 'Contact & Referrals →';
        display: block;
        text-align: center;
        background-color: var(--medical-accent);
        color: #ffffff;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        font-size: 0.85rem;
        padding: 16px 25px;
        margin: 20px 25px 30px 25px;
        border-radius: 6px;
        cursor: pointer;
    }
}

@keyframes fadeUp {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}