:root {
  --bg-1: #2b6df2;
  --bg-2: #7e3df0;
  --bg-3: #ef4d8c;
  --tube-glass: rgba(255, 255, 255, 0.18);
  --tube-stroke: rgba(255, 255, 255, 0.85);
  --tube-stroke-soft: rgba(255, 255, 255, 0.55);
  --tube-shadow: 0 10px 28px rgba(0, 0, 0, 0.32);
  --text: #ffffff;
  --text-soft: rgba(255, 255, 255, 0.85);
  --accent: #ffd24a;
  --accent-strong: #f0a90c;
  --selected: #fff170;
  --ad: #ff7a1a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  overscroll-behavior-y: none;
  background:
    radial-gradient(circle at 15% 10%, #5e4dff 0%, transparent 55%),
    radial-gradient(circle at 90% 25%, #ff5db1 0%, transparent 55%),
    radial-gradient(circle at 60% 100%, #ff8a3d 0%, transparent 55%),
    linear-gradient(160deg, #2b3aef 0%, #6e2dd7 60%, #ff5d9e 100%);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Fixed-size design scene. Scaled as a whole via CSS transform so that
   contents (tubes, buttons, hint text) keep their relative proportions
   inside any iframe size. The JS resize handler swaps the design size
   between landscape (800×720) and portrait (540×960) depending on the
   actual viewport orientation. */
#app {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 800px;
  height: 820px;
  transform-origin: center center;
  transform: translate(-50%, -50%) scale(1);
  display: flex;
  flex-direction: column;
}

/* ---------- Top bar ---------- */
#topbar {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 14px 18px 4px;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: transform 0.08s ease, background 0.12s ease;
  padding: 0;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn-left { justify-self: start; }
.topbar-spacer { }

.level-pill {
  justify-self: center;
  text-align: center;
  padding: 4px 22px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.25);
  min-width: 120px;
}
.level-label {
  font-size: 11px;
  letter-spacing: 2.5px;
  text-transform: uppercase;
  color: var(--text-soft);
  font-weight: 700;
}
.level-value {
  font-size: 28px;
  font-weight: 800;
  line-height: 1.05;
  color: #fff;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

/* ---------- Stage ---------- */
#stage {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 12px;
  overflow: visible;
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
  touch-action: manipulation;
}

/* Hand pointer */
.hand-pointer {
  position: absolute;
  width: 56px;
  height: 70px;
  object-fit: contain;
  pointer-events: none;
  z-index: 50;
  transition: left 0.35s ease, top 0.35s ease;
  animation: handBob 1.2s ease-in-out infinite;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.5));
}
@keyframes handBob {
  0%, 100% { transform: translate(-50%, 0); }
  50% { transform: translate(-50%, 8px); }
}

/* Bottom hint text under the board */
.hint {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
  background: rgba(0, 0, 0, 0.28);
  padding: 8px 16px;
  border-radius: 14px;
  max-width: calc(100% - 32px);
  line-height: 1.3;
  pointer-events: none;
  z-index: 8;
}
.hint .strong {
  display: inline-block;
  background: #fff170;
  color: #5b3b00;
  padding: 0 6px;
  border-radius: 6px;
  margin: 0 2px;
}

/* ---------- Bottom bar (Undo + Add Tube) ---------- */
#bottombar {
  flex: 0 0 auto;
  display: flex;
  gap: 12px;
  justify-content: center;
  padding: 10px 18px 18px;
}

.action-btn {
  position: relative;
  background: linear-gradient(180deg, #ffffff 0%, #f0f3fb 100%);
  color: #2b3aef;
  border: none;
  border-radius: 18px;
  padding: 12px 18px;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  box-shadow:
    0 4px 0 rgba(0, 0, 0, 0.18),
    0 8px 18px rgba(0, 0, 0, 0.25);
  transition: transform 0.06s ease, box-shadow 0.06s ease, opacity 0.12s ease;
  touch-action: manipulation;
}
.action-btn:active {
  transform: translateY(2px);
  box-shadow:
    0 2px 0 rgba(0, 0, 0, 0.18),
    0 4px 10px rgba(0, 0, 0, 0.25);
}
.action-btn:disabled {
  opacity: 0.5;
  cursor: default;
  transform: none;
  box-shadow:
    0 2px 0 rgba(0, 0, 0, 0.12),
    0 4px 10px rgba(0, 0, 0, 0.15);
}
.action-label { white-space: nowrap; }

.ad-badge {
  background: var(--ad);
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  padding: 3px 6px;
  border-radius: 6px;
  letter-spacing: 0.5px;
  margin-left: 2px;
}
.count-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  background: linear-gradient(180deg, #36c1ff 0%, #2b6df2 100%);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  border-radius: 11px;
  margin-left: 2px;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}

/* ---------- Victory overlay ---------- */
/* Fixed positioning so it covers the full iframe regardless of the
   #app design-size + CSS scale. */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 18, 40, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.overlay-content {
  text-align: center;
  padding: 12px 24px;
}

.overlay-title {
  font-size: 56px;
  font-weight: 900;
  letter-spacing: 2px;
  margin: 0 0 28px;
  color: #fff170;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.25), 0 8px 24px rgba(0, 0, 0, 0.5);
  animation: pop 0.5s cubic-bezier(.34,1.56,.64,1) both;
}
@keyframes pop {
  0%   { transform: scale(0.4); opacity: 0; }
  100% { transform: scale(1);   opacity: 1; }
}

.btn-next {
  background: linear-gradient(180deg, #ffd24a 0%, #f0a90c 100%);
  color: #4a2b00;
  border: none;
  border-radius: 22px;
  padding: 16px 56px;
  font: inherit;
  font-size: 20px;
  font-weight: 800;
  cursor: pointer;
  box-shadow:
    0 6px 0 #b67800,
    0 12px 24px rgba(0, 0, 0, 0.35);
  letter-spacing: 1px;
  text-transform: uppercase;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
  touch-action: manipulation;
}
.btn-next:active {
  transform: translateY(3px);
  box-shadow:
    0 3px 0 #b67800,
    0 6px 14px rgba(0, 0, 0, 0.35);
}

.hidden { display: none !important; }

/* ---------- Responsive ---------- */
@media (max-width: 480px) {
  .level-value { font-size: 24px; }
  .action-btn { font-size: 14px; padding: 10px 14px; }
  .hint { font-size: 14px; padding: 7px 12px; }
}

@media (max-height: 600px) {
  .tube { --slot-h: 36px; --tube-w: 50px; }
  .hand-pointer { width: 48px; height: 60px; }
}

@media (max-height: 480px) {
  .tube { --slot-h: 30px; --tube-w: 42px; }
  #board { gap: 12px; }
  .overlay-title { font-size: 40px; margin-bottom: 16px; }
  .btn-next { padding: 12px 40px; font-size: 16px; }
}
