:root {
  --bg-surface: hsl(20, 38%, 14%);
  --bg-elevated: hsl(20, 36%, 18%);
  --bg-glass: hsla(24, 32%, 24%, 0.65);
  --border-subtle: hsla(28, 35%, 75%, 0.16);
  --border-highlight: hsla(32, 45%, 85%, 0.36);
  --text-primary: hsl(34, 40%, 96%);
  --text-secondary: hsl(30, 24%, 78%);
  --text-muted: hsl(26, 18%, 58%);
  --accent-primary: hsl(32, 90%, 56%);
  --accent-primary-hover: hsl(32, 95%, 62%);
  --accent-warm: hsl(42, 96%, 58%);
  --accent-success: hsl(112, 76%, 48%);
  --accent-danger: hsl(356, 84%, 60%);
  --stage-wood-bg:
    radial-gradient(ellipse at 50% 35%, hsla(26, 52%, 28%, 0.45) 0%, hsla(18, 48%, 11%, 0.75) 100%),
    repeating-linear-gradient(
      90deg,
      hsl(20, 44%, 21%) 0px,
      hsl(21, 46%, 23%) 18px,
      hsl(19, 45%, 19%) 38px,
      hsl(22, 43%, 22%) 64px,
      hsl(18, 46%, 18%) 92px
    );
  --tube-border: hsla(0, 0%, 100%, 0.78);
  --tube-rim-bg: hsla(0, 0%, 100%, 0.14);
  --tube-glass-bg: linear-gradient(
    90deg,
    hsla(0, 0%, 100%, 0.24) 0%,
    hsla(0, 0%, 100%, 0.06) 18%,
    hsla(0, 0%, 0%, 0.08) 50%,
    hsla(0, 0%, 100%, 0.05) 82%,
    hsla(0, 0%, 100%, 0.22) 100%
  );
  --shadow-soft: 0 16px 40px -10px hsla(16, 60%, 4%, 0.7);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 20px;
  --font-sans: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}

[data-theme="light"] {
  --bg-surface: hsl(34, 30%, 94%);
  --bg-elevated: hsl(36, 35%, 98%);
  --bg-glass: hsla(32, 25%, 90%, 0.8);
  --border-subtle: hsla(24, 30%, 25%, 0.12);
  --border-highlight: hsla(24, 30%, 25%, 0.28);
  --text-primary: hsl(22, 35%, 15%);
  --text-secondary: hsl(22, 20%, 38%);
  --text-muted: hsl(22, 14%, 54%);
  --accent-primary: hsl(26, 88%, 48%);
  --accent-primary-hover: hsl(26, 90%, 42%);
  --shadow-soft: 0 12px 32px -8px hsla(22, 35%, 20%, 0.15);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-surface);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 1.25rem;
  transition: background-color 0.25s ease, color 0.25s ease;
  user-select: none;
  -webkit-user-select: none;
}

.app-shell {
  width: 100%;
  max-width: 960px;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

/* Header & Top Controls */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  box-shadow: var(--shadow-soft);
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-title {
  font-size: 1.125rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text-primary);
}

.level-badge {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  font-weight: 800;
  padding: 0.28rem 0.85rem;
  border-radius: 999px;
  background: hsla(18, 48%, 12%, 0.75);
  border: 1.5px solid hsla(28, 45%, 65%, 0.28);
  color: #fff;
  letter-spacing: 0.01em;
}

.difficulty-tabs {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.2rem;
  background: var(--bg-surface);
  padding: 0.2rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
}

.diff-btn {
  appearance: none;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.76rem;
  font-weight: 600;
  padding: 0.32rem 0.62rem;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.diff-btn:hover {
  color: var(--text-primary);
}

.diff-btn.active {
  background: var(--bg-elevated);
  color: var(--accent-primary);
  box-shadow: 0 1px 4px hsla(0, 0%, 0%, 0.25);
}

.metrics-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  font-size: 0.82rem;
}

.metric-item {
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
}

.metric-label {
  color: var(--text-muted);
  font-weight: 500;
}

.metric-value {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--text-primary);
}

/* Action Toolbar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}

.tool-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
}

.btn {
  appearance: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.45rem 0.8rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: transform 0.12s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.btn:hover:not(:disabled) {
  border-color: var(--border-highlight);
  transform: translateY(-1px);
}

.btn:active:not(:disabled) {
  transform: translateY(0);
}

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

.btn-primary {
  background: linear-gradient(180deg, hsl(34, 88%, 58%) 0%, hsl(26, 86%, 44%) 100%);
  border-color: hsla(38, 100%, 75%, 0.45);
  color: #fff;
  text-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.35);
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, hsl(34, 92%, 62%) 0%, hsl(26, 88%, 48%) 100%);
}

.kbd-hint {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  padding: 0.08rem 0.3rem;
  border-radius: 4px;
  background: hsla(0, 0%, 50%, 0.18);
  color: var(--text-secondary);
}

/* Wood Table Stage (matching reference video) */
.stage-card {
  position: relative;
  background: var(--stage-wood-bg);
  border: 2px solid hsla(26, 45%, 36%, 0.55);
  border-radius: var(--radius-lg);
  padding: 2.8rem 1.25rem 2.2rem;
  min-height: 460px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-soft), inset 0 0 60px hsla(16, 65%, 6%, 0.55);
  overflow: hidden;
}

.tubes-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), auto);
  justify-content: center;
  align-items: end;
  gap: 3.2rem clamp(0.85rem, 2.2vw, 2.2rem);
  width: 100%;
  max-width: 860px;
}

/* Individual 3D Glass Tube */
.tube-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.45rem;
  cursor: pointer;
  position: relative;
  outline: none;
  z-index: 2;
  will-change: transform;
}

.tube-wrapper.selected {
  transform: translateY(-22px);
  transition: transform 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}

.tube-wrapper.pouring-active {
  z-index: 40;
  pointer-events: none;
  transition: none !important;
}

/* 3D Glass Cylinder Body */
.tube-vessel {
  position: relative;
  width: 56px;
  height: 184px;
  border-left: 2.2px solid var(--tube-border);
  border-right: 2.2px solid var(--tube-border);
  border-bottom: 2.6px solid var(--tube-border);
  border-radius: 0 0 28px 28px;
  background: var(--tube-glass-bg);
  box-shadow:
    inset 4px 0 8px hsla(0, 0%, 100%, 0.18),
    inset -4px 0 8px hsla(0, 0%, 100%, 0.14),
    0 12px 22px -6px hsla(0, 0%, 0%, 0.55);
}

/* 3D Elliptical Glass Mouth Rim (top of tube) */
.tube-rim {
  position: absolute;
  top: -7px;
  left: -2.2px;
  right: -2.2px;
  height: 13px;
  border: 2.2px solid var(--tube-border);
  border-radius: 50%;
  background: radial-gradient(
    ellipse at 50% 50%,
    hsla(0, 0%, 100%, 0.06) 0%,
    hsla(0, 0%, 100%, 0.22) 100%
  );
  z-index: 12;
  pointer-events: none;
  box-shadow: 0 1px 3px hsla(0, 0%, 0%, 0.25);
}

/* Specular Vertical Glass Wall Highlights */
.tube-vessel::before {
  content: '';
  position: absolute;
  top: 8px;
  bottom: 20px;
  left: 5px;
  width: 3.5px;
  border-radius: 2px;
  background: linear-gradient(
    180deg,
    hsla(0, 0%, 100%, 0.45) 0%,
    hsla(0, 0%, 100%, 0.12) 100%
  );
  pointer-events: none;
  z-index: 11;
}

.tube-vessel::after {
  content: '';
  position: absolute;
  top: 10px;
  bottom: 22px;
  right: 5px;
  width: 2px;
  border-radius: 2px;
  background: linear-gradient(
    180deg,
    hsla(0, 0%, 100%, 0.32) 0%,
    hsla(0, 0%, 100%, 0.06) 100%
  );
  pointer-events: none;
  z-index: 11;
}

/* Inner Clipping Mask for Gravity-Aligned Liquid */
.tube-clip {
  position: absolute;
  inset: 0;
  border-radius: 0 0 26px 26px;
  overflow: hidden;
  z-index: 4;
}

/* Gravity-Aligned Liquid Container (counter-rotates by -tiltAngle so liquid stays horizontal!) */
.liquid-gravity-box {
  position: absolute;
  width: 380px;
  left: calc(50% - 190px);
  bottom: 0;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-start;
  align-items: stretch;
  transform-origin: 50% 0%;
  will-change: transform, bottom;
}

/* Individual Horizontal Liquid Band */
.liquid-layer {
  --layer-h: 39px;
  width: 100%;
  height: var(--layer-h);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Bottom Skirt on Lowest Layer so Steep Tilt (70 deg) Fills the Entire Rounded Base */
.liquid-layer.bottom-most::after {
  content: '';
  position: absolute;
  top: 98%;
  left: 0;
  right: 0;
  height: 150px;
  background: inherit;
  pointer-events: none;
}

/* 3D Elliptical Liquid Surface Meniscus Disc */
.meniscus-disc {
  position: absolute;
  top: -5.5px;
  left: calc(50% - 26px);
  width: 52px;
  height: 11px;
  border-radius: 50%;
  z-index: 6;
  pointer-events: none;
  box-shadow: inset 0 1px 2px hsla(0, 0%, 100%, 0.45);
  transition: width 0.15s ease, left 0.15s ease;
}

.tube-wrapper.pouring-active .meniscus-disc {
  width: 170px;
  left: calc(50% - 85px);
}

/* 3D Bubbling Foam Domes Where Stream Impacts the Rising Surface (matches oar2.jpg & sd1.jpg) */
.impact-foam-group {
  position: absolute;
  top: -13px;
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 16px;
  pointer-events: none;
  z-index: 9;
}

.foam-dome {
  position: absolute;
  bottom: 0;
  border-radius: 50%;
  box-shadow:
    inset 0 2px 3px hsla(0, 0%, 100%, 0.65),
    0 1px 2px hsla(0, 0%, 0%, 0.18);
  animation: domePulse 0.26s ease-in-out infinite alternate;
}

.foam-dome.d1 {
  left: 1px;
  width: 15px;
  height: 13px;
  animation-delay: 0s;
}

.foam-dome.d2 {
  left: 10px;
  width: 17px;
  height: 15px;
  animation-delay: 0.08s;
}

.foam-dome.d3 {
  right: 1px;
  width: 14px;
  height: 12px;
  animation-delay: 0.15s;
}

@keyframes domePulse {
  from { transform: translateY(1px) scale(0.92); }
  to { transform: translateY(-2px) scale(1.08); }
}

/* 3D Tapered Wooden Cork Stopper (matches oar2.jpg completed tubes) */
.tube-cork {
  position: absolute;
  top: -17px;
  left: 50%;
  transform: translateX(-50%);
  width: 58px;
  height: 28px;
  z-index: 15;
  pointer-events: none;
  animation: corkInsert 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* Upper Cork Cap */
.tube-cork::before {
  content: '';
  position: absolute;
  top: 0;
  left: 1px;
  right: 1px;
  height: 13px;
  border-radius: 50% / 45%;
  background:
    radial-gradient(ellipse at 50% 28%, hsl(35, 58%, 68%) 0%, hsl(30, 48%, 52%) 65%, hsl(26, 45%, 38%) 100%);
  border: 1px solid hsla(24, 45%, 25%, 0.65);
  box-shadow: 0 2px 4px hsla(0, 0%, 0%, 0.35);
  z-index: 2;
}

/* Lower Tapered Cork Plug Inside Glass Neck */
.tube-cork::after {
  content: '';
  position: absolute;
  top: 7px;
  left: 5px;
  right: 5px;
  height: 19px;
  background: linear-gradient(
    90deg,
    hsl(27, 44%, 40%) 0%,
    hsl(32, 52%, 56%) 32%,
    hsl(34, 56%, 62%) 55%,
    hsl(26, 44%, 38%) 100%
  );
  clip-path: polygon(0% 0%, 100% 0%, 91% 100%, 9% 100%);
  border-radius: 0 0 8px 8px;
  box-shadow: inset 0 -3px 5px hsla(22, 50%, 20%, 0.45);
  z-index: 1;
}

@keyframes corkInsert {
  from {
    transform: translateX(-50%) translateY(-22px) scale(0.85);
    opacity: 0;
  }
  to {
    transform: translateX(-50%) translateY(0) scale(1);
    opacity: 1;
  }
}

/* Falling Liquid Stream from Lower Pouring Lip */
.pour-stream {
  position: absolute;
  width: 5.5px;
  border-radius: 999px;
  pointer-events: none;
  z-index: 25;
  transform-origin: top center;
  box-shadow: 0 0 8px hsla(0, 0%, 100%, 0.22);
}

/* Tube States */
.tube-wrapper.selected .tube-vessel,
.tube-wrapper.selected .tube-rim {
  border-color: hsl(45, 100%, 72%);
}

.tube-wrapper.hint-src .tube-vessel,
.tube-wrapper.hint-dst .tube-vessel,
.tube-wrapper.hint-src .tube-rim,
.tube-wrapper.hint-dst .tube-rim {
  border-color: var(--accent-warm);
  box-shadow: 0 0 0 3px hsla(42, 96%, 58%, 0.32);
}

.tube-wrapper.shake {
  animation: tubeShake 0.32s ease;
}

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

.liquid-symbol {
  font-size: 0.85rem;
  font-weight: 700;
  color: hsla(0, 0%, 100%, 0.9);
  text-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.5);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease;
  z-index: 7;
}

body.show-symbols .liquid-symbol {
  opacity: 1;
}

.tube-index {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: hsla(34, 35%, 88%, 0.72);
  font-weight: 600;
}

.hint-pill {
  position: absolute;
  top: -30px;
  font-family: var(--font-mono);
  font-size: 0.66rem;
  font-weight: 700;
  padding: 0.12rem 0.42rem;
  border-radius: 4px;
  background: var(--accent-warm);
  color: hsl(20, 40%, 10%);
  white-space: nowrap;
  z-index: 20;
}

/* Status & Solver Bar */
.statusbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.65rem 1rem;
  font-size: 0.82rem;
}

.status-message {
  color: var(--text-secondary);
  font-weight: 500;
}

.solvability-pill {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.22rem 0.6rem;
  border-radius: 6px;
}

.solvability-pill.ok {
  background: hsla(112, 76%, 48%, 0.16);
  color: var(--accent-success);
}

.solvability-pill.warn {
  background: hsla(356, 84%, 60%, 0.18);
  color: var(--accent-danger);
}

/* Victory Banner Overlay */
.victory-banner {
  margin-top: 1.5rem;
  padding: 1rem 1.25rem;
  border-radius: var(--radius-md);
  background: hsla(18, 45%, 12%, 0.88);
  border: 1.5px solid hsla(112, 76%, 48%, 0.55);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  width: 100%;
  max-width: 640px;
  color: #fff;
}

.victory-banner[hidden] {
  display: none;
}

@media (max-width: 640px) {
  .tubes-grid {
    gap: 2.5rem 1.35rem;
  }
}
