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

:root {
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;
}

[data-theme="classic"] {
  --bg: #eef1f7;
  --bg-elevated: #ffffff;
  --border-color: #dde3ec;
  --text-primary: #2b3245;
  --text-dim: #7c8598;
  --cell-empty: #f4f6fa;
  --cell-empty-alt: #e9edf4;
  --divider-color: rgba(43, 50, 69, 0.14);
  --accent-1: #3d7dff;
  --accent-2: #ff8a3d;
  --block-1: #4a90e2;
  --block-2: #f5a623;
  --block-3: #7ed957;
  --block-4: #ff6b6b;
  --block-5: #a374db;
}

[data-theme="violet"] {
  --bg: #0a0a0b;
  --bg-elevated: #131315;
  --border-color: #2a2a2e;
  --text-primary: #f2f2f2;
  --text-dim: #8a8a90;
  --cell-empty: #1a1a1d;
  --cell-empty-alt: #1e1e22;
  --divider-color: rgba(255, 255, 255, 0.1);
  --accent-1: #b45cff;
  --accent-2: #9dff4a;
  --block-1: #b45cff;
  --block-2: #9dff4a;
  --block-3: #ff4ab4;
  --block-4: #4affea;
  --block-5: #ffb347;
}

[data-theme="ocean"] {
  --bg: #0a0a0b;
  --bg-elevated: #131315;
  --border-color: #2a2a2e;
  --text-primary: #f2f2f2;
  --text-dim: #8a8a90;
  --cell-empty: #1a1a1d;
  --cell-empty-alt: #1e1e22;
  --divider-color: rgba(255, 255, 255, 0.1);
  --accent-1: #29b6f6;
  --accent-2: #1de9b6;
  --block-1: #29b6f6;
  --block-2: #1de9b6;
  --block-3: #ff7043;
  --block-4: #ffee58;
  --block-5: #7e57c2;
}

[data-theme="blood"] {
  --bg: #0a0a0b;
  --bg-elevated: #131315;
  --border-color: #2a2a2e;
  --text-primary: #f2f2f2;
  --text-dim: #8a8a90;
  --cell-empty: #1a1a1d;
  --cell-empty-alt: #1e1e22;
  --divider-color: rgba(255, 255, 255, 0.1);
  --accent-1: #ff3b3b;
  --accent-2: #ffb300;
  --block-1: #ff3b3b;
  --block-2: #ffb300;
  --block-3: #ff6ec7;
  --block-4: #7cff6b;
  --block-5: #3bb0ff;
}

[data-theme="acid"] {
  --bg: #0a0a0b;
  --bg-elevated: #131315;
  --border-color: #2a2a2e;
  --text-primary: #f2f2f2;
  --text-dim: #8a8a90;
  --cell-empty: #1a1a1d;
  --cell-empty-alt: #1e1e22;
  --divider-color: rgba(255, 255, 255, 0.1);
  --accent-1: #7bff3b;
  --accent-2: #ff3bd4;
  --block-1: #7bff3b;
  --block-2: #ff3bd4;
  --block-3: #3bffe0;
  --block-4: #ffe83b;
  --block-5: #ff7b3b;
}

[data-theme="vapor"] {
  --bg: #0a0a0b;
  --bg-elevated: #131315;
  --border-color: #2a2a2e;
  --text-primary: #f2f2f2;
  --text-dim: #8a8a90;
  --cell-empty: #1a1a1d;
  --cell-empty-alt: #1e1e22;
  --divider-color: rgba(255, 255, 255, 0.1);
  --accent-1: #ff7bd4;
  --accent-2: #3be0ff;
  --block-1: #ff7bd4;
  --block-2: #3be0ff;
  --block-3: #ffe83b;
  --block-4: #b47bff;
  --block-5: #7bff9e;
}

html {
  background: var(--bg);
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-mono);
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

ul, ol {
  list-style: none;
}

button {
  font-family: var(--font-mono);
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: 2px;
}

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  transition: background-color 0.4s ease;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 32px;
  border-bottom: 2px solid var(--border-color);
  flex-wrap: wrap;
  gap: 12px;
}

.logo {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 1px;
}

.logo span {
  color: var(--accent-1);
  transition: color 0.4s ease;
}

.stats {
  display: flex;
  gap: 32px;
}

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

.stat-label {
  font-size: 11px;
  color: var(--text-dim);
  letter-spacing: 1px;
}

.stat-value {
  font-size: 22px;
  font-weight: 800;
  color: var(--accent-2);
  transition: color 0.4s ease;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 4px;
  border: 2px solid var(--border-color);
  background: transparent;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  transition: border-color 0.15s ease;
}

.icon-btn:hover {
  border-color: var(--accent-2);
}

.text-btn {
  border: 2px solid var(--accent-1);
  background: transparent;
  color: var(--accent-1);
  font-family: var(--font-mono);
  font-weight: 800;
  letter-spacing: 1px;
  padding: 9px 18px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.4s ease;
}

.text-btn:hover {
  background: var(--accent-1);
  color: #0a0a0a;
}

.layout {
  flex: 1;
  display: flex;
  gap: 28px;
  padding: 32px;
  max-width: 1200px;
  margin: 0 auto;
  align-items: flex-start;
  justify-content: center;
  width: 100%;
}

.side-panel {
  background: var(--bg-elevated);
  border: 2px solid var(--border-color);
  border-radius: 8px;
  padding: 20px;
  width: 220px;
  flex-shrink: 0;
}

.side-panel h3 {
  font-size: 13px;
  letter-spacing: 1px;
  color: var(--accent-2);
  margin-bottom: 14px;
  transition: color 0.4s ease;
}

.tips-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.5;
}

.tips-list li {
  padding-left: 16px;
  position: relative;
}

.tips-list li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 7px;
  width: 6px;
  height: 6px;
  background: var(--accent-1);
  transition: background-color 0.4s ease;
}

.game-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
}

.board-wrap {
  position: relative;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 4px;
  background: var(--bg-elevated);
  border: 3px solid var(--border-color);
  border-radius: 8px;
  padding: 8px;
  width: min(85vw, 480px, 58vh);
  width: min(85vw, 480px, 58dvh);
  aspect-ratio: 1 / 1;
  touch-action: none;
}

.cell {
  background: var(--cell-empty);
  border-radius: 3px;
  position: relative;
  box-sizing: border-box;
  transition: background-color 0.15s ease;
}

.cell.cell-alt {
  background: var(--cell-empty-alt);
}

.cell.quadrant-left {
  border-left: 2px solid var(--divider-color);
}

.cell.quadrant-top {
  border-top: 2px solid var(--divider-color);
}

.cell.filled {
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.35), 0 2px 6px rgba(0, 0, 0, 0.4);
}

.cell.preview-valid {
  background: rgba(74, 222, 128, 0.4) !important;
  box-shadow: inset 0 0 0 2px #4ade80;
}

.cell.preview-invalid {
  background: rgba(248, 113, 113, 0.4) !important;
  box-shadow: inset 0 0 0 2px #f87171;
}

.cell.preview-clear-line {
  animation: clear-line-pulse 0.7s ease-in-out infinite;
  box-shadow: inset 0 0 0 3px var(--accent-2), 0 0 16px var(--accent-2);
  z-index: 3;
}

.cell.clearing {
  animation: cell-flash 0.28s ease-in-out;
  z-index: 2;
}

.cell.placed-pop {
  animation: placed-pop 0.22s ease-out;
}

@keyframes cell-flash {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(2.4); transform: scale(1.08); }
}

@keyframes clear-line-pulse {
  0%, 100% { filter: brightness(1); transform: scale(1); }
  50% { filter: brightness(1.7); transform: scale(1.05); }
}

@keyframes placed-pop {
  0% { transform: scale(0.6); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.tray {
  display: flex;
  justify-content: center;
  gap: 20px;
  min-height: 130px;
  touch-action: none;
}

.tray-slot {
  width: 130px;
  height: 130px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  border: 2px dashed var(--border-color);
  border-radius: 6px;
}

.piece {
  display: grid;
  gap: 3px;
  padding: 4px;
  cursor: grab;
  touch-action: none;
  transition: transform 0.15s ease;
}

.piece:hover {
  transform: scale(1.06);
}

.piece.dragging-source {
  visibility: hidden;
}

.piece-cell {
  width: var(--piece-cell-size, 22px);
  height: var(--piece-cell-size, 22px);
  background: transparent;
  border-radius: 3px;
}

.piece-cell.filled {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
}

.drag-ghost {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  opacity: 0.95;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.5));
  cursor: grabbing;
}

.theme-flash {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, var(--accent-1), var(--accent-2));
  opacity: 0;
  pointer-events: none;
  z-index: 900;
}

.theme-flash.flashing {
  animation: theme-flash-anim 0.9s ease-in-out;
}

@keyframes theme-flash-anim {
  0% { opacity: 0; }
  35% { opacity: 0.92; }
  100% { opacity: 0; }
}

.particle-layer, .score-popup-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 500;
}

.score-popup-layer {
  z-index: 600;
}

.particle {
  position: fixed;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  pointer-events: none;
  animation: particle-burst 0.6s ease-out forwards;
  z-index: 500;
}

@keyframes particle-burst {
  0% { transform: translate(0, 0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(0.3); opacity: 0; }
}

.score-popup {
  position: fixed;
  transform: translate(-50%, -50%);
  font-weight: 800;
  font-size: 28px;
  color: var(--accent-2);
  text-shadow: 0 0 12px var(--accent-2);
  pointer-events: none;
  animation: score-float 1s ease-out forwards;
  z-index: 600;
}

@keyframes score-float {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.6); }
  20% { opacity: 1; transform: translate(-50%, -60%) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -140%) scale(1); }
}

.combo-banner {
  position: fixed;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.5);
  font-size: 40px;
  font-weight: 800;
  color: var(--accent-1);
  text-shadow: 0 0 20px var(--accent-1), 0 4px 12px rgba(0, 0, 0, 0.6);
  opacity: 0;
  pointer-events: none;
  z-index: 700;
  letter-spacing: 2px;
  text-align: center;
  max-width: 90vw;
}

.combo-banner.show {
  animation: combo-pop 1.1s cubic-bezier(0.2, 0.8, 0.3, 1) forwards;
}

.combo-banner.big {
  color: var(--accent-2);
  text-shadow: 0 0 28px var(--accent-2), 0 4px 12px rgba(0, 0, 0, 0.6);
  font-size: 34px;
}

@keyframes combo-pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.4); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.15); }
  30% { transform: translate(-50%, -50%) scale(1); }
  75% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(0.95); }
}

@keyframes screen-shake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-6px, 4px); }
  40% { transform: translate(6px, -4px); }
  60% { transform: translate(-4px, -4px); }
  80% { transform: translate(4px, 4px); }
}

@keyframes screen-shake-strong {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-10px, 6px); }
  40% { transform: translate(10px, -6px); }
  60% { transform: translate(-8px, -8px); }
  80% { transform: translate(8px, 8px); }
}

.shake {
  animation: screen-shake 0.35s ease-in-out;
}

.shake-strong {
  animation: screen-shake-strong 0.4s ease-in-out;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 800;
  padding: 20px;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  background: var(--bg-elevated);
  border: 2px solid var(--border-color);
  border-radius: 8px;
  padding: 32px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  max-height: 88vh;
  overflow-y: auto;
}

.modal h2 {
  font-size: 24px;
  letter-spacing: 2px;
  margin-bottom: 12px;
  color: var(--accent-1);
  transition: color 0.4s ease;
}

.modal-logo {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: 1px;
  margin-bottom: 8px;
}

.modal-logo span {
  color: var(--accent-1);
  transition: color 0.4s ease;
}

.modal-tagline {
  color: var(--text-dim);
  font-size: 13px;
  letter-spacing: 1px;
  margin-bottom: 24px;
}

.modal-sub {
  color: var(--text-dim);
  font-size: 13px;
  letter-spacing: 1px;
}

.modal-tips {
  text-align: left;
  margin-bottom: 24px;
}

.final-score {
  font-size: 56px;
  font-weight: 800;
  color: var(--accent-2);
  text-shadow: 0 0 20px var(--accent-2);
  margin: 8px 0 20px;
  transition: color 0.4s ease;
}

.name-input {
  width: 100%;
  background: var(--bg);
  border: 2px solid var(--border-color);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 16px;
  padding: 12px;
  border-radius: 4px;
  text-align: center;
  letter-spacing: 1px;
  margin: 16px 0;
  text-transform: uppercase;
}

.name-input:focus {
  outline: none;
  border-color: var(--accent-2);
}

.btn-primary {
  background: var(--accent-1);
  color: #0a0a0a;
  border: none;
  font-family: var(--font-mono);
  font-weight: 800;
  font-size: 15px;
  letter-spacing: 1px;
  padding: 14px 28px;
  cursor: pointer;
  border-radius: 4px;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.4s ease;
  width: 100%;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.3);
}

.btn-primary:active {
  transform: translateY(0);
  box-shadow: none;
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-secondary {
  background: transparent;
  color: var(--text-primary);
  border: 2px solid var(--border-color);
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 1px;
  padding: 12px 20px;
  cursor: pointer;
  border-radius: 4px;
  flex: 1;
  transition: border-color 0.15s ease, color 0.15s ease;
}

.btn-secondary:hover {
  border-color: var(--accent-2);
  color: var(--accent-2);
}

.modal-row {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.leaderboard-list, .mini-leaderboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.leaderboard-list {
  margin: 16px 0;
  max-height: 50vh;
  overflow-y: auto;
}

.lb-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 10px 14px;
}

.lb-rank {
  font-weight: 800;
  width: 20px;
  color: var(--text-dim);
  flex-shrink: 0;
}

.lb-item:nth-child(1) .lb-rank { color: #ffd60a; }
.lb-item:nth-child(2) .lb-rank { color: #d1d5db; }
.lb-item:nth-child(3) .lb-rank { color: #cd7f32; }

.lb-name {
  flex: 1;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.lb-score {
  font-weight: 700;
  color: var(--accent-1);
  font-size: 13px;
  transition: color 0.4s ease;
  flex-shrink: 0;
}

.lb-empty, .lb-error {
  text-align: center;
  color: var(--text-dim);
  padding: 20px 10px;
  font-size: 12px;
  line-height: 1.5;
}

@media (max-width: 1040px) {
  .left-panel {
    display: none;
  }
}

@media (max-width: 780px) {
  .right-panel {
    display: none;
  }
  .layout {
    padding: 20px;
  }
  .topbar {
    padding: 14px 18px;
  }
  .stats {
    gap: 20px;
  }
}

@media (max-width: 480px) {
  .board {
    width: min(92vw, 400px, 58vh);
    width: min(92vw, 400px, 58dvh);
  }
  .tray {
    min-height: 108px;
  }
  .tray-slot {
    width: 96px;
    height: 96px;
  }
  .combo-banner {
    font-size: 28px;
  }
  .combo-banner.big {
    font-size: 24px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
