*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #ecfdf5;
  --bg-gradient: linear-gradient(135deg, #34d399 0%, #10b981 55%, #0891b2 100%);
  --card: #ffffff;
  --primary: #059669;
  --primary-dark: #047857;
  --secondary: #fbbf24;
  --success: #06d6a0;
  --error: #ef476f;
  --text: #2d3748;
  --text-light: #718096;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
  --radius: 20px;
}

body {
  font-family: "Fredoka", sans-serif;
  background: var(--bg-gradient);
  min-height: 100vh;
  color: var(--text);
  line-height: 1.5;
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

.header {
  text-align: center;
  margin-bottom: 28px;
  color: white;
}

.header-top {
  text-align: left;
  margin-bottom: 12px;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 99px;
  text-decoration: none;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.2s, transform 0.2s;
}

.back-link:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateX(-3px);
}

.back-link:focus-visible {
  outline: 2px solid white;
  outline-offset: 2px;
}

.logo {
  font-size: 2.4rem;
  font-weight: 700;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.tagline {
  font-size: 1.05rem;
  opacity: 0.95;
  margin-top: 4px;
}

.screen {
  display: none;
}

.screen.active {
  display: block;
  animation: fadeIn 0.35s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 32px 28px;
  box-shadow: var(--shadow);
}

.card h1 {
  font-size: 1.8rem;
  text-align: center;
  margin-bottom: 8px;
}

.subtitle {
  text-align: center;
  color: var(--text-light);
  margin-bottom: 24px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 24px;
}

.field span {
  font-weight: 500;
  font-size: 0.95rem;
}

.field input {
  font-family: inherit;
  font-size: 1.1rem;
  padding: 12px 16px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  outline: none;
  transition: border-color 0.2s;
}

.field input:focus {
  border-color: var(--primary);
}

.mode-label {
  font-weight: 600;
  margin-bottom: 12px;
  text-align: center;
}

.mode-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}

.mode-btn {
  font-family: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 18px 12px;
  border: 3px solid #e2e8f0;
  border-radius: 16px;
  background: white;
  cursor: pointer;
  transition: all 0.2s;
}

.mode-btn:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
}

.mode-btn.selected {
  border-color: var(--primary);
  background: #ecfdf5;
  box-shadow: 0 4px 12px rgba(5, 150, 105, 0.25);
}

.mode-icon {
  font-size: 2rem;
}

.mode-title {
  font-weight: 600;
  font-size: 1rem;
}

.mode-desc {
  font-size: 0.75rem;
  color: var(--text-light);
  text-align: center;
}

.btn {
  font-family: inherit;
  font-size: 1.1rem;
  font-weight: 600;
  padding: 14px 28px;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark);
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(5, 150, 105, 0.4);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: default;
}

.btn-secondary {
  background: #edf2f7;
  color: var(--text);
}

.btn-secondary:hover {
  background: #e2e8f0;
}

/* ===== Game Screen ===== */

.game-card {
  position: relative;
  padding: 24px 20px 20px;
}

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  gap: 16px;
}

.progress-info {
  flex: 1;
}

.progress-info span {
  font-size: 0.9rem;
  color: var(--text-light);
  font-weight: 500;
}

.progress-bar {
  height: 8px;
  background: #edf2f7;
  border-radius: 99px;
  margin-top: 6px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 99px;
  transition: width 0.4s ease;
  width: 5%;
}

.game-badges {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
}

.score-badge {
  background: var(--secondary);
  padding: 8px 16px;
  border-radius: 99px;
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
}

.streak-badge {
  background: linear-gradient(135deg, #f97316, #dc2626);
  color: white;
  padding: 8px 14px;
  border-radius: 99px;
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  animation: streakPop 0.35s ease-out;
}

@keyframes streakPop {
  0% { transform: scale(0.7); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

/* Play area (falling shapes) */
.play-area {
  --shape-size: 80px;
  position: relative;
  height: min(58vh, 460px);
  min-height: 320px;
  border-radius: 16px;
  background:
    linear-gradient(180deg, rgba(5, 150, 105, 0.04), rgba(5, 150, 105, 0.10)),
    repeating-linear-gradient(
      45deg,
      transparent 0 24px,
      rgba(5, 150, 105, 0.03) 24px 48px
    );
  border: 2px dashed rgba(5, 150, 105, 0.18);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  overflow: visible;
}

/* Floor line at the bottom of the play area */
.play-area::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 0;
  height: 3px;
  background: repeating-linear-gradient(
    90deg,
    var(--primary) 0 12px,
    transparent 12px 24px
  );
  border-radius: 2px;
  opacity: 0.5;
  pointer-events: none;
}

.falling-shape {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--shape-size);
  height: var(--shape-size);
  cursor: grab;
  touch-action: none;
  will-change: transform;
  z-index: 5;
}

.falling-shape svg {
  width: 100%;
  height: 100%;
  display: block;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.18));
  pointer-events: none;
}

.falling-shape.dragging {
  cursor: grabbing;
  z-index: 10;
}

.falling-shape.dragging svg {
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.28));
  transform: scale(1.05);
  transition: transform 0.15s;
}

.falling-shape.snapping {
  transition: transform 0.35s cubic-bezier(0.5, 0, 0.5, 1.2),
              opacity 0.35s 0.15s;
  cursor: default;
  pointer-events: none;
}

.falling-shape.leaving {
  transition: opacity 0.3s;
  pointer-events: none;
}

/* Slot row (drop targets) */
.slot-row {
  --shape-size: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
  min-height: calc(var(--shape-size) + 24px);
  flex-wrap: wrap;
}

.slot {
  width: calc(var(--shape-size) + 20px);
  height: calc(var(--shape-size) + 20px);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  background: #f7fafc;
  border: 3px dashed #cbd5e0;
  transition: transform 0.2s, background 0.3s, border-color 0.3s,
              box-shadow 0.3s;
  position: relative;
  padding: 6px;
}

.slot svg {
  width: 100%;
  height: 100%;
  opacity: 0.55;
}

.slot.pulse {
  animation: slotPulse 0.6s ease-in-out;
}

@keyframes slotPulse {
  0%, 100% { transform: scale(1); }
  50% {
    transform: scale(1.12);
    background: rgba(5, 150, 105, 0.12);
    border-color: var(--primary);
  }
}

.slot.ghost-hint {
  border-color: currentColor;
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.6),
              0 0 22px currentColor;
  animation: ghostGlow 1.4s ease-in-out infinite;
}

.slot.ghost-hint svg {
  opacity: 0.9;
}

@keyframes ghostGlow {
  0%, 100% { box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.5), 0 0 14px currentColor; }
  50% { box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.7), 0 0 28px currentColor; }
}

.slot.correct-flash {
  background: #d4faf0;
  border-color: var(--success);
  border-style: solid;
  box-shadow: 0 0 20px rgba(6, 214, 160, 0.5);
  animation: correctPop 0.5s ease-out;
}

@keyframes correctPop {
  0% { transform: scale(1); }
  40% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

.slot.wrong-flash {
  background: #fde8ed;
  border-color: var(--error);
  border-style: solid;
  animation: wrongShake 0.35s ease-in-out;
}

@keyframes wrongShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* Toast messages */
.toast-container {
  position: absolute;
  top: 76px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  z-index: 30;
}

.toast {
  padding: 10px 18px;
  border-radius: 99px;
  font-weight: 700;
  font-size: 0.95rem;
  color: white;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
  white-space: nowrap;
  animation: toastPop 1.4s ease-out forwards;
}

.toast.toast-correct { background: var(--success); }
.toast.toast-wrong   { background: var(--error); }
.toast.toast-streak  { background: linear-gradient(135deg, #f97316, #dc2626); }

@keyframes toastPop {
  0%   { opacity: 0; transform: translateY(14px) scale(0.7); }
  18%  { opacity: 1; transform: translateY(0)    scale(1.08); }
  30%  { transform: translateY(0)    scale(1); }
  75%  { opacity: 1; transform: translateY(-8px) scale(1); }
  100% { opacity: 0; transform: translateY(-32px) scale(0.85); }
}

.hidden {
  display: none !important;
}

/* ===== Results Screen ===== */

.results-card {
  text-align: center;
}

.results-emoji {
  font-size: 4rem;
  margin-bottom: 8px;
}

.results-name {
  color: var(--text-light);
  margin-bottom: 20px;
}

.results-score-box {
  background: linear-gradient(135deg, #34d399, #059669);
  color: white;
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 16px;
}

.results-label {
  display: block;
  font-size: 0.95rem;
  opacity: 0.85;
}

.final-score {
  display: block;
  font-size: 3.5rem;
  font-weight: 700;
  line-height: 1.2;
}

.results-stats {
  color: var(--text-light);
  margin-bottom: 10px;
  font-size: 0.95rem;
}

.results-stats strong {
  color: var(--text);
}

.results-message {
  color: var(--text-light);
  margin-bottom: 24px;
}

.results-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}

.save-confirmation {
  color: var(--success);
  font-weight: 600;
  font-size: 0.95rem;
}

/* ===== Leaderboards ===== */

.leaderboard-section {
  margin-top: 28px;
}

.leaderboards-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.leaderboard-card h2 {
  text-align: center;
  margin-bottom: 16px;
  font-size: 1.1rem;
}

.leaderboard-card.highlight {
  box-shadow: 0 0 0 3px var(--primary), var(--shadow);
}

.leaderboard-empty {
  text-align: center;
  color: var(--text-light);
  padding: 12px;
  font-size: 0.9rem;
}

.leaderboard-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.leaderboard-item {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: #f7fafc;
  border-radius: 12px;
  font-size: 0.9rem;
}

.leaderboard-item:first-child {
  background: linear-gradient(135deg, #fff9db, #ffe06633);
  border: 2px solid var(--secondary);
}

.rank {
  font-weight: 700;
  color: var(--primary);
  text-align: center;
}

.lb-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-score {
  font-weight: 700;
  color: var(--primary);
}

@media (max-width: 720px) {
  .leaderboards-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  .mode-buttons {
    grid-template-columns: 1fr;
  }

  .card {
    padding: 24px 18px;
  }

  .game-card {
    padding: 20px 14px 14px;
  }

  .toast-container {
    top: 68px;
  }
}
