:root {
  --ink: #111111;
  --muted: #6b7280;
  --line: #dedede;
  --soft: #f6f7f8;
  --panel: #ffffff;
  --accent: #202020;
  --good: #0f766e;
  --warn: #a16207;
  --bad: #b91c1c;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: #f2f3f4;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; }

.app-shell { min-height: 100vh; }

.top-nav {
  height: 68px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 28px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.top-nav span { display: block; color: var(--muted); font-size: 13px; }

.logo-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  background: #111;
  color: #fff;
  font-weight: 800;
  font-size: 13px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(420px, .85fr);
  gap: 22px;
  padding: 24px;
  align-items: start;
}

.dashboard, .preview-pane, .invoice-form {
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 22px;
}

.toolbar, .form-head, .section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

h1, h2, p { margin: 0; }
h1 { font-size: 28px; font-weight: 750; }
h2 { font-size: 18px; }
.eyebrow, .label { color: var(--muted); text-transform: uppercase; font-size: 11px; font-weight: 700; letter-spacing: .08em; }

.btn {
  border: 1px solid var(--accent);
  padding: 10px 14px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  min-height: 40px;
}

.btn.primary { background: var(--accent); color: #fff; }
.btn.secondary { border-color: var(--line); }

.cards {
  display: grid;
  grid-template-columns: repeat(5, minmax(130px, 1fr));
  gap: 12px;
  margin: 22px 0;
}

.card {
  border: 1px solid var(--line);
  background: #fff;
  padding: 16px;
}

.card span { display: block; color: var(--muted); font-size: 12px; margin-bottom: 10px; }
.card strong { font-size: 22px; }

.filters {
  display: grid;
  grid-template-columns: 1fr 170px 180px;
  gap: 10px;
  margin-bottom: 22px;
}

.whatsapp-panel {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  border: 1px solid var(--line);
  background: #fbfbfb;
  padding: 16px;
  margin-bottom: 22px;
}

.whatsapp-panel textarea {
  min-height: 112px;
  background: #fff;
}

.whatsapp-panel .btn {
  justify-self: end;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  padding: 10px 11px;
  color: var(--ink);
}

textarea { min-height: 78px; resize: vertical; }

.section-title { margin-bottom: 10px; }
.section-title p { color: var(--muted); font-size: 13px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); }
.app-table, .invoice-lines { width: 100%; border-collapse: collapse; }
.app-table th, .app-table td, .invoice-lines th, .invoice-lines td { border-bottom: 1px solid var(--line); padding: 12px; text-align: left; vertical-align: top; }
.app-table th, .invoice-lines th { font-size: 12px; color: #444; background: var(--soft); font-weight: 750; }
.app-table td { font-size: 14px; }
.empty { text-align: center; color: var(--muted); padding: 28px !important; }

.link-btn {
  border: 0;
  background: transparent;
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
  font-weight: 700;
}

.row-actions { display: flex; flex-wrap: wrap; gap: 6px; min-width: 180px; }
.row-actions button {
  border: 1px solid var(--line);
  background: #fff;
  padding: 6px 8px;
  cursor: pointer;
}

.status {
  display: inline-block;
  border: 1px solid var(--line);
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 700;
  background: #fff;
}
.status.paid { color: var(--good); border-color: #99d3ca; }
.status.unpaid { color: #374151; }
.status.partially-paid { color: var(--warn); border-color: #e4c36d; }
.status.overdue { color: var(--bad); border-color: #efb0b0; }

.preview-pane {
  position: sticky;
  top: 18px;
}

.export-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-bottom: 14px;
}

.invoice-paper {
  background: #fff;
  border: 1px solid #cfcfcf;
  padding: 34px;
  max-width: 780px;
  margin: 0 auto;
  color: #151515;
}

.invoice-head {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  border-bottom: 2px solid #111;
  padding-bottom: 22px;
}

.invoice-head h1 { font-size: 42px; }
.tax-title { margin-top: 4px; font-size: 17px; font-weight: 650; }

.brand-block {
  max-width: 330px;
  text-align: right;
  color: #333;
  line-height: 1.45;
}
.brand-block span { display: block; }

.invoice-logo {
  display: block;
  width: 210px;
  max-width: 100%;
  height: auto;
  margin: 0 0 10px auto;
  object-fit: contain;
}

.invoice-grid {
  display: grid;
  grid-template-columns: 1fr 270px;
  gap: 28px;
  margin: 28px 0;
}

.invoice-grid p, .bank-due p, .invoice-footer p { line-height: 1.45; color: #333; }

.details-table {
  border-top: 1px solid var(--line);
}
.details-table p {
  display: grid;
  grid-template-columns: 120px 1fr;
  border-bottom: 1px solid var(--line);
  padding: 8px 0;
  gap: 12px;
}
.details-table span { color: var(--muted); }

.bank-due {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  border: 1px solid var(--line);
  padding: 16px;
  margin-top: 22px;
  margin-bottom: 0;
}
.bank-due > div:last-child { text-align: right; }
.bank-due strong { font-size: 18px; }

.invoice-lines th:nth-child(n+2), .invoice-lines td:nth-child(n+2) { text-align: right; }
.invoice-lines td:first-child { width: 44%; }

.totals {
  width: min(320px, 100%);
  margin-left: auto;
  margin-top: 18px;
}
.totals p, .form-totals p {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  padding: 8px 0;
}
.totals .strong, .form-totals .strong {
  border-top: 2px solid #111;
  font-size: 18px;
}

.invoice-footer {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 18px;
  border-top: 1px solid var(--line);
  margin-top: 34px;
  padding-top: 18px;
  font-size: 13px;
}

.invoice-form {
  max-width: 1120px;
  margin: 24px auto;
}

.form-actions { display: flex; gap: 10px; }

fieldset {
  border: 1px solid var(--line);
  margin: 20px 0 0;
  padding: 18px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
}

legend { padding: 0 8px; font-weight: 750; }
label { display: grid; gap: 7px; color: #333; font-size: 13px; }
.check { display: flex; align-items: center; gap: 10px; align-self: end; }
.check input { width: auto; }

.items-fieldset { grid-template-columns: 1fr; }
.items { display: grid; gap: 10px; }
.item-row {
  display: grid;
  grid-template-columns: 2fr .55fr .8fr .75fr auto;
  gap: 10px;
  align-items: end;
  border-bottom: 1px solid var(--line);
  padding-bottom: 12px;
}
.item-row button {
  border: 1px solid var(--line);
  background: #fff;
  padding: 10px;
  cursor: pointer;
}

.form-totals {
  margin-left: auto;
  margin-top: 18px;
  width: min(380px, 100%);
  border: 1px solid var(--line);
  padding: 14px;
}

@media (max-width: 1180px) {
  .workspace { grid-template-columns: 1fr; }
  .preview-pane { position: static; }
  .cards { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 760px) {
  .top-nav { padding: 0 16px; }
  .workspace, .dashboard, .preview-pane, .invoice-form { padding: 14px; }
  .toolbar, .form-head, .invoice-head, .bank-due { align-items: flex-start; flex-direction: column; }
  .brand-block, .bank-due > div:last-child { text-align: left; }
  .invoice-logo { margin-left: 0; width: 185px; }
  .filters, .invoice-grid, fieldset, .item-row, .invoice-footer { grid-template-columns: 1fr; }
  .whatsapp-panel .btn { justify-self: stretch; }
  .cards { grid-template-columns: 1fr; }
  .invoice-paper { padding: 20px; }
  .invoice-head h1 { font-size: 34px; }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 8mm;
  }

  html, body {
    width: 210mm;
    min-height: 297mm;
    background: #fff;
  }

  .top-nav, .dashboard, .export-actions { display: none !important; }
  .workspace {
    display: block;
    padding: 0;
  }

  .preview-pane {
    display: block;
    border: 0;
    padding: 0;
    position: static;
  }

  .invoice-paper {
    width: 194mm;
    max-width: none;
    min-height: auto;
    border: 0;
    padding: 8mm 9mm;
    font-size: 10.5px;
    line-height: 1.25;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .invoice-head {
    gap: 12mm;
    padding-bottom: 6mm;
  }

  .invoice-head h1 { font-size: 30px; }
  .tax-title { font-size: 13px; }
  .invoice-logo { width: 142px; margin-bottom: 4px; }
  .brand-block { max-width: 250px; line-height: 1.25; }

  .invoice-grid {
    grid-template-columns: 1fr 68mm;
    gap: 10mm;
    margin: 7mm 0;
  }

  .details-table p {
    grid-template-columns: 28mm 1fr;
    padding: 2.2mm 0;
  }

  .bank-due {
    padding: 4mm;
    margin-top: 5mm;
    margin-bottom: 0;
  }

  .invoice-lines th,
  .invoice-lines td {
    padding: 3mm;
  }

  .totals {
    width: 72mm;
    margin-top: 5mm;
  }

  .totals p {
    padding: 2mm 0;
  }

  .totals .strong {
    font-size: 13px;
  }

  .invoice-footer {
    gap: 8mm;
    margin-top: 7mm;
    padding-top: 4mm;
    font-size: 9.5px;
  }

  .invoice-head,
  .bank-due,
  .invoice-lines,
  .totals,
  .invoice-footer {
    page-break-inside: avoid;
    break-inside: avoid;
  }
}
