:root {
  --bg: #f6f5f1;
  --card: #ffffff;
  --text: #1d2420;
  --muted: #5d6762;
  --border: #dcdcd4;
  --accent: #2f6b3f;
  --accent-text: #ffffff;
  --accent-soft: #e6f0e8;
  --danger: #a3302a;
  --warn-bg: #fff4d6;
  --warn-border: #e2c46b;
  --error-bg: #fde8e6;
  --note-bg: #eef3f8;
  --mark: #fff0a8;
  --radius: 12px;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121513;
    --card: #1b201d;
    --text: #e7ece8;
    --muted: #a3ada7;
    --border: #333b36;
    --accent: #6fbf86;
    --accent-text: #0d1a11;
    --accent-soft: #213027;
    --danger: #f08b84;
    --warn-bg: #3a3015;
    --warn-border: #7a6526;
    --error-bg: #3d1f1d;
    --note-bg: #1d2832;
    --mark: #5a4d12;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  /* Keep focused / scrolled-to controls clear of the fixed summary bar. */
  scroll-padding-bottom: calc(72px + env(safe-area-inset-bottom));
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}

.topbar {
  background: var(--accent);
  color: var(--accent-text);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
}
.topbar h1 {
  margin: 0 auto;
  max-width: 720px;
  font-size: 1.15rem;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 16px;
}

h2 {
  font-size: 1.1rem;
  margin: 0 0 8px;
}
h3 {
  font-size: 1rem;
  margin: 8px 0;
}
h4 {
  font-size: 0.95rem;
  margin: 12px 0 6px;
}
.section-title {
  margin: 16px 0 8px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin: 0 0 12px;
}

.row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 8px 0;
}
.row.wrap {
  flex-wrap: wrap;
}
.row > input,
.row > select {
  flex: 1 1 auto;
  min-width: 0;
}

.grid2,
.grid3 {
  display: grid;
  gap: 8px;
  grid-template-columns: 1fr 1fr;
}
.grid3 {
  grid-template-columns: 1fr 1.3fr 1fr;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 8px 0;
  min-width: 0;
}
.field > span,
.field > label,
fieldset.field legend {
  font-size: 0.85rem;
  color: var(--muted);
}
fieldset.field {
  border: 0;
  padding: 0;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 4px 16px;
}
.field-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin: 4px 0;
}

input,
select,
button {
  font: inherit;
  color: inherit;
}
input:not([type="checkbox"]):not([type="radio"]),
select {
  min-height: var(--tap);
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  width: 100%;
  /* 16px+ prevents iOS Safari zooming into focused inputs */
  font-size: 16px;
}
input[aria-invalid="true"] {
  border-color: var(--danger);
}
input:focus-visible,
select:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 2px;
}

.btn {
  min-height: var(--tap);
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn:disabled {
  opacity: 0.55;
  cursor: default;
}
.btn-secondary {
  background: var(--accent-soft);
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: transparent;
}
.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--border);
}
.link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
  min-height: 0;
}
a {
  color: var(--accent);
}

.check {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  min-height: var(--tap);
}
.check input {
  width: 20px;
  height: 20px;
}

.muted {
  color: var(--muted);
}
.small {
  font-size: 0.85rem;
}

.alert {
  border-radius: 8px;
  padding: 10px 12px;
  margin: 8px 0;
  font-size: 0.92rem;
}
.alert-error {
  background: var(--error-bg);
}
.alert-warn {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
}
.alert-note {
  background: var(--note-bg);
}

.badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 999px;
  padding: 2px 8px;
  border: 1px solid var(--border);
}
.badge-generic {
  background: var(--accent-soft);
}
.badge-branded {
  background: var(--note-bg);
}
.badge-manual {
  background: var(--warn-bg);
}
.brand {
  font-weight: 600;
}
.source {
  font-size: 0.85rem;
}

mark {
  background: var(--mark);
  color: inherit;
  border-radius: 3px;
  padding: 0 2px;
}

/* Ingredients */
.ingredients {
  list-style: none;
  margin: 0;
  padding: 0;
}
.ingredient.needs-attention {
  border-color: var(--warn-border);
}
.ing-amount {
  font-weight: 700;
}
.ing-food {
  font-weight: 600;
}
.amount-line {
  margin: 6px 0;
  font-variant-numeric: tabular-nums;
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  list-style: none;
  padding: 0;
  margin: 8px 0;
}
.chips li {
  font-size: 0.82rem;
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 3px 9px;
  font-variant-numeric: tabular-nums;
}
.chips li.missing {
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
}
.actions {
  margin-top: 12px;
}
.amount-editor {
  border-top: 1px solid var(--border);
  margin-top: 8px;
  padding-top: 4px;
}

/* Matches */
.match {
  border-top: 1px solid var(--border);
  margin-top: 8px;
  padding-top: 8px;
}
.candidates {
  list-style: none;
  margin: 8px 0;
  padding: 0;
  max-height: 60vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.candidates li + li {
  border-top: 1px solid var(--border);
}
.candidate {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 10px 12px;
  min-height: var(--tap);
  cursor: pointer;
}
.candidate:hover,
.candidate:focus-visible {
  background: var(--accent-soft);
}
.cand-name {
  font-weight: 600;
}
.cand-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  font-size: 0.8rem;
  align-items: center;
}

/* Barcode */
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.scanner {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.viewfinder {
  position: relative;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  aspect-ratio: 4 / 3;
}
.viewfinder video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.reticle {
  position: absolute;
  left: 10%;
  right: 10%;
  top: 35%;
  bottom: 35%;
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 8px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.25);
}
.product {
  border-top: 1px solid var(--border);
  margin-top: 10px;
}
.facts {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 12px;
  font-size: 0.9rem;
  margin: 8px 0;
}
.facts dt {
  color: var(--muted);
}
.facts dd {
  margin: 0;
}

/* Totals */
.totals table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}
.totals th,
.totals td {
  text-align: right;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
}
.totals th:first-child,
.totals tbody th {
  text-align: left;
}
.totals thead th {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}
.totals tr.sub th {
  padding-left: 16px;
  font-weight: 400;
  color: var(--muted);
}
.totals tr.partial td {
  color: var(--muted);
}
.footnote {
  font-size: 0.82rem;
  margin: 8px 0 0;
}
.footnote ul {
  margin: 4px 0 0;
  padding-left: 18px;
}

.stepper {
  display: grid;
  grid-template-columns: var(--tap) 1fr var(--tap);
  gap: 8px;
  max-width: 220px;
}
.stepper input {
  text-align: center;
}

.attribution {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 16px 0;
}

.summary-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--accent);
  color: var(--accent-text);
  text-align: center;
  font-weight: 600;
  text-decoration: none;
  font-variant-numeric: tabular-nums;
  z-index: 10;
}

@media (max-width: 380px) {
  .grid3 {
    grid-template-columns: 1fr 1fr;
  }
  .btn {
    padding: 8px 10px;
  }
}
