:root {
  --primary: #6366f1;
  --primary-glow: #818cf8;
  --bg: #0a0e1a;
  --surface: #141928;
  --surface-light: #1e293b;
  --text: #e2e8f0;
  --text-dim: #64748b;
  --border: #2d3a52;
  --cyan: #06b6d4;
  --yellow: #eab308;
  --purple: #a855f7;
  --green: #22c55e;
  --red: #ef4444;
  --orange: #f97316;
  --blue: #3b82f6;
  --cell-size: 28px;
}

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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

#game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 16px;
  width: 100%;
  max-width: 600px;
}

header {
  text-align: center;
}

h1 {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.3em;
  background: linear-gradient(135deg, var(--cyan), var(--primary), var(--purple));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: none;
  filter: drop-shadow(0 0 20px rgba(99, 102, 241, 0.3));
}

#game-layout {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: center;
}

#left-panel,
#right-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 110px;
}

.panel-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
}

.panel-box h3 {
  font-size: 0.7rem;
  letter-spacing: 0.15em;
  color: var(--text-dim);
  margin-bottom: 8px;
  text-transform: uppercase;
}

.panel-box canvas {
  display: block;
  margin: 0 auto;
  background: transparent;
}

#stats-box {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  font-size: 0.65rem;
  letter-spacing: 0.15em;
  color: var(--text-dim);
  text-transform: uppercase;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

#board-wrapper {
  position: relative;
  border: 2px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
  box-shadow:
    0 0 30px rgba(99, 102, 241, 0.08),
    inset 0 0 30px rgba(0, 0, 0, 0.3);
  line-height: 0;
}

#game-canvas {
  display: block;
}

#overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 14, 26, 0.88);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  backdrop-filter: blur(4px);
  animation: fadeIn 0.3s ease;
}

#overlay.hidden {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

#overlay-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

#overlay-title {
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  background: linear-gradient(135deg, var(--cyan), var(--primary));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#overlay-message {
  font-size: 0.95rem;
  color: var(--text-dim);
  max-width: 220px;
  line-height: 1.5;
}

#overlay-btn {
  padding: 10px 36px;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--primary), var(--purple));
  color: white;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 4px 15px rgba(99, 102, 241, 0.3);
}

#overlay-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
}

#overlay-btn:active {
  transform: translateY(0);
}

#controls {
  display: flex;
  gap: 10px;
  justify-content: center;
}

#controls button {
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
}

#controls button:hover {
  background: var(--surface-light);
  border-color: var(--primary);
}

#controls button:active {
  background: var(--primary);
}

#touch-controls {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

#touch-row-top,
#touch-row-mid,
#touch-row-bot {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.touch-btn {
  width: 56px;
  height: 56px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.3rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.touch-btn:active {
  background: var(--primary);
  border-color: var(--primary-glow);
}

.touch-btn.wide {
  width: 180px;
  font-size: 1rem;
  letter-spacing: 0.15em;
}

footer {
  text-align: center;
}

.controls-hint {
  font-size: 0.72rem;
  color: var(--text-dim);
  line-height: 1.8;
}

kbd {
  display: inline-block;
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  font-family: inherit;
  font-size: 0.7rem;
  color: var(--text);
  vertical-align: middle;
}

/* Line clear flash */
@keyframes lineClear {
  0% { background: rgba(255, 255, 255, 0.8); }
  100% { background: transparent; }
}

/* Score popup */
@keyframes scorePopup {
  0% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-40px) scale(1.3); }
}

.score-popup {
  position: absolute;
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--yellow);
  pointer-events: none;
  animation: scorePopup 0.8s ease-out forwards;
  z-index: 5;
  text-shadow: 0 0 10px rgba(234, 179, 8, 0.5);
}

/* Level up flash */
@keyframes levelUp {
  0% { box-shadow: 0 0 30px rgba(99, 102, 241, 0.08), inset 0 0 30px rgba(0, 0, 0, 0.3), 0 0 60px rgba(99, 102, 241, 0.6); }
  100% { box-shadow: 0 0 30px rgba(99, 102, 241, 0.08), inset 0 0 30px rgba(0, 0, 0, 0.3); }
}

#board-wrapper.level-up {
  animation: levelUp 0.6s ease-out;
}

/* Responsive: mobile */
@media (max-width: 520px) {
  :root {
    --cell-size: 22px;
  }

  #game-container {
    padding: 10px;
    gap: 8px;
  }

  h1 {
    font-size: 1.4rem;
  }

  #left-panel,
  #right-panel {
    min-width: 80px;
  }

  .panel-box {
    padding: 6px;
  }

  .stat-value {
    font-size: 1rem;
  }

  .panel-box canvas {
    width: 70px !important;
    height: 70px !important;
  }

  #next-box canvas {
    height: 210px !important;
  }

  #touch-controls {
    display: flex;
  }

  footer {
    display: none;
  }
}

@media (max-width: 400px) {
  :root {
    --cell-size: 18px;
  }

  #game-layout {
    gap: 6px;
  }

  #left-panel,
  #right-panel {
    min-width: 60px;
  }

  .panel-box h3 {
    font-size: 0.6rem;
  }

  .stat-value {
    font-size: 0.85rem;
  }

  .touch-btn {
    width: 48px;
    height: 48px;
    font-size: 1.1rem;
  }

  .touch-btn.wide {
    width: 156px;
  }
}

/* Hover-capable devices only */
@media (hover: none) {
  #touch-controls {
    display: flex;
  }

  footer {
    display: none;
  }
}

@media (min-width: 521px) and (hover: hover) {
  #touch-controls {
    display: none;
  }
}
