/* Reset + app shell + primitives */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
img, svg, video { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
h1, h2, h3, h4, p { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }
table { border-collapse: collapse; }
[hidden] { display: none !important; }
.num { font-family: var(--font-num); font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }
.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
:focus-visible { outline: 2px solid var(--cheer); outline-offset: 2px; border-radius: 6px; }

/* ---------- Shell ---------- */
#app { min-height: 100vh; min-height: 100dvh; }

.topbar {
  position: sticky; top: 0; z-index: 40;
  background: var(--topbar-bg);
  color: var(--topbar-ink);
  padding-top: var(--safe-top);
  box-shadow: 0 1px 0 rgba(255, 255, 255, .06), 0 4px 16px rgba(11, 31, 58, .18);
}
.topbar-inner {
  max-width: var(--page-max); margin: 0 auto;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 10px;
  padding: 0 calc(var(--gutter) + var(--safe-right)) 0 calc(var(--gutter) + var(--safe-left));
}
.tb-back {
  display: none; width: 36px; height: 36px; margin-left: -8px;
  border: 0; border-radius: 50%; background: transparent; color: inherit;
  place-items: center;
}
.tb-back:hover { background: rgba(255, 255, 255, .1); }
.tb-back svg { width: 22px; height: 22px; }
body.route-game .tb-back { display: grid; }
.tb-brand { display: flex; align-items: center; gap: 8px; min-width: 0; text-decoration: none; color: inherit; }
.tb-logo { width: 28px; height: 28px; flex: none; border-radius: 8px; }
.tb-title { font-weight: 800; font-size: 17px; letter-spacing: .5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tb-sub { font-size: 12px; opacity: .75; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tb-titles { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.tb-spacer { flex: 1; }
.tb-icon-btn {
  width: 36px; height: 36px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .08);
  color: inherit; display: grid; place-items: center; flex: none;
}
.tb-icon-btn:hover { background: rgba(255, 255, 255, .16); }
.tb-icon-btn svg { width: 19px; height: 19px; }

.wallet-chip {
  display: inline-flex; align-items: center; gap: 6px; height: 32px;
  padding: 0 12px 0 5px; border-radius: 999px;
  background: rgba(255, 255, 255, .1); color: #fff; text-decoration: none;
  box-shadow: inset 0 0 0 1px rgba(245, 179, 1, .35);
  font-weight: 800; font-size: 15px; flex: none;
}
.wallet-chip:hover { background: rgba(255, 255, 255, .16); }
.wallet-chip { position: relative; }
.wallet-chip .coin-ico { width: 22px; height: 22px; }
.wallet-glow {
  position: absolute; inset: 0; border-radius: inherit; pointer-events: none; opacity: 0;
  box-shadow: 0 0 0 2px var(--glow, rgba(245, 179, 1, .75)), 0 0 14px var(--glow, rgba(245, 179, 1, .75));
}

.conn-bar {
  position: sticky; top: calc(var(--topbar-h) + var(--safe-top)); z-index: 39;
  background: #7A2E00; color: #FFE7D6; font-size: 12.5px; text-align: center; padding: 4px 12px;
}

#view {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 12px calc(var(--gutter) + var(--safe-right)) calc(var(--tabbar-h) + var(--safe-bottom) + 24px) calc(var(--gutter) + var(--safe-left));
  min-height: 60vh;
}
#view.enter { animation: view-in 220ms var(--ease-out); }
/* opacity only: a transform here would become the containing block for fixed children (dock, my-rank bar) */
@keyframes view-in { from { opacity: 0; } to { opacity: 1; } }
body.route-game #view { padding-bottom: calc(var(--dock-h) + var(--safe-bottom) + 20px); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: var(--tabbar-bg);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}
.tabbar-inner { max-width: var(--page-max); margin: 0 auto; height: var(--tabbar-h); display: grid; grid-template-columns: repeat(4, 1fr); }
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--text-2); font-size: 11.5px; font-weight: 600;
  position: relative;
}
.tab svg { width: 24px; height: 24px; }
.tab[aria-current="page"] { color: var(--cheer); }
.tab[aria-current="page"]::after {
  content: ""; position: absolute; top: 0; left: 50%; width: 28px; height: 3px; margin-left: -14px;
  border-radius: 0 0 3px 3px; background: var(--cheer);
}
body.route-game .tabbar { display: none; }

/* ---------- Primitives ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow-1); }
.card-pad { padding: 14px 16px; }
.section { margin-top: 18px; }
.section-title { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 800; margin: 0 2px 10px; }
.section-title .muted { font-weight: 600; font-size: 12.5px; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.row { display: flex; align-items: center; gap: 8px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 40px; padding: 0 16px; border-radius: 999px; border: 0;
  background: var(--card-3); color: var(--text); font-weight: 700; font-size: 14px;
  text-decoration: none; white-space: nowrap; user-select: none;
  transition: transform var(--dur-fast) var(--ease-out), background var(--dur-fast);
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--cheer); color: #fff; box-shadow: 0 4px 12px rgba(255, 90, 31, .32); }
.btn-primary:hover:not(:disabled) { background: #F04E14; }
.btn-brand { background: var(--brand); color: #fff; }
.btn-ghost { background: transparent; box-shadow: inset 0 0 0 1px var(--line-2); }
.btn-coin { background: linear-gradient(180deg, var(--coin-2), var(--coin)); color: #3D2B00; box-shadow: 0 4px 12px rgba(245, 179, 1, .35); }
.btn-danger { background: #FDECEC; color: #B42318; }
:root[data-theme="dark"] .btn-danger { background: #3A1616; color: #FCA5A5; }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; }
.btn-lg { height: 48px; font-size: 16px; padding: 0 22px; }
.btn-block { width: 100%; }
.icon-btn { width: 36px; height: 36px; padding: 0; border-radius: 50%; display: inline-grid; place-items: center; border: 0; background: var(--card-3); }
.icon-btn svg { width: 18px; height: 18px; }

.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding: 2px; margin: 0 -2px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 14px;
  border-radius: 999px; border: 0; background: var(--card); color: var(--text-2);
  font-weight: 700; font-size: 13px; box-shadow: inset 0 0 0 1px var(--line);
}
.chip[aria-pressed="true"] { background: var(--brand); color: #fff; box-shadow: none; }
:root[data-theme="dark"] .chip[aria-pressed="true"] { background: #E6EDF3; color: #0D1117; }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dot, currentColor); }

.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px;
  background: var(--card-3); border-radius: 12px; padding: 3px;
}
.seg button {
  border: 0; background: transparent; height: 34px; border-radius: 9px; font-weight: 700; font-size: 13.5px; color: var(--text-2);
  display: inline-flex; align-items: center; justify-content: center; gap: 4px; white-space: nowrap;
}
.seg button[aria-selected="true"], .seg button[aria-pressed="true"] { background: var(--card); color: var(--text); box-shadow: var(--shadow-1); }

.tag { display: inline-flex; align-items: center; height: 18px; padding: 0 6px; border-radius: 5px; font-size: 11px; font-weight: 800; letter-spacing: .3px; flex: none; }
.tag-sim { background: var(--sim-bg); color: var(--sim-ink); box-shadow: inset 0 0 0 1px var(--sim-line); }
.tag-live { background: var(--live); color: #fff; }
.tag-bot { background: var(--card-3); color: var(--text-3); height: 16px; font-size: 10px; padding: 0 4px; }
.tag-stream { gap: 3px; background: rgba(229, 50, 45, .1); color: var(--live); box-shadow: inset 0 0 0 1px rgba(229, 50, 45, .35); }
.tag-stream .ts-dot { width: 0; height: 0; border-style: solid; border-width: 3.5px 0 3.5px 5.5px; border-color: transparent transparent transparent currentColor; }
.tag-admin { background: var(--live); color: #fff; }
.link-btn { border: 0; background: none; padding: 6px 0; color: var(--info); font-weight: 700; font-size: 13.5px; text-decoration: underline; text-underline-offset: 3px; }

.input, .select {
  width: 100%; height: 42px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--line-2); background: var(--card); color: var(--text);
  outline: none; transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
}
.input:focus, .select:focus { border-color: var(--cheer); box-shadow: 0 0 0 3px rgba(255, 90, 31, .18); }
.field-label { display: block; font-size: 12.5px; font-weight: 700; color: var(--text-2); margin: 10px 2px 6px; }
.field-err { color: var(--live); font-size: 12px; margin: 4px 2px 0; min-height: 0; }

.switch { position: relative; width: 46px; height: 28px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--line-2); transition: background var(--dur); }
.switch span::after {
  content: ""; position: absolute; left: 3px; top: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); transition: transform var(--dur) var(--ease-out);
}
.switch input:checked + span { background: var(--win); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--cheer); outline-offset: 2px; }

.list-row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; min-height: 52px; }
.list-row + .list-row { border-top: 1px solid var(--line); }

.empty { text-align: center; padding: 40px 16px; color: var(--text-2); }
.empty .empty-ico { font-size: 40px; line-height: 1; margin-bottom: 10px; }

.skel { position: relative; overflow: hidden; background: var(--card-3); border-radius: 8px; }
.skel::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .45), transparent);
  transform: translateX(-100%); animation: skel 1.3s infinite;
}
:root[data-theme="dark"] .skel::after { background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .06), transparent); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .skel::after { background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .06), transparent); } }
@keyframes skel { to { transform: translateX(100%); } }

.coin-ico { width: 18px; height: 18px; flex: none; display: inline-block; vertical-align: -3px; }
.coin-amt { display: inline-flex; align-items: center; gap: 4px; font-weight: 800; }

.boot-splash { display: grid; place-items: center; min-height: 50vh; color: var(--text-2); gap: 12px; text-align: center; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--cheer); animation: spin 800ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.update-bar {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 12px); z-index: 70;
  transform: translateX(-50%); display: flex; align-items: center; gap: 10px;
  background: var(--brand); color: #fff; border-radius: 999px; padding: 6px 6px 6px 16px;
  box-shadow: var(--shadow-3); font-size: 13.5px; font-weight: 700; white-space: nowrap;
  animation: view-in 260ms var(--ease-out);
}
body.route-game .update-bar { bottom: calc(var(--dock-h) + var(--safe-bottom) + 12px); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; }
  #view.enter { animation: none; }
  .skel::after { animation: none; }
}
