/* ===== THEME VARIABLES ===== */

:root {
  --bg-color: #ffffff;
  --text-color: #333333;
  --text-color-rgb: 51, 51, 51;

  --grid-bg: #f5f5f5;
  --grid-border: #dddddd;

  --tile-back: #4a6fa5;
  --tile-back-dark: #223555;
  --tile-hover: #5c7fbd;
  --tile-border: #2f4a7c;

  --button-bg: #4a6fa5;
  --button-hover: #3a5a8c;

  --status-bg: #f9f9f9;
  --status-border: #e0e0e0;

  --shape-bg: #f3f6ff;
  --shape-border: #d0daf5;
  --shape-selected: #ffd54f;

  --completion-bg: #ffffff;
  --completion-active-bg: #f8f8f8;
  --box-shadow: rgba(0, 0, 0, 0.15);

  /* Approximate per-row vertical step: tile size + gap */
  --tile-step: 72px;
}

.dark-mode {
  --bg-color: #121212;
  --text-color: #e0e0e0;
  --text-color-rgb: 224, 224, 224;

  --grid-bg: #1e1e1e;
  --grid-border: #444444;

  --tile-back: #2c3e70;
  --tile-back-dark: #1b2747;
  --tile-hover: #3b5690;
  --tile-border: #163059;

  --button-bg: #3b5c9e;
  --button-hover: #314e87;

  --status-bg: #1a1a1a;
  --status-border: #333333;

  --shape-bg: #1e2436;
  --shape-border: #303b57;
  --shape-selected: #ffca28;

  --completion-bg: #1a1a1a;
  --completion-active-bg: #252525;
  --box-shadow: rgba(0, 0, 0, 0.4);
}

/* ===== BASE LAYOUT ===== */

body {
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  text-align: center;
  padding: 1.5em;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: background-color 0.3s ease, color 0.3s ease;
}

#puzzle-pairs-game {
  max-width: 700px;
  width: 100%;
}

h1 {
  margin-top: 0.2em;
  margin-bottom: 0.5em;
}

/* ===== MODE SELECTOR ===== */

#mode-selector {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 10px;
}

.mode-button {
  background-color: transparent;
  color: var(--text-color);
  border: 1px solid var(--status-border);
  box-shadow: none;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 500;
}

.mode-button:hover {
  background-color: var(--status-bg);
  transform: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.mode-button.active {
  background-color: var(--button-bg);
  color: #ffffff;
  border-color: var(--button-bg);
}

/* ===== BUTTONS ===== */

button {
  background-color: var(--button-bg);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
  font-family: inherit;
  letter-spacing: 0.5px;
}

button:hover {
  background-color: var(--button-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

button:active {
  transform: translateY(0);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

/* Dark mode + mute top buttons */

#mode-toggle,
#mute-button {
  position: fixed;
  top: 12px;
  padding: 6px 10px;
  font-size: 16px;
  z-index: 50;
}

#mode-toggle {
  right: 12px;
  border-radius: 50%;
  background-color: transparent;
  box-shadow: 0 2px 5px var(--box-shadow);
}

#mode-toggle:hover {
  transform: rotate(30deg);
}

#mute-button {
  left: 12px;
}

/* ===== HUD ===== */

#hud {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px 16px;
  margin-bottom: 10px;
  font-size: 14px;
}

#timer {
  font-weight: 700;
}

.stat span {
  font-weight: 700;
}

/* ===== GRID & TILES ===== */

#grid-container {
  position: relative;
  margin: 0 auto;
  display: inline-block;
}

#tile-grid {
  background-color: var(--grid-bg);
  border-radius: 10px;
  padding: 10px;
  border: 1px solid var(--grid-border);
  display: grid;
  grid-template-columns: repeat(5, 64px);
  grid-template-rows: repeat(5, 64px);
  gap: 8px;
  transform-origin: center center;
  /* Smooth rotation of the whole grid */
  transition: transform 0.9s ease-in-out;
}

/* Rotation animation classes */

#tile-grid.rotate-cw {
  transform: rotate(90deg);
}

#tile-grid.rotate-ccw {
  transform: rotate(-90deg);
}

.tile {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  background-color: var(--tile-back);
  border: 2px solid var(--tile-border);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
  transition: background-color 0.2s ease, transform 0.15s ease,
    box-shadow 0.15s ease, opacity 0.3s ease, top 0.15s ease;
  user-select: none;
}

.tile.hidden:hover {
  background-color: var(--tile-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.35);
}

.tile.revealed {
  background-color: #ffffff;
  color: #222222;
}

.dark-mode .tile.revealed {
  background-color: #2a2a2a;
  color: #f5f5f5;
}

.tile .icon {
  font-size: 28px;
}

.tile.hidden .icon {
  opacity: 0;
}

.tile.revealed .icon {
  opacity: 1;
}

.tile.matched {
  opacity: 0;
  pointer-events: none;
}

/* Empty cells (no tile present) – gives visible gaps for gravity */

.tile.empty {
  background-color: transparent;
  border: none;
  box-shadow: none;
  cursor: default;
}

.tile.empty:hover {
  background-color: transparent;
  transform: none;
  box-shadow: none;
}

/* Tile preview highlight when dragging scanners */

.tile.preview {
  outline: 3px solid rgba(255, 215, 0, 0.8);
  outline-offset: -3px;
}

/* Shake for invalid placement or errors */

@keyframes shake {
  0% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-4px);
  }
  40% {
    transform: translateX(4px);
  }
  60% {
    transform: translateX(-3px);
  }
  80% {
    transform: translateX(3px);
  }
  100% {
    transform: translateX(0);
  }
}

.shake {
  animation: shake 0.3s ease;
}

/* Smooth falling animation for gravity */

@keyframes tile-fall {
  from {
    transform: translateY(
      calc(-1 * var(--fall-distance, 1) * var(--tile-step))
    );
  }
  to {
    transform: translateY(0);
  }
}

.tile.falling-down {
  animation: tile-fall 0.15s ease-in;
}

/* ===== START OVERLAY ===== */

#start-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: calc(100%);
  height: calc(100%);
  border-radius: 10px;
  background-color: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

#start-game-button {
  font-size: 18px;
  padding: 12px 24px;
}

/* ===== CONTROLS / STATUS ===== */

.button-row {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin: 14px 0;
}

#status-message {
  margin-bottom: 10px;
  padding: 8px 12px;
  background-color: var(--status-bg);
  border-radius: 6px;
  border: 1px solid var(--status-border);
  font-size: 13px;
}

/* ===== POLYOMINO SHAPES ===== */

#shape-container {
  margin-top: 12px;
  padding: 12px;
  border-radius: 10px;
  background-color: var(--shape-bg);
  border: 1px solid var(--shape-border);
  box-shadow: 0 2px 6px var(--box-shadow);
}

#shape-container h3 {
  margin-top: 0;
  margin-bottom: 4px;
}

.shape-help {
  margin: 0 0 10px 0;
  font-size: 13px;
  opacity: 0.9;
}

#shape-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.polyomino {
  padding: 6px;
  border-radius: 8px;
  background-color: #ffffff;
  border: 2px solid var(--shape-border);
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.15s ease, box-shadow 0.15s ease,
    border-color 0.15s ease, background-color 0.15s ease;
}

.dark-mode .polyomino {
  background-color: #111827;
}

.polyomino:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.35);
}

.polyomino.selected {
  border-color: var(--shape-selected);
  background-color: rgba(255, 213, 79, 0.14);
}

.polyomino-grid {
  display: grid;
  gap: 2px;
}

.poly-cell {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background-color: transparent;
  border: 1px solid transparent;
}

.poly-cell.filled {
  background-color: var(--button-bg);
  border-color: var(--button-bg);
}

/* Ghost element while dragging shapes */

.drag-ghost {
  opacity: 0.9;
  transform: scale(1.05);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.35);
}

/* ===== SCANNER PROGRESS INDICATOR ===== */

#scanner-progress {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--shape-border);
}

.progress-label {
  margin: 0 0 8px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-color);
}

.progress-dots {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 6px;
}

.progress-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: var(--grid-border);
  border: 2px solid var(--shape-border);
  transition: all 0.3s ease;
}

.progress-dot.filled {
  background-color: var(--button-bg);
  border-color: var(--button-bg);
  box-shadow: 0 0 8px rgba(74, 111, 165, 0.5);
  transform: scale(1.1);
}

.dark-mode .progress-dot.filled {
  box-shadow: 0 0 8px rgba(59, 92, 158, 0.7);
}

.progress-hint {
  margin: 0;
  font-size: 11px;
  opacity: 0.7;
  font-style: italic;
}

/* ===== RESULTS MESSAGE ===== */

#gameCompletionMessage {
  display: none;
  width: 100%;
  max-width: 480px;
  height: 0;
  overflow: hidden;
  background: var(--completion-bg);
  color: var(--text-color);
  padding: 0 16px;
  border-radius: 10px;
  box-shadow: 0 4px 12px var(--box-shadow);
  font-size: 14px;
  text-align: left;
  opacity: 0;
  transition: height 1.2s ease, opacity 1.2s ease, padding 1.2s ease;
  margin: 18px auto 10px;
}

#gameCompletionMessage.active {
  display: block;
  min-height: 220px;
  opacity: 1;
  padding: 10px 16px;
  background: var(--completion-active-bg);
}

#results-buttons {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 20px;
}

/* ===== INSTRUCTIONS ===== */

#instructions-container {
  max-width: 700px;
  margin: 16px auto 0;
}

#instructions-toggle {
  margin: 0 auto 6px;
}

#instructions {
  padding: 12px 14px;
  border-radius: 8px;
  background-color: var(--status-bg);
  border: 1px solid var(--status-border);
  text-align: left;
  font-size: 14px;
}

#instructions h3 {
  margin-top: 0;
}

#instructions ul {
  padding-left: 1.4em;
  line-height: 1.5;
}

/* ===== RULEBREAKERS BANNER ===== */

#rulebreakers-banner {
  margin-bottom: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  background-color: var(--status-bg);
  border: 1px solid var(--status-border);
  text-align: left;
  font-size: 13px;
}

#rulebreakers-banner h3 {
  margin: 0 0 4px 0;
  font-size: 14px;
  font-weight: 600;
}

#rulebreakers-banner ul {
  margin: 0;
  padding-left: 1.2em;
}

#rulebreakers-banner li {
  margin: 0 0 2px 0;
}

/* ===== COMPLETION OVERLAY ===== */

#completion-overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(
      circle at top,
      rgba(255, 255, 255, 0.22),
      transparent 55%
    ),
    rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.8s ease;
}

#completion-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.completion-dialog {
  background: #ffffff;
  color: #222222;
  padding: 24px 28px;
  border-radius: 14px;
  max-width: 420px;
  width: 90%;
  text-align: center;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.5);
  animation: pop-in 0.5s ease-out;
}

.dark-mode .completion-dialog {
  background: #1f2933;
  color: #f9fafb;
}

.completion-dialog h2 {
  margin-top: 0;
  margin-bottom: 8px;
}

.completion-dialog p {
  margin: 0 0 16px 0;
  font-size: 14px;
}

/* Pop-in animation for the dialog */
@keyframes pop-in {
  from {
    transform: scale(0.6);
    opacity: 0;
  }
  60% {
    transform: scale(1.05);
    opacity: 1;
  }
  to {
    transform: scale(1);
  }
}

/* Grid celebration when game completes */
@keyframes grid-celebrate {
  0% {
    transform: scale(1);
  }
  20% {
    transform: scale(1.05) rotate(-1deg);
  }
  40% {
    transform: scale(1.05) rotate(1deg);
  }
  100% {
    transform: scale(1);
  }
}

#tile-grid.complete-flash {
  animation: grid-celebrate 0.8s ease-out;
}

/* ===== RESPONSIVE ===== */

@media (max-width: 600px) {
  #tile-grid {
    grid-template-columns: repeat(5, 56px);
    grid-template-rows: repeat(5, 56px);
    gap: 6px;
    padding: 8px;
  }

  .tile {
    width: 56px;
    height: 56px;
  }

  .tile .icon {
    font-size: 24px;
  }

  button {
    padding: 8px 13px;
    font-size: 13px;
  }

  #mode-toggle,
  #mute-button {
    font-size: 14px;
    padding: 5px 8px;
    top: 8px;
  }

  #hud {
    font-size: 12px;
  }

  #shape-container {
    padding: 10px;
  }

  #instructions {
    font-size: 13px;
  }

  /* Smaller vertical step on mobile: 56px tile + 6px gap */
  :root {
    --tile-step: 62px;
  }
}

/* ===== SELECTION (NO GROWTH) ===== */

/* Guess selection highlight: only the edge / glow, no scaling */
.tile.guess-selected {
  background-color: #fffde7; /* very pale yellow */
  border-color: #ffeb3b;
  z-index: 2;

  /* No transform scale here so tile size stays the same */
  box-shadow:
    0 0 0 4px #ffeb3b,
    0 0 18px rgba(255, 235, 59, 0.9),
    0 0 30px rgba(255, 235, 59, 0.9);
}

/* For delayed-flip mode: first tile picked but not yet revealed */
.tile.pending-guess {
  box-shadow:
    0 0 0 3px #ff9800,
    0 0 14px rgba(255, 152, 0, 0.9);
  opacity: 0.95;
}

.dark-mode .tile.guess-selected {
  background-color: #3b2f00;
  border-color: #ffd54f;

  /* No transform scale here either */
  box-shadow:
    0 0 0 4px #ffd54f,
    0 0 20px rgba(255, 213, 79, 0.95),
    0 0 34px rgba(255, 213, 79, 0.95);
}

.dark-mode .tile.pending-guess {
  box-shadow:
    0 0 0 3px #ffb74d,
    0 0 14px rgba(255, 183, 77, 0.95);
  opacity: 0.95;
}

/* ===== OTHER GAMES BANNER ===== */

#other-games-banner {
  max-width: 600px;
  margin: 0 auto 10px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid #ffd28a;
  background: #fff7e6;
  font-size: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  align-items: center;
}

#other-games-banner span {
  font-weight: 600;
}

.other-games-link {
  color: #c45f00;
  font-weight: 600;
  text-decoration: none;
}

.other-games-link:hover {
  text-decoration: underline;
}

.dark-mode #other-games-banner {
  border-color: #8b5a21;
  background: #2b2114;
}

.dark-mode .other-games-link {
  color: #ffb74d;
}

/* ===== MORE GAMES SECTION ===== */

.other-puzzles-section {
  margin: 24px auto 10px;
  max-width: 700px;
}

.other-puzzles-section h3 {
  margin-bottom: 16px;
  font-size: 18px;
  color: var(--text-color);
  position: relative;
  display: inline-block;
}

.other-puzzles-section h3::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 3px;
  background-color: var(--button-bg);
  border-radius: 3px;
}

.puzzle-cards-container {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.puzzle-card {
  background-color: var(--grid-bg);
  border-radius: 12px;
  box-shadow: 0 4px 12px var(--box-shadow);
  padding: 16px;
  width: 220px;
  text-decoration: none;
  color: var(--text-color);
  position: relative;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border: 1px solid var(--grid-border);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.puzzle-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px var(--box-shadow);
}

.new-badge {
  position: absolute;
  top: -10px;
  right: -10px;
  background-color: #ff5252;
  color: white;
  font-size: 11px;
  font-weight: bold;
  padding: 4px 8px;
  border-radius: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.puzzle-icon {
  font-size: 28px;
  margin-bottom: 8px;
}

.puzzle-card h4 {
  margin: 0 0 6px 0;
  font-size: 16px;
}

.puzzle-card p {
  margin: 0;
  font-size: 13px;
  opacity: 0.85;
  text-align: center;
}

/* Mobile adjustments */
@media (max-width: 600px) {
  .other-puzzles-section {
    margin-top: 18px;
  }

  .puzzle-card {
    width: 100%;
    max-width: 260px;
  }
}
