/*
 * Typing Practice Pro - typing workspace, keyboard and result screen.
 *
 * Accessibility note: correct and incorrect characters are never signalled by
 * colour alone. Incorrect characters also carry an underline and a background
 * tint; missed characters carry a strike-through; corrected characters carry a
 * dotted underline. The legend below the workspace states this in words.
 */

/* ==========================================================================
   1. Shell
   ========================================================================== */

.tpp-tool {
	--tpp-tool-gap: var(--tpp-space-4);
	display: flex;
	flex-direction: column;
	gap: var(--tpp-tool-gap);
	padding: var(--tpp-space-5);
	background: var(--tpp-surface);
	border: 1px solid var(--tpp-border);
	border-radius: var(--tpp-radius-lg);
	box-shadow: var(--tpp-shadow-sm);
	container-type: inline-size;
}

.tpp-tool.is-compact { padding: var(--tpp-space-4); }

.tpp-tool.is-loading { opacity: 0.6; pointer-events: none; }

.tpp-tool:fullscreen {
	justify-content: center;
	border-radius: 0;
	padding: var(--tpp-space-7) var(--tpp-space-6);
	background: var(--tpp-bg);
}

.tpp-tool-header {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-3);
	align-items: center;
	justify-content: space-between;
}

.tpp-tool-title {
	margin: 0;
	font-size: 1.05rem;
}

.tpp-tool-subtitle {
	margin: 0;
	font-size: 0.88rem;
	color: var(--tpp-text-muted);
}

/* ==========================================================================
   2. Toolbar
   ========================================================================== */

.tpp-toolbar {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-3);
	align-items: center;
	padding: var(--tpp-space-3);
	background: var(--tpp-bg-alt);
	border-radius: var(--tpp-radius);
}

.tpp-toolbar-group {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: var(--tpp-space-1);
}

.tpp-toolbar-label {
	font-size: 0.78rem;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.05em;
	color: var(--tpp-text-muted);
	margin-right: var(--tpp-space-1);
}

.tpp-chip {
	padding: 0.35rem 0.7rem;
	border: 1px solid var(--tpp-border-strong);
	border-radius: 999px;
	background: var(--tpp-surface);
	color: var(--tpp-text);
	font: inherit;
	font-size: 0.85rem;
	font-weight: 600;
	cursor: pointer;
	transition: background var(--tpp-transition), color var(--tpp-transition);
}

.tpp-chip:hover { border-color: var(--tpp-primary); color: var(--tpp-primary); }

.tpp-chip.is-active,
.tpp-chip[aria-pressed="true"] {
	background: var(--tpp-primary);
	border-color: var(--tpp-primary);
	color: #fff;
}

.tpp-toolbar select,
.tpp-toolbar input[type="number"] {
	width: auto;
	min-width: 8rem;
	padding: 0.35rem 0.5rem;
	font-size: 0.88rem;
}

.tpp-toolbar-spacer { margin-left: auto; }

/* ==========================================================================
   3. Stats bar
   ========================================================================== */

.tpp-stats-bar {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
	gap: var(--tpp-space-2);
	padding: var(--tpp-space-3);
	background: var(--tpp-bg-sunken);
	border-radius: var(--tpp-radius);
}

.tpp-stat-cell {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.1rem;
	text-align: center;
}

.tpp-stat-cell b {
	font-size: clamp(1.15rem, 2.4vw, 1.6rem);
	font-weight: 700;
	line-height: 1.1;
	font-variant-numeric: tabular-nums;
}

.tpp-stat-cell span {
	font-size: 0.72rem;
	text-transform: uppercase;
	letter-spacing: 0.04em;
	color: var(--tpp-text-muted);
}

.tpp-stat-cell.is-primary b { color: var(--tpp-primary); }
.tpp-stat-cell.is-time b { color: var(--tpp-accent); }

.tpp-progress {
	height: 6px;
	background: var(--tpp-bg-sunken);
	border-radius: 999px;
	overflow: hidden;
}

.tpp-progress-bar {
	display: block;
	height: 100%;
	width: 0;
	background: var(--tpp-primary);
	transition: width 120ms linear;
}

/* ==========================================================================
   4. Passage
   ========================================================================== */

.tpp-workspace {
	position: relative;
}

.tpp-passage {
	position: relative;
	max-width: var(--tpp-type-width);
	margin-inline: auto;
	height: calc(var(--tpp-type-size) * var(--tpp-type-line) * 3);
	padding: var(--tpp-space-4);
	overflow: hidden;
	background: var(--tpp-bg);
	border: 2px solid var(--tpp-border);
	border-radius: var(--tpp-radius);
	cursor: text;
}

.tpp-tool.is-focused .tpp-passage {
	border-color: var(--tpp-primary);
}

.tpp-tool.is-blocked .tpp-passage {
	animation: tpp-shake 160ms ease;
	border-color: var(--tpp-incorrect);
}

@keyframes tpp-shake {
	0%, 100% { transform: translateX(0); }
	25% { transform: translateX(-4px); }
	75% { transform: translateX(4px); }
}

.tpp-passage-inner {
	font-family: var(--tpp-font-mono);
	font-size: var(--tpp-type-size);
	line-height: var(--tpp-type-line);
	letter-spacing: 0.01em;
	color: var(--tpp-text-muted);
	transition: transform 140ms ease;
	will-change: transform;
}

.tpp-passage-inner.is-code {
	white-space: pre-wrap;
	tab-size: 4;
}

.tpp-word {
	display: inline-block;
	white-space: nowrap;
	border-radius: 3px;
}

.tpp-word.is-active {
	background: color-mix(in srgb, var(--tpp-primary) 10%, transparent);
	box-shadow: 0 0 0 2px color-mix(in srgb, var(--tpp-primary) 10%, transparent);
}

.tpp-c {
	position: relative;
	white-space: pre;
}

.tpp-c.is-correct {
	color: var(--tpp-correct);
}

/* Colour plus an underline plus a tint: three independent signals. */
.tpp-c.is-incorrect {
	color: var(--tpp-incorrect);
	background: color-mix(in srgb, var(--tpp-incorrect) 14%, transparent);
	text-decoration: underline;
	text-decoration-style: wavy;
	text-decoration-thickness: 1px;
	text-underline-offset: 3px;
	border-radius: 2px;
}

.tpp-c.is-corrected {
	color: var(--tpp-correct);
	text-decoration: underline dotted;
	text-underline-offset: 3px;
	opacity: 0.85;
}

.tpp-c.is-missed {
	color: var(--tpp-incorrect);
	text-decoration: line-through;
	opacity: 0.75;
}

.tpp-c.tpp-space.is-incorrect,
.tpp-c.tpp-space.is-missed {
	background: color-mix(in srgb, var(--tpp-incorrect) 30%, transparent);
}

.tpp-c.tpp-newline,
.tpp-c.tpp-tab {
	color: var(--tpp-border-strong);
	opacity: 0.7;
}

/* ---- Caret ------------------------------------------------------------- */

.tpp-c.is-current::before {
	content: "";
	position: absolute;
	left: -1px;
	top: 0.12em;
	bottom: 0.12em;
	width: 2px;
	background: var(--tpp-primary);
	animation: tpp-caret 1s steps(2, start) infinite;
}

@keyframes tpp-caret {
	to { opacity: 0; }
}

[data-caret="box"] .tpp-c.is-current::before { display: none; }

[data-caret="box"] .tpp-c.is-current {
	background: var(--tpp-primary);
	color: #fff;
	border-radius: 2px;
}

[data-caret="underline"] .tpp-c.is-current::before {
	top: auto;
	bottom: 0;
	left: 0;
	right: 0;
	width: auto;
	height: 3px;
}

@media (prefers-reduced-motion: reduce) {
	.tpp-c.is-current::before { animation: none; }
}

/* ---- Capture field ------------------------------------------------------- */

.tpp-capture {
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	padding: 0;
	border: 0;
	opacity: 0;
	resize: none;
	background: transparent;
	color: transparent;
	caret-color: transparent;
	font-size: 16px; /* Stops iOS zooming when the field takes focus. */
}

.tpp-capture:focus { outline: none; }

/* ---- Overlay ------------------------------------------------------------- */

/*
 * The "click here to begin" veil.
 *
 * pointer-events: none is deliberate and important. The overlay sits directly
 * on top of the typing area, so if it were ever left on screen - a stale
 * cached stylesheet, a failed script, a browser that ignores the hidden
 * attribute - it would swallow every click and the passage could never be
 * focused. Letting clicks fall through to .tpp-passage underneath means the
 * tool stays usable even when the overlay misbehaves.
 */
.tpp-overlay {
	position: absolute;
	inset: 0;
	z-index: 2;
	display: grid;
	place-items: center;
	padding: var(--tpp-space-4);
	background: var(--tpp-bg);
	background: color-mix(in srgb, var(--tpp-bg) 82%, transparent);
	backdrop-filter: blur(2px);
	border-radius: var(--tpp-radius);
	font-weight: 600;
	color: var(--tpp-text);
	text-align: center;
	pointer-events: none;
}

/* ---- Legend --------------------------------------------------------------- */

.tpp-legend {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-2) var(--tpp-space-4);
	justify-content: center;
	font-size: 0.8rem;
	color: var(--tpp-text-muted);
	list-style: none;
	margin: 0;
	padding: 0;
}

.tpp-legend li {
	display: flex;
	align-items: center;
	gap: var(--tpp-space-1);
}

.tpp-legend .tpp-legend-sample {
	font-family: var(--tpp-font-mono);
	font-weight: 700;
}

.tpp-legend .is-correct { color: var(--tpp-correct); }

.tpp-legend .is-incorrect {
	color: var(--tpp-incorrect);
	text-decoration: underline wavy;
}

.tpp-legend .is-missed {
	color: var(--tpp-incorrect);
	text-decoration: line-through;
}

.tpp-legend .is-corrected {
	color: var(--tpp-correct);
	text-decoration: underline dotted;
}

/* ==========================================================================
   5. Controls
   ========================================================================== */

.tpp-controls {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-2);
	align-items: center;
	justify-content: center;
}

.tpp-tool-notice {
	padding: var(--tpp-space-3);
	background: color-mix(in srgb, #d97706 12%, transparent);
	border-left: 4px solid #d97706;
	border-radius: var(--tpp-radius-sm);
	font-size: 0.9rem;
}

.tpp-finger-hint {
	min-height: 1.5em;
	text-align: center;
	font-size: 0.92rem;
	font-weight: 600;
	color: var(--tpp-accent);
}

.tpp-tool.is-guide-off .tpp-finger-hint { display: none; }

/* ==========================================================================
   6. Virtual keyboard
   ========================================================================== */

.tpp-keyboard-wrap {
	max-width: 760px;
	margin-inline: auto;
	width: 100%;
}

.tpp-tool.is-keyboard-hidden .tpp-keyboard-wrap { display: none; }

.tpp-keyboard {
	display: flex;
	flex-direction: column;
	gap: 4px;
	padding: var(--tpp-space-3);
	background: var(--tpp-bg-alt);
	border-radius: var(--tpp-radius);
	user-select: none;
}

.tpp-kb-row {
	display: flex;
	gap: 4px;
}

.tpp-kb-key {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	min-width: 0;
	padding: 0.5rem 0.2rem;
	background: var(--tpp-surface);
	border: 1px solid var(--tpp-border);
	border-bottom-width: 2px;
	border-radius: var(--tpp-radius-sm);
	font-family: var(--tpp-font-mono);
	font-size: 0.78rem;
	font-weight: 600;
	color: var(--tpp-text-muted);
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	transition: background var(--tpp-transition), color var(--tpp-transition);
}

.tpp-kb-key small {
	display: block;
	font-size: 0.62rem;
	opacity: 0.7;
	line-height: 1;
}

.tpp-kb-key.is-home {
	border-bottom-color: var(--tpp-accent);
	border-bottom-width: 3px;
}

.tpp-kb-key.is-next {
	background: var(--tpp-primary);
	border-color: var(--tpp-primary);
	color: #fff;
	transform: translateY(1px);
}

.tpp-kb-key.is-next-shift {
	background: var(--tpp-accent);
	border-color: var(--tpp-accent);
	color: #fff;
}

/* Hand zones, shown only in beginner guidance mode. */
.tpp-tool[data-guide="hands"] .tpp-kb-key[data-hand="left"] {
	background: color-mix(in srgb, var(--tpp-primary) 8%, var(--tpp-surface));
}

.tpp-tool[data-guide="hands"] .tpp-kb-key[data-hand="right"] {
	background: color-mix(in srgb, var(--tpp-accent) 8%, var(--tpp-surface));
}

.tpp-keyboard-legend {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-3);
	justify-content: center;
	margin-top: var(--tpp-space-2);
	font-size: 0.78rem;
	color: var(--tpp-text-muted);
}

@media (max-width: 640px) {
	.tpp-keyboard { padding: var(--tpp-space-2); }
	.tpp-kb-key { font-size: 0.62rem; padding: 0.35rem 0.1rem; }
	.tpp-kb-key small { display: none; }
}

/* ==========================================================================
   7. Settings drawer
   ========================================================================== */

.tpp-drawer {
	display: grid;
	gap: var(--tpp-space-4);
	grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
	padding: var(--tpp-space-5);
	background: var(--tpp-bg-alt);
	border: 1px solid var(--tpp-border);
	border-radius: var(--tpp-radius);
}

.tpp-drawer fieldset {
	border: 0;
	margin: 0;
	padding: 0;
}

.tpp-drawer legend {
	font-size: 0.78rem;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.05em;
	color: var(--tpp-text-muted);
	margin-bottom: var(--tpp-space-2);
	padding: 0;
}

.tpp-drawer label {
	display: flex;
	align-items: center;
	gap: var(--tpp-space-2);
	font-weight: 400;
	font-size: 0.9rem;
	margin-bottom: var(--tpp-space-2);
}

.tpp-drawer input[type="checkbox"] { width: auto; }
.tpp-drawer input[type="range"] { flex: 1; width: auto; }
.tpp-drawer select { padding: 0.35rem 0.5rem; font-size: 0.88rem; }

/* ==========================================================================
   8. Result screen
   ========================================================================== */

.tpp-result {
	padding: var(--tpp-space-5);
	background: var(--tpp-bg-alt);
	border: 1px solid var(--tpp-border);
	border-radius: var(--tpp-radius);
}

.tpp-result:focus { outline: none; }

.tpp-result.is-visible {
	animation: tpp-fade-in 200ms ease;
}

@keyframes tpp-fade-in {
	from { opacity: 0; transform: translateY(6px); }
	to { opacity: 1; transform: none; }
}

.tpp-result-head {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-3);
	align-items: baseline;
	justify-content: space-between;
	margin-bottom: var(--tpp-space-4);
}

.tpp-result-headline {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-5);
	align-items: flex-end;
}

.tpp-result-primary {
	display: flex;
	flex-direction: column;
}

.tpp-result-primary b {
	font-size: clamp(2.4rem, 7vw, 3.6rem);
	line-height: 1;
	font-weight: 800;
	font-variant-numeric: tabular-nums;
	color: var(--tpp-primary);
}

.tpp-result-primary span {
	font-size: 0.85rem;
	text-transform: uppercase;
	letter-spacing: 0.05em;
	color: var(--tpp-text-muted);
}

.tpp-rating {
	display: inline-block;
	padding: 0.3rem 0.9rem;
	border-radius: 999px;
	background: var(--tpp-primary-soft);
	color: var(--tpp-primary-dark);
	font-weight: 700;
	font-size: 0.9rem;
}

[data-rating="needs-practice"] .tpp-rating { background: #fee2e2; color: #991b1b; }
[data-rating="developing"] .tpp-rating { background: #fef3c7; color: #92400e; }
[data-rating="good"] .tpp-rating { background: #dbeafe; color: #1e40af; }
[data-rating="very-good"] .tpp-rating { background: #d1fae5; color: #065f46; }
[data-rating="excellent"] .tpp-rating { background: #cffafe; color: #155e75; }
[data-rating="professional"] .tpp-rating { background: #ede9fe; color: #5b21b6; }

.tpp-personal-best {
	display: inline-flex;
	align-items: center;
	gap: var(--tpp-space-1);
	padding: 0.25rem 0.7rem;
	border-radius: 999px;
	background: var(--tpp-correct);
	color: #fff;
	font-size: 0.8rem;
	font-weight: 700;
}

.tpp-result-grid {
	display: grid;
	gap: var(--tpp-space-2);
	grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
	margin-bottom: var(--tpp-space-5);
}

.tpp-result-item {
	display: flex;
	justify-content: space-between;
	gap: var(--tpp-space-2);
	padding: var(--tpp-space-2) var(--tpp-space-3);
	background: var(--tpp-surface);
	border-radius: var(--tpp-radius-sm);
	font-size: 0.9rem;
}

.tpp-result-item dt { color: var(--tpp-text-muted); margin: 0; }
.tpp-result-item dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }

.tpp-result-section {
	margin-bottom: var(--tpp-space-5);
}

.tpp-result-section h3 {
	font-size: 1rem;
	margin-bottom: var(--tpp-space-3);
}

.tpp-sparkline {
	color: var(--tpp-primary);
	background: var(--tpp-surface);
	border-radius: var(--tpp-radius-sm);
	padding: var(--tpp-space-2);
}

.tpp-sparkline svg {
	display: block;
	width: 100%;
	height: 110px;
}

.tpp-achievements {
	padding: var(--tpp-space-4);
	background: var(--tpp-surface);
	border: 1px solid var(--tpp-correct);
	border-radius: var(--tpp-radius);
}

.tpp-achievements ul { margin: 0; padding-left: 1.2rem; }

.tpp-result-actions {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-2);
}

/* ==========================================================================
   9. Certificate
   ========================================================================== */

.tpp-certificate {
	padding: var(--tpp-space-4);
	background: var(--tpp-bg-alt);
	border-radius: var(--tpp-radius);
}

.tpp-certificate-inner {
	max-width: 820px;
	margin-inline: auto;
	padding: var(--tpp-space-7) var(--tpp-space-6);
	background: #fff;
	color: #111827;
	border: 3px double #94a3b8;
	border-radius: var(--tpp-radius);
	text-align: center;
}

.tpp-certificate-logo { margin-bottom: var(--tpp-space-4); }

.tpp-certificate-site {
	margin: 0 0 var(--tpp-space-2);
	font-size: 0.85rem;
	text-transform: uppercase;
	letter-spacing: 0.14em;
	color: #64748b;
}

.tpp-certificate-title {
	margin: 0 0 var(--tpp-space-5);
	font-size: clamp(1.4rem, 3.5vw, 2rem);
	color: #0f172a;
}

.tpp-certificate-presented {
	margin: 0;
	font-size: 0.95rem;
	color: #475569;
}

.tpp-certificate-name {
	margin: var(--tpp-space-2) 0 var(--tpp-space-5);
	font-size: clamp(1.5rem, 4vw, 2.2rem);
	font-weight: 700;
	border-bottom: 1px solid #cbd5e1;
	display: inline-block;
	padding: 0 var(--tpp-space-5) var(--tpp-space-2);
}

.tpp-certificate-stats {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	gap: var(--tpp-space-6);
	margin-bottom: var(--tpp-space-5);
}

.tpp-certificate-stats > div {
	display: flex;
	flex-direction: column;
}

.tpp-certificate-figure {
	font-size: 1.9rem;
	font-weight: 800;
	color: #1d4ed8;
	line-height: 1.1;
}

.tpp-certificate-label {
	font-size: 0.78rem;
	text-transform: uppercase;
	letter-spacing: 0.08em;
	color: #64748b;
}

.tpp-certificate-meta {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
	gap: var(--tpp-space-3);
	margin-bottom: var(--tpp-space-6);
	text-align: left;
	font-size: 0.85rem;
}

.tpp-certificate-meta dt { color: #64748b; margin: 0; }
.tpp-certificate-meta dd { margin: 0; font-weight: 600; }

.tpp-certificate-signature {
	margin: 0 auto var(--tpp-space-3);
	padding-top: var(--tpp-space-2);
	border-top: 1px solid #0f172a;
	width: min(100%, 260px);
	font-weight: 600;
}

.tpp-certificate-verify {
	font-size: 0.78rem;
	color: #475569;
	word-break: break-all;
}

.tpp-certificate-disclaimer {
	margin: var(--tpp-space-4) 0 0;
	font-size: 0.72rem;
	color: #64748b;
	max-width: 60ch;
	margin-inline: auto;
}

/* ==========================================================================
   10. Exam mode
   ========================================================================== */

.tpp-tool.is-exam {
	border-color: var(--tpp-accent);
	border-width: 2px;
}

.tpp-exam-banner {
	display: flex;
	flex-wrap: wrap;
	gap: var(--tpp-space-2);
	align-items: center;
	justify-content: center;
	padding: var(--tpp-space-2) var(--tpp-space-4);
	background: var(--tpp-accent);
	color: #fff;
	border-radius: var(--tpp-radius-sm);
	font-size: 0.88rem;
	font-weight: 600;
}

/* ==========================================================================
   11. Small screens
   ========================================================================== */

@container (max-width: 620px) {
	.tpp-stats-bar { grid-template-columns: repeat(3, 1fr); }
	.tpp-toolbar { gap: var(--tpp-space-2); }
	.tpp-toolbar-spacer { margin-left: 0; }
}

@media (max-width: 640px) {
	.tpp-tool {
		padding: var(--tpp-space-3);
		border-radius: var(--tpp-radius);
	}

	.tpp-passage {
		height: calc(var(--tpp-type-size) * var(--tpp-type-line) * 4);
		padding: var(--tpp-space-3);
	}

	.tpp-passage-inner { font-size: clamp(16px, 5vw, var(--tpp-type-size)); }

	.tpp-controls .tpp-btn { flex: 1 1 auto; }
	.tpp-result-actions .tpp-btn { flex: 1 1 auto; }
}
