/**
 * T-Rent layout fix, 1.0.0.
 * Scoped to the public category covers and classic WooCommerce product lists.
 * The theme's colours, fonts, product links and rental prices are preserved.
 */

body.trent-layout-fix {
	--trent-category-gap: 20px;
	--trent-product-gap: 16px;
	--trent-product-padding: 12px;
}

/*
 * These are the homepage's category columns, identified by their category
 * links. Other covers, groups and columns are not affected. :has() is
 * supported by current Chrome, Edge, Firefox and Safari.
 */
body.home.trent-layout-fix .entry-content .wp-block-columns:has(> .wp-block-column > .wp-block-cover a[href*="/product-category/"]) {
	gap: var(--trent-category-gap) !important;
	align-items: stretch !important;
}

body.home.trent-layout-fix .entry-content .wp-block-column:has(> .wp-block-cover a[href*="/product-category/"]) {
	min-width: 0;
	margin: 0 !important;
	padding: 0 !important;
	align-self: stretch !important;
}

/* Remove the theme's extra bottom margin on only the aligncenter covers. */
body.home.trent-layout-fix .entry-content .wp-block-column > .wp-block-cover:has(a[href*="/product-category/"]) {
	box-sizing: border-box;
	width: 100%;
	max-width: 100%;
	height: 100%;
	margin: 0 !important;
}

/* A single grid gap replaces WooCommerce's floats and percentage margins. */
body.trent-layout-fix ul.products {
	--trent-product-columns: 3;
	display: grid !important;
	grid-template-columns: repeat(var(--trent-product-columns), minmax(0, 1fr));
	grid-auto-rows: 1fr;
	align-items: stretch;
	gap: var(--trent-product-gap) !important;
	padding: 0 !important;
}

body.trent-layout-fix ul.products.columns-1 { --trent-product-columns: 1; }
body.trent-layout-fix ul.products.columns-2 { --trent-product-columns: 2; }
body.trent-layout-fix ul.products.columns-3 { --trent-product-columns: 3; }
body.trent-layout-fix ul.products.columns-4 { --trent-product-columns: 4; }
body.trent-layout-fix ul.products.columns-5 { --trent-product-columns: 5; }
body.trent-layout-fix ul.products.columns-6 { --trent-product-columns: 6; }

/* WooCommerce clearfix pseudo-elements would otherwise occupy grid cells. */
body.trent-layout-fix ul.products::before,
body.trent-layout-fix ul.products::after {
	display: none !important;
	content: none !important;
}

body.trent-layout-fix ul.products > li.product {
	box-sizing: border-box;
	display: flex !important;
	flex-direction: column;
	float: none !important;
	clear: none !important;
	width: auto !important;
	min-width: 0;
	max-width: none !important;
	height: auto;
	margin: 0 !important;
	padding: var(--trent-product-padding) !important;
}

body.trent-layout-fix ul.products > li.product > a.woocommerce-loop-product__link,
body.trent-layout-fix ul.products > li.product.product-category > a {
	display: flex;
	flex: 1 1 auto;
	flex-direction: column;
	min-width: 0;
}

/* Identical image fields. Contain keeps the whole thumbnail visible. */
body.trent-layout-fix ul.products > li.product > a img {
	display: block;
	box-sizing: border-box;
	width: 100% !important;
	height: auto !important;
	max-width: 100%;
	max-height: 220px;
	aspect-ratio: 4 / 3;
	object-fit: contain;
	object-position: center;
	flex: 0 0 auto;
	margin: 0 0 10px !important;
}

/*
 * Reserve two lines even for short names. Longer names end with an ellipsis;
 * the complete name remains in the DOM and on the linked product page.
 */
body.trent-layout-fix ul.products > li.product .woocommerce-loop-product__title,
body.trent-layout-fix ul.products > li.product .woocommerce-loop-category__title {
	box-sizing: content-box;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	line-clamp: 2;
	overflow: hidden;
	flex: 0 0 auto;
	min-height: 2.5em;
	max-height: 2.5em;
	margin: 0 0 8px !important;
	padding: 0 !important;
	font-size: 20px !important;
	line-height: 1.25 !important;
	white-space: normal;
	overflow-wrap: anywhere;
}

/*
 * Never clip a price, rental period or tax suffix. An unusually long price
 * increases all rows equally rather than making just one card taller.
 */
body.trent-layout-fix ul.products > li.product .price {
	display: block;
	min-height: 3.9em;
	margin: 0 !important;
	font-size: 16px !important;
	line-height: 1.3 !important;
	white-space: normal;
	overflow: visible;
	overflow-wrap: anywhere;
}

body.trent-layout-fix ul.products > li.product .price .woocommerce-Price-amount {
	display: inline-block;
	max-width: 100%;
}

body.trent-layout-fix ul.products > li.product .price .woocommerce-price-suffix {
	display: block;
	margin-top: 2px;
	font-size: 0.85em;
	line-height: 1.3;
}

/* Keep the existing button colours while aligning every button at the foot. */
body.trent-layout-fix ul.products > li.product > .button {
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: center;
	align-self: stretch;
	min-height: 44px;
	margin: 10px 0 0 !important;
	padding: 10px 12px !important;
	font-size: 15px !important;
	line-height: 1.2 !important;
	white-space: normal;
	text-align: center;
}

/* Restore a visible keyboard focus ring removed by the site's button CSS. */
body.trent-layout-fix ul.products > li.product > a:focus-visible {
	outline: 2px solid currentColor !important;
	outline-offset: 3px;
}

body.trent-layout-fix ul.products > li.product > .button:focus-visible {
	outline-color: #222 !important;
}

@media (max-width: 768px) {
	body.trent-layout-fix {
		--trent-product-gap: 14px;
		--trent-product-padding: 10px;
	}

	body.trent-layout-fix ul.products[class*="columns-"] {
		--trent-product-columns: 2;
	}

	body.trent-layout-fix ul.products.columns-1 {
		--trent-product-columns: 1;
	}

	body.trent-layout-fix ul.products > li.product > a img {
		max-height: 160px;
		margin-bottom: 8px !important;
	}

	body.trent-layout-fix ul.products > li.product .woocommerce-loop-product__title,
	body.trent-layout-fix ul.products > li.product .woocommerce-loop-category__title {
		font-size: 18px !important;
		margin-bottom: 6px !important;
	}

	body.trent-layout-fix ul.products > li.product .price {
		font-size: 15px !important;
	}
}

/* Keep very narrow screens and high zoom usable. */
@media (max-width: 319px) {
	body.trent-layout-fix ul.products[class*="columns-"] {
		--trent-product-columns: 1;
	}
}
