:root {
  --green: #1f5c3f;
  --green-dark: #17452f;
  --cream: #faf6ee;
  --card: #ffffff;
  --ink: #1d2420;
  --muted: #5f6b64;
  --line: #e4ddcf;
  --gold: #c98a1b;
  --red: #b3321f;
  --ok: #1f7a4a;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--cream);
  color: var(--ink);
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

#top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  background: var(--green); color: #fff;
  padding: 10px 16px; min-height: 56px;
}
#top h1 { font-size: 20px; margin: 0; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { background: none; border: 0; color: #fff; font-size: 26px; width: 44px; height: 44px; border-radius: 50%; margin-left: -8px; }

main { max-width: 560px; margin: 0 auto; padding: 16px; outline: none; }

#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; background: #fff; border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
#tabs button {
  flex: 1; background: none; border: 0; padding: 8px 2px 10px;
  font-size: 12px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-height: 60px;
}
#tabs button span { font-size: 22px; line-height: 1.1; filter: grayscale(1); opacity: .7; }
#tabs button.on { color: var(--green); font-weight: 700; }
#tabs button.on span { filter: none; opacity: 1; }

h2 { font-size: 18px; margin: 22px 0 10px; }
h2:first-child { margin-top: 4px; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 52px; padding: 10px 16px;
  border-radius: 14px; border: 1.5px solid var(--green);
  background: #fff; color: var(--green); font-weight: 700; text-decoration: none;
}
.btn.primary { background: var(--green); color: #fff; }
.btn.primary:active { background: var(--green-dark); }
.btn.big { min-height: 68px; font-size: 21px; border-radius: 18px; box-shadow: 0 4px 14px rgba(31,92,63,.25); }
.btn.wa { background: #128c4a; border-color: #128c4a; color: #fff; }
.btn.danger { border-color: var(--red); color: var(--red); }
.btn.slim { min-height: 44px; width: auto; padding: 6px 14px; font-size: 15px; }
.btn + .btn, .stack > * + * { margin-top: 10px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; margin-top: 0 !important; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 14px 16px; margin: 10px 0; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px; }
.stat b { display: block; font-size: 22px; margin-top: 2px; }
.stat span { font-size: 14px; color: var(--muted); }
.stat.owed { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; text-align: left; width: 100%; }
.stat.owed b { color: var(--red); }

.list { list-style: none; margin: 0; padding: 0; }
.item {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 12px 14px; margin: 8px 0; min-height: 60px;
}
.item .grow { flex: 1; min-width: 0; }
.item .grow b, .item .grow span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .grow span { font-size: 14px; color: var(--muted); }
.item .right { text-align: right; white-space: nowrap; }
.item .right span { display: block; font-size: 13px; }
.chip { display: inline-block; font-size: 13px; font-weight: 700; padding: 2px 9px; border-radius: 99px; }
.chip.paid { background: #e2f3e8; color: var(--ok); }
.chip.owes { background: #fbe6e1; color: var(--red); }
.chip.void { background: #eee; color: #666; }

label { display: block; font-weight: 600; margin: 14px 0 6px; }
input, select {
  width: 100%; min-height: 50px; padding: 10px 12px;
  border: 1.5px solid #cfc7b6; border-radius: 12px; background: #fff;
}
input:focus, select:focus, button:focus-visible { outline: 3px solid rgba(201,138,27,.55); outline-offset: 1px; }

.seg { display: flex; border: 1.5px solid var(--green); border-radius: 12px; overflow: hidden; margin: 10px 0; }
.seg button { flex: 1; min-height: 48px; border: 0; background: #fff; color: var(--green); font-weight: 600; padding: 6px 4px; font-size: 15px; }
.seg button + button { border-left: 1.5px solid var(--green); }
.seg button.on { background: var(--green); color: #fff; }

.prods { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.prod { background: #fff; border: 1.5px solid var(--line); border-radius: 14px; padding: 10px 12px; text-align: left; min-height: 72px; }
.prod:active { background: #eef6f0; border-color: var(--green); }
.prod b { display: block; line-height: 1.25; }
.prod span { font-size: 14px; color: var(--muted); }
.prod em { display: block; font-style: normal; color: var(--green); font-weight: 700; margin-top: 2px; }

.line { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; margin: 8px 0; }
.line-top { display: flex; justify-content: space-between; gap: 8px; align-items: start; }
.line-top button { border: 0; background: none; color: var(--red); font-size: 15px; padding: 4px 0 4px 8px; min-height: 32px; }
.line-bot { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.step { width: 44px; height: 44px; border-radius: 12px; border: 1.5px solid var(--green); background: #fff; color: var(--green); font-size: 22px; font-weight: 700; flex: none; }
.line-bot input { min-height: 44px; text-align: center; padding: 6px; }
.line-bot .qty { width: 58px; flex: none; }
.line-bot .x { color: var(--muted); }
.line-bot .amt { margin-left: auto; font-weight: 700; white-space: nowrap; }

.totals { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin: 12px 0; }
.totals div { display: flex; justify-content: space-between; padding: 3px 0; }
.totals .grand { font-size: 22px; font-weight: 800; border-top: 1px solid var(--line); margin-top: 6px; padding-top: 8px; }
.totals .bal { color: var(--red); font-weight: 700; }

.doc-img { width: 100%; border: 1px solid var(--line); border-radius: 12px; display: block; background: #fff; }

.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.nudge { background: #fff6df; border: 1px solid #ecd9a8; border-radius: 14px; padding: 12px 14px; margin: 10px 0; display: flex; gap: 10px; align-items: center; justify-content: space-between; width: 100%; text-align: left; }
.nudge b { color: var(--gold); white-space: nowrap; }

dialog { border: 0; border-radius: 18px; padding: 18px; width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 40px); overflow: auto; }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin-top: 0; }
dialog .actions { margin-top: 18px; }

#toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #1d2420; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 15px;
  max-width: calc(100vw - 32px); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 20; text-align: center;
}
#toast.show { opacity: 1; }
