/*
 * Code blocks of the research site: the editor frame around code coloured at build time (highlighter.mjs).
 *
 * Ground and token colours are the docs code theme oraclizer-dark (Web/Docs/src/lib/highlight.ts): the night
 * ground (--codeblock-bg, #0b111b on light pages, #05080d on dark pages) and the theme's token colours,
 * written inline by the build. The frame follows the docs code block (hairline border, mono header with the
 * language, outlined Copy button) and adds the editor parts: line numbers that stay put while the code
 * scrolls sideways, the row under the pointer lit, marked rows, a visible keyboard focus.
 *
 * The block is dark on both page themes. Load after research.css and features/site/theme.css.
 * Markup (from highlighter.mjs renderBlock, or built by code.js around an older block):
 *   <div class="orz-code code-block" data-lang data-kind="code|plain" [data-numbers] [data-highlight]>
 *     <div class="orz-code__bar" data-orz-ui> language, file name, Copy button </div>
 *     <pre class="shiki" tabindex="0"><code><span class="line">...</span>\n...</code></pre>
 *   </div>
 */

.orz-code {
  --orz-code-bg: var(--codeblock-bg, #0b111b);
  --orz-code-rule: var(--code-line, #1b2638);
  --orz-code-text: #d7dfeb;
  --orz-code-chrome: #9aa8bd;
  --orz-code-gutter: #71819a;
  --orz-code-gutter-lit: #d7dfeb;
  /* Row tints stay low enough that the dimmest token colour (comments, #71819a) keeps 4.5:1. */
  --orz-code-hover: rgba(126, 166, 255, 0.04);
  --orz-code-hover-edge: rgba(126, 166, 255, 0.2);
  --orz-code-mark: rgba(126, 166, 255, 0.04);
  --orz-code-mark-bar: #7ea6ff;
  /* The number cell holds only the bright line number, so it can take a stronger tint. */
  --orz-code-gutter-hover: rgba(126, 166, 255, 0.12);
  --orz-code-gutter-mark: rgba(126, 166, 255, 0.16);
  --orz-code-focus: var(--cyan, #5bdcf3);
  --orz-code-size: 13.5px;
  --orz-code-leading: 1.7;
  position: relative;
  max-width: 100%;
  margin: 0 0 24px;
  overflow: hidden;
  border: 1px solid var(--orz-code-rule);
  background: var(--orz-code-bg);
  color: var(--orz-code-text);
}
html[data-theme="dark"] .orz-code {
  --orz-code-hover: rgba(126, 166, 255, 0.08);
  --orz-code-mark: rgba(126, 166, 255, 0.08);
}
/* research.css draws a language strip before .code-block; the bar below replaces it. */
.orz-code.code-block::before {
  content: none !important;
  display: none !important;
}

/* ---------- header bar ---------- */

.orz-code__bar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 5px 6px 5px 14px;
  border-bottom: 1px solid var(--orz-code-rule);
  background: rgba(221, 235, 255, 0.025);
  color: var(--orz-code-chrome);
  font-family: var(--mono, ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace);
  font-size: 12px;
  line-height: 1.4;
}
.orz-code__lang {
  flex: none;
  white-space: nowrap;
  letter-spacing: 0.02em;
}
.orz-code__file {
  min-width: 0;
  overflow: hidden;
  color: #c7d4ea;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.orz-code__lang--tag {
  padding: 1px 6px;
  border: 1px solid var(--orz-code-rule);
  font-size: 11px;
}
.orz-code__copy {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  margin-left: auto;
  padding: 0 10px;
  border: 1px solid #2a3850;
  border-radius: 0;
  background: transparent;
  color: #b9c5d8;
  font-family: var(--body, "Manrope", system-ui, sans-serif);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
  cursor: pointer;
  transition: border-color 0.16s, color 0.16s;
}
.orz-code__copy:hover {
  border-color: var(--blue-soft, #5b7bfb);
  color: #fff;
}
.orz-code__copy:focus-visible {
  outline: 2px solid var(--orz-code-focus);
  outline-offset: 2px;
}
.orz-code__copy[data-state="copied"] {
  border-color: rgba(91, 220, 243, 0.55);
  color: var(--cyan, #5bdcf3);
}
.orz-code__copy[data-state="failed"] {
  border-color: rgba(242, 201, 143, 0.55);
  color: #f2c98f;
}
.orz-code__icon {
  flex: none;
  width: 14px;
  height: 14px;
}
.orz-code__icon--done,
.orz-code__copy[data-state="copied"] .orz-code__icon--copy {
  display: none;
}
.orz-code__copy[data-state="copied"] .orz-code__icon--done {
  display: block;
}

/* ---------- code area ---------- */

.orz-code.code-block > pre,
.orz-code > pre {
  margin: 0;
  padding: 14px 0 16px;
  overflow-x: auto;
  overflow-y: hidden;
  background: transparent !important;
  color: var(--orz-code-text);
  font-family: var(--mono, ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace);
  font-size: var(--orz-code-size);
  line-height: var(--orz-code-leading);
  white-space: pre;
  tab-size: 4;
  scrollbar-color: #2a3850 transparent;
  scrollbar-width: thin;
}
.orz-code > pre:focus {
  outline: none;
}
.orz-code > pre:focus-visible {
  outline: 2px solid var(--orz-code-focus);
  outline-offset: -2px;
}
.orz-code > pre > code {
  display: block;
  width: max-content;
  min-width: 100%;
  font: inherit;
  counter-reset: orz-line var(--orz-code-start, 0);
}
.orz-code .line {
  display: inline-block;
  box-sizing: border-box;
  min-width: 100%;
  padding: 0 20px 0 18px;
  vertical-align: top;
}
.orz-code:not([data-numbers]) .line:empty::before {
  content: "\200b";
}

/* Line numbers: drawn by CSS, so they are never selected, copied or read as code. They stay at the left edge
 * while long lines scroll sideways. */
.orz-code[data-numbers] .line {
  padding-left: 0;
}
/* The gutter rule also lives on the scroll area itself (it does not move with the code), so it stays whole
 * where Chrome skips painting a row's number cell (rows whose code is scrolled out of view). It sits exactly
 * under the number cells' own rule. */
.orz-code.code-block[data-numbers] > pre,
.orz-code[data-numbers] > pre {
  --orz-code-gutter-pad: 26px;
  background: linear-gradient(var(--orz-code-rule), var(--orz-code-rule)) no-repeat calc(var(--orz-code-gutter-pad) + var(--orz-code-digits, 2) * 1ch - 1px) 0 / 1px 100% !important;
}
.orz-code[data-numbers] .line::before {
  content: counter(orz-line);
  counter-increment: orz-line;
  position: sticky;
  left: 0;
  display: inline-block;
  box-sizing: content-box;
  width: calc(var(--orz-code-digits, 2) * 1ch);
  margin-right: 16px;
  padding: 0 12px 0 14px;
  background: var(--orz-code-bg);
  box-shadow: inset -1px 0 0 var(--orz-code-rule);
  color: var(--orz-code-gutter);
  text-align: right;
  -webkit-user-select: none;
  user-select: none;
}

/* The row under the pointer lights up (code only; diagrams and tool output stay still). */
@media (hover: hover) {
  .orz-code[data-kind="code"] .line:hover {
    background-color: var(--orz-code-hover);
    box-shadow: inset 0 1px 0 var(--orz-code-hover-edge), inset 0 -1px 0 var(--orz-code-hover-edge);
  }
  .orz-code[data-kind="code"]:not([data-numbers]) .line:hover {
    box-shadow: inset 2px 0 0 var(--orz-code-mark-bar), inset 0 1px 0 var(--orz-code-hover-edge), inset 0 -1px 0 var(--orz-code-hover-edge);
  }
  .orz-code[data-numbers] .line:hover::before {
    background: linear-gradient(var(--orz-code-gutter-hover), var(--orz-code-gutter-hover)) var(--orz-code-bg);
    box-shadow: inset 2px 0 0 var(--orz-code-mark-bar), inset -1px 0 0 var(--orz-code-rule), inset 0 1px 0 var(--orz-code-hover-edge), inset 0 -1px 0 var(--orz-code-hover-edge);
    color: var(--orz-code-gutter-lit);
  }
}

/* Marked rows (data-highlight="3" or "2,5-7"). */
.orz-code .line.is-marked {
  background-color: var(--orz-code-mark);
  box-shadow: inset 2px 0 0 var(--orz-code-mark-bar);
}
.orz-code[data-numbers] .line.is-marked {
  box-shadow: none;
}
.orz-code[data-numbers] .line.is-marked::before {
  background: linear-gradient(var(--orz-code-gutter-mark), var(--orz-code-gutter-mark)) var(--orz-code-bg);
  box-shadow: inset 2px 0 0 var(--orz-code-mark-bar), inset -1px 0 0 var(--orz-code-rule);
  color: var(--orz-code-gutter-lit);
}

/* The status line read out after a copy (one per page, visually hidden). */
.orz-code-status {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (max-width: 640px) {
  .orz-code {
    --orz-code-size: 12.5px;
    margin-inline: 0;
  }
  .orz-code__bar {
    padding-left: 12px;
  }
  .orz-code__copy {
    min-height: 34px;
  }
  .orz-code .line {
    padding: 0 16px 0 14px;
  }
  .orz-code[data-numbers] .line::before {
    margin-right: 12px;
    padding: 0 10px 0 12px;
  }
  .orz-code.code-block[data-numbers] > pre,
  .orz-code[data-numbers] > pre {
    --orz-code-gutter-pad: 22px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .orz-code__copy {
    transition: none;
  }
}

/* Paper: light ground, dark text, long lines wrapped, no controls. */
@media print {
  .orz-code {
    border-color: #d5deeb !important;
    background: #f4f6fa !important;
    break-inside: avoid;
  }
  .orz-code__bar {
    border-color: #d5deeb !important;
    background: none !important;
    color: #4c596b !important;
  }
  .orz-code__file {
    color: #07101d !important;
  }
  .orz-code__copy {
    display: none !important;
  }
  .orz-code > pre,
  .orz-code.code-block[data-numbers] > pre {
    overflow: visible !important;
    background: none !important;
    color: #07101d !important;
    white-space: pre-wrap !important;
    overflow-wrap: anywhere;
  }
  .orz-code > pre > code {
    width: auto;
  }
  .orz-code pre span {
    color: #07101d !important;
  }
  .orz-code .line {
    background: none !important;
    box-shadow: none !important;
  }
  .orz-code[data-numbers] .line::before {
    position: static;
    background: none !important;
    box-shadow: none !important;
    color: #7a8698 !important;
  }
}
