:root {
  --bg: #eafaf1;
  --primary: #ff6b6b;
  --secondary: #4ecdc4;
  --accent: #ffd93d;
  --text: #26333f;
  --card: #ffffff;
  --track: #dff6ff;
  font-size: 18px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Comic Sans MS", "Trebuchet MS", "Segoe UI", sans-serif;
  background: linear-gradient(180deg, var(--bg), #d4f1ea);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
}

.container {
  width: 100%;
  max-width: 720px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
}

h1 {
  font-size: clamp(1.8rem, 6vw, 3rem);
  color: var(--primary);
  text-align: center;
  margin: 0.5rem 0;
  text-shadow: 2px 2px 0 var(--accent);
}

h2 {
  font-size: clamp(1.3rem, 4vw, 2rem);
  color: var(--secondary);
  margin: 0;
}

.card {
  background: var(--card);
  border-radius: 20px;
  padding: 1.5rem;
  width: 100%;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.08);
}

.btn {
  font-family: inherit;
  font-size: 1.2rem;
  font-weight: bold;
  padding: 0.9rem 1.4rem;
  border: none;
  border-radius: 16px;
  background: var(--primary);
  color: white;
  cursor: pointer;
  box-shadow: 0 5px 0 #c0392b;
  transition: transform 0.05s ease;
  touch-action: manipulation;
  min-height: 48px;
}

.btn:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #c0392b;
}

.btn.secondary {
  background: var(--secondary);
  box-shadow: 0 5px 0 #2e8b84;
}

.btn.secondary:active {
  box-shadow: 0 1px 0 #2e8b84;
}

.btn.ghost {
  background: white;
  color: var(--text);
  border: 3px solid var(--secondary);
  box-shadow: none;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
  width: 100%;
}

.difficulty-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
  width: 100%;
}

.difficulty-card {
  border: 3px solid transparent;
  border-radius: 16px;
  padding: 1rem;
  background: var(--track);
  cursor: pointer;
  text-align: center;
  font-weight: bold;
  font-size: 1.1rem;
}

.difficulty-card.selected {
  border-color: var(--primary);
  background: #ffe3e3;
}

input[type="text"] {
  font-family: inherit;
  font-size: 1.2rem;
  padding: 0.75rem 1rem;
  border-radius: 14px;
  border: 3px solid var(--secondary);
  width: 100%;
  min-height: 48px;
}

.track {
  position: relative;
  width: 100%;
  height: 90px;
  background: var(--track);
  border-radius: 16px;
  overflow: hidden;
  border: 3px dashed var(--secondary);
}

.racer {
  position: absolute;
  top: 50%;
  left: 0;
  transform: translate(0, -50%);
  font-size: 2.5rem;
  transition: left 0.25s ease-out;
}

.finish-line {
  position: absolute;
  right: 6px;
  top: 0;
  bottom: 0;
  width: 6px;
  background: repeating-linear-gradient(45deg, #000 0 6px, #fff 6px 12px);
}

.word-display {
  font-size: clamp(1.6rem, 6vw, 2.5rem);
  font-weight: bold;
  text-align: center;
  padding: 0.5rem;
  border-radius: 14px;
  background: #fff7e0;
  letter-spacing: 1px;
}

.word-display .typed {
  color: var(--secondary);
}

.word-display .typed-wrong {
  color: #c0392b;
  text-decoration: underline;
}

.word-display.shake {
  animation: shake 0.3s ease;
  background: #ffe0e0;
}

.word-display.correct {
  background: #d4f7d4;
}

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

.stats-row {
  display: flex;
  gap: 1rem;
  justify-content: center;
  flex-wrap: wrap;
  font-weight: bold;
}

.stat {
  background: var(--track);
  padding: 0.5rem 1rem;
  border-radius: 12px;
}

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

th, td {
  padding: 0.6rem;
  text-align: left;
  border-bottom: 2px solid var(--track);
}

th {
  color: var(--primary);
}

.msg {
  text-align: center;
  font-weight: bold;
}

.msg.error {
  color: #c0392b;
}

.msg.success {
  color: #2e8b84;
}

@media (max-width: 480px) {
  :root { font-size: 16px; }
  .racer { font-size: 2rem; }
}

.certificate {
  background: #fffdf5;
  border: 10px solid var(--accent);
  outline: 4px solid var(--primary);
  outline-offset: -20px;
  border-radius: 20px;
  padding: 2.5rem 1.5rem;
  text-align: center;
  width: 100%;
}

.certificate .medal {
  font-size: clamp(3rem, 12vw, 5rem);
}

.certificate h1 {
  margin: 0.25rem 0;
}

.certificate .cert-name {
  font-size: clamp(1.6rem, 6vw, 2.4rem);
  color: var(--primary);
  font-weight: bold;
  margin: 0.75rem 0;
}

.certificate .cert-line {
  font-size: 1.1rem;
  margin: 0.25rem 0;
}

.certificate .cert-stats {
  margin: 1rem 0;
}

.certificate .cert-date {
  margin-top: 1rem;
  font-style: italic;
  color: #6b7c8c;
}

@media print {
  @page {
    size: landscape;
    margin: 0;
  }
  html, body {
    margin: 0;
    padding: 0;
  }
  body * {
    visibility: hidden;
  }
  .certificate, .certificate * {
    visibility: visible;
  }
  .certificate {
    visibility: visible;
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    margin: 0;
    padding: 2vh 4vw;
    border-radius: 0;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: none;
  }
  .certificate .medal {
    font-size: 12vh;
  }
  .certificate h1 {
    font-size: 5vh;
  }
  .certificate .cert-line {
    font-size: 2.2vh;
  }
  .certificate .cert-name {
    font-size: 6vh;
  }
  .certificate .cert-stats .stat {
    font-size: 2.6vh;
    padding: 1.5vh 2vw;
  }
  .certificate .cert-date {
    font-size: 2vh;
  }
}
