/* Invoicing app. No frameworks, system fonts. */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2230;
  --muted: #6b7385;
  --border: #e3e6ec;
  --border-strong: #cfd4dd;
  --primary: #2f5bea;
  --primary-dark: #2448c4;
  --primary-soft: #eaf0ff;
  --good: #16803c;
  --good-soft: #e5f6eb;
  --warn: #b45309;
  --warn-soft: #fdf1e1;
  --bad: #c42b2b;
  --bad-soft: #fde8e8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 var(--font); color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; margin: 0; line-height: 1.25; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
h2 { font-size: 1.08rem; margin: 0 0 .8rem; }
h3 { font-size: .98rem; margin: .5rem 0; }
p { margin: 0 0 .8rem; }
img { max-width: 100%; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.strong { font-weight: 600; }
.nowrap { white-space: nowrap; }
.text-good { color: var(--good); }
.text-warn { color: var(--warn); }
.text-bad { color: var(--bad); }
.hint { display: block; color: var(--muted); font-size: .8rem; font-weight: 400; margin-top: .2rem; }
span.hint { display: inline; }
label > .hint { display: block; }
.req { color: var(--bad); }

/* ---------------------------------------------------------------- layout */
.topbar { background: #151b2c; color: #fff; }
.topbar-inner { max-width: 1180px; margin: 0 auto; padding: .65rem 16px; display: flex; align-items: center; gap: 1.2rem; flex-wrap: wrap; }
.brand { color: #fff; font-weight: 700; font-size: 1.02rem; display: flex; align-items: center; gap: .5rem; }
.brand:hover { text-decoration: none; }
.brand-logo { height: 28px; width: auto; background: #fff; border-radius: 4px; padding: 2px; }
.nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.nav a { color: #c5cbe0; padding: .35rem .7rem; border-radius: 6px; }
.nav a:hover { color: #fff; background: rgba(255,255,255,.08); text-decoration: none; }
.nav a.active { color: #fff; background: rgba(255,255,255,.14); }
.portal-client { flex: 1; color: #c5cbe0; }
.logout { margin: 0; }
.topbar .link-btn { color: #c5cbe0; }
.container { max-width: 1180px; margin: 0 auto; padding: 1.5rem 16px 4rem; }

.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; margin-bottom: 1.2rem; flex-wrap: wrap; }
.page-head p { margin: .3rem 0 0; }
.crumbs { font-size: .85rem; color: var(--muted); margin-bottom: .2rem; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.2rem; margin-bottom: 1.2rem; box-shadow: var(--shadow); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: .8rem; }
.card-head h2 { margin: 0; }
.card.empty { text-align: center; padding: 2rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; align-items: start; }
.grid-2 > .card { margin-bottom: 0; }
.grid-2 + * { margin-top: 1.2rem; }

/* ---------------------------------------------------------------- stats */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1.2rem; }
.stats-4 { grid-template-columns: repeat(4, 1fr); }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.stat-label { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.stat-value { font-size: 1.45rem; font-weight: 700; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.stat-highlight { border-color: var(--primary); background: var(--primary-soft); }

/* ---------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: .55rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fafbfc; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table-compact td { padding: .4rem .5rem; }
.is-archived { opacity: .6; }
.check-cell { width: 28px; }

/* ---------------------------------------------------------------- badges */
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 99px; font-size: .75rem; font-weight: 600; white-space: nowrap; background: #eef0f4; color: #4b5263; vertical-align: middle; }
.badge-unbilled { background: #eef0f4; color: #4b5263; }
.badge-draft { background: #f1ecfd; color: #6941c6; }
.badge-invoiced, .badge-unpaid { background: var(--warn-soft); color: var(--warn); }
.badge-partial { background: #fff6d6; color: #946200; }
.badge-paid { background: var(--good-soft); color: var(--good); }
.badge-overdue { background: var(--bad-soft); color: var(--bad); }
.badge-muted { background: #eef0f4; color: var(--muted); }

/* ---------------------------------------------------------------- tabs */
.tabs { display: flex; gap: .25rem; flex-wrap: wrap; }
.tabs a { padding: .3rem .7rem; border-radius: 99px; color: var(--muted); font-size: .88rem; }
.tabs a:hover { background: var(--bg); text-decoration: none; }
.tabs a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

/* ---------------------------------------------------------------- forms */
label { display: block; font-weight: 600; font-size: .88rem; }
.label { display: block; font-weight: 600; font-size: .88rem; }
input, select, textarea { display: block; width: 100%; margin-top: .3rem; padding: .55rem .7rem; font: inherit; font-weight: 400; color: var(--text); background: #fff; border: 1px solid var(--border-strong); border-radius: 8px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
input[disabled] { background: var(--bg); color: var(--muted); }
input[type=checkbox], input[type=radio] { display: inline-block; width: auto; margin: 0 .35rem 0 0; vertical-align: middle; accent-color: var(--primary); }
input[type=file] { padding: .4rem; }
textarea { resize: vertical; }
label.check { font-weight: 500; display: inline-flex; align-items: center; cursor: pointer; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem 1.2rem; }
.form-grid h2 { margin: 0; }
.span-2 { grid-column: span 2; }
.stack > * + * { margin-top: .9rem; }
.form-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-top: 1rem; }
.form-grid .form-actions { margin-top: 0; }
.inline-form { display: flex; gap: .5rem; align-items: flex-end; margin: 1rem 0 .3rem; }
.inline-form label { flex: 1; }
.inline { display: inline; margin: 0; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.due-row { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.danger-zone { margin-top: 1rem; }
.copy-row { display: flex; gap: .5rem; align-items: center; margin-bottom: 1rem; }
.copy-row input { margin: 0; font-size: .85rem; }
.logo-row { display: flex; gap: 1.2rem; align-items: center; flex-wrap: wrap; }
.logo-preview { max-height: 60px; border: 1px solid var(--border); border-radius: 6px; padding: 4px; background: #fff; }

/* cost entry */
.cost-fields { display: grid; grid-template-columns: 150px 1fr auto 110px 130px 130px; gap: .8rem; align-items: end; }
.f-desc { min-width: 0; }
.f-total strong { display: block; padding: .55rem 0; font-variant-numeric: tabular-nums; }
.segmented { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; margin-top: .3rem; }
.segmented label { font-weight: 500; padding: .5rem .75rem; cursor: pointer; display: flex; align-items: center; border-right: 1px solid var(--border-strong); }
.segmented label:last-child { border-right: 0; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { background: var(--primary-soft); color: var(--primary); }
.quick-add h2 { margin-bottom: .8rem; }
[data-cost-form][data-type="fixed"] [data-when="hourly"],
[data-cost-form][data-type="hourly"] [data-when="fixed"] { display: none; }

/* invoice builder */
.cost-group { border: 1px solid var(--border); border-radius: 8px; margin-bottom: .9rem; overflow: hidden; }
.cost-group-head { display: flex; justify-content: space-between; align-items: center; padding: .6rem .8rem; background: #f8f9fb; border-bottom: 1px solid var(--border); gap: .8rem; flex-wrap: wrap; }
.selected-total { text-align: right; font-size: 1.05rem; padding-top: .4rem; }
.sticky-actions { position: sticky; bottom: 0; background: var(--bg); padding: .8rem 0; border-top: 1px solid var(--border); }
.preview-actions { margin: 0 0 1rem; }
.spacer { flex: 1; }
.pick-list { display: grid; gap: .5rem; }
.pick { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .8rem 1rem; border: 1px solid var(--border); border-radius: 8px; color: var(--text); }
.pick:hover { border-color: var(--primary); background: var(--primary-soft); text-decoration: none; }
.pick .muted { margin-left: .4rem; }
.pick-amount { font-weight: 600; white-space: nowrap; }

/* invoice page */
.invoice-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 1.2rem; align-items: start; }
.doc-frame { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
.details { display: grid; grid-template-columns: auto 1fr; gap: .35rem 1rem; margin: 0 0 1rem; }
.details dt { color: var(--muted); font-size: .88rem; }
.details dd { margin: 0; }
.details.compact dd { text-align: right; font-variant-numeric: tabular-nums; }
.payment-list { list-style: none; margin: 0 0 1rem; padding: 0; border-top: 1px solid var(--border); }
.payment-list li { display: flex; justify-content: space-between; gap: .8rem; padding: .6rem 0; border-bottom: 1px solid var(--border); }
.payment-form { border-top: 1px solid var(--border); padding-top: .6rem; }

/* ---------------------------------------------------------------- buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .3rem; padding: .5rem .95rem; font: inherit; font-weight: 600; font-size: .9rem; color: var(--text); background: #fff; border: 1px solid var(--border-strong); border-radius: 8px; cursor: pointer; white-space: nowrap; line-height: 1.3; }
.btn:hover { background: #f3f4f7; text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-sm { padding: .32rem .7rem; font-size: .83rem; }
.btn-block { width: 100%; }
.btn-danger-outline { color: var(--bad); border-color: #efb8b8; }
.btn-danger-outline:hover { background: var(--bad-soft); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.link-btn { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; }
.link-btn:hover { text-decoration: underline; }
.new-invoice { margin-left: auto; }

/* ---------------------------------------------------------------- flash */
.flash { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid; }
.flash-success { background: var(--good-soft); border-color: #b5e2c3; color: #0f5a2a; }
.flash-error { background: var(--bad-soft); border-color: #f2b9b9; color: #8f1d1d; }
.flash-info { background: var(--primary-soft); border-color: #c7d5fb; color: #1e3a8a; }

/* ---------------------------------------------------------------- plain pages (login, PIN) */
body.plain { display: flex; min-height: 100vh; align-items: center; justify-content: center; padding: 16px; }
.plain-box { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 2rem; box-shadow: 0 8px 30px rgba(20, 30, 50, .08); }
.plain-box h1 { font-size: 1.35rem; margin-bottom: .4rem; }
.plain-logo { max-height: 56px; margin-bottom: 1rem; }
.pin-input { font-size: 1.4rem; letter-spacing: .4em; text-align: center; }
.portal-footer { margin-top: 2rem; text-align: center; }

/* ---------------------------------------------------------------- invoice document */
.invoice-doc { background: #fff; color: #1a1f2b; padding: 44px 48px; max-width: 820px; margin: 0 auto; font-size: 13.5px; line-height: 1.5; }
.inv-head { display: flex; justify-content: space-between; gap: 2rem; margin-bottom: 32px; }
.inv-logo { max-height: 64px; max-width: 220px; margin-bottom: 10px; display: block; }
.inv-biz-name { font-size: 17px; font-weight: 700; margin-bottom: 4px; }
.inv-biz { color: #555d6e; font-size: 12.5px; margin-bottom: 4px; }
.inv-meta { text-align: right; }
.inv-title { font-size: 30px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase; color: var(--primary); margin-bottom: 8px; }
.inv-meta-table { margin-left: auto; border-collapse: collapse; }
.inv-meta-table th { text-align: left; font-weight: 500; color: #6b7385; padding: 2px 16px 2px 0; font-size: 12.5px; }
.inv-meta-table td { text-align: right; padding: 2px 0; }
.draft-mark { color: #6941c6; font-weight: 700; letter-spacing: .08em; }
.inv-to { margin-bottom: 26px; }
.inv-label { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: #6b7385; margin-bottom: 4px; }
.inv-lines { width: 100%; border-collapse: collapse; }
.inv-lines th { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: #6b7385; text-align: left; padding: 8px 8px; border-bottom: 2px solid #1a1f2b; }
.inv-lines td { padding: 8px; border-bottom: 1px solid #e6e8ee; vertical-align: top; }
.inv-lines .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.inv-lines .c-date { white-space: nowrap; width: 95px; }
.inv-lines .c-qty { width: 60px; }
.inv-lines .c-rate { width: 95px; }
.inv-lines .c-amt { width: 110px; }
.inv-project td { font-weight: 700; background: #f5f6f9; padding-top: 7px; padding-bottom: 7px; border-bottom: 1px solid #e6e8ee; }
.inv-end-client { font-weight: 400; color: #6b7385; }
.inv-subtotal td { font-weight: 600; color: #444b5a; border-bottom: 1px solid #c9ced8; }
.inv-subtotal td:first-child { text-align: right; }
.inv-totals { margin: 16px 0 0 auto; border-collapse: collapse; min-width: 280px; }
.inv-totals th { text-align: left; font-weight: 500; padding: 5px 24px 5px 0; }
.inv-totals td { text-align: right; padding: 5px 8px; font-variant-numeric: tabular-nums; }
.inv-grand th, .inv-grand td { font-size: 16px; font-weight: 700; border-top: 2px solid #1a1f2b; padding-top: 9px; }
.inv-balance th, .inv-balance td { font-weight: 700; color: var(--primary); }
.inv-block { margin-top: 26px; font-size: 12.5px; color: #3a4150; page-break-inside: avoid; }

/* print page */
body.print-page { background: #e9ebef; }
.print-toolbar { max-width: 820px; margin: 16px auto; padding: 0 16px; display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
body.print-page .invoice-doc { box-shadow: 0 2px 16px rgba(0,0,0,.08); margin-bottom: 40px; }

@page { size: A4; margin: 14mm 12mm; }
@media print {
  body, body.print-page { background: #fff; }
  .no-print, .print-toolbar, .topbar { display: none !important; }
  .invoice-doc { padding: 0; max-width: none; box-shadow: none !important; margin: 0; font-size: 12px; }
  .inv-lines tr { page-break-inside: avoid; }
  .inv-lines thead { display: table-header-group; }
  a { color: inherit; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---------------------------------------------------------------- responsive */
@media (max-width: 960px) {
  .invoice-layout { grid-template-columns: 1fr; }
  .cost-fields { grid-template-columns: 1fr 1fr; }
  .f-desc { grid-column: span 2; }
  .stats-4 { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 720px) {
  .grid-2, .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .stats { grid-template-columns: 1fr; }
  .stats-4 { grid-template-columns: 1fr 1fr; }
  .topbar-inner { gap: .6rem; }
  .nav { order: 3; flex-basis: 100%; }
  .invoice-doc { padding: 24px 18px; }
  .inv-head { flex-direction: column; gap: 1rem; }
  .inv-meta { text-align: left; }
  .inv-meta-table { margin-left: 0; }
  h1 { font-size: 1.35rem; }
}
