/**
 * woocommerce/cart block styles.
 *
 * Enqueued as-is for the cart block from functions.php; there is no build step
 * for this file, so edit it directly.
 *
 * WooCommerce ships its own stylesheet for this block
 * (assets/client/blocks/cart.css) and load order between two block stylesheets
 * is not guaranteed, so overrides below use the 4-class specificity root:
 *
 *   .wp-block-woocommerce-cart .wc-block-cart .wc-block-components-main table.wc-block-cart-items
 *
 * Sections:
 *   1. Cart items (main column)
 *   2. Components (Quantity selector, Price, Totals)
 *   3. Order summary (sidebar)
 *   4. Responsive (container queries & media queries)
 */

/* -------------------------------------------------------------------------
 * 1. Cart items (main column)
 * ----------------------------------------------------------------------- */

.wp-block-woocommerce-cart .wc-block-cart .wc-block-components-main {
	container-type: inline-size;
}

table.wc-block-cart-items,
.editor-styles-wrapper table.wc-block-cart-items {
	.wc-block-cart-items__row {
		.wc-block-cart-item__prices,
		.wc-block-cart-item__total {
			font-size: 16px;
			line-height: 28px;
			color: var(--wp--preset--color--primary-2);
		}

		.wc-block-components-product-name {
			font-size: 16px;
			color: var(--wp--preset--color--contrast);
			line-height: 28px;
			font-weight: 400;
		}

		.wc-block-components-product-metadata__description {
			display: none;
		}
	}
}

.wc-block-cart__main .wc-block-cart-items,
.wc-block-cart__main .wc-block-cart-items td {
	border-color: #ddd;
}

.wc-block-cart__main .wc-block-cart-items tr:first-child td {
	border-top: 0;
}

/* Hide the skeleton description placeholder so height doesn't shift on hydration */
.wc-block-cart-item__wrap > .wc-block-components-skeleton__element + .wc-block-components-skeleton__element {
	display: none;
}

.wc-block-cart-item__remove-link {
	text-decoration: none !important;

	&:focus-visible {
		outline: 2px solid var(--wp--preset--color--primary-2);
		outline-offset: 2px;
	}

	&::after {
		content: '\f5de';
		font-family: "bootstrap-icons";
		font-size: 15px;
		color: var(--wp--preset--color--contrast);
	}

	svg {
		display: none;
	}
}

.wc-block-cart-item__total-price-and-sale-badge-wrapper {
	position: relative;

	.wc-block-components-product-badge {
		float: right;
	}
}

/* -------------------------------------------------------------------------
 * 2. Components
 * ----------------------------------------------------------------------- */

/* Quantity Selector component */
.wc-block-components-quantity-selector {
	display: flex;
	align-items: center;
	border: 1px solid #ddd;
	border-radius: 0;
	background-color: var(--wp--preset--color--white);
	height: 40px;
	box-sizing: border-box;

	input.wc-block-components-quantity-selector__input {
		min-width: 36px;
		width: 40px;
		padding: 0;
		border: 0;
		border-radius: 0;
		font-size: 14px;
		font-weight: 400;
		text-align: center;
		height: 100%;
		line-height: 38px;
		margin: 0;
		box-shadow: none;
		outline: none;

		&:focus-visible {
			outline: 2px solid var(--wp--preset--color--primary-2);
			outline-offset: -1px;
			z-index: 1;
		}
	}

	.wc-block-components-quantity-selector__button {
		flex-shrink: 0;
		width: 36px;
		height: 100%;
		min-width: 0;
		padding: 0;
		border: 0;
		border-radius: 0;
		background: transparent;
		color: var(--wp--preset--color--contrast);
		font-size: 14px;
		display: flex;
		align-items: center;
		justify-content: center;
		cursor: pointer;
		opacity: 1;
		outline: none;
		box-shadow: none;
		transition: background-color 0.2s ease, color 0.2s ease;

		&:hover {
			background-color: var(--wp--preset--color--light-1);
		}

		&:focus-visible {
			outline: 2px solid var(--wp--preset--color--primary-2);
			outline-offset: -2px;
			z-index: 1;
		}

		&:disabled {
			opacity: 0.4;
			cursor: not-allowed;
		}
	}
}

/* Product Price component */
.wc-block-components-product-price {
	gap: 7px;

	.wc-block-components-product-price__regular {
		order: 2;
	}

	.wc-block-components-product-price__value.is-discounted {
		margin-left: 0;
	}
}

/* Sale price badge */
.wc-block-components-product-badge {
	border-radius: 4px;
	font-size: 12px;
}

/* Totals component */
.wc-block-components-totals-wrapper {
	border-color: #ddd;
}

.wc-block-components-totals-discount__coupon-list {
	margin: 14px 0 0;
	display: flex;
	gap: 5px;

	.wc-block-components-chip {
		margin: 0;
	}
}

.wc-block-components-totals-footer-item .wc-block-components-totals-item__label,
.wc-block-components-totals-footer-item .wc-block-components-totals-item__value {
	font-size: 17px;
	font-weight: 700;
	line-height: 28px;
}

.wc-block-components-totals-coupon {
	padding: 0;
	font-size: 15px;

	.wc-block-components-text-input {
		input[type=text] {
			padding-left: 16px;
		}

		label {
			left: 16px;
			font-size: 15px;
			font-weight: 400 !important;
			color: #757575;
		}

		&.is-active {
			input:focus {
				padding-left: 16px;
				border-color: #ddd;
			}
		}
	}

	.wc-block-components-button {
		background: var(--wp--preset--color--primary-2);
		border-radius: 0;

		&:focus,
		&:active,
		&:hover {
			background: var(--wp--preset--color--primary-2-hover);
		}
	}
}

/* -------------------------------------------------------------------------
 * 3. Order summary (sidebar)
 * ----------------------------------------------------------------------- */

.wp-block-woocommerce-cart .wc-block-cart .wc-block-components-sidebar,
.wc-block-components-sidebar-layout .wc-block-components-sidebar,
.wc-block-components-sidebar {
	width: 100%;
	padding: 24px 20px;
	background-color: var(--wp--preset--color--light-2);
	align-self: flex-start;
	box-sizing: border-box;

	@media (max-width: 782px) {
		padding-bottom: 84px;
	}

	@media (min-width: 783px) {
		padding: 30px;
	}
}

.wc-block-cart .wc-block-cart__totals-title {
	text-transform: none;
	font-size: 30px;
	font-weight: 700;
	line-height: 38.4px;
	color: var(--wp--preset--color--contrast);
}

.wp-block-woocommerce-cart-order-summary-totals-block {
	border-bottom: 1px solid #ddd;
	padding: 0;
	border: none;

	.wc-block-components-totals-wrapper {
		padding: 20px 0 15px 0;
		border-top: 1px solid #ddd;
	}
}

.wc-block-components-totals-wrapper {
	padding: 20px 0 15px 0;

	&.wp-block-woocommerce-cart-order-summary-coupon-form-block {
		border: none;
	}
}

.wc-block-cart .wp-block-woocommerce-cart-order-summary-block {
	font-size: 15px;
	line-height: 1.6;
	border-bottom: none;
	margin-bottom: 0;
}

.wc-block-cart__submit {
	margin: 0;
}

/* -------------------------------------------------------------------------
 * 4. Responsive
 * ----------------------------------------------------------------------- */

/*
 * Below 700px -- Mobile cart items layout.
 * Fixes narrow screen layout and prevents horizontal overflow.
 */
@container (max-width: 699px) {

	.wp-block-woocommerce-cart .wc-block-cart .wc-block-components-main table.wc-block-cart-items,
	.wc-block-cart .wc-block-components-main table.wc-block-cart-items,
	table.wc-block-cart-items {
		width: 100%;
		border-collapse: collapse;

		.wc-block-cart-items__header {
			display: none;
		}

		.wc-block-cart-items__row {
			display: grid;
			grid-template-columns: 72px minmax(0, 1fr) auto;
			column-gap: 16px;
			row-gap: 6px;
			align-items: start;
			padding: 20px 0;
			border-bottom: 1px solid #ddd;
			box-sizing: border-box;

			&:last-child {
				border-bottom: 0;
			}
		}

		tbody tr td {
			padding: 0;
			border: 0;
		}

		.wc-block-cart-item__product,
		.wc-block-cart-item__wrap {
			display: contents;
		}

		.wc-block-cart-item__image {
			grid-column: 1;
			grid-row: 1 / span 3;
			padding: 0;
			width: 72px;

			a {
				display: flex;

				img {
					width: 72px;
					height: auto;
					max-width: 100%;
					object-fit: cover;
					border-radius: 4px;
				}
			}
		}

		.wc-block-components-product-name {
			grid-column: 2;
			grid-row: 1;
			display: block;
			font-size: 15px;
			font-weight: 500;
			line-height: 1.4;
			color: var(--wp--preset--color--contrast);
			word-break: break-word;
			margin: 0;
			padding: 0;
		}

		.wc-block-cart-item__total {
			grid-column: 3;
			grid-row: 1;
			padding: 0;
			display: flex;
			flex-direction: column;
			align-items: flex-end;
			text-align: end;
			justify-content: flex-start;

			.price.wc-block-components-product-price {
				font-size: 16px;
				font-weight: 700;
				color: var(--wp--preset--color--primary-2);
				margin-bottom: 0;
				line-height: 1.4;
				white-space: nowrap;
			}

			.wc-block-components-product-badge {
				margin-top: 2px;
				float: none;
				white-space: nowrap;
			}
		}

		.wc-block-cart-item__prices {
			grid-column: 2;
			grid-row: 2;
			margin: 0;

			.price.wc-block-components-product-price {
				font-size: 14px;
				line-height: 1.4;
				gap: 6px;
			}
		}

		.wc-block-components-product-metadata {
			grid-column: 2 / -1;
			grid-row: auto;
			margin: 2px 0;
		}

		.wc-block-cart-item__quantity {
			grid-column: 2 / -1;
			grid-row: 3;
			padding: 0;
			display: flex;
			align-items: center;
			gap: 12px;
			margin-top: 6px;

			.wc-block-components-quantity-selector {
				height: 38px;
			}

			.wc-block-cart-item__remove-link {
				position: static;
				display: inline-flex;
				align-items: center;
				justify-content: center;
				padding: 6px;
				color: var(--wp--preset--color--contrast);
			}
		}
	}
}

/*
 * The wide cart -- six-column grid table.
 * Hangs off @container (min-width: 700px) on .wc-block-components-main so it
 * turns on when the table itself has at least 700px width.
 */
@container (min-width: 700px) {

	.wp-block-woocommerce-cart .wc-block-cart .wc-block-components-main table.wc-block-cart-items {
		--kereta-cart-col-remove: 20px;
		--kereta-cart-col-image: 80px;
		--kereta-cart-col-price: 110px;
		--kereta-cart-col-quantity: 120px;
		--kereta-cart-col-subtotal: 100px;
		--kereta-cart-columns:
			var(--kereta-cart-col-remove) var(--kereta-cart-col-image) minmax(0, 1fr)
			var(--kereta-cart-col-price) var(--kereta-cart-col-quantity)
			var(--kereta-cart-col-subtotal);
		--kereta-cart-gap: 20px;
		--kereta-cart-padding-inline: 35px;

		border-collapse: separate;
		border-spacing: 0;
		border-bottom: 1px solid #ddd;

		.wc-block-cart-items__header,
		.wc-block-cart-items__row {
			display: grid;
			grid-template-columns: var(--kereta-cart-columns);
			align-items: center;
			column-gap: var(--kereta-cart-gap);
			padding-inline: var(--kereta-cart-padding-inline);
		}

		.wc-block-cart-items__header {
			min-height: 52px;
			background-color: var(--wp--preset--color--light-1);
			color: var(--wp--preset--color--contrast);

			th {
				width: auto;
				padding: 12px 0;
				border: 0;
				font-size: 14px;
				font-weight: 500;
				line-height: 28px;
				color: var(--wp--preset--color--contrast);
				text-align: start;
				text-transform: uppercase;
				white-space: nowrap;
			}

			th.wc-block-cart-items__header-image {
				grid-column: 1 / 4;
				padding-inline-start: calc(var(--kereta-cart-col-remove) + var(--kereta-cart-gap));
			}

			th.wc-block-cart-items__header-product {
				display: grid;
				grid-column: 4 / 6;
				grid-template-columns: var(--kereta-cart-col-price) var(--kereta-cart-col-quantity);
				column-gap: var(--kereta-cart-gap);

				&::before {
					content: var(--kereta-cart-label-price, "Price");
				}

				&::after {
					content: var(--kereta-cart-label-quantity, "Quantity");
				}
			}

			th.wc-block-cart-items__header-total {
				grid-column: 6;
				text-align: end;
			}
		}

		tbody tr td {
			padding: 0;
			border: 0;
		}

		.wc-block-cart-items__row {
			padding-block: 25px;
			border-bottom: 1px solid #ddd;

			&:last-child {
				border-bottom: 0;
			}
		}

		.wc-block-cart-item__product,
		.wc-block-cart-item__wrap,
		.wc-block-cart-item__quantity {
			display: contents;
		}

		.wc-block-cart-item__wrap > * {
			grid-column: 3;
		}

		.wc-block-cart-item__remove-link,
		.wc-block-cart-item__image,
		.wc-block-cart-item__prices,
		.wc-block-components-quantity-selector,
		.wc-block-cart-item__total {
			grid-row: 1 / -1;
		}

		.wc-block-cart-item__quantity .wc-block-cart-item__remove-link {
			grid-column: 1;
		}

		.wc-block-cart-item__image {
			grid-column: 2;

			a {
				display: flex;

				img {
					width: var(--kereta-cart-col-image);
					height: auto;
					max-width: 100%;
					object-fit: cover;
				}
			}
		}

		.wc-block-components-product-name {
			grid-column: 3;
			grid-row: 1;
			max-width: 290px;
		}

		.wc-block-cart-item__prices {
			grid-column: 4;

			.price.wc-block-components-product-price {
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				gap: 4px;
				margin-bottom: 0;
				font-size: 16px;
				font-weight: 600;
				line-height: 28px;
				color: var(--wp--preset--color--primary-2);
			}
		}

		.wc-block-components-quantity-selector {
			grid-column: 5;
		}

		.wc-block-cart-item__total {
			grid-column: 6;
			display: flex;
			justify-content: flex-end;
			font-size: 16px;
			line-height: 28px;

			.price.wc-block-components-product-price {
				margin-bottom: 0;
				font-size: 16px;
				font-weight: 600;
				line-height: 28px;
				color: var(--wp--preset--color--primary-2);
			}
		}

		.wc-block-cart-items__row:has(.wc-block-components-product-metadata > *:not(.wc-block-components-product-metadata__description)) {
			grid-template-rows: 1fr 1fr;

			.wc-block-components-product-name {
				align-self: end;
			}

			.wc-block-components-product-metadata {
				align-self: start;
			}
		}
	}
}

/*
 * Below 82rem (1312px) -- table and order summary stack to preserve full
 * width for the 6-column items table down to tablet widths.
 */
@media not all and (min-width: 82rem) {

	.wp-block-woocommerce-cart .wc-block-cart {
		row-gap: 40px;

		.wc-block-components-main,
		.wc-block-cart__sidebar {
			width: 100%;
		}

		.wc-block-components-main {
			padding-right: 0;
		}
	}
}

/*
 * Min width 82rem (1312px) -- table and order summary sit side by side.
 */
@media (width >= 82rem) {

	.wc-block-components-sidebar-layout,
	.wp-block-woocommerce-cart .wc-block-components-sidebar-layout > .block-editor-inner-blocks > .block-editor-block-list__layout {
		display: grid;
		grid-template-columns: calc(65% - (48 / 1060 * 100%)) calc(35% - (24 / 1060 * 100%));
		column-gap: calc(72 / 1060 * 100%);

		.wc-block-components-main {
			width: 100%;
			padding: 0;
		}
	}
}

@media (max-width: 320px) {
	table.wc-block-cart-items .wc-block-cart-items__row .wc-block-cart-item__wrap {
		max-width: 120px;
	}
}
