/* Reset & Base */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #ffffff;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  display: flex;
  justify-content: center;
  min-height: 100vh;
  padding: 2rem;
  position: relative;
  overflow-x: hidden;
}

body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: 
    radial-gradient(circle at 20% 80%, rgba(120, 119, 198, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(255, 119, 198, 0.3) 0%, transparent 50%);
  pointer-events: none;
  z-index: -1;
}

.container {
  max-width: 1024px;
  width: 100%;
  text-align: center;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  padding: 2.5rem;
  box-shadow: 
    0 32px 64px rgba(0, 0, 0, 0.15),
    0 0 0 1px rgba(255, 255, 255, 0.2);
  color: #1a1a1a;
  position: relative;
}

/* Title & How To */
.title { 
  font-size: 2.5rem; 
  font-weight: 800;
  margin-bottom: 1.5rem;
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: none;
}

.emoji { 
  margin-right: 0.5rem;
  font-size: 1.1em;
}

.how-to-play {
  background: rgba(102, 126, 234, 0.08);
  border: 1px solid rgba(102, 126, 234, 0.2);
  border-radius: 16px;
  padding: 1.5rem;
  margin-bottom: 2rem;
  text-align: left;
  position: relative;
}

.how-to-play h2 { 
  font-size: 1.3rem; 
  font-weight: 700;
  margin-bottom: 1rem;
  color: #4c51bf;
}

.how-to-play ol { 
  list-style: none;
  counter-reset: step-counter;
}

.how-to-play li { 
  margin-bottom: 0.75rem;
  counter-increment: step-counter;
  position: relative;
  padding-left: 2rem;
  line-height: 1.5;
}

.how-to-play li::before {
  content: counter(step-counter);
  position: absolute;
  left: 0;
  top: 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 600;
}

/* Controls */
.controls { 
  margin: 1.25rem 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
  align-items: center;
  position: relative;  /* ensure on top */
  z-index: 20;
}

.controls button {
  background: linear-gradient(135deg, #667eea, #764ba2);
  border: none;
  color: #ffffff;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 0.75rem 1.5rem;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  min-width: 120px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
  z-index: 21;
}

.controls button::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.2), transparent);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.controls button:hover::before {
  opacity: 1;
}

.controls button:disabled {
  background: linear-gradient(135deg, #a0a0a0, #808080);
  cursor: not-allowed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transform: none;
}

.controls button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
}

.controls button:active:not(:disabled) {
  transform: translateY(0);
  transition: transform 0.1s ease;
}

/* 🔥 Restart button special styling */
#restartBtn {
  background: linear-gradient(135deg, #ff5f6d, #ffc371); /* red → orange */
  box-shadow: 0 4px 15px rgba(255, 95, 109, 0.4);
}

#restartBtn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(255, 95, 109, 0.6);
}

#restartBtn:disabled {
  background: linear-gradient(135deg, #a0a0a0, #808080);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: not-allowed;
}

/* Wallet status */
#walletStatus {
  display: inline-block;
  font-size: 0.9rem;
  font-weight: 500;
  background: rgba(102, 126, 234, 0.1);
  padding: 0.5rem 1rem;
  border-radius: 8px;
  border: 1px solid rgba(102, 126, 234, 0.2);
  color: #4c51bf;
  white-space: nowrap;
}

/* Timer */
.timer { 
  font-size: 1.2rem;
  font-weight: 700;
  margin: 1.5rem 0;
  color: #4c51bf;
  background: rgba(102, 126, 234, 0.1);
  padding: 1rem;
  border-radius: 12px;
  border: 1px solid rgba(102, 126, 234, 0.2);
}

#timeLeft {
  background: linear-gradient(135deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-size: 1.3em;
}

/* Game Row */
.game-row {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 24px;
  margin-bottom: 2rem;
}

/* Puzzle Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
  width: 420px;
  height: 420px;
  margin: 0;
  background: rgba(0, 0, 0, 0.05);
  padding: 8px;
  border-radius: 16px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 10; /* keep below controls */
}

.cell {
  background: #f8f9fa;
  border-radius: 8px;
  background-size: cover;
  background-position: center;
  cursor: grab;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 2px solid rgba(255, 255, 255, 0.8);
}

.cell:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  border-color: rgba(102, 126, 234, 0.5);
}

.cell:active { 
  cursor: grabbing;
  transform: scale(0.98);
}

/* Preview */
.preview h3 {
  margin-bottom: 1rem;
  font-size: 1.1rem;
  font-weight: 600;
  color: #4c51bf;
}

.preview img {
  width: 100%;
  max-width: 420px;
  border: 3px solid transparent;
  border-radius: 16px;
  background: linear-gradient(white, white) padding-box,
              linear-gradient(135deg, #667eea, #764ba2) border-box;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s ease;
}

.preview img:hover {
  transform: scale(1.02);
}

/* Responsive */
@media (max-width: 900px) {
  .game-row {
    flex-direction: column;
    align-items: center;
  }
}

@media (max-width: 600px) {
  body { padding: 1rem; }
  .container { padding: 1.5rem; border-radius: 20px; margin: 0; }
  .title { font-size: 2rem; }
  .controls { flex-direction: column; gap: 0.5rem; }
  .controls button { font-size: 0.9rem; padding: 0.65rem 1.25rem; width: 100%; max-width: 200px; }
  .how-to-play { padding: 1.25rem; }
  .grid { width: 300px; height: 300px; gap: 4px; padding: 6px; }
  .preview img { max-width: 300px; }
}

@media (max-width: 400px) {
  .container { padding: 1rem; }
  .title { font-size: 1.8rem; }
  .grid { width: 280px; height: 280px; }
  .preview img { max-width: 280px; }
}

/* ==== Stability patches & lock styling ==== */
body, .grid, .cell { user-select: none; -webkit-user-select: none; }
body, .grid { -webkit-user-drag: none; }
.grid { will-change: contents; }
.cell { will-change: transform; }

.cell.dragging {
  transform: none !important;
  transition: box-shadow 0.2s ease, border-color 0.2s ease !important;
  opacity: 0.9;
  cursor: grabbing;
  box-shadow: 0 0 0 2px rgba(102,126,234,.25), 0 2px 8px rgba(0,0,0,.15);
}
.cell.dragging:hover,
.cell.dragging:active { transform: none !important; }

.cell.locked {
  cursor: default !important;
  box-shadow: 0 0 0 2px rgba(76,81,191,.18), 0 2px 8px rgba(0,0,0,.10);
}

@media (prefers-reduced-motion: reduce) {
  .cell, .cell:hover, .cell:active {
    transition: none !important;
    transform: none !important;
  }
}

/* ✅ Preview integrity fix: the preview image must never be sliced like the grid */
.preview img {
  display: block;
  object-fit: contain;
  width: 100%;
  height: auto;
  background: none !important;
  background-size: auto !important;
  background-position: center !important;
}
