/**
 * OmniCalc shared responsive structural shell.
 *
 * Current architecture:
 * - >=1280: accepted wide desktop geometry;
 * - 1024–1279: compact-wide / rail-capable geometry converging to desktop;
 * - <1024: theme-owned narrow/flow presentation (preserved).
 */

/* Compact-wide variables. The shell reaches the accepted 1200px desktop size at 1232. */
@media (min-width: 1024px) and (max-width: 1279px) {
	body.omni-layout-shell-page {
		--omni-layout-shell-max: min(1200px, calc(100vw - 32px));
		--omni-layout-reading-max: clamp(728px, calc(100vw - 296px), 760px);
		--omni-layout-standard-main-width: var(--omni-layout-reading-max);
		--omni-layout-standard-rail-gap: clamp(
			24px,
			calc(9.090909vw - 72px),
			40px
		);
		--omni-layout-standard-rail-region: clamp(
			240px,
			calc(90.909091vw - 720px),
			400px
		);
		--omni-layout-standard-rail-ad-max: min(
			336px,
			var(--omni-layout-standard-rail-region)
		);
	}
}

/* Frozen accepted wide-desktop target. */
@media (min-width: 1280px) {
	body.omni-layout-shell-page {
		--omni-layout-shell-max: 1200px;
		--omni-layout-reading-max: 760px;
		--omni-layout-standard-main-width: 760px;
		--omni-layout-standard-rail-gap: 40px;
		--omni-layout-standard-rail-region: 400px;
		--omni-layout-standard-rail-ad-max: 336px;
	}
}

/* Shared rail-capable shell structure. */
@media (min-width: 1024px) {
	/* The actual page composition owner: H1 region + post-content subtree. */
	body.omni-layout-shell-page .omni-layout-page-shell {
		width: 100% !important;
		max-width: var(--omni-layout-shell-max) !important;
		box-sizing: border-box;
		margin-left: auto !important;
		margin-right: auto !important;
		padding-left: 0 !important;
		padding-right: 0 !important;
	}

	/* Neutralize a local constrained wrapper; the marked parent owns the shell. */
	body.omni-layout-shell-page
		.omni-layout-page-shell
		.wp-block-group:has(> .wp-block-post-title),
	body.omni-layout-shell-page
		.omni-layout-page-shell
		.wp-block-group:has(> .wp-block-post-content) {
		width: 100% !important;
		max-width: none !important;
		box-sizing: border-box;
		margin-left: 0 !important;
		margin-right: 0 !important;
		padding-left: 0 !important;
		padding-right: 0 !important;
	}

	/* H1 and post-content consume the shell structurally, not by coordinates. */
	body.omni-layout-shell-page .omni-layout-page-shell .wp-block-post-title {
		width: 100%;
		max-width: none !important;
		box-sizing: border-box;
		margin-top: 0 !important;
		margin-right: 0 !important;
		margin-left: 0 !important;
	}

	body.omni-layout-shell-page .omni-layout-page-shell .wp-block-post-content {
		display: flow-root;
		width: 100% !important;
		max-width: none !important;
		box-sizing: border-box;
		margin-right: 0 !important;
		margin-bottom: 0 !important;
		margin-left: 0 !important;
		padding-left: 0 !important;
		padding-right: 0 !important;
	}

	/* Tool families retain their plugin-owned internal maxima inside the main owner. */
	body.omni-layout-tool-page
		.omni-layout-page-shell
		.wp-block-post-content
		> .omnicalc {
		margin-left: 0 !important;
		margin-right: auto !important;
	}

	/* Generic top-level reading-track ownership without flattening internal blocks. */
	body.omni-layout-shell-page
		.omni-layout-page-shell
		.wp-block-post-content
		> :where(
			:not(
				.omnicalc,
				.omni-monetization-slot,
				.omni-layout-content-grid,
				.omni-layout-content-rail,
				.alignwide,
				.alignfull,
				.oc-binary-stats,
				.oc-biznes-marzha__external-result
			)
		) {
		width: 100%;
		max-width: var(--omni-layout-reading-max);
		box-sizing: border-box;
		margin-left: 0 !important;
		margin-right: auto !important;
	}

	/* Header and footer share the exact page-shell axis. */
	body.omni-layout-shell-page .omni-layout-header-shell,
	body.omni-layout-shell-page .omni-layout-footer-shell {
		width: 100% !important;
		max-width: var(--omni-layout-shell-max) !important;
		box-sizing: border-box;
		margin-left: auto !important;
		margin-right: auto !important;
		padding-left: 0 !important;
		padding-right: 0 !important;
	}
}

/**
 * Standard-content rail owner. It is absent below 1024 and becomes the finite
 * right-side owner for the primary rail when rail-capable.
 */
body.omni-layout-content-page .omni-layout-content-rail--standard {
	display: none;
}

@media (min-width: 1024px) {
	body.omni-layout-content-page .omni-layout-page-shell .wp-block-post-content {
		position: relative;
		overflow: visible;
	}

	body.omni-layout-content-page .omni-layout-content-rail--standard {
		--omni-layout-rail-region: var(--omni-layout-standard-rail-region);
		--omni-layout-rail-ad-max: var(--omni-layout-standard-rail-ad-max);
		display: block;
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		width: var(--omni-layout-rail-region);
		max-width: var(--omni-layout-rail-region);
		min-height: 0;
		box-sizing: border-box;
		margin: 0;
		padding: 0;
		pointer-events: none;
	}
}

/**
 * Tool-page structural content owner.
 *
 * The post remains one uninterrupted left flow. Rail-capable widths place the
 * rail as a sibling in column 2. Standard tools share Standard geometry; Wheel
 * is only a physical 240px-rail exception.
 */
body.omni-layout-tool-page
	.omni-layout-page-shell
	.wp-block-post-content
	> .omni-layout-content-grid {
	width: 100%;
	max-width: 760px;
	box-sizing: border-box;
	margin-top: 0 !important;
	margin-right: auto !important;
	margin-bottom: 0 !important;
	margin-left: auto !important;
}

body.omni-layout-tool-page .omni-layout-content-main {
	width: 100%;
	min-width: 0;
	box-sizing: border-box;
}

/* Preserve constrained-layout vertical rhythm inside the single left flow. */
body.omni-layout-tool-page .omni-layout-content-main > * {
	margin-block-start: 1.2rem;
	margin-block-end: 0;
}

body.omni-layout-tool-page .omni-layout-content-main > :first-child {
	margin-block-start: 0;
}

body.omni-layout-tool-page .omni-layout-content-main > :last-child {
	margin-block-end: 0;
}

body.omni-layout-tool-page .omni-layout-content-rail {
	display: none;
}

/* Preserved narrow/flow ownership. */
@media (max-width: 1023px) {
	body.omni-layout-tool-page
		.omni-layout-page-shell
		.wp-block-post-content
		> .omni-layout-content-grid[data-omni-layout="wheel-rail"] {
		max-width: 880px;
	}

	body.omni-layout-tool-page
		.omni-layout-content-main
		> :where(
			:not(
				.omnicalc,
				.omni-monetization-slot,
				.alignwide,
				.alignfull,
				.oc-binary-stats,
				.oc-biznes-marzha__external-result
			)
		) {
		width: 100%;
		max-width: var(--wp--style--global--content-size, 740px);
		box-sizing: border-box;
		margin-left: auto !important;
		margin-right: auto !important;
	}
}

/* Compact-wide grid variables. */
@media (min-width: 1024px) and (max-width: 1279px) {
	body.omni-layout-tool-page
		.omni-layout-content-grid[data-omni-layout="standard-rail"] {
		--omni-layout-main-width: var(--omni-layout-standard-main-width);
		--omni-layout-rail-gap: var(--omni-layout-standard-rail-gap);
		--omni-layout-rail-region: var(--omni-layout-standard-rail-region);
		--omni-layout-rail-ad-max: var(--omni-layout-standard-rail-ad-max);
	}

	body.omni-layout-tool-page
		.omni-layout-content-grid[data-omni-layout="wheel-rail"] {
		--omni-layout-main-width: clamp(728px, calc(100vw - 296px), 760px);
		--omni-layout-rail-gap: 24px;
		--omni-layout-rail-region: 240px;
		--omni-layout-rail-ad-max: 240px;
	}
}

/* Wheel grows smoothly after the shared 1056 reference point. */
@media (min-width: 1056px) and (max-width: 1279px) {
	body.omni-layout-tool-page
		.omni-layout-content-grid[data-omni-layout="wheel-rail"] {
		--omni-layout-main-width: clamp(
			760px,
			calc(68.181818vw + 40px),
			880px
		);
		--omni-layout-rail-gap: clamp(
			24px,
			calc(31.818182vw - 312px),
			80px
		);
	}
}

/* Frozen accepted desktop grid variables. */
@media (min-width: 1280px) {
	body.omni-layout-tool-page
		.omni-layout-content-grid[data-omni-layout="standard-rail"] {
		--omni-layout-main-width: 760px;
		--omni-layout-rail-gap: 40px;
		--omni-layout-rail-region: 400px;
		--omni-layout-rail-ad-max: 336px;
	}

	body.omni-layout-tool-page
		.omni-layout-content-grid[data-omni-layout="wheel-rail"] {
		--omni-layout-main-width: 880px;
		--omni-layout-rail-gap: 80px;
		--omni-layout-rail-region: 240px;
		--omni-layout-rail-ad-max: 240px;
	}
}

/* By 1232 the Wheel composition has converged to the accepted desktop shift. */
@media (min-width: 1232px) {
	body.omni-layout-tool-page
		.omni-layout-content-grid[data-omni-layout="wheel-rail"]
		.omni-layout-content-main
		> .omnicalc--wheel {
		--oc-wheel-stage-shift-x: 34px;
	}
}

/* Shared rail-capable two-column tool structure. */
@media (min-width: 1024px) {
	body.omni-layout-tool-page
		.omni-layout-page-shell
		.wp-block-post-content
		> .omni-layout-content-grid {
		display: grid;
		grid-template-columns:
			minmax(0, var(--omni-layout-main-width))
			minmax(0, var(--omni-layout-rail-region));
		column-gap: var(--omni-layout-rail-gap);
		align-items: start;
		width: 100%;
		max-width: none;
		margin-left: 0 !important;
		margin-right: 0 !important;
	}

	body.omni-layout-tool-page .omni-layout-content-main {
		grid-column: 1;
		grid-row: 1;
		min-width: 0;
	}

	body.omni-layout-tool-page .omni-layout-content-rail {
		display: block;
		grid-column: 2;
		grid-row: 1;
		align-self: stretch;
		min-width: 0;
		min-height: 0;
		width: 100%;
		max-width: var(--omni-layout-rail-region);
		box-sizing: border-box;
	}

	body.omni-layout-tool-page
		.omni-layout-page-shell
		.wp-block-post-content
		.omni-layout-content-main
		> .omnicalc {
		margin-left: 0 !important;
		margin-right: auto !important;
	}

	body.omni-layout-tool-page
		.omni-layout-page-shell
		.wp-block-post-content
		.omni-layout-content-main
		> :where(
			:not(
				.omnicalc,
				.omni-monetization-slot,
				.alignwide,
				.alignfull,
				.oc-binary-stats,
				.oc-biznes-marzha__external-result
			)
		) {
		width: 100%;
		max-width: var(--omni-layout-reading-max);
		box-sizing: border-box;
		margin-left: 0 !important;
		margin-right: auto !important;
	}
}
