/* ============ Tube Sort ============ */
:root {
  --bg: #141414;
  --board: #1b1b1b;
  --grid: rgba(255, 255, 255, 0.055);
  --cell: 64px;
  --glass: rgba(205, 205, 215, 0.55);
  --blue-1: #3f95ff; --blue-2: #1f63e6; --blue-edge: #133f99;
  --pur-1: #e866ff;  --pur-2: #aa2fd6;  --pur-edge: #6d1691;
  --btn: clamp(46px, 13vw, 60px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: #fff; overflow: hidden; }
body { touch-action: manipulation; user-select: none; -webkit-user-select: none; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; }

#app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- top bar ---------- */
.topbar {
  flex: none;
  display: grid;
  grid-template-columns: var(--btn) var(--btn) 1fr var(--btn) var(--btn);
  gap: clamp(10px, 4vw, 26px);
  align-items: center;
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
  padding: clamp(12px, 3vh, 22px) clamp(16px, 7vw, 34px);
}

.btn {
  position: relative;
  width: var(--btn);
  height: var(--btn);
  border: 0;
  border-radius: 14px;
  display: grid;
  place-items: center;
  color: #fff;
  transition: transform 0.12s ease, filter 0.2s ease, opacity 0.2s ease;
}
.btn svg { width: 58%; height: 58%; fill: currentColor; filter: drop-shadow(0 1px 0 rgba(0, 0, 0, 0.25)); }
.btn svg[fill="none"] { fill: none; }
.btn::before { /* glossy top highlight */
  content: ""; position: absolute; left: 7%; right: 7%; top: 5%; height: 42%;
  border-radius: 10px 10px 40% 40% / 10px 10px 18px 18px;
  background: linear-gradient(rgba(255, 255, 255, 0.38), rgba(255, 255, 255, 0.04));
  pointer-events: none;
}
.btn-blue {
  background: linear-gradient(180deg, var(--blue-1), var(--blue-2));
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.18), 0 4px 0 var(--blue-edge), 0 6px 10px rgba(0, 0, 0, 0.45);
}
.btn-purple {
  background: linear-gradient(180deg, var(--pur-1), var(--pur-2));
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.18), 0 4px 0 var(--pur-edge), 0 6px 10px rgba(0, 0, 0, 0.45);
}
.btn:active { transform: translateY(3px); }
.btn-blue:active { box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.18), 0 1px 0 var(--blue-edge); }
.btn-purple:active { box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.18), 0 1px 0 var(--pur-edge); }
.btn.off { filter: saturate(0.35) brightness(0.75); }
.btn:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }

#btn-undo svg { width: 46%; margin-right: 18%; }
.badge {
  position: absolute; right: 12%; top: 50%; transform: translateY(-50%);
  font-weight: 800; font-size: calc(var(--btn) * 0.36); line-height: 1;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
}

.level-pill {
  height: var(--btn);
  border-radius: 16px;
  background: #2a2a2a;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 2px 6px rgba(0, 0, 0, 0.4);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1.05;
  min-width: 0;
}
.level-label { font-size: calc(var(--btn) * 0.22); letter-spacing: 0.08em; color: #a9a9a9; font-weight: 600; }
.level-num { font-size: calc(var(--btn) * 0.42); font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- board ---------- */
#board {
  position: relative;
  flex: 1;
  min-height: 0;
  margin-bottom: clamp(8px, 4vh, 48px);
  background-color: var(--board);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  background-position: center top;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  overflow: hidden;
  --D: 30px;
}

.tube, .tube-front, .hit { position: absolute; }

.tube { /* glass body behind the balls */
  z-index: 1;
  border-radius: 9px 9px 999px 999px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.035), rgba(255, 255, 255, 0.015) 50%, rgba(255, 255, 255, 0.04));
  transition: box-shadow 0.25s ease, background 0.25s ease;
}
.tube-front { /* rim + reflections in front of the balls */
  z-index: 3;
  pointer-events: none;
  border: 2px solid var(--glass);
  border-radius: 9px 9px 999px 999px;
  transition: border-color 0.2s ease, box-shadow 0.25s ease, transform 0.25s cubic-bezier(.3, 1.6, .5, 1);
}
.tube-front::before { /* vertical glass shine */
  content: ""; position: absolute; left: 12%; top: 6%; bottom: 14%; width: 9%;
  border-radius: 99px;
  background: linear-gradient(rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0.03));
}
.tube-front.selected {
  border-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.25), 0 0 18px rgba(120, 180, 255, 0.35);
}

/* completed tube: glow + a stopper on top */
.tube.done { box-shadow: 0 0 22px rgba(255, 255, 255, 0.12); background: rgba(255, 255, 255, 0.05); }
.tube-front.done { border-color: rgba(255, 255, 255, 0.8); animation: doneGlow 0.9s ease-out; }
.tube-front.done::after {
  content: ""; position: absolute; left: -12%; right: -12%; top: calc(var(--D) * -0.3); height: calc(var(--D) * 0.36);
  border-radius: 6px;
  background: linear-gradient(#8f8f99, #55555d);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.35), 0 2px 4px rgba(0, 0, 0, 0.5);
  animation: capOn 0.38s cubic-bezier(.3, 1.6, .5, 1) both;
}
@keyframes doneGlow {
  0% { box-shadow: 0 0 0 rgba(255, 255, 255, 0); }
  35% { box-shadow: 0 0 26px rgba(255, 255, 255, 0.55); }
  100% { box-shadow: 0 0 0 rgba(255, 255, 255, 0); }
}
@keyframes capOn {
  from { transform: translateY(calc(var(--D) * -0.9)); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.hit { z-index: 4; cursor: pointer; }

/* ---------- balls ---------- */
.ball {
  position: absolute; left: 0; top: 0;
  width: var(--D); height: var(--D);
  z-index: 2;
  pointer-events: none;
  will-change: transform;
}
.ball.flying, .ball.lifted { z-index: 5; }
#board.relayout .ball { transition: transform 0.38s cubic-bezier(.3, .9, .4, 1), width 0.38s, height 0.38s; }
#board.relayout .tube, #board.relayout .tube-front { transition: left 0.38s cubic-bezier(.3, .9, .4, 1), top 0.38s, width 0.38s, height 0.38s; }

.skin {
  position: absolute; inset: 3%;
  border-radius: 50%;
  transform-origin: 50% 100%;
  background:
    radial-gradient(circle at 33% 27%, rgba(255, 255, 255, 0.95) 0 7%, rgba(255, 255, 255, 0.5) 12%, rgba(255, 255, 255, 0) 27%),
    radial-gradient(ellipse 45% 22% at 50% 88%, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0) 100%),
    radial-gradient(circle at 42% 36%, var(--l) 0%, var(--b) 40%, var(--d) 100%);
  box-shadow:
    inset 0 calc(var(--D) * -0.06) calc(var(--D) * 0.12) rgba(0, 0, 0, 0.3),
    0 calc(var(--D) * 0.05) calc(var(--D) * 0.08) rgba(0, 0, 0, 0.45);
}
.ball.lifted .skin { animation: bob 1.1s ease-in-out 0.3s infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(calc(var(--D) * -0.08)); }
}

.c0  { --b: #f5222d; --l: #ff8a8a; --d: #7d0008; }
.c1  { --b: #38c92a; --l: #a6ff8f; --d: #0f5c08; }
.c2  { --b: #1f7cf0; --l: #8cc4ff; --d: #0a2f80; }
.c3  { --b: #ff8c12; --l: #ffd08a; --d: #9c3d00; }
.c4  { --b: #8a2fe0; --l: #d1a3ff; --d: #3c0d75; }
.c5  { --b: #e2e2e8; --l: #ffffff; --d: #75757e; }
.c6  { --b: #ffd91a; --l: #fff5a3; --d: #9a7400; }
.c7  { --b: #ff6fb5; --l: #ffc4e1; --d: #a3205f; }
.c8  { --b: #2fd6f5; --l: #b5f4ff; --d: #0a6c80; }
.c9  { --b: #9a5a2c; --l: #d9a37a; --d: #45210a; }
.c10 { --b: #12b394; --l: #86f2d9; --d: #055443; }
.c11 { --b: #d42cc8; --l: #f59cee; --d: #650a5e; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(3px);
  opacity: 0;
  transition: opacity 0.22s ease;
}
.overlay.show { opacity: 1; }
.overlay[hidden] { display: none; }

.sheet {
  width: min(460px, 100%);
  max-height: min(640px, 88dvh);
  display: flex; flex-direction: column;
  background: #222;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 22px;
  padding: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
  transform: translateY(16px) scale(0.98);
  transition: transform 0.25s cubic-bezier(.3, 1.3, .5, 1);
}
.overlay.show .sheet { transform: none; }
.all-games-link { align-self: flex-start; color: #9d9d9d; text-decoration: none; font-weight: 600; font-size: 14px; padding: 0 0 10px; }
.all-games-link:hover { color: #fff; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet h2 { margin: 0; font-size: 22px; }
.sheet-sub { margin: 4px 0 16px; color: #9d9d9d; font-size: 14px; }
.icon-close { background: #333; border: 0; width: 36px; height: 36px; border-radius: 50%; font-size: 22px; line-height: 1; }

.menu-actions { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.pill {
  border: 0; border-radius: 14px; padding: 13px 18px; font-weight: 700; font-size: 15px;
  transition: transform 0.1s ease;
}
.pill:active { transform: translateY(2px); }
.pill-blue { background: linear-gradient(var(--blue-1), var(--blue-2)); box-shadow: 0 3px 0 var(--blue-edge); }
.pill-dark { background: #333; box-shadow: 0 3px 0 #1a1a1a; }
.pill-green { background: linear-gradient(#56e04a, #23a81b); box-shadow: 0 4px 0 #13700d, 0 8px 18px rgba(35, 168, 27, 0.35); }
.pill.big { width: 100%; padding: 16px; font-size: 18px; }

.grid-title { margin: 20px 0 10px; font-size: 15px; color: #cfcfcf; display: flex; justify-content: space-between; }
.grid-title span { color: #8a8a8a; font-weight: 500; }
.level-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 8px;
  overflow-y: auto; padding: 2px 2px 8px; min-height: 0;
}
.lv {
  aspect-ratio: 1; border: 0; border-radius: 12px; background: #2f2f2f; font-weight: 700; font-size: 15px;
}
.lv.cleared { background: #26402a; color: #9ff09a; }
.lv.current { background: linear-gradient(var(--blue-1), var(--blue-2)); color: #fff; }
.lv:disabled { background: #262626; color: #555; cursor: default; }

.link-btn { background: none; border: 0; color: #9d9d9d; padding: 12px; font-size: 14px; text-decoration: underline; text-underline-offset: 3px; }
#m-reset { margin-top: 6px; align-self: center; }

/* win */
.win-card {
  width: min(360px, 100%);
  text-align: center;
  background: radial-gradient(120% 80% at 50% 0%, #2f2f3a, #1f1f24);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 26px;
  padding: 26px 24px 14px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
  transform: scale(0.85);
  transition: transform 0.4s cubic-bezier(.3, 1.6, .5, 1);
}
.overlay.show .win-card { transform: none; }
.win-card h2 { margin: 14px 0 6px; font-size: 28px; }
.win-stat { margin: 0 0 22px; display: flex; flex-direction: column; align-items: center; }
.win-stat span { font-size: 44px; font-weight: 800; line-height: 1.1; }
.win-stat small { color: #9d9d9d; font-size: 13px; letter-spacing: 0.1em; text-transform: uppercase; }
.win-balls { display: flex; justify-content: center; gap: 8px; --D: 34px; }
.win-balls i {
  position: relative; width: var(--D); height: var(--D); border-radius: 50%;
  background:
    radial-gradient(circle at 33% 27%, rgba(255, 255, 255, 0.95) 0 7%, rgba(255, 255, 255, 0.5) 12%, rgba(255, 255, 255, 0) 27%),
    radial-gradient(circle at 42% 36%, var(--l), var(--b) 40%, var(--d));
  animation: hop 0.9s ease-in-out infinite;
}
.win-balls i:nth-child(1) { --b: #f5222d; --l: #ff8a8a; --d: #7d0008; }
.win-balls i:nth-child(2) { --b: #ff8c12; --l: #ffd08a; --d: #9c3d00; animation-delay: 0.1s; }
.win-balls i:nth-child(3) { --b: #38c92a; --l: #a6ff8f; --d: #0f5c08; animation-delay: 0.2s; }
.win-balls i:nth-child(4) { --b: #1f7cf0; --l: #8cc4ff; --d: #0a2f80; animation-delay: 0.3s; }
.win-balls i:nth-child(5) { --b: #8a2fe0; --l: #d1a3ff; --d: #3c0d75; animation-delay: 0.4s; }
@keyframes hop { 0%, 60%, 100% { transform: translateY(0); } 30% { transform: translateY(-12px); } }

/* toast + fx */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0;
  background: rgba(40, 40, 40, 0.95); border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 10px 16px; border-radius: 999px; font-size: 14px; white-space: nowrap;
  transition: opacity 0.2s ease, transform 0.2s ease; pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
#fx { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 25; pointer-events: none; }

@media (min-width: 900px) {
  #board { margin: 0 auto 32px; width: min(1000px, 100%); border: 1px solid rgba(255, 255, 255, 0.06); border-radius: 18px; }
}
@media (prefers-reduced-motion: reduce) {
  .ball.lifted .skin, .win-balls i { animation: none; }
}
