/* AdminPortal - application styles. No external fonts or libraries. */

:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e2e6ea;
  --border-strong: #c9d0d7;
  --text: #17212b;
  --muted: #5b6977;
  --faint: #8794a1;
  --primary: #0b6e63;
  --primary-hover: #09574f;
  --primary-soft: #e2f2ef;
  --on-primary: #ffffff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #a8510a;
  --warn-soft: #fff3e2;
  --ok: #067647;
  --ok-soft: #e4f5eb;
  --info: #1a56c4;
  --info-soft: #e7f0fd;
  --sidebar: #10212b;
  --sidebar-text: #b9c6cf;
  --sidebar-muted: #6f8391;
  --sidebar-hover: #19303d;
  --sidebar-active: #214151;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.22);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "Segoe UI Mono", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1317;
    --surface: #161c22;
    --surface-2: #1b232a;
    --border: #27313a;
    --border-strong: #3a4753;
    --text: #e4e9ee;
    --muted: #9ba8b4;
    --faint: #73818e;
    --primary: #2fa596;
    --primary-hover: #49b8aa;
    --primary-soft: #12332f;
    --on-primary: #06211e;
    --danger: #f1705f;
    --danger-soft: #3a1a17;
    --warn: #f0a04b;
    --warn-soft: #382611;
    --ok: #4cc38a;
    --ok-soft: #12301f;
    --info: #6ea8fe;
    --info-soft: #14263f;
    --sidebar: #0a1116;
    --sidebar-hover: #131f27;
    --sidebar-active: #1a2c37;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0; font-weight: 650; line-height: 1.25; }
h1 { font-size: 21px; letter-spacing: -0.01em; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
p { margin: 0 0 8px; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
small, .small { font-size: 12px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.center { text-align: center; }
.neg { color: var(--danger); }
.hidden { display: none !important; }
.grow { flex: 1 1 auto; }
.stack > * + * { margin-top: 16px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

/* ---------- boot, sign-in, setup ---------- */
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: linear-gradient(160deg, #0d2a33 0%, #10212b 55%, #0b3b36 100%); }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-lg); padding: 28px 28px 24px; }
.auth-card.wide { max-width: 520px; }
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.brand-mark { width: 34px; height: 34px; border-radius: 8px; background: var(--primary); color: var(--on-primary); display: grid; place-items: center; font-weight: 800; font-size: 15px; letter-spacing: -0.03em; }
.brand-name { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.auth-card h1 { margin-bottom: 4px; }
.auth-card .lead { color: var(--muted); margin-bottom: 18px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 236px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.sidebar .brand { padding: 16px 16px 10px; margin: 0; color: #fff; }
.sidebar .company { padding: 0 16px 12px; font-size: 12px; color: var(--sidebar-muted); border-bottom: 1px solid rgba(255, 255, 255, 0.06); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav { padding: 8px 8px 16px; flex: 1; }
.nav-group { margin-top: 14px; padding: 0 8px 4px; font-size: 10.5px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--sidebar-muted); }
.nav a { display: flex; align-items: center; gap: 10px; padding: 7px 10px; margin: 1px 0; border-radius: 6px; color: var(--sidebar-text); font-size: 13.5px; }
.nav a:hover { background: var(--sidebar-hover); color: #fff; text-decoration: none; }
.nav a.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav a svg { flex: none; opacity: 0.85; }
.nav .count { margin-left: auto; background: #d97706; color: #fff; border-radius: 10px; font-size: 11px; font-weight: 700; padding: 0 6px; line-height: 17px; }
.sidebar-foot { padding: 10px 16px 14px; font-size: 11px; color: var(--sidebar-muted); border-top: 1px solid rgba(255, 255, 255, 0.06); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { height: 52px; display: flex; align-items: center; gap: 12px; padding: 0 24px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; }
.menu-btn { display: none; }
.rate-chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-size: 12.5px; background: var(--primary-soft); color: var(--primary); font-variant-numeric: tabular-nums; border: 0; cursor: pointer; font-family: inherit; }
.rate-chip.stale { background: var(--warn-soft); color: var(--warn); }
.rate-chip .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.user-btn { display: inline-flex; align-items: center; gap: 8px; background: none; border: 0; padding: 4px 6px; border-radius: 6px; cursor: pointer; color: var(--text); font: inherit; }
.user-btn:hover { background: var(--surface-2); }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--primary); color: var(--on-primary); display: grid; place-items: center; font-weight: 700; font-size: 12px; }
.user-meta { text-align: left; line-height: 1.15; }
.user-meta .role { font-size: 11px; color: var(--muted); }
.menu { position: absolute; right: 16px; top: 48px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow-lg); min-width: 190px; padding: 4px; z-index: 40; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 5px; cursor: pointer; font: inherit; color: var(--text); }
.menu button:hover { background: var(--surface-2); }

.content { padding: 22px 24px 48px; max-width: 1320px; width: 100%; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 3px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.crumb { font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.card-body { padding: 16px; }
.card-foot { padding: 10px 16px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12.5px; }
.grid { display: grid; gap: 16px; }
.grid.c2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.c3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.c4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpi { padding: 14px 16px; }
.kpi .label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.kpi .value small { font-size: 12px; font-weight: 600; color: var(--muted); margin-right: 3px; }
.kpi .foot { font-size: 12px; color: var(--muted); margin-top: 3px; }
.kpi .foot.bad { color: var(--danger); }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 34px; padding: 0 13px; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: none; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 12.5px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.icon-btn { width: 28px; height: 28px; display: inline-grid; place-items: center; border: 0; background: none; border-radius: 5px; color: var(--muted); cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); color: var(--danger); }

/* ---------- forms ---------- */
label.field { display: block; }
.field > span, .field-label { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field .hint { font-size: 12px; color: var(--faint); margin-top: 3px; font-weight: 400; }
input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="month"], input[type="email"], input[type="search"], select, textarea {
  width: 100%; height: 34px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--surface); color: var(--text); font: inherit;
}
textarea { height: auto; min-height: 70px; padding: 8px 10px; resize: vertical; }
input:disabled, select:disabled, textarea:disabled, input[readonly] { background: var(--surface-2); color: var(--muted); }
input.num, input[data-money] { text-align: right; font-variant-numeric: tabular-nums; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid.c3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.form-grid.c4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.form-grid .span2 { grid-column: span 2; }
.form-grid .span-all { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; }
.check input { width: 16px; height: 16px; accent-color: var(--primary); }
.form-error { background: var(--danger-soft); color: var(--danger); border-radius: 6px; padding: 8px 12px; margin-bottom: 12px; font-size: 13px; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar input, .toolbar select { width: auto; min-width: 150px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; }
table.data th { text-align: left; font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); padding: 9px 12px; border-bottom: 1px solid var(--border); background: var(--surface-2); white-space: nowrap; }
table.data td { padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr.click { cursor: pointer; }
table.data tbody tr.click:hover { background: var(--surface-2); }
table.data th.num, table.data td.num { text-align: right; }
table.data tfoot td { font-weight: 700; border-top: 2px solid var(--border-strong); border-bottom: 0; background: var(--surface-2); }
table.data tr.group td { background: var(--surface-2); font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
table.data tr.subtotal td { font-weight: 700; border-top: 1px solid var(--border-strong); }
table.data tr.total td { font-weight: 800; border-top: 2px solid var(--text); border-bottom: 3px double var(--text); font-size: 14.5px; }
table.data tr.indent td:first-child { padding-left: 28px; }
table.data tr.dept td { background: var(--surface-2); }
table.data tr.indent2 td:first-child { padding-left: 48px; }
table.data td.tight { width: 1%; white-space: nowrap; }
.empty { padding: 34px 16px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--text); margin-bottom: 4px; }

/* line-entry grids (journals, documents) */
table.lines { width: 100%; border-collapse: collapse; }
table.lines th { text-align: left; font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); padding: 6px 6px; }
table.lines td { padding: 3px 4px; vertical-align: top; }
table.lines td.amt { padding-top: 10px; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
table.lines input, table.lines select { height: 32px; padding: 0 8px; }
.totals { margin-left: auto; width: 340px; max-width: 100%; }
.totals div { display: flex; justify-content: space-between; padding: 4px 0; font-variant-numeric: tabular-nums; }
.totals .grand { font-weight: 800; font-size: 16px; border-top: 2px solid var(--text); margin-top: 4px; padding-top: 8px; }
.totals .equiv { color: var(--muted); font-size: 12.5px; }

/* ---------- badges, notices ---------- */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 650; line-height: 18px; white-space: nowrap; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge.info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.notice { border-radius: var(--radius); padding: 10px 14px; background: var(--info-soft); color: var(--info); font-size: 13.5px; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.bad { background: var(--danger-soft); color: var(--danger); }
.notice.ok { background: var(--ok-soft); color: var(--ok); }
.notice strong { font-weight: 700; }

/* ---------- tabs ---------- */
/* Import screen */
ul.plain { margin: 0; padding-left: 18px; }
ul.plain li + li { margin-top: 4px; }
.step-no { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); font-weight: 700; font-size: 12.5px; }
table.import-steps td { vertical-align: top; }
.sample, .result-rows { border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
.sample { max-height: 240px; }
.result-rows { max-height: 46vh; }
.sample table.data th, .sample table.data td { padding: 5px 9px; font-size: 12.5px; white-space: nowrap; max-width: 260px; overflow: hidden; text-overflow: ellipsis; }
.result-rows table.data th { position: sticky; top: 0; z-index: 1; }
.result-rows table.data td { padding: 6px 10px; font-size: 13px; }
select.account-pick { min-width: 220px; height: 30px; font-size: 12.5px; }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs a, .tabs button { padding: 8px 14px; border: 0; background: none; font: inherit; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; cursor: pointer; white-space: nowrap; }
.tabs a:hover, .tabs button:hover { color: var(--text); text-decoration: none; }
.tabs a.active, .tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ---------- modal, toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(10, 18, 24, 0.5); display: grid; place-items: start center; padding: 6vh 16px 16px; z-index: 100; overflow-y: auto; }
.dialog { background: var(--surface); border-radius: 10px; box-shadow: var(--shadow-lg); width: 100%; max-width: 560px; }
.dialog.wide { max-width: 860px; }
.dialog.narrow { max-width: 420px; }
.dialog.xwide { max-width: 1080px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.dialog-body { padding: 18px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 10px 10px; }
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: #17212b; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow-lg); max-width: 380px; font-size: 13.5px; border-left: 4px solid var(--ok); }
.toast.error { border-left-color: #f1705f; }

/* ---------- dashboard chart ---------- */
.chart { width: 100%; height: 180px; display: block; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.progress { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; margin-top: 8px; }
.progress i { display: block; height: 100%; background: var(--primary); }
.progress.low i { background: #d97706; }

/* ---------- printable document ("paper") ---------- */
.paper { background: #fff; color: #111; border: 1px solid var(--border); border-radius: 4px; box-shadow: var(--shadow); padding: 44px 48px; max-width: 880px; margin: 0 auto; font-size: 13px; }
.paper h1 { font-size: 22px; letter-spacing: 0.02em; text-transform: uppercase; color: #111; }
.paper .co-name { font-size: 18px; font-weight: 750; }
.paper .doc-head { display: flex; justify-content: space-between; gap: 24px; margin-bottom: 26px; }
.paper .doc-meta { text-align: right; }
.paper .doc-meta table { margin-left: auto; border-collapse: collapse; margin-top: 8px; }
.paper .doc-meta td { padding: 1px 0 1px 14px; text-align: right; }
.paper .doc-meta td:first-child { color: #555; }
.paper .parties { display: flex; gap: 32px; margin-bottom: 22px; }
.paper .parties > div { flex: 1; }
.paper .cap { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.07em; color: #666; margin-bottom: 3px; }
.paper table.items { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.paper table.items th { text-align: left; font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.06em; color: #444; border-bottom: 2px solid #111; padding: 6px 8px; }
.paper table.items td { padding: 7px 8px; border-bottom: 1px solid #ddd; vertical-align: top; }
.paper table.items .num { text-align: right; }
.paper .doc-totals { margin-left: auto; width: 300px; }
.paper .doc-totals div { display: flex; justify-content: space-between; padding: 3px 8px; font-variant-numeric: tabular-nums; }
.paper .doc-totals .grand { font-weight: 800; font-size: 15px; border-top: 2px solid #111; border-bottom: 3px double #111; margin-top: 4px; padding: 7px 8px; }
.paper .doc-totals .equiv { color: #555; font-size: 12px; }
.paper .pay-block { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; margin-top: 22px; padding: 12px 14px; border: 1px solid #c9c9c9; border-radius: 6px; break-inside: avoid; }
.paper .pay-text p { white-space: pre-line; margin: 0 0 6px; }
.paper .pay-text .pay-ref { color: #555; font-size: 12px; margin: 0; }
.paper .pay-qr { flex: none; text-align: center; }
.paper .qr { display: block; width: 132px; height: 132px; }
.paper .pay-qr-cap { font-size: 10.5px; color: #555; margin-top: 2px; }
.paper .doc-foot { margin-top: 26px; display: grid; gap: 14px; font-size: 12.5px; }
.paper .doc-foot p { white-space: pre-line; margin: 0; }
.paper .sign { display: flex; gap: 40px; margin-top: 40px; }
.paper .sign div { flex: 1; border-top: 1px solid #333; padding-top: 4px; font-size: 11.5px; color: #555; }
.paper .stamp { display: inline-block; border: 2px solid #b42318; color: #b42318; font-weight: 800; padding: 2px 10px; border-radius: 4px; text-transform: uppercase; letter-spacing: 0.08em; transform: rotate(-4deg); margin-top: 8px; }
.paper .stamp.ok { border-color: #067647; color: #067647; }
.paper a { color: inherit; }

/* company logo */
.co-logo { display: block; max-height: 72px; max-width: 300px; margin-bottom: 8px; object-fit: contain; }
.report-head .co-logo { max-height: 50px; max-width: 240px; margin: 0 auto 8px; }
.auth-card .co-logo { max-height: 76px; max-width: 100%; margin: 2px 0 16px; }
.logo-preview { background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 10px 14px; }
.logo-preview .co-logo { margin: 0; max-height: 64px; }
@media (prefers-color-scheme: dark) {
  /* Logos are drawn for white paper; give them a white plate on dark surfaces. */
  .report-head .co-logo, .auth-card .co-logo { background: #fff; padding: 6px 10px; border-radius: 6px; }
}
@media print {
  .report-head .co-logo { background: none; padding: 0; }
}

.report-head { text-align: center; margin-bottom: 14px; }
.report-head .co { font-size: 16px; font-weight: 700; }
.report-head .title { font-size: 14px; font-weight: 600; }
.report-head .period { color: var(--muted); font-size: 12.5px; }

details.text-preview summary { cursor: pointer; color: var(--muted); font-size: 12.5px; }
details.text-preview pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 10px; font-size: 12px; max-height: 220px; overflow: auto; white-space: pre-wrap; }
.matrix th.rot { font-size: 10.5px; text-align: center; white-space: normal; min-width: 76px; }
.matrix td.center { color: var(--ok); font-weight: 800; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .grid.c4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.c3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid.c4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; left: 0; top: 0; width: 250px; z-index: 60; transform: translateX(-100%); transition: transform 0.18s ease; }
  .shell.nav-open .sidebar { transform: none; box-shadow: var(--shadow-lg); }
  .menu-btn { display: inline-grid; }
  .topbar { padding: 0 12px; }
  .content { padding: 16px 12px 40px; }
  .grid.c2, .grid.c3, .grid.c4, .form-grid, .form-grid.c3, .form-grid.c4 { grid-template-columns: minmax(0, 1fr); }
  .form-grid .span2 { grid-column: auto; }
  .user-meta { display: none; }
  .paper { padding: 22px 18px; overflow-x: auto; }
  .paper .doc-head, .paper .parties { flex-direction: column; gap: 12px; }
  .paper .doc-meta { text-align: left; }
  .paper .doc-meta table { margin-left: 0; }
  .paper .doc-meta td { text-align: left; padding: 1px 14px 1px 0; }
}

/* ---------- phones ---------- */
@media (max-width: 640px) {
  /* Bigger targets for fingers. Text of 16px stops a phone zooming in when a box is tapped. */
  input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="month"], input[type="email"], input[type="search"], select, textarea { font-size: 16px; min-height: 40px; }
  .btn { min-height: 40px; }
  .btn.sm { min-height: 34px; height: auto; }
  .icon-btn, .menu-btn { width: 40px; height: 40px; }
  .nav a { min-height: 44px; }
  .sidebar { width: min(290px, 86vw); overflow-y: auto; }
  .toolbar input, .toolbar select { width: 100%; min-width: 0; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1 1 auto; }
  .page-head h1 { overflow-wrap: anywhere; }
  .overlay { padding: 12px 8px; }
  .dialog-foot { flex-wrap: wrap; }
  .dialog-foot .btn { flex: 1 1 auto; }
  .totals { width: 100%; }

  /* Lists become cards: one record per block, each value beside its heading. */
  table.data.stack thead { display: none; }
  table.data.stack, table.data.stack tbody, table.data.stack tfoot { display: block; }
  table.data.stack tr { display: block; padding: 10px 12px; border-bottom: 1px solid var(--border); }
  table.data.stack tr:last-child { border-bottom: 0; }
  table.data.stack td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 2px 0; border: 0; text-align: right; width: auto; white-space: normal; }
  table.data.stack td::before { content: attr(data-label); flex: none; color: var(--muted); font-size: 12px; text-align: left; }
  table.data.stack td:first-child { font-size: 15px; font-weight: 650; }
  table.data.stack td:first-child::before { font-weight: 400; }
  table.data.stack td:empty, table.data.stack td.hide-sm { display: none; }
  table.data.stack tfoot tr { background: var(--surface-2); }
  table.data.stack tfoot td { border: 0; background: none; }

  /* Lines of a document: one line per block, with boxes a thumb can use. */
  table.lines.stackable { display: block; min-width: 0 !important; }
  table.lines.stackable thead { display: none; }
  table.lines.stackable tbody { display: block; }
  table.lines.stackable tr { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; padding: 12px 0; border-bottom: 1px solid var(--border); }
  table.lines.stackable td { display: block; width: auto !important; padding: 0; border: 0; text-align: left; }
  table.lines.stackable td::before { content: attr(data-label); display: block; font-size: 12px; color: var(--muted); margin-bottom: 2px; }
  table.lines.stackable td.wide { grid-column: 1 / -1; }
  table.lines.stackable td.amt { padding-top: 0; font-weight: 650; font-size: 15px; }
  table.lines.stackable td.remove { text-align: right; align-self: end; }
  table.lines.stackable td.remove::before { content: none; }
  table.lines.stackable input, table.lines.stackable select { width: 100%; height: 40px; }

  /* The printed document on a narrow screen. */
  .paper { padding: 16px 12px; font-size: 12.5px; }
  .paper h1 { font-size: 18px; }
  .paper table.items th, .paper table.items td { padding: 6px 4px; }
  .paper .doc-totals { width: 100%; }
  .paper .pay-block { flex-direction: column; }
  .paper .sign { flex-direction: column; gap: 22px; margin-top: 26px; }
}

/* ---------- print ---------- */
@media print {
  :root { --bg: #fff; --surface: #fff; --surface-2: #fff; --text: #000; --muted: #333; --border: #bbb; --border-strong: #888; }
  body { background: #fff; font-size: 12px; }
  .sidebar, .topbar, .page-actions, .no-print, .toasts, .tabs, .toolbar { display: none !important; }
  .shell { display: block; }
  .content { padding: 0; max-width: none; }
  .card { border: 0; box-shadow: none; }
  .paper { border: 0; box-shadow: none; padding: 0; max-width: none; }
  table.data th { background: #fff; }
  a { color: inherit; text-decoration: none; }
  @page { margin: 16mm 14mm; }
}
