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

// ----------------------------------------------------------------------------
// 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: var(--md-typeset-color);
  font-feature-settings: "kern", "liga";
  font-family: -apple-system, BlinkMacSystemFont, Helvetica, Arial, sans-serif;
}

// Proportionally spaced fonts
code,
pre,
kbd {
  color: var(--md-typeset-color);
  font-feature-settings: "kern";
  font-family: SFMono-Regular, Consolas, Menlo, monospace;
}

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

// Icon definitions
:root {
  --md-typeset-table--ascending: svg-load("@mdi/svg/svg/arrow-down.svg");
  --md-typeset-table--descending: svg-load("@mdi/svg/svg/arrow-up.svg");
}

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

// Content that is typeset - if possible, all margins, paddings and font sizes
// should be set in ems, so nested blocks (e.g. admonitions) 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: px2rem(16px);
  line-height: 1.6;
  color-adjust: exact;

  // We'll use a smaller font-size for printing, so code examples don't break
  // too early, and 16px looks too big anyway.
  @media print {
    font-size: px2rem(13.6px);
  }

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

  // 1st level headline
  h1 {
    margin: 0 0 px2em(40px, 32px);
    color: var(--md-default-fg-color--light);
    font-weight: 300;
    font-size: px2em(32px);
    line-height: 1.3;
    letter-spacing: -0.01em;
  }

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

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

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

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

  // 5th and 6th level headline
  h5,
  h6 {
    margin: px2em(16px, 12.8px) 0;
    color: var(--md-default-fg-color--light);
    font-weight: 700;
    font-size: px2em(12.8px);
    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 var(--md-default-fg-color--lighter);
  }

  // Links
  a {
    color: var(--md-typeset-a-color);
    word-break: break-word;

    // Also enable color transition on pseudo elements
    &,
    &::before {
      transition: color 125ms;
    }

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

  // Code blocks
  code,
  pre,
  kbd {
    color: var(--md-code-fg-color);
    direction: ltr;

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

  // Inline code blocks
  code {
    padding: 0 px2em(4px, 13.6px);
    font-size: px2em(13.6px);
    word-break: break-word;
    background-color: var(--md-code-bg-color);
    border-radius: px2rem(2px);
    box-decoration-break: clone;
  }

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

  // Ensure link color in code blocks
  a > code {
    color: currentColor;
  }

  // Unformatted code blocks
  pre {
    position: relative;
    margin: 1em 0;
    line-height: 1.4;

    // Actual container with code, overflowing
    > code {
      display: block;
      margin: 0;
      padding: px2em(10.5px, 13.6px) px2em(16px, 13.6px);
      overflow: auto;
      word-break: normal;
      box-shadow: none;
      box-decoration-break: slice;
      touch-action: auto;
      // Override Firefox scrollbar style
      scrollbar-width: thin;
      scrollbar-color: var(--md-default-fg-color--lighter) transparent;

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

  // [mobile -]: Stretch to whole width
  @include break-to-device(mobile) {

    // Stretch top-level containers
    > pre {
      margin: 1em px2rem(-16px);

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

  // Keyboard key
  kbd {
    display: inline-block;
    padding: 0 px2em(8px, 12px);
    color: var(--md-default-fg-color);
    font-size: px2em(12px);
    vertical-align: text-top;
    word-break: break-word;
    background-color: var(--md-typeset-kbd-color);
    border-radius: px2rem(2px);
    box-shadow:
      0 px2rem(2px)  0 px2rem(1px) var(--md-typeset-kbd-border-color),
      0 px2rem(2px)  0             var(--md-typeset-kbd-border-color),
      0 px2rem(-2px) px2rem(4px)   var(--md-typeset-kbd-accent-color) inset;
  }

  // Text highlighting marker
  mark {
    color: inherit;
    word-break: break-word;
    background-color: var(--md-typeset-mark-color);
    box-decoration-break: clone;
  }

  // Abbreviations
  abbr {
    text-decoration: none;
    border-bottom: px2rem(1px) dotted var(--md-default-fg-color--light);
    cursor: help;

    // Render a tooltip for touch devices
    @media (hover: none) {
      position: relative;

      // Tooltip
      &[title]:focus::after,
      &[title]:hover::after {
        @include z-depth(2);

        position: absolute;
        left: 0;
        display: inline-block;
        width: auto;
        min-width: max-content;
        max-width: 80%;
        margin-top: 2em;
        padding: px2rem(4px) px2rem(6px);
        color: var(--md-default-bg-color);
        font-size: px2rem(14px);
        background: var(--md-default-fg-color);
        border-radius: px2rem(2px);
        content: attr(title);
      }
    }

  }

  // Small text
  small {
    opacity: 0.75;
  }

  // Superscript and subscript
  sup,
  sub {
    margin-left: px2em(1px, 12.8px);

    // Adjust for right-to-left languages
    [dir="rtl"] & {
      margin-right: px2em(1px, 12.8px);
      margin-left: initial;
    }
  }

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

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

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

  // Unordered and ordered lists
  ul,
  ol {
    margin-left: px2em(10px);
    padding: 0;

    // Adjust for right-to-left languages
    [dir="rtl"] & {
      margin-right: px2em(10px);
      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: px2em(20px);

      // Adjust for right-to-left languages
      [dir="rtl"] & {
        margin-right: px2em(20px);
        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 px2em(10px);

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

  // Definition lists
  dd {
    margin: 1em 0 1.5em px2em(30px);

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

  // Limit width to container, scale height proportionally
  img,
  svg {
    max-width: 100%;
    height: auto;

    // Left-aligned
    &[align="left"] {
      margin: 1em;
      margin-left: 0;
    }

    // Right-aligned
    &[align="right"] {
      margin: 1em;
      margin-right: 0;
    }

    // Remove top spacing of sole children
    &[align]:only-child {
      margin-top: 0;
    }
  }

  // Figures
  figure {
    width: fit-content;
    max-width: 100%;
    margin: 0 auto;
    text-align: center;
  }

  // Figure captions
  figcaption {
    max-width: px2rem(480px);
    margin: 0.5em auto 2em;
    font-style: italic;
  }

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

  // Data tables
  table:not([class]) {
    display: inline-block;
    max-width: 100%;
    overflow: auto;
    font-size: px2rem(12.8px);
    background: var(--md-default-bg-color);
    border-radius: px2rem(2px);
    box-shadow:
      0 px2rem(4px) px2rem(10px) hsla(0, 0%, 0%, 0.05),
      0 0           px2rem(1px)  hsla(0, 0%, 0%, 0.1);
    touch-action: auto;

    // Reset display mode so table header wraps correctly when printing
    @media print {
      display: table;
    }

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

    // Elements inside cells
    th > *,
    td > * {

      // Remove top spacing of first child
      &:first-child {
        margin-top: 0;
      }

      // Remove bottom spacing of last child
      &:last-child {
        margin-bottom: 0;
      }
    }

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

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

    // Table headings
    th {
      min-width: px2rem(100px);
      padding: px2em(12px, 12.8px) px2em(16px, 12.8px);
      color: var(--md-default-bg-color);
      vertical-align: top;
      background-color: var(--md-default-fg-color--light);

      // Links in table headings
      a {
        color: inherit;
      }
    }

    // Table cells
    td {
      padding: px2em(12px, 12.8px) px2em(16px, 12.8px);
      vertical-align: top;
      border-top: px2rem(1px) solid var(--md-default-fg-color--lightest);
    }

    // Table rows
    tr {
      transition: background-color 125ms;

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

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

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

  // Sortable tables
  table th[role="columnheader"] {
    cursor: pointer;

    // Sort icon
    &::after {
      display: inline-block;
      width: 1.2em;
      height: 1.2em;
      margin-left: 0.5em;
      vertical-align: sub;
      mask-repeat: no-repeat;
      content: " ";
    }

    // Sort ascending
    &[aria-sort="ascending"]::after {
      background-color: currentColor;
      mask-image: var(--md-typeset-table--ascending);
    }

    // Sort descending
    &[aria-sort="descending"]::after {
      background-color: currentColor;
      mask-image: var(--md-typeset-table--descending);
    }
  }

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

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

    // Reset display mode so table header wraps correctly when printing
    @media print {
      display: block;
    }

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