/* ===== RESET & BASE ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #6c5ce7;
  --primary-dark: #5a4bd1;
  --secondary: #00cec9;
  --accent: #fd79a8;
  --bg: #f8f9fe;
  --bg2: #eef0fb;
  --text: #2d3436;
  --text-muted: #636e72;
  --card: #ffffff;
  --card-border: rgba(108, 92, 231, 0.15);
  --shadow: 0 4px 20px rgba(108, 92, 231, 0.12);
  --shadow-lg: 0 8px 40px rgba(108, 92, 231, 0.2);
  --radius: 16px;
  --radius-sm: 10px;
  --tile-gap: 3px;
  --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
  --bg: #1a1a2e;
  --bg2: #16213e;
  --text: #edf2f7;
  --text-muted: #a0aec0;
  --card: #1e2940;
  --card-border: rgba(108, 92, 231, 0.3);
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 40px rgba(0, 0, 0, 0.4);
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background var(--transition), color var(--transition);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ===== SCREENS ===== */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
  overflow-y: auto;
  padding: 20px;
}

.screen.active {
  opacity: 1;
  pointer-events: all;
}

.screen-inner {
  width: 100%;
  max-width: 420px;
  text-align: center;
}

/* ===== WELCOME ===== */
.logo {
  font-size: 2.2rem;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
  animation: bounceIn 0.6s ease;
}

.subtitle {
  color: var(--text-muted);
  font-size: 1rem;
  margin-bottom: 32px;
}

.upload-zone {
  border: 3px dashed var(--card-border);
  border-radius: var(--radius);
  padding: 40px 20px;
  margin-bottom: 24px;
  cursor: pointer;
  transition: all var(--transition);
  background: var(--card);
}

.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--primary);
  background: rgba(108, 92, 231, 0.05);
  transform: scale(1.02);
}

.upload-icon {
  font-size: 3rem;
  margin-bottom: 12px;
}

.upload-zone p {
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 24px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  text-decoration: none;
  color: var(--text);
  background: var(--card);
  box-shadow: var(--shadow);
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
}

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

.btn-secondary {
  background: var(--card);
  border: 2px solid var(--card-border);
}

.btn-secondary:hover {
  border-color: var(--primary);
}

.btn-large {
  padding: 16px 40px;
  font-size: 1.15rem;
  border-radius: var(--radius);
}

.btn-small {
  padding: 8px 16px;
  font-size: 0.85rem;
}

.btn-tiny {
  padding: 4px 12px;
  font-size: 0.75rem;
  border-radius: 8px;
}

.btn-icon {
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: 50%;
  font-size: 1.3rem;
}

.btn-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
}

/* ===== INSTALL BANNER ===== */
.install-banner {
  margin-top: 24px;
  padding: 16px;
  background: var(--card);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.install-banner p {
  font-weight: 600;
  font-size: 0.9rem;
}

/* ===== SETTINGS / PREVIEW ===== */
.preview-wrapper {
  position: relative;
  display: inline-block;
  margin-bottom: 24px;
}

.preview-wrapper img {
  width: 200px;
  height: 200px;
  object-fit: cover;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.preview-wrapper .btn-tiny {
  position: absolute;
  bottom: -10px;
  right: -10px;
}

.difficulty-section {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-bottom: 32px;
}

.diff-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px 20px;
  border: 2px solid var(--card-border);
  border-radius: var(--radius);
  background: var(--card);
  cursor: pointer;
  transition: all var(--transition);
  min-width: 90px;
}

.diff-btn:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
}

.diff-btn.active {
  border-color: var(--primary);
  background: linear-gradient(135deg, rgba(108, 92, 231, 0.1), rgba(253, 121, 168, 0.1));
  box-shadow: var(--shadow);
}

.diff-emoji {
  font-size: 1.8rem;
}

.diff-label {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
}

.diff-name {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ===== GAME SCREEN ===== */
#screen-game {
  flex-direction: column;
  padding: 12px;
  gap: 12px;
}

.game-header {
  display: flex;
  justify-content: center;
  gap: 24px;
  width: 100%;
  max-width: 420px;
}

.stat {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--card);
  padding: 8px 16px;
  border-radius: 50px;
  box-shadow: var(--shadow);
  font-weight: 600;
}

.stat-icon {
  font-size: 1rem;
}

.stat-value {
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
}

/* ===== PUZZLE BOARD ===== */
#puzzle-container {
  position: relative;
  width: min(85vw, 85vh - 160px, 420px);
  height: min(85vw, 85vh - 160px, 420px);
  margin: 0 auto;
}

#puzzle-board {
  width: 100%;
  height: 100%;
  display: grid;
  gap: var(--tile-gap);
  background: var(--bg2);
  border-radius: var(--radius);
  padding: var(--tile-gap);
  box-shadow: var(--shadow-lg);
  position: relative;
}

.tile {
  border-radius: var(--radius-sm);
  cursor: pointer;
  background-size: cover;
  background-repeat: no-repeat;
  transition: transform 0.15s cubic-bezier(0.4, 0, 0.2, 1),
              opacity 0.15s ease,
              box-shadow 0.15s ease;
  position: relative;
  overflow: hidden;
}

.tile:hover:not(.tile-empty) {
  box-shadow: 0 0 0 3px var(--primary);
  z-index: 1;
}

.tile:active:not(.tile-empty) {
  transform: scale(0.95);
}

.tile-empty {
  opacity: 0;
  cursor: default;
  pointer-events: none;
}

.tile.tile-hint {
  box-shadow: 0 0 0 3px var(--secondary), 0 0 12px rgba(0, 206, 201, 0.4);
  animation: hintPulse 1s ease infinite;
}

.tile.tile-correct {
  box-shadow: 0 0 0 2px rgba(0, 206, 201, 0.5) inset;
}

.tile .tile-number {
  position: absolute;
  top: 4px;
  left: 4px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  opacity: 0;
  transition: opacity var(--transition);
}

.show-numbers .tile .tile-number {
  opacity: 1;
}

#preview-overlay {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background-size: cover;
  background-position: center;
  opacity: 0.85;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

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

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

/* ===== WIN SCREEN ===== */
#screen-win {
  z-index: 100;
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.win-content {
  position: relative;
  z-index: 1;
}

.win-title {
  font-size: 2.5rem;
  margin-bottom: 20px;
  animation: bounceIn 0.6s ease;
}

.win-stats {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 24px;
}

.win-stat {
  background: var(--card);
  padding: 16px 20px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  text-align: center;
  min-width: 80px;
}

.win-stat-label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.win-stat-value {
  display: block;
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}

.win-image-wrapper {
  margin-bottom: 24px;
}

.win-image-wrapper img {
  width: 180px;
  height: 180px;
  object-fit: cover;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  animation: popIn 0.5s ease 0.3s both;
}

/* ===== UTILITIES ===== */
.hidden {
  display: none !important;
}

/* ===== ANIMATIONS ===== */
@keyframes bounceIn {
  0% { transform: scale(0.3); opacity: 0; }
  50% { transform: scale(1.05); }
  70% { transform: scale(0.95); }
  100% { transform: scale(1); opacity: 1; }
}

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

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 0 3px var(--secondary), 0 0 12px rgba(0, 206, 201, 0.4); }
  50% { box-shadow: 0 0 0 5px var(--secondary), 0 0 20px rgba(0, 206, 201, 0.6); }
}

@keyframes tileSlide {
  0% { transform: scale(1); }
  50% { transform: scale(0.97); }
  100% { transform: scale(1); }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 380px) {
  .logo { font-size: 1.7rem; }
  .subtitle { font-size: 0.85rem; }
  .difficulty-section { gap: 8px; }
  .diff-btn { padding: 12px 14px; min-width: 75px; }
  .game-header { gap: 12px; }
  .stat { padding: 6px 12px; font-size: 0.85rem; }
  .game-controls { gap: 8px; }
  .btn-icon { width: 42px; height: 42px; font-size: 1.1rem; }
  .win-stats { gap: 10px; }
  .win-stat { padding: 12px 14px; min-width: 70px; }
}

@media (min-height: 800px) {
  #puzzle-container {
    width: min(85vw, 75vh - 160px, 420px);
    height: min(85vw, 75vh - 160px, 420px);
  }
}

/* ===== SCROLLBAR (dark mode) ===== */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--card-border);
  border-radius: 3px;
}
