:root {
  color-scheme: dark;
  --background: #17132f;
  --surface: rgba(38, 32, 73, 0.78);
  --surface-strong: #2b2454;
  --text: #f8f7ff;
  --muted: #b9b4d4;
  --accent: #9b8cff;
  --accent-light: #c3baff;
  --glass: rgba(255, 255, 255, 0.18);
  --glass-border: rgba(255, 255, 255, 0.68);
  --shadow: 0 24px 70px rgba(5, 3, 20, 0.38);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  min-height: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  overflow-x: hidden;
  font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 12% 8%, rgba(114, 92, 225, 0.33), transparent 30rem),
    radial-gradient(circle at 88% 86%, rgba(54, 190, 186, 0.18), transparent 28rem),
    linear-gradient(145deg, #17132f 0%, #201943 50%, #121027 100%);
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  content: "";
  opacity: 0.24;
  background-image: radial-gradient(rgba(255, 255, 255, 0.7) 0.7px, transparent 0.7px);
  background-size: 24px 24px;
  mask-image: linear-gradient(to bottom, black, transparent 80%);
}

button {
  font: inherit;
}

button:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 4px;
}

[hidden] {
  display: none !important;
}

.app-shell {
  width: min(100%, 920px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: clamp(20px, 5vw, 52px) clamp(16px, 4vw, 36px) 24px;
}

.start-screen {
  display: grid;
  min-height: calc(100vh - 76px);
  min-height: calc(100dvh - 76px);
  place-items: center;
  animation: fade-up 500ms ease both;
}

.start-card {
  width: min(100%, 520px);
  padding: clamp(30px, 7vw, 56px);
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 36px;
  background: linear-gradient(145deg, rgba(60, 49, 110, 0.72), rgba(27, 23, 57, 0.7));
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.logo {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  height: 92px;
  margin-bottom: 24px;
  gap: 10px;
}

.logo span {
  display: block;
  width: 38px;
  height: 78px;
  border: 3px solid rgba(255, 255, 255, 0.7);
  border-top: 0;
  border-radius: 0 0 19px 19px;
  background: linear-gradient(to top, var(--logo-color) 52%, transparent 52%);
  box-shadow: inset 8px 0 10px rgba(255, 255, 255, 0.1), 0 8px 24px color-mix(in srgb, var(--logo-color) 28%, transparent);
  transform: rotate(-8deg);
}

.logo span:nth-child(2) {
  height: 88px;
  transform: translateY(-5px);
}

.logo span:nth-child(3) {
  transform: rotate(8deg);
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--accent-light);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 10px;
  font-size: clamp(2rem, 6vw, 3.3rem);
  line-height: 1.02;
  letter-spacing: -0.045em;
}

h2 {
  margin-bottom: 24px;
  font-size: clamp(1.8rem, 6vw, 2.45rem);
  letter-spacing: -0.035em;
}

.start-description {
  max-width: 360px;
  margin: 0 auto 30px;
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1.6;
}

.start-actions {
  display: grid;
  gap: 12px;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.mode-button {
  display: flex;
  min-height: 78px;
  align-items: center;
  padding: 13px;
  gap: 11px;
  border: 1px solid rgba(255, 255, 255, 0.11);
  border-radius: 19px;
  color: var(--text);
  text-align: left;
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.11), rgba(255, 255, 255, 0.055));
  cursor: pointer;
  transition: transform 180ms ease, border-color 180ms ease, background 180ms ease;
}

.mode-button:hover:not(:disabled) {
  transform: translateY(-3px);
  border-color: rgba(195, 186, 255, 0.5);
  background: rgba(155, 140, 255, 0.16);
}

.mode-button:disabled {
  cursor: wait;
  opacity: 0.62;
}

.mode-button__icon {
  display: grid;
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  place-items: center;
  border-radius: 13px;
  color: #28204e;
  font-size: 1.45rem;
  font-weight: 900;
  background: linear-gradient(135deg, #c9c0ff, #7ae4ca);
}

.mode-button strong,
.mode-button small {
  display: block;
}

.mode-button strong {
  margin-bottom: 3px;
  font-size: 0.86rem;
}

.mode-button small {
  color: var(--muted);
  font-size: 0.7rem;
}

.button,
.icon-button,
.level-button {
  border: 0;
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.button {
  min-height: 52px;
  padding: 13px 20px;
  border-radius: 18px;
  font-weight: 750;
  transition: transform 180ms ease, background 180ms ease, box-shadow 180ms ease, opacity 180ms ease;
}

.button:hover:not(:disabled) {
  transform: translateY(-2px);
}

.button:active:not(:disabled) {
  transform: translateY(0) scale(0.98);
}

.button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.button--primary {
  color: #19142f;
  background: linear-gradient(135deg, #c6bdff, #8f80f5);
  box-shadow: 0 12px 26px rgba(111, 93, 228, 0.28);
}

.button--secondary {
  border: 1px solid rgba(255, 255, 255, 0.11);
  background: rgba(255, 255, 255, 0.075);
}

.button--ghost {
  min-height: 44px;
  color: var(--muted);
  background: transparent;
}

.game-screen {
  animation: fade-up 420ms ease both;
}

.game-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
}

.header-tools {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sound-button {
  display: grid;
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.11);
  border-radius: 16px;
  color: var(--text);
  font-size: 1.35rem;
  background: rgba(255, 255, 255, 0.075);
  cursor: pointer;
  transition: transform 180ms ease, background 180ms ease, opacity 180ms ease;
  -webkit-tap-highlight-color: transparent;
}

.sound-button:hover {
  transform: translateY(-2px);
  background: rgba(255, 255, 255, 0.12);
}

.sound-button[aria-pressed="false"] {
  color: var(--muted);
  opacity: 0.66;
}

.game-header h1 {
  margin-bottom: 0;
  font-size: clamp(1.8rem, 5vw, 2.6rem);
}

.game-stats {
  display: flex;
  gap: 10px;
}

.stat {
  min-width: 80px;
  padding: 10px 14px;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.07);
}

.stat__label {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.stat strong {
  font-size: 1.35rem;
}

.hint {
  margin: 24px 0 12px;
  color: var(--muted);
  text-align: center;
}

.game-meta {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 22px 0 -12px;
  gap: 10px;
}

.mode-badge,
.timer {
  padding: 6px 11px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 750;
  background: rgba(255, 255, 255, 0.06);
}

.timer {
  min-width: 62px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.tubes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(70px, 92px));
  justify-content: center;
  align-content: center;
  min-height: clamp(350px, 56vh, 520px);
  padding: 34px clamp(12px, 4vw, 32px) 24px;
  gap: clamp(28px, 6vw, 48px) clamp(18px, 5vw, 36px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 32px;
  background: linear-gradient(145deg, rgba(46, 38, 88, 0.56), rgba(24, 20, 52, 0.5));
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.tube {
  position: relative;
  width: 72px;
  height: 212px;
  margin: 0 auto;
  padding: 0;
  border: 0;
  border-radius: 0 0 29px 29px;
  color: inherit;
  background: transparent;
  cursor: pointer;
  filter: drop-shadow(0 12px 12px rgba(4, 3, 18, 0.24));
  transition: transform 220ms ease, filter 220ms ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.tube::before {
  position: absolute;
  inset: 7px 0 0;
  z-index: 3;
  content: "";
  pointer-events: none;
  border: 3px solid var(--glass-border);
  border-top: 0;
  border-radius: 0 0 29px 29px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.13), transparent 25%, transparent 76%, rgba(255, 255, 255, 0.07));
  box-shadow: inset 0 -10px 18px rgba(255, 255, 255, 0.06);
}

.tube::after {
  position: absolute;
  top: 0;
  left: -4px;
  z-index: 4;
  width: calc(100% + 8px);
  height: 12px;
  content: "";
  pointer-events: none;
  border: 3px solid rgba(255, 255, 255, 0.82);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.07);
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.18);
}

.tube:hover:not(:disabled) {
  transform: translateY(-5px);
}

.tube.is-selected {
  z-index: 2;
  transform: translateY(-18px);
  filter: drop-shadow(0 0 14px rgba(178, 166, 255, 0.8)) drop-shadow(0 14px 14px rgba(4, 3, 18, 0.28));
}

.tube.is-selected::before {
  border-color: #d5cfff;
}

.tube.is-invalid {
  animation: shake 320ms ease;
}

.tube.is-hint-source {
  z-index: 2;
  animation: hint-source 1s ease-in-out infinite;
  filter: drop-shadow(0 0 14px rgba(255, 226, 125, 0.85));
}

.tube.is-hint-target {
  z-index: 2;
  animation: hint-target 1s 120ms ease-in-out infinite;
  filter: drop-shadow(0 0 14px rgba(110, 231, 199, 0.88));
}

.tube.is-hint-source::after {
  border-color: #ffe27d;
}

.tube.is-hint-target::after {
  border-color: #6ee7c7;
}

.tube.is-pouring-right {
  z-index: 5;
  animation: pour-right 640ms cubic-bezier(0.3, 0.75, 0.25, 1) both;
}

.tube.is-pouring-left {
  z-index: 5;
  animation: pour-left 640ms cubic-bezier(0.3, 0.75, 0.25, 1) both;
}

.tube.is-receiving {
  animation: receive-glow 640ms ease both;
}

.tube__liquid {
  position: absolute;
  right: 7px;
  bottom: 6px;
  left: 7px;
  display: flex;
  height: 192px;
  flex-direction: column-reverse;
  overflow: hidden;
  border-radius: 0 0 22px 22px;
}

.liquid-layer {
  position: relative;
  display: block;
  width: 100%;
  height: 25%;
  flex: 0 0 25%;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.22), transparent 20%, transparent 75%, rgba(0, 0, 0, 0.06)),
    var(--liquid-color);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14);
}

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

.liquid-layer.is-new {
  animation: layer-arrive 440ms 170ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.flying-liquid {
  position: fixed;
  z-index: 40;
  width: var(--drop-size, 22px);
  height: var(--drop-size, 22px);
  pointer-events: none;
  border-radius: 52% 48% 58% 42%;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 255, 255, 0.75) 0 9%, transparent 11%),
    var(--drop-color);
  box-shadow: 0 0 18px color-mix(in srgb, var(--drop-color) 64%, transparent);
  transform: translate(-50%, -50%);
}

.flying-liquid::before,
.flying-liquid::after {
  position: absolute;
  content: "";
  border-radius: 50%;
  background: var(--drop-color);
  box-shadow: 0 0 10px color-mix(in srgb, var(--drop-color) 55%, transparent);
}

.flying-liquid::before {
  top: -13px;
  left: 2px;
  width: 8px;
  height: 8px;
}

.flying-liquid::after {
  right: -7px;
  bottom: -8px;
  width: 6px;
  height: 6px;
}

.controls {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  margin-top: 18px;
  gap: 12px;
}

.controls .button span {
  margin-right: 5px;
  font-size: 1.25em;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  padding: 20px;
  place-items: center;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(9, 7, 24, 0.72);
  backdrop-filter: blur(8px);
  animation: fade-in 220ms ease both;
}

.modal__card {
  position: relative;
  z-index: 1;
  width: min(100%, 430px);
  padding: clamp(28px, 7vw, 46px);
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 30px;
  background: linear-gradient(145deg, #373068, #211b46);
  box-shadow: var(--shadow);
  animation: modal-arrive 320ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

#levelModal .modal__card {
  width: min(100%, 560px);
  max-height: min(86vh, 720px);
  overflow-y: auto;
}

.icon-button {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  color: var(--muted);
  font-size: 1.75rem;
  line-height: 1;
  background: rgba(255, 255, 255, 0.07);
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
}

.level-button {
  position: relative;
  min-height: 72px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 21px;
  font-size: 1.35rem;
  font-weight: 800;
  background: rgba(255, 255, 255, 0.08);
  transition: transform 180ms ease, background 180ms ease;
}

.level-button:hover:not(:disabled) {
  transform: translateY(-3px);
  background: rgba(155, 140, 255, 0.22);
}

.level-button.is-current {
  border-color: var(--accent-light);
  background: rgba(155, 140, 255, 0.23);
}

.level-button:disabled {
  cursor: not-allowed;
  color: #88829f;
}

.level-button__best {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.66rem;
  font-weight: 650;
}

.celebration {
  display: grid;
  width: 78px;
  height: 78px;
  margin: 0 auto 22px;
  place-items: center;
  border-radius: 50%;
  color: #362d66;
  font-size: 2.4rem;
  background: linear-gradient(135deg, #ffe783, #ffae70);
  box-shadow: 0 0 34px rgba(255, 204, 110, 0.32);
}

.celebration span {
  animation: star-pop 1.5s ease-in-out infinite;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  margin: 6px 0 18px;
  gap: 9px;
}

.result-actions {
  display: grid;
  gap: 10px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.stats-card {
  padding: 16px 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 19px;
  background: rgba(255, 255, 255, 0.065);
}

.stats-card--wide {
  grid-column: span 2;
}

.stats-card span {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 720;
}

.stats-card strong {
  font-size: 1.45rem;
}

.confetti {
  position: fixed;
  inset: 0;
  z-index: 60;
  overflow: hidden;
  pointer-events: none;
}

.confetti-piece {
  position: absolute;
  top: -22px;
  left: var(--confetti-x);
  width: var(--confetti-size);
  height: calc(var(--confetti-size) * 0.55);
  border-radius: 2px;
  background: var(--confetti-color);
  animation: confetti-fall var(--confetti-duration) var(--confetti-delay) cubic-bezier(0.2, 0.65, 0.35, 1) forwards;
}

.result-stat {
  padding: 13px 7px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.06);
}

.result-stat--accent {
  border-color: rgba(195, 186, 255, 0.55);
  background: rgba(155, 140, 255, 0.18);
}

.result-stat span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.66rem;
  font-weight: 750;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.result-stat strong {
  font-size: 1.4rem;
}

.win-result strong {
  color: var(--text);
}

.best-result {
  min-height: 22px;
  margin-bottom: 24px;
  color: #ffe18a;
  font-size: 0.9rem;
}

.modal__card--win .button {
  width: 100%;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@keyframes fade-up {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes modal-arrive {
  from { opacity: 0; transform: translateY(16px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

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

@keyframes layer-arrive {
  from { opacity: 0.3; transform: translateY(-34px) scaleY(0.72); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes hint-source {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-13px); }
}

@keyframes hint-target {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

@keyframes pour-right {
  0% { transform: translateY(0) rotate(0); }
  28% { transform: translateY(-28px) rotate(0); }
  58% { transform: translate(18px, -31px) rotate(18deg); }
  78% { transform: translate(18px, -31px) rotate(18deg); }
  100% { transform: translateY(0) rotate(0); }
}

@keyframes pour-left {
  0% { transform: translateY(0) rotate(0); }
  28% { transform: translateY(-28px) rotate(0); }
  58% { transform: translate(-18px, -31px) rotate(-18deg); }
  78% { transform: translate(-18px, -31px) rotate(-18deg); }
  100% { transform: translateY(0) rotate(0); }
}

@keyframes receive-glow {
  0%, 100% { filter: drop-shadow(0 12px 12px rgba(4, 3, 18, 0.24)); }
  58% { filter: drop-shadow(0 0 18px rgba(110, 231, 199, 0.9)); }
}

@keyframes star-pop {
  0%, 100% { transform: rotate(-8deg) scale(0.92); }
  50% { transform: rotate(8deg) scale(1.12); }
}

@keyframes confetti-fall {
  0% { opacity: 0; transform: translate3d(0, -20px, 0) rotate(0); }
  8% { opacity: 1; }
  100% { opacity: 0; transform: translate3d(var(--confetti-drift), 105vh, 0) rotate(var(--confetti-spin)); }
}

@media (max-width: 560px) {
  .app-shell {
    padding-top: 20px;
  }

  .game-header {
    align-items: center;
    gap: 8px;
  }

  .header-tools {
    gap: 6px;
  }

  .sound-button {
    width: 42px;
    height: 42px;
    flex-basis: 42px;
    border-radius: 14px;
  }

  .game-header .eyebrow {
    display: none;
  }

  .game-header h1 {
    max-width: 88px;
    font-size: 1.28rem;
  }

  .game-stats {
    gap: 6px;
  }

  .stat {
    min-width: 60px;
    padding: 8px 7px;
    border-radius: 15px;
  }

  .hint {
    margin-top: 18px;
    font-size: 0.88rem;
  }

  .game-meta {
    margin-top: 16px;
  }

  .tubes {
    grid-template-columns: repeat(3, minmax(62px, 72px));
    min-height: 438px;
    padding: 28px 8px 22px;
    gap: 30px 22px;
    border-radius: 25px;
  }

  .tube {
    width: 62px;
    height: 178px;
  }

  .tube::before {
    border-radius: 0 0 25px 25px;
  }

  .tube__liquid {
    height: 158px;
    border-radius: 0 0 18px 18px;
  }

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

  .controls {
    grid-template-columns: repeat(2, 1fr);
    gap: 7px;
  }

  .controls .button {
    min-width: 0;
    min-height: 50px;
    padding: 10px 5px;
    border-radius: 15px;
    font-size: 0.78rem;
  }

  .controls .button span {
    margin-right: 4px;
  }

  .level-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  .result-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 350px) {
  .mode-grid {
    grid-template-columns: 1fr;
  }

  .tubes {
    gap: 28px 13px;
  }

  .level-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

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