/* === Global Defaults === */
html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  min-height: 100vh;
  min-width: 100vw;
  background: linear-gradient(135deg, #1a1a1a, #000000);
  font-family: 'Courier New', monospace;
  color: #eee;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
  /* REMOVE position: fixed & display:flex here! Let the wrapper do layout. */
}

/* === Arcade Shell === */
#arcade-shell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
}

/* === Insert Coin Button === */
#tetris-toggle {
  font-family: 'Courier New', monospace;
  font-size: 2.5rem;
  padding: 24px 48px;
  touch-action: manipulation;
  background: #00ffff;
  color: #000;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 5000;
  box-shadow: 0 0 12px #0ff;
  transition: all 0.3s ease;
}

#tetris-toggle:hover {
  background: #0ff;
  box-shadow: 0 0 16px #0ff;
}

/* === Game Wrapper === */
#tetris-wrapper {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  background: #111;
  width: 100vw;
  height: 100vh;
  min-height: 100vh;
  min-width: 100vw;
  padding: 0;
  box-sizing: border-box;
  overflow: hidden;
}

/* === Layout === */
#game-area {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  width: 100vw;
  max-width: 100vw;
  flex: 1;
  max-height: 100vh;
  overflow: hidden;
  box-sizing: border-box;
  padding: 0;
}

/* === Canvas & Container === */
#tetris-container {
  width: 100%;
  max-width: 90vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  box-sizing: border-box;
  padding: 0;
  border: 4px ridge #0ff;
  background: repeating-linear-gradient(0deg, #000, #000 2px, #111 2px, #111 4px);
  box-shadow: 0 0 20px #0ff inset;
  margin: 0 auto;
}

/* ======= CRITICAL: Fixes for canvas stretching/auto-sizing issues ======= */
#tetris {
  display: block;
  margin: 0 auto;
  width: 100%;
  height: auto;
  aspect-ratio: 10 / 20;
  max-height: 65vh;
  max-width: 100%;
  background: rgba(0, 0, 0, 0.85);
  border: 4px double #0ff;
  image-rendering: pixelated;
  box-shadow: 0 0 10px #0f0, 0 0 40px #0ff;
  /* Prevent resizing bugs on load/OS window resize */
  min-width: 200px;
  min-height: 400px;
}

/* === Preview Box === */
#preview-box {
  width: 16vw;
  height: 16vw;
  max-width: 90px;
  max-height: 90px;
  aspect-ratio: 1 / 1;
  border: 2px dashed #0ff;
  margin-bottom: 6px;
  background: rgba(0, 0, 0, 0.5);
  image-rendering: pixelated;
}

/* === Scoreboard === */
#scoreboard {
  margin-top: 6px;
  font-size: 1rem;
  text-align: center;
  color: #00ffcc;
  background: rgba(0, 0, 0, 0.6);
  padding: 4px 6px;
  border: 1px solid #00ffcc;
  border-radius: 6px;
  text-shadow: 1px 1px 2px black;
  width: 100%;
  max-width: 300px;
}

#score, #level, #lines, #highScore {
  font-size: 12px;
  color: #0f0;
  text-shadow: 0 0 2px #0f0;
}

/* === Touch Controls === */
#touch-controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 10px;
  gap: 10px;
  width: 100%;
  max-width: 360px;
}

#touch-controls button {
  flex: 1 1 40%;
  font-size: 2rem;
  padding: 14px;
  border: none;
  border-radius: 6px;
  background: #222;
  color: #fff;
  cursor: pointer;
}

#touch-controls button:hover {
  background: #00ffff;
  color: #000;
}

/* === Overlay === */
#ready-overlay {
  font-size: 4rem;
  pointer-events: none;
  z-index: 10000;
}

#crt-flash {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: white;
  opacity: 0;
  z-index: 9999;
  pointer-events: none;
}

@keyframes crt-flash {
  0%   { opacity: 1; }
  30%  { opacity: 0.4; }
  60%  { opacity: 0.9; }
  100% { opacity: 0; }
}

@keyframes flashPulse {
  0% { filter: brightness(1.5) saturate(1.5); }
  50% { filter: brightness(2) saturate(2); }
  100% { filter: brightness(1.5) saturate(1.5); }
}

.tetris-flash {
  animation: screenFlash 0.2s ease-in-out alternate 2;
}

@keyframes screenFlash {
  from { background-color: #fff; }
  to { background-color: #000; }
}

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

#insert-coin {
  animation: blink 1s infinite;
}

/* === Responsive Tweaks === */
@media (max-width: 600px) {
  #tetris-container {
    max-width: 98vw;
    max-height: 99vh;
  }

  #tetris {
    max-height: 60vh;
    min-width: 120px;
    min-height: 200px;
  }

  #preview-box {
    width: 22vw;
    height: 22vw;
    max-width: 75px;
    max-height: 75px;
    margin-bottom: 4px;
  }

  #scoreboard {
    font-size: 0.9rem;
    padding: 3px 4px;
    max-width: 240px;
  }

  #game-area {
    gap: 8px;
  }
}

/* === Background Screensaver === */
#tepris-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -9999;
  background: linear-gradient(270deg, #0f0c29, #302b63, #24243e);
  background-size: 800% 800%;
  animation: bgScreensaver 30s ease infinite;
  filter: brightness(0.7) blur(3px) saturate(1.2);
  pointer-events: none;
}

@keyframes bgScreensaver {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* === Pause Menu Overlay === */
#pause-menu {
  display: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #000c;
  color: #0f0;
  padding: 24px 28px 20px 28px;
  border: 2.5px solid #0ff;
  border-radius: 14px;
  font-family: 'Press Start 2P', monospace;
  z-index: 10000;
  min-width: 320px;
  box-shadow: 0 0 20px #0ff3, 0 2px 18px #001;
}

#pause-menu button {
  display: block;
  width: 100%;
  margin: 12px 0;
  padding: 12px 0;
  font-size: 1.08rem;
  background: #00161b;
  color: #0ff;
  border: 2px solid #0ff7;
  border-radius: 6px;
  outline: none;
  cursor: pointer;
  letter-spacing: 0.06em;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
  box-shadow: 0 0 7px #0ff3 inset;
}
#pause-menu button:hover,
#pause-menu button.selected,
#pause-menu button:focus {
  background: #0ff;
  color: #00161b;
  box-shadow: 0 0 16px #0ff, 0 0 2px #0ff inset;
  border-color: #fff;
  outline: 2px solid #fff;
}

#pause-menu #controls-layout {
  margin-top: 20px;
  background: rgba(20,32,32,0.96);
  padding: 14px 18px 12px 18px;
  border-radius: 10px;
  border: 1.8px solid #0ff4;
  color: #fff;
  font-size: 1rem;
  text-align: left;
  box-shadow: 0 0 12px #0ff2;
}

#pause-menu .controls-title {
  text-align: center;
  font-weight: bold;
  color: #0ff;
  margin-bottom: 10px;
  font-size: 1.22rem;
  letter-spacing: 0.13em;
}
#pause-menu .controls-row {
  margin-bottom: 5px;
  font-size: 1.02em;
  letter-spacing: 0.04em;
  line-height: 1.25;
}
#pause-menu hr {
  border: none;
  border-top: 2px solid #088;
  margin: 12px 0 15px 0;
}