:root {
  --bg: #ece6d8;
  --surface: #fbf8f1;
  --surface-2: #f1ebdd;
  --ink: #17181a;
  --muted: #6f6a5d;
  --border: rgba(23, 24, 26, 0.12);
  --accent: #f2541b;
  --accent-ink: #fff7f0;
  --win: #0f9d58;
  --lose: #d33a2c;
  --gold: #c9962b;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-quart: cubic-bezier(0.16, 1, 0.3, 1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --r0: #8d9096;
  --r1: #2f7fd6;
  --r2: #7b4fd1;
  --r3: #d6409f;
  --r4: #d8362b;
  --r5: #c9962b;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: var(--tg-viewport-stable-height, 100dvh);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.35;
}

input, textarea, [contenteditable="true"] { user-select: text; font-size: 16px; }

button { font: inherit; color: inherit; cursor: pointer; }

.num { font-family: "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }

#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;
  transition: transform 280ms var(--ease-quart), filter 280ms var(--ease-quart);
}

.app-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand__mark {
  width: 22px; height: 22px;
  background: var(--accent);
  clip-path: polygon(0 100%, 0 42%, 42% 42%, 42% 0, 100% 0, 100% 100%);
  transform: rotate(180deg);
}
.brand__name {
  font-family: "Unbounded", system-ui, sans-serif;
  font-weight: 800;
  font-size: 15px;
  letter-spacing: 0.08em;
}

.balance {
  display: inline-flex; align-items: center; gap: 7px;
  height: 38px; padding: 0 8px 0 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-weight: 700;
  font-size: 15px;
}
.balance svg { width: 17px; height: 17px; color: var(--gold); }
.balance__plus {
  display: grid; place-items: center;
  width: 22px; height: 22px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  line-height: 1;
}

.app-viewport { flex: 1; position: relative; min-height: 0; overflow: hidden; }

.view {
  position: absolute; inset: 0;
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  scrollbar-gutter: stable;
}
.view[hidden] { display: none; }
.view__pad { padding: 4px 16px 28px; display: flex; flex-direction: column; gap: 12px; }

@keyframes paneFromRight { from { opacity: 0; transform: translate3d(16px, 0, 0); } to { opacity: 1; transform: translate3d(0, 0, 0); } }
@keyframes paneFromLeft  { from { opacity: 0; transform: translate3d(-16px, 0, 0); } to { opacity: 1; transform: translate3d(0, 0, 0); } }
.view.enter-right > .view__pad { animation: paneFromRight 220ms var(--ease-out) both; }
.view.enter-left  > .view__pad { animation: paneFromLeft 220ms var(--ease-out) both; }

.pressable { transition: transform 180ms var(--ease-out), opacity 180ms var(--ease-out), background-color 180ms var(--ease-out); will-change: transform; }
.pressable:active:not(:disabled) { transform: scale(0.972); transition-duration: 80ms; }
@media (hover: hover) { .pressable:hover:not(:disabled) { opacity: 0.92; } }

.tabbar {
  flex-shrink: 0;
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--tabs, 4), 1fr);
  padding: 6px 8px calc(8px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 20;
}
.tabbar__pill {
  position: absolute;
  top: 6px; left: 8px;
  width: calc((100% - 16px) / var(--tabs, 4));
  height: 50px;
  border-radius: 14px;
  background: var(--surface-2);
  transform: translate3d(calc(var(--active, 0) * 100%), 0, 0);
  transition: transform 240ms var(--ease-out);
  pointer-events: none;
}
.tab {
  position: relative; z-index: 1;
  height: 50px;
  border: 0; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px;
  font-size: 10.5px; font-weight: 600;
  color: var(--muted);
  transition: color 180ms var(--ease-out);
  padding: 0;
}
.tab svg { width: 21px; height: 21px; transition: transform 200ms var(--ease-out); }
.tab.is-active { color: var(--ink); }
.tab.is-active svg { transform: translate3d(0, -1px, 0); color: var(--accent); }
body.kb-open .tabbar { display: none; }

.h2 {
  margin: 6px 0 0;
  font-family: "Unbounded", system-ui, sans-serif;
  font-size: 13px; font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ---------- Wheel ---------- */
.wheel-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 14px 14px 14px;
  display: flex; flex-direction: column; gap: 12px;
}
.wheel { position: relative; width: min(232px, 62vw); aspect-ratio: 1; margin: 2px auto 0; }
.wheel__svg { width: 100%; height: 100%; display: block; }
.wheel__track { fill: none; stroke: #e3dccb; stroke-width: 20; }
.wheel__zone {
  fill: none; stroke: var(--win); stroke-width: 20;
  stroke-dasharray: 0 100;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
  transition: stroke-dasharray 420ms var(--ease-quart), stroke 200ms var(--ease-out);
}
.wheel.is-lose .wheel__zone { stroke: var(--lose); }
.wheel.is-idle .wheel__zone { stroke: #c9c1ad; }
#wheel-ticks line { stroke: rgba(23, 24, 26, 0.3); stroke-width: 1.5; stroke-linecap: round; }
.wheel__center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  pointer-events: none;
}
.wheel__label { font-size: 10px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); }
.wheel__chance { font-family: "Unbounded", system-ui, sans-serif; font-weight: 800; font-size: 30px; line-height: 1.1; }
.wheel__pointer {
  position: absolute; top: -7px; left: 50%;
  width: 0; height: 0;
  margin-left: -9px;
  border-left: 9px solid transparent;
  border-right: 9px solid transparent;
  border-top: 18px solid var(--ink);
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.25));
}

.duel { display: grid; grid-template-columns: 1fr 28px 1fr; align-items: center; gap: 6px; }
.duel__arrow { display: grid; place-items: center; color: var(--muted); }
.duel__arrow svg { width: 22px; height: 22px; }
.duel__side { min-width: 0; }
.slot {
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px;
  border-radius: 16px;
  background: var(--surface-2);
  border: 1px dashed rgba(23, 24, 26, 0.18);
  min-height: 104px;
  transition: background-color 180ms var(--ease-out), border-color 180ms var(--ease-out);
}
.slot.is-filled { border-style: solid; border-color: transparent; background: var(--surface-2); }
.slot__caption { font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.slot__empty { margin: auto; font-size: 12px; color: var(--muted); text-align: center; }
.slot__name { font-weight: 700; font-size: 12.5px; line-height: 1.2; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.slot__price { font-size: 12px; color: var(--muted); }

.art {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: linear-gradient(135deg, var(--rc, var(--r0)) 0%, var(--rc2, var(--r0)) 100%);
  display: grid; place-items: center;
}
.art::after {
  content: "";
  position: absolute; inset: 0;
  background: radial-gradient(120% 80% at 50% 100%, rgba(0, 0, 0, 0.28), transparent 60%);
  pointer-events: none;
}
.art svg { position: relative; z-index: 1; width: 82%; height: 82%; filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.35)); }
.art--sm { height: 58px; }
.art--md { height: 92px; }
.art--lg { height: 132px; border-radius: 16px; }

.cta {
  height: 50px; width: 100%;
  border: 0; border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-family: "Unbounded", system-ui, sans-serif;
  font-weight: 700; font-size: 13px;
  letter-spacing: 0.02em;
}
.cta:disabled { background: #cfc8b6; color: #8a8474; cursor: default; }
.ghost {
  height: 50px; width: 100%;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  font-weight: 700;
}

/* ---------- Segmented ---------- */
.seg {
  position: relative;
  display: inline-flex; width: 100%;
  padding: 3px;
  border-radius: 13px;
  background: var(--surface-2);
}
.seg__thumb {
  position: absolute;
  top: 3px; bottom: 3px; left: 0;
  width: var(--seg-w, 0px);
  transform: translate3d(var(--seg-x, 3px), 0, 0);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: transform 220ms var(--ease-out), width 220ms var(--ease-out);
}
.seg__btn {
  position: relative; z-index: 1;
  flex: 1; height: 36px;
  border: 0; background: none;
  font-size: 13px; font-weight: 600;
  color: var(--muted);
  transition: color 180ms var(--ease-out);
}
.seg__btn.is-active { color: var(--ink); }

.pane { display: flex; flex-direction: column; gap: 10px; }
.pane[hidden] { display: none; }

/* ---------- Cards ---------- */
.strip { display: flex; gap: 10px; overflow-x: auto; padding: 2px 2px 6px; scroll-snap-type: x proximity; scrollbar-width: none; }
.strip::-webkit-scrollbar { display: none; }

.card {
  flex: 0 0 124px;
  scroll-snap-align: start;
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px;
  border-radius: 16px;
  background: var(--surface);
  border: 1.5px solid transparent;
  box-shadow: 0 0 0 1px var(--border);
  text-align: left;
  transition: transform 180ms var(--ease-out), border-color 180ms var(--ease-out);
}
.card.is-selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.card__name { font-weight: 700; font-size: 12px; line-height: 1.2; min-height: 29px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card__meta { display: flex; align-items: center; justify-content: space-between; gap: 6px; font-size: 11.5px; color: var(--muted); }
.card__price { font-weight: 700; color: var(--ink); }
.rar { font-size: 10px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: var(--rc, var(--r0)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.targets { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.targets .card { flex: initial; }
.targets .card.is-locked { opacity: 0.5; }

.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.grid .card { flex: initial; }

@keyframes cardRise { from { opacity: 0; transform: translate3d(0, 10px, 0); } to { opacity: 1; transform: translate3d(0, 0, 0); } }
.stagger-list > * { animation: cardRise 240ms var(--ease-out) both; animation-delay: calc(min(var(--i, 0), 8) * 32ms); }

.rarity-bar { display: flex; gap: 6px; overflow-x: auto; padding: 2px 2px 4px; scrollbar-width: none; }
.rarity-bar::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  height: 32px; padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 12px; font-weight: 600;
  color: var(--muted);
  transition: background-color 180ms var(--ease-out), color 180ms var(--ease-out);
}
.chip.is-active { background: var(--ink); color: var(--bg); border-color: var(--ink); }

.coins-row { display: grid; grid-template-columns: 48px 1fr 48px; gap: 8px; }
.step {
  height: 48px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  font-size: 22px; font-weight: 600;
  line-height: 1;
}
.coins-input {
  height: 48px; width: 100%;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  text-align: center;
  font-weight: 700;
  color: var(--ink);
  outline: none;
  -moz-appearance: textfield;
}
.coins-input::-webkit-outer-spin-button, .coins-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.coins-input:focus { border-color: var(--accent); }
.chips { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.chips .chip { height: 36px; padding: 0; }

/* ---------- Inventory ---------- */
.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.summary__cell {
  display: flex; flex-direction: column; gap: 2px;
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.summary__label { font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.summary__value { font-family: "Unbounded", system-ui, sans-serif; font-weight: 700; font-size: 18px; }
.card__actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 2px; }
.mini {
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 11.5px; font-weight: 700;
}
.mini--accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.empty {
  grid-column: 1 / -1;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 30px 16px;
  border: 1px dashed rgba(23, 24, 26, 0.2);
  border-radius: 18px;
  color: var(--muted);
  text-align: center;
}
.empty svg { width: 34px; height: 34px; opacity: 0.6; }

/* ---------- Profile ---------- */
.me { display: flex; align-items: center; gap: 12px; }
.me__avatar {
  width: 52px; height: 52px; flex-shrink: 0;
  border-radius: 16px;
  background: var(--ink);
  color: var(--bg);
  display: grid; place-items: center;
  font-family: "Unbounded", system-ui, sans-serif;
  font-weight: 800; font-size: 20px;
}
.me__info { display: flex; flex-direction: column; min-width: 0; }
.me__name { font-family: "Unbounded", system-ui, sans-serif; font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me__sub { color: var(--muted); font-size: 13px; }

.bonus, .topup {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  text-align: left;
}
.bonus__icon {
  width: 42px; height: 42px; flex-shrink: 0;
  border-radius: 13px;
  background: var(--surface-2);
  color: var(--accent);
  display: grid; place-items: center;
}
.bonus__icon svg { width: 22px; height: 22px; }
.bonus__text, .topup__text { display: flex; flex-direction: column; gap: 1px; min-width: 0; flex: 1; }
.bonus__title, .topup__title { font-weight: 700; }
.bonus__sub, .topup__sub { font-size: 12.5px; color: var(--muted); }
.bonus__cta, .topup__cta {
  flex-shrink: 0;
  height: 34px; padding: 0 14px;
  display: inline-flex; align-items: center;
  border-radius: 11px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700; font-size: 12.5px;
}
.bonus.is-waiting .bonus__cta { background: var(--surface-2); color: var(--muted); }
.topup__cta { background: var(--ink); color: var(--bg); }

.pbar { height: 6px; border-radius: 99px; background: rgba(23, 24, 26, 0.1); overflow: hidden; margin-top: -4px; }
.pbar__fill { height: 100%; width: 100%; border-radius: inherit; background: var(--accent); transform-origin: left center; transform: scaleX(var(--p, 0)); transition: transform 380ms var(--ease-quart); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { display: flex; flex-direction: column; gap: 2px; padding: 10px 12px; border-radius: 14px; background: var(--surface); border: 1px solid var(--border); }
.stat__label { font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.stat__value { font-family: "Unbounded", system-ui, sans-serif; font-weight: 700; font-size: 15px; }

.leaders, .history { display: flex; flex-direction: column; gap: 6px; }
.row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.row.is-me { border-color: var(--accent); }
.row__rank { width: 22px; font-family: "JetBrains Mono", monospace; font-weight: 700; color: var(--muted); font-size: 13px; }
.row__main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row__title { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row__sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row__value { font-weight: 700; font-size: 13px; }
.row__value.is-win { color: var(--win); }
.row__value.is-lose { color: var(--lose); }

/* ---------- Sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(23, 24, 26, 0.46);
  opacity: 0; pointer-events: none;
  transition: opacity 240ms var(--ease-out);
}
.sheet-backdrop.is-open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  max-height: 88%;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-radius: 24px 24px 0 0;
  padding: 8px 16px calc(20px + var(--safe-bottom));
  transform: translate3d(0, 100%, 0);
  transition: transform 300ms var(--ease-quart);
}
.sheet.is-open { transform: translate3d(0, 0, 0); }
.sheet.is-dragging { transition: none; }
.sheet__handle { width: 36px; height: 4px; border-radius: 99px; background: rgba(23, 24, 26, 0.25); margin: 4px auto 12px; flex-shrink: 0; }
.sheet__body { overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 10px; }
.sheet__title { margin: 0; font-family: "Unbounded", system-ui, sans-serif; font-size: 17px; font-weight: 700; }
.sheet__sub, .sheet__note { margin: 0; font-size: 12.5px; color: var(--muted); }
.sheet__calc { margin: 0; font-weight: 700; text-align: center; }
.packs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.pack {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 10px 6px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-2);
  transition: background-color 180ms var(--ease-out), border-color 180ms var(--ease-out);
}
.pack.is-active { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.pack__stars { font-family: "Unbounded", system-ui, sans-serif; font-weight: 700; font-size: 14px; }
.pack__coins { font-size: 11.5px; opacity: 0.75; }

/* ---------- Result ---------- */
.result { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 20px; }
.result[hidden] { display: none; }
.result__backdrop { position: absolute; inset: 0; background: rgba(23, 24, 26, 0.6); animation: fadeIn 200ms var(--ease-out) both; }
.result__card {
  position: relative;
  width: min(340px, 100%);
  display: flex; flex-direction: column; gap: 10px;
  padding: 16px;
  border-radius: 24px;
  background: var(--surface);
  animation: cardRise 280ms var(--ease-quart) both;
}
.result__tag { font-size: 10px; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.result.is-win .result__tag { color: var(--win); }
.result.is-lose .result__tag { color: var(--lose); }
.result__title { margin: 0; font-family: "Unbounded", system-ui, sans-serif; font-size: 18px; font-weight: 700; line-height: 1.2; }
.result__sub { margin: 0; color: var(--muted); font-size: 13px; }
.result__actions { display: grid; grid-template-columns: 1fr 1.4fr; gap: 8px; margin-top: 4px; }
.result.is-lose .result__actions { grid-template-columns: 1fr; }
.result.is-lose #result-sell { display: none; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.toast {
  position: fixed;
  top: calc(10px + var(--safe-top));
  left: 50%; z-index: 80;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 13px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  opacity: 0;
  transform: translate3d(-50%, -14px, 0) scale(0.96);
  pointer-events: none;
  transition: opacity 200ms var(--ease-out), transform 220ms var(--ease-out);
}
.toast.is-shown { opacity: 1; transform: translate3d(-50%, 0, 0) scale(1); }

@media (max-width: 340px) {
  .targets, .grid { grid-template-columns: 1fr; }
  .chips { grid-template-columns: repeat(2, 1fr); }
}

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