:root {
  --bg: #e8f5d8;
  --ink: #2a3b1f;
  --on-accent: #2a3b1f;
  --accent: #f0a83a;
  --wall: #6b8f4e;
  --path: #fff8e7;
  --btn: #ffd66b;
  --radius: 20px;
  /* System fonts only — no remote font fetch on first paint */
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

body[data-theme="forest"]  { --bg: #e8f5d8; --ink: #2a3b1f; --on-accent: #2a3b1f; --wall: #6b8f4e; --path: #fff8e7; --accent: #f0a83a; }
body[data-theme="ocean"]   { --bg: #d9f1f7; --ink: #1a3a4a; --on-accent: #1a3a4a; --wall: #3aa0b8; --path: #eefcff; --accent: #ff8fab; }
body[data-theme="space"]   { --bg: #1b2340; --ink: #f2e9ff; --on-accent: #1b2340; --wall: #6b5b95; --path: #2a3358; --accent: #ffd36a; }
body[data-theme="candy"]   { --bg: #ffe4f1; --ink: #6b2048; --on-accent: #6b2048; --wall: #e8789a; --path: #fff5fa; --accent: #ffb347; }
body[data-theme="dino"]    { --bg: #e5f2c9; --ink: #2f4a1c; --on-accent: #2f4a1c; --wall: #7a9e3e; --path: #f4fadb; --accent: #c4a035; }
body[data-theme="farm"]    { --bg: #f3e6c8; --ink: #4a3420; --on-accent: #4a3420; --wall: #a67c52; --path: #fff8e8; --accent: #7cb342; }
body[data-theme="pirate"]  { --bg: #d8ecf5; --ink: #1e3a4c; --on-accent: #1e3a4c; --wall: #3d6f8c; --path: #eef7fb; --accent: #e0a100; }
body[data-theme="snow"]    { --bg: #e8f2ff; --ink: #243b55; --on-accent: #243b55; --wall: #7aa0c4; --path: #f7fbff; --accent: #5ec4d4; }
body[data-theme="volcano"] { --bg: #2b1a18; --ink: #ffe7d6; --on-accent: #2b1a18; --wall: #a04830; --path: #3a2420; --accent: #ff7a3d; }
body[data-theme="garden"]  { --bg: #eef8e4; --ink: #35522a; --on-accent: #35522a; --wall: #7cbc6a; --path: #f7fff0; --accent: #f0a0c0; }
body[data-theme="train"]   { --bg: #e7eef8; --ink: #243048; --on-accent: #243048; --wall: #5a7aa8; --path: #f4f7fc; --accent: #e25b4c; }
body[data-theme="castle"]  { --bg: #efe6f5; --ink: #3a2a55; --on-accent: #3a2a55; --wall: #8a6fb0; --path: #faf6ff; --accent: #d4a017; }
body[data-theme="desert"]  { --bg: #f6e3c0; --ink: #5a3a18; --on-accent: #5a3a18; --wall: #c49a5a; --path: #fff6e4; --accent: #2f9e7a; }
body[data-theme="rainbow"] { --bg: #fff0e8; --ink: #4a2a50; --on-accent: #4a2a50; --wall: #e07a9a; --path: #fffaf5; --accent: #5ec4e0; }
body[data-theme="moon"]    { --bg: #1a1f3a; --ink: #e8e4ff; --on-accent: #1a1f3a; --wall: #5a6088; --path: #262b4a; --accent: #f0d878; }

html {
  height: 100%;
}
body {
  margin: 0;
  background: radial-gradient(circle at 20% 20%, #fff6, transparent 40%), var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding:
    env(safe-area-inset-top, 0px)
    env(safe-area-inset-right, 0px)
    env(safe-area-inset-bottom, 0px)
    env(safe-area-inset-left, 0px);
  box-sizing: border-box;
}
#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}
.screen { display: none; padding: 1.25rem; max-width: 480px; margin: 0 auto; box-sizing: border-box; width: 100%; }
.screen.active { display: flex; flex-direction: column; gap: 1rem; flex: 1; min-height: 0; box-sizing: border-box; }
/* Home + play use the full viewport */
#screen-home.screen,
#screen-play.screen {
  max-width: none;
  width: 100%;
  padding: 0.5rem clamp(0.45rem, 1.5vw, 1.25rem);
  gap: 0.45rem;
}
#screen-home.active { overflow: hidden; }
#screen-home .brand {
  margin: 0.15rem 0 0;
  flex-shrink: 0;
  font-size: clamp(1.45rem, 5.5vw, 2.2rem);
}
#screen-home .tagline {
  margin: 0;
  text-align: center;
  flex-shrink: 0;
  font-size: clamp(0.85rem, 2.8vw, 1.05rem);
  opacity: 0.85;
}
/* Dense emoji grid — fit all levels on one screen, no scroll */
.level-grid {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
  gap: clamp(0.2rem, 0.8vw, 0.4rem);
  overflow: hidden;
  align-content: stretch;
}
@media (min-width: 700px) {
  .level-grid { grid-template-columns: repeat(11, minmax(0, 1fr)); }
}
.level-tile {
  position: relative;
  min-width: 0;
  min-height: 0;
  height: 100%;
  margin: 0;
  padding: 0.1rem;
  border: 2px solid color-mix(in srgb, var(--wall) 55%, transparent);
  border-radius: 12px;
  background: var(--path);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.05rem;
  font-weight: 800;
  touch-action: manipulation;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.level-tile:active { transform: scale(0.94); }
.level-tile .tile-emoji {
  font-size: clamp(0.85rem, 3.6vmin, 1.55rem);
  line-height: 1;
}
.level-tile .tile-num {
  font-size: clamp(0.55rem, 2vmin, 0.8rem);
  line-height: 1;
  opacity: 0.75;
}
.level-tile.cleared {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 45%, transparent);
}
.level-tile.cleared::after {
  content: "✓";
  position: absolute;
  top: 1px;
  right: 3px;
  font-size: clamp(0.5rem, 1.8vmin, 0.75rem);
  color: var(--accent);
  line-height: 1;
}
.brand { font-size: clamp(2.4rem, 10vw, 3.2rem); margin: 2rem 0 0.5rem; text-align: center; }
.btn { min-height: 52px; min-width: 52px; border: none; border-radius: var(--radius); background: var(--btn); color: var(--on-accent); font-size: 1.15rem; font-weight: 800; touch-action: manipulation; }
.btn.primary { background: var(--accent); }
.btn.ghost { background: transparent; color: var(--ink); border: 3px solid color-mix(in srgb, var(--ink) 25%, transparent); }
/* Maze + D-pad share the leftover play area */
.play-body {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}
.maze-host {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.maze {
  --maze-n: 10;
  --maze-side: min(92vw, calc(100dvh - 14rem));
  display: grid;
  /* Fallback size; JS fitMaze() overrides with exact pixels */
  width: var(--maze-side);
  height: var(--maze-side);
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1;
  margin: 0;
  flex-shrink: 0;
  background: var(--wall);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: inset 0 0 0 3px color-mix(in srgb, var(--ink) 12%, transparent);
}
.cell { position: relative; background: var(--path); box-sizing: border-box; border-style: solid; border-color: var(--wall); border-width: 0; }
.cell.player::after, .cell.goal::before {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: clamp(6px, calc(var(--maze-side) / var(--maze-n) * 0.62), 48px);
  line-height: 1;
}
.cell.goal::before { content: attr(data-goal); }
.cell.player::after { content: attr(data-player); z-index: 1; }
.cell.player.bump { animation: bump 0.28s ease; }
@keyframes bump { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
.dpad {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 0.35rem;
  flex-shrink: 0;
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom, 0px));
}
.dpad-mid { display: flex; gap: 2.5rem; }
.dpad-btn { width: 72px; height: 72px; font-size: 1.6rem; border-radius: 18px; background: var(--accent); }

/*
  Wide / landscape: D-pad sits beside the maze; center the maze+D-pad pair
  as one group along the long axis (don't pin D-pad to the far right).
*/
@media (orientation: landscape), (min-aspect-ratio: 5 / 4) {
  .play-body {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: 0.85rem;
  }
  .maze-host {
    flex: 0 1 auto;
    width: auto;
    height: 100%;
    max-height: 100%;
    aspect-ratio: 1 / 1;
    max-width: calc(100% - 10.5rem);
  }
  .dpad {
    width: min(9.5rem, 28vw);
    padding: 0.25rem 0.15rem;
    align-self: center;
  }
  .dpad-mid { gap: 0.85rem; }
  .dpad-btn {
    width: clamp(56px, 12vmin, 72px);
    height: clamp(56px, 12vmin, 72px);
    font-size: clamp(1.25rem, 4vmin, 1.6rem);
  }
}
.win-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; }
.win-overlay.hidden { display: none; }
.loading-overlay {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  display: grid;
  place-items: center;
  z-index: 20;
}
.loading-overlay.hidden { display: none; }
.loading-text {
  font-size: 1.35rem;
  font-weight: 800;
  margin: 0;
  padding: 1rem 1.4rem;
  border-radius: var(--radius);
  background: var(--path);
  color: var(--ink);
}
.win-card { background: var(--path); color: var(--ink); padding: 1.5rem; border-radius: 24px; text-align: center; display: flex; flex-direction: column; gap: 0.75rem; min-width: 240px; }
.win-countdown {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  opacity: 0.85;
}
.win-countdown.is-last {
  opacity: 1;
  color: var(--accent);
}
.play-bar { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.play-bar h2 { font-size: 1.1rem; margin: 0; text-align: center; flex: 1; }
@media (prefers-reduced-motion: reduce) {
  .cell.player.bump { animation: none; }
}
