:root {
  --bg: #042833;
  --ink: #e8f7f3;
  --muted: #9ec9c3;
  --line: rgba(255, 255, 255, 0.38);
  --glass: rgba(255, 255, 255, 0.08);
  --accent: #8ef0e4;
  --app-h: 100svh;
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

#bgm {
  position: fixed;
  width: 0;
  height: 0;
  opacity: 0;
  pointer-events: none;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", sans-serif;
}

button, input { font: inherit; color: inherit; }

button { cursor: pointer; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.scene {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 50% 110%, rgba(40, 140, 140, 0.35), transparent 55%),
    radial-gradient(ellipse at 50% -10%, rgba(80, 170, 190, 0.18), transparent 45%),
    var(--bg);
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(30px);
  opacity: 0.55;
}

.orb-a { width: 280px; height: 180px; left: -60px; top: 15%; background: #14606a; }
.orb-b { width: 220px; height: 220px; right: -40px; bottom: 10%; background: #0d4e68; }
.orb-c { width: 160px; height: 160px; left: 40%; top: 55%; background: #1a7a6a; opacity: 0.35; }

#app {
  position: relative;
  z-index: 1;
  height: var(--app-h);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.topbar {
  display: grid;
  grid-template-columns: 72px 1fr 72px;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 6px;
}

.brand { text-align: center; min-width: 0; }

.brand-kicker {
  margin: 0;
  font-size: 0.68rem;
  letter-spacing: 0.16em;
  color: var(--muted);
}

.brand h1 {
  margin: 0;
  font-family: Fraunces, "Times New Roman", serif;
  font-weight: 500;
  font-size: clamp(1.2rem, 4vw, 1.7rem);
  letter-spacing: 0.02em;
}

.topbar-side { min-width: 0; }
.icon-btn, .sound-toggle {
  width: 48px;
  height: 44px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--glass);
}

.sound-dock { position: relative; justify-self: end; }

.sound-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 5;
  width: 210px;
  padding: 12px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(6, 36, 44, 0.94);
  display: grid;
  gap: 10px;
}

.sound-panel label {
  display: grid;
  grid-template-columns: 42px 1fr;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

.sound-panel input[type="range"] { width: 100%; accent-color: var(--accent); }

.sound-mute {
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: transparent;
}

#screen-select, #screen-play {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.hint {
  margin: 0;
  padding: 0 16px 8px;
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
}

#select-fit, #play-field {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
}

#stage-grid {
  display: grid;
  justify-content: center;
  align-content: center;
}

#board {
  position: relative;
  display: block;
}

.stage-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 4%;
  padding: 9% 6% 10%;
  border-radius: 18px;
  border: 2px solid rgba(186, 232, 224, 0.42);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.03));
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.22);
  color: var(--ink);
}

.stage-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.stage-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", sans-serif;
  font-size: 0.78em;
  font-weight: 700;
  line-height: 1;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.stage-no {
  font-size: 1.35em;
  font-weight: 700;
}

.stage-mark {
  width: 46%;
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--accent);
}

.stage-mark svg {
  width: 100%;
  height: 78%;
}

.stage-btn.is-current {
  border-color: var(--accent);
  background: linear-gradient(180deg, rgba(142, 240, 228, 0.34), rgba(142, 240, 228, 0.12));
  box-shadow: 0 5px 0 rgba(28, 110, 102, 0.55), 0 0 0 3px rgba(142, 240, 228, 0.28);
}

.stage-btn.is-cleared {
  background: linear-gradient(180deg, rgba(142, 240, 228, 0.16), rgba(142, 240, 228, 0.05));
}

.stage-btn.is-locked {
  color: rgba(200, 228, 224, 0.72);
  border-color: rgba(186, 232, 224, 0.22);
  background: rgba(255, 255, 255, 0.04);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.16);
  cursor: default;
}

.stage-btn.is-locked .stage-mark { color: rgba(200, 228, 224, 0.7); }

.select-links {
  margin: 0;
  padding: 6px 16px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  gap: 18px;
  font-size: 0.85rem;
}

.select-links a { color: var(--muted); }

.vial {
  width: var(--vial-w, 40px);
  height: var(--vial-h, 104px);
  padding: 0;
  border: 0;
  background: transparent;
  position: absolute;
  touch-action: manipulation;
}

.vial-lift, .vial-tilt, .vial-glass {
  display: block;
  height: 100%;
}

.vial-tilt {
  height: 100%;
  transform-origin: 50% 4%;
  transition: transform 0.28s cubic-bezier(0.22, 0.8, 0.2, 1);
}

.vial.is-selected .vial-tilt { transform: translateY(-12px); }

.vial.is-pouring { z-index: 1000 !important; }

.vial.is-pouring .vial-liquid { justify-content: flex-start; }

.vial.is-shake { animation: shake 0.36s ease; }

.vial.is-new .vial-lift { animation: pop 0.35s ease; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

@keyframes pop {
  from { transform: translateY(10px) scale(0.88); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.vial-glass {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  filter: drop-shadow(0 10px 12px rgba(0, 0, 0, 0.28));
}

.vial-mouth {
  width: 34%;
  height: 7%;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.82);
  background: rgba(190, 235, 232, 0.25);
  position: relative;
  z-index: 3;
}

.vial-neck {
  width: 26%;
  height: 13%;
  margin-top: -2%;
  border: 2px solid rgba(255, 255, 255, 0.72);
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0.04) 46%, rgba(255, 255, 255, 0.16));
}

.vial-body {
  width: 74%;
  flex: 1;
  margin-top: -1px;
  position: relative;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.75);
  border-radius: 8px 8px 16px 16px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.03) 28%, rgba(255, 255, 255, 0.05) 70%, rgba(255, 255, 255, 0.16));
  box-shadow: inset 0 -8px 12px rgba(0, 0, 0, 0.12), inset 0 0 0 1px rgba(255, 255, 255, 0.12);
}

.vial.is-done .vial-body {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.3), 0 0 18px var(--glow, rgba(142, 240, 228, 0.45));
}

.vial-liquid {
  position: absolute;
  left: 9%;
  right: 9%;
  bottom: 5%;
  top: 7%;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.layer {
  flex: 0 0 25%;
  width: 100%;
  position: relative;
}

.layer:not(:first-child)::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 3px;
  background: rgba(3, 24, 32, 0.38);
}

.layer:first-child::before {
  content: "";
  position: absolute;
  left: -6%;
  right: -6%;
  top: -4px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.42);
}

.layer[data-c="r"] {
  background:
    radial-gradient(circle, rgba(255, 255, 255, 0.7) 1.2px, transparent 1.5px) 0 0 / 9px 9px,
    linear-gradient(#ff6672, #d10d2b);
}
.layer[data-c="b"] {
  background:
    repeating-linear-gradient(180deg, transparent 0 4px, rgba(255, 255, 255, 0.45) 4px 6px),
    linear-gradient(#79b6ff, #1c4ad4);
}
.layer[data-c="g"] {
  background:
    repeating-linear-gradient(45deg, transparent 0 4px, rgba(255, 255, 255, 0.4) 4px 6px),
    linear-gradient(#47d483, #0d8a44);
}
.layer[data-c="y"] {
  background:
    repeating-linear-gradient(90deg, transparent 0 3px, rgba(90, 50, 0, 0.28) 3px 5px),
    linear-gradient(#ffe56a, #e0a200);
}
.layer[data-c="p"] {
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.45) 25%, transparent 25% 50%, rgba(255, 255, 255, 0.45) 50% 75%, transparent 75%) 0 0 / 9px 9px,
    linear-gradient(#b592ff, #6424d6);
}
.layer[data-c="o"] {
  background:
    radial-gradient(circle, transparent 2.5px, rgba(255, 255, 255, 0.55) 3px 4px, transparent 4.4px) 0 0 / 11px 11px,
    linear-gradient(#ffb15e, #e25800);
}

.seal {
  position: absolute;
  left: 50%;
  top: 46%;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  border-radius: 50%;
  background: #3dcb74;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
  opacity: 0;
  transform: scale(0.7);
  z-index: 2;
}

.vial.is-done .seal { opacity: 1; transform: scale(1); }

.seal::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 4px;
  width: 6px;
  height: 3px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(-45deg);
}

.vial-shine {
  position: absolute;
  left: 12%;
  top: 8%;
  width: 16%;
  height: 62%;
  border-radius: 999px;
  background: linear-gradient(rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0));
  pointer-events: none;
}

.toolbar {
  display: flex;
  gap: 10px;
  padding: 8px 14px calc(10px + env(safe-area-inset-bottom));
}

.toolbar button {
  flex: 1;
  height: 48px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--glass);
}

#btn-reset { flex: 0.7; }
#btn-bonus {
  flex: 1.6;
  background: rgba(142, 240, 228, 0.14);
  font-size: 0.78rem;
  line-height: 1.25;
  padding: 0 12px;
}

.pips {
  display: inline-flex;
  gap: 4px;
  margin-left: 8px;
  vertical-align: middle;
}

.pips i {
  width: 8px;
  height: 13px;
  border-radius: 2px 2px 5px 5px;
  border: 1px solid currentColor;
  opacity: 0.45;
}

.pips i.on { background: currentColor; opacity: 1; }

#stream {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 6;
  overflow: visible;
}

#stream-shine { stroke: rgba(255, 255, 255, 0.55); }

.modal, .clear {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal { background: rgba(2, 16, 22, 0.62); }

.clear {
  background: rgba(4, 22, 30, 0.42);
  backdrop-filter: blur(4px);
}

.modal-card, .clear-card {
  width: min(420px, 100%);
  text-align: center;
  border-radius: 22px;
  border: 1px solid var(--line);
  background: rgba(8, 48, 58, 0.94);
  padding: 22px 18px 18px;
}

.modal-card h2, .clear-card h2 {
  margin: 0 0 8px;
  font-family: Fraunces, serif;
  font-weight: 500;
}

.clear-card h2 {
  font-size: clamp(3rem, 12vw, 4.6rem);
  letter-spacing: 0.04em;
  margin: 4px 0 8px;
}

.clear-kicker, .modal-card p, .clear-card p {
  margin: 0 0 16px;
  color: var(--muted);
}

.modal-actions, .clear-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-actions button, .clear-actions button {
  height: 46px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(142, 240, 228, 0.16);
}

.ghost { background: transparent !important; }

#particles { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }

.particle {
  position: absolute;
  left: 50%;
  top: 46%;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  animation: fly 900ms ease-out forwards;
}

@keyframes fly {
  to { transform: translate(var(--dx), var(--dy)) scale(0.4); opacity: 0; }
}

.is-celebrate .vial-lift { animation: bob 0.7s ease; }

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  45% { transform: translateY(-8px); }
}

@media (prefers-reduced-motion: reduce) {
  .vial-tilt, .vial-lift, .particle, .is-celebrate .vial-lift, .vial.is-new .vial-lift {
    animation: none;
    transition: none;
  }
}
