:root {
  --ink: #1f2933; --muted: #6b7785; --line: #e3e7eb; --bg: #f6f7f9; --card: #fff;
  --brand: #d2a26b; --brand-ink: #5a3d1c; --brand-soft: #f6ede2;
  --ok: #2f855a; --ok-bg: #def7e7; --warn: #975a16; --warn-bg: #fdf3d8; --bad: #c53030; --bad-bg: #fde2e2; --info-bg: #e6f0fb;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink); background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
a { color: var(--brand-ink); }
h1 { font-size: 1.6rem; margin: 0 0 1rem; display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
h2 { font-size: 1.1rem; margin: 2rem 0 .6rem; }
h3 { margin: 0; font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.lede { font-size: 1.05rem; margin: -.25rem 0 1rem; }
[hidden] { display: none !important; }

/* ---------- shell ---------- */
#top { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: .75rem; padding: .5rem 1.25rem; background: var(--card); border-bottom: 1px solid var(--line); }
#top:empty, #menu:empty { display: none; }
.brand { display: flex; align-items: center; gap: .4rem; font-weight: 700; text-decoration: none; color: var(--ink); min-width: 150px; }
.brand img { height: 28px; }
.searchwrap { position: relative; flex: 1; max-width: 520px; }
.searchwrap .search { width: 100%; padding-right: 2rem; }
.searchwrap kbd { position: absolute; right: .55rem; top: 50%; transform: translateY(-50%); font: 12px/1 ui-monospace, monospace; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 2px 5px; pointer-events: none; }
.results { position: absolute; left: 0; right: 0; top: calc(100% + 4px); margin: 0; padding: .25rem; list-style: none; background: var(--card); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.08); max-height: 60vh; overflow: auto; }
.results li { display: flex; justify-content: space-between; gap: 1rem; padding: .45rem .6rem; border-radius: 6px; cursor: pointer; }
.results li.on, .results li:hover { background: var(--brand-soft); }
.results small { color: var(--muted); white-space: nowrap; }
.newmenu { position: relative; }
.newmenu summary { list-style: none; }
.newmenu summary::-webkit-details-marker { display: none; }
.newmenu .menu { position: absolute; right: 0; top: calc(100% + 4px); background: var(--card); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.08); padding: .3rem; min-width: 190px; display: grid; z-index: 20; }
.newmenu .menu a { padding: .5rem .7rem; border-radius: 6px; text-decoration: none; color: var(--ink); }
.newmenu .menu a:hover { background: var(--brand-soft); }
.who { display: flex; flex-direction: column; line-height: 1.15; text-align: right; margin-left: auto; }
.who small { color: var(--muted); }

.layout { display: flex; align-items: flex-start; }
#menu { position: sticky; top: 53px; width: 200px; flex: none; padding: 1rem .75rem; display: grid; gap: 1rem; }
.navgroup { display: grid; gap: .1rem; }
.navlabel { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 0 .7rem .2rem; }
#menu a { display: flex; justify-content: space-between; align-items: center; text-decoration: none; color: var(--ink); padding: .45rem .7rem; border-radius: 6px; }
#menu a:hover { background: var(--line); }
#menu a.active { background: var(--brand-soft); color: var(--brand-ink); font-weight: 600; }
.count { display: inline-block; min-width: 1.4em; padding: 0 .4em; margin-left: .4rem; border-radius: 99px; background: var(--brand-ink); color: #fff; font-size: .75rem; text-align: center; font-weight: 600; }

main { flex: 1; min-width: 0; max-width: 1060px; padding: 1.5rem 1.5rem 4rem; }
.signedout main { margin: 0 auto; }

@media (max-width: 800px) {
  #top { flex-wrap: wrap; }
  .brand { min-width: 0; }
  .searchwrap { order: 5; flex-basis: 100%; max-width: none; }
  .layout { display: block; }
  #menu { position: static; width: auto; display: flex; flex-wrap: wrap; gap: .25rem; padding: .5rem 1rem; border-bottom: 1px solid var(--line); background: var(--card); }
  .navgroup { display: flex; gap: .25rem; }
  .navlabel { display: none; }
  main { padding: 1rem; }
}

/* ---------- controls ---------- */
button, .button { font: inherit; padding: .45rem 1rem; border-radius: 6px; border: 1px solid var(--brand-ink); background: var(--brand-ink); color: #fff; cursor: pointer; text-decoration: none; display: inline-block; white-space: nowrap; }
button:hover, .button:hover { filter: brightness(1.12); }
button:disabled { opacity: .5; cursor: progress; }
button.ghost, .button.ghost { background: transparent; color: var(--brand-ink); }
button.danger { background: var(--bad); border-color: var(--bad); }
button.ghost.danger { background: transparent; color: var(--bad); border-color: transparent; }
button.small { padding: .2rem .6rem; font-size: .85rem; }
button.icon { padding: .1rem .5rem; border-color: transparent; font-size: 1.1rem; line-height: 1; }
input, select, textarea { font: inherit; padding: .45rem .6rem; border: 1px solid #cbd2d9; border-radius: 6px; background: #fff; color: var(--ink); min-width: 0; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
input:invalid:not(:placeholder-shown) { border-color: var(--bad); }
.money { display: inline-flex; align-items: center; gap: .25rem; }
.money input { width: 7.5em; text-align: right; }
.check { display: flex; align-items: center; gap: .4rem; }

.back { display: inline-block; margin-bottom: .4rem; text-decoration: none; color: var(--muted); font-size: .9rem; }
.back:hover { color: var(--brand-ink); }
.titlebar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.titlebar h1, .titlebar h2 { margin: 0; }
.titlebar:has(> h2) { margin: 2rem 0 .6rem; }
.actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.actions.end { justify-content: flex-end; }
.assign { display: flex; gap: .5rem; align-items: center; margin-bottom: 1rem; }
.assign > span { color: var(--muted); }

.tabs { display: flex; gap: .25rem; flex-wrap: wrap; margin-bottom: .75rem; }
.tab { background: transparent; color: var(--ink); border-color: var(--line); }
.tab.active { background: var(--brand-soft); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
.tab .count { background: var(--muted); }
.tab.active .count { background: var(--brand-ink); }

/* ---------- content ---------- */
.tablewrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: 8px; }
.tablewrap .filter { margin: .6rem; width: calc(100% - 1.2rem); max-width: 320px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .5rem .75rem; border-top: 1px solid var(--line); vertical-align: middle; }
thead th { border-top: 0; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
th.sortable button { all: unset; cursor: pointer; font: inherit; color: inherit; letter-spacing: inherit; text-transform: inherit; white-space: nowrap; }
th.sortable button:hover, th[aria-sort$="ending"] button { color: var(--ink); }
th.sortable button:focus-visible { outline: 2px solid var(--brand-ink); outline-offset: 2px; border-radius: 2px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.end, th.end { text-align: right; width: 1%; white-space: nowrap; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #fbf8f4; }

.facts { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1.5rem; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 1rem 1.25rem; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; white-space: pre-line; overflow-wrap: anywhere; }

.pill { font-size: .75rem; font-weight: 600; padding: .1rem .55rem; border-radius: 99px; background: var(--line); color: var(--ink); white-space: nowrap; }
.pill.active, .pill.client, .pill.paid, .pill.complete, .pill.completed, .pill.placed { background: var(--ok-bg); color: var(--ok); }
.pill.prospect, .pill.partial, .pill.assigned, .pill.proposed { background: var(--warn-bg); color: var(--warn); }
.pill.unpaid, .pill.unassigned { background: var(--bad-bg); color: var(--bad); }
.pill.cancelled, .pill.draft { background: var(--line); color: var(--muted); }

.notice { padding: .7rem 1rem; border-radius: 6px; margin: .75rem 0; border-left: 4px solid; background: var(--card); }
.notice.error { border-color: var(--bad); background: var(--bad-bg); }
.notice.warn { border-color: var(--warn); background: var(--warn-bg); }
.notice.info { border-color: #3b74c4; background: var(--info-bg); }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1rem 1.25rem; display: grid; gap: .5rem; align-content: start; }
.card h2 { margin: 0; font-size: 1rem; }
.card p { margin: 0; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.card .kind { font-size: .85rem; color: var(--brand-ink); font-weight: 600; }
.big { font-size: 1.6rem; font-weight: 700; }
.big .muted { font-size: 1rem; font-weight: 400; }
.todo-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .35rem; }
.todo-list li { display: flex; align-items: center; gap: .35rem; flex-wrap: wrap; }
.todo-list li.split { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: .75rem; padding: .35rem 0; border-top: 1px solid var(--line); }
.todo-list li.split:first-child { border-top: 0; }

.notes { display: grid; gap: .5rem; }
.note { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: .6rem 1rem; }
.note time { font-size: .8rem; color: var(--muted); }
.note p { margin: .2rem 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.addnote { display: grid; gap: .5rem; margin-top: .75rem; justify-items: start; }
.addnote textarea { width: 100%; }

/* ---------- forms ---------- */
.form { display: grid; gap: .9rem; max-width: 680px; }
.form.wide { max-width: none; }
.form h2 { margin: 1rem 0 0; }
.field { display: grid; gap: .25rem; flex: 1; min-width: 180px; align-content: start; }
.field > span { font-weight: 600; font-size: .9rem; }
.row { display: flex; gap: 1rem; flex-wrap: wrap; }
.helpers-row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.helpers { display: contents; }

/* invoice lines */
.lines td, .lines th { padding: .45rem .6rem; }
.lines tfoot td { font-weight: 600; }
.lines tfoot .due td { font-size: 1.1rem; border-top: 2px solid var(--ink); }
.lines.edit td:first-child { min-width: 260px; }
.lines.edit td:first-child input { width: 100%; }
.lines.edit td:first-child small { display: block; }
.lines.edit .qty { width: 5.5em; text-align: right; }
.lines .total { font-size: 1.1rem; }

/* invoice paper */
.paper { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1.5rem; display: grid; gap: 1.25rem; }
.paper-head { display: flex; justify-content: space-between; gap: 2rem; flex-wrap: wrap; }
.paper-head .facts { border: 0; padding: 0; }
.invoice-notes { white-space: pre-wrap; color: var(--muted); margin: 0; }
.print-letter { display: none; }
/* Agreements (contractors.js): the filled text, then both signatures. */
.doc-logo { display: block; width: 300px; max-width: 80%; margin: 0 auto 1rem; }
.legal { overflow-wrap: anywhere; line-height: 1.5; }
.legal h1, .legal h2, .legal h3 { font-size: 1.05rem; }
.sigs { display: flex; gap: 3rem; flex-wrap: wrap; }
.sigblock { display: grid; gap: .15rem; justify-items: start; }
.sig { max-width: 280px; max-height: 90px; border-bottom: 1px solid var(--ink); }

/* reports: profit and loss, client statement */
.report-nav { display: flex; gap: 1rem; align-items: center; margin: -.5rem 0 1rem; }
.paper-title { font-size: 1.25rem; font-weight: 700; margin-top: .5rem; }
.paper-title.small-title { font-size: 1rem; margin: .25rem 0 -.75rem; }
.letterhead { height: 40px; width: auto; display: block; }
.report td, .report th { padding: .35rem .6rem; white-space: nowrap; vertical-align: top; }
.report td:first-child, .report th:first-child { white-space: normal; }
.report td.date { font-variant-numeric: tabular-nums; }
.report .acct { color: var(--muted); font: 12px ui-monospace, monospace; margin-right: .5rem; }
.report .neg, strong.neg { color: var(--bad); }
.report .zero { color: var(--muted); }
.report td.yr, .report th.yr { font-weight: 600; background: var(--bg); }   /* the year column ("total" is taken by invoice lines) */
.report tr.group td { padding-top: 1rem; font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }
.report tr.line td:first-child { padding-left: 1.25rem; }
.report tr.subtotal td { border-top: 1px solid var(--line); font-weight: 600; }
.report tr.grand td { border-top: 2px solid var(--ink); border-bottom: 3px double var(--ink); font-weight: 700; }
.report.pl { min-width: 900px; font-size: .88rem; }
.report.pl td, .report.pl th { padding: .35rem .4rem; }
.report.pl td:first-child { white-space: nowrap; }
.report a { white-space: nowrap; }
.report.activity tr.pay td, .report.activity tr.pay td a { color: var(--ok); }
.pill.overdue { background: var(--bad-bg); color: var(--bad); }
.pill.current { background: var(--info-bg); color: var(--ink); }

/* sign in */
.signin { max-width: 380px; margin: 10vh auto 0; text-align: center; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 2rem; }
.signin h1 { justify-content: center; margin-bottom: .25rem; }
.signin-logo { height: 64px; }
.google-slot { display: flex; justify-content: center; margin: 1.25rem 0; }
.devlogin { display: grid; gap: .4rem; margin-top: 1.25rem; padding-top: 1rem; border-top: 1px dashed var(--line); }

/* modal and toast */
.modal { border: 0; border-radius: 12px; padding: 1.25rem 1.5rem; width: min(560px, calc(100vw - 2rem)); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal::backdrop { background: rgba(20, 24, 28, .45); }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: .75rem; }
.modal-head h2 { margin: 0; }
.toast { position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: .6rem 1.1rem; border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.2); z-index: 100; }

@media print {
  #top, #menu, .back, .titlebar .actions, .noprint, h2, .tablewrap:not(.paper .tablewrap) { display: none !important; }
  body, main { background: #fff; padding: 0; }
  .paper { border: 0; padding: 0; }
  /* An invoice prints as the letter alone (letter.css), nothing else on the page. */
  main:has(.print-letter) > :not(.print-letter) { display: none !important; }
  .print-letter { display: block; }
  main:has(.print-doc) > :not(.print-doc) { display: none !important; }
  .print-doc .legal h2 { display: block !important; }   /* the rule above hides h2s app-wide */
  /* Reports: whole tables, no scrolling; the P&L's month columns need a landscape page. */
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .report-paper .tablewrap { overflow: visible; border: 0; }
  main:has(.report-paper.landscape) { page: landscape; }   /* the whole printout, so no portrait first page */
  main:has(.report-paper) .titlebar { display: none; }      /* the paper carries its own title */
  .report { font-size: 10.5px; }
  .report.pl { min-width: 0; font-size: 9.5px; }
  .report td, .report th { padding: 3px 5px; }
  .report thead { display: table-header-group; }
  .report tr { break-inside: avoid; }
}
@page landscape { size: letter landscape; margin: 12mm 10mm; }

/* share dialog */
.share { display: grid; gap: .75rem; }
.share p { margin: 0; }
.recipients { margin: 0; padding-left: 1.2rem; }
.recipients .pill { margin-left: .4rem; }
.linkbox { display: flex; gap: .5rem; flex-wrap: wrap; }
.linkbox input { flex: 1; min-width: 220px; font: 13px ui-monospace, monospace; }
