/* ------------------------------
   Base Header (Desktop) Styles
------------------------------- */

#header {
  position: fixed;
  z-index: 10000;
  top: 0;
  left: 0;
  width: 100%;
  background: rgba(255, 253, 253, 0.95);
  height: 3em;
  line-height: 3em;
  box-shadow: 0 0 0.15em rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  font-weight: 700;
  justify-content: space-between;
  padding: 0 1em;
}

#header picture {
  height: 100%;
  display: flex;
  align-items: center;
}

#header picture img {
  max-height: 80%;
  max-width: 150px;
  width: auto;
  height: auto;
  object-fit: contain;
}

/* Desktop Navigation */
.desktop-nav {
  height: 100%;
  line-height: 3em;
}

.desktop-nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.desktop-nav ul li {
  display: inline-block;
  margin-left: 0.5em;
  font-size: 0.9em;
}

.desktop-nav ul li a {
  display: block;
  color: #353434;
  text-decoration: none;
  height: 3em;
  line-height: 2.5em;
  transition: color 0.3s ease;
}

.desktop-nav ul li a:hover {
  color: #534dcf;
}

/* Desktop Dropdown - Improved Scrolling */
.desktop-nav .has-submenu {
  position: relative;
  padding-right:0;
}

.desktop-nav .sub-menu {
  position: absolute;
  top: 100%;
  left: 0;
  background: rgba(255, 253, 253, 0.98);
  padding: 0.5em 0;
  list-style: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 0.5em;
  border: 1px solid rgba(0, 0, 0, 0.05);
  display: none;
  min-width: 220px;
  z-index: 999;

  /* Improved scrolling properties */
  max-height: min(400px, calc(100vh - 4em));
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: #bbb #f0f0f0;
  overscroll-behavior: contain;

  /* Smooth scrolling */
  scroll-behavior: smooth;
}

.desktop-nav .has-submenu:hover>.sub-menu {
  display: block;
}

.desktop-nav .has-submenu.open .sub-menu {
  max-height: min(500px, calc(100vh - 4em));
}

/* Enhanced scrollbar styling for desktop */
.desktop-nav .sub-menu::-webkit-scrollbar,
.desktop-nav .nested-sub-menu::-webkit-scrollbar {
  width: 8px;
}

.desktop-nav .sub-menu::-webkit-scrollbar-track,
.desktop-nav .nested-sub-menu::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 10px;
}

.desktop-nav .sub-menu::-webkit-scrollbar-thumb,
.desktop-nav .nested-sub-menu::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 10px;
  border: 2px solid #f1f1f1;
}

.desktop-nav .sub-menu::-webkit-scrollbar-thumb:hover,
.desktop-nav .nested-sub-menu::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

.desktop-nav .sub-menu li {
  display: block;
  width: 100%;
}

.desktop-nav .sub-menu li a {
  display: block;
  padding: 0.6em 1em;
  white-space: nowrap;
  color: #353434;
  font-size: 0.9em;
  transition: background 0.3s, color 0.3s;
  border-radius: 0.25em;
  margin: 0 0.25em;
}

.desktop-nav .sub-menu li a:hover {
  background: rgba(83, 77, 207, 0.1);
  color: #534dcf;
}

/* Nested Submenu (Desktop) - Improved */
.desktop-nav .has-submenu>a {
  position: relative;
  padding-right: 0;
}

@media screen and (max-width: 1000px) {
  .desktop-nav .has-submenu>a {
    padding-right: 0;
  }
}

.desktop-nav .nested-sub-menu {
  display: none;
  padding: 0.25em 0 0.25em 1.5em;
  background: transparent;
  list-style: none;
  margin: 0.25em 0;
  position: static;

  /* Enhanced scrolling for nested menus */
  max-height: min(250px, calc(100vh - 6em));
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: #bbb #f0f0f0;
  overscroll-behavior: contain;
  scroll-behavior: smooth;

  border-left: 2px solid rgba(83, 77, 207, 0.2);
}

.desktop-nav .sub-menu li.has-submenu:hover>.nested-sub-menu {
  display: block;
}

.desktop-nav .submenu-arrow {
  position: absolute;
  right: 1em;
  top: 50%;
  transform: translateY(-50%) rotate(0deg);
  transition: transform 0.3s ease, color 0.3s ease;
  font-size: 0.7em;
  color: #666;
  pointer-events: none;
}

.desktop-nav .has-submenu:hover .submenu-arrow {
  color: #000;
  transform: translateY(-50%) rotate(90deg);
}

/* Hamburger (hidden on desktop) */
.hamburger {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5em;
  z-index: 10001;
}

.hamburger span {
  display: block;
  width: 25px;
  height: 3px;
  background: #353434;
  margin: 5px 0;
  transition: all 0.3s ease;
}

.mobile-nav {
  display: none;
}

/* Mobile Styles */
@media screen and (max-width: 840px) {
  #header {
    height: 3em;
    line-height: 3em;
  }

  #header picture img {
    max-height: 90%;
    max-width: 120px;
  }

  .hamburger {
    display: block;
  }

  .desktop-nav {
    display: none;
  }

  .mobile-nav {
    position: fixed;
    display: block;
    top: 3em;
    left: 50%;
    width: 300px;
    background: rgba(255, 253, 253, 0.98);
    border-radius: 1rem;
    padding: 1rem;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    transform: translate(-50%, -200%);
    transition: transform 0.3s ease, visibility 0.3s ease;
    z-index: 9999;
    visibility: hidden;
    max-height: calc(100vh - 4em);
    overflow-y: auto;
    scroll-behavior: smooth;
  }

  .mobile-nav.active {
    transform: translate(-50%, 0);
    visibility: visible;
  }

  .mobile-nav .has-submenu {
    position: relative;
  }

  .mobile-nav .main-menu {
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .mobile-nav .main-menu>li {
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    padding: 0.75rem 1rem;
    position: relative;
    opacity: 0;
  }

  .mobile-nav .main-menu>li:last-child {
    border-bottom: none;
  }

  .mobile-nav .main-menu>li a {
    text-decoration: none;
    color: #000;
    display: block;
    font-family: "Georgia", serif;
    font-size: 1.2rem;
  }

  .mobile-nav .submenu-arrow {
    position: absolute;
    right: 1rem;
    top: 1.2rem;
    transform: translateY(-50%);
    cursor: pointer;
    font-size: 1rem;
    color: #353434;
    transition: transform 0.3s ease;
  }

  .mobile-nav .has-submenu.open .submenu-arrow {
    transform: rotate(180deg);
    margin-top: -0.6rem;
  }

  .mobile-nav .sub-menu {
    list-style: none;
    margin: 0;
    padding: 0 0.5rem;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
  }

  .mobile-nav .has-submenu.open .sub-menu {
    max-height: 500px;
  }

  .mobile-nav .sub-menu li {
    padding: 0.5rem 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    word-break: break-word;
  }

  .mobile-nav .sub-menu li:last-child {
    border-bottom: none;
  }

  .mobile-nav .sub-menu li a {
    color: #333;
    white-space: nowrap;
    font-size: 0.9rem;
  }

  @keyframes slideDown {
    from {
      opacity: 0;
      transform: translateY(-10px);
    }

    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .mobile-nav.active .main-menu>li {
    animation: slideDown 0.3s forwards;
  }

  .mobile-nav.active .main-menu>li:nth-child(1) {
    animation-delay: 0.1s;
  }

  .mobile-nav.active .main-menu>li:nth-child(2) {
    animation-delay: 0.2s;
  }

  .mobile-nav.active .main-menu>li:nth-child(3) {
    animation-delay: 0.3s;
  }

  .mobile-nav.active .main-menu>li:nth-child(4) {
    animation-delay: 0.4s;
  }

  .mobile-nav.active .main-menu>li:nth-child(5) {
    animation-delay: 0.5s;
  }

  .mobile-nav.active .main-menu>li:nth-child(6) {
    animation-delay: 0.6s;
  }

  .mobile-nav .nested-sub-menu {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
    padding-left: 1rem;
  }

  .mobile-nav .has-submenu.open>.nested-sub-menu {
    max-height: 500px;
  }
}

@media screen and (min-width: 1200px) {
  #header picture img {
    max-height: 90%;
    max-width: 180px;
  }

  /* Larger screens get bigger submenus */
  .desktop-nav .sub-menu {
    min-width: 250px;
    max-height: min(500px, calc(100vh - 4em));
  }

  .desktop-nav .nested-sub-menu {
    max-height: min(300px, calc(100vh - 6em));
  }
}