/* ── Design tokens ───────────────────────────────────── */
:root {
  --bg: var(--tg-theme-bg-color, #0b0b0e);
  --surface: var(--tg-theme-secondary-bg-color, #16161c);
  --surface-2: rgba(255, 255, 255, 0.04);
  --text: var(--tg-theme-text-color, #f4f4f7);
  --hint: var(--tg-theme-hint-color, #85859a);
  --accent: var(--tg-theme-button-color, #7c5cff);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --accent-soft: color-mix(in srgb, var(--accent) 14%, transparent);
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.14);
  --danger: #e5484d;
  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 18px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
  --pad-x: 16px;
  --ease: cubic-bezier(0.25, 0.8, 0.35, 1);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  touch-action: pan-x pan-y;
  -ms-touch-action: pan-x pan-y;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(40px + env(safe-area-inset-bottom, 0px));
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
}

button { font-family: inherit; }

/* ── Top bar ─────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top, 0px)) var(--pad-x) 10px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
.brand-row { display: flex; align-items: center; gap: 8px; }
.brand { font-size: 19px; font-weight: 800; letter-spacing: -0.01em; }

.beta-badge {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 2px 7px; border-radius: 999px;
  font-size: 9px; font-weight: 800; letter-spacing: 0.08em;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
}
.beta-badge--sm { padding: 2px 6px; font-size: 8px; }

.beta-strip {
  display: flex; align-items: center; gap: 8px;
  margin: 0 var(--pad-x) 12px; padding: 8px 12px;
  border-radius: var(--r-sm); background: var(--surface); border: 1px solid var(--border);
  font-size: 12px; color: var(--hint);
}

.generate .beta-badge--sm { margin-left: 4px; }
.overlay-beta { margin-bottom: 4px; }
.overlay-beta .beta-badge { font-size: 9px; }

.balance {
  display: flex; align-items: center; gap: 6px;
  background: var(--surface); border: 1px solid var(--border);
  padding: 7px 13px; border-radius: 999px;
  font-weight: 700; font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.coin { color: var(--accent); font-size: 12px; }

/* ── Boot screen ─────────────────────────────────────── */
.boot-screen {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; background: var(--bg);
}
.boot-screen.hidden { display: none; }
.boot-text { font-size: 14px; color: var(--hint); font-weight: 600; }

/* ── Nav ─────────────────────────────────────────────── */
.nav {
  position: sticky;
  top: calc(54px + env(safe-area-inset-top, 0px));
  z-index: 19;
  display: flex; gap: 6px;
  padding: 4px var(--pad-x) 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
.nav::-webkit-scrollbar { display: none; }
.nav-btn {
  flex: 0 0 auto; padding: 8px 15px;
  border: 1px solid transparent;
  border-radius: 999px; background: transparent; color: var(--hint);
  font-size: 13px; font-weight: 650; cursor: pointer; white-space: nowrap;
  transition: color 0.18s var(--ease), background 0.18s var(--ease);
}
.nav-btn:active { transform: scale(0.96); }
.nav-btn.active {
  background: var(--accent); color: var(--accent-text);
  box-shadow: 0 2px 12px color-mix(in srgb, var(--accent) 40%, transparent);
}

/* ── Layout ──────────────────────────────────────────── */
main { padding: 0; }
.screen { display: none; padding: 0 var(--pad-x); }
.screen.active { display: block; animation: fade 0.22s var(--ease); }
.panel { display: none; }
.panel.active { display: block; animation: fade 0.22s var(--ease); }
.panel[hidden] { display: none !important; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.screen-title { font-size: 20px; font-weight: 800; letter-spacing: -0.01em; margin: 8px 0 14px; }
.screen-subtitle {
  font-size: 11px; color: var(--hint); font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.08em; margin: 22px 0 10px;
}
.row-between { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.row-between .screen-title { margin: 8px 0; }
.empty { color: var(--hint); font-size: 14px; text-align: center; padding: 40px 20px; }
.hint-text { font-size: 13px; color: var(--hint); margin-bottom: 14px; }

/* ── Tabs ────────────────────────────────────────────── */
.tabs {
  display: flex; gap: 4px; padding: 4px; margin-bottom: 16px;
  background: var(--surface); border-radius: var(--r-md);
  border: 1px solid var(--border);
}
.tab {
  flex: 1; padding: 10px; border: none; border-radius: var(--r-sm);
  background: transparent; color: var(--hint);
  font-size: 14px; font-weight: 700; cursor: pointer;
  transition: all 0.18s var(--ease);
}
.tab.active { background: var(--accent); color: var(--accent-text); }

/* ── Fields ──────────────────────────────────────────── */
.field-label {
  display: block; font-size: 12px; color: var(--hint);
  margin: 20px 0 8px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.06em;
}

textarea {
  width: 100%; border: 1px solid var(--border); border-radius: var(--r-md);
  background: var(--surface); color: var(--text); padding: 13px 14px;
  font-size: 15px; font-family: inherit; resize: vertical;
  transition: border-color 0.18s var(--ease);
  min-height: 84px;
}
textarea::placeholder { color: var(--hint); opacity: 0.7; }
textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ── Uploads ─────────────────────────────────────────── */
.uploads { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-start; }
.ref-slots { display: contents; }
.add-ref { margin: 12px 0 0; }
.upload {
  flex: 0 0 84px; width: 84px; height: 84px; aspect-ratio: 1;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--r-md); background: var(--surface-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 5px; cursor: pointer; overflow: hidden; position: relative;
  transition: border-color 0.18s var(--ease), background 0.18s var(--ease);
}
.upload:active { transform: scale(0.97); }
.upload.filled { border-style: solid; border-color: var(--accent); background: var(--surface); }
.upload .plus { font-size: 20px; color: var(--accent); line-height: 1; }
.upload .upcap { font-size: 10px; color: var(--hint); text-align: center; padding: 0 4px; font-weight: 600; }
.upload img, .upload video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

.ref-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.ref-tags[hidden] { display: none; }
.ref-tag {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 11px 4px 4px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 12px; font-weight: 700; cursor: pointer;
  transition: border-color 0.15s var(--ease);
}
.ref-tag img, .ref-tag video {
  width: 22px; height: 22px; border-radius: 50%;
  object-fit: cover; display: block;
}
.ref-tag .rt-dot {
  width: 22px; height: 22px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-text); font-size: 11px;
}
.ref-tag:active { border-color: var(--accent); transform: scale(0.97); }

/* ── Chips ───────────────────────────────────────────── */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 9px 16px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 13px;
  font-weight: 650; cursor: pointer;
  transition: all 0.16s var(--ease);
  font-variant-numeric: tabular-nums;
}
.chip:active { transform: scale(0.95); }
.chip.selected {
  background: var(--accent); color: var(--accent-text); border-color: var(--accent);
  box-shadow: 0 2px 10px color-mix(in srgb, var(--accent) 35%, transparent);
}

/* ── Switch ──────────────────────────────────────────── */
.switch-row {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 20px; padding: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-size: 14px; font-weight: 650; cursor: pointer;
}
.switch-row input {
  appearance: none; -webkit-appearance: none;
  width: 46px; height: 28px; border-radius: 999px;
  background: var(--border-strong); position: relative;
  cursor: pointer; transition: background 0.2s var(--ease);
  flex: 0 0 auto; margin: 0;
}
.switch-row input::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff; transition: transform 0.2s var(--ease);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.switch-row input:checked { background: var(--accent); }
.switch-row input:checked::after { transform: translateX(18px); }

/* ── Buttons ─────────────────────────────────────────── */
.generate {
  width: 100%; margin-top: 26px; padding: 16px;
  border: none; border-radius: var(--r-md);
  background: var(--accent); color: var(--accent-text);
  font-size: 16px; font-weight: 750; cursor: pointer;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px;
  box-shadow: 0 4px 18px color-mix(in srgb, var(--accent) 35%, transparent);
  transition: transform 0.15s var(--ease), opacity 0.15s var(--ease);
}
.generate:active { transform: scale(0.98); }
.generate:disabled { opacity: 0.45; box-shadow: none; }
.generate.is-disabled { opacity: 0.45; pointer-events: none; box-shadow: none; }
.cost { font-size: 13px; opacity: 0.85; font-weight: 600; font-variant-numeric: tabular-nums; }

.btn-small {
  padding: 8px 15px; border: none; border-radius: var(--r-sm);
  background: var(--accent-soft); color: var(--accent);
  font-weight: 700; font-size: 13px; cursor: pointer;
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  transition: all 0.15s var(--ease);
}
.btn-small:active { transform: scale(0.96); }
.btn-small:disabled { opacity: 0.4; }
.btn-small.danger {
  background: color-mix(in srgb, var(--danger) 14%, transparent);
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 32%, transparent);
}

/* ── Access gate ─────────────────────────────────────── */
.access-gate {
  margin: 28px 0; padding: 28px 22px; border-radius: var(--r-lg); text-align: center;
  background: var(--surface); border: 1px solid var(--border);
}
.gate-title { font-size: 18px; font-weight: 800; margin-bottom: 8px; }
.gate-text { font-size: 14px; color: var(--hint); margin-bottom: 18px; }

.photo-disabled {
  margin: 24px 0; padding: 18px; border-radius: var(--r-md);
  background: var(--surface); border: 1px solid var(--border);
  color: var(--hint); font-size: 14px; text-align: center;
}

/* ── Presets ─────────────────────────────────────────── */
.presets { display: flex; flex-wrap: wrap; gap: 7px; margin-bottom: 6px; }
.preset-chip {
  padding: 7px 13px; border-radius: 999px; border: 1px dashed var(--border-strong);
  background: transparent; color: var(--hint); font-size: 12px; font-weight: 600;
  cursor: pointer; transition: all 0.15s var(--ease);
}
.preset-chip:active { background: var(--surface); color: var(--text); transform: scale(0.96); }

/* ── Gallery ─────────────────────────────────────────── */
.gallery-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.gcell {
  position: relative; aspect-ratio: 1; border-radius: var(--r-sm); overflow: hidden;
  background: var(--surface); cursor: pointer; border: 2px solid transparent;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: border-color 0.15s var(--ease), transform 0.15s var(--ease);
}
.gcell:active { transform: scale(0.97); }
.gcell .gcheck {
  position: absolute; top: 6px; left: 6px; z-index: 3;
  width: 21px; height: 21px; border-radius: 50%;
  background: rgba(0, 0, 0, 0.45); border: 2px solid rgba(255, 255, 255, 0.9);
  display: none; align-items: center; justify-content: center;
  color: #fff; font-size: 12px; font-weight: 700;
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}
.gallery-grid.selecting .gcell .gcheck { display: flex; }
.gcell.selected .gcheck { background: var(--accent); border-color: var(--accent); }
.gcell img, .gcell video {
  position: absolute; inset: 0; z-index: 1;
  width: 100%; height: 100%; object-fit: cover;
  opacity: 0; transition: opacity 0.3s var(--ease);
}
.gcell img.media-loaded, .gcell video.media-loaded,
.gcell.media-loaded img, .gcell.media-loaded video { opacity: 1; }
.gph {
  position: absolute; inset: 0; z-index: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; color: var(--hint); opacity: 0.4;
  background: var(--surface);
}
.gcell.selected { border-color: var(--accent); }
.gbadge {
  position: absolute; top: 6px; right: 6px; z-index: 2;
  background: rgba(0, 0, 0, 0.55);
  color: #fff; font-size: 9px; font-weight: 700; letter-spacing: 0.04em;
  padding: 3px 7px; border-radius: 999px;
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}

/* ── Stats ───────────────────────────────────────────── */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-md); padding: 14px;
}
.stat-k { font-size: 11px; color: var(--hint); font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; }
.stat-v {
  font-size: 20px; font-weight: 800; margin-top: 6px;
  word-break: break-word; font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}
.ledger { margin-top: 4px; }
.ledrow {
  display: flex; justify-content: space-between; gap: 10px;
  font-size: 13px; padding: 11px 2px;
  border-bottom: 1px solid var(--border); color: var(--hint);
  font-variant-numeric: tabular-nums;
}
.ledrow:last-child { border-bottom: none; }

/* ── Buy ─────────────────────────────────────────────── */
.discount-badge {
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  display: inline-block; padding: 5px 12px; border-radius: 999px;
  font-size: 12px; font-weight: 700; margin-bottom: 8px;
}
.cards { display: flex; flex-direction: column; gap: 10px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md);
  padding: 15px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  transition: border-color 0.15s var(--ease);
}
.card:active { border-color: var(--border-strong); }
.card-title { font-size: 15px; font-weight: 750; }
.card-sub { font-size: 12px; color: var(--hint); margin-top: 3px; font-variant-numeric: tabular-nums; }

/* ── MCP ─────────────────────────────────────────────── */
.mcp-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.mcp-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-md); padding: 13px 14px;
}
.mcp-info { display: flex; flex-direction: column; gap: 3px; font-size: 13px; min-width: 0; font-weight: 650; }
.mcp-info span { color: var(--hint); font-size: 11px; word-break: break-all; font-weight: 500; }
.mcp-info em { color: var(--accent); font-size: 11px; font-style: normal; font-weight: 700; }
.mcp-form { display: flex; flex-direction: column; gap: 9px; }
.mcp-form input, #admin-search {
  width: 100%; border: 1px solid var(--border); border-radius: var(--r-sm);
  background: var(--surface); color: var(--text); padding: 12px 13px; font-size: 14px;
  font-family: inherit;
  transition: border-color 0.18s var(--ease);
}
.mcp-form input::placeholder, #admin-search::placeholder { color: var(--hint); opacity: 0.7; }
.mcp-form input:focus, #admin-search:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ── Admin ───────────────────────────────────────────── */
.flags { display: flex; flex-direction: column; gap: 7px; }
.flag-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-sm); padding: 11px 13px; font-size: 13px; font-weight: 600;
}
.flag-row input { width: 40px; height: 22px; accent-color: var(--accent); }
.admin-users { display: flex; flex-direction: column; gap: 7px; margin-top: 8px; }
.user-row {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-sm); padding: 11px 13px; cursor: pointer;
  transition: border-color 0.15s var(--ease);
}
.user-row:active { border-color: var(--accent); }
.user-main { display: flex; flex-direction: column; font-weight: 650; font-size: 14px; }
.user-main span { font-size: 11px; color: var(--hint); font-weight: 500; }
.tags { display: flex; gap: 4px; flex-wrap: wrap; }
.tag {
  background: var(--accent-soft); color: var(--accent);
  font-size: 10px; padding: 3px 7px; border-radius: 999px; font-weight: 700;
}
.admin-modal-box { text-align: left; width: 88%; max-width: 360px; }
.adm-field { margin-top: 14px; }
.adm-field > label {
  font-size: 11px; color: var(--hint); display: block; margin-bottom: 6px;
  font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em;
}
.adm-inline { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.adm-inline input, .adm-inline select {
  flex: 1; min-width: 60px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); padding: 9px; font-size: 13px;
  font-family: inherit;
}
.adm-inline .mini { font-size: 12px; color: var(--text); display: flex; align-items: center; gap: 4px; flex: 1; }

/* ── Overlay ─────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.66);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center; z-index: 50;
  animation: fade 0.2s var(--ease);
}
.overlay[hidden] { display: none !important; }
.overlay-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg); padding: 30px 24px;
  width: 84%; max-width: 330px; text-align: center;
  box-shadow: var(--shadow);
}
.overlay-title { font-size: 17px; font-weight: 800; margin-top: 16px; }
.progress-wrap { width: 100%; margin-top: 16px; text-align: left; }
.progress-wrap[hidden] { display: none !important; }
.progress-bar {
  height: 6px; border-radius: 999px; background: var(--border-strong); overflow: hidden;
}
.progress-fill {
  height: 100%; width: 0%; border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), color-mix(in srgb, var(--accent) 60%, #ffffff));
  transition: width 0.45s var(--ease);
}
.progress-meta {
  display: flex; justify-content: space-between; margin-top: 8px;
  font-size: 12px; color: var(--hint); font-weight: 600;
  font-variant-numeric: tabular-nums;
}
#progress-pct { color: var(--accent); }
.overlay-text { font-size: 14px; color: var(--hint); margin-top: 10px; line-height: 1.5; }
.overlay-close {
  margin-top: 22px; padding: 12px 26px; border: none; border-radius: var(--r-sm);
  background: var(--accent); color: var(--accent-text); font-weight: 700; cursor: pointer;
  font-size: 14px;
}
.overlay-close:active { transform: scale(0.97); }

.spinner {
  width: 38px; height: 38px; margin: 0 auto; border-radius: 50%;
  border: 3px solid var(--border-strong); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
.spinner.hidden { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ───────────────────────────────────────────── */
.toast {
  position: fixed; left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%) translateY(16px);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 12px 20px; border-radius: var(--r-md);
  font-size: 14px; font-weight: 600; z-index: 80; opacity: 0;
  transition: opacity 0.25s var(--ease), transform 0.25s var(--ease);
  max-width: 84%; text-align: center;
  box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast[hidden] { display: none; }

/* ── Lightbox ────────────────────────────────────────── */
.lightbox {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(0, 0, 0, 0.93);
  display: flex; align-items: center; justify-content: center; padding: 16px;
  animation: fade 0.2s var(--ease);
}
.lightbox[hidden] { display: none !important; }
.lightbox-content {
  max-width: 100%; max-height: 100%;
  display: flex; align-items: center; justify-content: center;
}
.lightbox-content img, .lightbox-content video {
  max-width: 100%; max-height: 88vh; border-radius: var(--r-md);
  object-fit: contain;
}
.lightbox-close {
  position: absolute;
  top: calc(16px + env(safe-area-inset-top, 0px));
  right: 16px; z-index: 91;
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: rgba(255, 255, 255, 0.12); color: #fff;
  font-size: 17px; cursor: pointer;
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
}
.lightbox-close:active { transform: scale(0.94); }

/* ── Reduced motion ──────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
