:root {
  --bg0: #1a103a;
  --bg1: #0a3d4a;
  --card: rgba(255,255,255,0.10);
  --stroke: rgba(160,240,230,0.35);
  --text: #f4fbff;
  --muted: #b6d0e0;
  --accent: #2dd4bf;
  --accent2: #a78bfa;
  --danger: #fb7185;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --tube-w: clamp(56px, 14vw, 92px);
  --tube-h: clamp(200px, 38vh, 340px);
  --cap: 4;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; touch-action: manipulation; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(ellipse 80% 50% at 20% 15%, rgba(167,139,250,0.45), transparent 55%),
    radial-gradient(ellipse 70% 45% at 85% 25%, rgba(45,212,191,0.28), transparent 50%),
    radial-gradient(ellipse 60% 40% at 50% 90%, rgba(56,189,248,0.22), transparent 55%),
    radial-gradient(circle at 30% 60%, rgba(251,191,36,0.08), transparent 40%),
    linear-gradient(165deg, #3b1d6e 0%, #1a103a 38%, #0b3a48 72%, #062830 100%);
  background-attachment: fixed;
}
#app { height: 100%; display: flex; flex-direction: column; }
.screen {
  flex: 1; display: flex; flex-direction: column; min-height: 0;
  padding: calc(14px + var(--safe-top)) 14px calc(10px + var(--safe-bot));
  gap: 10px;
}
.hidden { display: none !important; }

/* Home */
.home-hero { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; }
.logo-tubes { display: flex; gap: 14px; margin-bottom: 10px; }
.mini-tube {
  width: 36px; height: 96px; border-radius: 12px 12px 18px 18px;
  border: 2.5px solid rgba(210,240,255,0.6);
  background: linear-gradient(180deg, transparent 18%, #ff5a8a 18% 42%, #2dd4bf 42% 68%, #a78bfa 68%);
  box-shadow: inset -8px 0 14px rgba(255,255,255,0.22), 0 12px 28px rgba(0,0,0,0.4);
}
.mini-tube.t2 { background: linear-gradient(180deg, transparent 8%, #fbbf24 8% 38%, #38bdf8 38% 68%, #fb7185 68%); transform: translateY(8px); }
.mini-tube.t3 { background: linear-gradient(180deg, transparent 28%, #a78bfa 28% 52%, #34d399 52% 78%, #f97316 78%); }
.logo-title {
  margin: 0; font-size: 2.8rem; font-weight: 800; letter-spacing: 0.02em;
  background: linear-gradient(120deg, #e9d5ff, #5eead4, #93c5fd);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.logo-sub { margin: 0 0 24px; color: var(--muted); font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; font-size: 0.8rem; }

.btn {
  appearance: none; border: 1px solid var(--stroke); background: var(--card);
  color: var(--text); border-radius: 18px; padding: 15px 18px; font-size: 1.05rem; font-weight: 700;
  margin: 6px 0; width: min(340px, 100%); align-self: center; cursor: pointer;
  backdrop-filter: blur(10px);
}
.btn.primary, .overlay-btn.primary {
  background: linear-gradient(135deg, #7c3aed, #0d9488);
  border-color: transparent; box-shadow: 0 12px 32px rgba(124,58,237,0.4);
}
.btn.big { padding: 17px 22px; font-size: 1.15rem; }
.btn.ghost, .overlay-btn.ghost { background: transparent; }
.btn:active, .overlay-btn:active, .tool-btn:active { transform: scale(0.98); }

.credit { text-align: center; color: var(--muted); font-size: 0.8rem; margin-top: 14px; opacity: 0.85; }
.credit.thin { margin: 2px 0 0; font-size: 0.72rem; opacity: 0.7; }

/* Topbar — premium pills */
.topbar, .subbar {
  display: flex; align-items: center; gap: 12px; flex-shrink: 0;
}
.subbar h2 { margin: 0; font-size: 1.25rem; flex: 1; text-align: center; }
.spacer { width: 48px; }
.btn-icon {
  width: 52px; height: 52px; border-radius: 16px; border: 1.5px solid var(--stroke);
  background: linear-gradient(160deg, rgba(255,255,255,0.14), rgba(255,255,255,0.04));
  color: var(--text); font-size: 1.35rem; cursor: pointer;
  box-shadow: 0 6px 18px rgba(0,0,0,0.25);
  flex-shrink: 0;
}
.level-pill {
  flex: 1.2; text-align: center;
  background: linear-gradient(135deg, rgba(124,58,237,0.55), rgba(13,148,136,0.45));
  border: 1.5px solid rgba(196,181,253,0.5);
  border-radius: 999px; padding: 10px 16px;
  box-shadow: 0 8px 24px rgba(124,58,237,0.3), inset 0 1px 0 rgba(255,255,255,0.2);
}
.level-pill .box-label { font-size: 0.68rem; letter-spacing: 0.12em; color: rgba(255,255,255,0.75); font-weight: 700; }
.level-pill .box-value { font-size: 1.55rem; font-weight: 850; line-height: 1.15; }
.moves-pill {
  flex: 0.9; text-align: center;
  background: linear-gradient(160deg, rgba(255,255,255,0.14), rgba(255,255,255,0.05));
  border: 1.5px solid var(--stroke);
  border-radius: 999px; padding: 10px 14px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.22);
}
.moves-pill .box-label { font-size: 0.65rem; letter-spacing: 0.1em; color: var(--muted); font-weight: 700; }
.moves-pill .box-value { font-size: 1.4rem; font-weight: 800; line-height: 1.15; }
.moves-pill .coin { margin-right: 2px; }

.rule-banner {
  flex-shrink: 0; padding: 12px 16px; border-radius: 16px;
  background: linear-gradient(90deg, rgba(167,139,250,0.5), rgba(45,212,191,0.38));
  border: 1.5px solid rgba(196,181,253,0.55);
  font-size: 0.95rem; font-weight: 750; text-align: center; letter-spacing: 0.02em;
  box-shadow: 0 8px 24px rgba(167,139,250,0.25);
  animation: bannerIn 0.45s ease;
}
@keyframes bannerIn { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }

.timer-bar {
  position: relative; height: 28px; border-radius: 999px; background: rgba(0,0,0,0.35);
  flex-shrink: 0; overflow: hidden; border: 1.5px solid var(--stroke);
}
#timer-fill {
  position: absolute; inset: 0 auto 0 0; width: 100%;
  background: linear-gradient(90deg, #2dd4bf, #a78bfa, #f472b6);
  transition: width 0.2s linear;
}
#timer-text {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 0.9rem; font-weight: 800; text-shadow: 0 1px 3px rgba(0,0,0,0.55);
}

/* Board — fills remaining space */
.board-area {
  flex: 1; position: relative; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 4px 0;
}
.tubes {
  display: flex; flex-wrap: wrap; gap: clamp(10px, 2.2vw, 22px) clamp(12px, 2.5vw, 24px);
  justify-content: center; align-items: flex-end; align-content: center;
  width: 92%; max-width: 560px; z-index: 1;
}
.stream-fx {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 2;
}

/* Glossy glass tubes */
.tube {
  width: var(--tube-w); height: var(--tube-h); position: relative;
  border-radius: 18px 18px 28px 28px;
  border: 3px solid rgba(220,245,255,0.62);
  background:
    linear-gradient(105deg, rgba(255,255,255,0.18) 0%, rgba(255,255,255,0.02) 28%, rgba(255,255,255,0.06) 100%),
    linear-gradient(180deg, rgba(255,255,255,0.10), rgba(255,255,255,0.02));
  box-shadow:
    inset -14px 0 22px rgba(255,255,255,0.14),
    inset 10px 0 18px rgba(0,0,0,0.22),
    inset 0 -8px 16px rgba(0,0,0,0.18),
    0 16px 36px rgba(0,0,0,0.42),
    0 0 0 1px rgba(255,255,255,0.08);
  cursor: pointer; overflow: hidden;
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s;
  touch-action: manipulation;
  flex: 0 0 auto;
}
.tube::before {
  content: ""; position: absolute; left: 9px; top: 10px; bottom: 18px; width: 9px;
  border-radius: 10px;
  background: linear-gradient(180deg, rgba(255,255,255,0.55), rgba(255,255,255,0.08) 70%, transparent);
  pointer-events: none; z-index: 5;
}
.tube::after {
  content: ""; position: absolute; right: 8px; top: 14px; bottom: 22px; width: 4px;
  border-radius: 6px;
  background: linear-gradient(180deg, rgba(255,255,255,0.12), transparent 60%);
  pointer-events: none; z-index: 5;
}
.tube.selected {
  transform: translateY(-18px) scale(1.04);
  border-color: #5eead4;
  box-shadow:
    0 0 0 4px rgba(45,212,191,0.4),
    inset -14px 0 22px rgba(255,255,255,0.14),
    0 22px 40px rgba(0,0,0,0.5);
}
.tube.frozen {
  border-color: rgba(186,230,253,0.95);
  box-shadow:
    inset 0 0 30px rgba(186,230,253,0.4),
    0 0 24px rgba(125,211,252,0.35),
    0 16px 36px rgba(0,0,0,0.42);
}
.tube.frozen .frost-overlay {
  position: absolute; inset: 0; z-index: 6; pointer-events: none;
  background:
    radial-gradient(circle at 28% 22%, rgba(255,255,255,0.7), transparent 42%),
    radial-gradient(circle at 72% 55%, rgba(186,230,253,0.5), transparent 48%),
    radial-gradient(circle at 45% 80%, rgba(255,255,255,0.3), transparent 40%),
    linear-gradient(155deg, rgba(255,255,255,0.5), rgba(147,197,253,0.25) 50%, rgba(255,255,255,0.18));
  display: grid; place-items: center;
  backdrop-filter: blur(2.5px);
  box-shadow: inset 0 0 36px rgba(186,230,253,0.4);
}
.tube.frozen .frost-overlay::before {
  content: "❄"; position: absolute; top: 8px; left: 8px; font-size: 1.15rem; opacity: 0.85;
}
.tube.frozen .frost-overlay::after {
  content: "❄"; position: absolute; bottom: 14px; right: 10px; font-size: 0.95rem; opacity: 0.7;
}
.tube.frozen .frost-overlay .frost-num {
  font-size: 1.6rem; font-weight: 900; color: #e0f2fe;
  text-shadow: 0 2px 8px rgba(0,40,80,0.55), 0 0 12px rgba(186,230,253,0.8);
}
.tube.oneway { border-style: dashed; border-color: #fbbf24; }
.tube.oneway .oneway-tag {
  position: absolute; top: 6px; right: 6px; z-index: 7;
  font-size: 0.7rem; background: rgba(251,191,36,0.95); color: #1a103a;
  border-radius: 8px; padding: 2px 6px; font-weight: 850;
}
.tube.pouring { pointer-events: none; }

.layers {
  position: absolute; left: 4px; right: 4px; bottom: 5px; top: 12px;
  display: flex; flex-direction: column-reverse; justify-content: flex-start;
  border-radius: 0 0 22px 22px; overflow: hidden;
}
.layer {
  flex: 1 1 0; min-height: 0; width: 100%;
  position: relative; overflow: hidden;
}
.layer.has-liquid {
  /* gradient set inline; meniscus + shine via children */
}
.layer .meniscus {
  position: absolute; left: 0; right: 0; top: -3px; height: 8px;
  border-radius: 50%; background: inherit; filter: brightness(1.15);
  opacity: 0.9; z-index: 1;
}
.layer .shine {
  position: absolute; left: 0; right: 0; top: 0; height: 38%;
  background: linear-gradient(180deg, rgba(255,255,255,0.38), transparent);
  pointer-events: none; z-index: 2;
}
.layer .spec {
  position: absolute; left: 18%; top: 22%; width: 22%; height: 18%;
  border-radius: 50%; background: rgba(255,255,255,0.22); z-index: 2;
}
.layer.hidden-layer {
  background: repeating-linear-gradient(135deg, #3b4558, #3b4558 7px, #526074 7px, #526074 14px) !important;
}
.layer.hidden-layer::after {
  content: "?"; position: absolute; inset: 0; display: grid; place-items: center;
  color: #e2e8f0; font-weight: 900; font-size: 1.1rem; z-index: 3;
}

/* Toolbar — large rounded icon buttons */
.toolbar {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px;
  flex-shrink: 0; padding: 4px 0 0;
}
.tool-btn {
  border: 1.5px solid var(--stroke);
  background: linear-gradient(160deg, rgba(255,255,255,0.14), rgba(255,255,255,0.04));
  color: var(--text);
  border-radius: 20px; padding: 14px 6px 12px; font-size: 0.78rem; font-weight: 750;
  display: flex; flex-direction: column; align-items: center; gap: 4px; cursor: pointer;
  box-shadow: 0 8px 22px rgba(0,0,0,0.28);
  min-height: 72px;
}
.tool-btn span { font-size: 1.55rem; line-height: 1; }
.tool-btn:disabled { opacity: 0.4; }

/* Level grid */
.level-grid {
  flex: 1; overflow: auto; display: grid;
  grid-template-columns: repeat(auto-fill, minmax(68px, 1fr));
  gap: 12px; padding: 8px 2px 20px; align-content: start;
}
.level-cell {
  aspect-ratio: 1; border-radius: 16px; border: 1.5px solid var(--stroke);
  background: var(--card); color: var(--text); font-weight: 800; font-size: 1rem;
  cursor: pointer; display: grid; place-items: center; position: relative;
}
.level-cell.done { background: linear-gradient(145deg, rgba(45,212,191,0.4), rgba(124,58,237,0.35)); border-color: rgba(45,212,191,0.55); }
.level-cell.locked { opacity: 0.4; cursor: default; }
.level-cell.current { box-shadow: 0 0 0 2px #5eead4; }
.level-cell .badge {
  position: absolute; top: 5px; right: 5px; font-size: 0.58rem;
  background: rgba(167,139,250,0.95); border-radius: 5px; padding: 1px 4px; font-weight: 800;
}

/* Overlays */
.overlay {
  position: fixed; inset: 0; background: rgba(8,6,20,0.75);
  display: grid; place-items: center; padding: 20px; z-index: 50;
  backdrop-filter: blur(8px);
}
.overlay-card {
  width: min(380px, 100%); background: linear-gradient(165deg, #2e1a5c, #0f3d48);
  border: 1.5px solid var(--stroke); border-radius: 26px; padding: 26px 20px 18px;
  box-shadow: 0 28px 70px rgba(0,0,0,0.55); text-align: center;
}
.overlay-card h2 { margin: 0 0 12px; font-size: 1.5rem; }
.overlay-score, .overlay-mode, .about-text { color: var(--muted); margin: 0 0 14px; line-height: 1.45; }
.overlay-btn {
  display: block; width: 100%; margin: 9px 0; padding: 14px; border-radius: 16px;
  border: 1.5px solid var(--stroke); background: var(--card); color: var(--text);
  font-weight: 750; font-size: 1rem; cursor: pointer;
}
.overlay-btn.share { background: rgba(45,212,191,0.2); }
.win-card h2 {
  background: linear-gradient(120deg, #e9d5ff, #5eead4);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  font-size: 2.1rem;
}
.win-burst {
  font-size: 2.4rem; margin-bottom: 6px; filter: drop-shadow(0 0 12px rgba(94,234,212,0.5));
}

/* Dynamic tube sizing — fill ~85% width, scale height to board */
.tubes[data-count="4"], .tubes[data-count="5"] {
  --tube-w: clamp(110px, 20vw, 210px); --tube-h: clamp(300px, 50vh, 560px); width: 90%;
}
.tubes[data-count="6"], .tubes[data-count="7"] {
  --tube-w: clamp(90px, 16.5vw, 175px); --tube-h: clamp(260px, 44vh, 480px); width: 92%;
}
.tubes[data-count="8"], .tubes[data-count="9"] {
  --tube-w: clamp(78px, 15vw, 165px); --tube-h: clamp(230px, 36vh, 420px); width: 94%;
}
.tubes[data-count="10"], .tubes[data-count="11"], .tubes[data-count="12"] {
  --tube-w: clamp(68px, 13.8vw, 152px); --tube-h: clamp(200px, 32vh, 380px); width: 96%;
}

@media (max-width: 360px) {
  .btn-icon { width: 44px; height: 44px; }
  .tool-btn { min-height: 64px; padding: 10px 4px; }
}
