:root {
  --ink: #111;
  --text: #222;
  --muted: #666;
  --line: #e4e4e4;
  --line-strong: #d0d0d0;
  --green: #3b7d43;
  --amber: #96651f;
  --bg: #f3f3f1;
  --surface: #fff;
  --font: Inter, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); }
button, input, textarea { font: inherit; color: inherit; }

/* App chrome */
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px; height: 52px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { color: var(--ink); }
.tabs { display: flex; gap: 4px; }
.tab { padding: 6px 12px; border: 0; border-radius: 6px; background: none; color: var(--muted); cursor: pointer; }
.tab:hover { color: var(--ink); }
.tab.active { background: var(--bg); color: var(--ink); font-weight: 600; }

.editor { display: grid; grid-template-columns: 430px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; height: calc(100vh - 52px); height: calc(100dvh - 52px); }
.editor .panel { grid-column: 1; grid-row: 1; }
.editor .stage { grid-column: 2; grid-row: 1 / 3; }
.pane-switch { display: none; }
.panel { overflow-y: auto; padding: 20px; background: var(--surface); border-right: 1px solid var(--line); }
.stage { overflow: auto; padding: 28px; }
.panel h3 { margin: 22px 0 10px; font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }

.biz-picker { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.biz { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 6px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); cursor: pointer; font-size: 12px; line-height: 1.25; text-align: center; }
.biz img { height: 34px; max-width: 100%; object-fit: contain; }
.biz:hover { border-color: #888; }
.biz.active { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); font-weight: 600; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; margin-bottom: 14px; padding: 3px; border-radius: 8px; background: var(--bg); }
.segmented input { position: absolute; opacity: 0; }
.segmented span { display: block; padding: 6px; border-radius: 6px; text-align: center; cursor: pointer; }
.segmented input:checked + span { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, .12); }
.segmented input:focus-visible + span { outline: 2px solid var(--ink); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.field { display: block; margin-bottom: 10px; min-width: 0; }
.field > span { display: block; margin-bottom: 3px; font-size: 12px; color: var(--muted); }
.field input, .field textarea, .item input, .item textarea { width: 100%; padding: 7px 9px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface); }
.field textarea, .item textarea { display: block; resize: vertical; }
input:focus, textarea:focus { outline: 2px solid var(--ink); outline-offset: -1px; }

.item { position: relative; margin-bottom: 8px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; }
.item-nums { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 6px; margin-top: 6px; }
.item-dates { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
.item-dates.single { grid-template-columns: 1fr; }
.item-dates label { display: block; min-width: 0; font-size: 11px; color: var(--muted); }
.item-nums label { font-size: 11px; color: var(--muted); }
.item-pricing { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 11px; color: var(--muted); }
.item-pricing select { flex: 1; padding: 6px 8px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface); color: var(--text); font-size: 13px; }
.item-warn { margin: 6px 0 0; }
.item-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; font-weight: 600; color: var(--ink); }
.remove { border: 0; background: none; color: var(--muted); font-size: 12px; font-weight: 400; cursor: pointer; }
.remove:hover { color: #b3261e; }

.tick { display: flex; align-items: center; gap: 9px; margin: 14px 0 10px; padding: 10px 12px; border: 1px solid var(--line-strong); border-radius: 8px; font-weight: 600; color: var(--ink); cursor: pointer; }
.tick input { width: 17px; height: 17px; accent-color: var(--ink); }
.warn { margin: -4px 0 12px; font-size: 12px; color: var(--amber); }
.link { padding: 0; border: 0; background: none; text-decoration: underline; color: inherit; cursor: pointer; }

.actions { grid-column: 1; grid-row: 2; display: flex; align-items: center; gap: 8px; padding: 10px 20px; padding-bottom: max(10px, env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line); border-right: 1px solid var(--line); }
.actions { flex-wrap: wrap; }
.actions .status { flex: 0 0 100%; margin: 0; min-height: 0; font-size: 13px; }
.actions .status:empty { display: none; }
.action-total { margin-right: auto; font-size: 13px; line-height: 1.25; }
.action-total span, .action-total strong { display: block; }
.action-total strong { color: var(--ink); font-size: 15px; }
.btn.wide { width: 100%; }
.pay-line { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin: 0 0 12px; padding: 10px 12px; border-radius: 8px; background: var(--bg); font-size: 13px; }
.more { margin: 8px 0; padding-top: 8px; border-top: 1px solid var(--line); }
.more summary { padding: 8px 0; font-weight: 600; color: var(--ink); cursor: pointer; }
.more[open] summary { margin-bottom: 8px; }
select { max-width: 100%; }
.field select { width: 100%; padding: 7px 9px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface); color: var(--text); }
.btn { padding: 8px 14px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface); cursor: pointer; }
.btn:hover { border-color: #888; }
.btn.primary { border-color: var(--ink); background: var(--ink); color: #fff; font-weight: 600; }
.btn.ghost { border-color: transparent; background: var(--bg); }
.btn.danger:hover { border-color: #b3261e; color: #b3261e; }
.btn.small { padding: 4px 9px; font-size: 12px; }
.status { min-height: 20px; margin: 10px 0 0; font-size: 12px; color: var(--muted); }
.status.error { color: #b3261e; }

.page { max-width: 1000px; margin: 0 auto; padding: 28px 20px; }
.page h2 { margin: 0 0 6px; color: var(--ink); }
.muted { color: var(--muted); }

.filters { display: grid; gap: 10px; padding: 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.filters input[type=search] { width: 100%; padding: 8px 10px; border: 1px solid var(--line-strong); border-radius: 6px; }
.filters .segmented { margin: 0; grid-template-columns: repeat(3, 1fr); }
.more-filters summary { padding: 4px 0 10px; font-size: 13px; font-weight: 600; color: var(--ink); cursor: pointer; }
.filter-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 10px; align-items: end; }
.filter-grid .btn { margin-bottom: 10px; }
.summary { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin: 14px 0 6px; }
.summary > div { padding: 10px 12px; border-radius: 8px; background: var(--surface); }
.summary span, .summary strong { display: block; }
.summary span { font-size: 11px; }
.summary strong { color: var(--ink); font-size: 15px; overflow-wrap: anywhere; }
.doc-card { margin-bottom: 8px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.card-top { display: flex; justify-content: space-between; gap: 10px; color: var(--ink); }
.card-meta { margin-top: 2px; font-size: 12.5px; color: var(--muted); }
.card-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.doc-badge { font-size: 11.5px; font-weight: 500; color: var(--muted); }
.login { display: grid; place-items: center; min-height: 100vh; min-height: 100dvh; padding: 16px; }
.login-card { width: min(360px, 100%); padding: 24px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.login-card h1 { margin: 0 0 18px; font-size: 20px; color: var(--ink); }
.fatal { padding: 24px; }
.form-foot { display: flex; align-items: center; gap: 10px; }
.form-foot .status { margin: 0; min-height: 0; }
#logout { margin-top: 16px; }
dialog { width: min(440px, calc(100vw - 24px)); padding: 18px; border: 0; border-radius: 10px; color: var(--text); background: var(--surface); box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog h2 { margin: 0 0 12px; font-size: 16px; color: var(--ink); }
.pay-summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; padding: 10px; border-radius: 8px; background: var(--bg); }
.pay-summary span, .pay-summary strong { display: block; }
.pay-summary span { font-size: 11px; }
.pay-summary strong { color: var(--ink); }
.pay-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.pay-row strong { margin-left: auto; color: var(--ink); }
#pay-entry { margin-top: 12px; }
.dialog-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.dialog-buttons.end { justify-content: flex-end; margin-top: 12px; }
.empty { padding: 40px 16px; border-radius: 8px; background: var(--surface); text-align: center; color: var(--muted); }

.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(290px, 1fr)); gap: 14px; margin-top: 16px; }
.settings form { padding: 16px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.settings h3 { margin: 18px 0 10px; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.colour-field input { width: 64px; height: 36px; padding: 2px; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface); cursor: pointer; }
.logo-row { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.logo-row img { width: 72px; height: 56px; object-fit: contain; }

/* The document: pink corner design */
#sheet { zoom: var(--sheet-zoom, 1); }
.page, #measure {
  --band: #eff0f2;
  --doc-ink: #3a3f45;
  color: var(--doc-ink); font: 12px/1.55 Montserrat, var(--font); letter-spacing: .01em;
}
.page { position: relative; width: 210mm; height: 297mm; margin: 0 auto 16px; overflow: hidden; background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .14); }
.page-bg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
.page-body { position: relative; padding: 14mm 16mm 0; }
.blk { display: flow-root; }
#measure, .pdf-root { position: absolute; top: 0; left: -10000px; width: 210mm; }
#measure { height: auto; visibility: hidden; }
.pdf-root .page { margin: 0; box-shadow: none; }

.doc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; min-height: 28mm; }
.doc-title { margin: 6mm 0 0; color: #fff; font-size: 34px; line-height: 1; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.doc-brand { display: flex; align-items: center; gap: 10px; max-width: 46%; margin-top: 8mm; }
.doc-logo { flex: none; }
.brand-name { font-size: 17px; line-height: 1.2; font-weight: 600; color: var(--doc-ink); }
.brand-line { font-size: 8.5px; line-height: 1.45; letter-spacing: .05em; color: #777; }

.doc-parties { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; margin: 5mm 0 9mm; }
.bill-to h3 { margin: 0 0 3px; font-size: 15px; font-weight: 600; letter-spacing: .03em; color: var(--doc-ink); }
.client-name { font-size: 13px; font-weight: 600; letter-spacing: .03em; color: var(--doc-ink); }
.client-meta { max-width: 80mm; font-size: 10.5px; font-weight: 500; white-space: pre-line; }
.doc-meta { display: grid; gap: 3px; min-width: 62mm; margin-top: 6mm; }
.doc-meta div { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; }
.doc-meta span { font-size: 12px; font-weight: 600; letter-spacing: .04em; }
.doc-meta strong { font-size: 10.5px; font-weight: 600; text-align: right; }
.doc-badge.unpaid, .doc-badge.partial { color: #b7791f; }
.doc-badge.paid { color: #2f855a; }
.doc-meta .doc-badge { font-size: 10.5px; font-weight: 600; }

.doc-table { width: 100%; border-collapse: collapse; background: #fff; }
.doc-table th { padding: 8px 10px; border-top: 1.5px solid var(--doc-ink); border-bottom: 1.5px solid var(--doc-ink); font-size: 10.5px; font-weight: 500; letter-spacing: .05em; text-align: left; }
.doc-table th:first-child { border-left: 1.5px solid var(--doc-ink); }
.doc-table th:last-child { border-right: 1.5px solid var(--doc-ink); }
.doc-table td { padding: 10px 10px; border-bottom: 1px solid #c9ccd0; font-size: 10.5px; letter-spacing: .03em; vertical-align: middle; }
.doc-table tr { break-inside: avoid; }
.doc-table .sl { width: 10mm; text-align: center; }
.doc-table .r { text-align: right; white-space: nowrap; }
.doc-table .c { text-align: center; white-space: nowrap; }
.doc-table td.amount { font-weight: 600; }
.doc-table td.desc { white-space: pre-line; }
.doc-table .when { display: block; margin-top: 2px; font-size: 9px; color: #888; }
.doc-table td.none { padding: 22px 10px; text-align: center; color: #999; }

.doc-bottom { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; margin-top: 10mm; break-inside: avoid; }
.doc-notes { flex: 1; min-width: 0; }
.thanks { margin: 0 0 6mm; font-size: 10.5px; font-weight: 600; letter-spacing: .04em; }
.doc-block { margin-bottom: 5mm; break-inside: avoid; }
.doc-block h4 { margin: 0 0 4px; font-size: 11.5px; font-weight: 600; letter-spacing: .04em; color: var(--doc-ink); }
.doc-block p { margin: 0; max-width: 85mm; font-size: 9px; line-height: 1.6; white-space: pre-line; }
.info-grid { display: grid; grid-template-columns: max-content 1fr; gap: 1px 16px; font-size: 9px; }
.info-grid strong { font-weight: 500; }

.doc-sums { width: 62mm; flex: none; margin-top: 6mm; }
.doc-sums div { display: flex; justify-content: space-between; gap: 12px; padding: 2px 0; font-size: 10.5px; font-weight: 600; letter-spacing: .04em; }
.doc-sums .grand { margin: 6px 0 4px; padding-top: 9px; border-top: 1.5px solid var(--doc-ink); font-size: 14px; }
.doc-sums .due strong { color: var(--accent); }
.doc-sums .sign { display: block; width: 44mm; margin: 14mm 0 0 auto; padding-top: 7px; border-top: 1.5px solid var(--doc-ink); text-align: center; font-size: 10.5px; }

@media (max-width: 1000px) {
  .editor { display: block; height: auto; padding-bottom: 72px; }
  .pane-switch { position: sticky; top: 52px; z-index: 4; display: grid; grid-template-columns: 1fr 1fr; padding: 6px 12px; background: var(--surface); border-bottom: 1px solid var(--line); }
  .pane-switch button { min-height: 38px; border: 0; border-radius: 6px; background: none; color: var(--muted); }
  .pane-switch button.active { background: var(--bg); color: var(--ink); font-weight: 600; }
  .editor[data-pane="form"] .stage, .editor[data-pane="preview"] .panel { display: none; }
  .panel { overflow: visible; padding: 16px; border-right: 0; }
  .stage { padding: 12px; }
  .actions { position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; padding-left: 12px; padding-right: 12px; border-right: 0; }
}

@media (max-width: 640px) {
  .topbar { justify-content: center; padding: 0 8px; }
  .brand { display: none; }
  .tabs { width: 100%; }
  .tab { flex: 1; min-height: 38px; }
  .page { padding: 14px 12px; }
  /* 16px stops iPhones zooming the page when a field is tapped. */
  input, textarea, select { font-size: 16px !important; }
  .btn { min-height: 42px; }
  .btn.small { min-height: 38px; padding: 6px 11px; font-size: 13px; }
  .remove { min-height: 34px; padding: 0 4px; }
  .item-pricing { flex-direction: column; align-items: stretch; gap: 3px; }
  .filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .settings { grid-template-columns: minmax(0, 1fr); }
  .actions .btn { padding-left: 11px; padding-right: 11px; }
  input[type="date"] { min-width: 0; -webkit-appearance: none; appearance: none; min-height: 40px; }
}

@page { size: A4; margin: 0; }
@media print {
  html, body { background: #fff; }
  .no-print { display: none !important; }
  #view-editor { display: block !important; height: auto; padding: 0; }
  .stage { display: block !important; overflow: visible; padding: 0; }
  #sheet { zoom: 1; }
  .page { height: 296mm; margin: 0; box-shadow: none; break-after: page; }
  .page:last-child { break-after: auto; }
  #measure { display: none; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
