.dropup,
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: @zindex-dropdown;
  display: none;
  float: left;
  margin: 0;
  padding: 0;
  min-width: 160px;
  border-color: transparent;
  border-radius: @component-border-radius-base;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  background-color: var(--c8y-dropdown-background-default, @component-background-default);
  background-clip: padding-box;
  list-style: none;
  text-align: left;
  text-transform: none;
  font-size: @font-size-base;

  .boxShadowHelper(md);

  &--date-range {
    min-width: 240px;
    c8y-wrapper-form-field > .form-group {
      margin-bottom: 8px;
    }
  }

  &--modal {
    z-index: @zindex-modal + 1 !important;

    .p-l-24 {
      padding-left: 16px !important;
    }

    .p-r-24 {
      padding-right: 16px !important;
    }

    .m-l-24 {
      margin-left: 16px !important;
    }

    .m-r-24 {
      margin-right: 16px !important;
    }

    .m-l-16 {
      margin-left: 0 !important;
    }

    .m-r-16 {
      margin-right: 0 !important;
    }
  }

  &.pull-right {
    right: 0;
    left: auto;
  }

  .divider {
    overflow: hidden;
    margin: 0 !important;
    height: 2px;
    background: @component-border-color;
  }

  // Links within the dropdown menu
  > li,
  > .d-contents > li {
    a:not(.btn):not(.c8y-asset-selector__btn):not(.btn-help),
    button:not(.btn):not(.c8y-asset-selector__btn):not(.btn-help) {
      &:not(.btn-add-block):not(.btn-clean):not(.collapse-btn.btn-dot) {
        display: flex;
        align-items: center;
        gap: 4px;
        padding: 9px 15px 10px;
        width: 100%;
        border: 0;
        border-top: 1px solid @component-border-color;
        border-radius: 0;
        background-color: var(--c8y-dropdown-background-default, @component-background-default);
        background-image: none;
        color: @component-color-actions;
        text-align: left;
        white-space: nowrap;

        &:hover {
          color: @component-color-actions-hover;
          text-decoration: none;
        }

        &:focus {
          outline: none;
          border-radius: @component-border-radius-focus!important;
          box-shadow: inset 0 0 0 2px @component-color-focus;
        }
      }
    }
    [disabled],
    .disabled {
      &,
      &:hover,
      &:focus {
        cursor: @cursor-disabled !important;
        opacity: var(--c8y-root-component-disabled-opacity);
        &:not(.btn) {
          color: @component-color-actions!important;
        }
      }
    }
  }

  > li:first-child a,
  > li:first-child > button,
  .divider + li > a {
    border-top-color: transparent !important;
  }

  // forms in dropdown-menu
  > .dropdown-form {
    padding: 16px;
    min-width: 250px;
    background-color: @component-background-default;
    overflow-y: auto;
    .c8y-scrollbar();
    &:has(.dropdown-footer) {
      padding-bottom: 0;
    }
    .dropdown-footer {
      margin: 0 -16px;
      padding: 16px;
      .separator-top();
      background-color: @component-background-default;
    }
  }

  .form-group.datepicker {
    .uib-datepicker-popup {
      min-width: 281px;
    }
  }
}

.dropdown.open .dropdown-menu-grid-4 {
  display: grid !important;

  grid-template-columns: 1fr 1fr 1fr 1fr;

  li > a {
    padding: 9px 8px;
    border-width: 0;
    text-align: center;
    transition: box-shadow 0.25s ease;

    &:hover,
    &.active {
      box-shadow: inset 0 0 0 2px @component-color-actions-hover;
      color: @component-color-actions-hover;
    }
  }
}

.dropdown.open,
.uib-dropdown-open {
  > .dropdown-menu {
    display: block;
    height: auto;

    &.visible-overflow {
      overflow: visible;
    }

    li {
      top: 0 !important;
    }
  }

  // Remove the outline when :focus is triggered
  > a {
    outline: 0;
  }
}

// Hover/Focus state
.dropdown-menu > li > a:not(.btn) {
  &:hover,
  &:focus {
    color: @component-color-actions-hover;
    text-decoration: none;
  }
}

// Active state
.dropdown-menu > .active > a:not(.btn),
.dropdown-menu > .active > button:not(.btn) {
  &,
  &:hover,
  &:focus {
    outline: 0;
    color: @component-color-active !important;
    text-decoration: none;
  }
}

// Disabled state

.dropdown-menu > .disabled {
  cursor: @cursor-disabled;
  > a,
  > button {
    pointer-events: none;
    opacity: @component-disabled-opacity;
  }
}
.dropdown-menu button[disabled]:not(.btn) {
  &,
  &:hover,
  &:focus {
    color: @component-color-disabled !important;
  }

  // Nuke hover/focus effects
  &:hover,
  &:focus {
    background-color: @component-background-default;
    background-image: none;
    color: @component-color-disabled !important;
    text-decoration: none;
    cursor: @cursor-disabled;
    .reset-filter();
  }
}

// Menu positioning
// Add extra class to `.dropdown-menu` to flip the alignment of the dropdown
// menu with the parent.
.dropdown-menu-right {
  right: 0;
  left: auto; // Reset the default from `.dropdown-menu`
  &-grid {
    right: 9px !important;
    left: auto !important;
  }
}

@media (max-width: @screen-xs-max) {
  .dropdown-menu-right--xs {
    right: 0;
    left: auto; // Reset the default from `.dropdown-menu`
    &-grid {
      right: 9px !important;
      left: auto !important;
    }
  }
}

// This is only for left-aligning a dropdown menu within a `.navbar-right` or
// `.pull-right` nav component.
.dropdown-menu-left {
  right: auto !important;
  left: 0 !important;
}

.uib-dropdown-open {
  .dropdown-menu-action-bar {
    position: fixed;
    top: 112px !important;
  }
}
.uib-dropdown-open,
.open {
  .dropdown-menu-action-bar {
    max-height: calc(100vh - 200px);
    overflow-y: auto !important;
    height: auto;
  }
}

// Dropdown section headers
.dropdown-header {
  .text-label-small();
  font-weight: 500;
  display: block;
  padding: @margin-base @component-padding;
  border-top: 1px solid fade(@black, 5%);
  background-color: @component-background-default;
  color: @component-color-default!important;
  white-space: nowrap;
  font-size: @font-size-small;
  line-height: @line-height-base;

  &:not(:first-child) {
    border-top: 2px solid @component-border-color;
  }
  &.no-border-top {
    border-top: 0;
  }
}

// Backdrop to catch body clicks on mobile, etc.
.dropdown-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: (@zindex-dropdown - 10);
}

// Right aligned dropdowns
.pull-right > .dropdown-menu {
  right: 0;
  left: auto;
}

// Allow for dropdowns to go bottom up (aka, dropup-menu)
// Just add .dropup after the standard .dropdown class and you're set.
.dropup,
.navbar-fixed-bottom .dropdown {
  // Different positioning for bottom up menu
  .dropdown-menu {
    top: auto;
    bottom: 100%;
    margin-top: 0;
    margin-bottom: 1px;
  }
}

// Component alignment
// Reiterate per navbar.less and the modified component alignment there.
@media (min-width: @grid-float-breakpoint) {
  .navbar-right {
    .dropdown-menu {
      .dropdown-menu-right();
    }

    .dropdown-menu-left {
      .dropdown-menu-left();
    }
  }
}

// wrapper for the toolbar
.more-wrapper {
  .dropdown-menu > li > .btn.btn-link {
    display: block;
    padding: 10px 15px 9px !important;
    width: 100%;
    border-top: 1px solid @component-border-color;
    background-color: @component-background-default;
    color: @component-color-actions;
    text-align: left;
    white-space: nowrap; // prevent links from randomly breaking onto new lines
    font-weight: normal;
    line-height: @line-height-base;
  }
}

.dropup,
.navbar-fixed-bottom .dropdown {
  // Different positioning for bottom up menu
  .dropdown-menu {
    top: auto;
    bottom: 100%;
    margin-top: 0;
    margin-bottom: 1px;
  }
}

.c8y-dropdown {
  position: relative;
  display: inline-block;
  margin: 0;

  &:not(.btn) {
    padding: 10px;
    border: 0;
    border-radius: 0;
    background-color: transparent;
    background-image: none;
    color: @component-color-actions;
  }

  &:hover,
  .open & {
    text-decoration: none;

    > span {
      position: relative;
      z-index: 10;
    }
  }

  [class^='dlt-c8y-icon-'],
  [class*=' dlt-c8y-icon-'] {
    position: relative;
  }

  + .dropdown-menu {
    overflow: hidden;
    margin: 0;
    height: 0;
    border: 0;

    > li,
    > .d-contents > li {
      a:not(.btn):not(.btn-help):not(.btn-clean),
      button:not(.btn):not(.btn-help):not(.btn-clean),
      label:not(.c8y-checkbox):not(.c8y-radio) {
        display: block;
        padding: 9px 16px 10px;
        width: 100%;
        border: 0;
        border-top: 1px solid @component-border-color;
        border-radius: 0;
        background-color: @component-background-default;
        background-image: none;
        color: @component-color-actions;
        text-align: left;
        white-space: nowrap;

        &:hover {
          color: @component-color-actions-hover;
          text-decoration: none;
        }
      }
      @media (min-width: @screen-md-min) {
        label {
          padding: 9px 16px 10px;
        }
      }
    }
  }
}

.dropdown.open,
[dropdown].open,
.dropdown[dropdown-append-to-body='true'],
.btn-group.open {
  .c8y-dropdown:not([aria-expanded='false']),
  .c8y-dropdown[aria-expanded='true'] {
    &:not(.btn) {
      background: @component-background-default;
      box-shadow: var(--c8y-elevation-md-bottom);
      color: @component-color-actions-hover;
      opacity: 1;

      &:focus {
        outline: 2px solid @component-color-focus;
        outline-offset: -2px;
        border-radius: @component-border-radius-focus!important;
      }
    }

    + .dropdown-menu {
      height: auto;
      border: 1px solid @component-border-color;
    }
  }

  &.dropup {
    .c8y-dropdown {
      position: relative;
      z-index: 1002;
    }
  }
}

.dropup.open,
.dropup.show,
[dropup].open {
  .c8y-dropdown {
    &:not(.btn) {
      color: currentColor;
      opacity: 1;
    }

    + .dropdown-menu {
      z-index: @zindex-navbar-fixed + 1;
      height: auto;
      // top: 0!important;
    }
  }
}

.dropdown[c8yDropdownDirection],
.dropdown {
  &.dropup {
    .dropdown-menu {
      transform: translateY(calc(-100% - 32px)) !important;
    }
  }
}

body {
  > .dropdown-menu {
    overflow: hidden;
    margin: 0;
    height: 0;
    border: 0;
    background-color: transparent;
    > li {
      position: relative;

      a:not(.btn):not(.btn-help):not(.btn-clean),
      button:not(.btn):not(.btn-help):not(.btn-clean) {
        display: block;
        padding: 10px 15px 9px;
        width: 100%;
        border: 0;
        border-top: 1px solid @component-border-color;
        border-radius: 0;
        background-color: @component-background-default;
        background-image: none;
        text-align: left;
        white-space: nowrap;

        &:hover {
          color: @component-color-actions-hover;
          text-decoration: none;
        }
        &:focus {
          outline: 2px solid @component-color-focus;
          outline-offset: -2px;
          border-radius: @component-border-radius-focus!important;
        }
      }
    }
  }

  &.uib-dropdown-open {
    > .dropdown-menu {
      z-index: @zindex-modal + 1 !important;
      height: auto;
    }
  }

  > bs-dropdown-container {
    z-index: @zindex-modal + 1 !important; // fix z-index for ngx-bootstrap dropdown attached to body
    // line-height: 0;
    height: 0;
    .dropdown {
      position: unset;
    }
  }
}

// fix for AngularJS typeahead dropdown
[uib-typeahead-popup].dropdown-menu {
  overflow: auto;
  max-height: 200px;
  width: 100%;

  .c8y-scrollbar();

  &:not(.uib-datepicker-popup) li {
    position: static;
  }
}

.c8y-search-dropdown {
  .input-group-dropdown {
    z-index: 1;
  }
}
