/**
 * RSL Navbar Component
 * @version 2.1.0
 *
 * Multi-level dropdown navigation with mobile hamburger menu
 * Mega menu support for rich, multi-column layouts
 * Fully ADA-compliant with keyboard navigation and ARIA support
 * Fixed z-index to ensure navbar always appears above other content
 *
 * Dropdown Behavior:
 * - Default: Click-only mode (best for mobile and accessibility)
 * - Optional: Add data-hover-dropdowns="true" for hover on desktop (>768px)
 *
 * Mega Menus:
 * - Add data-menu-type="mega" to navbar item
 * - Use .rsl-navbar-mega-menu instead of .rsl-navbar-dropdown
 * - Supports arbitrary HTML content, images, buttons, and RSL grid layouts
 * - Automatically stacks columns on mobile
 * - Viewport-aware positioning: auto-adjusts to prevent horizontal scroll
 */

/* ============================================
   CSS VARIABLES
   ============================================ */

:root {
  /* Z-index layers - navbar always on top */
  --navbar-z-index: 10000;
  --navbar-dropdown-z-index: 10001;
  --navbar-mobile-menu-z-index: 10002;
  --navbar-overlay-z-index: 9999;

  /* Colors */
  --navbar-bg: #ffffff;
  --navbar-text: #333333;
  --navbar-hover-bg: #f5f5f5;
  --navbar-active-bg: #6E7BFF;
  --navbar-active-text: #ffffff;
  --navbar-border: #e0e0e0;
  --navbar-shadow: rgba(0, 0, 0, 0.1);

  /* Spacing */
  --navbar-height: 60px;
  --navbar-padding: 1rem;
  --navbar-item-padding: 0.75rem 1rem;
  --navbar-dropdown-width: 220px;

  /* Mobile */
  --navbar-mobile-breakpoint: 768px;
}

/* ============================================
   NAVBAR CONTAINER
   ============================================ */

.rsl-navbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: var(--navbar-bg);
  padding: 0 var(--navbar-padding);
  min-height: var(--navbar-height);
  box-shadow: 0 2px 4px var(--navbar-shadow);
  z-index: var(--navbar-z-index);
  width: 100%;
  max-width: 100vw;
  box-sizing: border-box;
}

/* Sticky positioning */
.rsl-navbar.sticky-top {
  position: sticky;
  top: 0;
  z-index: calc(var(--navbar-z-index) + 100);
}

.rsl-navbar.sticky-bottom {
  position: sticky;
  bottom: 0;
  z-index: calc(var(--navbar-z-index) + 100);
}

.rsl-navbar.fixed-top {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: calc(var(--navbar-z-index) + 100);
}

.rsl-navbar.fixed-bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: calc(var(--navbar-z-index) + 100);
}

/* ============================================
   NAVBAR BRAND / LOGO
   ============================================ */

.rsl-navbar-brand {
  display: flex;
  align-items: center;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--navbar-text);
  text-decoration: none;
  margin-right: auto;
}

/* Logo right layout */
.rsl-navbar.layout-logo-right .rsl-navbar-brand {
  margin-right: 0;
  margin-left: auto;
  order: 3;
}

.rsl-navbar.layout-logo-right .rsl-navbar-menu {
  margin-right: 0;
  order: 1;
}

.rsl-navbar.layout-logo-right .rsl-navbar-toggle {
  order: 2;
}

/* Logo center layout */
.rsl-navbar.layout-logo-center {
  justify-content: space-between;
}

.rsl-navbar.layout-logo-center .rsl-navbar-brand {
  margin: 0 2rem;
  order: 2;
}

.rsl-navbar.layout-logo-center .rsl-navbar-menu-left {
  margin-right: 0;
  order: 1;
}

.rsl-navbar.layout-logo-center .rsl-navbar-menu-right {
  margin-left: 0;
  margin-right: 0;
  order: 3;
}

.rsl-navbar.layout-logo-center .rsl-navbar-toggle {
  order: 4;
}

.rsl-navbar-brand img {
  height: 40px;
  width: auto;
  margin-right: 0.5rem;
}

.rsl-navbar-brand:hover {
  color: var(--navbar-active-bg);
}

/* ============================================
   NAVBAR MENU (Desktop)
   ============================================ */

.rsl-navbar-menu {
  display: flex;
  align-items: center;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 0.5rem;
  flex-wrap: wrap;
  max-width: 100%;
}

.rsl-navbar-item {
  position: relative;
  list-style: none;
}

.rsl-navbar-link {
  display: flex;
  align-items: center;
  padding: var(--navbar-item-padding);
  color: var(--navbar-text);
  text-decoration: none;
  white-space: nowrap;
  border-radius: 4px;
  transition: background-color 0.2s ease, color 0.2s ease;
  cursor: pointer;
  user-select: none;
}

.rsl-navbar-link:hover,
.rsl-navbar-link:focus {
  background-color: var(--navbar-hover-bg);
  outline: none;
}

.rsl-navbar-link.active {
  background-color: var(--navbar-active-bg);
  color: var(--navbar-active-text);
}

/* Dropdown indicator icon */
.rsl-navbar-link .dropdown-icon {
  margin-left: 0.35rem;
  font-size: 0.7rem;
  transition: transform 0.2s ease;
  flex-shrink: 0;
}

.rsl-navbar-item.open > .rsl-navbar-link .dropdown-icon {
  transform: rotate(180deg);
}

/* Ensure nested dropdown icons are visible */
.rsl-navbar-dropdown .rsl-navbar-link .dropdown-icon {
  margin-left: auto;
  font-size: 0.65rem;
}

/* ============================================
   DROPDOWN MENUS (Multi-level)
   ============================================ */

.rsl-navbar-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  min-width: var(--navbar-dropdown-width);
  background-color: var(--navbar-bg);
  border: 1px solid var(--navbar-border);
  border-radius: 4px;
  box-shadow: 0 4px 12px var(--navbar-shadow);
  list-style: none;
  margin: 0;
  padding: 0.5rem 0;
  z-index: var(--navbar-dropdown-z-index);
}

/* Show dropdown when parent is open */
.rsl-navbar-item.open > .rsl-navbar-dropdown {
  display: block;
}

.rsl-navbar-dropdown .rsl-navbar-item {
  position: relative;
}

.rsl-navbar-dropdown .rsl-navbar-link {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  border-radius: 0;
}

.rsl-navbar-dropdown .rsl-navbar-link:hover,
.rsl-navbar-dropdown .rsl-navbar-link:focus {
  background-color: var(--navbar-hover-bg);
}

/* Nested dropdown positioning (2nd level and beyond) */
.rsl-navbar-dropdown .rsl-navbar-dropdown {
  top: 0;
  left: 100%;
  margin: 0;
}

/* Viewport-aware positioning for regular dropdowns */
.rsl-navbar-dropdown.align-right {
  left: auto;
  right: 0;
}

/* Right-aligned nested dropdowns (when near right edge) */
.rsl-navbar-dropdown .rsl-navbar-item.align-right > .rsl-navbar-dropdown {
  left: auto;
  right: 100%;
  margin: 0 0.25rem 0 0;
}

/* ============================================
   MEGA MENUS
   ============================================ */

.rsl-navbar-mega-menu {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  min-width: 600px;
  max-width: 1200px;
  background-color: var(--navbar-bg);
  border: 1px solid var(--navbar-border);
  border-radius: 4px;
  box-shadow: 0 4px 12px var(--navbar-shadow);
  padding: 2rem;
  z-index: var(--navbar-dropdown-z-index);
  margin-top: 0;
}

/* Show mega menu when parent is open */
.rsl-navbar-item.open > .rsl-navbar-mega-menu {
  display: block;
}

/* Full-width mega menu variant */
.rsl-navbar-mega-menu.full-width {
  left: 0;
  right: 0;
  max-width: 100vw;
  border-radius: 0;
  border-left: none;
  border-right: none;
}

/* Viewport-aware positioning for mega menus */
.rsl-navbar-mega-menu.align-right {
  left: auto;
  right: 0;
}

.rsl-navbar-mega-menu.align-left {
  left: 0;
  right: auto;
}

/* Mega menu headings */
.rsl-navbar-mega-menu h3,
.rsl-navbar-mega-menu h4 {
  color: var(--navbar-text);
  margin-top: 0;
  margin-bottom: 1rem;
  font-size: 1rem;
  font-weight: 600;
}

/* Mega menu links */
.rsl-navbar-mega-menu a {
  display: block;
  padding: 0.5rem 0;
  color: var(--navbar-text);
  text-decoration: none;
  transition: color 0.2s ease;
}

.rsl-navbar-mega-menu a:hover,
.rsl-navbar-mega-menu a:focus {
  color: var(--navbar-active-bg);
  outline: none;
}

/* Mega menu sections */
.rsl-navbar-mega-menu .mega-section {
  padding: 0 1rem;
}

.rsl-navbar-mega-menu .mega-section:first-child {
  padding-left: 0;
}

.rsl-navbar-mega-menu .mega-section:last-child {
  padding-right: 0;
}

/* Mega menu with images/thumbnails */
.rsl-navbar-mega-menu img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin-bottom: 0.5rem;
}

/* Mega menu featured items */
.rsl-navbar-mega-menu .mega-featured {
  background: var(--navbar-hover-bg);
  padding: 1rem;
  border-radius: 4px;
  margin-bottom: 1rem;
}

/* Mega menu description text */
.rsl-navbar-mega-menu .mega-description {
  font-size: 0.875rem;
  color: var(--navbar-text);
  opacity: 0.8;
  margin-top: 0.25rem;
}

/* ============================================
   MOBILE HAMBURGER TOGGLE
   ============================================ */

.rsl-navbar-toggle {
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
  z-index: calc(var(--navbar-mobile-menu-z-index) + 1);
  position: relative;
}

.rsl-navbar-toggle span {
  display: block;
  width: 100%;
  height: 3px;
  background-color: var(--navbar-text);
  border-radius: 2px;
  transition: all 0.3s ease;
  transform-origin: center;
}

/* Hamburger animation - X shape */
.rsl-navbar-toggle.active span:nth-child(1) {
  position: absolute;
  top: 50%;
  transform: translateY(-50%) rotate(45deg);
}

.rsl-navbar-toggle.active span:nth-child(2) {
  opacity: 0;
}

.rsl-navbar-toggle.active span:nth-child(3) {
  position: absolute;
  top: 50%;
  transform: translateY(-50%) rotate(-45deg);
}

/* ============================================
   MOBILE STYLES
   ============================================ */

@media (max-width: 768px) {
  .rsl-navbar {
    flex-wrap: wrap;
  }

  /* Show hamburger toggle */
  .rsl-navbar-toggle {
    display: flex;
  }

  /* Hide desktop menu */
  .rsl-navbar-menu {
    display: none;
    flex-direction: column;
    align-items: stretch;
    width: 100%;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    max-height: calc(100vh - var(--navbar-height));
    background-color: var(--navbar-bg);
    padding: 1rem;
    overflow-y: auto;
    overflow-x: hidden;
    z-index: var(--navbar-mobile-menu-z-index);
    gap: 0;
    box-shadow: 0 4px 12px var(--navbar-shadow);
    box-sizing: border-box;
  }

  /* Show mobile menu when open */
  .rsl-navbar-menu.mobile-open {
    display: flex;
  }

  /* Mobile menu items */
  .rsl-navbar-item {
    width: 100%;
    border-bottom: 1px solid var(--navbar-border);
    box-sizing: border-box;
  }

  .rsl-navbar-item:last-child {
    border-bottom: none;
  }

  .rsl-navbar-link {
    width: 100%;
    padding: 1rem;
    border-radius: 0;
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Mobile dropdowns */
  .rsl-navbar-dropdown {
    position: static;
    width: 100%;
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding-left: 1rem;
    background-color: var(--navbar-hover-bg);
    box-sizing: border-box;
  }

  .rsl-navbar-dropdown .rsl-navbar-dropdown {
    padding-left: 2rem;
    margin: 0;
  }

  /* Fix dropdown link alignment on mobile */
  .rsl-navbar-dropdown .rsl-navbar-link {
    justify-content: flex-start;
    gap: 0.5rem;
  }

  /* Mobile mega menus */
  .rsl-navbar-mega-menu {
    position: static;
    width: 100%;
    min-width: auto;
    max-width: 100%;
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 1rem;
    background-color: var(--navbar-hover-bg);
    box-sizing: border-box;
  }

  .rsl-navbar-mega-menu.full-width {
    border: none;
  }

  /* Stack mega menu columns on mobile */
  .rsl-navbar-mega-menu .slot-layout {
    display: flex;
    flex-direction: column;
  }

  .rsl-navbar-mega-menu .slot-item {
    width: 100%;
    margin-bottom: 1.5rem;
  }

  .rsl-navbar-mega-menu .slot-item:last-child {
    margin-bottom: 0;
  }

  /* Adjust mega menu sections for mobile */
  .rsl-navbar-mega-menu .mega-section {
    padding: 0;
    margin-bottom: 1.5rem;
  }

  .rsl-navbar-mega-menu .mega-section:last-child {
    margin-bottom: 0;
  }

  /* Nested dropdown positioning reset for mobile */
  .rsl-navbar-dropdown .rsl-navbar-item.align-right > .rsl-navbar-dropdown {
    left: 0;
    right: auto;
    margin: 0;
  }

  /* Reset right-aligned items on mobile */
  .rsl-navbar-right {
    margin-left: 0;
  }
}

/* Mobile overlay */
.rsl-navbar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: var(--navbar-overlay-z-index);
}

.rsl-navbar-overlay.active {
  display: block;
}

/* ============================================
   ACCESSIBILITY ENHANCEMENTS
   ============================================ */

/* Focus visible styles */
.rsl-navbar-link:focus-visible {
  outline: 2px solid var(--navbar-active-bg);
  outline-offset: 2px;
}

.rsl-navbar-toggle:focus-visible {
  outline: 2px solid var(--navbar-active-bg);
  outline-offset: 4px;
}

/* Skip link */
.rsl-navbar-skip-link {
  position: absolute;
  left: -9999px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
  z-index: calc(var(--navbar-z-index) + 1);
}

.rsl-navbar-skip-link:focus {
  position: fixed;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  width: auto;
  height: auto;
  padding: 1rem 2rem;
  background-color: var(--navbar-active-bg);
  color: var(--navbar-active-text);
  text-decoration: none;
  border-radius: 0 0 4px 4px;
  box-shadow: 0 4px 12px var(--navbar-shadow);
}

/* Screen reader only text */
.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;
}

/* ============================================
   DENSITY MODE INTEGRATION
   ============================================ */

/* Respect density settings */
[data-rsl-density="compact"] .rsl-navbar {
  --navbar-padding: 0.75rem;
  --navbar-item-padding: 0.5rem 0.75rem;
  --navbar-height: 50px;
}

[data-rsl-density="spacious"] .rsl-navbar {
  --navbar-padding: 1.5rem;
  --navbar-item-padding: 1rem 1.5rem;
  --navbar-height: 70px;
}

/* ============================================
   THEME VARIANTS
   ============================================ */

/* Dark mode support (theme toggle integration) */
[data-theme="dark"] .rsl-navbar {
  --navbar-bg: #1a1a1a;
  --navbar-text: #ffffff;
  --navbar-hover-bg: #2a2a2a;
  --navbar-border: #333333;
  --navbar-shadow: rgba(0, 0, 0, 0.3);
}

/* Dark theme class */
.rsl-navbar.dark-theme {
  --navbar-bg: #1a1a1a;
  --navbar-text: #ffffff;
  --navbar-hover-bg: #2a2a2a;
  --navbar-border: #333333;
  --navbar-shadow: rgba(0, 0, 0, 0.3);
}

/* Light theme (default - already defined in :root) */

/* Primary theme */
.rsl-navbar.primary-theme {
  --navbar-bg: #6E7BFF;
  --navbar-text: #ffffff;
  --navbar-hover-bg: #5a67d8;
  --navbar-active-bg: #ffffff;
  --navbar-active-text: #6E7BFF;
  --navbar-border: #5a67d8;
}

/* ============================================
   UTILITY CLASSES
   ============================================ */

.rsl-navbar-right {
  margin-left: auto;
}

.rsl-navbar-center {
  margin: 0 auto;
}

/* Divider */
.rsl-navbar-divider {
  width: 1px;
  height: 24px;
  background-color: var(--navbar-border);
  margin: 0 0.5rem;
}

/* Badge on nav items */
.rsl-navbar-badge {
  display: inline-block;
  padding: 0.15rem 0.4rem;
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1;
  color: #fff;
  background-color: #dc3545;
  border-radius: 10px;
  margin-left: 0.5rem;
}
