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

// Scoped in typesetted content to match specificity of regular content
.md-typeset {

  // Tabbed container
  .tabbed-set {
    position: relative;
    display: flex;
    flex-flow: column wrap;
    margin: 1em 0;
    border-radius: px2rem(2px);

    // Tab radio button - the Tabbed extension will generate radio buttons with
    // labels, so tabs can be triggered without the necessity for JavaScript.
    // This is pretty cool, as it has great accessibility out-of-the box, so
    // we just hide the radio button and toggle the label color for indication.
    > input {
      position: absolute;
      width: 0;
      height: 0;
      opacity: 0;

      // Tab label states
      @for $i from 20 through 1 {
        &:nth-child(#{$i}) {

          // Tab is active
          &:checked {

            // Tab label
            ~ .tabbed-labels > :nth-child(#{$i}) {
              @extend %tabbed-label;
            }

            // Tab content
            ~ .tabbed-content > :nth-child(#{$i}) {
              @extend %tabbed-content;
            }
          }

          // Tab label on keyboard focus
          &.focus-visible ~ .tabbed-labels > :nth-child(#{$i}) {
            @extend %tabbed-label-focus-visible;
          }
        }
      }
    }
  }

  // Tabbed labels
  .tabbed-labels {
    display: flex;
    max-width: 100%;
    overflow: auto;
    box-shadow: 0 px2rem(-1px) var(--md-default-fg-color--lightest) inset;
    scroll-snap-type: x proximity;
    -ms-overflow-style: none;          // IE, Edge
    scrollbar-width: none;             // Firefox

    // [print]: Move one layer up for ordering
    @media print {
      display: contents;
    }

    // [screen and no reduced motion]: Disable animation
    @media screen {

      // [js]: Show animated tab indicator
      .js & {
        position: relative;

        // Tab indicator
        &::before {
          position: absolute;
          bottom: 0;
          left: 0;
          display: block;
          width: var(--md-indicator-width);
          height: 2px;
          background: var(--md-accent-fg-color);
          transform: translateX(var(--md-indicator-x));
          transition:
            width     225ms,
            transform 250ms;
          transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
          content: "";
        }
      }
    }

    // Webkit scrollbar
    &::-webkit-scrollbar {
      display: none;                   // Chrome, Safari
    }

    // Tab label
    > label {
      flex-shrink: 0;
      width: auto;
      padding: px2em(10px, 12.8px) 1.25em px2em(8px, 12.8px);
      color: var(--md-default-fg-color--light);
      font-weight: 700;
      font-size: px2rem(12.8px);
      white-space: nowrap;
      border-bottom: px2rem(2px) solid transparent;
      scroll-snap-align: start;
      border-radius: px2rem(2px) px2rem(2px) 0 0;
      cursor: pointer;
      transition:
        background-color 250ms,
        color            250ms;

      // [print]: Intersperse labels with containers
      @media print {

        // Ensure correct order of labels
        @for $i from 1 through 20 {
          &:nth-child(#{$i}) {
            order: $i;
          }
        }
      }

      // Tab label on hover
      &:hover {
        color: var(--md-accent-fg-color);
      }
    }
  }

  // Tabbed content
  .tabbed-content {
    width: 100%;

    // [print]: Move one layer up for ordering
    @media print {
      display: contents;
    }
  }

  // Tabbed block
  .tabbed-block {
    display: none;

    // [print]: Intersperse labels with containers
    @media print {
      display: block;

      // Ensure correct order of containers
      @for $i from 1 through 20 {
        &:nth-child(#{$i}) {
          order: $i;
        }
      }
    }

    // Code block is the first child of a tab - remove margin and mirror
    // previous (now deprecated) SuperFences code block grouping behavior
    > pre:first-child,
    > .highlight:first-child pre,
    > .highlighttable:first-child {
      margin: 0;

      // Remove rounded borders
      code {
        border-start-start-radius: 0;
        border-start-end-radius: 0;
      }
    }

    // Remove rounded border on line numbers container
    > .highlighttable:first-child .linenos {
      border-start-start-radius: 0;
      border-start-end-radius: 0;
    }

    // Code block result container is the second child
    > .result:nth-child(2) {
      margin-top: 0;
    }

    // Adjust spacing for nested tabbed container
    > .tabbed-set {
      margin: 0;
    }
  }
}

// ----------------------------------------------------------------------------
// Rules: top-level
// ----------------------------------------------------------------------------

// [mobile -]: Align with body copy
@include break-to-device(mobile) {

  // Top-level tabbed labels
  .md-content__inner > .tabbed-set .tabbed-labels {
    max-width: 100vw;
    margin: 0 px2rem(-16px);
    padding-inline-start: px2rem(16px);
    scroll-padding-inline-start: px2rem(16px);

    // Hack: some browsers ignore the right padding on flex containers,
    // see https://bit.ly/3lsPS3S
    &::after {
      padding-inline-end: px2rem(16px);
      content: "";
    }
  }
}

// ----------------------------------------------------------------------------
// Placeholders: improve colocation for better compression
// ----------------------------------------------------------------------------

// Tab label placeholder
%tabbed-label {

  // [screen]: Show active state
  @media screen {
    color: var(--md-accent-fg-color);

    // [no-js]: Show border (indicator is animated with JavaScript)
    .no-js & {
      border-color: var(--md-accent-fg-color);
    }
  }
}

// Tab label on keyboard focus placeholder
%tabbed-label-focus-visible {
  background-color: var(--md-accent-fg-color--transparent);
}

// Tab content placeholder
%tabbed-content {
  display: block;
}
