@use "mixins/border-radius";
@use "mixins/hover";
@use "mixins/breakpoints";
@use "variables";

// Base class
//
// Kickstart any navigation component with a set of style resets. Works with
// `<nav>`s, `<ul>`s or `<ol>`s.

.nav {
  display: flex;
  flex-wrap: wrap;
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
}

.nav-link {
  display: block;
  padding: variables.$nav-link-padding-y variables.$nav-link-padding-x;
  text-decoration: if(variables.$link-decoration == none, null, none);

  // Disabled state lightens text
  &.disabled {
    color: variables.$nav-link-disabled-color;
    pointer-events: none;
    cursor: default;
  }
}

.nav-button {
  @include breakpoints.media-breakpoint-up(lg) {
    &:hover {
      /* stylelint-disable-next-line declaration-no-important */
      background: variables.$blue-50 !important;
    }

    &:focus,
    &.focus {
      border-color: variables.$blue-500;
    }
  }
}

//
// Tabs
//

.nav-tabs {
  border-bottom: variables.$nav-tabs-border-width solid variables.$nav-tabs-border-color;

  .nav-link {
    margin-bottom: -(variables.$nav-tabs-border-width);
    border: variables.$nav-tabs-border-width solid transparent;
    @include border-radius.border-top-radius(variables.$nav-tabs-border-radius);

    @include hover.hover-focus() {
      border-color: variables.$nav-tabs-link-hover-border-color;
    }

    &.disabled {
      color: variables.$nav-link-disabled-color;
      background-color: transparent;
      border-color: transparent;
    }
  }

  .nav-link.active,
  .nav-item.show .nav-link {
    color: variables.$nav-tabs-link-active-color;
    background-color: variables.$nav-tabs-link-active-bg;
    border-color: variables.$nav-tabs-link-active-border-color;
  }

  .dropdown-menu {
    // Make dropdown border overlap tab border
    margin-top: -(variables.$nav-tabs-border-width);
    // Remove the top rounded corners here since there is a hard edge above the menu
    @include border-radius.border-top-radius(0);
  }
}

//
// Pills
//

.nav-pills {
  .nav-link {
    @include border-radius.border-radius(variables.$nav-pills-border-radius);
  }

  .nav-link.active,
  .show > .nav-link {
    color: variables.$nav-pills-link-active-color;
    background-color: variables.$nav-pills-link-active-bg;
  }
}

//
// Justified variants
//

.nav-fill {
  > .nav-link,
  .nav-item {
    flex: 1 1 auto;
    text-align: center;
  }
}

.nav-justified {
  > .nav-link,
  .nav-item {
    flex-basis: 0;
    flex-grow: 1;
    text-align: center;
  }
}

// Tabbable tabs
//
// Hide tabbable panes to start, show them when `.active`

.tab-content {
  > .tab-pane {
    display: none;
  }
  > .active {
    display: block;
  }
}

//
// EnergySage Global Navigation
//

$desktop-min-width: 992px;
$mobile-max-width: $desktop-min-width - 1;
$nav-hover-delay: 300ms;

// Overlay that is displayed on navbar hovers on desktop, menu open on mobile
// pull overlay up to avoid it from getting covered by other DOM elements
.content-overlay {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  display: block;
  width: 100%;
  height: 0;
  pointer-events: none;
  visibility: hidden;
  background: rgba(64, 64, 64, 0.45);
  opacity: 0;
  // stylelint-disable-next-line property-disallowed-list
  transition: height 0s, opacity 0s, visibility 0s;

  &.show {
    height: 100%;
    pointer-events: auto;
    visibility: visible;
    opacity: 1;
    transition-delay: $nav-hover-delay;
  }
}

/* including footer styles in here so they get ripped by rip-the-nav */
.es-footer {
  .es-footer-logo {
    svg {
      width: 160px !important;
      height: 36px !important;
    }
  }
}

.social-icons-container {
    // Use CSS gap for proper spacing that handles line wrapping correctly
    // This automatically handles spacing between items and line endings: 24px or 1.5rem
    gap: 24px;

    // Ensure at least 2 icons per line when wrapping
    // Use negative margin technique to force last two items to wrap together
    a {
        // Force the last item to "pull" the second-to-last item with it
        &:last-child {
            // Increased negative margin to be more aggressive about pulling
            // Use larger value to account for icon + padding + gap: -56px or 3.5rem
            margin-left: -56px;
        }

        // Make room for the pulled last item
        &:nth-last-child(2) {
            // Corresponding positive margin to make space: 56px or 3.5rem
            margin-right: 56px;
        }
    }
}

.nav-es-container {
  .nav-es-global,
  .nav-es-sticky {
    // pull navbar up to avoid overlay from getting covered by other DOM elements
    z-index: 1000;
    // default should be size 14px according to figma
    font-size: variables.$font-size-75;

    // shadow for additional visual separation between nav and more plain pages
    @include breakpoints.media-breakpoint-up(lg) {
      filter: drop-shadow(0 1px 10px rgba(0, 0, 0, 0.1));
      box-shadow: none;
    }
  }

  .nav-es-global {
    // add a background so veil goes under the nav and nav stands out against page bg
    background-color: variables.$white;

    @include breakpoints.media-breakpoint-up(lg) {
      // shadow for additional visual separation between nav and more plain pages
      box-shadow: 0 4px 4px rgba(221, 221, 221, 0.25);
    }
  }

  .nav-search-bar-mobile {
    z-index: 1000;
    font-size: variables.$font-size-75;
    background-color: variables.$white;

    .input-wrapper {
      width: 100% !important;
    }

    .btn-primary {
      display: none !important;
    }

    .nav-search-close-mobile {
      @include breakpoints.media-breakpoint-up(lg) {
        position: absolute;
      }
    }
  }

  // override es-ds default
  .btn-link {
    color: variables.$body-color;
  }

  // All links and labels in the navigation should start at a font-weight-normal (weight 400)
  // TODO: This overrides styles for `eyebrow`
  a,
  label {
    font-weight: variables.$font-weight-normal;
  }

  .nav-es-logo-mobile svg,
  .nav-es-logo-desktop-sticky svg {
    width: auto !important;
    height: 28px !important;
  }

  .nav-es-logo-mobile svg {
    margin: 0 auto;
  }

  @include breakpoints.media-breakpoint-up(lg) {
    .nav-es-logo-desktop {
      svg {
        width: auto !important;
        height: 42px !important;
      }
    }
  }

  // Adjust default nav-link styling
  .nav-es-sticky,
  .navbar-light .navbar-nav {
    // stylelint-disable-next-line selector-max-class
    .nav-item.nav-link,
    .dropdown-item.nav-link {
      color: variables.$navbar-light-color;

      // stylelint-disable-next-line selector-max-class
      &:hover {
        font-weight: variables.$font-weight-bold;
        color: variables.$navbar-light-color;
        text-decoration: underline;
      }
    }
  }

  // Hide the default BS4 expansion indicator
  .dropdown-toggle::after {
    content: none;
  }

  // Hide elements only needed for the mobile navigation
  .dropdown-label,
  .menu-checkbox,
  .menu-header,
  .menu-toggle,
  .expand-icon {
    display: none;
  }

  // Override default blue background
  .dropdown-item:hover,
  .dropdown-item:active,
  .dropdown-item:focus {
    background: none;
  }

  .featured-article-image {
    // override local style attribute on image
    // stylelint-disable-next-line declaration-no-important
    height: auto !important;
  }

  // cta callout with image
  .dropdown-cta-right .cta-link:hover {
    .link-name {
      text-decoration: underline;
    }

    .link-subheading {
      // stylelint-disable-next-line declaration-no-important
      text-decoration: none !important;
    }
  }

  .account-icon,
  .search-icon {
    fill: variables.$blue-900;
  }

  .search-open {
    fill: variables.$blue-600 !important;
  }

  // On Desktop a hovers display the dropdowns
  @media only screen and (min-width: $desktop-min-width) {
    // sticky nav bar, initially positioned out of viewport
    .nav-es-sticky {
      top: 0;
      right: 0;
      left: 0;
      z-index: 1000;
      // stylelint-disable-next-line property-disallowed-list
      transition: variables.$transition-base;
      transform: translateY(-100%);
    }

    // when scrolled down, animate sticky nav bar into viewport
    &.scrolled {
      .nav-es-sticky {
        transform: translateY(0%);
      }
    }

    // Allow dropdown content to take up the full width of the page
    .dropdown-full-page {
      position: static;
    }

    // Full expand top level nav's topics section
    .dropdown-menu-full-page {
      top: 65%;
      width: 100%;
      padding-bottom: 0;
    }

    // line below topic header in desktop top-level flyout
    .topic-menu-header::after {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 64px;
      height: 1px;
      margin: 0; // override .dropdown-toggle::after style
      content: "";
      background-color: variables.$gray-100;
      border-style: none; // override .dropdown-toggle::after style
      // stylelint-disable-next-line property-disallowed-list
      border-radius: 1px;
    }

    // Show top headers down chevron icon
    .nav-item {
      .dropdown-toggle,
      .nav-link {
        cursor: pointer;
      }

      .dropdown-menu {
        // prepare menu for delayed hover showing
        display: block;
        height: 0;
        overflow: hidden;
        pointer-events: none;
        visibility: hidden;
        opacity: 0;
        // stylelint-disable-next-line property-disallowed-list
        transition: height 0s, opacity 0s, visibility 0s;
      }
    }

    .product-menu .nav-item:last-child .product-menu-header-link {
      /* stylelint-disable-next-line declaration-no-important */
      padding-right: 2em !important;
    }

    .product-menu-header-link {
      color: variables.$blue-800;
    }

    .product-menu-link {
      font-size: variables.$font-size-sm;
    }

    .product-menu-flyout {
      left: 50%;
      width: 310px; // approx width of four columns at 992px breakpoint / 960px max width
      box-shadow: 0 0 5px rgba(0, 0, 0, 0.15);
      transform: translateX(-50%);
    }
    // stylelint-disable-next-line selector-class-pattern
    .product-menu-flyout--large {
      left: 0;
      width: 775px; // approx width of ten columns at 992px breakpoint / 960px max width
      transform: none;
    }

    // ensure the last flyout on small desktop doesn't go off the screen
    .nav-item:last-child .product-menu-flyout {
      transform: translateX(-75%);
    }

    .account-menu {
      right: 50%;
      left: auto;
      transform: translateX(25%);
    }

    /* stylelint-disable-next-line selector-class-pattern */
    .account-menu.loggedIn {
      font-size: variables.$font-size-75;
    }

    // Show dropdown and hover colors on nav-item hover in desktop view
    // Show top headers up chevron icon on nav-item hover
    .nav-item:hover {
      .dropdown-menu {
        height: auto;
        pointer-events: auto;
        visibility: visible;
        opacity: 1;
        transition-delay: $nav-hover-delay;
      }

      // black bar at bottom of header link when product menu is up
      .product-menu-header-text::after {
        position: absolute;
        right: 0;
        bottom: -1px;
        left: 0;
        z-index: 1001;
        height: 2px;
        content: "";
        background: variables.$orange-800;
        border-style: none;
      }
    }

    // primary nav buttons desktop
    .nav-item.top-header {
      padding: 1em 0 2em;
      user-select: none;

      /* stylelint-disable-next-line selector-max-class */
      .top-header-inner > .nav-link {
        line-height: variables.$line-height-75;
        color: variables.$blue-900;
        text-decoration: none;
        background: variables.$gray-50;
        border: variables.$btn-border-width solid transparent;
        @include border-radius.border-radius(8px);
      }

      &:hover {
        /* stylelint-disable-next-line selector-max-class */
        .top-header-inner > .nav-link {
          background: variables.$blue-50;
        }
      }
    }

    .navbar-light .navbar-nav {
      // stylelint-disable-next-line selector-max-class
      .nav-item.nav-link {
        // stylelint-disable-next-line selector-max-class
        &:hover {
          font-weight: variables.$font-weight-normal;
        }
      }
    }

    // stylelint-disable-next-line selector-max-class
    .topic-group {
      .eyebrow {
        color: variables.$gray-900;
      }

      .nav-link:has(.eyebrow) {
        /* stylelint-disable-next-line declaration-no-important */
        color: variables.$blue-600 !important;

        // stylelint-disable-next-line selector-max-class
        &:hover {
          /* stylelint-disable-next-line declaration-no-important */
          color: variables.$blue-600 !important;
        }

        .eyebrow {
          color: variables.$blue-600;
        }
      }

      .dropdown-item {
        font-size: variables.$font-size-sm;
      }
    }

    // Position person icon and dropdown menu
    .icon-dropdown {
      position: relative;
      display: inline-block;
    }

    // Fix top header logged in/logged out state section with a fixed width
    .icon-dropdown .container {
      width: 225px;
    }

    // Fix width of the dropdown menu on person icon hover
    .icon-dropdown .dropdown-menu {
      width: 250px;
    }

    // style account menu name with truncation
    .icon-dropdown .dropdown-toggle .first-name {
      max-width: 180px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .dropdown-menu-primary {
      position: relative;
      display: flex;
      justify-content: space-between;
    }

    .dropdown-list {
      display: flex;
      flex-wrap: wrap;
      padding-left: 0;
      list-style: none;
    }

    .dropdown-item {
      // stylelint-disable-next-line declaration-no-important
      white-space: normal !important;
    }

    .dropdown-cta-bottom {
      align-items: center;
      height: 78px;
      color: variables.$white;
      background-color: variables.$gray-900;

      // stylelint-disable-next-line selector-class-pattern
      .dropdown-cta-bottom--inner {
        display: flex;
        justify-content: space-between;
        margin: 0 auto;
      }

      .btn-lg {
        width: 250px;
      }
    }

    // menu item with eyebrow heading
    .nav-link.topic-menu-header {
      .link-subheading {
        color: variables.$gray-900;
      }

      &:hover {
        // stylelint-disable-next-line declaration-no-important
        text-decoration: none !important;

        .link-name {
          text-decoration: underline;
        }

        .link-subheading {
          font-weight: variables.$font-weight-normal;
          color: variables.$gray-900;
        }
      }
    }

    // nav-link styling for desktop account menu
    .navbar-nav {
      /* stylelint-disable-next-line selector-class-pattern */
      .loggedIn {
        /* stylelint-disable-next-line selector-max-class */
        .dropdown-item.nav-link:hover {
          font-weight: variables.$font-weight-normal;
        }
      }
    }

    // override eyebrow bottom margin for desktop primary menu items
    .dropdown-menu-primary .eyebrow.link-name {
      /* stylelint-disable-next-line declaration-no-important */
      margin-bottom: 0 !important;
    }
  }

  @include breakpoints.media-breakpoint-up(xl) {
    .product-menu-flyout {
      width: 370px; // approx width of four columns at 1200px breakpoint / 1140px max width
    }
    // stylelint-disable-next-line selector-class-pattern
    .product-menu-flyout--large {
      width: 832px; // approx width of nine columns at 1200px breakpoint / 1140px max width
    }

    // ensure the last flyout on small desktop doesn't go off the screen
    .nav-item:last-child .product-menu-flyout {
      transform: translateX(-60%);
    }
  }

  @include breakpoints.media-breakpoint-up(xxl) {
    // reset the last child back to normal
    .nav-item:last-child .product-menu-flyout {
      transform: translateX(-50%);
    }
    // allow the large flyout to be slightly more centered on its trigger link
    // stylelint-disable-next-line selector-class-pattern
    .product-menu-flyout--large {
      transform: translateX(-25%);
    }
  }

  // A lot of custom styles for the mobile navigation menu
  @media only screen and (max-width: $mobile-max-width) {
    .nav-es-global {
      // Set collapsed navbar height
      height: 56px;
    }

    // Force navigation items to align to the top and avoid conflict with other components on the page
    // Ensure menus are visible above overlay
    .top-level-menu {
      z-index: 1000;
    }

    // Make primary menu scrollable
    // Add padding to account for iOS menus
    .navbar-nav {
      padding-bottom: 120px;
      overflow-y: scroll;
    }

    // Allow the dropdowns to overlap each other
    .dropdown {
      position: unset;
    }

    // Make menu scrollable, prevent it from making the page longer
    // Prevent horizontal scrolling on menu
    .dropdown-menu {
      // stylelint-disable-next-line declaration-no-important
      position: static !important;
      // stylelint-disable-next-line declaration-no-important
      display: flex !important;
      // stylelint-disable-next-line function-disallowed-list
      max-height: calc(100% - 64px);
      padding: 0;
      overflow-x: hidden;
      overflow-y: scroll;

      // Add padding below last menu element (not full page drop-downs) to account for iOS menus
      &:not(.dropdown-menu-full-page) li:last-child {
        margin-bottom: 120px;
      }
    }

    .dropdown-menu-primary {
      flex-direction: column;
      width: 100%;
      border-style: none;

      .nav-item:not(.nav-link) {
        margin: 0;
      }
    }

    .dropdown-list {
      padding: 0;
    }

    // Prevent horizontal scrolling on menu
    .dropdown-menu-full-page {
      // stylelint-disable-next-line declaration-no-important
      display: flex !important;
      border-style: none;
    }

    // Style the label as a proper tappable block target
    .dropdown-label {
      display: block;
      cursor: pointer;
    }

    // primary nav buttons mobile
    .nav-item.top-header {
      margin-bottom: variables.$navbar-padding-y;
      background: variables.$gray-50;
      @include border-radius.border-radius(3px);

      /* stylelint-disable-next-line selector-max-class */
      .top-header-inner > .nav-link {
        color: variables.$blue-900;

        /* stylelint-disable-next-line selector-max-class */
        &:hover {
          color: variables.$blue-900;
        }
      }
    }

    .nav-item.top-header .top-header-inner {
      /* stylelint-disable-next-line selector-max-class */
      > .dropdown-label {
        color: variables.$blue-900;

        /* stylelint-disable-next-line selector-max-class */
        &:hover {
          color: variables.$blue-900;
          text-decoration: underline;
        }
      }
    }

    .nav-item.dropdown > .dropdown-label:hover,
    .topic-group > .dropdown-label:hover {
      text-decoration: underline;
    }

    // mobile heading link style
    .topic-group .nav-item .nav-link {
      color: variables.$link-color;

      &:hover {
        color: variables.$link-color;
        text-decoration: underline;
      }
    }

    .topic-group .dropdown-item {
      font-weight: variables.$font-weight-bold;

      &:hover {
        font-weight: variables.$font-weight-bold;
      }
    }

    // allow longer items (e.g. news article titles) to wrap on mobile
    .dropdown-item {
      white-space: normal;
    }

    // Add padding below featured article to account for iOS menus
    .dropdown-cta-right {
      padding-bottom: 120px;
    }

    // Show chevrons on mobile
    .expand-icon {
      display: flex;
    }

    .product-menu .expand-icon,
    .topic-group .expand-icon {
      fill: variables.$gray-400;
    }

    .submenu .expand-icon,
    .product-menu-flyout .expand-icon,
    .topic-menu-back .expand-icon {
      fill: variables.$blue-900;
    }

    .product-menu-link {
      font-weight: variables.$font-weight-bold;
    }

    // Style rows in hamburger, add bottom border
    .nav-item {
      align-items: center;
      height: 64px;
      margin: 0 15px;
    }

    .nav-item-border-mobile {
      // stylelint-disable-next-line declaration-no-important
      border-bottom: 1px solid variables.$gray-100 !important;
    }

    // Override default margin to allow centered titles
    .dropdown-label.nav-link {
      margin-bottom: 0;
    }

    // Style the menu for mobile navigation bit but use translate to keep it off screen
    .menu {
      position: absolute;
      top: 0;
      left: 0;
      flex-direction: column;
      width: 100%;
      min-width: 300px;
      max-width: 540px;
      height: 100%;
      min-height: 100vh;
      max-height: 100%;
      background: #fff;
      // stylelint-disable-next-line property-disallowed-list
      transition: transform 0.3s;
      transform: translate3d(-100%, 0, 0);

      // Override BS4 defaults for consistency
      ul,
      li {
        display: block;
        width: 100%;
        border-style: none;
      }
      li,
      .nav-item {
        display: flex;
        justify-content: space-between;
        width: 100%;
      }
    }

    // Avoid top level nav's subtopics level menu from laying on its topic level menu
    .submenu {
      position: fixed;
      z-index: 2;

      ul {
        // stylelint-disable-next-line function-disallowed-list
        max-height: calc(100% - 64px);
        overflow-x: hidden;
        overflow-y: scroll;

        li:last-child {
          margin-bottom: 120px;
        }
      }
    }

    // The menu after any checked checkboxes is translated on screen
    .menu-checkbox:checked + .menu {
      transform: translate3d(0, 0, 0);
    }

    // The menu header
    .menu-header {
      display: flex;
      align-items: center;
      width: 100%;
      height: 64px;
      max-height: 64px;
      font-weight: bolder;
    }

    // This is a generic placeholder button for open/close/back in the mobile menu
    .menu-toggle {
      display: flex;
      vertical-align: center;
      cursor: pointer;
    }

    .product-menu-flyout-see-all {
      text-decoration: underline;
      letter-spacing: 0.08em;
    }

    // menu item with eyebrow heading
    .nav-item.nav-item-menu {
      height: 76px;
      margin-bottom: 0.5em;
      text-decoration: none;

      &:hover {
        .link-name {
          text-decoration: underline;
        }

        .link-subheading {
          font-weight: variables.$font-weight-normal;
          color: variables.$gray-900;
          // stylelint-disable-next-line declaration-no-important
          text-decoration: none !important;
        }
      }
    }
  }
}
