@use "@sass-palette/hope-config";

.theme-hope-content {
  /* Code blocks */
  pre {
    overflow: auto;

    margin: 0.85rem 0;
    padding: 1rem;
    border-radius: 6px;

    // WARNING: Safari only support integer pixels for `<pre>` tag line-height
    // so we override this into a integer
    line-height: 1.375;

    code {
      padding: 0;
      border-radius: 0;

      background: transparent !important;
      color: var(--code-color);

      font-family: var(--font-family-mono);
      text-align: left;
      white-space: pre;
      word-spacing: normal;
      word-wrap: normal;
      word-break: normal;
      overflow-wrap: unset;
      -webkit-hyphens: none;
      -moz-hyphens: none;
      hyphens: none;

      transition: color var(--color-transition);

      -webkit-font-smoothing: auto;
      -moz-osx-font-smoothing: auto;

      @media print {
        white-space: pre-wrap;
      }
    }
  }

  .line-number {
    font-family: var(--font-family-mono);
  }
}

div[class*="language-"] {
  position: relative;
  border-radius: 6px;
  background: var(--code-bg-color);
  transition: background var(--color-transition);

  // narrow mobile
  @media (max-width: hope-config.$mobile) {
    .theme-hope-content > & {
      margin: 0.85rem -1.5rem;
      border-radius: 0;
    }
  }

  &::before {
    content: attr(data-ext);

    position: absolute;
    top: 0;
    right: 1em;
    z-index: 3;

    color: var(--code-line-color);

    font-size: 0.75rem;

    transition: color var(--color-transition);
  }

  pre {
    position: relative;
    z-index: 1;
  }

  .highlight-lines {
    position: absolute;
    top: 0;
    left: 0;

    width: 100%;
    padding-top: 1rem;

    line-height: 1.375;

    user-select: none;
  }

  .highlight-line {
    background: var(--code-highlight-line-color);
    transition: background var(--color-transition);
  }

  &.line-numbers-mode {
    &::after {
      content: "";

      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;

      width: var(--line-numbers-width);
      height: 100%;
      border-right: 1px solid var(--code-highlight-line-color);
      border-radius: 6px 0 0 6px;

      transition: border-color var(--color-transition);

      @media (max-width: hope-config.$mobile) {
        border-radius: 0;
      }

      @media print {
        display: none;
      }
    }

    .highlight-line {
      position: relative;

      &::before {
        content: " ";

        position: absolute;
        top: 0;
        left: 0;
        z-index: 3;

        display: block;

        width: var(--line-numbers-width);
        height: 100%;
      }
    }

    pre {
      vertical-align: middle;
      margin-left: var(--line-numbers-width);
      padding-left: 0.5rem;

      @media print {
        margin-left: 0;
        padding-left: 1rem;
      }
    }

    .line-numbers {
      position: absolute;
      top: 0;
      left: 0;

      width: var(--line-numbers-width);
      padding: 1rem 0;

      color: var(--code-line-color);

      // WARNING: Safari only support integer pixels for `<pre>` tag line-height
      // so we override this into a integer
      line-height: 1.375;
      counter-reset: line-number;
      text-align: center;

      transition: color var(--color-transition);

      // This is a hacky fix to align line number with code line
      transform: translateY(1px);

      @media print {
        display: none;
      }
    }

    .line-number {
      position: relative;
      z-index: 4;

      // WARNING: Safari only support integer pixels for `<pre>` tag line-height
      // so we override this into a integer
      height: 1.375em;
      user-select: none;

      &::before {
        content: counter(line-number);
        font-size: 0.85em;
        counter-increment: line-number;
      }
    }
  }

  &:not(.line-numbers-mode) {
    .line-numbers {
      display: none;
    }
  }
}
