/* KeyboardTest — on-screen keyboard + readout panels.
   Theme colors come from site.config.json (--primary blue / --accent amber).
   Note: elements that can carry [hidden] keep display rules behind
   :not([hidden]) per repo convention (P015). */

.kb-tool {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 1.1rem 1.25rem 1.35rem;
  margin: 1.4rem 0 2rem;
}

/* ---- status bar ---- */

.kb-status {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.6rem;
  margin-bottom: 0.9rem;
}
.kb-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  padding: 0.55rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.kb-stat-num {
  font-size: 1.25rem;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  color: var(--primary);
}
.kb-stat-lbl {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

/* ---- event readout ---- */

.kb-readout {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.6rem;
  margin-bottom: 0.9rem;
}
.kb-ro:not([hidden]) {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  min-width: 0;
}
.kb-ro-lbl {
  font-size: 0.64rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}
.kb-ro-val {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92rem;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- stuck warning ---- */

.kb-warn:not([hidden]) {
  display: block;
  margin: 0 0 0.9rem;
  padding: 0.6rem 0.85rem;
  border: 1px solid #dc2626;
  border-left-width: 4px;
  border-radius: var(--radius);
  background: #fef2f2;
  color: #991b1b;
  font-size: 0.92rem;
  font-weight: 600;
}

/* ---- controls ---- */

.kb-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}
.kb-btn {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 650;
  padding: 0.42rem 0.95rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, color 0.15s;
}
.kb-btn:hover { border-color: var(--primary); color: var(--primary); }
.kb-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.kb-btn.is-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.kb-reset { margin-left: auto; }
.kb-capture {
  font-size: 0.78rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

/* ---- keyboard board ---- */

.kb-scroll {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding: 0.4rem 0.25rem 0.6rem;
  border-radius: var(--radius);
  outline: none;
}
/* edge fades flag hidden board — JS sets data-can-l/r from scrollLeft so
   only the side(s) with remaining content get the fade (scroll affordance) */
.kb-scroll[data-can-r] {
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 1.4rem), transparent 100%);
  mask-image: linear-gradient(to right, #000 calc(100% - 1.4rem), transparent 100%);
}
.kb-scroll[data-can-l]:not([data-can-r]) {
  -webkit-mask-image: linear-gradient(to right, transparent, #000 1.4rem);
  mask-image: linear-gradient(to right, transparent, #000 1.4rem);
}
.kb-scroll[data-can-l][data-can-r] {
  -webkit-mask-image: linear-gradient(to right, transparent, #000 1.4rem, #000 calc(100% - 1.4rem), transparent);
  mask-image: linear-gradient(to right, transparent, #000 1.4rem, #000 calc(100% - 1.4rem), transparent);
}
.kb-scroll:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
.kb-scroll.is-armed {
  background: color-mix(in srgb, var(--primary) 5%, transparent);
}

.kb {
  display: grid;
  grid-template-columns: repeat(92, 1fr);
  grid-template-rows: var(--kb-key) 0.55rem repeat(5, var(--kb-key));
  gap: 4px;
  min-width: 58rem;
  --kb-key: 2.7rem;
  padding: 0.55rem;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  background: color-mix(in srgb, var(--text) 4%, var(--surface));
  user-select: none;
  -webkit-user-select: none;
}

.kb-key {
  font: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.66rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.08s, border-color 0.08s, transform 0.05s;
  position: relative;
  z-index: 1;
}
.kb-key--char { font-size: 0.82rem; font-weight: 700; }
.kb-key--wide { font-size: 0.64rem; }
.kb-key--space { border-radius: 8px; }
.kb-key:hover { border-color: var(--primary); }

/* pressed right now */
.kb-key.is-down {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  transform: translateY(1px);
}
/* released at least once — latched "it works" state */
.kb-key.is-tested {
  background: color-mix(in srgb, var(--primary) 14%, var(--surface));
  border-color: var(--primary);
  color: var(--primary);
}
.kb-key.is-tested.is-down {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
/* held past the stuck threshold */
.kb-key.is-stuck {
  background: #dc2626;
  border-color: #b91c1c;
  color: #fff;
}

/* ISO enter: L-shape, clipped from a 1.5u×2r box (bottom-left 1 cell notch).
   The <>/# keys that share its bounding box sit above it. */
.kb-key--iso-enter {
  clip-path: polygon(0 0, 100% 0, 100% 100%, 16.67% 100%, 16.67% 50%, 0 50%);
  align-items: flex-start;
  padding-top: 0.45rem;
  z-index: 0;
}
.kb-key--over-iso { z-index: 2; }

.kb-hint {
  margin: 0.6rem 0 0;
  font-size: 0.82rem;
  color: var(--text-dim);
}

/* ---- held + history strips ---- */

.kb-held,
.kb-hist {
  display: flex;
  align-items: baseline;
  gap: 0.45rem;
  flex-wrap: wrap;
  margin-top: 0.7rem;
  font-size: 0.85rem;
}
.kb-held-lbl {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}
.kb-held-empty { color: var(--text-dim); font-size: 0.82rem; }
.kb-held-list,
.kb-hist-list {
  display: inline-flex;
  gap: 0.3rem;
  flex-wrap: wrap;
}
.kb-hist-list { max-height: 3.4em; overflow: hidden; }
.kb-chip {
  display: inline-block;
  padding: 0.12rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  font-size: 0.76rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
.kb-chip--held {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ---- small screens ---- */

@media (max-width: 700px) {
  .kb-tool { padding: 0.9rem 0.75rem 1.1rem; }
  .kb-status { grid-template-columns: repeat(2, 1fr); }
  .kb-readout { grid-template-columns: repeat(3, 1fr); }
  .kb { min-width: 52rem; --kb-key: 2.35rem; gap: 3px; }
  .kb-reset { margin-left: 0; }
}
