:root {
  --bg: #12122a;
  --bg-2: #1c1c3a;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #eef0ff;
  --text-dim: #a7abce;
  --accent: #6c5ce7;
  --accent-2: #00d2ff;
  --good: #2ecc71;
  --bad: #e74c3c;
  --glass: rgba(255, 255, 255, 0.1);
  --tube-width: clamp(46px, 9vw, 66px);
  --tube-height: clamp(110px, 22vw, 148px);
  --radius: 16px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(108, 92, 231, 0.35), transparent 60%),
    radial-gradient(900px 500px at 90% 10%, rgba(0, 210, 255, 0.18), transparent 55%),
    linear-gradient(160deg, var(--bg), var(--bg-2));
  background-attachment: fixed;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 980px;
  margin: 0 auto;
  padding: 18px 16px 40px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ---------- header ---------- */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 6px 0 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.title {
  font-size: 1.15rem;
  margin: 0;
  letter-spacing: 0.02em;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 800;
}

.level-label {
  color: var(--text-dim);
  font-size: 0.82rem;
  margin-top: 2px;
}

.moves-badge {
  background: var(--glass);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
}

.toolbar {
  display: flex;
  gap: 8px;
}

/* ---------- buttons ---------- */

.btn,
.icon-btn {
  font-family: inherit;
  color: var(--text);
  background: var(--glass);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, opacity 0.15s ease;
}

.btn {
  padding: 9px 16px;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.04em;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none;
  color: #fff;
}

.btn-ghost {
  background: transparent;
  border-color: var(--panel-border);
  color: var(--text-dim);
}

.btn-brand {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none;
  color: #fff;
  padding: 8px 12px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
}

.btn:hover:not(:disabled),
.icon-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.18);
}

.btn:active:not(:disabled),
.icon-btn:active:not(:disabled) {
  transform: scale(0.94);
}

.btn:focus-visible,
.icon-btn:focus-visible,
.tube:focus-visible,
.level-cell:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: 2px;
}

.btn:disabled,
.icon-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.icon-btn.is-muted {
  opacity: 0.5;
}

.icon-btn.spinner {
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- board ---------- */

.board {
  position: relative;
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: clamp(12px, 2.5vw, 22px);
  padding: 18px 4px 40px;
  min-height: 320px;
}

.tube {
  position: relative;
  width: var(--tube-width);
  height: var(--tube-height);
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
  border-radius: 0 0 14px 14px;
}

.tube-glass {
  position: absolute;
  inset: 0;
  border: 2px solid rgba(255, 255, 255, 0.16);
  border-radius: 4px 4px 14px 14px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.02)),
    rgba(255, 255, 255, 0.04);
  box-shadow:
    inset 0 0 18px rgba(0, 0, 0, 0.35),
    0 6px 16px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

.tube::after {
  content: '';
  position: absolute;
  top: -14px;
  left: 50%;
  transform: translateX(-50%);
  width: 56%;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.16);
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  background: rgba(255, 255, 255, 0.03);
}

.liquids {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100%;
  display: flex;
  flex-direction: column-reverse;
}

.liquid {
  display: block;
  width: 100%;
  height: calc(100% / var(--cap));
  background: var(--c);
  background: linear-gradient(180deg, color-mix(in srgb, #fff 22%, var(--c)), var(--c) 55%);
  transition: opacity 0.18s ease;
}

.liquid:first-child {
  border-radius: 0 0 12px 12px;
}

/* selection & targets */

.tube.is-selected .tube-glass {
  border-color: var(--accent-2);
  box-shadow:
    inset 0 0 18px rgba(0, 0, 0, 0.3),
    0 0 0 3px rgba(0, 210, 255, 0.45),
    0 6px 18px rgba(0, 210, 255, 0.25);
}

.tube.is-target .tube-glass {
  border-color: rgba(46, 204, 113, 0.85);
  box-shadow:
    inset 0 0 18px rgba(0, 0, 0, 0.3),
    0 0 0 2px rgba(46, 204, 113, 0.4);
}

.tube.is-complete .tube-glass {
  border-color: rgba(46, 204, 113, 0.7);
  box-shadow:
    inset 0 0 18px rgba(0, 0, 0, 0.3),
    0 0 14px rgba(46, 204, 113, 0.35);
}

.tube.is-pouring .liquid:nth-last-child(-n + var(--pour-count, 4)) {
  opacity: 0;
}

.tube.shake .tube-glass {
  animation: shake 0.3s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-4px); }
}

.tube.hint-source .tube-glass {
  animation: pulse-glow 0.5s ease infinite alternate;
  border-color: var(--accent);
}

.tube.hint-target .tube-glass {
  animation: pulse-glow 0.5s ease 0.25s infinite alternate;
  border-color: var(--good);
}

@keyframes pulse-glow {
  from { box-shadow: 0 0 6px rgba(0, 0, 0, 0.25); }
  to { box-shadow: 0 0 18px 3px rgba(0, 210, 255, 0.55); }
}

/* ---------- pour animation layer ---------- */

.pour-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 5;
}

.fly-liquid {
  position: absolute;
  border-radius: 4px;
  background: var(--c);
  background: linear-gradient(180deg, color-mix(in srgb, #fff 22%, var(--c)), var(--c) 55%);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translate(-50%, 16px);
  background: rgba(20, 20, 42, 0.95);
  border: 1px solid var(--panel-border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 60;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast.error {
  border-color: rgba(231, 76, 60, 0.6);
}

/* ---------- modals ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(8, 8, 22, 0.72);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 18px;
}

.modal[hidden] {
  display: none;
}

.modal-panel {
  background: linear-gradient(180deg, #1d1d3c, #171730);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
  width: min(720px, 96vw);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  animation: pop 0.22s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes pop {
  from {
    transform: scale(0.92);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 8px;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

/* level grid */

.level-panel {
  width: min(760px, 96vw);
}

.level-grid {
  overflow-y: auto;
  padding: 14px 20px 6px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 10px;
  min-height: 0;
}

.level-cell {
  position: relative;
  aspect-ratio: 1;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  transition: transform 0.08s ease, background 0.15s ease;
}

.level-cell:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.14);
  transform: translateY(-2px);
}

.level-cell:active:not(:disabled) {
  transform: scale(0.94);
}

.level-cell.is-current {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 2px rgba(0, 210, 255, 0.4);
}

.level-cell.is-locked {
  opacity: 0.45;
  cursor: default;
}

.level-cell .level-stars {
  font-size: 0.55rem;
  letter-spacing: -0.06em;
  color: #f1c40f;
}

.level-cell.has-stars:not(.is-locked) .level-stars {
  color: #f1c40f;
}

.level-cell .level-stars:empty {
  display: none;
}

.level-lock {
  position: absolute;
  font-size: 0.8rem;
  opacity: 0.8;
}

.modal-footer {
  padding: 10px 20px 18px;
  display: flex;
  justify-content: center;
}

/* win panel */

.win-panel {
  width: min(420px, 92vw);
  text-align: center;
  padding: 28px 24px 26px;
}

.win-stars {
  font-size: 3.2rem;
  line-height: 1;
  margin-bottom: 10px;
}

.star {
  display: inline-block;
  color: rgba(255, 255, 255, 0.18);
  animation: star-pop 0.4s cubic-bezier(0.2, 0.9, 0.3, 1.4) backwards;
}

.star.lit {
  color: #f1c40f;
  text-shadow: 0 0 18px rgba(241, 196, 15, 0.7);
}

@keyframes star-pop {
  from {
    transform: scale(0) rotate(-30deg);
  }
  to {
    transform: scale(1) rotate(0deg);
  }
}

.win-title {
  margin: 6px 0 4px;
  font-size: 1.5rem;
}

.win-detail {
  color: var(--text-dim);
  margin: 0 0 20px;
  font-size: 0.95rem;
}

.win-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.win-actions .btn {
  min-width: 120px;
}

/* help panel */

.help-panel {
  width: min(560px, 94vw);
}

.help-body {
  padding: 10px 24px 24px;
  color: var(--text);
  line-height: 1.6;
  font-size: 0.98rem;
}

.help-body ul {
  padding-left: 20px;
  margin: 10px 0;
}

.help-body li {
  margin: 6px 0;
}

.help-tip {
  color: var(--text-dim);
  font-size: 0.9rem;
  border-left: 3px solid var(--accent);
  padding-left: 12px;
}

/* confetti */

.confetti-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 70;
}

.confetti-overlay canvas {
  width: 100%;
  height: 100%;
}

/* ---------- responsive ---------- */

@media (max-width: 560px) {
  #app {
    padding: 12px 10px 28px;
  }

  .app-header {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .toolbar {
    justify-content: center;
  }

  .brand {
    justify-content: space-between;
  }

  .moves-badge {
    order: 3;
    align-self: center;
  }
}
