:root {
  --bg0: #0a0a1f;
  --bg1: #0d1030;
  --ink: #eaf0ff;
  --muted: #8b93c7;
  --accent: #22d3ee;
  --accent2: #a855f7;
  --gold: #ffd166;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  background: var(--bg0);
  color: var(--ink);
  font-family: "Baloo 2", system-ui, -apple-system, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;   /* fallback */
  height: 100svh;  /* small viewport (stable, no jump when URL bar toggles) */
  height: 100dvh;  /* dynamic — wins on modern Safari */
  position: fixed;
  inset: 0;
  overflow: hidden;
}

/* ---- HUD ---- */
#hud {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(var(--safe-top) + 12px) 16px 10px;
  position: relative;
  z-index: 5;
  background: linear-gradient(180deg, rgba(10,12,34,0.78), rgba(10,12,34,0));
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.stat { display: flex; flex-direction: column; line-height: 1; }
.stat.best { margin-left: auto; align-items: flex-end; }
.label { font-size: 11px; letter-spacing: 2px; color: var(--muted); font-weight: 700; }
.value { font-size: 30px; font-weight: 800; text-shadow: 0 2px 12px rgba(34,211,238,0.35); }
.stat.best .value { color: var(--gold); text-shadow: 0 2px 12px rgba(255,209,102,0.3); }

#combo {
  position: absolute; left: 50%; top: calc(var(--safe-top) + 14px); transform: translateX(-50%);
  font-size: 22px; font-weight: 800; color: var(--gold);
  text-shadow: 0 0 16px rgba(255,209,102,0.6); min-height: 26px;
}

.controls { display: flex; gap: 8px; margin-left: 8px; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 14px; border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.06); color: var(--ink); font-size: 18px;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  display: grid; place-items: center; cursor: pointer; transition: transform .1s, background .2s;
}
.icon-btn:active { transform: scale(0.9); }
.icon-btn.off { opacity: 0.5; }
body:not(.playing) #btn-pause { display: none; }

/* ---- canvas ---- */
/* flex:1 + min-height:0 lets the canvas absorb leftover height without overflowing
   the column (the classic flexbox overflow trap); renderer reads clientWidth/Height. */
#game { flex: 1 1 auto; width: 100%; min-height: 0; display: block; touch-action: none; }

/* ---- item bar ---- */
#items {
  display: flex; gap: 10px; justify-content: center;
  padding: 10px 14px calc(var(--safe-bottom) + 12px);
  z-index: 6;
  background: linear-gradient(0deg, rgba(10,12,34,0.82), rgba(10,12,34,0));
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.item-btn {
  position: relative; flex: 1 1 0; max-width: 120px;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 8px 6px 6px; border-radius: 16px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(255,255,255,0.06); color: var(--ink);
  font-family: inherit; cursor: pointer;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  transition: transform .1s, background .2s, box-shadow .2s, opacity .2s;
}
.item-btn:active { transform: scale(0.93); }
.item-btn:disabled { opacity: 0.32; cursor: default; }
.item-icon { font-size: 24px; line-height: 1; }
.item-name { font-size: 11px; font-weight: 700; letter-spacing: 0.5px; color: var(--muted); }
.item-count {
  position: absolute; top: -6px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 999px; background: linear-gradient(120deg, #22d3ee, #a855f7);
  color: #06121a; font-size: 12px; font-weight: 800; display: grid; place-items: center;
  box-shadow: 0 2px 8px rgba(34,211,238,0.5);
}
.item-btn.targeting {
  background: rgba(34,211,238,0.18);
  box-shadow: 0 0 0 2px var(--accent), 0 6px 18px rgba(34,211,238,0.4);
}
.item-btn.bump { animation: bump .4s ease; }
@keyframes bump { 0%,100% { transform: scale(1); } 35% { transform: scale(1.18); } }

.target-banner {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 96px); transform: translateX(-50%);
  background: rgba(10,12,30,0.82); border: 1px solid rgba(255,255,255,0.14);
  color: var(--ink); font-size: 14px; font-weight: 700; padding: 8px 18px; border-radius: 999px;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 8; white-space: nowrap;
}
.target-banner.show { opacity: 1; }

/* chase mode */
.chase-only { display: none; }
body.chase .chase-only { display: flex; }
.item-btn[data-item="eat"] .item-count { background: linear-gradient(120deg, #ffd166, #f472b6); }
.cta-chase {
  font-size: 18px; padding: 14px 36px;
  background: linear-gradient(120deg, #a855f7, #f472b6);
  box-shadow: 0 10px 30px rgba(168,85,247,0.45), inset 0 2px 0 rgba(255,255,255,0.4);
}

/* ---- mode select grid ---- */
#menu .panel { gap: 14px; }
.mode-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; }
.mode-btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 13px 10px; border-radius: 18px; cursor: pointer; font-family: inherit; color: var(--ink);
  background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.10);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  transition: transform .1s, background .2s, box-shadow .2s;
}
.mode-btn:active { transform: scale(0.95); }
@keyframes cardIn { from { opacity: 0; transform: translateY(16px) scale(0.94); } to { opacity: 1; transform: none; } }
.mode-btn .m-ic { font-size: 26px; line-height: 1.1; }
.mode-btn .m-name { font-size: 16px; font-weight: 800; letter-spacing: 0.4px; }
.mode-btn .m-tag { font-size: 10.5px; font-weight: 600; color: var(--muted); }
.mode-btn[data-mode="classic"] { box-shadow: inset 0 0 0 1px rgba(34,211,238,0.40), 0 6px 20px rgba(34,211,238,0.14); }
.mode-btn[data-mode="chase"]   { box-shadow: inset 0 0 0 1px rgba(168,85,247,0.45), 0 6px 20px rgba(168,85,247,0.16); }
.mode-btn[data-mode="blitz"]   { box-shadow: inset 0 0 0 1px rgba(96,165,250,0.45), 0 6px 20px rgba(96,165,250,0.16); }
.mode-btn[data-mode="zen"]     { box-shadow: inset 0 0 0 1px rgba(45,212,191,0.45), 0 6px 20px rgba(45,212,191,0.14); }
.mode-btn[data-mode="bomb"]    { box-shadow: inset 0 0 0 1px rgba(251,113,133,0.50), 0 6px 20px rgba(251,113,133,0.16); }
.mode-btn[data-mode="daily"]   { box-shadow: inset 0 0 0 1px rgba(255,209,102,0.50), 0 6px 20px rgba(255,209,102,0.14); }
.mode-btn[data-mode="puzzle"]  { box-shadow: inset 0 0 0 1px rgba(163,230,53,0.50), 0 6px 20px rgba(163,230,53,0.14); }
/* full-width feature button (Puzzle sits alone on the last row) */
.mode-btn.wide { grid-column: 1 / -1; flex-direction: row; justify-content: center; gap: 10px; padding: 12px 14px; }
.mode-btn.wide .m-ic { font-size: 24px; }
.mode-btn.wide .m-tag { font-size: 11px; }

/* ---- in-game mode / timer pill ---- */
.mode-pill {
  position: fixed; left: 50%; top: calc(var(--safe-top) + 58px); transform: translateX(-50%);
  z-index: 7; pointer-events: none; white-space: nowrap;
  background: rgba(10,12,30,0.70); border: 1px solid rgba(255,255,255,0.14);
  color: var(--ink); font-size: 15px; font-weight: 800; letter-spacing: 1px;
  padding: 6px 16px; border-radius: 999px;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.mode-pill[hidden] { display: none; }
.mode-pill.timer { font-variant-numeric: tabular-nums; font-size: 20px; color: var(--accent); border-color: rgba(34,211,238,0.4); box-shadow: 0 0 18px rgba(34,211,238,0.25); }
.mode-pill.low { color: #fb7185; border-color: rgba(251,113,133,0.6); box-shadow: 0 0 22px rgba(251,113,133,0.45); animation: pillpulse .8s ease-in-out infinite; }
@keyframes pillpulse { 0%,100% { transform: translateX(-50%) scale(1); } 50% { transform: translateX(-50%) scale(1.10); } }

/* ---- overlays ---- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: radial-gradient(120% 100% at 50% 0%, rgba(20,24,60,0.72), rgba(6,7,18,0.92));
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  opacity: 0; pointer-events: none; transition: opacity .28s ease;
  padding: max(var(--safe-top), 20px) 24px max(var(--safe-bottom), 20px);
}
.overlay.show { opacity: 1; pointer-events: auto; }

.panel {
  text-align: center; max-width: 420px; width: 100%;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  animation: pop .35s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.title { font-size: 30px; font-weight: 700; letter-spacing: 3px; line-height: 1.05; color: var(--muted); }
.title-big {
  font-size: 46px; font-weight: 800; letter-spacing: 1px;
  background: linear-gradient(120deg, #22d3ee, #a855f7 55%, #f472b6);
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 4px 24px rgba(168,85,247,0.5));
}
.go-title {
  font-size: 40px; font-weight: 800; letter-spacing: 2px;
  background: linear-gradient(120deg, #f472b6, #fbbf24);
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.tag { color: var(--muted); font-size: 16px; font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; font-weight: 600; opacity: 0.8; margin-top: 4px; }

.cta {
  font-family: inherit; font-weight: 800; font-size: 22px; letter-spacing: 1px;
  color: #06121a; border: none; cursor: pointer;
  padding: 16px 54px; border-radius: 999px;
  background: linear-gradient(120deg, #22d3ee, #38bdf8);
  box-shadow: 0 10px 30px rgba(34,211,238,0.45), inset 0 2px 0 rgba(255,255,255,0.5);
  transition: transform .1s;
}
.cta:active { transform: scale(0.95); }
.ghost {
  font-family: inherit; font-weight: 700; font-size: 16px; color: var(--muted);
  background: none; border: none; cursor: pointer; padding: 8px 20px;
}

.cta-continue {
  background: linear-gradient(120deg, #34d399, #22d3ee);
  box-shadow: 0 10px 30px rgba(52,211,153,0.45), inset 0 2px 0 rgba(255,255,255,0.5);
}
.cta[hidden] { display: none; }

.menu-row { display: flex; gap: 12px; margin-top: 4px; }
.ghost-btn {
  font-family: inherit; font-weight: 700; font-size: 14px; color: var(--ink);
  background: rgba(255,255,255,0.07); border: 1px solid rgba(255,255,255,0.12);
  border-radius: 14px; padding: 11px 18px; cursor: pointer;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); transition: transform .1s, background .2s;
}
.ghost-btn:active { transform: scale(0.94); }

/* best scores + settings */
.scores-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; }
.score-card {
  display: flex; flex-direction: column; gap: 3px; align-items: center;
  padding: 13px 10px; border-radius: 16px;
  background: rgba(34,211,238,0.1); border: 1px solid rgba(34,211,238,0.25);
}
.score-card .label { font-size: 10px; }
.score-card.chase { background: rgba(168,85,247,0.1); border-color: rgba(168,85,247,0.3); }
.score-card.puzzle { background: rgba(163,230,53,0.1); border-color: rgba(163,230,53,0.3); }
.score-card .big { font-size: 28px; font-weight: 800; color: var(--accent); }
.score-card.chase .big { color: #c084fc; }
.score-card.puzzle .big { color: #bef264; }
.stats-row { display: flex; gap: 32px; margin: 4px 0; }
.stats-row > div { display: flex; flex-direction: column; gap: 2px; align-items: center; }
.stats-row .mid { font-size: 24px; font-weight: 800; color: var(--gold); }

.settings-list { display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 320px; }
.setting-row {
  display: flex; justify-content: space-between; align-items: center;
  font-family: inherit; font-size: 17px; font-weight: 700; color: var(--ink);
  background: rgba(255,255,255,0.07); border: 1px solid rgba(255,255,255,0.12);
  border-radius: 16px; padding: 16px 20px; cursor: pointer;
}
.setting-row .toggle {
  font-size: 14px; font-weight: 800; padding: 4px 14px; border-radius: 999px;
  background: linear-gradient(120deg, #22d3ee, #34d399); color: #06121a;
}
.setting-row .toggle.off { background: rgba(255,255,255,0.15); color: var(--muted); }

.final { display: flex; gap: 36px; margin: 6px 0; }
.final > div { display: flex; flex-direction: column; gap: 4px; }
.final .big { font-size: 38px; font-weight: 800; }
.final > div:first-child .big { color: var(--accent); }
.final > div:last-child .big { color: var(--gold); }
