:root {
  --bg: #f7f4ec;
  --ink: #1d2630;
  --muted: #687381;
  --panel: #fffaf0;
  --line: rgba(29, 38, 48, 0.18);
  --accent: #28666e;
  --accent-strong: #17494f;
  --glass: rgba(255, 255, 255, 0.58);
  --shadow: 0 16px 42px rgba(31, 41, 55, 0.14);
  --capacity: 4;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 20% 0%, rgba(252, 211, 77, 0.22), transparent 26rem),
    radial-gradient(circle at 90% 10%, rgba(45, 212, 191, 0.18), transparent 20rem),
    linear-gradient(180deg, #fbf7ed 0%, #edf3f2 100%);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button {
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.82);
  font: inherit;
  font-weight: 750;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.app {
  min-height: 100svh;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.topbar {
  width: min(760px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: 78px 1fr 78px;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  text-align: center;
}

.topbar h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: 0;
}

.label {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.topbar strong {
  display: block;
  font-size: 1.2rem;
}

.menu,
.game-screen {
  width: min(760px, 100%);
  margin: 0 auto;
}

.menu {
  display: grid;
  place-items: center;
}

.menu-panel,
.complete-panel {
  width: min(420px, 100%);
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.82);
  box-shadow: var(--shadow);
  text-align: center;
}

.menu-panel h2,
.complete-panel h2 {
  margin: 0 0 10px;
  font-size: 1.5rem;
}

.menu-panel p,
.complete-panel p {
  margin: 0 0 18px;
  color: #46515e;
  line-height: 1.45;
}

.primary-button,
.secondary-button {
  width: 100%;
  margin-top: 10px;
}

.primary-button {
  border-color: transparent;
  color: #fff;
  background: var(--accent);
}

.secondary-button {
  background: #fff;
}

.game-screen {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: 12px;
  min-height: 0;
}

.status-row {
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #42505c;
  font-size: 0.92rem;
  font-weight: 700;
}

.status-row span:last-child {
  color: var(--accent-strong);
  text-align: right;
}

.board {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(74px, 1fr));
  align-content: center;
  justify-items: center;
  gap: clamp(12px, 3vw, 24px) clamp(8px, 2.8vw, 18px);
  min-height: 0;
  padding: 6px 0 10px;
  position: relative;
}

.bottle-button {
  width: clamp(68px, 18vw, 94px);
  height: clamp(142px, 32svh, 218px);
  min-height: 0;
  display: grid;
  place-items: end center;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  position: relative;
  transition: transform 130ms ease, filter 130ms ease;
  touch-action: manipulation;
}

.board.many .bottle-button {
  width: clamp(58px, 15vw, 82px);
  height: clamp(118px, 25svh, 174px);
}

.bottle-button.selected {
  animation: selectedBob 850ms ease-in-out infinite;
}

.bottle-button.invalid {
  animation: shake 210ms ease;
}

.bottle-button.pour-source .tube,
.bottle-button.pour-target .tube {
  animation: pulse 420ms ease;
}

.bottle-button.pour-source {
  animation: pourTilt 420ms ease;
  transform-origin: 50% 22%;
}

.bottle-button.pour-target {
  animation: receiveBounce 420ms ease;
}

.tube {
  width: 74%;
  height: 88%;
  display: flex;
  flex-direction: column-reverse;
  overflow: hidden;
  position: relative;
  border: 3px solid rgba(73, 89, 102, 0.58);
  border-top: 0;
  border-radius: 0 0 24px 24px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.62), transparent 18%, transparent 72%, rgba(255, 255, 255, 0.38)),
    var(--glass);
  box-shadow: inset 0 0 14px rgba(255, 255, 255, 0.62), 0 8px 18px rgba(45, 55, 72, 0.12);
}

.tube::before {
  content: "";
  position: absolute;
  top: 0;
  left: -10%;
  width: 120%;
  height: 12px;
  border: 3px solid rgba(73, 89, 102, 0.58);
  border-bottom: 0;
  border-radius: 12px 12px 0 0;
  background: rgba(255, 255, 255, 0.52);
  z-index: 2;
}

.bottle-button.selected .tube {
  border-color: var(--accent);
  box-shadow: inset 0 0 14px rgba(255, 255, 255, 0.72), 0 0 0 5px rgba(40, 102, 110, 0.18), 0 10px 24px rgba(45, 55, 72, 0.16);
}

.segment {
  width: 100%;
  height: calc(100% / var(--capacity));
  min-height: calc(100% / var(--capacity));
  border-top: 1px solid rgba(255, 255, 255, 0.58);
  box-shadow: inset 0 5px 10px rgba(255, 255, 255, 0.22), inset 0 -7px 12px rgba(0, 0, 0, 0.13);
  animation: liquidSettle 220ms ease;
}

.segment:first-child {
  border-radius: 0 0 18px 18px;
}

.pour-stream {
  position: absolute;
  z-index: 4;
  height: 9px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transform-origin: 0 50%;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.36), 0 6px 12px rgba(31, 41, 55, 0.18);
  transition: transform 260ms ease, opacity 120ms ease;
}

.pour-stream::after {
  content: "";
  position: absolute;
  inset: -5px 12% -5px auto;
  width: 20%;
  border-radius: inherit;
  background: rgba(255, 255, 255, 0.46);
  animation: streamShine 420ms linear;
}

.pour-stream.active {
  opacity: 0.95;
}

.celebration-drop {
  position: absolute;
  top: 5%;
  z-index: 6;
  width: 10px;
  height: 16px;
  border-radius: 999px 999px 999px 3px;
  pointer-events: none;
  transform: translateY(0) rotate(45deg);
  animation: dropFall 980ms ease-in forwards;
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.controls button {
  padding: 0 10px;
}

.complete-panel {
  position: fixed;
  left: 50%;
  bottom: max(86px, calc(env(safe-area-inset-bottom) + 76px));
  transform: translateX(-50%);
  z-index: 5;
}

.complete-panel[hidden],
.game-screen[hidden],
.menu[hidden] {
  display: none;
}

.win-flash {
  animation: winFlash 720ms ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  30% { transform: translateX(-7px); }
  65% { transform: translateX(7px); }
}

@keyframes selectedBob {
  0%, 100% { transform: translateY(-8px); }
  50% { transform: translateY(-13px); }
}

@keyframes pourTilt {
  0%, 100% { transform: translateY(0) rotate(0); }
  40%, 70% { transform: translateY(-12px) rotate(-10deg); }
}

@keyframes receiveBounce {
  0%, 100% { transform: translateY(0); }
  55% { transform: translateY(6px); }
}

@keyframes liquidSettle {
  0% {
    transform: translateY(-8px) scaleY(0.72);
    opacity: 0.68;
  }
  100% {
    transform: translateY(0) scaleY(1);
    opacity: 1;
  }
}

@keyframes pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.12); }
}

@keyframes streamShine {
  from { transform: translateX(-220%); }
  to { transform: translateX(520%); }
}

@keyframes dropFall {
  0% {
    opacity: 0;
    transform: translate(0, 0) rotate(45deg) scale(0.7);
  }
  12% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate(var(--drift), 170px) rotate(225deg) scale(1);
  }
}

@keyframes winFlash {
  0%, 100% { filter: brightness(1); }
  45% { filter: brightness(1.16) saturate(1.2); }
}

@media (max-width: 380px) {
  .app {
    padding-left: 8px;
    padding-right: 8px;
  }

  .topbar {
    grid-template-columns: 62px 1fr 62px;
  }

  .topbar h1 {
    font-size: 1.1rem;
  }

  .board {
    grid-template-columns: repeat(auto-fit, minmax(62px, 1fr));
    gap: 10px 4px;
  }

  .bottle-button {
    width: 62px;
    height: clamp(150px, 41svh, 190px);
  }

  .controls button {
    font-size: 0.88rem;
    padding: 0 6px;
  }
}

@media (min-width: 720px) {
  .topbar h1 {
    font-size: 1.7rem;
  }

  .board {
    grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
  }
}
