/* CFan Code: code blocks (see inc/render.php and assets/code.js). The colours are variables a theme can set:
   --cfan-code-bg, -bar (title row and line-number column), -line (borders), -text, -muted, -stripe, -button-hover,
   -on (a switched-on button), and the token colours -comment, -string, -keyword, -entity, -variable, -constant, -tag,
   -operator, -symbol, -deleted, -added. */

.cfan-code {
	position: relative;
	margin: 1.5em 0;
	padding: 0;
	border: 1px solid var(--cfan-code-line, rgb(128 128 128 / 0.25));
	border-radius: 6px;
	overflow: hidden;
	background: var(--cfan-code-bg, rgb(128 128 128 / 0.06));
	color: var(--cfan-code-text, inherit);
}

.cfan-code-title {
	margin: 0;
	padding: 0.4em 0.9em;
	border-bottom: 1px solid var(--cfan-code-line, rgb(128 128 128 / 0.25));
	background: var(--cfan-code-bar, rgb(128 128 128 / 0.08));
	color: var(--cfan-code-muted, inherit);
	font-size: 0.85em;
	line-height: 1.5;
	overflow-wrap: anywhere;
}
.cfan-code.has-title .cfan-code-title {
	padding-right: 9em; /* room for the buttons */
}

.cfan-code-pre {
	margin: 0;
	padding: 0.6em 0.9em;
	border: 0;
	border-radius: 0;
	background: none;
	color: inherit;
	font-family: var(--cfan-code-font, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
	font-size: var(--cfan-code-size, 0.875em);
	line-height: 1.6;
	tab-size: 4;
	white-space: pre;
	overflow-x: auto;
}
.cfan-code-pre code {
	display: block;
	padding: 0;
	border: 0;
	background: none;
	color: inherit;
	font: inherit;
	white-space: inherit;
}
.cfan-code.is-wrapped .cfan-code-pre {
	white-space: pre-wrap;
	overflow-wrap: anywhere;
}

/* numbered lines (once the script has run) */
.cfan-code.is-ready .cfan-code-pre {
	padding: 0.5em 0;
}
.cfan-code.is-ready .cfan-code-pre code {
	width: max-content;
	min-width: 100%;
	counter-reset: cfan-code-line;
}
.cfan-code.is-ready.is-wrapped .cfan-code-pre code {
	width: auto;
}
.cfan-code-line {
	display: grid;
	grid-template-columns: calc(var(--cfan-code-digits, 2) * 1ch + 1.8em) minmax(0, 1fr);
	padding-right: 0.9em;
}
.cfan-code-text {
	min-width: 0;
}
.cfan-code-line:nth-child(even) {
	background: var(--cfan-code-stripe, rgb(128 128 128 / 0.05));
}
.cfan-code-line::before {
	counter-increment: cfan-code-line;
	content: counter(cfan-code-line);
	position: sticky;
	left: 0;
	z-index: 1;
	margin-right: 0.9em;
	padding: 0 0.6em 0 0.5em;
	border-right: 1px solid var(--cfan-code-line, rgb(128 128 128 / 0.25));
	background: var(--cfan-code-bar-solid, var(--cfan-code-bar, #f2f3f5));
	color: var(--cfan-code-number, var(--cfan-code-muted, #888));
	text-align: right;
	user-select: none;
	-webkit-user-select: none;
}
.cfan-code-line:first-child::before {
	box-shadow: 0 -0.5em 0 var(--cfan-code-bar-solid, var(--cfan-code-bar, #f2f3f5));
}
.cfan-code-line:last-child::before {
	box-shadow: 0 0.5em 0 var(--cfan-code-bar-solid, var(--cfan-code-bar, #f2f3f5));
}
.cfan-code-line:only-child::before {
	box-shadow: 0 -0.5em 0 var(--cfan-code-bar-solid, var(--cfan-code-bar, #f2f3f5)), 0 0.5em 0 var(--cfan-code-bar-solid, var(--cfan-code-bar, #f2f3f5));
}

/* language, line-wrap switch and copy button: in the title row, or over the code's top right corner when pointed at */
.cfan-code-tools {
	position: absolute;
	top: 0.3em;
	right: 0.35em;
	z-index: 2;
	display: flex;
	align-items: center;
	gap: 0.2em;
	font-size: 0.78rem;
	line-height: 1;
	opacity: 0;
	transition: opacity 0.15s;
}
.cfan-code.has-title .cfan-code-tools,
.cfan-code:hover .cfan-code-tools,
.cfan-code:focus-within .cfan-code-tools {
	opacity: 1;
}
.cfan-code:not(.has-title) .cfan-code-tools {
	padding: 0.15em;
	border-radius: 6px;
	background: var(--cfan-code-bg-solid, var(--cfan-code-bar, #f2f3f5));
}
/* on touch screens (nothing to point with) the buttons always show, in a bar of their own above the code */
@media (hover: none) {
	.cfan-code.is-ready:not(.has-title)::before {
		content: "";
		display: block;
		height: calc(0.78rem + 1.25em);
		border-bottom: 1px solid var(--cfan-code-line, rgb(128 128 128 / 0.25));
		background: var(--cfan-code-bar, rgb(128 128 128 / 0.08));
	}
	.cfan-code-tools {
		opacity: 1;
		top: 0.2em;
	}
	.cfan-code:not(.has-title) .cfan-code-tools {
		padding: 0;
		background: none;
	}
}
.cfan-code-lang {
	padding: 0 0.4em;
	color: var(--cfan-code-muted, #888);
	letter-spacing: 0.02em;
}
.cfan-code-button {
	display: inline-flex;
	align-items: center;
	gap: 0.3em;
	margin: 0;
	padding: 0.3em 0.45em;
	border: 0;
	border-radius: 5px;
	background: transparent;
	color: var(--cfan-code-muted, inherit);
	font: inherit;
	cursor: pointer;
}
.cfan-code-button:hover,
.cfan-code-button:focus-visible {
	background: var(--cfan-code-button-hover, rgb(128 128 128 / 0.15));
	color: var(--cfan-code-text, inherit);
}
.cfan-code-button[aria-pressed="true"],
.cfan-code-button.is-done {
	color: var(--cfan-code-on, #1a73e8);
}
.cfan-code-button svg {
	display: block;
	flex: none;
}
@media (max-width: 480px) {
	.cfan-code-copy span {
		display: none;
	}
	.cfan-code.has-title .cfan-code-title {
		padding-right: 6em;
	}
}

/* the code's colours (highlight.js's classes) */
.cfan-code .hljs-comment,
.cfan-code .hljs-quote {
	color: var(--cfan-code-comment, #6a737d);
	font-style: italic;
}
.cfan-code .hljs-string,
.cfan-code .hljs-regexp,
.cfan-code .hljs-char.escape_ {
	color: var(--cfan-code-string, #22863a);
}
.cfan-code .hljs-keyword,
.cfan-code .hljs-type,
.cfan-code .hljs-literal,
.cfan-code .hljs-selector-tag,
.cfan-code .hljs-doctag {
	color: var(--cfan-code-keyword, #6f42c1);
}
.cfan-code .hljs-title,
.cfan-code .hljs-section,
.cfan-code .hljs-built_in,
.cfan-code .hljs-selector-id,
.cfan-code .hljs-selector-class {
	color: var(--cfan-code-entity, #005cc5);
}
.cfan-code .hljs-variable,
.cfan-code .hljs-template-variable,
.cfan-code .hljs-attr,
.cfan-code .hljs-attribute,
.cfan-code .hljs-params,
.cfan-code .hljs-property {
	color: var(--cfan-code-variable, #2a5c9e);
}
.cfan-code .hljs-number,
.cfan-code .hljs-symbol,
.cfan-code .hljs-meta,
.cfan-code .hljs-bullet,
.cfan-code .hljs-link {
	color: var(--cfan-code-constant, #b35900);
}
.cfan-code .hljs-tag,
.cfan-code .hljs-name {
	color: var(--cfan-code-tag, #c03d58);
}
.cfan-code .hljs-tag .hljs-attr {
	color: var(--cfan-code-variable, #2a5c9e);
}
.cfan-code .hljs-tag .hljs-string {
	color: var(--cfan-code-string, #22863a);
}
.cfan-code .hljs-operator,
.cfan-code .hljs-punctuation {
	color: var(--cfan-code-operator, inherit);
}
.cfan-code .hljs-subst {
	color: var(--cfan-code-text, inherit);
}
.cfan-code .hljs-deletion {
	color: var(--cfan-code-deleted, #b31d28);
	background: color-mix(in srgb, currentColor 8%, transparent);
}
.cfan-code .hljs-addition {
	color: var(--cfan-code-added, #22863a);
	background: color-mix(in srgb, currentColor 8%, transparent);
}
.cfan-code .hljs-emphasis {
	font-style: italic;
}
.cfan-code .hljs-strong {
	font-weight: 600;
}
