////
/// Copyright (c) 2016-2019 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) search
$md-toggle__search--checked:
  "[data-md-toggle=\"search\"]:checked ~ .md-header";

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

// Search container
.md-search {

  // Hide search, if JavaScript is not available.
  .no-js & {
    display: none;
  }

  // [tablet landscape +]: Header-embedded search
  @include break-from-device(tablet landscape) {
    padding: px2rem(4px);
  }

  // Search modal overlay
  &__overlay {
    opacity: 0;
    z-index: 1;

    // [tablet portrait -]: Full-screen search bar
    @include break-to-device(tablet portrait) {
      position: absolute;
      top: px2rem(4px);
      left: px2rem(4px);
      width: px2rem(36px);
      height: px2rem(36px);
      transform-origin: center;
      transition:
        transform 0.3s 0.1s,
        opacity   0.2s 0.2s;
      border-radius: px2rem(20px);
      background-color: $md-color-white;
      overflow: hidden;
      pointer-events: none;

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

      // Expanded overlay
      #{$md-toggle__search--checked} & {
        transition:
          transform 0.4s,
          opacity   0.1s;
        opacity: 1;
      }
    }

    // Set scale factors
    #{$md-toggle__search--checked} & {

      // [mobile portrait -]: Scale up 45 times
      @include break-to-device(mobile portrait) {
        transform: scale(45);
      }

      // [mobile landscape]: Scale up 60 times
      @include break-at-device(mobile landscape) {
        transform: scale(60);
      }

      // [tablet portrait]: Scale up 75 times
      @include break-at-device(tablet portrait) {
        transform: scale(75);
      }
    }

    // [tablet landscape +]: Overlay for better focus on search
    @include break-from-device(tablet landscape) {
      position: fixed;
      top: 0;
      left: 0;
      width: 0;
      height: 0;
      transition:
        width   0s 0.25s,
        height  0s 0.25s,
        opacity 0.25s;
      background-color: $md-color-black--light;
      cursor: pointer;

      // Adjust for RTL languages
      [dir="rtl"] & {
        right: 0;
        left: initial;
      }

      // Expanded overlay
      #{$md-toggle__search--checked} & {
        width: 100%;
        height: 100%;
        transition:
          width   0s,
          height  0s,
          opacity 0.25s;
        opacity: 1;
      }
    }
  }

  // Search modal wrapper
  &__inner {

    // [tablet portrait -]: Put search modal off-canvas by default
    @include break-to-device(tablet portrait) {
      position: fixed;
      top: 0;
      left: 100%;
      width: 100%;
      height: 100%;
      transform: translateX(5%);
      transition:
        right     0s    0.3s,
        left      0s    0.3s,
        transform 0.15s 0.15s cubic-bezier(0.4, 0, 0.2, 1),
        opacity   0.15s 0.15s;
      opacity: 0;
      z-index: 2;

      // Active search modal
      #{$md-toggle__search--checked} & {
        left: 0;
        transform: translateX(0);
        transition:
          right     0s    0s,
          left      0s    0s,
          transform 0.15s 0.15s cubic-bezier(0.1, 0.7, 0.1, 1),
          opacity   0.15s 0.15s;
        opacity: 1;

        // Adjust for RTL languages
        [dir="rtl"] & {
          right: 0;
          left: initial;
        }
      }

      // Adjust for RTL languages
      html [dir="rtl"] & {
        right: 100%;
        left: initial;
        transform: translateX(-5%);
      }
    }

    // [tablet landscape +]: Header-embedded search
    @include break-from-device(tablet landscape) {
      position: relative;
      width: px2rem(230px);
      margin-right: px2rem(20px);
      padding: px2rem(2px) 0;
      float: right;
      transition: width 0.25s cubic-bezier(0.1, 0.7, 0.1, 1);

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

    // [screen +]: Increase spacing of search bar
    @include break-from-device(screen) {
      margin-right: px2rem(28px);

      // Adjust for RTL languages
      [dir="rtl"] & {
        margin-left: px2rem(28px);
      }
    }

    // Set maximum width
    #{$md-toggle__search--checked} & {

      // [tablet landscape]: Do not overlay title
      @include break-at-device(tablet landscape) {
        width: px2rem(468px);
      }

      // [screen +]: Match content width
      @include break-from-device(screen) {
        width: px2rem(688px);
      }
    }
  }

  // Search form
  &__form {
    position: relative;

    // [tablet landscape +]: Header-embedded search
    @include break-from-device(tablet landscape) {
      border-radius: px2rem(2px);
    }
  }

  // Search input
  &__input {
    position: relative;
    padding: 0 px2rem(44px) 0 px2rem(72px);
    text-overflow: ellipsis;
    z-index: 2;

    // Adjust for RTL languages
    [dir="rtl"] & {
      padding: 0 px2rem(72px) 0 px2rem(44px);
    }

    // Transition on placeholder
    &::placeholder {
      transition: color 0.25s cubic-bezier(0.1, 0.7, 0.1, 1);
    }

    // Placeholder and icon color in active state
    ~ .md-search__icon,
    &::placeholder {
      color: $md-color-black--light;
    }

    // Remove the "x" rendered by Internet Explorer
    &::-ms-clear {
      display: none;
    }

    // [tablet portrait -]: Full-screen search bar
    @include break-to-device(tablet portrait) {
      width: 100%;
      height: px2rem(48px);
      font-size: px2rem(18px);
    }

    // [tablet landscape +]: Header-embedded search
    @include break-from-device(tablet landscape) {
      width: 100%;
      height: px2rem(36px);
      padding-left: px2rem(44px);
      transition:
        background-color 0.25s cubic-bezier(0.1, 0.7, 0.1, 1),
        color            0.25s cubic-bezier(0.1, 0.7, 0.1, 1);
      border-radius: px2rem(2px);
      background-color: $md-color-black--lighter;
      color: inherit;
      font-size: ms(0);

      // Adjust for RTL languages
      [dir="rtl"] & {
        padding-right: px2rem(44px);
      }

      // Icon color
      + .md-search__icon {
        color: inherit;
      }

      // Placeholder color
      &::placeholder {
        color: $md-color-white--light;
      }

      // Hovered search field
      &:hover {
        background-color: $md-color-white--lightest;
      }

      // Set light background on active search field
      #{$md-toggle__search--checked} & {
        border-radius: px2rem(2px) px2rem(2px) 0 0;
        background-color: $md-color-white;
        color: $md-color-black;
        text-overflow: clip;

        // Placeholder and icon color in active state
        + .md-search__icon,
        &::placeholder {
          color: $md-color-black--light;
        }
      }
    }
  }

  // Icons
  &__icon {
    position: absolute;
    transition:
      color   0.25s cubic-bezier(0.1, 0.7, 0.1, 1),
      opacity 0.25s;
    font-size: $md-icon-size;
    cursor: pointer;
    z-index: 2;

    // Hovered icon
    &:hover {
      opacity: 0.7;
    }

    // Search icon
    &[for="__search"] {
      top: px2rem(6px);
      left: px2rem(10px);

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

      // Set search icon on pseudo class, so it can be overridden for mobile
      // and tablet when the search is rendered in an overlay
      &::before {
        content: "\E8B6"; // search
      }

      // [tablet portrait -]: Full-screen search bar
      @include break-to-device(tablet portrait) {
        top: px2rem(12px);
        left: px2rem(16px);

        // Show back arrow instead of search icon
        &[for="__search"]::before {
          content: "\E5C4"; // arrow_back

          // Adjust for RTL languages
          [dir="rtl"] & {
            content: "\E5C8"; // arrow_forward
          }
        }
      }
    }

    // Reset button
    &[type="reset"] {
      top: px2rem(6px);
      right: px2rem(10px);
      transform: scale(0.125);
      transition:
        transform 0.15s cubic-bezier(0.1, 0.7, 0.1, 1),
        opacity   0.15s;
      opacity: 0;

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

      // [tablet portrait -]: Full-screen search bar
      @include break-to-device(tablet portrait) {
        top: px2rem(12px);
        right: px2rem(16px);
      }

      // Show reset button if search is active and input non-empty
      #{$md-toggle__search--checked} .md-search__input:valid ~ & {
        transform: scale(1);
        opacity: 1;

        // Hovered icon
        &:hover {
          opacity: 0.7;
        }
      }
    }
  }

  // Search output container
  &__output {
    position: absolute;
    width: 100%;
    border-radius: 0 0 px2rem(2px) px2rem(2px);
    overflow: hidden;
    z-index: 1;

    // [tablet portrait -]: Full-screen search bar
    @include break-to-device(tablet portrait) {
      top: px2rem(48px);
      bottom: 0;
    }

    // [tablet landscape +]: Header-embedded search
    @include break-from-device(tablet landscape) {
      top: px2rem(38px);
      transition: opacity 0.4s;
      opacity: 0;

      // Show search output in active state
      #{$md-toggle__search--checked} & {
        @include z-depth(6);

        opacity: 1;
      }
    }
  }

  // Wrapper for scrolling on overflow
  &__scrollwrap {
    height: 100%;
    background-color: $md-color-white;
    box-shadow: 0 px2rem(1px) 0 $md-color-black--lightest inset;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;

    // [tablet landscape]: Set absolute width to omit unnecessary reflow
    @include break-at-device(tablet landscape) {
      width: px2rem(468px);
    }

    // [screen +]: Set absolute width to omit unnecessary reflow
    @include break-from-device(screen) {
      width: px2rem(688px);
    }

    // [tablet landscape +]: Limit height to viewport
    @include break-from-device(tablet landscape) {
      max-height: 0;

      // Expand in active state
      #{$md-toggle__search--checked} & {
        max-height: 75vh;
      }

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

      // Style scrollbar thumb
      &::-webkit-scrollbar-thumb {
        background-color: $md-color-black--lighter;

        // Hovered scrollbar thumb
        &:hover {
          background-color: $md-color-accent;
        }
      }
    }
  }
}

// Search result
.md-search-result {
  color: $md-color-black;
  word-break: break-word;

  // Search metadata
  &__meta {
    padding: 0 px2rem(16px);
    background-color: $md-color-black--lightest;
    color: $md-color-black--light;
    font-size: ms(-1);
    line-height: px2rem(36px);

    // [tablet landscape +]: Increase left indent
    @include break-from-device(tablet landscape) {
      padding-left: px2rem(44px);

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

  // List of items
  &__list {
    margin: 0;
    padding: 0;
    border-top: px2rem(1px) solid $md-color-black--lightest;
    list-style: none;
  }

  // List item
  &__item {
    box-shadow: 0 px2rem(-1px) 0 $md-color-black--lightest;
  }

  // Link inside item
  &__link {
    display: block;
    transition: background 0.25s;
    outline: 0;
    overflow: hidden;

    // Active or hovered link
    &[data-md-state="active"],
    &:hover {
      background-color: transparentize($md-color-accent, 0.9);

      // Slightly transparent icon
      .md-search-result__article::before {
        opacity: 0.7;
      }
    }

    // Add a little spacing on the teaser of the last link
    &:last-child .md-search-result__teaser {
      margin-bottom: px2rem(12px);
    }
  }

  // Article - document or section
  &__article {
    position: relative;
    padding: 0 px2rem(16px);
    overflow: auto;

    // [tablet landscape +]: Increase left indent
    @include break-from-device(tablet landscape) {
      padding-left: px2rem(44px);

      // Adjust for RTL languages
      [dir="rtl"] & {
        padding-right: px2rem(44px);
        padding-left: px2rem(16px);
      }
    }

    // Document
    &--document {

      // Icon
      &::before {
        @extend %md-icon, %md-icon__button;

        position: absolute;
        left: 0;
        margin: px2rem(2px);
        transition: opacity 0.25s;
        color: $md-color-black--light;
        content: "\E880"; // find_in_page

        // Adjust for RTL languages
        [dir="rtl"] & {
          right: 0;
          left: initial;
        }

        // [tablet portrait -]: Hide page icon
        @include break-to-device(tablet portrait) {
          display: none;
        }
      }

      // Title
      .md-search-result__title {
        margin: px2rem(11px) 0;
        font-size: ms(0);
        font-weight: 400;
        line-height: 1.4;
      }
    }
  }

  // Title
  &__title {
    margin: 0.5em 0;
    font-size: ms(-1);
    font-weight: 700;
    line-height: 1.4;
  }

  // stylelint-disable value-no-vendor-prefix, property-no-vendor-prefix

  // Teaser
  &__teaser {
    display: -webkit-box;
    max-height: px2rem(33px);
    margin: 0.5em 0;
    color: $md-color-black--light;
    font-size: ms(-1);
    line-height: 1.4;
    text-overflow: ellipsis;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;

    // [mobile -]: Increase number of lines
    @include break-to-device(mobile) {
      max-height: px2rem(50px);
      -webkit-line-clamp: 3;
    }

    // [tablet landscape]: Increase number of lines
    @include break-at-device(tablet landscape) {
      max-height: px2rem(50px);
      -webkit-line-clamp: 3;
    }
  }

  // stylelint-enable value-no-vendor-prefix, property-no-vendor-prefix

  // Search term highlighting
  em {
    font-style: normal;
    font-weight: 700;
    text-decoration: underline;
  }
}
