/* =========================================================
   COUNT CONTROL – UI styles
   ========================================================= */
:root {
  --sky: #39b7f2; --cyan: #13d8e8; --blue: #168cff; --red: #ff2828; --purple: #5716d9;
  --magenta: #b000c8; --green: #37d77c; --yellow: #ffd52b; --plat: #e7ebef; --ink: #33334f;
  --pill: rgba(78, 62, 104, 0.82);
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px);
  --font: "Fredoka", "Nunito", "Baloo 2", "Arial Rounded MT Bold", "Trebuchet MS", system-ui, sans-serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  font-family: var(--font); color: #fff; user-select: none; -webkit-user-select: none; touch-action: none;
  background: #39b7f2 url(assets/backgrounds/sky-background.svg) center / cover no-repeat;
  display: flex; align-items: center; justify-content: center;
}
#stage {
  position: relative; width: 100%; height: 100%; overflow: hidden;
  background: #fff;
  box-shadow: 0 0 60px rgba(20, 40, 90, 0.35);
}
/* desktop: centered play area with the sky around it */
@media (min-height: 520px) and (min-aspect-ratio: 5/4) { #stage { max-width: calc(100vh * 1.25); } }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.hidden { display: none !important; }
button { font-family: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
img { pointer-events: none; }

/* ---------- logo ---------- */
.logo { display: flex; flex-direction: column; align-items: center; line-height: 0.88; font-weight: 900; letter-spacing: 0.02em; transform: rotate(-4deg); }
.logo span { display: block; font-size: 54px; -webkit-text-stroke: 3px var(--ink); paint-order: stroke fill; text-shadow: 0 6px 0 var(--ink), 0 10px 18px rgba(0, 0, 0, 0.25); }
.logo .l1 { color: #fff; }
.logo .l2 { color: var(--yellow); font-size: 60px; }
.logo.big span { font-size: clamp(46px, 11vw, 74px); }
.logo.big .l2 { font-size: clamp(52px, 12.5vw, 84px); }

/* ---------- buttons ---------- */
.btn {
  min-height: 52px; padding: 0 22px; border-radius: 16px; font-size: 20px; font-weight: 900; letter-spacing: 0.02em;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px; color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.18); transition: transform 0.1s, filter 0.1s;
}
.btn img { width: 22px; height: 22px; }
.btn:active { transform: translateY(3px) scale(0.98); }
.btn:hover { filter: brightness(1.07); }
.btn-play { background: linear-gradient(180deg, #5cf08f, #20b85a); box-shadow: 0 6px 0 #148c41, 0 10px 20px rgba(0, 0, 0, 0.2); font-size: 26px; min-height: 62px; }
.btn-blue { background: linear-gradient(180deg, #46b0ff, #1478e6); box-shadow: 0 5px 0 #0c56a8, 0 8px 16px rgba(0, 0, 0, 0.18); }
.btn-purple { background: linear-gradient(180deg, #8a52ff, #5716d9); box-shadow: 0 5px 0 #3b0c99, 0 8px 16px rgba(0, 0, 0, 0.18); }
.btn-red { background: linear-gradient(180deg, #ff6a5c, #e0261d); box-shadow: 0 5px 0 #9b1610, 0 8px 16px rgba(0, 0, 0, 0.18); }
.btn-ghost { background: rgba(51, 51, 79, 0.08); color: var(--ink); text-shadow: none; box-shadow: inset 0 0 0 2px rgba(51, 51, 79, 0.15); }
.btn-ghost img { filter: invert(0.2) sepia(1) hue-rotate(200deg) saturate(2) brightness(0.4); }
.btn.small { min-height: 44px; font-size: 16px; }
.btn.tog.off { filter: grayscale(1) brightness(0.85); }
.icon-btn { width: 44px; height: 44px; border-radius: 14px; background: var(--pill); display: grid; place-items: center; }
.icon-btn img { width: 22px; height: 22px; }

/* ---------- chips ---------- */
.chip { white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px 5px 8px; border-radius: 999px; background: var(--pill); font-weight: 800; font-size: 15px; }
.chip img { width: 18px; height: 18px; }
.chip.big { font-size: 17px; padding: 7px 14px 7px 10px; }
.chip.big img { width: 22px; height: 22px; }
.coin-pill { display: inline-flex; align-items: center; gap: 4px; height: 36px; padding: 0 4px 0 14px; border-radius: 12px; background: var(--pill); font-weight: 900; font-size: 19px; }
.coin-pill img { width: 34px; height: 34px; margin-right: -12px; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.25)); }
.coin-pill.bump { animation: bump 0.25s; }
@keyframes bump { 50% { transform: scale(1.15); } }

/* ---------- HUD ---------- */
.hud { position: absolute; inset: 0; pointer-events: none; }
.hud > * { pointer-events: auto; }
.hud-top { position: absolute; left: 0; right: 0; top: 0; padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) 0 calc(12px + var(--safe-l)); display: grid; grid-template-columns: 1fr auto 1fr; align-items: start; gap: 8px; pointer-events: none; }
.hud-top > * { pointer-events: auto; }
.hud-left { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.hud-right { display: flex; gap: 14px; justify-content: flex-end; align-items: center; }
.hud-center { display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 0; }
.progress { position: relative; width: clamp(150px, 30vw, 260px); height: 32px; display: flex; align-items: center; }
.progress-track { position: absolute; left: 10px; right: 14px; height: 18px; border-radius: 999px; background: #fff; box-shadow: 0 0 0 4px #5b4d74, 0 4px 8px rgba(0, 0, 0, 0.2); overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: linear-gradient(180deg, #4fe6ff, #12b5e8); border-radius: 999px; }
.progress-dot { position: absolute; left: 0; width: 30px; height: 30px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #7af0ff, #0fb7e6 70%); box-shadow: 0 0 0 4px #5b4d74; transform: translateX(-4px); }
.progress-flag { position: absolute; right: -4px; width: 32px; height: 32px; }
.hud-level { font-weight: 900; font-size: 13px; letter-spacing: 0.12em; color: var(--ink); background: rgba(255, 255, 255, 0.75); padding: 2px 10px; border-radius: 999px; }

.move-panel { position: absolute; left: calc(14px + var(--safe-l)); top: 42%; transform: translateY(-50%); padding: 12px 12px 10px; border-radius: 14px; background: rgba(78, 62, 104, 0.6); display: flex; flex-direction: column; align-items: center; gap: 8px; }
.move-panel b { font-size: 20px; font-weight: 900; letter-spacing: 0.04em; }
.move-panel .keys { display: flex; gap: 4px; }
.move-panel kbd { width: 26px; height: 24px; border-radius: 6px; background: #fff; color: var(--ink); font: 900 14px var(--font); display: grid; place-items: center; box-shadow: 0 2px 0 rgba(0, 0, 0, 0.3); }
.move-panel img { width: 54px; height: 36px; }
body.touch .move-panel { display: none; }

.bonus-card { position: absolute; right: calc(12px + var(--safe-r)); top: 42%; transform: translateY(-50%); width: 112px; padding: 4px 6px 8px; border-radius: 16px; background: linear-gradient(180deg, rgba(78, 62, 104, 0.85), rgba(60, 46, 86, 0.92)); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25); display: flex; flex-direction: column; align-items: center; gap: 2px; transition: transform 0.12s, opacity 0.3s; }
.bonus-card img { width: 96px; height: 66px; margin-top: -26px; filter: drop-shadow(0 0 0 #fff) drop-shadow(0 3px 4px rgba(0, 0, 0, 0.3)); animation: crowdBob 1.2s ease-in-out infinite; }
@keyframes crowdBob { 50% { transform: translateY(-3px); } }
.bonus-card .bonus-plus { font-size: 34px; font-weight: 900; line-height: 1; -webkit-text-stroke: 2px #0c56a8; paint-order: stroke fill; }
.bonus-card .bonus-tag { width: 100%; font-size: 12px; font-weight: 900; letter-spacing: 0.06em; background: linear-gradient(180deg, #46b0ff, #1478e6); border-radius: 8px; padding: 4px 0; box-shadow: 0 3px 0 #0c56a8; }
.bonus-card small { font-size: 9px; opacity: 0.75; font-weight: 700; }
.bonus-card:active { transform: translateY(-50%) scale(0.95); }
.bonus-card.used { opacity: 0.45; filter: grayscale(0.7); pointer-events: none; }
.bonus-card.pulse { animation: bonusPulse 1.6s ease-in-out infinite; }
@keyframes bonusPulse { 50% { box-shadow: 0 0 0 4px rgba(79, 230, 255, 0.6), 0 6px 18px rgba(0, 0, 0, 0.25); } }

.start-hint { position: absolute; left: 50%; bottom: calc(14% + var(--safe-b)); transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 4px; pointer-events: none; }
.start-hint img { width: 56px; height: 56px; animation: swipe 1.4s ease-in-out infinite; }
.start-hint span { font-weight: 900; font-size: 22px; color: var(--ink); background: rgba(255, 255, 255, 0.85); padding: 4px 16px; border-radius: 999px; }
@keyframes swipe { 0%, 100% { transform: translateX(-40px) rotate(-10deg); } 50% { transform: translateX(40px) rotate(10deg); } }

#labels, #floaters { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.count-label { position: absolute; left: 0; top: 0; min-width: 34px; padding: 3px 9px; border-radius: 8px; font-weight: 900; font-size: 17px; text-align: center; color: #fff; transform: translate(-50%, -100%); will-change: transform; }
.count-label::after { content: ""; position: absolute; left: 50%; bottom: -7px; margin-left: -7px; border: 7px solid transparent; border-bottom: 0; border-top-color: inherit; }
.count-label.blue { background: #2f8df2; border-color: #2f8df2; box-shadow: 0 3px 0 #1a63b8; }
.count-label.red { background: #ff5a2e; border-color: #ff5a2e; box-shadow: 0 3px 0 #b8371a; font-size: 14px; }
.count-label.grey { background: #8a9ab5; border-color: #8a9ab5; box-shadow: 0 3px 0 #5c6b85; font-size: 14px; }
.count-label.pop { animation: lpop 0.25s; }
@keyframes lpop { 50% { scale: 1.25; } }
.floater { position: absolute; left: 0; top: 0; font-weight: 900; font-size: 44px; color: #fff; -webkit-text-stroke: 3px rgba(40, 40, 90, 0.55); paint-order: stroke fill; text-shadow: 0 4px 10px rgba(0, 0, 0, 0.2); white-space: nowrap; animation: floatUp 1.1s ease-out forwards; }
.floater.bad { color: #ffd1c4; -webkit-text-stroke-color: #c0301a; }
.floater.gold { color: var(--yellow); font-size: 30px; -webkit-text-stroke-color: #9b6200; }
.floater.small { font-size: 26px; }
@keyframes floatUp {
  0% { opacity: 0; translate: -50% 0; scale: 0.5; }
  15% { opacity: 1; scale: 1.15; }
  30% { scale: 1; }
  100% { opacity: 0; translate: -50% -90px; }
}

/* ---------- screens ---------- */
.screen { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 10; }
.screen.dim { background: rgba(30, 25, 60, 0.45); backdrop-filter: blur(3px); }
.panel { position: relative; width: min(92%, 400px); max-height: calc(100% - 24px); overflow-y: auto; padding: 22px 20px 20px; border-radius: 24px; background: #fff; color: var(--ink); box-shadow: 0 8px 0 #c9d2e0, 0 24px 50px rgba(0, 0, 0, 0.3); display: flex; flex-direction: column; gap: 12px; animation: panelIn 0.35s cubic-bezier(0.3, 1.5, 0.5, 1); touch-action: pan-y; }
.panel.wide { width: min(94%, 560px); }
@keyframes panelIn { from { transform: scale(0.8); opacity: 0; } }
.panel h2 { font-size: 28px; font-weight: 900; text-align: center; display: flex; align-items: center; justify-content: center; gap: 8px; }
.panel p { text-align: center; opacity: 0.8; font-weight: 600; }
.h-icon { width: 28px; height: 28px; filter: invert(0.2) sepia(1) hue-rotate(200deg) saturate(2) brightness(0.4); }
.close-btn { position: absolute; right: 12px; top: 12px; width: 40px; height: 40px; border-radius: 12px; background: var(--ink); padding: 10px; z-index: 2; }
.close-btn img { width: 100%; height: 100%; }
.menu-row { display: flex; gap: 10px; }
.menu-row .btn { flex: 1; }

/* loading */
.loading { background: linear-gradient(180deg, #39b7f2 0%, #7fd6f7 45%, #ffe2a8 85%, #fff6e6 100%); }
.ld-bg { position: absolute; inset: 0; background: url(assets/backgrounds/sky-background.svg) center / cover; opacity: 0.6; }
.ld-box { position: relative; display: flex; flex-direction: column; align-items: center; gap: 22px; width: min(86%, 380px); }
.ld-crowd { position: relative; width: 100%; height: 90px; overflow: hidden; }
.ld-crowd i { position: absolute; bottom: 4px; width: 26px; height: 46px; background: url(assets/ui/crowd.svg) 50% 0 / 78px 54px no-repeat; animation: ldRun 0.38s ease-in-out infinite alternate, ldMove 3.2s linear infinite; }
@keyframes ldRun { to { translate: 0 -7px; } }
@keyframes ldMove { from { left: -10%; } to { left: 104%; } }
.ld-bar { width: 100%; height: 20px; border-radius: 999px; background: #fff; box-shadow: 0 0 0 4px #5b4d74; overflow: hidden; }
.ld-bar div { height: 100%; width: 0%; background: linear-gradient(180deg, #4fe6ff, #12b5e8); border-radius: 999px; transition: width 0.2s; }
.ld-text { font-weight: 800; color: var(--ink); }
.ld-error { background: #fff; color: var(--ink); padding: 14px; border-radius: 14px; text-align: center; display: flex; flex-direction: column; gap: 10px; }

/* menu */
.menu { flex-direction: column; justify-content: space-between; padding: calc(14px + var(--safe-t)) calc(14px + var(--safe-r)) calc(22px + var(--safe-b)) calc(14px + var(--safe-l)); background: linear-gradient(180deg, rgba(255, 255, 255, 0) 55%, rgba(57, 183, 242, 0.25)); }
.menu-top { width: 100%; display: flex; justify-content: space-between; align-items: center; }
.menu-center { display: flex; flex-direction: column; align-items: center; gap: 18px; margin-top: -8vh; }
.menu-level { color: var(--ink); background: rgba(255, 255, 255, 0.88); padding: 6px 16px; border-radius: 999px; font-weight: 800; font-size: 16px; box-shadow: 0 3px 0 rgba(51, 51, 79, 0.15); }
.menu-bottom { width: min(100%, 360px); display: flex; flex-direction: column; gap: 14px; }
.menu-bottom .btn-play { animation: playPulse 1.6s ease-in-out infinite; }
@keyframes playPulse { 50% { transform: scale(1.04); } }

/* level select */
.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
.lvl { aspect-ratio: 1; border-radius: 14px; font-size: 22px; font-weight: 900; color: #fff; position: relative; display: grid; place-items: center; box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2); }
.lvl.e0 { background: linear-gradient(180deg, #8a52ff, #5716d9); }
.lvl.e1 { background: linear-gradient(180deg, #ffc65c, #ff9a1f); }
.lvl.e2 { background: linear-gradient(180deg, #4fd88f, #1fae63); }
.lvl.locked { background: #d5dbe5; color: transparent; box-shadow: 0 4px 0 #b6bfcc; cursor: default; }
.lvl.locked::after { content: ""; position: absolute; inset: 30%; background: url(assets/ui/lock.svg) center / contain no-repeat; filter: brightness(0.6); }
.lvl.done::before { content: ""; position: absolute; right: -4px; top: -4px; width: 20px; height: 20px; border-radius: 50%; background: var(--green) url(assets/ui/check.svg) center / 70% no-repeat; box-shadow: 0 0 0 2px #fff; }
.lvl.current { outline: 3px solid var(--yellow); outline-offset: 2px; }

/* settings */
.row-set { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-radius: 14px; background: #f1f4f8; font-weight: 800; cursor: pointer; }
.row-set.col { flex-direction: column; align-items: stretch; cursor: default; }
.row-set span { display: flex; align-items: center; gap: 8px; }
.row-set span img { width: 20px; height: 20px; filter: invert(0.2) sepia(1) hue-rotate(200deg) saturate(2) brightness(0.4); }
.row-set input[type="checkbox"] { display: none; }
.sw { width: 50px; height: 28px; border-radius: 999px; background: #c9d2e0; position: relative; transition: background 0.2s; flex: none; }
.sw::after { content: ""; position: absolute; left: 3px; top: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: left 0.2s; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2); }
input:checked + .sw { background: var(--green); }
input:checked + .sw::after { left: 25px; }
input[type="range"] { width: 100%; accent-color: var(--blue); }
.seg { display: flex; gap: 4px; background: #dfe5ee; padding: 4px; border-radius: 12px; }
.seg button { flex: 1; padding: 8px 0; border-radius: 9px; font-weight: 800; color: var(--ink); }
.seg button.on { background: var(--blue); color: #fff; }

/* results */
.result { align-items: stretch; text-align: center; padding-top: 40px; margin-top: 20px; overflow: visible; }
.ribbon { position: absolute; left: 50%; top: -22px; transform: translateX(-50%); white-space: nowrap; padding: 10px 26px; font-size: 22px; font-weight: 900; color: #fff; border-radius: 14px; background: linear-gradient(180deg, #46b0ff, #1478e6); box-shadow: 0 5px 0 #0c56a8; }
.ribbon.red { background: linear-gradient(180deg, #ff6a5c, #e0261d); box-shadow: 0 5px 0 #9b1610; }
.stars { display: flex; justify-content: center; gap: 6px; }
.stars i { width: 46px; height: 46px; background: url(assets/ui/star.svg) center / contain no-repeat; filter: grayscale(1) opacity(0.3); transform: scale(0.6); transition: 0.3s; }
.stars i:nth-child(2) { width: 58px; height: 58px; margin-top: -10px; }
.stars i.on { filter: none; transform: scale(1); animation: starPop 0.4s; }
@keyframes starPop { 50% { transform: scale(1.35) rotate(15deg); } }
.res-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.res-grid.two { grid-template-columns: 1fr 1fr; }
.res-grid div { background: #f1f4f8; border-radius: 14px; padding: 10px 4px; display: flex; flex-direction: column; gap: 2px; }
.res-grid small { font-size: 12px; font-weight: 800; opacity: 0.6; }
.res-grid b { font-size: 24px; font-weight: 900; }
.reward { display: flex; align-items: center; justify-content: center; gap: 6px; font-size: 32px; font-weight: 900; color: #e09a00; }
.reward img { width: 40px; height: 40px; }
.best-note { font-weight: 900; color: var(--magenta); animation: playPulse 1s infinite; }

.toast { position: absolute; left: 50%; top: 22%; transform: translate(-50%, -20px); padding: 10px 18px; border-radius: 14px; background: rgba(51, 51, 79, 0.92); font-weight: 800; opacity: 0; transition: 0.25s; pointer-events: none; z-index: 30; white-space: nowrap; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.noscript { position: fixed; inset: 0; display: grid; place-items: center; background: #39b7f2; color: #fff; font: 700 20px sans-serif; }

/* ---------- responsive ---------- */
@media (max-width: 520px) {
  .hud-top { grid-template-columns: auto minmax(0, 1fr) auto; gap: 6px; }
  .progress { width: 100%; }
  .chip { font-size: 13px; padding: 4px 10px 4px 6px; }
  .coin-pill { font-size: 16px; height: 32px; }
  .coin-pill img { width: 30px; height: 30px; }
  .icon-btn { width: 38px; height: 38px; }
  .hud-right { gap: 10px; }
  .coin-pill { padding-left: 10px; }
  .bonus-card { width: 78px; top: auto; bottom: calc(16px + var(--safe-b)); transform: none; padding: 3px 4px 6px; border-radius: 13px; }
  .bonus-card:active { transform: scale(0.95); }
  .bonus-card img { width: 66px; height: 46px; margin-top: -20px; }
  .bonus-card .bonus-plus { font-size: 24px; }
  .bonus-card .bonus-tag { font-size: 10px; padding: 3px 0; }
  .bonus-card small { font-size: 8px; }
  .level-grid { gap: 8px; }
  .lvl { font-size: 18px; }
  .floater { font-size: 36px; }
}
@media (max-height: 480px) {
  .menu-center { margin-top: 0; gap: 8px; }
  .logo.big span { font-size: 40px; }
  .logo.big .l2 { font-size: 46px; }
  .btn-play { min-height: 50px; font-size: 22px; }
  .menu-bottom { gap: 10px; }
  .bonus-card { top: auto; bottom: 10px; transform: none; width: 92px; }
  .bonus-card img { width: 76px; height: 50px; margin-top: -20px; }
  .move-panel { top: 55%; padding: 8px; gap: 5px; }
  .move-panel b { font-size: 15px; }
  .panel { padding: 16px; gap: 8px; }
  .result { padding-top: 32px; }
  .level-grid { grid-template-columns: repeat(10, 1fr); }
}

@media (max-height: 640px) and (min-height: 481px) {
  .move-panel { padding: 8px; gap: 5px; }
  .move-panel b { font-size: 15px; }
  .move-panel kbd { width: 22px; height: 20px; font-size: 12px; }
  .move-panel img { width: 42px; height: 28px; }
  .bonus-card { width: 86px; }
  .bonus-card img { width: 72px; height: 50px; margin-top: -20px; }
  .bonus-card .bonus-plus { font-size: 26px; }
  .bonus-card .bonus-tag { font-size: 10px; }
}
