:root {
  --bg-gradient: linear-gradient(135deg, #a8e6cf 0%, #dcedc1 100%);
  --panel: #ffffff;
  --ink: #5d4037;
  --accent: #ff8b94;
  --shadow-color: rgba(0, 0, 0, 0.15);
  --shadow: 0 8px 0 rgba(0, 0, 0, 0.1), 0 20px 20px rgba(0, 0, 0, 0.1);
  --border-radius: 24px;
  --font-main: "Fredoka", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-main);
  color: var(--ink);
  background: var(--bg-gradient);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Prevent body scroll by default on desktop */
}

/* Layout Containers */
.game-container {
  display: flex;
  flex-direction: row;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.puzzle-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  /* Stack canvas and gallery */
  align-items: center;
  justify-content: flex-start;
  /* Start from top */
  padding: 8px;
  position: relative;
  background-image:
    radial-gradient(#fff 15%, transparent 16%),
    radial-gradient(#fff 15%, transparent 16%);
  background-size: 60px 60px;
  background-position: 0 0, 30px 30px;
  background-color: rgba(255, 255, 255, 0.1);
  overflow-y: auto;
  /* Allow scrolling if gallery overflows */
}

#gallery-section {
  width: 100%;
  max-width: 600px;
  margin-top: 20px;
  background: rgba(255, 255, 255, 0.8);
  padding: 15px;
  border-radius: 20px;
  border: 4px solid #fff;
  box-shadow: var(--shadow);
  text-align: center;
}

#gallery-section h3 {
  margin: 0 0 10px 0;
  color: var(--ink);
  font-size: 1.2rem;
}

#gallery-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.gallery-item {
  width: 100%;
  aspect-ratio: 1/1;
  background-color: #ddd;
  border-radius: 10px;
  overflow: hidden;
  border: 2px solid #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.gallery-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: grayscale(1) brightness(0.5) contrast(0.8);
  opacity: 0.6;
  transition: all 0.5s;
}

.gallery-item.unlocked {
  border-color: #4cd137;
  transform: scale(1.05);
  box-shadow: 0 5px 15px rgba(76, 209, 55, 0.3);
}

.gallery-item.unlocked img {
  filter: none;
  opacity: 1;
}

.sidebar {
  width: 350px;
  min-width: 350px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  padding: 30px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
  box-shadow: -5px 0 30px rgba(0, 0, 0, 0.05);
  z-index: 100;
  overflow-y: auto;
  overflow-x: hidden;
  border-left: 2px solid rgba(255, 255, 255, 0.5);
}

.left-sidebar {
  border-left: none;
  border-right: 2px solid rgba(255, 255, 255, 0.5);
  box-shadow: 5px 0 30px rgba(0, 0, 0, 0.05);
}

#gallery-sidebar h3 {
  margin: 0;
  color: #ff6f61;
  font-size: 1.5rem;
  text-align: center;
}

#gallery-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  /* 2 cols for sidebar */
  gap: 10px;
  width: 100%;
}

.gallery-item {
  width: 100%;
  aspect-ratio: 1/1;
  background-color: #eee;
  border-radius: 12px;
  overflow: hidden;
  border: 4px solid #fff;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  position: relative;
}

.gallery-item::after {
  content: "🔒";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.5rem;
  opacity: 0.5;
}

.gallery-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: grayscale(1) brightness(0.5);
  opacity: 0.3;
  transition: all 0.5s;
}

.gallery-item.unlocked::after {
  display: none;
}

.gallery-item.unlocked {
  border-color: #ffd700;
  box-shadow: 0 0 15px rgba(255, 215, 0, 0.4);
  transform: scale(1.05);
}

.gallery-item.unlocked img {
  filter: none;
  opacity: 1;
}

/* Canvas Sizing */
canvas {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1/1;
  border-radius: var(--border-radius);
  background: #fff;
  box-shadow: var(--shadow);
  border: 6px solid #fff;
  object-fit: contain;
}

/* UI Elements */
h1 {
  margin: 0;
  font-size: clamp(24px, 3vw, 32px);
  color: #ff6f61;
  text-shadow: 2px 2px 0 #fff;
  letter-spacing: 1px;
  font-weight: 700;
  text-align: center;
  margin-bottom: 5px;
}

.level-badge {
  position: relative;
  display: flex;
  /* Use flex for centering */
  align-items: center;
  justify-content: center;
  width: 317px;
  height: 130px;
  margin-bottom: 5px;
  transition: transform 0.2s;
}

.level-badge:hover {
  transform: scale(1.05);
}

.level-badge .badge-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.1));
}

#level-label {
  position: relative;
  z-index: 2;
  font-weight: 800;
  font-size: 1.6rem;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  margin-top: -2px;
  /* Visual optical adjustment */
}

/* Victory Overlay */
.victory-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 50;
  /* Optional: Adds a subtle backdrop to separate text from busy puzzle */
  backdrop-filter: blur(2px) contrast(1.1);
  border-radius: 20px;
  padding: 20px;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  /* Default none */
}

.victory-overlay.show {
  opacity: 1;
  pointer-events: auto;
  /* Enable clicks when shown */
}

/* Pause Overlay */
.paused-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 5rem;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
  pointer-events: none;
  z-index: 55;
  opacity: 1;
  transition: opacity 0.3s;
}

.paused-overlay.hidden {
  opacity: 0;
}

.badge-star {
  width: 250px;
  height: 250px;
  opacity: 0;
  transform: scale(0.1) rotate(-180deg);
  transition: all 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  /* Stronger 3D shadow effect */
  filter: drop-shadow(0 20px 30px rgba(0, 0, 0, 0.5)) drop-shadow(0 0 10px rgba(255, 255, 200, 0.8));
  z-index: 60;
  cursor: pointer;
  /* Clickable */
}

.badge-star:hover {
  transform: scale(1.1) rotate(5deg) !important;
  /* Pulse on hover */
}

.victory-text {
  font-size: 4rem;
  font-weight: 900;
  color: #ffca28;
  /* Deep 3D text shadow */
  text-shadow:
    0 5px 0 #d35400,
    0 10px 0 #a04000,
    0 15px 20px rgba(0, 0, 0, 0.6),
    0 0 30px rgba(255, 200, 0, 0.8);
  margin-top: 10px;
  opacity: 0;
  transform: translateY(20px) scale(0.9);
  transition: all 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55) 0.2s;
  z-index: 61;
  position: relative;
}

/* Animation State */
.victory-overlay.show .badge-star {
  opacity: 1;
  transform: scale(1) rotate(0deg);
}

.victory-overlay.show .victory-text {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.status-panel {
  width: 100%;
  background: var(--panel);
  padding: 16px;
  border-radius: var(--border-radius);
  box-shadow:
    0 10px 20px rgba(0, 0, 0, 0.05),
    inset 0 -4px 0 rgba(0, 0, 0, 0.05);
  text-align: center;
  border: 4px solid #fff;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#score {
  font-weight: 700;
  font-size: 1.6rem;
  color: #ffaa5e;
  text-shadow: 2px 2px 0 #fff;
}

#message {
  font-size: 0.95rem;
  color: #8d6e63;
  line-height: 1.3;
}

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  width: 100%;
}

.icon-btn {
  border: none;
  padding: 0;
  border-radius: 50%;
  background: var(--panel);
  box-shadow: 0 8px 0 #eee, 0 15px 20px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  display: flex;
  align-items: center;
  justify-content: center;
  height: 56px;
  width: 56px;
  border: 4px solid #fff;
}

.icon-btn:hover {
  transform: translateY(-4px) scale(1.1);
  box-shadow: 0 12px 0 #eee, 0 20px 25px rgba(0, 0, 0, 0.1);
}

.icon-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #eee, 0 5px 10px rgba(0, 0, 0, 0.1);
}

.icon-btn img {
  width: 65%;
  height: 65%;
  object-fit: contain;
  pointer-events: none;
}

.rotate img {
  transform: rotate(180deg);
}

/* Sidebar Button */
.btn-sidebar {
  background: linear-gradient(to bottom, #4cd137, #44bd32);
  border: 4px solid #fff;
  padding: 12px 24px;
  border-radius: 50px;
  font-family: var(--font-main);
  font-size: 1.5rem;
  color: #fff;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 5px 0 #2f8c25, 0 10px 10px rgba(0, 0, 0, 0.1);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
  transition: all 0.2s;
  width: 100%;
  margin-top: 10px;
}

.btn-sidebar:hover:not(:disabled) {
  transform: scale(1.05) translateY(-2px);
  filter: brightness(1.1);
}

.btn-sidebar:active:not(:disabled) {
  transform: scale(0.95) translateY(2px);
  box-shadow: 0 0 0 #2f8c25, 0 5px 5px rgba(0, 0, 0, 0.2);
}

.btn-sidebar:disabled {
  background: #bdbdbd;
  box-shadow: 0 5px 0 #9e9e9e;
  cursor: not-allowed;
  opacity: 0.7;
  transform: none;
  filter: grayscale(1);
}

.btn-sidebar:enabled {
  animation: pulse-green 2s infinite;
}

@keyframes pulse-green {
  0% {
    box-shadow: 0 0 0 0 rgba(76, 209, 55, 0.7);
  }

  70% {
    box-shadow: 0 0 0 10px rgba(76, 209, 55, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(76, 209, 55, 0);
  }
}

.btn-primary {
  background: linear-gradient(to bottom, #4cd137, #44bd32);
  border: 4px solid #fff;
  padding: 20px 60px;
  border-radius: 50px;
  font-family: var(--font-main);
  font-size: 2.5rem;
  color: #fff;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 10px 0 #2f8c25, 0 20px 20px rgba(0, 0, 0, 0.2);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
  transition: transform 0.1s;
}

.btn-primary:hover {
  transform: scale(1.05) translateY(-5px);
  filter: brightness(1.1);
}

.btn-primary:active {
  transform: scale(0.95) translateY(5px);
  box-shadow: 0 0 0 #2f8c25, 0 5px 10px rgba(0, 0, 0, 0.2);
}

@keyframes bounce-in {
  0% {
    transform: scale(0.3);
    opacity: 0;
  }

  50% {
    transform: scale(1.1);
    opacity: 1;
  }

  70% {
    transform: scale(0.95);
  }

  100% {
    transform: scale(1);
  }
}

.foot {
  margin-top: auto;
  opacity: 0.6;
  font-size: 0.8rem;
  text-align: center;
}

/* Animations */
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

@keyframes pop {
  0% {
    transform: scale(0);
    opacity: 0;
  }

  80% {
    transform: scale(1.1);
    opacity: 1;
  }

  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes particle-fire {
  0% {
    transform: translate(0, 0) scale(1);
    opacity: 1;
  }

  100% {
    transform: translate(var(--dx), var(--dy)) scale(0);
    opacity: 0;
  }
}

/* Overlays */
.screen-overlay {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  background: var(--bg-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  transition: opacity 0.5s;
}

.screen-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.loader {
  font-size: 2rem;
  font-weight: bold;
  color: #fff;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.2);
  animation: bounce-in 1s infinite alternate;
}

.start-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
  animation: pop 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.portada-img {
  width: min(90vw, 400px);
  /* Responsive max size */
  height: auto;
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  border: 8px solid #fff;
  transform: rotate(-2deg);
}

.btn-large {
  font-size: 3rem;
  padding: 24px 80px;
  box-shadow: 0 15px 0 #2f8c25, 0 25px 30px rgba(0, 0, 0, 0.25);
  animation: bounce-in 2s infinite;
  box-shadow: 0 15px 0 #2f8c25, 0 25px 30px rgba(0, 0, 0, 0.25);
  animation: bounce-in 2s infinite;
}

/* Image Viewer Modal */
.modal-content {
  position: relative;
  background: rgba(255, 255, 255, 0.95);
  padding: 20px;
  border-radius: 20px;
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
  max-width: 90vw;
  max-height: 90vh;
  animation: bounce-in 0.5s;
}

.rules-content {
  max-width: 500px;
  text-align: left;
  padding: 30px;
}

.rules-content h2 {
  color: #ff6f61;
  margin-top: 0;
  text-align: center;
  font-size: 2rem;
}

.rules-list {
  list-style: none;
  padding: 0;
  font-size: 1.2rem;
  line-height: 1.6;
  color: #5d4037;
}

.rules-list li {
  margin-bottom: 12px;
  padding-left: 24px;
  position: relative;
}

.rules-list li::before {
  content: "•";
  color: #ff6f61;
  font-weight: bold;
  position: absolute;
  left: 0;
}

#modal-img {
  max-width: 100%;
  max-height: 60vh;
  border-radius: 10px;
  border: 4px solid #fff;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.btn-close {
  position: absolute;
  top: -15px;
  right: -15px;
  background: #ff6f61;
  color: white;
  border: 4px solid #fff;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  z-index: 10;
}

.btn-close:hover {
  transform: scale(1.1);
  background: #e74c3c;
}

.btn-secondary {
  background: linear-gradient(to bottom, #3498db, #2980b9);
  color: white;
  padding: 12px 30px;
  border-radius: 30px;
  text-decoration: none;
  font-weight: bold;
  border: 3px solid #fff;
  box-shadow: 0 5px 0 #1f618d, 0 10px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.2s;
  font-family: var(--font-main);
  font-size: 1.2rem;
}

.btn-secondary:hover {
  transform: translateY(-2px);
  filter: brightness(1.1);
}

.btn-secondary:active {
  transform: translateY(4px);
  box-shadow: 0 0 0 #1f618d;
}

/* Particles */
.particle {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 60;
  /* Above star */
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
  animation: particle-fire 1s forwards ease-out;
}

/* Responsive adjustments */
@media (max-width: 1000px) {
  .game-container {
    flex-direction: column;
    align-items: center;
    gap: 20px;
  }

  /* 1. Menu Top */
  .sidebar:not(.left-sidebar) {
    order: 1;
    width: 90%;
    max-width: 500px;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    height: auto;
    padding: 10px;
    margin-bottom: 10px;
  }

  .sidebar h1 {
    width: 100%;
    margin-bottom: 5px;
    font-size: 1.8rem;
  }

  .controls {
    flex-direction: row;
    justify-content: center;
    width: 100%;
  }

  /* 2. Puzzle Middle */
  .puzzle-area {
    order: 2;
    width: 100%;
    margin: 0;
  }

  canvas {
    max-width: 90vw;
    max-height: 90vw;
  }

  /* 3. Gallery Bottom */
  .left-sidebar {
    order: 3;
    width: 90%;
    margin-right: 0;
  }
}

/* Ranking Styles */
.ranking-container {
  background: rgba(255, 255, 255, 0.9);
  padding: 15px;
  border-radius: 15px;
  width: 90%;
  max-width: 400px;
  max-height: 200px;
  overflow-y: auto;
  border: 4px solid #ffca28;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.ranking-container h3 {
  margin: 0 0 10px 0;
  color: #d35400;
  text-align: center;
  font-size: 1.5rem;
  border-bottom: 2px solid #ffe082;
  padding-bottom: 5px;
}

.ranking-list {
  font-size: 0.9rem;
  width: 100%;
}

.ranking-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 0;
  border-bottom: 1px dotted #ccc;
}

.ranking-row:nth-child(1) {
  color: #d35400;
  font-weight: bold;
}

.ranking-row:nth-child(2) {
  color: #7f8c8d;
  font-weight: bold;
}

.ranking-row:nth-child(3) {
  color: #a1887f;
  font-weight: bold;
}

/* Input Styles */
.input-name {
  padding: 15px;
  font-size: 1.5rem;
  border-radius: 10px;
  border: 4px solid #4cd137;
  text-align: center;
  font-family: var(--font-main);
  width: 100%;
  max-width: 300px;
  outline: none;
  background: #fff;
  transition: all 0.3s;
}

.input-name:focus {
  transform: scale(1.05);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

/* Timer Styles */
.timer-display {
  font-family: monospace;
  font-size: 2rem;
  font-weight: bold;
  color: #2c3e50;
  background: #ecf0f1;
  padding: 5px 15px;
  border-radius: 8px;
  border: 2px solid #bdc3c7;
  margin: 5px 0;
}



#gallery-grid {
  display: flex;
  /* Horizontal row or grid */
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.gallery-item {
  width: 70px;
  height: 70px;
}

.victory-text {
  font-size: 3rem;
}


.player-label {
  font-size: 1.5rem;
  color: #fff;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  margin-bottom: 5px;
  background: rgba(255, 111, 97, 0.9);
  padding: 5px 20px;
  border-radius: 20px;
  border: 2px solid #fff;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
  animation: pop 0.5s;
}

@media (max-width: 900px) {

  /* ... (keep existing media queries) ... */
  body {
    overflow: auto;
  }

  .game-container {
    flex-direction: column;
    width: 100%;
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }

  .sidebar {
    width: 100%;
    min-width: auto;
    order: 2;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    /* Center items on mobile too */
    padding: 20px;
    gap: 16px;
    border-left: none;
    border-top: 2px solid rgba(255, 255, 255, 0.5);
    overflow: visible;
  }

  .level-badge {
    margin-right: auto;
    margin-left: auto;
  }

  .puzzle-area {
    min-height: 60vh;
    padding: 10px;
    overflow: visible;
  }

  canvas {
    max-height: 80vh;
  }

  .controls {
    order: 2;
    width: 100%;
  }

  .status-panel {
    order: 3;
    width: 100%;
  }

  h1 {
    width: 100%;
    flex-basis: 100%;
  }
}