:root {
  --bg: #f3f5f4;
  --card: #ffffff;
  --text: #17211b;
  --muted: #66736b;
  --line: #e1e6e3;
  --brand: #15803d;
  --brand-dark: #116432;
  --brand-soft: #e3f4e9;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --warn: #a15c00;
  --warn-soft: #fff3dc;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111613;
    --card: #1b221e;
    --text: #edf2ee;
    --muted: #9aa8a0;
    --line: #2b352f;
    --brand: #2fa35a;
    --brand-dark: #258449;
    --brand-soft: #1d3527;
    --danger: #ef7a6d;
    --danger-soft: #3a211e;
    --warn: #f0b354;
    --warn-soft: #3a2e17;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0 auto;
  max-width: 560px;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
h1, h2, h3, p { margin: 0; }

/* Header */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--brand); color: #fff;
}
.top h1 { font-size: 19px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { background: none; border: 0; color: inherit; padding: 8px; border-radius: 10px; display: grid; place-items: center; }
.icon-btn:active { background: rgba(255,255,255,.18); }

.top h1 { flex: 1; }
#sync { display: none; font-size: 12px; font-weight: 600; background: rgba(255,255,255,.2); padding: 4px 10px; border-radius: 99px; white-space: nowrap; margin: 0 6px; }
#sync.show { display: inline-block; }
body.auth .tabs, body.auth #gear { display: none; }
.auth-card { padding: 22px 16px; display: flex; flex-direction: column; gap: 12px; margin-top: 24px; }
.auth-card h2 { font-size: 21px; }
.btn:disabled { opacity: .6; }

main { padding: 14px 16px; }

/* Bottom tabs */
.tabs {
  position: fixed; bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 560px; z-index: 5;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  background: none; border: 0; padding: 9px 0 8px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: 12px; font-weight: 600;
}
.tabs button.on { color: var(--brand); }

/* Building blocks */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 4px 8px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { padding: 12px 10px; text-align: center; }
.stat b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat span { font-size: 12px; color: var(--muted); }
.stat.bad b { color: var(--danger); }

.progress { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; margin: 0 2px 14px; }
.progress i { display: block; height: 100%; background: var(--brand); border-radius: 99px; }

.list { overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-bottom: 1px solid var(--line);
  width: 100%; background: none; border-left: 0; border-right: 0; border-top: 0; text-align: left;
}
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .amt { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.row .amt small { display: block; font-weight: 500; font-size: 12px; }
button.row:active { background: var(--brand-soft); }

.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; flex: none; }

.pill { display: inline-block; padding: 3px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill.ok { background: var(--brand-soft); color: var(--brand); }
.pill.bad { background: var(--danger-soft); color: var(--danger); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.owes { color: var(--danger); }
.credit { color: var(--brand); }

.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 11px 16px; font-weight: 600; min-height: 46px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; color: var(--text);
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:active { background: var(--brand-dark); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: none; }
.btn.sm { padding: 7px 12px; min-height: 36px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }

.fab {
  position: fixed; right: max(16px, calc(50% - 264px)); bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 4;
  height: 54px; padding: 0 20px; border-radius: 27px; border: 0;
  background: var(--brand); color: #fff; font-weight: 700; font-size: 16px;
  box-shadow: 0 6px 18px rgba(0,0,0,.25);
}

/* Date / month navigator */
.datenav { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.datenav .mid { flex: 1; text-align: center; }
.datenav .mid b { display: block; font-size: 17px; }
.datenav .mid span { font-size: 13px; color: var(--muted); }
.datenav .nav { width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-size: 20px; }

/* Search and chips */
.search { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); margin-bottom: 10px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 99px; padding: 7px 14px; font-size: 14px; font-weight: 600; color: var(--muted); }
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--line); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.seg button { border: 0; background: none; padding: 9px 0; border-radius: 10px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--text); }

.empty { text-align: center; padding: 36px 20px; color: var(--muted); }
.empty h3 { color: var(--text); margin-bottom: 6px; }
.empty .btn { margin-top: 14px; }
.link { background: none; border: 0; color: var(--brand); font-weight: 600; text-decoration: underline; padding: 10px; }

/* Client detail */
.detail-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.detail-head h2 { font-size: 20px; overflow-wrap: anywhere; }
.back { width: 42px; height: 42px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-size: 20px; flex: none; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: right; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
th { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
th:first-child, td:first-child { text-align: left; }
tr:last-child td { border-bottom: 0; }

/* Bottom sheet */
dialog#sheet {
  margin: auto auto 0; width: 100%; max-width: 560px; max-height: 92vh;
  border: 0; padding: 0; border-radius: 18px 18px 0 0;
  background: var(--card); color: var(--text);
}
dialog#sheet::backdrop { background: rgba(0,0,0,.45); }
.sheet-body { padding: 20px 16px calc(18px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 12px; }
.sheet-body h2 { font-size: 19px; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--muted); }
label input, label select {
  padding: 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font-size: 16px; font-weight: 400; width: 100%;
}
label input:focus, label select:focus, .search:focus { outline: 2px solid var(--brand); outline-offset: 0; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions { display: flex; gap: 8px; margin-top: 6px; }
.actions .btn { flex: 1; }
.actions .btn.danger { flex: none; }

#toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #1d2a22; color: #fff; padding: 10px 18px; border-radius: 99px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 50; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
