:root {
  --sky: #ffd9a8; --cream: #fff7e8; --card: #fff3dc; --line: #f0dcbc; --text: #4a2c1a; --muted: #8a5a3b;
  --orange: #ff8a3d; --orange-dark: #c95f1c; --flag: #e2553f; --good: #4f9a3a;
  --stone: #e7bd7f; --stone-line: #c8955a; --stone-top: #f3d29c; --wood: #8a5a3b; --wood-dark: #5b3a26;
  --water: #3fbcd9; --water-deep: #1f8fb8; --foam: #a6ecf7; --basin: #f7e3c2;
  --font: 'Baloo 2', ui-rounded, 'SF Pro Rounded', system-ui, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; color: var(--text); font-family: var(--font); background: linear-gradient(160deg, #ffe2bd, #f2e6d2); overscroll-behavior: none; -webkit-user-select: none; user-select: none; overflow: hidden; }
body.paused *, body.paused *::before, body.paused *::after { animation-play-state: paused !important; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; padding: 0; -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 3px solid var(--flag); outline-offset: 2px; }
.defs { position: absolute; width: 0; height: 0; }
.splash { height: 100%; display: grid; place-items: center; color: var(--muted); font-weight: 600; }
.screen { position: relative; height: 100%; max-width: 520px; margin: auto; display: flex; flex-direction: column; overflow: hidden; background: linear-gradient(var(--sky), #ffeccd 38%, var(--cream) 62%); }

/* Illustrated scene behind each screen. */
.scene { position: absolute; left: 0; right: 0; top: 0; width: 100%; height: 56%; pointer-events: none; }
.s-sun { fill: #ffd35a; }
.s-sun-glow { fill: #ffd35a; opacity: .25; }
.s-cloud { fill: #ffffffcc; }
.s-far { fill: #c3e09e; }
.s-near { fill: #a3cf86; }
.s-tree path { stroke: #6b4a2e; stroke-width: 3; stroke-linecap: round; fill: none; }
.s-tree .s-leaf { fill: #7dbb5c; stroke: none; }

/* Menu */
#menu { overflow-y: auto; }
.menu-inner { position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: max(28px, env(safe-area-inset-top)) 22px max(18px, env(safe-area-inset-bottom)); }
h1 { font-weight: 800; font-size: clamp(46px, 15vw, 64px); line-height: 1; letter-spacing: -.5px; margin: 12px 0 0; }
.tagline { color: var(--muted); font-size: 17px; font-weight: 600; margin: 4px 0 0; }
.hero-art { flex: 1 1 auto; width: 100%; max-width: 340px; min-height: 150px; max-height: 360px; margin: 8px 0 14px; overflow: visible; }
.big-btn { position: relative; width: min(100%, 300px); min-height: 60px; border-radius: 20px; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 22px; font-weight: 800; line-height: 1.05; margin-bottom: 14px; transition: transform .08s, box-shadow .08s; }
.big-btn small { font-size: 13px; font-weight: 600; opacity: .85; margin-top: 2px; }
.big-btn b { font-weight: 800; }
.big-btn.orange { background: var(--orange); color: #fff; box-shadow: 0 6px 0 var(--orange-dark); }
.big-btn.cream { background: var(--card); color: var(--muted); box-shadow: 0 6px 0 #e3c69a; }
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--orange-dark); }
.big-btn.cream:active { box-shadow: 0 2px 0 #e3c69a; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: min(100%, 300px); margin: 4px 0 0; }
.stats div { display: flex; flex-direction: column; align-items: center; background: #fff3dccc; border-radius: 14px; padding: 6px 4px 5px; }
.stats dd { margin: 0; font-size: 21px; font-weight: 800; line-height: 1.1; }
.stats dt { color: var(--muted); font-size: 12px; font-weight: 600; }

/* Play */
#play .scene { height: 62%; }
.top-bar { position: relative; flex: none; display: flex; align-items: center; gap: 10px; padding: max(12px, env(safe-area-inset-top)) 14px 0; }
.round-plain { position: relative; flex: none; width: 46px; height: 46px; display: grid; place-items: center; color: var(--muted); border-radius: 50%; background: var(--card); }
.round-plain svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.round-plain .fill { fill: currentColor; stroke: none; }
#sound[aria-pressed="false"] .waves { display: none; }
#sound[aria-pressed="false"] { opacity: .55; }
.hud-title { flex: 1; min-width: 0; text-align: center; font-size: 23px; font-weight: 700; }
.hud-title strong { font-weight: 800; }
#play.no-sound .hud-title { margin-right: 56px; }
.status-row { position: relative; flex: none; display: flex; justify-content: center; gap: 8px; margin-top: 8px; }
.pill { background: var(--card); color: var(--muted); border-radius: 30px; padding: 4px 15px 3px; font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
#moves i { font-style: normal; color: var(--orange); letter-spacing: 1px; margin-right: 3px; }
.coach { position: relative; flex: none; min-height: 42px; margin: 4px 18px 0; text-align: center; font-size: 15px; font-weight: 600; color: var(--flag); line-height: 1.3; display: flex; align-items: center; justify-content: center; }
.board-wrap { position: relative; flex: 1; min-height: 0; min-width: 0; margin: 0 10px; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: manipulation; overflow: visible; }
.tools { position: relative; flex: none; display: flex; justify-content: space-around; background: var(--card); border-radius: 26px; padding: 9px 8px 7px; margin: 6px 14px max(12px, env(safe-area-inset-bottom)); }
.tool { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 76px; font-size: 13px; font-weight: 700; color: var(--muted); }
.tool-icon { position: relative; width: 48px; height: 48px; display: grid; place-items: center; border-radius: 50%; background: var(--cream); border: 2px solid var(--line); color: var(--orange); transition: transform .08s; }
.tool:active .tool-icon { transform: scale(.92); }
.tool svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.tool.main .tool-icon { background: var(--orange); border-color: var(--orange); color: #fff; }
.tool b { position: absolute; top: -4px; right: -5px; min-width: 20px; height: 20px; display: grid; place-items: center; background: var(--wood-dark); color: #fff; border: 2px solid var(--card); border-radius: 50%; font-size: 11px; font-weight: 700; }
.tool:disabled { opacity: .4; cursor: default; }

/* Canal */
.basin-back { fill: var(--basin); }
.water { fill: url(#water-fill); }
.water-line { fill: none; stroke: var(--foam); stroke-width: 3; }
.wave { animation: wave 2.6s linear infinite; }
.mark { stroke: var(--stone-line); stroke-opacity: .6; stroke-width: 2; }
.flag-line { stroke: var(--flag); stroke-width: 3; stroke-dasharray: 6 5; }
.flag-pole { stroke: var(--text); stroke-width: 2.6; stroke-linecap: round; }
.flag-cloth { fill: var(--flag); }
.basin.matched .flag-line { stroke: var(--good); stroke-dasharray: none; }
.basin.matched .flag-cloth { fill: var(--good); }
.wall { fill: var(--stone); }
.wall-top { fill: var(--stone-top); }
.brick { stroke: var(--stone-line); stroke-width: 1.5; }
.gate-panel { fill: var(--wood); transition: transform .35s ease-out; }
.gate-band { stroke: var(--wood-dark); stroke-width: 2; }
.conn.open .gate-panel, .conn.open .gate-band { transform: translateY(-34px); }
.post { fill: var(--wood-dark); }
.wheel circle, .wheel path { fill: none; stroke: var(--wood-dark); stroke-width: 4.5; stroke-linecap: round; }
.wheel .hub-dot { fill: var(--wood-dark); stroke: none; }
.wheel-spin { transform-box: fill-box; transform-origin: center; }
.conn.open .wheel-spin { animation: spin .5s ease-out; }
.pump-body { fill: var(--flag); }
.pump-arrow { fill: none; stroke: #fff; stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.pump-pipe { fill: var(--wood-dark); }
.conn.open .pump-press { animation: press .3s ease-out; }
.pump-press { transform-box: fill-box; transform-origin: center; }
.siphon-pipe { fill: none; stroke: var(--wood); stroke-width: 11; stroke-linecap: round; stroke-linejoin: round; }
.siphon-inner { fill: none; stroke: var(--stone-top); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; opacity: .7; }
.conn.open .siphon-inner { stroke: var(--foam); opacity: 1; }
.hero-halo { fill: #ffcf4a; opacity: .4; }
.halo { fill: #ffcf4a; opacity: .45; visibility: hidden; }
.conn.hint .halo { visibility: visible; animation: glow .8s ease-in-out infinite; }
.hit { fill: transparent; cursor: pointer; outline: none; }
.conn:has(.hit:focus-visible) .focus-ring { visibility: visible; }
.focus-ring { fill: none; stroke: var(--flag); stroke-width: 3; visibility: hidden; }
.conn.stuck { animation: shake .3s; }
.label { font-weight: 800; text-anchor: middle; fill: var(--muted); }
.label tspan { font-weight: 700; }
.basin.matched .label { fill: var(--good); }
.boat-hull { fill: var(--flag); }
.boat-stripe { stroke: #fff; stroke-opacity: .6; stroke-width: 2; }
.boat-sail { fill: var(--card); }
.boat-mast { stroke: var(--wood-dark); stroke-width: 3; }
.splash-ring { fill: none; stroke: var(--foam); stroke-width: 3; animation: ripple .6s ease-out forwards; pointer-events: none; }

/* Result */
.overlay { position: fixed; inset: 0; background: #4a2c1a99; display: grid; place-items: center; padding: 16px; z-index: 6; overflow-y: auto; }
.card { width: min(100%, 360px); background: var(--cream); border-radius: 30px; overflow: hidden; text-align: center; box-shadow: 0 22px 50px #4a2c1a66; animation: land .34s cubic-bezier(.3, 1.4, .5, 1); }
.card-top { position: relative; height: 132px; display: grid; place-items: center; background: linear-gradient(var(--sky), #ffeccd); }
.card-top .scene { height: 100%; }
#result-art { position: relative; width: 150px; height: 105px; overflow: visible; margin-bottom: 20px; }
.card-body { position: relative; display: flex; flex-direction: column; align-items: center; background: var(--cream); border-radius: 26px 26px 0 0; margin-top: -22px; padding: 12px 22px 8px; }
.card h2 { font-weight: 800; font-size: 26px; margin: 2px 0 0; line-height: 1.15; }
.card p { color: var(--muted); font-size: 15px; font-weight: 600; margin: 2px 0 14px; line-height: 1.35; }
.card .big-btn { min-height: 54px; font-size: 19px; margin-bottom: 12px; }
.result-stars { font-size: 42px; line-height: 1.1; color: var(--line); }
.result-stars span { display: inline-block; margin: 0 1px; }
.result-stars .on { color: var(--orange); animation: stamp .42s backwards; }
.result-stars .on:nth-child(2) { animation-delay: .15s; }
.result-stars .on:nth-child(3) { animation-delay: .3s; }
.text-btn { color: var(--muted); font-weight: 700; padding: 6px 10px; font-size: 15px; min-height: 44px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--wood-dark); color: #fff; font-weight: 600; font-size: 15px; padding: 10px 16px; border-radius: 14px; z-index: 8; white-space: nowrap; }
.confetti { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 7; }
.confetti span { position: absolute; top: -24px; width: 12px; height: 12px; border-radius: 3px; animation: fall linear forwards; }

@keyframes wave { to { transform: translateX(-37px); } }
@keyframes spin { to { transform: rotate(240deg); } }
@keyframes press { 40% { transform: scale(.85); } }
@keyframes ripple { from { transform: scale(.4); opacity: 1; } to { transform: scale(1.6); opacity: 0; } }
@keyframes stamp { 0% { transform: scale(2.2); opacity: 0; } 70% { transform: scale(.9); opacity: 1; } 100% { } }
@keyframes land { from { transform: translateY(26px) scale(.94); opacity: 0; } }
@keyframes glow { 50% { opacity: .15; } }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
@keyframes fall { to { transform: translate3d(var(--drift), 112vh, 0) rotate(var(--spin)); } }

/* Larger screens: the game sits as a framed card on the warm background. */
@media (min-width: 560px) and (min-height: 521px) {
  .screen { max-height: min(100%, 900px); top: 50%; transform: translateY(-50%); border-radius: 34px; box-shadow: 0 24px 60px #4a2c1a33; height: calc(100% - 32px); }
}
/* Short, wide screens: canal on the left, title and controls on the right. */
@media (max-height: 520px) {
  .screen { max-width: none; }
  #menu .menu-inner { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 300px); grid-template-rows: repeat(5, auto); align-content: center; column-gap: 24px; align-items: center; justify-items: center; padding: 12px 20px; }
  #menu h1 { grid-column: 2; font-size: 44px; margin: 0; }
  #menu .tagline { grid-column: 2; font-size: 15px; margin-bottom: 10px; }
  .hero-art { grid-column: 1; grid-row: 1 / span 5; height: calc(100vh - 24px); max-height: none; min-height: 0; margin: 0; }
  #play-level, #play-daily, .stats { grid-column: 2; }
  .big-btn { min-height: 50px; margin-bottom: 9px; font-size: 19px; }
  .big-btn small { display: none; }
  .stats dd { font-size: 17px; }
  #play { display: grid; grid-template-columns: minmax(0, 1fr) 230px; grid-template-rows: auto auto minmax(0, 1fr) auto; }
  #play .scene { height: 100%; }
  .top-bar { grid-column: 2; grid-row: 1; padding-top: 10px; }
  .hud-title { font-size: 19px; }
  .status-row { grid-column: 2; grid-row: 2; flex-wrap: wrap; }
  .coach { grid-column: 2; grid-row: 3; align-self: center; font-size: 13.5px; margin: 0 8px; }
  .board-wrap { grid-column: 1; grid-row: 1 / span 4; margin: 10px 4px 8px 12px; }
  .tools { grid-column: 2; grid-row: 4; margin: 0 8px 8px; }
  .tool { min-width: 56px; font-size: 11.5px; }
  .tool-icon { width: 42px; height: 42px; }
  .overlay { padding: 8px; }
  .card { display: grid; grid-template-columns: 36% 1fr; width: min(100%, 560px); }
  .card-top { height: auto; }
  .card-body { margin: 0 0 0 -18px; border-radius: 22px 0 0 22px; padding: 8px 16px 2px; }
  .result-stars { font-size: 30px; }
  .card h2 { font-size: 21px; }
  .card p { margin-bottom: 8px; font-size: 13.5px; }
  .card .big-btn { min-height: 46px; margin-bottom: 8px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
