/*
 * Letter counter page.
 *
 * Reads from the theme's design tokens, so it follows the Customizer palette
 * without a second set of rules. Grid and flex children carry min-width: 0
 * throughout; they default to min-width: auto and refuse to shrink below their
 * content, which is what caused horizontal overflow elsewhere in this theme.
 */

.site-main.cw-letter-page {
	grid-template-columns: 1fr;
	gap: 0;
}

/* ------------------------------------------------------------------ *
 * Header
 * ------------------------------------------------------------------ */

.cw-letter-header { text-align: center; padding-block: 28px 20px; min-width: 0; }
.cw-letter-title { font-size: clamp(1.75rem, 4vw, 2.4rem); line-height: 1.15; margin: 0 0 12px; }
.cw-letter-lede { max-width: 68ch; margin-inline: auto; color: var(--cw-color-text); }

/* ------------------------------------------------------------------ *
 * The tool
 * ------------------------------------------------------------------ */

.cw-letter-app {
	background: var(--cw-color-card);
	border: 1px solid var(--cw-color-border);
	border-radius: var(--cw-radius);
	box-shadow: var(--cw-shadow);
	padding: 16px;
	min-width: 0;
}

/* The search row sits above the text, clear of the mobile keyboard. */
.cw-letter-search {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 10px 14px;
	min-width: 0;
}
.cw-letter-search-label {
	font-weight: 600;
	font-size: 0.92rem;
	color: var(--cw-color-heading);
	flex: 0 0 auto;
}
.cw-letter-input-target {
	flex: 1 1 240px;
	min-width: 0;
	min-height: var(--cw-tap);
	padding: 9px 12px;
	border: 1px solid var(--cw-color-border);
	border-radius: var(--cw-radius-sm);
	background: var(--cw-color-bg);
	color: var(--cw-color-heading);
	font: inherit;
	font-family: var(--cw-font-mono);
	font-size: 0.95rem;
}
.cw-letter-input-target:focus-visible {
	outline: 2px solid var(--cw-color-primary);
	outline-offset: 1px;
	border-color: var(--cw-color-primary);
}

.cw-letter-options {
	display: flex;
	flex-wrap: wrap;
	gap: 6px 16px;
	flex: 0 0 auto;
	min-width: 0;
}
.cw-letter-options label {
	display: inline-flex;
	align-items: center;
	gap: 7px;
	min-height: var(--cw-tap);
	font-size: 0.87rem;
	color: var(--cw-color-text);
	cursor: pointer;
}

.cw-letter-hint {
	margin: 8px 0 12px;
	font-size: 0.83rem;
	color: var(--cw-color-muted);
}

/* ------------------------------------------------------------------ *
 * Results per target
 * ------------------------------------------------------------------ */

.cw-letter-results {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	margin-bottom: 12px;
	min-width: 0;
}
.cw-letter-results[hidden] { display: none; }

.cw-letter-result {
	display: inline-flex;
	align-items: baseline;
	gap: 8px;
	min-width: 0;
	padding: 8px 12px;
	background: var(--cw-color-bg);
	border: 1px solid var(--cw-color-border);
	border-inline-start: 3px solid var(--cw-color-primary);
	border-radius: var(--cw-radius-sm);
}
/* A target with no matches is still listed, so "none" is an answer rather
   than a missing row, but it reads as quieter. */
.cw-letter-result.is-zero { border-inline-start-color: var(--cw-color-border); opacity: 0.75; }

.cw-letter-result-target {
	font-family: var(--cw-font-mono);
	font-weight: 700;
	color: var(--cw-color-heading);
	overflow-wrap: anywhere;
}
.cw-letter-result-count {
	font-size: 1.15rem;
	font-weight: 700;
	color: var(--cw-color-primary);
	font-variant-numeric: tabular-nums;
}
.cw-letter-result.is-zero .cw-letter-result-count { color: var(--cw-color-muted); }
.cw-letter-result-pct { font-size: 0.8rem; color: var(--cw-color-muted); }

/* ------------------------------------------------------------------ *
 * Overlay editor
 *
 * The textarea and the mirror MUST share identical metrics or the highlight
 * drifts away from the text it marks. Everything affecting line breaking is
 * set once, here, for both layers.
 * ------------------------------------------------------------------ */

.cw-letter-editor { position: relative; min-width: 0; }

.cw-letter-text,
.cw-letter-mirror {
	font-family: var(--cw-font-body);
	font-size: 1rem;
	line-height: 1.6;
	letter-spacing: normal;
	padding: 14px;
	border: 1px solid transparent;
	white-space: pre-wrap;
	overflow-wrap: break-word;
	word-break: normal;
	tab-size: 4;
}

.cw-letter-text {
	position: relative;
	z-index: 2;
	display: block;
	width: 100%;
	min-width: 0;
	min-height: 200px;
	border-color: var(--cw-color-border);
	border-radius: var(--cw-radius-sm);
	background: var(--cw-color-editor-bg);
	color: var(--cw-color-editor-text);
	resize: vertical;
	overflow: auto;
}
/* Transparent only while a highlight is showing beneath it. */
.cw-letter-text.is-overlaid { background: transparent; }
.cw-letter-text:focus-visible {
	outline: 2px solid var(--cw-color-primary);
	outline-offset: 1px;
	border-color: var(--cw-color-primary);
}

.cw-letter-backdrop {
	position: absolute;
	inset: 0;
	z-index: 1;
	overflow: hidden;
	border-radius: var(--cw-radius-sm);
	background: var(--cw-color-editor-bg);
	pointer-events: none;
}
.cw-letter-mirror { color: transparent; min-height: 100%; }
.cw-letter-mirror mark {
	/*
	 * Explicit tokens rather than a color-mix derivation: this highlight is
	 * the whole point of the tool, so it must not depend on a function some
	 * browsers lack. The text itself stays transparent because the real text
	 * is painted by the textarea on top.
	 */
	background: var(--cw-color-match);
	color: transparent;
	border-radius: 2px;
	box-shadow: 0 0 0 1px var(--cw-color-match-edge);
}

.cw-letter-toolrow {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	margin-top: 10px;
	min-width: 0;
}
.cw-letter-toolrow .cw-btn { gap: 6px; }
.cw-letter-clear:hover,
.cw-letter-clear:focus-visible { color: var(--cw-color-danger); border-color: var(--cw-color-danger); }

/* ------------------------------------------------------------------ *
 * Breakdown figures
 * ------------------------------------------------------------------ */

.cw-letter-figures {
	display: grid;
	grid-template-columns: repeat(6, minmax(0, 1fr));
	gap: 10px;
	margin-top: 14px;
}
.cw-letter-figure {
	min-width: 0;
	padding: 12px 8px;
	text-align: center;
	background: var(--cw-color-bg);
	border: 1px solid var(--cw-color-border);
	border-radius: var(--cw-radius-sm);
}
.cw-letter-figure strong {
	display: block;
	font-size: 1.3rem;
	line-height: 1.1;
	color: var(--cw-color-heading);
	font-variant-numeric: tabular-nums;
}
.cw-letter-figure span {
	display: block;
	margin-top: 3px;
	font-size: 0.74rem;
	color: var(--cw-color-muted);
}
.cw-letter-figure.is-primary { border-color: var(--cw-color-primary); background: var(--cw-color-card); }
.cw-letter-figure.is-primary strong { color: var(--cw-color-primary); font-size: 1.6rem; }

/* ------------------------------------------------------------------ *
 * Frequency table
 * ------------------------------------------------------------------ */

.cw-letter-freq { margin-top: 18px; min-width: 0; }
.cw-letter-freq[hidden] { display: none; }

.cw-letter-freq-head {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
	gap: 10px;
	margin-bottom: 10px;
	min-width: 0;
}
.cw-letter-freq-title { margin: 0; font-size: 1.05rem; }
.cw-letter-freq-scope select {
	min-height: 38px;
	padding: 6px 10px;
	border: 1px solid var(--cw-color-border);
	border-radius: var(--cw-radius-sm);
	background: var(--cw-color-bg);
	color: var(--cw-color-heading);
	font: inherit;
	font-size: 0.86rem;
}

.cw-letter-freq-list { list-style: none; margin: 0; padding: 0; }
.cw-letter-freq-row {
	display: grid;
	grid-template-columns: 2.2rem minmax(0, 1fr) 3.5rem 3.5rem;
	align-items: center;
	gap: 10px;
	padding: 4px 0;
	min-width: 0;
}
.cw-letter-freq-char {
	font-family: var(--cw-font-mono);
	font-weight: 700;
	font-size: 0.95rem;
	color: var(--cw-color-heading);
	text-align: center;
	overflow-wrap: anywhere;
}
.cw-letter-freq-bar {
	position: relative;
	height: 14px;
	min-width: 0;
	background: var(--cw-color-bg);
	border-radius: 999px;
	overflow: visible;
}
.cw-letter-freq-fill {
	display: block;
	height: 100%;
	background: var(--cw-color-primary);
	border-radius: 999px;
}
/* Where English would put this letter, on the same scale as the bar. */
.cw-letter-expected {
	position: absolute;
	top: -2px;
	width: 2px;
	height: 18px;
	background: var(--cw-color-heading);
	opacity: 0.45;
	border-radius: 1px;
}
.cw-letter-freq-count,
.cw-letter-freq-pct {
	font-size: 0.84rem;
	text-align: end;
	font-variant-numeric: tabular-nums;
	min-width: 0;
}
.cw-letter-freq-count { color: var(--cw-color-heading); font-weight: 600; }
.cw-letter-freq-pct { color: var(--cw-color-muted); }

.cw-letter-freq-note {
	margin: 10px 0 0;
	font-size: 0.82rem;
	color: var(--cw-color-muted);
}

.cw-letter-status {
	margin: 10px 0 0;
	min-height: 1.4em;
	font-size: 0.88rem;
	color: var(--cw-color-success);
}
.cw-letter-privacy {
	display: flex;
	align-items: center;
	gap: 7px;
	margin: 6px 0 0;
	font-size: 0.85rem;
	color: var(--cw-color-muted);
}
.cw-letter-privacy svg { flex: 0 0 auto; }

/* ------------------------------------------------------------------ *
 * Explanatory sections
 * ------------------------------------------------------------------ */

.cw-letter-grid {
	display: grid;
	grid-template-columns: repeat(3, minmax(0, 1fr));
	gap: 18px 26px;
}
.cw-letter-feature { min-width: 0; }
.cw-letter-feature h3 { font-size: 1rem; margin: 0 0 6px; color: var(--cw-color-primary); }
.cw-letter-feature p { margin: 0; color: var(--cw-color-text); font-size: 0.93rem; }

/* ------------------------------------------------------------------ *
 * English frequency reference
 * ------------------------------------------------------------------ */

.cw-letter-ref-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
	gap: 10px;
}
.cw-letter-ref {
	display: flex;
	align-items: center;
	gap: 8px;
	min-width: 0;
	padding: 10px 12px;
	background: var(--cw-color-card);
	border: 1px solid var(--cw-color-border);
	border-radius: var(--cw-radius-sm);
}
.cw-letter-ref-rank {
	font-size: 0.72rem;
	color: var(--cw-color-muted);
	font-variant-numeric: tabular-nums;
	flex: 0 0 auto;
}
.cw-letter-ref-char {
	font-family: var(--cw-font-mono);
	font-weight: 700;
	font-size: 1.05rem;
	color: var(--cw-color-primary);
	flex: 0 0 auto;
}
.cw-letter-ref-pct {
	margin-inline-start: auto;
	font-size: 0.8rem;
	color: var(--cw-color-text);
	font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------------------ *
 * FAQ
 * ------------------------------------------------------------------ */

.cw-letter-faq-item { border-bottom: 1px solid var(--cw-color-border); }
.cw-letter-faq-item summary {
	cursor: pointer;
	list-style: none;
	padding: 14px 34px 14px 0;
	position: relative;
	min-height: var(--cw-tap);
	display: flex;
	align-items: center;
}
.cw-letter-faq-item summary::-webkit-details-marker { display: none; }
.cw-letter-faq-item summary h3 {
	margin: 0;
	font-size: 1.02rem;
	font-weight: 600;
	color: var(--cw-color-heading);
}
.cw-letter-faq-item summary::after {
	content: "";
	position: absolute;
	inset-inline-end: 6px;
	top: 50%;
	width: 10px; height: 10px;
	margin-top: -6px;
	border-inline-end: 2px solid var(--cw-color-muted);
	border-block-end: 2px solid var(--cw-color-muted);
	transform: rotate(45deg);
	transition: transform 0.18s ease;
}
.cw-letter-faq-item[open] summary::after { transform: rotate(-135deg); margin-top: -2px; }
.cw-letter-faq-item p {
	margin: 0 0 16px;
	padding-inline-end: 34px;
	color: var(--cw-color-text);
}
.cw-letter-faq-item summary:focus-visible {
	outline: 2px solid var(--cw-color-primary);
	outline-offset: 2px;
	border-radius: var(--cw-radius-sm);
}

@media (prefers-reduced-motion: reduce) {
	.cw-letter-faq-item summary::after { transition: none; }
}

/* ------------------------------------------------------------------ *
 * Related
 * ------------------------------------------------------------------ */

.cw-letter-related {
	background: var(--cw-color-card);
	border: 1px solid var(--cw-color-border);
	border-radius: var(--cw-radius);
	padding: 24px 20px;
}
.cw-letter-related h2 { margin-top: 0; font-size: 1.2rem; }
.cw-letter-related p { margin: 0; color: var(--cw-color-text); }

/* ------------------------------------------------------------------ *
 * Responsive
 * ------------------------------------------------------------------ */

@media (max-width: 1023px) {
	.cw-letter-figures { grid-template-columns: repeat(3, minmax(0, 1fr)); }
	.cw-letter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
	.cw-letter-grid { grid-template-columns: minmax(0, 1fr); }
	.cw-letter-search-label { width: 100%; }
	.cw-letter-input-target { flex: 1 1 100%; }
	.cw-letter-options { width: 100%; }
	.cw-letter-toolrow .cw-btn { flex: 1 1 auto; justify-content: center; }
}

@media (max-width: 600px) {
	.cw-letter-app { padding: 12px; }
	.cw-letter-header { padding-block: 18px 14px; }
	.cw-letter-title { margin-bottom: 8px; }
	.cw-letter-lede { font-size: 0.95rem; }

	.cw-letter-text { min-height: 170px; }
	.cw-letter-text,
	.cw-letter-mirror { font-size: 0.95rem; padding: 12px; }

	/* Three across leaves about 100px per figure, enough for the count and
	   its label. Two would waste a row. */
	.cw-letter-figures { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
	.cw-letter-figure { padding: 10px 4px; }
	.cw-letter-figure strong { font-size: 1.1rem; }
	.cw-letter-figure.is-primary strong { font-size: 1.3rem; }
	.cw-letter-figure span { font-size: 0.68rem; }

	/* The percentage column is the first thing to go: the bar already shows
	   proportion, so the number is the redundant one. */
	.cw-letter-freq-row { grid-template-columns: 1.9rem minmax(0, 1fr) 3rem; }
	.cw-letter-freq-pct { display: none; }

	.cw-letter-ref-grid { grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); }
}
