:root {
  --bg-color: #0f1115;
  --panel-bg: #1e2128;
  --empty-cell: #2a2d36;
  --empty-border: #383c47;
  
  --block-blank: #b45309; /* Matte orange/brown */
  
  --yellow: #facc15;
  --green: #4ade80;
  --pink: #f472b6;
}

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: var(--bg-color);
  font-family: system-ui, -apple-system, sans-serif;
  color: #fff;
}

.game-container {
  background: var(--panel-bg);
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 16px 32px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 32px;
  width: 100%;
  max-width: 550px;
  box-sizing: border-box;
}

/* Header & Counters */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
  width: 100%;
  box-sizing: border-box;
}

.counters {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
}

.counter {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0, 0, 0, 0.4);
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 15px;
}

.gem-icon {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: inline-block;
}

.gem-icon.green { background: var(--green); box-shadow: 0 0 10px var(--green); }
.gem-icon.pink { background: var(--pink); box-shadow: 0 0 10px var(--pink); }
.gem-icon.yellow { background: var(--yellow); box-shadow: 0 0 10px var(--yellow); }

.header-buttons {
  display: flex;
  gap: 12px;
}

#undo-btn, #reset-btn {
  background: #333;
  color: #fff;
  border: 1px solid #444;
  padding: 8px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.2s;
}

#undo-btn:hover {
  background: #444;
}

#reset-btn:hover {
  background: rgba(220, 38, 38, 0.4);
  border-color: rgba(239, 68, 68, 0.8);
  box-shadow: 0 0 10px rgba(239, 68, 68, 0.4);
}

/* 8x8 Board */
.board {
  display: inline-grid;
  margin: 0 auto;
  grid-template-columns: repeat(8, min(10vw, 44px));
  gap: min(1.5vw, 6px);
  background: rgba(0, 0, 0, 0.5);
  padding: 12px;
  border-radius: 12px;
  box-shadow: inset 0 4px 12px rgba(0,0,0,0.5);
  box-sizing: border-box;
}

.cell {
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
}

/* Empty Cell Styling */
.empty {
  background: var(--empty-cell);
  border: 1px solid var(--empty-border);
}

/* Matte Blank Block */
.block-blank {
  background: var(--block-blank);
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.1), inset 0 -2px 4px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(0,0,0,0.5);
}

/* Vibrant Gem Blocks */
.gem-yellow {
  background: var(--yellow);
  box-shadow: 0 0 12px rgba(250, 204, 21, 0.6), inset 0 2px 6px rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255,255,255,0.4);
}

.gem-green {
  background: var(--green);
  box-shadow: 0 0 12px rgba(74, 222, 128, 0.6), inset 0 2px 6px rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255,255,255,0.4);
}

.gem-pink {
  background: var(--pink);
  box-shadow: 0 0 12px rgba(244, 114, 182, 0.6), inset 0 2px 6px rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255,255,255,0.4);
}

/* Tray Area */
.tray {
  display: flex;
  justify-content: center;
  gap: min(4vw, 20px);
  width: 100%;
  margin: 0 auto;
}

.tray-slot {
  width: 25vw;
  max-width: 120px;
  height: 25vw;
  max-height: 120px;
  aspect-ratio: 1 / 1;
  background: rgba(0, 0, 0, 0.2);
  border: 2px dashed rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

/* Rendered Pieces in Tray */
.piece {
  display: flex;
  flex-direction: column;
  gap: 3px;
  touch-action: none; /* Disable native swipe/scroll gestures for custom draggable elements */
}

.piece-row {
  display: flex;
  gap: 3px;
}

.piece-cell {
  width: 5vw;
  max-width: 24px;
  height: 5vw;
  max-height: 24px;
  border-radius: 4px;
}

/* Modal Styles */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--panel-bg);
  padding: 32px;
  border-radius: 16px;
  border: 1px solid var(--empty-border);
  box-shadow: 0 10px 40px rgba(0,0,0,0.8);
  display: flex;
  flex-direction: column;
  gap: 24px;
  align-items: center;
  max-width: 90vw;
}

.modal-content h2 {
  margin: 0;
  font-size: 20px;
}

.builder-grid {
  display: grid;
  grid-template-columns: repeat(5, 44px);
  grid-template-rows: repeat(5, 44px);
  gap: 6px;
  background: var(--bg-dark);
  padding: 15px;
  border-radius: 12px;
  margin: 15px auto;
  border: 1px solid #333;
}

.builder-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 10px;
}

#save-piece-btn {
  background: var(--green);
  color: #111;
  border: none;
  padding: 10px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: bold;
  transition: transform 0.2s, box-shadow 0.2s;
}

#save-piece-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 0 15px var(--green);
}

.customize-btn {
  background: #333;
  color: #fff;
  border: 1px solid #444;
  padding: 5px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: bold;
  margin-top: auto;
  transition: all 0.2s;
}

.customize-btn:hover {
  background: #444;
}

.tray-slot-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#close-modal-btn {
  background: #333;
  color: #fff;
  border: none;
  padding: 10px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: bold;
  transition: background 0.2s;
}

#close-modal-btn:hover {
  background: #444;
}

/* Drag & Drop Visuals */
.is-dragging {
  position: fixed !important;
  z-index: 2000;
  pointer-events: none;
  transform: scale(1.05);
  box-shadow: 0 12px 30px rgba(0,0,0,0.8);
  cursor: grabbing;
}

.shadow-valid {
  background: rgba(255, 255, 255, 0.1) !important;
  box-shadow: inset 0 0 10px rgba(255, 255, 255, 0.3) !important;
  border-color: rgba(255, 255, 255, 0.6) !important;
  transition: all 0.1s;
}

.shadow-invalid {
  background: rgba(239, 68, 68, 0.2) !important;
  box-shadow: inset 0 0 10px rgba(239, 68, 68, 0.4) !important;
  border-color: rgba(239, 68, 68, 0.8) !important;
  transition: all 0.1s;
}

.is-snapping {
  transition: all 0.3s ease-out !important;
}

.clear-preview {
  background: rgba(255, 255, 255, 0.4) !important;
  box-shadow: 0 0 15px rgba(255, 255, 255, 0.8), inset 0 0 10px rgba(255, 255, 255, 1) !important;
  border-color: rgba(255, 255, 255, 1) !important;
  animation: pulse-clear 1s infinite alternate;
  z-index: 10;
  position: relative;
}

@keyframes pulse-clear {
  from { opacity: 0.8; }
  to { opacity: 1; transform: scale(1.05); }
}

/* Victory Screen Overlay */
#victory-screen {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 5000;
  backdrop-filter: blur(5px);
}

#victory-screen.hidden {
  display: none;
}

.victory-content {
  background: var(--panel-bg);
  padding: 40px 60px;
  border-radius: 16px;
  border: 1px solid var(--empty-border);
  box-shadow: 0 10px 40px rgba(0,0,0,0.9);
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: center;
}

.victory-content h2 {
  margin: 0;
  color: var(--green);
  font-size: 2.2rem;
}

.victory-content p {
  font-size: 1.2rem;
  color: #ccc;
  margin: 0;
}

#play-again-btn {
  background: var(--green);
  color: #111;
  border: none;
  padding: 12px 30px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: bold;
  font-size: 1.1rem;
  transition: transform 0.2s, box-shadow 0.2s;
  margin-top: 10px;
}

#play-again-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 0 15px var(--green);
}

/* Mobile Responsiveness Fine-Tuning */
@media (max-width: 550px) {
  .game-container {
    padding: 16px;
    gap: 20px;
  }
  
  .header-buttons {
    margin: 0 auto;
  }
  
  .header {
    justify-content: center;
  }
}
