* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #ffffff;
  -ms-touch-action: none;
  touch-action: none;
  /* Prevent text selection */
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Screen Management */
.screen {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  overflow: hidden;
}

.screen.active {
  display: flex;
}

.overlay {
  background: rgba(0, 0, 0, 0.85);
  z-index: 1000;
  /* Allow scrolling inside overlay for small viewports */
  overflow-y: auto;
  align-items: flex-start;
  padding: 20px 0;
}

/* Loading Screen */
#loading-screen {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.loading-content {
  text-align: center;
}

.spinner {
  width: clamp(30px, 5vmin, 50px);
  height: clamp(30px, 5vmin, 50px);
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 20px auto;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Main Menu */
.menu-content {
  text-align: center;
  padding: clamp(10px, 3vmin, 30px);
  max-width: 500px;
  width: 90%;
}

.game-title {
  font-size: clamp(1.5rem, 5vmin, 3rem);
  font-weight: bold;
  margin-bottom: 8px;
  text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.subtitle {
  font-size: clamp(0.8rem, 2vmin, 1.1rem);
  margin-bottom: clamp(10px, 2vmin, 30px);
  opacity: 0.9;
}

.high-score-display {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: clamp(10px, 2vmin, 20px);
  margin: clamp(10px, 2vmin, 20px) 0;
  backdrop-filter: blur(10px);
}

.instructions {
  margin-top: clamp(10px, 2vmin, 30px);
  font-size: clamp(0.75rem, 1.8vmin, 0.9rem);
  opacity: 0.8;
}

.instructions p {
  margin: 5px 0;
}

/* Buttons */
.btn {
  padding: clamp(8px, 1.5vmin, 12px) clamp(16px, 3vmin, 30px);
  font-size: clamp(0.85rem, 2vmin, 1rem);
  font-weight: bold;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  font-family: inherit;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

.btn:active {
  transform: translateY(0);
}

.btn-primary {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.9);
  color: #333;
  margin-top: 8px;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 1);
}

.btn-large {
  padding: clamp(12px, 2vmin, 18px) clamp(30px, 5vmin, 50px);
  font-size: clamp(1rem, 2.5vmin, 1.3rem);
  margin: 8px;
}

.btn-icon {
  width: clamp(38px, 5vmin, 50px);
  height: clamp(38px, 5vmin, 50px);
  padding: 0;
  font-size: clamp(1.1rem, 2.5vmin, 1.5rem);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  flex-shrink: 0;
}

.btn-bonus {
  position: fixed;
  bottom: clamp(60px, 10vh, 100px);
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, #ffd700 0%, #ff8c00 100%);
  color: #000;
  font-size: clamp(0.85rem, 2vmin, 1.1rem);
  padding: clamp(10px, 1.5vmin, 15px) clamp(20px, 3vmin, 30px);
  animation: bounce 1s ease-in-out infinite;
  z-index: 100;
}

@keyframes bounce {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(-8px); }
}

/* Game Screen */
#game-screen {
  padding: clamp(8px, 1.5vmin, 20px);
  justify-content: flex-start;
  overflow: hidden;
}

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 600px;
  margin: 0 auto clamp(6px, 1.5vmin, 20px);
  gap: clamp(4px, 1vmin, 10px);
  flex-wrap: nowrap;
}

.stat-box {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: clamp(6px, 1.2vmin, 12px) clamp(8px, 1.5vmin, 20px);
  backdrop-filter: blur(10px);
  flex: 1;
  min-width: 0;
  text-align: center;
}

.stat-label {
  font-size: clamp(0.6rem, 1.3vmin, 0.8rem);
  opacity: 0.8;
  margin-bottom: 2px;
}

.stat-value {
  font-size: clamp(1.1rem, 3vmin, 1.8rem);
  font-weight: bold;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
}

.combo-value {
  color: #ffd700;
}

/* Game Content */
.game-content {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  overflow: hidden;
}

.message {
  font-size: clamp(1rem, 2.5vmin, 1.5rem);
  font-weight: bold;
  height: clamp(28px, 4vmin, 40px);
  margin: clamp(4px, 1vmin, 10px) 0;
  text-align: center;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: scale(0.8); }
  to { opacity: 1; transform: scale(1); }
}

/* Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(6px, 1.5vmin, 12px);
  width: 100%;
  max-width: min(450px, 90vw, 80vh);
  padding: 0 clamp(4px, 1vmin, 10px);
}

.cell {
  aspect-ratio: 1;
  font-size: clamp(1.5rem, 5vmin, 3rem);
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(10px);
  border-radius: clamp(8px, 2vmin, 15px);
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}

.cell:hover {
  transform: scale(1.1);
  background: rgba(255, 255, 255, 0.25);
}

.cell:active {
  transform: scale(0.95);
}

.cell.correct {
  animation: correct 0.5s ease;
}

.cell.wrong {
  animation: wrong 0.5s ease;
}

@keyframes correct {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); background: rgba(76, 175, 80, 0.5); }
}

@keyframes wrong {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-10px); }
  75% { transform: translateX(10px); }
}

/* Mobile Controls - shown only on touch devices via JS class */
.mobile-controls {
  display: none;
  text-align: center;
  margin-top: clamp(8px, 1.5vmin, 20px);
  opacity: 0.7;
}

.mobile-hint {
  font-size: clamp(0.75rem, 1.8vmin, 0.9rem);
}

body.is-touch .mobile-controls {
  display: block;
}

/* Modal */
.modal {
  background: rgba(255, 255, 255, 0.95);
  color: #333;
  border-radius: 20px;
  padding: clamp(20px, 4vmin, 40px) clamp(16px, 3vmin, 40px);
  max-width: 450px;
  width: 90%;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  animation: modalIn 0.3s ease;
  /* Allow scrolling if content overflows viewport */
  max-height: 90vh;
  overflow-y: auto;
  margin: auto;
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.8); }
  to { opacity: 1; transform: scale(1); }
}

.modal h2 {
  color: #764ba2;
  margin-bottom: clamp(10px, 2vmin, 20px);
  font-size: clamp(1.3rem, 4vmin, 2rem);
}

.modal-text {
  font-size: clamp(0.9rem, 2vmin, 1.1rem);
  margin: clamp(8px, 1.5vmin, 15px) 0;
}

.modal-large {
  max-width: 500px;
}

/* Keep Streak Modal */
.streak-info {
  background: rgba(255, 87, 108, 0.1);
  border-radius: 10px;
  padding: clamp(10px, 2vmin, 15px);
  margin: clamp(10px, 2vmin, 20px) 0;
}

.streak-lost {
  font-size: clamp(1rem, 2.5vmin, 1.3rem);
  font-weight: bold;
  color: #f5576c;
}

.ad-notice {
  font-size: clamp(0.75rem, 1.8vmin, 0.9rem);
  color: #666;
  margin: clamp(8px, 1.5vmin, 15px) 0;
}

/* Game Over Screen */
.final-stats {
  background: rgba(102, 126, 234, 0.1);
  border-radius: 15px;
  padding: clamp(12px, 2.5vmin, 25px);
  margin: clamp(10px, 2vmin, 20px) 0;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: clamp(6px, 1.2vmin, 12px) 0;
  font-size: clamp(0.9rem, 2vmin, 1.2rem);
}

.stat-highlight {
  font-size: clamp(1.2rem, 3vmin, 1.8rem);
  font-weight: bold;
  color: #764ba2;
}

.new-record {
  font-size: clamp(1rem, 2.5vmin, 1.5rem);
  font-weight: bold;
  color: #ffd700;
  animation: pulse 1s ease-in-out infinite;
}

.double-points-section {
  background: rgba(255, 215, 0, 0.1);
  border-radius: 15px;
  padding: clamp(10px, 2vmin, 20px);
  margin: clamp(10px, 2vmin, 20px) 0;
}

.points-preview {
  font-size: clamp(1.3rem, 3.5vmin, 2rem);
  font-weight: bold;
  margin: clamp(8px, 1.5vmin, 15px) 0;
  color: #333;
}

.points-preview .highlight {
  color: #ff8c00;
}

.game-over-actions {
  margin-top: clamp(12px, 2.5vmin, 25px);
}

/* High contrast for visibility */
.paused .grid {
  opacity: 0.5;
  pointer-events: none;
}

/* Mobile Responsive */
@media (max-width: 600px) {
  .grid {
    max-width: min(350px, 95vw);
  }
}

@media (max-width: 400px) {
  .grid {
    grid-template-columns: repeat(3, 1fr);
    max-width: min(280px, 95vw);
  }

  .game-header {
    gap: 4px;
  }

  .stat-box {
    padding: 6px 8px;
  }
}

/* Short viewport (landscape mobile / small iframe height) */
@media (max-height: 500px) {
  .game-header {
    margin-bottom: 4px;
  }

  .message {
    margin: 2px 0;
    height: 24px;
  }

  .grid {
    max-width: min(300px, 70vh, 90vw);
    gap: 4px;
  }

  .cell {
    border-radius: 8px;
  }

  .modal {
    padding: 16px;
    border-radius: 14px;
  }

  .modal h2 {
    margin-bottom: 8px;
  }

  .final-stats {
    padding: 10px;
    margin: 8px 0;
  }

  .double-points-section {
    padding: 10px;
    margin: 8px 0;
  }

  .game-over-actions {
    margin-top: 8px;
  }
}

/* Prevent zoom on double-tap (iOS) */
@media (hover: none) {
  .cell {
    touch-action: manipulation;
  }
}