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

:root {
  --primary: #e91e63;
  --primary-light: #f48fb1;
  --gold: #ffd700;
  --bg: #1a1a2e;
  --bg-light: #16213e;
  --surface: #0f3460;
  --text: #f0f0f0;
  --text-muted: #a0a0b8;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

/* === Screens === */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease, transform 0.4s ease;
  transform: translateY(20px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.screen.active {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 14px 36px;
  border: none;
  border-radius: 50px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  min-height: 48px;
}

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

.btn-primary {
  background: linear-gradient(135deg, var(--primary), #c2185b);
  color: white;
  box-shadow: 0 4px 20px rgba(233, 30, 99, 0.4);
}

.btn-primary:disabled {
  background: #444;
  box-shadow: none;
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-glow {
  animation: glow 2s ease-in-out infinite alternate;
}

@keyframes glow {
  from { box-shadow: 0 4px 20px rgba(233, 30, 99, 0.4); }
  to { box-shadow: 0 4px 40px rgba(233, 30, 99, 0.7), 0 0 60px rgba(233, 30, 99, 0.3); }
}

.btn-icon {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  transition: background 0.2s;
}

.btn-icon:active {
  background: rgba(255,255,255,0.1);
}

/* === Landing Screen === */
.landing-content {
  text-align: center;
  padding: 20px;
  position: relative;
}

.landing-title {
  font-size: clamp(36px, 10vw, 64px);
  line-height: 1.2;
  margin-bottom: 16px;
}

.landing-title .word {
  display: block;
  opacity: 0;
  animation: fadeInUp 0.6s ease forwards;
}

.landing-title .word:nth-child(1) { animation-delay: 0.3s; }
.landing-title .word:nth-child(2) { animation-delay: 0.7s; }
.landing-title .word:nth-child(3) { animation-delay: 1.1s; }

.birthday-name {
  background: linear-gradient(135deg, var(--gold), #ffaa00, var(--gold));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.landing-subtitle {
  font-size: 16px;
  line-height: 1.6;
  color: var(--text-muted);
  margin-bottom: 40px;
  max-width: 360px;
  text-align: center;
  opacity: 0;
  animation: fadeInUp 0.6s ease 1.5s forwards;
}

#btn-start {
  opacity: 0;
  animation: fadeInUp 0.6s ease 1.9s forwards;
}

.landing-footer {
  position: fixed;
  bottom: 24px;
  font-size: 14px;
  color: var(--text-muted);
  opacity: 0;
  animation: fadeInUp 0.6s ease 2.3s forwards;
}

/* Sparkles */
.sparkles {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.sparkle {
  position: absolute;
  width: 6px;
  height: 6px;
  background: var(--gold);
  border-radius: 50%;
  animation: sparkle 3s ease-in-out infinite;
}

.sparkle:nth-child(1) { top: 10%; left: 15%; animation-delay: 0s; }
.sparkle:nth-child(2) { top: 20%; left: 80%; animation-delay: 0.5s; }
.sparkle:nth-child(3) { top: 50%; left: 10%; animation-delay: 1s; }
.sparkle:nth-child(4) { top: 60%; left: 90%; animation-delay: 1.5s; }
.sparkle:nth-child(5) { top: 80%; left: 25%; animation-delay: 2s; }
.sparkle:nth-child(6) { top: 30%; left: 60%; animation-delay: 2.5s; }

@keyframes sparkle {
  0%, 100% { opacity: 0; transform: scale(0); }
  50% { opacity: 1; transform: scale(1); }
}

/* === Select Screen === */
#screen-select {
  justify-content: stretch;
  padding: 0;
  display: flex;
  flex-direction: column;
}

.select-header {
  flex-shrink: 0;
  padding: max(16px, env(safe-area-inset-top)) 16px 8px;
}

.select-header h2 {
  font-size: 24px;
  text-align: center;
}

.photo-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-auto-rows: min-content;
  align-content: start;
  gap: 12px;
  padding: 8px 16px;
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
  min-height: 0;
}

.select-bottom {
  flex-shrink: 0;
  padding: 12px 16px max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
  background: linear-gradient(transparent, var(--bg) 20%);
}

.photo-card {
  position: relative;
  aspect-ratio: 1;
  border-radius: var(--radius);
  overflow: hidden;
  isolation: isolate;
  cursor: pointer;
  border: 3px solid transparent;
  transition: border-color 0.2s, transform 0.2s;
}

.photo-card:active {
  transform: scale(0.96);
}

.photo-card.selected {
  border-color: var(--primary);
  box-shadow: 0 0 20px rgba(233, 30, 99, 0.4);
}

.photo-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: filter 0.4s ease;
}

.photo-card img.blurred {
  filter: blur(12px) brightness(0.8);
  scale: 1.1;
}

.photo-card .photo-label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 6px 8px;
  background: linear-gradient(transparent, rgba(0,0,0,0.7));
  font-size: 12px;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stars {
  display: flex;
  gap: 2px;
}

.star {
  font-size: 14px;
  color: rgba(255,255,255,0.25);
  transition: color 0.3s;
}

.star.earned {
  color: var(--gold);
  text-shadow: 0 0 6px rgba(255, 215, 0, 0.5);
}

/* Difficulty */
.difficulty-section {
  text-align: center;
}

.difficulty-label {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.difficulty-pills {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.pill {
  padding: 10px 24px;
  border: 2px solid var(--surface);
  border-radius: 50px;
  background: transparent;
  color: var(--text-muted);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  min-height: 44px;
}

.pill.active {
  border-color: var(--primary);
  background: var(--primary);
  color: white;
}

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

#btn-play {
  flex-shrink: 0;
}

/* === Puzzle Screen === */
#screen-puzzle {
  justify-content: flex-start;
  padding: 0;
  background: #0d1117;
}

.puzzle-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 8px 8px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: rgba(0,0,0,0.3);
  flex-shrink: 0;
  z-index: 10;
}

.puzzle-info {
  display: flex;
  gap: 16px;
  font-size: 15px;
  font-weight: 500;
}

#timer {
  font-variant-numeric: tabular-nums;
  color: var(--gold);
}

.puzzle-canvas {
  flex: 1;
  width: 100%;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

.puzzle-canvas canvas {
  display: block;
}

/* Preview overlay */
.preview-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  transition: opacity 0.2s;
}

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

.preview-overlay img {
  max-width: 100%;
  max-height: 100%;
  border-radius: var(--radius);
  object-fit: contain;
  transition: filter 0.3s ease;
}

.preview-overlay img.blurred {
  filter: blur(20px) brightness(0.8);
}

/* === Celebration Screen === */
#screen-celebrate {
  background: var(--bg);
}

.celebrate-content {
  text-align: center;
  padding: 20px;
}

.celebrate-emoji {
  font-size: 80px;
  margin-bottom: 16px;
  animation: bounce 0.6s ease infinite alternate;
}

@keyframes bounce {
  from { transform: translateY(0); }
  to { transform: translateY(-12px); }
}

.celebrate-title {
  font-size: 40px;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--gold), #ffaa00);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.celebrate-message {
  font-size: 18px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.celebrate-time {
  font-size: 24px;
  font-weight: 600;
  color: var(--gold);
  margin-bottom: 24px;
  font-variant-numeric: tabular-nums;
}

.celebrate-image-container {
  max-width: 300px;
  margin: 0 auto 32px;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0,0,0,0.4);
}

.celebrate-image-container img {
  width: 100%;
  display: block;
}

#btn-again {
  margin-bottom: max(20px, env(safe-area-inset-bottom));
}

/* === Loading spinner === */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 16px;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* === Tablet/Desktop === */
@media (min-width: 768px) {
  .photo-grid {
    grid-template-columns: repeat(3, 1fr);
    max-width: 700px;
  }

  .landing-title {
    font-size: 72px;
  }
}
