:root {
  --stc-bg: #f5f7fb;
  --stc-card: #ffffff;
  --stc-text: #172033;
  --stc-muted: #64748b;
  --stc-border: #dbe3ef;
  --stc-primary: #2563eb;
  --stc-primary-dark: #1d4ed8;
  --stc-primary-soft: #eaf1ff;
  --stc-success: #15803d;
  --stc-success-soft: #ecfdf3;
  --stc-danger: #c62828;
  --stc-danger-soft: #fff0f0;
  --stc-warning: #b45309;
  --stc-shadow: 0 20px 60px rgba(30, 41, 59, 0.12);
}

.stc-shell {
  width: min(1120px, calc(100% - 1.5rem));
  margin-inline: auto;
}

.stc-panel {
  background: var(--stc-card);
  border: 1px solid var(--stc-border);
  border-radius: 1.5rem;
  box-shadow: var(--stc-shadow);
}

.stc-hidden {
  display: none !important;
}

.stc-how-to summary {
  list-style: none;
  cursor: pointer;
}

.stc-how-to summary::-webkit-details-marker {
  display: none;
}

.stc-how-to summary::after {
  content: "+";
  margin-left: auto;
  font-size: 1.3rem;
  line-height: 1;
}

.stc-how-to[open] summary::after {
  content: "−";
}

.stc-rival-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
}

.stc-rival-option {
  position: relative;
  min-height: 7.25rem;
  padding: 0.9rem 0.65rem;
  border: 2px solid var(--stc-border);
  border-radius: 1.1rem;
  background: #fff;
  text-align: center;
  cursor: pointer;
  transition: transform 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

.stc-rival-option:hover {
  transform: translateY(-2px);
  border-color: #9bb7f8;
}

.stc-rival-option[aria-pressed="true"] {
  border-color: var(--stc-primary);
  background: var(--stc-primary-soft);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.12);
}

.stc-rival-icon {
  display: block;
  font-size: 2.2rem;
  line-height: 1;
  transform: scaleX(-1);
  margin-bottom: 0.55rem;
  filter: drop-shadow(0 5px 8px rgba(15, 23, 42, 0.12));
}

.stc-rival-name {
  display: block;
  font-weight: 900;
  color: var(--stc-text);
}

.stc-rival-score {
  display: block;
  margin-top: 0.2rem;
  color: var(--stc-muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.stc-recommended {
  position: absolute;
  top: 0.45rem;
  right: 0.45rem;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  background: #dcfce7;
  color: #166534;
  font-size: 0.64rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stc-hud {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 0.65rem;
}

.stc-stat {
  border: 1px solid var(--stc-border);
  border-radius: 1rem;
  background: rgba(255, 255, 255, 0.96);
  padding: 0.8rem 0.9rem;
}

.stc-stat-label {
  display: block;
  color: var(--stc-muted);
  font-size: 0.68rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  margin-bottom: 0.12rem;
}

.stc-stat-value {
  color: var(--stc-text);
  font-size: 1.1rem;
  font-weight: 950;
}

.stc-race-meta {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 0.75rem;
  align-items: center;
  color: var(--stc-muted);
  font-size: 0.83rem;
  font-weight: 800;
}

.stc-track {
  position: relative;
  height: 6rem;
  overflow: hidden;
  border: 1px solid var(--stc-border);
  border-radius: 1.15rem;
  background:
    repeating-linear-gradient(90deg, transparent 0, transparent calc(10% - 1px), rgba(37, 99, 235, 0.07) calc(10% - 1px), rgba(37, 99, 235, 0.07) 10%),
    linear-gradient(180deg, #fbfdff 0%, #edf3fb 100%);
}

.stc-track::after {
  content: "🏁";
  position: absolute;
  right: 0.55rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 2rem;
}

.stc-runner,
.stc-opponent {
  position: absolute;
  left: 0;
  transform: scaleX(-1);
  transform-origin: center;
  transition: left 0.12s linear, transform 0.12s ease;
  user-select: none;
  filter: drop-shadow(0 5px 7px rgba(15, 23, 42, 0.18));
}

.stc-runner {
  top: 0.55rem;
  font-size: 2.45rem;
}

.stc-opponent {
  bottom: 0.4rem;
  font-size: 2.2rem;
}

.stc-runner.stc-bump {
  transform: scaleX(-1) translateY(-4px) scale(1.08);
}

.stc-opponent.stc-attack {
  transform: scaleX(-1) scale(1.16) rotate(-5deg);
}

.stc-text-card {
  position: relative;
  min-height: 20rem;
  overflow: hidden;
}

.stc-typing-text {
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.28rem, 2.7vw, 1.78rem);
  line-height: 1.92;
  letter-spacing: 0.015em;
  overflow-wrap: anywhere;
}

.stc-char {
  border-radius: 0.28rem;
  transition: color 0.08s ease, background 0.08s ease, box-shadow 0.08s ease, outline 0.08s ease;
}

.stc-char.stc-space {
  display: inline-block;
  width: 0.72em;
  min-width: 0.72em;
}

.stc-char.stc-done {
  color: var(--stc-success);
}

.stc-char.stc-current {
  color: var(--stc-text);
  background: var(--stc-primary-soft);
  outline: 2px solid var(--stc-primary);
  outline-offset: 2px;
}

.stc-char.stc-space.stc-current {
  min-height: 1.15em;
  vertical-align: -0.18em;
}

.stc-char.stc-pending {
  color: #8f9aaa;
}

.stc-char.stc-hard-error {
  color: #fff;
  background: var(--stc-danger);
  box-shadow: 0 0 0 5px rgba(198, 40, 40, 0.13);
}

.stc-char.stc-soft-error {
  color: #fff;
  background: var(--stc-danger);
  box-shadow: inset 0 -2px 0 rgba(105, 15, 15, 0.3);
}

.stc-line-break {
  display: block;
  height: 0.15em;
}

.stc-message {
  min-height: 1.75rem;
  margin-top: 1rem;
  text-align: center;
  color: var(--stc-muted);
  font-weight: 800;
}

.stc-message[data-type="error"] {
  color: var(--stc-danger);
}

.stc-message[data-type="success"] {
  color: var(--stc-success);
}

.stc-special-grid {
  display: grid;
  grid-template-columns: repeat(9, minmax(2.7rem, 1fr));
  gap: 0.5rem;
}

.stc-special-key {
  min-height: 3rem;
  border: 1px solid var(--stc-border);
  border-bottom-width: 3px;
  border-radius: 0.8rem;
  background: #fff;
  color: var(--stc-text);
  font-size: 1.18rem;
  font-weight: 950;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
}

.stc-special-key:hover {
  transform: translateY(-1px);
  border-color: #92aef0;
}

.stc-special-key.stc-key-hint {
  background: #fff7d6;
  border-color: #e2a800;
  box-shadow: 0 0 0 5px rgba(226, 168, 0, 0.15);
}

.stc-special-key.stc-key-urgent {
  animation: stc-key-pulse 0.62s ease-in-out 3;
}

@keyframes stc-key-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.14); background: #ffe49a; }
}

.stc-capture {
  position: fixed;
  left: -9999px;
  width: 2px;
  height: 2px;
  opacity: 0.01;
}

.stc-section-toast {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(255, 255, 255, 0.94);
  color: var(--stc-success);
  font-size: clamp(1.45rem, 5vw, 2.5rem);
  font-weight: 950;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: scale(0.95);
  transition: opacity 0.16s ease, transform 0.16s ease;
}

.stc-section-toast.stc-show {
  opacity: 1;
  transform: scale(1);
}

.stc-result-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
}

.stc-result-item {
  padding: 0.9rem;
  border: 1px solid var(--stc-border);
  border-radius: 1rem;
  background: #fbfcff;
}

.stc-result-item span {
  display: block;
  color: var(--stc-muted);
  font-size: 0.68rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stc-result-item strong {
  display: block;
  margin-top: 0.16rem;
  color: var(--stc-text);
  font-size: 1.1rem;
}

.stc-patterns {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.stc-patterns span {
  padding: 0.38rem 0.62rem;
  border: 1px solid #cbd8f4;
  border-radius: 999px;
  background: #fff;
  color: #1e3a8a;
  font-size: 0.82rem;
  font-weight: 850;
}

.stc-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--stc-border);
  border-radius: 1rem;
}

.stc-leaderboard {
  width: 100%;
  min-width: 850px;
  border-collapse: collapse;
  background: #fff;
}

.stc-leaderboard th,
.stc-leaderboard td {
  padding: 0.78rem 0.75rem;
  border-bottom: 1px solid #e7edf5;
  text-align: left;
  white-space: nowrap;
  font-size: 0.86rem;
}

.stc-leaderboard th {
  background: #f8fafc;
  color: #475569;
  font-size: 0.7rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stc-leaderboard tr:last-child td {
  border-bottom: 0;
}

.stc-leaderboard tr.stc-you td {
  background: #eff6ff;
  color: #1e3a8a;
  font-weight: 800;
}

.stc-status-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  border-radius: 999px;
  padding: 0.22rem 0.48rem;
  font-size: 0.72rem;
  font-weight: 900;
}

.stc-status-badge.stc-complete {
  background: #dcfce7;
  color: #166534;
}

.stc-status-badge.stc-caught {
  background: #fee2e2;
  color: #991b1b;
}

@media (max-width: 920px) {
  .stc-rival-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stc-hud {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .stc-result-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 700px) {
  .stc-race-meta {
    grid-template-columns: 1fr auto;
  }

  .stc-race-meta [data-role="opponent-label"] {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .stc-special-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .stc-text-card {
    min-height: 23rem;
  }
}

@media (max-width: 480px) {
  .stc-shell {
    width: min(100% - 0.75rem, 1120px);
  }

  .stc-hud {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stc-special-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .stc-rival-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stc-rival-option {
    min-height: 6.9rem;
  }
}

/* Mobile typing fix: keep the capture field close to the active text and show only ñ. */
.stc-keyboard-mobile-title,
.stc-keyboard-mobile-help {
  display: none;
}

.stc-capture {
  position: absolute;
  top: 4.75rem;
  left: 1rem;
  width: 1px;
  height: 1px;
  min-width: 1px;
  min-height: 1px;
  padding: 0;
  border: 0;
  opacity: 0.01;
  overflow: hidden;
  font-size: 16px;
  line-height: 1;
  resize: none;
  pointer-events: none;
}

@media (max-width: 700px) {
  .stc-keyboard-panel {
    padding: 0.75rem !important;
  }

  .stc-keyboard-copy {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    margin-bottom: 0.55rem !important;
    text-align: center;
  }

  .stc-keyboard-desktop-title,
  .stc-keyboard-desktop-help {
    display: none;
  }

  .stc-keyboard-mobile-title,
  .stc-keyboard-mobile-help {
    display: inline;
  }

  .stc-keyboard-mobile-help {
    font-size: 0.78rem;
  }

  .stc-special-grid {
    display: flex;
    justify-content: center;
    gap: 0;
  }

  .stc-special-key:not(#enye-key) {
    display: none;
  }

  #enye-key {
    width: 4.25rem;
    min-height: 3.15rem;
    flex: 0 0 4.25rem;
    font-size: 1.4rem;
  }

  .stc-text-card {
    min-height: 20rem;
  }
}


/* Final race visibility and feedback improvements. */
.stc-race-panel {
  position: relative;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
}

.stc-race-panel.stc-race-warning {
  border-color: #f59e0b;
  background: #fffdf5;
  box-shadow: 0 18px 50px rgba(180, 83, 9, 0.18);
}

.stc-race-panel.stc-race-danger {
  border-color: var(--stc-danger);
  background: var(--stc-danger-soft);
  box-shadow: 0 18px 55px rgba(198, 40, 40, 0.24);
  animation: stc-danger-panel-pulse 0.72s ease-in-out infinite alternate;
}

.stc-race-panel.stc-race-caught {
  border-color: var(--stc-danger);
  background: #fff1f2;
  box-shadow: 0 20px 65px rgba(153, 27, 27, 0.3);
}

@keyframes stc-danger-panel-pulse {
  from { box-shadow: 0 15px 42px rgba(198, 40, 40, 0.17); }
  to { box-shadow: 0 18px 58px rgba(198, 40, 40, 0.32); }
}

.stc-danger-alert {
  position: absolute;
  z-index: 8;
  top: 0.38rem;
  left: 50%;
  transform: translate(-50%, -8px);
  max-width: calc(100% - 7rem);
  padding: 0.28rem 0.65rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.94);
  color: var(--stc-warning);
  font-size: 0.76rem;
  font-weight: 950;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
}

.stc-danger-alert.stc-show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.stc-danger-alert.stc-critical {
  background: #fff;
  color: var(--stc-danger);
  box-shadow: 0 0 0 3px rgba(198, 40, 40, 0.14);
}

.stc-caught-overlay {
  position: absolute;
  z-index: 12;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  background: rgba(127, 29, 29, 0.88);
  color: #fff;
  font-size: clamp(1.2rem, 3vw, 2rem);
  font-weight: 950;
  letter-spacing: 0.01em;
  opacity: 0;
  visibility: hidden;
  transform: scale(0.97);
  transition: opacity 0.16s ease, transform 0.16s ease, visibility 0s linear 0.16s;
}

.stc-caught-overlay.stc-show {
  opacity: 1;
  visibility: visible;
  transform: scale(1);
  transition-delay: 0s;
}

.stc-caught-icon {
  display: inline-block;
  font-size: 2.25rem;
  transform: scaleX(-1);
  animation: stc-caught-bounce 0.42s ease-in-out 2;
}

@keyframes stc-caught-bounce {
  0%, 100% { transform: scaleX(-1) translateY(0); }
  50% { transform: scaleX(-1) translateY(-6px) scale(1.12); }
}

/* Keep the race visible on short laptop screens without crowding mobile keyboards. */
@media (min-width: 701px) and (max-height: 850px) {
  .stc-race-panel {
    position: sticky;
    top: 4.5rem;
    z-index: 40;
    padding: 0.62rem 0.8rem !important;
    border-radius: 1.05rem;
  }

  .stc-race-panel .stc-race-meta {
    margin-bottom: 0.32rem !important;
    font-size: 0.74rem;
  }

  .stc-race-panel .stc-track {
    height: 4.25rem;
    border-radius: 0.85rem;
  }

  .stc-race-panel .stc-runner {
    top: 0.25rem;
    font-size: 1.95rem;
  }

  .stc-race-panel .stc-opponent {
    bottom: 0.18rem;
    font-size: 1.8rem;
  }

  .stc-race-panel .stc-track::after {
    font-size: 1.55rem;
  }
}

@media (max-width: 700px) {
  .stc-danger-alert {
    top: 0.25rem;
    max-width: calc(100% - 4.5rem);
    font-size: 0.7rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .stc-race-panel.stc-race-danger,
  .stc-caught-icon {
    animation: none;
  }
}

/* Shared-engine rival locking for challenges with a minimum starting speed. */
.stc-rival-option:disabled,
.stc-rival-option.stc-rival-locked {
  cursor: not-allowed;
  opacity: 0.48;
  filter: grayscale(0.45);
  background: #f1f5f9;
  border-color: #cbd5e1;
  box-shadow: none;
}

.stc-rival-option:disabled:hover,
.stc-rival-option.stc-rival-locked:hover {
  transform: none;
  border-color: #cbd5e1;
}

.stc-locked-label {
  position: absolute;
  top: 0.45rem;
  right: 0.45rem;
  padding: 0.2rem 0.45rem;
  border-radius: 999px;
  background: #e2e8f0;
  color: #475569;
  font-size: 0.64rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
