:root {
  --bg: #0b0b10;
  --panel: #12121a;
  --panel2: #171723;
  --text: #f2f4ff;
  --muted: rgba(242, 244, 255, 0.72);
  --stroke: rgba(255, 255, 255, 0.10);
  --primary: #6d7bff;
  --accent: #ff2d88;
  --danger: #ff4d4d;
  --shadow: 0 18px 70px rgba(0, 0, 0, 0.55);
  --radius: 18px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  background: radial-gradient(1000px 800px at 20% 10%, rgba(109,123,255,0.18), transparent 60%),
              radial-gradient(900px 700px at 80% 30%, rgba(255,45,136,0.14), transparent 55%),
              var(--bg);
  color: var(--text);
  overflow: hidden;
}

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: 14px;
  padding: 14px;
  overflow: scroll;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.03));
  box-shadow: var(--shadow);
}

.brand {
  font-weight: 700;
  letter-spacing: 0.3px;
}

.screen { min-height: 0; display: grid; place-items: center; }
.screen, .stageFull { grid-row: 2; }
.bottomActions { grid-row: 3; }
.footer { grid-row: 4; }

.panel {
  width: min(980px, 100%);
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.04);
  padding: 14px;
  box-shadow: var(--shadow);
}

.panel-title { font-weight: 800; font-size: 20px; }
.panel-sub { margin-top: 6px; color: var(--muted); font-size: 14px; }
.panel-actions { margin-top: 12px; display: flex; }
.panel-actions .btn { flex: 1; }

.bg-grid {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
}

.bg-card {
  border: 1px solid var(--stroke);
  border-radius: 16px;
  overflow: hidden;
  background: rgba(255,255,255,0.03);
  cursor: pointer;
  transition: transform 90ms ease, border-color 140ms ease, background 140ms ease;
  display: grid;
  grid-template-rows: 1fr auto;
  text-align: left;
}
.bg-card:hover { transform: translateY(-1px); background: rgba(255,255,255,0.05); }
.bg-card.selected { border-color: rgba(109,123,255,0.70); }
.bg-thumb { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; display: block; }
.bg-name { padding: 10px; font-weight: 650; font-size: 13px; color: rgba(242,244,255,.9); }

.frame {
  position: relative;
  height: 100%;
  min-height: 0;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--stroke);
  background: linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.02));
  box-shadow: var(--shadow);
}

video, #captureImg, #finalImg {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Review + final should show the whole image (no cropping). */
#captureImg.visible, #finalImg.visible { object-fit: contain; background: rgba(0,0,0,0.35); }

#captureImg, #finalImg {
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

#captureImg.visible, #finalImg.visible {
  opacity: 1;
}

.stageFull { min-height: 0; }
.frame-full { height: 100%; }

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

.btn {
  appearance: none;
  border: 1px solid var(--stroke);
  background: rgba(255,255,255,0.05);
  color: var(--text);
  padding: 12px 14px;
  border-radius: 14px;
  font-weight: 650;
  letter-spacing: 0.2px;
  cursor: pointer;
  user-select: none;
  transition: transform 90ms ease, background 140ms ease, border-color 140ms ease;
  flex: 1;
}
.btn:hover { background: rgba(255,255,255,0.08); }
.btn:active { transform: translateY(1px); }
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  border-color: rgba(109,123,255,0.45);
  background: linear-gradient(180deg, rgba(109,123,255,0.30), rgba(109,123,255,0.10));
}
.btn-accent {
  border-color: rgba(255,45,136,0.45);
  background: linear-gradient(180deg, rgba(255,45,136,0.24), rgba(255,45,136,0.08));
}
.btn-ghost {
  flex: 0 0 auto;
  padding: 10px 12px;
}

.hint {
  margin-top: 10px;
  font-size: 14px;
  color: var(--muted);
  min-height: 18px;
}

.result {
  border-radius: var(--radius);
  border: 1px solid var(--stroke);
  background: rgba(255,255,255,0.04);
  padding: 12px;
  box-shadow: var(--shadow);
}

.result-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 14px;
  align-items: center;
}

.result-title { font-weight: 750; font-size: 18px; }
.result-sub { margin-top: 4px; color: var(--muted); font-size: 14px; }
.result-actions { margin-top: 10px; }

.qrcode {
  padding: 10px;
  border-radius: 14px;
  background: white;
  width: max-content;
}

.footer {
  display: flex;
  justify-content: center;
  padding: 6px 0 2px;
}
.health {
  font-size: 13px;
  color: var(--muted);
}

.hidden { display: none !important; }

.floatBar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(18px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  z-index: 50;
  padding: 10px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(10,10,16,0.55);
  box-shadow: var(--shadow);
}

@supports ((-webkit-backdrop-filter: blur(10px)) or (backdrop-filter: blur(10px))) {
  .fx-blur:not(.no-blur) .floatBar {
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
  }
}

.fab {
  appearance: none;
  border: 1px solid rgba(109,123,255,0.55);
  background: linear-gradient(180deg, rgba(109,123,255,0.34), rgba(109,123,255,0.12));
  color: var(--text);
  padding: 14px 18px;
  border-radius: 999px;
  font-weight: 800;
  letter-spacing: 0.2px;
  cursor: pointer;
  user-select: none;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 140px;
}
.fab:active { transform: translateY(1px); }
.fab-ghost {
  border-color: rgba(255,255,255,0.16);
  background: rgba(255,255,255,0.06);
  min-width: 160px;
}

.bottomActions {
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.04);
  padding: 12px;
  box-shadow: var(--shadow);
  text-align: center;
  /* Leave room for the floating Download bar on iOS/phones */
  padding-bottom: calc(12px + 82px + env(safe-area-inset-bottom));
}
.resultTitle { font-weight: 800; font-size: 18px; }
.resultSub { margin-top: 4px; color: var(--muted); font-size: 14px; min-height: 18px; }
.qrWrap { margin-top: 10px; display: flex; justify-content: center; }

.bottomRow {
  margin-top: 12px;
  display: flex;
  gap: 10px;
}

.spinner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0,0,0,0.55);
}

@supports ((-webkit-backdrop-filter: blur(6px)) or (backdrop-filter: blur(6px))) {
  .fx-blur:not(.no-blur) .spinner {
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
  }
}
.spinner-ring {
  width: 56px;
  height: 56px;
  border-radius: 999px;
  border: 4px solid rgba(255,255,255,0.18);
  border-top-color: rgba(255,255,255,0.92);
  animation: spin 1s linear infinite;
}
.spinner-text {
  margin-top: 10px;
  font-weight: 650;
  color: rgba(255,255,255,0.92);
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 680px) {
  .app { padding: 10px; }
  .fab { min-width: 120px; }
  .qrcode { margin: 0 auto; }
  .floatBar { bottom: calc(12px + env(safe-area-inset-bottom)); }

  /* Background picker: list-style like a file list */
  .bg-grid { grid-template-columns: 1fr; }
  .bg-card { grid-template-rows: none; grid-template-columns: 74px 1fr; align-items: center; }
  .bg-thumb { width: 74px; height: 74px; aspect-ratio: 1 / 1; object-fit: cover; }
  .bg-name { padding: 12px 10px; font-size: 15px; }
}
