:root {
  --ink: #2f3a33;
  --ink-soft: #6d7a6d;
  --paper: #fdf6e6;
  --card: #fffdf7;
  --line: #ece3cd;
  --liquid: #b7ff4a;
  --liquid-dark: #6aa32c;
  --radius: 22px;
  --shadow-soft: 0 10px 26px rgba(63, 74, 55, 0.09);
  --shadow-lift: 0 16px 38px rgba(63, 74, 55, 0.16);
  /* The drawing's width divided by its height; set per level from JS. */
  --stage-ratio: 0.4612;
  --stage-max-height: calc(100vh - 300px);
  --stage-max-height: calc(100svh - 300px);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Avenir Next Rounded",
    "Trebuchet MS", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-width: 320px;
  overflow-x: hidden;
  color: var(--ink);
  background: var(--paper);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
}

body.has-immersive { overflow: hidden; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---------- playful background ---------- */

.sky { position: fixed; inset: 0; z-index: -1; overflow: hidden; }
.blob { position: absolute; border-radius: 50%; filter: blur(48px); opacity: 0.5; }
.blob-a { width: 46vmax; height: 46vmax; top: -16vmax; left: -12vmax; background: #cdf0ff; }
.blob-b { width: 38vmax; height: 38vmax; bottom: -14vmax; right: -10vmax; background: #ffe6b8; }
.blob-c { width: 30vmax; height: 30vmax; top: 40%; right: 18%; background: #d9f7c8; opacity: 0.42; }

/* ---------- shell ---------- */

.app {
  width: min(1240px, 100% - 32px);
  margin: 0 auto;
  padding: clamp(18px, 3.4vh, 34px) 0 40px;
}

.top { text-align: center; }
h1 {
  margin: 0;
  font-size: clamp(30px, 5.4vw, 54px);
  font-weight: 800;
  letter-spacing: -0.02em;
}
h1 em { font-style: normal; color: #3aa0d8; }
.sub { margin: 8px 0 0; color: var(--ink-soft); font-size: clamp(14px, 1.6vw, 17px); font-weight: 600; }

/* ---------- level picker ---------- */

.levels { margin: clamp(14px, 2.4vh, 24px) 0; }
.level-strip {
  display: flex;
  gap: 10px;
  padding: 4px 2px 10px;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scrollbar-width: thin;
}
.level-card {
  flex: 0 0 auto;
  scroll-snap-align: center;
  display: grid;
  justify-items: center;
  gap: 3px;
  min-width: 86px;
  padding: 10px 12px;
  border: 2px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow-soft);
  color: var(--ink);
  font-family: inherit;
  cursor: pointer;
  transition: transform 140ms ease, border-color 140ms ease, box-shadow 140ms ease;
}
.level-card:hover { transform: translateY(-3px); }
.level-card[aria-pressed="true"] {
  border-color: var(--liquid-dark);
  box-shadow: var(--shadow-lift), 0 0 0 4px color-mix(in srgb, var(--liquid) 32%, transparent);
}
.level-emoji { font-size: 26px; line-height: 1; }
.level-name { font-size: 12px; font-weight: 700; white-space: nowrap; }

/* ---------- stage + panel ---------- */

.play-area {
  display: grid;
  grid-template-columns: 1fr minmax(280px, 360px);
  gap: clamp(20px, 3vw, 44px);
  align-items: start;
}

.stage-shell { display: flex; justify-content: center; }
.stage {
  position: relative;
  aspect-ratio: var(--stage-ratio);
  width: min(100%, calc(var(--stage-max-height) * var(--stage-ratio)));
  overflow: hidden;
  border-radius: var(--radius);
  background: #efece1;
  box-shadow: var(--shadow-lift);
}
.stage-photo, .stage-art, .stage-art svg {
  display: block;
  width: 100%;
  height: 100%;
}
.stage-photo { position: absolute; inset: 0; object-fit: fill; filter: saturate(0.7) contrast(1.08); }
.stage-art { position: absolute; inset: 0; }

.fluid-layer { position: absolute; inset: 0; width: 100%; height: 100%; overflow: hidden; pointer-events: none; }
.pool { fill: url(#liquidGradient); opacity: 0.85; }
.jet { fill: url(#liquidGradient); opacity: 0.97; filter: url(#softGlow); }
.surface { fill: none; stroke-width: 0.9; opacity: 0.9; stroke-linecap: round; filter: url(#softGlow); }
.bubbles circle { fill: #ffffff; opacity: 0.5; }
.flow-head { filter: url(#softGlow); transition: opacity 350ms ease; }

.stage-button {
  position: absolute;
  right: 10px;
  top: 10px;
  display: grid;
  place-items: center;
  width: 46px; height: 46px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.86);
  box-shadow: var(--shadow-soft);
  color: var(--ink);
  font-size: 19px;
  cursor: pointer;
  backdrop-filter: blur(6px);
}
.stage-button:hover { background: #fff; }

/* ---------- fullscreen / immersive ---------- */

.stage-shell:fullscreen,
.stage-shell.is-immersive {
  position: fixed;
  inset: 0;
  z-index: 60;
  align-items: center;
  padding: 12px;
  background: var(--paper);
}
.stage-shell:fullscreen .stage,
.stage-shell.is-immersive .stage {
  width: min(100%, calc((100svh - 24px) * var(--stage-ratio)));
  max-height: calc(100svh - 24px);
}

/* ---------- panel ---------- */

.panel {
  display: grid;
  gap: 16px;
  padding: clamp(16px, 2vw, 22px);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow-soft);
}

.gauge-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.gauge-status { color: var(--ink-soft); font-size: 14px; font-weight: 700; }
.gauge-value { font-size: 15px; font-weight: 700; color: var(--ink-soft); }
.gauge-value b { font-size: 26px; color: var(--ink); }
.gauge-track { height: 14px; margin-top: 8px; border-radius: 999px; background: #eee8d6; overflow: hidden; }
.gauge-bar {
  display: block; height: 100%; width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, color-mix(in srgb, var(--liquid) 70%, #fff), var(--liquid));
  transition: width 180ms linear;
}

.big-buttons { display: flex; justify-content: center; gap: 16px; }
.round-button {
  display: grid;
  place-items: center;
  width: 62px; height: 62px;
  border: 2px solid var(--line);
  border-radius: 50%;
  background: #fff;
  box-shadow: var(--shadow-soft);
  color: var(--ink);
  font-family: inherit;
  font-size: 20px;
  cursor: pointer;
  transition: transform 140ms ease, box-shadow 140ms ease;
}
.round-play {
  width: 82px; height: 82px;
  border: 0;
  background: var(--liquid);
  color: #22331a;
  font-size: 26px;
  box-shadow: 0 12px 26px color-mix(in srgb, var(--liquid) 55%, transparent);
}
.round-button:hover { transform: translateY(-3px); box-shadow: var(--shadow-lift); }
.round-button:active { transform: translateY(0) scale(0.96); }

.control-title { margin: 0 0 10px; font-size: 15px; font-weight: 800; }
/* Six in a row, always - a wrapped single dot on its own line looks broken. */
.swatches { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.swatch {
  width: 100%; aspect-ratio: 1; max-width: 46px;
  border: 3px solid transparent;
  border-radius: 50%;
  background: var(--dot);
  box-shadow: var(--shadow-soft);
  cursor: pointer;
  transition: transform 140ms ease, border-color 140ms ease;
}
.swatch:hover { transform: scale(1.08); }
.swatch[aria-pressed="true"] { border-color: var(--ink); transform: scale(1.1); }

/* Grid and flex children default to min-content width; a range input's
   min-content is wide enough to burst a narrow column, so let them shrink. */
.panel > *, .slider-row { min-width: 0; }
.slider-row { display: flex; align-items: center; gap: 10px; }
.slider-end { flex: 0 0 auto; color: var(--ink-soft); font-size: 13px; font-weight: 700; }
input[type="range"] {
  flex: 1 1 0;
  min-width: 0;
  height: 34px;
  accent-color: var(--liquid-dark);
  cursor: pointer;
}

button:focus-visible, input:focus-visible {
  outline: 3px solid #3aa0d8;
  outline-offset: 3px;
}

/* ---------- celebration ---------- */

.cheer {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(253, 246, 230, 0.72);
  backdrop-filter: blur(3px);
}
.cheer[hidden] { display: none; }
.cheer-card {
  position: relative; z-index: 2;
  padding: 20px 18px;
  border-radius: var(--radius);
  background: #fff;
  box-shadow: var(--shadow-lift);
  text-align: center;
  animation: pop 320ms cubic-bezier(0.2, 1.4, 0.4, 1);
}
.cheer-title { margin: 0; font-size: 24px; font-weight: 800; }
.cheer-text { margin: 6px 0 14px; color: var(--ink-soft); font-size: 14px; font-weight: 600; }
.cheer-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.pill {
  padding: 12px 16px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.pill-strong { border-color: transparent; background: var(--liquid); color: #22331a; }
.pill:hover { transform: translateY(-2px); }

.confetti { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.confetti-piece {
  position: absolute;
  top: -12%;
  left: var(--x);
  width: 10px; height: 14px;
  border-radius: 3px;
  background: var(--tint);
  animation: fall 1900ms var(--delay) cubic-bezier(0.3, 0.7, 0.5, 1) forwards;
}

@keyframes pop { from { transform: scale(0.82); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes fall {
  to { transform: translateY(115vh) rotate(var(--spin)); opacity: 0.1; }
}

/* ---------- responsive ---------- */

@media (max-width: 900px) {
  :root { --stage-max-height: calc(100svh - 300px); }
  .play-area { gap: 18px; grid-template-columns: 1fr minmax(250px, 320px); }
}

/* Below this the two columns stop fitting, so the panel moves underneath. */
@media (max-width: 700px) {
  :root { --stage-max-height: calc(100svh - 330px); }
  .play-area { grid-template-columns: 1fr; }
  .panel { width: min(100%, 520px); margin: 0 auto; }
}

@media (max-width: 560px) {
  /* Keep the drawing and the play button on one screen. */
  :root { --stage-max-height: 54svh; }
  .app { width: min(100% - 20px, 460px); padding-top: 14px; }
  h1 { font-size: clamp(26px, 8.5vw, 38px); }
  .sub { font-size: 13px; }
  .levels { margin: 12px 0; }
  .level-card { min-width: 76px; padding: 8px 10px; }
  .level-emoji { font-size: 23px; }
  .panel { gap: 14px; padding: 14px; }
  .round-play { width: 74px; height: 74px; }
  .round-button { width: 58px; height: 58px; }
  .swatch { max-width: 44px; }
}

/* Phone in landscape: almost no height, so the panel goes two columns wide
   and everything shrinks, rather than pushing the drawing off screen. */
@media (max-height: 560px) and (orientation: landscape) {
  :root { --stage-max-height: calc(100svh - 96px); }
  .app { width: min(100% - 20px, 100%); padding: 8px 0 12px; }
  .top { display: flex; align-items: baseline; justify-content: center; gap: 12px; }
  h1 { font-size: 20px; }
  .sub { display: none; }
  .levels { margin: 8px 0; }
  .level-card { min-width: 66px; padding: 5px 8px; border-radius: 14px; }
  .level-emoji { font-size: 18px; }
  .level-name { font-size: 10px; }
  .play-area { grid-template-columns: 1fr minmax(300px, 400px); gap: 16px; }
  .panel { grid-template-columns: 1fr 1fr; gap: 10px 14px; padding: 12px; align-items: center; }
  .panel > .gauge { grid-column: 1 / -1; }
  .round-play { width: 58px; height: 58px; font-size: 20px; }
  .round-button { width: 48px; height: 48px; }
  .swatches { gap: 6px; }
  .swatch { max-width: 36px; }
  .control-title { margin-bottom: 6px; font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  .level-card, .round-button, .swatch, .pill, .flow-head, .gauge-bar { transition: none; }
  .cheer-card { animation: none; }
  .confetti-piece { display: none; }
}
