:root {
  --primary: #EF4444;    /* Red */
  --secondary: #3B82F6;  /* Blue */
  --accent: #F59E0B;     /* Amber */
  --bg: #0F172A;         /* Slate background */
  --panel-bg: #1E293B;   /* Slate panel */
  --panel-border: #334155;
  --text-main: #F8FAFC;
  --text-muted: #94A3B8;
  --correct: #10B981;    /* Green */
  --incorrect: #EF4444;  
  --radius: 12px;
  --font-sans: 'Inter', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--bg);
  color: var(--text-main);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  justify-content: center;
}

.app-container {
  width: 100%;
  max-width: 800px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

h1 {
  font-weight: 800;
  font-size: 2rem;
  letter-spacing: -0.5px;
  color: var(--text-main);
}

.stat-pill {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  padding: 8px 16px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--accent);
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

/* Controls */
.controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
}

.mode-select {
  display: flex;
  background: var(--bg);
  padding: 4px;
  border-radius: var(--radius);
  border: 1px solid var(--panel-border);
}

.mode-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 16px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.mode-btn.active {
  background: var(--panel-bg);
  color: var(--text-main);
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.settings {
  display: flex;
  gap: 16px;
  align-items: center;
}

.setting-group {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

select {
  background: var(--bg);
  color: var(--text-main);
  border: 1px solid var(--panel-border);
  padding: 6px 12px;
  border-radius: 6px;
  font-family: var(--font-sans);
  outline: none;
}

.primary-btn {
  background: var(--primary);
  color: white;
  border: none;
  padding: 10px 24px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.primary-btn:hover { background: #DC2626; }
.primary-btn:active { transform: translateY(1px); }

.secondary-btn {
  background: transparent;
  color: var(--text-main);
  border: 1px solid var(--panel-border);
  padding: 10px 24px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.secondary-btn:hover { background: var(--bg); }

/* Race Area */
.tracks {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.track {
  display: flex;
  align-items: center;
  gap: 16px;
}

.track-label {
  width: 60px;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-muted);
  text-align: right;
}

.track-lane {
  flex: 1;
  position: relative;
  height: 40px;
  background: var(--bg);
  border-radius: 20px;
  border: 1px solid var(--panel-border);
  display: flex;
  align-items: center;
  padding: 0 10px;
  overflow: hidden;
}

/* Finish line pattern */
.track-lane::after {
  content: '';
  position: absolute;
  right: 10px;
  top: 0;
  bottom: 0;
  width: 10px;
  background: repeating-linear-gradient(
    45deg,
    #fff,
    #fff 4px,
    #000 4px,
    #000 8px
  );
  opacity: 0.3;
}

.progress-line {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  background: rgba(59, 130, 246, 0.1);
  transition: width 0.2s linear;
}

#aiProgress {
  background: rgba(239, 68, 68, 0.1);
}

.car {
  font-size: 1.5rem;
  position: absolute;
  left: 10px;
  transform: translateX(-50%); /* Center the icon on its percentage */
  transition: left 0.2s linear;
  z-index: 2;
}

.wpm-tag {
  width: 80px;
  text-align: right;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 1.1rem;
}
#playerWpmLive { color: var(--secondary); }
#aiWpmLive { color: var(--primary); }

/* Typing Area */
.typing-area {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.countdown {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 6rem;
  font-weight: 800;
  color: var(--accent);
  text-shadow: 0 4px 12px rgba(0,0,0,0.5);
  z-index: 10;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}

.countdown.visible {
  opacity: 1;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.1); }
  100% { transform: translate(-50%, -50%) scale(1); }
}

.text-display {
  font-family: var(--font-mono);
  font-size: 1.3rem;
  line-height: 1.6;
  color: var(--text-muted);
  user-select: none;
  padding: 10px;
  min-height: 100px;
}

.text-display span {
  transition: color 0.1s, background-color 0.1s;
  border-radius: 2px;
}

.word { margin-right: 0.5em; display: inline-block; }

.char.correct { color: var(--text-main); }
.char.incorrect { 
  color: white;
  background-color: var(--incorrect); 
}
.char.cursor {
  position: relative;
}
.char.cursor::before {
  content: '';
  position: absolute;
  left: 0;
  bottom: 2px;
  width: 100%;
  height: 2px;
  background-color: var(--secondary);
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.type-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
} /* hidden input to capture mobile/desktop typing reliably */

.live-stats {
  display: flex;
  gap: 24px;
  border-top: 1px solid var(--panel-border);
  padding-top: 16px;
}

.stat-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: 0.5px;
}

.stat-value {
  font-family: var(--font-mono);
  font-size: 1.2rem;
  font-weight: 700;
}

/* Modals & Overlays */
.overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: blur(4px);
  z-index: 100;
  justify-content: center;
  align-items: center;
}
.overlay.active { display: flex; }

.results-card {
  background: var(--panel-bg);
  padding: 32px;
  border-radius: var(--radius);
  border: 1px solid var(--panel-border);
  text-align: center;
  max-width: 400px;
  width: 90%;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.2);
}

.results-card h2 {
  margin-bottom: 24px;
  font-size: 1.8rem;
}

.results-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 24px;
}

.result-box {
  background: var(--bg);
  padding: 16px;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
}

.result-box.primary { border-color: var(--secondary); }
.result-box.primary .result-value { color: var(--secondary); }
.result-box.secondary .result-value { color: var(--correct); }

.result-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: 4px;
}

.result-value {
  font-family: var(--font-mono);
  font-size: 1.8rem;
  font-weight: 800;
}

.results-card .primary-btn { width: 100%; }

/* Custom Text Modal */
.modal {
  display: none;
  position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.6);
  z-index: 100;
  justify-content: center;
  align-items: center;
}
.modal.active { display: flex; }

.modal-content {
  background: var(--panel-bg);
  padding: 24px;
  border-radius: var(--radius);
  width: 90%;
  max-width: 500px;
  border: 1px solid var(--panel-border);
}

.modal-content h2 { margin-bottom: 16px; }

textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  color: var(--text-main);
  padding: 12px;
  border-radius: 8px;
  font-family: var(--font-mono);
  resize: vertical;
  margin-bottom: 16px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* History */
.history-panel h3 {
  font-size: 1.1rem;
  margin-bottom: 16px;
  color: var(--text-muted);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  display: flex;
  justify-content: space-between;
  background: var(--bg);
  padding: 12px 16px;
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  border: 1px solid var(--panel-border);
}

.history-item.win { border-left: 4px solid var(--correct); }
.history-item.loss { border-left: 4px solid var(--incorrect); }
.history-item.practice { border-left: 4px solid var(--text-muted); }

.history-empty {
  color: var(--text-muted);
  text-align: center;
  padding: 20px;
  font-style: italic;
}

@media (max-width: 600px) {
  .controls { flex-direction: column; align-items: stretch; }
  .settings { flex-direction: column; align-items: stretch; }
  .header-stats { width: 100%; text-align: center; margin-top: 10px; }
  header { flex-direction: column; }
  #startBtn { padding: 16px; }
}
