/* Code blocks with a copy button (design pack part 10, brand.toml [ui] code_copy, default off).
   Markup: app/codeblock.py (figure) and app/templates/_code.html (code_block). Script: code-copy.js adds the button.
   Docs: docs/ui-switches.md. Glow family: a card with a 1px top hairline, a bar with the file name and the
   language, the code on an inset well. Colours come from the site's own tokens (admin: --card --ink --muted
   --hairline; public: --surface --ink --ink-soft --border) with the amarbaro dark values as the last fallback.
   Token colours are the accent pulled toward the text colour, so they keep 5:1 on the well in a light and a dark theme.
   Only opacity changes between labels (150 ms); with reduced motion the label swaps at once. */
.code {
  --c: var(--stat-blue, #4FA3D9);
  --cb-card: var(--card, var(--surface, #0E131B));
  --cb-ink: var(--ink, #E6EAF0);
  --cb-mut: var(--muted, var(--ink-soft, #8A94A3));
  --cb-line: var(--hairline, var(--border, #1C2430));
  --cb-well: color-mix(in srgb, var(--cb-ink) 5%, var(--cb-card));
  --cb-mono: var(--stat-mono, 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
  margin: 1.4em 0;
  color: var(--cb-ink);
  background: linear-gradient(180deg, color-mix(in oklab, var(--c) 10%, var(--cb-card)), var(--cb-card) 60%);
  border: 1px solid var(--cb-line);
  border-radius: 12px;
}
.code::before {
  content: "";
  position: absolute;
  left: 16px;
  right: 16px;
  top: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--c), transparent);
}
.code__bar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  padding: 4px 4px 4px 16px;
  border-bottom: 1px solid var(--cb-line);
}
.code__title { font: 600 13px/1.2 var(--cb-mono); color: var(--cb-ink); overflow-wrap: anywhere; }
.code__lang { font: 600 12px/1.2 var(--cb-mono); letter-spacing: .14em; text-transform: uppercase; color: var(--cb-mut); }
.code__live { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

.code__copy {
  display: inline-grid;
  place-items: center;
  box-sizing: border-box;
  min-width: 104px; /* room for "Copied" so the bar never jumps */
  min-height: 44px;
  margin: 0 0 0 auto;
  padding: 0 14px;
  font: 600 13px/1 var(--cb-mono);
  color: var(--cb-ink);
  background: color-mix(in oklab, var(--c) 14%, var(--cb-card));
  border: 1px solid color-mix(in oklab, var(--c) 42%, var(--cb-line));
  border-radius: 8px;
  cursor: pointer;
}
.code__copy:hover { border-color: color-mix(in oklab, var(--c) 70%, var(--cb-line)); }
.code__copy:focus-visible {
  outline: 2px solid var(--c);
  outline-offset: 2px;
  box-shadow: 0 0 0 6px color-mix(in oklab, var(--c) 24%, transparent);
}
.code__l { grid-area: 1 / 1; display: inline-flex; align-items: center; gap: 6px; }
.code__l svg { width: 14px; height: 14px; }
.code__l--b { opacity: 0; }
.code__copy[data-state="copied"] .code__l--a,
.code__copy[data-state="failed"] .code__l--a { opacity: 0; }
.code__copy[data-state="copied"] .code__l--b,
.code__copy[data-state="failed"] .code__l--b { opacity: 1; }

.code__pre {
  margin: 0;
  padding: 18px;
  overflow-x: auto;
  font: 400 13.5px/1.7 var(--cb-mono);
  tab-size: 2;
  color: var(--cb-ink);
  background-color: var(--cb-well);
  /* scroll cue: a fade at the edge that still has code behind it (no JS) */
  background-image:
    linear-gradient(to right, var(--cb-well) 30%, transparent),
    linear-gradient(to left, var(--cb-well) 30%, transparent),
    radial-gradient(farthest-side at 0 50%, color-mix(in srgb, var(--cb-ink) 22%, transparent), transparent),
    radial-gradient(farthest-side at 100% 50%, color-mix(in srgb, var(--cb-ink) 22%, transparent), transparent);
  background-position: left center, right center, left center, right center;
  background-repeat: no-repeat;
  background-size: 28px 100%, 28px 100%, 12px 100%, 12px 100%;
  background-attachment: local, local, scroll, scroll;
}
.code__pre code { font: inherit; background: none; padding: 0; color: inherit; white-space: pre; }
.code__pre:focus-visible { outline: 2px solid var(--c); outline-offset: -2px; }

.tk-k { color: color-mix(in srgb, var(--stat-olive, #9DAE51) 50%, var(--cb-ink)); }
.tk-s { color: color-mix(in srgb, var(--stat-gold, #C9A445) 50%, var(--cb-ink)); }
.tk-l { color: color-mix(in srgb, var(--stat-violet, #A48BE0) 50%, var(--cb-ink)); }
.tk-h { color: color-mix(in srgb, var(--stat-blue, #4FA3D9) 50%, var(--cb-ink)); font-weight: 600; }
.tk-c { color: var(--cb-mut); font-style: italic; }

@media (max-width: 480px) {
  .code__pre { padding: 14px; font-size: 13px; }
}
@media (forced-colors: active) {
  .code { border-color: CanvasText; }
  .code__copy { border-color: ButtonText; }
}
@media (prefers-reduced-motion: no-preference) {
  .code__l { transition: opacity var(--m-fast, 150ms) var(--m-out, cubic-bezier(.23, 1, .32, 1)); }
}
