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

:root {
  --bg: #1a1f3c;
  --bg-light: #252b50;
  --card-coral: #ff6b6b;
  --card-blue: #4ecdc4;
  --card-yellow: #ffe66d;
  --card-green: #a8e6cf;
  --white: #fff;
  --cream: #fef9ef;
  --text-dark: #1a1f3c;
  --correct-glow: #00e676;
  --wrong-dim: rgba(0,0,0,0.5);
}

body {
  font-family: 'Nunito', sans-serif;
  background: var(--bg);
  color: var(--cream);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  font-size: 18px;
  overflow-x: hidden;
}

h1, h2, h3 {
  font-family: 'Fredoka One', cursive;
}

/* ── HEADER ── */
.header {
  text-align: center;
  margin-bottom: 10px;
  position: relative;
  width: 100%;
  max-width: 700px;
}

.header h1 {
  font-size: 2.4rem;
  background: linear-gradient(135deg, var(--card-coral), var(--card-yellow), var(--card-green), var(--card-blue));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header .puzzle-number {
  font-size: 1.1rem;
  opacity: 0.8;
  margin-top: 4px;
}

.how-to-play-btn {
  position: absolute;
  top: 5px;
  right: 0;
  background: var(--bg-light);
  border: 2px solid rgba(255,255,255,0.2);
  color: var(--cream);
  font-family: 'Nunito', sans-serif;
  font-weight: 700;
  font-size: 1rem;
  padding: 8px 16px;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.2s;
}

.how-to-play-btn:hover,
.how-to-play-btn:focus {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.4);
}

/* ── TIMER ── */
.timer-container {
  display: flex;
  justify-content: center;
  margin: 15px 0;
}

.timer-ring-wrap {
  position: relative;
  width: 100px;
  height: 100px;
}

.timer-ring-wrap svg {
  transform: rotate(-90deg);
  width: 100px;
  height: 100px;
}

.timer-bg {
  fill: none;
  stroke: rgba(255,255,255,0.1);
  stroke-width: 8;
}

.timer-fg {
  fill: none;
  stroke: var(--correct-glow);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 251.2;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 1s linear, stroke 0.5s;
}

.timer-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: 'Fredoka One', cursive;
  font-size: 1.8rem;
  color: var(--cream);
}

/* ── CARDS GRID ── */
.cards-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  width: 100%;
  max-width: 600px;
  margin: 10px auto;
}

.card {
  position: relative;
  min-height: 90px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 20px;
  cursor: pointer;
  border: 4px solid transparent;
  transition: transform 0.2s, border-color 0.2s, opacity 0.3s, box-shadow 0.3s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  animation: cardBounce 0.5s ease-out backwards;
}

.card:nth-child(1) { background: var(--card-coral); animation-delay: 0.05s; }
.card:nth-child(2) { background: var(--card-blue); animation-delay: 0.15s; }
.card:nth-child(3) { background: var(--card-yellow); animation-delay: 0.25s; }
.card:nth-child(4) { background: var(--card-green); animation-delay: 0.35s; }

.card .card-label {
  font-family: 'Fredoka One', cursive;
  font-size: 1.5rem;
  color: var(--text-dark);
  text-align: center;
  line-height: 1.3;
}

.card .speak-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0,0,0,0.15);
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.card .speak-btn:hover,
.card .speak-btn:focus {
  background: rgba(0,0,0,0.3);
}

.card .speak-btn.speaking {
  animation: speakerWave 0.6s ease-in-out infinite alternate;
}

.card:hover {
  transform: translateY(-3px);
}

.card:focus {
  outline: 3px solid var(--cream);
  outline-offset: 2px;
}

.card.selected {
  border-color: var(--cream);
  box-shadow: 0 0 20px rgba(255,255,255,0.4);
  transform: scale(1.03);
}

.card.correct {
  border-color: var(--correct-glow);
  box-shadow: 0 0 30px rgba(0,230,118,0.6);
}

.card.correct::after {
  content: '\2713  Correct';
  position: absolute;
  bottom: -28px;
  left: 50%;
  transform: translateX(-50%);
  font-family: 'Nunito', sans-serif;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--correct-glow);
  white-space: nowrap;
}

.card.wrong {
  animation: shake 0.5s;
  opacity: 0.5;
}

.card.dimmed {
  opacity: 0.45;
  pointer-events: none;
}

.card.disabled {
  pointer-events: none;
  cursor: default;
}

/* ── ANIMATIONS ── */
@keyframes cardBounce {
  0% { opacity: 0; transform: scale(0.7) translateY(20px); }
  60% { transform: scale(1.05) translateY(-5px); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

@keyframes speakerWave {
  0% { transform: scale(1); }
  100% { transform: scale(1.2); }
}

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

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

/* ── BUTTONS ── */
.btn {
  font-family: 'Nunito', sans-serif;
  font-weight: 700;
  font-size: 1.1rem;
  padding: 14px 36px;
  border: none;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.btn:focus {
  outline: 3px solid var(--cream);
  outline-offset: 2px;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: var(--white);
  font-size: 1.2rem;
  padding: 16px 48px;
}

.btn-primary:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 20px rgba(102,126,234,0.5);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-speak {
  background: var(--bg-light);
  color: var(--cream);
  border: 2px solid rgba(255,255,255,0.2);
  font-size: 1rem;
  padding: 10px 24px;
}

.btn-speak:hover {
  background: rgba(255,255,255,0.15);
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin: 16px 0;
  width: 100%;
  max-width: 600px;
}

/* ── RESULT SCREEN ── */
.result-container {
  text-align: center;
  max-width: 600px;
  width: 100%;
  animation: fadeIn 0.5s ease-out;
}

.result-message {
  font-family: 'Fredoka One', cursive;
  font-size: 2rem;
  margin: 20px 0 10px;
}

.result-message.win { color: var(--correct-glow); }
.result-message.loss { color: var(--card-yellow); }

.answer-box {
  background: var(--bg-light);
  border-radius: 16px;
  padding: 20px;
  margin: 16px 0;
  border-left: 5px solid var(--correct-glow);
  text-align: left;
}

.answer-box .answer-label {
  font-weight: 800;
  color: var(--correct-glow);
  font-size: 1.3rem;
  margin-bottom: 8px;
}

.answer-box .explanation {
  font-size: 1.05rem;
  line-height: 1.6;
  color: rgba(254,249,239,0.9);
}

/* ── STATS PANEL ── */
.stats-panel {
  margin: 24px 0;
  width: 100%;
  max-width: 500px;
}

.stats-panel h3 {
  text-align: center;
  font-size: 1.3rem;
  margin-bottom: 14px;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-card {
  background: var(--bg-light);
  border-radius: 16px;
  padding: 18px 12px;
  text-align: center;
}

.stat-card .stat-number {
  font-family: 'Fredoka One', cursive;
  font-size: 2.2rem;
  color: var(--card-yellow);
}

.stat-card .stat-label {
  font-size: 0.9rem;
  opacity: 0.7;
  margin-top: 4px;
}

/* ── BADGES ── */
.badges-section {
  margin-top: 16px;
  width: 100%;
}

.badges-section h3 {
  text-align: center;
  font-size: 1.3rem;
  margin-bottom: 14px;
}

.badges-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.badge-item {
  background: var(--bg-light);
  border-radius: 16px;
  padding: 14px 8px;
  text-align: center;
  transition: transform 0.2s;
}

.badge-item.earned {
  border: 2px solid var(--card-yellow);
}

.badge-item.locked {
  opacity: 0.35;
  filter: grayscale(1);
}

.badge-item.earned:hover {
  transform: scale(1.05);
}

.badge-img {
  width: 72px;
  height: 72px;
  object-fit: contain;
  margin-bottom: 6px;
}

.badge-name {
  font-family: 'Fredoka One', cursive;
  font-size: 0.85rem;
  color: var(--card-yellow);
  margin-bottom: 2px;
}

.badge-desc {
  font-size: 0.75rem;
  opacity: 0.7;
  line-height: 1.3;
}

.comeback-msg {
  font-family: 'Fredoka One', cursive;
  font-size: 1.3rem;
  color: var(--card-blue);
  margin: 20px 0;
  text-align: center;
}

/* ── TUTORIAL MODAL ── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  z-index: 1000;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: var(--bg-light);
  border-radius: 24px;
  padding: 30px;
  max-width: 520px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  animation: fadeIn 0.3s ease-out;
}

.modal h2 {
  font-size: 1.6rem;
  text-align: center;
  margin-bottom: 18px;
  color: var(--card-yellow);
}

.modal .step {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 14px;
  font-size: 1.05rem;
  line-height: 1.5;
}

.modal .step-emoji {
  font-size: 1.5rem;
  flex-shrink: 0;
}

.modal .sample-puzzle {
  background: rgba(255,255,255,0.05);
  border-radius: 12px;
  padding: 16px;
  margin: 16px 0;
}

.modal .sample-puzzle .sample-items {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.modal .sample-puzzle .sample-item {
  padding: 6px 16px;
  border-radius: 10px;
  font-family: 'Fredoka One', cursive;
  font-size: 1rem;
  color: var(--text-dark);
}

.modal .sample-puzzle .sample-item:nth-child(1) { background: var(--card-coral); }
.modal .sample-puzzle .sample-item:nth-child(2) { background: var(--card-blue); }
.modal .sample-puzzle .sample-item:nth-child(3) { background: var(--card-yellow); }
.modal .sample-puzzle .sample-item.odd {
  background: var(--card-green);
  border: 3px solid var(--correct-glow);
}

.modal .sample-explanation {
  font-size: 0.95rem;
  line-height: 1.5;
  opacity: 0.85;
}

.modal .close-modal-btn {
  display: block;
  margin: 20px auto 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: var(--white);
  font-family: 'Nunito', sans-serif;
  font-weight: 700;
  font-size: 1.1rem;
  padding: 14px 36px;
  border: none;
  border-radius: 50px;
  cursor: pointer;
  transition: transform 0.2s;
}

.modal .close-modal-btn:hover {
  transform: scale(1.05);
}

/* ── CONFETTI ── */
.confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 999;
  overflow: hidden;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  font-size: 1.8rem;
  animation: confettiFall 3s ease-in forwards;
}

/* ── FOOTER ── */
.footer {
  margin-top: auto;
  padding-top: 30px;
  font-size: 0.85rem;
  opacity: 0.5;
  text-align: center;
}

/* ── RESPONSIVE ── */
@media (max-width: 480px) {
  .header h1 { font-size: 1.8rem; }
  .cards-grid { gap: 12px; }
  .card { min-height: 80px; padding: 14px 16px; }
  .card .card-label { font-size: 1.25rem; }
  .how-to-play-btn { position: static; margin-top: 10px; }
  .badges-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .badge-img { width: 56px; height: 56px; }
  .badge-name { font-size: 0.75rem; }
  .badge-desc { font-size: 0.65rem; }
}

@media (min-width: 900px) {
  .cards-grid {
    grid-template-columns: 1fr 1fr;
    max-width: 700px;
  }
  .card { min-height: 110px; }
  .card .card-label { font-size: 1.7rem; }
}
