/*
 * The syntax addon's stylesheet, served at /@syntax/syntax.css and linked by the site's
 * global controller. Two halves: the .code chrome (card, title bar, copy button, gutter, line
 * markers) and the token palette. The highlighter emits class names only - every token span
 * carries its TextMate scopes as phiki-{scope} classes, one dotted class per scope - so the
 * palette matches them by substring: span[class*="phiki-comment"] matches every comment scope
 * in every grammar without enumerating them. Rules sit in precedence order (later wins): a
 * token's scopes stack families (string + punctuation, keyword + meta), and the family that
 * should color it is the later rule.
 *
 * Every color derives from the theme tokens ui emits under [data-theme], so the palette
 * re-derives when the site's theme switches - one palette, zero re-render, zero script.
 * The ink shares are tuned so every token color keeps 4.5:1 on the code background in both
 * the light and dark themes. A site wanting exact control of the background sets --code-bg.
 */

/* the derived code background: warm toward primary on a light theme, deep toward black on a dark one */
:root,
[data-theme="light"] {
	--syntax-bg: color-mix(in oklab, hsl(var(--canvas)), hsl(var(--primary)) 6%);
}

[data-theme="dark"] {
	--syntax-bg: color-mix(in oklab, hsl(var(--canvas)), black 25%);
}

/* -- chrome ---------------------------------------------------------------- */

.code {
	position: relative;
	margin: 1.25em 0;
	border: 1px solid color-mix(in oklab, hsl(var(--ink)) 12%, hsl(var(--canvas)));
	border-radius: var(--radius, 0.5rem);
	background: var(--code-bg, var(--syntax-bg));
	font-family: var(--font-mono, ui-monospace, Menlo, Consolas, monospace);
	font-size: var(--text-sm, 0.875rem);
	line-height: 1.6;
	overflow: hidden;
}

.code > header {
	padding: 0.375rem 3rem 0.375rem 1rem;
	border-bottom: 1px solid color-mix(in oklab, hsl(var(--ink)) 12%, hsl(var(--canvas)));
	color: color-mix(in oklab, hsl(var(--ink)) 70%, hsl(var(--canvas)));
	font-size: 0.75rem;
	font-weight: 600;
	letter-spacing: 0.08em;
	text-transform: uppercase;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.code [data-copy] {
	position: absolute;
	top: 0.25rem;
	right: 0.25rem;
	display: grid;
	place-items: center;
	padding: 0.375rem;
	border: 0;
	border-radius: var(--radius, 0.5rem);
	background: transparent;
	color: color-mix(in oklab, hsl(var(--ink)) 70%, hsl(var(--canvas)));
	cursor: pointer;
}

.code [data-copy]:hover {
	color: hsl(var(--ink));
}

.code [data-copy]:focus-visible {
	outline: 2px solid hsl(var(--primary));
	outline-offset: 2px;
}

.code [data-copy] svg {
	display: none;
	width: 1rem;
	height: 1rem;
}

.code [data-copy] .syntax-copy-icon {
	display: block;
}

.code [data-copy][data-copied] .syntax-copy-icon {
	display: none;
}

.code [data-copy][data-copied] .syntax-check-icon {
	display: block;
}

.code pre {
	margin: 0;
	padding: 1rem;
	overflow-x: auto;
	white-space: pre;
	color: hsl(var(--ink));
	tab-size: 4;
}

/* the wrap setting replaces horizontal scroll with soft wrapping */
.code.wrap pre {
	white-space: pre-wrap;
	overflow-wrap: anywhere;
}

.code pre::-webkit-scrollbar {
	height: 0.5rem;
}

.code pre::-webkit-scrollbar-thumb {
	border-radius: var(--radius, 0.5rem);
	background: color-mix(in oklab, hsl(var(--ink)) 20%, hsl(var(--canvas)));
}

/* the renderer tabs into the pre so keyboard visitors can scroll wide code; focus shows */
.code pre:focus-visible {
	outline: 2px solid hsl(var(--primary));
	outline-offset: -2px;
}

/* -- gutter and line markers ------------------------------------------------ */

.code .line-number {
	display: inline-block;
	min-width: 2ch;
	margin-right: 1.25ch;
	/* 55% ink reads ~4:1 on the light code background; 70% clears AA in both themes
	   while staying a clear step dimmer than the code text itself */
	color: color-mix(in oklab, hsl(var(--ink)) 70%, hsl(var(--canvas)));
	text-align: right;
	-webkit-user-select: none;
	user-select: none;
}

/* a line stays inline (its own trailing newline breaks it), so a marker paints the
   line's extent with a clone-per-box background and a left edge in the family color */
.code .line.insert,
.code .line.remove,
.code .line.highlight,
.code .line.focus {
	padding: 0 0.375rem;
	-webkit-box-decoration-break: clone;
	box-decoration-break: clone;
}

.code .line.insert {
	background: color-mix(in oklab, hsl(var(--success)) 12%, var(--code-bg, var(--syntax-bg)));
	box-shadow: inset 3px 0 0 color-mix(in oklab, hsl(var(--success)), hsl(var(--ink)) 45%);
}

.code .line.remove {
	background: color-mix(in oklab, hsl(var(--destructive)) 12%, var(--code-bg, var(--syntax-bg)));
	box-shadow: inset 3px 0 0 color-mix(in oklab, hsl(var(--destructive)), hsl(var(--ink)) 35%);
}

.code .line.highlight,
.code .line.focus {
	background: color-mix(in oklab, hsl(var(--primary)) 10%, var(--code-bg, var(--syntax-bg)));
	box-shadow: inset 3px 0 0 color-mix(in oklab, hsl(var(--primary)), hsl(var(--ink)) 55%);
}

.code .line.insert > .line-number {
	color: color-mix(in oklab, hsl(var(--success)), hsl(var(--ink)) 45%);
}

.code .line.remove > .line-number {
	color: color-mix(in oklab, hsl(var(--destructive)), hsl(var(--ink)) 35%);
}

/* a fenced {{n}} focus set dims every line outside it */
.code .phiki.focus .line:not(.focus) {
	opacity: 0.4;
}

/* -- token palette ------------------------------------------------------------
   Ambient default first, then families in precedence order; the exact scope a later rule
   matches wins over the broader family of an earlier one (htmx attributes after entity,
   operators after keywords). */

.code .token {
	color: hsl(var(--ink));
}

.code span[class*="phiki-punctuation"] {
	color: color-mix(in oklab, hsl(var(--ink)) 80%, hsl(var(--canvas)));
}

.code span[class*="phiki-comment"] {
	color: color-mix(in oklab, hsl(var(--ink)) 70%, hsl(var(--canvas)));
	font-style: italic;
}

.code span[class*="phiki-constant"] {
	color: color-mix(in oklab, hsl(var(--info)), hsl(var(--ink)) 50%);
}

.code span[class*="phiki-string"] {
	color: color-mix(in oklab, hsl(var(--success)), hsl(var(--ink)) 45%);
}

.code span[class*="phiki-entity"] {
	color: color-mix(in oklab, hsl(var(--info)), hsl(var(--ink)) 45%);
}

.code span[class*="phiki-support"] {
	color: color-mix(in oklab, hsl(var(--info)), hsl(var(--ink)) 45%);
}

.code span[class*="phiki-storage"],
.code span[class*="phiki-keyword"] {
	color: color-mix(in oklab, hsl(var(--primary)), hsl(var(--ink)) 55%);
}

/* operators read as structure, not keywords */
.code span[class*="phiki-keyword.operator"] {
	color: color-mix(in oklab, hsl(var(--ink)) 80%, hsl(var(--canvas)));
}

/* the diff grammar's own +/- markup */
.code span[class*="phiki-markup.inserted"] {
	color: color-mix(in oklab, hsl(var(--success)), hsl(var(--ink)) 45%);
}

.code span[class*="phiki-markup.deleted"] {
	color: color-mix(in oklab, hsl(var(--destructive)), hsl(var(--ink)) 35%);
}

.code span[class*="phiki-invalid"] {
	color: color-mix(in oklab, hsl(var(--destructive)), hsl(var(--ink)) 35%);
}

/* htmx attributes in the addon's grammar carry their own scope */
.code span[class*="phiki-entity.other.attribute-name.htmx"] {
	color: color-mix(in oklab, hsl(var(--accent)), hsl(var(--ink)) 55%);
}
