/* ==========================================================================
   功夫牛牛 mini app — design system
   --------------------------------------------------------------------------
   Palette, radii and type scale are sampled directly from the live reference
   app (Android capture, .scratch/ref2/*.png) rather than guessed, so surfaces
   sit at the same lightness. Sampled values:

     page bg  #1c1d22    card  #25262b    chip  #343539
     nav pill #292a30    primary #3b82f6  link  #60a5fa   reward #f59e0b

   The live app is built on Tailwind's default scale (blue-500 / blue-400 /
   amber-500), so the ramps below follow it too — that is why the numbers look
   familiar rather than arbitrary.
   ========================================================================== */

:root {
  /* --- surfaces (sampled) --- */
  --bg: #1c1d22;
  --surface: #25262b;
  --surface-2: #2b2c31;
  --surface-3: #343539;
  --nav-pill: rgba(41, 42, 48, .92);
  --border: rgba(255, 255, 255, .07);
  --border-strong: rgba(255, 255, 255, .13);
  --divider: #303136;

  /* --- text --- */
  --text: #ffffff;
  --text-2: #d4d5d9;
  --text-dim: #8e8f94;
  --text-mute: #6b6c71;

  /* --- accents --- */
  --blue: #3b82f6;
  --blue-light: #60a5fa;
  --blue-deep: #2563eb;
  --amber: #f59e0b;
  --amber-light: #fbbf24;
  --green: #34d399;
  --red: #ef4444;
  --gold: #f5c453;

  /* --- brand (kfnn-brand tokens, used for artwork surrounds only) --- */
  --brand-purple: #4c1d95;
  --brand-violet: #7c3aed;

  /* --- geometry --- */
  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 18px;
  --r-xl: 22px;
  --r-pill: 999px;

  /* --- motion ---
     One easing for entrances (decelerate) and one for exits. Durations are
     deliberately short: the live app never makes you wait for a transition. */
  --ease-out: cubic-bezier(.22, .61, .36, 1);
  --ease-in-out: cubic-bezier(.4, 0, .2, 1);
  --ease-spring: cubic-bezier(.34, 1.4, .64, 1);
  --t-fast: 120ms;
  --t-base: 220ms;
  --t-slow: 320ms;

  /* --- safe areas (populated by tg.js; sane fallbacks for a browser) --- */
  --safe-top: 0px;
  --safe-bottom: 0px;
  --safe-left: 0px;
  --safe-right: 0px;
  --content-top: 0px;
  --vh: 100dvh;

  --header-h: 52px;
  --nav-h: 64px;
}

/* ==========================================================================
   Reset
   ========================================================================== */

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

html {
  background: var(--bg);
  /* Stop the whole document rubber-banding; scroll belongs to #main. */
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
               'Hiragino Sans GB', 'Microsoft YaHei', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
  /* Telegram sends a synthetic 300ms tap delay without this. */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* The `hidden` attribute is a UA rule of the same specificity as any class, so a
   component that sets `display: flex` silently defeats it — which is exactly how
   the locked composer and the quick-bet chips stayed on screen. Make it win. */
[hidden] { display: none !important; }

a { color: inherit; text-decoration: none; }
button, input, textarea, select { font: inherit; color: inherit; }
button { border: none; background: none; cursor: pointer; }
input, textarea { background: none; border: none; outline: none; }
img, svg { display: block; max-width: 100%; }
ul, ol { list-style: none; }

/* Icons inherit colour and never shrink in a flex row. */
.icon { flex: none; display: block; }

/* Locally generated avatars (see js/avatar.js). */
.avatar-img,
.avatar-gen {
  border-radius: 50%;
  object-fit: cover;
  flex: none;
  background: var(--surface-3);
}
.avatar-gen {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  line-height: 1;
  user-select: none;
}

/* ==========================================================================
   App shell
   ========================================================================== */

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  position: relative;
  /* Fullscreen draws under the status bar — inset the whole shell instead of
     patching each page. */
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

#main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  /* Keep scroll momentum inside the page and off Telegram's dismiss gesture. */
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 0 calc(var(--nav-h) + var(--safe-bottom) + 24px);
  scrollbar-width: none;
}
#main::-webkit-scrollbar { width: 0; height: 0; }

/* Pages that own the full viewport (game room, support chat) opt out of the
   nav gutter and manage their own scrolling. */
#main.is-immersive { padding-bottom: 0; overflow: hidden; }

.page { padding: 0 16px; }
.page-wide { padding: 0; }

/* ==========================================================================
   Header — mirrors the live app: a centred title with a hairline under it.
   Back/Close are Telegram's own controls, so we draw no chrome of our own
   unless we are running in a plain browser.
   ========================================================================== */

#header {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--header-h);
  padding: 10px 12px;
  padding-top: calc(10px + var(--content-top));
  background: var(--bg);
  border-bottom: 1px solid var(--divider);
  transition: border-color var(--t-base) var(--ease-out);
}
#header.is-flat { border-bottom-color: transparent; }

.header-title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: .01em;
  text-align: center;
  /* Cross-fades on route change (see .header-title.swapping). */
  transition: opacity var(--t-fast) var(--ease-out);
}
.header-title.swapping { opacity: 0; }

.header-back,
.header-action {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  margin-top: calc(var(--content-top) / 2);
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-pill);
  color: var(--text-2);
  transition: background var(--t-fast) var(--ease-out),
              transform var(--t-fast) var(--ease-out);
}
.header-back { left: 8px; }
.header-action { right: 8px; }
.header-back:active,
.header-action:active { background: var(--surface-2); transform: translateY(-50%) scale(.9); }

/* Inside Telegram the native BackButton handles this, so hide ours. */
.is-telegram .header-back { display: none; }
.header-back[hidden] { display: none; }

/* ==========================================================================
   Bottom navigation — a floating translucent pill, not a full-width bar.
   ========================================================================== */

#nav {
  position: fixed;
  left: 50%;
  bottom: calc(12px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 70;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  border-radius: var(--r-pill);
  background: var(--nav-pill);
  border: 1px solid var(--border);
  backdrop-filter: blur(20px) saturate(160%);
  -webkit-backdrop-filter: blur(20px) saturate(160%);
  box-shadow: 0 8px 28px rgba(0, 0, 0, .45);
  transition: opacity var(--t-base) var(--ease-out),
              transform var(--t-base) var(--ease-out);
}
/* The nav animates out rather than disappearing, so it uses a CLASS — never the
   hidden attribute, which is now display:none !important above. */
#nav.is-hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(120%);
}

.nav-item {
  position: relative;
  width: 66px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 4px 0 2px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-mute);
  border-radius: var(--r-md);
  transition: color var(--t-base) var(--ease-out),
              transform var(--t-fast) var(--ease-out);
}
.nav-item:active { transform: scale(.92); }
.nav-item.active { color: var(--text); }

/* Both glyph variants are rendered; we cross-fade rather than re-render so the
   swap cannot drop a frame. */
.nav-glyph { position: relative; display: block; width: 26px; height: 26px; }
.nav-glyph .icon {
  position: absolute;
  inset: 0;
  transition: opacity var(--t-base) var(--ease-out),
              transform var(--t-base) var(--ease-spring);
}
.nav-glyph .icon-solid { opacity: 0; transform: scale(.82); }
.nav-item.active .nav-glyph .icon:not(.icon-solid) { opacity: 0; transform: scale(1.1); }
.nav-item.active .nav-glyph .icon-solid { opacity: 1; transform: scale(1); }

/* ==========================================================================
   Motion primitives
   ========================================================================== */

/* Route change: content lifts in. Applied by app.js on every render. */
@keyframes page-in {
  from { opacity: 0; transform: translate3d(0, 10px, 0); }
  to   { opacity: 1; transform: none; }
}
.page-enter { animation: page-in var(--t-base) var(--ease-out) both; }

/* Staggered list reveal — each row is 26ms behind the one above it. */
@keyframes row-in {
  from { opacity: 0; transform: translate3d(0, 8px, 0); }
  to   { opacity: 1; transform: none; }
}
.stagger > * {
  animation: row-in var(--t-base) var(--ease-out) both;
}
.stagger > *:nth-child(1) { animation-delay: 0ms; }
.stagger > *:nth-child(2) { animation-delay: 26ms; }
.stagger > *:nth-child(3) { animation-delay: 52ms; }
.stagger > *:nth-child(4) { animation-delay: 78ms; }
.stagger > *:nth-child(5) { animation-delay: 104ms; }
.stagger > *:nth-child(6) { animation-delay: 130ms; }
.stagger > *:nth-child(7) { animation-delay: 156ms; }
.stagger > *:nth-child(8) { animation-delay: 182ms; }
.stagger > *:nth-child(n+9) { animation-delay: 200ms; }

/* Anything tappable gets the same press response. */
.pressable { transition: transform var(--t-fast) var(--ease-out), background var(--t-fast) var(--ease-out); }
.pressable:active { transform: scale(.97); }

/* Skeletons beat spinners: the layout does not jump when data lands. */
@keyframes shimmer { to { background-position-x: -200%; } }
.skeleton {
  background: linear-gradient(90deg, var(--surface) 40%, var(--surface-3) 50%, var(--surface) 60%);
  background-size: 200% 100%;
  animation: shimmer 1.3s linear infinite;
  border-radius: var(--r-sm);
  color: transparent !important;
  pointer-events: none;
}
.skeleton-text { height: 12px; margin: 6px 0; }
.skeleton-row { height: 64px; margin-bottom: 10px; border-radius: var(--r-lg); }

@keyframes spin { to { transform: rotate(360deg); } }
.loading { display: flex; align-items: center; justify-content: center; padding: 48px 0; }
.loading-spinner {
  width: 26px; height: 26px;
  border: 2.5px solid var(--surface-3);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

/* Count-up / value change pulse. */
@keyframes value-pop {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.06); }
  100% { transform: scale(1); }
}
.value-pop { animation: value-pop 380ms var(--ease-spring); }

/* People who ask for less motion get none of it. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ==========================================================================
   Buttons
   ========================================================================== */

.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  height: 52px;
  padding: 0 20px;
  border-radius: var(--r-xl);
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  transition: transform var(--t-fast) var(--ease-out),
              filter var(--t-fast) var(--ease-out),
              background var(--t-fast) var(--ease-out),
              opacity var(--t-fast) var(--ease-out);
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .4; pointer-events: none; }

/* The live primary is a flat blue with a soft blue glow beneath — not a
   gradient. The glow is what makes it read as "the" action on the screen. */
.btn-primary {
  background: var(--blue);
  color: #fff;
  box-shadow: 0 6px 20px rgba(59, 130, 246, .38);
}
.btn-primary:active { filter: brightness(1.08); }

.btn-secondary {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border-strong);
}
.btn-secondary:active { background: var(--surface-2); }

.btn-tonal { background: var(--surface-3); color: var(--text); }
.btn-danger { background: var(--red); color: #fff; box-shadow: 0 6px 20px rgba(239, 68, 68, .3); }
.btn-gold {
  background: linear-gradient(135deg, #f7d774, #d9a441);
  color: #3a2a06;
  box-shadow: 0 6px 20px rgba(217, 164, 65, .32);
}

.btn-sm { height: 38px; font-size: 14px; border-radius: var(--r-md); padding: 0 16px; }
.btn-block { display: flex; }
.btn-row { display: flex; gap: 12px; }
.btn-row .btn { flex: 1; }

/* ==========================================================================
   Cards, sections, list rows
   ========================================================================== */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 16px;
  margin-bottom: 12px;
}
.card-flush { padding: 0; overflow: hidden; }

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 20px 0 10px;
}
.section-title { font-size: 17px; font-weight: 700; }
.section-sub-title { font-size: 13px; color: var(--text-dim); }
.section-link { font-size: 14px; color: var(--blue-light); font-weight: 500; }
.section-count {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--surface-2);
  border-radius: var(--r-pill);
  padding: 3px 11px;
}

/* Full-bleed grouped list, the pattern the live settings screen uses. */
.list { background: var(--surface); overflow: hidden; }
.list-group { margin-bottom: 10px; }
.list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 58px;
  padding: 12px 16px;
  background: var(--surface);
  position: relative;
  transition: background var(--t-fast) var(--ease-out);
}
.list-item:active { background: var(--surface-2); }
/* Hairline inset to match the text, not the card edge. */
.list-item + .list-item::before {
  content: '';
  position: absolute;
  top: 0; left: 16px; right: 0;
  height: 1px;
  background: var(--divider);
}
.list-item-body { flex: 1; min-width: 0; }
.list-item-title { font-size: 16px; font-weight: 500; }
.list-item-sub { font-size: 13px; color: var(--text-dim); margin-top: 2px; }
.list-item-right { display: flex; align-items: center; gap: 6px; color: var(--text-dim); font-size: 14px; }
.list-item .chev { color: var(--text-mute); }
.list-item.is-danger .list-item-title { color: var(--red); }

/* Circular tinted leading icon (transaction rows, reward rows). */
.row-icon {
  width: 42px; height: 42px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-3);
  color: var(--text-2);
  flex: none;
}
.row-icon.is-green { background: rgba(52, 211, 153, .14); color: var(--green); }
.row-icon.is-red   { background: rgba(239, 68, 68, .14);  color: var(--red); }
.row-icon.is-blue  { background: rgba(59, 130, 246, .16); color: var(--blue-light); }
.row-icon.is-gold  { background: rgba(245, 158, 11, .15); color: var(--amber); }

/* ==========================================================================
   Chips & tabs
   ========================================================================== */

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 16px;
  border-radius: var(--r-pill);
  background: var(--surface-3);
  color: var(--text-dim);
  font-size: 14px;
  white-space: nowrap;
  transition: background var(--t-fast) var(--ease-out),
              color var(--t-fast) var(--ease-out),
              transform var(--t-fast) var(--ease-out);
}
.chip:active { transform: scale(.95); }
/* Active filter chip in the live app is a solid WHITE pill with dark text. */
.chip.active { background: #fff; color: #17181c; font-weight: 600; }

.chip-scroller {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding: 12px 16px;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
  overscroll-behavior-x: contain;
}
.chip-scroller::-webkit-scrollbar { display: none; }
.chip-scroller > * { scroll-snap-align: start; }

/* Underline tabs. Accent colour is per-feature: amber for rewards, blue for
   leaderboards — matching the live app exactly. */
.tabs {
  position: relative;
  display: flex;
  border-bottom: 1px solid var(--divider);
}
.tab {
  flex: 1;
  padding: 15px 4px;
  text-align: center;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-dim);
  transition: color var(--t-base) var(--ease-out);
}
.tab.active { color: var(--accent, var(--blue-light)); font-weight: 700; }
/* One sliding underline rather than a border per tab, so it animates. */
.tab-underline {
  position: absolute;
  bottom: -1px;
  height: 2.5px;
  border-radius: 2px;
  background: var(--accent, var(--blue-light));
  transition: transform var(--t-base) var(--ease-in-out),
              width var(--t-base) var(--ease-in-out);
}
.tabs-amber { --accent: var(--amber); }
.tabs-blue  { --accent: var(--blue-light); }

/* ==========================================================================
   Carousel — the live lobby's 8-banner announcement rail.
   Drag-tracked, momentum-free (snaps), autoplays, pauses on touch.
   ========================================================================== */

.carousel { position: relative; overflow: hidden; border-radius: var(--r-lg); }
.carousel-track {
  display: flex;
  will-change: transform;
  touch-action: pan-y;
}
.carousel-track.is-animating { transition: transform 420ms var(--ease-in-out); }
.carousel-slide { flex: 0 0 100%; position: relative; }
.carousel-slide img,
.carousel-slide .slide-art {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: var(--r-lg);
  background: var(--surface);
  display: block;
  -webkit-user-drag: none;
  user-select: none;
}

.banner-dots {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 0 4px;
}
.banner-dots .dot {
  width: 6px; height: 6px;
  border-radius: var(--r-pill);
  background: var(--surface-3);
  transition: width var(--t-base) var(--ease-out),
              background var(--t-base) var(--ease-out);
}
/* Active dot stretches into a blue capsule, as in the live app. */
.banner-dots .dot.active { width: 22px; background: var(--blue); }

/* ==========================================================================
   Toast, modal, sheet
   ========================================================================== */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 26px);
  transform: translate(-50%, 8px) scale(.96);
  z-index: 300;
  max-width: 80%;
  padding: 11px 20px;
  border-radius: var(--r-md);
  background: rgba(20, 21, 25, .96);
  border: 1px solid var(--border);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  color: #fff;
  font-size: 14px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t-base) var(--ease-out),
              transform var(--t-base) var(--ease-spring);
}
#toast.show { opacity: 1; transform: translate(-50%, 0) scale(1); }

#modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  background: rgba(0, 0, 0, .6);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity var(--t-base) var(--ease-out);
}
#modal-overlay.show { display: flex; opacity: 1; }

@keyframes sheet-up {
  from { transform: translate3d(0, 100%, 0); }
  to   { transform: none; }
}
.modal {
  width: 100%;
  max-width: 520px;
  max-height: 88dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface);
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  border-top: 1px solid var(--border);
  padding: 20px 18px calc(20px + var(--safe-bottom));
  animation: sheet-up var(--t-slow) var(--ease-out) both;
}
/* Grab handle, so it reads as a sheet you can dismiss. */
.modal::before {
  content: '';
  display: block;
  width: 38px; height: 4px;
  margin: -6px auto 14px;
  border-radius: var(--r-pill);
  background: var(--surface-3);
}
.modal h3 { font-size: 17px; font-weight: 700; margin-bottom: 12px; }
.modal-input {
  width: 100%;
  height: 50px;
  padding: 0 15px;
  margin-bottom: 12px;
  border-radius: var(--r-md);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 16px;
  transition: border-color var(--t-fast) var(--ease-out);
}
.modal-input:focus { border-color: var(--blue); }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions .btn { flex: 1; }

/* ==========================================================================
   Form controls
   ========================================================================== */

.toggle {
  width: 50px; height: 30px;
  border-radius: var(--r-pill);
  background: var(--surface-3);
  position: relative;
  flex: none;
  transition: background var(--t-base) var(--ease-out);
}
.toggle::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .35);
  transition: transform var(--t-base) var(--ease-spring);
}
.toggle.on { background: var(--blue); }
.toggle.on::after { transform: translateX(20px); }

.radio-dot {
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  position: relative;
  flex: none;
  transition: border-color var(--t-fast) var(--ease-out);
}
.radio-dot.checked { border-color: var(--blue); }
.radio-dot.checked::after {
  content: '';
  position: absolute;
  inset: 3.5px;
  border-radius: 50%;
  background: var(--blue);
  animation: value-pop 260ms var(--ease-spring);
}

/* ==========================================================================
   Empty & error states
   ========================================================================== */

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 52px 20px;
  color: var(--text-mute);
  font-size: 14px;
}
.empty .icon { width: 52px; height: 52px; opacity: .3; }
.error { color: var(--red); font-size: 14px; padding: 16px; }
