/*
 * Size chart front end.
 *
 * Every colour and metric is a custom property with a fallback, so a theme
 * restyles the chart by redefining tokens rather than by out-specifying these
 * rules. Nothing here uses !important.
 */

.wc-size-chart {
	--sc-fg: #1f2328;
	--sc-muted: #5b6470;
	--sc-bg: #ffffff;
	--sc-surface: #f6f7f9;
	--sc-border: #dfe3e8;
	--sc-accent: #1f2328;
	--sc-accent-fg: #ffffff;
	--sc-radius: 8px;
	--sc-gap: 12px;
	--sc-shadow: 0 10px 40px rgba(0, 0, 0, 0.18);
	--sc-focus: #2563eb;

	color: var(--sc-fg);
	font-size: 15px;
	line-height: 1.5;

	/*
	 * The chart is dropped into whatever container a theme's product page
	 * happens to use, and plenty of them lay the add-to-cart area out as a grid
	 * or a flex row. Without these the chart becomes a single grid item and gets
	 * squeezed into a quantity-width column. Spanning every track and refusing
	 * to flex is what keeps it a full-width block wherever it lands.
	 */
	grid-column: 1 / -1;
	flex: 0 0 100%;
	width: 100%;
	max-width: 100%;
	min-width: 0;
	box-sizing: border-box;
	margin: 0 0 14px;
	text-align: start;
}

/*
 * Inside a dialog the block is the dialog, so the escape hatches come off.
 *
 * margin MUST stay auto. A modal <dialog> is centred by the user-agent rule
 * `inset: 0; margin: auto`, so zeroing the margin pins it to the top-left
 * corner of the viewport. Setting `margin: 0` here is precisely the bug this
 * comment exists to stop someone reintroducing.
 */
.wc-size-chart-dialog.wc-size-chart {
	grid-column: auto;
	flex: none;
	margin: auto;
	width: min(760px, calc(100vw - 32px));
	max-width: min(760px, calc(100vw - 32px));
}

@media (prefers-color-scheme: dark) {
	.wc-size-chart:not([data-sc-light]) {
		--sc-fg: #e8eaed;
		--sc-muted: #a2abb6;
		--sc-bg: #16181c;
		--sc-surface: #1f2228;
		--sc-border: #343941;
		--sc-accent: #e8eaed;
		--sc-accent-fg: #16181c;
		--sc-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
		--sc-focus: #7aa2f7;
	}
}

/* --- Trigger ------------------------------------------------------------- */

.wc-size-chart__trigger {
	display: inline-flex;
	align-items: center;
	gap: 8px;
	padding: 6px 2px;
	background: none;
	border: 0;
	color: inherit;
	font: inherit;
	cursor: pointer;
	text-decoration: underline;
	text-underline-offset: 3px;
	list-style: none;
}

.wc-size-chart__trigger::-webkit-details-marker {
	display: none;
}

.wc-size-chart__trigger:hover {
	color: var(--sc-muted);
}

.wc-size-chart__trigger:focus-visible,
.wc-size-chart__scroll:focus-visible,
.wc-size-chart__tab:focus-visible,
.wc-size-chart__unit:focus-visible + span,
.wc-size-chart__close:focus-visible {
	outline: 2px solid var(--sc-focus);
	outline-offset: 2px;
	border-radius: 4px;
}

.wc-size-chart__icon {
	flex: none;
}

/* --- Panel --------------------------------------------------------------- */

.wc-size-chart__panel {
	margin-top: var(--sc-gap);
}

.wc-size-chart__title {
	margin: 0 0 4px;
	font-size: 1.25em;
	line-height: 1.25;
}

.wc-size-chart__description,
.wc-size-chart__notes {
	color: var(--sc-muted);
	font-size: 0.92em;
}

.wc-size-chart__notes {
	margin-top: var(--sc-gap);
	padding-top: var(--sc-gap);
	border-top: 1px solid var(--sc-border);
}

/* --- Toolbar: tabs and units (built by script) --------------------------- */

.wc-size-chart__toolbar:empty {
	display: none;
}

.wc-size-chart__toolbar {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
	gap: var(--sc-gap);
	margin: var(--sc-gap) 0;
}

.wc-size-chart__tabs {
	display: flex;
	flex-wrap: wrap;
	gap: 4px;
}

.wc-size-chart__tab {
	padding: 7px 14px;
	border: 1px solid var(--sc-border);
	border-radius: 999px;
	background: transparent;
	color: var(--sc-muted);
	font: inherit;
	font-size: 0.9em;
	cursor: pointer;
}

.wc-size-chart__tab[aria-selected="true"] {
	background: var(--sc-accent);
	border-color: var(--sc-accent);
	color: var(--sc-accent-fg);
}

.wc-size-chart__units {
	display: inline-flex;
	margin: 0;
	padding: 3px;
	border: 1px solid var(--sc-border);
	border-radius: 999px;
	background: var(--sc-surface);
}

.wc-size-chart__units legend {
	position: absolute;
	width: 1px;
	height: 1px;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
}

.wc-size-chart__unit-label {
	position: relative;
	display: inline-flex;
}

.wc-size-chart__unit {
	position: absolute;
	inset: 0;
	opacity: 0;
	margin: 0;
	cursor: pointer;
}

.wc-size-chart__unit + span {
	padding: 4px 14px;
	border-radius: 999px;
	color: var(--sc-muted);
	font-size: 0.85em;
	text-transform: uppercase;
	letter-spacing: 0.04em;
}

.wc-size-chart__unit:checked + span {
	background: var(--sc-bg);
	color: var(--sc-fg);
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

/* --- Table --------------------------------------------------------------- */

.wc-size-chart__scroll {
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
	border: 1px solid var(--sc-border);
	border-radius: var(--sc-radius);
}

.wc-size-chart__table {
	width: 100%;
	border-collapse: collapse;
	font-variant-numeric: tabular-nums;
}

.wc-size-chart__caption {
	position: absolute;
	width: 1px;
	height: 1px;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
}

.wc-size-chart__table th,
.wc-size-chart__table td {
	padding: 11px 14px;
	border-bottom: 1px solid var(--sc-border);
	text-align: start;
	white-space: nowrap;
}

.wc-size-chart__table thead th {
	background: var(--sc-surface);
	font-size: 0.82em;
	font-weight: 600;
	text-transform: uppercase;
	letter-spacing: 0.04em;
	color: var(--sc-muted);
}

.wc-size-chart__col-unit {
	margin-inline-start: 4px;
	opacity: 0.75;
	text-transform: none;
}

.wc-size-chart__table tbody tr:last-child th,
.wc-size-chart__table tbody tr:last-child td {
	border-bottom: 0;
}

.wc-size-chart__table tbody tr:nth-child(even) th,
.wc-size-chart__table tbody tr:nth-child(even) td {
	background: color-mix(in srgb, var(--sc-surface) 55%, transparent);
}

.wc-size-chart__table tbody tr:hover th,
.wc-size-chart__table tbody tr:hover td {
	background: color-mix(in srgb, var(--sc-surface) 100%, transparent);
}

/*
 * The size column stays put while the measurements scroll. On a phone a chart
 * is wider than the screen, and without this a shopper scrolled to "Sleeve" has
 * no idea which size row they are reading.
 */
.wc-size-chart__table tbody th[scope="row"],
.wc-size-chart__table thead th:first-child {
	position: sticky;
	inset-inline-start: 0;
	z-index: 1;
	background: var(--sc-bg);
	font-weight: 600;
}

.wc-size-chart__table thead th:first-child {
	z-index: 2;
	background: var(--sc-surface);
}

.wc-size-chart__cell--empty {
	color: var(--sc-muted);
}

/* --- Views --------------------------------------------------------------- */

.wc-size-chart__view + .wc-size-chart__view {
	margin-top: 24px;
}

.wc-size-chart__view-title {
	margin: 0 0 8px;
	font-size: 1em;
}

/* Script takes over the labelling, so the stacked headings come out. */
.wc-size-chart[data-sc-views-managed] .wc-size-chart__view-title {
	display: none;
}

.wc-size-chart[data-sc-enhanced] .wc-size-chart__view[hidden] {
	display: none;
}

.wc-size-chart[data-sc-views-managed] .wc-size-chart__view + .wc-size-chart__view {
	margin-top: 0;
}

/* --- How to measure ------------------------------------------------------ */

.wc-size-chart__guide {
	margin-top: 20px;
	padding-top: var(--sc-gap);
	border-top: 1px solid var(--sc-border);
}

.wc-size-chart__guide-title {
	margin: 0 0 10px;
	font-size: 1em;
}

.wc-size-chart__guide-body {
	display: grid;
	gap: 16px;
	grid-template-columns: 1fr;
}

@media (min-width: 620px) {
	.wc-size-chart__guide-body:has(.wc-size-chart__guide-figure):has(.wc-size-chart__guide-steps) {
		grid-template-columns: minmax(0, 220px) minmax(0, 1fr);
		align-items: start;
	}
}

.wc-size-chart__guide-figure {
	margin: 0;
}

.wc-size-chart__guide-image {
	max-width: 100%;
	height: auto;
	border-radius: var(--sc-radius);
}

.wc-size-chart__guide-steps {
	margin: 0;
	padding-inline-start: 1.2em;
	color: var(--sc-muted);
	font-size: 0.92em;
}

.wc-size-chart__guide-steps li + li {
	margin-top: 6px;
}

/* --- Dialog -------------------------------------------------------------- */

.wc-size-chart-dialog {
	width: min(760px, calc(100vw - 32px));
	max-height: min(86vh, 900px);
	padding: 0;
	border: 0;
	border-radius: var(--sc-radius, 8px);
	background: var(--sc-bg, #fff);
	color: var(--sc-fg, #1f2328);
	box-shadow: var(--sc-shadow, 0 10px 40px rgba(0, 0, 0, 0.18));
	overflow: hidden;
}

.wc-size-chart-dialog::backdrop {
	background: rgba(0, 0, 0, 0.55);
}

.wc-size-chart-dialog__body {
	max-height: min(86vh, 900px);
	padding: 22px;
	overflow-y: auto;
}

.wc-size-chart__close {
	position: absolute;
	inset-block-start: 12px;
	inset-inline-end: 12px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 34px;
	height: 34px;
	padding: 0;
	border: 1px solid var(--sc-border);
	border-radius: 50%;
	background: var(--sc-bg);
	color: var(--sc-fg);
	font-size: 18px;
	line-height: 1;
	cursor: pointer;
}

.wc-size-chart-dialog .wc-size-chart__panel {
	margin-top: 0;
}

.wc-size-chart-dialog .wc-size-chart__title {
	padding-inline-end: 44px;
}

@media (prefers-reduced-motion: no-preference) {
	.wc-size-chart-dialog[open] {
		animation: wc-size-chart-in 160ms ease-out;
	}
}

@keyframes wc-size-chart-in {
	from {
		opacity: 0;
		transform: translateY(8px);
	}
}

/* A button rendered in place of <summary> once script takes over. */
.wc-size-chart__trigger[type="button"] {
	padding: 6px 2px;
}
