/*
 * Bevel code theme: MicroLighter ::highlight() colours + .code-block component.
 * Consumes the markup in .weave/notes/code-block-contract.md. Self-contained: all values are literals from
 * docs/Bevel.Docs/DESIGN.md, so it works before the layout stylesheet exists. Light default; dark via
 * :root[data-theme="dark"] or prefers-color-scheme when data-theme is not "light".
 *
 * Computed WCAG 2.x contrast of every token colour on every surface a line can have
 * (code surface / highlighted line / diff added / diff removed). All >= 4.5:1.
 *
 * LIGHT          colour    surface  hl    add   del
 *  text          #16161D   15.1    15.1  15.5  14.2
 *  comment       #5B5B66    5.6     5.6   5.8   5.3
 *  keyword/tag   #8A2A9B    6.1     6.1   6.3   5.8
 *  string        #8A4B00    5.7     5.7   5.9   5.4
 *  number/const  #0B5C9E    5.8     5.8   5.9   5.5
 *  function      #1A5C3A    6.7     6.7   6.9   6.3
 *  type          #9A2F1F    6.3     6.3   6.5   5.9
 *  variable      #5B3FA8    6.5     6.5   6.6   6.1
 *  punctuation   #3A3A46    9.4     9.4   9.6   8.9
 *  (surface #F2EADC, hl #FFEAA6, add #D6F5E7, del #F6E0DA)
 * DARK
 *  text          #FBF6EE   16.7    11.5  11.7  13.8
 *  comment       #AAAAB6    7.8     5.4   5.5   6.5
 *  keyword/tag   #E59BFF    9.0     6.2   6.3   7.4
 *  string        #FFC21A   11.1     7.6   7.8   9.2
 *  number/const  #7CC4FF    9.6     6.6   6.7   7.9
 *  function      #6FE3B0   11.4     7.8   8.0   9.4
 *  type          #FF9C8A    8.9     6.1   6.2   7.4
 *  variable      #B9A8FF    8.7     5.9   6.1   7.2
 *  punctuation   #D4D0C8   11.7     8.0   8.2   9.7
 *  (surface #16161D, hl #3D3414, add #123A2B, del #3F1E19)
 * Gutter numbers use --muted (#5B5B66 / #AAAAB6), same ratios as "comment".
 */

:root {
    --cb-bg: #F2EADC;
    --cb-raised: #FFFFFF;
    --cb-text: #16161D;
    --cb-muted: #5B5B66;
    --cb-line: #E4DACA;
    --cb-edge: #16161D;
    --cb-shadow: #16161D;
    --cb-accent: #FFC21A;
    --cb-hl: #FFEAA6;
    --cb-add: #D6F5E7;
    --cb-add-bar: #2FD18B;
    --cb-del: #F6E0DA;
    --cb-del-bar: #B4412F;
    --cb-keyword: #8A2A9B;
    --cb-string: #8A4B00;
    --cb-number: #0B5C9E;
    --cb-function: #1A5C3A;
    --cb-type: #9A2F1F;
    --cb-variable: #5B3FA8;
    --cb-punct: #3A3A46;
}

:root[data-theme="dark"] {
    --cb-bg: #16161D;
    --cb-raised: #22222B;
    --cb-text: #FBF6EE;
    --cb-muted: #AAAAB6;
    --cb-line: #34343F;
    --cb-edge: #FBF6EE;
    --cb-shadow: #4A4A58;
    --cb-hl: #3D3414;
    --cb-add: #123A2B;
    --cb-del: #3F1E19;
    --cb-del-bar: #FF8F7D;
    --cb-keyword: #E59BFF;
    --cb-string: #FFC21A;
    --cb-number: #7CC4FF;
    --cb-function: #6FE3B0;
    --cb-type: #FF9C8A;
    --cb-variable: #B9A8FF;
    --cb-punct: #D4D0C8;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --cb-bg: #16161D;
        --cb-raised: #22222B;
        --cb-text: #FBF6EE;
        --cb-muted: #AAAAB6;
        --cb-line: #34343F;
        --cb-edge: #FBF6EE;
        --cb-shadow: #4A4A58;
        --cb-hl: #3D3414;
        --cb-add: #123A2B;
        --cb-del: #3F1E19;
        --cb-del-bar: #FF8F7D;
        --cb-keyword: #E59BFF;
        --cb-string: #FFC21A;
        --cb-number: #7CC4FF;
        --cb-function: #6FE3B0;
        --cb-type: #FF9C8A;
        --cb-variable: #B9A8FF;
        --cb-punct: #D4D0C8;
    }
}

/* ---- Token colours (MicroLighter ::highlight() names) ---- */
::highlight(comment), ::highlight(quote) {
    color: var(--cb-muted);
}

::highlight(keyword), ::highlight(storage), ::highlight(doctype) {
    color: var(--cb-keyword);
}

::highlight(important), ::highlight(section), ::highlight(tag) {
    color: var(--cb-keyword);
}

::highlight(operator), ::highlight(punctuation) {
    color: var(--cb-punct);
}

::highlight(string), ::highlight(regexp), ::highlight(attribute-value), ::highlight(link), ::highlight(raw) {
    color: var(--cb-string);
}

::highlight(numeric), ::highlight(constant), ::highlight(boolean), ::highlight(symbol) {
    color: var(--cb-number);
}

::highlight(property), ::highlight(entity), ::highlight(anchor) {
    color: var(--cb-type);
}

::highlight(key), ::highlight(attribute-name) {
    color: var(--cb-number);
}

::highlight(function), ::highlight(decorator), ::highlight(animation), ::highlight(inserted) {
    color: var(--cb-function);
}

::highlight(type), ::highlight(support), ::highlight(selector), ::highlight(deleted) {
    color: var(--cb-type);
}

::highlight(variable), ::highlight(interpolation) {
    color: var(--cb-variable);
}

/* ---- Component ---- */
.code-block {
    /* Silhouette (cut, border, shadow) comes from the shared bevel recipe in site.css. */
    --cut: 24px;
    --bw: 2px;
    --sh: 6px;
    --fill: var(--cb-bg);
    --edge: var(--cb-edge);
    --shade: var(--cb-shadow);
    margin: 28px 6px 28px 0;
    color: var(--cb-text);
    font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.code-block__header {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 48px;
    padding: 0 44px 0 16px;
    border-bottom: 1px solid var(--cb-line);
    font-size: 13px;
}

/* No title: a slim bar with just the language and Copy, and no rule under it. */
.code-block__header:not(:has(.code-block__title)) {
    min-height: 40px;
    border-bottom: 0;
    padding-bottom: 0;
}

.code-block__header:not(:has(.code-block__title)) + .code-block__body {
    --cb-pad-top: 6px;
}

.code-block__title {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 400;
}

.code-block__title::before {
    content: "";
    flex: none;
    width: 18px;
    height: 18px;
    background: currentColor;
    -webkit-mask: var(--cb-file-icon) center / contain no-repeat;
    mask: var(--cb-file-icon) center / contain no-repeat;
    --cb-file-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linejoin='round'%3E%3Cpath d='M6 3h8l5 5v13H6z'/%3E%3Cpath d='M9 13h7M9 17h7'/%3E%3C/svg%3E");
}

.code-block__lang {
    margin-left: auto;
    font-size: 12px;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--cb-muted);
}

.code-block__copy {
    all: unset;
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 30px;
    padding: 0 10px;
    font: 700 12px/1 "JetBrains Mono", ui-monospace, Menlo, monospace;
    color: var(--cb-text);
    cursor: pointer;
    background: var(--cb-raised);
    border: 2px solid var(--cb-edge);
    box-shadow: 2px 2px 0 var(--cb-shadow);
}

.code-block__copy::before {
    content: "";
    width: 14px;
    height: 14px;
    background: currentColor;
    -webkit-mask: var(--cb-copy-icon) center / contain no-repeat;
    mask: var(--cb-copy-icon) center / contain no-repeat;
    --cb-copy-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.4'%3E%3Crect x='8' y='8' width='12' height='12'/%3E%3Cpath d='M4 16V4h12'/%3E%3C/svg%3E");
}

.code-block__copy:hover {
    background: var(--cb-hl);
}

.code-block__copy:focus-visible {
    outline: 3px solid var(--cb-accent);
    outline-offset: 2px;
}

.code-block__copy[data-state="copied"] {
    background: var(--cb-add);
}

.code-block__copy[data-state="copied"]::before {
    --cb-copy-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='3'%3E%3Cpath d='M4 12l5 5L20 6'/%3E%3C/svg%3E");
}

.code-block__copy[data-state="failed"] {
    background: var(--cb-del);
}

/* One source of truth for the code rows: line height and vertical padding feed the code, the bands and the gutter. */
.code-block__body {
    position: relative;
    --cb-gutter: 0px;
    --cb-lh: 26px;
    --cb-pad-top: 16px;
    --cb-pad-bottom: 16px;
}

.code-block__pre {
    position: relative;
    z-index: 1;
    margin: 0;
    overflow-x: auto;
    background: transparent;
    padding: var(--cb-pad-top) 20px var(--cb-pad-bottom) calc(var(--cb-gutter, 0px) + 20px);
    font: 400 14px/var(--cb-lh) "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    white-space: pre;
    tab-size: 4;
    color: var(--cb-text);
}

.code-block__pre > code {
    font: inherit;
    white-space: inherit;
    background: none;
    padding: 0;
    border: 0;
    color: inherit;
}

.code-block__pre:focus-visible {
    outline: 3px solid var(--cb-accent);
    outline-offset: -3px;
}

/* overlay layers (built by code-blocks.js): every row is exactly one --cb-lh line, so no font measuring is needed */
.code-block__bands, .code-block__gutter {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    padding: var(--cb-pad-top) 0 var(--cb-pad-bottom);
    font: 400 14px/var(--cb-lh) "JetBrains Mono", ui-monospace, Menlo, monospace;
    user-select: none;
    pointer-events: none;
}

.code-block__bands {
    z-index: 0;
    right: 0;
}

.code-block__gutter {
    z-index: 2;
    width: var(--cb-gutter);
    background: var(--cb-bg);
    color: var(--cb-muted);
}

.code-block__row {
    display: flex;
    height: var(--cb-lh);
    align-items: center;
    line-height: var(--cb-lh);
}

.code-block__ln {
    width: 44px;
    flex: none;
    padding-right: 12px;
    text-align: right;
    font-size: 13px;
}

.code-block__glyph {
    width: 28px;
    flex: none;
    text-align: center;
    font-weight: 700;
}

.code-block__row.is-hl {
    background: var(--cb-hl);
    box-shadow: inset 3px 0 0 var(--cb-accent);
}

.code-block__row.is-add {
    background: var(--cb-add);
    box-shadow: inset 3px 0 0 var(--cb-add-bar);
    color: var(--cb-text);
}

.code-block__row.is-del {
    background: var(--cb-del);
    box-shadow: inset 3px 0 0 var(--cb-del-bar);
    color: var(--cb-text);
}

.code-block__row.is-hl .code-block__ln, .code-block__row.is-add .code-block__ln, .code-block__row.is-del .code-block__ln {
    color: var(--cb-text);
}

@media (max-width: 767px) {
    .code-block {
        margin-right: 6px;
    }

    .code-block__pre, .code-block__gutter, .code-block__bands {
        font-size: 13px;
    }
}

@media print {
    .code-block__copy {
        display: none;
    }
}
