@use 'sass:color';

@use '../core/colors' as *;
@use '../core/variables' as *;
@use '../core/bootstrap';

pre {
  margin-bottom: 1rem;
  font-size: 0.9375rem;
  font-weight: bootstrap.$font-weight-normal;
  padding: 1.25rem;

  a {
    font-family: inherit;
    font-weight: inherit;
  }
  
  .highlight {
    background: $flutter-color-yellow-300;
    padding: 0.1em;
  }

  .highlight-diagnostics & {
    .highlight {
      border-bottom: 2px $red-500 dashed;
      background: inherit;
      padding: 0;
    }
  }
  
  &:focus {
    outline: none;
  }
  
  span.line {
    padding-left: 1.25rem;
    padding-right: 1.25rem;
    width: 100%;

    &.highlighted-line {
      background: color.change($brand-primary, $alpha: 0.1);
    }
  }
  
  &.show-line-numbers code {
    span.line {
      padding-left: 0.5rem;
      
      &[data-line]::before {
        display: inline-block;
        content: attr(data-line) "";
        width: 2em;
        margin-right: 0.5rem;
        text-align: right;
        color: $dash-sub-grey;
      }
    }
  }
}

.code-copy-button {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 10;

  width: 2rem;
  height: 2rem;
  padding: 0;

  font: $site-font-icon;

  appearance: none;
  border: none;
  color: $site-color-nav-links;
  background: none;
  opacity: 0;
  transition: opacity 0.4s;

  &:hover, &:focus {
    color: $site-color-primary;
    opacity: 1;
  }

  &:active {
    color: $flutter-color-blue-700;
  }
}

.code-block-language {
  font-family: $font-family-monospace;
  user-select: none;
  font-size: 0.8125rem;
  color: $site-color-nav-links;
  opacity: 1;
  transition: opacity 0.25s;

  position: absolute;
  top: 3px;
  right: 6px;
  z-index: 5;

  .highlight-languages .language-dart & {
    color: $brand-primary;
  }

  .highlight-languages .language-js & {
    color: #f1a85a;
  }

  .highlight-languages .language-swift & {
    color: #f05137;
  }

  .highlight-languages :not(.has-tag) & {
    font-size: 0.875rem;
    left: 6px;
    right: unset;
  }
}

.code-block-tag {
  font-family: $font-family-monospace;
  font-size: 0.8125rem;
  font-weight: 500;
  position: absolute;
  top: 3px;
  left: 6px;

  .tag-good &, .tag-passes-sa &, .tag-runtime-success & {
    color: $alert-success-fg;
  }

  .tag-bad &, .tag-fails-sa &, .tag-runtime-fail & {
    color: $alert-danger-fg;
  }
}

.code-block-wrapper {
  margin-bottom: 1rem;
  border: 1px solid $flutter-color-grey-500;
  
  .code-block-header {
    background-color: #e9ecef;
    border-bottom: 1px solid $flutter-color-grey-500;
    font-size: 0.9375rem;
    font-weight: 500;
    padding: 0.75rem 0.5rem 0.67rem 1rem;
  }
  
  .code-block-body {
    position: relative;
    background-color: $site-color-codeblock-bg;

    &:hover, &:focus-within {
      .code-block-language {
        opacity: 0;
      }

      .code-copy-button {
        opacity: 1;
      }
    }

    &.tag-good, &.tag-passes-sa, &.tag-runtime-success {
      background-color: $alert-success-bg;
    }

    &.tag-bad, &.tag-fails-sa, &.tag-runtime-fail {
      background-color: $alert-danger-bg;
    }
  }
  
  &:has(:focus-visible) {
    border-color: #1389FD;
  }
  
  pre {
    margin-bottom: 0;
    padding-right: 0;
    padding-left: 0;
    
    &:not([lang="console"]) {
      line-height: 1.8;
    }
  }
}

// Border and resizability for dartpad
iframe[src^="https://dartpad"]:not(#try-dart-pad) {
  border: 1px solid #ccc;
  margin-bottom: 1rem;
  min-height: 400px;
  resize: vertical;
  width: 100%;
}
