:root {
  --accent: #0f5bd7;
  --accent-ink: #ffffff;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --ink: #14181f;
  --muted: #5d6675;
  --line: #e1e4ea;
  --ok: #12804a;
  --ok-bg: #e3f5ea;
  --bad: #b3261e;
  --bad-bg: #fbe9e7;
  --warn-bg: #fff1cc;
  --radius: 28px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- Barra superior ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(16px, 3vw, 32px);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-logo { height: 40px; max-width: 180px; object-fit: contain; }
.brand-name {
  font-size: clamp(1.05rem, 2.2vw, 1.4rem);
  font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 12px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:active { background: var(--bg); }
.pill { padding: 6px 12px; border-radius: 999px; font-size: .9rem; font-weight: 600; }
.pill-warn { background: var(--warn-bg); color: #6b4a00; }

/* ---------- Vistas ---------- */
main { flex: 1; position: relative; min-height: 0; }
.view {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 2vh, 20px);
  padding: clamp(16px, 3vw, 40px);
  text-align: center;
}
.view.is-active { display: flex; animation: fade .22s ease-out; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

h1 { margin: 0; font-size: clamp(1.9rem, 5vw, 3.4rem); font-weight: 800; letter-spacing: -.02em; }
.lead { margin: 0; font-size: clamp(1.1rem, 2.6vw, 1.7rem); color: var(--muted); }

/* ---------- Inicio ---------- */
.scan-art {
  position: relative;
  display: grid; place-items: center;
  width: clamp(150px, 30vmin, 280px);
  aspect-ratio: 1;
  padding: clamp(14px, 2.8vmin, 26px);
  margin-bottom: clamp(6px, 2vh, 20px);
  color: var(--ink);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(20, 24, 31, .08);
  overflow: hidden;
}
.scan-art svg { width: 100%; height: auto; aspect-ratio: 1; display: block; opacity: .9; }
.scan-line {
  position: absolute; left: 6%; right: 6%; top: 10%;
  height: 4px; border-radius: 4px;
  background: var(--accent);
  box-shadow: 0 0 18px 4px color-mix(in srgb, var(--accent) 55%, transparent);
  animation: sweep 2.4s ease-in-out infinite alternate;
}
@keyframes sweep { to { top: 88%; } }
.idle-actions {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 14px;
  margin-top: clamp(10px, 3vh, 32px);
}

/* ---------- Botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 60px; padding: 0 28px;
  border: 2px solid transparent; border-radius: 18px;
  font: inherit; font-size: 1.15rem; font-weight: 700;
  cursor: pointer;
  transition: transform .08s ease;
}
.btn:active { transform: scale(.97); }
.btn svg { width: 26px; height: 26px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn-dark { background: rgba(0, 0, 0, .55); color: #fff; border-color: rgba(255, 255, 255, .35); }
.btn-light { background: #fff; color: #111; }
.btn-small { min-height: 46px; padding: 0 20px; font-size: 1rem; border-radius: 14px; }

/* ---------- Cargando ---------- */
.spinner {
  width: 72px; height: 72px; border-radius: 50%;
  border: 7px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Resultado ---------- */
#view-result { justify-content: stretch; gap: 14px; }
.card {
  flex: 1; min-height: 0; width: 100%; max-width: 1280px;
  display: grid;
  grid-template-columns: minmax(0, 5fr) minmax(0, 7fr);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(20, 24, 31, .08);
  overflow: hidden;
  text-align: left;
}
.card-media {
  display: flex; align-items: center; justify-content: center;
  min-height: 0; padding: clamp(14px, 3vmin, 36px);
  background: #fff;
  border-right: 1px solid var(--line);
}
.card-media img { max-width: 100%; max-height: 100%; object-fit: contain; }
.noimage { width: 45%; color: #c4c9d2; }
.noimage svg { width: 100%; }
.card-body {
  display: flex; flex-direction: column; justify-content: center;
  gap: clamp(8px, 1.6vh, 18px);
  min-width: 0; padding: clamp(18px, 4vmin, 52px);
}
.ref {
  margin: 0; color: var(--muted);
  font-size: clamp(.95rem, 1.8vw, 1.2rem); font-weight: 600; letter-spacing: .04em;
}
.name {
  margin: 0;
  font-size: clamp(1.4rem, 3.4vw, 2.5rem); font-weight: 700; line-height: 1.15;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.price-block { margin-top: clamp(4px, 1.5vh, 16px); }
.price-label {
  margin: 0; color: var(--muted);
  font-size: clamp(.95rem, 1.8vw, 1.2rem); font-weight: 600; text-transform: uppercase; letter-spacing: .08em;
}
.price {
  margin: 2px 0 10px; color: var(--accent);
  font-size: clamp(2.8rem, 9vw, 6.5rem); font-weight: 800; line-height: 1.1; letter-spacing: -.03em;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.price-sub { margin: 0; color: var(--muted); font-size: clamp(1rem, 2vw, 1.35rem); min-height: 1.3em; }
.stock {
  align-self: flex-start; margin: clamp(4px, 1vh, 10px) 0 0;
  padding: 10px 18px; border-radius: 999px;
  font-size: clamp(1rem, 2vw, 1.3rem); font-weight: 700;
}
.stock.is-ok { background: var(--ok-bg); color: var(--ok); }
.stock.is-out { background: var(--bad-bg); color: var(--bad); }

.result-footer {
  display: flex; align-items: center; justify-content: center; gap: 18px;
  color: var(--muted); font-size: clamp(1rem, 2vw, 1.2rem);
}
.countdown { width: 100%; max-width: 1280px; height: 5px; border-radius: 5px; background: var(--line); overflow: hidden; }
.countdown span { display: block; height: 100%; width: 100%; background: var(--accent); transform-origin: left; }
.countdown span.run { animation: shrink linear forwards; }
@keyframes shrink { to { transform: scaleX(0); } }

@media (orientation: portrait) {
  .card { grid-template-columns: 1fr; grid-template-rows: minmax(0, 4fr) minmax(0, 6fr); }
  .card-media { border-right: 0; border-bottom: 1px solid var(--line); }
}

/* ---------- Mensajes ---------- */
.msg-icon {
  width: clamp(84px, 16vmin, 130px); aspect-ratio: 1; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(2.6rem, 8vmin, 4.2rem); font-weight: 800;
  background: var(--warn-bg); color: #8a5a00;
}
.msg-icon.is-error { background: var(--bad-bg); color: var(--bad); }
.code-chip {
  margin: 0; padding: 8px 16px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--line);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.1rem; max-width: 90%; overflow-wrap: anywhere;
  -webkit-user-select: text; user-select: text;
}
.inline-form { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; }

input[type="text"], input[type="password"] {
  min-height: 60px; width: min(420px, 80vw); padding: 0 20px;
  border: 2px solid var(--line); border-radius: 18px;
  font: inherit; font-size: 1.3rem; color: var(--ink); background: var(--surface);
  -webkit-user-select: text; user-select: text;
}
input:focus { outline: none; border-color: var(--accent); }

/* ---------- Capas ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: #000;
}
.overlay-dim { background: rgba(20, 24, 31, .55); align-items: flex-start; padding-top: 12vh; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.reticle {
  position: relative; width: min(62vmin, 440px); aspect-ratio: 1;
  border-radius: 32px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .5);
  outline: 4px solid rgba(255, 255, 255, .9); outline-offset: -4px;
}
.camera-hint {
  position: absolute; top: calc(env(safe-area-inset-top) + 5vh); left: 0; right: 0; margin: 0;
  padding: 0 20px; text-align: center; color: #fff;
  font-size: clamp(1.1rem, 2.6vw, 1.6rem); font-weight: 600;
  text-shadow: 0 1px 8px rgba(0, 0, 0, .7);
}
.camera-actions {
  position: absolute; bottom: calc(env(safe-area-inset-bottom) + 4vh); left: 0; right: 0;
  display: flex; justify-content: center; gap: 14px;
}
.dialog {
  width: min(560px, 92vw); padding: 28px;
  display: flex; flex-direction: column; gap: 18px;
  background: var(--surface); border-radius: var(--radius);
  box-shadow: 0 24px 80px rgba(0, 0, 0, .3);
}
.dialog h2 { margin: 0; font-size: 1.5rem; }
.dialog input { width: 100%; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 12px; }

@media (prefers-reduced-motion: reduce) {
  .scan-line, .view.is-active { animation: none; }
}
