:root {
  --brand-gold: #ffc20e;
  --brand-gold-dark: #d9a300;

  --bg-primary: #0b0f16;
  --bg-secondary: #131a26;
  --surface: #171f2e;
  --text-primary: #f2f5fa;
  --text-secondary: #9aa8bd;
  --border: rgba(255, 255, 255, 0.10);

  --chapter-accent: var(--brand-gold);
  --chapter-accent2: #64b5f6;
  --chapter-bg1: #1c2436;
  --chapter-bg2: #0d1220;

  --shadow-card: 0 20px 60px rgba(0, 0, 0, 0.45);
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --ease-bounce: cubic-bezier(0.175, 0.885, 0.32, 1.275);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg-primary: #f4f6fb;
    --bg-secondary: #ffffff;
    --surface: #ffffff;
    --text-primary: #101521;
    --text-secondary: #5a6579;
    --border: rgba(10, 15, 25, 0.10);
    --shadow-card: 0 16px 44px rgba(20, 30, 50, 0.16);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--cb-font-family, system-ui), -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

.muted { color: var(--text-secondary); }

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
  position: relative;
  background: radial-gradient(1200px 600px at 20% -10%, rgba(255, 194, 14, 0.08), transparent 60%),
              var(--bg-primary);
}

/* ---------- header / HUD ---------- */
header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 14px 10px;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.18), transparent);
  flex: 0 0 auto;
  z-index: 5;
}

.icon-btn {
  width: 42px;
  height: 42px;
  min-width: 42px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-primary);
  font-size: 1.2rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s var(--ease-out), background 0.2s;
}
.icon-btn:hover { background: var(--bg-secondary); }
.icon-btn:active { transform: scale(0.92); }

.brand {
  font-size: 1.3rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.brand b { color: var(--brand-gold); }

.hud-chips {
  margin-left: auto;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 700;
  white-space: nowrap;
}
.chip-ic { font-size: 0.95rem; line-height: 1; }
.chip-lives { border-color: rgba(255, 82, 82, 0.4); }
.chip-play { border-color: rgba(255, 194, 14, 0.4); }
.chip-earn { border-color: rgba(124, 77, 255, 0.4); }

/* ---------- screens ---------- */
main {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  overflow-y: auto;
  padding: 16px;
  opacity: 0;
}

.screen.active {
  display: flex;
  animation: screenIn 0.32s var(--ease-out);
  opacity: 1;
}

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

.glass-card {
  background: linear-gradient(180deg, var(--surface), var(--bg-secondary));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

.screen-title {
  margin: 4px 0 14px;
  font-size: 1.3rem;
  font-weight: 800;
}

/* ---------- buttons ---------- */
.btn {
  border: none;
  border-radius: var(--radius-md);
  padding: 14px 20px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s var(--ease-out), box-shadow 0.2s;
  font-family: inherit;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: linear-gradient(135deg, var(--brand-gold), #ffab00);
  color: #1a1200;
  box-shadow: 0 10px 26px rgba(255, 194, 14, 0.28);
}
.btn-primary:hover { box-shadow: 0 12px 32px rgba(255, 194, 14, 0.4); }

.btn-ghost {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text-primary);
}
.btn-ghost:hover { background: var(--surface); }

.btn-lg { width: 100%; padding: 18px; font-size: 1.1rem; }

/* ---------- home ---------- */
#screen-home { align-items: center; justify-content: center; }

.home-card {
  max-width: 420px;
  width: 100%;
  padding: 32px 26px;
  text-align: center;
  margin: auto;
}

.logo-mark {
  font-size: 2.6rem;
  font-weight: 900;
  letter-spacing: -0.03em;
  margin-bottom: 4px;
}
.logo-mark b { color: var(--brand-gold); }

.tagline { color: var(--text-secondary); margin: 0 0 24px; }

.home-desc {
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin: 8px 0 20px;
}

#screen-home .btn { width: 100%; margin-bottom: 4px; display: flex; flex-direction: column; gap: 2px; }
#campaign-progress { font-weight: 500; opacity: 0.75; font-size: 0.78rem; }

/* ---------- map ---------- */
.map-chapters {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding-bottom: 20px;
}

.chapter-block {
  border-radius: var(--radius-lg);
  padding: 16px;
  border: 1px solid var(--border);
  background: linear-gradient(135deg, var(--ch-bg1, var(--surface)), var(--ch-bg2, var(--bg-secondary)));
  position: relative;
  overflow: hidden;
}
.chapter-block.locked { filter: grayscale(0.6); opacity: 0.55; }

.chapter-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 4px;
  color: #fff;
}
.chapter-head .num {
  font-size: 0.75rem;
  font-weight: 800;
  opacity: 0.6;
}
.chapter-head h3 { margin: 0; font-size: 1.05rem; font-weight: 800; }
.chapter-desc { font-size: 0.78rem; color: rgba(255,255,255,0.75); margin: 0 0 12px; }

.level-nodes {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 10px;
}

.level-node {
  aspect-ratio: 1;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s var(--ease-out);
  position: relative;
}
.level-node:active { transform: scale(0.92); }
.level-node.locked { cursor: not-allowed; opacity: 0.5; }
.level-node.done {
  background: linear-gradient(135deg, var(--ch-accent, var(--brand-gold)), var(--ch-accent2, #fff));
  color: #1a1200;
  border-color: transparent;
}
.level-node.current { border-color: var(--brand-gold); box-shadow: 0 0 0 3px rgba(255,194,14,0.25); }
.level-node .stars { font-size: 0.55rem; letter-spacing: 1px; }

/* ---------- quick play menu ---------- */
#screen-quickplay { align-items: center; justify-content: center; }
.menu-card { max-width: 380px; width: 100%; padding: 28px; text-align: center; margin: auto; }
.difficulty-buttons { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }

/* ---------- game ---------- */
#screen-game { padding: 10px 12px; gap: 10px; }

.game-hud {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  flex: 0 0 auto;
}
.game-hud .chip { font-variant-numeric: tabular-nums; }
#pause-btn { margin-left: 8px; }

.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  min-height: 0;
}

.fx-canvas {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
}

.grid {
  --cols: 4;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: clamp(4px, 1.4vw, 10px);
  width: min(92vw, calc(100dvh - 260px), 520px);
  aspect-ratio: 1;
  padding: 14px;
  border-radius: var(--radius-lg);
  background: linear-gradient(160deg, var(--chapter-bg1), var(--chapter-bg2));
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: inset 0 2px 16px rgba(0,0,0,0.35), 0 20px 50px rgba(0,0,0,0.4);
}

.cell {
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-width: 0;
  aspect-ratio: 1;
  transition: background 0.18s var(--ease-out), box-shadow 0.18s var(--ease-out), transform 0.12s var(--ease-out);
  background: rgba(255, 255, 255, 0.06);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
}
.cell:hover { filter: brightness(1.08); }
.cell:active { transform: scale(0.92); }

.cell.on {
  background: radial-gradient(circle at 35% 30%, #fff, var(--chapter-accent) 70%);
  box-shadow: 0 0 18px 4px var(--chapter-accent), inset 0 1px 2px rgba(255,255,255,0.6);
  animation: cellPop 0.22s var(--ease-bounce);
}

.cell.focused { outline: 2px solid var(--brand-gold); outline-offset: 2px; }

.cell.hint-pulse {
  box-shadow: 0 0 0 4px var(--brand-gold), 0 0 22px var(--brand-gold);
  animation: hintPulse 0.9s ease-in-out infinite;
}
@keyframes hintPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

@keyframes cellPop {
  0% { transform: scale(0.82); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.cell:active { transform: scale(0.9); }

/* ---------- tools bar ---------- */
.tools-bar {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  flex: 0 0 auto;
  padding-bottom: max(4px, env(safe-area-inset-bottom));
}

.tool-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-primary);
  border-radius: 999px;
  padding: 10px 14px;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 46px;
  min-height: 46px;
  justify-content: center;
  transition: transform 0.15s var(--ease-out), border-color 0.2s;
}
.tool-btn:active { transform: scale(0.92); }
.tool-paid { border-color: rgba(255, 194, 14, 0.4); }
.tool-cost {
  font-size: 0.7rem;
  font-weight: 800;
  color: var(--brand-gold);
}
.tool-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 14, 0.6);
  backdrop-filter: blur(6px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.overlay.show { display: flex; animation: overlayFade 0.2s var(--ease-out); }

@keyframes overlayFade { from { opacity: 0; } to { opacity: 1; } }

.overlay-card {
  max-width: 380px;
  width: 100%;
  padding: 28px 24px;
  text-align: center;
  animation: cardPop 0.35s var(--ease-bounce);
}

@keyframes cardPop {
  from { opacity: 0; transform: translateY(14px) scale(0.95); }
  to { opacity: 1; transform: none; }
}

.overlay-card h2 { margin: 0 0 10px; font-size: 1.4rem; }
.overlay-card p { color: var(--text-secondary); margin: 0 0 18px; }
.overlay-card .btn { width: 100%; margin-bottom: 8px; }

.stars {
  font-size: 2.2rem;
  letter-spacing: 6px;
  color: var(--brand-gold);
  margin: 10px 0;
}
.stars .dim { color: var(--border); }

.life-timer { font-variant-numeric: tabular-nums; font-weight: 800; color: var(--brand-gold); }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(120%);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 12px 20px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.88rem;
  transition: transform 0.3s var(--ease-out);
  z-index: 100;
  max-width: 86vw;
  text-align: center;
  box-shadow: var(--shadow-card);
}
.toast.show { transform: translateX(-50%) translateY(0); }

[dir="rtl"] .toast { direction: rtl; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

@media (max-width: 420px) {
  .home-card, .menu-card { padding: 22px 18px; }
  .logo-mark { font-size: 2.1rem; }
}
