/* CSS Variables */
:root {
  /* Colors */
  --neutral-900: hsl(0, 0%, 7%);
  --neutral-800: hsl(0, 0%, 15%);
  --neutral-500: hsl(240, 3%, 46%);
  --neutral-400: hsl(240, 1%, 59%);
  --neutral-0: hsl(0, 0%, 100%);

  --blue-600: hsl(214, 100%, 55%);
  --blue-400: hsl(210, 100%, 65%);

  --red-500: hsl(354, 63%, 57%);
  --green-500: hsl(140, 63%, 57%);
  --yellow-400: hsl(49, 85%, 70%);

  /* Typography */
  --font-family: "Sora", sans-serif;

  /* Spacing */
  --container-padding: 2rem;
}

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

/* Body */
body {
  font-family: var(--font-family);
  background-color: var(--neutral-900);
  color: var(--neutral-0);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  line-height: 1.6;
}

/* Container */
.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: var(--container-padding);
  width: 100%;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Header */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.logo-icon {
  width: 56px;
  height: 56px;
  display: block;
}

.logo-text {
  display: flex;
  flex-direction: column;
}

.logo-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--neutral-0);
  margin: 0;
  line-height: 1.2;
}

.logo-subtitle {
  font-size: 0.875rem;
  color: var(--neutral-400);
  font-weight: 400;
  margin: 0;
  line-height: 1.2;
}

.personal-best {
  font-size: 1rem;
  color: var(--neutral-400);
  font-weight: 400;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.pb-icon {
  width: 20px;
  height: 20px;
  display: block;
}

.pb-value {
  color: var(--neutral-0);
  font-weight: 700;
  margin: 0 0.25rem;
}

.pb-unit {
  color: var(--neutral-400);
  font-weight: 400;
}

/* Main */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
}

/* Stats and Controls Row */
.stats-controls-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}

/* Stats Bar */
.stats-bar {
  display: flex;
  gap: 2rem;
  align-items: center;
  flex-shrink: 0;
}

.stat-divider {
  width: 1px;
  height: 40px;
  background-color: var(--neutral-500);
  opacity: 0.3;
}

.horizontal-divider {
  width: 100%;
  height: 1px;
  background-color: var(--neutral-500);
  opacity: 0.2;
  margin-bottom: 2rem;
}

.stats-left {
  display: flex;
  gap: 3rem;
  align-items: center;
  flex-shrink: 0;
}

.stats-right {
  display: flex;
  gap: 2rem;
  align-items: center;
  flex-shrink: 0;
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}

.stat-label {
  font-size: 0.875rem;
  color: var(--neutral-500);
  font-weight: 400;
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--neutral-0);
}

.stat-value.low-accuracy {
  color: var(--red-500);
}

/* Controls */
.controls {
  display: flex;
  gap: 1.5rem;
  align-items: center;
  flex-shrink: 0;
}

/* Control Groups */
.control-group {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.control-label {
  font-size: 0.875rem;
  font-weight: 400;
  color: var(--neutral-400);
  white-space: nowrap;
}

.button-group {
  display: flex;
  gap: 0.5rem;
}

.btn-control {
  padding: 0.5rem 1rem;
  background-color: transparent;
  color: var(--neutral-400);
  border: 1px solid var(--neutral-500);
  border-radius: 6px;
  font-family: var(--font-family);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-control:hover {
  background-color: var(--neutral-800);
  color: var(--neutral-0);
  border-color: var(--neutral-400);
}

.btn-control:focus {
  outline: 2px solid var(--blue-600);
  outline-offset: 2px;
}

.btn-control.active {
  background-color: var(--blue-600);
  color: var(--neutral-0);
  border-color: var(--blue-600);
}

.btn-control.active:hover {
  background-color: var(--blue-400);
  border-color: var(--blue-400);
}

/* Passage Container */
.passage-container {
  position: relative;
  background-color: transparent;
  border-radius: 16px;
  padding: 2rem;
  margin-bottom: 2rem;
  min-height: 200px;
  cursor: text;
  transition: border 0.2s ease;
  border: 2px solid transparent;
}

.passage-container:hover {
  border-color: var(--neutral-500);
}

.passage-container.typing {
  border-color: var(--blue-600);
}

.passage {
  font-size: 1.5rem;
  line-height: 2.25rem;
  color: var(--neutral-400);
  letter-spacing: 0.01em;
  user-select: none;
  position: relative;
  word-wrap: break-word;
}

.passage span {
  position: relative;
}

.passage span.correct {
  color: var(--green-500);
}

.passage span.incorrect {
  color: var(--red-500);
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

.passage span.current {
  background-color: var(--yellow-400);
  color: var(--neutral-900);
  animation: blink 1s infinite;
}

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

/* Start Section */
.start-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  margin-bottom: 2rem;
}

.start-section.hidden {
  display: none;
}

.btn-start {
  padding: 0.75rem 1.5rem;
  background-color: transparent;
  color: var(--neutral-0);
  border: 1px solid var(--neutral-500);
  border-radius: 8px;
  font-family: var(--font-family);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-start:hover {
  background-color: var(--neutral-800);
  border-color: var(--neutral-400);
}

.btn-start:focus {
  outline: 2px solid var(--blue-600);
  outline-offset: 2px;
}

.start-hint {
  font-size: 0.875rem;
  color: var(--neutral-400);
  font-weight: 400;
}

/* Restart Section */
.restart-section {
  display: none;
  justify-content: center;
  margin-bottom: 2rem;
}

.restart-section.visible {
  display: flex;
}

.btn-restart {
  padding: 0.75rem 1.5rem;
  background-color: transparent;
  color: var(--neutral-0);
  border: 1px solid var(--neutral-500);
  border-radius: 8px;
  font-family: var(--font-family);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-restart:hover {
  background-color: var(--neutral-800);
  border-color: var(--neutral-400);
}

.btn-restart:focus {
  outline: 2px solid var(--blue-600);
  outline-offset: 2px;
}

.restart-icon {
  width: 18px;
  height: 18px;
  display: block;
}

/* Hidden Input */
.hidden-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  left: -9999px;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.8);
  z-index: 1000;
  justify-content: center;
  align-items: center;
  animation: fadeIn 0.3s ease;
}

.modal.show {
  display: flex;
}

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

.modal-content {
  background-color: hsl(0, 0%, 12%);
  border-radius: 16px;
  padding: 3rem;
  max-width: 500px;
  width: 90%;
  animation: slideUp 0.3s ease;
}

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

.results-header {
  text-align: center;
  margin-bottom: 2rem;
}

.results-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 1.5rem;
  display: none;
}

.results-icon img {
  width: 100%;
  height: 100%;
  display: block;
}

.results-icon.show {
  display: block;
}

.results-title {
  font-size: 2rem;
  font-weight: 700;
  color: var(--neutral-0);
  margin-bottom: 0.5rem;
}

.results-title.celebration {
  color: var(--yellow-400);
}

.results-title.baseline {
  color: var(--green-500);
}

.results-message {
  font-size: 1rem;
  color: var(--neutral-400);
  font-weight: 400;
}

.results-stats {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.result-stat {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background-color: var(--neutral-800);
  border-radius: 8px;
}

.result-label {
  font-size: 1rem;
  font-weight: 600;
  color: var(--neutral-400);
}

.result-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--neutral-0);
}

.btn-again {
  width: 100%;
  padding: 1rem 2rem;
  background-color: var(--blue-600);
  color: var(--neutral-0);
  border: none;
  border-radius: 8px;
  font-family: var(--font-family);
  font-size: 1.125rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-again:hover {
  background-color: var(--blue-400);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);
}

.btn-again:focus {
  outline: 2px solid var(--blue-600);
  outline-offset: 4px;
}

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

/* Attribution */
.attribution {
  font-size: 0.6875rem;
  text-align: center;
  padding: 2rem 0 1rem;
  color: var(--neutral-400);
}

.attribution a {
  color: var(--blue-600);
  text-decoration: none;
  transition: color 0.2s ease;
}

.attribution a:hover {
  color: var(--blue-400);
  text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 768px) {
  :root {
    --container-padding: 1rem;
  }

  .header {
    flex-direction: column;
    align-items: flex-start;
  }

  .logo-section {
    width: 100%;
  }

  .personal-best {
    width: 100%;
  }

  .stats-controls-row {
    flex-direction: column;
    align-items: stretch;
    gap: 1.5rem;
  }

  .stats-bar {
    gap: 1.5rem;
    justify-content: space-between;
    width: 100%;
  }

  .controls {
    flex-direction: column;
    gap: 1rem;
    width: 100%;
  }

  .control-group {
    width: 100%;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .button-group {
    width: 100%;
  }

  .btn-control {
    flex: 1;
    text-align: center;
  }

  .stat {
    flex: 1;
    min-width: 80px;
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .stat-label {
    font-size: 0.875rem;
  }

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

  .controls {
    flex-direction: column;
    gap: 1rem;
  }

  .control-group {
    flex-direction: column;
    align-items: flex-start;
    min-width: 100%;
  }

  .button-group {
    width: 100%;
  }

  .btn-control {
    flex: 1;
    text-align: center;
  }

  .passage {
    font-size: 1.25rem;
    line-height: 2rem;
  }

  .passage-container {
    padding: 1.5rem;
  }

  .modal-content {
    padding: 2rem;
  }

  .results-title {
    font-size: 1.5rem;
  }

  .result-stat {
    padding: 0.75rem;
  }

  .result-label {
    font-size: 0.875rem;
  }

  .result-value {
    font-size: 1.25rem;
  }
}

@media (max-width: 480px) {
  .personal-best {
    font-size: 0.875rem;
  }

  .stats-bar {
    gap: 1rem;
  }

  .passage {
    font-size: 1.125rem;
    line-height: 1.75rem;
  }

  .btn-start,
  .btn-again {
    font-size: 1rem;
    padding: 0.875rem 1.5rem;
  }
}
