////
/// 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
////

// ----------------------------------------------------------------------------
// Variables
// ----------------------------------------------------------------------------

// Active (toggled) drawer
$md-toggle__drawer--checked:
  "[data-md-toggle=\"drawer\"]:checked ~ .md-container";

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

// Activate scroll snapping with delay
@keyframes md-sidebar__scrollwrap--hack {
  0%, 99% {
    scroll-snap-type: none;
  }

  100% {
    scroll-snap-type: y mandatory;
  }
}

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

// Sidebar container
.md-sidebar {
  position: sticky;
  top: px2rem(48px);
  align-self: flex-start;
  width: px2rem(242px);
  padding: px2rem(24px) 0;
  overflow: hidden;

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

  // [tablet -]: Convert navigation to drawer
  @include break-to-device(tablet) {

    // Render primary sidebar as a slideout container
    &--primary {
      position: fixed;
      top: 0;
      left: px2rem(-242px);
      z-index: 3;
      width: px2rem(242px);
      height: 100%;
      background-color: var(--md-default-bg-color);
      transform: translateX(0);
      transition:
        transform  250ms cubic-bezier(0.4, 0, 0.2, 1),
        box-shadow 250ms;

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

      // Expanded drawer
      #{$md-toggle__drawer--checked} & {
        @include z-depth(8);

        transform: translateX(px2rem(242px));

        // Adjust for right-to-left languages
        [dir="rtl"] & {
          transform: translateX(px2rem(-242px));
        }
      }

      // Hide overflow for nested navigation
      .md-sidebar__scrollwrap {
        overflow: hidden;
      }
    }
  }

  // Secondary sidebar with table of contents
  &--secondary {
    display: none;
    order: 2;

    // [tablet landscape +]: Show table of contents next to body copy
    @include break-from-device(tablet landscape) {
      display: block;

      // Ensure smooth scrolling on iOS
      .md-sidebar__scrollwrap {
        touch-action: pan-y;
      }
    }
  }

  // Wrapper for scrolling on overflow
  &__scrollwrap {
    max-height: 100%;
    margin: 0 px2rem(4px);
    overflow-y: auto;
    // Hack: reduce jitter
    backface-visibility: hidden;
    // Override Firefox scrollbar style
    scrollbar-width: thin;
    scrollbar-color: var(--md-default-fg-color--lighter) transparent;

    // Hack: Chrome 81+ exhibits a strange bug, where it scrolls the container
    // to the bottom if `scroll-snap-type` is set on the initial render. For
    // this reason, we use an animation to set scroll snaping with a slight
    // delay, which seems to fix the issue (#1667).
    .js & {
      animation: md-sidebar__scrollwrap--hack 400ms forwards;
    }

    // [tablet -]: Adjust margins
    @include break-to-device(tablet) {

      // Stretch scrollwrap for primary sidebar
      .md-sidebar--primary & {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: 0;
        scroll-snap-type: none;
      }
    }

    // Override Firefox scrollbar hover color
    &:hover {
      scrollbar-color: var(--md-accent-fg-color) transparent;
    }

    // Override native scrollbar styles
    &::-webkit-scrollbar {
      width: px2rem(4px);
      height: px2rem(4px);
    }

    // Scrollbar thumb
    &::-webkit-scrollbar-thumb {
      background-color: var(--md-default-fg-color--lighter);

      // Hovered scrollbar thumb
      &:hover {
        background-color: var(--md-accent-fg-color);
      }
    }
  }
}
