/* --- ROOT VARIABLES & GENERAL STYLING --- */
:root {
  /* Fonts */
  --font-body: 'Montserrat', sans-serif;
  --font-display: 'Fredoka One', cursive;

  /* Colors - These will be overridden by theme system */
  --bg-image: url('https://raw.githubusercontent.com/arpit456jain/Amazing-Js-Projects/master/Candy%20Crush/utils/bg.png');
  --panel-bg: var(--panel-bg, rgba(255, 255, 255, 0.5));
  --grid-bg: var(--grid-bg, rgba(255, 255, 255, 0.4));
  --text-color: var(--text-color, #5d4037);
  --score-color: var(--score-color, #d81b60);

  /* Theme-specific colors - set by config.js */
  --grid-border-color: var(--grid-border-color, #ffa726);
  --tile-shadow: var(--tile-shadow, 0 2px 4px rgba(0, 0, 0, 0.1));
  --tile-hover-shadow: var(--tile-hover-shadow, 0 4px 12px rgba(0, 0, 0, 0.2));
  --drag-color: var(--drag-color, rgba(0, 0, 0, 0.4));
  --swap-color: var(--swap-color, #4caf50);
  --invalid-color: var(--invalid-color, #e74c3c);
  --clearing-color: var(--clearing-color, #ff6b9d);
  --glow-color: var(--glow-color, #ffa726);
  --shadow-color: var(--shadow-color, rgba(0, 0, 0, 0.2));
  --backdrop-color: var(--backdrop-color, rgba(0, 0, 0, 0.3));

  /* Buttons */
  --btn-primary-bg: var(--btn-primary-bg, linear-gradient(145deg, #81d4fa, #4fc3f7));
  --btn-secondary-bg: linear-gradient(145deg, #ff8a65, #ff7043);
  --btn-text-color: var(--btn-text-color, #ffffff);
  --btn-shadow: var(--btn-shadow, 0 4px 6px rgba(0, 0, 0, 0.1));
  --btn-hover-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

body {
  background-image: var(--bg-image);
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  /* Keeps background still on scroll */
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  font-family: var(--font-body);
  color: var(--text-color);
  margin: 0;
  padding: 16px;
  box-sizing: border-box;
}

/* --- GAME LAYOUT & CONTAINERS --- */
.game-container {
  display: none;
  /* Hidden by default, shown via JS */
  flex-direction: column;
  align-items: center;
  gap: 20px;
  width: 100%;
  max-width: 600px;
  /* Max width for large screens */
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--board-width, 8), 1fr);
  gap: 6px;
  width: 100%;
  max-width: 560px;
  height: 560px;
  /* Fixed height to prevent size changes */
  aspect-ratio: 1 / 1;
  background-color: var(--grid-bg);
  padding: 8px;
  border-radius: 16px;
  box-shadow: 0 8px 16px var(--shadow-color), inset 0 2px 4px var(--shadow-color);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  /* Safari support */
  border: 1px solid var(--grid-border-color);
  box-sizing: border-box;
  overflow: hidden;
  /* Prevent content from affecting grid size */
}

.grid div {
  width: 100%;
  height: 100%;
  /* Fixed height to prevent size changes */
  aspect-ratio: 1 / 1;
  background-size: cover;
  background-position: center;
  border-radius: 8px;
  transition: transform 0.2s ease, opacity 0.2s ease, box-shadow 0.2s ease;
  touch-action: none;
  cursor: grab;
  box-shadow: var(--tile-shadow);
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  position: relative;
  /* Ensure proper positioning during animations */
  overflow: hidden;
  /* Prevent content overflow */
}

.grid div:active {
  cursor: grabbing;
}

/* --- SCOREBOARD & CONTROLS --- */
.scoreBoard {
  display: none;
  /* Hidden by default, shown via JS */
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 560px;
  background-color: var(--panel-bg);
  border-radius: 16px;
  padding: 12px 20px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  text-align: center;
  flex-wrap: wrap;
  /* Allows controls to wrap on small screens */
  gap: 16px;
  box-sizing: border-box;
}

.score-display {
  line-height: 1;
}

h3 {
  font-family: var(--font-body);
  font-weight: 600;
  text-transform: uppercase;
  font-size: 16px;
  margin: 0 0 4px 0;
}

h1 {
  font-family: var(--font-display);
  font-size: 36px;
  color: var(--score-color);
  margin: 0;
  -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);
}

#timer {
  font-family: var(--font-display);
  font-size: 24px;
  background: rgba(0, 0, 0, 0.05);
  padding: 4px 12px;
  border-radius: 8px;
}

.controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

/* --- BUTTONS --- */
button {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 14px;
  padding: 10px 16px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  color: var(--btn-text-color);
  background: var(--btn-primary-bg);
  box-shadow: var(--btn-shadow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

button:hover {
  transform: translateY(-2px);
  box-shadow: var(--btn-hover-shadow);
}

button:active {
  transform: translateY(1px);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15);
}

#changeMode,
#modalChangeMode {
  background: var(--btn-secondary-bg);
}

/* --- TILE INTERACTION EFFECTS --- */
.grid div:hover {
  transform: scale(1.08);
  box-shadow: var(--tile-hover-shadow);
}

.tile--dragging {
  transform: scale(1.15) rotate(5deg);
  box-shadow: 0 8px 20px var(--drag-color);
  cursor: grabbing;
  z-index: 1000;
  transition: none;
  opacity: 0.9;
}

.tile--swap-animation {
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.tile--invalid-move {
  animation: shake 0.4s ease-in-out;
}

.tile--clearing {
  animation: pop 0.3s ease-out forwards;
}

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

  50% {
    transform: scale(1.3);
    opacity: 0.7;
  }

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

@keyframes shake {

  0%,
  100% {
    transform: translateX(0);
  }

  10%,
  30%,
  50%,
  70%,
  90% {
    transform: translateX(-3px);
  }

  20%,
  40%,
  60%,
  80% {
    transform: translateX(3px);
  }
}

/* Smooth swap animation */
.tile--swapping {
  transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 500;
}

/* Enhanced visual feedback */
.tile--swap-animation {
  transform: scale(1.05);
  box-shadow: 0 4px 12px var(--swap-color);
  border: 2px solid var(--swap-color);
}

/* Prevent text selection during drag */
.grid {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Smooth transitions for all interactive elements */
.grid div {
  will-change: transform, box-shadow, opacity;
  transform-origin: center;
  /* Ensure animations scale from center */
  contain: layout style;
  /* Prevent layout shifts during animations */
}

/* --- MODALS & OVERLAYS --- */
#modeSelection {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: absolute;
  inset: 0;
  background-color: var(--backdrop-color);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 10;
  text-align: center;
  animation: fadeIn 0.5s ease;
}

#modeSelection h2,
.modal h2 {
  font-family: var(--font-display);
  font-size: 40px;
  color: white;
  margin-bottom: 24px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

#modeSelection button {
  min-width: 200px;
  font-size: 20px;
  padding: 16px 24px;
  margin: 10px;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  animation: fadeIn 0.3s ease;
}

.modal__content {
  background: var(--panel-bg);
  border-radius: 16px;
  padding: 24px 32px;
  min-width: 280px;
  color: var(--text-color);
  text-align: center;
  box-shadow: 0 8px 24px var(--modal-shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--grid-border-color);
}

.modal p {
  font-size: 18px;
  margin: 0 0 20px 0;
}

.modal p span {
  font-family: var(--font-display);
  color: var(--score-color);
  font-size: 24px;
}

.modal__actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

/* --- RESPONSIVE DESIGN --- */
@media (max-width: 600px) {
  body {
    padding: 0;
    touch-action: manipulation;
    /* Optimize touch interactions */
  }

  .game-container {
    gap: 0;
    max-width: 100%;
  }

  .scoreBoard {
    border-radius: 0;
    padding: 10px 16px;
    justify-content: space-around;
    /* Better spacing on mobile */
  }

  h1 {
    font-size: 32px;
  }

  h3 {
    font-size: 14px;
  }

  #timer {
    font-size: 20px;
  }

  .grid {
    border-radius: 0;
    width: 100vw;
    height: 100vw;
    /* Maintain square aspect ratio on mobile */
    max-height: 100vh;
    /* Prevent overflow on very small screens */
    padding: 6px;
    gap: 4px;
    touch-action: none;
    /* Prevent scrolling during drag */
  }

  .grid div {
    border-radius: 6px;
    /* Enhanced touch feedback for mobile */
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
  }

  /* Larger touch targets on mobile */
  .grid div:hover {
    transform: scale(1.12);
  }

  /* Optimize animations for mobile performance */
  .tile--dragging {
    transform: scale(1.2) rotate(3deg);
  }
}