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

:root {
  --board: 360px;
  --cell: 36px;
  --gap: 5px;
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1f2937;
  --line: #30363d;
  --text: #e6edf3;
  --text-dim: #8b949e;
  --accent: #3b82f6;
  --accent-soft: #16314f;
  --gold: #fbbf24;
  --good: #34d399;
  --bad: #fb7185;
  --shadow: 0 24px 64px rgba(0, 0, 0, 0.52);
  --g0: radial-gradient(circle at 34% 30%, #ff8a7a, #ea3d3d 58%, #b21f1f);
  --g1: radial-gradient(circle at 34% 30%, #ffe79a, #ffc107 56%, #d18f00);
  --g2: radial-gradient(circle at 34% 30%, #7df0a3, #2eb85c 58%, #18803f);
  --g3: radial-gradient(circle at 34% 30%, #9cc2ff, #3b82f6 58%, #1d5bc9);
  --g4: radial-gradient(circle at 34% 30%, #d3b2f7, #9b5de5 58%, #6a31b4);
  --g5: radial-gradient(circle at 34% 30%, #ffb0d4, #f25ca2 58%, #c3357b);
  --s0: #ea3d3d;
  --s1: #ffc107;
  --s2: #2eb85c;
  --s3: #3b82f6;
  --s4: #9b5de5;
  --s5: #f25ca2;
}

html,
body {
  height: 100%;
}

body {
  background:
    radial-gradient(circle at top, rgba(59, 130, 246, 0.15), transparent 30%),
    linear-gradient(180deg, #0d1117 0%, #06080d 100%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
}

#app {
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  max-width: 560px;
  padding: calc(env(safe-area-inset-top) + 8px) 10px calc(env(safe-area-inset-bottom) + 8px);
}

#topbar {
  align-items: center;
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.icon-btn,
.chip-btn,
.ctrl-btn,
.menu-btn {
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  transition: background 0.12s ease, transform 0.08s ease, border-color 0.12s ease;
}

.icon-btn,
.chip-btn {
  flex: 0 0 auto;
}

.icon-btn {
  align-items: center;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  font-size: 20px;
  height: 44px;
  justify-content: center;
  width: 44px;
}

.chip-btn {
  border-radius: 999px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
  min-width: 74px;
  padding: 10px 14px;
}

.icon-btn:active,
.chip-btn:active,
.ctrl-btn:active,
.menu-btn:active {
  background: var(--panel-2);
  transform: scale(0.97);
}

.stat-group {
  display: flex;
  flex: 1 1 auto;
  gap: 8px;
}

.stat,
.next-group,
.account-card {
  background: rgba(22, 27, 34, 0.92);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.stat {
  align-items: center;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 72px;
  padding: 6px 12px;
}

.stat-label {
  color: var(--text-dim);
  font-size: 10px;
  letter-spacing: 1.4px;
  text-transform: uppercase;
}

.stat-value {
  color: var(--gold);
  font-size: 22px;
  font-weight: 800;
  line-height: 1.15;
}

.stat-value.alt {
  color: #c084fc;
}

.next-group {
  align-items: center;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 6px 10px;
}

.next-balls {
  display: flex;
  gap: 4px;
}

.mini-ball {
  border-radius: 50%;
  box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.24);
  height: 16px;
  width: 16px;
}

#board-wrap {
  align-items: center;
  display: flex;
  flex: 1 1 auto;
  justify-content: center;
  min-height: 0;
  position: relative;
}

#board {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.03), rgba(255, 255, 255, 0)),
    var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  height: var(--board);
  padding: var(--gap);
  position: relative;
  width: var(--board);
}

#board.tension {
  animation: tension 1.05s ease-in-out infinite;
}

#cells {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(9, 1fr);
  height: 100%;
  width: 100%;
}

.cell {
  background: #11161f;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.1s ease;
}

.cell.reachable {
  background: #183150;
  box-shadow: inset 0 0 0 1px #255088;
}

.cell.invalid {
  animation: invalid 0.38s ease;
}

.cell.hint-target {
  animation: hint-target 1.4s ease-in-out 2;
}

#ball-layer,
#fx-layer {
  inset: var(--gap);
  pointer-events: none;
  position: absolute;
}

#ball-layer {
  z-index: 2;
}

#fx-layer {
  overflow: visible;
  z-index: 3;
}

.ball {
  height: var(--cell);
  left: 0;
  position: absolute;
  top: 0;
  width: var(--cell);
  will-change: transform;
}

.ball-inner {
  border-radius: 50%;
  box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.35), 0 3px 5px rgba(0, 0, 0, 0.4);
  inset: 7%;
  position: absolute;
}

.ball-inner::after {
  background: rgba(255, 255, 255, 0.58);
  border-radius: 50%;
  content: "";
  filter: blur(2px);
  height: 24%;
  left: 20%;
  position: absolute;
  top: 15%;
  width: 30%;
}

.ball.selected .ball-inner {
  animation: bob 0.58s ease-in-out infinite;
}

.ball.hint-source .ball-inner {
  animation: hint-source 1.35s ease-in-out 2;
}

.c0 { background: var(--g0); }
.c1 { background: var(--g1); }
.c2 { background: var(--g2); }
.c3 { background: var(--g3); }
.c4 { background: var(--g4); }
.c5 { background: var(--g5); }

.ball-symbol {
  background: rgba(255, 255, 255, 0.92);
  display: none;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.35));
  inset: 33%;
  position: absolute;
}

body.colorblind .ball-symbol {
  display: block;
}

.sym0 { clip-path: circle(50%); }
.sym1 { clip-path: polygon(50% 4%, 96% 92%, 4% 92%); }
.sym2 { clip-path: inset(0); }
.sym3 { clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%); }
.sym4 { clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); }
.sym5 { clip-path: polygon(24% 4%, 76% 4%, 100% 50%, 76% 96%, 24% 96%, 0 50%); }

.particle {
  border-radius: 50%;
  position: absolute;
  will-change: transform, opacity;
}

.float-score {
  color: var(--gold);
  font-size: clamp(18px, calc(var(--cell) * 0.72), 38px);
  font-weight: 900;
  pointer-events: none;
  position: absolute;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.68);
  white-space: nowrap;
}

.float-combo {
  color: #ff7eb6;
  display: block;
  font-size: 0.56em;
  text-align: center;
}

#controls {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.ctrl-btn {
  border-radius: 12px;
  cursor: pointer;
  flex: 1 1 0;
  font-size: 14px;
  font-weight: 700;
  height: 46px;
}

.ctrl-btn:disabled,
.menu-btn:disabled,
.chip-btn:disabled {
  cursor: default;
  opacity: 0.42;
}

#toast {
  background: rgba(13, 17, 23, 0.96);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  left: 50%;
  max-width: calc(100% - 32px);
  opacity: 0;
  padding: 8px 16px;
  pointer-events: none;
  position: absolute;
  text-align: center;
  top: 10px;
  transform: translateX(-50%) translateY(-8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 10;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#toast.good {
  border-color: #6b5a1f;
  color: #ffe082;
}

#toast.bad {
  border-color: #6b2b2b;
  color: #fca5a5;
}

.overlay {
  align-items: center;
  background: rgba(4, 7, 11, 0.84);
  backdrop-filter: blur(6px);
  display: flex;
  inset: 0;
  justify-content: center;
  opacity: 0;
  padding: 24px 16px;
  pointer-events: none;
  position: fixed;
  transition: opacity 0.22s ease;
  z-index: 50;
}

.overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.panel {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.04), rgba(255, 255, 255, 0)),
    var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 32px 80px rgba(0, 0, 0, 0.72);
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 90vh;
  max-width: 360px;
  overflow-y: auto;
  padding: 28px 24px;
  width: 100%;
}

.start-panel {
  max-width: 380px;
}

.panel h2 {
  font-size: 25px;
  font-weight: 800;
  text-align: center;
}

.game-title {
  background: linear-gradient(135deg, #58a6ff, #bc8cff, #ff7eb6);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-size: 54px;
  font-weight: 900;
  letter-spacing: 6px;
  text-align: center;
}

.tagline {
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.5;
  text-align: center;
}

.account-card {
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 16px;
}

.account-card strong {
  font-size: 15px;
}

.account-card span {
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.45;
}

.menu-btn {
  border-radius: 12px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 700;
  min-height: 46px;
  padding: 12px 14px;
  width: 100%;
}

.menu-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.menu-btn.armed {
  background: #7a2233;
  border-color: #b13651;
  color: #ffd6dd;
}

.switch-row {
  align-items: center;
  background: rgba(31, 41, 55, 0.9);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  padding: 11px 14px;
}

.switch-row span:first-child {
  font-size: 14px;
  font-weight: 600;
}

.switch {
  background: #334155;
  border-radius: 999px;
  flex: 0 0 auto;
  height: 26px;
  position: relative;
  transition: background 0.15s ease;
  width: 44px;
}

.switch::after {
  background: #d1d5db;
  border-radius: 50%;
  content: "";
  height: 20px;
  left: 3px;
  position: absolute;
  top: 3px;
  transition: transform 0.15s ease;
  width: 20px;
}

.switch.on {
  background: var(--accent);
}

.switch.on::after {
  background: #ffffff;
  transform: translateX(18px);
}

.menu-stats,
.profile-meta,
.helper-text,
.save-note {
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.5;
  text-align: center;
}

.menu-stats b {
  color: var(--text);
}

.final-score {
  color: var(--gold);
  font-size: 60px;
  font-weight: 900;
  line-height: 1;
  text-align: center;
}

.go-note {
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
}

.go-note.record {
  color: #ff7eb6;
  font-weight: 700;
}

.rules {
  display: flex;
  flex-direction: column;
  gap: 10px;
  list-style: none;
}

.rules li {
  color: var(--text);
  font-size: 13.5px;
  line-height: 1.5;
  padding-left: 22px;
  position: relative;
}

.rules li::before {
  color: var(--accent);
  content: "\2022";
  left: 0;
  position: absolute;
  top: 0;
}

.field-label {
  font-size: 13px;
  font-weight: 700;
}

.text-input {
  background: #0f172a;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  min-height: 48px;
  outline: none;
  padding: 0 14px;
}

.text-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
}

.error-text {
  color: var(--bad);
  font-size: 13px;
  text-align: center;
}

.leaderboard-list {
  border: 1px solid var(--line);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 220px;
  padding: 12px;
}

.leaderboard-row,
.leaderboard-empty {
  align-items: center;
  background: rgba(31, 41, 55, 0.82);
  border: 1px solid rgba(71, 85, 105, 0.5);
  border-radius: 12px;
  display: grid;
  gap: 10px;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  padding: 12px 14px;
}

.leaderboard-empty {
  color: var(--text-dim);
  display: flex;
  justify-content: center;
  min-height: 100%;
  text-align: center;
}

.leaderboard-row.current-user {
  border-color: rgba(59, 130, 246, 0.8);
}

.leaderboard-rank {
  color: var(--gold);
  font-size: 16px;
  font-weight: 800;
  text-align: center;
}

.leaderboard-player {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.leaderboard-name {
  font-size: 14px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-date {
  color: var(--text-dim);
  font-size: 12px;
}

.leaderboard-score {
  color: var(--gold);
  font-size: 17px;
  font-weight: 800;
}

@keyframes bob {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-13%) scale(1.05); }
}

@keyframes invalid {
  0%, 100% { background: #11161f; }
  35% { background: #5a1d1d; }
}

@keyframes tension {
  0%, 100% { box-shadow: var(--shadow), 0 0 0 0 rgba(239, 68, 68, 0); }
  50% { box-shadow: var(--shadow), 0 0 0 3px rgba(239, 68, 68, 0.48); }
}

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

@keyframes hint-target {
  0%, 100% { background: #11161f; box-shadow: none; }
  50% { background: #183150; box-shadow: inset 0 0 0 2px var(--accent); }
}

@media (max-width: 420px) {
  #app {
    padding-left: 8px;
    padding-right: 8px;
  }

  .chip-btn {
    min-width: 64px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .ctrl-btn {
    font-size: 13px;
  }

  .game-title {
    font-size: 46px;
    letter-spacing: 4px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
  }

  .ball.selected .ball-inner,
  .ball.hint-source .ball-inner,
  #board.tension,
  .cell.hint-target {
    animation: none !important;
  }
}
