/* Typing area: on-screen keyboard (.kb-), typing view (.tv-), passage screen (.passage-),
   drills screen (.drill-). Colors come only from the tokens in style.css. */

/* ---------- Keyboard: chunky rounded keycaps with an ink outline ---------- */

.kb {
  --kb-gap: clamp(2px, 0.6vw, 6px);
  --kb-unit: calc((100% - 14 * var(--kb-gap)) / 15);
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  font-family: var(--font-display);
  user-select: none;
  -webkit-user-select: none;
}
.hide-keyboard .kb:not(.kb-heatmap),
.hide-keyboard .tv-kb { display: none; }

.kb-row {
  display: flex;
  justify-content: center;
  gap: var(--kb-gap);
  margin-bottom: calc(var(--kb-gap) + 2px);
}
.kb-row:last-child { margin-bottom: 0; }

.kb-key {
  --kb-bg: var(--surface-2);
  position: relative;
  flex: 0 0 calc(var(--kb-unit) * var(--u, 1) + (var(--u, 1) - 1) * var(--kb-gap));
  min-width: 0;
  height: clamp(28px, 6.4vw, 48px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 2px solid var(--sb-outline);
  border-radius: clamp(5px, 1.2vw, 10px);
  background: color-mix(in srgb, var(--bad) var(--kb-heat, 0%), var(--kb-bg));
  color: var(--sb-card-ink);
  box-shadow: 0 clamp(2px, 0.45vw, 4px) 0 var(--sb-outline);
  font-size: clamp(0.62rem, 2vw, 0.95rem);
  line-height: 1.05;
  transition: background-color 0.15s, transform 0.12s var(--ease-spring), box-shadow 0.12s;
}
.kb-f1 { --kb-bg: var(--finger-1); }
.kb-f2 { --kb-bg: var(--finger-2); }
.kb-f3 { --kb-bg: var(--finger-3); }
.kb-f4 { --kb-bg: var(--finger-4); }
.kb-thumb { --kb-bg: var(--finger-thumb); }
.kb-mod {
  --kb-bg: var(--surface);
  color: var(--ink-muted);
  font-size: clamp(0.55rem, 1.6vw, 0.78rem);
}

.kb-top {
  font-size: 0.8em;
  color: var(--ink-muted);
}
.kb-main { font-weight: 700; }

.kb-key.kb-next {
  background: var(--sb-primary);
  border-color: var(--sb-outline);
  color: var(--sb-primary-ink);
  transform: translateY(-2px);
  box-shadow: 0 clamp(3px, 0.6vw, 5px) 0 var(--sb-outline), 0 0 0 clamp(2px, 0.5vw, 4px) var(--sb-glow-color);
  z-index: 1;
}
.kb-key.kb-next .kb-top { color: var(--sb-primary-ink); }

.kb-key.kb-flash-good { animation: kb-good 0.22s ease-out; }
.kb-key.kb-flash-bad { animation: kb-bad 0.26s ease-out; }
@keyframes kb-good {
  from { background-color: color-mix(in srgb, var(--good) 70%, var(--kb-bg)); transform: translateY(2px); box-shadow: 0 0 0 var(--sb-outline); }
  to { transform: none; }
}
@keyframes kb-bad {
  from { background-color: color-mix(in srgb, var(--bad) 80%, var(--kb-bg)); transform: translateY(2px); box-shadow: 0 0 0 var(--sb-outline); }
  to { transform: none; }
}

.kb-heatmap .kb-key { cursor: default; }

@media (max-width: 520px) {
  .kb-top { display: none; }
  .kb-key { border-width: 1.5px; }
}

/* ---------- Typing view ---------- */

.tv {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.tv-countdown {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(3.6rem, 14vw, 6rem);
  line-height: 1;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: var(--sb-sun);
  paint-order: stroke fill;
  -webkit-text-stroke: 12px var(--sb-outline);
  text-shadow: 0 5px 0 var(--sb-outline);
  transition: color 0.3s;
}
.tv-countdown.is-low { color: var(--sb-danger); }
.tv-countdown-note {
  text-align: center;
  margin: -4px 0 0;
  font-size: 1rem;
  font-weight: 600;
  min-height: 1.5em;
}
.tv.is-running .tv-countdown-note,
.tv.is-finished .tv-countdown-note { visibility: hidden; }

/* stats as sticker chips */
.tv-stats {
  display: flex;
  gap: 10px 12px;
  align-items: center;
  flex-wrap: wrap;
}
.tv-timed .tv-stats { justify-content: center; }
.tv-stat {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 6px 16px;
  border: var(--sb-outline-w) solid var(--sb-outline);
  border-radius: 999px;
  background: var(--sb-chip);
  color: var(--sb-chip-ink);
  box-shadow: 0 3px 0 var(--sb-outline);
}
.tv-stat-label {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--sb-card-muted);
}
.tv-stat-value {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.3rem;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  display: inline-block;
  min-width: 4ch;
  text-align: right;
}

/* the progress bar: a slim sticker with a glossy fill */
.tv-bar {
  height: 16px;
  border: 3px solid var(--sb-outline);
  border-radius: 999px;
  background: var(--sb-card);
  box-shadow: 0 3px 0 var(--sb-outline);
  overflow: hidden;
  margin-top: -2px;
}
.tv-bar-fill {
  height: 100%;
  background:
    linear-gradient(180deg, rgb(255 255 255 / 0.5) 0 36%, transparent 36% 100%),
    var(--sb-primary);
  border-radius: 999px;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.25s linear;
}

/* The text box (an .sb-card look) has a fixed three-line height so nothing shifts while typing. */
.tv-box {
  position: relative;
  background: var(--sb-card);
  border: var(--sb-outline-w) solid var(--sb-outline);
  border-radius: var(--radius);
  padding: 20px 24px;
  box-shadow: inset 0 -5px 0 var(--sb-card-2), var(--sb-pop);
  cursor: text;
}
.tv-text {
  position: relative;
  max-width: 65ch;
  margin: 0 auto;
  height: calc(1.95em * 3);
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: 1.35rem;
  line-height: 1.95;
  scroll-behavior: smooth;
  overflow-wrap: normal;
  word-break: normal;
  -webkit-user-select: none;
  user-select: none;
}
.reduced-motion .tv-text { scroll-behavior: auto; }

.tv-word {
  display: inline-block;
  white-space: nowrap;
  vertical-align: top;
}
.tv-char {
  position: relative;
  color: var(--pending);
  border-radius: 3px;
}
.tv-char.is-correct { color: var(--sb-card-ink); }
.tv-char.is-wrong {
  color: var(--bad);
  background: color-mix(in srgb, var(--bad) 16%, transparent);
}
/* stopOnError: the waiting character shakes after a wrong key (inline text: shake with `left`) */
.tv-char.is-miss, .tv-char.is-current.is-miss { position: relative; color: var(--bad); animation: tv-miss 0.28s ease; }
@keyframes tv-miss { 0%, 100% { left: 0; } 25% { left: -2px; } 75% { left: 2px; } }
@media (prefers-reduced-motion: reduce) { .tv-char.is-miss { animation: none; } }
.reduced-motion .tv-char.is-miss { animation: none; }
.tv-sp {
  display: inline-block;
  width: 1ch;
  white-space: pre;
  vertical-align: top;
}
/* A space typed wrongly (or skipped over) shows a visible dot. */
.tv-sp.is-wrong::after {
  content: '\00b7';
  position: absolute;
  inset: 0;
  text-align: center;
  color: var(--bad);
  font-weight: 700;
}

/* the next letter sits on a sunny highlight, with a chunky caret */
.tv-char.is-current { background: color-mix(in srgb, var(--sb-glow-color) 55%, transparent); color: var(--sb-card-ink); }
.tv-char.is-current::before {
  content: '';
  position: absolute;
  left: -1px;
  top: 10%;
  bottom: 10%;
  width: 3px;
  border-radius: 2px;
  background: var(--sb-outline);
  animation: tv-blink 1.1s steps(1, end) infinite;
}
.tv.is-typing .tv-char.is-current::before { animation: none; }
@keyframes tv-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.tv-focus-hint {
  display: none;
  position: absolute;
  inset: 0;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--sb-card) 80%, transparent);
  color: var(--sb-card-ink);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.1rem;
  pointer-events: none;
}
@media (pointer: coarse) {
  .tv:not(.is-focused):not(.is-finished) .tv-focus-hint { display: flex; }
}

.tv.is-finished .tv-box { opacity: 0.85; }

.tv-kb { min-height: 0; padding-bottom: 4px; }
.tv-kb .kb { margin-top: 2px; }

.tv-hints {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  min-height: 40px;
  font-size: 0.95rem;
  font-weight: 600;
}
.tv-hints kbd {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.9em;
  padding: 1px 9px 2px;
  border: 2px solid var(--sb-outline);
  border-radius: 8px;
  background: var(--sb-chip);
  color: var(--sb-chip-ink);
  box-shadow: 0 2px 0 var(--sb-outline);
}
.tv-actions { display: none; gap: 8px; }
@media (pointer: coarse) {
  .tv-hint-keys { display: none; }
  .tv-actions { display: flex; }
}

@media (max-width: 640px) {
  .tv-box { padding: 14px 12px; }
  .tv-text { font-size: 1.1rem; }
  .tv-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
  .tv-timed .tv-stats { justify-items: center; }
  .tv-stat { flex-direction: column; justify-content: center; gap: 0; padding: 4px 6px 5px; min-width: 0; border-radius: 18px; }
  .tv-stat-label { font-size: 0.85rem; line-height: 1.1; }
  .tv-stat-value { min-width: 0; text-align: center; font-size: 1.2rem; }
}

/* ---------- Passage screen ---------- */

.passage { display: flex; flex-direction: column; gap: 16px; }
.passage-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 4px 14px 4px 10px;
  border: 2px solid var(--sb-outline);
  border-radius: 999px;
  background: var(--sb-chip);
  color: var(--sb-chip-ink);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.95rem;
  text-decoration: none;
  box-shadow: 0 2px 0 var(--sb-outline);
  transition: transform 0.15s var(--ease-spring);
}
.passage-back:hover { transform: translateY(-1px); }
.passage-back .sb-icon { width: 1.2em; height: 1.2em; }
.passage-head .eyebrow { margin: 0 0 6px; }
.passage-title { margin: 0; font-size: clamp(1.7rem, 4.5vw, 2.5rem); }
.passage-chapter { margin: 4px 0 0; font-size: 1.1rem; font-weight: 700; }

/* ---------- Drills ---------- */

.drill { display: flex; flex-direction: column; gap: 22px; }
.drill-head h1 { margin-bottom: 0; }
.drill-head p { margin: 8px 0 0; }
.drill-section h2 { margin-bottom: 0.4em; }
.drill-top { grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); gap: 18px; }

.drill-card { position: relative; display: flex; flex-direction: column; gap: 8px; padding-top: 22px; }
.drill-card h3 { margin-bottom: 0; }
.drill-card p { margin: 0; }
.drill-card .btn { margin-top: auto; align-self: flex-start; }
.drill-card > .btn { margin-top: auto; }
.drill-card > p:nth-last-of-type(1) { margin-bottom: 12px; }
.drill-badge {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: var(--sb-outline-w) solid var(--sb-outline);
  border-radius: 50%;
  background: var(--sb-sun);
  color: var(--sb-outline);
  box-shadow: var(--sb-pop);
  transform: rotate(-6deg);
}
.drill-badge-2 { background: var(--sb-pink); transform: rotate(5deg); }
.drill-badge .sb-icon { width: 26px; height: 26px; }
.drill-last { font-size: 1.05rem; }
.drill-note { font-size: 0.95rem; }
.drill-keys { display: flex; flex-wrap: wrap; gap: 6px; }
.drill-keys kbd {
  min-width: 1.9em;
  padding: 1px 8px 2px;
  text-align: center;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 1rem;
  color: var(--sb-card-ink);
  background: var(--sb-card);
  border: 2px solid var(--sb-outline);
  border-radius: 8px;
  box-shadow: 0 2px 0 var(--sb-outline);
}
.drill-lessons { grid-template-columns: repeat(auto-fill, minmax(min(100%, 250px), 1fr)); gap: 18px; padding-top: 4px; }
.drill-lesson { box-shadow: inset 0 8px 0 var(--sb-sky), var(--sb-pop); padding-top: 28px; }
.drill-lesson:nth-child(4n+2) { box-shadow: inset 0 8px 0 var(--sb-pink), var(--sb-pop); }
.drill-lesson:nth-child(4n+3) { box-shadow: inset 0 8px 0 var(--sb-kelp), var(--sb-pop); }
.drill-lesson:nth-child(4n+4) { box-shadow: inset 0 8px 0 var(--sb-sun), var(--sb-pop); }

.drill-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  list-style: none;
  margin: 2px 0 4px;
  padding: 0;
}
.drill-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  padding: 3px 12px;
  border: 2px solid var(--sb-outline);
  border-radius: 999px;
  background: var(--sb-chip);
  color: var(--sb-chip-ink);
  box-shadow: 0 2px 0 var(--sb-outline);
  font-family: var(--font-mono);
}
.drill-chip-key { font-weight: 700; }
.drill-chip-rate { color: var(--bad); font-size: 0.9em; font-weight: 700; font-variant-numeric: tabular-nums; }

.drill-heat { padding: 18px 12px 20px; background: var(--sb-card); border: var(--sb-outline-w) solid var(--sb-outline); border-radius: var(--radius); box-shadow: var(--sb-pop); }
