.jigsaw {
	position: relative; max-width: 1030px; margin: auto; isolation: isolate;
	border: 2px solid var(--wood-dark); border-radius: var(--radius); background: var(--paper);
	box-shadow: var(--shadow); color: var(--ink); -webkit-tap-highlight-color: transparent;
	--jg-sliders: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cg stroke='%23000' stroke-width='2.4' stroke-linecap='round'%3E%3Cpath d='M3 6h18M3 12h18M3 18h18'/%3E%3Ccircle cx='8' cy='6' r='2.6'/%3E%3Ccircle cx='16' cy='12' r='2.6'/%3E%3Ccircle cx='10' cy='18' r='2.6'/%3E%3C/g%3E%3C/svg%3E");
}
.jigsaw [hidden], #jg-sheet { display: none !important; }
.jg-tools {
	display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 8px;
	position: relative; z-index: 2; background: var(--wood); border-radius: var(--radius) var(--radius) 0 0;
}
.jg-actions { flex: 1 1 auto; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.jigsaw :is(button, summary, select), #jg-ranking {
	display: inline-flex; align-items: center; justify-content: center; gap: .3em;
	min-height: 44px; padding: 5px 12px; margin: 0; border: 1px solid var(--wood-dark); border-radius: 12px;
	color: var(--ink); background: #fffaf0; font: 750 15px/1.15 var(--font); cursor: pointer;
}
.jg-settings summary { list-style: none; }
.jg-settings summary::-webkit-details-marker { display: none; }
.jigsaw :is(button, summary):hover, .jg-settings[open] summary { background: #fff; }
.jigsaw :is(button:enabled, summary):active { transform: translateY(1px); }
.jigsaw button[aria-pressed="true"], #jg-full { background: var(--sun); }
#jg-full::before {
	content: ""; flex: none; width: 1.1em; height: 1.1em;
	background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='none' stroke='%235a2e0c' stroke-width='3' d='M3 9V3h6m6 0h6v6m0 6v6h-6m-6 0H3v-6'/%3E%3C/svg%3E") center / contain no-repeat;
}
#jg-full[aria-pressed="true"]::before {
	background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='none' stroke='%235a2e0c' stroke-width='3' d='M9 3v6H3m12-6v6h6M9 21v-6H3m12 6v-6h6'/%3E%3C/svg%3E");
}
.jigsaw :disabled { opacity: .6; cursor: default; }
.jg-time { margin-inline-start: auto; white-space: nowrap; font-size: 15px; font-weight: 800; font-variant-numeric: tabular-nums; }

/* Difficulty tabs: three piece counts, plus "custom", which opens the options menu. */
.jg-levels { display: flex; border: 1px solid var(--wood-dark); border-radius: 14px; background: #fffaf0; }
.jg-settings { position: relative; flex: 1 1 auto; }
.jigsaw .jg-level {
	flex: 1 1 auto; flex-direction: column; gap: 0; min-height: 42px; padding: 2px 12px;
	border: 0; border-radius: 0; background: none; box-shadow: inset 0 -4px var(--level);
	line-height: 1.1; white-space: nowrap;
}
.jigsaw .jg-levels > * + * { border-inline-start: 1px solid var(--wood-dark); }
.jigsaw .jg-levels > :first-child { border-radius: 13px 0 0 13px; }
.jigsaw .jg-level--custom { display: flex; width: 100%; border-radius: 0 13px 13px 0; }
.jg-level--easy { --level: var(--green-dark); }
.jg-level--medium { --level: var(--piece-yellow); }
.jg-level--hard { --level: var(--piece-red); }
.jg-level--custom { --level: var(--piece-blue); }
.jigsaw .jg-level:is([aria-pressed="true"], .jg-level--on) { color: #fff; background: var(--level); }
.jigsaw .jg-level--medium[aria-pressed="true"] { color: var(--ink); }
.jigsaw .jg-level[aria-pressed="true"]:disabled { opacity: 1; }
.jg-settings[open] > .jg-level { box-shadow: inset 0 0 0 3px var(--level); }
.jg-settings[open] > .jg-level--on { box-shadow: inset 0 0 0 3px rgb(255 255 255 / .6); }
.jg-level small { display: inline-flex; align-items: center; gap: 2px; font-size: .8em; opacity: .9; }
.jg-level small::after {
	content: ""; width: 1em; height: 1em; background: currentColor;
	-webkit-mask: var(--icon-piece) center / contain no-repeat; mask: var(--icon-piece) center / contain no-repeat;
}
.jg-level--custom > span { display: inline-flex; align-items: center; gap: 5px; }
.jg-level--custom > span::before {
	content: ""; flex: none; width: 16px; height: 16px; background: currentColor;
	-webkit-mask: var(--jg-sliders) center / contain no-repeat; mask: var(--jg-sliders) center / contain no-repeat;
}
.jg-menu {
	position: absolute; top: calc(100% + 6px); right: 0; width: min(280px, calc(100vw - 24px));
	max-height: min(60vh, 400px); max-height: min(60dvh, 400px); overflow: auto; overscroll-behavior: contain;
	padding: 12px; display: grid; gap: 8px; background: var(--paper); border: 2px solid var(--wood-dark);
	border-radius: 14px; box-shadow: var(--shadow);
}
.jg-menu p { margin: 0; font-size: 14px; }
.jg-menu label { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 15px; }
.jg-menu .jg-check { justify-content: start; min-height: 44px; cursor: pointer; }
.jg-check input { width: 21px; height: 21px; accent-color: var(--green-dark); }

.jg-field {
	position: relative; height: clamp(280px, 61vh, 630px); height: clamp(280px, 61svh, 630px);
	min-height: 0; overflow: hidden; background-color: #f2ead9;
	background-image: radial-gradient(#ded2b9 1px, transparent 1px); background-size: 12px 12px;
}
#jg-board { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; }
#jg-board:focus-visible { outline-offset: -4px; }
#jg-board.jg-pointer:focus-visible { outline: none; }
#jg-target { fill: #fffcf4; stroke: #cebc97; stroke-dasharray: 5 4; vector-effect: non-scaling-stroke; }
#jg-pieces path {
	cursor: grab; stroke: #614c3f; stroke-width: 1; stroke-linejoin: round; vector-effect: non-scaling-stroke;
	transition: stroke-opacity .8s;
}
#jg-pieces path:active { cursor: grabbing; }
#jg-pieces .jg-selected path { stroke: var(--green-dark); stroke-width: 2; }
#jg-pieces.jg-done path { stroke-opacity: 0; cursor: default; }
/* Effects: sparkles where pieces join, the shine and confetti at the end (player.js skips motion for reduced-motion users). */
#jg-fx, .jg-shine { pointer-events: none; }
#jg-fx > *, .jg-shine > * { transform-box: fill-box; transform-origin: center; }
.jg-flash { fill: #fff; }
.jg-spark { stroke: #fff; stroke-width: 1.5; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
#jg-party { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
#jg-party i { position: absolute; left: 0; top: 0; width: 8px; height: 13px; border-radius: 2px; }
#jg-party i:nth-child(3n+1) { width: 10px; height: 10px; border-radius: 50%; }
#jg-party i:nth-child(3n) {
	width: 15px; height: 15px; border-radius: 0;
	-webkit-mask: var(--icon-piece) center / contain no-repeat; mask: var(--icon-piece) center / contain no-repeat;
}
#jg-thumbnail {
	position: absolute; right: 8px; top: 8px; width: min(22%, 140px); max-height: 25%; object-fit: contain;
	background: white; border: 3px solid white; border-radius: 6px; box-shadow: var(--shadow); pointer-events: none;
}
#jg-result, #jg-message {
	position: absolute; left: 50%; transform: translateX(-50%); padding: 10px 14px;
	width: max-content; max-width: 95%; text-align: center; background: var(--paper); border-radius: 14px;
	box-shadow: var(--shadow);
}
#jg-message { top: 35%; }
#jg-result {
	bottom: 8px; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 8px;
	animation: jg-pop .5s .35s cubic-bezier(.3, 1.5, .6, 1) both;
}
@keyframes jg-pop {
	from { opacity: 0; transform: translateX(-50%) translateY(16px) scale(.85); }
	to { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
}
.jigsaw .game-hint { margin: 0; padding: 6px 8px; font-size: 14px; }
.jigsaw.jg-expanded {
	position: fixed; inset: 0; z-index: 2147483647; width: 100%; height: 100%; height: 100dvh; max-width: none;
	padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
	display: flex; flex-direction: column; border: 0; border-radius: 0;
}
.jg-expanded .jg-field { flex: 1; }
.jg-expanded .jg-tools { border-radius: 0; }
html.jg-full-page { overflow: hidden; }
/* Narrower screens: fullscreen becomes an icon, the tabs take a row of their own, then text shrinks. */
@media (max-width: 899px) {
	#jg-full { width: 46px; padding: 0; }
	#jg-full > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
}
@media (max-width: 560px), (orientation: landscape) and (max-height: 540px) {
	.jg-tools, .jg-actions { gap: 5px; }
	.jg-tools { padding: 5px; }
	.jigsaw :is(button, summary) { padding-inline: 8px; font-size: 14px; }
	.jigsaw .jg-level { padding-inline: 6px; }
	.jg-time { font-size: 14px; }
}
@media (max-width: 560px) {
	.jg-levels { flex: 1 1 100%; }
	.jg-settings { position: static; } /* The menu then aligns with the whole toolbar. */
	.jg-menu { width: min(280px, 100%); }
}
@media (max-width: 380px) {
	.jigsaw :is(button, summary) { padding-inline: 6px; font-size: 13px; }
}
@media (max-width: 359px) {
	.jg-level--custom > span { gap: 0; font-size: 0; }
}
@media (orientation: landscape) and (max-height: 540px) {
	.jg-field { height: 69svh; }
	.jg-tools { padding-block: 4px; }
	.jigsaw .game-hint { font-size: 12px; padding-block: 2px; }
}
@media print {
	@page { size: landscape; margin: 10mm; }
	html.jg-full-page { overflow: visible; }
	body.page-game { background: white; }
	body.page-game > :not(main), .page-game .page--game > :not(#jg-sheet) { display: none !important; }
	.page-game :is(.site-main, .page--game) { padding: 0; margin: 0; max-width: none; background: white; box-shadow: none; }
	#jg-sheet { display: block !important; color: #333; text-align: center; break-inside: avoid; }
	#jg-sheet h2, #jg-sheet p { margin: 0; font: 16pt/1.25 sans-serif; }
	#jg-print-board { display: block; width: 100%; height: 150mm; margin: 3mm auto; }
}
