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

:root {
  --bg:        #1f2230;
  --surface:   #282c3f;
  --surface2:  #353a52;
  --border:    #454b68;
  --text:      #f5f6fa;
  --muted:     #9298b3;
  --accent:    #f7931a;
  --accent2:   #d97e0a;
  --success:   #22c55e;
  --danger:    #ef4444;
  --radius:    12px;
  --radius-sm: 8px;
}

html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; flex-direction: column; min-height: 100dvh; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; background: rgba(31,34,48,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 6px; font-size: 17px; font-weight: 800; color: var(--text); text-decoration: none; }
.brand-mark { color: var(--accent); }
.cart-btn { position: relative; font-size: 20px; text-decoration: none; padding: 6px; }
.cart-count {
  position: absolute; top: -2px; right: -2px;
  background: var(--accent); color: #1f2230;
  font-size: 10px; font-weight: 800; min-width: 16px; height: 16px;
  border-radius: 99px; display: flex; align-items: center; justify-content: center;
  padding: 0 3px;
}

.view { flex: 1; max-width: 560px; margin: 0 auto; width: 100%; padding: 16px; padding-bottom: 48px; }

/* Headings */
h1 { font-size: 22px; font-weight: 800; margin-bottom: 4px; }
h2 { font-size: 16px; font-weight: 700; }
.subtitle { color: var(--muted); font-size: 14px; margin-bottom: 20px; }
.section-title { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 20px 0 12px; }

.filter-tabs { display: flex; gap: 8px; margin: 16px 0 20px; border-bottom: 1px solid var(--border); padding-bottom: 0; }
.filter-tab {
  flex: 1; padding: 10px 12px; background: none; border: none; border-bottom: 3px solid transparent;
  color: var(--muted); font-size: 14px; font-weight: 700; cursor: pointer; white-space: nowrap;
}
.filter-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* Cards / grids */
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; text-decoration: none; color: var(--text); display: block;
  transition: transform .12s, border-color .12s;
}
.card:active { transform: scale(.98); }
.card-img { width: 100%; aspect-ratio: 1; object-fit: cover; background: var(--surface2); display: block; }
.card-img-placeholder { width: 100%; aspect-ratio: 1; background: var(--surface2); display: flex; align-items: center; justify-content: center; font-size: 32px; color: var(--muted); }
.card-body { padding: 10px 12px; }
.card-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-price { color: var(--accent); font-weight: 800; font-size: 15px; margin-top: 2px; }
.card-shop  { color: var(--muted); font-size: 12px; margin-top: 2px; }

.seller-row {
  display: flex; align-items: center; gap: 12px; padding: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 10px; text-decoration: none; color: var(--text);
}
.seller-logo { width: 48px; height: 48px; border-radius: 50%; object-fit: cover; background: var(--surface2); flex-shrink: 0; }
.seller-logo-fallback {
  width: 48px; height: 48px; border-radius: 50%; background: var(--accent); color: #1f2230;
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 18px; flex-shrink: 0;
}
.seller-info h3 { font-size: 15px; font-weight: 700; }
.seller-info p { font-size: 12px; color: var(--muted); }
.badge-soon { font-size: 10px; background: var(--surface2); color: var(--muted); padding: 2px 8px; border-radius: 99px; margin-left: auto; }

/* Badges */
.tag { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 10px; font-weight: 700; margin-right: 4px; }
.tag-preorder { background: #78350f; color: #fde68a; }
.tag-ready    { background: #14532d; color: #4ade80; }
.tag-delivery { background: var(--surface2); color: var(--muted); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 18px; border-radius: var(--radius-sm); border: none;
  font-size: 15px; font-weight: 700; cursor: pointer; text-decoration: none;
  transition: opacity .15s, transform .1s;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #1f2230; }
.btn-ghost   { background: var(--surface2); color: var(--text); }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-full    { width: 100%; }
.btn-sm      { padding: 7px 12px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Product detail */
.gallery { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; border-radius: var(--radius); margin-bottom: 16px; gap: 0; }
.gallery img { width: 100%; flex: 0 0 100%; scroll-snap-align: start; aspect-ratio: 1; object-fit: cover; background: var(--surface2); }
.gallery-placeholder { width: 100%; aspect-ratio: 1; background: var(--surface2); border-radius: var(--radius); display: flex; align-items: center; justify-content: center; font-size: 48px; color: var(--muted); margin-bottom: 16px; }
.price-lg { font-size: 26px; font-weight: 800; color: var(--accent); margin: 8px 0; }
.qty-row { display: flex; align-items: center; gap: 14px; margin: 16px 0; }
.qty-btn { width: 36px; height: 36px; border-radius: 50%; background: var(--surface2); border: none; color: var(--text); font-size: 18px; cursor: pointer; }
.qty-val { font-size: 17px; font-weight: 700; min-width: 24px; text-align: center; }

/* Cart */
.cart-line { display: flex; gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--border); }
.cart-line img { width: 56px; height: 56px; border-radius: var(--radius-sm); object-fit: cover; background: var(--surface2); flex-shrink: 0; }
.cart-line-info { flex: 1; min-width: 0; }
.cart-line-info h4 { font-size: 14px; font-weight: 600; }
.cart-line-info .price { color: var(--accent); font-size: 13px; font-weight: 700; }
.cart-total-row { display: flex; justify-content: space-between; align-items: center; padding: 16px 0; font-size: 17px; font-weight: 800; }

/* Forms */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text); padding: 11px 12px; font-size: 15px; outline: none;
}
.form-group input:focus, .form-group textarea:focus { border-color: var(--accent); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.delivery-choice { display: flex; gap: 10px; margin-bottom: 16px; }
.delivery-option {
  flex: 1; padding: 14px; border: 2px solid var(--border); border-radius: var(--radius); text-align: center;
  cursor: pointer; background: var(--surface);
}
.delivery-option.selected { border-color: var(--accent); background: rgba(247,147,26,.1); }
.delivery-option .icon { font-size: 24px; margin-bottom: 4px; }

/* Map picker (Grab delivery) */
.map-picker-wrap { position: relative; margin-top: 10px; border-radius: var(--radius); overflow: hidden; }
.search-clear-wrap { position: relative; }
.search-clear-wrap input { padding-right: 38px; }
.search-clear-btn {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 26px; height: 26px; border-radius: 50%; border: none;
  background: var(--surface2); color: var(--muted); cursor: pointer;
  font-size: 13px; display: flex; align-items: center; justify-content: center;
}
.search-clear-btn:hover { background: var(--border); color: var(--text); }
.map-picker { width: 100%; height: 260px; background: var(--surface2); }
.map-picker-loading { position: absolute; inset: 0; background: var(--surface2); }
.map-locate-btn {
  width: 40px; height: 40px; margin: 10px; border-radius: 50%;
  background: #fff; color: #1f2230; border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 6px rgba(0,0,0,.3);
}
.map-locate-btn:disabled { opacity: .5; }
.pac-container {
  z-index: 10000; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); margin-top: 4px; font-family: inherit;
}
.pac-item { color: var(--text); border-color: var(--border); padding: 8px 10px; font-size: 13px; }
.pac-item:hover, .pac-item-selected { background: var(--surface2); }
.pac-item-query { color: var(--text); }
.pac-matched { color: var(--accent); }
.pac-icon { filter: invert(1) brightness(1.6); }

/* Payment / QR */
.pay-box { text-align: center; padding: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.qr-wrap { display: inline-block; background: #fff; padding: 10px; border-radius: var(--radius); margin: 16px 0; }
.qr-wrap canvas, .qr-wrap img { display: block; }
.pay-actions { display: flex; gap: 8px; justify-content: center; margin-top: 4px; }
.pay-amount-thb { font-size: 26px; font-weight: 800; color: var(--accent); }
.pay-amount-sat { font-size: 15px; font-weight: 600; color: var(--muted); margin-top: 2px; }
.pay-timer { font-size: 13px; color: var(--muted); margin-top: 8px; }
.pay-status { display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 16px; font-size: 14px; color: var(--muted); }

/* Checkout order summary — highlighted total */
.order-summary-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 20px;
}
.order-summary-row {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 14px; color: var(--muted); padding: 4px 0;
}
.order-summary-divider { border-top: 1px dashed var(--border); margin: 8px 0; }
.order-summary-total {
  font-size: 20px; font-weight: 800; color: var(--text);
}
.order-summary-total span:last-child { color: var(--accent); }

/* Misc */
.error-msg { color: var(--danger); font-size: 13px; margin-top: 8px; }
.empty-state { text-align: center; color: var(--muted); padding: 60px 0; }
.empty-state .icon { font-size: 44px; margin-bottom: 12px; }
.spinner { display: inline-block; width: 18px; height: 18px; border: 2px solid rgba(255,255,255,.2); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-center { display: flex; justify-content: center; padding: 60px 0; }
.copy-hint { font-size: 12px; color: var(--muted); margin-top: 6px; word-break: break-all; cursor: pointer; }

/* ── Marketplace stats widget (home / main shop list only) ──────────────────
   Scoped to #view > #mkt-stats which only renderSellers() injects, so it never
   appears inside a shop, cart, or checkout. Overflow-guarded so large numbers
   (or long BTC / ล้านบาท strings) can never break the layout or spill wide. */
.mkt-stats {
  margin: 14px 0 10px;
  padding: 12px 14px;
  background: linear-gradient(135deg, rgba(247,147,26,.12), rgba(247,147,26,.03));
  border: 1px solid var(--border);
  border-radius: var(--radius, 12px);
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--muted);
  max-width: 100%;
  overflow: hidden;
}
.mkt-stats--empty {          /* pre-fetch placeholder: take no visible space */
  margin: 0; padding: 0; border: 0; background: none;
}
.mkt-stats__row {
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;   /* clamp any single line instead of overflowing */
}
.mkt-stats__row b {
  color: var(--accent);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
@media (max-width: 380px) {
  .mkt-stats { font-size: 12.5px; padding: 10px 12px; }
}
