/* =========================================================================
 * Journal page — page-specific styles (page ID 507, /journal-2027/)
 *
 * The journal is a full-bleed Real3D Flipbook. Two problems are fixed here:
 *
 *  1. The theme printed the page title ("ژورنال مارک گالری") above the book.
 *     A magazine cover already states its own name, so the duplicate is
 *     removed — visually only. It stays in the DOM as an accessible heading
 *     (clipped, not display:none), so the page keeps exactly one <h1> for
 *     screen readers and for SEO instead of losing its heading entirely.
 *
 *  2. The book rendered inside `.content.narrow` (max-width 900px) with 40px
 *     of vertical padding, which left it roughly 375x530px in the middle of
 *     the screen. Real3D sizes a page to the *shorter* of the box it is given,
 *     so a small box means a small book no matter what the plugin settings say.
 *     The page is therefore widened to the full window and the viewer is given
 *     the rest of the viewport height instead of a fixed 900/1.3 box.
 *
 * The viewer writes `height` to the element as an inline style on load and on
 * every resize (js/flipbook.js → resizeContainer). `!important` here is what
 * lets the CSS value win over that inline style; the plugin then reads the
 * real box back from getBoundingClientRect() and fits the pages to it, so no
 * plugin file has to be patched and a plugin update cannot undo this.
 *
 * Loaded on this one page only, so nothing here can affect any other page.
 * Every selector is scoped to body.mg-journal-page.
 * ========================================================================= */

/* -------------------------------------------------------------------------
 * Canvas — full bleed.
 *
 * Zero padding as well as zero max-width: the dark viewer panel then runs
 * edge to edge like a printed catalogue spread, and every pixel the theme
 * would have spent on margins goes to the book.
 * ---------------------------------------------------------------------- */
body.mg-journal-page .content {
	padding-top: 0;
	padding-bottom: 0;
}

body.mg-journal-page .content.narrow {
	max-width: none;
}

body.mg-journal-page .wrap.content {
	padding-left: 0;
	padding-right: 0;
}

/* The duplicate title: visible heading removed, document outline kept. */
body.mg-journal-page .page-title {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0 0 0 0);
	clip-path: inset(50%);
	white-space: nowrap;
	border: 0;
}

/* The article wrapper must not add its own spacing either. */
body.mg-journal-page .entry,
body.mg-journal-page article {
	margin: 0;
}

/* -------------------------------------------------------------------------
 * The viewer box — desktop and tablet only.
 *
 * SELECTOR: the plugin does not keep the class it renders. embed.js rewrites it
 * to `real3dflipbook-<uniqueId>`, so a rule on `.real3dflipbook` matches nothing
 * and silently does nothing (it did nothing until this was measured in the
 * browser). The attribute-prefix selector below is what actually reaches it,
 * and `.entry >` keeps it on this page's shortcode only.
 *
 * SIZING. The pages are portrait (1000x1414), so on a landscape screen the
 * spread is limited by whichever side runs out first. Left to itself the plugin
 * picks `min(width / 1.414, window.innerHeight)` — and that window height still
 * includes the notice bar and the sticky header, so the book overflowed the
 * first screen and left wide dark bands on both sides.
 *
 * The box is therefore sized from the width instead: the formula below is the
 * height a spread needs to span the whole window, i.e.
 *
 *     spread width = 1.414 × page height,  page height = box − 47px
 *     ⇒ box = 100vw / 1.414 + 47px
 *
 * 1.414 is the page aspect (1414/1000) and 47px is the viewer's own vertical
 * padding (measured: box 835px → page 788px, and 685px → 641px, so the padding
 * is constant and not proportional).
 *
 * A portrait spread cannot fill both dimensions at once on a landscape screen.
 * Filling the width makes the book taller than the window, so the page is
 * scrolled a little — which is the trade the shop owner chose ("a bit bigger,
 * taller is fine"). The 1.35 cap stops an ultrawide window (21:9 and up) from
 * asking for a book far taller than any screen; there the spread stays at
 * roughly 80% of the width instead.
 *
 * PHONES: below 768px the plugin switches to single-page mode and sizes the box
 * from the page ratio, which is exactly right for a phone and already fills the
 * width once the gutter is gone. So this block is scoped to min-width rather
 * than neutralised inside a max-width query — the box holds only absolutely
 * positioned children, and forcing `height: auto` back on it would collapse it
 * to zero and take the whole viewer with it.
 * ---------------------------------------------------------------------- */
/* -------------------------------------------------------------------------
 * The area around the viewer is the same dark as the panel.
 *
 * The panel is narrower than the window, so the strips beside it would
 * otherwise be the theme's white — which made the book look like it was
 * floating on a blank page.
 *
 * SCOPED TO <main>, NOT <body>. The footer is a sibling of <main> and paints
 * no background of its own, so colouring the body turned the whole footer grey
 * too — the whole lower half of the page. <main id="main" class="wrap content">
 * ends where the journal ends, so the dark stops at the footer's top edge.
 *
 * Desktop only: on a phone the panel is full width, so there are no strips to
 * fill and the page stays light.
 * ---------------------------------------------------------------------- */
@media (min-width: 768px) {
	body.mg-journal-page main.content,
	body.mg-journal-page main.content.narrow,
	body.mg-journal-page main.content .entry,
	body.mg-journal-page main.content article {
		background-color: #515558;
	}
}

@media (min-width: 768px) {
	body.mg-journal-page .entry > [class^="real3dflipbook"] {
		/* Panel: 1010px wide, centred, one screen tall.
		 *
		 * MEASURED RELATION: the viewer fits the spread to the panel WIDTH —
		 * page height = panelWidth / 1.414 — and the panel height only decides how
		 * much room is left over. A 1600px panel therefore asks for 1131px-tall
		 * pages inside an 835px panel, which is why the book overflowed above and
		 * below into the header.
		 *
		 * 1010px gives 714px-tall pages: a ~60px dark edge above and below inside
		 * the 835px panel, well clear of the 47px toolbar. The page behind is the
		 * same dark, so the strips either side of the panel are invisible and the
		 * whole surround reads as one colour.
		 *
		 * To make the book bigger or smaller, change this one number: pages come
		 * out at panelWidth / 1.414, and it must stay under 1131 for the frame to
		 * exist at all. */
		/* Panel width and height are one decision: the pages follow the panel
		 * WIDTH, not its height (measured — pageHeight = panelWidth / 1.414), so
		 * height alone does nothing.
		 *
		 *   1010 wide → 714 pages → + 47 toolbar = 761 tall
		 *   60px top margin = the dark edge above the book
		 *
		 * Total 821px under the header, which fits a 950px window. A shorter
		 * laptop window (768px tall) gets its own block below rather than
		 * overflowing. Plain numbers on purpose: a var()/min() chain here once
		 * made the whole viewer collapse to nothing. */
		width: 1010px !important;
		height: 761px !important;
		margin-top: 60px;
		margin-left: auto;
		margin-right: auto;
		background-color: #515558;
	}

	/* Shorter screens: 772 wide → 546 pages → 593 panel, so the book still ends
	 * inside the window with the same 60px edge above it. */
	@media (max-height: 899px) {
		body.mg-journal-page .entry > [class^="real3dflipbook"] {
			width: 772px !important;
			height: 593px !important;
		}
	}

	/* ------------------------------------------------------------------
	 * The pages are left to the viewer's own fit — no layer override.
	 *
	 * A previous attempt pushed `top`/`bottom` on `.flipbook-bookLayer` to
	 * control the page size. It does not behave linearly: `adjustZoomLimits()`
	 * computes `zoomMin = target / fitH` and then clamps the result to the range
	 * 0.95–1, so shrinking the layer silently stops shrinking the book and then
	 * jumps, and the book ends up half under the toolbar. Measured: insets of
	 * 44px gave 470px pages, 144px gave 795px, 108px overflowed the panel.
	 *
	 * With the layer untouched the viewer sizes the pages to
	 * `min(visibleHeight, visibleWidth / 1.414)` and the panel height is the only
	 * lever — predictable, and the frame comes out even on its own.
	 * ------------------------------------------------------------------ */

	/* The viewer's bottom menu paints an opaque light bar over the panel colour,
	 * which cut a white strip out of the dark surround. Transparent lets the
	 * panel show through. Attribute-contains is used because the plugin builds
	 * these class names by string concatenation; a missing one matches nothing. */
	body.mg-journal-page .entry > [class^="real3dflipbook"] [class*="menu-bottom"],
	body.mg-journal-page .entry > [class^="real3dflipbook"] [class*="menuBottom"] {
		background-color: transparent !important;
	}
}

/* Older browsers without dvh (Safari < 15.4) fall back to the safe value.
 * The panel is sized in plain pixels above, so nothing here depends on dvh. */

/* -------------------------------------------------------------------------
 * PHONE STAGE
 *
 * The viewer sized its own box to "page + toolbar" — 552px of an 844px phone —
 * so the footer started immediately under the book and the journal read as a
 * small widget on the page rather than a viewer. A portrait page cannot fill
 * both axes at once (it fills the width, and now it does so completely: 390 of
 * 390, measured), so the honest fix is to let the stage take the rest of the
 * screen. journal-nav.js sets that height because it has to be measured; this
 * block handles the two consequences.
 * ---------------------------------------------------------------------- */
@media (max-width: 767px) {
	/* If the script has not run yet (or is blocked) the strip under the book is
	 * the page's own colour instead of the theme's white. */
	body.mg-journal-page main.content {
		background-color: #515558;
	}

	/* The floating support button landed exactly on the viewer's last toolbar
	 * button once that toolbar moved to the bottom of the screen (measured: both
	 * in the bottom-right corner). Lifting it clear of the bar keeps both
	 * usable — it now floats over the page's margin instead of over a control. */
	body.mg-journal-page .mgt-support {
		bottom: calc(70px + env(safe-area-inset-bottom, 0px));
	}
}

/* -------------------------------------------------------------------------
 * Why the quality improved with this size (measured, not guessed):
 *
 * The page images are 1000x1414. The viewer draws into a canvas that follows
 * devicePixelRatio (verified: 1600x1296 CSS becomes 3200x2591 at DPR 2), so a
 * page needs  pageWidth × dpr  source pixels to stay sharp.
 *
 *   panel 1600  →  page ≈ 565px CSS  →  1130 device px at DPR 2  (upscaled)
 *   panel 1010  →  page ≈ 357px CSS  →   714 device px at DPR 2  (fine)
 *
 * The previous, larger book was asking for more pixels than the files contain,
 * which is exactly the softness that was reported. At 1010 the pages sit inside
 * their own resolution up to DPR 2.8, so no hi-DPI special case is needed.
 *
 * If the book is ever made larger again, the honest fix is to re-export the
 * eight page images at 2x (2000x2828) — the originals are not on disk, so that
 * needs the source material.
 * ---------------------------------------------------------------------- */

/* -------------------------------------------------------------------------
 * Summary of the geometry, so the next change is one number:
 *
 *   --mg-journal-panel = 1010px
 *     → page height = 1010 / 1.414 = 714px
 *     → panel height = 714 + 47px toolbar = 761px
 *     → margin-top 60px gives the dark edge above the book
 *
 * The viewer pins the book to the top of its own layer rather than centring it
 * (measured: a 5px band inside an 835px panel), so the space above the book is
 * produced by the panel's top margin, which is reliable. The page background is
 * painted the same #515558, so the strips beside the panel are invisible and
 * the surround reads as one continuous dark frame.
 *
 * Things that look right but are not (all tried on the live page):
 *  - A fixed width/height/centring on `.flipbook-bookLayer` collapses the book
 *    to a thumbnail: the viewer subtracts its own flip headroom from that box.
 *  - Pushing `top`/`bottom` on that layer does not scale linearly either —
 *    `adjustZoomLimits()` clamps its zoom factor to 0.95–1, so the book stops
 *    shrinking and then jumps, ending half under the toolbar.
 *  - Sizing the panel from the window height does nothing: the pages follow the
 *    panel WIDTH (`pageHeight = panelWidth / 1.414`). Only width and the panel's
 *    own margins control the result.
 *
 * Scoped to `body.mg-journal-page` and min-width 768px: no other page is
 * affected and phones keep their own single-page layout.
 * ---------------------------------------------------------------------- */
