/**
 * Nav Menu Enhancements - Ôn thi 360
 * Enhanced navigation system with improved UX, animations, and accessibility
 */

/* =================================
   RESPONSIVE NAVIGATION FIXES
   ================================= */

/* Responsive navigation spacing - Balanced spacing for readability */
@media (max-width: 1024px) {
    nav .space-x-6 > :not([hidden]) ~ :not([hidden]) {
        margin-left: 0.25rem !important; /* Compact spacing trên tablet - 4px */
    }

    nav[class*="flex"] {
        gap: 0.25rem !important; /* Compact gap trên tablet - 4px */
    }

    nav a,
    nav span[tabindex] {
        padding: 0.25rem 0.375rem !important; /* Compact padding trên tablet */
        font-size: 0.8125rem !important; /* 13px font trên tablet */
    }
}

@media (max-width: 768px) {
    nav .space-x-6 > :not([hidden]) ~ :not([hidden]),
    nav .space-x-4 > :not([hidden]) ~ :not([hidden]),
    nav .space-x-8 > :not([hidden]) ~ :not([hidden]) {
        margin-left: 0.1875rem !important; /* Tight spacing trên mobile - 3px */
    }

    nav[class*="flex"] {
        gap: 0.1875rem !important; /* Tight gap trên mobile - 3px */
    }

    /* Đảm bảo menu items không bị overflow */
    nav > div {
        overflow-x: auto;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }

    nav > div::-webkit-scrollbar {
        display: none;
    }

    /* Extra small screens - hide some menu items if needed */
    @media (max-width: 640px) {
        nav a,
        nav span[tabindex] {
            padding: 0.25rem 0.375rem !important; /* Reasonable padding - 4px 6px */
            font-size: 0.75rem !important; /* Readable font - 12px */
            line-height: 1.2;
        }

        /* Priority-based hiding - hide less important menu items */
        nav .menu-item-secondary {
            display: none;
        }

        /* Maintain readable spacing on small screens */
        nav .space-x-6 > :not([hidden]) ~ :not([hidden]),
        nav .space-x-4 > :not([hidden]) ~ :not([hidden]),
        nav .space-x-8 > :not([hidden]) ~ :not([hidden]) {
            margin-left: 0.125rem !important; /* 2px - minimal but visible */
        }
    }
}

/* =================================
   MOBILE MENU IMPROVEMENTS
   ================================= */

/* Mobile menu container with better backdrop */
#mobileMenu {
    transform: translateY(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
}

#mobileMenu.show {
    transform: translateY(0);
}

/* Add subtle backdrop blur */
#mobileMenu::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 118, 110, 0.95);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: -1;
}

/* Enhanced mobile menu items */
#mobileMenu a,
#mobileMenu details > summary {
    position: relative;
    transition: all 0.2s ease;
    border-radius: 8px;
    margin: 3px 0; /* Tăng margin giữa mobile items */
    padding: 14px 16px !important; /* Tăng padding cho mobile items */
    line-height: 1.5 !important; /* Tăng line-height cho mobile */
    display: block;
}

#mobileMenu a:hover,
#mobileMenu details > summary:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateX(4px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Active state for mobile menu items - Yellow highlight like desktop */
#mobileMenu a.active,
#mobileMenu a.selected,
#mobileMenu a.nav-item.active,
#mobileMenu .nav-item.active {
    background: linear-gradient(90deg, #fbbf24, #f59e0b) !important;
    color: #ffffff !important;
    border-left: 4px solid #f59e0b;
    border-radius: 0 12px 12px 0;
    font-weight: 600;
    box-shadow: 0 2px 8px rgba(251, 191, 36, 0.3);
    margin: 2px 0;
    transform: translateX(4px);
    transition: all 0.3s ease;
}

/* Mobile dropdown animations */
#mobileMenu details[open] > div {
    animation: slideDown 0.3s ease-out;
}

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

/* Mobile menu user section enhancements */
#mobileMenu .user-info-section {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
    border: 1px solid rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(20px);
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
}

#mobileMenu .user-info-section img {
    transition: transform 0.2s ease;
}

#mobileMenu .user-info-section:hover img {
    transform: scale(1.05);
}

/* =================================
   DESKTOP MENU IMPROVEMENTS
   ================================= */

/* Enhanced desktop navigation */
nav.desktop-nav {
    position: relative;
}

/* Fix navigation menu gap/spacing issues - Balanced spacing */
nav .space-x-6 > :not([hidden]) ~ :not([hidden]) {
    margin-left: 0.375rem !important; /* 6px - balanced spacing */
}

nav .space-x-4 > :not([hidden]) ~ :not([hidden]) {
    margin-left: 0.25rem !important; /* 4px - compact but readable */
}

nav .space-x-8 > :not([hidden]) ~ :not([hidden]) {
    margin-left: 0.5rem !important; /* 8px - slightly larger */
}

nav .space-x-2 > :not([hidden]) ~ :not([hidden]) {
    margin-left: 0.1875rem !important; /* 3px - minimal */
}

/* Override any excessive gap in flex navigation - Balanced spacing */
nav[class*="flex"] {
    gap: 0.375rem !important; /* 6px - balanced spacing */
}

nav[class*="flex"] > div {
    gap: 0.25rem !important; /* 4px cho inner items */
}

/* Fix menu item padding to prevent overflow and logo hiding */
nav a,
nav span[tabindex] {
    padding: 0.375rem 0.5rem !important; /* Balanced padding - 6px 8px */
    white-space: nowrap; /* Prevent text wrapping */
    font-size: 0.875rem; /* 14px - readable size */
    line-height: 1.25; /* Balanced line height */
}

/* Ensure navigation container doesn't overflow and preserves logo space */
nav {
    max-width: 100%;
    overflow: visible;
}

nav > div {
    max-width: 100%;
    flex-wrap: nowrap; /* Prevent wrapping */
}

/* Logo protection - ensure logo area gets priority */
.header-logo,
.logo,
[class*="logo"] {
    flex-shrink: 0 !important; /* Logo never shrinks */
    margin-right: 1rem !important; /* Minimum spacing after logo */
    min-width: auto !important; /* Allow natural logo width */
}

/* Navigation menu should be flexible to accommodate logo */
nav[class*="flex"] {
    flex: 1 1 0% !important; /* Allow nav to shrink if needed */
    min-width: 0 !important; /* Allow nav to shrink below content size */
}

/* Desktop menu item hover effects */
nav.desktop-nav > div > div > a,
nav.desktop-nav > div > div > div > span {
    position: relative;
    transition: all 0.2s ease;
}

nav.desktop-nav > div > div > a::after,
nav.desktop-nav > div > div > div > span::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 50%;
    width: 0;
    height: 2px;
    background: #fb923c;
    transition: all 0.3s ease;
    transform: translateX(-50%);
}

nav.desktop-nav > div > div > a:hover::after,
nav.desktop-nav > div > div > div:hover > span::after {
    width: 100%;
}

/* Enhanced dropdown menus - Hover to open, auto-close on mouse leave */
.dropdown-menu {
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1); /* Faster transition */
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    border: 1px solid rgba(0, 0, 0, 0.05);
    pointer-events: none;
}

/* Show dropdown on hover - immediate response */
.group:hover .dropdown-menu,
.group:focus-within .dropdown-menu,
.dropdown-menu.show {
    opacity: 1 !important;
    visibility: visible !important;
    transform: translateY(0) !important;
    pointer-events: auto !important;
    transition-delay: 0s; /* No delay for opening */
}

/* Hide dropdown when not hovering - immediate close */
.group:not(:hover):not(:focus-within) .dropdown-menu {
    opacity: 0 !important;
    visibility: hidden !important;
    transform: translateY(-10px) !important;
    pointer-events: none !important;
    transition-delay: 0s; /* No delay for closing */
}

/* Active/selected menu item styling - Yellow highlight like homepage */
/* Highlight cho link trực tiếp và span không có dropdown */
a.nav-item.active,
a.nav-item.selected,
a.nav-item[aria-current="page"],
span.nav-item.active,
span.nav-item.selected,
span.nav-item[aria-current="page"],
.nav-item.active:not(.group),
.nav-item.selected:not(.group),
.nav-item[aria-current="page"]:not(.group) {
    background: linear-gradient(90deg, #fbbf24, #f59e0b) !important;
    color: #ffffff !important;
    border-radius: 20px;
    padding: 8px 16px;
    font-weight: 600;
    box-shadow: 0 4px 12px rgba(251, 191, 36, 0.3);
    transform: translateY(-1px);
    transition: all 0.3s ease;
}

/* Đảm bảo chỉ dropdown containers có menu con mới không bị highlight */
.group:has(.dropdown-menu) > span:not(.nav-item.active),
.group:has(.user-dropdown) > span:not(.nav-item.active),
.group:has(.mega-menu) > span:not(.nav-item.active) {
    background: none !important;
    color: inherit !important;
    box-shadow: none !important;
    transform: none !important;
}

/* Fallback cho browsers không support :has() */
@supports not (selector(:has(*))) {
    .group.has-dropdown > span:not(.nav-item.active) {
        background: none !important;
        color: inherit !important;
        box-shadow: none !important;
        transform: none !important;
    }
}

a.nav-item.active:hover,
a.nav-item.selected:hover,
span.nav-item.active:hover,
span.nav-item.selected:hover,
.nav-item.active:not(.group):hover,
.nav-item.selected:not(.group):hover {
    background: linear-gradient(90deg, #f59e0b, #d97706) !important;
    box-shadow: 0 6px 16px rgba(251, 191, 36, 0.4);
    transform: translateY(-2px);
}

/* Enhanced dropdown items */
.dropdown-menu a {
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
    padding: 8px 16px !important; /* Tăng padding cho dropdown items */
    line-height: 1.6 !important; /* Tăng line-height để giãn dòng */
    margin-bottom: 2px; /* Khoảng cách giữa các items */
    display: block;
    font-size: 0.9rem;
}

.dropdown-menu a::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 0;
    background: rgba(251, 146, 60, 0.04); /* Rất nhẹ - chỉ gợi ý */
    transition: width 0.3s ease;
    z-index: -1;
}

.dropdown-menu a:hover::before {
    width: 100%;
}

.dropdown-menu a:hover {
    color: #e67e22; /* Màu chữ nhẹ hơn nữa */
    background: rgba(251, 146, 60, 0.03); /* Background rất nhẹ */
    transform: translateX(1px); /* Transform tối thiểu */
}

/* Global line-height improvements for all dropdown/menu items */
.dropdown-menu a,
.user-dropdown a,
.mega-menu a,
#mobileMenu a,
details > div a {
    line-height: 1.6 !important; /* Consistent line-height cho tất cả menu items */
    padding-top: 8px !important;
    padding-bottom: 8px !important;
}

/* =================================
   MEGA MENU STYLES - Click to activate
   ================================= */

.mega-menu {
    width: 680px; /* Giảm lại cho vừa phải */
    max-width: 95vw;
    padding: 24px;
    display: grid;
    grid-template-columns: 1.2fr 1fr; /* Cột 1 chiếm 55%, cột 2 chiếm 45% */
    gap: 32px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
}

/* Show mega menu on hover - immediate response */
.group:hover .mega-menu,
.group:focus-within .mega-menu,
.mega-menu.show {
    opacity: 1 !important;
    visibility: visible !important;
    transform: translateY(0) !important;
    pointer-events: auto !important;
    transition-delay: 0s; /* No delay for opening */
}

/* Hide mega menu when not hovering - immediate close */
.group:not(:hover):not(:focus-within) .mega-menu {
    opacity: 0 !important;
    visibility: hidden !important;
    transform: translateY(-10px) !important;
    pointer-events: none !important;
    transition-delay: 0s; /* No delay for closing */
}

.mega-menu-section h4 {
    font-size: 14px;
    font-weight: 600;
    color: #374151;
    margin-bottom: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 2px solid #f3f4f6;
    padding-bottom: 8px;
}

.mega-menu-item {
    display: flex;
    align-items: flex-start; /* Align top để text dài không bị cắt */
    gap: 12px;
    padding: 14px 16px !important; /* Tăng padding horizontal */
    border-radius: 8px;
    transition: all 0.2s ease;
    text-decoration: none;
    color: #374151;
    line-height: 1.5 !important;
    margin-bottom: 4px;
    min-height: auto; /* Cho phép height tự động */
    word-wrap: break-word; /* Wrap text nếu quá dài */
}

.mega-menu-item:hover {
    background-color: rgba(59, 130, 246, 0.025) !important; /* Ultra light blue highlight */
    color: #1e40af;
}

/* Text styling for mega menu items */
.mega-menu-item span,
.mega-menu-item .text {
    white-space: normal !important;
    line-height: 1.4 !important;
    word-break: keep-all;
    overflow-wrap: break-word;
    display: block;
    width: 100%;
}

.mega-menu-item .icon {
    width: 20px;
    height: 20px;
    background: linear-gradient(135deg, #0f766e, #14b8a6);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 12px;
    flex-shrink: 0;
}

.mega-menu-item .content {
    flex: 1;
}

.mega-menu-item .title {
    font-weight: 500;
    font-size: 15px;
    margin-bottom: 2px;
}

.mega-menu-item .description {
    font-size: 13px;
    color: #6b7280;
    line-height: 1.4;
}

/* =================================
   USER AVATAR DROPDOWN
   ================================= */

.user-dropdown {
    min-width: 280px;
    padding: 0;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
}

/* Show user dropdown on hover - immediate response */
.group:hover .user-dropdown,
.group:focus-within .user-dropdown,
.user-dropdown.show {
    opacity: 1 !important;
    visibility: visible !important;
    transform: translateY(0) !important;
    pointer-events: auto !important;
    transition-delay: 0s; /* No delay for opening */
}

/* Hide user dropdown when not hovering - immediate close */
.group:not(:hover):not(:focus-within) .user-dropdown {
    opacity: 0 !important;
    visibility: hidden !important;
    transform: translateY(-10px) !important;
    pointer-events: none !important;
    transition-delay: 0s; /* No delay for closing */
}

.user-dropdown-header {
    background: linear-gradient(135deg, #0f766e, #14b8a6);
    color: white;
    padding: 20px;
    text-align: center;
}

.user-dropdown-header img {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.3);
    margin-bottom: 12px;
}

.user-dropdown-header .name {
    font-weight: 600;
    font-size: 16px;
    margin-bottom: 4px;
}

.user-dropdown-header .role {
    font-size: 13px;
    opacity: 0.9;
}

.user-dropdown-menu {
    padding: 8px 0;
}

.user-dropdown-menu a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px !important;
    transition: all 0.2s ease;
    text-decoration: none;
    color: #374151;
    line-height: 1.6 !important; /* Tăng line-height cho user dropdown */
    margin-bottom: 2px; /* Khoảng cách giữa items */
}

.user-dropdown-menu a:hover {
    background: rgba(251, 146, 60, 0.025); /* Highlight rất nhẹ */
    color: #e67e22; /* Màu chữ nhẹ hơn */
}

.user-dropdown-menu a i {
    width: 20px;
    text-align: center;
    color: #6b7280;
    transition: color 0.2s ease;
}

.user-dropdown-menu a:hover i {
    color: #0f766e;
}

.user-dropdown-divider {
    height: 1px;
    background: #e5e7eb;
    margin: 8px 0;
}

/* =================================
   SEARCH INTEGRATION
   ================================= */

.nav-search {
    position: relative;
    max-width: 300px;
    margin: 0 20px;
}

.nav-search input {
    width: 100%;
    padding: 10px 16px 10px 44px;
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 25px;
    background: rgba(255, 255, 255, 0.1);
    color: white;
    font-size: 14px;
    transition: all 0.3s ease;
    backdrop-filter: blur(10px);
}

.nav-search input::placeholder {
    color: rgba(255, 255, 255, 0.7);
}

.nav-search input:focus {
    outline: none;
    border-color: #fb923c;
    background: rgba(255, 255, 255, 0.15);
    box-shadow: 0 0 0 3px rgba(251, 146, 60, 0.1);
}

.nav-search .search-icon {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: rgba(255, 255, 255, 0.7);
    pointer-events: none;
}

.nav-search-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    border-radius: 12px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
    margin-top: 8px;
    padding: 8px 0;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.3s ease;
}

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

.nav-search-result {
    padding: 12px 16px;
    border-bottom: 1px solid #f3f4f6;
    transition: background 0.2s ease;
    cursor: pointer;
}

.nav-search-result:hover {
    background: #f9fafb;
}

.nav-search-result:last-child {
    border-bottom: none;
}

.nav-search-result .title {
    font-weight: 500;
    color: #374151;
    margin-bottom: 2px;
}

.nav-search-result .type {
    font-size: 12px;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* =================================
   ACTIVE STATES & INDICATORS
   ================================= */

.nav-item-active {
    position: relative;
}

.nav-item-active::before {
    content: '';
    position: absolute;
    top: 50%;
    left: -8px;
    transform: translateY(-50%);
    width: 4px;
    height: 20px;
    background: #fb923c;
    border-radius: 2px;
}

/* Breadcrumb indicators */
.nav-breadcrumb {
    position: absolute;
    bottom: -4px;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, #fb923c, #f97316);
    border-radius: 1px;
    transform: scaleX(0);
    transition: transform 0.3s ease;
}

.nav-item-active .nav-breadcrumb {
    transform: scaleX(1);
}

/* =================================
   LOADING STATES
   ================================= */

.nav-loading {
    position: relative;
    pointer-events: none;
    opacity: 0.6;
}

.nav-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    right: 12px;
    transform: translateY(-50%);
    width: 12px;
    height: 12px;
    border: 2px solid transparent;
    border-top: 2px solid #fb923c;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: translateY(-50%) rotate(0deg); }
    100% { transform: translateY(-50%) rotate(360deg); }
}

/* =================================
   ACCESSIBILITY IMPROVEMENTS
   ================================= */

/* Focus styles */
nav a:focus,
nav button:focus,
nav summary:focus {
    outline: 2px solid #fb923c;
    outline-offset: 2px;
    border-radius: 4px;
}

/* Screen reader only content */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .dropdown-menu {
        border: 2px solid;
    }

    .mega-menu-item:hover {
        border: 2px solid;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* =================================
   RESPONSIVE BREAKPOINTS
   ================================= */

@media (max-width: 768px) {
    .mega-menu {
        grid-template-columns: 1fr;
        width: calc(100vw - 32px);
        max-height: 70vh;
        overflow-y: auto;
    }

    .nav-search {
        margin: 12px 16px;
        max-width: none;
    }
}

@media (max-width: 480px) {
    .user-dropdown {
        min-width: calc(100vw - 32px);
    }

    .dropdown-menu {
        width: calc(100vw - 32px) !important;
        left: 16px !important;
        right: 16px !important;
        transform: translateX(0) !important;
    }
}

/* =================================
   DARK MODE SUPPORT (if needed)
   ================================= */

@media (prefers-color-scheme: dark) {
    .dropdown-menu {
        background: #1f2937;
        border-color: #374151;
    }

    .dropdown-menu a {
        color: #f9fafb;
    }

    .dropdown-menu a:hover {
        background: #374151;
    }

    .mega-menu-section h4 {
        color: #f9fafb;
        border-color: #374151;
    }

    .mega-menu-item {
        color: #f9fafb;
    }

    .mega-menu-item:hover {
        background: #374151;
    }

    .nav-search-results {
        background: #1f2937;
        border-color: #374151;
    }

    .nav-search-result .title {
        color: #f9fafb;
    }
}

/* Mouse leave event to clean up .show classes
   NOTE: JavaScript does not belong in CSS files — move the following code to a .js file (e.g., assets/frontend/distO360/js/nav-menu.js)

   Example JS to place in nav-menu.js:
   // const dropdown = document.querySelector('.dropdown');
   // const menu = dropdown.querySelector('.dropdown-menu');
   // dropdown.addEventListener('mouseleave', () => {
   //     menu.classList.remove('show');
   //     dropdown.setAttribute('aria-expanded', 'false');
   // });

*/
