/* =============================================================================
   ShortcutSchool — Virtual keyboard
   Morphing QWERTY ↔ AZERTY · Windows ↔ Mac · glow · tooltips
   ============================================================================= */

.keyboard-wrap {
  --kb-gap: 6px;
  --kb-key-h: 48px;
  --kb-key-min: 42px;
  --kb-radius: 8px;
  --kb-font: 0.78rem;
  --kb-transition: 0.35s cubic-bezier(0.22, 1, 0.36, 1);

  width: 100%;
  max-width: 920px;
  margin-inline: auto;
  user-select: none;
  -webkit-user-select: none;
}

.keyboard {
  display: flex;
  flex-direction: column;
  gap: var(--kb-gap);
  padding: 1rem 1.1rem 1.15rem;
  background:
    linear-gradient(180deg, rgba(30, 41, 59, 0.55) 0%, rgba(15, 23, 42, 0.85) 100%);
  border: 1px solid var(--border-strong, rgba(148, 163, 184, 0.22));
  border-radius: calc(var(--radius, 16px) + 4px);
  box-shadow:
    0 16px 40px rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.05);
  position: relative;
  overflow: visible;
}

/* Subtle morph flash when OS/layout changes */
.keyboard.is-morphing {
  animation: kb-morph 0.45s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes kb-morph {
  0%   { filter: brightness(1); transform: scale(1); }
  40%  { filter: brightness(1.12); transform: scale(1.008); }
  100% { filter: brightness(1); transform: scale(1); }
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: var(--kb-gap);
  flex-wrap: nowrap;
  min-height: var(--kb-key-h);
}

/* ---------------------------------------------------------------------------
   Keys
   --------------------------------------------------------------------------- */
.kb-key {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: var(--kb-key-min);
  height: var(--kb-key-h);
  padding: 0 0.35rem;
  flex: 0 0 auto;

  font-family: var(--mono, "JetBrains Mono", ui-monospace, monospace);
  font-size: var(--kb-font);
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.1;
  color: var(--text-muted, #94a3b8);
  text-align: center;

  background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: var(--kb-radius);
  box-shadow:
    0 3px 0 #020617,
    inset 0 1px 0 rgba(255, 255, 255, 0.06);

  cursor: default;
  transition:
    color var(--kb-transition),
    background var(--kb-transition),
    border-color var(--kb-transition),
    box-shadow var(--kb-transition),
    transform 0.15s cubic-bezier(0.22, 1, 0.36, 1),
    min-width var(--kb-transition),
    flex-basis var(--kb-transition),
    opacity var(--kb-transition);
}

.kb-key .kb-label {
  display: block;
  transition: opacity 0.2s ease, transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
}

.kb-key .kb-sub {
  display: block;
  font-size: 0.62em;
  font-weight: 500;
  opacity: 0.55;
  margin-top: 1px;
  transition: opacity 0.2s ease;
}

/* Width variants */
.kb-key.w-1-25 { min-width: calc(var(--kb-key-min) * 1.25); }
.kb-key.w-1-5  { min-width: calc(var(--kb-key-min) * 1.5); }
.kb-key.w-1-75 { min-width: calc(var(--kb-key-min) * 1.75); }
.kb-key.w-2    { min-width: calc(var(--kb-key-min) * 2); }
.kb-key.w-2-25 { min-width: calc(var(--kb-key-min) * 2.25); }
.kb-key.w-2-75 { min-width: calc(var(--kb-key-min) * 2.75); }
.kb-key.w-space {
  flex: 1 1 auto;
  min-width: calc(var(--kb-key-min) * 6);
  max-width: 360px;
}

/* Pressed (physical or virtual) */
.kb-key.is-pressed {
  transform: translateY(2px);
  box-shadow:
    0 1px 0 #020617,
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  color: var(--text, #e8eefc);
  border-color: rgba(34, 211, 238, 0.35);
}

/* Used in current profile (any shortcut) */
.kb-key.is-active {
  color: #041016;
  background: linear-gradient(180deg, #67e8f9 0%, #22d3ee 100%);
  border-color: #a5f3fc;
  box-shadow:
    0 0 16px rgba(34, 211, 238, 0.5),
    0 2px 0 #0891b2,
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
  animation: kb-glow-pulse 1.8s ease-in-out infinite;
}

.kb-key.is-active .kb-sub { opacity: 0.7; color: #0e7490; }

/* Highlighted as target for current prompt */
.kb-key.is-target {
  color: #0a1405;
  background: linear-gradient(180deg, #bef264 0%, #a3e635 100%);
  border-color: #d9f99d;
  box-shadow:
    0 0 20px rgba(163, 230, 53, 0.55),
    0 2px 0 #65a30d,
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
  animation: kb-glow-pulse 1.2s ease-in-out infinite;
  z-index: 1;
}

/* Correct flash */
.kb-key.is-correct {
  animation: kb-flash-green 0.55s cubic-bezier(0.22, 1, 0.36, 1) !important;
  color: #052e16 !important;
  background: linear-gradient(180deg, #86efac 0%, #4ade80 100%) !important;
  border-color: #bbf7d0 !important;
  box-shadow: 0 0 28px rgba(74, 222, 128, 0.7), 0 2px 0 #16a34a !important;
}

/* Wrong flash */
.kb-key.is-wrong {
  animation: kb-flash-red 0.45s cubic-bezier(0.22, 1, 0.36, 1) !important;
  color: #450a0a !important;
  background: linear-gradient(180deg, #fca5a5 0%, #f87171 100%) !important;
  border-color: #fecaca !important;
  box-shadow: 0 0 24px rgba(248, 113, 113, 0.65), 0 2px 0 #dc2626 !important;
}

@keyframes kb-glow-pulse {
  0%, 100% {
    filter: brightness(1);
    box-shadow:
      0 0 12px rgba(34, 211, 238, 0.4),
      0 2px 0 #0891b2,
      inset 0 1px 0 rgba(255, 255, 255, 0.35);
  }
  50% {
    filter: brightness(1.08);
    box-shadow:
      0 0 28px rgba(34, 211, 238, 0.75),
      0 2px 0 #0891b2,
      inset 0 1px 0 rgba(255, 255, 255, 0.45);
  }
}

.kb-key.is-target {
  animation-name: kb-target-pulse;
}

@keyframes kb-target-pulse {
  0%, 100% {
    filter: brightness(1);
    box-shadow:
      0 0 14px rgba(163, 230, 53, 0.45),
      0 2px 0 #65a30d,
      inset 0 1px 0 rgba(255, 255, 255, 0.4);
  }
  50% {
    filter: brightness(1.1);
    box-shadow:
      0 0 30px rgba(163, 230, 53, 0.8),
      0 2px 0 #65a30d,
      inset 0 1px 0 rgba(255, 255, 255, 0.5);
  }
}

@keyframes kb-flash-green {
  0%   { transform: scale(1); }
  30%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}

@keyframes kb-flash-red {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-3px); }
  40% { transform: translateX(3px); }
  60% { transform: translateX(-2px); }
  80% { transform: translateX(2px); }
}

/* Hover only on interactive / active keys */
.kb-key.is-active:hover,
.kb-key.is-target:hover,
.kb-key.has-tooltip:hover {
  z-index: 5;
  transform: translateY(-2px) scale(1.04);
}

/* ---------------------------------------------------------------------------
   Tooltip
   --------------------------------------------------------------------------- */
.kb-tooltip {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 10px);
  transform: translateX(-50%) translateY(4px) scale(0.96);
  min-width: 160px;
  max-width: 260px;
  padding: 0.55rem 0.7rem;
  border-radius: 10px;
  background: #0f172a;
  border: 1px solid rgba(34, 211, 238, 0.35);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45), 0 0 20px rgba(34, 211, 238, 0.15);
  color: var(--text, #e8eefc);
  font-family: var(--font, "Outfit", system-ui, sans-serif);
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1.35;
  text-align: left;
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.2s cubic-bezier(0.22, 1, 0.36, 1),
    transform 0.2s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 20;
  white-space: normal;
}

.kb-tooltip::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: rgba(34, 211, 238, 0.35);
}

.kb-key:hover .kb-tooltip,
.kb-key:focus-visible .kb-tooltip,
.kb-tooltip.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0) scale(1);
}

.kb-tooltip-title {
  display: block;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--cyan, #22d3ee);
  margin-bottom: 0.35rem;
}

.kb-tooltip-item {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  padding: 0.3rem 0;
  border-top: 1px solid rgba(148, 163, 184, 0.12);
}

.kb-tooltip-item:first-of-type { border-top: none; padding-top: 0; }

.kb-tooltip-action {
  font-weight: 600;
  color: var(--text, #e8eefc);
}

.kb-tooltip-combo {
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 0.7rem;
  color: var(--lime, #a3e635);
}

/* Flip tooltip downward for top row keys to avoid clipping */
.keyboard-row:first-child .kb-tooltip {
  bottom: auto;
  top: calc(100% + 10px);
}

.keyboard-row:first-child .kb-tooltip::after {
  top: auto;
  bottom: 100%;
  border-top-color: transparent;
  border-bottom-color: rgba(34, 211, 238, 0.35);
}

/* ---------------------------------------------------------------------------
   Legend under keyboard
   --------------------------------------------------------------------------- */
.keyboard-legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.75rem 1.25rem;
  margin-top: 0.85rem;
  font-size: 0.8rem;
  color: var(--text-muted, #94a3b8);
}

.keyboard-legend span {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 1px solid transparent;
}

.legend-swatch.active {
  background: linear-gradient(180deg, #67e8f9, #22d3ee);
  box-shadow: 0 0 8px rgba(34, 211, 238, 0.5);
}

.legend-swatch.target {
  background: linear-gradient(180deg, #bef264, #a3e635);
  box-shadow: 0 0 8px rgba(163, 230, 53, 0.5);
}

/* ---------------------------------------------------------------------------
   Responsive
   --------------------------------------------------------------------------- */
@media (max-width: 900px) {
  .keyboard-wrap {
    --kb-key-h: 42px;
    --kb-key-min: 34px;
    --kb-gap: 4px;
    --kb-font: 0.68rem;
  }
}

@media (max-width: 640px) {
  .keyboard-wrap {
    --kb-key-h: 36px;
    --kb-key-min: 26px;
    --kb-gap: 3px;
    --kb-font: 0.58rem;
    --kb-radius: 6px;
  }

  .keyboard {
    padding: 0.65rem 0.5rem 0.75rem;
    overflow-x: auto;
  }

  .kb-key.w-space {
    min-width: calc(var(--kb-key-min) * 4.5);
  }

  .kb-key .kb-sub { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .kb-key.is-active,
  .kb-key.is-target {
    animation: none;
  }

  .keyboard.is-morphing {
    animation: none;
  }
}
