
:root {
  --bg-color: #1a1a2e;
  --primary-color: #16213e;
  --secondary-color: #0f3460;
  --accent-color: #e94560;
  --font-color: #dcdcdc;
  --tile-bg: #2c3e50;
  --tile-face: #34495e;
  --tile-shadow: #233140;
  --tile-glow: #00e5ff;
  --tile-gap: 10px; /* Increased gap */

  /* These will be set by JS */
  --board-cols: 1;
  --board-rows: 1;
}

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

html, body {
  height: 100vh; /* Crucial for vertical fitting */
  width: 100vw;
  overflow: hidden; /* Prevent all scrollbars */
  font-family: 'Poppins', sans-serif;
  background-color: var(--bg-color);
  color: var(--font-color);
}

body {
  display: flex;
  flex-direction: column;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  min-height: 0; /* Allow container to shrink */
  padding: 5px; /* Reduced padding to give more space to board */
  gap: 10px;
}

header {
  width: 100%;
  padding: 20px;
  background-color: var(--primary-color);
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
  flex-shrink: 0; /* Prevent header from shrinking */
}

header h1 {
  color: var(--accent-color);
  font-weight: 700;
  text-shadow: 0 0 5px var(--accent-color);
}

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

.stat {
  text-align: center;
}

.stat h2 {
  font-size: 0.9rem;
  font-weight: 400;
  opacity: 0.8;
}

.stat span {
  font-size: 1.5rem;
  font-weight: 600;
  color: white;
}

.controls {
  display: flex;
  gap: 15px;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  background-color: var(--accent-color);
  color: white;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s, background-color 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

.btn:hover {
  transform: scale(1.05);
  background-color: #ff5a75;
}

.btn:disabled {
  background-color: #555;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
  opacity: 0.7;
}


main {
  width: 100%;
  flex-grow: 1; /* Allow main to take up remaining space */
  min-height: 0; /* Crucial for flex children to shrink */
  display: flex; /* Use flex to help center the child */
  justify-content: center;
  /* align-items: center; <-- REMOVED to allow board to grow vertically */
}

#game-board-container {
    position: relative;
    padding: var(--tile-gap); /* Use variable for padding */
    background: var(--primary-color);
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    width: 100%;
    max-height: 100%; /* Constrain to parent height */
    overflow-y: auto; /* Allow internal scrolling if needed */
}

#effects-container {
    position: absolute;
    top: var(--tile-gap); /* Match padding */
    left: var(--tile-gap); /* Match padding */
    right: var(--tile-gap); /* Match padding */
    bottom: var(--tile-gap); /* Match padding */
    pointer-events: none;
    z-index: 5;
}

.path-line {
    position: absolute;
    background-color: var(--tile-glow);
    box-shadow: 0 0 10px 2px var(--tile-glow);
    border-radius: 3px;
    transform-origin: center;
    animation: fade-in-out 0.4s forwards;
}

@keyframes fade-in-out {
    0% { transform: scale(0); opacity: 0; }
    50% { opacity: 1; }
    100% { transform: scale(1); opacity: 0; }
}

.invalid-x {
    position: absolute;
    font-size: 2.5em;
    color: var(--accent-color);
    text-shadow: 0 0 5px rgba(0,0,0,0.5);
    animation: pop-out 0.5s forwards ease-in-out;
    user-select: none;
    transform: translate(-50%, -50%);
}

@keyframes pop-out {
    0% { transform: translate(-50%, -50%) scale(0.5); opacity: 0; }
    50% { transform: translate(-50%, -50%) scale(1.2); opacity: 1; }
    100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

#game-board {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(var(--board-cols), 1fr);
  gap: var(--tile-gap);
  perspective: 1200px;
}

.tile {
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55);
  cursor: pointer;
  border-radius: 6px;
  aspect-ratio: 1 / 1; /* Make tiles perfectly square */
}

.tile-inner {
  position: absolute;
  width: 100%;
  height: 100%;
  background: var(--tile-face);
  border-radius: 6px;
  display: flex;
  justify-content: center;
  align-items: center;
  /* Increased responsive icon size */
  font-size: clamp(1rem, 6vmin, 4rem);
  /* 3D effect */
  transform: translateZ(4px);
  box-shadow: 0 -4px 0 var(--tile-shadow);
  transition: transform 0.1s, box-shadow 0.1s, background-color 0.2s;
  /* FIX: Add a comprehensive font stack to ensure emojis render correctly on all platforms */
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji", sans-serif;
}

.tile:not(.empty):hover .tile-inner {
  transform: translateZ(6px);
  box-shadow: 0 -6px 0 var(--tile-shadow);
}

.tile.selected {
  transform: translateZ(25px) scale(1.1) rotateX(10deg) rotateY(-15deg);
}
.tile.selected .tile-inner {
    box-shadow: 0 0 30px 8px var(--tile-glow), 0 -2px 0 var(--tile-shadow);
    background: #5fa8d3;
}

.tile.matched {
  animation: match-animation 0.5s forwards ease-in-out;
}

@keyframes match-animation {
  0% { transform: scale(1) rotateY(0); opacity: 1; }
  100% { transform: scale(0) rotateY(180deg); opacity: 0; }
}

.tile.shuffling .tile-inner {
  animation: shuffle-anim 0.6s ease-in-out;
}

@keyframes shuffle-anim {
  0% { transform: translateZ(4px) scale(1) rotateY(0); }
  50% { transform: translateZ(20px) scale(0.8) rotateY(180deg); background: var(--accent-color); }
  100% { transform: translateZ(4px) scale(1) rotateY(360deg); }
}

.tile.hint .tile-inner {
  animation: hint-glow 1.2s infinite ease-in-out;
}

@keyframes hint-glow {
  0%, 100% { transform: translateZ(4px) scale(1.0); box-shadow: 0 -4px 0 var(--tile-shadow); background-color: var(--tile-face); }
  50% { transform: translateZ(10px) scale(1.05); box-shadow: 0 0 25px 5px var(--tile-glow), 0 -4px 0 var(--tile-shadow); background-color: #5fa8d3; }
}

.tile.empty {
  visibility: hidden;
  pointer-events: none;
}

.tile.win-animation {
  visibility: visible !important;
  animation: win-burst 0.5s ease-out forwards;
}

@keyframes win-burst {
  0% { transform: scale(0.5); opacity: 0; background-color: rgba(255, 255, 255, 0); }
  50% { transform: scale(1.2); opacity: 1; background-color: var(--tile-glow); box-shadow: 0 0 20px 10px var(--tile-glow); }
  100% { transform: scale(1); opacity: 0; background-color: rgba(255, 255, 255, 0); }
}

footer {
    text-align: center;
    opacity: 0.7;
    flex-shrink: 0;
}

/* Modal Styles */
.modal-overlay {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0, 0, 0, 0.7);
  justify-content: center;
  align-items: center;
  animation: fade-in 0.3s;
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
  background-color: var(--primary-color);
  padding: 30px;
  border-radius: 12px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.5);
  text-align: center;
  color: var(--font-color);
  animation: slide-up 0.4s cubic-bezier(0.165, 0.84, 0.44, 1);
}

@keyframes slide-up {
    from { transform: translateY(50px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-content h2 {
  color: var(--accent-color);
  margin-bottom: 15px;
}

.modal-content p {
  margin-bottom: 20px;
  font-size: 1.1rem;
}

.final-stats {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-bottom: 25px;
    font-size: 1.2rem;
}

/* Mobile adjustments */
@media (max-width: 768px) {
  .container {
    padding: 0;
    gap: 5px;
  }
  
  header {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 10px;
    border-radius: 0;
  }
  
  .stats {
    order: 2;
    gap: 20px;
  }
  .stat span {
    font-size: 1.3rem;
  }

  .controls {
    order: 3;
    flex-direction: column;
    width: 100%;
    align-items: center;
  }
  
  .top-controls {
    display: flex;
    justify-content: center;
    width: 100%;
    gap: 15px;
  }
  
  .top-controls .btn {
    flex: 1;
    max-width: 150px;
  }
  
  #restart-button {
    width: 100%;
    max-width: 315px;
  }

  header h1 {
    order: 1;
    font-size: 1.8rem;
  }

  .modal-content {
      padding: 20px;
  }

  footer p {
      font-size: 0.8rem;
      padding: 0 10px;
  }
}
