/*
 * On-screen keyboard and typing session UI. Loaded only on views that run the app.
 */

/* ---------- Session layout ---------- */
.ty-session {
	display: grid;
	gap: 1.1rem;
}

.ty-session.is-blocked .ty-toolbar,
.ty-session.is-blocked .ty-kb-wrap {
	display: none;
}

.ty-toolbar {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.75rem 1.25rem;
}

.ty-seg {
	display: inline-flex;
	flex-wrap: wrap;
	padding: 4px;
	border-radius: 999px;
	background: var(--ty-surface-2);
}

.ty-seg__btn {
	min-height: 40px;
	padding: 0.4rem 1.05rem;
	border: 0;
	border-radius: 999px;
	background: transparent;
	color: var(--ty-muted);
	font: inherit;
	font-size: 0.95rem;
	font-weight: 500;
	cursor: pointer;
	transition: background-color 0.15s ease, color 0.15s ease;
}

.ty-seg__btn:hover {
	color: var(--ty-ink);
}

.ty-seg__btn[aria-pressed="true"] {
	background: var(--ty-surface);
	box-shadow: 0 1px 2px rgb(26 29 46 / 10%), 0 2px 8px rgb(26 29 46 / 6%);
	color: var(--ty-ink);
	font-weight: 600;
}

.ty-live {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
	margin: 0 auto 0 0;
}

.ty-live div {
	display: flex;
	flex-direction: column-reverse;
	min-width: 5.5rem;
	padding: 0.35rem 0.9rem;
	border: 1px solid var(--ty-line);
	border-radius: 12px;
	background: var(--ty-surface);
}

.ty-live dt {
	color: var(--ty-muted);
	font-size: 0.72rem;
	font-weight: 500;
	letter-spacing: 0.04em;
	text-transform: uppercase;
}

.ty-live dd {
	margin: 0;
	font-family: var(--ty-font-mono);
	font-size: 1.3rem;
	font-weight: 600;
	font-variant-numeric: tabular-nums;
	line-height: 1.3;
}

.ty-caps {
	margin: 0;
}

/* ---------- Stage: the text being typed ---------- */
.ty-stage {
	position: relative;
	padding: clamp(1.1rem, 3vw, 1.75rem) clamp(1.1rem, 3.5vw, 2.25rem);
	border: 2px solid var(--ty-line);
	border-radius: var(--ty-radius);
	background: var(--ty-surface);
	cursor: text;
	transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.ty-stage.is-focused {
	border-color: color-mix(in srgb, var(--ty-primary) 70%, var(--ty-line));
	box-shadow: 0 0 0 5px color-mix(in srgb, var(--ty-primary) 12%, transparent);
}

.ty-stage.is-complete {
	cursor: default;
	opacity: 0.7;
}

/* The field that owns keyboard input: focusable, invisible, never zooms on iOS. */
.ty-input {
	position: absolute;
	top: 0;
	left: 0;
	width: 1px;
	height: 1px;
	padding: 0;
	border: 0;
	opacity: 0;
	font-size: 16px;
	resize: none;
	pointer-events: none;
}

.ty-text {
	--ty-line-h: 2em;
	overflow: hidden;
	height: calc(var(--ty-line-h) * 3);
	font-family: var(--ty-font-mono);
	font-size: clamp(1.3rem, 2.7vw, 1.8rem);
	font-weight: 400;
}

.ty-text__inner {
	position: relative;
	line-height: var(--ty-line-h);
	transition: transform 0.2s ease;
	word-break: normal;
	overflow-wrap: normal;
}

.ty-ch {
	position: relative;
	border-radius: 5px;
	color: var(--ty-ink);
	white-space: pre-wrap;
}

.ty-ch.is-ok {
	color: var(--ty-good);
}

.ty-ch.is-bad {
	background: var(--ty-bad-bg);
	color: var(--ty-bad);
	text-decoration: underline wavy var(--ty-bad);
	text-decoration-thickness: 1px;
	text-underline-offset: 0.2em;
}

.ty-ch.is-bad.is-space {
	background: color-mix(in srgb, var(--ty-bad) 35%, transparent);
}

.ty-ch.is-current {
	background: color-mix(in srgb, var(--ty-primary) 16%, transparent);
	color: var(--ty-ink);
	box-shadow: inset 0 -3px 0 var(--ty-primary);
}

.ty-stage:not(.is-focused) .ty-ch.is-current {
	box-shadow: inset 0 -3px 0 var(--ty-line);
}

.ty-ch.is-miss {
	animation: ty-miss 0.3s ease;
}

@keyframes ty-miss {
	0%,
	100% {
		transform: translateX(0);
	}

	30% {
		transform: translateX(-2px);
	}

	60% {
		transform: translateX(2px);
	}
}

.ty-stage__overlay {
	position: absolute;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 1rem;
	border-radius: inherit;
	background: color-mix(in srgb, var(--ty-surface) 70%, transparent);
	backdrop-filter: blur(3px);
	color: var(--ty-ink);
	font-weight: 600;
	text-align: center;
}

.ty-stage__overlay[hidden] {
	display: none;
}

@media (prefers-reduced-motion: reduce) {
	.ty-text__inner {
		transition: none;
	}

	.ty-ch.is-miss {
		animation: none;
		outline: 2px solid var(--ty-bad);
	}
}

/* ---------- Keyboard ---------- */
.ty-kb {
	--ty-kb-gap: clamp(3px, 0.5vw, 6px);
	display: grid;
	gap: var(--ty-kb-gap);
	width: 100%;
	max-width: 880px;
	margin-inline: auto;
	padding: clamp(8px, 1.4vw, 14px);
	border: 1px solid var(--ty-line);
	border-radius: calc(var(--ty-radius) + 2px);
	background: var(--ty-surface-2);
	user-select: none;
	font-family: var(--ty-font-sans);
}

.ty-kb__row {
	display: flex;
	gap: var(--ty-kb-gap);
}

.ty-kb__key,
.ty-kb__gap {
	flex: var(--w) 1 0;
	min-width: 0;
}

.ty-kb__key {
	position: relative;
	display: flex;
	aspect-ratio: calc(var(--w) * 1.05) / 1;
	border: 1px solid color-mix(in srgb, var(--ty-f) 35%, var(--ty-line));
	border-radius: clamp(5px, 0.8vw, 10px);
	background: color-mix(in srgb, var(--ty-f) 13%, var(--ty-surface));
	box-shadow: inset 0 -3px 0 color-mix(in srgb, var(--ty-f) 70%, transparent);
	color: var(--ty-ink);
	transition: transform 0.08s ease, background-color 0.12s ease, box-shadow 0.12s ease;
}

.ty-kb__key.ty-finger--none {
	--ty-f: var(--ty-line);
}

.ty-kb__cap {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	width: 100%;
	padding-bottom: 3px;
	overflow: hidden;
	font-size: clamp(0.62rem, 1.5vw, 1rem);
	font-weight: 500;
	line-height: 1.1;
}

.ty-kb__cap.is-named {
	font-size: clamp(0.5rem, 1.05vw, 0.78rem);
	font-weight: 400;
	white-space: nowrap;
}

.ty-kb__shift {
	color: var(--ty-muted);
	font-size: 0.75em;
}

.ty-kb__key.has-bump::after {
	content: "";
	position: absolute;
	bottom: 16%;
	left: 50%;
	width: 24%;
	height: 2px;
	border-radius: 2px;
	background: currentcolor;
	opacity: 0.6;
	transform: translateX(-50%);
}

.ty-kb__key.is-next {
	z-index: 1;
	background: var(--ty-f);
	border-color: color-mix(in srgb, var(--ty-f) 60%, #000);
	box-shadow: 0 0 0 4px color-mix(in srgb, var(--ty-f) 40%, transparent), 0 6px 14px color-mix(in srgb, var(--ty-f) 45%, transparent), inset 0 -3px 0 rgb(0 0 0 / 18%);
	color: #1a1d2e;
	font-weight: 700;
	transform: translateY(-2px);
}

.ty-kb__key.is-next .ty-kb__shift {
	color: #1a1d2e;
}

.ty-kb__key.is-pressed {
	transform: translateY(2px);
}

.ty-kb__key.is-wrong {
	background: color-mix(in srgb, var(--ty-bad) 30%, var(--ty-surface));
	animation: ty-flash 0.32s ease;
}

@keyframes ty-flash {
	from {
		box-shadow: 0 0 0 4px color-mix(in srgb, var(--ty-bad) 45%, transparent);
	}

	to {
		box-shadow: none;
	}
}

@media (prefers-reduced-motion: reduce) {
	.ty-kb__key {
		transition: none;
	}

	.ty-kb__key.is-pressed {
		transform: none;
	}

	.ty-kb__key.is-wrong {
		animation: none;
		outline: 2px solid #d9534f;
	}
}

/* Heatmap after a test: tint by finger accuracy; finger names are listed in the results. */
.ty-kb.is-heatmap .ty-kb__key {
	--ty-f: var(--ty-line);
}

.ty-kb.is-heatmap .ty-kb__key[data-heat="good"] {
	--ty-f: #40c057;
}

.ty-kb.is-heatmap .ty-kb__key[data-heat="ok"] {
	--ty-f: #fab005;
}

.ty-kb.is-heatmap .ty-kb__key[data-heat="weak"] {
	--ty-f: #f06a5f;
}

.ty-kb.is-heatmap .ty-kb__key[data-heat] {
	background: color-mix(in srgb, var(--ty-f) 50%, var(--ty-surface));
}

/* ---------- Results ---------- */
.ty-results {
	padding: clamp(1.25rem, 3.5vw, 2.25rem);
	border: 1px solid var(--ty-line);
	border-radius: var(--ty-radius);
	background: var(--ty-surface);
	box-shadow: var(--ty-shadow);
}

.ty-results__title {
	font-size: 1.45rem;
}

.ty-results__title:focus {
	outline: none;
}

.ty-results__headline {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr));
	gap: 0.75rem;
	margin-bottom: 0.9rem;
}

.ty-stat {
	display: flex;
	flex-direction: column;
	gap: 0.2rem;
	margin: 0;
	padding: 1rem 1.2rem;
	border-radius: 14px;
	background: var(--ty-surface-2);
}

.ty-stat__label {
	color: var(--ty-muted);
	font-size: 0.85rem;
	font-weight: 500;
}

.ty-stat__num {
	font-family: var(--ty-font-mono);
	font-size: clamp(2.2rem, 6vw, 3rem);
	font-weight: 600;
	font-variant-numeric: tabular-nums;
	line-height: 1.05;
}

.ty-stat--acc {
	background: color-mix(in srgb, var(--ty-accent) 12%, var(--ty-surface));
}

.ty-stat--acc .ty-stat__num {
	color: var(--ty-good);
}

.ty-results__explain {
	max-width: 70ch;
	color: var(--ty-muted);
	font-size: 0.92rem;
}

.ty-results__more {
	margin-bottom: 1.25rem;
	padding: 0.75rem 1rem;
	border-radius: 12px;
	background: var(--ty-surface-2);
}

.ty-results__more summary {
	font-weight: 600;
	cursor: pointer;
}

.ty-results__more .ty-results__facts {
	margin: 0.75rem 0 0;
}

.ty-results__facts {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr));
	gap: 0.5rem 1.5rem;
	margin: 0 0 1.5rem;
}

.ty-results__facts dt {
	color: var(--ty-muted);
	font-size: 0.85rem;
}

.ty-results__facts dd {
	margin: 0;
	font-weight: 600;
}

.ty-results__sub {
	font-size: 1.05rem;
}

.ty-results__fingers {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
	gap: 0.4rem 1.5rem;
	margin: 0 0 0.75rem;
	padding: 0;
	list-style: none;
}

.ty-results__finger {
	display: grid;
	grid-template-columns: auto 1fr auto;
	align-items: center;
	gap: 0 0.6rem;
	padding: 0.35rem 0;
	border-bottom: 1px dashed var(--ty-line);
}

.ty-results__dot {
	width: 0.85rem;
	height: 0.85rem;
	border-radius: 50%;
	background: var(--ty-f);
}

.ty-results__pct {
	font-family: var(--ty-font-mono);
	font-weight: 700;
}

.ty-results__level {
	grid-column: 2 / 4;
	color: var(--ty-muted);
	font-size: 0.82rem;
}

.ty-results__finger.is-weak .ty-results__level {
	color: var(--ty-bad);
	font-weight: 600;
}

.ty-results__legend {
	color: var(--ty-muted);
	font-size: 0.88rem;
}

.ty-results__actions {
	display: flex;
	flex-wrap: wrap;
	gap: 0.75rem;
}

/* ---------- No keyboard (touch devices) ---------- */
.ty-nokb {
	padding: 1.5rem;
	border: 1px dashed var(--ty-line);
	border-radius: var(--ty-radius);
	text-align: center;
}

.ty-nokb .ty-actions {
	justify-content: center;
}

/* ---------- Lesson (day pages) ---------- */
.ty-stepbar {
	display: flex;
	flex-wrap: wrap;
	gap: 0.35rem;
	margin: 0 auto 0 0;
	padding: 0;
	list-style: none;
	counter-reset: ty-step;
}

.ty-stepbar li {
	display: inline-flex;
	align-items: center;
	gap: 0.4rem;
	padding: 0.3rem 0.75rem 0.3rem 0.35rem;
	border-radius: 999px;
	background: var(--ty-surface-2);
	color: var(--ty-muted);
	font-size: 0.85rem;
	font-weight: 500;
	counter-increment: ty-step;
}

.ty-stepbar li::before {
	content: counter(ty-step);
	display: inline-grid;
	place-items: center;
	width: 1.45rem;
	height: 1.45rem;
	border-radius: 50%;
	background: var(--ty-surface);
	font-size: 0.75rem;
}

.ty-stepbar li.is-current {
	background: color-mix(in srgb, var(--ty-primary) 12%, var(--ty-surface));
	color: var(--ty-ink);
	font-weight: 600;
}

.ty-stepbar li.is-current::before {
	background: var(--ty-primary);
	color: var(--ty-on-primary);
}

.ty-stepbar li.is-done::before {
	content: "✓";
	background: var(--ty-accent);
	color: #fff;
}

.ty-coachline {
	margin: 0;
	padding: 0.8rem 1.1rem;
	border: 1px solid color-mix(in srgb, var(--ty-accent) 25%, var(--ty-line));
	border-radius: 14px;
	background: color-mix(in srgb, var(--ty-accent) 7%, var(--ty-surface));
	font-size: 1.05rem;
}

.ty-coachline[hidden] {
	display: none;
}

.ty-coachline__who {
	padding: 0.1rem 0.6rem;
	border-radius: 999px;
	background: color-mix(in srgb, var(--ty-f, var(--ty-accent)) 35%, var(--ty-surface));
	font-weight: 600;
}

.ty-stepmeta {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.5rem 1rem;
	min-height: 2.25rem;
	color: var(--ty-muted);
	font-size: 0.92rem;
}

.ty-stepmeta__clock {
	color: var(--ty-ink);
	font-family: var(--ty-font-mono);
	font-size: 1.2rem;
	font-weight: 600;
}

.ty-card-note--rest {
	text-align: center;
}

.ty-rest__count {
	font-family: var(--ty-font-mono);
	font-size: 2.5rem;
	font-weight: 600;
}

.ty-wrap__facts {
	margin: 0 0 1rem;
	padding-left: 1.2rem;
}

.ty-wrap__facts li + li {
	margin-top: 0.3rem;
}

.ty-wrap__encourage {
	font-size: 1.1rem;
	font-weight: 500;
}

/* ---------- Practice: custom text ---------- */
.ty-custom {
	display: grid;
	gap: 0.5rem;
}

.ty-custom[hidden] {
	display: none;
}

.ty-custom__label {
	font-weight: 700;
}

.ty-custom__text {
	min-height: 6rem;
	font-family: var(--ty-font-mono);
}

.ty-custom__hint {
	margin: 0;
	color: var(--ty-muted);
	font-size: 0.88rem;
}

.ty-custom .ty-button {
	justify-self: start;
}

/* ---------- Coach: letters mastered + milestone line ---------- */
.ty-coach {
	display: grid;
	gap: 0.3rem;
	flex: 1 1 260px;
	min-width: 0;
}

.ty-coach__mastered {
	position: relative;
	justify-self: start;
	min-width: 12rem;
	margin: 0;
	padding-bottom: 7px;
	color: var(--ty-muted);
	font-size: 0.85rem;
	font-weight: 500;
}

.ty-coach__mastered::after {
	content: "";
	position: absolute;
	right: 0;
	bottom: 0;
	left: 0;
	height: 4px;
	border-radius: 2px;
	background: linear-gradient(90deg, var(--ty-accent) calc(var(--ty-progress, 0) * 100%), var(--ty-line) 0);
}

/* Always one line tall, so a milestone never moves the typing text. */
.ty-coach__news {
	min-height: 1.5em;
	margin: 0;
	overflow: hidden;
	color: var(--ty-good);
	font-size: 0.92rem;
	font-weight: 600;
	line-height: 1.5;
	white-space: nowrap;
	text-overflow: ellipsis;
}

.ty-coach__news:not(:empty) {
	animation: ty-news-in 0.25s ease;
}

@keyframes ty-news-in {
	from {
		opacity: 0;
		transform: translateY(-3px);
	}
}

@media (prefers-reduced-motion: reduce) {
	.ty-coach__news:not(:empty) {
		animation: none;
	}
}

.ty-messages:empty {
	display: none;
}

.ty-stage-wrap:empty,
.ty-results-wrap:empty,
.ty-stepmeta:empty {
	display: none;
}
