:root {
  --brand: #c4621d;
  --brand-dark: #2b2622;
  --cream: #f6ead7;
  --bg: #f4f0eb;
  --card: #ffffff;
  --text: #2b2622;
  --muted: #7a7068;
  --line: #e7ded4;
  --ok: #2f7d4f;
  --warn: #b7791f;
  --bad: #b83a2e;
  --info: #3b6ea8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(43, 38, 34, 0.06), 0 2px 8px rgba(43, 38, 34, 0.05);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { font-size: 15px; line-height: 1.45; -webkit-text-size-adjust: 100%; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; line-height: 1.2; }
h1 { font-size: 24px; }
h3 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.n { text-align: right; font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side { background: var(--brand-dark); color: var(--cream); padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
.side .brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 16px; color: var(--cream); }
.side .brand img { width: 46px; height: auto; }
.side .brand b { display: block; font-size: 15px; letter-spacing: 0.4px; }
.side .brand span { font-size: 11px; opacity: 0.7; text-transform: uppercase; letter-spacing: 1px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: #e9dccb; font-weight: 500; }
.nav a:hover { background: rgba(255, 255, 255, 0.07); text-decoration: none; }
.nav a.on { background: var(--brand); color: #fff; }
.nav svg { width: 18px; height: 18px; flex: none; }
.side .new { margin: 10px 4px 14px; }
.side .foot { margin-top: auto; font-size: 11px; opacity: 0.55; padding: 8px; }
main { padding: 24px 28px 90px; max-width: 1180px; width: 100%; }
.topbar { display: none; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.crumb { font-size: 13px; color: var(--muted); margin-bottom: 6px; display: inline-block; }

/* ---------- controls ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: #fff; color: var(--text); padding: 9px 14px; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; min-height: 40px; }
.btn:hover { border-color: #cdbfb1; text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.dark { background: var(--brand-dark); border-color: var(--brand-dark); color: var(--cream); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { padding: 5px 10px; min-height: 32px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }

label.f { display: block; font-size: 13px; font-weight: 600; color: #5d544d; }
label.f > span { display: block; margin-bottom: 4px; }
input, select, textarea { font: inherit; width: 100%; padding: 9px 10px; border: 1px solid #d9cec2; border-radius: 8px; background: #fff; color: var(--text); min-height: 40px; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 35%, transparent); border-color: var(--brand); }
textarea { resize: vertical; min-height: 70px; }
input[type='checkbox'] { width: auto; min-height: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.stack { display: grid; gap: 12px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.search { max-width: 320px; }

/* ---------- cards / tables ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.card h3 { margin-bottom: 12px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
.kpi { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; border-top: 4px solid var(--brand); }
.kpi .v { font-size: 24px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .l { color: var(--muted); font-size: 13px; font-weight: 600; }
.kpi .s { color: var(--muted); font-size: 12px; margin-top: 2px; }
.kpi.bad { border-top-color: var(--bad); }
.kpi.ok { border-top-color: var(--ok); }
.kpi.info { border-top-color: var(--info); }
.cols { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; align-items: start; }
.cols > .card { margin-top: 0; } /* side by side: the grid gap spaces them */

table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); }
table.list td { padding: 11px 10px; border-bottom: 1px solid #f0e9e1; vertical-align: middle; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: #fbf7f3; }
table.list th.n { text-align: right; }
.empty { padding: 26px; text-align: center; color: var(--muted); }

.pill { display: inline-block; font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 99px; background: #eee6dd; color: #5d544d; text-transform: capitalize; white-space: nowrap; }
.pill.draft { background: #ece9e6; color: #6b6560; }
.pill.sent, .pill.estimate-sent { background: #e3edf8; color: var(--info); }
.pill.viewed { background: #ece6f7; color: #6a4fa3; }
.pill.accepted, .pill.paid, .pill.deposit-paid { background: #e2f1e7; color: var(--ok); }
.pill.partial, .pill.invoiced { background: #fbefd9; color: var(--warn); }
.pill.overdue, .pill.declined { background: #f8e1de; color: var(--bad); }
.pill.void { background: #eee; color: #999; text-decoration: line-through; }

.attn { display: flex; align-items: center; gap: 12px; padding: 11px 4px; border-bottom: 1px solid #f0e9e1; cursor: pointer; }
.attn:last-child { border-bottom: 0; }
.attn:hover { background: #fbf7f3; }
.attn .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); flex: none; }
.attn .dot.bad { background: var(--bad); }
.attn .dot.info { background: var(--info); }
.attn .dot.ok { background: var(--ok); }
.attn .grow { flex: 1; min-width: 0; }
.attn .t { font-weight: 600; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.ledger { width: 100%; }
.ledger td { padding: 5px 0; }
.ledger td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.ledger tr.total td { border-top: 2px solid var(--text); font-weight: 700; font-size: 17px; padding-top: 8px; }
.tl { list-style: none; padding: 0; margin: 0; }
.tl li { padding: 8px 0 8px 18px; border-left: 2px solid var(--line); position: relative; font-size: 14px; }
.tl li::before { content: ''; position: absolute; left: -6px; top: 13px; width: 10px; height: 10px; border-radius: 50%; background: var(--brand); }

/* ---------- editor ---------- */
.lines { width: 100%; border-collapse: collapse; }
.lines th { font-size: 12px; text-transform: uppercase; color: var(--muted); text-align: left; padding: 6px 4px; }
.lines td { padding: 4px; vertical-align: top; }
.lines td.amt { padding-top: 13px; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.lines .c-rate { width: 140px; } .lines .c-x { width: 40px; }
.lines textarea { min-height: 42px; }
.tot-box { margin-left: auto; width: min(340px, 100%); }
.tot-box .ledger tr.total td { font-size: 19px; }
.pricebook { max-height: 320px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.pricebook button { display: flex; width: 100%; justify-content: space-between; padding: 10px 12px; border: 0; border-bottom: 1px solid #f0e9e1; background: #fff; font: inherit; cursor: pointer; text-align: left; gap: 10px; }
.pricebook button:hover { background: #fbf7f3; }

/* ---------- modal / toast ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(30, 25, 20, 0.45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal { background: #fff; border-radius: 12px; width: min(560px, 100%); max-height: 92vh; overflow: auto; padding: 20px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25); }
.modal.wide { width: min(760px, 100%); }
.modal h3 { margin-bottom: 14px; }
.modal .foot { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--brand-dark); color: var(--cream); padding: 11px 18px; border-radius: 8px; z-index: 60; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); font-weight: 600; max-width: 90vw; }
.toast.err { background: var(--bad); color: #fff; }
.banner { background: #fff7e8; border: 1px solid #f0d9a8; color: #6d5214; padding: 10px 14px; border-radius: 8px; font-size: 14px; margin-bottom: 16px; }
.emailframe { width: 100%; height: 640px; border: 1px solid var(--line); border-radius: 8px; background: #f3eee8; }

/* ---------- printable document ---------- */
.doc { --brand: #c4621d; --brand-dark: #2b2622; position: relative; background: #fff; max-width: 820px; margin: 0 auto; padding: 36px 40px; border-radius: var(--radius); box-shadow: var(--shadow); color: #2b2622; overflow: hidden; }
.d-head { display: flex; justify-content: space-between; gap: 20px; flex-wrap: wrap; }
.d-co { display: flex; gap: 14px; align-items: center; }
.d-logo { width: 92px; height: auto; }
.d-coname { font-size: 19px; font-weight: 800; color: var(--brand-dark); margin-bottom: 4px; }
.d-muted { color: #7a7068; font-size: 13px; }
.d-title { text-align: right; }
.d-title h2 { font-size: 28px; letter-spacing: 1px; text-transform: uppercase; color: var(--brand); margin-bottom: 6px; }
.d-meta { margin-left: auto; font-size: 13px; border-collapse: collapse; }
.d-meta td { padding: 1px 0 1px 14px; }
.d-meta td:first-child { color: #7a7068; }
.d-meta td:last-child { font-weight: 700; text-align: right; }
.d-bar { height: 4px; background: linear-gradient(90deg, var(--brand), var(--brand-dark)); margin: 20px 0; border-radius: 2px; }
.d-parties { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; font-size: 14px; margin-bottom: 22px; }
.doc h4 { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--brand); margin-bottom: 5px; }
.d-lines { width: 100%; border-collapse: collapse; font-size: 14px; }
.d-lines th { background: var(--brand-dark); color: #f6ead7; text-align: left; padding: 8px 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; }
.d-lines td { padding: 9px 10px; border-bottom: 1px solid #eee6dd; }
.d-lines .n { text-align: right; white-space: nowrap; }
.d-totals { margin: 14px 0 0 auto; border-collapse: collapse; font-size: 14px; min-width: 300px; }
.d-totals td { padding: 5px 10px; }
.d-totals td:last-child { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.d-totals .d-grand td { font-weight: 800; font-size: 16px; border-top: 2px solid var(--brand-dark); padding-top: 8px; }
.d-totals .d-due td { background: var(--brand); color: #fff; font-size: 17px; }
.d-totals .d-dep td { color: var(--brand); font-weight: 700; }
.d-totals .d-pay td { color: #2f7d4f; }
.d-totals.d-wide { margin-top: 20px; }
.d-section { margin-top: 20px; font-size: 13px; color: #4a423c; }
.d-section p { margin: 0; line-height: 1.5; }
.d-signed { margin-top: 18px; padding: 10px 12px; background: #eef7f1; border-radius: 6px; font-size: 13px; color: #2f7d4f; }
.d-foot { margin-top: 30px; padding-top: 12px; border-top: 1px solid #eee6dd; text-align: center; font-size: 12px; color: #7a7068; }
.d-stamp { position: absolute; top: 120px; right: -10px; transform: rotate(-14deg); border: 4px solid #2f7d4f; color: #2f7d4f; font-weight: 900; font-size: 30px; letter-spacing: 3px; padding: 4px 18px; border-radius: 8px; opacity: 0.22; pointer-events: none; }
.d-receipt { text-align: center; padding: 10px 0 4px; }
.d-paid-amt { font-size: 40px; font-weight: 800; color: #2f7d4f; }

/* ---------- mobile ---------- */
@media (max-width: 900px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .cols { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .shell { display: block; }
  .side { display: none; }
  .topbar { display: flex; align-items: center; gap: 10px; background: var(--brand-dark); color: var(--cream); padding: 10px 16px; position: sticky; top: 0; z-index: 20; }
  .topbar img { width: 34px; }
  .topbar b { flex: 1; font-size: 15px; }
  main { padding: 16px 16px 96px; }
  h1 { font-size: 21px; }
  .tabbar { position: fixed; bottom: 0; left: 0; right: 0; background: #fff; border-top: 1px solid var(--line); display: grid; grid-template-columns: repeat(5, 1fr); z-index: 30; padding-bottom: env(safe-area-inset-bottom); }
  .tabbar a { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 7px; font-size: 11px; color: var(--muted); font-weight: 600; }
  .tabbar a.on { color: var(--brand); }
  .tabbar svg { width: 22px; height: 22px; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .search { max-width: none; }
  table.list .hide-m { display: none; }
  .card { padding: 14px; }
  /* line items become cards */
  .lines thead { display: none; }
  .lines, .lines tbody, .lines tr, .lines td { display: block; width: 100% !important; }
  .lines tr { display: grid; grid-template-columns: 1fr; gap: 6px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin-bottom: 10px; position: relative; }
  .lines td.c-desc { grid-column: 1 / -1; padding-right: 40px; }
  .lines td.c-x { position: absolute; top: 6px; right: 4px; width: auto !important; }
  .doc { padding: 20px 16px; }
  .d-title { text-align: left; }
  .d-meta { margin-left: 0; }
  .d-meta td { padding: 1px 14px 1px 0; }
  .d-parties { grid-template-columns: 1fr; }
  .d-totals { min-width: 0; width: 100%; }
  .d-logo { width: 64px; }
}
@media (min-width: 761px) { .tabbar { display: none; } }

@media print {
  body { background: #fff; }
  .side, .topbar, .tabbar, .no-print, .page-head, .banner { display: none !important; }
  .shell { display: block; }
  main { padding: 0; max-width: none; }
  .doc { box-shadow: none; padding: 0; max-width: none; }
  .d-lines th, .d-totals .d-due td, .d-bar { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
.attn svg { width: 20px; height: 20px; display: block; }
label.row:has(> input[type="checkbox"]) { flex-wrap: nowrap; align-items: flex-start; cursor: pointer; }
label.row > input[type="checkbox"] { margin-top: 3px; flex: none; }
@media (max-width: 760px) { .toast { bottom: calc(76px + env(safe-area-inset-bottom)); } }

/* ---------- schedule ---------- */
.cal-cols { grid-template-columns: 1.6fr 1fr; }
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-top: 1px solid var(--line); border-left: 1px solid var(--line); }
.cal-h { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--muted); padding: 6px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: #faf6f1; }
.cal-day { min-height: 92px; padding: 4px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); cursor: pointer; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.cal-day:hover { background: #fbf7f3; }
.cal-day.out { background: #faf8f6; }
.cal-day.out .cal-num { opacity: 0.4; }
.cal-num { font-size: 12px; font-weight: 600; color: var(--muted); }
.cal-day.today .cal-num { background: var(--brand); color: #fff; border-radius: 99px; width: 22px; height: 22px; display: grid; place-items: center; }
.cal-job { border: 0; background: color-mix(in srgb, var(--brand) 16%, #fff); color: var(--brand-dark); border-left: 3px solid var(--brand); border-radius: 4px; font: inherit; font-size: 12px; padding: 2px 5px; text-align: left; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-job.done { background: #e2f1e7; border-left-color: var(--ok); }
.tabbar { grid-template-columns: repeat(6, 1fr) !important; }
@media (max-width: 900px) { .cal-cols { grid-template-columns: 1fr; } }
@media (max-width: 760px) {
  .cal-day { min-height: 58px; padding: 2px; }
  .cal-job { font-size: 10px; padding: 1px 3px; border-left-width: 2px; }
  .cal-h { padding: 4px 2px; font-size: 10px; text-align: center; }
  .tabbar a { font-size: 10px; }
}

/* ---------- profit ---------- */
.neg { color: var(--bad); }
.year-pick { width: auto; margin-left: 6px; padding: 2px 6px; font-size: 14px; font-weight: 600; }
.ytd-grid { display: grid; grid-template-columns: 1fr 1.3fr; gap: 24px; align-items: start; }
.ytd-line { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px solid #f0e9e1; font-variant-numeric: tabular-nums; }
.ytd-line b { font-size: 17px; }
.ytd-line.total { border-bottom: 0; border-top: 2px solid var(--text); margin-top: 2px; }
.ytd-line.total b { font-size: 24px; }
.ytd-line.pos b { color: var(--ok); }
.ytd-line.neg b { color: var(--bad); }
.ytd-months { max-height: 260px; overflow: auto; }
.ytd-months td, .ytd-months th { padding: 7px 8px; }
@media (max-width: 760px) { .ytd-grid { grid-template-columns: 1fr; gap: 12px; } }
@media (max-width: 760px) { .ytd-months { overflow: visible; max-height: none; } .ytd-months table { font-size: 13px; } .ytd-months td, .ytd-months th { padding: 6px 3px; } .ytd-months th { font-size: 10px; letter-spacing: 0; white-space: normal; } }
