:root {
  --bg1: #1b1330;
  --bg2: #2c1b4a;
  --gold: #f5c542;
  --gold-dark: #a97e15;
  --card: #2a2144;
  --card-light: #362a5a;
  --text: #f1eaff;
  --text-dim: #b7a9d9;
  --win: #4fd67a;
  --lose: #ff6b7a;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(circle at 50% 0%, var(--bg2), var(--bg1) 70%);
  color: var(--text);
  font-family: "Hiragino Sans", "Yu Gothic", "Segoe UI", sans-serif;
  overscroll-behavior: none;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100vh;
  padding: 0 0 40px;
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  position: sticky;
  top: 0;
  background: linear-gradient(180deg, rgba(27,19,48,0.97), rgba(27,19,48,0.85));
  backdrop-filter: blur(6px);
  z-index: 20;
  border-bottom: 1px solid rgba(245,197,66,0.2);
}

.topbar h1 {
  font-size: 19px;
  margin: 0;
  letter-spacing: 0.5px;
}
.topbar h1 span {
  color: var(--gold);
  font-weight: 900;
}

.coinBadge {
  display: flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(180deg, #3a2c5c, #241a3a);
  border: 1px solid rgba(245,197,66,0.4);
  border-radius: 999px;
  padding: 6px 14px;
  font-weight: 700;
  font-size: 15px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.08);
}
.coinIcon { font-size: 15px; }
#coinCount { color: var(--gold); font-variant-numeric: tabular-nums; }

#toast {
  position: fixed;
  top: 66px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  pointer-events: none;
}
.toastItem {
  background: rgba(20,14,36,0.95);
  border: 1px solid rgba(245,197,66,0.4);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  animation: toastIn 0.25s ease, toastOut 0.4s ease 1.8s forwards;
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
}
@keyframes toastIn { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(-8px); } }

main { padding: 14px 16px; display: flex; flex-direction: column; gap: 18px; }

.freeBanner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(135deg, #ffdd7a, #f5a742);
  color: #3a2500;
  border-radius: 14px;
  padding: 12px 16px;
  font-weight: 700;
  box-shadow: 0 6px 18px rgba(245,167,66,0.35);
}
.freeBtn {
  background: #3a2500;
  color: #ffdd7a;
  border: none;
  border-radius: 999px;
  padding: 8px 16px;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}
.hidden { display: none !important; }

.shop {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.tierCard {
  background: linear-gradient(160deg, var(--card-light), var(--card));
  border-radius: 16px;
  border: 1px solid rgba(255,255,255,0.06);
  padding: 12px 8px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.tierCard:active { transform: scale(0.96); }
.tierCard.affordable:hover { box-shadow: 0 0 0 2px var(--tier-color, var(--gold)); }
.tierCard.disabled { opacity: 0.45; cursor: not-allowed; }
.tierIcon { font-size: 30px; }
.tierName { font-weight: 800; font-size: 13px; color: var(--tier-color, var(--gold)); }
.tierCost { font-size: 12px; color: var(--text-dim); font-weight: 700; }
.tierJackpot { font-size: 10px; color: var(--text-dim); }

.cardArea {
  background: linear-gradient(160deg, var(--card-light), var(--card));
  border-radius: 20px;
  padding: 16px;
  border: 1px solid rgba(255,255,255,0.07);
  box-shadow: 0 10px 30px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.cardHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.cardTitle { font-weight: 800; font-size: 15px; }
.cardBackBtn {
  background: rgba(255,255,255,0.08);
  border: none;
  color: var(--text);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}

.legend {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.legendItem {
  flex: 0 0 auto;
  background: rgba(0,0,0,0.25);
  border-radius: 10px;
  padding: 6px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 52px;
  font-size: 11px;
}
.legendItem .sym { font-size: 18px; }
.legendItem .prize { color: var(--gold); font-weight: 700; margin-top: 2px; }

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.cell {
  position: relative;
  aspect-ratio: 1;
  border-radius: 12px;
  overflow: hidden;
  background: #1b1330;
  touch-action: none;
}
.cellReveal {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle, #3a2c5c, #1e1638);
}
.cellReveal .sym { font-size: 30px; }
.cellReveal .prize { font-size: 11px; color: var(--gold); font-weight: 700; margin-top: 2px; }
.cell canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}
.cell.done canvas { pointer-events: none; }

.cardControls {
  display: flex;
  gap: 10px;
}
.btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 11px 10px;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}
.btnScratchAll {
  background: rgba(255,255,255,0.1);
  color: var(--text);
}
.btnAgain {
  background: linear-gradient(135deg, var(--gold), var(--gold-dark));
  color: #2a1c00;
}
.btnAgain:disabled { opacity: 0.4; cursor: not-allowed; }

.resultBanner {
  text-align: center;
  border-radius: 14px;
  padding: 14px;
  font-weight: 800;
  font-size: 16px;
  animation: popIn 0.35s ease;
}
.resultBanner.win { background: rgba(79,214,122,0.15); color: var(--win); border: 1px solid rgba(79,214,122,0.4); }
.resultBanner.lose { background: rgba(255,107,122,0.1); color: var(--lose); border: 1px solid rgba(255,107,122,0.3); }
@keyframes popIn { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.statsRow {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 8px;
  align-items: stretch;
}
.statBox {
  background: rgba(255,255,255,0.05);
  border-radius: 12px;
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.statLabel { font-size: 10px; color: var(--text-dim); }
.statBox span#statBest, .statBox span#statNet, .statBox span#statTickets { font-weight: 800; font-size: 14px; }
.resetBtn {
  background: rgba(255,107,122,0.12);
  border: 1px solid rgba(255,107,122,0.3);
  color: var(--lose);
  border-radius: 12px;
  width: 40px;
  font-size: 15px;
  cursor: pointer;
}

.historyWrap h2 {
  font-size: 13px;
  color: var(--text-dim);
  margin: 0 0 8px;
  font-weight: 700;
}
.historyList {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 260px;
  overflow-y: auto;
}
.historyList li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255,255,255,0.04);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 12px;
}
.historyList .hTier { color: var(--text-dim); }
.historyList .hAmt.win { color: var(--win); font-weight: 700; }
.historyList .hAmt.lose { color: var(--text-dim); }
.historyEmpty { color: var(--text-dim); font-size: 12px; text-align: center; padding: 10px; }

#confettiLayer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 200;
}
.confetti {
  position: absolute;
  top: -20px;
  font-size: 22px;
  animation: confettiFall linear forwards;
}
@keyframes confettiFall {
  to { transform: translateY(110vh) rotate(360deg); opacity: 0.9; }
}

@media (max-width: 420px) {
  .tierIcon { font-size: 24px; }
  .cellReveal .sym { font-size: 24px; }
}
