:root {
  /* Cozy Color Tokens */
  --bg-primary: hsl(36, 40%, 96%);
  /* Creamy Sand #FAF6F0 */
  --bg-secondary: hsl(36, 25%, 90%);
  --text-primary: hsl(12, 20%, 30%);
  /* Espresso brown #5C4A43 */
  --text-secondary: hsl(12, 10%, 50%);
  --card-bg: hsl(36, 30%, 98%);
  --border-color: hsl(36, 15%, 82%);
  --shadow-color: hsla(12, 20%, 30%, 0.08);
  --tile-border: 2px solid hsla(12, 20%, 30%, 0.15);

  /* Region Harmony Colors (High Contrast Cozy Theme) */
  --region-0: hsl(343, 90%, 80%);
  /* Rich Cozy Pink */
  --region-1: hsl(36, 95%, 72%);
  /* Golden Honey Orange */
  --region-2: hsl(175, 60%, 65%);
  /* Sage Teal */
  --region-3: hsl(255, 60%, 78%);
  /* Warm Lavender */
  --region-4: hsl(200, 80%, 74%);
  /* Ocean Sky Blue */
  --region-5: hsl(48, 95%, 72%);
  /* Custard Yellow */
  --region-6: hsl(110, 60%, 70%);
  /* Matcha Green */
  --region-7: hsl(12, 85%, 78%);
  /* Peach Pink */
  --region-8: hsl(280, 65%, 78%);
  /* Plum Lilac */
  --region-9: hsl(140, 60%, 70%);
  /* Crisp Mint */

  /* UI Interactive Tokens */
  --glow-gold: 0 0 20px 6px hsla(45, 100%, 65%, 0.85);
  --error-red: hsl(0, 80%, 65%);
  --error-bg: hsl(0, 80%, 95%);

  --border-radius-premium: 20px;
  --border-radius-tile: 12px;
  --transition-smooth: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-bounce: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);

  /* Font Stack */
  --font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* Dark Mode Tokens */
body.dark-mode {
  --bg-primary: hsl(36, 12%, 12%);
  --bg-secondary: hsl(36, 15%, 18%);
  --text-primary: hsl(36, 20%, 90%);
  --text-secondary: hsl(36, 10%, 70%);
  --card-bg: hsl(36, 12%, 16%);
  --border-color: hsl(36, 12%, 24%);
  --shadow-color: rgba(0, 0, 0, 0.3);
  --tile-border: 2px solid hsla(36, 20%, 90%, 0.22);

  /* Dimmer Region Colors for Dark Mode readability */
  --region-0: hsl(343, 65%, 38%);
  --region-1: hsl(36, 70%, 35%);
  --region-2: hsl(175, 55%, 30%);
  --region-3: hsl(255, 50%, 38%);
  --region-4: hsl(200, 60%, 35%);
  --region-5: hsl(48, 70%, 32%);
  --region-6: hsl(110, 50%, 32%);
  --region-7: hsl(12, 60%, 36%);
  --region-8: hsl(280, 50%, 36%);
  --region-9: hsl(140, 50%, 32%);
}

/* Global Reset & Base */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background-color 0.4s ease, color 0.4s ease;
}

/* Core Container */
.app-container {
  width: 100%;
  max-width: 500px;
  height: 100%;
  max-height: 900px;
  display: flex;
  flex-direction: column;
  background-color: var(--card-bg);
  box-shadow: 0 10px 40px var(--shadow-color);
  position: relative;
  overflow: hidden;
  border-radius: 0;
}

@media (min-width: 500px) and (min-height: 600px) {
  .app-container {
    border-radius: var(--border-radius-premium);
    height: 94vh;
  }
}

/* Header styling */
.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 24px 8px;
  z-index: 10;
}

.level-title {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  color: var(--text-primary);
}

.icon-btn {
  background: var(--bg-secondary);
  border: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  color: var(--text-primary);
  cursor: pointer;
  transition: var(--transition-bounce);
}

.icon-btn:hover {
  transform: scale(1.08);
}

.icon-btn:active {
  transform: scale(0.94);
}

.icon-btn svg {
  width: 22px;
  height: 22px;
}

/* Dashboard Badges */
.dashboard {
  display: flex;
  justify-content: space-between;
  padding: 8px 24px;
  gap: 16px;
}

.badge {
  background: var(--bg-primary);
  border: 2px solid var(--border-color);
  border-radius: 50px;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  box-shadow: 0 4px 8px var(--shadow-color);
  transition: var(--transition-smooth);
}

.badge.success {
  border-color: hsl(140, 50%, 45%);
  background-color: hsl(140, 50%, 94%);
  color: hsl(140, 50%, 25%);
}

.badge-icon {
  font-size: 1.4rem;
  display: flex;
  align-items: center;
}

.badge-text {
  font-size: 1.1rem;
  font-weight: 800;
}

.hearts-list {
  display: flex;
  gap: 4px;
}

.heart-icon {
  font-size: 1.1rem;
  transition: var(--transition-bounce);
}

.heart-icon.lost {
  opacity: 0.15;
  transform: scale(0.7) rotate(-20deg);
}

/* Rules & Tabs Navigation */
.help-tabs {
  display: flex;
  padding: 12px 24px;
  gap: 8px;
  justify-content: space-between;
}

.tab-btn {
  flex: 1;
  background: var(--bg-secondary);
  border: 2px solid transparent;
  color: var(--text-secondary);
  font-family: var(--font-family);
  font-weight: 600;
  font-size: 0.8rem;
  padding: 10px 4px;
  border-radius: 12px;
  cursor: pointer;
  transition: var(--transition-smooth);
  text-align: center;
}

.tab-btn.active {
  background: var(--card-bg);
  border-color: var(--text-primary);
  color: var(--text-primary);
  box-shadow: 0 4px 8px var(--shadow-color);
  transform: translateY(-1px);
}

/* Grid Arena */
.grid-outer-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px 24px;
  overflow: hidden;
}

.game-grid {
  display: grid;
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: 380px;
  gap: 6px;
  background: var(--border-color);
  padding: 8px;
  border-radius: var(--border-radius-premium);
  box-shadow: 0 8px 24px var(--shadow-color);
  position: relative;
}

/* Grid Tiles */
.grid-tile {
  position: relative;
  aspect-ratio: 1 / 1;
  min-width: 0;
  min-height: 0;
  border-radius: var(--border-radius-tile);
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: var(--dynamic-tile-size, 2rem);
  transition: background-color 0.2s ease, transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.2s ease;
  overflow: visible;
  border: var(--tile-border);
}

.grid-tile:active {
  transform: scale(0.92);
}

/* Region Colors Classes */
.region-c0 {
  background-color: var(--region-0);
}

.region-c1 {
  background-color: var(--region-1);
}

.region-c2 {
  background-color: var(--region-2);
}

.region-c3 {
  background-color: var(--region-3);
}

.region-c4 {
  background-color: var(--region-4);
}

.region-c5 {
  background-color: var(--region-5);
}

.region-c6 {
  background-color: var(--region-6);
}

.region-c7 {
  background-color: var(--region-7);
}

.region-c8 {
  background-color: var(--region-8);
}

.region-c9 {
  background-color: var(--region-9);
}

/* Tile States & Helpers overlays */
.grid-tile .marker-x {
  font-size: var(--dynamic-marker-size, 1.5rem);
  font-weight: 800;
  color: white;
  opacity: 0.85;
  animation: fadeIn 0.2s ease;
  pointer-events: none;
}

.grid-tile .marker-question {
  font-size: var(--dynamic-marker-size, 1.5rem);
  font-weight: 800;
  color: white;
  opacity: 0.9;
  animation: fadeIn 0.2s ease;
  pointer-events: none;
}

/* Snack Item styling */
.snack-item {
  font-size: var(--dynamic-snack-size, 2.2rem);
  display: flex;
  justify-content: center;
  align-items: center;
  animation: bounceIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
  z-index: 2;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.15));
}

.grid-tile.glow {
  box-shadow: var(--glow-gold);
  z-index: 3;
}

/* Dynamic visual help highlights */
.grid-tile.highlight-region {
  filter: saturate(1.4) brightness(1.05);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.6);
}

.grid-tile.highlight-axis {
  filter: brightness(0.92);
}

.grid-tile.highlight-touch {
  filter: brightness(0.85) saturate(1.2);
  box-shadow: inset 0 0 0 3px rgba(255, 69, 0, 0.4);
}

/* Error flash state */
.grid-tile.error-flash {
  background-color: var(--error-red) !important;
  animation: shake 0.4s ease-in-out;
}

/* Footer Section */
.game-footer {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  background-color: transparent;
  z-index: 1;
}

.footer-wave {
  width: 100%;
  position: relative;
  display: flex;
  justify-content: center;
}

.footer-wave svg {
  display: block;
  width: 100%;
  height: auto;
}

.footer-mascot-container {
  position: absolute;
  bottom: 0px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
}

.footer-mascot {
  font-size: 4rem;
  display: inline-block;
  animation: floatMascot 4s ease-in-out infinite;
  filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.1));
}

.footer-content {
  background-color: #E3F2FD;
  width: 100%;
  text-align: center;
  padding: 10px 0 20px;
  z-index: 2;
}

.footer-text {
  font-size: 1.1rem;
  font-weight: 800;
  color: hsl(200, 80%, 30%);
  letter-spacing: -0.2px;
}

/* Modals & Overlays */
.modal {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(92, 74, 67, 0.4);
  /* Espresso overlay */
  backdrop-filter: blur(8px);
  z-index: 100;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.modal.hidden {
  opacity: 0;
  pointer-events: none;
}

/* Modal Content Card */
.card {
  background-color: var(--card-bg);
  border-radius: var(--border-radius-premium);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 380px;
  padding: 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  border: 1px solid var(--border-color);
  animation: scaleUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-title {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--text-primary);
  margin-bottom: 24px;
}

/* Settings Items */
.setting-item {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid var(--border-color);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text-primary);
}

.setting-item:last-of-type {
  border-bottom: none;
  margin-bottom: 20px;
}

/* Toggle Switches */
.switch {
  position: relative;
  display: inline-block;
  width: 52px;
  height: 28px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--bg-secondary);
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .3s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

input:checked+.slider {
  background-color: var(--text-primary);
}

input:checked+.slider:before {
  transform: translateX(24px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

/* Buttons */
.btn {
  font-family: var(--font-family);
  font-weight: 800;
  font-size: 1rem;
  padding: 12px 24px;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  transition: var(--transition-bounce);
  width: 100%;
  margin-top: 10px;
}

.btn:active {
  transform: scale(0.95);
}

.btn-primary {
  background: var(--text-primary);
  color: var(--card-bg);
  box-shadow: 0 6px 16px hsla(12, 20%, 30%, 0.25);
}

.btn-secondary {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.btn-large {
  padding: 16px 32px;
  font-size: 1.15rem;
  border-radius: 16px;
}

/* Victory Screen Details */
.win-emoji-container {
  width: 90px;
  height: 90px;
  background: hsl(45, 100%, 92%);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
  box-shadow: 0 8px 16px rgba(255, 193, 7, 0.15);
}

.win-emoji {
  font-size: 3rem;
  animation: winJump 0.8s ease infinite alternate;
}

.win-subtitle {
  color: var(--text-secondary);
  font-weight: 500;
  margin-bottom: 20px;
}

.win-stats {
  display: flex;
  gap: 16px;
  width: 100%;
  margin-bottom: 24px;
}

.stat-box {
  flex: 1;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  padding: 12px;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
}

.stat-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--text-primary);
  margin-top: 4px;
}

/* GameOver Modal */
.gameover-emoji {
  font-size: 4rem;
  margin-bottom: 16px;
  animation: wobble 1s infinite;
}

.gameover-subtitle {
  color: var(--text-secondary);
  margin-bottom: 24px;
}

/* Main Menu & Level Grid */
.menu-logo-container {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.logo-emoji {
  font-size: 3rem;
  animation: floatMascot 4s ease-in-out infinite;
}

.logo-emoji:nth-child(2) {
  animation-delay: 0.5s;
}

.logo-emoji:nth-child(3) {
  animation-delay: 1s;
}

.menu-title {
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--text-primary);
  letter-spacing: -1px;
}

.menu-tagline {
  color: var(--text-secondary);
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 24px;
}

.level-selector-container {
  width: 100%;
  margin-bottom: 24px;
}

.level-selector-container h3 {
  font-size: 1rem;
  color: var(--text-secondary);
  text-align: left;
  margin-bottom: 12px;
}

.level-selector-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  max-height: 180px;
  overflow-y: auto;
  padding: 4px;
}

.level-btn {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  aspect-ratio: 1;
  border-radius: 10px;
  font-family: var(--font-family);
  font-weight: 800;
  font-size: 0.95rem;
  color: var(--text-primary);
  cursor: pointer;
  transition: var(--transition-bounce);
  display: flex;
  justify-content: center;
  align-items: center;
}

.level-btn.completed {
  background: hsl(140, 50%, 92%);
  border-color: hsl(140, 50%, 80%);
  color: hsl(140, 50%, 30%);
}

.level-btn.active {
  background: var(--text-primary);
  border-color: var(--text-primary);
  color: var(--card-bg);
}

.level-btn.locked {
  opacity: 0.35;
  cursor: not-allowed;
}

/* Animations */
@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 0.85;
  }
}

@keyframes bounceIn {
  0% {
    transform: scale(0);
    opacity: 0;
  }

  60% {
    transform: scale(1.15);
  }

  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes floatMascot {

  0%,
  100% {
    transform: translateY(0) rotate(0deg);
  }

  50% {
    transform: translateY(-10px) rotate(5deg);
  }
}

@keyframes winJump {
  0% {
    transform: translateY(0) scale(1);
  }

  100% {
    transform: translateY(-12px) scale(1.05);
  }
}

@keyframes scaleUp {
  from {
    transform: scale(0.9);
    opacity: 0;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes wobble {

  0%,
  100% {
    transform: rotate(0);
  }

  25% {
    transform: rotate(-8deg) scale(1.05);
  }

  75% {
    transform: rotate(8deg) scale(1.05);
  }
}

@keyframes shake {

  0%,
  100% {
    transform: translateX(0);
  }

  20%,
  60% {
    transform: translateX(-8px);
  }

  40%,
  80% {
    transform: translateX(8px);
  }
}

/* Cheat Mode Highlight styling */
body.cheat-active .grid-tile.cheat-tile {
  filter: brightness(0.88) saturate(1.15);
  box-shadow: inset 0 0 0 3px hsla(45, 100%, 45%, 0.5);
}

.tutorial-btn {
  background: hsl(200, 40%, 90%) !important;
  border-color: hsl(200, 40%, 80%) !important;
  color: hsl(200, 50%, 25%) !important;
}

.tutorial-btn.active {
  background: hsl(200, 80%, 40%) !important;
  border-color: hsl(200, 80%, 40%) !important;
  color: white !important;
}

.grid-tile .marker-x.wrong-x-mark {
  color: hsl(0, 85%, 60%) !important;
  font-weight: 900 !important;
  opacity: 1 !important;
  transform: scale(1.15);
  text-shadow: 0 0 8px rgba(255, 0, 0, 0.4);
}

@keyframes hintRevealPulse {
  0% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.8);
    border-color: #f59e0b;
  }

  30% {
    transform: scale(1.5);
    box-shadow: 0 0 25px 8px rgba(245, 158, 11, 0.9);
    border-color: #f59e0b;
  }

  70% {
    transform: scale(0.95);
    box-shadow: 0 0 35px 12px rgba(245, 158, 11, 0.4);
    border-color: #f59e0b;
  }

  100% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(245, 158, 11, 0);
  }
}

.grid-tile.hint-pulse-reveal {
  animation: hintRevealPulse 1s ease-in-out 1;
  animation-iteration-count: 3;
  z-index: 10;
  position: relative;
}