:root {
  --bg: #0c0b1d;
  --bg-2: #15132e;
  --panel: rgba(28, 25, 56, 0.82);
  --panel-solid: #1b1836;
  --line: rgba(255, 255, 255, 0.09);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #f2eefc;
  --muted: #a49fc2;
  --brass: #e9b45e;
  --brass-hi: #ffe3a3;
  --good: #3dff9a;
  --warn: #ffb347;
  --bad: #ff4d6d;
  --radius: 16px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --display: 'Fraunces', Georgia, 'Times New Roman', serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body {
  background:
    radial-gradient(1200px 700px at 50% -10%, rgba(120, 90, 255, 0.22), transparent 60%),
    radial-gradient(900px 600px at 100% 110%, rgba(255, 120, 80, 0.10), transparent 60%),
    radial-gradient(900px 600px at 0% 100%, rgba(40, 200, 255, 0.08), transparent 60%),
    var(--bg);
}

button { font: inherit; color: inherit; cursor: pointer; }
kbd {
  font-family: var(--font);
  font-size: 0.78em;
  padding: 1px 6px;
  border-radius: 6px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.06);
}

/* ---------------------------------------------------------------- layout */

.app {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  grid-template-columns: minmax(0, 1fr);
  height: 100%;
  height: 100dvh;
  padding: max(10px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  gap: 8px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 0;
  padding: 4px 6px;
  border-radius: 12px;
  flex-shrink: 0;
}
.brand:hover { background: rgba(255, 255, 255, 0.05); }
.brand-marble, .title .brand-marble {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffb3bd, #ff4f64 45%, #8c1023);
  box-shadow: 0 0 16px rgba(255, 79, 100, 0.5), inset -2px -3px 6px rgba(0, 0, 0, 0.3);
}
.brand-name {
  font-family: var(--display);
  font-weight: 800;
  font-size: 20px;
  letter-spacing: 0.2px;
}
.brand-name em, .title em { font-style: normal; color: var(--brass); }

.level-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
  padding-left: 16px;
  border-left: 1px solid var(--line);
}
.level-num {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1.6px;
  color: var(--muted);
  font-weight: 600;
}
.level-info h1 {
  margin: 0;
  font-family: var(--display);
  font-weight: 600;
  font-size: 22px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats { display: flex; gap: 18px; }
.stat { display: flex; flex-direction: column; align-items: flex-end; }
.stat-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.4px;
  color: var(--muted);
  font-weight: 600;
}
.stat-value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-value.bump { animation: bump 0.35s ease; }
@keyframes bump { 40% { transform: scale(1.18); } }

.actions { display: flex; gap: 4px; }
.icon-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.04);
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.09); border-color: var(--line); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn .ic-play, .icon-btn .ic-off { display: none; }
.icon-btn.on .ic-play, .icon-btn.muted .ic-off { display: block; }
.icon-btn.on .ic-pause, .icon-btn.muted .ic-on { display: none; }
.icon-btn.active { background: rgba(233, 180, 94, 0.16); border-color: rgba(233, 180, 94, 0.35); color: var(--brass-hi); }
.icon-btn .ic-play { fill: currentColor; stroke: none; }

.hint-bar {
  display: none;
  align-items: center;
  gap: 10px;
  margin: 0 auto;
  max-width: 760px;
  padding: 8px 16px;
  border-radius: 999px;
  background: rgba(233, 180, 94, 0.09);
  border: 1px solid rgba(233, 180, 94, 0.22);
  color: #f6e6c8;
  font-size: 14px;
  line-height: 1.35;
  animation: fadeIn 0.3s ease;
}
.hint-bar.show { display: flex; }
.hint-icon { color: var(--brass); }

.stage { min-height: 0; position: relative; }
.board-wrap { position: absolute; inset: 0; }
#board { display: block; touch-action: none; }
.paused-badge {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid var(--line-strong);
  font-size: 12px;
  letter-spacing: 2px;
  text-transform: uppercase;
  font-weight: 600;
  display: none;
}
.paused .paused-badge { display: block; }

/* ------------------------------------------------------------------ tray */

.tray {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 12px;
  border-radius: 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}

.reservoir {
  display: flex;
  min-width: 0;
  gap: 8px;
  flex: 1;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px;
}
.reservoir::-webkit-scrollbar { display: none; }

.chip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 6px 6px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  flex-shrink: 0;
  transition: border-color 0.15s, background 0.15s, transform 0.15s;
}
.chip.sep { margin-left: 10px; }
.chip.sep::before {
  content: '';
  position: absolute;
  left: -10px;
  top: 8px;
  bottom: 8px;
  width: 1px;
  background: var(--line-strong);
}
.chip.selected {
  border-color: var(--chip);
  background: color-mix(in srgb, var(--chip) 16%, transparent);
  box-shadow: 0 0 0 1px var(--chip), 0 0 18px color-mix(in srgb, var(--chip) 40%, transparent);
}
.chip.empty .pick { opacity: 0.35; }
.chip .pick {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 0;
  background: none;
  padding: 0;
  touch-action: none;
}
.marble-dot {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--light), var(--hex) 45%, var(--dark));
  box-shadow: 0 0 12px color-mix(in srgb, var(--hex) 55%, transparent), inset -2px -3px 6px rgba(0, 0, 0, 0.25);
  position: relative;
  flex-shrink: 0;
}
.marble-dot::after {
  content: '';
  position: absolute;
  left: 22%;
  top: 16%;
  width: 34%;
  height: 22%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  transform: rotate(-35deg);
}
.marble-dot.steel { background: radial-gradient(circle at 35% 30%, #fff, #c9d1dc 35%, #7d8796 60%, #d6dce5 75%, #3c4452); box-shadow: inset -2px -3px 6px rgba(0, 0, 0, 0.25); }
.marble-dot.chameleon { background: conic-gradient(from 0deg, #ff4f64, #ffc93d, #2fd67b, #3d8bff, #b066ff, #ff4f64); animation: spin 4s linear infinite; }
.marble-dot.chameleon::before { content: ''; position: absolute; inset: 4px; border-radius: 50%; background: radial-gradient(circle at 40% 35%, #3a2f55, #0d0a16); }
@keyframes spin { to { transform: rotate(360deg); } }
.chip .count { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 14px; font-size: 15px; }
.chip .buy {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px dashed color-mix(in srgb, var(--hex) 70%, white);
  background: transparent;
  color: var(--light);
  font-weight: 700;
  font-size: 16px;
  line-height: 1;
  display: grid;
  place-items: center;
  opacity: 0.75;
  transition: opacity 0.15s, background 0.15s;
}
.chip .buy:hover { opacity: 1; background: color-mix(in srgb, var(--hex) 20%, transparent); }

.controls { display: flex; align-items: center; gap: 18px; flex-shrink: 0; }
.ctl-label {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.4px;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 4px;
}
.load { display: flex; align-items: center; gap: 10px; }
.lamp {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #4a4560, #1b1830);
  border: 1px solid var(--line-strong);
  transition: background 0.2s, box-shadow 0.2s;
}
.lamp.warn {
  background: radial-gradient(circle at 40% 35%, #fff2c2, var(--warn) 50%, #8a4b00);
  box-shadow: 0 0 18px var(--warn);
  animation: blink 0.6s steps(2) infinite;
}
.lamp.boom { background: radial-gradient(circle at 40% 35%, #ffd0d8, var(--bad) 50%, #7a0018); box-shadow: 0 0 22px var(--bad); }
@keyframes blink { 50% { opacity: 0.45; } }
.load-dots { display: flex; gap: 4px; }
.load-dots i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  transition: background 0.15s;
}
.load-dots i.on { background: var(--good); box-shadow: 0 0 6px var(--good); }
.load-dots.warn i.on { background: var(--warn); box-shadow: 0 0 6px var(--warn); }

.speed-ctl {
  display: flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.speed-ctl button {
  width: 30px;
  height: 28px;
  border: 0;
  background: rgba(255, 255, 255, 0.04);
  font-size: 16px;
}
.speed-ctl button:hover { background: rgba(255, 255, 255, 0.1); }
.speed-ctl span { min-width: 42px; text-align: center; font-weight: 600; font-size: 14px; font-variant-numeric: tabular-nums; }

/* --------------------------------------------------------------- screens */

.screen {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  z-index: 20;
  padding: 20px;
}
.screen.show { display: grid; animation: fadeIn 0.25s ease; }
@keyframes fadeIn { from { opacity: 0; } }

.modal-screen { background: rgba(6, 5, 16, 0.66); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }

.panel {
  width: min(460px, 100%);
  max-height: calc(100dvh - 40px);
  overflow: auto;
  background: var(--panel-solid);
  border: 1px solid var(--line-strong);
  border-radius: 24px;
  padding: 24px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
  animation: pop 0.3s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.panel.wide { width: min(880px, 100%); }
@keyframes pop { from { transform: scale(0.94) translateY(8px); opacity: 0; } }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.panel h2 { margin: 0; font-family: var(--display); font-weight: 600; font-size: 26px; }
.panel h3 { margin: 18px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 1.4px; color: var(--brass); }
.panel p { margin: 0 0 8px; line-height: 1.55; color: #ddd8f0; }
.muted { color: var(--muted) !important; font-size: 14px; }

.btn {
  padding: 12px 22px;
  border-radius: 14px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.06);
  font-weight: 600;
  font-size: 15px;
  transition: transform 0.1s, background 0.15s, box-shadow 0.15s;
}
.btn:hover { background: rgba(255, 255, 255, 0.11); }
.btn:active { transform: scale(0.97); }
.btn.primary {
  border: 0;
  color: #241503;
  background: linear-gradient(180deg, var(--brass-hi), var(--brass));
  box-shadow: 0 8px 26px rgba(233, 180, 94, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.6);
}
.btn.primary:hover { box-shadow: 0 10px 34px rgba(233, 180, 94, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.6); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: rgba(255, 255, 255, 0.05); }

/* title */
.title-screen { background: radial-gradient(900px 600px at 50% 30%, rgba(120, 90, 255, 0.25), transparent 70%), var(--bg); z-index: 30; overflow: hidden; }
.title-card { position: relative; text-align: center; max-width: 560px; }
.eyebrow {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 2.4px;
  color: var(--brass);
  font-weight: 600;
  margin: 0 0 10px;
}
.title {
  margin: 0;
  font-family: var(--display);
  font-weight: 800;
  font-size: clamp(52px, 11vw, 96px);
  line-height: 0.95;
  letter-spacing: -1px;
  text-shadow: 0 10px 50px rgba(120, 90, 255, 0.45);
}
.tagline { color: var(--muted); font-size: 18px; margin: 16px 0 32px; }
.title-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.progress-line { color: var(--muted); font-size: 14px; margin-top: 22px; min-height: 1em; }

.rain { position: absolute; inset: 0; pointer-events: none; }
.rain span {
  position: absolute;
  top: -60px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, var(--light), var(--hex) 45%, var(--dark));
  box-shadow: 0 0 20px color-mix(in srgb, var(--hex) 60%, transparent);
  opacity: 0.5;
  animation: fall linear infinite;
}
@keyframes fall { to { transform: translateY(calc(100vh + 120px)); } }

/* level select */
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}
.level-card {
  position: relative;
  text-align: left;
  padding: 14px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.035);
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 104px;
  transition: transform 0.12s, border-color 0.15s, background 0.15s;
}
.level-card:hover { transform: translateY(-2px); border-color: var(--line-strong); background: rgba(255, 255, 255, 0.06); }
.level-card.current { border-color: rgba(233, 180, 94, 0.6); }
.level-card .num { font-size: 12px; color: var(--muted); font-weight: 600; letter-spacing: 1px; display: flex; align-items: center; gap: 6px; }
.level-card .status { width: 8px; height: 8px; border-radius: 50%; background: rgba(255, 255, 255, 0.18); }
.level-card .status.started { background: var(--warn); }
.level-card .status.solved { background: var(--good); }
.level-card .name { font-weight: 600; font-size: 15px; line-height: 1.25; }
.level-card .mini-stars { margin-top: auto; display: flex; gap: 2px; }
.mini-stars svg { width: 15px; height: 15px; fill: rgba(255, 255, 255, 0.12); }
.mini-stars svg.lit { fill: var(--brass); filter: drop-shadow(0 0 4px rgba(233, 180, 94, 0.6)); }

/* help */
.help-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 28px; }
.score-table { width: 100%; border-collapse: collapse; font-size: 14px; margin-bottom: 10px; }
.score-table td { padding: 6px 0; border-bottom: 1px solid var(--line); color: #ddd8f0; }
.score-table td:last-child { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.legend {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 8px;
}
.legend-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.035);
  border: 1px solid var(--line);
}
.legend-item canvas { width: 46px; height: 46px; flex-shrink: 0; }
.legend-item b { display: block; font-size: 14px; }
.legend-item span { font-size: 12.5px; color: var(--muted); line-height: 1.35; }
.keys { margin-top: 16px !important; }

/* win */
.win-panel { text-align: center; }
.win-panel h2 { font-size: 36px; }
.stars { display: flex; justify-content: center; gap: 6px; margin: 16px 0 18px; }
.stars svg {
  width: 58px;
  height: 58px;
  fill: rgba(255, 255, 255, 0.1);
  transform: scale(0.6);
  opacity: 0.4;
  transition: transform 0.4s cubic-bezier(0.2, 1.8, 0.4, 1), fill 0.2s, opacity 0.2s;
}
.stars svg:nth-child(2) { transform: scale(0.6) translateY(-8px); }
.stars svg.lit { fill: var(--brass); transform: scale(1); opacity: 1; filter: drop-shadow(0 0 14px rgba(233, 180, 94, 0.7)); }
.stars svg.lit:nth-child(2) { transform: scale(1.15) translateY(-8px); }
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0 0 14px; }
.win-stats div { background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line); border-radius: 14px; padding: 10px 6px; }
.win-stats dt { font-size: 10px; text-transform: uppercase; letter-spacing: 1.4px; color: var(--muted); font-weight: 600; }
.win-stats dd { margin: 4px 0 0; font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* misc */
.toast {
  position: fixed;
  left: 50%;
  bottom: 110px;
  transform: translate(-50%, 20px);
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(20, 18, 40, 0.92);
  border: 1px solid var(--line-strong);
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 40;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { border-color: rgba(255, 77, 109, 0.5); }

.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  pointer-events: none;
  z-index: 50;
  display: none;
  border-radius: 50%;
}
.drag-ghost.show { display: block; }
.drag-ghost .marble-dot { width: 40px; height: 40px; box-shadow: 0 10px 24px rgba(0, 0, 0, 0.5), 0 0 22px var(--hex); }

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

@media (max-width: 860px) {
  .brand-name { display: none; }
  .stats { gap: 12px; }
  .stat-value { font-size: 17px; }
  .help-cols { grid-template-columns: 1fr; gap: 0; }
}

@media (max-width: 640px) {
  .app { gap: 6px; padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .topbar { flex-wrap: wrap; gap: 8px 12px; }
  .level-info { order: 3; flex-basis: 100%; padding-left: 0; border-left: 0; flex-direction: row; align-items: baseline; gap: 10px; }
  .level-info h1 { font-size: 19px; }
  .brand { padding: 2px; }
  .stats { margin-left: auto; }
  .actions { gap: 2px; }
  .icon-btn { width: 34px; height: 34px; border-radius: 10px; }
  .icon-btn svg { width: 18px; height: 18px; }
  #btn-help { display: none; }
  .hint-bar { font-size: 13px; border-radius: 14px; padding: 7px 12px; }
  .tray { flex-direction: column; align-items: stretch; gap: 8px; padding: 8px; border-radius: 16px; }
  .controls { justify-content: space-between; }
  .load, .speed { display: flex; align-items: center; gap: 8px; }
  .speed .ctl-label, .load .ctl-label { margin: 0; }
  .speed { gap: 8px; }
  .marble-dot { width: 26px; height: 26px; }
  .chip .buy { width: 24px; height: 24px; }
  .panel { padding: 18px; border-radius: 20px; }
  .level-grid { grid-template-columns: repeat(2, 1fr); }
  .toast { bottom: 150px; }
}

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