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

body {
  font-family: system-ui, sans-serif;
  background: #1a1a2e;
  color: #eee;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 16px;
  background: #16213e;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
}

header h1 {
  font-size: 1.3rem;
  letter-spacing: 1px;
  color: #e94560;
}

.controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 0.95rem;
}

#timer, #moves {
  min-width: 60px;
  text-align: right;
  color: #aaa;
  font-size: 0.85rem;
}

.controls label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.82rem;
  color: #aaa;
}

.controls input[type="text"] {
  width: 90px;
  padding: 5px 8px;
  border-radius: 5px;
  border: 1px solid #0f3460;
  background: #1a1a2e;
  color: #eee;
  font-size: 0.82rem;
}

.controls input[type="text"]:focus {
  outline: none;
  border-color: #e94560;
}

.controls input[type="number"] {
  width: 48px;
  padding: 5px 4px;
  border-radius: 5px;
  border: 1px solid #0f3460;
  background: #1a1a2e;
  color: #eee;
  font-size: 0.82rem;
  text-align: center;
}

.controls input[type="number"]:focus {
  outline: none;
  border-color: #e94560;
}

button {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  transition: opacity 0.15s;
  touch-action: manipulation;
}

button:hover { opacity: 0.85; }

#new-game-btn {
  background: #e94560;
  color: #fff;
}

main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

#grid {
  display: grid;
  gap: 3px;
}

.tile {
  width: var(--tile-size, 192px);
  height: var(--tile-size, 192px);
  overflow: hidden;
  cursor: pointer;
  border-radius: 3px;
  position: relative;
  transition: box-shadow 0.15s;
  touch-action: manipulation;
}

.tile:hover {
  box-shadow: 0 0 0 3px #e94560;
  z-index: 1;
}


.tile img {
  width: 100%;
  height: 100%;
  display: block;
  transition: transform 0.25s ease;
  user-select: none;
  pointer-events: none;
}

/* Overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.overlay.hidden {
  display: none;
}

@keyframes spin-settle {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

.tile.spinning img {
  transition: none !important;
  animation: spin-settle 1s ease-out forwards;
}

@keyframes overlay-in {
  from { opacity: 0; transform: scale(0.8); }
  to   { opacity: 1; transform: scale(1); }
}

.overlay-box {
  background: #16213e;
  border-radius: 12px;
  padding: 32px 28px;
  text-align: center;
  max-width: 380px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}

.overlay:not(.hidden) .overlay-box {
  animation: overlay-in 0.25s ease;
}

.overlay-box h2 {
  font-size: 1.6rem;
  margin-bottom: 12px;
  color: #e94560;
}

.overlay-box p {
  color: #aaa;
  margin-bottom: 20px;
  line-height: 1.5;
}

.win-buttons {
  display: flex;
  gap: 10px;
}

.win-buttons button {
  flex: 1;
  padding: 12px;
  font-size: 1rem;
}

#admire-btn {
  background: #0f3460;
  color: #eee;
}

#play-again-btn {
  background: #e94560;
  color: #fff;
}

.mapy-link {
  display: block;
  font-size: 0.78rem;
  color: #555;
  text-decoration: none;
  margin-bottom: 16px;
}

.mapy-link:hover {
  color: #aaa;
}

#win-stats {
  font-size: 1.05rem;
  color: #4ecca3;
}

/* Leaderboard */
#leaderboard {
  max-width: 640px;
  margin: 0 auto 40px;
  padding: 0 16px;
  overflow-x: auto;
}

#leaderboard h2 {
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #aaa;
  margin-bottom: 10px;
}

#lb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  min-width: 400px;
}

#lb-table th {
  text-align: left;
  padding: 6px 8px;
  color: #666;
  border-bottom: 1px solid #0f3460;
  font-weight: 600;
  white-space: nowrap;
}

#lb-table td {
  padding: 6px 8px;
  border-bottom: 1px solid #0f3460;
  color: #ccc;
  white-space: nowrap;
}

#lb-table tbody tr:hover td {
  background: #16213e;
}

.lb-empty {
  text-align: center;
  color: #555;
  padding: 20px !important;
}

.lb-anon {
  color: #555;
  font-style: italic;
}

@media (max-width: 520px) {
  .lb-col-grid,
  .lb-col-zoom,
  .lb-col-date { display: none; }

  #lb-table { min-width: unset; }
}

.lb-avg td {
  color: #4ecca3;
  font-weight: 600;
  border-bottom: none;
  padding-top: 10px;
}
