/*
 * Per-device columns and mobile order chosen in Kan Studio (block metadata,
 * see class-responsive-columns.php). The classes and custom properties are on
 * the block's own root element (and the editor wrapper, responsive.js).
 * Child combinators keep a setting from reaching nested blocks.
 * Breakpoints match device visibility: tablet 768–1023 px, mobile < 768 px.
 * !important: the user's explicit choice wins over each block's own
 * responsive fallback, whatever its selector weight.
 */
@media (768px <= width < 1024px) {

	.kan-tcols > .kan-cards-grid,
	.kan-tcols > .kan-pcards-grid,
	.kan-tcols > .kan-cat-list--grid,
	.kan-tcols > .wp-block-kanweb-pricing-table__grid,
	.wp-block-kanweb-gallery.kan-tcols.is-layout-grid > .wp-block-kanweb-gallery__grid,
	.kan-tcols.is-layout-grid:not(.wp-block-kanweb-gallery),
	.wp-block-columns.kan-tcols {
		grid-template-columns: repeat(var(--kan-tablet-cols, 2), minmax(0, 1fr)) !important;
	}

	/* Core Columns is a flex row; a grid gives equal columns and keeps its gap. */
	.wp-block-columns.kan-tcols {
		display: grid !important;
	}

	.wp-block-kanweb-gallery.kan-tcols.is-layout-masonry > .wp-block-kanweb-gallery__grid {
		column-count: var(--kan-tablet-cols, 2) !important;
	}

	.wp-block-kanweb-video-collection.kan-tcols {
		--kan-video-columns: var(--kan-tablet-cols, 2) !important;
	}

	.wp-block-gallery.has-nested-images.kan-tcols > figure.wp-block-image:not(#individual-image) {
		width: calc((100% - var(--wp--style--unstable-gallery-gap, 16px) * (var(--kan-tablet-cols, 2) - 1)) / var(--kan-tablet-cols, 2)) !important;
	}
}

@media (width < 768px) {

	.kan-mcols > .kan-cards-grid,
	.kan-mcols > .kan-pcards-grid,
	.kan-mcols > .kan-cat-list--grid,
	.kan-mcols > .wp-block-kanweb-pricing-table__grid,
	.wp-block-kanweb-gallery.kan-mcols.is-layout-grid > .wp-block-kanweb-gallery__grid,
	.kan-mcols.is-layout-grid:not(.wp-block-kanweb-gallery),
	.wp-block-columns.kan-mcols {
		grid-template-columns: repeat(var(--kan-mobile-cols, 1), minmax(0, 1fr)) !important;
	}

	.wp-block-columns.kan-mcols {
		display: grid !important;
	}

	.wp-block-kanweb-gallery.kan-mcols.is-layout-masonry > .wp-block-kanweb-gallery__grid {
		column-count: var(--kan-mobile-cols, 1) !important;
	}

	.wp-block-kanweb-video-collection.kan-mcols {
		--kan-video-columns: var(--kan-mobile-cols, 1) !important;
	}

	.wp-block-gallery.has-nested-images.kan-mcols > figure.wp-block-image:not(#individual-image) {
		width: calc((100% - var(--wp--style--unstable-gallery-gap, 16px) * (var(--kan-mobile-cols, 1) - 1)) / var(--kan-mobile-cols, 1)) !important;
	}

	/*
	 * Reverse order (kan-mreverse, only with at most one column on mobile).
	 * Stacked Columns: last column first. Weight beats the grid rule above
	 * when one mobile column was also chosen.
	 */
	body .wp-block-columns.kan-mreverse:not(.is-not-stacked-on-mobile) {
		display: flex !important;
		flex-direction: column-reverse !important;
		flex-wrap: nowrap !important;
	}

	body .wp-block-columns.kan-mreverse:not(.is-not-stacked-on-mobile) > .wp-block-column {
		flex-basis: auto !important;
		align-self: stretch !important;
	}

	body .wp-block-columns.kan-mreverse.is-not-stacked-on-mobile {
		flex-direction: row-reverse !important;
	}

	/* Row containers: reverse the row and, when items wrap, the lines too. */
	.kan-mreverse.is-layout-flex:not(.wp-block-columns, .is-vertical) {
		flex-direction: row-reverse !important;
		flex-wrap: wrap-reverse !important;
	}

	.kan-mreverse.is-layout-flex.is-vertical {
		flex-direction: column-reverse !important;
	}
}
