/**
 * WordPop — scoped styles.
 *
 * Every selector is prefixed with .wordpop so the plugin cannot leak into the
 * host theme. CSS custom properties let a theme retint the game without
 * touching this file.
 *
 * The layout follows Wordle: a centred stack of square tiles, six rows of
 * five or six depending on the chosen difficulty. Letters are typed on the
 * physical keyboard and fill the active row directly. Colours are the
 * canonical Wordle palette.
 */

.wordpop {
	--wp-tile-size: clamp(2.5rem, 13vw, 3.25rem);
	--wp-tile-gap: 0.3rem;
	--wp-key-size: clamp(2.1rem, 10vw, 2.9rem);
	--wp-key: #818384;
	--wp-radius: 3px;
	--wp-border: #3a3a3c;
	--wp-absent: #3a3a3c;
	--wp-present: #b59f3b;
	--wp-correct: #538d4e;
	--wp-text: #d7dadc;
	--wp-muted: #818384;
	--wp-error: #d9534f;
	--wp-surface: #121213;

	box-sizing: border-box;
	max-width: 30rem;
	margin: 0 auto;
	padding: 1rem 0.5rem 2rem;
	color: var(--wp-text);
	background: var(--wp-surface);
	font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	text-align: center;
	/* Stops a parent theme forcing its own font size onto the tiles. */
	font-size: 16px;
	line-height: 1.5;
}

.wordpop *,
.wordpop *::before,
.wordpop *::after {
	box-sizing: inherit;
}

.wordpop [hidden] {
	display: none !important;
}

.wordpop__notice,
.wordpop__message {
	min-height: 1.25rem;
	margin: 0.75rem 0 0;
	font-size: 0.8rem;
	color: var(--wp-muted);
}

.wordpop__message.is-error {
	color: var(--wp-error);
}

.wordpop__daily {
	margin: 1.5rem 0 0;
	color: var(--wp-muted);
	font-size: 0.7rem;
	letter-spacing: 0.08em;
	text-transform: uppercase;
}

/* ------------------------------------------------------------------ gate */

.wordpop__gate-form {
	padding: 1.5rem 1rem;
	border: 1px solid var(--wp-border);
	border-radius: var(--wp-radius);
	background: #1a1a1b;
}

.wordpop__gate-title {
	margin: 0 0 0.5rem;
	font-size: 1.1rem;
	font-weight: 700;
}

.wordpop__gate-copy {
	margin: 0 0 1.25rem;
	color: var(--wp-muted);
	font-size: 0.85rem;
}

.wordpop__gate-row {
	display: flex;
	gap: 0.5rem;
	flex-wrap: wrap;
}

.wordpop__gate-row .wordpop__input {
	flex: 1 1 14rem;
	min-width: 0;
}

.wordpop__input {
	padding: 0.7rem 0.85rem;
	border: 1px solid var(--wp-border);
	border-radius: var(--wp-radius);
	background: #1c1c1e;
	color: var(--wp-text);
	font-size: 1rem;
	font-family: inherit;
	text-align: center;
}

/*
 * The guess field is not shown on screen. It exists only to hold the typed
 * letters and to give screen readers something to announce, so it must stay
 * focusable and exposed to assistive technology while taking up no space.
 *
 * This has to come after .wordpop__input: the shared rule sets padding, border
 * and background on every field, and because both selectors weigh the same,
 * whichever is declared later wins. Declared earlier, the generic rule below
 * reinstated a visible text box on top of the hidden one.
 */
.wordpop__input--guess {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0 0 0 0);
	clip-path: inset(50%);
	white-space: nowrap;
	border: 0;
	background: transparent;
}

.wordpop__key:focus-visible {
	outline: 2px solid #fff;
	outline-offset: 2px;
}

.wordpop__btn:focus-visible,
.wordpop__mode:focus-visible {
	outline: 2px solid #fff;
	outline-offset: 2px;
}

.wordpop__btn {
	padding: 0.7rem 1.1rem;
	border: 1px solid var(--wp-border);
	border-radius: var(--wp-radius);
	background: transparent;
	color: var(--wp-text);
	font-size: 0.8rem;
	font-family: inherit;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.06em;
	cursor: pointer;
	transition: background 0.15s ease;
}

.wordpop__btn:hover:not(:disabled) {
	background: rgba(255, 255, 255, 0.08);
}

.wordpop__btn:disabled {
	opacity: 0.55;
	cursor: not-allowed;
}

.wordpop__btn--primary {
	background: var(--wp-correct);
	border-color: var(--wp-correct);
	color: #fff;
}

.wordpop__btn--primary:hover:not(:disabled) {
	background: #6aa664;
}

/* ----------------------------------------------------------------- modes */

.wordpop__modes {
	display: flex;
	gap: 0.5rem;
	justify-content: center;
	margin-bottom: 1.25rem;
}

.wordpop__mode {
	padding: 0.5rem 1.1rem;
	border: 1px solid var(--wp-border);
	border-radius: var(--wp-radius);
	background: transparent;
	color: var(--wp-muted);
	font-family: inherit;
	font-size: 0.75rem;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.08em;
	cursor: pointer;
	transition: all 0.15s ease;
}

.wordpop__mode:hover {
	color: var(--wp-text);
	border-color: var(--wp-muted);
}

.wordpop__mode.is-active {
	border-color: var(--wp-correct);
	background: rgba(83, 141, 78, 0.2);
	color: var(--wp-text);
}

/* ----------------------------------------------------------------- board */

.wordpop__board {
	display: flex;
	flex-direction: column;
	gap: var(--wp-tile-gap);
	align-items: center;
}

.wordpop__row {
	display: flex;
	gap: var(--wp-tile-gap);
	justify-content: center;
}

/*
 * The row the player types into.
 *
 * This needs the same flex treatment as .wordpop__row. Without it the host is
 * a plain block, so the tiles stack vertically, the row collapses to the
 * width of a single tile and sits off to one side instead of under the last
 * guess, and the empty rows below it appear to vanish.
 */
.wordpop__tiles {
	display: flex;
	gap: var(--wp-tile-gap);
	justify-content: center;
}

.wordpop__tile {
	display: grid;
	place-items: center;
	width: var(--wp-tile-size);
	height: var(--wp-tile-size);
	border: 2px solid var(--wp-border);
	border-radius: var(--wp-radius);
	background: transparent;
	color: var(--wp-text);
	font-size: clamp(1.05rem, 5vw, 1.4rem);
	font-weight: 700;
	line-height: 1;
	text-transform: uppercase;
}

.wordpop__tile--correct {
	background: var(--wp-correct);
	border-color: var(--wp-correct);
}

.wordpop__tile--present {
	background: var(--wp-present);
	border-color: var(--wp-present);
}

.wordpop__tile--absent {
	background: var(--wp-absent);
	border-color: var(--wp-absent);
}

/*
 * The row being typed into. It uses the same border as the empty guess rows so
 * the six-row grid reads as one block, with filled letters shown in a lighter
 * shade the way Wordle distinguishes them.
 */
.wordpop__tile--input {
	border-color: #565758;
	font-size: clamp(1rem, 4.6vw, 1.3rem);
}

.wordpop__tile--input.is-filled {
	border-color: var(--wp-muted);
}

/*
 * Wordle pops each tile in as the guess lands. Only the newest row animates,
 * so a re-render of earlier rows does not replay the effect.
 */
.wordpop__row--new .wordpop__tile {
	animation: wordpop-pop 0.12s ease-out backwards;
}

.wordpop__row--new .wordpop__tile:nth-child(1) { animation-delay: 0.00s; }
.wordpop__row--new .wordpop__tile:nth-child(2) { animation-delay: 0.04s; }
.wordpop__row--new .wordpop__tile:nth-child(3) { animation-delay: 0.08s; }
.wordpop__row--new .wordpop__tile:nth-child(4) { animation-delay: 0.12s; }
.wordpop__row--new .wordpop__tile:nth-child(5) { animation-delay: 0.16s; }
.wordpop__row--new .wordpop__tile:nth-child(6) { animation-delay: 0.20s; }

@keyframes wordpop-pop {
	0%   { transform: scale(0.4); opacity: 0; }
	70%  { transform: scale(1.08); opacity: 1; }
	100% { transform: scale(1); opacity: 1; }
}

/* Rejected guess, mirroring Wordle's shake. */
.wordpop__board.is-shaking {
	animation: wordpop-shake 0.4s ease;
}

@keyframes wordpop-shake {
	0%, 100% { transform: translateX(0); }
	20%      { transform: translateX(-5px); }
	40%      { transform: translateX(5px); }
	60%      { transform: translateX(-3px); }
	80%      { transform: translateX(3px); }
}

@media (prefers-reduced-motion: reduce) {
	.wordpop__row--new .wordpop__tile,
	.wordpop__board.is-shaking {
		animation: none;
	}

	.wordpop__btn,
	.wordpop__mode,
	.wordpop__key {
		transition: none;
	}
}

/* ------------------------------------------------------------- keyboard */

/*
 * The on-screen keyboard shown under the board.
 *
 * Rows are centred and each row is indented by half a key, the way Wordle
 * staggers them. Keys keep their own colours so a letter that has been used is
 * readable at a glance: green for correct, yellow for present, dark grey for
 * absent. Keyboard-only play still works, so the keys are an addition rather
 * than a replacement.
 */
.wordpop__keyboard {
	width: 100%;
	max-width: 30rem;
	margin: 1rem auto 0;
	user-select: none;
}

.wordpop__keyboard-row {
	display: flex;
	justify-content: center;
	gap: var(--wp-tile-gap);
	margin: 0 auto var(--wp-tile-gap);
}

/*
 * Stagger the rows by half a key each, matching Wordle's pyramid.
 *
 * The keys are a fixed width rather than flexed to fill the row, because the
 * stagger has to come from the row's own width. Shrinking the row and letting
 * justify-content centre the keys inside it produces the offset for free, and
 * it works out to exactly half a key per step: the top row is ten keys wide,
 * the next nine, so it is half a key narrower on each side.
 *
 * The offset used to be written as `margin-right: 0.5 * (var(--a) + var(--b))`,
 * which is not valid CSS. calc() cannot multiply two var() operands that are
 * lengths, so the declaration was dropped, every row stayed full width, and
 * the keys all sat flush left with no pyramid at all.
 */
.wordpop__keyboard-row:nth-child(2) {
	width: 95%;
}

.wordpop__keyboard-row:nth-child(3) {
	width: 90%;
}

/* Del and Enter, sized to sit under the narrower middle row. */
.wordpop__keyboard-row:nth-child(4) {
	width: 95%;
}

/*
 * Keys fill their row evenly. The row width, not the key width, is what
 * creates the stagger, so flex: 1 is correct here: narrowing the row narrows
 * every key in it and the block stays centred.
 */
.wordpop__key {
	flex: 1 1 0;
	min-width: 0;
	height: var(--wp-key-size);
	padding: 0;
	border: none;
	border-radius: var(--wp-radius);
	background: var(--wp-key);
	color: var(--wp-text);
	font-family: inherit;
	font-size: clamp(0.7rem, 2.6vw, 0.95rem);
	font-weight: 700;
	line-height: 1;
	text-transform: uppercase;
	cursor: pointer;
	transition: background 0.12s ease, transform 0.06s ease;
}

.wordpop__key:hover {
	background: #565758;
}

.wordpop__key:active {
	transform: scale(0.94);
}

/* Del and Enter are wider than a single letter key, as in Wordle. */
.wordpop__key--wide {
	flex: 1.5 1 0;
	font-size: clamp(0.6rem, 2.2vw, 0.8rem);
}

/*
 * Letter states. These must come after the base .wordpop__key rule because
 * both selectors weigh the same, so the later declaration wins.
 */
.wordpop__key.is-correct { background: var(--wp-correct); }
.wordpop__key.is-present { background: var(--wp-present); }
.wordpop__key.is-absent  { background: var(--wp-absent); }

/*
 * Every key keeps the same near-white label, including the ones ruled out.
 *
 * The absent state used to be dimmed to a mid grey, which on the dark grey key
 * is barely legible and reads as a muddy smudge on a phone. The background
 * already carries the state, so the text stays bright for contrast.
 */
.wordpop__key.is-correct,
.wordpop__key.is-present,
.wordpop__key.is-absent {
	color: #fff;
}

.wordpop__key.is-correct,
.wordpop__key.is-present,
.wordpop__key.is-absent {
	/* A scored key should not look pressable any more. */
	pointer-events: none;
}

.wordpop__keyboard[hidden] {
	display: none;
}

/* --------------------------------------------------------------- mobile */

/*
 * Phone layout.
 *
 * Two problems on a small screen. The keys shrink with the viewport, which put
 * them at roughly 34px wide, well under the 44px touch target that Apple and
 * Google both recommend, so they were hard to hit. And the whole game sat in a
 * column with the keyboard directly under the board, so on a tall phone the
 * keyboard could fall below the fold while the board stayed put.
 *
 * The fix is to stop letting the keys scale with width at all, and to pin the
 * keyboard to the bottom of the screen while the board scrolls above it. That
 * keeps the alphabet reachable in the same place your thumb already is, which
 * is where Wordle puts it on mobile.
 */
@media (max-width: 640px) {
	.wordpop {
		/* Full-bleed on a phone, with a little breathing room at the sides. */
		max-width: none;
		padding: 0.5rem 0.25rem 0;

		/*
		 * Keys keep a usable height instead of tracking the viewport width,
		 * which left them at about 34px tall. The floor is the 44px touch
		 * target that Apple and Google both recommend. Key width is not
		 * affected by this; the keys are flexed, so the row widths and the gap
		 * below decide that.
		 */
		--wp-key-size: clamp(2.75rem, 11vw, 3.25rem);
		--wp-tile-gap: 0.15rem;
	}

	/*
	 * The board scrolls under a fixed keyboard. `100dvh` tracks the real
	 * viewport height and shrinks when the on-screen keyboard opens, so the
	 * alphabet is never pushed off the bottom by it. The 100vh fallback is
	 * for browsers without dynamic viewport units.
	 */
	.wordpop__game {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		min-height: 100dvh;
	}

	.wordpop__board {
		flex: 1 1 auto;
		/* Prevent the board being crushed by the flex algorithm. */
		min-height: 0;
	}

	.wordpop__keyboard {
		/* Stay put while the board scrolls, so the keys are always in reach. */
		position: sticky;
		bottom: 0;
		z-index: 2;
		/* Bleed to the screen edges so the keys are not cramped by the gutter. */
		margin: 0 -0.25rem;
		padding: 0.4rem 0.25rem calc(0.4rem + env(safe-area-inset-bottom, 0px));
		background: var(--wp-surface);
	}

	/* Tiles and gaps scale down a little to leave the keyboard more room. */
	.wordpop__daily,
	.wordpop__message {
		font-size: 0.75rem;
	}

	/* Mode buttons get real tap targets too. */
	.wordpop__mode {
		padding: 0.75rem 1rem;
		min-height: 44px;
	}

	/*
	 * Keys are narrower on a phone, so the label needs to shrink to match or it
	 * clips against its neighbours.
	 */
	.wordpop__key {
		font-size: clamp(0.8rem, 3.4vw, 1rem);
	}
}

/*
 * Landscape phones are short, so a sticky keyboard would eat the whole screen.
 * Everything scrolls normally instead, which is the lesser evil when there is
 * barely any vertical room.
 */
@media (max-width: 900px) and (orientation: landscape) {
	.wordpop__game {
		display: block;
		min-height: 0;
	}

	.wordpop__keyboard {
		position: static;
		margin: 0;
		padding: 0.5rem 0;
	}
}

/* --------------------------------------------------------------- result */

.wordpop__result {
	margin-top: 1.25rem;
	padding: 1rem;
	border-top: 1px solid var(--wp-border);
}

.wordpop__result-title {
	margin: 0 0 0.35rem;
	font-size: 1.15rem;
	font-weight: 700;
}

.wordpop__result-answer {
	margin: 0;
	color: var(--wp-text);
	font-size: 0.95rem;
	letter-spacing: 0.1em;
	text-transform: uppercase;
}

.wordpop__result-next {
	margin: 0.75rem 0 0;
	color: var(--wp-muted);
	font-size: 0.75rem;
}

/* --------------------------------------------------------------- admin */

.wordpop-admin-grid {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(22rem, 1fr));
	gap: 20px;
	margin: 20px 0;
	align-items: start;
}

.wordpop-admin-grid .card {
	padding: 16px;
	margin: 0;
}

.wordpop-admin-grid .card h2 {
	margin-top: 0;
}

.wordpop-admin-grid hr {
	margin: 20px 0 16px;
	border: 0;
	border-top: 1px solid #dcdcde;
}

/* --------------------------------------------------------------- mobile */

@media (max-width: 26rem) {
	.wordpop {
		--wp-tile-gap: 0.22rem;
		padding: 0.75rem 0.35rem 1.5rem;
	}
}

@media (min-width: 40rem) {
	.wordpop {
		--wp-tile-size: 3.5rem;
	}
}
