:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #f0f3ef;
  --text: #17231d;
  --muted: #5d6b63;
  --border: #dde3dc;
  --brand: #1f5f45;
  --brand-ink: #ffffff;
  --brand-soft: #e3efe8;
  --good: #1d7a4a;
  --good-soft: #e1f3e8;
  --warn: #9a5b00;
  --warn-soft: #fdf0d9;
  --bad: #b3261e;
  --bad-soft: #fbe5e3;
  --shadow: 0 1px 2px rgba(20, 40, 30, .06), 0 4px 16px rgba(20, 40, 30, .05);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101512;
    --surface: #181f1b;
    --surface-2: #1f2823;
    --text: #e6ece8;
    --muted: #9aa8a0;
    --border: #2c3731;
    --brand: #4fae84;
    --brand-ink: #0b1510;
    --brand-soft: #1d3329;
    --good: #5cc98f;
    --good-soft: #17301f;
    --warn: #f0b45a;
    --warn-soft: #33270f;
    --bad: #f28b82;
    --bad-soft: #3a1c1a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; }
a { color: var(--brand); }
.loading { padding: 40px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }

/* ---------- Header & nav ---------- */
.topbar {
  background: var(--brand);
  color: var(--brand-ink);
  padding: 12px 16px;
  display: flex; align-items: center; gap: 12px;
}
.topbar .logo { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.1rem; }
.topbar .logo svg { width: 28px; height: 28px; }
.topbar .who { margin-left: auto; text-align: right; font-size: .85rem; line-height: 1.25; }
.topbar .who b { display: block; font-size: .95rem; }
.topbar .who .role { opacity: .8; text-transform: capitalize; }
.topbar .menu-btn {
  background: transparent; border: 1px solid currentColor; color: inherit; border-radius: 8px;
  padding: 6px 10px; cursor: pointer; font: inherit; font-size: .85rem; opacity: .9;
}
nav.tabs {
  background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; gap: 4px; padding: 0 12px; overflow-x: auto; scrollbar-width: none;
}
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs button {
  background: none; border: 0; border-bottom: 3px solid transparent; color: var(--muted);
  font: inherit; font-weight: 600; padding: 12px 10px; cursor: pointer; white-space: nowrap;
}
nav.tabs button.active { color: var(--brand); border-bottom-color: var(--brand); }
main { max-width: 1120px; margin: 0 auto; padding: 20px 16px 60px; }

/* ---------- Building blocks ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-bottom: 16px; }
.page-head h2 { margin-right: auto; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px;
}
.card > h3 { margin-bottom: 12px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.stat .value { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat .sub { color: var(--muted); font-size: .85rem; }
.stat.good .value { color: var(--good); }
.stat.bad .value { color: var(--bad); }
.stat.warn .value { color: var(--warn); }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 8px; }
.bar > span { display: block; height: 100%; background: var(--good); border-radius: 99px; }

.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; font-weight: 600; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: 0; }
tr.voided td { color: var(--muted); text-decoration: line-through; }
tr.voided td.keep { text-decoration: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }

.pill { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: .75rem; font-weight: 600; white-space: nowrap; text-decoration: none; }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.plain { background: var(--surface-2); color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 9px;
  padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; min-height: 38px;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: .85rem; }
.btn:disabled { opacity: .55; cursor: default; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: .85rem; font-weight: 600; margin-bottom: 4px; }
label.field > small { display: block; color: var(--muted); font-size: .8rem; margin-top: 3px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px; padding: 9px 11px; min-height: 40px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 14px; }
.filters label.field { margin: 0; min-width: 160px; flex: 1 1 160px; max-width: 260px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.check input { width: auto; min-height: 0; }
.error { background: var(--bad-soft); color: var(--bad); border-radius: 9px; padding: 9px 12px; margin-bottom: 12px; font-size: .9rem; }
.error:empty { display: none; }
.empty { text-align: center; color: var(--muted); padding: 28px 10px; }
.note { background: var(--brand-soft); border-radius: 10px; padding: 10px 14px; font-size: .9rem; margin-bottom: 16px; }

/* ---------- Block cards ---------- */
.block-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.block-card { margin: 0; }
.block-card header { display: flex; align-items: start; gap: 8px; margin-bottom: 8px; }
.block-card header div { margin-right: auto; }
.unit-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid var(--border); }
.unit-row .u-main { flex: 1; min-width: 0; }
.unit-row .u-main b { display: block; }
.unit-row .u-main span { color: var(--muted); font-size: .85rem; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Auth screens ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px 16px; }
.auth .card { width: 100%; max-width: 400px; padding: 28px 24px; }
.auth .brand { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 8px; margin-bottom: 20px; }
.auth .brand svg { width: 56px; height: 56px; }
.auth .brand h1 { font-size: 1.5rem; }
.auth .btn { width: 100%; margin-top: 6px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10, 20, 15, .5); display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto; z-index: 50;
}
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 480px; padding: 20px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal.wide { max-width: 760px; }
.modal-head { display: flex; align-items: center; margin-bottom: 14px; }
.modal-head h3 { margin-right: auto; font-size: 1.1rem; }
.modal-head .x { background: none; border: 0; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); padding: 0 4px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

/* ---------- Receipt ---------- */
.receipt { border: 2px solid var(--text); border-radius: 10px; padding: 18px; position: relative; overflow: hidden; background: var(--surface); }
.receipt .r-head { display: flex; justify-content: space-between; gap: 12px; border-bottom: 1px dashed var(--muted); padding-bottom: 12px; margin-bottom: 12px; }
.receipt .r-head h2 { font-size: 1.3rem; }
.receipt .r-no { text-align: right; }
.receipt .r-no b { font-size: 1.15rem; display: block; }
.receipt dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; }
.receipt dt { color: var(--muted); }
.receipt dd { margin: 0; font-weight: 600; }
.receipt .r-amount { margin-top: 14px; padding: 12px; background: var(--surface-2); border-radius: 8px; display: flex; justify-content: space-between; align-items: center; font-size: 1.05rem; }
.receipt .r-amount b { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.receipt .r-foot { margin-top: 12px; font-size: .8rem; color: var(--muted); }
.receipt .stamp {
  position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%) rotate(-18deg);
  font-size: 3rem; font-weight: 800; color: var(--bad); border: 5px solid var(--bad); border-radius: 10px;
  padding: 0 16px; opacity: .3; pointer-events: none; letter-spacing: .08em;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(120px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-weight: 600;
  transition: transform .2s; z-index: 80; max-width: calc(100% - 32px);
}
#toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 600px) {
  .grid2 { grid-template-columns: 1fr; }
  .topbar .who { display: none; }
  .topbar .menu-btn { margin-left: auto; }
  .stat .value { font-size: 1.35rem; }
  .block-grid { grid-template-columns: 1fr; }
  .modal-backdrop { padding: 16px 10px; }
  .hide-sm { display: none; }
}

/* ---------- Print: only the receipt ---------- */
@media print {
  body.printing > *:not(#modal-root) { display: none !important; }
  body.printing .modal-backdrop { position: static; background: none; padding: 0; }
  body.printing .modal { box-shadow: none; padding: 0; max-width: none; }
  body.printing .modal-head, body.printing .modal-foot { display: none; }
  .receipt { --surface: #fff; --surface-2: #f2f2f2; --text: #000; --muted: #555; color: #000; }
}
