/*
 * Finance visualisations (Figma Viz/Budget, Viz/Savings, Viz/Credit, Viz/Debt) inside the topic cards.
 * Markup is a Custom HTML block in patterns/topics.php; viz.js drives the controls. Without JavaScript
 * every chart shows its default state (665 €, 50 € a month, hidden costs on, the full debt), so the
 * numbers are always readable. Heights are fractions set inline (--kchsi-h, --kchsi-base) and scaled
 * by the viz/bar-height token.
 *
 * Tones: needs navy, wants amber, savings teal, debt coral. The colour sits on the dot, bar or meter;
 * text stays navy (contrast rule).
 */

.kchsi-viz {
	display: flex;
	flex-direction: column;
	gap: var(--wp--custom--space--lg);
}

.kchsi-viz p,
.kchsi-viz ul,
.kchsi-viz ol,
.kchsi-viz fieldset {
	margin: 0;
}

.kchsi-tone--needs { --kchsi-tone: var(--wp--custom--color--bg--inverse-elevated); }
.kchsi-tone--wants { --kchsi-tone: var(--wp--custom--color--status--warning); }
.kchsi-tone--savings { --kchsi-tone: var(--wp--custom--color--bg--brand); }
.kchsi-tone--debt { --kchsi-tone: var(--wp--custom--color--bg--accent); }

.kchsi-viz__row {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
	gap: var(--wp--custom--space--sm);
}

.kchsi-viz__row--end {
	align-items: flex-end;
}

.kchsi-viz__label {
	padding: 0;
	font-weight: var(--wp--custom--weight--semibold);
	line-height: var(--wp--custom--line-height--label);
}

.kchsi-viz__muted,
.kchsi-viz__note,
.kchsi-viz__caption,
.kchsi-viz__scale {
	font-size: var(--wp--custom--font-size--14);
	line-height: var(--wp--custom--line-height--small);
	color: var(--wp--custom--color--text--muted);
}

.kchsi-viz__pill {
	display: inline-flex;
	padding: var(--wp--custom--space--xs) var(--wp--custom--space--md);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--bg--brand-soft);
	color: var(--wp--custom--color--text--brand);
	font-weight: var(--wp--custom--weight--semibold);
	line-height: var(--wp--custom--line-height--label);
	font-variant-numeric: tabular-nums;
}

.kchsi-viz__pill.kchsi-tone--debt {
	background-color: var(--wp--custom--color--bg--accent-soft);
	color: var(--wp--custom--color--text--accent);
}

.kchsi-viz__total {
	font-family: var(--wp--custom--font--heading);
	font-size: var(--wp--custom--type--heading-3);
	font-weight: var(--wp--custom--weight--heading-3);
	line-height: var(--wp--custom--line-height--heading-3);
	font-variant-numeric: tabular-nums;
}

.kchsi-viz__dot {
	flex: none;
	width: var(--wp--custom--viz--dot);
	height: var(--wp--custom--viz--dot);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--kchsi-tone);
}

/* ---------- Range slider (budget) ---------- */

.kchsi-range {
	--kchsi-fill: 44%;
	width: 100%;
	height: var(--wp--custom--viz--thumb);
	margin: 0;
	appearance: none;
	background: transparent;
	cursor: pointer;
}

.kchsi-range::-webkit-slider-runnable-track {
	height: var(--wp--custom--viz--track);
	border-radius: var(--wp--custom--radius--pill);
	background: linear-gradient(to right, var(--wp--custom--color--bg--brand) var(--kchsi-fill), var(--wp--custom--color--bg--muted) var(--kchsi-fill));
}

.kchsi-range::-moz-range-track {
	height: var(--wp--custom--viz--track);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--bg--muted);
}

.kchsi-range::-moz-range-progress {
	height: var(--wp--custom--viz--track);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--bg--brand);
}

.kchsi-range::-webkit-slider-thumb {
	width: var(--wp--custom--viz--thumb);
	height: var(--wp--custom--viz--thumb);
	margin-top: calc((var(--wp--custom--viz--track) - var(--wp--custom--viz--thumb)) / 2);
	appearance: none;
	border: var(--wp--custom--border--width-strong) solid var(--wp--custom--color--bg--brand);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--bg--surface);
	box-shadow: var(--wp--preset--shadow--floating);
}

.kchsi-range::-moz-range-thumb {
	width: var(--wp--custom--viz--thumb);
	height: var(--wp--custom--viz--thumb);
	box-sizing: border-box;
	border: var(--wp--custom--border--width-strong) solid var(--wp--custom--color--bg--brand);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--bg--surface);
	box-shadow: var(--wp--preset--shadow--floating);
}

.kchsi-range:focus-visible {
	outline: none;
}

.kchsi-range:focus-visible::-webkit-slider-thumb {
	outline: var(--wp--custom--border--width-strong) solid var(--wp--custom--color--border--focus);
	outline-offset: var(--wp--custom--border--width-strong);
}

.kchsi-range:focus-visible::-moz-range-thumb {
	outline: var(--wp--custom--border--width-strong) solid var(--wp--custom--color--border--focus);
	outline-offset: var(--wp--custom--border--width-strong);
}

.kchsi-viz__scale {
	display: flex;
	justify-content: space-between;
	margin-top: calc(-1 * var(--wp--custom--space--sm));
}

/* Split tiles: wrap to one per row when narrow (Figma: min width 160). */
.kchsi-viz__split {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(var(--wp--custom--spacing--32), 1fr));
	gap: var(--wp--custom--space--md);
}

.kchsi-viz__tile {
	display: flex;
	flex-direction: column;
	gap: var(--wp--custom--space--sm);
	padding: var(--wp--custom--space--md);
	border: var(--wp--custom--border--width) solid var(--wp--custom--color--border--default);
	border-radius: var(--wp--custom--radius--control);
	background-color: var(--wp--custom--glass--fill);
}

.kchsi-viz__tile.kchsi-tone--savings {
	box-shadow: var(--wp--preset--shadow--glow-soft-brand);
}

.kchsi-viz__tile-head {
	display: flex;
	align-items: center;
	gap: var(--wp--custom--space--sm);
	font-size: var(--wp--custom--font-size--14);
	line-height: var(--wp--custom--line-height--small);
	color: var(--wp--custom--color--text--secondary);
}

.kchsi-viz__tile-head .kchsi-viz__muted {
	margin-left: auto;
}

.kchsi-viz__amount {
	font-family: var(--wp--custom--font--heading);
	font-size: var(--wp--custom--type--heading-3);
	font-weight: var(--wp--custom--weight--heading-3);
	line-height: var(--wp--custom--line-height--heading-3);
	font-variant-numeric: tabular-nums;
}

.kchsi-viz__meter {
	display: block;
	height: var(--wp--custom--viz--track);
	border-radius: var(--wp--custom--radius--pill);
	background: linear-gradient(to right, var(--kchsi-tone) calc(var(--kchsi-share) * 100%), var(--wp--custom--color--bg--muted) 0);
}

/* ---------- Segmented control (savings) ---------- */

.kchsi-viz__segmented {
	display: flex;
	flex-direction: column;
	gap: var(--wp--custom--space--sm);
	padding: 0;
	border: 0;
}

.kchsi-viz__options {
	display: grid;
	grid-template-columns: repeat(3, minmax(0, 1fr));
	gap: var(--wp--custom--space--xs);
	padding: var(--wp--custom--space--xs);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--bg--muted);
}

.kchsi-viz__options label {
	position: relative;
	display: flex;
	justify-content: center;
	padding-block: var(--wp--custom--space--sm);
	border-radius: var(--wp--custom--radius--pill);
	color: var(--wp--custom--color--text--muted);
	line-height: var(--wp--custom--line-height--label);
	cursor: pointer;
	transition:
		background-color var(--wp--custom--motion--duration--fast) var(--wp--custom--motion--ease--out),
		box-shadow var(--wp--custom--motion--duration--fast) var(--wp--custom--motion--ease--out);
}

.kchsi-viz__options input {
	position: absolute;
	opacity: 0;
	pointer-events: none;
}

.kchsi-viz__options label:has(input:checked) {
	background-color: var(--wp--custom--color--bg--surface);
	box-shadow: var(--wp--preset--shadow--card);
	color: var(--wp--custom--color--text--primary);
	font-weight: var(--wp--custom--weight--semibold);
}

.kchsi-viz__options label:has(input:focus-visible) {
	outline: var(--wp--custom--border--width-strong) solid var(--wp--custom--color--border--focus);
	outline-offset: var(--wp--custom--border--width-strong);
}

/* ---------- Bar columns (savings, credit) ---------- */

.kchsi-viz__bars {
	display: grid;
	grid-auto-columns: minmax(0, 1fr);
	grid-auto-flow: column;
	align-items: end;
	gap: var(--wp--custom--space--lg);
	min-height: calc(var(--wp--custom--viz--bar-height) + var(--wp--custom--spacing--16));
}

.kchsi-viz__col {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: var(--wp--custom--space--sm);
	min-width: 0;
}

.kchsi-viz__value {
	font-weight: var(--wp--custom--weight--semibold);
	line-height: var(--wp--custom--line-height--label);
	font-variant-numeric: tabular-nums;
	white-space: nowrap;
}

.kchsi-viz__bar {
	display: block;
	width: 100%;
	height: max(var(--wp--custom--viz--track), calc(var(--kchsi-h) * var(--wp--custom--viz--bar-height)));
	border-radius: var(--wp--custom--radius--control);
	background-color: color-mix(in srgb, var(--wp--custom--color--bg--brand) var(--kchsi-mix, 100%), transparent);
	transition: height var(--wp--custom--motion--duration--slow) var(--wp--custom--motion--ease--out);
}

.kchsi-viz__col:last-child .kchsi-viz__bar {
	box-shadow: var(--wp--preset--shadow--glow-soft-brand);
}

/* Credit: stacked segments, the hidden costs grow from zero when revealed. */
.kchsi-viz__stack {
	display: flex;
	flex-direction: column;
	gap: var(--wp--custom--viz--gap);
	width: 100%;
	border-radius: var(--wp--custom--radius--control);
}

.kchsi-viz__seg {
	display: block;
	height: max(var(--wp--custom--spacing--1), calc(var(--kchsi-h) * var(--wp--custom--viz--bar-height)));
	border-radius: var(--wp--custom--radius--small);
	background-color: var(--kchsi-tone);
	transition:
		height var(--wp--custom--motion--duration--slow) var(--wp--custom--motion--ease--out),
		opacity var(--wp--custom--motion--duration--base) var(--wp--custom--motion--ease--out);
}

.kchsi-viz__seg--hidden {
	box-shadow: var(--wp--preset--shadow--glow-soft-accent);
}

.kchsi-viz:not(.is-revealed) .kchsi-viz__seg--hidden {
	height: 0;
	opacity: 0;
}

.kchsi-viz:not(.is-revealed) .kchsi-viz__stack--real {
	outline: var(--wp--custom--border--width) dashed var(--wp--custom--color--border--strong);
	outline-offset: var(--wp--custom--space--xs);
}

.kchsi-viz__note {
	padding: var(--wp--custom--space--md);
	border-radius: var(--wp--custom--radius--control);
	background-color: var(--wp--custom--color--bg--muted);
	color: var(--wp--custom--color--text--primary);
}

.kchsi-viz.is-revealed .kchsi-viz__note {
	background-color: var(--wp--custom--color--bg--accent-soft);
}

/* Switch: the toggle for the hidden costs. */
.kchsi-switch {
	display: inline-flex;
	align-items: center;
	gap: var(--wp--custom--space--md);
	padding: 0;
	border: 0;
	background: none;
	color: var(--wp--custom--color--text--primary);
	font: inherit;
	font-weight: var(--wp--custom--weight--semibold);
	line-height: var(--wp--custom--line-height--label);
	cursor: pointer;
}

.kchsi-switch__track {
	position: relative;
	flex: none;
	width: var(--wp--custom--spacing--12);
	height: var(--wp--custom--spacing--7);
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--border--strong);
	transition: background-color var(--wp--custom--motion--duration--base) var(--wp--custom--motion--ease--out);
}

.kchsi-switch__track::after {
	content: "";
	position: absolute;
	top: var(--wp--custom--viz--gap);
	left: var(--wp--custom--viz--gap);
	width: calc(var(--wp--custom--spacing--7) - 2 * var(--wp--custom--viz--gap));
	height: calc(var(--wp--custom--spacing--7) - 2 * var(--wp--custom--viz--gap));
	border-radius: var(--wp--custom--radius--pill);
	background-color: var(--wp--custom--color--bg--surface);
	box-shadow: var(--wp--preset--shadow--card);
	transition: transform var(--wp--custom--motion--duration--base) var(--wp--custom--motion--ease--out);
}

.kchsi-switch[aria-pressed="true"] .kchsi-switch__track {
	background-color: var(--wp--custom--color--bg--accent);
}

.kchsi-switch[aria-pressed="true"] .kchsi-switch__track::after {
	transform: translateX(calc(var(--wp--custom--spacing--12) - var(--wp--custom--spacing--7)));
}

/* ---------- Waterfall (debt) ---------- */

.kchsi-viz__waterfall {
	position: relative;
	display: grid;
	grid-template-columns: repeat(9, minmax(0, 1fr));
	gap: var(--wp--custom--space--sm);
	align-items: end;
	height: var(--wp--custom--viz--bar-height);
	border-bottom: var(--wp--custom--border--width) solid var(--wp--custom--color--border--default);
	background-image: linear-gradient(to top, var(--wp--custom--color--border--default) var(--wp--custom--border--width), transparent var(--wp--custom--border--width));
	background-size: 100% 50%;
}

.kchsi-viz__step {
	display: block;
	height: max(var(--wp--custom--spacing--1), calc(var(--kchsi-h) * var(--wp--custom--viz--bar-height)));
	margin-bottom: calc(var(--kchsi-base, 0) * var(--wp--custom--viz--bar-height));
	border-radius: var(--wp--custom--radius--small);
	background-color: var(--kchsi-tone);
	transform-origin: center bottom;
	transition:
		transform var(--wp--custom--motion--duration--slow) var(--wp--custom--motion--ease--out),
		opacity var(--wp--custom--motion--duration--base) var(--wp--custom--motion--ease--out);
}

.kchsi-viz__step.is-total {
	box-shadow: var(--wp--preset--shadow--glow-soft-accent);
}

[data-viz-step="1"] .kchsi-viz__step[data-step="2"],
[data-viz-step="1"] .kchsi-viz__step[data-step="3"],
[data-viz-step="2"] .kchsi-viz__step[data-step="3"] {
	opacity: 0;
	transform: scaleY(0);
}

.kchsi-viz__legend {
	display: flex;
	flex-wrap: wrap;
	gap: var(--wp--custom--space--sm) var(--wp--custom--space--lg);
	padding: 0;
	list-style: none;
}

.kchsi-viz__legend li {
	display: flex;
	align-items: center;
	gap: var(--wp--custom--space--sm);
	font-size: var(--wp--custom--font-size--14);
	line-height: var(--wp--custom--line-height--small);
	color: var(--wp--custom--color--text--secondary);
}

.kchsi-viz__timeline {
	display: grid;
	grid-template-columns: repeat(3, minmax(0, 1fr));
	gap: var(--wp--custom--space--sm);
	padding: 0;
	list-style: none;
}

.kchsi-viz__timeline button {
	display: flex;
	flex-direction: column;
	gap: var(--wp--custom--space--xs);
	width: 100%;
	height: 100%;
	padding: var(--wp--custom--space--sm);
	border: var(--wp--custom--border--width) solid var(--wp--custom--color--border--default);
	border-radius: var(--wp--custom--radius--control);
	background: none;
	color: var(--wp--custom--color--text--muted);
	font: inherit;
	font-size: var(--wp--custom--font-size--14);
	line-height: var(--wp--custom--line-height--small);
	text-align: left;
	cursor: pointer;
	transition:
		border-color var(--wp--custom--motion--duration--base) var(--wp--custom--motion--ease--out),
		box-shadow var(--wp--custom--motion--duration--base) var(--wp--custom--motion--ease--out);
}

.kchsi-viz__timeline strong {
	color: var(--wp--custom--color--text--primary);
	font-weight: var(--wp--custom--weight--semibold);
}

.kchsi-viz__timeline button[aria-pressed="true"] {
	border-color: var(--wp--custom--color--bg--accent);
	background-color: var(--wp--custom--color--bg--surface);
	box-shadow: var(--wp--preset--shadow--glow-soft-accent);
	color: var(--wp--custom--color--text--secondary);
}

@media (max-width: 767px) {
	.kchsi-viz__waterfall {
		gap: var(--wp--custom--space--xs);
	}

	.kchsi-viz__timeline {
		grid-template-columns: minmax(0, 1fr);
	}
}

@media (prefers-reduced-motion: reduce) {
	.kchsi-viz *,
	.kchsi-viz *::after {
		transition: none;
	}
}
