/* PieceCraft — responsive layer. Loaded last so it can correct earlier rules on phones, tablets and large screens.
   Test with a REAL mobile viewport (touch + mobile emulation), not just a narrow desktop window: python tools/mobile_check.py */

/* ================================================================== 1. No sideways scrolling, ever */
html { overflow-x: hidden; max-width: 100%; }
body { overflow-x: clip; max-width: 100%; }
img, video, iframe, table { max-width: 100%; }
main, section { max-width: 100%; }
/* .container keeps its own cap (base.css). It used to be listed above, which overrode that cap and let every
   section stretch to the full screen width (2560px+ on large monitors). */
.container { max-width: min(100%, calc(var(--container) + var(--gutter) * 2)); }
.container.container-narrow, .container-narrow { max-width: min(100%, calc(820px + var(--gutter) * 2)); }
/* wide content scrolls inside its own box instead of stretching the page */
.table-wrap, .prose table { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.prose { overflow-wrap: anywhere; }
pre, code { white-space: pre-wrap; overflow-wrap: anywhere; }

/* Off-screen drawers: they slide in with a clip instead of a transform, so a closed drawer can never widen the page on a phone */
.mobile-nav, .cart-drawer {
	transform: none; left: auto; max-width: 100%;
	clip-path: inset(0 0 0 100%); visibility: hidden;
	transition: clip-path .45s var(--ease), visibility 0s .45s;
}
.mobile-nav.is-open, .cart-drawer.is-open { clip-path: inset(0 0 0 0); visibility: visible; transition-delay: 0s; }
@media (max-width: 980px) {
	.filters { transform: none; right: auto; clip-path: inset(0 100% 0 0); transition: clip-path .45s var(--ease), visibility 0s .45s; }
	.filters.is-open { clip-path: inset(0 0 0 0); }
}
html.pc-fs-lock, html.pc-fs-lock body { overflow: hidden; }

/* ================================================================== 2. Reveal animations: gentler on small screens */
@media (max-width: 760px) {
	html.rv-ready [data-reveal="left"], html.rv-ready [data-reveal="right"] { --rv-x: 0px; --rv-y: 28px; }
	html.rv-ready [data-reveal="rise-lg"] { --rv-y: 40px; }
}

/* ================================================================== 3. Mobile menu (full-height panel, scrolls, respects notches) */
.mobile-nav {
	width: min(440px, 100%); height: 100vh; height: 100dvh; max-height: 100dvh;
	padding: max(var(--s-4), env(safe-area-inset-top)) max(var(--s-5), env(safe-area-inset-right)) max(var(--s-5), env(safe-area-inset-bottom)) var(--s-5);
	gap: var(--s-4); overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
}
.mn-head { flex-shrink: 0; height: 52px; }
.mn-list { flex: 0 0 auto; min-width: 0; }
.mn-group > summary, .mn-link { min-height: 56px; font-size: 1.25rem; }
.mn-group a.sub { padding: 12px var(--s-3); min-height: 44px; display: flex; align-items: center; overflow-wrap: anywhere; }
.mn-foot { position: sticky; bottom: 0; margin-top: auto; padding-top: var(--s-3); background: linear-gradient(to bottom, transparent, var(--bg) 22%); }
.mn-foot .btn { width: 100%; white-space: normal; text-align: center; line-height: 1.25; min-height: 48px; height: auto; padding-block: 10px; }
.cart-drawer { width: min(440px, 100%); height: 100vh; height: 100dvh; }
@media (max-height: 640px) {
	.mn-group > summary, .mn-link { min-height: 48px; font-size: 1.125rem; }
	.mn-foot { position: static; background: none; }
}
@media (max-width: 380px) {
	.mn-group > summary, .mn-link { font-size: 1.125rem; }
}

/* ================================================================== 4. Header on small phones */
@media (max-width: 480px) {
	.hdr { column-gap: var(--s-2); }
	.hdr-actions { gap: var(--s-2); }
	.brand { min-width: 0; }
	.brand > span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
	.announce { font-size: .75rem; }
	.announce .container { padding-block: 7px; }
}
@media (max-width: 360px) {
	.hdr-actions .icon-btn { width: 40px; height: 40px; }
	.user-chip { display: none; }
	.nav-toggle { width: 44px; height: 44px; }
}

/* ================================================================== 5. Play board */
.stage, .stage canvas { touch-action: auto; }
.stage:fullscreen, .stage:-webkit-full-screen { width: 100vw !important; height: 100vh !important; aspect-ratio: auto !important; border-radius: 0; border: 0; touch-action: none; }
.stage:fullscreen canvas, .stage:-webkit-full-screen canvas, .stage.is-pseudo-fs canvas { touch-action: none; }
.stage.is-pseudo-fs { position: fixed; inset: 0; z-index: 600; width: 100vw !important; height: 100vh !important; height: 100dvh !important; aspect-ratio: auto !important; border-radius: 0; border: 0; }
.stage-fsbar { position: absolute; top: max(12px, env(safe-area-inset-top)); right: 12px; z-index: 5; display: flex; gap: var(--s-2); }
.stage-fsbar button { height: 40px; padding: 0 var(--s-4); border-radius: var(--r-pill); background: var(--ink); color: #fff; font-weight: 700; font-size: .8125rem; box-shadow: var(--sh-md); }
.stage-fsbar button:first-child { background: #fff; color: var(--ink); }
@media (max-width: 420px) { .stage-fsbar { left: 12px; justify-content: space-between; } .stage-fsbar button { flex: 1; } }
.stage-hint { white-space: normal; text-align: center; height: auto; min-height: 34px; padding-block: 6px; line-height: 1.25; max-width: calc(100% - 24px); }
@media (max-width: 700px) {
	.stage { aspect-ratio: auto; min-height: 440px; border-radius: var(--r-md); }
	.play-card { padding: var(--s-4); border-radius: var(--r-lg); }
	.play-top { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "title timer" "sizes sizes"; align-items: center; gap: var(--s-3); margin-bottom: var(--s-3); }
	.play-top .play-title { grid-area: title; gap: var(--s-3); }
	.play-top .play-title .ico-tile { width: 40px; height: 40px; border-radius: 12px; }
	.play-top .play-title h3 { font-size: 1.0625rem; gap: var(--s-2); }
	.play-top .segmented { grid-area: sizes; justify-self: stretch; display: flex; flex-wrap: nowrap; }
	.play-top .segmented button { flex: 1 1 0; min-width: 0; padding-inline: 4px; }
	.play-top .play-right { grid-area: timer; justify-content: flex-end; }
	.play-top .play-right .pill { height: 34px; padding-inline: var(--s-3); }
	.hud { margin-top: var(--s-4); }
	.hud-tray { display: grid; grid-template-columns: 1fr 1fr; width: 100%; border-radius: var(--r-lg); gap: var(--s-1); }
	.hud-btn { justify-content: center; white-space: normal; text-align: center; padding: 0 var(--s-3); min-height: 44px; height: auto; line-height: 1.2; }
	.hud-btn.is-primary { grid-column: 1 / -1; }
	.play-meta { flex-direction: column; align-items: stretch; }
	.play-meta .who { min-width: 0 !important; }
	.play-meta .row { justify-content: stretch; }
	.play-meta .row .btn { flex: 1 1 0; }
}

/* ================================================================== 6. Puzzle cards */
.pz-foot { gap: var(--s-3); align-items: center; }
/* the clock icon had a width but no height, so the SVG defaulted to 150px tall and stretched the whole footer */
.pz-foot svg { width: 14px; height: 14px; flex-shrink: 0; }
.pz-foot > span:last-child > span { display: inline-flex; width: 14px; height: 14px; }
.pz-foot > span:first-child { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pz-foot > span:last-child { flex: 0 0 auto; white-space: nowrap; }
.pz-body { gap: var(--s-2); }
.pz-row h3 { overflow-wrap: anywhere; }
@media (max-width: 640px) {
	/* two rows on phones: the description on top, the time under it */
	.pz-foot { flex-direction: column; align-items: flex-start; gap: var(--s-1); }
	.pz-foot > span:first-child { white-space: normal; overflow: visible; text-overflow: clip; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
	.pz-body { padding: var(--s-4); }
}
/* Printable cards use the foot for two download buttons */
.pz-foot .btn { flex: 1 1 0; }
@media (max-width: 640px) { .pz-foot:has(.btn) { flex-direction: row; } }

/* ================================================================== 7. Forms */
.scm-form .btn { margin-top: 0; }
.scm-form__grid + .scm-form__consent { margin-top: var(--s-4); }
.site-footer .news-form, .news-form { max-width: 100%; }
@media (max-width: 480px) {
	.news-form { flex-direction: column; border-radius: var(--r-lg); padding: var(--s-2); gap: var(--s-2); }
	.news-form input { min-height: 48px; padding: 0 var(--s-4); }
	.news-form .btn { width: 100%; }
}
input, select, textarea { max-width: 100%; font-size: 16px; } /* 16px stops iOS zooming into fields */

/* ================================================================== 8. Buttons that sit together share the row equally on phones */
@media (max-width: 560px) {
	.btn { white-space: normal; text-align: center; line-height: 1.25; }
	.actions, .share-row, .row.btn-group, .cta-band .row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-3); width: 100%; }
	.actions .btn, .share-row .btn, .cta-band .row .btn { width: 100%; justify-content: center; }
	.actions .btn:only-child, .cta-band .row .btn:only-child { grid-column: 1 / -1; }
	.hero-cta { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-3); }
	.hero-cta .btn { width: 100%; justify-content: center; }
}
@media (max-width: 380px) {
	.actions, .share-row, .cta-band .row, .hero-cta { grid-template-columns: 1fr; }
}

/* ================================================================== 9. General rhythm: tighter vertical spacing and gutters on small screens */
@media (max-width: 760px) {
	.card-pad { padding: var(--s-5); }
	h1 { overflow-wrap: anywhere; }
	.section-head.split { flex-direction: column; align-items: flex-start; gap: var(--s-4); }
	.split, .contact, .cart-layout, .daily-layout, .shop-layout { gap: var(--s-6); }
}
@media (max-width: 480px) {
	.grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
	.band, .cta-band { padding: var(--s-6) var(--s-5); border-radius: var(--r-lg); }
}
/* Large screens: content (and the puzzle board) grows to 1440px, then stops — wider monitors just get more margin */
@media (min-width: 1560px) {
	:root { --container: 1440px; }
}
/* Keep the board a comfortable height on wide-but-short screens.
   v1.1.1: width:100% is required. With width:auto + aspect-ratio, the browser transfers max-height through the
   ratio into a max-width, so the board shrank horizontally inside .play-card. With a definite width the height is
   simply min(width × 8.2/16, 78vh, 760px) and the board always spans the card. The canvas sizes itself from
   getBoundingClientRect and fits the picture inside, so a non-16:8.2 stage lays out correctly. */
@media (min-width: 1100px) {
	.play-card .stage:not(:fullscreen):not(.is-pseudo-fs) { width: 100%; max-height: min(78vh, 760px); }
}

/* ================================================================== 10. Toast: one tidy line (wraps only on very narrow screens), above the home-bar */
.toast { width: max-content; max-width: min(92vw, 440px); text-align: center; bottom: max(var(--s-5), env(safe-area-inset-bottom)); display: flex; align-items: center; gap: var(--s-2); justify-content: center; border-radius: var(--r-lg); line-height: 1.35; z-index: 700; }

/* ================================================================== 11. Whole card is the link (puzzle and product cards) */
.pz-card h3 a::after, .pr-card .pr-body h3 a::after { content: ""; position: absolute; inset: 0; z-index: 2; }
.pz-card, .pr-card { cursor: pointer; }
.pz-card .fav, .pr-card .fav, .pr-card .pr-actions .btn, .pr-card .pr-actions a { z-index: 4; }
.pr-card .pr-actions .btn { position: relative; }
.pz-card:focus-within, .pr-card:focus-within { outline: 2px solid var(--accent-2); outline-offset: 2px; }
.pz-card h3 a:focus-visible, .pr-card h3 a:focus-visible { outline: 0; }

/* ================================================================== 12. Home hero on phones: headline, then the board, then the intro text and buttons */
@media (max-width: 760px) {
	.hero > .container { display: flex; flex-direction: column; }
	.hero-copy { display: contents; }
	.hero-copy > div:first-child:not(.hero-cta) { order: 1; }
	.hero-copy > h1 { order: 2; margin-bottom: var(--s-5); text-align: center; }
	.hero > .container > .play-card { order: 3; margin-bottom: var(--s-6); }
	.hero-copy > .lede { order: 4; text-align: center; margin: 0 auto var(--s-5); }
	.hero-copy > .hero-cta { order: 5; margin-bottom: 0; }
	.hero-copy .trust-pill { margin-bottom: var(--s-4); }
}
@media (max-width: 560px) {
	/* the three hero buttons stack, one per row */
	.hero-cta { grid-template-columns: 1fr !important; }
}

/* ================================================================== 13. One vertical rhythm on phones (sections were 64px + 64px apart, with extra gaps from headings and inline margins) */
.cta-after { margin-top: clamp(64px, 8vw, 112px); }
@media (max-width: 760px) {
	.section { padding-block: 44px; }
	.section-sm { padding-block: 28px; }
	.page-hero { padding-block: 24px 28px; }
	.hero { padding-top: 20px; padding-bottom: 32px; }
	.section-head { margin-bottom: var(--s-6); }
	.section-head h2 + p, .section-head h2 + .lede { margin-top: var(--s-3); }
	.pagination { margin-top: var(--s-6); }
	.site-footer { margin-top: 36px; padding-top: 44px; }
	.cta-after { margin-top: var(--s-7); }
	.filter-bar { margin-bottom: var(--s-4); }
	.grid, .values, .steps, .journal, .shop-grid { row-gap: var(--s-5); }
}

/* The first heading inside a text block must not add its own top margin on top of the section padding */
.prose > :first-child, .acc-a p:first-child { margin-top: 0; }
@media (max-width: 760px) { .cart-empty { padding-block: var(--s-7); } .cart-empty svg { width: 56px; height: 56px; margin-bottom: var(--s-4); } }

/* Split headers become a column on phones: an inline "flex: 1 1 560px" would then mean "560px tall", so reset it */
@media (max-width: 760px) {
	.section-head.split > *, .section-head.split > [style*="flex"] { flex: 0 0 auto !important; width: 100%; max-width: 100%; min-width: 0; }
	.section-head.split > .row, .section-head.split > .search-field { width: auto; }
	.section-head.split > .search-field, .lib-hero-search { min-width: 0 !important; width: 100%; }
}
