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

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

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: #d4b896;
  color: #3a2010;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top, 8px) 8px env(safe-area-inset-bottom, 8px);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* --- Header --- */
header {
  flex-shrink: 0;
  margin-bottom: 8px;
}

header.hidden, #grid.hidden { display: none; }

.header-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}

h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: #5a2d0c;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.trophy-btn {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.trophy-btn:hover { opacity: 1; }

.mute-btn {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.mute-btn:hover { opacity: 1; }

.splash-buttons {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 100%;
  padding: 0 12px;
}

#splash-trophy-btn {
  font-size: 1.6rem;
  background: #c4944a;
  border: 3px solid #a87830;
  border-radius: 12px;
  padding: 14px 20px;
  opacity: 1;
}

.stats {
  font-size: 0.9rem;
  color: #7a5a3a;
}

.stats span {
  font-weight: 600;
  color: #5a2d0c;
}

.stats .sep {
  color: #b89878;
  margin: 0 2px;
}

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

/* --- Buttons --- */
.diff-btn {
  background: #c4944a;
  color: #fff;
  border: 2px solid #a87830;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s;
  min-height: 36px;
}

.diff-btn:hover { background: #d4a45a; border-color: #b88840; }
.diff-btn.active { background: #5a2d0c; color: #f5e6d0; border-color: #8b5e3c; }

#new-game-btn {
  background: #c0453a;
  color: #fff;
  border: 2px solid #a03028;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  transition: background 0.15s;
  margin-left: auto;
  min-height: 36px;
}

#new-game-btn:hover { background: #d05548; }

/* --- Splash screen --- */
#splash {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  overflow: hidden;
  min-height: 0;
}

#splash.hidden { display: none; }

#cover-img {
  max-width: 80vw;
  max-height: 60%;
  object-fit: contain;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  flex-shrink: 1;
}

#play-btn {
  background: #c0453a;
  color: #fff;
  border: 3px solid #a03028;
  padding: 14px 48px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  transition: background 0.15s, transform 0.1s;
  min-height: 52px;
}

#play-btn:hover { background: #d05548; }
#play-btn:active { transform: scale(0.96); }

.mp-splash-btn {
  background: #c4944a;
  color: #fff;
  border: 3px solid #a87830;
  padding: 14px 24px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  transition: background 0.15s, transform 0.1s;
  min-height: 52px;
}

.mp-splash-btn:hover { background: #d4a45a; }
.mp-splash-btn:active { transform: scale(0.96); }

/* --- Card grid --- */
#grid {
  display: grid;
  width: fit-content;
  margin: 0 auto;
  align-content: center;
}

/* --- Card --- */
.card {
  perspective: 600px;
  cursor: pointer;
  touch-action: manipulation;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.4s ease;
  transform-style: preserve-3d;
}

.card.flipped .card-inner,
.card.matched .card-inner {
  transform: rotateY(180deg);
}

.card:active:not(.matched):not(.flipped) .card-inner {
  transform: scale(0.95);
}

.card-front,
.card-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 8px;
  overflow: hidden;
}

/* Face-down: red/coral with speckled pattern (matching the original box) */
.card-front {
  background: #c0453a;
  border: 3px solid #a03028;
  background-image:
    radial-gradient(circle, rgba(255,255,255,0.15) 1px, transparent 1px);
  background-size: 8px 8px;
}

.card-front::after {
  content: '';
}

/* Face-up: warm cream background matching the box tiles */
.card-back {
  transform: rotateY(180deg);
  background: #f5ece0;
}

.card-back img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.card.matched { cursor: default; }
.card.matched .card-back { box-shadow: 0 0 10px rgba(194, 148, 74, 0.5); }

/* --- Win overlay --- */
#win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(58, 32, 16, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
  z-index: 100;
}

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

.win-card {
  background: #f5ece0;
  border: 4px solid #c0453a;
  border-radius: 16px;
  padding: 40px;
  text-align: center;
  max-width: 360px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.win-card h2 { font-size: 2rem; color: #5a2d0c; margin-bottom: 16px; }
.win-stats { font-size: 1.1rem; color: #7a5a3a; margin-bottom: 24px; line-height: 1.8; }
.win-stats span { color: #5a2d0c; font-weight: 700; }

#win-new-game {
  background: #c0453a;
  color: #fff;
  border: 2px solid #a03028;
  padding: 12px 28px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: background 0.15s;
  min-height: 44px;
}

#win-new-game:hover { background: #d05548; }

/* --- High score in win card --- */
.hidden { display: none !important; }

.win-highscore {
  margin-bottom: 16px;
}

.highscore-label {
  color: #c0453a;
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 10px;
}

.name-input-row {
  display: flex;
  gap: 8px;
  justify-content: center;
}

#name-input {
  padding: 8px 12px;
  border: 2px solid #c4944a;
  border-radius: 6px;
  font-size: 1rem;
  background: #fff;
  color: #3a2010;
  width: 140px;
  font-family: inherit;
}

#name-input:focus { outline: none; border-color: #c0453a; }

#submit-score-btn {
  background: #c0453a;
  color: #fff;
  border: 2px solid #a03028;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
}

#submit-score-btn:hover { background: #d05548; }

/* --- Leaderboard overlay --- */
#leaderboard-overlay {
  position: fixed;
  inset: 0;
  background: rgba(58, 32, 16, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
  z-index: 200;
}

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

.leaderboard-card {
  background: #f5ece0;
  border: 4px solid #c0453a;
  border-radius: 16px;
  padding: 24px;
  max-width: 480px;
  width: 90vw;
  max-height: 85dvh;
  overflow-y: auto;
}

.leaderboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.leaderboard-header h2 { font-size: 1.4rem; color: #5a2d0c; }

.close-btn {
  background: none;
  border: none;
  font-size: 1.8rem;
  cursor: pointer;
  color: #7a5a3a;
  padding: 0 4px;
  line-height: 1;
}

.close-btn:hover { color: #c0453a; }

.leaderboard-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 16px;
}

.lb-diff-btn {
  background: #c4944a;
  color: #fff;
  border: 2px solid #a87830;
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
}

.lb-diff-btn.active { background: #5a2d0c; border-color: #8b5e3c; }

.leaderboard-tables {
  display: flex;
  gap: 16px;
}

.lb-section {
  flex: 1;
  min-width: 0;
}

.lb-section h3 {
  font-size: 0.85rem;
  color: #7a5a3a;
  margin-bottom: 8px;
  text-align: center;
}

.lb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.lb-table td {
  padding: 4px 6px;
  border-bottom: 1px solid #e0d0c0;
  color: #3a2010;
}

.lb-rank { width: 24px; color: #7a5a3a; text-align: center; }
.lb-name { font-weight: 600; }
.lb-value { text-align: right; color: #5a2d0c; font-weight: 600; }

.lb-highlight td {
  background: #fef3c7;
  color: #92400e;
}

.lb-empty {
  text-align: center;
  color: #7a5a3a;
  font-size: 0.85rem;
  padding: 16px 0;
}

/* Win card leaderboard tables */
#win-leaderboard {
  margin-bottom: 16px;
}

#win-leaderboard .leaderboard-tables { gap: 12px; }
#win-leaderboard .lb-section h3 { font-size: 0.8rem; }
#win-leaderboard .lb-table { font-size: 0.75rem; }

/* --- Responsive --- */
@media (max-width: 520px) {
  .leaderboard-tables { flex-direction: column; gap: 12px; }
}

@media (max-width: 480px) {
  body { padding: env(safe-area-inset-top, 4px) 4px env(safe-area-inset-bottom, 4px); }
  h1 { font-size: 1.1rem; }
  .stats { font-size: 0.8rem; }
  .card-front { border-width: 2px; }
  .diff-btn, #new-game-btn { padding: 4px 10px; font-size: 0.75rem; min-height: 32px; }
  .controls { gap: 4px; }
  /* Splash buttons: shrink and allow wrap on narrow screens */
  #play-btn { padding: 12px 28px; font-size: 1.1rem; }
  .mp-splash-btn { padding: 12px 20px; font-size: 0.9rem; }
  #splash-trophy-btn { padding: 12px 16px; }
}

@media (max-height: 500px) {
  header { margin-bottom: 4px; }
  .header-row { margin-bottom: 2px; }
  h1 { font-size: 1rem; }
  .diff-btn, #new-game-btn { padding: 2px 8px; font-size: 0.7rem; min-height: 28px; }
  .controls { gap: 4px; }
  .card-front { border-width: 1px; }
}
