/**
 * Typography Stylist Block - Frontend Styles
 */

.wp-block-typost {
	position: relative;
}

/* Screen reader only classes - ensure they exist and prevent layout impact */
.wp-block-typost .visually-hidden,
.wp-block-typost .sr-only,
.wp-block-typost .screen-reader-text,
/* The "Custom" option once saved the literal class "custom" on the screen
   reader copy; changing save() would invalidate those blocks. */
.wp-block-typost > .custom {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	padding: 0;
	margin: 0;
	overflow: hidden;
	clip: rect(0, 0, 0, 0); /* Legacy support for older browsers */
	clip-path: inset(50%);  /* Modern approach - makes content invisible while maintaining full size */
	white-space: nowrap;
	border-width: 0;
	color: transparent; /* Make text invisible */
	background: transparent; /* Ensure no background shows */
	pointer-events: none; /* Don't block clicks on the styled text below */
	z-index: 1; /* Ensure it's above styled text for focus */
}

/* Windows Narrator Focus Enhancement:
 * When screen reader heading receives focus, show outline around the styled text area.
 * The element is already full-size (100% width/height) to match the styled headline.
 * IMPORTANT: The text content remains invisible (via clip-path/color above).
 * Only the OUTLINE becomes visible to highlight the styled text area.
 * Do NOT remove clip-path, overflow:hidden, or color:transparent on focus -
 * this would cause duplicate visible text and break the dual-content pattern.
 */
.wp-block-typost .visually-hidden:focus,
.wp-block-typost .sr-only:focus,
.wp-block-typost .screen-reader-text:focus {
	pointer-events: auto; /* Re-enable pointer events when focused */
	outline: 3px solid #007acc; /* WordPress standard focus color */
	outline-offset: 4px; /* Space between text and outline */
	border-radius: 2px; /* Subtle rounding for modern look */
}

/* High contrast mode support for Windows users */
@media (prefers-contrast: high) {
	.wp-block-typost .visually-hidden:focus,
	.wp-block-typost .sr-only:focus,
	.wp-block-typost .screen-reader-text:focus {
		outline-width: 4px;
		outline-color: Highlight; /* System color for high contrast */
	}
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
	.wp-block-typost .visually-hidden:focus,
	.wp-block-typost .sr-only:focus,
	.wp-block-typost .screen-reader-text:focus {
		outline-color: #4cc2ff; /* Lighter blue for dark backgrounds */
	}
}

/* Styled content */
.wp-block-typost .typost-styled {
	/* Inherit all spacing from parent */
	margin: 0;
	padding: 0;
}

/* Fit-to-width sizing (fontSize: "fit")
 * The styled heading becomes an inline-size container so the per-line
 * font-size: calc(R * 100cqi) declarations resolve against its width.
 * Scoped to .typost-fit only — inline-size containment changes layout
 * semantics and must never apply to existing blocks. */
.wp-block-typost .typost-styled.typost-fit {
	container-type: inline-size;
}

/* Keep a fit-to-width block at full width when its parent sizes to its
 * content (#243). Inline-size containment makes the container's natural
 * width 0, so a parent that shrinks to fit (a column flex item with auto
 * margins, which is how a sticky-footer theme centers the editor's root
 * container, or a float) gives the block 0px and every cqi line size
 * becomes 0. This empty line adds width only to the block's max-content
 * size: one space, widened to 100vw by word-spacing, between two zero
 * width spaces. A parent that shrinks to fit then grows to the available
 * width, up to its own max-width. Its min-content size stays 0 (the line
 * can break at the space, and a space at a line break takes no width), so
 * a flex item, grid track or table cell can still shrink it and nothing
 * overflows. contain-intrinsic-inline-size cannot do this: it sets the
 * min-content size too, which made Row items and Media & Text overflow.
 * The line is a zero-height float because a float is out of flow: an
 * in-flow line (or an empty grid) between the wrapper and the styled
 * heading stopped their top margins from collapsing and made the block
 * taller wherever a theme gives the heading a margin. A float still adds
 * to the parent's intrinsic width.
 * The float still takes part in float placement, so the negative end
 * margin is needed. A percentage margin counts as 0 when the parent
 * computes its intrinsic width, so the max-content width stays 100vw. In
 * layout, the float's width is capped by the wrapper's width W minus the
 * margin, so with -1000% its outer width is min(11W, 100vw) - 10W, which is
 * 0 or less when W is at least 10vw: the float fits beside any earlier
 * float. (-100% was not enough: in a content column narrower than half the
 * viewport the outer width came back to W.) A length margin such as -100vw
 * cannot do this, because it counts in the intrinsic width and cancels the
 * 100vw. Without the margin, the float was as wide as the wrapper: after an
 * earlier float (an aligned image) it dropped below that float, every later
 * float had to sit below it, and in Chromium the styled heading moved below
 * the image too.
 * The two spaces after the first escape are needed: CSS takes the first
 * one as the end of the escape. "/ ''" keeps the text out of the
 * accessibility tree. Not ::after, which the editor uses for the block
 * outline.
 * A Stack (vertical flex) sizes its items' width the same way, so it gets
 * the line too. In a Row, only an item with no width setting gets it (see
 * the flex rule below): its flex-basis is 0, so the line only makes the Row
 * itself as wide as the space it has. A Grow item has flex-basis auto, so
 * the line would become its basis and push its neighbors aside. A grid sizes
 * its tracks itself. */
:not(.is-layout-flex, .is-layout-grid) > .wp-block-typost:has(> .typost-fit)::before,
.is-layout-flex.is-vertical > .wp-block-typost:has(> .typost-fit)::before,
.is-layout-flex:not(.is-vertical) > .wp-block-typost:has(> .typost-fit):not([class*="wp-container-content-"])::before {
	content: "\200B  \200B";
	content: "\200B  \200B" / "";
	float: left;
	height: 0;
	font-size: 0;
	line-height: 0;
	letter-spacing: 0;
	word-spacing: 100vw;
	margin-inline-end: -1000%;
	text-indent: 0;
	white-space: normal;
	pointer-events: none;
}

/* In a Row, a fit-to-width block with no width setting fills the space the
 * other items leave, as if its width were set to Grow: at its content size
 * it would be 0px wide. A Grow or Fixed width gets a wp-container-content-*
 * class from core (frontend and editor add it only when it carries CSS, so
 * an explicit "Fit" has none), and core's rules then apply unchanged. */
.is-layout-flex:not(.is-vertical) > .wp-block-typost:has(> .typost-fit):not([class*="wp-container-content-"]) {
	flex: 1 1 0;
}

/* Each visual line is its own block-level, non-wrapping row; the <br>
 * separators are dropped from the fit markup.
 * font-optical-sizing is disabled because fit sizing stores one
 * measured width ratio per line: automatic optical sizing changes glyph
 * widths with the rendered font size (Fraunces' opsz made lines run
 * ~17% over), so widths must scale linearly for the stored ratio to
 * hold at every container width. A manually chosen opsz axis value
 * (variable-font sliders) is size-independent and still works. */
.wp-block-typost .typost-fit .typost-line {
	display: block;
	white-space: nowrap;
	font-optical-sizing: none;
}

/* Fit-to-width lines grow with browser zoom (#235).
 * A line is calc(R * 100cqi), and zoom makes the container narrower in CSS
 * px, so on screen the line kept its size at every zoom level (WCAG 1.4.4).
 * CSS cannot tell zoom from a small window, but a zoomed desktop window
 * keeps its landscape shape while it gets narrow, and a phone held upright
 * does not. So in a landscape viewport narrower than 782px (core's columns
 * breakpoint: a zoomed window stacks its columns there), a line whose
 * container is narrower than 32rem is sized as if the container were
 * 32rem, and wraps at its spaces. Its font-size also has a word cap,
 * min(…, calc(W * 100cqi)), where W sizes the line's widest word to the
 * container width, so the growth stops before a word is wider than the
 * container: no word is broken and nothing scrolls sideways (1.4.10
 * Reflow). overflow-wrap is reset to normal because many themes set
 * break-word on headings, which would split a word a fraction of a pixel
 * too wide. 32rem is 0.4 × 1280px, so with a 16px root a line whose
 * widest word allows it doubles by 500% zoom. A one-word line cannot wrap,
 * so its cap equals its line size and it keeps filling its container.
 * Wider containers fill exactly as before, so 100% zoom is unchanged
 * everywhere except in a landscape viewport under 782px with a container
 * under 32rem.
 * Only lines saved since #233 read the variable (buildFitFontSize() in
 * utils.js writes min(calc(R * var(--typost-fit-width, 100cqi)),
 * calc(C * 100cqi))); older px lines keep calc(R * 100cqi) and never wrap,
 * hence the attribute test. Frontend only: in the editor the canvas iframe
 * is its own viewport, often landscape and under 782px because of the
 * sidebars while the window is wide, so the editor would wrap lines the
 * frontend shows on one line. The editor's block wrapper always carries
 * block-editor-block-list__block, and the saved markup never does. */
@media (orientation: landscape) and (max-width: 781.98px) {
	@container (width < 32rem) {
		.wp-block-typost:not(.block-editor-block-list__block) .typost-fit .typost-line[style*="--typost-fit-width"] {
			--typost-fit-width: 32rem;
			white-space: normal;
			overflow-wrap: normal;
		}
	}
}

/* Inline font sizes are ignored in fit mode — each line renders at one
 * uniform size (the block controls sizing). Inline styles can only be
 * beaten by !important. */
.wp-block-typost .typost-fit .typost-line [data-fontsize] {
	font-size: inherit !important;
}

/* Hanging initial (#242)
 * Moves the first letter past the start edge by --typost-hang em, so a
 * swash hangs into the margin while the letter's main shape aligns with the
 * text edge. The value comes from the block's inline style or from a
 * paragraph style's class; an inline style cannot reach ::first-letter,
 * which is why both write a custom property. ::first-letter adds no
 * element, so the word is never split.
 *
 * Frontend only: the visible copy is the direct .typost-styled child. The
 * editor's RichText (.typost-block-content, which also carries
 * .typost-styled under a paragraph style) is excluded because Chromium
 * mishandles the caret around ::first-letter in an editable element: Home
 * stops after the first letter and Shift+Home leaves it unselected (core's
 * drop cap turns its ::first-letter off on :focus for the same reason).
 * editor.css renders the same hang there with text-indent instead.
 * Inline .typost-styled spans never get a ::first-letter.
 *
 * Fit-to-width blocks use no ::first-letter at all. Every line hangs
 * through a start margin on its own typost-line wrapper: line 1 from the
 * block value (--typost-hang, inherited, so a paragraph style drives it),
 * lines 2+ from --typost-line-hang on the wrapper (the builders never write
 * a line-1 value). Firefox does not carry a block's ::first-letter into a
 * block-level child, and frontend lines are display: block, so line 1 got no
 * hang there while fit measurement still sized it as hung and the line ran
 * past the right edge (PR review, Firefox 146). A start margin moves the
 * whole line in every browser, and the same rules serve the editor, where
 * the wrappers are inline runs. The margin's em is the line's own size, which
 * is what measureFitLines() subtracts.
 *
 * --typost-hang-on turns both off for centered text and for text aligned to
 * the end side, where moving the first letter would only shift the line.
 * The block writes its alignment as an inline text-align (with a space in
 * the editor's React style, without one in saved markup). Mirrored in JS by
 * initialHangApplies() in utils.js, which fit measurement uses.
 *
 * --typost-plain (#315) is the documented plain typography switch: a theme
 * sets it to 1 on any ancestor (frontend.css sets it for .typost-plain), and
 * every hang becomes 0. --typost-hang-on stays internal: the fit-size rules
 * below still need it to know which lines were measured as hung. */
.wp-block-typost > .typost-styled:not(.typost-block-content, .typost-fit)::first-letter {
	margin-inline-start: calc(var(--typost-hang, 0) * var(--typost-hang-on, 1) * (1 - var(--typost-plain, 0)) * -1em);
}

.wp-block-typost .typost-fit .typost-line {
	margin-inline-start: calc(var(--typost-line-hang, 0) * var(--typost-hang-on, 1) * (1 - var(--typost-plain, 0)) * -1em);
}

/* Line 1 is always the first child: save wraps every line, and the editing
 * value leaves an empty line 1 unwrapped (its <br> is then the first child,
 * so no other line takes line 1's value). */
.wp-block-typost .typost-fit > .typost-line:first-child {
	margin-inline-start: calc(var(--typost-hang, 0) * var(--typost-hang-on, 1) * (1 - var(--typost-plain, 0)) * -1em);
}

/* Plain typography keeps hung fit lines inside the block (#315).
 * measureFitLines() sized a hung line so its ink minus the hang fills the
 * container: R = ref / (measured - hang * ref). With the hang removed the
 * line is hang × font-size too wide; at font-size / (1 + R × hang) it fills
 * the container exactly. A line held below R × 100cqi by its cap or word
 * cap shrinks by the same factor, so it also stays inside.
 *
 * R and the line's size are only literals in its inline font-size, which
 * CSS cannot read, so Typost::add_fit_line_plain_properties() (a
 * render_block filter) copies them into --typost-fit-ratio and
 * --typost-fit-size at render time. Saved markup is unchanged, content saved
 * before #315 is covered, and the editor (no render_block) never matches.
 * !important beats the inline font-size; with --typost-plain at 0 (or a
 * line measured unhung, --typost-hang-on 0) the divisor is 1 and the size is
 * the inline value. */
.wp-block-typost .typost-fit .typost-line[style*="--typost-fit-ratio"] {
	font-size: calc(var(--typost-fit-size) / (1 + var(--typost-fit-ratio) * var(--typost-line-hang, 0) * var(--typost-hang-on, 1) * var(--typost-plain, 0))) !important;
}

.wp-block-typost .typost-fit > .typost-line[style*="--typost-fit-ratio"]:first-child {
	font-size: calc(var(--typost-fit-size) / (1 + var(--typost-fit-ratio) * var(--typost-hang, 0) * var(--typost-hang-on, 1) * var(--typost-plain, 0))) !important;
}

/* The switch is frontend only: authors see the hang they set. The editor
 * wrapper always carries block-editor-block-list__block, saved markup never
 * does. */
.wp-block-typost.block-editor-block-list__block {
	--typost-plain: 0;
}

.wp-block-typost > :is(.typost-styled, .typost-block-content):is([style*="text-align:center"], [style*="text-align: center"]) {
	--typost-hang-on: 0;
}

/* Separate rules: a browser without :dir() drops only these two. */
.wp-block-typost > :is(.typost-styled, .typost-block-content):dir(ltr):is([style*="text-align:right"], [style*="text-align: right"]) {
	--typost-hang-on: 0;
}

.wp-block-typost > :is(.typost-styled, .typost-block-content):dir(rtl):is([style*="text-align:left"], [style*="text-align: left"]) {
	--typost-hang-on: 0;
}
