/**
 * Kreativdekor Variations Table — base styles.
 *
 * Everything is scoped to .kd-vt and driven by custom properties, so a theme
 * or child theme can restyle the table by redefining the variables alone.
 */

.kd-vt {
	--kd-vt-border: #e2e2e2;
	--kd-vt-border-strong: #c9c9c9;
	--kd-vt-text: inherit;
	--kd-vt-muted: #6d6d6d;
	--kd-vt-row-hover: rgba(0, 0, 0, 0.02);
	--kd-vt-row-chosen: rgba(0, 0, 0, 0.04);
	--kd-vt-accent: currentColor;
	--kd-vt-error: #b32d2e;
	--kd-vt-success: #1e8f4e;
	--kd-vt-radius: 4px;
	--kd-vt-space: 0.75rem;
	--kd-vt-space-sm: 0.375rem;
	--kd-vt-cell-padding-x: 0.625rem;
	--kd-vt-font-size: 0.95em;
	--kd-vt-image-size: 56px;
	--kd-vt-qty-width: 4.5rem;
	--kd-vt-total-width: 5rem;

	color: var(--kd-vt-text);
	font-size: var(--kd-vt-font-size);
	margin-block: var(--kd-vt-space);
}

.kd-vt *,
.kd-vt *::before,
.kd-vt *::after {
	box-sizing: border-box;
}

.kd-vt [hidden] {
	display: none !important;
}

/* Toolbar ----------------------------------------------------------------- */

.kd-vt__toolbar {
	align-items: center;
	display: flex;
	flex-wrap: wrap;
	gap: var(--kd-vt-space-sm);
	margin-bottom: var(--kd-vt-space);
}

.kd-vt__search-label {
	font-weight: 600;
}

.kd-vt__search {
	flex: 1 1 12rem;
	max-width: 20rem;
	min-width: 0;
}

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

/*
 * Themes style the add-to-cart form as a flex container - XStore does it with
 * `.etheme-add-to-cart-form form` - and our form then inherits that. As flex
 * items the table and the button row shrink to their own content instead of
 * filling the width, which squeezes the table into a fraction of the space it
 * has. Our form is a plain block and needs to stay one.
 */
.kd-vt .kd-vt__form {
	display: block;
}

.kd-vt__scroll {
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
	width: 100%;
}

.kd-vt__table {
	border-collapse: collapse;
	margin: 0;
	width: 100%;
}

.kd-vt__table th,
.kd-vt__table td {
	border-bottom: 1px solid var(--kd-vt-border);
	padding: var(--kd-vt-space-sm) var(--kd-vt-cell-padding-x);
	text-align: left;
	vertical-align: middle;
}

.kd-vt__table thead th {
	border-bottom-width: 2px;
	border-bottom-color: var(--kd-vt-border-strong);
	font-weight: 600;
	white-space: nowrap;
}

.kd-vt__heading--sortable {
	cursor: pointer;
	user-select: none;
}

.kd-vt__heading--sortable::after {
	content: "\2195";
	font-size: 0.8em;
	margin-left: 0.35em;
	opacity: 0.35;
}

.kd-vt__heading--sortable[aria-sort="ascending"]::after {
	content: "\2191";
	opacity: 1;
}

.kd-vt__heading--sortable[aria-sort="descending"]::after {
	content: "\2193";
	opacity: 1;
}

.kd-vt__heading--sortable:focus-visible,
.kd-vt__page:focus-visible,
.kd-vt__qty:focus-visible,
.kd-vt__submit:focus-visible {
	outline: 2px solid var(--kd-vt-accent);
	outline-offset: 2px;
}

.kd-vt__row:hover {
	background-color: var(--kd-vt-row-hover);
}

.kd-vt__row--chosen {
	background-color: var(--kd-vt-row-chosen);
}

.kd-vt__row--unavailable {
	color: var(--kd-vt-muted);
}

/*
 * The image column must not react to how wide the rest of the table is.
 * Typing a quantity turns a row total from "-" into a price, which widens the
 * total column, and an auto-layout table takes that width from its neighbours.
 * Most themes also set `img { max-width: 100% }`, so without the rules below the
 * thumbnail would follow the column and change the height of the whole row.
 *
 * `width: 1%` makes the cell shrink to fit, and the image gets an explicit
 * height, so the row height is fixed no matter what happens horizontally.
 */
.kd-vt__cell--image {
	min-width: calc(var(--kd-vt-image-size) + var(--kd-vt-cell-padding-x) * 2);
	width: 1%;
	white-space: nowrap;
}

.kd-vt .kd-vt__image {
	border-radius: var(--kd-vt-radius);
	display: block;
	height: var(--kd-vt-image-size);
	max-height: var(--kd-vt-image-size);
	max-width: var(--kd-vt-image-size);
	min-width: var(--kd-vt-image-size);
	object-fit: contain;
	width: var(--kd-vt-image-size);
}

/*
 * Reserve a little room for a row total, so the column does not jump from the
 * width of a dash to the width of a price as the customer types. Deliberately
 * modest: the table often lives in a narrow product summary column, and an
 * over-generous reservation here is what pushes the last column out of view.
 */
.kd-vt__cell--total,
.kd-vt__grand-total {
	min-width: var(--kd-vt-total-width);
}

/*
 * Never break an amount in half - but do let the cell wrap around it. In
 * particular WooCommerce's tax suffix ("ink mva") goes on its own line rather
 * than forcing the whole price column wide enough to hold both.
 */
.kd-vt__price,
.kd-vt__row-total {
	display: block;
	line-height: inherit;
}

.kd-vt__price .woocommerce-Price-amount,
.kd-vt__row-total .woocommerce-Price-amount,
.kd-vt__grand-total .woocommerce-Price-amount {
	display: inline;
	line-height: inherit;
	white-space: nowrap;
}

.kd-vt__price .woocommerce-price-suffix {
	color: var(--kd-vt-muted);
	display: block;
	font-size: 0.85em;
	line-height: 1.3;
}

.kd-vt__cell--sku {
	font-variant-numeric: tabular-nums;
}

/*
 * A single very long SKU or attribute value breaks rather than widening the
 * table. `break-word` rather than `anywhere` on purpose: `anywhere` also lets
 * the browser shrink the column below its longest word, which would let these
 * columns re-wrap - and change the row height - every time the total column
 * grows a few pixels.
 */
.kd-vt__cell--sku,
.kd-vt__cell--attributes,
.kd-vt__cell--description,
.kd-vt__cell--stock,
.kd-vt__cell[class*="cell--attribute-"] {
	overflow-wrap: break-word;
}

.kd-vt__empty,
.kd-vt__estimate-note {
	color: var(--kd-vt-muted);
}

.kd-vt__unavailable {
	color: var(--kd-vt-muted);
	font-size: 0.9em;
}

.kd-vt__stock--out {
	color: var(--kd-vt-error);
}

/* Quantity ---------------------------------------------------------------- */

.kd-vt__qty-wrap {
	display: inline-flex;
	flex-direction: column;
	gap: 0.2em;
}

.kd-vt__qty {
	max-width: var(--kd-vt-qty-width);
	text-align: center;
	width: var(--kd-vt-qty-width);
}

.kd-vt__qty-notice {
	color: var(--kd-vt-muted);
	font-size: 0.8em;
	line-height: 1.2;
}

/* Footer ------------------------------------------------------------------ */

.kd-vt__totals th {
	font-weight: 600;
	text-align: right;
}

.kd-vt__estimate-note {
	display: block;
	font-size: 0.8em;
	font-weight: 400;
}

.kd-vt__actions {
	display: flex;
	justify-content: flex-end;
	margin-top: var(--kd-vt-space);
}

.kd-vt__submit[disabled] {
	cursor: not-allowed;
	opacity: 0.55;
}

.kd-vt--busy {
	opacity: 0.7;
	pointer-events: none;
}

/* Pagination -------------------------------------------------------------- */

.kd-vt__pagination {
	display: flex;
	flex-wrap: wrap;
	gap: 0.25rem;
	margin-top: var(--kd-vt-space);
}

.kd-vt__page {
	background: none;
	border: 1px solid var(--kd-vt-border-strong);
	border-radius: var(--kd-vt-radius);
	cursor: pointer;
	font: inherit;
	line-height: 1;
	padding: 0.4em 0.7em;
}

.kd-vt__page--current {
	border-color: var(--kd-vt-accent);
	font-weight: 700;
}

.kd-vt__page[disabled] {
	cursor: default;
	opacity: 0.4;
}

/* Messages ---------------------------------------------------------------- */

.kd-vt__no-results {
	color: var(--kd-vt-muted);
	margin-top: var(--kd-vt-space);
}

.kd-vt__message-list {
	list-style: none;
	margin: var(--kd-vt-space) 0 0;
	padding: 0;
}

.kd-vt__message {
	border-left: 3px solid var(--kd-vt-border-strong);
	margin-bottom: var(--kd-vt-space-sm);
	padding: var(--kd-vt-space-sm) var(--kd-vt-space);
}

.kd-vt__message--success {
	border-left-color: var(--kd-vt-success);
}

.kd-vt__message--error {
	border-left-color: var(--kd-vt-error);
	color: var(--kd-vt-error);
}

/* Stacked cards ----------------------------------------------------------- */

@media screen and (max-width: 767px) {
	.kd-vt {
		/*
		 * Set here rather than on the image, so the row's left gutter and the
		 * thumbnail are worked out from the same number.
		 */
		--kd-vt-image-size: 64px;
	}

	/*
	 * A page builder can hand us a container wider than the phone screen: the
	 * XStore Elementor add-to-cart widget carries a fixed pixel width unless a
	 * mobile override is set for it. Fill whatever container we are given, but
	 * never grow past the screen, or the right-hand end of every card - the
	 * quantity box included - is silently clipped by an ancestor.
	 *
	 * The cap goes on the form rather than the table, so the button and the
	 * message list are constrained with it.
	 */
	.kd-vt .kd-vt__form {
		max-width: calc(100vw - var(--kd-vt-viewport-gutter, 30px));
	}

	.kd-vt__scroll {
		overflow-x: visible;
		width: 100%;
	}

	.kd-vt__table,
	.kd-vt__table tbody,
	.kd-vt__table tfoot,
	.kd-vt__table tr,
	.kd-vt__table td,
	.kd-vt__table th {
		display: block;
		width: auto;
	}

	.kd-vt__table thead {
		border: 0;
		clip: rect(0 0 0 0);
		height: 1px;
		overflow: hidden;
		position: absolute;
		width: 1px;
	}

	/*
	 * One card per variation, shaped like a line in a cart rather than one
	 * label/value row per column: the thumbnail sits in a fixed gutter on the
	 * left and the details stack beside it. Giving the row a left padding and
	 * taking the image out of the flow keeps the text aligned even when the
	 * card is taller than the thumbnail.
	 */
	.kd-vt__row {
		border: 1px solid var(--kd-vt-border-strong);
		border-radius: var(--kd-vt-radius);
		margin-bottom: var(--kd-vt-space-sm);
		min-height: calc(var(--kd-vt-image-size) + var(--kd-vt-space));
		padding: var(--kd-vt-space-sm) var(--kd-vt-space-sm) var(--kd-vt-space-sm)
			calc(var(--kd-vt-image-size) + var(--kd-vt-space-sm) * 3);
		position: relative;
	}

	.kd-vt .kd-vt__table td {
		border-bottom: 0;
		display: flex;
		gap: var(--kd-vt-space-sm);
		justify-content: space-between;
		padding: 0.1rem 0;
	}

	.kd-vt .kd-vt__table td::before {
		color: var(--kd-vt-muted);
		content: attr(data-label);
		flex: 0 0 auto;
		font-size: 0.85em;
	}

	/*
	 * The thumbnail sits in the gutter, not in a labelled row of its own. The
	 * selector has to out-specify the generic cell rules above, or the cell
	 * keeps its "Image" label and the width that comes with it.
	 */
	.kd-vt .kd-vt__table td.kd-vt__cell--image {
		display: block;
		left: var(--kd-vt-space-sm);
		min-width: 0;
		padding: 0;
		position: absolute;
		top: var(--kd-vt-space-sm);
		width: var(--kd-vt-image-size);
	}

	.kd-vt .kd-vt__table td.kd-vt__cell--image::before {
		content: none;
	}

	/* The variation reads as the card's heading. */
	.kd-vt .kd-vt__table td.kd-vt__cell--attributes,
	.kd-vt .kd-vt__table td[class*="cell--attribute-"] {
		font-weight: 600;
	}

	/* Room is not tight here, so the tax suffix can stay on the price line. */
	.kd-vt__price .woocommerce-price-suffix {
		display: inline;
	}

	.kd-vt__cell--quantity {
		align-items: center;
		padding-top: 0.25rem;
	}

	/* Estimated total and the button, side by side under the cards. */
	.kd-vt__table tfoot .kd-vt__totals {
		align-items: baseline;
		display: flex;
		flex-wrap: wrap;
		gap: var(--kd-vt-space-sm);
		justify-content: space-between;
		padding: var(--kd-vt-space-sm) 0;
	}

	.kd-vt__totals th {
		flex: 1 1 8rem;
		text-align: left;
	}

	.kd-vt__totals th::before {
		content: none;
	}

	.kd-vt__grand-total {
		flex: 0 0 auto;
		font-weight: 700;
	}

	/*
	 * Keep the button in the flow and full width. The extra specificity is
	 * deliberate: themes routinely style .button with a narrow, auto width.
	 */
	.kd-vt .kd-vt__actions {
		display: block;
		position: static;
	}

	.kd-vt .kd-vt__actions .kd-vt__submit {
		display: block;
		position: static;
		width: 100%;
	}
}

/* Matrix layout gets a sticky first column instead of stacking. */
.kd-vt--matrix .kd-vt__scroll {
	overflow-x: auto;
}

.kd-vt--matrix .kd-vt__table tbody th:first-child,
.kd-vt--matrix .kd-vt__table td:first-child {
	background: inherit;
	left: 0;
	position: sticky;
	z-index: 1;
}
