////
/// Copyright (c) 2016-2020 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
////

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

// Tabs with outline
.md-tabs {
  width: 100%;
  overflow: auto;
  color: var(--md-primary-bg-color);
  background-color: var(--md-primary-fg-color);
  transition: background 250ms;

  // Omit transitions, in case JavaScript is not available
  .no-js & {
    transition: none;
  }

  // [tablet -]: Hide tabs for tablet and below, as they don't make any sense
  @include break-to-device(tablet) {
    display: none;
  }

  // Hide for print
  @media print {
    display: none;
  }

  // List of items
  &__list {
    margin: 0;
    margin-left: px2rem(4px);
    padding: 0;
    white-space: nowrap;
    list-style: none;
    contain: content;

    // Adjust for right-to-left languages
    [dir="rtl"] & {
      margin-right: px2rem(4px);
      margin-left: initial;
    }
  }

  // List item
  &__item {
    display: inline-block;
    height: px2rem(48px);
    padding-right: px2rem(12px);
    padding-left: px2rem(12px);
  }

  // Link inside item - could be defined as block elements and aligned via
  // line height, but this would imply more repaints when scrolling
  &__link {
    display: block;
    margin-top: px2rem(16px);
    font-size: px2rem(14px);
    // Hack: save a repaint when the tabs are appearing when scrolling up
    backface-visibility: hidden;
    opacity: 0.7;
    transition:
      transform 400ms cubic-bezier(0.1, 0.7, 0.1, 1),
      opacity   250ms;

    // Omit transitions, in case JavaScript is not available
    .no-js & {
      transition: none;
    }

    // Active or hovered link
    &--active,
    &:hover {
      color: inherit;
      opacity: 1;
    }

    // Delay transitions by a small amount
    @for $i from 2 through 16 {
      .md-tabs__item:nth-child(#{$i}) & {
        transition-delay: 20ms * ($i - 1);
      }
    }
  }

  // Fade-out tabs background upon scrolling
  &[data-md-state="hidden"] {
    pointer-events: none;

    // Hide tabs upon scrolling - disable transition to minimizes repaints
    // while scrolling down, while scrolling up seems to be okay
    .md-tabs__link {
      transform: translateY(50%);
      opacity: 0;
      transition:
        transform 0ms 100ms,
        opacity 100ms;
    }
  }

  // [screen +]: Adjust main navigation styles
  @include break-from-device(screen) {

    // Hide 1st level nested items, as they are listed in the tabs
    ~ .md-main .md-nav--primary > .md-nav__list > .md-nav__item--nested {
      display: none;
    }

    // Active tab
    &--active ~ .md-main {

      // Adjust 1st level styles
      .md-nav--primary {

        // Show title and remove spacing
        .md-nav__title {
          display: block;
          padding: 0 px2rem(12px);
          pointer-events: none;
          scroll-snap-align: start;

          // Hide site title
          &[for="__drawer"] {
            display: none;
          }
        }

        // Hide 1st level items
        > .md-nav__list > .md-nav__item {
          display: none;

          // Show 1st level active nested items
          &--active {
            display: block;
            padding: 0;

            // Hide nested links
            > .md-nav__link {
              display: none;
            }
          }
        }
      }

      // Always expand nested navigation on 2nd level
      .md-nav[data-md-level="1"] {
        // Hack: always show active navigation tab on breakpoint screen, despite
        // of checkbox being checked or not. Fixes #1655.
        display: block;

        // Remove spacing on 2nd level items
        > .md-nav__list > .md-nav__item {
          padding: 0 px2rem(12px);

          // Add bottom spacing to last item
          &:last-child {
            padding-bottom: px2rem(12px);

            // Remove bottom spacing for nested items
            .md-nav__item {
              padding-bottom: 0;
            }
          }
        }

        // Hide titles from 2nd level on
        .md-nav .md-nav__title {
          display: none;
        }
      }
    }
  }
}
