:root {
  --ink: #1a1330;
  --ink-deep: #0f0a1f;
  --plum: #2c1f52;
  --text: #f6f0ff;
  --muted: #b8acd6;
  --brass: #ffcf6b;
  --brass-deep: #c98a2b;
  --glass: rgba(255, 255, 255, 0.07);
  --rim: rgba(255, 255, 255, 0.62);
  --shelf: #5b3f8f;
  --vial-w: 56px;
  --vial-h: 220px;
  --gap: 18px;
  --row-gap: 30px;
  --cols: 5;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--ink-deep);
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", "PingFang HK", "Noto Sans TC", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

body {
  background:
    radial-gradient(120% 70% at 50% 0%, #3a2a6e 0%, transparent 60%),
    radial-gradient(90% 60% at 50% 110%, #43214f 0%, transparent 65%),
    linear-gradient(180deg, var(--ink) 0%, var(--ink-deep) 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image: radial-gradient(rgba(255, 255, 255, 0.05) 1px, transparent 1.5px);
  background-size: 22px 22px;
  mask-image: linear-gradient(180deg, #000 0%, transparent 70%);
}

button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
button:disabled { cursor: default; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.app {
  position: relative;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  height: 100dvh;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.title { text-align: center; }
.title h1 { margin: 0; font-size: 1.35rem; font-weight: 800; letter-spacing: 0.02em; }
.count { margin: 2px 0 0; color: var(--muted); font-size: 0.9rem; }
.count b { color: var(--brass); font-variant-numeric: tabular-nums; }

.round {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 0;
  background: transparent;
}
.round.ghost { background: rgba(255, 255, 255, 0.07); box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12); }
.round.ghost:active { background: rgba(255, 255, 255, 0.16); }

.status {
  min-height: 2.6em;
  margin: 6px auto 0;
  max-width: 28rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.3;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--vial-w));
  justify-content: center;
  align-content: center;
  column-gap: var(--gap);
  row-gap: var(--row-gap);
  min-height: 0;
  padding-top: calc(var(--vial-w) * 0.35);
}

.vial {
  position: relative;
  width: var(--vial-w);
  height: var(--vial-h);
  padding: 0;
  border: 0;
  background: none;
  transition: transform 230ms cubic-bezier(0.3, 0.7, 0.3, 1);
  transform-origin: 50% 0;
}

.vial::after {
  content: "";
  position: absolute;
  left: calc(var(--gap) / -2 - 1px);
  right: calc(var(--gap) / -2 - 1px);
  bottom: -9px;
  height: 7px;
  border-radius: 3px;
  background: linear-gradient(180deg, #7a58b8, var(--shelf) 60%, #3b2766);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.45);
}

.vial.pouring { z-index: 5; transition-duration: 230ms; }
.vial.pouring::after { opacity: 0; }

.glass {
  position: absolute;
  inset: calc(var(--vial-w) * 0.16) 0 0;
  display: flex;
  flex-direction: column-reverse;
  padding: calc(var(--vial-w) * 0.42) calc(var(--vial-w) * 0.1) calc(var(--vial-w) * 0.12);
  border-radius: calc(var(--vial-w) * 0.14) calc(var(--vial-w) * 0.14) calc(var(--vial-w) * 0.5) calc(var(--vial-w) * 0.5);
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.16) 0 12%, transparent 22% 76%, rgba(255, 255, 255, 0.08) 86%),
    var(--glass);
  box-shadow:
    inset 0 0 0 2px var(--rim),
    inset 0 -10px 18px rgba(0, 0, 0, 0.18),
    0 0 0 1px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  transition: transform 180ms ease, box-shadow 180ms ease;
}

.glass::before {
  content: "";
  position: absolute;
  top: calc(var(--vial-w) * -0.04);
  left: -3px;
  right: -3px;
  height: calc(var(--vial-w) * 0.12);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.28);
  box-shadow: inset 0 0 0 2px var(--rim);
}

.slot {
  position: relative;
  flex: 1 1 0;
  display: grid;
  place-items: center;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.22), transparent 30% 70%, rgba(0, 0, 0, 0.16)),
    var(--c, transparent);
  transform: scaleY(0);
  transform-origin: 50% 100%;
  transition: transform 300ms cubic-bezier(0.25, 0.8, 0.3, 1);
}
.slot:first-child { border-radius: 0 0 calc(var(--vial-w) * 0.4) calc(var(--vial-w) * 0.4); }
.slot.on { transform: scaleY(1); }
.slot.surface { box-shadow: inset 0 calc(var(--vial-w) * 0.1) 0 calc(var(--vial-w) * -0.04) rgba(255, 255, 255, 0.32); }
.slot i {
  font-style: normal;
  font-size: calc(var(--vial-w) * 0.26);
  line-height: 1;
  color: rgba(20, 10, 40, 0.42);
  font-variant-emoji: text;
  opacity: 0;
  transition: opacity 200ms ease;
}
.slot.on i { opacity: 1; }

.vial.picked .glass,
.vial.picked .cork { transform: translateY(calc(var(--vial-w) * -0.32)); }
.vial.picked .glass {
  box-shadow:
    inset 0 0 0 2px var(--brass),
    inset 0 -10px 18px rgba(0, 0, 0, 0.18),
    0 0 22px rgba(255, 207, 107, 0.45);
}

.cork {
  position: absolute;
  left: 18%;
  right: 18%;
  top: calc(var(--vial-w) * -0.18);
  height: calc(var(--vial-w) * 0.3);
  border-radius: 5px 5px 3px 3px;
  background:
    repeating-linear-gradient(90deg, rgba(0, 0, 0, 0.12) 0 2px, transparent 2px 6px),
    linear-gradient(180deg, #f2c079, var(--brass-deep));
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
  opacity: 0;
  transform: translateY(-140%);
  pointer-events: none;
  transition: transform 180ms ease;
}

.vial.sealed .cork { animation: cork-in 420ms cubic-bezier(0.3, 1.5, 0.5, 1) forwards; }
.vial.sealed .glass { animation: glint 900ms ease-out; }

@keyframes cork-in {
  0% { opacity: 0; transform: translateY(-160%); }
  60% { opacity: 1; transform: translateY(10%); }
  100% { opacity: 1; transform: translateY(0); }
}

@keyframes glint {
  0% { box-shadow: inset 0 0 0 2px var(--rim), 0 0 0 rgba(255, 255, 255, 0); }
  30% { box-shadow: inset 0 0 0 2px #fff, 0 0 26px rgba(255, 255, 255, 0.55); }
  100% { box-shadow: inset 0 0 0 2px var(--rim), 0 0 0 rgba(255, 255, 255, 0); }
}

.vial.deny .glass { animation: deny 320ms ease; }
@keyframes deny {
  0%, 100% { translate: 0; }
  20% { translate: -5px; }
  40% { translate: 5px; }
  60% { translate: -3px; }
  80% { translate: 3px; }
}

.stream {
  position: fixed;
  z-index: 4;
  width: 6px;
  margin-left: -3px;
  border-radius: 3px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.35), transparent 60%), var(--c);
  transform-origin: 50% 0;
  animation: stream-in 140ms ease-out forwards;
  pointer-events: none;
}
.stream.done { animation: stream-out 180ms ease-in forwards; }
@keyframes stream-in { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes stream-out { from { transform: scaleY(1); opacity: 1; } to { transform: translateY(40%) scaleY(0.2); opacity: 0; } }

.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  max-width: 28rem;
  width: 100%;
  margin: 12px auto 0;
}

.control {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-height: 64px;
  padding: 10px 6px 8px;
  border: 0;
  border-radius: 20px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0.05));
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.14), 0 6px 16px rgba(0, 0, 0, 0.3);
  font-size: 0.82rem;
  font-weight: 700;
  transition: transform 120ms ease, opacity 160ms ease;
}
.control:active:not(:disabled) { transform: scale(0.95); }
.control:disabled { opacity: 0.4; }
.control em {
  position: absolute;
  top: 6px;
  right: 10px;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--brass);
  color: var(--ink);
  font-style: normal;
  font-size: 0.75rem;
}

body.won .board { animation: hum 420ms ease; }
@keyframes hum { 50% { transform: scale(1.03); filter: brightness(1.25); } }

.result {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: end center;
  padding: 16px 16px max(24px, env(safe-area-inset-bottom));
  background: rgba(8, 4, 18, 0.55);
  backdrop-filter: blur(3px);
  animation: fade 220ms ease;
}
.result[hidden] { display: none; }

.card {
  position: relative;
  width: 100%;
  max-width: 24rem;
  padding: 34px 22px 22px;
  border-radius: 28px;
  text-align: center;
  background: linear-gradient(180deg, #3a2a6e, #241848);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16), 0 20px 50px rgba(0, 0, 0, 0.5);
  animation: rise 360ms cubic-bezier(0.2, 1.3, 0.4, 1);
}
.card h2 { margin: 0 0 6px; font-size: 1.6rem; }
.card p { margin: 4px 0; font-size: 1.05rem; }
.muted { color: var(--muted); }

.burst { position: absolute; left: 50%; top: -34px; width: 68px; height: 68px; margin-left: -34px; }
.burst span {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff6d8, var(--brass) 45%, var(--brass-deep));
  box-shadow: 0 0 30px rgba(255, 207, 107, 0.6);
}
.burst span:nth-child(2), .burst span:nth-child(3) {
  background: none;
  box-shadow: none;
  border: 3px solid var(--brass);
  animation: ring 1.2s ease-out infinite;
}
.burst span:nth-child(3) { animation-delay: 0.4s; }

.primary {
  width: 100%;
  min-height: 54px;
  margin-top: 16px;
  border: 0;
  border-radius: 18px;
  background: linear-gradient(180deg, #ffdf94, var(--brass) 50%, var(--brass-deep));
  color: #2a1a05;
  font-size: 1.1rem;
  font-weight: 800;
  box-shadow: 0 6px 0 #8a5a17, 0 12px 24px rgba(0, 0, 0, 0.35);
}
.primary:active { transform: translateY(3px); box-shadow: 0 3px 0 #8a5a17; }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(40px) scale(0.96); opacity: 0; } }
@keyframes ring { from { transform: scale(1); opacity: 0.9; } to { transform: scale(2.1); opacity: 0; } }

.confetti { position: fixed; inset: 0; z-index: 11; pointer-events: none; overflow: hidden; }
.confetti i {
  position: absolute;
  top: -12px;
  left: var(--x);
  width: 8px;
  height: 14px;
  border-radius: 2px;
  animation: fall var(--d) cubic-bezier(0.3, 0.6, 0.5, 1) var(--delay) forwards;
}
.confetti i.round { width: 10px; height: 10px; border-radius: 50%; }
@keyframes fall { to { transform: translate(var(--dx), 105vh) rotate(var(--r)); } }

.menu {
  width: 100%;
  max-width: 30rem;
  max-height: 85dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 26px 26px 0 0;
  background: #221742;
  color: var(--text);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.5);
}
.menu::backdrop { background: rgba(8, 4, 18, 0.6); }
.menu[open] { animation: sheet 260ms cubic-bezier(0.2, 1, 0.4, 1); }
@keyframes sheet { from { transform: translateY(40%); opacity: 0; } }
.sheet { padding: 16px 18px max(18px, env(safe-area-inset-bottom)); }
.sheet header { display: flex; align-items: center; justify-content: space-between; }
.sheet h2 { margin: 0; font-size: 1.2rem; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(54px, 1fr));
  gap: 8px;
  max-height: 40dvh;
  margin: 12px 0;
  overflow-y: auto;
}
.chip {
  display: grid;
  place-items: center;
  min-height: 54px;
  border: 0;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.08);
}
.chip b { font-size: 1.05rem; }
.chip small { min-height: 1em; color: var(--muted); font-size: 0.7rem; }
.chip.now { background: var(--brass); color: var(--ink); }
.chip.now small { color: var(--ink); }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 52px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.pill, select {
  min-width: 84px;
  min-height: 40px;
  padding: 0 14px;
  border: 0;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  font: inherit;
}
.pill[aria-pressed="true"] { background: var(--brass); color: var(--ink); font-weight: 700; }
.note { margin: 10px 0 0; color: var(--muted); font-size: 0.88rem; }
.note[hidden] { display: none; }
.link {
  margin-top: 14px;
  padding: 10px 0;
  border: 0;
  background: none;
  color: #ff9c8f;
  font-weight: 600;
}

:focus-visible { outline: 3px solid var(--brass); outline-offset: 3px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
