@layer components {
  ::highlight(we-code-comment) {
    color: var(--color-syntax-comment);
  }

  ::highlight(we-code-keyword) {
    color: var(--color-charcoal);
  }

  ::highlight(we-code-number) {
    color: var(--color-syntax-number);
  }

  ::highlight(we-code-string) {
    color: var(--color-syntax-string);
  }

  ::highlight(we-code-function) {
    color: var(--color-syntax-title);
  }

  ::highlight(we-code-tag) {
    color: var(--color-syntax-tag);
  }

  ::highlight(we-code-property) {
    color: var(--color-syntax-number);
  }

  ::highlight(we-code-operator) {
    color: var(--color-muted);
  }

  ::highlight(we-code-deleted) {
    color: var(--color-syntax-title);
    background-color: var(--color-syntax-deletion);
  }

  ::highlight(we-code-inserted) {
    color: var(--color-green-deep);
    background-color: var(--color-syntax-addition);
  }

  pre:has(> :is(.code-lines, .code-tools)) {
    position: relative;
    overflow: visible;

    & > code {
      overflow-x: auto;
    }

    &.wrapped > code {
      white-space: pre-wrap;
      overflow-wrap: anywhere;
    }

    &.wrapped > .code-lines {
      display: none;
    }
  }

  pre:has(> .code-tools) {
    @media (hover: none) {
      padding-block-start: calc(var(--space-8) + 32px + var(--space-4));
    }
  }

  .code-tools {
    position: absolute;
    inset-block-start: var(--space-8);
    inset-inline-end: var(--space-8);
    display: flex;
    gap: var(--space-4);
    border-radius: var(--radius-control);
    user-select: none;

    @media (hover: hover) {
      background-color: color-mix(in oklch, var(--color-code) 92%, transparent);

      pre:not(:hover, :focus-within) > & {
        opacity: 0;
      }

      @media (prefers-reduced-motion: no-preference) {
        pre:is(:hover, :focus-within) > & {
          transition: opacity var(--transition-state);
        }
      }
    }
  }

  pre:not(.overflow, .wrapped) > .code-tools > .code-wrap {
    display: none;
  }

  .code-wrap {
    --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23000' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M2 3.5h12M2 8h9.5a2.25 2.25 0 0 1 0 4.5H8M2 12.5h3'/%3E%3Cpath d='m9.5 10.5-2 2 2 2'/%3E%3C/svg%3E");
  }

  .code-lines {
    display: none;

    @media (width > 600px) {
      position: absolute;
      inset-block-start: var(--space-16);
      inset-inline-end: calc(100% + var(--space-12));
      display: flex;
      flex-direction: column;
      color: var(--color-line);
      font-variant-numeric: tabular-nums;
      line-height: var(--leading-code);
      text-align: end;
      white-space: nowrap;
      counter-reset: line;
      pointer-events: none;
      user-select: none;

      & > span::before {
        content: counter(line, decimal-leading-zero);
        counter-increment: line;
      }
    }
  }
}
