/* Ultimate Clean Laundry — app theme */
@font-face {
  font-family: 'Inter';
  src: url('/fonts/InterVariable.woff2') format('woff2');
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

:root {
  --teal-900: #134e4a;
  --teal-700: #0e7490;
  --teal-600: #0891b2;
  --teal-500: #06b6d4;
  --sky-400: #38bdf8;
  --sky-100: #e0f2fe;
  --ink: #0f2a33;
  --ink-soft: #4b6570;
  --line: #d7e7ee;
  --card: #ffffff;
  --bg: #eef6f9;
  --good: #0f9d58;
  --warn: #b45309;
  --bad: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(14, 116, 144, .06), 0 8px 24px -12px rgba(14, 116, 144, .25);
  --font: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  font-size: 14.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  letter-spacing: -0.006em;
}
button, input, select, textarea { font: inherit; color: inherit; }
button, a, input, select, textarea { touch-action: manipulation; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, [tabindex]:focus-visible {
  outline: 3px solid rgba(6, 182, 212, .42);
  outline-offset: 2px;
}
h1, h2, h3 { margin: 0; font-weight: 700; }
a { color: var(--teal-700); }

.boot-splash { display: grid; place-items: center; gap: 10px; min-height: 100vh; min-height: 100dvh; color: var(--ink-soft); }

/* ---------- Login / setup ---------- */
.auth-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 85% -10%, rgba(56, 189, 248, .35), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(8, 145, 178, .28), transparent 60%),
    linear-gradient(160deg, #f0f9ff 0%, #dbeffb 50%, #cfeef7 100%);
  position: relative;
  overflow-x: hidden;
  overflow-y: auto;
}
.bubble {
  position: absolute;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, rgba(255,255,255,.9), rgba(165,243,252,.25));
  border: 1px solid rgba(255,255,255,.7);
  animation: floatUp linear infinite;
  bottom: -120px;
  pointer-events: none;
}
@keyframes floatUp {
  to { transform: translateY(-115vh) translateX(30px); opacity: 0; }
}
.auth-card {
  width: min(420px, 100%);
  background: var(--card);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 34px 34px 28px;
  position: relative;
  z-index: 2;
}
.auth-card .brand { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 20px; text-align: center; }
.auth-card .brand img { width: 118px; height: 118px; border-radius: 50%; box-shadow: 0 4px 18px -6px rgba(14, 116, 144, .35); }
.auth-card .brand .drum { animation: badgeFloat 5s ease-in-out infinite; }
@keyframes badgeFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}
.auth-card h1 { font-size: 21px; color: var(--teal-900); }
.auth-card .sub { color: var(--ink-soft); font-size: 13px; margin: 2px 0 0; }
.auth-error {
  background: #fef2f2; color: var(--bad); border: 1px solid #fecaca;
  padding: 9px 12px; border-radius: 9px; font-size: 13px; margin-bottom: 12px;
}
@keyframes spinDrum {
  0%, 15% { transform: rotate(0); }
  55% { transform: rotate(360deg); }
  100% { transform: rotate(360deg); }
}

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: var(--ink-soft); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: #fbfeff;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.field textarea { resize: vertical; min-height: 64px; }
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--teal-500);
  box-shadow: 0 0 0 3px rgba(6, 182, 212, .18);
}
.hint { font-size: 12px; color: var(--ink-soft); margin-top: 4px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 1.5px solid transparent;
  background: linear-gradient(135deg, var(--teal-600), var(--teal-700));
  color: #fff; font-weight: 600;
  padding: 10px 18px; border-radius: 10px;
  cursor: pointer;
  transition: filter .15s, transform .05s;
}
.btn:hover { filter: brightness(1.07); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.block { width: 100%; }
.btn.ghost { background: #fff; color: var(--teal-700); border-color: var(--line); }
.btn.ghost:hover { border-color: var(--teal-500); background: #f4fbfd; }
.btn.danger { background: linear-gradient(135deg, #ef4444, #b91c1c); }
.btn.small { padding: 5px 11px; font-size: 13px; border-radius: 8px; }
.btn.tiny { padding: 2px 9px; font-size: 12px; border-radius: 7px; }

/* ---------- App shell ---------- */
.shell { display: grid; grid-template-columns: 226px 1fr; min-height: 100vh; min-height: 100dvh; }
.sidebar {
  background: linear-gradient(180deg, #0c4a6e 0%, #0e7490 100%);
  color: #e6f7fd;
  padding: 18px 12px;
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; height: 100dvh;
}
.sidebar .brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; }
.sidebar .brand img { width: 42px; height: 42px; background: #fff; border-radius: 50%; padding: 0; }
.sidebar .brand b { font-size: 14.5px; line-height: 1.2; display: block; }
.sidebar .brand span { font-size: 11px; opacity: .75; }
.nav { display: flex; flex-direction: column; gap: 3px; margin-top: 6px; }
.nav a {
  display: flex; align-items: center; gap: 11px;
  color: #d3eef8; text-decoration: none;
  padding: 10px 12px; border-radius: 10px;
  font-weight: 500; font-size: 14px;
  border: 1px solid transparent;
}
.nav a svg { width: 19px; height: 19px; flex: none; opacity: .9; }
.nav a:hover { background: rgba(255,255,255,.09); }
.nav a.active { background: rgba(255,255,255,.16); border-color: rgba(255,255,255,.22); color: #fff; font-weight: 600; }
.sidebar .foot { margin-top: auto; padding: 10px 8px 2px; font-size: 12px; opacity: .75; }

.main { padding: 22px 26px 60px; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.topbar h2 { font-size: 20px; color: var(--teal-900); }
.topbar .crumb { color: var(--ink-soft); font-size: 12.5px; }
.topbar .right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.userchip {
  display: flex; align-items: center; gap: 8px;
  background: #fff; border: 1.5px solid var(--line); border-radius: 999px;
  padding: 5px 6px 5px 12px; font-size: 13px;
}
.userchip .avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: linear-gradient(135deg, var(--sky-400), var(--teal-600));
  color: #fff; display: grid; place-items: center; font-size: 12px; font-weight: 700;
}
.userchip .role { color: var(--ink-soft); font-size: 11px; }

/* ---------- Cards / KPI ---------- */
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px 20px; margin-bottom: 18px; min-width: 0;
}
.card > h3 { font-size: 15px; color: var(--teal-900); margin-bottom: 12px; display: flex; align-items: center; gap: 8px; justify-content: space-between; flex-wrap: wrap; }
.card > h3 .sub { font-weight: 500; font-size: 12px; color: var(--ink-soft); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; margin-bottom: 18px; }
.kpi {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px; border-top: 3px solid var(--teal-500);
}
.kpi .label { font-size: 12px; font-weight: 600; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .4px; }
.kpi .value { font-size: 21px; font-weight: 750; margin-top: 3px; color: var(--teal-900); font-variant-numeric: tabular-nums; }
.kpi .note { font-size: 11.5px; color: var(--ink-soft); margin-top: 2px; }
.kpi.good { border-top-color: var(--good); }
.kpi.warn { border-top-color: #f59e0b; }
.kpi.bad { border-top-color: var(--bad); }
.kpi.bad .value { color: var(--bad); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
.grid2 > * { min-width: 0; }
@media (max-width: 1080px) { .grid2 { grid-template-columns: 1fr; } }

/* ---------- Tables ---------- */
.table-wrap { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-inline: contain; }
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data th {
  text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .4px;
  color: var(--ink-soft); padding: 8px 10px; border-bottom: 2px solid var(--line);
  white-space: nowrap;
}
table.data td { padding: 9px 10px; border-bottom: 1px solid #ecf4f8; vertical-align: middle; }
table.data tr:hover td { background: #f6fbfd; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data .empty { text-align: center; color: var(--ink-soft); padding: 22px; }
.rowactions { display: flex; gap: 5px; justify-content: flex-end; }

.badge {
  display: inline-block; padding: 2.5px 9px; border-radius: 999px;
  font-size: 11.5px; font-weight: 650; white-space: nowrap;
}
.badge.paid, .badge.closed, .badge.delivered, .badge.yes { background: #dcfce7; color: #15803d; }
.badge.partially { background: #fef3c7; color: #92400e; }
.badge.unpaid, .badge.no { background: #fee2e2; color: #b91c1c; }
.badge.overdue { background: #fee2e2; color: #b91c1c; }
.badge.open, .badge.received { background: var(--sky-100); color: var(--teal-700); }
.badge.duesoon { background: #fef3c7; color: #92400e; }
.badge.inwash { background: #dbeafe; color: #1d4ed8; }
.badge.ready { background: #ede9fe; color: #6d28d9; }
.badge.cancelled { background: #f3f4f6; color: #6b7280; }

/* Receivables aging */
.aging-bar { display: flex; height: 18px; border-radius: 99px; overflow: hidden; background: #e8f2f7; margin: 6px 0 14px; }
.aging-bar .seg { height: 100%; min-width: 0; transition: width .3s; }
.aging-bar .seg.current { background: var(--teal-500); }
.aging-bar .seg.d1to30 { background: #f59e0b; }
.aging-bar .seg.d31to60 { background: #f97316; }
.aging-bar .seg.d60plus { background: var(--bad); }
.aging-legend { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.aging-legend .item { display: flex; align-items: center; gap: 8px; font-size: 12.5px; }
.aging-legend .dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.aging-legend b { font-variant-numeric: tabular-nums; }
.aging-legend .sub { color: var(--ink-soft); font-size: 11.5px; }

/* Click-to-chase contact links */
.contact { display: flex; align-items: center; gap: 7px; white-space: nowrap; }
.contact a[href^="tel:"] { color: var(--ink); text-decoration: none; }
.contact a[href^="tel:"]:hover { color: var(--teal-700); text-decoration: underline; }
.contact .wa {
  display: inline-flex; align-items: center; gap: 4px;
  background: #dcfce7; color: #15803d;
  border-radius: 999px; padding: 2px 9px;
  font-size: 11.5px; font-weight: 650; text-decoration: none;
}
.contact .wa:hover { filter: brightness(.96); }

/* KPI progress (collection rate) */
.kpi .progress { height: 7px; border-radius: 99px; background: #e8f2f7; overflow: hidden; margin-top: 7px; }
.kpi .progress .fill { height: 100%; border-radius: 99px; background: linear-gradient(90deg, var(--sky-400), var(--teal-600)); }
.kpi.bad .progress .fill { background: linear-gradient(90deg, #f87171, var(--bad)); }
.kpi.warn .progress .fill { background: linear-gradient(90deg, #fbbf24, #d97706); }

.mixbar { display: flex; align-items: center; gap: 8px; min-width: 130px; }
.mixbar .track { flex: 1; height: 8px; border-radius: 99px; background: #e8f2f7; overflow: hidden; }
.mixbar .fill { height: 100%; border-radius: 99px; background: linear-gradient(90deg, var(--sky-400), var(--teal-600)); }
.mixbar .pct { font-size: 11.5px; color: var(--ink-soft); width: 42px; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Toolbars ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
.toolbar input[type="search"], .toolbar select {
  padding: 8px 12px; border: 1.5px solid var(--line); border-radius: 10px; background: #fff; outline: none;
}
.toolbar input[type="search"] { width: 230px; }
.toolbar input[type="search"]:focus, .toolbar select:focus { border-color: var(--teal-500); }
.toolbar label.inline { font-size: 12.5px; font-weight: 600; color: var(--ink-soft); }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(7, 44, 56, .45);
  display: grid; place-items: center; padding: 20px; z-index: 50;
  backdrop-filter: blur(2px);
}
.modal {
  background: #fff; border-radius: 16px; box-shadow: 0 24px 70px -20px rgba(0,0,0,.45);
  width: min(640px, 100%); max-height: 92vh; max-height: 92dvh; overflow: auto; padding: 22px 24px;
  overscroll-behavior: contain;
}
.modal.wide { width: min(860px, 100%); }
.modal h3 { font-size: 17px; color: var(--teal-900); margin-bottom: 16px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.formgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.formgrid .full { grid-column: 1 / -1; }
@media (max-width: 620px) { .formgrid { grid-template-columns: 1fr; } }

/* order line editor */
.lines-editor { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.lines-editor table { width: 100%; border-collapse: collapse; font-size: 13px; }
.lines-editor th { text-align: left; font-size: 11px; text-transform: uppercase; color: var(--ink-soft); padding: 4px 6px; }
.lines-editor td { padding: 3px 4px; }
.lines-editor input, .lines-editor select {
  width: 100%; padding: 7px 8px; border: 1.5px solid var(--line); border-radius: 8px; background: #fbfeff;
}
.lines-editor .linetotal { font-variant-numeric: tabular-nums; text-align: right; font-weight: 600; white-space: nowrap; padding: 0 8px; }
.lines-editor .rm { color: var(--bad); background: none; border: none; cursor: pointer; font-size: 16px; padding: 4px; }
.order-total-strip {
  display: flex; justify-content: flex-end; gap: 26px; font-size: 14px;
  background: #f2fafc; border-radius: 10px; padding: 10px 14px; margin-top: 10px;
}
.order-total-strip b { font-variant-numeric: tabular-nums; }

/* ---------- Toast ---------- */
#toast-root { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: var(--teal-900); color: #fff; padding: 10px 18px; border-radius: 10px;
  box-shadow: 0 10px 30px -8px rgba(0,0,0,.4); font-size: 13.5px;
  animation: toastIn .2s ease-out;
}
.toast.error { background: #b91c1c; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Invoice ---------- */
/* Builder bar: client/mode/order side by side with the action buttons,
   so the Invoice Log + preview sit directly below without scrolling. */
.inv-controls .formrow { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; }
.inv-controls .formrow .field { flex: 1 1 190px; margin-bottom: 0; min-width: 170px; }
.inv-controls .inv-actions { display: flex; gap: 9px; flex-wrap: wrap; padding-bottom: 1px; }
.inv-controls .mode-hint { margin-top: 8px; }
.btn.whatsapp { background: linear-gradient(135deg, #25d366, #128c7e); }
.btn.whatsapp:disabled { filter: grayscale(.4); }

.invoice-layout { display: grid; grid-template-columns: minmax(330px, 400px) 1fr; gap: 18px; align-items: start; margin-top: 18px; }
@media (max-width: 1080px) { .invoice-layout { grid-template-columns: 1fr; } }
.invoice-paper {
  background: #fff; border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 34px 38px; font-size: 13.5px;
  position: relative; overflow: hidden;
}
/* Watermark: the pale company badge centred behind the content (as in the workbook). */
.invoice-paper .inv-watermark {
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 62%; max-width: 460px;
  opacity: .3; pointer-events: none; user-select: none;
}
.invoice-paper > *:not(.inv-watermark) { position: relative; z-index: 1; }
.invoice-paper .inv-head { display: flex; justify-content: space-between; gap: 18px; border-bottom: 3px solid var(--teal-700); padding-bottom: 16px; }
.invoice-paper .inv-brand { display: flex; align-items: center; gap: 14px; }
.invoice-paper .inv-badge { width: 92px; height: 92px; border-radius: 50%; flex: none; }
.invoice-paper .inv-head h2 { color: var(--teal-900); font-size: 19px; }
.invoice-paper .tax-label { font-size: 12px; color: var(--ink-soft); letter-spacing: .12em; margin-top: 2px; }
.invoice-paper .inv-head .biz { font-size: 12.5px; color: var(--ink-soft); white-space: pre-line; text-align: right; }
.invoice-paper .inv-meta { display: flex; justify-content: space-between; gap: 18px; margin: 16px 0; flex-wrap: wrap; }
.invoice-paper .inv-meta .blk { font-size: 12.5px; color: var(--ink-soft); }
.invoice-paper .inv-meta .blk b { display: block; color: var(--ink); font-size: 13.5px; }
.invoice-paper table.inv { width: 100%; border-collapse: collapse; margin-top: 6px; }
.invoice-table-scroll { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-inline: contain; }
.invoice-paper table.inv th { background: var(--teal-700); color: #fff; text-align: left; padding: 7px 10px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .3px; }
.invoice-paper table.inv th.num, .invoice-paper table.inv td.num { text-align: right; font-variant-numeric: tabular-nums; }
.invoice-paper table.inv td { padding: 7px 10px; border-bottom: 1px solid #e8f1f5; }
.invoice-paper .totals { margin-left: auto; margin-top: 14px; width: 280px; font-size: 13.5px; }
.invoice-paper .totals .row { display: flex; justify-content: space-between; padding: 4px 0; }
.invoice-paper .totals .row.grand { border-top: 2px solid var(--teal-700); margin-top: 6px; padding-top: 9px; font-size: 16px; font-weight: 750; color: var(--teal-900); }
.invoice-paper .banknote { margin-top: 22px; border-top: 1px dashed var(--line); padding-top: 12px; font-size: 11.8px; color: var(--ink-soft); }
.invoice-paper .banknote b { color: var(--ink); }

/* ---------- Settings ---------- */
.taglist { display: flex; flex-wrap: wrap; gap: 7px; }
.taglist .tag {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--sky-100); color: var(--teal-700);
  border-radius: 999px; padding: 4px 6px 4px 12px; font-size: 12.5px; font-weight: 600;
}
.taglist .tag button { border: none; background: rgba(14,116,144,.12); color: var(--teal-700); border-radius: 50%; width: 18px; height: 18px; line-height: 1; cursor: pointer; font-size: 11px; }
.taglist .add { display: inline-flex; gap: 6px; }
.taglist .add input { width: 150px; padding: 4px 10px; border: 1.5px dashed var(--line); border-radius: 999px; font-size: 12.5px; outline: none; }

.expand-row td { background: #f6fbfd !important; padding: 4px 10px 12px !important; }
.expand-row table.data td { background: transparent !important; }

/* ---------- Print (invoice only) ---------- */
#print-zone { display: none; }
@page { size: A4; margin: 14mm; }
@media print {
  html, body { height: auto; background: #fff; }
  /* Hide the app shell entirely — leaving it display:grid with min-height:100vh
     used to emit a blank first page before the invoice. */
  body.printing-invoice .shell { display: none !important; }
  /* static (not absolute) so long invoices flow onto extra pages instead of clipping */
  body.printing-invoice #print-zone { display: block !important; position: static; background: #fff; }
  #print-zone .invoice-paper { box-shadow: none; border-radius: 0; padding: 0; font-size: 10.5pt; overflow: visible; }
  /* keep the teal header + watermark inks on paper (browsers strip backgrounds by default) */
  .invoice-paper table.inv th,
  .invoice-paper .inv-watermark,
  .invoice-paper .totals .row.grand {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  /* fallback so headers stay legible even if backgrounds are stripped */
  .invoice-paper table.inv th { border-bottom: 2px solid var(--teal-700); }
  .invoice-paper table.inv tr, .invoice-paper .totals, .invoice-paper .banknote { break-inside: avoid; }
  .invoice-table-scroll { overflow: visible !important; }
  .invoice-table-scroll table.inv { min-width: 0 !important; }
  #toast-root, #modal-root { display: none !important; }
}

/* responsive shell */
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky; top: 0; z-index: 40; height: auto;
    flex-direction: row; align-items: center; gap: 8px; padding: 8px 10px;
    overflow: hidden;
  }
  .sidebar .brand { padding: 0 8px 0 4px; }
  .sidebar .brand div { display: none; }
  .nav {
    flex: 1; min-width: 0; flex-direction: row; margin: 0; overflow-x: auto;
    scroll-snap-type: x proximity; scrollbar-width: none; overscroll-behavior-inline: contain;
  }
  .nav::-webkit-scrollbar { display: none; }
  .nav a { min-height: 44px; padding: 9px 11px; white-space: nowrap; scroll-snap-align: center; }
  .sidebar .foot { display: none; }
  .main { padding: 16px 14px 50px; }
}

/* ---------- Phone layout ---------- */
@media screen and (max-width: 640px) {
  body { overflow-x: hidden; }
  body.modal-open { overflow: hidden; }

  .shell { display: block; }
  .sidebar {
    position: fixed; inset: auto 0 0; z-index: 45;
    display: block; width: 100%;
    padding: 5px max(4px, env(safe-area-inset-right)) calc(5px + env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left));
    border-top: 1px solid rgba(255,255,255,.22);
    box-shadow: 0 -8px 24px -16px rgba(7,44,56,.65);
  }
  .sidebar .brand { display: none; }
  .nav {
    width: 100%; display: flex; gap: 0; overflow-x: auto;
    scroll-padding-inline: 8px; scroll-snap-type: x mandatory;
  }
  .nav a {
    flex: 1 1 44px; min-width: 44px; min-height: 52px;
    flex-direction: column; justify-content: center; gap: 2px;
    padding: 5px 2px 4px; border-radius: 9px;
    font-size: 9px; line-height: 1.1; scroll-snap-align: center;
  }
  .nav a svg { width: 20px; height: 20px; }
  .nav a span { display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }

  .main { padding: 12px 10px calc(76px + env(safe-area-inset-bottom)); }
  .topbar { align-items: stretch; gap: 9px; margin-bottom: 12px; }
  .topbar > div:first-child { min-width: 0; }
  .topbar h2 { font-size: 18px; }
  .topbar .right { width: 100%; min-width: 0; }
  .userchip { width: 100%; min-width: 0; border-radius: 12px; padding-left: 9px; }
  .userchip > div:nth-child(2) { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .userchip .role { overflow: hidden; text-overflow: ellipsis; }

  .card { padding: 14px 12px; margin-bottom: 12px; border-radius: 12px; }
  .card > h3 { gap: 7px; margin-bottom: 10px; }
  .grid2 { gap: 12px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; margin-bottom: 12px; }
  .kpi { min-width: 0; padding: 11px 10px; }
  .kpi .label { font-size: 10.5px; overflow-wrap: anywhere; }
  .kpi .value { font-size: clamp(16px, 5vw, 20px); overflow-wrap: anywhere; }

  input, select, textarea { font-size: 16px; }
  .field { margin-bottom: 12px; }
  .field input, .field select, .field textarea,
  .toolbar input[type="search"], .toolbar select,
  table.data input:not([type="checkbox"]), table.data select {
    min-height: 44px;
  }
  .btn { min-height: 44px; padding: 9px 14px; }
  .btn.small, .btn.tiny { min-height: 44px; padding: 8px 12px; font-size: 13px; }

  .toolbar { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; margin-bottom: 12px; }
  .toolbar > *, .toolbar input[type="search"], .toolbar select { width: 100%; min-width: 0; }
  .toolbar .spacer { display: none; }
  .toolbar .crumb { text-align: center; overflow-wrap: anywhere; }

  /* Data tables become labelled record cards; the desktop table remains unchanged. */
  table.data[data-mobile-cards],
  table.data[data-mobile-cards] > tbody { display: block; width: 100%; }
  table.data[data-mobile-cards] > thead {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
  }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) {
    display: block; width: 100%; margin-bottom: 10px;
    border: 1px solid var(--line); border-radius: 11px; overflow: hidden;
    background: #fff;
  }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row):last-child { margin-bottom: 0; }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td {
    display: grid; grid-template-columns: minmax(88px, 36%) minmax(0, 1fr);
    align-items: center; gap: 10px; width: 100%; min-width: 0;
    padding: 8px 10px; border-bottom: 1px solid #ecf4f8;
    text-align: right; white-space: normal; overflow-wrap: anywhere;
  }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td:last-child { border-bottom: 0; }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td::before {
    content: attr(data-label); color: var(--ink-soft); font-size: 11px; font-weight: 700;
    grid-column: 1; grid-row: 1;
    line-height: 1.2; text-align: left; text-transform: uppercase; letter-spacing: .32px;
  }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td > * {
    grid-column: 2; min-width: 0;
  }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td[data-label=""] {
    grid-template-columns: minmax(0, 1fr);
  }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td[data-label=""]::before { display: none; }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td[data-label=""] > * { grid-column: 1; }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td[colspan] {
    display: block; padding: 18px 10px; text-align: center;
  }
  table.data[data-mobile-cards] > tbody > tr:not(.expand-row) > td[colspan]::before { display: none; }
  table.data[data-mobile-cards] tr:hover td { background: inherit; }
  table.data[data-mobile-cards] td .crumb { overflow-wrap: anywhere; }
  .rowactions { justify-content: flex-end; flex-wrap: wrap; }
  .rowactions .btn { flex: 1 1 auto; }
  .expand { width: 100%; }
  .expand[aria-expanded="false"]::after { content: 'Show details'; }
  .expand[aria-expanded="true"]::after { content: 'Hide details'; }
  .contact { justify-content: flex-end; flex-wrap: wrap; white-space: normal; }
  .contact .wa { min-height: 44px; padding: 7px 12px; }
  .mixbar { width: 100%; min-width: 0; }

  .expand-row { display: block; margin: -4px 0 10px; }
  .expand-row > td { display: block; width: 100%; padding: 8px !important; }
  .expand-row table.data { margin-bottom: 8px; }

  table.data input:not([type="checkbox"]), table.data select { width: 100% !important; max-width: none; }
  table.data input[type="checkbox"] { width: 24px; height: 24px; justify-self: end; }

  .modal-backdrop { align-items: end; padding: 8px 0 0; }
  .modal, .modal.wide {
    width: 100%; max-height: calc(100vh - 8px); max-height: calc(100dvh - 8px);
    border-radius: 17px 17px 0 0; padding: 18px 14px calc(14px + env(safe-area-inset-bottom));
  }
  .modal h3 { margin-bottom: 13px; }
  .modal .actions {
    position: sticky; bottom: calc(-14px - env(safe-area-inset-bottom)); z-index: 3;
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
    margin: 14px -14px 0; padding: 12px 14px calc(14px + env(safe-area-inset-bottom));
    background: rgba(255,255,255,.97); border-top: 1px solid var(--line);
  }
  .modal .actions .btn { width: 100%; }

  .lines-editor { overflow: visible; }
  .lines-editor table, .lines-editor tbody { display: block; width: 100%; }
  .lines-editor thead {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
  }
  .lines-editor .line-row {
    display: block; margin-bottom: 10px; padding: 8px 10px;
    border: 1px solid var(--line); border-radius: 11px; background: #f8fcfe;
  }
  .lines-editor .line-row td {
    display: grid; grid-template-columns: minmax(92px, 36%) minmax(0, 1fr);
    align-items: center; gap: 9px; width: 100% !important; padding: 5px 0; text-align: right;
  }
  .lines-editor .line-row td::before {
    content: attr(data-label); color: var(--ink-soft); font-size: 11px; font-weight: 700;
    text-align: left; text-transform: uppercase; letter-spacing: .3px;
  }
  .lines-editor input, .lines-editor select { min-height: 44px; font-size: 16px; }
  .lines-editor .rm { width: 44px; height: 44px; justify-self: end; }
  .order-total-strip { justify-content: space-between; gap: 10px; flex-wrap: wrap; }

  .inv-controls .formrow { display: block; }
  .inv-controls .formrow .field { min-width: 0; margin-bottom: 10px; }
  .inv-controls .inv-actions { display: grid; grid-template-columns: 1fr; gap: 8px; width: 100%; }
  .inv-controls .inv-actions .btn { width: 100%; }
  .invoice-layout { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
  .invoice-layout > * { width: 100%; min-width: 0; }
  #inv-preview { order: 1; }
  .invoice-layout > .card { order: 2; }
  .invoice-paper { padding: 16px 13px; border-radius: 12px; }
  .invoice-paper .inv-head { flex-direction: column; gap: 12px; padding-bottom: 12px; }
  .invoice-paper .inv-brand { align-items: center; gap: 10px; }
  .invoice-paper .inv-badge { width: 64px; height: 64px; }
  .invoice-paper .inv-head h2 { font-size: 17px; }
  .invoice-paper .inv-head .biz { text-align: left; white-space: pre-line; overflow-wrap: anywhere; }
  .invoice-paper .inv-meta { display: grid; grid-template-columns: 1fr; gap: 11px; margin: 13px 0; }
  .invoice-paper .inv-meta .blk { min-width: 0; overflow-wrap: anywhere; }
  .invoice-paper .inv-meta .blk[style] { text-align: left !important; }
  .invoice-table-scroll { border: 1px solid var(--line); border-radius: 9px; }
  .invoice-table-scroll table.inv { min-width: 620px; margin-top: 0; }
  .invoice-paper .totals { width: 100%; margin-top: 13px; }
  .invoice-paper .banknote { margin-top: 17px; overflow-wrap: anywhere; }

  .taglist .tag { min-height: 44px; }
  .taglist .tag button { width: 44px; height: 44px; font-size: 14px; }
  .taglist .add { width: 100%; }
  .taglist .add input { flex: 1; min-width: 0; width: auto; min-height: 44px; font-size: 16px; }

  #toast-root {
    bottom: calc(70px + env(safe-area-inset-bottom));
    width: min(calc(100% - 20px), 420px); align-items: stretch;
  }
  .toast { width: 100%; padding: 11px 14px; text-align: center; }

  .auth-wrap { place-items: center; padding: 14px; }
  .auth-card { padding: 24px 18px 20px; border-radius: 18px; }
  .auth-card .brand { gap: 7px; margin-bottom: 15px; }
  .auth-card .brand img { width: 88px; height: 88px; }
}

@media screen and (max-width: 340px) {
  .kpis { grid-template-columns: 1fr; }
  .modal .actions { grid-template-columns: 1fr; }
}

@media screen and (max-height: 560px) and (max-width: 860px) {
  .auth-wrap { place-items: start center; padding-block: 10px; }
  .auth-card { padding-block: 16px; }
  .auth-card .brand img { width: 64px; height: 64px; }
  .auth-card .brand { margin-bottom: 10px; }
  .modal-backdrop { align-items: end; padding: 8px 8px 0; }
  .modal { max-height: calc(100dvh - 8px); border-radius: 16px 16px 0 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
