:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #1d2621;
  --muted: #66736c;
  --border: #dfe5e1;
  --primary: #1f7a4d;
  --primary-dark: #17603c;
  --primary-soft: #e3f2ea;
  --secondary: #eef1ef;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --warn: #b7791f;
  --warn-soft: #fff4dc;
  --info-soft: #e8f1fb;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, .06), 0 1px 2px rgba(0, 0, 0, .04);
  --nav-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121714;
    --card: #1b221e;
    --text: #e6ece8;
    --muted: #93a29a;
    --border: #2c3631;
    --primary: #3fae74;
    --primary-dark: #2f9862;
    --primary-soft: #1d3428;
    --secondary: #26302b;
    --danger: #e5675a;
    --danger-soft: #3a1f1c;
    --warn: #e0a843;
    --warn-soft: #3a2f17;
    --info-soft: #1b2a3a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(var(--nav-h) + 24px + env(safe-area-inset-bottom));
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.4rem; margin: .2rem 0 .8rem; }
h2 { font-size: 1.15rem; margin: 0 0 .7rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
code { background: var(--secondary); padding: .1em .4em; border-radius: 6px; }
img { max-width: 100%; }

/* ---------- Topo e navegação ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  padding: .6rem 1rem; background: var(--primary); color: #fff;
}
.topbar a { color: #fff; }
.brand { font-weight: 700; font-size: 1.1rem; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.house-name { opacity: .9; font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.house-switch select { background: rgba(255, 255, 255, .15); color: #fff; border: 0; padding: .35rem .5rem; border-radius: 8px; max-width: 45vw; }
.house-switch option { color: #000; }
.user-menu { position: relative; }
.user-menu summary {
  list-style: none; cursor: pointer; width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; background: rgba(255, 255, 255, .2); font-weight: 700;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu .menu {
  position: absolute; right: 0; top: 42px; background: var(--card); color: var(--text);
  border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0, 0, 0, .18); padding: .6rem;
  min-width: 220px; display: flex; flex-direction: column; gap: .15rem;
}
.user-menu .menu a, .user-menu .menu .linklike { color: var(--text); padding: .5rem .6rem; border-radius: 8px; text-align: left; }
.user-menu .menu a:hover, .user-menu .menu .linklike:hover { background: var(--secondary); text-decoration: none; }
.user-menu .menu small { color: var(--muted); padding: 0 .6rem .4rem; border-bottom: 1px solid var(--border); margin-bottom: .3rem; }
.user-menu .menu strong { padding: 0 .6rem; }
.linklike { background: none; border: 0; font: inherit; cursor: pointer; width: 100%; }

.mainnav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; justify-content: space-around; background: var(--card);
  border-top: 1px solid var(--border); height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
}
.mainnav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); font-size: .72rem; gap: 2px;
}
.mainnav a .ico { font-size: 1.3rem; }
.mainnav a.active { color: var(--primary); font-weight: 600; }
.mainnav a:hover { text-decoration: none; }

@media (min-width: 900px) {
  body { padding-bottom: 2rem; }
  .mainnav { position: sticky; top: 54px; bottom: auto; height: auto; padding: .3rem; border-top: 0; border-bottom: 1px solid var(--border); justify-content: center; gap: 1rem; }
  .mainnav a { flex: 0 0 auto; flex-direction: row; font-size: .95rem; padding: .4rem .9rem; border-radius: 999px; gap: .4rem; }
  .mainnav a .ico { font-size: 1rem; }
  .mainnav a.active { background: var(--primary-soft); }
}

.container { max-width: 960px; margin: 0 auto; padding: 1rem; }

/* ---------- Cartões ---------- */
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; margin-bottom: 1rem; box-shadow: var(--shadow);
}
.card.narrow { max-width: 520px; margin-left: auto; margin-right: auto; }
.card.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.cards { display: grid; gap: .8rem; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); margin-bottom: 1rem; }
.cards .card { margin-bottom: 0; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: .6rem; flex-wrap: wrap; margin-bottom: .6rem; }
.section-head h1, .section-head h2, .section-head h3 { margin: 0; }
.hero h1 { font-size: 1.3rem; }

.list-card { display: flex; flex-direction: column; gap: .5rem; }
.list-title { font-weight: 700; font-size: 1.05rem; color: var(--text); }
.list-stats strong { font-size: 1.6rem; color: var(--primary); }
.link-card { flex-direction: row; justify-content: space-between; align-items: center; color: var(--text); }
.link-card:hover { text-decoration: none; border-color: var(--primary); }

/* ---------- Formulários ---------- */
.field { margin-bottom: .8rem; display: flex; flex-direction: column; gap: .25rem; }
.field label, .edit-form label { font-size: .85rem; font-weight: 600; color: var(--muted); }
input[type=text], input[type=email], input[type=password], input[type=search], input:not([type]), select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--card);
  border: 1px solid var(--border); border-radius: 10px; padding: .6rem .7rem; min-height: 44px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.hint { color: var(--muted); font-size: .8rem; }
.check { display: flex; align-items: center; gap: .5rem; margin: .4rem 0 .8rem; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--primary); }
fieldset.box { border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem; margin: 0 0 1rem; }
legend { font-weight: 700; padding: 0 .3rem; }

.input-group { display: flex; gap: .4rem; align-items: stretch; }
.input-group > input, .input-group > .ac-wrap, .input-group > select { flex: 1; min-width: 0; }
.input-group.big input { font-size: 1.1rem; min-height: 52px; }
.qty-input { max-width: 80px; }
.icon-input { max-width: 64px; text-align: center; }
.order-input { max-width: 64px; }
.inline-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.inline-form > input, .inline-form > select { flex: 1 1 160px; }
.inline-form.grow { width: 100%; }
.inline-form.grow > input[name=name] { flex: 3 1 140px; }
.inline-form.grow > .icon-input, .inline-form.grow > .order-input { flex: 0 0 60px; }
form.inline { display: inline; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; }
.filters > * { flex: 1 1 160px; }
.filters > .btn { flex: 0 0 auto; }

.grid-2, .grid-3 { display: grid; gap: .6rem; grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: 1fr 1fr 1fr; }
@media (max-width: 520px) {
  .grid-3 { grid-template-columns: 1fr 1fr; }
  .grid-3 > :first-child { grid-column: span 2; }
  .grid-3.compact > :first-child { grid-column: auto; }
  .grid-3.compact > :last-child { grid-column: span 2; }
}
.compact .field { margin-bottom: .5rem; }
.dim { opacity: .45; }

/* ---------- Botões ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  font: inherit; font-weight: 600; cursor: pointer; border: 1px solid transparent;
  border-radius: 10px; padding: .55rem 1rem; min-height: 44px;
  background: var(--secondary); color: var(--text); white-space: nowrap;
}
.btn:hover { text-decoration: none; filter: brightness(.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--secondary); border-color: var(--border); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-small { padding: .3rem .65rem; min-height: 36px; font-size: .88rem; }
.btn-block { width: 100%; min-height: 50px; font-size: 1.05rem; }
.actions { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }
.mt { margin-top: 1rem; }
.center { text-align: center; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }

.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip {
  font: inherit; font-size: .9rem; border: 1px solid var(--border); background: var(--primary-soft);
  color: var(--primary); border-radius: 999px; padding: .35rem .8rem; cursor: pointer; min-height: 36px;
}
.badge { display: inline-block; font-size: .72rem; font-weight: 700; padding: .12rem .5rem; border-radius: 999px; background: var(--secondary); color: var(--muted); vertical-align: middle; }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-good { background: var(--primary-soft); color: var(--primary); }

/* ---------- Mensagens ---------- */
.flash { padding: .7rem .9rem; border-radius: 10px; margin-bottom: .8rem; background: var(--info-soft); }
.flash-success { background: var(--primary-soft); color: var(--primary); }
.flash-error { background: var(--danger-soft); color: var(--danger); }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 20px + env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  background: #26302b; color: #fff; padding: .7rem 1rem; border-radius: 10px; opacity: 0; pointer-events: none;
  transition: .25s; z-index: 100; max-width: 90vw; box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.success { background: var(--primary); }
.toast.error { background: var(--danger); }

/* ---------- Autocompletar ---------- */
.ac-wrap { position: relative; }
.ac-list {
  position: absolute; z-index: 30; left: 0; right: 0; top: 100%; margin-top: 2px;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .15); max-height: 300px; overflow: auto;
}
.ac-item { padding: .6rem .8rem; cursor: pointer; border-bottom: 1px solid var(--border); }
.ac-item:last-child { border-bottom: 0; }
.ac-item:hover, .ac-item.active { background: var(--primary-soft); }

/* ---------- Listas de itens ---------- */
.group h3 { color: var(--muted); font-size: .9rem; text-transform: uppercase; letter-spacing: .03em; }
.items { list-style: none; margin: 0; padding: 0; }
.item { display: flex; justify-content: space-between; gap: .6rem; padding: .65rem 0; border-top: 1px solid var(--border); }
.item:first-child { border-top: 0; }
.item-main { display: flex; flex-direction: column; min-width: 0; gap: .1rem; }
.item-desc { font-weight: 600; overflow-wrap: anywhere; }
.qty { color: var(--muted); font-weight: 500; }
.item-side { display: flex; flex-direction: column; align-items: flex-end; gap: .25rem; flex-shrink: 0; }
.item-side.buttons { flex-direction: row; align-items: center; }
.status-bought .item-desc { color: var(--muted); }
.status-skipped .item-desc { text-decoration: line-through; color: var(--muted); }
.price { font-weight: 700; color: var(--primary); white-space: nowrap; }
.item-edit summary { list-style: none; cursor: pointer; padding: .2rem .4rem; border-radius: 8px; }
.item-edit summary::-webkit-details-marker { display: none; }
.item-edit[open] { position: relative; }
.edit-form {
  position: absolute; right: 0; z-index: 15; width: min(320px, 85vw); background: var(--card);
  border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18); display: flex; flex-direction: column; gap: .5rem;
}
.edit-form label { display: flex; flex-direction: column; gap: .2rem; }
.total-bar { display: flex; justify-content: space-between; align-items: center; background: var(--primary-soft); }
.total-bar strong { font-size: 1.3rem; color: var(--primary); }
.simple-list { list-style: none; padding: 0; margin: 0; }
.simple-list li { display: flex; justify-content: space-between; align-items: center; gap: .6rem; padding: .55rem 0; border-top: 1px solid var(--border); flex-wrap: wrap; }
.simple-list li:first-child { border-top: 0; }

/* ---------- Produtos ---------- */
.product-list { list-style: none; margin: 0; padding: 0; }
.product-list li { border-top: 1px solid var(--border); }
.product-list li:first-child { border-top: 0; }
.product-list a { display: flex; align-items: center; gap: .7rem; padding: .6rem 0; color: var(--text); }
.product-list a:hover { text-decoration: none; background: var(--secondary); }
.pl-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.pl-price { display: flex; flex-direction: column; align-items: flex-end; white-space: nowrap; }
.thumb { max-width: 120px; max-height: 120px; border-radius: 10px; object-fit: contain; background: #fff; display: block; margin-bottom: .6rem; }
.thumb-sm { width: 44px; height: 44px; border-radius: 8px; object-fit: contain; background: #fff; flex-shrink: 0; }
.thumb-sm.ph { display: grid; place-items: center; background: var(--secondary); font-size: 1.3rem; }
.product-head { display: flex; gap: 1rem; flex-wrap: wrap; }
.meta { display: grid; grid-template-columns: auto 1fr; gap: .2rem .8rem; margin: 0 0 .8rem; }
.meta dt { color: var(--muted); font-size: .85rem; }
.meta dd { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem; display: flex; flex-direction: column; }
.stat span { color: var(--muted); font-size: .8rem; }
.stat strong { font-size: 1.25rem; }
.stat small { color: var(--muted); }
.stat.good strong { color: var(--primary); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: .5rem .4rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-size: .78rem; text-transform: uppercase; font-weight: 600; }
tr.best td { background: var(--primary-soft); }

/* ---------- Barras (histórico) ---------- */
.bars { list-style: none; margin: 0; padding: 0; }
.bars li, .bars li a { display: grid; grid-template-columns: minmax(80px, 1.2fr) 3fr auto; gap: .6rem; align-items: center; padding: .25rem 0; color: var(--text); }
.bars li a { grid-column: 1 / -1; }
.bars li a:hover { text-decoration: none; }
.bar { height: 14px; background: var(--secondary); border-radius: 999px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.bars li.current .bar-label { font-weight: 700; color: var(--primary); }
.bar-value { font-variant-numeric: tabular-nums; font-size: .9rem; white-space: nowrap; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; }
.pager { display: flex; justify-content: space-between; }

/* ---------- Casa ---------- */
.invite { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-bottom: .5rem; }
.invite code { font-size: 1.4rem; letter-spacing: .12em; padding: .4rem .8rem; }
.danger-zone { border-color: var(--danger-soft); }

/* ---------- Modo compras ---------- */
.shop-head { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; position: sticky; top: 54px; z-index: 10; }
.shop-head h1 { margin-bottom: .3rem; }
.shop-total { display: flex; flex-direction: column; align-items: flex-end; }
.shop-total strong { font-size: 1.6rem; color: var(--primary); }
.store-form { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; }
.store-form select { width: auto; min-height: 36px; padding: .3rem .5rem; }
.store-form .input-group { width: 100%; flex-wrap: wrap; }
@media (min-width: 900px) { .shop-head { top: 104px; } }
@media (max-width: 520px) {
  .shop-head { position: static; }
  .shop-total { align-items: flex-start; }
}
.group.bought { border-color: var(--primary-soft); }
.fab {
  position: fixed; right: 16px; bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); z-index: 25;
  border: 0; border-radius: 999px; background: var(--primary); color: #fff; font: inherit; font-weight: 700;
  font-size: 1.4rem; padding: .8rem 1.2rem; box-shadow: 0 6px 20px rgba(0, 0, 0, .3); cursor: pointer;
  display: flex; align-items: center; gap: .5rem;
}
.fab span { font-size: 1rem; }
@media (min-width: 900px) { .fab { bottom: 24px; } }

/* ---------- Diálogos ---------- */
.dialog {
  border: 0; border-radius: 16px; padding: 1.1rem; width: min(480px, 94vw); max-height: 90vh;
  background: var(--card); color: var(--text); box-shadow: 0 20px 50px rgba(0, 0, 0, .35);
}
.dialog::backdrop { background: rgba(0, 0, 0, .5); }
.dlg-head { display: flex; gap: .8rem; align-items: center; margin-bottom: .8rem; }
.dlg-head img { width: 64px; height: 64px; object-fit: contain; border-radius: 8px; background: #fff; }
.dlg-head h3 { margin: 0; }
.dlg-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .6rem; }
#buy-price { font-size: 1.4rem; font-weight: 700; }

/* ---------- Leitor de código de barras ---------- */
.scanner-modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .85); z-index: 200; display: none; align-items: center; justify-content: center; padding: 1rem; }
.scanner-modal.open { display: flex; }
.scanner-box { background: var(--card); border-radius: 16px; padding: 1rem; width: min(520px, 100%); }
.scanner-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; }
.scanner-view { position: relative; background: #000; border-radius: 12px; overflow: hidden; aspect-ratio: 4 / 3; }
.scanner-view video, .scanner-view #scanner-lib { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner-view video[hidden] { display: none; }
.scanner-line { position: absolute; left: 8%; right: 8%; top: 50%; height: 2px; background: #ff3b30; box-shadow: 0 0 8px #ff3b30; pointer-events: none; }
.scanner-msg { margin: .6rem 0; min-height: 1.2em; }
.auth h1 { text-align: center; }
.table-wrap td:not(:first-child) { white-space: nowrap; }

/* ---------- Receitas ---------- */
.recipe-card { align-items: flex-start; }
.total-bar.warn { background: var(--warn-soft); }
.total-bar.warn strong { font-size: inherit; color: var(--warn); }
.ing-row { display: flex; gap: .4rem; align-items: center; margin-bottom: .5rem; }
.ing-row .ing-amount { flex: 0 0 110px; }
.ing-row .ing-name { flex: 1; min-width: 0; }
.ing-row .check.opt { margin: 0; font-size: .8rem; white-space: nowrap; gap: .25rem; }
@media (max-width: 520px) {
  .ing-row { flex-wrap: wrap; padding-bottom: .5rem; border-bottom: 1px solid var(--border); }
  .ing-row .ing-amount { flex: 1 1 40%; }
  .ing-row .ing-name { flex: 1 1 100%; order: -1; }
}
.ing-check .item { padding: .3rem 0; }
.ing-label { margin: 0; align-items: flex-start; width: 100%; }
.ing-label input { margin-top: .2rem; flex-shrink: 0; }
.is-optional .item-desc { font-weight: 500; }
.instructions { line-height: 1.6; }
textarea { min-height: 120px; resize: vertical; }
button:disabled { opacity: .55; cursor: default; }
