:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #eef2ec;
  --text: #1b2420;
  --muted: #66736c;
  --border: #dde4dc;
  --primary: #1f8a4c;
  --primary-ink: #ffffff;
  --primary-soft: #e3f3e9;
  --accent: #f29c1f;
  --danger: #c93a32;
  --danger-soft: #fbe9e7;
  --shadow: 0 1px 2px rgba(16, 32, 24, .06), 0 4px 16px rgba(16, 32, 24, .06);
  --radius: 14px;
  --tabbar-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1412;
    --surface: #19201c;
    --surface-2: #222b26;
    --text: #e8efea;
    --muted: #93a199;
    --border: #2c3630;
    --primary: #3cb371;
    --primary-ink: #06130b;
    --primary-soft: #1d3326;
    --danger: #ef6b61;
    --danger-soft: #3a1f1c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1412;
  --surface: #19201c;
  --surface-2: #222b26;
  --text: #e8efea;
  --muted: #93a199;
  --border: #2c3630;
  --primary: #3cb371;
  --primary-ink: #06130b;
  --primary-soft: #1d3326;
  --danger: #ef6b61;
  --danger-soft: #3a1f1c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--primary);
  color: var(--primary-ink);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  max-width: 760px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.15rem; }
.brand img { border-radius: 7px; background: #fff; }
.list-switch {
  display: flex; align-items: center; gap: 6px;
  background: rgba(255, 255, 255, .18); border: 0; border-radius: 999px;
  padding: 7px 12px; cursor: pointer; max-width: 55vw;
  color: inherit; font-weight: 600;
}
.list-switch span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

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

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin-bottom: 12px;
}
.card h2 { font-size: 1.05rem; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* Summary */
.summary-row { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; }
.summary .label { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.summary .big { font-size: 1.9rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.summary .mid { font-size: 1.2rem; font-weight: 700; color: var(--primary); font-variant-numeric: tabular-nums; }
.summary .right { text-align: right; }
.budget { margin-top: 10px; }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.fill { height: 100%; width: 0; background: var(--primary); border-radius: 99px; transition: width .3s; }
.fill.warn { background: var(--accent); }
.fill.over { background: var(--danger); }
.budget-text { font-size: .85rem; color: var(--muted); margin-top: 5px; }

/* Search */
.search-wrap { position: relative; }
.search-wrap.sticky { position: sticky; top: 58px; z-index: 10; padding: 4px 0 8px; background: var(--bg); }
.search-ico { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search-wrap.sticky .search-ico { top: calc(50% - 2px); }
input[type="search"], input[type="text"], input[type="number"], select {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 12px;
  outline: none;
}
input[type="search"] { padding-left: 40px; }
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }

.quickadd { position: relative; margin-bottom: 12px; }
.suggest {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 15;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); overflow: hidden; max-height: 60vh; overflow-y: auto;
}
.suggest button {
  display: flex; width: 100%; align-items: center; gap: 10px; text-align: left;
  padding: 10px 12px; background: none; border: 0; border-bottom: 1px solid var(--border); cursor: pointer;
}
.suggest button:last-child { border-bottom: 0; }
.suggest button:hover, .suggest button.hl { background: var(--surface-2); }
.suggest .s-main { flex: 1; min-width: 0; }
.suggest .s-name { font-weight: 600; }
.suggest .s-sub { font-size: .8rem; color: var(--muted); }
.suggest .s-price { font-weight: 700; font-variant-numeric: tabular-nums; }

/* List */
.group-title {
  display: flex; align-items: center; gap: 8px;
  font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); margin: 16px 4px 6px;
}
.group-title .gsum { margin-left: auto; font-variant-numeric: tabular-nums; }
.items { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.item:last-child { border-bottom: 0; }
.check {
  flex: none; width: 26px; height: 26px; border-radius: 50%;
  border: 2px solid var(--border); background: var(--surface); cursor: pointer;
  display: grid; place-items: center; padding: 0;
}
.check svg { opacity: 0; }
.item.done .check { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.item.done .check svg { opacity: 1; }
.item-main { flex: 1; min-width: 0; cursor: pointer; }
.item-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.item-sub { font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item.done .item-name { text-decoration: line-through; color: var(--muted); }
.item-right { flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 5px; }
.item-price { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.stepper { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 10px; flex: none; }
.stepper button { width: 32px; height: 28px; border: 0; background: none; cursor: pointer; font-size: 1.1rem; color: var(--primary); }
.stepper span { min-width: 34px; text-align: center; font-size: .9rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.empty .em { font-size: 2.4rem; }
.empty p { margin: 6px 0; }

.list-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.list-actions .primary { flex: 1 1 100%; }
.list-actions .btn:not(.primary) { flex: 1; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 11px 16px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 600; text-align: center;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 8px 12px; font-size: .9rem; }
.btn:active { transform: scale(.98); }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.btn-row .btn { flex: 1 1 160px; }
.icon-btn { background: none; border: 0; cursor: pointer; font-size: 1.1rem; padding: 6px 8px; color: var(--muted); border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }

/* Catalog */
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 12px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-size: .88rem; white-space: nowrap;
}
.chip.active { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.cat-toolbar { white-space: nowrap; display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 4px 0 10px; }
.toolbar-right { display: flex; gap: 8px; align-items: center; }
.toolbar-right { min-width: 0; }
.toolbar-right select { width: auto; max-width: 150px; padding: 8px 10px; font-size: .9rem; }

.products { display: grid; gap: 8px; }
.product {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 12px;
}
.p-ico { flex: none; width: 40px; height: 40px; border-radius: 10px; background: var(--surface-2); display: grid; place-items: center; font-size: 1.3rem; }
.p-main { flex: 1; min-width: 0; cursor: pointer; }
.p-name { font-weight: 600; }
.p-sub { font-size: .8rem; color: var(--muted); }
.p-price { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.p-price .edited { display: block; font-size: .7rem; color: var(--accent); font-weight: 600; }
.p-price .src { display: block; font-size: .68rem; color: var(--primary); font-weight: 600; }
.p-price .unit { font-size: .75rem; color: var(--muted); font-weight: 500; }
.add-btn {
  flex: none; width: 38px; height: 38px; border-radius: 50%; border: 0;
  background: var(--primary-soft); color: var(--primary); font-size: 1.4rem; font-weight: 700; cursor: pointer;
  position: relative;
}
.add-btn.in { background: var(--primary); color: var(--primary-ink); font-size: .95rem; }
.fav { color: var(--accent); }
.more-btn { margin: 12px auto 0; display: flex; }

/* History */
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat .label { font-size: .75rem; color: var(--muted); }
.stat .val { font-size: 1.15rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.months { margin-top: 14px; display: grid; gap: 6px; }
.month-row { display: grid; grid-template-columns: 70px 1fr 80px; align-items: center; gap: 8px; font-size: .85rem; }
.month-row .bar { height: 10px; }
.month-row .amt { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.trip { cursor: pointer; }
.trip-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.trip-title { font-weight: 700; }
.trip-total { font-weight: 800; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.trip-body { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 8px; }
.trip-line { display: flex; justify-content: space-between; gap: 8px; font-size: .9rem; padding: 3px 0; }
.trip-line span:last-child { font-variant-numeric: tabular-nums; }

/* Forms */
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.lists-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.lists-row:last-child { border-bottom: 0; margin-bottom: 10px; }
.lists-row .ln { flex: 1; font-weight: 600; }
.lists-row .ln small { display: block; font-weight: 400; color: var(--muted); }
.tag { font-size: .7rem; background: var(--primary-soft); color: var(--primary); padding: 2px 7px; border-radius: 99px; font-weight: 700; }
.info p { margin: 0 0 8px; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  position: relative; flex: 1; max-width: 190px; height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  background: none; border: 0; cursor: pointer; color: var(--muted); font-size: .75rem; font-weight: 600;
}
.tab svg { width: 24px; height: 24px; }
.tab.active { color: var(--primary); }
.badge {
  position: absolute; top: 8px; left: calc(50% + 6px);
  background: var(--danger); color: #fff; font-size: .68rem; min-width: 18px; height: 18px;
  border-radius: 99px; display: grid; place-items: center; padding: 0 5px;
}

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .45);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--surface); width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto;
  border-radius: 20px 20px 0 0; padding: 6px 16px calc(16px + env(safe-area-inset-bottom));
  animation: up .2s ease-out;
}
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: 20px; }
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 0; }
.modal-head h3 { margin: 0; font-size: 1.1rem; }
.modal-body .meta { color: var(--muted); font-size: .9rem; margin: -4px 0 14px; }
.modal-body .btn-row { margin-top: 4px; }
.pick-list { display: grid; gap: 6px; margin-bottom: 12px; }
.pick-list button {
  display: flex; justify-content: space-between; align-items: center; text-align: left;
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); cursor: pointer;
}
.pick-list button.current { border-color: var(--primary); background: var(--primary-soft); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: .9rem; font-weight: 600; transition: all .25s; z-index: 60; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 380px) {
  .stepper button { width: 26px; }
  .toolbar-right select { max-width: 150px; }
}
