/*
 * PureCSS.com
 * https://purecss.com/character-counter/
 * A live character count for a single-line text field, read from the field's own width with container query units. No JavaScript.
 */

/* ── The counter ──
   In a monospace font every character is 1ch wide, and field-sizing: content makes the
   field exactly as wide as its text, so a field holding n characters is n ch wide. A size
   container in the field's own grid column has that width too, and inside it 100cqi / 1ch
   is the count. Lengths are in ch and em, not rem: WebKit misplaces rem grid tracks inside
   an element with CSS zoom, and the site plays this demo zoomed. */

/* the count as a whole number; registered so the division below rounds to an integer */
@property --ctr-n {
	syntax: "<integer>";
	inherits: true;
	initial-value: 0;
}

/* sizes the field to the space it has: everything inside is in em and ch, so it scales whole.
   A size container takes no width from its content, so it is given one */
.demo-character-counter .ctr-demo {
	container: ctr-demo / inline-size;
	width: 100%;
	display: grid;
	justify-items: center;
	gap: 1.25rem;
}

/* columns: inner padding, the text, the rest of the row, inner padding.
   Thirty characters of room, the field's maxlength */
.demo-character-counter .ctr {
	display: grid;
	grid-template-columns: 1em auto 1fr 1em;
	width: calc(30ch + 2em);
	font: clamp(.8125rem, 4.6cqi, 1.25rem)/1.5 var(--font-mono);
}

/* the label is the visible box, so a click anywhere in it focuses the field */
.demo-character-counter .ctr-box {
	grid-column: 1 / -1;
	grid-row: 1;
	padding: 1em calc(1.3333em - 2px) 0;
	border: 2px solid var(--border);
	border-radius: var(--radius);
	background: var(--surface-2);
	color: var(--text);
	font: 600 .75em/1.2 var(--font-sans);
	letter-spacing: .02em;
	cursor: text;
}

.demo-character-counter .ctr:focus-within .ctr-box {
	border-color: var(--accent);
}

.demo-character-counter .ctr:has(.ctr-input:focus-visible) .ctr-box {
	outline: 2px solid var(--accent);
	outline-offset: 3px;
}

/* no padding, border or background at the sides, so the field's width
   is the typed text's width and nothing else */
.demo-character-counter input.ctr-input {
	grid-column: 2 / 4;
	grid-row: 1;
	z-index: 1;
	min-width: 0;
	padding: 2.15em 0 .75em;
	border: 0;
	background: none;
	color: var(--text);
	caret-color: var(--accent);
	font: inherit;
	letter-spacing: 0;
	outline: none;
}

.demo-character-counter .ctr-input::placeholder {
	color: var(--text-muted);
	opacity: 1;
}

/* the count's container: in the text's own column, so exactly as wide as the text */
.demo-character-counter .ctr-len {
	grid-column: 2 / 4;
	grid-row: 1;
	z-index: 1;
	padding-top: calc(.7em + 2px);
	pointer-events: none;
}

/* 30ch wide whatever the text's width, so the count ends at the box's right padding */
.demo-character-counter .ctr-count {
	display: flex;
	justify-content: flex-end;
	width: 30ch;
}

/* without field-sizing the field keeps one width, so only the limit shows.
   The state color is the pill's, so the number itself always reads at full contrast */
.demo-character-counter .ctr-count::before {
	--ctr-tone: var(--text-muted);
	content: "Max 30";
	padding: .15em .6em;
	border-radius: 999px;
	background: color-mix(in srgb, var(--ctr-tone) 22%, transparent);
	box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ctr-tone) 60%, transparent);
	color: var(--text);
	font: 500 .75em/1.2 var(--font-mono);
}

.demo-character-counter .ctr-limit {
	grid-column: 1 / -1;
	margin-top: .55em;
	padding-inline: 1.3333em;
	color: var(--text-muted);
	font: .75em/1.4 var(--font-sans);
}

/* the demo's teaching switch: outlines the size container the count is read from */
.demo-character-counter .ctr-show {
	display: flex;
	align-items: center;
	gap: .5rem;
	color: var(--text-muted);
	font: .875rem/1.3 var(--font-sans);
	cursor: pointer;
}

.demo-character-counter .ctr-show input {
	width: 1rem;
	height: 1rem;
	margin: 0;
	accent-color: var(--accent);
}

.demo-character-counter .ctr-demo:has(.ctr-show input:checked) .ctr-len {
	outline: 2px dashed var(--accent);
	outline-offset: -2px;
	background: var(--accent-dim);
}

/* the count, switched on only where field-sizing is supported */
@supports (field-sizing: content) {
	.demo-character-counter input.ctr-input {
		grid-column: 2;
		field-sizing: content;
	}

	.demo-character-counter .ctr-len {
		grid-column: 2;
		container: ctr-len / inline-size;
	}

	/* 100cqi is the text's width and 1ch one character. tan(atan2(a, b)) is a / b
	   as a plain number, and the <integer> type rounds it, so this is the count */
	.demo-character-counter .ctr-count {
		--ctr-n: tan(atan2(100cqi, 1ch));
		counter-reset: ctr-n var(--ctr-n);
	}

	/* an empty field is as wide as its placeholder, so the empty count is pinned to 0 */
	.demo-character-counter .ctr-input:placeholder-shown ~ .ctr-len .ctr-count {
		--ctr-n: 0;
	}

	/* clamp(0, n - 24, 1) is 0 up to 24 characters and 1 from 25, so the count turns
	   orange with five or fewer left, then coral at 30 */
	.demo-character-counter .ctr-count::before {
		--ctr-tone: color-mix(in srgb, var(--coral) calc(clamp(0, var(--ctr-n) - 29, 1) * 100%), color-mix(in srgb, var(--orange) calc(clamp(0, var(--ctr-n) - 24, 1) * 100%), var(--text-muted)));
		content: counter(ctr-n) " / 30";
	}
}

/* ── Variant: characters left, in words, one container query per count ── */

/* 24 characters of room, so the rest of the row is exactly the space left */
.demo-character-counter .ctrl {
	width: calc(24ch + 2em);
}

/* the static limit sits on the label's line, outside the label */
.demo-character-counter .ctrl-limit {
	grid-column: 2 / 4;
	grid-row: 1;
	z-index: 1;
	align-self: start;
	justify-self: end;
	padding-top: calc(1em + 2px);
	color: var(--text-muted);
	font: .75em/1.2 var(--font-sans);
	pointer-events: none;
}

/* only shown where the field's width follows its text */
.demo-character-counter .ctrl-rest {
	display: none;
	grid-column: 3;
	grid-row: 2;
	margin-top: .55em;
}

/* right-aligned on the box's inner edge; a sentence wider than the space
   left runs out to the left */
.demo-character-counter .ctrl-count {
	display: flex;
	justify-content: flex-end;
}

/* the same pill as the main count */
.demo-character-counter .ctrl-count::before {
	--ctr-tone: var(--coral);
	content: "No characters left";
	padding: .15em .6em;
	border-radius: 999px;
	background: color-mix(in srgb, var(--ctr-tone) 22%, transparent);
	box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ctr-tone) 60%, transparent);
	color: var(--text);
	font: 500 .75em/1.2 var(--font-mono);
	white-space: nowrap;
}

@supports (field-sizing: content) {
	.demo-character-counter .ctrl-rest {
		display: block;
		container: ctrl-left / inline-size;
	}
}

/* the container is the rest of the row: k characters left is k ch, one query per count */
@container ctrl-left (width > 0.5ch) { .demo-character-counter .ctrl-count::before { content: "1 character left"; } }
@container ctrl-left (width > 1.5ch) { .demo-character-counter .ctrl-count::before { content: "2 characters left"; } }
@container ctrl-left (width > 2.5ch) { .demo-character-counter .ctrl-count::before { content: "3 characters left"; } }
@container ctrl-left (width > 3.5ch) { .demo-character-counter .ctrl-count::before { content: "4 characters left"; } }
@container ctrl-left (width > 4.5ch) { .demo-character-counter .ctrl-count::before { content: "5 characters left"; } }
@container ctrl-left (width > 5.5ch) { .demo-character-counter .ctrl-count::before { content: "6 characters left"; } }
@container ctrl-left (width > 6.5ch) { .demo-character-counter .ctrl-count::before { content: "7 characters left"; } }
@container ctrl-left (width > 7.5ch) { .demo-character-counter .ctrl-count::before { content: "8 characters left"; } }
@container ctrl-left (width > 8.5ch) { .demo-character-counter .ctrl-count::before { content: "9 characters left"; } }
@container ctrl-left (width > 9.5ch) { .demo-character-counter .ctrl-count::before { content: "10 characters left"; } }
@container ctrl-left (width > 10.5ch) { .demo-character-counter .ctrl-count::before { content: "11 characters left"; } }
@container ctrl-left (width > 11.5ch) { .demo-character-counter .ctrl-count::before { content: "12 characters left"; } }
@container ctrl-left (width > 12.5ch) { .demo-character-counter .ctrl-count::before { content: "13 characters left"; } }
@container ctrl-left (width > 13.5ch) { .demo-character-counter .ctrl-count::before { content: "14 characters left"; } }
@container ctrl-left (width > 14.5ch) { .demo-character-counter .ctrl-count::before { content: "15 characters left"; } }
@container ctrl-left (width > 15.5ch) { .demo-character-counter .ctrl-count::before { content: "16 characters left"; } }
@container ctrl-left (width > 16.5ch) { .demo-character-counter .ctrl-count::before { content: "17 characters left"; } }
@container ctrl-left (width > 17.5ch) { .demo-character-counter .ctrl-count::before { content: "18 characters left"; } }
@container ctrl-left (width > 18.5ch) { .demo-character-counter .ctrl-count::before { content: "19 characters left"; } }
@container ctrl-left (width > 19.5ch) { .demo-character-counter .ctrl-count::before { content: "20 characters left"; } }
@container ctrl-left (width > 20.5ch) { .demo-character-counter .ctrl-count::before { content: "21 characters left"; } }
@container ctrl-left (width > 21.5ch) { .demo-character-counter .ctrl-count::before { content: "22 characters left"; } }
@container ctrl-left (width > 22.5ch) { .demo-character-counter .ctrl-count::before { content: "23 characters left"; } }
@container ctrl-left (width > 23.5ch) { .demo-character-counter .ctrl-count::before { content: "24 characters left"; } }

/* five or fewer left */
@container ctrl-left (width > .5ch) { .demo-character-counter .ctrl-count::before { --ctr-tone: var(--orange); } }
@container ctrl-left (width > 5.5ch) { .demo-character-counter .ctrl-count::before { --ctr-tone: var(--text-muted); } }

/* ── Variant: a meter bar that fills with the text ── */

/* room under the text for the bar */
.demo-character-counter .ctrm input.ctr-input {
	padding-bottom: 1.4em;
}

/* the rail spans the text's room; the fill sits in the text's own column, so it is
   exactly as wide as the text with no number in between */
.demo-character-counter .ctrm-rail,
.demo-character-counter .ctrm-len {
	display: none;
	grid-row: 1;
	z-index: 1;
	align-self: end;
	height: .3125em;
	margin-bottom: .7em;
	border-radius: 999px;
	pointer-events: none;
}

.demo-character-counter .ctrm-rail {
	grid-column: 2 / 4;
	/* a tick every five characters */
	background: repeating-linear-gradient(90deg, transparent 0 calc(5ch - 2px), var(--text-muted) calc(5ch - 2px) 5ch), var(--border);
}

.demo-character-counter .ctrm-fill {
	height: 100%;
	border-radius: inherit;
	background: var(--teal);
}

@supports (field-sizing: content) {
	.demo-character-counter .ctrm-rail,
	.demo-character-counter .ctrm-len {
		display: block;
	}

	.demo-character-counter .ctrm-len {
		grid-column: 2;
		container: ctrm-len / inline-size;
	}

	/* the count only picks the fill's color: teal, orange from 25, coral at 30 */
	.demo-character-counter .ctrm-fill {
		--ctr-n: tan(atan2(100cqi, 1ch));
		background: color-mix(in srgb, var(--coral) calc(clamp(0, var(--ctr-n) - 29, 1) * 100%), color-mix(in srgb, var(--orange) calc(clamp(0, var(--ctr-n) - 24, 1) * 100%), var(--teal)));
	}
}

/* ── Variant: in a form, over the limit and the button reacts ── */

/* pattern sets the limit at 20, maxlength stops the field at 30, so the overrun
   is allowed in, can be seen, and is never longer than the row */
.demo-character-counter .ctrf {
	gap: 1.25rem;
}

.demo-character-counter .ctrf .ctr-count::before {
	content: "Max 20";
}

.demo-character-counter .ctrf:has(.ctr-input:invalid) .ctr-box {
	border-color: var(--coral);
}

.demo-character-counter .ctrf-save {
	padding: .65rem 1.4rem;
	border: 0;
	border-radius: 999px;
	background: var(--accent);
	color: var(--bg);
	font: 600 .9375rem/1.2 var(--font-sans);
	cursor: pointer;
}

.demo-character-counter .ctrf-save:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 3px;
}

/* the form will not submit while the field is invalid, so the button says so;
   its label keeps full contrast, since it can still be pressed */
.demo-character-counter .ctrf:has(.ctr-input:invalid) .ctrf-save {
	background: var(--border);
	color: var(--text);
	cursor: not-allowed;
}

@supports (field-sizing: content) {
	.demo-character-counter .ctrf .ctr-count::before {
		--ctr-tone: color-mix(in srgb, var(--coral) calc(clamp(0, var(--ctr-n) - 20, 1) * 100%), color-mix(in srgb, var(--orange) calc(clamp(0, var(--ctr-n) - 15, 1) * 100%), var(--text-muted)));
		content: counter(ctr-n) " / 20";
	}

	/* the field is exactly as wide as its text, so from 20ch on its background
	   sits under the characters past the limit */
	.demo-character-counter .ctrf input.ctr-input {
		background: linear-gradient(90deg, transparent 20ch, var(--coral-dim) 20ch) content-box;
	}
}
