:root {
  --bg: #0f0f0f;
  --bg-card: #1a1a1c;
  --bg-soft: #232326;
  --border: #2e2e32;
  --text: #f4f4f5;
  --text-dim: #9d9da6;
  --accent: #3b82f6;
  --accent-2: #60a5fa;
  --accent-hover: #2563eb;
  --green: #22c55e;
  --lime: #84cc16;
  --orange: #f97316;
  --red: #ef4444;
  --yellow: #eab308;
  --danger-text: #fca5a5;
  --warn-text: #fde047;
  --overlay: rgba(15, 15, 15, .9);
  --footer-text: #6b6b74;
}

html[data-theme="light"] {
  --bg: #f5f6f8;
  --bg-card: #ffffff;
  --bg-soft: #eceef2;
  --border: #dfe2e8;
  --text: #16181d;
  --text-dim: #5d6370;
  --accent-2: #2563eb;
  --danger-text: #b91c1c;
  --warn-text: #a16207;
  --overlay: rgba(245, 246, 248, .92);
  --footer-text: #8a8f9a;
}
html[data-theme="light"] .hist-score { color: #fff; }
html[data-theme="light"] .product-head img { border: 1px solid var(--border); }

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  background: var(--bg);
  transition: background .2s ease, color .2s ease;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100%;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------- Header ---------- */
.topbar {
  padding: calc(env(safe-area-inset-top, 0px) + 18px) 20px 12px;
  text-align: center;
  position: relative;
}
.btn-theme {
  position: absolute;
  right: 16px;
  top: calc(env(safe-area-inset-top, 0px) + 16px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 40px; height: 40px;
  font-size: 1.1rem;
  cursor: pointer;
  line-height: 1;
}
.logo {
  font-size: 1.7rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  cursor: pointer;
  user-select: none;
}
.logo span { color: var(--accent-2); }
.tagline { color: var(--text-dim); font-size: 0.85rem; margin-top: 2px; }

/* ---------- Layout ---------- */
main { max-width: 560px; margin: 0 auto; padding: 8px 18px 40px; }
.view { animation: fadein .25s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---------- Home ---------- */
.hero { text-align: center; margin: 26px 0 24px; }
.hero h1 { font-size: 1.9rem; line-height: 1.15; letter-spacing: -0.5px; }
.hero p { color: var(--text-dim); margin-top: 12px; font-size: 0.98rem; line-height: 1.5; }
.hero b { color: var(--text); font-weight: 600; }

.btn-primary {
  display: block;
  width: 100%;
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: #fff;
  border: none;
  border-radius: 16px;
  padding: 18px;
  font-size: 1.15rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(59, 130, 246, .3);
  transition: transform .12s ease, box-shadow .12s ease;
}
.btn-primary:active { transform: scale(.98); box-shadow: 0 4px 12px rgba(59, 130, 246, .25); }
.btn-small { width: auto; padding: 12px 22px; font-size: 1rem; border-radius: 12px; box-shadow: none; }

.manual { display: flex; gap: 8px; margin-top: 14px; }
.manual input {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  padding: 14px 16px;
  font-size: 1rem;
  outline: none;
}
.manual input:focus { border-color: var(--accent); }
.manual button {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--accent-2);
  border-radius: 12px;
  width: 52px;
  font-size: 1.3rem;
  cursor: pointer;
}

.install-hint {
  margin-top: 18px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 0.88rem;
  color: var(--text-dim);
  line-height: 1.45;
}
.install-hint b { color: var(--text); }

/* ---------- Perfil de alergias ---------- */
.profile-block {
  margin-top: 18px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
}
.profile-block summary {
  cursor: pointer; font-weight: 600; font-size: 0.95rem;
  list-style: none; display: flex; align-items: center; gap: 6px;
}
.profile-block summary::-webkit-details-marker { display: none; }
#profile-count { color: var(--accent-2); font-size: 0.85rem; margin-left: auto; }
.profile-tip { color: var(--text-dim); font-size: 0.85rem; margin: 10px 0 12px; line-height: 1.4; }
.profile-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.pchip {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 0.88rem;
  cursor: pointer;
  user-select: none;
  transition: all .12s;
}
.pchip.on {
  background: rgba(59,130,246,.18);
  border-color: var(--accent);
  color: var(--text);
  font-weight: 600;
}

.alert-banner {
  border-radius: 14px;
  padding: 14px 16px;
  margin-top: 12px;
  font-weight: 600;
  font-size: 0.98rem;
  line-height: 1.4;
}
.alert-banner.danger { background: rgba(239,68,68,.14); border: 1px solid var(--red); color: var(--danger-text); }
.alert-banner.warn { background: rgba(234,179,8,.12); border: 1px solid var(--yellow); color: var(--warn-text); }
.alert-banner small { display: block; font-weight: 400; color: var(--text-dim); margin-top: 4px; }

.add-note {
  color: var(--text-dim); font-size: 0.82rem; line-height: 1.45;
  padding: 4px 0 8px 62px; margin-top: -4px;
}
.add-row.clickable { cursor: pointer; }
.add-toggle { color: var(--text-dim); font-size: 0.8rem; }

/* ---------- Historial ---------- */
.history { margin-top: 30px; }
.history h2 { font-size: 0.95rem; color: var(--text-dim); font-weight: 600; margin-bottom: 10px; text-transform: uppercase; letter-spacing: .5px; }
.hist-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color .12s;
}
.hist-item:active { border-color: var(--accent); }
.hist-score {
  min-width: 44px; height: 44px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 0.95rem;
  color: #0f0f0f;
}
.hist-info { flex: 1; min-width: 0; }
.hist-name { font-weight: 600; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hist-sub { color: var(--text-dim); font-size: 0.82rem; margin-top: 2px; }
.hist-flag { font-size: 1.3rem; }

/* ---------- Resultado ---------- */
.btn-back {
  background: none; border: none; color: var(--accent-2);
  font-size: 1rem; font-weight: 600; cursor: pointer; padding: 8px 0; margin-bottom: 6px;
}

.product-head {
  display: flex; gap: 16px; align-items: center;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
}
.product-head img {
  width: 84px; height: 84px; object-fit: contain;
  background: #fff; border-radius: 12px; flex-shrink: 0;
}
.product-head .noimg {
  width: 84px; height: 84px; border-radius: 12px; background: var(--bg-soft);
  display: flex; align-items: center; justify-content: center; font-size: 2rem; flex-shrink: 0;
}
.product-title { font-size: 1.15rem; font-weight: 700; line-height: 1.25; }
.product-brand { color: var(--text-dim); font-size: 0.9rem; margin-top: 3px; }

.score-card {
  display: flex; align-items: center; gap: 18px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 18px;
  margin-top: 12px;
}
.score-ring { position: relative; width: 92px; height: 92px; flex-shrink: 0; }
.score-ring svg { transform: rotate(-90deg); }
.score-num {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 800; font-size: 1.5rem; line-height: 1;
}
.score-num small { font-size: 0.62rem; font-weight: 600; color: var(--text-dim); margin-top: 2px; }
.score-label { font-size: 1.3rem; font-weight: 800; }
.score-detail { color: var(--text-dim); font-size: 0.85rem; margin-top: 6px; line-height: 1.5; }

/* ---------- Bloque origen ---------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 18px;
  margin-top: 12px;
}
.card h2 {
  font-size: 0.85rem; text-transform: uppercase; letter-spacing: .8px;
  color: var(--text-dim); font-weight: 700; margin-bottom: 14px;
}
.origin-card { border-color: rgba(59, 130, 246, .45); box-shadow: 0 0 0 1px rgba(59, 130, 246, .15) inset; }
.origin-card h2 { color: var(--accent-2); }

.origin-row { display: flex; gap: 12px; align-items: flex-start; padding: 9px 0; }
.origin-row + .origin-row { border-top: 1px solid var(--border); }
.origin-ico { font-size: 1.35rem; width: 30px; text-align: center; flex-shrink: 0; }
.origin-k { color: var(--text-dim); font-size: 0.8rem; }
.origin-v { font-weight: 600; font-size: 1rem; margin-top: 1px; line-height: 1.35; }
.origin-note { color: var(--text-dim); font-size: 0.78rem; margin-top: 3px; line-height: 1.4; }
.origin-cta {
  margin-top: 14px; width: 100%;
  background: var(--bg-soft);
  border: 1px dashed var(--accent);
  color: var(--accent-2);
  border-radius: 12px;
  padding: 13px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.badge-community {
  display: inline-block; background: rgba(59,130,246,.15); color: var(--accent-2);
  font-size: 0.7rem; font-weight: 700; border-radius: 6px; padding: 2px 7px; margin-left: 6px;
  vertical-align: middle;
}

/* ---------- Aditivos ---------- */
.add-chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.add-chip {
  display: flex; align-items: center; gap: 6px;
  background: var(--bg-soft);
  border-radius: 20px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 600;
}
.dot { width: 11px; height: 11px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.dot.red { background: var(--red); }
.dot.orange { background: var(--orange); }
.dot.yellow { background: var(--yellow); }
.dot.green { background: var(--green); }
.dot.lime { background: var(--lime); }
.dot.grey { background: #6b7280; }

.add-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--border); }
.add-row:first-of-type { border-top: none; }
.add-code { font-weight: 700; font-size: 0.9rem; min-width: 52px; }
.add-name { color: var(--text-dim); font-size: 0.88rem; flex: 1; }

/* ---------- Nutrientes ---------- */
.nut-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; }
.nut-row + .nut-row { border-top: 1px solid var(--border); }
.nut-name { flex: 1; font-size: 0.95rem; }
.nut-val { color: var(--text-dim); font-size: 0.92rem; font-weight: 600; }

.empty-box { text-align: center; padding: 30px 10px; }
.empty-box .big { font-size: 3rem; margin-bottom: 12px; }
.empty-box p { color: var(--text-dim); line-height: 1.5; margin-top: 8px; }

/* ---------- Modales ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, .82);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  backdrop-filter: blur(4px);
}
.scanner-box { width: 100%; max-width: 480px; text-align: center; }
#reader { position: relative; border-radius: 18px; overflow: hidden; border: 2px solid var(--accent); background: #000; aspect-ratio: 3 / 4; max-height: 62vh; margin: 0 auto; }
#reader video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
/* Recuadro = zona que realmente se analiza (90% × 40% del vídeo, centrado) */
.scan-frame { position: absolute; left: 5%; right: 5%; top: 30%; bottom: 30%; border: 3px solid rgba(255,255,255,.9); border-radius: 12px; box-shadow: 0 0 0 9999px rgba(0,0,0,.45); }
.scan-laser { position: absolute; left: 6%; right: 6%; top: 50%; height: 2px; background: #ef4444; box-shadow: 0 0 10px #ef4444; animation: laser 1.6s ease-in-out infinite; }
@keyframes laser { 0%,100% { transform: translateY(-28px); opacity: .6; } 50% { transform: translateY(28px); opacity: 1; } }
.scan-torch { position: absolute; right: 12px; bottom: 12px; width: 48px; height: 48px; border-radius: 50%; border: none; background: rgba(0,0,0,.55); font-size: 1.3rem; cursor: pointer; }
.scan-torch.on { background: #facc15; }
.scanner-tip { color: var(--text-dim); margin: 14px 0; font-size: 0.9rem; }
.btn-ghost {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 24px;
  font-size: 1rem;
  cursor: pointer;
}

.contrib-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 22px;
  width: 100%; max-width: 440px;
}
.contrib-box h3 { font-size: 1.2rem; }
.contrib-box p { color: var(--text-dim); font-size: 0.9rem; margin: 10px 0 14px; line-height: 1.45; }
.contrib-box textarea {
  width: 100%; min-height: 90px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  padding: 12px;
  font-size: 0.95rem;
  font-family: inherit;
  resize: vertical;
  outline: none;
}
.contrib-box textarea:focus { border-color: var(--accent); }
.file-label {
  display: block; margin-top: 12px;
  color: var(--accent-2); font-size: 0.9rem; font-weight: 600; cursor: pointer;
}
.file-label input { display: block; margin-top: 6px; color: var(--text-dim); font-size: 0.8rem; }
.contrib-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.hp { position: absolute; left: -9999px; opacity: 0; }

/* ---------- Loading / footer ---------- */
.loading { position: fixed; inset: 0; z-index: 40; background: var(--overlay); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; }
.spinner { width: 44px; height: 44px; border: 4px solid var(--bg-soft); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading p { color: var(--text-dim); }

.footer {
  max-width: 560px; margin: 0 auto;
  padding: 10px 20px calc(env(safe-area-inset-bottom, 0px) + 26px);
  color: var(--footer-text); font-size: 0.72rem; line-height: 1.5; text-align: center;
}
.footer a { color: var(--text-dim); }
.footer p + p { margin-top: 6px; }

.toast {
  position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%);
  background: var(--bg-soft); border: 1px solid var(--accent);
  color: var(--text); border-radius: 12px; padding: 12px 20px;
  font-size: 0.92rem; z-index: 60; max-width: 90%;
  animation: fadein .2s ease;
}

/* --- Datos incompletos: sin nota inventada --- */
.score-card.nodata .score-ring { display: flex; align-items: center; justify-content: center; border: 8px solid var(--border); border-radius: 50%; box-sizing: border-box; }
.score-q { font-size: 2.2rem; font-weight: 800; color: var(--text-dim); }
.ns-letter { display: inline-block; min-width: 1.5em; text-align: center; border-radius: 6px; color: #fff; padding: 0 4px; }
.ns-a { background: #038141; } .ns-b { background: #85bb2f; } .ns-c { background: #e6a800; } .ns-d { background: #ee8100; } .ns-e { background: #e63e11; }
.data-warn { border-color: #b45309; }
.data-warn h2 { color: #f59e0b; }
.data-warn p { color: var(--text-dim); font-size: .9rem; line-height: 1.45; margin: 0 0 10px; }
.nut-warn { color: #f59e0b; font-size: .85rem; margin: 0 0 10px; line-height: 1.4; }
.ingr summary { cursor: pointer; font-weight: 700; font-size: .8rem; letter-spacing: .08em; text-transform: uppercase; color: var(--text-dim); }
.ingr p { color: var(--text-dim); font-size: .88rem; line-height: 1.5; margin: 10px 0 0; }

.comm-note { border-color: #1d4ed8; color: var(--text-dim); font-size: .88rem; line-height: 1.45; }
.comm-note b { color: var(--accent-2); }
