:root {
  --bg: #f3f7ff;
  --bg-elev: #ffffff;
  --bg-deep: #e6eef9;
  --fg: #0e1b2c;
  --fg-muted: #5b6b80;
  --accent: #14b8a6;
  --accent-strong: #0d9488;
  --accent-fg: #ffffff;
  --danger: #ef4444;
  --tube-glass: rgba(255, 255, 255, 0.55);
  --tube-stroke: rgba(14, 27, 44, 0.22);
  --tube-shine: rgba(255, 255, 255, 0.7);
  --tube-shadow: rgba(14, 27, 44, 0.18);
  --selected-glow: rgba(20, 184, 166, 0.45);
  --shadow-sm: 0 2px 8px -2px rgba(14, 27, 44, 0.12);
  --shadow: 0 12px 32px -12px rgba(14, 27, 44, 0.22);
  --radius: 14px;
  --radius-lg: 22px;
  --tube-capacity: 4;

  --c-0: #ef4444;
  --c-1: #3b82f6;
  --c-2: #fbbf24;
  --c-3: #10b981;
  --c-4: #f97316;
  --c-5: #8b5cf6;
  --c-6: #06b6d4;
  --c-7: #ec4899;
  --c-8: #84cc16;
  --c-9: #92400e;
  --c-10: #94a3b8;
  --c-11: #d946ef;
}

html[data-theme="dark"] {
  --bg: #0b1424;
  --bg-elev: #16263a;
  --bg-deep: #07101c;
  --fg: #e8f0ff;
  --fg-muted: #8fa1b8;
  --tube-glass: rgba(255, 255, 255, 0.06);
  --tube-stroke: rgba(255, 255, 255, 0.22);
  --tube-shine: rgba(255, 255, 255, 0.35);
  --tube-shadow: rgba(0, 0, 0, 0.55);
  --selected-glow: rgba(45, 212, 191, 0.55);
  --shadow-sm: 0 2px 8px -2px rgba(0, 0, 0, 0.5);
  --shadow: 0 14px 40px -12px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  background:
    radial-gradient(circle at 20% 0%, var(--bg-elev), transparent 50%),
    radial-gradient(circle at 80% 100%, var(--bg-deep), transparent 60%),
    var(--bg);
}

button, select, input {
  font: inherit;
  color: inherit;
}

button {
  border: none;
  background: transparent;
  cursor: pointer;
  padding: 0;
}

h1, h2, h3 { margin: 0; }
p { margin: 0; }

.muted { color: var(--fg-muted); }

/* Screens */
.screen {
  display: none;
  flex: 1;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  animation: fade-in 240ms ease;
}
.screen[data-active] { display: flex; }

@keyframes fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px 10px;
}
.topbar h2 { font-size: 18px; font-weight: 600; flex: 1; text-align: center; }
.topbar .topbar-spacer { width: 40px; }

.btn-icon {
  width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 12px;
  background: var(--bg-elev);
  color: var(--fg);
  font-size: 22px;
  line-height: 1;
  box-shadow: var(--shadow-sm);
  transition: transform 120ms ease, background-color 120ms ease;
}
.btn-icon:active { transform: scale(0.94); }

.level-label {
  flex: 1;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
}
.level-label strong { font-size: 22px; font-weight: 700; }
.level-label .muted { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; }

/* Menu screen */
#screen-menu {
  align-items: center;
  justify-content: space-between;
  padding: max(48px, env(safe-area-inset-top) + 24px) 24px max(24px, env(safe-area-inset-bottom) + 12px);
}
.brand { text-align: center; margin-top: 32px; }
.brand-mark { color: var(--fg); display: inline-flex; }
.brand h1 {
  font-size: 40px;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-top: 8px;
}
.brand .tagline {
  color: var(--fg-muted);
  margin-top: 4px;
}

.menu {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 24px;
}

.btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 14px 20px;
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--fg);
  font-weight: 600;
  font-size: 16px;
  box-shadow: var(--shadow-sm);
  transition: transform 100ms ease, background-color 120ms ease, box-shadow 120ms ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  box-shadow: 0 8px 24px -8px var(--accent);
}
.btn-primary:active { background: var(--accent-strong); }
.btn-ghost { background: transparent; box-shadow: none; color: var(--fg-muted); }
.btn-danger { background: var(--danger); color: white; box-shadow: none; padding: 8px 14px; font-size: 14px; }
.btn-label { font-size: 16px; }
.btn-sub { font-size: 12px; font-weight: 500; opacity: 0.85; }

.menu-foot { padding-bottom: 8px; }

/* Game screen */
#screen-game .board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 8px 16px;
  min-height: 0;
}

.board {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  align-items: flex-end;
  gap: 18px 16px;
  max-width: 480px;
  width: 100%;
  padding: 16px;
  position: relative;
}

/* Liquid stream rendered during a pour. Positioned absolutely against .board,
   anchored at the source mouth, rotated toward the destination mouth. */
.pour-stream {
  position: absolute;
  height: 6px;
  border-radius: 3px;
  pointer-events: none;
  transform-origin: left center;
  filter: drop-shadow(0 0 4px rgba(255, 255, 255, 0.15));
  will-change: transform, opacity;
  z-index: 4;
}

/* Tube */
.tube {
  position: relative;
  width: 52px;
  height: 200px;
  flex: 0 0 auto;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 220ms cubic-bezier(0.3, 1.5, 0.5, 1), filter 220ms ease;
  filter: drop-shadow(0 4px 6px var(--tube-shadow));
}
.tube .tube-bg {
  position: absolute;
  inset: 0;
  border: 2px solid var(--tube-stroke);
  border-top: none;
  border-radius: 4px 4px 26px 26px;
  background: var(--tube-glass);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
.tube .layers {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column-reverse;
  overflow: hidden;
  border-radius: 0 0 24px 24px;
  padding-top: 2px;
}
.tube .layer {
  width: 100%;
  height: calc(100% / var(--tube-capacity));
  flex: 0 0 auto;
  position: relative;
  transition:
    height 220ms cubic-bezier(0.4, 0, 0.2, 1),
    opacity 180ms ease,
    transform 200ms ease,
    background-color 320ms ease;
}
.tube .layer[data-color="0"] { background: var(--c-0); }
.tube .layer[data-color="1"] { background: var(--c-1); }
.tube .layer[data-color="2"] { background: var(--c-2); }
.tube .layer[data-color="3"] { background: var(--c-3); }
.tube .layer[data-color="4"] { background: var(--c-4); }
.tube .layer[data-color="5"] { background: var(--c-5); }
.tube .layer[data-color="6"] { background: var(--c-6); }
.tube .layer[data-color="7"] { background: var(--c-7); }
.tube .layer[data-color="8"] { background: var(--c-8); }
.tube .layer[data-color="9"] { background: var(--c-9); }
.tube .layer[data-color="10"] { background: var(--c-10); }
.tube .layer[data-color="11"] { background: var(--c-11); }
/* Mystery (hidden) layers — keep these after data-color rules so they win the
   same-specificity tie. */
.tube .layer.hidden { background: #15171c; }
[data-theme="dark"] .tube .layer.hidden { background: #07090e; }
.tube .layer .mystery-mark {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 16px;
  color: rgba(255, 255, 255, 0.75);
  opacity: 0;
  pointer-events: none;
  transition: opacity 280ms ease;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.tube .layer.hidden .mystery-mark { opacity: 1; }
.tube .glass-shine {
  position: absolute;
  top: 6px;
  left: 8px;
  width: 5px;
  bottom: 28px;
  background: linear-gradient(to bottom, var(--tube-shine), rgba(255, 255, 255, 0.04));
  border-radius: 3px;
  pointer-events: none;
}
.tube .tube-rim {
  position: absolute;
  top: -2px;
  left: -4px;
  right: -4px;
  height: 6px;
  border-radius: 4px;
  background: var(--tube-stroke);
  opacity: 0.6;
}

.tube.selected {
  transform: translateY(-22px);
  filter: drop-shadow(0 10px 18px var(--selected-glow));
}
.tube.solved {
  cursor: default;
  filter: drop-shadow(0 4px 10px var(--selected-glow));
}
.tube.solved .tube-bg {
  border-color: var(--accent);
  border-width: 3px;
  border-radius: 4px 4px 26px 26px;
  box-shadow:
    0 0 0 2px var(--selected-glow) inset,
    0 0 0 1px var(--accent);
}
.tube.solved .tube-rim {
  background: var(--accent);
  opacity: 0.9;
}
.tube.solved .glass-shine {
  opacity: 0.4;
}
.tube.solved::after {
  content: '✓';
  position: absolute;
  top: -10px;
  right: -10px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 13px;
  line-height: 20px;
  text-align: center;
  font-weight: 700;
  box-shadow: var(--shadow-sm);
  pointer-events: none;
}
.tube.invalid { animation: shake 260ms ease; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

/* Toolbar */
.toolbar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  gap: 8px;
  padding: 10px 14px max(14px, env(safe-area-inset-bottom) + 6px);
  background: var(--bg-elev);
  border-top: 1px solid var(--tube-stroke);
}
.btn-tool {
  flex: 1;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 6px;
  border-radius: 12px;
  color: var(--fg);
  transition: background-color 120ms ease, transform 100ms ease, opacity 120ms ease;
  min-height: 52px;
}
.btn-tool:active { transform: scale(0.96); background: var(--bg-deep); }
.btn-tool[disabled] { opacity: 0.35; pointer-events: none; }
.tool-icon { font-size: 22px; line-height: 1; }
.tool-label { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--fg-muted); }

/* Level select */
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 12px;
  padding: 16px 20px 32px;
  max-width: 520px;
  width: 100%;
  margin: 0 auto;
}
.level-tile {
  aspect-ratio: 1;
  border-radius: 14px;
  background: var(--bg-elev);
  color: var(--fg);
  font-weight: 700;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  position: relative;
  transition: transform 100ms ease;
}
.level-tile:active { transform: scale(0.95); }
.level-tile.current { outline: 2px solid var(--accent); }
.level-tile.done::after {
  content: '✓';
  position: absolute;
  top: 4px; right: 6px;
  font-size: 11px;
  color: var(--accent);
}
.level-tile.locked { opacity: 0.4; pointer-events: none; }

/* Settings */
.settings {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 20px 24px;
  max-width: 480px;
  width: 100%;
  margin: 0 auto;
}
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--bg-elev);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.row select, .row input[type="checkbox"] { accent-color: var(--accent); }
.row select {
  background: transparent;
  border: 1px solid var(--tube-stroke);
  border-radius: 8px;
  padding: 6px 8px;
}
.meta { color: var(--fg-muted); font-size: 12px; padding: 12px 4px 0; text-align: center; }

/* Win overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: fade-in 200ms ease;
}
.win-card {
  background: var(--bg-elev);
  color: var(--fg);
  padding: 32px 24px 24px;
  border-radius: var(--radius-lg);
  max-width: 320px;
  width: calc(100% - 48px);
  text-align: center;
  box-shadow: var(--shadow);
  position: relative;
}
.win-card h2 { font-size: 28px; font-weight: 800; margin-bottom: 6px; }
.win-card p { color: var(--fg-muted); margin-bottom: 20px; }
.win-actions { display: flex; gap: 10px; }
.win-actions .btn { flex: 1; padding: 12px; }

.confetti {
  position: absolute;
  inset: -20px;
  pointer-events: none;
  background-image:
    radial-gradient(circle, var(--c-0) 2px, transparent 3px),
    radial-gradient(circle, var(--c-1) 2px, transparent 3px),
    radial-gradient(circle, var(--c-2) 2px, transparent 3px),
    radial-gradient(circle, var(--c-3) 2px, transparent 3px);
  background-size: 40px 40px, 50px 50px, 60px 60px, 70px 70px;
  background-position: 0 0, 20px 20px, 30px 10px, 10px 30px;
  opacity: 0.25;
  animation: confetti-fall 1.4s ease-out;
}
@keyframes confetti-fall {
  from { transform: translateY(-20px); opacity: 0.6; }
  to { transform: translateY(20px); opacity: 0.25; }
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 40;
  animation: toast-in 200ms ease, toast-out 200ms ease 1.6s forwards;
  pointer-events: none;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}
@keyframes toast-out {
  to { opacity: 0; transform: translateX(-50%) translateY(8px); }
}

/* Responsive */
@media (min-width: 480px) {
  .tube { width: 58px; height: 220px; }
}
@media (max-width: 360px) {
  .tube { width: 44px; height: 172px; }
  .board { gap: 14px 12px; }
}

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