:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #1d2420;
  --muted: #6b736e;
  --line: #e6e1d8;
  --brand: #1f6f4a;
  --brand-ink: #ffffff;
  --brand-soft: #e3f1e9;
  --danger: #b4232f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 14px rgba(0, 0, 0, .06);
  --continente: #e2001a;
  --pingodoce: #4c9a2a;
  --auchan: #d6180b;
  --intermarche: #c8102e;
  --mercadona: #00843d;
  --lidl: #0050aa;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --card: #1c2220;
    --ink: #e8ece9;
    --muted: #9aa49e;
    --line: #2b332f;
    --brand: #3fa874;
    --brand-ink: #0c1410;
    --brand-soft: #1d3329;
    --danger: #ff6b74;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; max-width: 100%; overflow-x: hidden; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: inherit; }
h1, h2 { margin: 0; letter-spacing: -.01em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.15rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.err { color: var(--danger); min-height: 1.2em; margin: .25rem 0 0; font-size: .9rem; }

/* Buttons */
.btn {
  border: 0; border-radius: 12px; padding: .8rem 1.1rem; font-weight: 600;
  background: var(--line); transition: transform .08s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid currentColor; }
.btn.round { width: 48px; height: 48px; padding: 0; border-radius: 50%; font-size: 1.5rem; flex: none; }
.btn.grow { flex: 1; }
.icon-btn {
  background: transparent; border: 0; width: 42px; height: 42px; border-radius: 50%;
  font-size: 1.3rem; display: grid; place-items: center; flex: none;
}
.icon-btn:active { background: var(--line); }
.link-btn { background: none; border: 0; color: var(--brand); font-weight: 600; padding: .25rem 0; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  background: var(--card); padding: 2rem 1.5rem; border-radius: 20px; box-shadow: var(--shadow);
  width: min(380px, 100%); display: grid; gap: .8rem; text-align: center;
}
.login-card img { margin: 0 auto; }
.login-card p { margin: 0; }

input[type=password], input[type=search], .field input {
  width: 100%; padding: .8rem .9rem; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); outline: none;
}
input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }

/* Header */
.top {
  position: sticky; top: 0; z-index: 5; background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.top-row { display: flex; justify-content: space-between; align-items: center; max-width: 640px; margin: 0 auto; }
.top-row p { margin: .1rem 0 0; }
.total {
  max-width: 640px; margin: 10px auto 0; display: flex; justify-content: space-between; align-items: baseline;
  background: var(--brand-soft); padding: .6rem .9rem; border-radius: 12px; font-size: .95rem;
}
.total strong { font-size: 1.2rem; color: var(--brand); }

.wrap { max-width: 640px; margin: 0 auto; padding: 12px 16px calc(100px + env(safe-area-inset-bottom)); }

/* Empty */
.empty { text-align: center; padding: 3rem 1rem 1rem; }
.empty p { margin: .3rem 0; }
.empty-emoji { font-size: 3rem; }

/* List */
.list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.item {
  display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: var(--radius);
  padding: 10px 12px; box-shadow: var(--shadow);
}
.check {
  width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--line); background: transparent; flex: none;
  display: grid; place-items: center; font-size: 1rem; color: transparent;
}
.done .check { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.thumb {
  width: 52px; height: 52px; border-radius: 10px; object-fit: contain; background: #fff; flex: none;
  border: 1px solid var(--line);
}
.thumb.ph {
  display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); font-weight: 700;
  font-size: 1.3rem; border: 0;
}
.item-main { flex: 1; min-width: 0; background: none; border: 0; padding: 0; text-align: left; }
.item-name { font-weight: 600; overflow-wrap: anywhere; }
.item-meta { min-width: 0; overflow-wrap: anywhere; font-size: .82rem; color: var(--muted); margin-top: 2px; display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; }
.item-note { overflow-wrap: anywhere; font-size: .85rem; margin-top: 2px; font-style: italic; color: var(--muted); }
.qty {
  flex: none; min-width: 38px; text-align: center; font-weight: 700; background: var(--brand-soft); color: var(--brand);
  border-radius: 999px; padding: .2rem .55rem; font-size: .95rem;
}
.done .item { opacity: .6; box-shadow: none; }
.done .item-name { text-decoration: line-through; }

.badge {
  display: inline-block; font-size: .72rem; font-weight: 700; color: #fff; padding: 1px 7px; border-radius: 999px;
  background: var(--muted); letter-spacing: .01em;
}
.badge.continente { background: var(--continente); }
.badge.pingodoce { background: var(--pingodoce); }
.badge.auchan { background: var(--auchan); }
.badge.intermarche { background: var(--intermarche); }
.badge.mercadona { background: var(--mercadona); }
.badge.lidl { background: var(--lidl); }
.chip.tag.on.continente { background: var(--continente); border-color: var(--continente); color: #fff; }
.chip.tag.on.pingodoce { background: var(--pingodoce); border-color: var(--pingodoce); color: #fff; }
.chip.tag.on.auchan { background: var(--auchan); border-color: var(--auchan); color: #fff; }
.chip.tag.on.intermarche { background: var(--intermarche); border-color: var(--intermarche); color: #fff; }
.chip.tag.on.mercadona { background: var(--mercadona); border-color: var(--mercadona); color: #fff; }
.chip.tag.on.lidl { background: var(--lidl); border-color: var(--lidl); color: #fff; }
.chip.tag { font-size: .85rem; padding: .35rem .7rem; }

.section-head {
  display: flex; justify-content: space-between; align-items: center; margin: 22px 2px 8px;
  font-size: .85rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
.section-head .link-btn { text-transform: none; letter-spacing: 0; font-size: .9rem; }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips.nowrap { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.chip {
  border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: .45rem .8rem;
  font-size: .9rem; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px;
  max-width: 100%; min-width: 0;
}
/* Nos frequentes os nomes podem ser compridos: deixar quebrar a linha em vez de alargar o ecrã */
.chips:not(.nowrap) .chip { white-space: normal; text-align: left; border-radius: 14px; overflow-wrap: anywhere; }
.chip img { flex: none; }
.chip img { width: 22px; height: 22px; object-fit: contain; border-radius: 4px; background: #fff; }
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.chip .x { color: var(--danger); font-weight: 700; }

/* Add bar */
.addbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; gap: 8px; align-items: center;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
}
.addbar input { flex: 1; min-width: 0; max-width: 580px; margin-left: auto; height: 48px; border-radius: 999px; padding: 0 1.1rem; }
.addbar .btn { margin-right: auto; }
@media (min-width: 672px) { .addbar { justify-content: center; } .addbar input, .addbar .btn { margin: 0; } }

/* Dialogs */
dialog.sheet {
  border: 0; padding: 0; background: var(--card); color: var(--ink);
  width: min(560px, 100vw); max-width: 100vw; max-height: 92dvh;
  margin: auto auto 0; border-radius: 20px 20px 0 0; box-shadow: 0 -10px 40px rgba(0, 0, 0, .25);
  overflow-x: hidden;
}
@media (min-width: 600px) { dialog.sheet { margin: auto; border-radius: 20px; } }
dialog.sheet::backdrop { background: rgba(0, 0, 0, .45); }
dialog.sheet.full { height: 100dvh; max-height: 100dvh; margin: 0 auto; border-radius: 0; display: flex; flex-direction: column; }
dialog.sheet.full:not([open]) { display: none; }
@media (min-width: 600px) { dialog.sheet.full { height: 90dvh; margin: auto; border-radius: 20px; } }

.sheet-head { display: flex; align-items: center; gap: 6px; padding: calc(8px + env(safe-area-inset-top)) 10px 8px; }
.sheet-head.plain { padding: 0; justify-content: space-between; }
.search-form { flex: 1; min-width: 0; }
.filters { display: flex; gap: 10px; align-items: center; padding: 0 12px 10px; border-bottom: 1px solid var(--line); }
.filters .chips { flex: 1; min-width: 0; }
.toggle { font-size: .85rem; white-space: nowrap; display: flex; gap: 4px; align-items: center; color: var(--muted); }
.sheet-body { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px; }

.manual-card {
  width: 100%; display: flex; gap: 12px; align-items: center; text-align: left; padding: 12px;
  background: var(--brand-soft); border: 1px dashed var(--brand); border-radius: var(--radius); margin-bottom: 12px;
}
.manual-card .plus {
  width: 40px; height: 40px; border-radius: 50%; background: var(--brand); color: var(--brand-ink);
  display: grid; place-items: center; font-size: 1.3rem; flex: none;
}

.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 520px) { .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.product {
  min-width: 0; overflow: hidden;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px;
  display: flex; flex-direction: column; gap: 6px; text-align: left;
}
.product:active { transform: scale(.98); }
.product .pimg { width: 100%; aspect-ratio: 1; object-fit: contain; background: #fff; border-radius: 10px; }
.product .pname { overflow-wrap: anywhere; font-size: .88rem; font-weight: 600; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.product .ppack { font-size: .78rem; color: var(--muted); }
.product .prow { display: flex; justify-content: space-between; align-items: center; margin-top: auto; gap: 4px; }
.product .pprice { font-weight: 800; font-size: 1.05rem; }
.product.best { border-color: var(--brand); box-shadow: 0 0 0 2px var(--brand-soft); }
.best-tag { font-size: .7rem; font-weight: 700; color: var(--brand); }

.skel { background: linear-gradient(90deg, var(--line), var(--bg), var(--line)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: var(--radius); height: 230px; }
@keyframes sk { to { background-position: -200% 0; } }

/* Item form */
.item-form { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); display: grid; gap: 14px; }
.item-form h2 + p { margin: -6px 0 0; }
.field { display: grid; gap: 6px; font-size: .9rem; font-weight: 600; }
.field input { font-weight: 400; }
.photo-row { display: flex; gap: 14px; align-items: center; }
.photo-pick {
  width: 96px; height: 96px; border-radius: 14px; border: 2px dashed var(--line); display: grid; place-items: center;
  text-align: center; color: var(--muted); font-size: 1.5rem; line-height: 1.1; flex: none; overflow: hidden; cursor: pointer; background: var(--bg);
}
.photo-pick small { font-size: .7rem; }
.photo-pick img { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.photo-info { flex: 1; min-width: 0; display: grid; gap: 4px; }
.product-info { font-size: .88rem; display: grid; gap: 4px; min-width: 0; overflow-wrap: anywhere; }
.product-info .big { font-size: 1.3rem; font-weight: 800; }
.product-info a { color: var(--brand); }

.stepper { display: flex; align-items: center; gap: 0; border: 1px solid var(--line); border-radius: 12px; width: fit-content; overflow: hidden; }
.stepper button { width: 52px; height: 48px; border: 0; background: var(--bg); font-size: 1.4rem; }
.stepper input { width: 70px; text-align: center; border: 0; border-radius: 0; font-size: 1.15rem; font-weight: 700; padding: .6rem 0; -moz-appearance: textfield; }
.stepper input::-webkit-inner-spin-button, .stepper input::-webkit-outer-spin-button { -webkit-appearance: none; }
.stepper input:focus { box-shadow: none; }
.actions { display: flex; gap: 10px; }

.menu-item { text-align: left; background: var(--bg); border: 0; border-radius: 12px; padding: .9rem 1rem; display: flex; gap: 8px; justify-content: space-between; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--ink); color: var(--bg); padding: .65rem 1rem; border-radius: 999px; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
