////
/// Copyright (c) 2016-2018 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: 0.4rem;
  }

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

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

      // Adjust for RTL languages
      [dir="rtl"] & {
        right: 0.4rem;
        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: 23rem;
      padding: 0.2rem 0;
      float: right;
      transition: width 0.25s cubic-bezier(0.1, 0.7, 0.1, 1);

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

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

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

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

  // Search form
  &__form {
    position: relative;

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

  // Search input
  &__input {
    position: relative;
    padding: 0 4.4rem 0 7.2rem;
    text-overflow: ellipsis;
    z-index: 2;

    // Adjust for RTL languages
    [dir="rtl"] & {
      padding: 0 7.2rem 0 4.4rem;
    }

    // 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: 4.8rem;
      font-size: 1.8rem;
    }

    // [tablet landscape +]: Header-embedded search
    @include break-from-device(tablet landscape) {
      width: 100%;
      height: 3.6rem;
      padding-left: 4.4rem;
      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: 0.2rem;
      background-color: $md-color-black--lighter;
      color: inherit;
      font-size: ms(0);

      // Adjust for RTL languages
      [dir="rtl"] & {
        padding-right: 4.4rem;
      }

      // 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: 0.2rem 0.2rem 0 0;
        background-color: $md-color-white;
        color: $md-color-black;
        text-overflow: none;

        // 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: 0.6rem;
      left: 1rem;

      // Adjust for RTL languages
      [dir="rtl"] & {
        right: 1rem;
        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: 1.2rem;
        left: 1.6rem;

        // 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: 0.6rem;
      right: 1rem;
      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: 1rem;
      }

      // [tablet portrait -]: Full-screen search bar
      @include break-to-device(tablet portrait) {
        top: 1.2rem;
        right: 1.6rem;
      }

      // 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 0.2rem 0.2rem;
    overflow: hidden;
    z-index: 1;

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

    // [tablet landscape +]: Header-embedded search
    @include break-from-device(tablet landscape) {
      top: 3.8rem;
      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 0.1rem 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: 46.8rem;
    }

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

    // [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: 0.4rem;
        height: 0.4rem;
      }

      // 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 1.6rem;
    background-color: $md-color-black--lightest;
    color: $md-color-black--light;
    font-size: ms(-1);
    line-height: 3.6rem;

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

      // Adjust for RTL languages
      [dir="rtl"] & {
        padding-right: 4.4rem;
        padding-left: initial;
      }
    }
  }

  // List of items
  &__list {
    margin: 0;
    padding: 0;
    border-top: 0.1rem solid $md-color-black--lightest;
    list-style: none;
  }

  // List item
  &__item {
    box-shadow: 0 -0.1rem 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: 1.2rem;
    }
  }

  // Article - document or section
  &__article {
    position: relative;
    padding: 0 1.6rem;
    overflow: auto;

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

      // Adjust for RTL languages
      [dir="rtl"] & {
        padding-right: 4.4rem;
        padding-left: 1.6rem;
      }
    }

    // Document
    &--document {

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

        position: absolute;
        left: 0;
        margin: 0.2rem;
        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: 1.1rem 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: 3.3rem;
    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: 5rem;
      -webkit-line-clamp: 3;
    }

    // [tablet landscape]: Increase number of lines
    @include break-at-device(tablet landscape) {
      max-height: 5rem;
      -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;
  }
}
