/* ---------- tokens (light default, dark by OS or data-theme) ---------- */
:root {
  color-scheme: light;
  --bg: #f4f4f1; --surface: #fcfcfb; --surface-2: #f0efec; --line: #e2e1dc; --line-2: #d3d2cb;
  --text: #0b0b0b; --text-2: #52514e; --text-3: #7a7974;
  --accent: #2a78d6; --accent-ink: #fff; --accent-soft: #e6f0fb;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --good: #0b7a3e; --good-soft: #e3f4ea; --warn: #9a6200; --warn-soft: #fdf1d6; --bad: #c02b2b; --bad-soft: #fbe7e6;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.05);
  --r: 12px; --nav-h: 60px; --top-h: 56px; --side-w: 236px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface: #1a1a19; --surface-2: #242423; --line: #2e2e2c; --line-2: #3d3d3a;
    --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
    --accent: #3987e5; --accent-soft: #1b2a3d;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s7: #9085e9; --s8: #e66767;
    --good: #4cc784; --good-soft: #17301f; --warn: #e5b04a; --warn-soft: #33290f; --bad: #ee7676; --bad-soft: #3a1d1d;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #242423; --line: #2e2e2c; --line-2: #3d3d3a;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
  --accent: #3987e5; --accent-soft: #1b2a3d;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s7: #9085e9; --s8: #e66767;
  --good: #4cc784; --good-soft: #17301f; --warn: #e5b04a; --warn-soft: #33290f; --bad: #ee7676; --bad-soft: #3a1d1d;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 1.35rem; } h2 { font-size: 1.05rem; } h3 { font-size: .95rem; }
.muted { color: var(--text-3); } .sub { color: var(--text-2); font-size: .86rem; }
.num, .tbl td.r, .tbl th.r { font-variant-numeric: tabular-nums; }
.r { text-align: right; } .c { text-align: center; }
.good { color: var(--good); } .bad { color: var(--bad); } .warn { color: var(--warn); }
.hide { display: none !important; }
.boot { display: grid; place-items: center; height: 100vh; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line-2); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- app frame ---------- */
.shell { min-height: 100vh; }
.top { position: sticky; top: 0; z-index: 30; height: var(--top-h); display: flex; align-items: center; gap: 10px; padding: 0 14px; padding-top: env(safe-area-inset-top); background: var(--surface); border-bottom: 1px solid var(--line); }
.top h1 { flex: 1; font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .brand { display: none; }
.main { padding: 14px 14px calc(var(--nav-h) + 90px + env(safe-area-inset-bottom)); max-width: 1280px; margin: 0 auto; }
.side { display: none; }
.bottom-nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); display: grid; grid-template-columns: repeat(5, 1fr); background: var(--surface); border-top: 1px solid var(--line); }
.bottom-nav a, .bottom-nav button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: .68rem; color: var(--text-3); background: none; border: 0; font-family: inherit; cursor: pointer; }
.bottom-nav .on { color: var(--accent); font-weight: 600; }
.ico { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.fab { position: fixed; right: 16px; bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); z-index: 31; width: 54px; height: 54px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); border: 0; box-shadow: 0 6px 18px rgba(42,120,214,.4); display: grid; place-items: center; cursor: pointer; }
.fab .ico { width: 26px; height: 26px; stroke-width: 2.2; }

@media (min-width: 992px) {
  .side { display: flex; flex-direction: column; position: fixed; inset: 0 auto 0 0; width: var(--side-w); background: var(--surface); border-right: 1px solid var(--line); padding: 14px 10px; overflow-y: auto; z-index: 40; }
  .side .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.05rem; padding: 4px 10px 14px; }
  .side .brand i { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; color: #fff; font-style: normal; font-size: .9rem; }
  .side .grp { font-size: .68rem; text-transform: uppercase; letter-spacing: .08em; color: var(--text-3); padding: 14px 12px 4px; }
  .side a { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 9px; color: var(--text-2); font-weight: 500; }
  .side a:hover { background: var(--surface-2); }
  .side a.on { background: var(--accent-soft); color: var(--accent); }
  .shell { padding-left: var(--side-w); }
  .bottom-nav { display: none; }
  .fab { bottom: 24px; right: 28px; }
  .main { padding: 20px 28px 90px; }
  .top { padding: 0 28px; }
}

/* ---------- surfaces ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; box-shadow: var(--shadow); }
.card + .card, .stack > * + * { margin-top: 12px; }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.page-h { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.page-h .acts { display: flex; gap: 8px; flex-wrap: wrap; }
.grid { display: grid; gap: 12px; }
.g2, .g3, .g4 { grid-template-columns: 1fr; }
@media (min-width: 640px) { .g2, .g3, .g4 { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 992px) { .g3 { grid-template-columns: repeat(3, 1fr); } .g4 { grid-template-columns: repeat(4, 1fr); } }
.span2 { grid-column: 1 / -1; }
@media (min-width: 992px) { .span2 { grid-column: span 2; } }

/* ---------- buttons / inputs ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line-2); background: var(--surface); color: var(--text); font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { min-height: 32px; padding: 0 10px; font-size: .85rem; border-radius: 8px; }
.btn.icon { width: 40px; padding: 0; }
.btn[disabled] { opacity: .55; pointer-events: none; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label, .lbl { font-size: .78rem; font-weight: 600; color: var(--text-2); }
.inp, select.inp, textarea.inp { width: 100%; min-height: 42px; padding: 8px 11px; border-radius: 10px; border: 1px solid var(--line-2); background: var(--surface); color: var(--text); font: inherit; font-size: 16px; }
textarea.inp { min-height: 74px; resize: vertical; }
.inp:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.inp.err { border-color: var(--bad); }
.hint { font-size: .78rem; color: var(--text-3); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 12px; }
.toolbar .field { flex: 1 1 140px; }
.toolbar .field.grow { flex: 2 1 220px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: none; padding: 6px 12px; border-radius: 8px; font: inherit; font-size: .85rem; color: var(--text-2); cursor: pointer; font-weight: 550; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.chk { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.chk input { width: 18px; height: 18px; accent-color: var(--accent); }

/* ---------- kpi ---------- */
.kpis { display: grid; gap: 10px; grid-template-columns: repeat(2, 1fr); }
@media (min-width: 992px) { .kpis { grid-template-columns: repeat(4, 1fr); } }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 12px 14px; box-shadow: var(--shadow); min-width: 0; display: block; color: inherit; }
.kpi .k { font-size: .76rem; color: var(--text-2); font-weight: 600; }
.kpi .v { font-size: 1.35rem; font-weight: 700; letter-spacing: -.02em; margin: 2px 0; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kpi .d { font-size: .76rem; color: var(--text-3); }
.kpi.hero { grid-column: 1 / -1; background: var(--accent); border-color: var(--accent); color: #fff; }
.kpi.hero .k, .kpi.hero .d { color: rgba(255,255,255,.85); }
.kpi.hero .v { font-size: 1.8rem; }
@media (min-width: 992px) { .kpi.hero { grid-column: span 2; } }
.delta { font-weight: 650; }
.delta.up { color: var(--good); } .delta.down { color: var(--bad); }
.kpi.hero .delta { color: #fff; }

/* ---------- charts ---------- */
.chart-box { position: relative; height: 260px; }
.chart-box.tall { height: 320px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: .8rem; color: var(--text-2); margin: 4px 0 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.bar-row { display: grid; grid-template-columns: minmax(80px, 1.3fr) 2fr auto; gap: 8px; align-items: center; padding: 6px 0; font-size: .88rem; }
.bar-row .bar { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.bar-row .bar b { display: block; height: 100%; background: var(--s1); border-radius: 4px; }
.bar-row .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th { text-align: left; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-3); font-weight: 650; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl th.sortable { cursor: pointer; user-select: none; } .tbl th.sortable:hover { color: var(--text); }
.tbl td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl tbody tr:hover { background: var(--surface-2); }
.tbl tr.click { cursor: pointer; }
.tbl tfoot td { font-weight: 700; border-top: 2px solid var(--line-2); border-bottom: 0; background: var(--surface-2); }
.tbl .acts { white-space: nowrap; text-align: right; }
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .74rem; font-weight: 650; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.badge.ok { background: var(--good-soft); color: var(--good); } .badge.warn { background: var(--warn-soft); color: var(--warn); } .badge.bad { background: var(--bad-soft); color: var(--bad); }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding-top: 12px; font-size: .86rem; color: var(--text-2); flex-wrap: wrap; }
.empty { text-align: center; padding: 34px 10px; color: var(--text-3); }
.sumbar { display: flex; flex-wrap: wrap; gap: 8px 22px; padding: 10px 14px; margin-bottom: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); font-size: .88rem; }
.sumbar b { font-variant-numeric: tabular-nums; }

@media (max-width: 719px) {
  .tbl.cards thead { display: none; }
  .tbl.cards, .tbl.cards tbody, .tbl.cards tr, .tbl.cards td { display: block; width: 100%; }
  .tbl.cards tbody tr { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 10px 12px; margin-bottom: 10px; box-shadow: var(--shadow); }
  .tbl.cards td { border: 0; padding: 3px 0; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  .tbl.cards td::before { content: attr(data-l); color: var(--text-3); font-size: .78rem; text-align: left; flex: none; padding-top: 2px; }
  .tbl.cards td.p { font-weight: 650; font-size: 1rem; text-align: left; display: block; padding-bottom: 4px; }
  .tbl.cards td.p::before { display: none; }
  .tbl.cards td.acts { justify-content: flex-end; padding-top: 6px; }
  .tbl.cards td.acts::before, .tbl.cards td.nolabel::before { display: none; }
  .tbl.cards tfoot { display: block; } .tbl.cards tfoot tr { display: flex; flex-wrap: wrap; justify-content: space-between; border-radius: var(--r); padding: 10px 12px; background: var(--surface-2); }
  .tbl.cards tfoot td { display: block; width: auto; }
  .tbl.cards tfoot td::before { display: none; }
  .tbl.cards tfoot td:empty { display: none; }
}

/* ---------- combo (searchable select) ---------- */
.combo { position: relative; }
.combo .list { position: absolute; z-index: 60; left: 0; right: 0; top: calc(100% + 4px); max-height: 280px; overflow-y: auto; background: var(--surface); border: 1px solid var(--line-2); border-radius: 10px; box-shadow: 0 10px 30px rgba(0,0,0,.18); display: none; min-width: 220px; }
.combo.open .list { display: block; }
.combo .opt { padding: 9px 12px; cursor: pointer; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 10px; }
.combo .opt:last-child { border-bottom: 0; }
.combo .opt.hi, .combo .opt:hover { background: var(--accent-soft); }
.combo .opt small { color: var(--text-3); white-space: nowrap; }
.combo .opt.new { color: var(--accent); font-weight: 600; }

/* ---------- document form ---------- */
.line { display: grid; gap: 8px; grid-template-columns: repeat(6, 1fr); padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); position: relative; }
.line + .line { margin-top: 8px; }
.line .f-item { grid-column: 1 / -1; }
.line .f-desc { grid-column: 1 / -1; }
.line .f-qty, .line .f-unit, .line .f-rate, .line .f-gst, .line .f-amt { grid-column: span 2; }
.line .f-amt { font-weight: 700; }
.line .f-amt .inp { background: transparent; border-color: transparent; font-weight: 700; padding-left: 0; }
.line .del { position: absolute; top: 6px; right: 6px; }
.line .hintline { grid-column: 1 / -1; font-size: .78rem; color: var(--text-3); min-height: 1em; }
.line .hintline .warn { color: var(--warn); font-weight: 600; }
.line-head { display: none; }
@media (min-width: 992px) {
  .line, .line-head { grid-template-columns: minmax(220px, 3.2fr) 1fr 90px 1.1fr 90px 1.2fr 34px; }
  .line-head { display: grid; gap: 8px; padding: 0 12px 6px; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-3); font-weight: 650; }
  .line { background: transparent; padding: 8px 12px; align-items: start; }
  .line .f-item, .line .f-qty, .line .f-unit, .line .f-rate, .line .f-gst, .line .f-amt { grid-column: auto; }
  .line .f-desc, .line .hintline { grid-column: 1 / 6; }
  .line .del { position: static; align-self: start; }
  .line .field > label { display: none; }
  .line .f-amt .inp { text-align: right; }
}
.totals { display: grid; gap: 4px; margin-left: auto; max-width: 360px; font-variant-numeric: tabular-nums; }
.totals div { display: flex; justify-content: space-between; gap: 20px; }
.totals .gt { font-size: 1.2rem; font-weight: 750; border-top: 2px solid var(--line-2); padding-top: 6px; margin-top: 4px; }
.savebar { position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); z-index: 32; background: var(--surface); border-top: 1px solid var(--line); padding: 10px 14px; display: flex; gap: 10px; justify-content: space-between; align-items: center; }
@media (min-width: 992px) { .savebar { left: var(--side-w); bottom: 0; padding: 12px 28px; } }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.45); display: flex; align-items: flex-end; justify-content: center; }
.modal { background: var(--surface); width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; border-radius: 18px 18px 0 0; padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); box-shadow: 0 -10px 40px rgba(0,0,0,.25); animation: up .18s ease-out; }
.modal.wide { max-width: 760px; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@media (min-width: 720px) { .overlay { align-items: center; } .modal { border-radius: 16px; } }
.modal .mh { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal .mf { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.menu-list a, .menu-list button { display: flex; align-items: center; gap: 12px; padding: 12px 6px; border: 0; border-bottom: 1px solid var(--line); background: none; width: 100%; color: var(--text); font: inherit; text-align: left; cursor: pointer; }
.menu-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.menu-grid a { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 14px 6px; border-radius: 12px; background: var(--surface-2); color: var(--text); font-size: .8rem; text-align: center; font-weight: 550; }
#toasts { position: fixed; z-index: 200; left: 0; right: 0; top: calc(10px + env(safe-area-inset-top)); display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.toast { background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.3); max-width: min(92vw, 460px); pointer-events: auto; animation: up .2s; }
.toast.err { background: var(--bad); } .toast.ok { background: var(--good); color: #fff; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login .card { width: 100%; max-width: 380px; padding: 26px; }
.login .logo { width: 52px; height: 52px; border-radius: 14px; background: var(--accent); display: grid; place-items: center; margin-bottom: 14px; }
.login .logo svg { width: 30px; height: 30px; stroke: #fff; }

/* ---------- permissions matrix / misc ---------- */
.perm-grid { display: grid; grid-template-columns: 1fr repeat(4, 48px); gap: 4px 6px; align-items: center; font-size: .88rem; }
.perm-grid .h { font-size: .72rem; color: var(--text-3); text-align: center; text-transform: uppercase; }
.perm-grid input { width: 18px; height: 18px; accent-color: var(--accent); justify-self: center; }
.tabs { display: flex; gap: 4px; overflow-x: auto; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tabs a { padding: 9px 14px; color: var(--text-2); white-space: nowrap; border-bottom: 2px solid transparent; font-weight: 550; }
.tabs a.on { color: var(--accent); border-color: var(--accent); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: .9rem; }
.kv dt { color: var(--text-3); } .kv dd { margin: 0; }

/* ---------- print ---------- */
.print-wrap { background: #fff; color: #000; max-width: 860px; margin: 0 auto; padding: 22px; border: 1px solid var(--line); border-radius: 6px; font-size: 13px; }
.print-wrap h1 { font-size: 1.4rem; }
.print-wrap table { width: 100%; border-collapse: collapse; }
.print-wrap th, .print-wrap td { border: 1px solid #999; padding: 5px 7px; text-align: left; vertical-align: top; }
.print-wrap th { background: #f0f0f0; font-size: 12px; }
.print-wrap .r { text-align: right; }
.print-wrap .box { border: 1px solid #999; padding: 8px; }
.print-wrap .noborder td { border: 0; padding: 2px 4px; }
.pr-head { display: flex; justify-content: space-between; gap: 16px; border-bottom: 2px solid #000; padding-bottom: 8px; margin-bottom: 10px; }
@media print {
  body { background: #fff; }
  .top, .side, .bottom-nav, .fab, .savebar, .no-print, #toasts { display: none !important; }
  .shell { padding: 0 !important; } .main { padding: 0 !important; max-width: none; }
  .print-wrap { border: 0; padding: 0; max-width: none; font-size: 12px; }
  @page { size: A4; margin: 12mm; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---------- overflow guards (grid children must be allowed to shrink so charts/tables never push the page wide) ---------- */
.grid > *, .kpis > *, .card, .main, .stack > * { min-width: 0; }
.chart-box canvas { max-width: 100%; }
html, body { overflow-x: hidden; }
@media (max-width: 719px) { .tbl.cards td.nolabel { display: none; } }
