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

:root {
  --bg:        #0F0F0F;
  --surface:   #1A1A1A;
  --surface2:  #252525;
  --text:      #F0F0F0;
  --muted:     #888;
  --accent:    #3B82F6;
  --green:     #10B981;
  --border:    rgba(255,255,255,0.08);
  --radius:    16px;
  --radius-sm: 10px;
  --nav-h:     49px;
  --card-emoji-size: 28px;
}

html { height: 100%; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ================================================================
   NAVBAR
================================================================ */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: var(--nav-h);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 100;
}

.nav-back {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 500;
  transition: color 0.2s;
}
.nav-back:hover { color: var(--text); }

.nav-title {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

/* ================================================================
   SCREENS
================================================================ */
.screen {
  display: none;
  max-width: 480px;
  margin: 0 auto;
}
.screen.active { display: block; }

/* ================================================================
   START SCREEN
================================================================ */
#screen-start {
  padding: 24px 20px 48px;
}

.start-hero { text-align: center; padding-bottom: 36px; }

.hero-icon {
  font-size: 64px;
  display: block;
  margin-bottom: 14px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-10px); }
}

.game-title {
  font-size: clamp(2rem, 9vw, 2.8rem);
  font-weight: 900;
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, #3B82F6 0%, #60A5FA 50%, #93C5FD 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
}

.game-subtitle {
  color: var(--muted);
  font-size: 0.95rem;
  margin-bottom: 24px;
}

.how-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 28px;
  text-align: left;
}

.how-intro {
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.7;
  margin-bottom: 16px;
}
.how-intro strong { color: var(--text); }

.how-steps {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.how-step {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 0.78rem;
  color: var(--muted);
  border: 1px solid var(--border);
}

.how-step-icon { font-size: 1rem; flex-shrink: 0; }

/* ================================================================
   BUTTONS
================================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  font-weight: 700;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
  text-decoration: none;
}
.btn:active:not(:disabled) { transform: scale(0.95); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: #fff;
  padding: 14px 28px;
  font-size: 1rem;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 12px 24px;
  font-size: 0.95rem;
  width: 100%;
}

.btn-xl {
  padding: 18px 40px;
  font-size: 1.05rem;
  border-radius: 100px;
  width: 100%;
}

.btn-lg {
  padding: 16px 32px;
  font-size: 1rem;
  width: 100%;
}

.btn-sm { padding: 6px 12px; font-size: 0.8rem; border-radius: 8px; }

.btn-outline {
  background: transparent;
  border: 2px solid var(--border);
  color: var(--text);
  padding: 14px 28px;
  font-size: 1rem;
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  padding: 6px 12px;
  font-size: 0.8rem;
  border-radius: 8px;
}
.btn-ghost:hover { background: var(--surface); color: var(--text); }

.pulse-btn { animation: btnPulse 2s ease-in-out infinite; }

@keyframes btnPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.45); }
  50%       { box-shadow: 0 0 0 18px rgba(59, 130, 246, 0); }
}

/* ================================================================
   GAME SCREEN
================================================================ */
#screen-game {
  height: calc(100dvh - var(--nav-h));
  display: flex;
  flex-direction: column;
  padding: 10px 12px 10px;
  overflow: hidden;
}

/* HUD */
.game-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  margin-bottom: 6px;
  gap: 8px;
}

.hud-left {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 60px;
}

.level-badge {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border: 2px solid var(--accent);
  color: var(--accent);
  border-radius: 100px;
  padding: 3px 10px;
  white-space: nowrap;
}

.pairs-wrap {
  display: flex;
  flex-direction: column;
  line-height: 1;
}

.hud-label {
  font-size: 0.55rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin-bottom: 1px;
}

.pairs-display {
  font-size: 0.85rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.hud-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1;
}

.timer-display {
  font-size: 2.4rem;
  font-weight: 900;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
  transition: color 0.3s;
}

.timer-display.timer-red {
  color: #EF4444;
  animation: timerPulse 0.8s ease-in-out infinite;
}

@keyframes timerPulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.6; }
}

.timer-label {
  font-size: 0.55rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin-top: 1px;
}

.hud-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-width: 60px;
  line-height: 1;
}

.score-display {
  font-size: 1.6rem;
  font-weight: 900;
  letter-spacing: -0.03em;
}

.score-display.score-pop { animation: scorePop 0.22s ease; }

@keyframes scorePop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.35); }
  100% { transform: scale(1); }
}

/* Combo display */
.combo-display {
  flex-shrink: 0;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
  color: var(--accent);
}

.combo-display.combo-active {
  animation: comboAppear 0.3s ease;
}

@keyframes comboAppear {
  0%   { transform: scale(0.5); opacity: 0; }
  60%  { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1); }
}

/* Card grid */
.card-grid {
  flex: 1;
  display: grid;
  gap: 6px;
  min-height: 0;
  padding: 2px;
}

/* ================================================================
   CARDS
================================================================ */
.card {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  animation: cardIn 0.35s ease backwards;
}

@keyframes cardIn {
  from { transform: scale(0.5) rotate(-8deg); opacity: 0; }
  to   { transform: scale(1) rotate(0); opacity: 1; }
}

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

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

.card-back,
.card-front {
  position: absolute;
  inset: 0;
  border-radius: 10px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-back {
  background: var(--surface);
  border: 1px solid var(--border);
}

.card-back-symbol {
  font-size: 1.2rem;
  font-weight: 900;
  color: var(--muted);
  opacity: 0.5;
  user-select: none;
}

.card-front {
  background: var(--surface2);
  border: 1px solid rgba(255,255,255,0.06);
  transform: rotateY(180deg);
}

.card-emoji {
  font-size: var(--card-emoji-size);
  user-select: none;
  line-height: 1;
}

.card.matched {
  pointer-events: none;
  animation: cardDisappear 0.4s 0.35s ease forwards;
}

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

/* Wrong match shake — applied to whole .card so no conflict with rotateY */
.card.wrong {
  animation: cardWrong 0.5s ease;
}

@keyframes cardWrong {
  0%, 100% { transform: translateX(0); }
  20%       { transform: translateX(-7px); }
  40%       { transform: translateX(7px); }
  60%       { transform: translateX(-5px); }
  80%       { transform: translateX(5px); }
}

/* ================================================================
   LEVEL COMPLETE SCREEN
================================================================ */
#screen-level-complete {
  padding: 20px 20px 40px;
}

.lc-wrap {
  text-align: center;
  padding-top: 32px;
}

.lc-icon { font-size: 56px; margin-bottom: 12px; }

.lc-title {
  font-size: 1.8rem;
  font-weight: 900;
  letter-spacing: -0.02em;
  margin-bottom: 24px;
}

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

.lc-stat {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
}

.lc-stat-label {
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin-bottom: 6px;
  text-transform: uppercase;
}

.lc-stat-value {
  font-size: 1.6rem;
  font-weight: 900;
  letter-spacing: -0.02em;
  line-height: 1;
}

.lc-stat-value.earned-val {
  background: linear-gradient(135deg, #3B82F6, #10B981);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.lc-breakdown-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 14px;
  text-align: left;
}

.lc-breakdown-title {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 10px;
}

.bonus-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  font-size: 0.83rem;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}
.bonus-row:last-child { border-bottom: none; }
.bonus-row span:last-child { color: var(--green); font-weight: 700; }
.bonus-row.bonus-time span:last-child { color: var(--accent); }

.lc-total-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(59,130,246,0.1);
  border: 1px solid rgba(59,130,246,0.25);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  margin-bottom: 24px;
}

.lc-total-label {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
}

.lc-total-value {
  font-size: 1.8rem;
  font-weight: 900;
  letter-spacing: -0.03em;
  color: var(--accent);
}

/* ================================================================
   GAME OVER SCREEN
================================================================ */
#screen-gameover {
  padding: 20px 20px 48px;
}

.gameover-wrap {
  text-align: center;
  max-width: 400px;
  margin: 0 auto;
}

.go-icon { font-size: 52px; margin-bottom: 10px; }

.go-title {
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: -0.03em;
  margin-bottom: 20px;
}

.go-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.go-stat {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.go-stat-label {
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 4px;
}

.go-stat-value {
  font-size: 2rem;
  font-weight: 900;
  letter-spacing: -0.03em;
  line-height: 1;
}

.go-stat-value.score-val {
  background: linear-gradient(135deg, #3B82F6, #60A5FA);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.go-rating {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 22px;
  padding: 10px 16px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  line-height: 1.5;
}

/* Submit box */
.submit-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 20px;
  text-align: left;
}

.submit-title {
  font-size: 0.875rem;
  font-weight: 700;
  margin-bottom: 12px;
}

.submit-row {
  display: flex;
  gap: 10px;
  margin-bottom: 8px;
}

.nickname-input {
  flex: 1;
  min-width: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  padding: 11px 13px;
  outline: none;
  transition: border-color 0.2s;
}
.nickname-input:focus { border-color: var(--accent); }

.submit-msg {
  font-size: 0.83rem;
  color: var(--accent);
  min-height: 18px;
}

/* Share box */
.share-box { margin-bottom: 20px; }
.share-box.hidden { display: none; }

.share-preview {
  background: var(--surface2);
  border-radius: var(--radius-sm);
  padding: 14px;
  font-size: 0.82rem;
  color: var(--muted);
  white-space: pre-line;
  text-align: left;
  margin-bottom: 10px;
  border: 1px solid var(--border);
  line-height: 1.6;
}

.copy-msg {
  font-size: 0.82rem;
  color: var(--green);
  text-align: center;
  margin-top: 8px;
  min-height: 18px;
}

/* ================================================================
   LEADERBOARD
================================================================ */
.leaderboard-section { margin-top: 4px; }

.lb-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.lb-title { font-size: 0.95rem; font-weight: 700; }

.lb-container {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 20px;
}

.lb-loading, .lb-empty {
  padding: 20px;
  text-align: center;
  color: var(--muted);
  font-size: 0.875rem;
}

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

.lb-table thead tr { border-bottom: 1px solid var(--border); }

.lb-table th {
  padding: 9px 12px;
  text-align: left;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.lb-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.lb-table tbody tr:last-child td { border-bottom: none; }
.lb-table tbody tr.my-row { background: rgba(59,130,246,0.1); }
.lb-table tbody tr.my-row td { color: var(--accent); font-weight: 700; }

.lb-challenge {
  padding: 10px 12px;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  border-top: 1px solid var(--border);
}

#btn-play-again { margin-top: 4px; }

/* ================================================================
   TOAST
================================================================ */
.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--surface2);
  color: var(--text);
  padding: 11px 20px;
  border-radius: 100px;
  font-size: 0.9rem;
  font-weight: 600;
  opacity: 0;
  transition: transform 0.28s ease, opacity 0.28s ease;
  pointer-events: none;
  z-index: 1000;
  white-space: nowrap;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5);
  border: 1px solid var(--border);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ================================================================
   CONFETTI
================================================================ */
.confetti-particle {
  position: fixed;
  top: -20px;
  z-index: 9999;
  pointer-events: none;
  animation: confettiFall linear forwards;
}

@keyframes confettiFall {
  0%   { transform: translateY(0) rotate(0deg); opacity: 1; }
  80%  { opacity: 1; }
  100% { transform: translateY(110vh) rotate(720deg); opacity: 0; }
}

/* ================================================================
   RESPONSIVE
================================================================ */
@media (max-height: 640px) {
  .game-hud { margin-bottom: 4px; }
  .combo-display { min-height: 22px; margin-bottom: 2px; }
  .card-grid { gap: 4px; }
  .timer-display { font-size: 1.9rem; }
}

@media (max-width: 380px) {
  .card-grid { gap: 4px; }
  #screen-game { padding: 8px 8px 8px; }
}
