:root {
  --bg-twilight: #090D1E;
  --surface-card: rgba(15, 22, 46, 0.88);
  --surface-elevated: rgba(27, 37, 75, 0.94);
  --border-glow: rgba(168, 85, 247, 0.36);
  --amethyst: #A855F7;
  --lavender-pill: #C084FC;
  --junco-cyan: #38BDF8;
  --emerald-leaf: #34D399;
  --amber-sap: #FBBF24;
  --crimson-berry: #F43F5E;
  --text-primary: #F8FAFC;
  --text-muted: #94A3B8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-twilight);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text-primary);
  touch-action: none;
}

#app-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  max-width: 600px;
  margin: 0 auto;
  overflow: hidden;
  background: radial-gradient(circle at 50% 25%, #161F42 0%, #090D1E 85%);
  box-shadow: 0 0 60px rgba(0, 0, 0, 0.85);
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
  touch-action: none;
}

.fit-text {
  display: block;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
}

/* Top HUD Bar — Zero-Overflow & Score-Safe even at 320px width and 999,999 score */
#top-hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  padding: 6px 8px;
  display: grid;
  grid-template-columns: minmax(68px, auto) minmax(96px, 1fr) minmax(132px, auto);
  align-items: center;
  gap: 6px;
  z-index: 10;
  background: linear-gradient(180deg, rgba(9, 13, 30, 0.95) 0%, rgba(9, 13, 30, 0.68) 78%, rgba(9, 13, 30, 0) 100%);
  pointer-events: none;
}

#top-hud > * {
  pointer-events: auto;
  min-width: 0;
}

.hud-left {
  display: flex;
  align-items: center;
  min-width: 0;
}

.hud-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  min-width: 0;
}

.pill-btn {
  height: 40px;
  min-width: 68px;
  max-width: 92px;
  padding: 0 8px;
  border-radius: 12px;
  border: 1px solid var(--border-glow);
  background: var(--surface-card);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  backdrop-filter: blur(10px);
  transition: transform 0.14s ease, border-color 0.14s ease;
}

.pill-btn:active {
  transform: scale(0.95);
}

.pill-badge {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--lavender-pill);
  white-space: nowrap;
}

.pill-sub {
  font-size: 11px;
  line-height: 1.05;
  color: var(--amber-sap);
  white-space: nowrap;
}

.hud-center {
  width: 100%;
  min-width: 0;
  padding: 0 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.score-cluster {
  width: 100%;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 4px;
  min-width: 0;
  overflow: hidden;
}

.score-caption {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.score-value {
  font-size: 17px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  text-shadow: 0 0 12px rgba(168, 85, 247, 0.6);
  white-space: nowrap;
  min-width: 0;
}

.progress-track {
  width: 100%;
  max-width: 150px;
  height: 5px;
  margin-top: 3px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  overflow: hidden;
}

.progress-fill {
  width: 0%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--amethyst), var(--emerald-leaf), var(--amber-sap));
  transition: width 0.22s ease-out;
}

.shots-pill {
  height: 40px;
  min-width: 42px;
  max-width: 54px;
  padding: 0 6px;
  border-radius: 12px;
  border: 1px solid rgba(56, 189, 248, 0.35);
  background: var(--surface-card);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.shots-label {
  font-size: 10px;
  font-weight: 800;
  color: var(--junco-cyan);
  line-height: 1;
  white-space: nowrap;
}

.shots-count {
  font-size: 13px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  line-height: 1.15;
  white-space: nowrap;
}

.icon-chip {
  height: 40px;
  min-width: 46px;
  max-width: 62px;
  padding: 0 7px;
  border-radius: 12px;
  border: 1px solid var(--border-glow);
  background: var(--surface-card);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.14s ease, background 0.14s ease;
}

.icon-chip:active {
  transform: scale(0.94);
}

.chip-text {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

/* Bottom Booster & Swap Controls */
#bottom-controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 66px;
  padding: 5px 8px 8px;
  z-index: 10;
  background: linear-gradient(0deg, rgba(9, 13, 30, 0.96) 0%, rgba(9, 13, 30, 0.72) 75%, rgba(9, 13, 30, 0) 100%);
  pointer-events: none;
}

.booster-dock {
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  pointer-events: auto;
}

.booster-btn {
  position: relative;
  height: 52px;
  min-width: 0;
  padding: 2px 4px;
  border-radius: 13px;
  border: 1px solid rgba(168, 85, 247, 0.28);
  background: rgba(15, 22, 46, 0.9);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.14s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.booster-btn:active {
  transform: scale(0.95);
}

.booster-btn.ready {
  border-color: var(--emerald-leaf);
  box-shadow: 0 0 14px rgba(52, 211, 153, 0.35);
}

.booster-btn.armed {
  border-color: var(--amber-sap);
  background: rgba(55, 38, 92, 0.95);
  box-shadow: 0 0 18px rgba(251, 191, 36, 0.65);
}

.booster-ring {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(255, 255, 255, 0.08);
}

.booster-fill {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--amethyst), var(--emerald-leaf));
  transition: width 0.2s ease;
}

.booster-icon {
  font-size: 13px;
  line-height: 1;
}

.booster-name {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.01em;
  white-space: nowrap;
  margin-top: 2px;
}

.booster-state {
  font-size: 10px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--emerald-leaf);
  white-space: nowrap;
  line-height: 1.05;
}

.swap-btn {
  border-color: rgba(251, 191, 36, 0.42);
}

.swap-btn .booster-state {
  color: var(--amber-sap);
}

/* Mobbin Phantom-Inspired Bottom Sheet & Result Modal */
.sheet-backdrop {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: rgba(5, 8, 20, 0.76);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  transition: opacity 0.2s ease;
}

.sheet-backdrop.hidden {
  display: none;
}

.phantom-sheet {
  width: 100%;
  max-height: 82vh;
  background: #11172E;
  border-top: 1px solid rgba(192, 132, 252, 0.35);
  border-radius: 24px 24px 0 0;
  padding: 12px 14px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 -16px 44px rgba(0, 0, 0, 0.75);
}

.sheet-drag-handle {
  width: 42px;
  height: 5px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.24);
  margin: 0 auto 2px;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

.sheet-title-wrap {
  min-width: 0;
  flex: 1;
}

.sheet-header h2 {
  font-size: 17px;
  font-weight: 800;
  color: var(--text-primary);
}

.sheet-subtitle {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.sheet-close-btn {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}

.sheet-segment-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 4px;
  border-radius: 13px;
  background: rgba(9, 13, 30, 0.75);
}

.seg-btn {
  height: 34px;
  padding: 0 8px;
  border-radius: 9px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
}

.seg-btn.active {
  background: var(--surface-elevated);
  color: var(--text-primary);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.sheet-panel {
  overflow-y: auto;
  max-height: 56vh;
  padding-right: 2px;
}

.sheet-panel.hidden {
  display: none;
}

.endless-banner {
  padding: 10px 12px;
  border-radius: 15px;
  border: 1px solid rgba(168, 85, 247, 0.4);
  background: linear-gradient(135deg, rgba(46, 26, 84, 0.85), rgba(17, 29, 62, 0.9));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.endless-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.endless-tag {
  font-size: 10px;
  font-weight: 800;
  color: var(--emerald-leaf);
  letter-spacing: 0.03em;
}

.endless-info strong {
  font-size: 14px;
  color: var(--text-primary);
}

.endless-best {
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.lavender-pill-btn {
  height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: none;
  background: linear-gradient(135deg, #C084FC, #9333EA);
  color: #FFFFFF;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
  white-space: nowrap;
  flex-shrink: 0;
  cursor: pointer;
  overflow: hidden;
  box-shadow: 0 4px 14px rgba(168, 85, 247, 0.45);
}

.secondary-pill-btn {
  height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-primary);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
  white-space: nowrap;
  flex-shrink: 0;
  cursor: pointer;
  overflow: hidden;
}

.stage-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
}

.stage-tile {
  padding: 8px 4px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(20, 29, 58, 0.85);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: pointer;
  overflow: hidden;
}

.stage-tile.current {
  border-color: var(--lavender-pill);
  box-shadow: 0 0 12px rgba(192, 132, 252, 0.35);
}

.stage-num {
  font-size: 13px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.stage-stars {
  font-size: 11px;
  color: var(--amber-sap);
}

/* Phantom-style Rank Rows */
.phantom-rank-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rank-row {
  padding: 9px 10px;
  border-radius: 14px;
  background: rgba(21, 30, 62, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}

.rank-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.avatar-medallion {
  position: relative;
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 900;
  border: 2px solid rgba(255, 255, 255, 0.25);
}

.rank-badge {
  position: absolute;
  bottom: -3px;
  left: -3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #1E293B;
  border: 1px solid rgba(255, 255, 255, 0.4);
  font-size: 10px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #FFFFFF;
}

.rank-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.rank-name {
  font-size: 13px;
  font-weight: 800;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rank-delta {
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--emerald-leaf);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Result Card */
#result-overlay {
  align-items: center;
  padding: 16px;
}

.result-card {
  width: 100%;
  max-width: 340px;
  border-radius: 22px;
  border: 1px solid rgba(192, 132, 252, 0.45);
  background: #111833;
  padding: 18px 14px;
  text-align: center;
  box-shadow: 0 20px 55px rgba(0, 0, 0, 0.85);
}

.result-kicker {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--emerald-leaf);
}

.result-title {
  font-size: 20px;
  font-weight: 900;
  margin-top: 4px;
}

.result-stars {
  font-size: 26px;
  color: var(--amber-sap);
  margin: 6px 0 12px;
  text-shadow: 0 0 16px rgba(251, 191, 36, 0.55);
}

.result-stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: 14px;
}

.stat-box {
  padding: 8px 4px;
  border-radius: 12px;
  background: rgba(9, 13, 30, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  min-width: 0;
  overflow: hidden;
}

.stat-label {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  white-space: nowrap;
}

.stat-num {
  font-size: 14px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.emerald-text {
  color: var(--emerald-leaf);
}

.amber-text {
  color: var(--amber-sap);
}

.result-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
