/* On-screen keyboard, used both as a typing hint and as a per-key heatmap. */

.keyboard-host {
  margin-top: 3rem;
  transition: opacity 250ms ease;
}
.keyboard-host[hidden] { display: none; }

.kb {
  display: grid;
  gap: 0.3rem;
  max-width: 780px;
  margin: 0 auto;
  font-family: var(--mono);
  user-select: none;
}

.kb-row { display: flex; gap: 0.3rem; }

.kb-key {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 2.4rem;
  padding: 0.25rem;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  /* Labels always take the theme's primary text colour: the finger tint lives
   * in the key surface and underline, and a muted glyph on a tinted surface
   * is unreadable on every dark theme. */
  color: var(--text);
  font-size: 0.82rem;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease,
              transform 80ms ease;
  overflow: hidden;
}

.kb-main { line-height: 1.1; }
.kb-shift { font-size: 0.62rem; color: var(--sub); line-height: 1; }
.kb-special { font-size: 0.62rem; color: var(--sub); }
.kb-space { flex-grow: 6; }

/* F and J carry the locating bumps. */
.kb-bump::after {
  content: '';
  position: absolute;
  bottom: 0.3rem;
  width: 0.7rem;
  height: 2px;
  border-radius: 2px;
  background: var(--sub-alt);
}

/* Finger colouring — one colour per finger, mirrored: pinky, ring, middle and
 * index wear the same colour on both hands, matching the mirrored key pairs
 * the lessons introduce. On by default, toggled in settings.
 *
 * This block sits above the .is-next/.is-hit/.is-miss states on purpose: equal
 * specificity, so the later state rules win while a key is highlighted. */
.kb-fingers .kb-key[data-finger='lp'],
.kb-fingers .kb-key[data-finger='rp'] { --finger: #e0679b; }
.kb-fingers .kb-key[data-finger='lr'],
.kb-fingers .kb-key[data-finger='rr'] { --finger: #e0a35c; }
.kb-fingers .kb-key[data-finger='lm'],
.kb-fingers .kb-key[data-finger='rm'] { --finger: #d9cf5c; }
.kb-fingers .kb-key[data-finger='li'],
.kb-fingers .kb-key[data-finger='ri'] { --finger: #5cc98a; }

.kb-fingers .kb-key {
  border-bottom: 3px solid var(--finger, var(--sub-alt));
  background: color-mix(in srgb, var(--finger, var(--surface)) 13%, var(--surface));
}

.kb-key.is-next {
  background: var(--main);
  border-color: var(--main);
  color: var(--bg);
  transform: translateY(1px);
}
.kb-key.is-next .kb-shift { color: var(--bg); opacity: 0.7; }

.kb-key.is-shift {
  border-color: var(--main);
  color: var(--main);
}

.kb-key.is-hit {
  /* A clear accent flash: the old surface-on-surface shade was nearly
   * invisible on several themes. */
  background: color-mix(in srgb, var(--main) 45%, var(--surface));
  border-color: var(--main);
  color: var(--text);
  transform: translateY(2px);
}
.kb-key.is-miss {
  background: var(--error);
  border-color: var(--error);
  color: var(--bg);
  transform: translateY(2px);
}

/* Heatmap: --heat runs 0 (clean) to 1 (missed a quarter of the time or worse). */
.kb-key[style*='--heat'] {
  background: color-mix(in srgb, var(--error) calc(var(--heat) * 75%), var(--surface));
  border-color: color-mix(in srgb, var(--error) calc(var(--heat) * 60%), var(--border));
  color: var(--text);
}
.kb-key.is-cold { opacity: 0.4; }

/* Speed map: --speed runs 0 (your fastest key) to 1 (your slowest), so the
 * scale always spans the hand in front of it rather than an absolute wpm that
 * would leave a beginner's board uniformly red. */
.kb-key[style*='--speed'] {
  background: color-mix(in srgb, var(--error) calc(var(--speed) * 75%), var(--surface));
  border-color: color-mix(in srgb, var(--error) calc(var(--speed) * 60%), var(--border));
  color: var(--text);
}

/* The scale legend under the board, so the colours mean something. */
.kb-scale {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 1rem;
  color: var(--sub);
  font-size: 0.8rem;
}
.kb-scale[hidden] { display: none; }
.kb-scale-bar {
  flex: 1;
  max-width: 16rem;
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(to right, var(--surface),
              color-mix(in srgb, var(--error) 75%, var(--surface)));
  border: 1px solid var(--border);
}

@media (max-width: 700px) {
  .kb { gap: 0.2rem; }
  .kb-row { gap: 0.2rem; }
  .kb-key { min-height: 1.9rem; font-size: 0.68rem; padding: 0.15rem; }
  .kb-shift, .kb-special { font-size: 0.5rem; }
  .kb-bump::after { bottom: 0.15rem; width: 0.5rem; }
}

@media (max-width: 460px) {
  .keyboard-host { display: none; }
}
