:root {
  --bg: #f6f5f1;
  --card: #ffffff;
  --ink: #1d2421;
  --muted: #6b7570;
  --line: #e3e1d9;
  --brand: #1f6f5c;
  --brand-ink: #ffffff;
  --brand-soft: #e3f1ed;
  --accent: #d98e04;
  --danger: #c0392b;
  --danger-soft: #fbe9e6;
  --ok: #2e7d32;
  --ok-soft: #e6f3e7;
  --warn-soft: #fdf1d6;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 14px rgba(0, 0, 0, 0.05);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121714;
    --card: #1b221e;
    --ink: #eef2ef;
    --muted: #9aa6a0;
    --line: #2a332e;
    --brand: #3fb596;
    --brand-ink: #08130f;
    --brand-soft: #1c3029;
    --accent: #f0b429;
    --danger: #ef6f60;
    --danger-soft: #3a1f1c;
    --ok: #6fcf74;
    --ok-soft: #1d3320;
    --warn-soft: #3a2f12;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { line-height: 1.2; }
h3 { font-size: 15px; margin: 22px 0 8px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
small, .muted { color: var(--muted); }
a { color: var(--brand); }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Estrutura ---------- */
.shell { min-height: 100dvh; display: flex; flex-direction: column; }
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav .brand { display: none; }
.nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 7px; color: var(--muted); text-decoration: none; font-size: 12px;
}
.nav a.on { color: var(--brand); font-weight: 600; }
.nav .ic { font-size: 21px; line-height: 1; }
.main { flex: 1; min-width: 0; padding-bottom: calc(90px + env(safe-area-inset-bottom)); }
.top {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; background: var(--bg);
}
.top { max-width: 760px; margin: 0 auto; width: 100%; }
.top h1 { margin: 0; font-size: 21px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { text-decoration: none; font-size: 24px; line-height: 1; padding: 4px 8px 4px 0; }
#view { padding: 0 16px 16px; max-width: 760px; margin: 0 auto; }

.sync {
  display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--line);
  background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: 13px; cursor: pointer; color: var(--muted);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); flex: none; }
.dot.busy { background: var(--accent); animation: pulse 1s infinite; }
.dot.off { background: var(--muted); }
.dot.err { background: var(--danger); }
@keyframes pulse { 50% { opacity: 0.35; } }

@media (min-width: 900px) {
  .shell { flex-direction: row; }
  .nav {
    position: sticky; top: 0; align-self: flex-start; height: 100dvh; width: 224px; flex: none;
    flex-direction: column; border-top: 0; border-right: 1px solid var(--line); padding: 18px 12px; gap: 4px;
  }
  .nav .brand { display: block; font-weight: 700; font-size: 18px; padding: 6px 12px 18px; }
  .nav a { flex: none; flex-direction: row; gap: 12px; padding: 11px 12px; font-size: 15px; border-radius: 10px; }
  .nav a.on { background: var(--brand-soft); }
  .main { padding-bottom: 32px; }
  .top { max-width: 860px; }
  #view { max-width: 860px; }
}

/* ---------- Componentes ---------- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; margin-bottom: 10px; width: 100%; text-align: left;
}
button.card { cursor: pointer; }
button.card:active { transform: scale(0.99); }
.row { display: flex; align-items: center; gap: 12px; }
.grow { flex: 1; min-width: 0; }
.grow b, .grow small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.right { text-align: right; flex: none; }
.right small { display: block; }

.badge {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-weight: 600; font-size: 14px;
  background: var(--ok-soft); color: var(--ok); white-space: nowrap;
}
.badge.low { background: var(--warn-soft); color: #9a6700; }
.badge.zero { background: var(--danger-soft); color: var(--danger); }
@media (prefers-color-scheme: dark) { .badge.low { color: var(--accent); } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px;
  padding: 0 16px; border-radius: 12px; border: 1px solid var(--line); background: var(--card);
  cursor: pointer; font-weight: 600; text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btns { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 4px; }
.btns .btn { flex: 1; }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar input[type="search"], .toolbar input[type="text"] { flex: 1; min-width: 160px; }
.chip {
  border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 0 14px; min-height: 44px;
  cursor: pointer; font-weight: 600; color: var(--muted);
}
.chip.on { background: var(--brand-soft); color: var(--brand); border-color: var(--brand); }

input, select, textarea {
  font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 0 14px; min-height: 44px; width: 100%;
}
textarea { padding: 10px 14px; min-height: 80px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
input[type="checkbox"] { width: 24px; min-height: 24px; height: 24px; accent-color: var(--brand); flex: none; }
.field { display: block; margin: 12px 0; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.err { background: var(--danger-soft); color: var(--danger); border-radius: 10px; padding: 9px 12px; margin: 10px 0 0; font-size: 14px; }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 14px; }
@media (min-width: 560px) { .stats.four { grid-template-columns: repeat(4, 1fr); } }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.stat small { display: block; font-size: 12.5px; }
.stat b { display: block; font-size: 21px; margin-top: 2px; }
.stat.warn b { color: var(--accent); }
.stat.bad b { color: var(--danger); }
a.stat { text-decoration: none; color: inherit; }

.empty { text-align: center; color: var(--muted); padding: 34px 12px; }
.empty .big { font-size: 38px; display: block; margin-bottom: 6px; }

.bal { font-weight: 700; font-size: 16px; }
.bal.owe { color: var(--danger); }
.bal.credit { color: var(--ok); }
.bal.zero { color: var(--muted); }
.hero { text-align: center; padding: 18px 16px; }
.hero .bal { font-size: 34px; display: block; margin: 4px 0; }

.entry { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--line); }
.entry:last-child { border-bottom: 0; }
.entry .ico { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; flex: none; background: var(--danger-soft); }
.entry.pay .ico { background: var(--ok-soft); }
.entry .amt { font-weight: 700; white-space: nowrap; }
.entry.pay .amt { color: var(--ok); }
.entry.charge .amt { color: var(--danger); }
.entry .x2 { background: none; border: 0; cursor: pointer; color: var(--muted); padding: 2px 6px; font-size: 15px; }

.check { display: flex; align-items: center; gap: 12px; }
.check.done .grow b { text-decoration: line-through; color: var(--muted); }

.line { display: grid; grid-template-columns: 1fr 84px 40px; gap: 8px; margin-bottom: 8px; }
.line.ing { grid-template-columns: 1fr 68px 62px 34px; }
.line .x2 { background: none; border: 0; cursor: pointer; font-size: 18px; color: var(--muted); }

.table-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Diálogo ---------- */
dialog {
  border: 0; padding: 0; background: var(--card); color: var(--ink); border-radius: 20px;
  width: min(520px, calc(100vw - 20px)); max-height: calc(100dvh - 24px); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
dialog::backdrop { background: rgba(10, 15, 12, 0.55); }
.sheet { padding: 18px 18px 16px; }
.sheet-h { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.sheet-h h2 { margin: 0; font-size: 19px; flex: 1; min-width: 0; }
.x { border: 0; background: var(--bg); width: 36px; height: 36px; border-radius: 50%; cursor: pointer; font-size: 16px; flex: none; }
.hist { font-size: 14px; padding: 7px 0; border-bottom: 1px solid var(--line); display: flex; gap: 10px; justify-content: space-between; }
.hist:last-child { border-bottom: 0; }
@media (max-width: 560px) {
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 20px 20px 0 0; max-height: 92dvh; }
}

/* ---------- Login ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.auth .box { width: min(420px, 100%); }
.auth .logo { width: 72px; height: 72px; border-radius: 18px; display: block; margin: 0 auto 10px; }
.auth h1 { text-align: center; margin: 0 0 4px; }
.auth p.sub { text-align: center; margin: 0 0 18px; color: var(--muted); }

/* ---------- Aviso (toast) ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: #1d2421; color: #fff; padding: 11px 18px; border-radius: 12px; opacity: 0; pointer-events: none;
  transition: 0.2s; z-index: 100; max-width: calc(100vw - 32px); font-size: 15px; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 900px) { #toast { bottom: 24px; } }

/* ---------- Ajustes para celular (precisam vir por último) ---------- */
@media (max-width: 560px) {
  .toolbar input[type="search"] { flex-basis: 100%; }
  .toolbar .btn.primary, .toolbar .chip { flex: 1; }
}
