/**
 * code.css — Code Block & Syntax Styles
 * ═══════════════════════════════════════════════════════════════
 * Inline code, fenced code blocks, copy button, line numbers,
 * and Mermaid/Kroki diagram containers.
 * ═══════════════════════════════════════════════════════════════
 */

/* ── 1. Inline Code ──────────────────────────────────────────── */
code:not(pre > code) {
  font-family: "JetBrains Mono", monospace;
  font-size: 0.82em;
  padding: 0.1em 0.35em;
  background: var(--mb-surface-2);
  color: var(--mb-accent-bright);
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius-sm);
}

/* ── 2. Code Blocks ──────────────────────────────────────────── */
/* Non-scrolling shell — copy button mounts here and stays pinned */
.pre-copy-wrapper {
  position: relative;
  margin: 1.25rem 0;
}

pre {
  position: relative;
  background: var(--mb-surface-1) !important;
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius);
  padding: 1rem 1rem 1rem 1rem;
  overflow-x: auto;     /* scrolls, but button is on the wrapper above */
  margin: 0;            /* wrapper handles margin */
  font-family: "JetBrains Mono", monospace;
  font-size: 0.83rem;
  line-height: 1.6;
}

/* .highlight (Hugo chroma wrapper) — never scrolls; button mounts here */
.highlight {
  position: relative;
  margin: 1.25rem 0;
}
.highlight pre {
  overflow-x: auto;
  margin: 0;
}

pre code {
  background: transparent !important;
  border: none !important;
  padding: 0 !important;
  font-size: inherit;
  color: var(--mb-text) !important;
  border-radius: 0 !important;
}

/* Language label */
pre[data-lang]::before {
  content: attr(data-lang);
  position: absolute;
  top: 0;
  right: 2.5rem;
  font-size: 0.65rem;
  font-family: "JetBrains Mono", monospace;
  color: var(--mb-text-faint);
  padding: 0.2rem 0.4rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--mb-border);
  border-left: 1px solid var(--mb-border);
  background: var(--mb-surface-2);
  border-radius: 0 0 0 var(--pico-border-radius-sm);
}

/* ── 3. Syntax Highlighting (Chroma / Hugo) ──────────────────── */
/* Maps Matte Black Zed syntax colors to Hugo/Chroma classes */

/* Keywords → green */
.chroma .k, .chroma .kc, .chroma .kd, .chroma .kn,
.chroma .kp, .chroma .kr, .chroma .kt {
  color: #059669;
}

/* Strings → text (eaeaea) */
.chroma .s, .chroma .sa, .chroma .sb, .chroma .sc,
.chroma .dl, .chroma .sd, .chroma .s2, .chroma .se,
.chroma .sh, .chroma .si, .chroma .sx, .chroma .sr,
.chroma .s1, .chroma .ss {
  color: #EAEAEA;
}

/* Numbers → gold/info */
.chroma .m, .chroma .mb, .chroma .mf, .chroma .mh,
.chroma .mi, .chroma .il, .chroma .mo {
  color: #EFBF04;
}

/* Functions → amber */
.chroma .nf, .chroma .fm { color: #F59E0B; }

/* Types → bright amber */
.chroma .nc, .chroma .nn { color: #FBBF24; }

/* Variables → amber dark */
.chroma .n, .chroma .nb, .chroma .bp { color: #D97706; }

/* Operators → muted */
.chroma .o, .chroma .ow { color: #bebebe; }

/* Comments → faint italic */
.chroma .c, .chroma .ch, .chroma .cm, .chroma .c1,
.chroma .cs, .chroma .cp, .chroma .cpf {
  color: #8A8A8D;
  font-style: italic;
}

/* Tags (HTML/XML) → green */
.chroma .nt { color: #059669; }

/* Attributes → amber dark italic */
.chroma .na { color: #D97706; font-style: italic; }

/* Punctuation → muted */
.chroma .p, .chroma .pi { color: #8A8A8D; }

/* Preprocessor / directives → blue */
.chroma .cp, .chroma .bp { color: #3B82F6; }

/* Error */
.chroma .err { color: #DC2626; }

/* Code block background */
.chroma { background: var(--mb-surface-1) !important; }

/* ── 4. Keyboard shortcut */
kbd {
  font-family: "JetBrains Mono", monospace;
  font-size: 0.75em;
  padding: 0.15em 0.4em;
  background: var(--mb-surface-2);
  border: 1px solid var(--mb-border);
  border-bottom-width: 2px;
  border-radius: var(--pico-border-radius-sm);
  color: var(--mb-text);
}

/* ── 5. Mermaid Diagrams ─────────────────────────────────────── */
.mermaid {
  background: var(--mb-surface-1);
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius);
  margin: 1.25rem 0;
  overflow: visible;     /* outer container never scrolls */
  position: relative;
  padding: 2.75rem 0.75rem 0.75rem; /* top room for toolbar */
}

/* Inner wrapper — this scrolls when SVG is zoomed > 100% */
.mermaid-scroll-inner {
  overflow-x: visible;
  overflow-y: visible;
  user-select: none; /* prevent text selection while panning */
}

.mermaid svg {
  /* Natural render: full width, proportional height */
  display: block;
  width: 100%;
  height: auto;
  max-width: none !important; /* override Mermaid 10's inline max-width */
}

/* Zoom toolbar — anchored to outer container, never scrolls */
.mermaid-toolbar {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  display: flex;
  gap: 0.25rem;
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.mermaid:hover .mermaid-toolbar {
  opacity: 1;
  pointer-events: auto;
}
.mermaid-toolbar button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  min-width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  background: var(--mb-surface-2);
  border: 1px solid var(--mb-border);
  border-radius: 3px;
  color: var(--mb-text-muted);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  outline: none;
  box-shadow: none;
  white-space: nowrap;
}
.mermaid-toolbar button svg {
  display: block;
  width: 1rem;
  height: 1rem;
}
.mermaid-toolbar button:hover {
  color: var(--mb-accent);
  border-color: var(--mb-accent);
}

body.mermaid-viewer-open {
  overflow: hidden;
}

.mermaid-viewer-modal[hidden] {
  display: none;
}

.mermaid-viewer-modal {
  position: fixed;
  inset: 0;
  z-index: 1650;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(0.75rem, 3vw, 2rem);
}

.mermaid-viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.74);
  backdrop-filter: blur(8px);
}

.mermaid-viewer-panel {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr;
  width: min(1180px, 100%);
  height: min(84vh, 920px);
  overflow: hidden;
  background: var(--mb-surface-1);
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius);
}

.mermaid-viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  background: var(--mb-surface-2);
  border-bottom: 1px solid var(--mb-border);
}

.mermaid-viewer-title {
  min-width: 0;
  overflow: hidden;
  color: var(--mb-accent-bright);
  font-family: "JetBrains Mono", monospace;
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mermaid-viewer-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 0.45rem;
}

.mermaid-viewer-actions button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: auto;
  min-width: 2.75rem;
  height: 2.75rem;
  margin: 0;
  padding: 0 0.7rem;
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius-sm);
  background: var(--mb-surface-1);
  color: var(--mb-text-faint);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
}

.mermaid-viewer-actions button:hover {
  color: var(--mb-accent);
  border-color: var(--mb-border-focus);
}

.mermaid-viewer-body {
  min-height: 0;
  overflow: hidden;
}

.mermaid-viewer-inner {
  width: 100%;
  height: 100%;
  padding: 1rem;
  overflow: visible;
  user-select: none;
}

.mermaid-viewer-inner svg {
  display: block;
  width: 100%;
  height: auto;
  max-width: none !important;
}

/* ── 6. Kroki Diagrams ───────────────────────────────────────── */
.kroki-shortcode {
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius);
  padding: 1rem;
  margin: 1.25rem 0;
  background: var(--mb-surface-1);
  overflow-x: auto;
  text-align: center;
}

.kroki-caption {
  font-size: 0.78rem;
  font-family: "JetBrains Mono", monospace;
  color: var(--mb-text-faint);
  margin-bottom: 0.75rem;
  text-align: left;
}

.kroki-shortcode svg { max-width: 100%; height: auto; }

.kroki-loading {
  color: var(--mb-text-faint);
  font-size: 0.8rem;
  font-family: "JetBrains Mono", monospace;
  padding: 1rem;
}

.kroki-error {
  color: var(--mb-red);
  font-size: 0.8rem;
  font-family: "JetBrains Mono", monospace;
  border: 1px solid var(--mb-red);
  padding: 0.5rem 0.75rem;
  border-radius: var(--pico-border-radius-sm);
  background: var(--mb-red-bg);
}

/* ── 7. Code Viewer Modal ────────────────────────────────────── */
body.code-viewer-open {
  overflow: hidden;
}

.code-viewer-modal[hidden] {
  display: none;
}

.code-viewer-modal {
  position: fixed;
  inset: 0;
  z-index: 1600;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(0.75rem, 3vw, 2rem);
}

.code-viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(8px);
}

.code-viewer-panel {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr;
  width: min(88vw, 1500px);
  min-width: min(620px, 100%);
  max-width: 100%;
  max-height: min(82vh, 900px);
  overflow: hidden;
  background: var(--mb-surface-1);
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius);
}

@media (max-width: 900px) {
  .code-viewer-modal {
    padding: 0.75rem;
  }

  .code-viewer-panel {
    width: 100%;
    max-height: min(88dvh, 900px);
  }
}

.code-viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  background: var(--mb-surface-2);
  border-bottom: 1px solid var(--mb-border);
}

.code-viewer-language {
  min-width: 0;
  overflow: hidden;
  color: var(--mb-accent-bright);
  font-family: "JetBrains Mono", monospace;
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}

.code-viewer-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 0.35rem;
}

.code-viewer-copy,
.code-viewer-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
  width: 1.85rem;
  min-width: 1.85rem;
  height: 1.85rem;
  border: 1px solid var(--mb-border);
  border-radius: var(--pico-border-radius-sm);
  background: var(--mb-surface-1);
  color: var(--mb-text-faint);
  line-height: 1;
}

.code-viewer-copy svg,
.code-viewer-close svg {
  display: block;
  width: 1.12rem;
  height: 1.12rem;
}

.code-viewer-copy:hover,
.code-viewer-close:hover {
  color: var(--mb-accent);
  border-color: var(--mb-border-focus);
}

.code-viewer-copy.copied {
  color: var(--mb-green);
  border-color: var(--mb-green);
}

.code-viewer-body {
  min-width: 0;
  min-height: 0;
  max-height: calc(min(82vh, 900px) - 3.1rem);
  overflow: auto;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.025) 1px, transparent 1px) 0 0 / 4ch 100%,
    var(--mb-surface-1);
}

@media (max-width: 900px) {
  .code-viewer-body {
    max-height: calc(min(88dvh, 900px) - 3.1rem);
  }
}

.code-viewer-code {
  display: table;
  min-width: 100%;
  width: max-content;
  padding: 0.75rem 0;
  font-family: "JetBrains Mono", monospace;
  font-size: 0.84rem;
  line-height: 1.62;
  color: var(--mb-text);
  counter-reset: code-viewer-line;
}

.code-viewer-line {
  display: table-row;
  min-height: 1.6em;
}

.code-viewer-line:hover {
  background: rgba(251, 191, 36, 0.06);
}

.code-viewer-gutter,
.code-viewer-line-content {
  display: table-cell;
  vertical-align: top;
}

.code-viewer-gutter {
  position: sticky;
  left: 0;
  z-index: 1;
  min-width: 3.5rem;
  padding: 0 0.85rem 0 0.75rem;
  border-right: 1px solid var(--mb-border);
  background: color-mix(in srgb, var(--mb-surface-2) 82%, transparent);
  color: var(--mb-text-faint);
  font-variant-numeric: tabular-nums;
  text-align: right;
  user-select: none;
}

.code-viewer-line-content {
  padding: 0 1.25rem;
  white-space: pre;
}

.code-viewer-code .line,
.code-viewer-code .cl {
  display: inline;
}

.code-viewer-modal.is-compact .code-viewer-body {
  max-height: min(44vh, calc((var(--code-viewer-lines) * 1.62em) + 1.5rem));
}

@media (max-width: 640px) {
  .code-viewer-modal {
    align-items: stretch;
    min-height: 100dvh;
    padding:
      max(0.45rem, env(safe-area-inset-top))
      max(0.45rem, env(safe-area-inset-right))
      max(0.45rem, env(safe-area-inset-bottom))
      max(0.45rem, env(safe-area-inset-left));
  }

  .code-viewer-panel {
    width: 100%;
    height: calc(100vh - 0.9rem);
    height: calc(100dvh - max(0.9rem, env(safe-area-inset-top) + env(safe-area-inset-bottom)));
    max-height: none;
    min-width: 0;
    border-radius: var(--pico-border-radius);
  }

  .code-viewer-modal.is-compact .code-viewer-body,
  .code-viewer-body {
    max-height: none;
  }

  .code-viewer-header {
    min-width: 0;
    padding: 0.55rem;
    gap: 0.5rem;
  }

  .code-viewer-actions {
    gap: 0.3rem;
  }

  .code-viewer-copy,
  .code-viewer-close {
    width: 2.15rem;
    min-width: 2.15rem;
    height: 2.15rem;
  }

  .code-viewer-copy svg,
  .code-viewer-close svg {
    width: 1.15rem;
    height: 1.15rem;
  }

  .code-viewer-code {
    font-size: 0.78rem;
    line-height: 1.58;
  }

  .code-viewer-gutter {
    min-width: 2.7rem;
    padding: 0 0.55rem 0 0.45rem;
  }

  .code-viewer-line-content {
    padding: 0 0.85rem;
  }

  .mermaid-viewer-modal {
    align-items: stretch;
    padding: 0;
  }

  .mermaid-viewer-panel {
    width: 100%;
    height: 100%;
    border-right: 0;
    border-left: 0;
    border-radius: 0;
  }

  .mermaid-viewer-header {
    padding: 0.6rem;
  }

  .mermaid-viewer-actions {
    gap: 0.35rem;
  }

  .mermaid-viewer-actions button {
    min-width: 2.35rem;
    height: 2.35rem;
    padding: 0 0.55rem;
    font-size: 1.22rem;
  }
}
