/* Jiken Tractor Services -- mobile-first app shell */

:root {
  --bg:        #f4f6f4;
  --surface:   #ffffff;
  --surface-2: #fafbfa;
  --border:    #dfe4df;
  --text:      #1d2420;
  --muted:     #67736c;
  --accent:    #2f6f4f;
  --accent-ink:#ffffff;
  --accent-soft:#e7f1ea;
  --warn:      #9a6b12;
  --warn-soft: #fbf1dc;
  --danger:    #a63232;
  --danger-soft:#fbe9e9;
  --ok:        #2f6f4f;
  --ok-soft:   #e7f1ea;
  --info:      #2b5d86;
  --info-soft: #e6eef6;
  --radius:    12px;
  --shadow:    0 1px 2px rgba(20, 30, 25, .06), 0 1px 8px rgba(20, 30, 25, .04);
  --tap:       44px;              /* minimum comfortable touch target */
  --tabbar-h:  58px;
  --safe-b:    env(safe-area-inset-bottom, 0px);
  --safe-l:    env(safe-area-inset-left, 0px);
  --safe-r:    env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  /* 16px base: anything smaller makes iOS Safari zoom on focus. */
  font: 16px/1.5 "Segoe UI", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}

body.drawer-open { overflow: hidden; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Kill the grey flash on tap, keep a real focus ring for keyboards. */
a, button, .btn, .nav-item, .tab, summary {
  -webkit-tap-highlight-color: transparent;
}
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- shell ---------- */

.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px;
  flex: 0 0 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.brand-mark {
  width: 36px; height: 36px; flex: 0 0 36px;
  border-radius: 8px;
}
.brand-text { font-weight: 650; font-size: 15px; line-height: 1.25; }

/* min-height:0 is what lets this shrink inside the flex column and actually
   scroll. Without it the list is clipped and the last items are unreachable. */
.sidebar nav {
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.nav-item {
  display: flex; align-items: center;
  min-height: var(--tap);
  padding: 10px 12px;
  border-radius: 9px;
  color: var(--text);
  font-size: 15px;
}
.nav-item:hover { background: var(--surface-2); text-decoration: none; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item.subtle { color: var(--muted); font-size: 14px; }
.nav-ico { width: 26px; flex: 0 0 26px; display: inline-block; }
.nav-label { min-width: 0; }

.sidebar-foot { flex: 0 0 auto; padding: 10px 8px 14px; border-top: 1px solid var(--border); }
.who { padding: 8px 12px; font-size: 14px; font-weight: 600; }
.who span { display: block; font-weight: 400; color: var(--muted); font-size: 13px; }

.drawer-close { display: none; }

.scrim {
  position: fixed; inset: 0;
  background: rgba(10, 16, 13, .45);
  opacity: 0; pointer-events: none;
  transition: opacity .18s ease;
  z-index: 35;
}
.scrim.show { opacity: 1; pointer-events: auto; }

.main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 19px; margin: 0; font-weight: 650; min-width: 0; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }

.menu-btn {
  display: none;
  background: none; border: 1px solid var(--border);
  border-radius: 9px;
  min-width: var(--tap); min-height: var(--tap);
  font-size: 18px; cursor: pointer;
  color: var(--text);
}

.content { padding: 22px; max-width: 1400px; width: 100%; }

/* Bottom tab bar: mobile only. */
.tabbar { display: none; }

/* ---------- cards & grids ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 18px;
  overflow: hidden;
}
.card-head {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.card-head h2 { font-size: 16px; margin: 0; font-weight: 650; }
.card-head .spacer { margin-left: auto; }
.card-body { padding: 16px; }
.card-foot { padding: 12px 16px; border-top: 1px solid var(--border); background: var(--surface-2); }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(178px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 22px; font-weight: 680; margin-top: 4px; letter-spacing: -.01em; }
.stat-sub { font-size: 12px; color: var(--muted); margin-top: 3px; }
.stat.ok     { border-left-color: var(--ok); }
.stat.warn   { border-left-color: var(--warn); }
.stat.danger { border-left-color: var(--danger); }
.stat.info   { border-left-color: var(--info); }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 18px; }

/* ---------- tables ---------- */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

table.data { width: 100%; border-collapse: collapse; font-size: 15px; }
table.data th, table.data td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data thead th {
  background: var(--surface-2);
  font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); font-weight: 650; white-space: nowrap;
}
table.data tbody tr:hover { background: var(--surface-2); }
table.data tbody tr:last-child td { border-bottom: none; }
table.data tfoot td { background: var(--surface-2); font-weight: 650; border-top: 2px solid var(--border); }

/* ---------- client groups in the receivables table ----------
   Each client is its own <tbody>, headed by a banded row carrying what they
   owe and the button that collects the lot onto one receipt. */

table.data tbody.group + tbody.group .group-head td {
  border-top: 2px solid var(--border);   /* separates one client from the last */
}
table.data tr.group-head td {
  background: var(--surface-2);
  padding-top: 10px;
  padding-bottom: 10px;
  vertical-align: middle;
}
table.data tr.group-head:hover td { background: var(--surface-2); }
.group-name {
  font-weight: 700;
  font-size: 15px;
}
table.data tr.group-head .sub { margin-top: 3px; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 28px 14px !important; }
.sub { display: block; font-size: 13px; color: var(--muted); margin-top: 2px; }

/* ---------- badges ---------- */

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-ok     { background: var(--ok-soft);     color: var(--ok); }
.badge-warn   { background: var(--warn-soft);   color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info   { background: var(--info-soft);   color: var(--info); }
.badge-muted  { background: var(--surface-2);   color: var(--muted); border: 1px solid var(--border); }

/* ---------- forms ---------- */

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
}
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.field label { font-size: 14px; font-weight: 600; color: var(--muted); }
.field .hint { font-size: 13px; color: var(--muted); font-weight: 400; }

input[type=text], input[type=number], input[type=date], input[type=password],
input[type=tel], select, textarea {
  font: inherit;
  font-size: 16px;               /* prevents iOS zoom-on-focus */
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  width: 100%;
  min-width: 0;
}
select {
  /* Native pickers are best on phones; just make it look consistent. */
  appearance: none; -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) calc(50% + 2px), calc(100% - 13px) calc(50% + 2px);
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 34px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}
textarea { resize: vertical; min-height: 76px; }
input[readonly] { background: var(--surface-2); color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  font: inherit; font-weight: 600; font-size: 15px;
  min-height: var(--tap);
  padding: 9px 16px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #275c42; }
.btn-danger { color: var(--danger); border-color: #e7c9c9; background: var(--danger-soft); }
.btn-danger:hover { background: #f6dcdc; }
.btn-sm { min-height: 36px; padding: 5px 12px; font-size: 14px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn-row.end { justify-content: flex-end; }

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  align-items: end;
}

/* Injected on small screens to collapse a filter panel. */
.filter-toggle { display: none; }

.alert {
  padding: 12px 14px;
  border-radius: 9px;
  margin-bottom: 14px;
  font-size: 15px;
  border: 1px solid transparent;
}
.alert-success { background: var(--ok-soft);     color: var(--ok);     border-color: #c9e0d3; }
.alert-error   { background: var(--danger-soft); color: var(--danger); border-color: #e7c9c9; }
.alert-warning { background: var(--warn-soft);   color: var(--warn);   border-color: #ecd9ae; }
.alert-info    { background: var(--info-soft);   color: var(--info);   border-color: #c7dcec; }

.errors { margin: 0 0 14px; padding: 12px 14px 12px 32px; border-radius: 9px; background: var(--danger-soft); color: var(--danger); border: 1px solid #e7c9c9; font-size: 15px; }
.errors li { margin: 3px 0; }

/* ---------- misc ---------- */

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px; font-size: 15px; }
.kv dt { color: var(--muted); font-size: 14px; }
.kv dd { margin: 0; font-weight: 500; min-width: 0; }

.bar { height: 7px; border-radius: 4px; background: var(--surface-2); overflow: hidden; border: 1px solid var(--border); }
.bar > span { display: block; height: 100%; background: var(--accent); }

.pill-row { display: flex; gap: 6px; flex-wrap: wrap; }

.money-pos { color: var(--ok); font-weight: 600; }
.money-neg { color: var(--danger); font-weight: 600; }

.print-head { display: none; }

/* Floating action button: the one main action, within thumb reach. */
.fab { display: none; }

/* ---------- login ---------- */

/* Flex, not grid: an auto-sized grid column takes its max-content width, which
   a long business name pushes past the viewport edge. */
.auth-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%; max-width: 400px; min-width: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow); padding: 26px;
}
.auth-card h1 { font-size: 20px; margin: 0 0 4px; }
.auth-card p.lead { color: var(--muted); font-size: 15px; margin: 0 0 18px; }


/* ============================================================
   PHONES AND SMALL TABLETS
   ============================================================ */

@media screen and (max-width: 860px) {

  /* --- off-canvas drawer --- */
  .sidebar {
    position: fixed; z-index: 40; top: 0; left: 0; height: 100%;
    width: 84vw; max-width: 320px; flex-basis: auto;
    transform: translateX(-102%);
    transition: transform .2s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, .25);
    padding-left: var(--safe-l);
  }
  .sidebar.open { transform: translateX(0); }

  /* Two columns so the whole menu is on screen at once -- no scrolling to
     reach the items at the bottom of the list. */
  .sidebar { width: 92vw; max-width: 380px; }
  .sidebar nav {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 12px 10px;
    flex: 0 1 auto;
    overflow: visible;
  }
  .nav-item {
    padding: 10px;
    gap: 6px;
    font-size: 14px;
    border: 1px solid var(--border);
  }
  .nav-item.active { border-color: var(--accent); }
  .nav-ico { width: 20px; flex-basis: 20px; font-size: 15px; }
  .nav-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Name and sign-out share one compact row. */
  .sidebar-foot {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px calc(10px + var(--safe-b));
  }
  .who { flex: 1 1 auto; padding: 0 2px; font-size: 13px; }
  .sidebar-foot .nav-item.subtle {
    flex: 0 0 auto; border: 1px solid var(--border);
    min-height: 38px; padding: 8px 12px;
  }
  .brand { padding: 12px 14px; }

  .drawer-close {
    display: grid; place-items: center;
    margin-left: auto;
    width: var(--tap); height: var(--tap);
    border: 1px solid var(--border); border-radius: 9px;
    background: var(--surface); color: var(--text);
    font-size: 20px; line-height: 1; cursor: pointer;
  }

  /* --- topbar --- */
  .topbar {
    padding: 10px 14px;
    padding-left: calc(14px + var(--safe-l));
    padding-right: calc(14px + var(--safe-r));
    gap: 10px;
  }
  .topbar h1 {
    font-size: 17px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .menu-btn { display: grid; place-items: center; }

  /* Actions move below the title. Two per row, so a three-action page costs
     two rows instead of three. */
  .topbar { flex-wrap: wrap; }
  .topbar-actions {
    margin-left: 0;
    flex-basis: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .topbar-actions:empty { display: none; }
  .topbar-actions .btn { width: 100%; }
  /* The primary action gets the full width and the rest pair up beneath it.
     The last rule covers a FAB page whose hidden primary leaves one button. */
  .topbar-actions .btn-primary,
  .topbar-actions .btn:only-child,
  .topbar-actions.has-fab .btn:nth-child(2):nth-last-child(1) { grid-column: 1 / -1; }

  .content {
    padding: 14px;
    padding-left: calc(14px + var(--safe-l));
    padding-right: calc(14px + var(--safe-r));
    /* room for the tab bar + FAB */
    padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 18px);
  }

  .card { margin-bottom: 14px; border-radius: var(--radius); }
  .card-body { padding: 14px; }
  .card-head { padding: 12px 14px; }
  .card-foot { padding: 12px 14px; }

  /* --- stats: two across, compact --- */
  .stats { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
  .stat { padding: 11px 12px; border-left-width: 3px; }
  .stat-label { font-size: 11px; }
  .stat-value { font-size: 19px; }
  .stat-sub { font-size: 11px; }

  .grid-2 { grid-template-columns: 1fr; gap: 14px; }

  .form-grid { grid-template-columns: 1fr; gap: 14px; }
  .filters { grid-template-columns: 1fr; gap: 12px; }
  .btn-row .btn { flex: 1 1 auto; }
  .card-foot .btn-row .btn { min-width: 120px; }

  /* --- collapsible filter panels --- */
  .filter-toggle {
    display: flex; align-items: center; gap: 8px;
    width: 100%;
    min-height: var(--tap);
    padding: 10px 14px;
    border: 0; border-bottom: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text); font: inherit; font-weight: 600; font-size: 15px;
    cursor: pointer; text-align: left;
  }
  .filter-toggle .chev { margin-left: auto; color: var(--muted); transition: transform .15s ease; }
  .filter-toggle[aria-expanded="true"] .chev { transform: rotate(180deg); }
  .filter-toggle .dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--accent); flex: 0 0 8px;
  }
  .filter-body[hidden] { display: none; }

  /* ============================================================
     TABLES BECOME CARDS
     Labels come from each table's own <thead>, injected by app.js.
     Without JS the table just scrolls sideways as before.
     ============================================================ */

  table.data.as-cards { font-size: 15px; display: block; }
  table.data.as-cards thead { display: none; }
  table.data.as-cards tbody,
  table.data.as-cards tfoot { display: block; }

  /* A flex column, so the identifying cell can be pulled to the top of the
     card even when it is not the table's first column. */
  table.data.as-cards tbody tr {
    display: flex;
    flex-direction: column;
    border: 1px solid var(--border);
    border-radius: 10px;
    margin: 0 0 10px;
    padding: 4px 0;
    background: var(--surface);
  }
  table.data.as-cards tbody tr:hover { background: var(--surface); }
  table.data.as-cards tbody tr:last-child { margin-bottom: 0; }

  table.data.as-cards td {
    display: flex;
    align-items: baseline;
    gap: 12px;
    padding: 6px 12px;
    border: 0;
    text-align: left !important;
    white-space: normal;
  }
  table.data.as-cards td::before {
    content: attr(data-col);
    flex: 0 0 40%;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
  }
  table.data.as-cards td.num { justify-content: space-between; }
  table.data.as-cards td > * { min-width: 0; }

  /* The identifying cell is the card's title: full width, bigger, no label. */
  table.data.as-cards tbody td.col-title {
    display: block;
    order: -1;                       /* always the first thing in the card */
    padding: 9px 12px 8px;
    margin-bottom: 2px;
    border-bottom: 1px solid var(--border);
    font-size: 16px;
    font-weight: 650;
  }
  /* Actions stay pinned to the bottom of the card. */
  table.data.as-cards td.actions { order: 1; }
  table.data.as-cards tbody td.col-title::before { display: none; }
  table.data.as-cards tbody td.col-title .sub { font-weight: 400; }

  /* Action buttons sit full width at the foot of the card.
     app.js tags these cells, so no :has() support is required. */
  table.data.as-cards td.actions {
    display: flex; flex-wrap: wrap; gap: 8px;
    padding: 8px 12px 10px;
    margin-top: 2px;
    border-top: 1px solid var(--border);
  }
  table.data.as-cards td.actions::before { display: none; }
  table.data.as-cards td.actions .btn { flex: 1 1 auto; }
  table.data.as-cards td.actions form { flex: 1 1 auto; display: flex; }
  table.data.as-cards td.actions form .btn { width: 100%; }

  /* Cells the table can do without on a phone. */
  table.data.as-cards .mob-hide { display: none !important; }

  /* An empty cell carries nothing; do not spend a card row on it. */
  table.data.as-cards tbody td:empty { display: none; }

  /* Full-width cells: empty states, spanned labels. */
  table.data.as-cards td[colspan] { display: block; }
  table.data.as-cards td[colspan]::before { display: none; }
  table.data.as-cards td.empty { padding: 22px 14px !important; text-align: center !important; }

  /* Totals row reads as a summary card. */
  table.data.as-cards tfoot tr {
    display: block;
    border: 1px solid var(--accent-soft);
    background: var(--accent-soft);
    border-radius: 10px;
    margin-top: 10px;
    padding: 4px 0;
  }
  table.data.as-cards tfoot td {
    background: none; border: 0;
    padding: 6px 12px;
    font-weight: 600;
  }
  table.data.as-cards tfoot td:empty { display: none; }

  /* The table no longer needs its own sideways scroller. */
  .table-wrap.cards-wrap { overflow: visible; padding: 12px; }

  /* Progress bars are noise once stacked. */
  table.data.as-cards td .bar { min-width: 120px; flex: 1 1 auto; }

  /* --- bottom tab bar --- */
  .tabbar {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    z-index: 30;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding-bottom: var(--safe-b);
    box-shadow: 0 -1px 10px rgba(20, 30, 25, .06);
  }
  .tab {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px;
    min-height: var(--tabbar-h);
    padding: 7px 2px;
    color: var(--muted);
    font-size: 11px; font-weight: 600;
    background: none; border: 0; cursor: pointer;
    text-decoration: none;
  }
  .tab:hover { text-decoration: none; }
  .tab .tab-ico { font-size: 19px; line-height: 1; }
  .tab.active { color: var(--accent); }
  .tab.active .tab-ico { transform: translateY(-1px); }

  /* --- floating action button --- */
  .fab {
    display: inline-flex; align-items: center; gap: 8px;
    position: fixed;
    right: calc(14px + var(--safe-r));
    bottom: calc(var(--tabbar-h) + var(--safe-b) + 14px);
    z-index: 25;
    min-height: 52px;
    padding: 0 20px;
    border-radius: 26px;
    background: var(--accent); color: var(--accent-ink);
    font-weight: 700; font-size: 15px;
    box-shadow: 0 4px 16px rgba(20, 60, 40, .3);
    text-decoration: none;
  }
  .fab:hover { text-decoration: none; background: #275c42; }
  .fab-ico { font-size: 20px; line-height: 1; }

  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv dt { margin-top: 10px; font-size: 13px; }
  .kv dt:first-child { margin-top: 0; }
  .kv dd { font-size: 16px; }

  .auth-wrap { padding: 18px; align-items: start; padding-top: 8vh; }
  .auth-card { padding: 22px 18px; }

  /* A client group heading is a section label above that client's cards, not
     a card of its own: no border, no rounding, and Collect all sits on the
     same line as the name instead of in a card action row. */
  table.data.as-cards tbody.group tr.group-head {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    border: 0;
    border-radius: 0;
    background: transparent;
    margin: 4px 0 8px;
    padding: 0;
  }
  table.data.as-cards tbody.group + tbody.group tr.group-head {
    margin-top: 18px;
    border-top: 1px solid var(--border);
    padding-top: 14px;
  }
  table.data.as-cards tr.group-head td {
    display: block;
    padding: 0;
    flex: 1 1 auto;
    min-width: 0;
    background: transparent;   /* the desktop band would look like a card here */
  }
  table.data.as-cards tr.group-head td::before { content: none; }
  table.data.as-cards tr.group-head td.actions {
    flex: 0 0 auto;
    margin: 0;
    padding: 0;
    border: 0;
  }
  table.data.as-cards tbody.group tr.group-head .group-name { font-size: 17px; }
}

/* Very narrow phones only. Most phones are 360-430 CSS px and keep two stat
   cards per row; one-up below this width, where two stop fitting. */
@media screen and (max-width: 339px) {
  .stats { grid-template-columns: 1fr; }
  .tab { font-size: 10px; }
  table.data.as-cards td::before { flex-basis: 46%; }
}

/* Larger phones in landscape / small tablets fit three or four stats. */
@media screen and (min-width: 620px) and (max-width: 860px) {
  .stats { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
  table.data.as-cards tbody tr { padding: 4px 0 6px; }
}

@media print {
  .sidebar, .topbar, .tabbar, .fab, .scrim, .no-print, .btn, .filter-toggle { display: none !important; }
  .print-head { display: block; margin-bottom: 14px; }
  body { background: #fff; }
  .card { box-shadow: none; border-color: #ccc; break-inside: avoid; }
  .content { padding: 0; max-width: none; }
  table.data.as-cards { display: table; }
  table.data.as-cards thead { display: table-header-group; }
  table.data.as-cards tbody { display: table-row-group; }
  table.data.as-cards tfoot { display: table-footer-group; }
  table.data.as-cards tr { display: table-row; }
  table.data.as-cards td { display: table-cell; }
  table.data.as-cards td::before { display: none; }
  table.data.as-cards .mob-hide { display: table-cell !important; }
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #141816;
    --surface:   #1c211e;
    --surface-2: #232925;
    --border:    #323a35;
    --text:      #e6ebe7;
    --muted:     #9aa8a0;
    --accent:    #6fbf92;
    --accent-ink:#10231a;
    --accent-soft:#25382d;
    --warn:      #e0b45f;
    --warn-soft: #382e1c;
    --danger:    #e98080;
    --danger-soft:#3a2323;
    --ok:        #6fbf92;
    --ok-soft:   #25382d;
    --info:      #7db3dd;
    --info-soft: #1f2e3a;
    --shadow:    0 1px 2px rgba(0,0,0,.3);
  }
  :root:not([data-theme="light"]) .btn-primary:hover { background: #82cba2; }
  :root:not([data-theme="light"]) .btn-danger:hover  { background: #472b2b; }
  :root:not([data-theme="light"]) .fab:hover { background: #82cba2; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* A floating action button replaces the duplicated top-bar primary on phones. */
@media screen and (max-width: 860px) {
  .topbar-actions.has-fab .btn-primary { display: none; }
  .topbar-actions.has-fab:not(:has(.btn:not(.btn-primary))) { display: none; }
}

/* The floating action slides out of the way while you read down a long list,
   and comes straight back when you scroll up. */
@media screen and (max-width: 860px) {
  .fab { transition: transform .18s ease, opacity .18s ease; }
  .fab.fab-away {
    transform: translateY(calc(var(--tabbar-h) + var(--safe-b) + 90px));
    opacity: 0;
    pointer-events: none;
  }
}

/* ============================================================
   BRAND
   ============================================================ */

img.brand-mark {
  background: #fff;
  padding: 2px;
  object-fit: contain;
}

.auth-logo-panel {
  background: #fff;
  border-radius: 12px;
  padding: 14px 18px;
  margin: 0 auto 18px;
  max-width: 300px;
}
.auth-logo { display: block; width: 100%; height: auto; }

/* ============================================================
   LETTERHEAD  (screen on the statement, print everywhere else)
   ============================================================ */

.letterhead {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding-bottom: 14px;
  margin-bottom: 18px;
  border-bottom: 2px solid var(--accent);
}
.letterhead-logo { width: 170px; height: auto; flex: 0 0 170px; }
.letterhead-text { display: flex; flex-direction: column; min-width: 0; }
.letterhead-text strong { font-size: 17px; line-height: 1.25; }
.letterhead-text span { font-size: 13px; color: var(--muted); }
.letterhead-doc { margin-left: auto; text-align: right; display: flex; flex-direction: column; }
.letterhead-doc strong { font-size: 14px; }
.letterhead-doc span { font-size: 13px; color: var(--muted); }

/* ============================================================
   THE STATEMENT SHEET
   ============================================================ */

.sheet {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 32px 24px;
  max-width: 860px;
  margin: 0 auto 18px;
}

.sheet-title { text-align: center; margin: 0 0 20px; }
.sheet-title h2 {
  margin: 0;
  font-size: 19px;
  letter-spacing: .06em;
  text-transform: uppercase;
}
.sheet-title p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }

table.fin { width: 100%; border-collapse: collapse; font-size: 15px; }
table.fin thead th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 650;
  text-align: left;
  padding: 0 0 6px;
  border-bottom: 1px solid var(--border);
}
table.fin td { padding: 5px 0; vertical-align: baseline; }
table.fin .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  padding-left: 14px;
}

.fin-section td {
  padding-top: 16px;
  font-weight: 700;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--accent);
}
.fin-item { padding-left: 18px !important; }
.fin-note { display: block; font-size: 12px; color: var(--muted); }
.fin-none { color: var(--muted); font-style: italic; }

.fin-total td {
  border-top: 1px solid var(--border);
  font-weight: 650;
}
.fin-result td {
  border-top: 2px solid var(--text);
  border-bottom: 1px solid var(--border);
  font-weight: 750;
  font-size: 16px;
  padding: 9px 0;
}
.fin-bottom td { border-bottom: 3px double var(--text); }
.fin-neg { color: var(--danger); }

.sheet-notes { margin-top: 26px; }
.sheet-notes h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin: 0 0 8px;
}
.sheet-notes ol { margin: 0; padding-left: 20px; font-size: 13px; line-height: 1.55; color: var(--text); }
.sheet-notes li { margin-bottom: 6px; }

.sheet-sign {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 28px;
  margin-top: 34px;
}
.sheet-sign > div { display: flex; flex-direction: column; font-size: 13px; }
.sign-line { display: block; border-top: 1px solid var(--text); margin-bottom: 6px; height: 34px; }
.sheet-sign strong { font-size: 13px; }
.sheet-sign span { color: var(--muted); }

.sheet-foot {
  margin: 26px 0 0;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
  text-align: center;
}

@media screen and (max-width: 860px) {
  .sheet { padding: 18px 16px 16px; border-radius: var(--radius); }
  .sheet-title h2 { font-size: 16px; }
  table.fin { font-size: 14px; }
  table.fin thead th:last-child,
  table.fin td:last-child { display: none; }     /* drop % column on a phone */
  .letterhead { gap: 12px; flex-wrap: wrap; }
  .letterhead-logo { width: 130px; flex-basis: 130px; }
  .letterhead-doc { margin-left: 0; text-align: left; flex-basis: 100%; }
  .sheet-sign { grid-template-columns: 1fr; gap: 20px; }
}

/* ============================================================
   PRINT: a clean sheet of A4
   ============================================================ */

@page { size: A4 portrait; margin: 14mm 14mm 12mm; }

@media print {
  .sheet {
    box-shadow: none;
    border: 0;
    padding: 0;
    max-width: none;
    margin: 0;
  }
  .letterhead { border-bottom: 2px solid #1d2420; }
  .letterhead-logo { width: 38mm; flex-basis: 38mm; }
  .letterhead-text strong { font-size: 13pt; }
  .letterhead-text span, .letterhead-doc span { color: #444; }
  .sheet-title h2 { font-size: 13pt; }
  table.fin { font-size: 10pt; }
  table.fin thead th:last-child,
  table.fin td:last-child { display: table-cell; }   /* keep % on paper */
  .fin-section td { color: #1d2420; }
  .sheet-notes { break-inside: avoid; }
  .sheet-notes ol { font-size: 8.5pt; }
  .sheet-sign { break-inside: avoid; margin-top: 16mm; }
  .sheet-foot { font-size: 7.5pt; color: #555; }
  table.fin tr { break-inside: avoid; }
}

/* ============================================================
   DESKTOP SIDEBAR: fit the whole menu, no scrolling
   At 44px rows the list needed 721px of window height, which a
   1366x768 laptop does not have. These rows are mouse targets, not
   thumb targets, so they can be tighter.
   ============================================================ */

@media screen and (min-width: 861px) {
  .brand { padding: 12px 14px; }
  .brand-mark { width: 32px; height: 32px; flex: 0 0 32px; }
  .brand-text { font-size: 14px; }

  .sidebar nav { padding: 8px; gap: 2px; }
  .nav-item { min-height: 38px; padding: 7px 12px; font-size: 14.5px; }

  /* Name and sign-out on one line instead of stacked. */
  .sidebar-foot {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px 10px;
  }
  .who { flex: 1 1 auto; padding: 0 2px; font-size: 13px; min-width: 0; }
  .who span { font-size: 12px; }
  .sidebar-foot .nav-item.subtle {
    flex: 0 0 auto;
    min-height: 34px; padding: 6px 10px;
    border: 1px solid var(--border);
  }
}

/* Short windows: tighten once more rather than start scrolling. */
@media screen and (min-width: 861px) and (max-height: 620px) {
  .sidebar nav { padding: 3px 8px; gap: 1px; }
  .nav-item { min-height: 32px; padding: 4px 12px; font-size: 14px; }
  .brand { padding: 9px 14px; }
  .brand-mark { width: 28px; height: 28px; flex: 0 0 28px; }
  .who span { display: none; }
  .sidebar-foot { padding: 6px 10px 8px; }
}

/*
 * Very short windows. The menu grew to twelve entries with the field map, and
 * at this height the previous tier no longer fits them -- so the icons carry
 * the rows and the labels tuck in beside them. Measured: twelve entries need
 * about 330px here, which leaves the brand and the sign-out line room down to
 * a 440px window.
 */
@media screen and (min-width: 861px) and (max-height: 520px) {
  .sidebar nav { padding: 2px 6px; gap: 0; }
  .nav-item {
    min-height: 26px;
    padding: 2px 10px;
    font-size: 13px;
    gap: 8px;
  }
  .nav-ico { font-size: 13px; }
  .brand { padding: 6px 12px; }
  .brand-mark { width: 22px; height: 22px; flex: 0 0 22px; }
  .brand-text { font-size: 13px; }
  .sidebar-foot { padding: 4px 8px 6px; }
  .sidebar-foot .nav-item.subtle { min-height: 28px; padding: 4px 9px; }
  .who { font-size: 12px; }
}

/* ---------- a box over the page ---------- */

/*
 * Used for adding a client from the job form. On a phone it sits at the
 * bottom, where a thumb is, and takes the full width; on a desktop it is a
 * panel in the middle.
 */
.sheet {
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100vh - 80px);
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 18px 50px rgba(0, 0, 0, .35);
  overflow: auto;
}
.sheet::backdrop {
  background: rgba(12, 18, 15, .55);
}

.sheet h2 {
  margin: 0 34px 4px 0;
  font-size: 18px;
}
.sheet > .sub {
  margin: 0 0 14px;
}
.sheet .field {
  margin-bottom: 12px;
}
.sheet .btn-row {
  margin-top: 16px;
}

/* The close cross, in its own little form so Escape and the button agree. */
.sheet-x {
  float: right;
  margin: -6px -6px 0 0;
}
.sheet-close {
  width: 34px;
  height: 34px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.sheet-close:hover {
  background: var(--border);
  color: var(--text);
}

.sheet .alert {
  margin: 0 0 12px;
}

@media screen and (max-width: 560px) {
  .sheet {
    width: 100%;
    max-width: none;
    margin: auto auto 0;          /* sit on the bottom edge */
    border-radius: var(--radius) var(--radius) 0 0;
    padding-bottom: calc(18px + var(--safe-b, 0px));
  }
}
/* A radio or checkbox with its label beside it, for a choice of arrangement. */
.choice {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 2px;
  font-weight: 400;
  cursor: pointer;
}
.choice input { width: auto; margin: 0; flex: none; }

/*
 * A job's notes, on their own row under the job.
 *
 * The row above it keeps the border, so the note reads as belonging to that
 * job rather than as a row of its own. Indented and set in the muted colour
 * for the same reason.
 */
table.data tr.note-row > td {
  border-top: none;
  padding-top: 0;
  padding-left: 28px;
}
table.data tr.note-row:hover { background: transparent; }
/* As a card on a phone the note stands alone, so it needs its own edge. */
@media (max-width: 860px) {
  table.data.as-cards tr.note-row > td { padding-left: 14px; }
}
