:root {
  --primary: #1a56db;
  --primary-dark: #1344b3;
  --accent: #f97316;
  --bg: var(--tg-theme-bg-color, #f8fafc);
  --text: var(--tg-theme-text-color, #0f172a);
  --muted: var(--tg-theme-hint-color, #64748b);
  --card: var(--tg-theme-secondary-bg-color, #ffffff);
  --button: var(--tg-theme-button-color, var(--primary));
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --radius: 16px;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  line-height: 1.4;
}

.hidden {
  display: none !important;
}

/* Loading */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  height: 100vh;
  color: var(--muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(26, 86, 219, 0.15);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Onboarding */
.screen {
  min-height: 100vh;
  padding: 20px 16px 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.onboard-card {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border-radius: 24px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
}

.logo-mark {
  width: 56px;
  height: 56px;
  margin: 0 auto 12px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--primary), #3b82f6);
  color: #fff;
  font-size: 28px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand {
  text-align: center;
  font-size: 26px;
  font-weight: 800;
  color: var(--primary);
}

.lead {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  margin: 8px 0 20px;
}

.tg-profile {
  text-align: center;
  margin-bottom: 16px;
}

.avatar-ring {
  width: 64px;
  height: 64px;
  margin: 0 auto 10px;
  border-radius: 50%;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  border: 2px solid rgba(26, 86, 219, 0.2);
}

.tg-profile-name {
  font-size: 18px;
  font-weight: 700;
}

.tg-profile-username {
  font-size: 14px;
  color: var(--primary);
  margin-top: 2px;
}

.perm-box {
  background: var(--bg);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 16px;
}

.perm-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 8px;
}

.perm-list {
  margin-left: 18px;
  color: var(--muted);
  font-size: 13px;
}

.btn-primary {
  width: 100%;
  padding: 15px;
  background: var(--button);
  color: var(--button-text);
  border: none;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-secondary {
  width: 100%;
  padding: 14px;
  border-radius: 14px;
  border: 1.5px solid var(--primary);
  background: transparent;
  color: var(--primary);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.error {
  color: #dc2626;
  text-align: center;
  margin-top: 12px;
  font-size: 13px;
}

/* Main shell */
#main {
  max-width: 480px;
  margin: 0 auto;
  padding-bottom: 88px;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 16px 16px 8px;
  gap: 12px;
}

.header-eyebrow {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.header-greeting {
  font-size: 22px;
  font-weight: 800;
  margin-top: 2px;
}

.tier-chip {
  border: none;
  background: var(--card);
  color: var(--primary);
  font-size: 12px;
  font-weight: 700;
  padding: 8px 12px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}

.panel {
  padding: 0 16px 16px;
}

.balance-card {
  position: relative;
  background: linear-gradient(135deg, var(--primary) 0%, #2563eb 55%, #3b82f6 100%);
  color: #fff;
  border-radius: 24px;
  padding: 22px;
  overflow: hidden;
  box-shadow: 0 12px 28px rgba(26, 86, 219, 0.35);
}

.balance-glow {
  position: absolute;
  width: 180px;
  height: 180px;
  right: -40px;
  top: -60px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 50%;
}

.balance-label {
  font-size: 13px;
  opacity: 0.9;
}

.balance-value {
  font-size: 40px;
  font-weight: 800;
  margin-top: 4px;
  letter-spacing: -0.02em;
}

.currency {
  font-size: 18px;
  font-weight: 600;
  opacity: 0.9;
}

.tier-badge {
  display: inline-block;
  margin-top: 14px;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.22);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.quick-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 14px 0 6px;
}

.quick-btn {
  background: var(--card);
  border: none;
  border-radius: 14px;
  padding: 14px 10px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.quick-icon {
  font-size: 20px;
  color: var(--primary);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 18px 2px 10px;
}

.section-head h2 {
  font-size: 17px;
  font-weight: 800;
}

.link-btn {
  border: none;
  background: none;
  color: var(--primary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.row {
  background: var(--card);
  border-radius: 14px;
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.04);
}

.row-title {
  font-size: 15px;
  font-weight: 600;
}

.row-sub {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.amount-plus {
  color: #16a34a;
  font-weight: 700;
  font-size: 15px;
}

.amount-minus {
  color: #dc2626;
  font-weight: 700;
  font-size: 15px;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 28px 16px;
  background: var(--card);
  border-radius: 14px;
  font-size: 14px;
}

.promo-card {
  border-left: 4px solid var(--accent);
}

.promo-card .row-title {
  color: var(--primary-dark);
}

/* QR */
.qr-card {
  background: var(--card);
  border-radius: 20px;
  padding: 24px 18px;
  text-align: center;
  box-shadow: var(--shadow);
}

.qr-card.compact {
  margin-top: 14px;
  padding: 16px;
}

.qr-title {
  font-size: 20px;
  font-weight: 800;
}

.qr-sub {
  color: var(--muted);
  font-size: 13px;
  margin: 6px 0 16px;
}

.barcode-frame {
  display: flex;
  justify-content: center;
  padding: 12px 8px 4px;
  background: #fff;
  border-radius: 12px;
}

#barcode-svg {
  max-width: 100%;
  height: auto;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.qr-meta {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--tg-theme-hint-color, #6b7280);
  text-align: center;
}

.qr-frame {
  display: inline-block;
  padding: 12px;
  background: #fff;
  border-radius: 16px;
  box-shadow: inset 0 0 0 1px #e2e8f0;
}

.qr-frame.small img {
  width: 180px;
  height: 180px;
}

#qr-img,
#invite-qr {
  width: 220px;
  height: 220px;
  display: block;
  border-radius: 8px;
}

.phone {
  margin-top: 14px;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

/* Invite */
.invite-hero {
  text-align: center;
  margin-bottom: 14px;
}

.invite-emoji {
  font-size: 36px;
}

.invite-lead {
  color: var(--muted);
  font-size: 14px;
  margin-top: 6px;
}

.code-card {
  background: var(--card);
  border-radius: 16px;
  padding: 16px;
  text-align: center;
  box-shadow: var(--shadow);
}

.code-label {
  font-size: 12px;
  color: var(--muted);
}

.code-value {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--primary);
  margin-top: 4px;
}

.invite-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 14px 0;
}

.invite-link {
  word-break: break-all;
  font-size: 12px;
  color: var(--primary);
  background: var(--card);
  padding: 10px 12px;
  border-radius: 12px;
  margin: 8px 0;
}

.hint {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

.stats-pill {
  margin-top: 12px;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  background: var(--card);
  border-radius: 999px;
  padding: 10px 14px;
}

/* Tabbar */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--card);
  border-top: 1px solid rgba(148, 163, 184, 0.25);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  max-width: 480px;
  margin: 0 auto;
  box-shadow: 0 -4px 20px rgba(15, 23, 42, 0.06);
}

.tabbar-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 10px;
  padding: 6px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
}

.tab-icon {
  font-size: 18px;
  line-height: 1;
}

.tabbar-btn.active {
  color: var(--primary);
  font-weight: 700;
}

.tabbar-btn.active .tab-icon {
  transform: scale(1.08);
}

.purchase-card {
  background: var(--card);
  border-radius: 16px;
  padding: 14px;
  box-shadow: var(--shadow);
  margin-bottom: 10px;
}

.purchase-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.purchase-title {
  font-size: 15px;
  font-weight: 800;
  color: var(--primary);
}

.purchase-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.purchase-amount {
  font-size: 16px;
  font-weight: 800;
  white-space: nowrap;
}

.purchase-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
}

.purchase-store {
  margin-top: 8px;
  font-size: 12px;
  color: var(--muted);
}

.purchase-lines {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  border-top: 1px solid rgba(15, 23, 42, 0.08);
}

.purchase-lines li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid rgba(15, 23, 42, 0.06);
  font-size: 12px;
}

.purchase-lines li span:first-child {
  flex: 1;
}

.purchase-lines li span:last-child {
  white-space: nowrap;
  color: var(--muted);
}

/* Profile */
.profile-card {
  background: var(--card);
  border-radius: 20px;
  padding: 22px 18px;
  text-align: center;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.avatar-ring.lg {
  width: 72px;
  height: 72px;
  font-size: 32px;
  margin-bottom: 0;
}

.profile-name {
  font-size: 19px;
  font-weight: 800;
  margin-top: 10px;
}

.profile-phone {
  color: var(--muted);
  font-size: 13px;
  margin-top: 2px;
}

.profile-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.field-input {
  width: 100%;
  padding: 13px 14px;
  border: 1.5px solid rgba(148, 163, 184, 0.4);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
}

.field-input:focus {
  outline: none;
  border-color: var(--primary);
}

select.field-input {
  cursor: pointer;
}

.dob-row {
  display: grid;
  grid-template-columns: 1fr 1.5fr 1.1fr;
  gap: 8px;
}

.dob-row .field-input {
  padding-left: 10px;
  padding-right: 6px;
}

.field-hint {
  font-size: 12px;
  color: var(--muted);
}

.gender-row {
  display: flex;
  gap: 10px;
}

.chip-toggle {
  flex: 1;
  padding: 13px;
  border-radius: 12px;
  border: 1.5px solid rgba(148, 163, 184, 0.4);
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.chip-toggle.active {
  border-color: var(--primary);
  background: rgba(26, 86, 219, 0.08);
  color: var(--primary);
}

.profile-status {
  text-align: center;
  font-size: 13px;
  min-height: 18px;
}

.profile-status.success {
  color: #16a34a;
}

.profile-status.error {
  color: #dc2626;
}
