:root {
  --primary: #6366f1;
  --primary-glow: rgba(99, 102, 241, 0.3);
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --text: #e2e8f0;
  --text-dim: #64748b;
  --correct: #22c55e;
  --error: #ef4444;
  --error-bg: rgba(239, 68, 68, 0.15);
  --warning: #f59e0b;
  --radius: 10px;
  --transition: 0.2s ease;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'JetBrains Mono', monospace;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 1.5rem;
}

#app {
  width: 100%;
  max-width: 800px;
}

header {
  text-align: center;
  margin-bottom: 1.5rem;
}

h1 {
  font-size: 2rem;
  color: var(--primary);
  letter-spacing: -0.5px;
}

.subtitle {
  color: var(--text-dim);
  margin-top: 0.3rem;
  font-size: 0.9rem;
}

/* Stats Bar */
#stats-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.stat {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.75rem;
  text-align: center;
  transition: transform var(--transition), box-shadow var(--transition);
}

.stat:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.stat-label {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin-bottom: 0.25rem;
}

.stat-value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
}

/* Settings */
#settings {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
  align-items: flex-end;
  flex-wrap: wrap;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.setting-group label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
}

select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--surface-2);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color var(--transition);
}

select:hover,
select:focus {
  border-color: var(--primary);
  outline: none;
}

#custom-text-btn {
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid var(--surface-2);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all var(--transition);
  margin-left: auto;
}

#custom-text-btn:hover {
  border-color: var(--primary);
  color: var(--text);
}

#custom-text-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  animation: fadeIn 0.2s ease;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 90%;
  max-width: 500px;
  animation: slideUp 0.3s ease;
}

.modal-content h2 {
  margin-bottom: 1rem;
  color: var(--primary);
}

.modal-content textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--surface-2);
  border-radius: 6px;
  padding: 0.75rem;
  font-family: inherit;
  font-size: 0.9rem;
  resize: vertical;
  margin-bottom: 1rem;
}

.modal-content textarea:focus {
  outline: none;
  border-color: var(--primary);
}

.modal-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

/* Text Display */
#text-display {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1rem;
  font-size: 1.2rem;
  line-height: 2;
  min-height: 150px;
  position: relative;
  overflow: hidden;
  user-select: none;
  cursor: text;
  transition: box-shadow var(--transition);
}

#text-display.focused {
  box-shadow: 0 0 0 2px var(--primary), 0 0 20px var(--primary-glow);
}

#text-content {
  position: relative;
  z-index: 1;
}

#text-content .char {
  position: relative;
  transition: color 0.05s;
}

#text-content .char.correct {
  color: var(--correct);
}

#text-content .char.incorrect {
  color: var(--error);
  background: var(--error-bg);
  border-radius: 2px;
}

#text-content .char.current {
  border-left: 2px solid var(--primary);
  animation: blink 1s infinite;
  margin-left: -1px;
  padding-left: 1px;
}

#text-content .char.untyped {
  color: var(--text-dim);
}

/* Input Area */
#input-area {
  margin-bottom: 1rem;
}

#typing-input {
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 2px solid var(--surface-2);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  font-family: inherit;
  font-size: 1.1rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

#typing-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 15px var(--primary-glow);
}

#typing-input.has-error {
  border-color: var(--error);
  box-shadow: 0 0 15px rgba(239, 68, 68, 0.2);
  animation: shake 0.3s ease;
}

/* Controls */
#controls {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-bottom: 1rem;
}

button {
  font-family: inherit;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  padding: 0.7rem 2rem;
  font-size: 1rem;
  font-weight: 600;
}

.btn-primary:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
  box-shadow: 0 4px 15px var(--primary-glow);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--surface-2);
  border-radius: var(--radius);
  padding: 0.7rem 1.5rem;
  font-size: 1rem;
}

.btn-secondary:hover {
  background: var(--surface-2);
  border-color: var(--text-dim);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none !important;
}

/* Progress Bar */
#progress-container {
  background: var(--surface);
  border-radius: 4px;
  height: 6px;
  margin-bottom: 1.5rem;
  overflow: hidden;
}

#progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--correct));
  border-radius: 4px;
  width: 0%;
  transition: width 0.3s ease;
}

/* Results */
#results {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 2rem;
  text-align: center;
  margin-bottom: 1.5rem;
  animation: slideUp 0.4s ease;
}

#results.hidden {
  display: none;
}

#results h2 {
  color: var(--primary);
  font-size: 1.8rem;
  margin-bottom: 1.5rem;
}

#results-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.result-item {
  background: var(--bg);
  border-radius: 8px;
  padding: 1rem;
}

.result-label {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin-bottom: 0.3rem;
}

.result-value {
  display: block;
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text);
}

#result-message {
  font-size: 1.1rem;
  margin-bottom: 1.5rem;
  min-height: 1.5em;
}

#name-entry {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  margin-bottom: 1rem;
}

#player-name {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--surface-2);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-family: inherit;
  font-size: 0.9rem;
  width: 220px;
}

#player-name:focus {
  outline: none;
  border-color: var(--primary);
}

#save-score-btn {
  background: var(--correct);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 0.5rem 1rem;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
}

#save-score-btn:hover {
  filter: brightness(1.1);
}

#try-again-btn {
  margin-top: 0.5rem;
}

/* Leaderboard */
#leaderboard {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

#leaderboard h2 {
  color: var(--primary);
  text-align: center;
  margin-bottom: 1rem;
  font-size: 1.3rem;
}

#leaderboard-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  justify-content: center;
}

.tab {
  background: var(--bg);
  color: var(--text-dim);
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0.4rem 1rem;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  background: var(--primary);
  color: white;
}

#leaderboard-table {
  width: 100%;
  border-collapse: collapse;
}

#leaderboard-table th {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  text-align: left;
  padding: 0.5rem;
  border-bottom: 1px solid var(--surface-2);
}

#leaderboard-table td {
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--surface-2);
  font-size: 0.9rem;
}

#leaderboard-table tr:last-child td {
  border-bottom: none;
}

#leaderboard-table tr.highlight {
  background: var(--primary-glow);
}

#leaderboard-empty {
  text-align: center;
  color: var(--text-dim);
  padding: 1.5rem;
  font-style: italic;
}

#leaderboard-empty.hidden {
  display: none;
}

/* Footer */
footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.8rem;
  padding: 0.5rem 0;
}

kbd {
  background: var(--surface-2);
  border-radius: 4px;
  padding: 0.15rem 0.4rem;
  font-family: inherit;
  font-size: 0.75rem;
  border: 1px solid var(--text-dim);
}

/* Animations */
@keyframes blink {
  0%, 100% { border-color: var(--primary); }
  50% { border-color: transparent; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes popIn {
  0% { transform: scale(0.8); opacity: 0; }
  70% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

/* Responsive */
@media (max-width: 600px) {
  body {
    padding: 1rem 0.75rem;
  }

  h1 {
    font-size: 1.4rem;
  }

  #stats-bar {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }

  .stat-value {
    font-size: 1.2rem;
  }

  #text-display {
    font-size: 1rem;
    line-height: 1.8;
    padding: 1rem;
    min-height: 120px;
  }

  #results-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  #settings {
    flex-direction: column;
    align-items: stretch;
  }

  .setting-group {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  #custom-text-btn {
    margin-left: 0;
  }

  #name-entry {
    flex-direction: column;
    align-items: center;
  }

  #player-name {
    width: 100%;
    max-width: 280px;
  }

  #leaderboard-tabs {
    flex-wrap: wrap;
  }

  footer kbd {
    display: none;
  }

  footer p::after {
    content: "Tap Start to begin";
  }

  footer p {
    font-size: 0;
  }
}

/* Paused overlay */
.paused-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  border-radius: var(--radius);
  animation: fadeIn 0.2s ease;
}

.paused-overlay span {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
  animation: popIn 0.3s ease;
}

/* Countdown overlay */
.countdown-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  border-radius: var(--radius);
}

.countdown-overlay span {
  font-size: 3rem;
  font-weight: 700;
  color: var(--primary);
  animation: popIn 0.4s ease;
}
