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

:root {
  --red: #e74c3c;
  --red-bright: #ff6b6b;
  --red-dim: #7a2020;
  --blue: #3498db;
  --blue-bright: #5dade2;
  --blue-dim: #1a4a6e;
  --green: #2ecc71;
  --green-bright: #58d68d;
  --green-dim: #1a6e3a;
  --yellow: #f1c40f;
  --yellow-bright: #f7dc6f;
  --yellow-dim: #7d6508;
  --orange: #e67e22;
  --orange-bright: #f0a04b;
  --orange-dim: #7a4210;
  --purple: #9b59b6;
  --purple-bright: #bb77d1;
  --purple-dim: #52306a;
  --cyan: #1abc9c;
  --cyan-bright: #48d6b8;
  --cyan-dim: #0e6652;
  --pink: #e84393;
  --pink-bright: #f06daf;
  --pink-dim: #7a224d;
  --white-btn: #bdc3c7;
  --white-btn-bright: #ecf0f1;
  --white-btn-dim: #5d6266;
  --bg: #1a1a2e;
  --bg-light: #16213e;
  --text: #eaeaea;
  --text-muted: #8a8a9a;
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Näkymät */
.screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  padding: 1.5rem;
}

.screen.hidden {
  display: none;
}

/* Aloitusnäkymä */
.title {
  font-size: 2.5rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--red-bright), var(--yellow-bright));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  font-size: 1.1rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.instructions {
  margin: 2rem 0;
  text-align: center;
  line-height: 1.6;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.action-btn {
  background: linear-gradient(135deg, var(--red), var(--blue));
  color: white;
  border: none;
  padding: 1rem 3rem;
  font-size: 1.2rem;
  font-weight: 700;
  border-radius: 50px;
  cursor: pointer;
  transition: transform 0.15s, opacity 0.15s;
  min-height: 54px;
  touch-action: manipulation;
}

.action-btn:active {
  transform: scale(0.96);
  opacity: 0.9;
}

.action-btn.secondary {
  background: transparent;
  border: 2px solid var(--text-muted);
  color: var(--text-muted);
  margin-top: 0.75rem;
  font-size: 1rem;
  padding: 0.75rem 2rem;
}

.action-btn.secondary:active {
  border-color: var(--text);
  color: var(--text);
}

/* Top-tulokset */
.highscores {
  margin-top: 2rem;
  width: 100%;
  max-width: 300px;
}

.highscores h2 {
  font-size: 1rem;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 0.75rem;
}

#score-list {
  list-style: none;
  counter-reset: score-counter;
}

#score-list li {
  counter-increment: score-counter;
  display: flex;
  align-items: center;
  padding: 0.4rem 0;
  font-size: 0.95rem;
  color: var(--text-muted);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

#score-list li::before {
  content: counter(score-counter) ".";
  width: 2rem;
  font-weight: 700;
  color: var(--text);
}

#score-list li .score-entry-points {
  margin-left: auto;
  font-weight: 700;
  color: var(--text);
}

/* Pelinäkymä */
#game-screen {
  padding: 0.75rem;
  justify-content: flex-start;
  gap: 0.75rem;
}

.game-header {
  display: flex;
  justify-content: space-between;
  width: 100%;
  padding: 0.5rem 0.25rem;
}

.score-display, .speed-display, .level-display {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.score-label, .speed-label, .level-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.score-value, .speed-value, .level-value {
  font-size: 2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.level-value {
  font-size: 1.5rem;
  color: var(--yellow-bright);
}

.speed-value {
  font-size: 1.2rem;
  color: var(--text-muted);
}

/* Nappigrid */
.game-grid {
  display: grid;
  gap: 0.75rem;
  width: 100%;
  flex: 1;
  max-width: 500px;
  max-height: 70dvh;
}

.game-grid.cols-2 {
  grid-template-columns: 1fr 1fr;
}

.game-grid.cols-3 {
  grid-template-columns: 1fr 1fr 1fr;
}

.game-btn {
  border: none;
  border-radius: 1rem;
  cursor: pointer;
  touch-action: manipulation;
  transition: filter 0.1s, transform 0.1s, box-shadow 0.15s;
  position: relative;
  min-height: 80px;
}

.game-grid.cols-3 .game-btn {
  min-height: 60px;
}

.game-btn:active {
  transform: scale(0.97);
}

/* Nappien värit - sammutettu tila */
.game-btn[data-index="0"] { background: var(--red-dim); }
.game-btn[data-index="1"] { background: var(--blue-dim); }
.game-btn[data-index="2"] { background: var(--green-dim); }
.game-btn[data-index="3"] { background: var(--yellow-dim); }
.game-btn[data-index="4"] { background: var(--orange-dim); }
.game-btn[data-index="5"] { background: var(--purple-dim); }
.game-btn[data-index="6"] { background: var(--cyan-dim); }
.game-btn[data-index="7"] { background: var(--pink-dim); }
.game-btn[data-index="8"] { background: var(--white-btn-dim); }

/* Syttynyt tila */
.game-btn[data-index="0"].lit {
  background: var(--red-bright);
  box-shadow: 0 0 30px var(--red-bright), 0 0 60px rgba(231, 76, 60, 0.4);
  animation: pulse-red 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="1"].lit {
  background: var(--blue-bright);
  box-shadow: 0 0 30px var(--blue-bright), 0 0 60px rgba(52, 152, 219, 0.4);
  animation: pulse-blue 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="2"].lit {
  background: var(--green-bright);
  box-shadow: 0 0 30px var(--green-bright), 0 0 60px rgba(46, 204, 113, 0.4);
  animation: pulse-green 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="3"].lit {
  background: var(--yellow-bright);
  box-shadow: 0 0 30px var(--yellow-bright), 0 0 60px rgba(241, 196, 15, 0.4);
  animation: pulse-yellow 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="4"].lit {
  background: var(--orange-bright);
  box-shadow: 0 0 30px var(--orange-bright), 0 0 60px rgba(230, 126, 34, 0.4);
  animation: pulse-orange 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="5"].lit {
  background: var(--purple-bright);
  box-shadow: 0 0 30px var(--purple-bright), 0 0 60px rgba(155, 89, 182, 0.4);
  animation: pulse-purple 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="6"].lit {
  background: var(--cyan-bright);
  box-shadow: 0 0 30px var(--cyan-bright), 0 0 60px rgba(26, 188, 156, 0.4);
  animation: pulse-cyan 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="7"].lit {
  background: var(--pink-bright);
  box-shadow: 0 0 30px var(--pink-bright), 0 0 60px rgba(232, 67, 147, 0.4);
  animation: pulse-pink 0.4s ease-in-out infinite alternate;
}

.game-btn[data-index="8"].lit {
  background: var(--white-btn-bright);
  box-shadow: 0 0 30px var(--white-btn-bright), 0 0 60px rgba(236, 240, 241, 0.4);
  animation: pulse-white 0.4s ease-in-out infinite alternate;
}

/* Painettu-oikein efekti */
.game-btn.correct {
  transform: scale(0.95);
  filter: brightness(1.3);
}

/* Väärä painallus */
.game-btn.wrong {
  animation: shake 0.3s ease-in-out;
}

@keyframes pulse-red {
  to { box-shadow: 0 0 40px var(--red-bright), 0 0 80px rgba(231, 76, 60, 0.5); }
}
@keyframes pulse-blue {
  to { box-shadow: 0 0 40px var(--blue-bright), 0 0 80px rgba(52, 152, 219, 0.5); }
}
@keyframes pulse-green {
  to { box-shadow: 0 0 40px var(--green-bright), 0 0 80px rgba(46, 204, 113, 0.5); }
}
@keyframes pulse-yellow {
  to { box-shadow: 0 0 40px var(--yellow-bright), 0 0 80px rgba(241, 196, 15, 0.5); }
}
@keyframes pulse-orange {
  to { box-shadow: 0 0 40px var(--orange-bright), 0 0 80px rgba(230, 126, 34, 0.5); }
}
@keyframes pulse-purple {
  to { box-shadow: 0 0 40px var(--purple-bright), 0 0 80px rgba(155, 89, 182, 0.5); }
}
@keyframes pulse-cyan {
  to { box-shadow: 0 0 40px var(--cyan-bright), 0 0 80px rgba(26, 188, 156, 0.5); }
}
@keyframes pulse-pink {
  to { box-shadow: 0 0 40px var(--pink-bright), 0 0 80px rgba(232, 67, 147, 0.5); }
}
@keyframes pulse-white {
  to { box-shadow: 0 0 40px var(--white-btn-bright), 0 0 80px rgba(236, 240, 241, 0.5); }
}

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

/* Tasonvaihto-overlay */
.level-up-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26, 26, 46, 0.9);
  z-index: 100;
}

.level-up-overlay.hidden {
  display: none;
}

.level-up-content {
  text-align: center;
  animation: level-up-pop 0.5s ease-out;
}

.level-up-label {
  font-size: 1.2rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.level-up-number {
  font-size: 5rem;
  font-weight: 900;
  background: linear-gradient(135deg, var(--yellow-bright), var(--orange-bright));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  line-height: 1.1;
}

.level-up-name {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--yellow-bright);
  margin-top: 0.5rem;
}

@keyframes level-up-pop {
  0% { transform: scale(0.3); opacity: 0; }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

/* Game Over -näkymä */
.gameover-title {
  font-size: 2.2rem;
  font-weight: 800;
  margin-bottom: 1.5rem;
}

.gameover-stats {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  justify-content: center;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.final-level {
  color: var(--yellow-bright);
}

.new-record {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--yellow-bright);
  margin-bottom: 1.5rem;
  animation: record-glow 0.6s ease-in-out infinite alternate;
}

.new-record.hidden {
  display: none;
}

@keyframes record-glow {
  from { opacity: 0.7; transform: scale(1); }
  to { opacity: 1; transform: scale(1.05); }
}

/* Countdown overlay */
.countdown-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26, 26, 46, 0.85);
  z-index: 100;
}

.countdown-number {
  font-size: 6rem;
  font-weight: 900;
  color: var(--text);
  animation: countdown-pop 0.5s ease-out;
}

@keyframes countdown-pop {
  from { transform: scale(2); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* Responsiivisuus */
@media (min-width: 500px) {
  .game-grid {
    max-width: 420px;
    margin: 0 auto;
  }
}

@media (max-height: 600px) {
  .game-header {
    padding: 0.25rem;
  }
  .score-value {
    font-size: 1.5rem;
  }
  .level-value {
    font-size: 1.2rem;
  }
}
