:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eceef3;
  --text: #15171c;
  --muted: #5f6573;
  --accent: #e8442e;
  --accent-text: #ffffff;
  --ok: #1f9d55;
  --danger: #c62828;
  --border: #d9dce4;
  --radius: 14px;
  --shadow: 0 4px 18px rgba(0, 0, 0, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111318;
    --surface: #1b1e25;
    --surface-2: #252932;
    --text: #eef0f4;
    --muted: #9aa1b0;
    --border: #2f3440;
    --shadow: 0 4px 18px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111318;
  --surface: #1b1e25;
  --surface-2: #252932;
  --text: #eef0f4;
  --muted: #9aa1b0;
  --border: #2f3440;
  --shadow: 0 4px 18px rgba(0, 0, 0, .4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
select, input[type="text"], input[type="search"], input[type="number"], input[type="url"], input[type="password"] {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; min-width: 0;
}
.game-picker select { font-weight: 600; }
.pickers { display: flex; align-items: center; gap: 8px; min-width: 0; }
.pickers select { font-weight: 600; max-width: 38vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 480px) { .brand span { display: none; } }
.cam-row { display: flex; align-items: center; gap: 10px; max-width: 420px; margin: 10px auto 0; padding: 0 12px; }
.cam-row select { flex: 1; min-width: 0; }
.cam-label { font-size: .85rem; opacity: .75; white-space: nowrap; }

main { flex: 1; overflow-y: auto; padding: 16px 16px 96px; max-width: 900px; width: 100%; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }
h2 { font-size: 1.05rem; margin: 22px 0 10px; }

.btn {
  font: inherit; font-weight: 600; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.icon { width: 46px; height: 46px; border-radius: 50%; padding: 0; font-size: 20px; }

/* Cámara */
.camera {
  position: relative; background: #000; border-radius: var(--radius); overflow: hidden;
  aspect-ratio: 3 / 4; max-height: 52vh; margin: 0 auto; width: 100%;
}
.camera video { width: 100%; height: 100%; object-fit: cover; display: block; }
.guide {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  height: 86%; aspect-ratio: 63 / 88; border: 3px solid rgba(255, 255, 255, .9); border-radius: 12px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .38); pointer-events: none;
  display: flex; align-items: flex-end; justify-content: center;
}
.guide span { color: #fff; font-size: 12px; background: rgba(0, 0, 0, .5); padding: 2px 8px; border-radius: 8px; margin-bottom: 8px; }
.guide.busy { border-color: #ffc93c; }
.guide.hit { border-color: #3ddc84; }
.camera-empty {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; color: #fff; padding: 20px; gap: 6px; background: #14161b;
}
.camera-empty .hint { color: #b7bcc8; }
.camera.on .camera-empty { display: none; }
.camera:not(.on) .guide { display: none; }

.scan-controls { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 14px 0 4px; }
.shutter {
  width: 70px; height: 70px; border-radius: 50%; background: var(--accent);
  border: 5px solid var(--surface); box-shadow: 0 0 0 3px var(--accent); cursor: pointer;
}
.shutter:disabled { opacity: .5; }
.toggle { display: inline-flex; align-items: center; gap: 4px; font-weight: 600; font-size: 13px; cursor: pointer; }
.status { text-align: center; color: var(--muted); min-height: 1.4em; margin: 6px 0 12px; font-size: 14px; overflow-wrap: anywhere; }
.hint { color: var(--muted); font-size: 13px; }

.search { display: flex; gap: 8px; margin-bottom: 14px; }
.search input { flex: 1; }

/* Resultados */
.candidates, .collection { display: grid; gap: 10px; }
.candidates { grid-template-columns: 1fr; }
.cand {
  display: flex; gap: 12px; align-items: center; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px; box-shadow: var(--shadow);
}
.cand img { width: 64px; aspect-ratio: 63 / 88; object-fit: cover; border-radius: 6px; background: var(--surface-2); flex: none; }
.cand .info { flex: 1; min-width: 0; }
.cand .name { font-weight: 700; overflow-wrap: anywhere; }
.cand .meta { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.cand.best { border-color: var(--ok); }
.price { font-weight: 700; color: var(--ok); }
.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; text-align: center; }
.stat b { display: block; font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 12px; }
.filters { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.filters input { flex: 1 1 160px; }
.export-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.export-bar .btn { font-size: 13px; padding: 6px 10px; }

.collection { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
  cursor: pointer; position: relative; text-align: left; padding: 0; font: inherit; color: inherit;
}
.item img { width: 100%; aspect-ratio: 63 / 88; object-fit: cover; display: block; background: var(--surface-2); }
.item .qty {
  position: absolute; top: 6px; right: 6px; background: var(--accent); color: #fff; font-weight: 700;
  border-radius: 999px; padding: 1px 8px; font-size: 13px;
}
.item .foil { position: absolute; top: 6px; left: 6px; }
.item .body { padding: 6px 8px 8px; }
.item .name { font-weight: 700; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .meta { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; grid-column: 1 / -1; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-weight: 600; font-size: 14px; }
.field small { color: var(--muted); }
.field.check { flex-direction: row; align-items: center; gap: 8px; }
.field.check label { font-weight: 500; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: var(--surface);
  border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); z-index: 6;
}
.tabbar button {
  flex: 1; background: none; border: 0; color: var(--muted); font: inherit; font-size: 12px; font-weight: 600;
  padding: 8px 0 10px; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
.tabbar button span { font-size: 20px; }
.tabbar button.active { color: var(--accent); }
.tabbar b { font-size: 11px; }

dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  width: min(480px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: 16px;
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dlg-head { display: flex; gap: 12px; margin-bottom: 12px; }
.dlg-head img { width: 110px; aspect-ratio: 63 / 88; object-fit: cover; border-radius: 8px; background: var(--surface-2); flex: none; }
.dlg-head h3 { margin: 0 0 4px; overflow-wrap: anywhere; }
.dlg-head p { margin: 0 0 4px; color: var(--muted); font-size: 13px; }
.dlg-head .price { color: var(--ok); font-size: 15px; }
.dlg-head a { color: var(--accent); font-size: 13px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.dlg-actions { display: flex; gap: 8px; margin-top: 8px; }
.spacer { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-weight: 600;
  transition: .25s; pointer-events: none; z-index: 20; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (min-width: 760px) {
  #view-scan.active { display: grid; grid-template-columns: 380px 1fr; gap: 0 20px; align-items: start; }
  #view-scan .camera, #view-scan .scan-controls, #view-scan .status { grid-column: 1; }
  #view-scan .search, #view-scan .candidates { grid-column: 2; }
  #view-scan .search { grid-row: 1; }
  #view-scan .candidates { grid-row: 2 / span 3; }
}
.cand .actions { display: flex; flex-direction: column; gap: 6px; }
.cand .actions .btn { width: 44px; height: 38px; padding: 0; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
