:root {
  --bg:      var(--tg-theme-bg-color, #080810);
  --bg2:     var(--tg-theme-secondary-bg-color, #10101e);
  --bg3:     #18182c;
  --bg4:     #202038;
  --text:    var(--tg-theme-text-color, #e8e8ff);
  --hint:    var(--tg-theme-hint-color, #5050a0);
  --accent:  #8B7FFF;
  --accent2: #c4bcff;
  --accent3: #5547ff;
  --gold:    #FFD166;
  --red:     #FF5C5C;
  --green:   #00E5A0;
  --border:  rgba(139,127,255,0.14);
  --border2: rgba(255,255,255,0.05);
  --shadow:  0 8px 40px rgba(0,0,0,0.55);
  --shadow2: 0 2px 16px rgba(0,0,0,0.35);
  --tab-h:   60px;
  --r:       16px;
  --rs:      10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 100% 50% at 50% 0%, rgba(139,127,255,0.1) 0%, transparent 60%),
    radial-gradient(ellipse 60% 30% at 80% 100%, rgba(85,71,255,0.06) 0%, transparent 50%);
  color: var(--text);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 8px);
}

/* ── Desktop ────────────────────────────────────────────────────────────────── */
@media (min-width: 600px) {
  body { max-width: 480px; margin: 0 auto; }
  /* Use left/right arithmetic instead of transform — more stable on resize */
  .tab-bar {
    left: max(0px, calc(50% - 240px));
    right: auto;
    width: min(480px, 100vw);
    transform: none;
  }
  .modal-sheet { max-width: 480px; margin: 0 auto; }
  .toast { left: 50%; }
}

/* ── Tab panes ─────────────────────────────────────────────────────────────── */
.tab-pane { display: none; padding: 12px 12px 0; }
.tab-pane.active { display: block; }

/* ── Tab Bar ───────────────────────────────────────────────────────────────── */
.tab-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  background: rgba(8,8,16,0.95);
  backdrop-filter: blur(28px); -webkit-backdrop-filter: blur(28px);
  border-top: 1px solid rgba(139,127,255,0.18);
  display: flex; z-index: 100;
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 3px;
  border: none; background: none; cursor: pointer;
  color: var(--hint); font-size: 9px; font-weight: 900;
  letter-spacing: .7px; text-transform: uppercase;
  transition: color .18s; position: relative; padding: 5px 0;
}
.tab.active { color: var(--accent2); }
.tab.active::before {
  content: "";
  position: absolute; top: 0; left: 20%; right: 20%; height: 2px;
  background: linear-gradient(90deg, var(--accent3), var(--accent), var(--accent2));
  border-radius: 0 0 3px 3px;
  box-shadow: 0 0 10px rgba(139,127,255,0.6);
}
.tab-icon { font-size: 22px; line-height: 1; }

/* ── Section title ─────────────────────────────────────────────────────────── */
.section-title {
  font-size: 10px; font-weight: 900; text-transform: uppercase;
  letter-spacing: 1.2px; color: var(--hint);
  margin: 14px 0 8px; padding: 0 2px;
  display: flex; align-items: center; gap: 8px;
}
.section-title::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* ── Form card ─────────────────────────────────────────────────────────────── */
.form-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 14px;
  margin-bottom: 4px;
  box-shadow: var(--shadow);
  position: relative; overflow: hidden;
}
.form-card::before {
  content: ""; position: absolute; top: 0; left: 20%; right: 20%; height: 1px;
  background: linear-gradient(90deg, transparent, rgba(139,127,255,0.6), transparent);
}

/* ── Fields ────────────────────────────────────────────────────────────────── */
.field { margin-bottom: 10px; }
.field:last-of-type { margin-bottom: 0; }

.field-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 10px; color: var(--hint);
  font-weight: 900; text-transform: uppercase;
  letter-spacing: 1px; margin-bottom: 5px;
}
.sel-count {
  background: var(--accent3); color: #fff;
  font-size: 9px; font-weight: 900;
  padding: 1px 6px; border-radius: 8px; letter-spacing: 0;
}

input[type="text"],
input[type="number"],
select {
  width: 100%; padding: 10px 12px;
  background: var(--bg3); border: 1.5px solid var(--border2);
  border-radius: var(--rs); color: var(--text);
  font-size: 14px; font-weight: 500; outline: none; appearance: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus {
  border-color: rgba(139,127,255,0.5);
  box-shadow: 0 0 0 3px rgba(139,127,255,0.1);
}
input::placeholder { color: var(--hint); font-weight: 400; }

.select-wrap { position: relative; }
.select-wrap::after {
  content: "›"; position: absolute; right: 12px; top: 50%;
  transform: translateY(-50%) rotate(90deg);
  color: var(--hint); pointer-events: none; font-size: 18px; line-height: 1;
}

/* ── Attr button ───────────────────────────────────────────────────────────── */
.attr-btn {
  width: 100%; padding: 10px 12px;
  background: var(--bg3); border: 1.5px solid var(--border2);
  border-radius: var(--rs); color: var(--text);
  font-size: 14px; font-weight: 500; text-align: left; cursor: pointer;
  display: flex; align-items: center; justify-content: space-between;
  transition: border-color .15s;
}
.attr-btn:focus { outline: none; border-color: rgba(139,127,255,0.5); box-shadow: 0 0 0 3px rgba(139,127,255,0.1); }
.attr-btn .chevron { color: var(--hint); font-size: 16px; }
.attr-btn.has-selection { border-color: rgba(139,127,255,0.4); color: var(--accent2); }

/* ── Skin chips ────────────────────────────────────────────────────────────── */
.chips-scroll { display: flex; flex-wrap: wrap; gap: 6px; max-height: 90px; overflow-y: auto; }
.chip {
  padding: 4px 12px; background: var(--bg3);
  border: 1.5px solid var(--border2); border-radius: 20px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  color: var(--hint); transition: all .15s; white-space: nowrap;
}
.chip.on { background: rgba(139,127,255,0.15); color: var(--accent2); border-color: rgba(139,127,255,0.5); }

/* ── Save button ───────────────────────────────────────────────────────────── */
.btn-save {
  width: 100%; padding: 14px;
  background: linear-gradient(135deg, var(--accent3) 0%, var(--accent) 50%, var(--accent2) 100%);
  color: #fff; border: none; border-radius: var(--rs);
  font-size: 15px; font-weight: 900; cursor: pointer;
  margin-top: 14px; letter-spacing: .3px;
  box-shadow: 0 4px 24px rgba(139,127,255,0.5), 0 1px 0 rgba(255,255,255,0.12) inset;
  transition: opacity .15s, transform .1s;
}
.btn-save:active { opacity: .85; transform: scale(.985); }

/* ── Criteria cards ────────────────────────────────────────────────────────── */
#criteria-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.c-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--r); padding: 11px 13px;
  display: flex; align-items: flex-start; gap: 10px;
  box-shadow: var(--shadow2); transition: border-color .18s;
}
.c-card:hover { border-color: rgba(139,127,255,0.3); }
.c-card-body { flex: 1; min-width: 0; }
.c-card-tags { display: flex; flex-wrap: wrap; gap: 5px; }
.c-tag {
  padding: 3px 9px; background: var(--bg3);
  border: 1px solid var(--border2); border-radius: 8px;
  font-size: 12px; font-weight: 600; color: var(--hint);
}
.c-tag.nft-tag { background: rgba(139,127,255,0.1); border-color: rgba(139,127,255,0.3); color: var(--accent2); }
.c-tag.price-tag { color: var(--gold); border-color: rgba(255,209,102,0.2); background: rgba(255,209,102,0.05); }
.btn-del {
  background: none; border: none; color: rgba(255,255,255,0.12);
  font-size: 18px; cursor: pointer; padding: 2px 4px; flex-shrink: 0;
  border-radius: 50%; transition: color .15s, background .15s;
}
.btn-del:hover { color: var(--red); background: rgba(255,92,92,0.1); }

/* ── Market filters ────────────────────────────────────────────────────────── */
.market-filters {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--r); padding: 10px; margin-bottom: 10px;
  box-shadow: var(--shadow2);
}
.filter-row-top { display: flex; gap: 6px; margin-bottom: 6px; }
.filter-row-top .select-wrap { flex: 1; min-width: 0; }
.f-select select { font-size: 13px; padding: 8px 10px; }

.filter-row-bot { display: flex; gap: 5px; align-items: center; margin-bottom: 7px; }
.filter-attr-btn {
  flex: 1; padding: 8px 5px;
  background: var(--bg3); border: 1.5px solid var(--border2);
  border-radius: var(--rs); color: var(--hint);
  font-size: 10px; font-weight: 900; cursor: pointer;
  text-transform: uppercase; letter-spacing: .5px;
  display: flex; align-items: center; justify-content: center; gap: 4px;
  transition: all .15s; min-width: 0;
}
.filter-attr-btn.active { color: var(--accent2); border-color: rgba(139,127,255,0.4); background: rgba(139,127,255,0.08); }
.f-count { background: var(--accent3); color: #fff; font-size: 9px; font-weight: 900; padding: 1px 5px; border-radius: 7px; flex-shrink: 0; }
.f-price { width: 80px !important; flex-shrink: 0 !important; padding: 8px 8px !important; font-size: 13px !important; border-radius: var(--rs) !important; }

.stats-row { display: flex; gap: 5px; flex-wrap: wrap; }
.stat-badge { padding: 3px 9px; border-radius: 20px; font-size: 10px; font-weight: 700; color: #fff; letter-spacing: .3px; }
.stat-total { background: rgba(139,127,255,0.2) !important; color: var(--accent2); border: 1px solid rgba(139,127,255,0.25); }

/* ── Listing cards ─────────────────────────────────────────────────────────── */
#listings-grid { display: flex; flex-direction: column; gap: 8px; }

.l-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--r); padding: 10px;
  display: flex; gap: 10px; align-items: flex-start;
  box-shadow: var(--shadow2);
  transition: border-color .2s, box-shadow .2s;
  overflow: hidden;
}
.l-card:hover { border-color: rgba(139,127,255,0.35); box-shadow: var(--shadow); }
.l-card:active { opacity: .92; }

.l-img {
  width: 72px; height: 72px; border-radius: 12px;
  object-fit: cover; background: var(--bg3); display: block;
  border: 1px solid var(--border2); flex-shrink: 0;
}
.l-img-placeholder {
  width: 72px; height: 72px; border-radius: 12px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--bg4), var(--bg3));
  border: 1px solid var(--border2);
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; opacity: .7;
}

.l-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.l-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.l-name { font-size: 14px; font-weight: 700; line-height: 1.25; color: var(--text); }
.l-price {
  font-size: 16px; font-weight: 900; color: var(--gold);
  white-space: nowrap; flex-shrink: 0; letter-spacing: -.5px;
  text-shadow: 0 0 20px rgba(255,209,102,0.3);
  font-variant-numeric: tabular-nums;
}
.l-price-unit { font-size: 11px; font-weight: 700; opacity: .7; }

.l-attrs { display: flex; flex-wrap: wrap; gap: 4px; }
.l-attr {
  padding: 2px 7px; background: var(--bg3); border: 1px solid var(--border2);
  border-radius: 6px; font-size: 11px; color: var(--hint); font-weight: 500;
}

.l-bottom { display: flex; align-items: center; justify-content: space-between; }
.l-mp { padding: 2px 9px; border-radius: 20px; font-size: 10px; font-weight: 800; color: #fff; letter-spacing: .3px; }
.l-open {
  padding: 6px 14px;
  background: linear-gradient(135deg, var(--accent3), var(--accent));
  color: #fff; border: none; border-radius: 8px;
  font-size: 12px; font-weight: 800; cursor: pointer; text-decoration: none;
  box-shadow: 0 2px 14px rgba(139,127,255,0.4);
  transition: box-shadow .15s, opacity .15s; letter-spacing: .2px;
}
.l-open:hover { box-shadow: 0 4px 22px rgba(139,127,255,0.65); }
.l-open:active { opacity: .8; }

/* ── Empty state ───────────────────────────────────────────────────────────── */
.empty-state { text-align: center; padding: 60px 24px; color: var(--hint); }
.empty-icon { font-size: 52px; margin-bottom: 16px; opacity: .5; }
.empty-state p { font-size: 15px; margin-bottom: 6px; font-weight: 600; }
.empty-hint { font-size: 13px; opacity: .7; }

/* ── Modal ─────────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.75);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  display: none; align-items: flex-end; justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal-overlay.open .modal-sheet { animation: sheetUp .28s cubic-bezier(.25,.46,.45,.94); }

@keyframes sheetUp {
  from { transform: translateY(100%); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

.modal-sheet {
  width: 100%; background: var(--bg2);
  border-radius: 22px 22px 0 0; max-height: 82vh;
  display: flex; flex-direction: column;
  border-top: 1px solid var(--border);
  padding-bottom: max(env(safe-area-inset-bottom), 10px);
  box-shadow: 0 -12px 50px rgba(0,0,0,0.7);
}

.modal-handle {
  width: 36px; height: 4px; border-radius: 2px;
  background: rgba(255,255,255,0.1); margin: 10px auto 0;
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border2);
}
.modal-title { font-size: 16px; font-weight: 800; }
.modal-close {
  width: 28px; height: 28px; background: var(--bg3); border: none;
  border-radius: 50%; color: var(--hint); font-size: 12px; font-weight: 900;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  transition: background .15s, color .15s;
}
.modal-close:hover { background: rgba(255,92,92,0.15); color: var(--red); }

.modal-search {
  margin: 8px 12px 2px; padding: 9px 12px;
  background: var(--bg3); border: 1.5px solid var(--border2);
  border-radius: 10px; color: var(--text); font-size: 14px; font-weight: 500;
  outline: none; width: calc(100% - 24px);
  transition: border-color .15s;
}
.modal-search:focus { border-color: rgba(139,127,255,0.5); }

.modal-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.modal-list::-webkit-scrollbar { width: 2px; }
.modal-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.m-item {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 10px; border-radius: 10px; cursor: pointer;
  font-size: 14px; font-weight: 500; transition: background .1s;
}
.m-item:hover { background: var(--bg3); }
.m-item.on { background: rgba(139,127,255,0.1); color: var(--accent2); }
.m-label { flex: 1; }
.m-check { color: var(--accent); font-weight: 900; opacity: 0; font-size: 14px; flex-shrink: 0; transition: opacity .1s; }
.m-item.on .m-check { opacity: 1; }

.modal-footer {
  display: flex; gap: 8px; padding: 8px 12px 2px;
  border-top: 1px solid var(--border2);
}
.btn-modal-clear {
  flex: 1; padding: 12px; background: var(--bg3);
  border: 1px solid var(--border2); border-radius: 10px;
  color: var(--hint); font-size: 14px; font-weight: 700; cursor: pointer;
  transition: color .15s, background .15s;
}
.btn-modal-clear:hover { color: var(--red); background: rgba(255,92,92,0.06); }
.btn-modal-done {
  flex: 2; padding: 12px;
  background: linear-gradient(135deg, var(--accent3), var(--accent));
  border: none; border-radius: 10px; color: #fff;
  font-size: 14px; font-weight: 900; cursor: pointer;
  box-shadow: 0 4px 20px rgba(139,127,255,0.45);
  transition: opacity .15s; letter-spacing: .2px;
}
.btn-modal-done:active { opacity: .85; }

/* ── Toast ─────────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: calc(var(--tab-h) + 16px + env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%) translateY(10px);
  background: rgba(16,16,30,0.97); backdrop-filter: blur(20px);
  color: var(--text); border: 1px solid var(--border);
  padding: 9px 18px; border-radius: 20px;
  font-size: 13px; font-weight: 600;
  opacity: 0; transition: all .22s cubic-bezier(.34,1.56,.64,1);
  pointer-events: none; white-space: nowrap; z-index: 300;
  box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
