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

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

// Icon definitions
:root {
  --md-search-result-icon: svg-load("@mdi/svg/svg/file-search-outline.svg");
}

// ----------------------------------------------------------------------------

// Search container
.md-search {
  position: relative;

  // 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) 0;
  }

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

    // [tablet portrait -]: Full-screen search bar
    @include break-to-device(tablet portrait) {
      position: absolute;
      top: px2rem(4px);
      left: px2rem(-44px);
      width: px2rem(40px);
      height: px2rem(40px);
      overflow: hidden;
      background-color: var(--md-default-bg-color);
      border-radius: px2rem(20px);
      transform-origin: center;
      transition:
        transform 300ms 100ms,
        opacity   200ms 200ms;
      pointer-events: none;

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

      // Expanded overlay
      #{$md-toggle__search--checked} & {
        opacity: 1;
        transition:
          transform 400ms,
          opacity   100ms;
      }
    }

    // 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;
      background-color: hsla(0, 0%, 0%, 0.54);
      cursor: pointer;
      transition:
        width     0ms 250ms,
        height    0ms 250ms,
        opacity 250ms;

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

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

  // Search modal wrapper
  &__inner {
    // Hack: reduce jitter
    backface-visibility: hidden;

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

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

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

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

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

      // Adjust for right-to-left 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: 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;
    z-index: 2;
    padding: 0 px2rem(44px) 0 px2rem(72px);
    text-overflow: ellipsis;
    background-color: var(--md-default-bg-color);
    transition:
      color            250ms,
      background-color 250ms;

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

    // Transition on placeholder
    &::placeholder {
      transition: color 250ms;
    }

    // Placeholder and icon color in active state
    ~ .md-search__icon,
    &::placeholder {
      color: var(--md-default-fg-color--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);
      color: inherit;
      font-size: px2rem(16px);
      background-color: hsla(0, 0%, 0%, 0.26);
      border-radius: px2rem(2px);

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

      // Search icon color
      + .md-search__icon {
        color: var(--md-primary-bg-color);
      }

      // Placeholder color
      &::placeholder {
        color: var(--md-primary-bg-color--light);
      }

      // Hovered search field
      &:hover {
        background-color: hsla(0, 0%, 100%, 0.12);
      }

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

        // Search icon and placeholder color in active state
        + .md-search__icon,
        &::placeholder {
          color: var(--md-default-fg-color--light);
        }
      }
    }
  }

  // Search icon
  &__icon {
    position: absolute;
    z-index: 2;
    width: px2rem(24px);
    height: px2rem(24px);
    cursor: pointer;
    transition:
      color   250ms,
      opacity 250ms;

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

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

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

        // Flip icon vertically
        svg {
          transform: scaleX(-1);
        }
      }

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

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

        // Hide the magnifying glass (1st icon)
        svg:first-child {
          display: none;
        }
      }

      // [tablet landscape +]: Header-embedded search
      @include break-from-device(tablet landscape) {
        pointer-events: none;

        // Hide the arrow (2nd icon)
        svg:last-child {
          display: none;
        }
      }
    }

    // Reset button
    &[type="reset"] {
      top: px2rem(6px);
      right: px2rem(10px);
      transform: scale(0.75);
      opacity: 0;
      transition:
        transform 150ms cubic-bezier(0.1, 0.7, 0.1, 1),
        opacity   150ms;
      pointer-events: none;

      // Adjust for right-to-left 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);

        // Adjust for right-to-left languages
        [dir="rtl"] & {
          right: initial;
          left: 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;
        pointer-events: initial;

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

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

    // [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);
      opacity: 0;
      transition: opacity 400ms;

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

        opacity: 1;
      }
    }
  }

  // Wrapper for scrolling on overflow
  &__scrollwrap {
    height: 100%;
    overflow-y: auto;
    background-color: var(--md-default-bg-color);
    // Hack: reduce jitter
    backface-visibility: hidden;
    scroll-snap-type: y mandatory;
    touch-action: pan-y;

    // Mitigiate excessive repaints on non-retina devices
    @media (max-resolution: 1dppx) {
      transform: translateZ(0);
    }

    // [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;
      // Override Firefox scrollbar style
      scrollbar-width: thin;
      scrollbar-color: var(--md-default-fg-color--lighter) transparent;

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

      // 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);
        }
      }
    }
  }
}

// Search result
.md-search-result {
  color: var(--md-default-fg-color);
  word-break: break-word;

  // Search metadata
  &__meta {
    padding: 0 px2rem(16px);
    color: var(--md-default-fg-color--light);
    font-size: px2rem(12.8px);
    line-height: px2rem(36px);
    background-color: var(--md-default-fg-color--lightest);
    scroll-snap-align: start;

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

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

  // List of items
  &__list {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  // List item
  &__item {
    box-shadow: 0 px2rem(-1px) 0 var(--md-default-fg-color--lightest);

    // No border for first item
    &:first-child {
      box-shadow: none;
    }
  }

  // Link inside item
  &__link {
    display: block;
    outline: none;
    transition: background 250ms;
    scroll-snap-align: start;

    // Focused or hovered link
    &:focus,
    &:hover {
      background-color: var(--md-accent-fg-color--transparent);

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

    // Add a little spacing on the last element of the last link
    &:last-child p:last-child {
      margin-bottom: px2rem(12px);
    }
  }

  // Search result container
  &__more summary {
    display: block;
    padding: px2em(12px) px2rem(16px);
    color: var(--md-typeset-a-color);
    font-size: px2rem(12.8px);
    outline: 0;
    cursor: pointer;
    transition:
      color            250ms,
      background-color 250ms;
    scroll-snap-align: start;

    // Focused or hovered button
    &:focus,
    &:hover {
      color: var(--md-accent-fg-color);
      background-color: var(--md-accent-fg-color--transparent);
    }

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

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

    // Remove default details marker
    &::-webkit-details-marker {
      display: none;
    }

    // All following elements
    & ~ * {

      // Make less relevant terms more transparent
      > * {
        opacity: 0.65;
      }
    }
  }

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

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

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

    // Document
    &--document {

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

  // Search result icon
  &__icon {
    position: absolute;
    left: 0;
    width: px2rem(24px);
    height: px2rem(24px);
    margin: px2rem(10px);
    color: var(--md-default-fg-color--light);

    // Inline icon and adjust icon to match font size
    &::after {
      display: inline-block;
      width: 100%;
      height: 100%;
      background-color: currentColor;
      mask-image: var(--md-search-result-icon);
      mask-repeat: no-repeat;
      mask-size: contain;
      content: "";
    }

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

      // Flip icon vertically
      &::after {
        transform: scaleX(-1);
      }
    }

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

  // Title
  &__title {
    margin: 0.5em 0;
    font-weight: 700;
    font-size: px2rem(12.8px);
    line-height: 1.6;
  }

  // Teaser
  &__teaser {
    display: -webkit-box;
    max-height: px2rem(40px);
    margin: 0.5em 0;
    overflow: hidden;
    color: var(--md-default-fg-color--light);
    font-size: px2rem(12.8px);
    line-height: 1.6;
    text-overflow: ellipsis;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;

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

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

    // Search term highlighting
    mark {
      text-decoration: underline;
      background-color: transparent;
    }
  }

  // Terms
  &__terms {
    margin: 0.5em 0;
    font-size: px2rem(12.8px);
    font-style: italic;
  }

  // Search term highlighting
  mark {
    color: var(--md-accent-fg-color);
    background-color: transparent;
  }
}
