:root {
  --bg: #f5f4f0;
  --surface: #ffffff;
  --ink: #1d2124;
  --muted: #646a70;
  --line: #e2dfd7;
  --line-strong: #c9c5bb;
  --accent: #2e5b4f;
  --accent-ink: #ffffff;
  --accent-soft: #e6efeb;
  --danger: #a33a2c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 20, 20, .05), 0 4px 16px rgba(20, 20, 20, .04);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- controls ---------- */
input, textarea, select {
  font: inherit; color: inherit;
  width: 100%;
  padding: 8px 10px;
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; min-height: 38px; line-height: 1.4; overflow: hidden; }
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }

.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; font-weight: 550; color: var(--muted); margin-bottom: 4px; }
.check { display: flex; gap: 10px; align-items: center; padding: 6px 0; cursor: pointer; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: 14px;
  padding: 9px 16px; min-height: 40px;
  border-radius: 8px; border: 1px solid transparent;
  cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #244a40; }
.btn-secondary { background: #fff; color: var(--ink); border-color: var(--line-strong); }
.btn-secondary:hover { border-color: var(--ink); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--ink); background: rgba(0, 0, 0, .04); }
.btn-danger, .btn-ghost.btn-danger { color: #fff; background: var(--danger); }
.btn-block { width: 100%; }
.link { background: none; border: 0; padding: 4px 6px; font: inherit; font-size: 14px; color: var(--accent); cursor: pointer; font-weight: 600; }
.link:hover { text-decoration: underline; }
.link.danger { color: var(--danger); }
.icon-btn {
  width: 32px; height: 32px; border-radius: 6px; border: 0; background: transparent;
  font-size: 20px; line-height: 1; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: #f3e6e3; color: var(--danger); }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 32px 28px; box-shadow: var(--shadow); text-align: left;
}
.login-card h1 { font-size: 22px; margin-top: 14px; }
.login-card p.muted { margin: 4px 0 22px; }
.brand-mark {
  display: inline-grid; place-items: center;
  width: 48px; height: 48px; border-radius: 10px;
  background: var(--accent); color: #fff; font-weight: 700; letter-spacing: .06em; font-size: 14px;
}
.brand-mark.small { width: 32px; height: 32px; font-size: 11px; border-radius: 7px; }
.error { color: var(--danger); font-size: 14px; margin: -4px 0 12px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.tab {
  font: inherit; font-weight: 600; font-size: 14px; color: var(--muted);
  background: none; border: 0; padding: 8px 12px; border-radius: 7px; cursor: pointer; white-space: nowrap;
}
.tab:hover { color: var(--ink); background: rgba(0, 0, 0, .04); }
.tab.active { color: var(--accent); background: var(--accent-soft); }

main { padding: 20px; max-width: 1500px; margin: 0 auto; }

/* ---------- cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.card > h2, .card-head { margin-bottom: 14px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 14px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 14px; }

/* ---------- editor ---------- */
.editor { display: grid; grid-template-columns: minmax(0, 620px) minmax(0, 1fr); gap: 20px; align-items: start; }
.editor-preview { position: sticky; top: 76px; }
.preview-frame {
  background: #e9e7e1; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
  height: calc(100vh - 100px); min-height: 560px;
}
.preview-frame iframe { width: 100%; height: 100%; border: 0; display: block; background: #e9e7e1; }

.segmented {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  padding: 4px; margin-bottom: 16px;
  background: #e9e7e1; border-radius: 10px;
}
.segmented button {
  font: inherit; font-weight: 600; font-size: 14px; color: var(--muted);
  border: 0; background: transparent; padding: 9px 10px; border-radius: 7px; cursor: pointer;
}
.segmented button.active { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }

.segmented.three { grid-template-columns: repeat(3, 1fr); }

.editor .only-claim { display: none; }
.editor.is-claim .only-claim { display: block; }
.editor.is-claim .only-statement { display: none; }
.editor .check.only-claim { display: none; }
.editor.is-claim .check.only-claim { display: flex; }
.editor .only-narrow { display: none; }

/* items */
.items { margin-bottom: 12px; }
.item-head, .item { display: grid; gap: 8px; align-items: start; }
.item-head { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; padding: 0 0 6px; }
.item { padding: 10px 0; border-top: 1px solid var(--line); }
.items > .item:first-child { border-top: 0; padding-top: 0; }
.mini { display: block; min-width: 0; }
.mini-label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 3px; }
.line-total { display: block; padding-top: 9px; font-weight: 600; font-variant-numeric: tabular-nums; }
.row-tools { display: flex; gap: 2px; }
.icon-btn.star:hover { background: var(--accent-soft); color: var(--accent); }

/* statement: one row per line, headings above the list */
.item-head.statement, .item.statement { grid-template-columns: 64px minmax(0, 1fr) 104px 96px 66px; }
.item.statement .mini-label { display: none; }

/* progress claim: description on top, numbers underneath, labelled */
.item.claim { grid-template-columns: repeat(4, minmax(0, 1fr)) 66px; row-gap: 6px; }
.item.claim > .mini-description { grid-column: 1 / 5; grid-row: 1; }
.item.claim > .row-tools { grid-column: 5; grid-row: 1; margin-top: 20px; }
.item.claim > .mini-agreed { grid-column: 1; grid-row: 2; }
.item.claim > .mini-percent { grid-column: 2; grid-row: 2; }
.item.claim > .mini-previous { grid-column: 3; grid-row: 2; }
.item.claim > .mini-out { grid-column: 4; grid-row: 2; text-align: right; }

.totals { display: grid; gap: 6px; }
.totals > div { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; }
.totals .grand { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 4px; font-size: 18px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.sticky-actions {
  position: sticky; bottom: 0; z-index: 5;
  padding: 12px 0; background: linear-gradient(to top, var(--bg) 75%, rgba(245, 244, 240, 0));
}
#save-status { min-height: 1.2em; margin: 0 0 24px; }
.inline-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form input { flex: 1 1 240px; width: auto; }
.btn-sm { min-height: 34px; padding: 6px 12px; font-size: 13px; }
.armed, .link.armed { color: #fff !important; background: var(--danger) !important; border-radius: 6px; }
.center { text-align: center; }
.ok-text { color: var(--accent); }
.warn-text { color: #8a5a00; }
.notice { background: #fff8e6; border: 1px solid #f0dca8; border-radius: 8px; padding: 10px 12px; }
.card.notice { box-shadow: none; }
.card.notice p { margin: 0 0 10px; }
#quote-link { margin: 0; }

/* ---------- status pills, badges ---------- */
.pill {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 650;
  background: #eceae4; color: var(--muted); white-space: nowrap;
}
.status-sent { background: #e5eef8; color: #22507e; }
.status-paid, .status-accepted { background: var(--accent-soft); color: var(--accent); }
.status-overdue { background: #f8e3df; color: var(--danger); }
.status-declined { background: #eee; color: #777; text-decoration: line-through; }
.status-new { background: #fff3d6; color: #7a5300; }
.badge {
  display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 5px; margin-left: 4px;
  border-radius: 999px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700;
}

/* ---------- list ---------- */
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat {
  display: grid; gap: 2px; text-align: left; font: inherit; color: inherit; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
}
.stat:hover { border-color: var(--line-strong); }
.stat strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 13px; font-weight: 600; color: var(--muted); }
.stat-overdue strong { color: var(--danger); }
.stat-paid strong { color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: inherit; font-size: 13px; font-weight: 600; padding: 6px 12px; border-radius: 999px; cursor: pointer;
  background: #fff; border: 1px solid var(--line-strong); color: var(--muted);
}
.chip.active { background: var(--ink); border-color: var(--ink); color: #fff; }
#list-search, #client-search { max-width: 300px; }

.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.data-table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line-strong); white-space: nowrap; }
.data-table td { padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.data-table th.num, .data-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table a { color: var(--accent); }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions.left { text-align: left; white-space: normal; }

/* ---------- enquiries ---------- */
.enquiries { display: grid; gap: 12px; }
.enquiry { border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; }
.enquiry.done { opacity: .7; }
.enquiry-head { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.enquiry-head .pill { margin-left: auto; }
.enquiry-meta { display: flex; gap: 6px 16px; flex-wrap: wrap; margin-top: 4px; }
.enquiry-meta a { color: var(--accent); }
.enquiry-msg { white-space: pre-wrap; margin: 8px 0; }
.enquiry .row-actions { margin-left: -6px; }

.settings textarea { overflow: auto; }

/* ---------- modal ---------- */
.modal {
  border: 0; padding: 0; border-radius: 14px; width: min(520px, calc(100% - 24px)); max-height: calc(100% - 24px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25); color: var(--ink);
}
.modal.wide { width: min(720px, calc(100% - 24px)); }
.modal::backdrop { background: rgba(20, 22, 24, .45); }
.modal-inner { display: flex; flex-direction: column; max-height: calc(100vh - 24px); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 8px; }
.modal-head h2 { font-size: 18px; }
.modal-body { padding: 8px 18px; overflow-y: auto; }
.modal-body h3 { margin: 14px 0 8px; }
.modal-body textarea { overflow: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; padding: 12px 18px 16px; border-top: 1px solid var(--line); }
.checks { display: grid; gap: 2px; margin-top: 4px; }

.pick-list { display: grid; margin: 10px 0; max-height: 340px; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; }
.pick-list > p { margin: 0; padding: 12px; }
.pick-row { display: flex; gap: 12px; align-items: center; padding: 10px 12px; border-top: 1px solid var(--line); }
.pick-row:first-child { border-top: 0; }
.pick-main { flex: 1; min-width: 0; }
.pick-desc { white-space: pre-wrap; font-size: 14px; }
.pick-actions { display: flex; gap: 4px; align-items: center; }
.pick-new { display: grid; gap: 8px; }

.labour-rows { display: grid; gap: 8px; margin-bottom: 8px; }
.labour-row { display: grid; grid-template-columns: minmax(0, 1fr) 90px auto 32px; gap: 8px; align-items: center; }
.labour-preview {
  margin: 0; padding: 10px 12px; background: #f3f2ee; border-radius: 8px; font: 13px/1.5 ui-monospace, Consolas, monospace; white-space: pre-wrap;
}

/* ---------- toast & boot ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 14px; box-shadow: 0 6px 24px rgba(0, 0, 0, .2); max-width: calc(100% - 32px);
}
.toast.error-toast { background: var(--danger); }
.boot { min-height: 100vh; display: grid; place-items: center; color: var(--muted); }

/* ---------- responsive ---------- */
@media (max-width: 1099px) {
  .editor { grid-template-columns: minmax(0, 1fr); max-width: 720px; margin: 0 auto; }
  .editor-preview { display: none; }
  .editor .only-narrow { display: inline-flex; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 640px) {
  body { font-size: 16px; }
  main { padding: 12px 16px; }
  .topbar { flex-wrap: wrap; padding: 8px 16px; gap: 6px 12px; }
  .brand { flex: 1; }
  .tabs { order: 3; flex-basis: 100%; }
  .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
  .card { padding: 14px; }
  .segmented.three button { font-size: 13px; padding: 9px 4px; }
  .stat strong { font-size: 18px; }
  #list-search, #client-search { max-width: none; }

  .item-head { display: none; }
  .item.statement .mini-label { display: block; }
  .item.statement .mini-description .mini-label { display: none; }
  .item.statement { grid-template-columns: 72px minmax(0, 1fr) minmax(0, 1fr) 66px; }
  .item.statement > .mini-description { grid-column: 1 / 4; grid-row: 1; }
  .item.statement > .row-tools { grid-column: 4; grid-row: 1; }
  .item.statement > .mini-qty { grid-column: 1; grid-row: 2; }
  .item.statement > .mini-each { grid-column: 2; grid-row: 2; }
  .item.statement > .mini-out { grid-column: 3 / 5; grid-row: 2; text-align: right; }

  .item.claim { grid-template-columns: repeat(3, minmax(0, 1fr)) 66px; }
  .item.claim > .mini-description { grid-column: 1 / 4; }
  .item.claim > .row-tools { grid-column: 4; }
  .item.claim > .mini-out { grid-column: 1 / 5; grid-row: 3; display: flex; justify-content: space-between; align-items: baseline; }
  .item.claim > .mini-out .line-total { padding-top: 0; }

  .sticky-actions { position: static; background: none; padding: 4px 0 8px; }
  .sticky-actions .btn { flex: 1 1 calc(50% - 8px); }

  .data-table thead { display: none; }
  .data-table tr { display: block; border-bottom: 1px solid var(--line); padding: 8px 0; }
  .data-table td { display: flex; justify-content: space-between; gap: 12px; border: 0; padding: 3px 0; text-align: right; }
  .data-table td::before { content: attr(data-label); color: var(--muted); font-size: 13px; text-align: left; }
  .data-table td.row-actions { justify-content: flex-start; flex-wrap: wrap; padding-top: 6px; }
  .data-table td.row-actions::before { content: none; }
  .labour-row { grid-template-columns: minmax(0, 1fr) 72px auto 32px; }
  .modal-foot .btn { flex: 1 1 auto; }
}
