////
/// 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: syntax highlighting
// ----------------------------------------------------------------------------

// Codehilite extension
.codehilite {

  .o,   // Operator
  .ow { // Operator, word
    color: var(--md-code-hl-operator-color);
  }

  .p {  // Punctuation
    color: var(--md-code-hl-punctuation-color);
  }

  .cpf, // Comment, preprocessor file
  .l,   // Literal
  .s,   // Literal, string
  .sb,  // Literal, string backticks
  .sc,  // Literal, string char
  .s2,  // Literal, string double
  .si,  // Literal, string interpol
  .s1,  // Literal, string single
  .ss { // Literal, string symbol
    color: var(--md-code-hl-string-color);
  }

  .cp,  // Comment, pre-processor
  .se,  // Literal, string escape
  .sh,  // Literal, string heredoc
  .sr,  // Literal, string regex
  .sx { // Literal, string other
    color: var(--md-code-hl-special-color);
  }

  .m,   // Number
  .mf,  // Number, float
  .mh,  // Number, hex
  .mi,  // Number, integer
  .il,  // Number, integer long
  .mo { // Number, octal
    color: var(--md-code-hl-number-color);
  }

  .k,   // Keyword,
  .kd,  // Keyword, declaration
  .kn,  // Keyword, namespace
  .kp,  // Keyword, pseudo
  .kr,  // Keyword, reserved
  .kt { // Keyword, type
    color: var(--md-code-hl-keyword-color);
  }

  .kc,  // Keyword, constant
  .n {  // Name
    color: var(--md-code-hl-name-color);
  }

  .no,  // Name, constant
  .nb,  // Name, builtin
  .bp { // Name, builtin pseudo
    color: var(--md-code-hl-constant-color);
  }

  .nc,  // Name, class
  .ne,  // Name, exception
  .nf,  // Name, function
  .nn { // Name, namespace
    color: var(--md-code-hl-function-color);
  }

  .nd,  // Name, decorator
  .ni,  // Name, entity
  .nl,  // Name, label
  .nt { // Name, tag
    color: var(--md-code-hl-keyword-color);
  }

  .c,   // Comment
  .cm,  // Comment, multiline
  .c1,  // Comment, single
  .ch,  // Comment, shebang
  .cs,  // Comment, special
  .sd { // Literal, string doc
    color: var(--md-code-hl-comment-color);
  }

  .na,  // Name, attribute
  .nv,  // Variable,
  .vc,  // Variable, class
  .vg,  // Variable, global
  .vi { // Variable, instance
    color: var(--md-code-hl-variable-color);
  }

  .ge,  // Generic, emph
  .gr,  // Generic, error
  .gh,  // Generic, heading
  .go,  // Generic, output
  .gp,  // Generic, prompt
  .gs,  // Generic, strong
  .gu,  // Generic, subheading
  .gt { // Generic, traceback
    color: var(--md-code-hl-generic-color);
  }

  .gd,  // Diff, delete
  .gi { // Diff, insert
    margin: 0 px2em(-2px);
    padding: 0 px2em(2px);
    border-radius: px2rem(2px);
  }

  .gd { // Diff, delete
    background-color: var(--md-typeset-del-color);
  }

  .gi { // Diff, insert
    background-color: var(--md-typeset-ins-color)
  }

  // Highlighted lines
  .hll {
    display: block;
    margin: 0 px2em(-16px, 13.6px);
    padding: 0 px2em(16px, 13.6px);
    background-color: var(--md-code-hl-color)
  }
}

// ----------------------------------------------------------------------------
// Rules: layout
// ----------------------------------------------------------------------------

// Block with line numbers
.codehilitetable {
  display: block;
  overflow: hidden;

  // Set table elements to block layout, because otherwise the whole flexbox
  // hacking won't work correctly
  tbody,
  td {
    display: block;
    padding: 0;
  }

  // We need to use flexbox layout, because otherwise it's not possible to
  // make the code container scroll while keeping the line numbers static
  tr {
    display: flex;
  }

  // The pre tags are nested inside a table, so we need to remove the
  // margin because it collapses below all the overflows
  pre {
    margin: 0;
  }

  // Disable user selection, so code can be easily copied without
  // accidentally also copying the line numbers
  .linenos {
    padding: px2em(10.5px, 13.6px) px2em(16px, 13.6px);
    padding-right: 0;
    font-size: px2em(13.6px);
    background-color: var(--md-code-bg-color);
    user-select: none;
  }

  // Add spacing to line number container
  .linenodiv {
    padding-right: px2em(8px, 13.6px);
    box-shadow: px2rem(-1px) 0 var(--md-default-fg-color--lighter) inset;

    // Reset spacings
    pre {
      color: var(--md-default-fg-color--light);
      text-align: right;
    }
  }

  // The table cell containing the code container wrapper and code should
  // stretch horizontally to the remaining space
  .code {
    flex: 1;
    overflow: hidden;
  }
}

// Scoped in typesetted content to match specificity of regular content
.md-typeset {

  // Block with line numbers
  .codehilitetable {
    margin: 1em 0;
    direction: ltr;
    border-radius: px2rem(2px);

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

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

    // Full-width container
    > .codehilite {
      margin: 1em px2rem(-16px);

      // Stretch highlighted lines
      .hll {
        margin: 0 px2rem(-16px);
        padding: 0 px2rem(16px);
      }

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

    // Full-width container on top-level
    > .codehilitetable {
      margin: 1em px2rem(-16px);
      border-radius: 0;

      // Stretch highlighted lines
      .hll {
        margin: 0 px2rem(-16px);
        padding: 0 px2rem(16px);
      }
    }
  }
}
