.highlight.has-copy {
  position: relative;
  --code-copy-fg: #57606a;
}

/* Label and button float over the code's top-right corner, shown only while the
   block is hovered or focused; the code's own background fades in behind them. */
.code-copy-bar {
  position: absolute;
  top: 1px;
  right: 1px;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  /* Flush with the block's edge so no code shows past the bar; the spacing is
     inside, keeping the button clear of the border. */
  padding: 4px 6px 0 8px;
  /* Take the block's own background so no theme colour is duplicated here. */
  background: inherit;
  border-radius: 0 0 0 0.375rem;
  font-size: 0.8rem;
  /* Blockquotes are italic site-wide; the label should not inherit that. */
  font-style: normal;
  color: var(--code-copy-fg);
  opacity: 0;
  user-select: none;
  -webkit-user-select: none;
  /* Hidden bar must not swallow clicks or text selection on the code beneath it. */
  pointer-events: none;
}

.highlight.has-copy:hover .code-copy-bar,
.highlight.has-copy:has(:focus-visible) .code-copy-bar {
  opacity: 1;
  pointer-events: auto;
}

/* Soft fade where code runs under the bar. */
.code-copy-bar::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 100%;
  width: 1rem;
  background: inherit;
  -webkit-mask-image: linear-gradient(to right, transparent, #000);
  mask-image: linear-gradient(to right, transparent, #000);
  pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
  .code-copy-bar {
    transition: opacity 0.12s ease-in-out;
  }
}

/* No hover on touch screens: keep the bar visible. */
@media (hover: none), (pointer: coarse) {
  .code-copy-bar {
    opacity: 1;
    pointer-events: auto;
  }
}

.code-copy-lang {
  font-size: 0.75rem;
  opacity: 0.55;
  font-family: "Menlo", "Consolas", "Monaco", monospace;
}

.code-copy-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.5rem;
  min-height: 1.5rem;
  padding: 0.2rem;
  border: 0;
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.code-copy-btn:hover,
.code-copy-btn:focus-visible {
  background: rgba(128, 128, 128, 0.2);
}

.code-copy-btn.copied {
  color: #1a7f37;
}

.code-copy-btn.failed {
  color: #cf222e;
}

.code-copy-live {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (prefers-color-scheme: dark) {
  .highlight.has-copy {
    --code-copy-fg: #b0b0b0;
  }

  .code-copy-btn.copied {
    color: #56d364;
  }

  .code-copy-btn.failed {
    color: #ff7b72;
  }
}

@media (forced-colors: active) {
  .code-copy-btn {
    border: 1px solid ButtonText;
  }
}

@media print {
  .code-copy-bar,
  .code-copy-live {
    display: none;
  }
}
