:root {
  --ground: #E4EBD9;
  --bed: #CFDCC0;
  --bed-edge: #B6C7A4;
  --pipe: #8E9A8C;
  --pipe-core: #BAC4B4;
  --water: #2A84D0;
  --water-core: #86CBF7;
  --bud: #9BA894;
  --bud-heart: #B7C1AE;
  --petal: #EC6A8C;
  --heart: #F4C242;
  --brass: #C08F37;
  --ink: #213520;
  --muted: #5A6D55;
  --panel: #F3F6EE;
  --serif: ui-serif, "New York", Georgia, serif;
  --text: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ground: #15231B;
    --bed: #213528;
    --bed-edge: #2D4535;
    --pipe: #6E8273;
    --pipe-core: #4A5D50;
    --water: #3A9FEE;
    --water-core: #94D5FF;
    --bud: #81937F;
    --bud-heart: #A0B09C;
    --ink: #E1EADC;
    --muted: #9BB097;
    --panel: #1D2F24;
  }
}

* { box-sizing: border-box; }
html { background: var(--ground); -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--ground);
  color: var(--ink);
  font: 400 16px/1.45 var(--text);
  padding: calc(env(safe-area-inset-top) + 8px) max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 24px) max(16px, env(safe-area-inset-left));
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--water); outline-offset: 2px; }

.top, .modes, .hint, main { width: 100%; max-width: 520px; margin-inline: auto; }

/* Header */
.top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 12px 0 14px; }
h1 { margin: 0; font: 700 38px/1 var(--serif); letter-spacing: -0.01em; }
.sub { margin: 6px 0 0; color: var(--muted); font-size: 15px; }
.top-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: var(--bed);
  color: var(--ink);
}

/* Daily / Practice switch */
.modes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  margin-bottom: 14px;
  border-radius: 14px;
  background: var(--bed);
}
.modes button {
  min-height: 40px;
  border: 0;
  border-radius: 10px;
  background: none;
  font-weight: 600;
  color: var(--muted);
}
.modes button[aria-pressed="true"] { background: var(--panel); color: var(--ink); box-shadow: 0 1px 2px rgb(0 0 0 / 0.12); }

/* Install hint */
.hint { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; padding: 12px 12px 12px 16px; border-radius: 12px; background: var(--panel); }
.hint[hidden] { display: none; }
.hint p { margin: 0; flex: 1; font-size: 15px; }
.hint .btn { flex: none; background: var(--ink); color: var(--ground); }

/* Board */
.board {
  --n: 6;
  width: min(100%, max(280px, calc(100dvh - 330px)));
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: 4px;
  padding: 6px;
  border-radius: 16px;
  background: var(--bed-edge);
  user-select: none;
  -webkit-user-select: none;
}

.tile {
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 9px;
  background: var(--bed);
  overflow: hidden;
  transition: transform 0.08s;
}
.tile:active { transform: scale(0.95); }
.won .tile { cursor: default; }
.won .tile:active { transform: none; }

.tile svg { display: block; width: 100%; height: 100%; transition: transform 0.16s ease-out; }

.arm, .core { fill: none; transition: stroke 0.15s; }
.arm { stroke: var(--pipe); stroke-width: 26; }
.core { stroke: var(--pipe-core); stroke-width: 9; }
.arm-fill { fill: var(--pipe); transition: fill 0.15s; }
.core-fill { fill: var(--pipe-core); transition: fill 0.15s; }
.spring { fill: var(--brass); }
.spring-water { fill: var(--water-core); }

.flower {
  transform-box: fill-box;
  transform-origin: center;
  transform: scale(0.62);
  transition: transform 0.28s cubic-bezier(.3,1.6,.5,1);
}
.petal { fill: var(--bud); transition: fill 0.2s; }
.heart { fill: var(--bud-heart); transition: fill 0.2s; }

.wet .arm { stroke: var(--water); }
.wet .core { stroke: var(--water-core); }
.wet .arm-fill { fill: var(--water); }
.wet .core-fill { fill: var(--water-core); }
.wet .flower { transform: scale(1); }
.wet .petal { fill: var(--petal); }
.wet .heart { fill: var(--heart); }

/* The one orchestrated moment: when solved, a ripple runs outward from the tap */
.won .core, .won .core-fill { animation: ripple 0.7s ease-out both; animation-delay: calc(var(--depth) * 55ms); }
.won .flower { animation: bloom 0.5s cubic-bezier(.3,1.6,.5,1) both; animation-delay: calc(var(--depth) * 55ms + 120ms); }
@keyframes ripple {
  0% { stroke: var(--water-core); fill: var(--water-core); }
  40% { stroke: #fff; fill: #fff; }
  100% { stroke: var(--water-core); fill: var(--water-core); }
}
@keyframes bloom {
  0% { transform: scale(1); }
  45% { transform: scale(1.3); }
  100% { transform: scale(1); }
}

/* Meter */
.meter {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 12px auto 0;
  width: min(100%, max(280px, calc(100dvh - 330px)));
  color: var(--muted);
  font-size: 15px;
}
.meter b { color: var(--ink); font-variant-numeric: tabular-nums; }

/* Practice controls */
.practice-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin: 14px auto 0;
  width: min(100%, max(280px, calc(100dvh - 330px)));
}
.practice-bar[hidden] { display: none; }
.sizes { display: flex; gap: 4px; padding: 4px; border-radius: 12px; background: var(--bed); }
.sizes button { min-height: 36px; padding: 0 10px; border: 0; border-radius: 8px; background: none; font-size: 15px; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }
.sizes button[aria-pressed="true"] { background: var(--panel); color: var(--ink); }

/* Result */
.result {
  margin: 16px auto 0;
  width: min(100%, max(280px, calc(100dvh - 330px)));
  padding: 16px;
  border-radius: 14px;
  background: var(--panel);
}
.result[hidden] { display: none; }
.result h2 { margin: 0 0 6px; font: 700 24px/1.2 var(--serif); }
.result p { margin: 0; }

/* Buttons */
.btn { min-height: 44px; padding: 0 18px; border: 0; border-radius: 22px; font-weight: 700; }
.btn.primary { background: var(--water); color: #fff; }
.btn.ghost { background: var(--bed); color: var(--ink); }
.btn.small { min-height: 36px; padding: 0 14px; background: var(--water); color: #fff; }
.actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.actions.start { justify-content: flex-start; margin-top: 14px; }

/* Dialogs */
dialog {
  width: min(100vw - 32px, 460px);
  max-height: calc(100dvh - 48px);
  overflow: auto;
  border: 0;
  border-radius: 16px;
  padding: 20px;
  background: var(--panel);
  color: var(--ink);
}
dialog::backdrop { background: rgb(10 20 14 / 0.55); }
dialog h2 { margin: 0 0 14px; font: 700 26px/1.2 var(--serif); }
dialog p, dialog ul { margin: 0 0 12px; }
dialog ul { padding-left: 1.2em; }
dialog li { margin-bottom: 6px; }

.help-art { display: block; width: 100%; max-width: 300px; margin: 0 0 14px; }
.help-bed { fill: var(--bed); }
.help-arm { stroke: var(--water); stroke-width: 26; fill: none; }
.help-core { stroke: var(--water-core); stroke-width: 9; fill: none; }
.help-arm-fill { fill: var(--water); }
.help-core-fill { fill: var(--water-core); }
.help-spring { fill: var(--brass); }
.help-flower { fill: var(--petal); }
.help-heart { fill: var(--heart); }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 0 0 14px; }
.stats dt { color: var(--muted); font-size: 14px; font-weight: 600; }
.stats dd { margin: 2px 0 0; font: 700 30px/1.1 var(--serif); font-variant-numeric: tabular-nums; }
.note { color: var(--muted); font-size: 14px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  transform: translateX(-50%);
  width: min(100vw - 32px, 460px);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 12px 12px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--ground);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.3);
  z-index: 10;
}
.toast[hidden] { display: none; }
.toast span { flex: 1; }

@media (prefers-reduced-motion: reduce) {
  .tile, .tile svg, .flower, .arm, .core, .petal, .heart { transition: none; }
  .won .core, .won .core-fill, .won .flower { animation: none; }
}
