/* Documents (#/documents): list, template chooser and editor. Built on the staff app's tokens in ui.css
   (--ui-*, --s-*), so it follows light and dark mode and matches the jobs pages. */

/* ---------- list */
.docs-filters { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.docs-filters .searchbox { flex: 1 1 280px; max-width: 520px; }
.docs-filters .searchbox input {
  width: 100%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%239AA5B0' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: 12px 50%;
}
/* The filter labels are kept for screen readers but shown as the select itself, like the jobs toolbar. */
.docs-filters > label:not(.searchbox) { position: relative; display: block; font-size: 0; }
.docs-filters > label:not(.searchbox) select,
.docs-filters > label:not(.searchbox) .ui-pick { font-size: 14px; max-width: 230px; margin: 0; }
.docs-filters > label:not(.searchbox) .ui-pick { width: auto; }
.docs-list { display: grid; gap: 10px; }
.docs-card { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 16px 18px; color: inherit; text-decoration: none; transition: border-color .15s, box-shadow .15s; }
.docs-card:hover { border-color: var(--ui-line-strong, var(--ui-faint)); box-shadow: var(--ui-shadow); }
.docs-card:focus-visible { outline: 2px solid var(--ui-accent); outline-offset: 2px; }
.docs-card h2 { margin: 8px 0 2px; font-size: 16px; font-weight: 700; color: var(--ui-ink); }
.docs-card p { margin: 0 0 4px; font-size: 14px; color: var(--ui-ink-2); }
.docs-card .hint { font-size: 13px; }
.docs-card > span[aria-hidden] { flex: none; color: var(--ui-faint); font-size: 18px; }
.docs-list > .box { padding: 18px; }
.docs-list > .box h2 { font-size: 16px; }

/* ---------- status labels: same shape and colours as the job status pills */
.docs-state { display: inline-block; padding: 1px 8px; border-radius: var(--ui-r-xs); line-height: 18px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--s-scheduled-bg); color: var(--s-scheduled); }
.docs-state.state-issued { background: var(--s-done-bg); color: var(--s-done); }
.docs-state.state-archived { background: var(--s-cancel-bg); color: var(--s-cancel); }
.ui-head .sub .docs-state { margin-right: 4px; vertical-align: 1px; background: var(--s-scheduled-bg); color: var(--s-scheduled); }
.ui-head .sub .docs-state.state-issued { background: var(--s-done-bg); color: var(--s-done); }
.ui-head .sub .docs-state.state-archived { background: var(--s-cancel-bg); color: var(--s-cancel); }

/* ---------- back links and the actions row */
.ui-head > div > a:not(.btn)[href^="#/documents"], .ui-main p > a:not(.btn)[href^="#/documents"], .ui-main p > a[href^="#/job/"],
.ui-head > div > a:not(.btn)[href^="#/certificates"], .ui-main p > a:not(.btn)[href^="#/certificates"] {
  display: inline-block; margin-bottom: 6px; font-size: 13px; font-weight: 600; color: var(--ui-muted); text-decoration: none;
}
.ui-head > div > a:not(.btn)[href^="#/documents"]:hover, .ui-main p > a:not(.btn)[href^="#/documents"]:hover, .ui-main p > a[href^="#/job/"]:hover { color: var(--ui-ink); }
.ui-head > div > a:not(.btn)[href^="#/certificates"]:hover, .ui-main p > a:not(.btn)[href^="#/certificates"]:hover { color: var(--ui-ink); }
.docs-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 4px 0 8px; }
#doc-save-status { margin: 0 0 12px; font-size: 13px; color: var(--ui-muted); }
#doc-error, #doc-create-error { margin: 0 0 12px; color: var(--s-bad); white-space: pre-wrap; font-size: 14px; }
#doc-error:empty, #doc-create-error:empty { display: none; }

/* locked (issued or archived) notice */
.docs-locked { display: flex; align-items: flex-start; gap: 10px; margin: 0 0 14px; padding: 12px 14px; border: 1px solid var(--ui-line); border-radius: var(--ui-r); background: var(--ui-sunken); color: var(--ui-ink-2); font-size: 14px; line-height: 1.45; }
.docs-locked svg { flex: none; margin-top: 1px; color: var(--ui-muted); }
.docs-locked p { margin: 0; }
.docs-locked strong { color: var(--ui-ink); }

/* ---------- forms (editor and chooser) */
.docs-editor, .docs-create { display: grid; gap: 14px; }
.docs-editor > .btn, .docs-create > .btn, .docs-editor > .docs-actions { justify-self: start; }
.docs-editor .box, .docs-create > .box { display: grid; gap: 14px; padding: 20px; }
.docs-editor h2 { margin: 0; font-size: 16px; font-weight: 700; color: var(--ui-ink); }
.docs-editor label, .docs-create label { display: grid; gap: 6px; margin: 0; font-size: 13px; font-weight: 600; color: var(--ui-ink-2); }
.docs-editor input, .docs-editor textarea, .docs-editor select, .docs-create select { width: 100%; min-width: 0; margin: 0; font-weight: 400; }
.docs-editor textarea { resize: vertical; line-height: 1.5; }
.docs-editor .hint, .docs-create .hint { margin: 0; }
.docs-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }

/* customer, then site */
.docs-pick { display: grid; gap: 8px; align-content: start; min-width: 0; }
.docs-pick .hint { margin: 0; font-size: 13px; }
.docs-editor .docs-sub { font-weight: 500; }
.docs-sub .hint { font-weight: 400; }
.docs-newsite { display: grid; gap: 10px; padding: 12px; border: 1px dashed var(--ui-line-strong, var(--ui-line)); border-radius: var(--ui-r-sm); background: var(--ui-sunken); }
.docs-editor .docs-short { max-width: 160px; }
.docs-newsite-acts { display: flex; gap: 8px; }

/* sections */
#doc-sections { display: grid; gap: 14px; }
.docs-section { gap: 12px; }
.docs-section-head { display: flex; align-items: flex-end; gap: 10px; }
.docs-section-head > label { flex: 1; min-width: 0; }
.docs-section-tools { display: flex; gap: 4px; flex: none; }
.docs-text-label { margin-top: 0; }

/* hazards */
.docs-risk { min-width: 0; display: grid; gap: 12px; margin: 0; padding: 16px; border: 1px solid var(--ui-line); border-radius: var(--ui-r); background: var(--ui-sunken); }
.docs-risk legend { padding: 0 6px; font-size: 13px; font-weight: 700; color: var(--ui-ink); }
.docs-risk > label { margin: 0; }
.docs-risk > .btn { justify-self: start; }
#doc-risks { display: grid; gap: 12px; }
.docs-ratings { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px 12px; align-items: end; }
.docs-ratings output { padding: 9px 10px; border-radius: var(--ui-r-sm); background: var(--ui-surface); border: 1px solid var(--ui-line); font-size: 13px; font-weight: 700; white-space: nowrap; color: var(--ui-ink); font-variant-numeric: tabular-nums; }

/* template chooser */
.docs-template-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 10px; }
.docs-create .docs-template { display: grid; grid-template-columns: 20px minmax(0, 1fr); column-gap: 10px; row-gap: 4px; align-content: start; padding: 16px; cursor: pointer; transition: border-color .15s, background .15s; }
.docs-template input { width: 18px; height: 18px; margin: 1px 0 0; accent-color: var(--ui-accent); }
.docs-template strong { font-size: 14px; color: var(--ui-ink); }
.docs-template span { grid-column: 2; font-size: 13px; font-weight: 400; line-height: 1.45; color: var(--ui-muted); }
.docs-template:hover { border-color: var(--ui-faint); }
.docs-template:has(input:checked) { border-color: var(--ui-accent); background: var(--ui-accent-soft); box-shadow: 0 0 0 1px var(--ui-accent) inset; }

@media (max-width: 650px) {
  .docs-fields { grid-template-columns: 1fr; }
  .docs-section-head { flex-wrap: wrap; }
  .docs-section-head > label { flex-basis: 100%; }
  .docs-editor .box, .docs-create > .box { padding: 16px; }
  .docs-ratings { grid-template-columns: 1fr 1fr auto; gap: 8px; }
  .docs-ratings label { font-size: 12px; }
  .docs-filters > label:not(.searchbox) { flex: 1 1 140px; }
  .docs-filters > label:not(.searchbox) select,
  .docs-filters > label:not(.searchbox) .ui-pick { max-width: none; width: 100%; }
  .docs-actions .btn { font-size: 13px; }
}

/* ---------- certificates (js/certificates.js) */
.cert-editor { gap: 14px; }
.cert-box h2 { margin: 0 0 4px; font-size: 16px; font-weight: 700; color: var(--ui-ink); }
.cert-box > .hint { margin: 0; }
.cert-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.cert-grid .cf-wide { grid-column: 1 / -1; }
.cert-grid label small.hint { display: block; font-weight: 400; margin-top: 2px; }
.cert-checks { display: grid; gap: 0; }
.cert-check { display: grid; grid-template-columns: minmax(0, 1fr) 110px minmax(0, 220px); gap: 10px; align-items: center; padding: 8px 0; border-top: 1px solid var(--ui-line-2); font-size: 14px; color: var(--ui-ink); }
.cert-check:first-child { border-top: 0; }
.cert-check input, .cert-check select { margin: 0; }
.cert-result.r-pass { color: var(--s-done); font-weight: 700; }
.cert-result.r-fail { color: var(--s-bad); font-weight: 700; }
.cert-result.r-na { color: var(--ui-muted); }
.cert-table-wrap { overflow-x: auto; margin: 0 -4px; padding: 0 4px; }
.cert-edit-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.cert-edit-table th { padding: 6px 6px; text-align: left; font-weight: 600; color: var(--ui-ink-2); white-space: nowrap; border-bottom: 1px solid var(--ui-line); }
.cert-edit-table td { padding: 4px 3px; vertical-align: top; }
.cert-edit-table td input, .cert-edit-table td select, .cert-edit-table td textarea { min-width: 90px; width: 100%; margin: 0; padding: 7px 8px; font-size: 13px; }
.cert-edit-table td textarea { min-width: 180px; }
.cert-box > .btn { justify-self: start; }
.cert-declaration { margin: 0; font-size: 14px; line-height: 1.55; color: var(--ui-ink-2); }
.cert-signs { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }
.cert-sign { display: grid; gap: 10px; padding: 14px; border: 1px solid var(--ui-line); border-radius: var(--ui-r); background: var(--ui-sunken); }
.cert-sign > strong { font-size: 14px; color: var(--ui-ink); }
.cert-badge { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: var(--ui-r-xs); font-size: 11px; font-weight: 600; line-height: 18px; vertical-align: 1px; background: var(--s-scheduled-bg); color: var(--s-scheduled); }
.cert-badge.own { background: var(--s-done-bg); color: var(--s-done); }
.docs-template em { font-style: normal; color: var(--ui-faint); font-size: 12px; }
.cert-tpl-acts { display: flex; gap: 6px; flex: none; }
/* template builder */
.cb-section { gap: 10px; }
.cb-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.cb-head > input { flex: 1 1 220px; font-weight: 600; }
.cb-head > select, .cb-head > .ui-pick { flex: 0 1 230px; }
.cb-head > .ui-pick { width: auto; }
.cb-head input, .cb-head select, .cb-head .ui-pick, .cb-note, .cb-field input, .cb-field select, .cb-field .ui-pick { margin: 0; }
.cb-field { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) auto; gap: 8px; align-items: center; }
.cb-field:has(input + select + input),
.cb-field:has(input + .ui-pick + input) { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) minmax(0, 2fr) auto; }
@media (max-width: 700px) {
  .cert-grid { grid-template-columns: 1fr; }
  .cert-check { grid-template-columns: minmax(0, 1fr) 96px; }
  .cert-check input { grid-column: 1 / -1; }
  .cb-field,
  .cb-field:has(input + select + input),
  .cb-field:has(input + .ui-pick + input) { grid-template-columns: minmax(0, 1fr) auto; }
  .cb-field select, .cb-field .ui-pick, .cb-field input + select + input, .cb-field input + .ui-pick + input { grid-column: 1 / 2; }
}
.docs-editor label.check-line { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.docs-editor label.check-line input { width: auto; }
.cert-grid label > small.hint:not(:last-child) { display: inline; margin: 0 0 0 6px; }
.docs-template strong .cert-badge { grid-column: auto; display: inline-block; font-size: 11px; line-height: 18px; }

/* ---------- quotes and invoices (js/sales.js) */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.docs-state.state-accepted, .docs-state.state-paid { background: var(--s-done-bg); color: var(--s-done); }
.docs-state.state-declined, .docs-state.state-void, .docs-state.state-overdue { background: var(--s-bad-bg); color: var(--s-bad); }
.docs-state.state-expired, .docs-state.state-part_paid { background: var(--s-onsite-bg); color: var(--s-onsite); }
.docs-state.state-draft { background: var(--s-cancel-bg); color: var(--ui-ink-2); }
.ui-head .sub .docs-state.state-accepted { background: var(--s-done-bg); color: var(--s-done); }
.ui-head .sub .docs-state.state-declined { background: var(--s-bad-bg); color: var(--s-bad); }
.ui-head .sub .docs-state.state-expired { background: var(--s-onsite-bg); color: var(--s-onsite); }
.ui-head .sub .docs-state.state-draft { background: var(--s-cancel-bg); color: var(--ui-ink-2); }
.sl-wide { grid-column: 1 / -1; }
.sl-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.sl-add-item select, .sl-add-item .ui-pick { min-width: 220px; }
.sl-add-item .ui-pick { width: auto; }
#sl-lines { display: grid; gap: 10px; margin: 12px 0; }
/* Columns are set from the script (--sl-cols), like a Xero invoice: Item, Description, Qty, Price, Disc %, Account, VAT, Amount. */
.sl-line, .sl-cols { display: grid; grid-template-columns: var(--sl-cols, minmax(0, 2fr) 132px 104px 64px 112px 92px 96px); gap: 8px; }
.sl-line { align-items: start; padding: 10px; border: 1px solid var(--ui-line); border-radius: var(--ui-r-sm); background: var(--ui-surface); }
.sl-cols { padding: 0 11px; margin-bottom: -4px; font-size: 11.5px; font-weight: 600; color: var(--ui-muted); }
.sl-cols span:nth-last-child(2) { text-align: right; }
.sl-line .sl-cap { display: none; }
.sl-qty { display: grid; grid-template-columns: 58px 1fr; gap: 4px; }
.sl-qty label { display: block; }
.sl-qty input { width: 100%; }
.sl-qty input[data-l="unit"] { font-size: 13px; }
.sl-line select, .sl-line input { width: 100%; min-width: 0; }
.sl-acct, .sl-opt { min-width: 0; }
.sl-opt { grid-column: 1 / -1; display: flex !important; align-items: center; gap: 8px; }
.sl-opt .sl-cap { display: inline; white-space: nowrap; flex: none; }
.sl-opt select { width: auto; min-width: 160px; }
.sl-line label { display: grid; gap: 3px; margin: 0; }
.sl-cap { font-size: 11px; font-weight: 600; color: var(--ui-muted); }
.sl-desc textarea { min-height: 38px; resize: vertical; overflow: hidden; line-height: 1.4; }
.sl-amount { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; padding-top: 9px; white-space: nowrap; }
.sl-tools { display: flex; gap: 2px; justify-content: flex-end; padding-left: 8px; }
.sl-totals { display: grid; gap: 6px; margin: 14px 0 4px auto; max-width: 360px; font-variant-numeric: tabular-nums; }
.sl-totals > div { display: flex; justify-content: space-between; gap: 16px; font-size: 14px; color: var(--ui-ink-2); }
.sl-totals .sl-grand { padding-top: 8px; border-top: 2px solid var(--ui-ink); font-size: 17px; font-weight: 700; color: var(--ui-ink); }
.sl-item { align-items: center; }
.sl-dialog { max-width: 460px; width: calc(100% - 32px); border: 1px solid var(--ui-line); border-radius: var(--ui-r); padding: 22px; background: var(--ui-surface); color: var(--ui-ink); box-shadow: var(--ui-shadow-lg); }
.sl-dialog::backdrop { background: rgba(16, 24, 40, .45); }
.sl-dialog h2 { margin: 0 0 8px; font-size: 18px; }
.sl-dialog p { margin: 0 0 14px; color: var(--ui-ink-2); font-size: 14px; }
.sl-dialog .docs-actions { margin-top: 18px; }
@media (max-width: 1100px) {
  .sl-cols { display: none; }
  .sl-line { grid-template-columns: repeat(4, minmax(0, 1fr)); align-items: end; }
  .sl-line .sl-cap { display: block; }
  .sl-itm, .sl-desc, .sl-acct { grid-column: 1 / -1; }
  .sl-qty { grid-column: span 2; }
  .sl-vat { grid-column: span 2; }
  .sl-amount { grid-column: span 2; align-self: center; padding: 0; text-align: left; font-size: 16px; }
  .sl-tools { grid-column: span 2; align-self: center; }
}

.sl-owed { display: flex; gap: 12px; flex-wrap: wrap; margin: 0 0 14px; }
.sl-owed > div { display: grid; gap: 2px; padding: 14px 18px; border: 1px solid var(--ui-line); border-radius: var(--ui-r); background: var(--ui-surface); min-width: 200px; }
.sl-owed span { font-size: 12px; font-weight: 600; color: var(--ui-muted); text-transform: uppercase; letter-spacing: .05em; }
.sl-owed strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.sl-owed small { color: var(--ui-muted); font-size: 13px; }
.sl-owed .is-late strong { color: var(--s-bad); }
.sl-pays { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 8px; }
.sl-pays li { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 12px; border: 1px solid var(--ui-line); border-radius: 8px; }
.sl-pays small { display: block; color: var(--ui-muted); font-size: 12px; margin-top: 2px; }
.sl-dialog form.stack { display: grid; gap: 12px; }
.sl-dialog label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
.sl-dialog-error { color: var(--s-bad); margin: 0 !important; font-size: 13px !important; }
.ui-head .sub .docs-state.state-paid { background: var(--s-done-bg); color: var(--s-done); }
.ui-head .sub .docs-state.state-overdue, .ui-head .sub .docs-state.state-void { background: var(--s-bad-bg); color: var(--s-bad); }
.ui-head .sub .docs-state.state-part_paid { background: var(--s-onsite-bg); color: var(--s-onsite); }

/* price list */
.sl-items-box { padding: 0; overflow-x: auto; }
.sl-items { width: 100%; border-collapse: collapse; font-size: 14px; }
.sl-items th { text-align: left; font-size: 12px; font-weight: 600; color: var(--ui-muted); padding: 12px 16px; border-bottom: 1px solid var(--ui-line); white-space: nowrap; }
.sl-items td { padding: 12px 16px; border-bottom: 1px solid var(--ui-line-2, var(--ui-line)); vertical-align: top; }
.sl-items tbody tr:last-child td { border-bottom: 0; }
.sl-items td small { display: block; margin-top: 2px; font-size: 13px; color: var(--ui-muted); }
.sl-items .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.sl-items tr.is-hidden td { color: var(--ui-muted); }
.sl-items .docs-state { margin-left: 6px; vertical-align: 1px; }
.sl-items-acts { text-align: right; white-space: nowrap; width: 1%; }
.sl-items-acts .btn + .btn { margin-left: 4px; }
.sl-dialog textarea, .sl-dialog input:not([type=checkbox]), .sl-dialog select { width: 100%; }
.sl-dialog .hint { font-weight: 400; }
.sl-dialog-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 600px) {
  .sl-items thead { display: none; }
  .sl-items tr { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 12px 14px; border-bottom: 1px solid var(--ui-line); }
  .sl-items td { padding: 0; border: 0; }
  .sl-items-acts { grid-column: 1 / -1; text-align: left; width: auto; }
}
.docs-editor .ui-pick, .docs-create .ui-pick, .cert-check .ui-pick, .sl-dialog .ui-pick { width: 100%; min-width: 0; margin: 0; }
.cert-edit-table .ui-pick-btn { min-width: 90px; padding: 7px 8px; font-size: 13px; }
.sl-import { max-width: 620px; max-height: calc(100dvh - 32px); overflow: auto; }
.sl-import .sl-import-cols { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.sl-import .sl-import-preview { margin-top: 12px; }

.sl-line .sl-acct, .sl-line .sl-opt { grid-column: 1 / -1; max-width: 460px; }
.sl-line .sl-acct select, .sl-line .sl-opt select { width: 100%; }
.sl-opt-total { color: var(--muted, #59677a); font-size: .92em; }

/* Photos on a quote or invoice */
.sl-photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; margin-top: 10px; }
.sl-photos figure { position: relative; margin: 0; aspect-ratio: 4 / 3; overflow: hidden; border-radius: 8px; background: var(--ui-sunken); }
.sl-photos img { width: 100%; height: 100%; object-fit: cover; display: block; }
.sl-photos figure button { position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; border: 0; border-radius: 50%; background: rgba(0,0,0,.65); color: #fff; cursor: pointer; }
.sl-photo-pick { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--ui-line); }
.sl-photo-pick[hidden] { display: none; }
.sl-photo-opt { position: relative; padding: 0; aspect-ratio: 4 / 3; overflow: hidden; border: 2px solid transparent; border-radius: 8px; background: var(--ui-sunken); cursor: pointer; }
.sl-photo-opt img { width: 100%; height: 100%; object-fit: cover; display: block; opacity: .75; }
.sl-photo-opt.is-on { border-color: var(--ui-accent); }
.sl-photo-opt.is-on img { opacity: 1; }
.sl-photo-opt.is-on::after { content: '✓'; position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 50%; background: var(--ui-accent); color: #fff; font-size: 13px; line-height: 22px; text-align: center; }
