/*
 * "Scholar Navy" — the site's own code highlighting theme.
 * Markup comes from /js/code-highlight.js (at build time for articles, in the browser for demos):
 *   <pre class="code-block code-lines"><code class="language-ts"><span class="tk tk-keyword">const</span> …</code></pre>
 * Line numbers live in code-lines.css.
 * Colors: the --syn-* tokens in style.css :root (also used by the demo code panel).
 */

/* ---------- block ---------- */
pre.code-block {
  direction: ltr;
  text-align: left;
  white-space: pre;
  word-spacing: normal;
  word-break: normal;
  overflow-wrap: normal;
  line-height: 1.6;
  tab-size: 4;
  hyphens: none;
  color: var(--syn-text);
  padding: 1.1em 1.25em;
  /* centered, hugging its longest line (like the diagrams); a long line scrolls inside the block */
  width: fit-content;
  min-width: min(100%, 24rem);
  max-width: 100%;
  margin: 1.5em auto;
  overflow: auto;
  background: var(--syn-bg);
  border: 1px solid var(--syn-border);
  border-radius: var(--radius-xl);
  box-shadow: 0 10px 30px -12px var(--color-shadow-dark), inset 0 1px 0 rgba(255, 255, 255, 0.03);
  /* thin Scholar's Blue scrollbar (DESIGN.md: Code Blocks) */
  scrollbar-width: thin;
  scrollbar-color: rgba(90, 169, 255, 0.35) transparent;
}

pre.code-block > code {
  color: inherit;
  background: none;
  white-space: inherit;
}

pre.code-block::-webkit-scrollbar { height: 8px; width: 8px; }
pre.code-block::-webkit-scrollbar-track { background: transparent; }
pre.code-block::-webkit-scrollbar-thumb { background: rgba(90, 169, 255, 0.3); border-radius: 999px; }
pre.code-block::-webkit-scrollbar-thumb:hover { background: rgba(90, 169, 255, 0.55); }

pre.code-block ::selection {
  background: var(--syn-selection);
}

/* ---------- tokens ---------- */
.tk-comment { color: var(--syn-comment); }
.tk-punctuation { color: var(--syn-punct); }
.tk-keyword,
.tk-builtin { color: var(--syn-keyword); }
.tk-function { color: var(--syn-function); }
.tk-string,
.tk-attr-value { color: var(--syn-string); }
.tk-number,
.tk-boolean { color: var(--syn-number); }
.tk-class-name { color: var(--syn-type); }
.tk-property,
.tk-variable { color: var(--syn-property); }
.tk-operator,
.tk-entity { color: var(--syn-operator); }
.tk-tag,
.tk-attr-name { color: var(--syn-tag); }

/* ---------- Arabic inside code ----------
   Code reads LTR, so an Arabic comment or string would be laid out left-to-right and an English word at
   its end ("// الوصول لعنصر بالـ index") would jump to the wrong side. plaintext gives the text its
   direction from its first letter: Arabic reads RTL, English stays LTR.
   - comments: the marker (//, #) stays on the left; only the text after it (.tk-comment-text) gets its own direction
   - strings: the whole string, quotes included (single-quoted strings in bash/php stay inline) */
.tk-comment { unicode-bidi: isolate; }
.tk-comment-text,
.tk-string { unicode-bidi: plaintext; }
.tk-string,
.tk-comment { display: inline-block; }
.tk-string.tk-sq,
.tk-string.tk-ml,
.tk-comment.tk-ml { display: inline; }
/* plaintext on a multi-line string would flip a line that starts with an Arabic word; keep it LTR like the code */
.tk-string.tk-ml { unicode-bidi: normal; }

/* ---------- nice-shape: ASCII diagrams ---------- */
/* a centered card that hugs its drawing */
.nice-shape pre.code-block {
  width: fit-content;
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
  padding: 1.25em 1.75em;
  background-color: var(--syn-bg);
}

/* only some frame characters are tokens (| but not + -), so the frame stays one plain color */
.nice-shape pre.code-block .tk-operator,
.nice-shape pre.code-block .tk-punctuation { color: inherit; }

/* ---------- diagrams (```diagram fences, drawn in the markdown with box-drawing lines) ----------
   ╭─┬─╮ │ ├ ┼ ┤ ╰─┴─╯ ▶ ▲ — scripts/draw-diagrams.js turns an ASCII sketch into this; code-highlight.js colors it.
   - font: the Hack cut of Vazir Code is the one with box-drawing glyphs (same width as letters, Arabic included)
   - line-height 1.2: the vertical stroke is 1.21em tall in that font, so at 1.2 the bars of
     consecutive rows touch and boxes close without gaps */
.nice-shape pre.code-diagram,
.nice-shape pre.code-diagram * {
  font-family: 'Vazir Code Hack', 'Vazir Code', monospace !important;
}

.nice-shape pre.code-diagram {
  /* exact pixel cells: in Vazir Code Hack every glyph is 1233/2048 em wide, so this font size makes one
     character exactly 9px wide; the line is 18px (vertical strokes are 1.21em ≈ 18.1px, so rows still join).
     Whole-pixel cells keep every column on exact pixels. */
  font-size: calc(9px * 2048 / 1233);
  line-height: 18px;
  padding: 18px 27px; /* one line, three characters */
}

.tk-dg-line { color: var(--syn-diagram-line); }
.tk-dg-arrow { color: var(--syn-keyword); }
/* in Vazir Code Hack the middle of ─ is 704/2048 em above the baseline, the middle of ► ◄ only 532/2048 em,
   so a sideways head sits 172/2048 em (0.084em) under its line; lift it so the head points straight along the line */
.tk-dg-arrow-side { position: relative; top: -0.084em; }

/* ---------- copy button (/js/code-block-enhancer.js) ----------
   The wrapper carries the block's outer margin so the button stays pinned to the corner while the code
   scrolls sideways. The button is quiet until the reader reaches the block (hover / keyboard focus);
   on touch screens there is no hover, so it is always shown. */
.code-block-wrap {
  position: relative;
  width: fit-content;
  min-width: min(100%, 24rem);
  max-width: 100%;
  margin: 1.5em auto;
}

.code-block-wrap > pre.code-block {
  margin: 0;
  min-width: 100%;
}

.code-copy {
  position: absolute;
  top: 0.6em;
  right: 0.6em;
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.3em 0.7em;
  font: 600 0.8125rem/1.4 'Vazirmatn RD', sans-serif;
  color: var(--syn-punct);
  background: rgba(17, 26, 43, 0.85); /* --syn-bg, so code under the button doesn't show through */
  border: 1px solid rgba(90, 169, 255, 0.3);
  border-radius: var(--radius-sm);
  cursor: pointer;
  opacity: 0;
  transition: opacity 150ms ease, color 150ms ease, border-color 150ms ease;
}

.code-copy i { font-size: 0.85em; }

.code-block-wrap:hover .code-copy,
.code-copy:focus-visible,
.code-copy.is-copied,
.code-copy.is-failed {
  opacity: 1;
}

.code-copy:hover {
  color: var(--syn-text);
  border-color: var(--syn-keyword);
}

.code-copy:focus-visible {
  outline: 2px solid var(--syn-keyword);
  outline-offset: 2px;
}

.code-copy.is-copied {
  color: var(--syn-string);
  border-color: var(--syn-string);
}

.code-copy.is-failed {
  color: var(--syn-tag);
  border-color: var(--syn-tag);
}

@media (hover: none) {
  .code-copy { opacity: 1; }
}

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