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

// ----------------------------------------------------------------------------
// Rules: font definitions
// ----------------------------------------------------------------------------

// Enable font-smoothing in Webkit and FF
body {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

// Default fonts
body,
input {
  color: $md-color-black;
  font-feature-settings: "kern", "liga";
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

// Proportionally spaced fonts
pre,
code,
kbd {
  color: $md-color-black;
  font-feature-settings: "kern";
  font-family: "Courier New", Courier, monospace;
}

// ----------------------------------------------------------------------------
// Rules: typesetted content
// ----------------------------------------------------------------------------

// Content that is typeset - if possible, all margins, paddings and font sizes
// should be set in ems, so nested blocks (e.g. Admonition) render correctly,
// except headlines that should only appear on the top level and need to have
// consistent spacing due to layout constraints.
.md-typeset {
  font-size: ms(0);
  line-height: 1.6;

  // Colors should be kept when printing
  -webkit-print-color-adjust: exact;

  // Default spacing
  p,
  ul,
  ol,
  blockquote {
    margin: 1em 0;
  }

  // 1st level headline
  h1 {
    margin: 0 0 px2rem(40px);
    color: $md-color-black--light;
    font-size: ms(3);
    font-weight: 300;
    letter-spacing: -0.01em;
    line-height: 1.3;
  }

  // 2nd level headline
  h2 {
    margin: px2rem(40px) 0 px2rem(16px);
    font-size: ms(2);
    font-weight: 300;
    letter-spacing: -0.01em;
    line-height: 1.4;
  }

  // 3rd level headline
  h3 {
    margin: px2rem(32px) 0 px2rem(16px);
    font-size: ms(1);
    font-weight: 400;
    letter-spacing: -0.01em;
    line-height: 1.5;
  }

  // 3rd level headline following an 2nd level headline
  h2 + h3 {
    margin-top: px2rem(16px);
  }

  // 4th level headline
  h4 {
    margin: px2rem(16px) 0;
    font-size: ms(0);
    font-weight: 700;
    letter-spacing: -0.01em;
  }

  // 5th and 6th level headline
  h5,
  h6 {
    margin: px2rem(16px) 0;
    color: $md-color-black--light;
    font-size: ms(-1);
    font-weight: 700;
    letter-spacing: -0.01em;
  }

  // Overrides for 5th level headline
  h5 {
    text-transform: uppercase;
  }

  // Horizontal separators
  hr {
    margin: 1.5em 0;
    border-bottom: px2rem(1px) dotted $md-color-black--lighter;
  }

  // Links
  a {
    color: $md-color-primary;
    word-break: break-word;

    // Also enable color transition on pseudo elements
    &,
    &::before {
      transition: color 0.125s;
    }

    // Hovered and active links
    &:hover,
    &:active {
      color: $md-color-accent;
    }

    // Add URLs for print
    @media print {

      // Show link URL
      &::after {
        color: $md-color-black--light;
        content: " [" attr(href) "]";
      }
    }
  }

  // Code blocks
  code,
  pre {
    background-color: $md-code-background;
    color: $md-code-color;
    font-size: 85%;
    direction: ltr;

    // Wrap text and hide scollbars
    @media print {
      white-space: pre-wrap;
    }
  }

  // Inline code blocks, correct relative ems for smaller font size
  code {
    $correct: 1 / 0.85;

    margin: 0 0.25em * $correct;
    padding: 0.0625em * $correct 0;
    border-radius: px2rem(2px);
    box-shadow:
      +0.25em * $correct 0 0 $md-code-background,
      -0.25em * $correct 0 0 $md-code-background;
    word-break: break-word;
    box-decoration-break: clone;

    // Remove box-shadows for print
    @media print {
      box-shadow: none;
      box-decoration-break: initial;
    }
  }

  // Disable containing block inside headlines
  h1 code,
  h2 code,
  h3 code,
  h4 code,
  h5 code,
  h6 code {
    margin: 0;
    background-color: transparent;
    box-shadow: none;
  }

  // Reset code if it's inside a link
  a > code {
    margin: inherit;
    padding: inherit;
    border-radius: initial;
    background-color: inherit;
    color: inherit;
    box-shadow: none;
  }

  // Unformatted code blocks
  pre {
    position: relative;
    margin: 1em 0;
    border-radius: px2rem(2px);
    line-height: 1.4;
    -webkit-overflow-scrolling: touch;

    // [mobile -]: Stretch to whole width
    @include break-to-device(mobile) {
      margin: 1em px2rem(-16px);
      border-radius: 0;
    }

    // Actual container with code, overflowing
    > code {
      display: block;
      margin: 0;
      padding: px2rem(10.5px) px2rem(12px);
      background-color: transparent;
      font-size: inherit;
      box-shadow: none;
      box-decoration-break: slice;
      overflow: auto;

      // [mobile -]: Increase padding to match text
      @include break-to-device(mobile) {
        padding: px2rem(10.5px) px2rem(16px);
      }

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

  // Keystrokes
  kbd {
    $correct: 1 / 0.85;

    padding: 0 0.25em * $correct;
    border: px2rem(1px) solid darken($md-keyboard-background, 20%);
    border-radius: px2rem(3px);
    border-bottom-color: darken($md-keyboard-background, 25%);
    background-color: $md-keyboard-background;
    color: $md-keyboard-color;
    font-size: 85%;
    box-shadow: 0 px2rem(1px) 0 darken($md-keyboard-background, 30%);
    word-break: break-word;
  }

  // Text highlighting marker
  mark {
    margin: 0 0.25em;
    padding: 0.0625em 0;
    border-radius: px2rem(2px);
    background-color: transparentize($clr-yellow-500, 0.5);
    box-shadow:
      +0.25em 0 0 transparentize($clr-yellow-500, 0.5),
      -0.25em 0 0 transparentize($clr-yellow-500, 0.5);
    word-break: break-word;
    box-decoration-break: clone;
  }

  // Abbreviations
  abbr {
    border-bottom: px2rem(1px) dotted $md-color-black--light;
    text-decoration: none;
    cursor: help;
  }

  // Small text
  small {
    opacity: 0.75;
  }

  // Superscript and subscript
  sup,
  sub {
    margin-left: 0.0625em * 1 / 0.8;

    // Adjust for RTL languages
    [dir="rtl"] & {
      margin-right: 0.0625em * 1 / 0.8;
      margin-left: initial;
    }
  }

  // Blockquotes, possibly nested
  blockquote {
    padding-left: px2rem(12px);
    border-left: px2rem(4px) solid $md-color-black--lighter;
    color: $md-color-black--light;

    // Adjust for RTL languages
    [dir="rtl"] & {
      padding-right: px2rem(12px);
      padding-left: initial;
      border-right: px2rem(4px) solid $md-color-black--lighter;
      border-left: initial;
    }
  }

  // Unordered lists
  ul {
    list-style-type: disc;
  }

  // Unordered and ordered lists
  ul,
  ol {
    margin-left: 0.625em;
    padding: 0;

    // Adjust for RTL languages
    [dir="rtl"] & {
      margin-right: 0.625em;
      margin-left: initial;
    }

    // Nested ordered lists
    ol {
      list-style-type: lower-alpha;

      // Triply nested ordered list
      ol {
        list-style-type: lower-roman;
      }
    }

    // List elements
    li {
      margin-bottom: 0.5em;
      margin-left: 1.25em;

      // Adjust for RTL languages
      [dir="rtl"] & {
        margin-right: 1.25em;
        margin-left: initial;
      }

      // Decrease vertical spacing
      p,
      blockquote {
        margin: 0.5em 0;
      }

      // Remove margin on last element
      &:last-child {
        margin-bottom: 0;
      }

      // Nested lists
      ul,
      ol {
        margin: 0.5em 0 0.5em 0.625em;

        // Adjust for RTL languages
        [dir="rtl"] & {
          margin-right: 0.625em;
          margin-left: initial;
        }
      }
    }
  }

  // Definition lists
  dd {
    margin: 1em 0 1em 1.875em;

    // Adjust for RTL languages
    [dir="rtl"] & {
      margin-right: 1.875em;
      margin-left: initial;
    }
  }

  // Limit width to container
  iframe,
  img,
  svg {
    max-width: 100%;
  }

  // Data tables
  table:not([class]) {
    @include z-depth(2);

    display: inline-block;
    max-width: 100%;
    border-radius: px2rem(2px);
    font-size: ms(-1);
    overflow: auto;
    -webkit-overflow-scrolling: touch;

    // Due to margin collapse because of the necessary inline-block hack, we
    // cannot increase the bottom margin on the table, so we just increase the
    // top margin on the following element
    & + * {
      margin-top: 1.5em;
    }

    // Table headings and cells
    th:not([align]),
    td:not([align]) {
      text-align: left;

      // Adjust for RTL languages
      [dir="rtl"] & {
        text-align: right;
      }
    }

    // Table headings
    th {
      min-width: px2rem(100px);
      padding: px2rem(12px) px2rem(16px);
      background-color: $md-color-black--light;
      color: $md-color-white;
      vertical-align: top;
    }

    // Table cells
    td {
      padding: px2rem(12px) px2rem(16px);
      border-top: px2rem(1px) solid $md-color-black--lightest;
      vertical-align: top;
    }

    // Table rows
    tr {
      transition: background-color 0.125s;

      // Add background on hover
      &:hover {
        background-color: rgba(0, 0, 0, 0.035);
        box-shadow: 0 px2rem(1px)  0 $md-color-white inset;
      }

      // Remove top border on first row
      &:first-child td {
        border-top: 0;
      }
    }


    // Do not wrap links in tables
    a {
      word-break: normal;
    }
  }

  // Wrapper for scrolling on overflow
  &__scrollwrap {
    margin: 1em px2rem(-16px);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  // Data table wrapper, in case JavaScript is available
  .md-typeset__table {
    display: inline-block;
    margin-bottom: 0.5em;
    padding: 0 px2rem(16px);

    // Data tables
    table {
      display: table;
      width: 100%;
      margin: 0;
      overflow: hidden;
    }
  }
}
