////
/// Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
///
/// Permission is hereby granted, free of charge, to any person obtaining a
/// copy of this software and associated documentation files (the "Software"),
/// to deal in the Software without restriction, including without limitation
/// the rights to use, copy, modify, merge, publish, distribute, sublicense,
/// and/or sell copies of the Software, and to permit persons to whom the
/// Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
/// DEALINGS
////

// ----------------------------------------------------------------------------
// Keyframes
// ----------------------------------------------------------------------------

// See https://github.com/squidfunk/mkdocs-material/issues/2429
@keyframes hoverfix {
  0% {
    pointer-events: none;
  }
}

// ----------------------------------------------------------------------------
// Rules
// ----------------------------------------------------------------------------

// Version selection variables
:root {
  --md-version-icon: svg-load("fontawesome/solid/caret-down.svg");
}

// ----------------------------------------------------------------------------

// Version selection
.md-version {
  flex-shrink: 0;
  height: px2rem(48px);
  font-size: px2rem(16px);

  // Current selection
  &__current {
    position: relative;
    // Hack: in general, we would use `vertical-align` to align the version at
    // the bottom with the title, but since the list uses absolute positioning,
    // this won't work consistently. Furthermore, we would need to use inline
    // positioning to align the links, which looks jagged.
    top: px2rem(1px);
    margin-inline: px2rem(28px) px2rem(8px);
    color: inherit;
    cursor: pointer;
    outline: none;

    // Version selection icon
    &::after {
      display: inline-block;
      width: px2rem(8px);
      height: px2rem(12px);
      margin-inline-start: px2rem(8px);
      content: "";
      background-color: currentcolor;
      mask-image: var(--md-version-icon);
      mask-position: center;
      mask-repeat: no-repeat;
      mask-size: contain;
    }
  }

  // Version selection list
  &__list {
    position: absolute;
    top: px2rem(3px);
    z-index: 3;
    max-height: 0;
    padding: 0;
    margin: px2rem(4px) px2rem(16px);
    overflow: auto;
    color: var(--md-default-fg-color);
    list-style-type: none;
    background-color: var(--md-default-bg-color);
    border-radius: px2rem(2px);
    box-shadow: var(--md-shadow-z2);
    opacity: 0;
    transition:
      max-height 0ms 500ms,
      opacity  250ms 250ms;
    scroll-snap-type: y mandatory;

    // Version selection list on parent focus/hover
    .md-version:is(:focus-within, :hover) & {
      max-height: px2rem(200px);
      opacity: 1;
      transition:
        max-height 0ms,
        opacity  250ms;
    }

    // Fix hover on touch devices
    @media (pointer: coarse), (hover: none) {
      // Switch off on hover
      .md-version:hover & {
        animation: hoverfix 250ms forwards;
      }

      // Enable on focus
      .md-version:focus-within & {
        animation: none;
      }
    }
  }

  // Version selection item
  &__item {
    line-height: px2rem(36px);
  }

  // Version selection link
  &__link {
    display: block;
    width: 100%;
    padding-inline: px2rem(12px) px2rem(24px);
    white-space: nowrap;
    cursor: pointer;
    outline: none;
    transition:
      color            250ms,
      background-color 250ms;
    scroll-snap-align: start;

    // Link on focus/hover
    &:is(:focus, :hover) {
      color: var(--md-accent-fg-color);
    }

    // Link on focus
    &:focus {
      background-color: var(--md-default-fg-color--lightest);
    }
  }
}
