:root {
  --paper: #f4f1e8;
  --sheet: #fffef9;
  --ink: #111412;
  --muted: #62665f;
  --line: rgba(17, 20, 18, .28);
  --faint: rgba(17, 20, 18, .09);
  --cobalt: #1248c7;
  --ember: #ef563f;
  --acid: #8bd35a;
  --paid: #268d4d;
  --overdue: #d92d20;
  --display: "Arial Narrow", "Helvetica Neue", Arial, sans-serif;
  --ui: "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html { background: var(--paper); }
body { margin: 0; color: var(--ink); background-color: var(--paper); background-image: linear-gradient(var(--faint) 1px, transparent 1px), linear-gradient(90deg, var(--faint) 1px, transparent 1px); background-size: 72px 72px; font-family: var(--ui); }
button, input, textarea, label.button { font: 700 13px/1 var(--ui); letter-spacing: .025em; }
button, label.button { min-height: 46px; padding: 0 22px; border: 1.5px solid var(--ink); border-radius: 0; color: var(--ink); background: rgba(255,254,249,.72); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
button:hover, label.button:hover { transform: translate(-2px,-2px); box-shadow: 3px 3px 0 var(--ink); }
button:focus-visible, input:focus-visible, textarea:focus-visible, label.button:focus-visible { outline: 3px solid rgba(18,72,199,.28); outline-offset: 2px; }

.app-header { min-height: 92px; padding: 18px 28px; border-bottom: 2px solid var(--ink); background: rgba(244,241,232,.94); display: grid; grid-template-columns: 180px 1fr auto; align-items: center; gap: 24px; position: sticky; top: 0; z-index: 20; backdrop-filter: blur(12px); }
.brand { color: var(--ink); text-decoration: none; display: flex; align-items: center; gap: 15px; border-right: 1px solid var(--ink); }
.brand strong { font: 950 24px/.82 var(--display); letter-spacing: -.03em; }
.brand-mark { width: 34px; height: 34px; position: relative; flex: 0 0 34px; }
.brand-mark i { position: absolute; width: 22px; height: 22px; }
.brand-mark i:first-child { left: 0; top: 0; background: var(--ember); }
.brand-mark i:last-child { right: 0; bottom: 0; background: var(--cobalt); }
h1 { margin: 0; font: 950 clamp(35px, 4vw, 58px)/.82 var(--display); letter-spacing: -.045em; }
.app-header nav { display: flex; gap: 12px; }
.app-header nav .review-nav { color: var(--sheet); background: var(--ink); }

main { width: min(1580px, 100%); margin: 0 auto; border-inline: 1px solid var(--line); background: rgba(244,241,232,.92); }
.workspace { display: grid; grid-template-columns: minmax(560px,.98fr) minmax(620px,1.02fr); min-height: 750px; border-bottom: 2px solid var(--ink); }
.editor { min-width: 0; padding: 22px 28px 18px; border-right: 1px solid var(--ink); display: grid; grid-template-columns: 1fr 1fr; align-content: start; gap: 16px; }
.field { display: grid; gap: 7px; }
.field.wide, .line-items, .actions, .workflow-review, .status-message { grid-column: 1 / -1; }
.field label, legend, .line-head { font: 800 11px/1 var(--ui); letter-spacing: .06em; }
.field-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.field-heading legend { margin: 0; }
.review-link { min-height: 24px; padding: 0; border: 0; color: var(--cobalt); background: transparent; font-size: 9px; text-decoration: underline; box-shadow: none !important; transform: none !important; }
input, textarea { width: 100%; border: 1px solid var(--line); border-radius: 0; color: var(--ink); background: rgba(255,254,249,.76); padding: 10px 12px; font: 500 14px/1.35 var(--ui); }
textarea { resize: vertical; }
.file-control { min-height: 42px; padding: 7px 10px; border: 1px dashed var(--line); display: flex; align-items: center; gap: 10px; background: rgba(255,254,249,.52); }
.file-control span { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.file-control label, .file-control button { min-height: auto; padding: 5px; border: 0; background: transparent; text-decoration: underline; font-size: 11px; }
.line-items { min-width: 0; padding: 4px 0 0; border: 0; margin: 0; }
legend { margin-bottom: 12px; }
.line-head, .line-row { display: grid; grid-template-columns: minmax(180px,2fr) .55fr .7fr .6fr .8fr 32px; gap: 9px; align-items: center; }
.line-head { margin-bottom: 7px; }
.line-row { margin-bottom: 8px; }
.line-row input { padding: 9px; min-width: 0; }
.line-row output { text-align: right; font: 700 13px/1 var(--ui); }
.remove-line { min-height: 34px; width: 32px; padding: 0; border: 0; background: transparent; font-size: 22px; }
.small-button { min-height: 36px; padding: 0 13px; font-size: 11px; }
.notes-field { grid-column: 1 / 2; }
.totals { grid-column: 2 / 3; margin: 2px 0 0; padding-top: 3px; border-top: 1px solid var(--line); }
.totals div { display: flex; justify-content: space-between; padding: 7px 10px; font-size: 13px; }
.totals dt, .totals dd { margin: 0; }
.totals .grand { border-top: 1.5px solid var(--ink); font: 900 19px/1 var(--display); }
.actions { display: grid; grid-template-columns: 1fr 1.18fr 1fr; gap: 14px; margin-top: 2px; }
.actions .primary { background: var(--acid); }
.workflow-review { min-height: 34px; padding: 0; border: 0; color: var(--cobalt); background: transparent; text-decoration: underline; box-shadow: none !important; transform: none !important; justify-self: start; font-size: 11px; }
.status-message { min-height: 18px; margin: -4px 0 0; color: var(--muted); font-size: 12px; }

.invoice-preview { min-width: 0; margin: 32px; min-height: 680px; padding: 42px 38px; background: var(--sheet); border: 1px dashed var(--line); box-shadow: 0 18px 50px rgba(17,20,18,.08); }
.preview-top { min-width: 0; display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 24px; }
.preview-brand { display: flex; gap: 14px; align-items: flex-start; }
.preview-logo { width: 58px; height: 58px; object-fit: contain; border: 1px solid var(--line); }
.preview-brand strong { font: 900 20px/.95 var(--display); }
.preview-brand p, .bill-to p, .preview-notes p { margin: 7px 0 0; white-space: pre-line; font-size: 12px; line-height: 1.35; }
.invoice-title { min-width: 0; }
.invoice-title h2 { margin: 0; font: 950 clamp(48px,5vw,72px)/.8 var(--display); letter-spacing: -.05em; }
.invoice-meta { margin-top: 20px; display: grid; grid-template-columns: auto auto; gap: 9px 18px; font-size: 11px; }
.invoice-meta dt, .invoice-meta dd { margin: 0; }
.invoice-meta dt { font-weight: 800; }
.bill-to { margin-top: 44px; }
.bill-to h3, .preview-notes h3 { margin: 0; font-size: 11px; letter-spacing: .06em; }
.preview-table { width: 100%; table-layout: fixed; margin-top: 25px; border-collapse: collapse; font-size: 11px; }
.preview-table th:first-child { width: 45%; }
.preview-table th { padding: 10px; color: var(--sheet); background: var(--ink); text-align: left; }
.preview-table td { padding: 9px 10px; border: 1px solid var(--line); }
.preview-table th:not(:first-child), .preview-table td:not(:first-child) { text-align: right; }
.preview-summary { width: min(310px, 55%); margin: 18px 0 0 auto; }
.preview-summary div { display: flex; justify-content: space-between; padding: 7px 4px; font-size: 12px; }
.preview-summary .preview-grand { border-top: 1.5px solid var(--ink); font: 900 20px/1 var(--display); }
.preview-notes { margin-top: 35px; display: grid; grid-template-columns: 1fr auto; gap: 20px; align-items: end; }
.preview-thanks { text-align: right; font-size: 11px; }

.ledger { padding: 16px 22px 24px; background: rgba(255,254,249,.7); }
.ledger-head { display: flex; justify-content: space-between; align-items: baseline; gap: 20px; }
.ledger h2 { margin: 0 0 12px; font: 900 18px/1 var(--display); }
.ledger-head span, .local-note { color: var(--muted); font-size: 11px; }
.ledger-table-wrap { overflow-x: auto; border-top: 1px solid var(--ink); }
.ledger table { width: 100%; border-collapse: collapse; min-width: 880px; font-size: 12px; }
.ledger th, .ledger td { padding: 11px 13px; border-bottom: 1px solid var(--line); text-align: left; }
.ledger th { font-size: 10px; letter-spacing: .05em; }
.status-select { min-height: 30px; padding: 0 8px; border: 1px solid currentColor; background: transparent; font: 800 11px/1 var(--ui); }
.status-paid { color: var(--paid); }
.status-unpaid { color: var(--ember); }
.status-overdue { color: var(--overdue); }
.row-actions { display: flex; gap: 8px; }
.row-actions button { min-height: 30px; padding: 0 10px; font-size: 10px; }
.local-note { text-align: right; margin: 16px 0 0; }
.empty-ledger { padding: 26px 13px; color: var(--muted); }

.review-layer[hidden] { display: none; }
.review-layer { position: fixed; inset: 0; z-index: 60; display: flex; justify-content: flex-end; }
.review-scrim { position: absolute; inset: 0; width: 100%; height: 100%; min-height: 0; padding: 0; border: 0; background: rgba(17,20,18,.45); box-shadow: none !important; transform: none !important; }
.review-drawer { position: relative; width: min(540px,100%); height: 100%; overflow: auto; padding: 26px 30px 34px; color: var(--ink); background: var(--paper); border-left: 2px solid var(--ink); box-shadow: -22px 0 70px rgba(17,20,18,.25); }
.review-drawer > header { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; padding-bottom: 18px; border-bottom: 1px solid var(--ink); }
.review-drawer h2 { margin: 0; font: 950 34px/.9 var(--display); letter-spacing: -.03em; }
.review-drawer header p { margin: 7px 0 0; color: var(--cobalt); font-size: 11px; font-weight: 800; }
.review-close { width: 38px; min-height: 38px; padding: 0; font-size: 26px; }
.review-intro { margin: 18px 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
#reviewForm { display: grid; gap: 15px; }
#reviewForm label { display: grid; gap: 7px; font: 800 11px/1.3 var(--ui); letter-spacing: .03em; }
#reviewForm input, #reviewForm textarea, #reviewForm select { width: 100%; min-height: 43px; padding: 10px 11px; border: 1px solid var(--line); color: var(--ink); background: var(--sheet); font: 500 14px/1.35 var(--ui); }
#reviewForm textarea { resize: vertical; }
.review-actions { display: grid; grid-template-columns: 1.35fr .8fr 1fr; gap: 9px; margin-top: 22px; }
.review-actions button:first-child { color: var(--sheet); background: var(--ink); }
.review-message { margin: 14px 0 0; color: var(--cobalt); font-size: 11px; line-height: 1.4; }

@media (max-width: 1600px) {
  .app-header { grid-template-columns: 150px minmax(300px,1fr) auto; gap: 16px; }
  .app-header nav { grid-column: auto; gap: 8px; }
  .app-header nav button, .app-header nav label { padding-inline: 14px; font-size: 11px; }
}

@media (max-width: 1240px) {
  .app-header { grid-template-columns: 150px 1fr; }
  .app-header nav { grid-column: 1 / -1; }
  .workspace { grid-template-columns: 1fr; }
  .editor { border-right: 0; border-bottom: 1px solid var(--ink); }
  .invoice-preview { min-height: 760px; }
}

@media (max-width: 1760px) and (min-width: 1601px) {
  .app-header { grid-template-columns: 150px minmax(300px,1fr) auto; gap: 16px; }
  .app-header nav { gap: 8px; }
  .app-header nav button, .app-header nav label { padding-inline: 14px; font-size: 11px; }
  .editor { padding-inline: 22px; }
  .invoice-preview { margin: 26px; padding: 36px 30px; }
}

@media (max-width: 640px) {
  .app-header { position: static; grid-template-columns: 1fr; padding: 17px 16px; gap: 15px; }
  .brand { width: fit-content; border-right: 0; }
  h1 { font-size: 44px; }
  .app-header nav { display: grid; grid-template-columns: repeat(2,1fr); width: 100%; }
  .app-header nav button, .app-header nav label { min-height: 42px; padding: 0 8px; font-size: 10px; }
  .editor { padding: 18px 14px; grid-template-columns: 1fr; }
  .field, .notes-field, .totals { grid-column: 1 / -1; }
  .line-head { display: none; }
  .line-row { grid-template-columns: 1fr 1fr 1fr 32px; padding: 12px 0; border-bottom: 1px solid var(--line); }
  .line-row .description { grid-column: 1 / -1; }
  .line-row .amount { grid-column: 1 / 4; text-align: left; }
  .line-row .remove-line { grid-column: 4; grid-row: 2; }
  .actions { grid-template-columns: 1fr; }
  .invoice-preview { margin: 18px 10px; padding: 25px 18px; min-height: 0; }
  .preview-top { grid-template-columns: 1fr; }
  .invoice-title h2 { font-size: 58px; }
  .preview-notes { grid-template-columns: 1fr; }
  .preview-thanks { text-align: left; }
  .review-drawer { padding: 21px 17px 30px; border-left: 0; }
  .review-actions { grid-template-columns: 1fr; }
}

@media print {
  body { background: white; }
  .app-header, .editor, .ledger, .review-layer { display: none !important; }
  main, .workspace { display: block; width: auto; border: 0; background: white; }
  .invoice-preview { margin: 0; min-height: 0; border: 0; box-shadow: none; page-break-inside: avoid; }
  @page { size: letter; margin: 12mm; }
}
