@font-face {
  font-family: "Sora";
  src: url("./assets/fonts/Sora/Sora-VariableFont_wght.ttf") format("truetype");
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --neutral-900: hsl(0, 0%, 7%);
  --neutral-800: hsl(0, 0%, 15%);
  --neutral-700: hsl(0, 0%, 20%);
  --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%);
  --border-subtle: hsla(240, 1%, 59%, 0.32);
  --surface-subtle: hsla(0, 0%, 100%, 0.02);
  --focus-ring: 0 0 0 3px var(--neutral-900), 0 0 0 5px var(--blue-600);
  --content-width: 1216px;
  --ease: 180ms ease-in-out;
}

/* 2. Reset & base styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  min-width: 320px;
  background: var(--neutral-900);
}

body {
  min-height: 100vh;
  background: var(--neutral-900);
  color: var(--neutral-0);
  font-family: "Sora", system-ui, sans-serif;
  font-size: 1rem;
  font-weight: 400;
}

button,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

img {
  display: block;
  max-width: 100%;
}

.hidden {
  display: none !important;
}

.app-shell {
  position: relative;
  width: min(100% - 2rem, var(--content-width));
  min-height: 100vh;
  margin-inline: auto;
  padding-block: 1.25rem 2rem;
}

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

picture {
  width: fit-content;
}
.best-score {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.625rem;
  color: var(--neutral-0);
  font-size: 1rem;
  line-height: 1.2;
  white-space: nowrap;
}

.best-score img {
  width: 1.3125rem;
  height: 1.125rem;
}

.best-label {
  color: var(--neutral-400);
  opacity: 0.8;
}

.best-label-desktop {
  display: none;
}

body.results-active .stats-bar {
  display: none;
}

/* 4. Stats bar */
.stats-bar {
  margin-bottom: 1.75rem;
  padding-bottom: 0.9375rem;
  border-bottom: 1px solid var(--border-subtle);
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-bottom: 1rem;
}

.stat-item {
  display: grid;
  gap: 0.25rem;
  justify-items: center;
  min-width: 0;
  padding-inline: 0.55rem;
  text-align: center;
}

.stat-item + .stat-item {
  border-left: 1px solid var(--border-subtle);
}

.stat-label {
  color: var(--neutral-400);
  font-size: clamp(0.875rem, 4vw, 1rem);
}

.stat-item strong {
  color: var(--neutral-0);
  font-size: clamp(1.35rem, 6vw, 1.5rem);
  font-weight: 700;
  letter-spacing: 0;
}

#accuracy-number.has-errors,
#result-accuracy.has-errors,
#result-errors {
  color: var(--red-500);
}

#result-accuracy.is-perfect {
  color: var(--green-500);
}

#time-number.is-active {
  color: var(--yellow-400);
}

/* 5. Controls */
.desktop-controls {
  display: none;
}

.mobile-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.625rem;
}

.dropdown-control {
  position: relative;
  min-width: 0;
}

.dropdown-trigger,
.dropdown-option,
.control-pill,
.start-btn,
.restart-btn,
.result-action {
  border-radius: 999px;
  transition:
    background-color var(--ease),
    border-color var(--ease),
    color var(--ease),
    box-shadow var(--ease),
    transform var(--ease);
}

.dropdown-trigger {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.625rem;
  width: 100%;
  min-height: 2rem;
  padding: 0.35rem 0.85rem;
  border: 1px solid var(--neutral-500);
  background: transparent;
  color: var(--neutral-0);
  font-weight: 400;
}

.dropdown-trigger img {
  width: 0.6875rem;
  height: 0.375rem;
  transition: transform var(--ease);
}

.dropdown-trigger[aria-expanded="true"] {
  border-color: var(--blue-400);
}

.dropdown-trigger[aria-expanded="true"] img {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  z-index: 20;
  top: calc(100% + 0.5rem);
  left: 0;
  right: 0;
  display: none;
  overflow: hidden;
  border: 1px solid var(--border-subtle);
  border-radius: 0.5rem;
  background: var(--neutral-800);
  box-shadow: 0 1rem 2rem hsla(0, 0%, 0%, 0.25);
}

.dropdown-menu.open {
  display: block;
}

.dropdown-option {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.85rem 0.75rem;
  border: 0;
  border-bottom: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--neutral-0);
  text-align: left;
}

.dropdown-option:last-child {
  border-bottom: 0;
}

.radio-dot {
  width: 1rem;
  height: 1rem;
  flex: 0 0 1rem;
  border: 1px solid var(--neutral-0);
  border-radius: 50%;
}

.dropdown-option[aria-checked="true"] .radio-dot {
  border: 0.25rem solid var(--blue-400);
  background: var(--neutral-900);
}

/* 6. Passage / typing area */
.typing-screen {
  position: relative;
}

.passage-shell {
  position: relative;
  min-height: min(33rem, 62vh);
}

.text-container {
  min-height: min(33rem, 62vh);
  color: var(--neutral-400);
  font-size: clamp(1.75rem, 8vw, 2.25rem);
  line-height: 1.52;
  outline: 2px solid transparent;
  outline-offset: 0.35rem;
  overflow-wrap: break-word;
  transition:
    filter var(--ease),
    opacity var(--ease);
  user-select: none;
  white-space: pre-wrap;
}

.text-container.blurred {
  filter: blur(6px);
  opacity: 0.72;
}

.text-container.loading,
.text-container.error {
  display: grid;
  min-height: 16rem;
  place-items: center;
  color: var(--neutral-400);
  font-size: 1.125rem;
  text-align: center;
}

.word {
  display: inline-block;
  white-space: nowrap;
}

/* 7. Character states */
.char {
  display: inline;
  color: var(--neutral-400);
  border-radius: 0.125rem;
  transition:
    color 80ms ease-in-out,
    background-color 80ms ease-in-out;
}

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

.char.incorrect {
  color: var(--red-500);
  text-decoration-line: underline;
  text-decoration-thickness: 0.12em;
  text-underline-offset: 0.16em;
}

.char.cursor {
  animation: cursorBlink 1s steps(2, start) infinite;
  background: hsla(0, 0%, 100%, 0.15);
  box-shadow: 0 0 0 0.08em hsla(0, 0%, 100%, 0.05);
}

.char.space.cursor {
  padding-inline: 0.12em;
}

@keyframes cursorBlink {
  50% {
    background: hsla(0, 0%, 100%, 0.06);
  }
}

/* 8. Start overlay */
.start-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-inline: 0.75rem;
  text-align: center;
  pointer-events: none;
}

.start-btn {
  margin-bottom: 1rem;
  padding: 0.95rem 1.55rem;
  border: 1px solid var(--blue-600);
  background: var(--blue-600);
  color: var(--neutral-0);
  font-size: clamp(1.125rem, 5vw, 1.25rem);
  font-weight: 700;
  pointer-events: auto;
}

.start-overlay p {
  color: var(--neutral-0);
  font-size: clamp(1rem, 4.8vw, 1.25rem);
  font-weight: 700;
  line-height: 1.35;
}

.restart-area {
  display: flex;
  justify-content: center;
  margin-top: 1.65rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border-subtle);
}

.restart-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.8rem 1.1rem;
  border: 1px solid transparent;
  background: var(--neutral-800);
  color: var(--neutral-0);
  font-size: 1.125rem;
  font-weight: 700;
}

.restart-btn img,
.result-action img {
  width: 1.25rem;
  height: 1.25rem;
}

/* 9. Results screen */
.results-screen {
  position: relative;
  display: flex;
  min-height: calc(100vh - 7rem);
  align-items: flex-start;
  justify-content: center;
  overflow: hidden;
  padding-block: 0.25rem 2rem;
}

.result-content {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 35rem;
  margin-inline: auto;
  text-align: center;
}

.result-icon {
  width: 5.5rem;
  height: 5.5rem;
  margin-inline: auto;
  margin-bottom: 1.35rem;
}

.results-screen.is-high-score .result-icon {
  width: 5rem;
  height: 5rem;
}

#result-heading {
  color: var(--neutral-0);
  font-size: clamp(1.7rem, 7vw, 2rem);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: 0;
}

#result-subtitle {
  max-width: 21rem;
  margin: 0.75rem auto 0;
  color: var(--neutral-400);
  font-size: clamp(1rem, 4.4vw, 1.125rem);
  line-height: 1.35;
}

/* 10. Stat cards */
.result-cards {
  display: grid;
  gap: 1rem;
  margin-top: 1.75rem;
}

.result-card {
  min-height: 5.75rem;
  padding: 1rem 1.5rem;
  border: 1px solid var(--border-subtle);
  border-radius: 0.5rem;
  background: var(--surface-subtle);
  text-align: left;
}

.result-card > span:first-child {
  display: block;
  color: var(--neutral-400);
  font-size: 1.25rem;
  line-height: 1.2;
}

.result-card strong {
  display: block;
  margin-top: 0.55rem;
  color: var(--neutral-0);
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.15;
}

#result-correct {
  color: var(--green-500);
}

.characters-score span:nth-child(2) {
  display: inline;
  color: var(--neutral-0);
}

.result-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.625rem;
  min-height: 3.5rem;
  margin-top: 2.4rem;
  padding: 0.85rem 1.25rem;
  border: 1px solid var(--neutral-0);
  background: var(--neutral-0);
  color: var(--neutral-900);
  font-size: 1.25rem;
  font-weight: 700;
}

.result-action img {
  filter: invert(1);
}

/* 11. Confetti canvas/pattern */
.typing-input {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

.confetti-pattern {
  position: absolute;
  z-index: 1;
  right: 50%;
  bottom: -0.75rem;
  display: none;
  width: max(100vw, 52rem);
  max-width: none;
  transform: translateX(50%);
  pointer-events: none;
}

.results-screen.is-high-score .confetti-pattern {
  display: block;
}

/* 12. Decorative elements */
.result-decor {
  position: absolute;
  z-index: 1;
  pointer-events: none;
}

.decor-left {
  top: 5.2rem;
  left: 0;
  width: 2rem;
  height: 2rem;
}

.decor-right {
  right: 0.9rem;
  bottom: 2.2rem;
  width: 3.35rem;
  height: 3.35rem;
}

/* 13. Hover & focus states */
.dropdown-trigger:hover,
.control-pill:hover {
  border-color: var(--blue-400);
  color: var(--blue-400);
}

.dropdown-option:hover,
.restart-btn:hover {
  background: var(--neutral-700);
}

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

.result-action:hover {
  transform: translateY(-1px);
}

.start-btn:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

button:focus-visible,
.text-container:focus-visible {
  outline: 2px solid var(--blue-600);
  outline-offset: 0.25rem;
  box-shadow: var(--focus-ring);
}

.dropdown-option:focus-visible {
  border-radius: 0;
  box-shadow: inset 0 0 0 2px var(--blue-400);
}

@media (min-width: 48rem) {
  .app-shell {
    width: min(100% - 14rem, var(--content-width));
    padding-block: 2rem 3rem;
  }

  .app-header {
    margin-bottom: 3.9rem;
  }

  .best-label-mobile {
    display: none;
  }

  .best-label-desktop {
    display: inline;
  }

  .stats-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2rem;
    margin-bottom: 2.2rem;
    padding-bottom: 0.95rem;
  }

  .stats-row {
    display: flex;
    align-items: center;
    gap: 1.45rem;
    margin-bottom: 0;
  }

  .stat-item {
    display: flex;
    align-items: baseline;
    gap: 0.8rem;
    padding: 0;
    text-align: left;
  }

  .stat-item + .stat-item {
    padding-left: 1.45rem;
  }

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

  .stat-item strong {
    font-size: 1.5rem;
  }

  .mobile-controls {
    display: none;
  }

  .desktop-controls {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 1rem;
  }

  .control-cluster {
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }

  .mode-cluster {
    padding-left: 1rem;
    border-left: 1px solid var(--border-subtle);
  }

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

  .pill-group {
    display: flex;
    align-items: center;
    gap: 0.375rem;
  }

  .control-pill {
    min-height: 2rem;
    padding: 0.25rem 0.75rem;
    border: 1px solid var(--neutral-500);
    background: transparent;
    color: var(--neutral-0);
    font-size: 1rem;
  }

  .control-pill.selected {
    border-color: var(--blue-600);
    color: var(--blue-400);
  }

  .passage-shell,
  .text-container {
    min-height: 34rem;
  }

  .text-container {
    font-size: clamp(2.1rem, 3vw, 2.5rem);
    line-height: 1.45;
  }

  .start-btn {
    padding: 0.75rem 1.45rem;
  }

  .restart-area {
    margin-top: 1.8rem;
    padding-top: 1.95rem;
  }

  .restart-btn {
    padding: 0.75rem 1rem;
  }

  .results-screen {
    min-height: calc(100vh - 8.5rem);
    align-items: flex-start;
    padding-top: 1.6rem;
  }

  .result-content {
    max-width: 40rem;
  }

  .result-icon {
    width: 6.25rem;
    height: 6.25rem;
    margin-bottom: 1.8rem;
  }

  .results-screen.is-high-score .result-icon {
    width: 5rem;
    height: 5rem;
  }

  #result-heading {
    font-size: 2.5rem;
  }

  #result-subtitle {
    max-width: none;
    font-size: 1.125rem;
  }

  .result-cards {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 1.25rem;
    max-width: 32.5rem;
    margin-inline: auto;
    margin-top: 3rem;
  }

  .result-card {
    min-height: 5.75rem;
    padding: 1rem 1.5rem;
  }

  .result-action {
    margin-top: 4rem;
    padding-inline: 1.25rem;
  }

  .decor-left {
    top: 12rem;
    left: 0;
  }

  .decor-right {
    right: 0;
    bottom: 15.5rem;
    width: 4.625rem;
    height: 4.625rem;
  }

  .confetti-pattern {
    bottom: -2rem;
    width: 100vw;
  }
}

@media (max-width: 23rem) {
  .app-shell {
    width: min(100% - 1.2rem, var(--content-width));
  }

  .best-score {
    font-size: 0.9rem;
  }

  .dropdown-trigger {
    padding-inline: 0.5rem;
    font-size: 0.9rem;
  }

  .text-container {
    font-size: 1.55rem;
  }
}
