:root {
  --bg: #f4f6f3;
  --card: #ffffff;
  --text: #1c2420;
  --muted: #6b766f;
  --line: #e2e7e2;
  --brand: #2f8a3a;
  --brand-dark: #236b2c;
  --brand-soft: #e6f3e7;
  --sell: #1f6fd1;
  --sell-soft: #e5effb;
  --buy: #c77712;
  --buy-soft: #fbf0e0;
  --danger: #c2392b;
  --danger-soft: #fbe9e7;
  --warn-soft: #fff6db;
  --expense: #9b5bc9;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 25, .06), 0 2px 8px rgba(20, 30, 25, .04);
  --side: 236px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --card: #1a201d;
    --text: #e6ece8;
    --muted: #93a098;
    --line: #2a332e;
    --brand: #4cb35a;
    --brand-dark: #6cc778;
    --brand-soft: #1f3324;
    --sell: #5c9df0;
    --sell-soft: #1a2a3f;
    --buy: #e3a04a;
    --buy-soft: #3a2c17;
    --danger: #ef6b5d;
    --danger-soft: #3b1f1c;
    --warn-soft: #3a3217;
    --expense: #b98ae0;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--brand-dark); }
h1 { font-size: 1.5rem; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
code, .code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pre { white-space: pre-line; }
.nowrap { white-space: nowrap; }
.pos { color: var(--brand); }
.neg { color: var(--danger); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.inline { display: inline; }
.print-only { display: none; }

/* ---------- Layout ---------- */
.topbar { display: none; }
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--side);
  background: var(--card); border-right: 1px solid var(--line);
  padding: 16px 12px; overflow-y: auto; z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--text); text-decoration: none; }
.brand img { width: 36px; height: 36px; object-fit: contain; border-radius: 6px; }
.side-brand { padding: 4px 8px 16px; }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-title { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 10px 6px; }
.nav-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none; font-size: .93rem;
}
.nav-link svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.nav-link:hover { background: var(--bg); }
.nav-link.active { background: var(--brand-soft); color: var(--brand-dark); font-weight: 600; }
.linklike { width: 100%; border: 0; background: none; font: inherit; cursor: pointer; text-align: left; }
.main { margin-left: var(--side); padding: 24px 28px 48px; max-width: 1240px; }
.nav-backdrop { display: none; }

@media (max-width: 860px) {
  .topbar {
    display: flex; align-items: center; gap: 8px; position: sticky; top: 0; z-index: 20;
    background: var(--card); border-bottom: 1px solid var(--line); padding: 8px 12px;
  }
  .menu-btn { border: 0; background: none; padding: 6px; cursor: pointer; color: var(--text); }
  .menu-btn svg { width: 24px; height: 24px; stroke: currentColor; stroke-width: 2; fill: none; stroke-linecap: round; }
  .sidebar { transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 40px rgba(0,0,0,.2); }
  .side-brand { display: none; }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .nav-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 25; }
  .main { margin-left: 0; padding: 16px 16px 40px; }
}

/* ---------- Page elements ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; min-width: 0; }
.card.narrow { max-width: 680px; }
.cols { display: grid; grid-template-columns: 1.6fr 1fr; gap: 16px; align-items: start; }
.cols-list { grid-template-columns: 1.7fr 1fr; }
.cols > .card { margin-bottom: 0; }
.cols + .card, .cols + section { margin-top: 16px; }
@media (max-width: 960px) { .cols, .cols-list { grid-template-columns: 1fr; } }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi span { display: block; color: var(--muted); font-size: .85rem; }
.kpi strong { display: block; font-size: 1.35rem; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi em { font-style: normal; font-size: .78rem; color: var(--muted); }
@media (max-width: 760px) { .kpis { grid-template-columns: 1fr 1fr; } .kpi strong { font-size: 1.1rem; } }

.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; border: 1px solid transparent; }
.alert-success { background: var(--brand-soft); color: var(--brand-dark); border-color: color-mix(in srgb, var(--brand) 25%, transparent); }
.alert-error { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 25%, transparent); }
.alert-warning { background: var(--warn-soft); }

.empty { color: var(--muted); padding: 12px 0; margin: 0; }
.summary-line { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 8px; }
.summary-line h2 { margin: 0; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font: inherit; font-weight: 500;
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn-buy { background: var(--buy); border-color: var(--buy); color: #fff; }
.btn-sell { background: var(--sell); border-color: var(--sell); color: #fff; }
.btn-buy:hover, .btn-sell:hover { filter: brightness(.93); border-color: transparent; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-danger-ghost { background: transparent; border-color: transparent; color: var(--danger); }
.btn-danger-ghost:hover { background: var(--danger-soft); border-color: transparent; }
.btn-small { padding: 4px 10px; font-size: .85rem; }
.btn-block { width: 100%; }
.icon-btn { border: 0; background: none; font-size: 1.4rem; line-height: 1; color: var(--muted); cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--danger-soft); color: var(--danger); }

/* ---------- Forms ---------- */
.form label { display: flex; flex-direction: column; gap: 4px; font-size: .88rem; font-weight: 500; margin-bottom: 12px; }
.form label .muted { font-weight: 400; }
input, select, textarea {
  font: inherit; font-size: 16px; color: var(--text); background: var(--card);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 40%, transparent); outline-offset: 0; border-color: var(--brand); }
input:disabled { opacity: .6; }
textarea.code { width: 100%; }
.form h2 { margin-top: 8px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 14px; }
.span2 { grid-column: span 2; }
@media (max-width: 760px) { .grid2, .grid3 { grid-template-columns: 1fr; } .span2 { grid-column: auto; } }
.check { flex-direction: row !important; align-items: center; gap: 8px !important; font-weight: 400 !important; }
.check input { width: auto; }
.check.inline { display: inline-flex; margin: 0 0 0 10px; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

.filters { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.filters label { display: flex; flex-direction: column; font-size: .8rem; color: var(--muted); gap: 2px; }
.filters input, .filters select { width: auto; padding: 7px 10px; }
.filters.secondary { margin-top: -4px; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 6px 11px; border-radius: 999px; border: 1px solid var(--line); background: var(--card); color: var(--text); text-decoration: none; font-size: .85rem; }
.chip.on { background: var(--brand-soft); border-color: var(--brand); color: var(--brand-dark); }

/* Line items */
.lines { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.line { display: grid; grid-template-columns: 2fr 1.1fr 1.1fr 1fr 40px; gap: 8px; align-items: center; }
.line-head { font-size: .8rem; color: var(--muted); font-weight: 500; }
.with-unit { position: relative; }
.with-unit .unit { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: .85rem; pointer-events: none; }
.with-unit input { padding-right: 48px; }
.l-amount { font-weight: 600; padding-right: 4px; }
@media (max-width: 760px) {
  .line-head { display: none; }
  .line { grid-template-columns: 1fr 1fr 40px; grid-template-areas: "m m x" "q p p" "a a a"; padding: 10px; border: 1px solid var(--line); border-radius: 8px; }
  .line .l-material { grid-area: m; }
  .line .with-unit { grid-area: q; }
  .line .l-price { grid-area: p; }
  .line .l-amount { grid-area: a; }
  .line .l-remove { grid-area: x; }
}
.totals { margin: 16px 0 12px auto; max-width: 320px; display: flex; flex-direction: column; gap: 6px; }
.totals > div { display: flex; justify-content: space-between; align-items: center; gap: 12px; font-variant-numeric: tabular-nums; }
.totals .check { margin: 0; }
.totals .grand { border-top: 2px solid var(--text); padding-top: 8px; font-size: 1.2rem; }

.type-picker { border: 0; padding: 0; margin: 0 0 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.type-picker legend { font-weight: 600; font-size: .88rem; margin-bottom: 6px; padding: 0; }
.radio-card { flex-direction: row !important; align-items: center; gap: 10px !important; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin: 0 !important; cursor: pointer; font-weight: 400 !important; }
.radio-card input { width: auto; }
.radio-card span { flex: 1; }
.radio-card em { font-style: normal; font-size: .78rem; }
.radio-card:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }
@media (max-width: 760px) { .type-picker { grid-template-columns: 1fr; } }

.pay-modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
@media (max-width: 760px) { .pay-modes { grid-template-columns: 1fr; } }
.tag-paid { background: var(--brand-soft); color: var(--brand-dark); border-color: transparent; }
.tag-part { background: var(--warn-soft); color: var(--buy); border-color: transparent; }
.tag-unpaid { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.overdue { color: var(--danger); font-weight: 600; }
.recon-summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; align-items: end; }
@media (max-width: 760px) { .recon-summary { grid-template-columns: 1fr 1fr; } }
.recon-summary .kpi { box-shadow: none; }
tr.matched td { color: var(--muted); }
.match-box { width: 20px; height: 20px; cursor: pointer; accent-color: var(--brand); }

.logo-preview { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.logo-preview img { width: 64px; height: 64px; object-fit: contain; border: 1px solid var(--line); border-radius: 8px; background: #fff; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line); }
.table td { padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th.num { text-align: right; }
.table.compact td { padding: 7px 4px; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tr.total td { font-weight: 700; border-top: 2px solid var(--text); border-bottom: 0; }
.table tfoot td { border-bottom: 0; }
.table tr.subtotal td { font-weight: 600; background: var(--bg); }
.table tr.section td { font-weight: 600; padding-top: 16px; border-bottom: 0; }
.table tr.indent td:first-child { padding-left: 24px; }
.table tr.inactive td { opacity: .55; }
.clickable { cursor: pointer; }
.clickable:hover td { background: var(--bg); }
.report .table { max-width: 640px; }
.report .table-wrap .table { max-width: none; }

.tag { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .75rem; background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.tag-sale { background: var(--sell-soft); color: var(--sell); border-color: transparent; }
.tag-purchase { background: var(--buy-soft); color: var(--buy); border-color: transparent; }
.tag-expense { background: color-mix(in srgb, var(--expense) 15%, transparent); color: var(--expense); border-color: transparent; }
.tag-capital { background: var(--brand-soft); color: var(--brand-dark); border-color: transparent; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs a { padding: 8px 14px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a.on { color: var(--brand-dark); border-bottom-color: var(--brand); font-weight: 600; }

/* ---------- Charts ---------- */
.chart { display: flex; align-items: flex-end; gap: 10px; height: 200px; padding-top: 8px; }
.bar-group { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; min-width: 0; }
.bars { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; gap: 4px; }
.bar { width: 42%; max-width: 28px; border-radius: 4px 4px 0 0; min-height: 1px; }
.bar.stack { display: flex; flex-direction: column; justify-content: flex-end; height: 100%; background: none; }
.bar.stack .bar { width: 100%; max-width: none; border-radius: 0; }
.bar.stack .bar:first-child { border-radius: 4px 4px 0 0; }
.bar.sale, .sw.sale { background: var(--sell); }
.bar.purchase, .sw.purchase { background: var(--buy); }
.bar.expense, .sw.expense { background: var(--expense); }
.bar.stackbar { width: 60%; max-width: 44px; display: flex; flex-direction: column; overflow: hidden; }
.bar.stackbar .seg { width: 100%; }
.c0 { background: #2f8a3a; } .c1 { background: #1f6fd1; } .c2 { background: #c77712; } .c3 { background: #9b5bc9; }
.c4 { background: #d1495b; } .c5 { background: #16a3a3; } .c6 { background: #8a8f98; }
.trend-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }
.trend-head h2 { margin: 0; }
.bar-label { font-size: .78rem; color: var(--muted); margin-top: 6px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: .82rem; color: var(--muted); margin-top: 10px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.hbars { display: flex; flex-direction: column; gap: 8px; }
.hbar { display: grid; grid-template-columns: 170px 1fr auto; gap: 10px; align-items: center; font-size: .9rem; }
.hbar-track { height: 10px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.hbar-fill { display: block; height: 100%; background: var(--expense); border-radius: 999px; }
@media (max-width: 600px) { .hbar { grid-template-columns: 1fr auto; } .hbar-track { grid-column: span 2; order: 3; } }

/* ---------- Printed receipt ---------- */
.doc { max-width: 820px; }
.doc-head { display: flex; justify-content: space-between; gap: 20px; margin-bottom: 18px; flex-wrap: wrap; }
.doc-logo { width: 64px; height: 64px; object-fit: contain; display: block; margin-bottom: 6px; }
.doc-meta { text-align: right; }
.doc-meta h2 { letter-spacing: .08em; margin-bottom: 4px; }
.doc-party { margin-bottom: 14px; padding: 10px 12px; background: var(--bg); border-radius: 8px; }

/* ---------- Login / install ---------- */
body.auth { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.auth-card.wide { max-width: 620px; }
.auth-card h1 { margin-bottom: 4px; }
.auth-card h2 { margin-top: 18px; }
.auth-logo { width: 72px; height: 72px; object-fit: contain; display: block; margin-bottom: 10px; }

@media print {
  :root { --bg: #fff; --card: #fff; --text: #000; --line: #ccc; color-scheme: light; }
  .sidebar, .topbar, .page-head .actions, .no-print, .filters, .tabs, .alert { display: none !important; }
  .print-only { display: block; }
  .main { margin: 0; padding: 0; max-width: none; }
  .card { border: 0; box-shadow: none; padding: 0; }
  .clickable:hover td { background: none; }
}
.form-inline-label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; font-weight: 500; }
.grid-inline { display: grid; grid-template-columns: repeat(4, 1fr) auto; gap: 10px; align-items: end; margin-top: 12px; }
.grid-inline label { margin: 0; }
@media (max-width: 860px) { .grid-inline { grid-template-columns: 1fr 1fr; } }
.cols.even { grid-template-columns: 1fr 1fr; margin-top: 16px; }
@media (max-width: 960px) { .cols.even { grid-template-columns: 1fr; } }

.invite-box { border-color: var(--brand); background: var(--brand-soft); }
.link-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.link-row input { flex: 1 1 320px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; background: var(--card); }
.btn-wa { background: #25d366; border-color: #25d366; color: #fff; }
.btn-wa:hover { filter: brightness(.93); }
