/* ========== RESET & BASE ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Kanit', system-ui, sans-serif;
  background: #1a3a1a;
  color: #2c3e2d;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: manipulation;
}

/* ========== SCREENS ========== */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1;
}
.screen.active { display: flex; }

.bg-forest {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, #87CEEB 0%, #a8d5a2 40%, #5a9e4a 70%, #3d7a30 100%);
  z-index: -1;
}
.bg-forest::before {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at 20% 80%, rgba(255,255,200,0.15) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 20%, rgba(255,255,255,0.2) 0%, transparent 40%);
  pointer-events: none;
}

/* ========== SOUND TOGGLE ========== */
.sound-toggle {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 100;
  background: rgba(255,255,255,0.85);
  backdrop-filter: blur(8px);
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: opacity 0.2s;
}
.sound-toggle:active { opacity: 0.7; }
#sound-icon { font-size: 16px; }

/* ========== HOME ========== */
.home-content {
  text-align: center;
  padding: 20px;
  max-width: 420px;
  width: 100%;
}
.logo-area { margin-bottom: 28px; }
.squirrel-top {
  font-size: 48px;
  animation: bob 2.5s ease-in-out infinite;
}
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}
.game-title {
  font-size: 52px;
  font-weight: 700;
  line-height: 1.05;
  color: #2d5a27;
  text-shadow: 2px 2px 0 #fff, 0 4px 12px rgba(0,0,0,0.1);
  margin: 8px 0;
}
.game-title span { color: #e67e22; }
.tagline {
  font-size: 16px;
  color: #4a7040;
  font-weight: 400;
}

.btn-primary {
  background: linear-gradient(180deg, #6bcb4a 0%, #4caf50 100%);
  color: #fff;
  border: none;
  border-radius: 50px;
  padding: 16px 48px;
  font-size: 22px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 6px 0 #2e7d32, 0 8px 20px rgba(0,0,0,0.2);
  transition: transform 0.1s, box-shadow 0.1s;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
.btn-primary:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #2e7d32, 0 4px 10px rgba(0,0,0,0.2);
}
.btn-primary.big { font-size: 24px; padding: 18px 56px; margin: 20px 0; }
.btn-primary .leaf { font-size: 26px; }

.home-nav {
  display: flex;
  justify-content: center;
  gap: 28px;
  margin: 24px 0 16px;
}
.btn-icon {
  background: rgba(255,255,255,0.7);
  border: none;
  border-radius: 16px;
  width: 64px;
  height: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 3px 10px rgba(0,0,0,0.08);
  transition: transform 0.15s;
}
.btn-icon:active { transform: scale(0.92); }
.btn-icon span { font-size: 24px; }
.btn-icon small { font-size: 11px; color: #555; }

.home-hint {
  font-size: 13px;
  color: #5a7a50;
  margin-top: 8px;
}

/* ========== PANEL (shared) ========== */
.panel {
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(12px);
  border-radius: 24px;
  padding: 28px 24px;
  max-width: 480px;
  width: 92%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0,0,0,0.15);
  text-align: center;
}
.panel h2 {
  font-size: 24px;
  color: #2d5a27;
  margin-bottom: 20px;
}

.btn-secondary {
  background: #e8f5e9;
  color: #2d5a27;
  border: none;
  border-radius: 30px;
  padding: 12px 28px;
  font-size: 16px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  margin-top: 12px;
}
.btn-secondary:active { background: #c8e6c9; }

.btn-danger {
  background: #ffebee;
  color: #c62828;
  border: none;
  border-radius: 30px;
  padding: 10px 24px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  margin: 8px 0;
}

/* ========== MODE SELECT ========== */
.mode-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}
.mode-card {
  background: #f8fbf5;
  border-radius: 16px;
  padding: 14px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  border: 2px solid transparent;
  transition: border-color 0.2s;
  min-height: 0;
}
.mode-card:active { border-color: #81c784; }
.mode-icon { font-size: 32px; }
.mode-card h3 { font-size: 15px; color: #2d5a27; text-align: center; line-height: 1.25; }
.mode-card p { font-size: 11px; color: #666; line-height: 1.35; text-align: center; }
.btn-play {
  border: none;
  border-radius: 30px;
  padding: 8px 28px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  color: #fff;
  cursor: pointer;
  margin-top: 4px;
}
.btn-play.green { background: #4caf50; }
.btn-play.purple { background: #7e57c2; }
.btn-play.orange { background: #ff9800; }
.btn-play.teal { background: #00897b; }
.btn-play:active { opacity: 0.85; }

/* ========== GAME HEADER ========== */
.game-screen {
  background: linear-gradient(180deg, #c8e6c9 0%, #a5d6a7 100%);
  justify-content: flex-start;
}
.game-header {
  width: 100%;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255,255,255,0.75);
  backdrop-filter: blur(8px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  z-index: 10;
}
.mode-badge {
  font-size: 14px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 20px;
  background: #e8f5e9;
}
.mode-badge.leaves { background: #e8f5e9; color: #2e7d32; }
.mode-badge.firefly { background: #ede7f6; color: #5e35b1; }
.mode-badge.run { background: #fff3e0; color: #e65100; }
.mode-badge.match { background: #e0f2f1; color: #00695c; }
.header-stats {
  display: flex;
  gap: 16px;
  font-size: 14px;
}
.header-stats b { color: #1b5e20; }
.btn-pause {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
}

.game-footer {
  width: 100%;
  padding: 10px 16px;
  text-align: center;
  background: rgba(255,255,255,0.6);
  font-size: 13px;
  color: #555;
}

/* ========== LEAVES BOARD ========== */
.leaves-board {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  padding: 16px;
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  align-content: center;
}
.tile {
  aspect-ratio: 1;
  background: #fff;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  box-shadow: 0 3px 0 #c8e6c9, 0 4px 12px rgba(0,0,0,0.08);
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s, background 0.15s;
  position: relative;
}
.tile:active { transform: scale(0.92); }
.tile.correct {
  background: #c8e6c9;
  box-shadow: 0 0 0 3px #4caf50;
  animation: pop 0.3s ease;
}
.tile.wrong {
  background: #ffcdd2;
  box-shadow: 0 0 0 3px #e53935;
  animation: shake 0.4s ease;
}
.tile.disabled { pointer-events: none; opacity: 0.6; }
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

/* ========== FIREFLY ========== */
.firefly-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 16px;
  gap: 24px;
}
.sequence-display {
  background: rgba(30,40,60,0.85);
  border-radius: 20px;
  padding: 24px;
  min-height: 120px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}
.seq-hint {
  color: #b0bec5;
  font-size: 15px;
  margin-bottom: 12px;
}
.seq-symbols {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}
.seq-sym {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  transition: all 0.25s;
  opacity: 0.3;
}
.seq-sym.lit {
  opacity: 1;
  transform: scale(1.2);
  box-shadow: 0 0 20px rgba(255,255,150,0.8);
  background: rgba(255,255,200,0.25);
}
.player-buttons {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: center;
}
.pbtn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  background: #fff;
  font-size: 30px;
  cursor: pointer;
  box-shadow: 0 4px 0 #cfd8dc, 0 6px 16px rgba(0,0,0,0.12);
  transition: transform 0.1s, box-shadow 0.1s;
}
.pbtn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #cfd8dc, 0 2px 8px rgba(0,0,0,0.12);
}
.pbtn.correct-flash {
  background: #c8e6c9;
  box-shadow: 0 0 0 3px #4caf50;
}
.pbtn.wrong-flash {
  background: #ffcdd2;
  box-shadow: 0 0 0 3px #e53935;
}

/* ========== RUN GAME (DOM lanes) ========== */
.run-area {
  flex: 1;
  width: 100%;
  min-height: 300px;
  position: relative;
  overflow: hidden;
  background: linear-gradient(180deg, #7cb342 0%, #558b2f 50%, #33691e 100%);
  display: flex;
  flex-direction: column;
}
.run-path {
  flex: 1;
  position: relative;
  margin: 8px 12% 0;
  background: linear-gradient(180deg, #bcaaa4 0%, #a1887f 40%, #8d6e63 100%);
  border-radius: 12px 12px 0 0;
  overflow: hidden;
  box-shadow: inset 0 0 40px rgba(0,0,0,0.15);
}
.run-sky {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 18%;
  background: linear-gradient(180deg, rgba(135,206,235,0.35), transparent);
  pointer-events: none;
  z-index: 1;
}
.run-hint {
  position: absolute;
  top: 14px;
  left: 0; right: 0;
  text-align: center;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  text-shadow: 0 2px 6px rgba(0,0,0,0.5);
  z-index: 6;
  transition: opacity 0.5s;
  pointer-events: none;
}
.run-lanes {
  position: absolute;
  inset: 0;
  display: flex;
  z-index: 2;
}
.run-lane {
  flex: 1;
  position: relative;
  border-right: 2px dashed rgba(255,255,255,0.2);
}
.run-lane:last-child { border-right: none; }
.lane-mark {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
}
.run-player {
  position: absolute;
  bottom: 10%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  transition: left 0.12s ease-out;
  pointer-events: none;
  text-align: center;
}
.run-player .player-emoji {
  font-size: 44px;
  display: block;
  filter: drop-shadow(0 3px 5px rgba(0,0,0,0.35));
  animation: playerBob 0.6s ease-in-out infinite;
}
.run-player .player-shadow {
  display: block;
  width: 28px;
  height: 8px;
  margin: -4px auto 0;
  background: rgba(0,0,0,0.25);
  border-radius: 50%;
}
.run-player.hit .player-emoji {
  animation: playerHit 0.4s ease;
}
@keyframes playerBob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}
@keyframes playerHit {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}
.run-obstacle {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  pointer-events: none;
  animation: obsSway 1.2s ease-in-out infinite;
}
.run-obstacle .obs-emoji {
  font-size: 40px;
  display: block;
  filter: drop-shadow(0 4px 6px rgba(0,0,0,0.35));
}
.run-obstacle.obs-rock .obs-emoji { font-size: 42px; }
.run-obstacle.obs-log .obs-emoji { font-size: 38px; }
.run-obstacle.obs-mushroom .obs-emoji { font-size: 36px; }
.run-obstacle.obs-cactus .obs-emoji { font-size: 40px; }
.run-obstacle.obs-nest .obs-emoji { font-size: 36px; }
@keyframes obsSway {
  0%, 100% { transform: translateX(-50%) rotate(-3deg); }
  50% { transform: translateX(-50%) rotate(3deg); }
}
.run-trees-left,
.run-trees-right {
  position: absolute;
  bottom: 20%;
  font-size: 32px;
  opacity: 0.7;
  z-index: 1;
  pointer-events: none;
}
.run-trees-left { left: -8px; }
.run-trees-right { right: -8px; }
.run-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px 14px;
  flex-shrink: 0;
}
.run-progress {
  flex: 1;
  height: 10px;
  background: rgba(255,255,255,0.35);
  border-radius: 6px;
  overflow: hidden;
}
.run-progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #aed581, #7cb342);
  border-radius: 6px;
  transition: width 0.1s linear;
}
.ctrl-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.92);
  font-size: 26px;
  color: #2d5a27;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.18);
  font-family: inherit;
  flex-shrink: 0;
}
.ctrl-btn:active { background: #fff; transform: scale(0.92); }

/* ========== MATCH BOARD ========== */
.match-board {
  flex: 1;
  display: grid;
  gap: 8px;
  padding: 12px;
  width: 100%;
  max-width: 440px;
  margin: 0 auto;
  align-content: center;
  overflow-y: auto;
}
.match-board.match-cols-4 {
  grid-template-columns: repeat(4, 1fr);
  max-width: 360px;
  gap: 8px;
}
.match-board.match-cols-6 {
  grid-template-columns: repeat(6, 1fr);
  max-width: 420px;
  gap: 6px;
}
.match-board.match-cols-8 {
  grid-template-columns: repeat(8, 1fr);
  max-width: 480px;
  gap: 4px;
}
.match-card {
  aspect-ratio: 1;
  border-radius: 12px;
  cursor: pointer;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.3s ease, opacity 0.35s ease, scale 0.35s ease;
  box-shadow: 0 3px 0 #a5d6a7, 0 4px 10px rgba(0,0,0,0.1);
}
.match-cols-6 .match-card { border-radius: 10px; }
.match-cols-8 .match-card { border-radius: 8px; }
.match-card .match-back,
.match-card .match-front {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  border-radius: inherit;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.match-cols-6 .match-card .match-back,
.match-cols-6 .match-card .match-front { font-size: 20px; }
.match-cols-8 .match-card .match-back,
.match-cols-8 .match-card .match-front { font-size: 16px; }
.match-card .match-back {
  background: linear-gradient(145deg, #66bb6a, #2e7d32);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.15);
}
.match-card .match-front {
  background: #fff;
  transform: rotateY(180deg);
}
.match-card.flipped,
.match-card.matched {
  transform: rotateY(180deg);
}
.match-card.matched {
  box-shadow: 0 0 0 2px #81c784;
  pointer-events: none;
}
.match-card.gone {
  opacity: 0;
  transform: rotateY(180deg) scale(0.3);
  pointer-events: none;
  visibility: hidden;
}
.match-card:active:not(.flipped):not(.matched):not(.gone) {
  transform: scale(0.94);
}

/* ========== RESULT ========== */
.result-panel { max-width: 380px; }
.result-icon { font-size: 56px; margin-bottom: 8px; }
.result-stats {
  display: flex;
  justify-content: space-around;
  margin: 20px 0;
  gap: 8px;
}
.result-stats > div {
  background: #f1f8e9;
  border-radius: 12px;
  padding: 12px 10px;
  flex: 1;
}
.result-stats span {
  display: block;
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}
.result-stats b {
  font-size: 22px;
  color: #2e7d32;
}

/* ========== STATS ========== */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}
.stat-box {
  background: #f1f8e9;
  border-radius: 14px;
  padding: 16px 10px;
}
.stat-label {
  display: block;
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}
.stat-box b {
  font-size: 26px;
  color: #2e7d32;
}

/* ========== HOWTO ========== */
.howto-panel { text-align: left; }
.howto-item {
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e0e0e0;
}
.howto-item h3 { font-size: 16px; color: #2d5a27; margin-bottom: 4px; }
.howto-item p { font-size: 14px; color: #555; line-height: 1.45; }
.howto-note {
  font-size: 13px;
  color: #777;
  margin: 12px 0;
  line-height: 1.4;
}

/* ========== SETTINGS ========== */
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid #e0e0e0;
  font-size: 16px;
}
.toggle-btn {
  border: none;
  border-radius: 20px;
  padding: 6px 18px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  background: #cfd8dc;
  color: #555;
}
.toggle-btn.on {
  background: #4caf50;
  color: #fff;
}

/* ========== OVERLAYS ========== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.overlay.hidden { display: none; }
.overlay-box {
  background: #fff;
  border-radius: 24px;
  padding: 32px 28px;
  text-align: center;
  max-width: 340px;
  width: 88%;
  box-shadow: 0 16px 48px rgba(0,0,0,0.25);
}
.overlay-box h2 {
  font-size: 24px;
  color: #2d5a27;
  margin-bottom: 12px;
}
.overlay-box p {
  font-size: 15px;
  color: #555;
  margin-bottom: 16px;
}
.overlay-box .btn-primary,
.overlay-box .btn-secondary {
  display: block;
  width: 100%;
  margin: 8px 0;
}

/* ========== RESPONSIVE ========== */
@media (max-width: 380px) {
  .game-title { font-size: 42px; }
  .leaves-board { gap: 8px; padding: 12px; }
  .tile { font-size: 26px; }
  .pbtn { width: 56px; height: 56px; font-size: 26px; }
  .ctrl-btn { width: 60px; height: 60px; }
}
@media (min-width: 600px) {
  .mode-cards {
    grid-template-columns: 1fr 1fr;
    max-width: 480px;
    margin-left: auto;
    margin-right: auto;
  }
}
