/**
 * TapDance Main Stylesheet
 * ========================
 * 
 * Organization:
 * 1. CSS Variables & Root Styles
 * 2. Base Layout
 * 3. Header & Settings Panel
 * 4. Mode Controls
 * 5. Typing Area & Caret
 * 6. Stats Display
 * 7. Keyboard
 * 8. Key Flash Mode
 * 9. Special Effects
 * 10. Animations
 * 11. Responsive Design
 */

/* ============================================================
   1. CSS VARIABLES & ROOT STYLES
   ============================================================ */

:root {
  /* Default Theme Variables (overridden by themes.css) */
  --bg-primary: #111827;
  --bg-secondary: #1f2937;
  --bg-tertiary: #374151;
  --text-primary: #f3f4f6;
  --text-secondary: #9ca3af;
  --text-muted: #4b5563;
  --accent-color: #BEB05B;
  --border-color: #374151;
  --incorrect-color: #f87171;

  /* Typography */
  --main-font: 'Roboto Mono', monospace;
  --ui-font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;

  /* Sizing */
  --key-unit-width: 2.25rem;
  --caret-transition-speed: 0.1s;

  /* Spacing */
  --container-padding: 2rem;
  --section-gap: 1.5rem;
}

/* Reset */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

body {
  font-family: var(--ui-font);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background-color 0.3s ease, color 0.3s ease;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ============================================================
   2. BASE LAYOUT
   ============================================================ */

.container {
  width: 100%;
  height: 100%;
  padding: var(--container-padding);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  position: relative;
  z-index: 1;
}

.hidden {
  opacity: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

/* ============================================================
   3. HEADER & SETTINGS PANEL
   ============================================================ */

.header {
  position: absolute;
  top: 1rem;
  left: 1rem;
  right: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 100;
}

.logo {
  font-weight: 600;
  font-size: 1.25rem;
  color: var(--text-secondary);
  transition: opacity 0.3s ease-in-out;
  user-select: none;
}

.logo.hidden {
  opacity: 0;
}

.settings-area {
  position: relative;
}

.settings-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
}

.settings-btn:hover {
  color: var(--accent-color);
  background-color: var(--bg-secondary);
  transform: scale(1.1);
}

.settings-btn svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
  transition: transform 0.4s ease;
}

.settings-btn.open svg {
  transform: rotate(60deg);
}

/* Settings Panel */
.settings-panel {
  display: none;
  flex-direction: column;
  gap: 1rem;
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  background: var(--bg-secondary);
  padding: 1.25rem;
  border-radius: 12px;
  border: 1px solid var(--border-color);
  width: 300px;
  max-height: 80vh;
  overflow-y: auto;
  z-index: 1000;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.25);
}

.settings-section {
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-color);
}

.settings-section:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.settings-section-title {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 0.75rem;
}

.setting {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.setting:last-child {
  margin-bottom: 0;
}

.setting label:first-child {
  color: var(--text-secondary);
  font-size: 0.875rem;
  font-weight: 500;
}

.setting select,
.setting input[type="color"] {
  background: var(--bg-primary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  padding: 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  width: 140px;
  font-family: var(--ui-font);
  font-size: 0.85rem;
}

.setting select:focus,
.setting input[type="color"]:focus {
  outline: none;
  border-color: var(--accent-color);
}

.setting select optgroup {
  font-weight: 600;
  color: var(--text-muted);
}

/* Slider with Reset Button */
.slider-with-reset {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.slider-with-reset input[type="range"] {
  width: 100px;
  cursor: pointer;
  accent-color: var(--accent-color);
}

.reset-btn {
  background: none;
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  border-radius: 4px;
  padding: 2px 6px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s ease;
}

.reset-btn:hover {
  border-color: var(--accent-color);
  color: var(--accent-color);
}

/* Toggle Switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 24px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-switch .slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--bg-tertiary);
  transition: 0.3s;
  border-radius: 24px;
}

.toggle-switch .slider::before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 4px;
  bottom: 4px;
  background-color: var(--text-primary);
  transition: 0.3s;
  border-radius: 50%;
}

.toggle-switch input:checked+.slider {
  background-color: var(--accent-color);
}

.toggle-switch input:checked+.slider::before {
  transform: translateX(16px);
}

/* ============================================================
   4. MODE CONTROLS
   ============================================================ */

.main-content-wrapper {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: 900px;
  gap: var(--section-gap);
}

.test-mode-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  background-color: var(--bg-secondary);
  padding: 0.5rem;
  border-radius: 8px;
  gap: 0.5rem;
  transition: all 0.3s ease;
}

.test-mode-controls.hidden {
  opacity: 0;
  visibility: hidden;
  transform: scale(0.95);
}

.mode-group {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}

.mode-divider {
  width: 1px;
  height: 24px;
  background-color: var(--border-color);
  margin: 0 0.5rem;
}

.mode-btn {
  background: none;
  color: var(--text-secondary);
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--ui-font);
  font-weight: 500;
  white-space: nowrap;
}

.mode-btn:hover {
  color: var(--text-primary);
  background-color: var(--bg-tertiary);
}

.mode-btn.active {
  color: var(--bg-primary);
  background-color: var(--accent-color);
}

.mode-btn.active:hover {
  filter: brightness(1.1);
}

.mode-btn svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

/* ============================================================
   5. TYPING AREA & CARET
   ============================================================ */

.typing-area {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  width: 100%;
}

.text-display {
  width: 100%;
  font-size: 1.8rem;
  line-height: 2;
  font-family: var(--main-font);
  overflow: hidden;
  height: 180px;
  position: relative;
  cursor: text;
  transition: opacity 0.3s ease;
}

.text-content-wrapper {
  position: relative;
  transition: transform 0.2s ease-out;
  width: 100%;
  white-space: normal;
}

.text-content-wrapper.scroll {
  white-space: nowrap;
}

/* Word wrapper prevents word breaking */
.word-wrapper {
  display: inline-block;
}

/* Characters */
.char {
  display: inline-block;
  position: relative;
  transition: color 0.2s ease, opacity 0.2s ease;
  color: var(--text-muted);
}

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

.char.incorrect {
  color: var(--incorrect-color);
  background-color: rgba(248, 113, 113, 0.1);
  border-radius: 4px;
}

/* Caret */
.caret {
  position: absolute;
  border-radius: 2px;
  opacity: 0;
  animation: blink 1s infinite;
  pointer-events: none;
  z-index: 10;
  transition: left var(--caret-transition-speed) ease,
    top var(--caret-transition-speed) ease,
    transform 0.1s ease-out;
}

.caret.line {
  width: 2px;
  background-color: var(--accent-color);
}

.caret.block {
  background-color: var(--accent-color);
  opacity: 0.3 !important;
  animation: none;
}

.caret.underline {
  height: 3px !important;
  background-color: var(--accent-color);
}

@keyframes blink {

  0%,
  100% {
    opacity: 0;
  }

  50% {
    opacity: 1;
  }
}

/* Restart Button */
.restart-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 1rem;
  opacity: 0;
  visibility: hidden;
  transform: scale(0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 0;
  border-radius: 50%;
}

.restart-btn.visible {
  opacity: 1;
  visibility: visible;
  height: 50px;
  transform: scale(1);
}

.restart-btn:hover,
.restart-btn:focus {
  color: var(--accent-color);
  transform: scale(1.1) rotate(15deg);
  outline: none;
  animation: pulse 1.5s infinite;
}

.restart-btn svg {
  width: 32px;
  height: 32px;
  fill: currentColor;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 var(--accent-color);
  }

  100% {
    box-shadow: 0 0 0 15px transparent;
  }
}

/* ============================================================
   6. STATS DISPLAY
   ============================================================ */

/* Final Results Stats */
.stats {
  display: flex;
  gap: 2rem;
  flex-wrap: wrap;
  justify-content: center;
  transition: opacity 0.3s ease, transform 0.3s ease;
  transform: translateY(20px);
  min-height: 60px;
}

.stats.hidden {
  opacity: 0;
  visibility: hidden;
  transform: translateY(0);
}

.stat {
  text-align: center;
}

.stat-value {
  font-size: 2rem;
  font-weight: 600;
  color: var(--accent-color);
}

.stat-label {
  font-size: 0.8rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* In-Test Live Stats */
.in-test-stats {
  display: flex;
  gap: 2rem;
  color: var(--text-secondary);
  font-family: var(--main-font);
  font-size: 1.1rem;
  height: 24px;
  transition: opacity 0.3s ease;
}

.in-test-stats.hidden {
  opacity: 0;
}

.live-stat-wpm {
  color: var(--accent-color);
  font-weight: 500;
}

.live-stat-acc {
  font-weight: 500;
}

/* ============================================================
   7. KEYBOARD
   ============================================================ */

.keyboard {
  width: 100%;
  max-width: 900px;
  padding: 1rem 0;
  transition: opacity 0.3s ease, visibility 0.3s ease, height 0.3s ease;
}

.keyboard.hidden {
  opacity: 0;
  visibility: hidden;
  height: 0;
  padding: 0;
  margin: 0;
  overflow: hidden;
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.key-gap {
  width: calc(var(--key-unit-width) * 2);
  flex-shrink: 0;
}

.key {
  background-color: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-bottom-width: 3px;
  border-radius: 6px;
  height: calc(var(--key-unit-width) * 1.1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  color: var(--text-secondary);
  transition: all 0.1s ease;
  cursor: default;
  font-family: var(--main-font);
  flex-shrink: 0;
  user-select: none;
}

.key.pressed {
  background: var(--accent-color);
  color: var(--bg-primary);
  border-color: var(--accent-color);
  transform: translateY(2px);
  border-bottom-width: 1px;
}

/* Key Flash Mode - Highlighted key */
.key.highlighted {
  background: var(--accent-color);
  color: var(--bg-primary);
  border-color: var(--accent-color);
  box-shadow: 0 0 20px var(--accent-color);
  animation: keyGlow 0.5s ease-in-out infinite alternate;
}

@keyframes keyGlow {
  from {
    box-shadow: 0 0 10px var(--accent-color);
  }

  to {
    box-shadow: 0 0 25px var(--accent-color);
  }
}

.key .special-text {
  font-size: 0.65rem;
}

/* Key Size Units */
.u-100 {
  width: var(--key-unit-width);
}

.u-125 {
  width: calc(var(--key-unit-width) * 1.25);
}

.u-150 {
  width: calc(var(--key-unit-width) * 1.5);
}

.u-175 {
  width: calc(var(--key-unit-width) * 1.75);
}

.u-200 {
  width: calc(var(--key-unit-width) * 2.0);
}

.u-225 {
  width: calc(var(--key-unit-width) * 2.25);
}

.u-275 {
  width: calc(var(--key-unit-width) * 2.75);
}

.u-625 {
  width: calc(var(--key-unit-width) * 6.25);
}

/* ============================================================
   8. KEY FLASH MODE
   ============================================================ */

.keyflash-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  width: 100%;
  min-height: 200px;
  justify-content: center;
}

.keyflash-area.hidden {
  display: none;
}

.keyflash-stats {
  display: flex;
  gap: 2rem;
  font-family: var(--main-font);
  color: var(--text-secondary);
}

.keyflash-stat strong {
  color: var(--accent-color);
}

.keyflash-prompt {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.keyflash-instruction {
  color: var(--text-muted);
  font-size: 1rem;
}

.keyflash-burst-display {
  font-family: var(--main-font);
  font-size: 2.5rem;
  color: var(--accent-color);
  letter-spacing: 0.5rem;
  text-transform: uppercase;
}

.keyflash-burst-display.hidden {
  display: none;
}

/* ============================================================
   9. SPECIAL EFFECTS
   ============================================================ */

/* Ghost Effect - Fades typed characters */
.char.ghost-fade {
  opacity: 0.15;
  transition: opacity 1.5s ease-out;
}

/* Redacted Effect - Blacks out errors */
.fx-redacted .char.incorrect {
  background-color: var(--text-primary);
  color: var(--text-primary);
  border-radius: 0;
  text-shadow: none;
}

/* Focused/Blind Effects - Hide words */
.word-wrapper.fx-hidden,
.char.fx-hidden {
  opacity: 0;
  pointer-events: none;
}

/* Recall Effect - Fade out typed words */
.fx-recall .word-wrapper.typed {
  opacity: 0;
  transition: opacity 0.5s ease-out;
}

/* ============================================================
   10. RESPONSIVE DESIGN
   ============================================================ */

@media (max-width: 900px) {
  .text-display {
    font-size: 1.6rem;
  }
}

@media (max-width: 768px) {
  :root {
    --container-padding: 1rem;
    --key-unit-width: 1.8rem;
  }

  .header {
    top: 1rem;
    left: 1rem;
    right: 1rem;
  }

  .keyboard {
    display: none;
  }

  .test-mode-controls {
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
  }

  .mode-divider {
    display: none;
  }

  .mode-btn {
    padding: 0.8rem 1rem;
    font-size: 1rem;
    min-height: 44px;
  }

  .settings-panel {
    width: calc(100vw - 2rem);
    max-width: 300px;
  }
}

@media (max-width: 480px) {
  .text-display {
    font-size: 1.4rem;
    line-height: 1.8;
    height: 120px;
  }

  .test-mode-controls {
    font-size: 0.8rem;
  }

  .stats {
    gap: 1rem;
    padding: 1rem 1.5rem;
  }

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