:root {
  --bg: #f4f6f5; --card: #fff; --ink: #1c2422; --muted: #66716e; --line: #dfe5e3;
  --brand: #0f766e; --brand-ink: #fff; --sale: #0f766e; --purchase: #2563eb; --expense: #c2410c;
  --danger: #b91c1c; --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 15px; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.spacer { flex: 1; }

.topbar { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; background: var(--brand); color: var(--brand-ink); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; gap: 10px; align-items: center; }
.logo { background: #fff; color: var(--brand); font-weight: 800; border-radius: 8px; padding: 6px 7px; }
.brand-name { font-weight: 700; }
.brand-sub { font-size: 12px; opacity: .85; }
.sync-pill { display: flex; align-items: center; gap: 6px; border: 1px solid rgba(255,255,255,.5); background: transparent; color: #fff; border-radius: 999px; padding: 5px 11px; font-size: 13px; cursor: pointer; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #fbbf24; }
.dot.ok { background: #4ade80; } .dot.off { background: #9ca3af; } .dot.busy { background: #60a5fa; }

.tabs { display: flex; gap: 4px; overflow-x: auto; background: #fff; border-bottom: 1px solid var(--line); padding: 6px 8px; position: sticky; top: 52px; z-index: 4; }
.tabs button { border: 0; background: transparent; padding: 8px 12px; border-radius: 8px; font-size: 14px; white-space: nowrap; cursor: pointer; color: var(--ink); }
.tabs button.active { background: var(--brand); color: #fff; }

main { max-width: 1100px; margin: 0 auto; padding: 12px; }
.view { display: none; } .view.active { display: block; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 12px; }
.grid2 .card { margin: 0; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .grow { flex: 1; min-width: 180px; }
input, select, textarea { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); width: 100%; }
.toolbar input, .toolbar select { width: auto; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); flex: 1; min-width: 140px; }
label.check { flex-direction: row; align-items: center; min-width: 0; flex: 0; white-space: nowrap; }
label.check input { width: auto; }
.form { display: flex; flex-direction: column; gap: 12px; }
.form h2 { margin: 0; font-size: 18px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; margin: 0; }
legend { font-size: 13px; font-weight: 600; padding: 0 4px; }

.btn { border: 1px solid var(--brand); background: #fff; color: var(--brand); padding: 8px 14px; border-radius: 8px; cursor: pointer; font: inherit; font-size: 14px; display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; min-width: 0; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.ghost { border-color: var(--line); color: var(--ink); }
.btn.danger { border-color: var(--danger); color: var(--danger); }
.btn.small { padding: 4px 8px; font-size: 12px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; align-self: flex-start; }
.seg button { border: 0; background: #fff; padding: 8px 16px; cursor: pointer; font: inherit; }
.seg button.on[data-type=sale] { background: var(--sale); color: #fff; }
.seg button.on[data-type=purchase] { background: var(--purchase); color: #fff; }
.seg button.on[data-type=expense] { background: var(--expense); color: #fff; }

.scanrow { display: flex; gap: 8px; margin-bottom: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 6px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.items input { padding: 6px; }
table.items td:nth-child(1) { width: 18%; } table.items td:nth-child(3) { width: 12%; } table.items td:nth-child(4) { width: 16%; }

.balance { font-size: 14px; }
.balance.due { color: var(--danger); font-weight: 600; }
.attach-actions { display: flex; gap: 8px; }
.thumbs { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.thumb { position: relative; width: 88px; height: 88px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; display: flex; align-items: center; justify-content: center; font-size: 11px; text-align: center; background: #fafafa; cursor: pointer; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .x { position: absolute; top: 2px; right: 2px; background: rgba(0,0,0,.6); color: #fff; border: 0; border-radius: 50%; width: 22px; height: 22px; cursor: pointer; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 12px; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; border-top: 4px solid var(--line); }
.kpi .label { font-size: 12px; color: var(--muted); }
.kpi .value { font-size: 20px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.sale { border-top-color: var(--sale); } .kpi.purchase { border-top-color: var(--purchase); } .kpi.expense { border-top-color: var(--expense); }
.neg { color: var(--danger); }

.bars { display: flex; align-items: flex-end; gap: 3px; height: 120px; margin-bottom: 8px; border-bottom: 1px solid var(--line); }
.bars .b { flex: 1; display: flex; gap: 1px; align-items: flex-end; height: 100%; }
.bars .b span { flex: 1; min-height: 1px; border-radius: 2px 2px 0 0; }
.legend { display: flex; gap: 12px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
.c-sale { background: var(--sale); } .c-purchase { background: var(--purchase); } .c-expense { background: var(--expense); }

.rec { display: flex; gap: 10px; align-items: center; padding: 10px 4px; border-bottom: 1px solid var(--line); cursor: pointer; }
.rec:hover { background: #f8faf9; }
.tag { font-size: 11px; padding: 2px 8px; border-radius: 999px; color: #fff; text-transform: capitalize; }
.tag.sale { background: var(--sale); } .tag.purchase { background: var(--purchase); } .tag.expense { background: var(--expense); }
.rec .main { flex: 1; min-width: 0; }
.rec .t1 { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rec .t2 { font-size: 12px; color: var(--muted); }
.rec .amt { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.rec .due { font-size: 12px; color: var(--danger); }
.empty { padding: 20px; text-align: center; color: var(--muted); }

dialog { border: 0; border-radius: 12px; padding: 14px; width: min(480px, 94vw); }
#scannerBox { width: 100%; min-height: 240px; }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(80px); background: #1c2422; color: #fff; padding: 10px 16px; border-radius: 8px; transition: transform .2s; z-index: 20; font-size: 14px; max-width: 90vw; }
.toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 600px) {
  table.items th:nth-child(1), table.items td:nth-child(1) { display: none; }
  .kpi .value { font-size: 17px; }
}
@media print {
  .topbar, .tabs, .toolbar, .toast { display: none !important; }
  .view { display: none !important; } #view-home { display: block !important; }
  .card, .kpi { break-inside: avoid; }
  body { background: #fff; }
}
