:root {
  --bg0: #060b16;
  --bg1: #101a33;
  --panel: rgba(8, 13, 28, 0.72);
  --border: rgba(255, 255, 255, 0.14);
  --text: #f7fbff;
  --muted: rgba(247, 251, 255, 0.66);
  --accent: #7fe8ff;
  --good: #7effbb;
  --bad: #ff5e7b;
  --gold: #ffd56a;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 14%, rgba(80, 120, 255, 0.28), transparent 28%),
    radial-gradient(circle at 18% 80%, rgba(60, 255, 213, 0.12), transparent 25%),
    radial-gradient(circle at 82% 72%, rgba(177, 80, 255, 0.12), transparent 26%),
    linear-gradient(180deg, var(--bg1), var(--bg0) 60%, #02040a);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  touch-action: none;
  user-select: none;
  overscroll-behavior: none;
}

canvas {
  display: block;
  width: 100vw;
  height: 100vh;
}

#hud {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  z-index: 20;
  pointer-events: none;
}

.panel {
  background: linear-gradient(180deg, rgba(17, 25, 48, 0.84), rgba(8, 11, 24, 0.76));
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.34), inset 0 1px 0 rgba(255,255,255,0.10);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

#stats {
  pointer-events: auto;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, auto));
  gap: 10px 14px;
  padding: 12px 14px;
  align-self: flex-start;
}

.stat {
  min-width: 68px;
}

.label {
  color: var(--muted);
  font-size: 10px;
  line-height: 1;
  letter-spacing: 0.16em;
  font-weight: 900;
  text-transform: uppercase;
}

.value {
  margin-top: 6px;
  font-size: 28px;
  font-weight: 1000;
  letter-spacing: -0.05em;
  line-height: 0.96;
  text-shadow: 0 0 22px rgba(127, 232, 255, 0.12);
}

#rightColumn {
  pointer-events: none;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  width: min(620px, calc(100vw - 20px));
}

#tools,
#piPanel {
  pointer-events: auto;
}

.toolbarPanel {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  padding: 8px;
}

#piPanel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  width: 100%;
  box-sizing: border-box;
}

#playerMeta {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  flex: 1;
}

#playerName {
  font-size: 15px;
  font-weight: 1000;
  letter-spacing: -0.03em;
}

#piStatus {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

.inventory {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: rgba(255,255,255,0.92);
  font-size: 12px;
  font-weight: 900;
}

.inventory span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
}

.actionsRow {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

button {
  appearance: none;
  border: 0;
  cursor: pointer;
  font-family: inherit;
  font-weight: 1000;
  letter-spacing: -0.02em;
  color: #06101a;
  border-radius: 999px;
  padding: 11px 14px;
  min-height: 42px;
  background: linear-gradient(135deg, #ffffff, #86f4ff 45%, #70ffb8);
  box-shadow: 0 14px 34px rgba(109, 240, 255, 0.20);
  transition: transform 120ms ease, opacity 120ms ease, filter 120ms ease;
  white-space: nowrap;
}

button.secondary {
  color: #f7fbff;
  background: rgba(255,255,255,0.10);
  border: 1px solid rgba(255,255,255,0.16);
  box-shadow: 0 10px 24px rgba(0,0,0,0.18);
}

button.gold {
  background: linear-gradient(135deg, #fff3b0, #ffd56a 52%, #ff9d2e);
  box-shadow: 0 14px 40px rgba(255, 213, 106, 0.22);
}

button:disabled {
  opacity: 0.50;
  cursor: not-allowed;
  filter: grayscale(0.5);
}

button:active:not(:disabled) {
  transform: scale(0.96);
}

#hint {
  position: fixed;
  left: 50%;
  bottom: max(12px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 20;
  max-width: min(680px, calc(100vw - 20px));
  padding: 11px 16px;
  text-align: center;
  font-size: 14px;
  color: rgba(255,255,255,0.86);
  pointer-events: none;
}

#toastLayer {
  position: fixed;
  inset: 0;
  z-index: 25;
  pointer-events: none;
}

.floatText {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  white-space: nowrap;
  font-size: 24px;
  font-weight: 1000;
  letter-spacing: -0.05em;
  text-shadow: 0 4px 0 rgba(0,0,0,0.34), 0 0 22px currentColor;
  animation: floatUp 980ms cubic-bezier(.19,1,.22,1) forwards;
}

@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%, -20%) scale(0.72); }
  16% { opacity: 1; transform: translate(-50%, -70%) scale(1.08); }
  100% { opacity: 0; transform: translate(-50%, -165%) scale(0.94); }
}

.overlay {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  z-index: 40;
  background: rgba(2, 4, 10, 0.72);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: 16px;
  box-sizing: border-box;
}

.overlay.visible {
  display: grid;
}

.modalCard {
  width: min(620px, calc(100vw - 20px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 24px 20px 20px;
  border-radius: 28px;
  background: linear-gradient(180deg, rgba(20, 29, 58, 0.96), rgba(6, 9, 20, 0.96));
  border: 1px solid rgba(255,255,255,0.16);
  box-shadow: 0 30px 90px rgba(0,0,0,0.60), inset 0 1px 0 rgba(255,255,255,0.12);
}

.modalHeader {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.modalCard h1,
.modalCard h2 {
  margin: 0 0 6px;
  font-size: 32px;
  letter-spacing: -0.06em;
}

.modalCard p {
  margin: 0;
  color: rgba(255,255,255,0.68);
  line-height: 1.35;
}

.closeBtn {
  min-width: 42px;
  padding: 10px 12px;
}

.shopGrid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 18px;
}

.pack {
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 22px;
  padding: 15px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.packName {
  font-size: 19px;
  font-weight: 1000;
  letter-spacing: -0.05em;
}

.packPrice {
  color: var(--gold);
  font-size: 28px;
  font-weight: 1000;
  letter-spacing: -0.06em;
}

.packItems {
  color: rgba(255,255,255,0.72);
  font-size: 13px;
  line-height: 1.45;
  min-height: 56px;
}

.leaderboardList {
  margin-top: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.leaderboardRow {
  display: grid;
  grid-template-columns: 44px 1fr 90px 78px;
  gap: 8px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.10);
  font-weight: 800;
}

.leaderboardRow .muted {
  color: rgba(255,255,255,0.58);
  font-size: 12px;
}

@media (max-width: 980px) {
  #hud {
    flex-direction: column;
    align-items: stretch;
  }

  #rightColumn {
    width: 100%;
    align-items: stretch;
  }

  .toolbarPanel {
    justify-content: flex-start;
  }

  #piPanel {
    flex-direction: column;
    align-items: stretch;
  }

  .actionsRow {
    justify-content: stretch;
  }

  .actionsRow button {
    flex: 1;
  }
}

@media (max-width: 720px) {
  #hud {
    gap: 8px;
  }

  #stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px 10px;
    padding: 11px 12px;
  }

  .stat {
    min-width: 0;
  }

  .value {
    font-size: 22px;
  }

  button {
    min-height: 40px;
    padding: 10px 12px;
    font-size: 13px;
  }

  .shopGrid {
    grid-template-columns: 1fr;
  }

  .leaderboardRow {
    grid-template-columns: 32px 1fr 64px;
  }

  .leaderboardRow .movesCol {
    display: none;
  }

  #hint {
    font-size: 13px;
    padding: 10px 14px;
    bottom: max(10px, env(safe-area-inset-bottom));
  }

  .modalCard h1,
  .modalCard h2 {
    font-size: 28px;
  }
}

@media (max-width: 520px) {
  #stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .toolbarPanel {
    gap: 6px;
  }

  #tools button {
    flex: 1 1 calc(50% - 6px);
  }

  #hint {
    max-width: calc(100vw - 16px);
  }
}


/* v3 mobile polish */
#tools button, .actionsRow button { min-width: 0; }
#piStatus { max-width: 100%; }

@media (max-width: 720px) {
  #hud {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
    top: max(8px, env(safe-area-inset-top));
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
  }

  #stats {
    width: fit-content;
    max-width: 170px;
    padding: 10px 12px;
    border-radius: 18px;
    gap: 8px 12px;
  }

  #rightColumn {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    align-items: start;
  }

  #tools {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    padding: 8px;
    border-radius: 18px;
    align-content: start;
  }

  #tools button {
    width: 100%;
    min-height: 36px;
    padding: 9px 8px;
    font-size: 12px;
  }

  #piPanel {
    padding: 10px;
    border-radius: 18px;
    gap: 8px;
    min-width: 0;
  }

  #playerName { font-size: 14px; }

  #piStatus {
    font-size: 11px;
    line-height: 1.15;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .inventory {
    gap: 6px;
    font-size: 11px;
  }

  .inventory span {
    padding: 3px 7px;
  }

  .actionsRow {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .actionsRow button {
    width: 100%;
    min-height: 38px;
    padding: 10px 8px;
    font-size: 12px;
  }

  #hint {
    font-size: 12px;
    line-height: 1.35;
    padding: 9px 12px;
    max-width: min(270px, calc(100vw - 20px));
    border-radius: 18px;
  }
}

@media (max-width: 420px) {
  #stats { max-width: 160px; }

  .value { font-size: 20px; }

  .label { font-size: 9px; }

  #rightColumn {
    grid-template-columns: 1fr 1fr;
    gap: 7px;
  }

  #tools, #piPanel {
    min-width: 0;
  }

  #tools button, .actionsRow button {
    font-size: 11px;
    min-height: 34px;
    padding: 8px 6px;
  }

  #playerName { font-size: 13px; }

  .inventory { font-size: 10px; }

  .inventory span {
    padding: 3px 6px;
  }
}


/* v4 perspective/mobile-pro HUD */
@media (max-width: 720px) {
  #hud {
    position: fixed;
    inset: 0;
    display: block;
    pointer-events: none;
    z-index: 30;
  }

  #stats {
    position: fixed;
    top: max(8px, env(safe-area-inset-top));
    left: max(8px, env(safe-area-inset-left));
    width: 148px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 10px;
    padding: 9px 10px;
    border-radius: 18px;
    box-sizing: border-box;
  }

  #stats .stat {
    min-width: 0;
  }

  #stats .stat:nth-child(3) {
    grid-column: 1 / -1;
  }

  .label {
    font-size: 8px;
    letter-spacing: 0.14em;
  }

  .value {
    margin-top: 5px;
    font-size: 19px;
    line-height: 0.95;
  }

  #rightColumn {
    position: static;
    width: auto;
    display: block;
    pointer-events: none;
  }

  #piPanel {
    position: fixed;
    top: max(8px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
    width: 154px;
    display: block;
    padding: 8px;
    border-radius: 18px;
    box-sizing: border-box;
    pointer-events: auto;
  }

  #playerMeta {
    display: block;
  }

  #playerName {
    font-size: 12px;
    line-height: 1.05;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #piStatus {
    display: none;
  }

  .inventory {
    margin-top: 6px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    font-size: 9px;
    line-height: 1;
  }

  .inventory span {
    padding: 3px 5px;
  }

  .actionsRow {
    margin-top: 7px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .actionsRow button {
    width: 100%;
    min-height: 32px;
    padding: 7px 5px;
    font-size: 10px;
  }

  #tools {
    position: fixed;
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
    bottom: max(62px, calc(env(safe-area-inset-bottom) + 62px));
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
    padding: 7px;
    border-radius: 18px;
    box-sizing: border-box;
    pointer-events: auto;
  }

  #tools button {
    width: 100%;
    min-height: 34px;
    padding: 7px 4px;
    font-size: 10px;
  }

  #hint {
    bottom: max(8px, env(safe-area-inset-bottom));
    max-width: min(290px, calc(100vw - 16px));
    padding: 8px 10px;
    font-size: 12px;
    line-height: 1.35;
    border-radius: 16px;
  }
}

@media (max-width: 420px) {
  #stats { width: 142px; }
  #piPanel { width: 148px; }
  #tools { gap: 5px; padding: 6px; }
  #tools button { font-size: 9.5px; min-height: 32px; padding: 6px 3px; }
  .inventory span { padding: 3px 4px; }
}
