:root {
  --ink: #4a3560; --dim: #8a7a9c; --card: #ffffff; --coral: #ff7a59; --sun: #ffc247; --leaf: #4fcf86; --sky: #bfe9ff;
  --shadow: 0 6px 18px rgba(74, 53, 96, .16);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Arial Rounded MT Bold", system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; margin: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; overflow: hidden; background: var(--sky); color: var(--ink);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
  touch-action: none; overscroll-behavior: none; }
body { position: fixed; inset: 0; height: 100dvh; }
button { font: inherit; color: inherit; border: 0; cursor: pointer; touch-action: manipulation; }

/* ---------- background: a sunny room with soft drifting bubbles ---------- */
.bg { position: absolute; inset: 0; overflow: hidden; background: linear-gradient(165deg, #bfe9ff 0%, #d5f5e6 55%, #fff1cf 100%); }
.bg i { position: absolute; border-radius: 50%; opacity: .55; animation: bob 16s ease-in-out infinite alternate; }
.bg i:nth-child(1) { width: 46vmax; height: 46vmax; left: -14vmax; top: -16vmax; background: radial-gradient(circle, #fff 0%, rgba(255, 255, 255, 0) 68%); }
.bg i:nth-child(2) { width: 38vmax; height: 38vmax; right: -12vmax; bottom: -10vmax; background: radial-gradient(circle, #ffd9b8 0%, rgba(255, 217, 184, 0) 68%); animation-duration: 21s; }
.bg i:nth-child(3) { width: 30vmax; height: 30vmax; right: 10vmax; top: -12vmax; background: radial-gradient(circle, #c9f3d8 0%, rgba(201, 243, 216, 0) 68%); animation-duration: 26s; }
@keyframes bob { to { transform: translate(4vmax, 3vmax) scale(1.12); } }

/* ---------- the playing screen. Tall windows: bar on top, Zoom below. Wide windows: both beside the board. ---------- */
#game { position: absolute; inset: 0; display: grid; grid-template: "hud" auto "stage" 1fr "controls" auto / 1fr; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left)); }
#hud { grid-area: hud; display: flex; align-items: stretch; justify-content: center; gap: 7px; }
.pill { min-width: 0; background: var(--card); border-radius: 16px; padding: 5px 11px 6px; box-shadow: var(--shadow); display: flex; flex-direction: column; justify-content: center; }
.pill.wide { flex: 1; max-width: 220px; }
.pill span { font-size: 10.5px; letter-spacing: .09em; text-transform: uppercase; color: var(--dim); font-weight: 800; }
.pill b { font-size: clamp(18px, 5.2vw, 25px); font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pill b i { font-style: normal; } .pill b small { font-size: .7em; color: var(--dim); }
.pill.bump b { animation: bump .28s; }
@keyframes bump { 40% { transform: scale(1.25); color: var(--coral); } }
.pill.bar { flex: 1; max-width: 190px; gap: 5px; }
.track { height: 12px; border-radius: 9px; background: #ebe4f2; overflow: hidden; }
.track i { display: block; height: 100%; width: 0; border-radius: 9px; background: var(--coral); transition: width .25s; }
#meter-fill { background: #4db8ff; }
#goalbox.done .track i { background: var(--leaf); } #goalbox.done span { color: #2fa866; }
#meterbox.done .track i { background: var(--sun); animation: ready .7s ease-in-out infinite; } #meterbox.done span { color: #c9820a; }
@keyframes ready { 50% { filter: brightness(1.25); } }
.icon { flex: 0 0 50px; min-height: 50px; border-radius: 16px; background: var(--card); box-shadow: var(--shadow); display: grid; place-items: center; }
.icon svg { fill: var(--ink); }
.icon:active, .btn:active, #btn-zoom:active { transform: scale(.96); }

#stage { grid-area: stage; position: relative; min-width: 0; min-height: 0; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#stage.shake { animation: shake .35s; }
@keyframes shake { 20% { transform: translate(-7px, 3px) rotate(-.6deg); } 45% { transform: translate(7px, -3px) rotate(.6deg); } 70% { transform: translate(-4px, -2px); } }

#controls { grid-area: controls; display: flex; justify-content: center; }
#btn-zoom { width: min(100%, 420px); min-height: 60px; border-radius: 20px; color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0;
  background: linear-gradient(180deg, #ff9a6b, #ff6a4a); box-shadow: 0 6px 0 #d94f33, 0 12px 22px rgba(255, 106, 74, .35); transition: transform .06s, filter .15s; }
#btn-zoom b { font-size: 23px; font-weight: 900; letter-spacing: .04em; line-height: 1.1; }
#btn-zoom span { font-size: 12px; font-weight: 700; opacity: .9; }
#btn-zoom.on { transform: translateY(4px); background: linear-gradient(180deg, #ffd978, #ffb347); color: #5a3200; box-shadow: 0 2px 0 #e39a1f, 0 0 26px rgba(255, 194, 71, .9); animation: ready .5s ease-in-out infinite; }
#btn-zoom:disabled { filter: grayscale(.7) opacity(.6); }

/* ---------- the "new on this level" screen: one big picture, a few big words ---------- */
.panel.intro { width: min(100%, clamp(480px, 52vw, 760px)); min-height: min(84dvh, 660px); justify-content: space-between; align-items: center; gap: 12px; padding: 22px 24px 24px;
  background: linear-gradient(180deg, #fff 0%, #fff 60%, #eafaf1 100%); }
.in-kicker { align-self: center; padding: 6px 16px; border-radius: 99px; background: var(--coral); color: #fff; font-weight: 900; font-size: 16px; letter-spacing: .05em; text-transform: uppercase; }
#in-pic { width: min(64vw, 38dvh, 250px); height: auto; aspect-ratio: 1; animation: breathe 1.8s ease-in-out infinite; }
.panel.intro h2 { font-size: clamp(34px, 9.5vmin, 46px); line-height: 1; }
#in-text { font-size: clamp(18px, 5vmin, 22px); font-weight: 700; line-height: 1.3; }
.panel.intro .btn.big { align-self: stretch; margin-bottom: 0; animation: none; }

/* ---------- the level-over screen: big, bouncy, and at least half the window ---------- */
.panel.lo { width: min(100%, clamp(520px, 66vw, 940px)); min-height: min(86dvh, 700px); justify-content: space-between; gap: 14px; padding: 20px 22px 24px;
  background: linear-gradient(180deg, #fff 0%, #fff 55%, #fff6e3 100%); }
.lo-top { display: flex; flex-direction: column; align-items: center; gap: 2px; position: relative; }
#lo-ham { width: min(62%, 230px); height: auto; }
.lo-top h2 { font-size: clamp(30px, 8.5vmin, 44px); line-height: 1; }
.lo-stamp { position: absolute; right: 2%; top: 8%; padding: 5px 14px; border-radius: 12px; font-weight: 900; font-size: 19px; letter-spacing: .04em; color: #fff; transform: rotate(9deg) scale(0); }
.lo-stamp.good { background: var(--leaf); box-shadow: 0 4px 0 #2fa866; animation: stampin .45s cubic-bezier(.2, .9, .3, 1.6) forwards; }
.lo-stamp.bad { background: #e2506a; box-shadow: 0 4px 0 #b83550; animation: stampin .45s cubic-bezier(.2, .9, .3, 1.6) forwards; }
@keyframes stampin { from { transform: rotate(-20deg) scale(2.6); opacity: 0; } to { transform: rotate(9deg) scale(1); opacity: 1; } }
.lo-mid { display: flex; flex-direction: column; gap: 10px; }
.lo-score span, .lo-total span, .lo-chips span { font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--dim); font-weight: 800; }
.lo-score b { display: block; font-size: clamp(54px, 15vmin, 80px); font-weight: 900; line-height: 1; color: var(--coral); font-variant-numeric: tabular-nums; }
.lo-score b.good { color: #2fa866; } .lo-score b.bad { color: #e2506a; }
.lo-bar { position: relative; height: 20px; border-radius: 12px; background: #ebe4f2; }
.lo-bar i { display: block; height: 100%; width: 0; border-radius: 12px; background: linear-gradient(90deg, #ffb347, var(--coral)); }
.lo-bar i.good { background: linear-gradient(90deg, #8fe3b0, var(--leaf)); }
.lo-bar em { position: absolute; left: 66%; top: -5px; bottom: -5px; width: 4px; border-radius: 3px; background: var(--ink); }
.lo-need { font-size: 15px; font-weight: 700; color: var(--dim); padding-left: 32%; }
.lo-need b { color: var(--ink); font-variant-numeric: tabular-nums; }
.lo-chips { display: flex; gap: 8px; }
.lo-chips > div { flex: 1; background: #f6f1fb; border-radius: 16px; padding: 9px 4px 8px; display: flex; flex-direction: column; gap: 1px; }
.lo-chips b { font-size: 24px; font-weight: 900; font-variant-numeric: tabular-nums; }
#lo-bonus-chip { background: #fff3d6; transform: scale(0); }
#lo-bonus-chip.in { animation: chipin .4s cubic-bezier(.2, .9, .3, 1.7) forwards; }
#lo-bonus-chip b { color: #e39a1f; }
@keyframes chipin { from { transform: scale(0) rotate(-12deg); } to { transform: scale(1); } }
.lo-total { display: flex; align-items: baseline; justify-content: space-between; border-top: 3px dashed #ebe4f2; padding-top: 10px; }
.lo-total span { font-size: 15px; }
.lo-total b { font-size: clamp(30px, 8vmin, 40px); font-weight: 900; font-variant-numeric: tabular-nums; }
.lo-total b.pop { animation: bump .3s; }
.lo-news { background: #fff3d6; border-radius: 14px; padding: 8px 10px; font-size: 14.5px; font-weight: 600; line-height: 1.3; }
.lo-news b { display: block; color: var(--coral); }
.panel.lo .btn.big { margin-bottom: 0; }

#fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 30; }

/* ---------- overlays ---------- */
.overlay { position: absolute; inset: 0; z-index: 20; display: grid; place-items: center; overflow: hidden;
  padding: calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  background: rgba(191, 233, 255, .72); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); animation: fade .2s; }
#title { background: linear-gradient(165deg, #bfe9ff 0%, #d5f5e6 55%, #fff1cf 100%); backdrop-filter: none; }
@keyframes fade { from { opacity: 0; } }
.panel { width: min(100%, 400px); max-height: 100%; display: flex; flex-direction: column; align-items: stretch; gap: 12px; text-align: center;
  padding: 22px 20px; border-radius: 28px; background: var(--card); box-shadow: 0 24px 60px rgba(74, 53, 96, .28); animation: rise .28s cubic-bezier(.2, .9, .3, 1.25); }
.panel.clear { background: none; box-shadow: none; width: min(100%, 440px); gap: 10px; }
@keyframes rise { from { transform: translateY(24px) scale(.95); opacity: 0; } }
#mascot { width: min(100%, 260px); height: auto; align-self: center; }
h1 { font-size: clamp(42px, 13vmin, 70px); font-weight: 900; letter-spacing: -.02em; line-height: .95; color: var(--ink); }
h1 em { font-style: normal; color: var(--coral); }
h2 { font-size: 26px; font-weight: 900; }
.sub { color: var(--ink); opacity: .8; font-size: 16.5px; font-weight: 700; margin-bottom: 4px; }
.small { color: var(--dim); font-size: 14px; font-weight: 700; min-height: 1.2em; }
.btn { min-height: 48px; padding: 9px 14px; border-radius: 16px; font-weight: 800; font-size: 15px; background: #f1ecf7; transition: transform .08s; }
.btn.primary { color: #fff; font-size: 18px; background: linear-gradient(180deg, #ff9a6b, #ff6a4a); box-shadow: 0 5px 0 #d94f33; margin-bottom: 5px; }
.btn.big { min-height: 66px; font-size: 27px; font-weight: 900; border-radius: 22px; animation: breathe 1.7s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.04); } }
.btn.gold { background: linear-gradient(180deg, #ffd978, #ffc247); box-shadow: 0 5px 0 #e39a1f; margin-bottom: 5px; }
.panel.clear .btn:not(.primary) { background: rgba(255, 255, 255, .85); box-shadow: var(--shadow); }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; padding: 9px 5px; }

.rules { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 9px; text-align: left; font-size: 14.5px; line-height: 1.3; font-weight: 600; overflow: auto; }
.rules li { display: flex; align-items: center; gap: 12px; }
.rules .ic { flex: 0 0 40px; height: 40px; border-radius: 12px; background: #fff3d6; display: grid; place-items: center; font-size: 21px; }

.slider { display: grid; grid-template-columns: 1fr 46px; align-items: center; gap: 0 8px; text-align: left; font-weight: 800; font-size: 15px; }
.slider span { grid-column: 1 / -1; }
.slider output { text-align: right; color: var(--dim); font-variant-numeric: tabular-nums; font-size: 14px; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 44px; background: none; touch-action: pan-x; cursor: pointer; }
input[type=range]::-webkit-slider-runnable-track { height: 12px; border-radius: 9px; background: linear-gradient(90deg, var(--coral) var(--fill, 50%), #ebe4f2 var(--fill, 50%)); }
input[type=range]::-moz-range-track { height: 12px; border-radius: 9px; background: linear-gradient(90deg, var(--coral) var(--fill, 50%), #ebe4f2 var(--fill, 50%)); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 30px; height: 30px; margin-top: -9px; border-radius: 50%; background: #fff; box-shadow: 0 2px 8px rgba(74, 53, 96, .35); border: 3px solid var(--coral); }
input[type=range]::-moz-range-thumb { width: 26px; height: 26px; border-radius: 50%; background: #fff; border: 3px solid var(--coral); }
.toggle { display: flex; align-items: center; justify-content: space-between; min-height: 44px; font-weight: 800; font-size: 15px; text-align: left; cursor: pointer; position: relative; }
.toggle input { position: absolute; right: 0; width: 58px; height: 44px; opacity: 0; margin: 0; cursor: pointer; }
.toggle i { width: 58px; height: 34px; border-radius: 99px; background: #e4dcee; position: relative; transition: background .15s; pointer-events: none; }
.toggle i::after { content: ""; position: absolute; left: 4px; top: 4px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 2px 5px rgba(0, 0, 0, .2); }
.toggle input:checked + i { background: var(--leaf); }
.toggle input:checked + i::after { transform: translateX(24px); }
#settings.from-title .in-game { display: none; }

.final span, .over-stats span { font-size: 11px; letter-spacing: .1em; text-transform: uppercase; color: var(--dim); font-weight: 800; }
.final b { display: block; font-size: 56px; font-weight: 900; line-height: 1.05; color: var(--coral); font-variant-numeric: tabular-nums; }
.over-stats { display: flex; justify-content: space-around; }
.over-stats > div { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.over-stats b { font-size: 23px; font-weight: 800; }
.rank { font-weight: 800; font-size: 15.5px; padding: 9px 12px; border-radius: 14px; background: #f1ecf7; }
.rank.podium { background: linear-gradient(90deg, #ffe07a, #ffc247); animation: bump .5s; }
.rank.quiet { font-weight: 600; font-size: 14px; color: var(--dim); background: none; padding: 0; }

/* ---------- wide windows and sideways phones: the bar and Zoom move beside the board ---------- */
@media (min-aspect-ratio: 11/10) {
  #game { grid-template: "hud stage" 1fr "controls stage" auto / clamp(132px, 19vw, 210px) 1fr; gap: 10px 14px; }
  #hud { flex-direction: column; justify-content: flex-start; }
  .pill.wide, .pill.bar { flex: 0 0 auto; max-width: none; }
  .icon { flex: 0 0 50px; width: 100%; }
  #btn-zoom { min-height: 76px; }
  #btn-zoom span { font-size: 11px; }
}
@media (min-aspect-ratio: 11/10) {
  .panel.lo { display: grid; grid-template-columns: 1fr 1.25fr; grid-template-rows: 1fr auto; column-gap: 30px; align-items: center; }
  .lo-top { grid-row: 1 / span 2; } #lo-ham { width: min(92%, 300px); }
  .lo-stamp { top: -34px; right: 4%; }
  .panel.lo .btn.big { align-self: end; }
}
@media (min-aspect-ratio: 11/10) and (max-height: 430px) {
  #hud { gap: 5px; } .pill { padding: 2px 10px 3px; border-radius: 12px; } .pill span { font-size: 9.5px; } .pill b { font-size: 18px; }
  .pill.bar { padding: 4px 10px 6px; } .icon { flex-basis: 44px; min-height: 44px; } #btn-zoom { min-height: 56px; }
}
@media (orientation: landscape) and (max-height: 520px) {
  .panel { width: min(100%, 620px); padding: 14px 18px; gap: 8px; }
  .panel.clear { display: grid; grid-template-columns: 1fr 1fr; align-items: center; column-gap: 24px; width: min(100%, 700px); }
  .panel.clear #mascot, .panel.clear h1, .panel.clear .sub { grid-column: 1; }
  .panel.clear .btn.big, .panel.clear .row, .panel.clear .small { grid-column: 2; }
  .panel.clear #mascot { grid-row: 1; width: 170px; } .panel.clear h1 { grid-row: 2; font-size: 40px; } .panel.clear .sub { grid-row: 3; }
  .panel.clear .btn.big { grid-row: 1; } .panel.clear .row { grid-row: 2; } .panel.clear .small { grid-row: 3; }
  h2 { font-size: 21px; } .final b { font-size: 38px; }
  #over .panel, #settings .panel { display: grid; grid-template-columns: 1fr 1fr; column-gap: 18px; align-items: center; }
  #over h2, #over .rank, #settings h2, #settings .row, #settings #btn-done { grid-column: 1 / -1; }
  .rules { font-size: 13px; gap: 5px; } .rules .ic { flex-basis: 32px; height: 32px; font-size: 17px; }
  .btn { min-height: 44px; } .btn.big { min-height: 58px; font-size: 24px; }
  .panel.intro { width: min(100%, 720px); min-height: min(92dvh, 660px); display: grid; grid-template-columns: auto 1fr; column-gap: 24px; row-gap: 6px; align-items: center; text-align: left; padding: 14px 22px; }
  .panel.intro .in-kicker { grid-column: 2; justify-self: start; font-size: 13px; padding: 4px 12px; } #in-pic { grid-column: 1; grid-row: 1 / span 4; width: min(34vw, 70dvh, 220px); }
  .panel.intro h2 { grid-column: 2; font-size: 30px; } #in-text { grid-column: 2; font-size: 17px; } .panel.intro .btn.big { grid-column: 2; }
  .panel.lo { min-height: min(92dvh, 680px); row-gap: 8px; padding: 12px 20px 14px; }
  #lo-ham { width: min(80%, 190px); } .lo-top h2 { font-size: 30px; }
  .lo-mid { gap: 6px; } .lo-score b { font-size: 46px; } .lo-chips > div { padding: 5px 4px; } .lo-chips b { font-size: 19px; } .lo-total { padding-top: 6px; } .lo-total b { font-size: 28px; }
  .lo-bar { height: 14px; } .lo-need { font-size: 13px; }
}
@media (max-width: 380px) { #hud { gap: 5px; } .pill { padding: 5px 8px 6px; } .row { flex-wrap: wrap; } }
@media (prefers-reduced-motion: reduce) { .bg i, .btn.big { animation: none; } }
