:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { font-weight: 700; font-size: 16px; }

main { max-width: 960px; margin: 20px auto; padding: 0 20px; }

h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 18px; margin: 24px 0 12px; }
h3 { font-size: 12px; margin: 16px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 16px;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }

button, .btn {
  font: inherit;
  padding: 7px 14px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
}
button:hover { background: #f3f4f6; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
button.danger { color: var(--danger); }
button.danger:hover { background: #fef2f2; }
button.link {
  background: none;
  border: 0;
  padding: 4px 6px;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
  font-size: inherit;
}
button.link:hover { background: transparent; color: var(--accent-hover); }
button.link.danger { color: var(--danger); }
button.wide { width: 100%; padding: 10px; font-weight: 600; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.row { display: flex; gap: 8px; align-items: center; }
.row.gap { gap: 12px; }

/* ---- Auth / company setup card ---- */
.auth-card {
  max-width: 440px;
  margin: 48px auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  box-shadow: 0 4px 20px rgba(17, 24, 39, 0.05);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-card.wide { max-width: 720px; }
.auth-card h1 { margin: 0; }
.auth-card p { margin: 0; }
.auth-card label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}
.auth-card input, .auth-card textarea {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}
.auth-card input:focus, .auth-card textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.auth-card textarea { min-height: 60px; resize: vertical; }
.auth-card .inline-action { margin-top: 4px; }

/* ---- Generic grid used in company form + invoice card ---- */
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
}
.grid .full { grid-column: 1 / -1; }
.grid label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}
.grid input, .grid textarea, .grid select {
  font: inherit;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}
.grid input:focus, .grid textarea:focus, .grid select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.grid textarea { min-height: 60px; resize: vertical; }

/* ---- Company-lookup autocomplete (buyer picker) ---- */
.company-search { position: relative; }
.company-suggest {
  position: absolute; left: 0; right: 0; top: 100%;
  margin-top: 2px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  max-height: 260px; overflow-y: auto;
  z-index: 20;
}
.company-suggest[hidden] { display: none; }
.company-suggest-item {
  display: block; width: 100%;
  border: 0; border-bottom: 1px solid #f3f4f6;
  background: none; padding: 8px 10px;
  text-align: left; cursor: pointer;
}
.company-suggest-item:last-child { border-bottom: 0; }
.company-suggest-item:hover { background: #f3f4f6; }
.company-suggest-name { font-weight: 600; font-size: 13px; }
.company-suggest-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.company-suggest-empty { padding: 8px 10px; font-size: 13px; color: var(--muted); }

/* ---- Home (invoice creation + history) ---- */
.home h1 { margin-top: 0; }

/* Lines editor */
.lines-head, .line-row {
  display: grid;
  grid-template-columns: 1fr 80px 110px 110px 40px;
  gap: 6px;
  align-items: center;
  padding: 4px 0;
}
.lines-head {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.line-row input {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
}
.line-row .total { text-align: right; font-variant-numeric: tabular-nums; }
.line-row button { padding: 4px 8px; font-size: 13px; }
.add-line { margin: 6px 0 12px; }

.totals {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}
.totals .grand {
  font-weight: 700;
  font-size: 16px;
  border-top: 2px solid var(--text);
  padding-top: 6px;
  min-width: 260px;
  text-align: right;
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.status {
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
  min-height: 18px;
}

/* ---- History ---- */
.history {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.history-row {
  display: grid;
  grid-template-columns: 110px 100px 1fr 110px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.history-row:last-child { border-bottom: 0; }
.history-row .num { font-weight: 600; font-variant-numeric: tabular-nums; }
.history-row .amount { text-align: right; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; gap: 2px; flex-wrap: wrap; justify-content: flex-end; }

/* ---- Print preview (pdf.js falls back to this layout) ---- */
.print-wrap {
  max-width: 800px;
  margin: 16px auto;
  background: #fff;
  padding: 40px 50px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.08);
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #1f2937;
}
.print-head { display: flex; justify-content: space-between; align-items: flex-start; }
.print-title { font-family: "Times New Roman", serif; font-size: 26pt; margin: 0; }
.print-meta { text-align: right; color: #6b7280; font-size: 11px; }
.print-rule { border: 0; border-top: 1px solid #e5e7eb; margin: 16px 0; }
.print-parties { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.print-parties h4 { margin: 0 0 6px; font-size: 10px; color: #6b7280; text-transform: uppercase; letter-spacing: 0.05em; }
.print-parties .party { white-space: pre-line; font-size: 12px; line-height: 1.4; }
.print-lines th { background: #f3f4f6; font-size: 11px; text-transform: uppercase; color: #374151; padding: 8px; text-align: left; }
.print-lines th.num, .print-lines td.num { text-align: right; }
.print-lines td { padding: 8px; border-bottom: 1px solid #eef0f3; font-size: 12px; }
.print-totals { display: flex; justify-content: flex-end; margin-top: 16px; }
.print-totals table { width: 280px; }
.print-totals td { padding: 4px 8px; font-size: 12px; }
.print-totals td.lbl { color: #6b7280; }
.print-totals td.val { text-align: right; font-variant-numeric: tabular-nums; }
.print-totals tr.grand td { font-weight: 700; border-top: 2px solid #1a1a1a; padding-top: 8px; }
.print-footer { margin-top: 24px; padding-top: 12px; border-top: 1px solid #e5e7eb; font-size: 11px; color: #374151; }
.print-footer h4 { margin: 0 0 4px; font-size: 10px; text-transform: uppercase; color: #6b7280; }
.print-footer .payment-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; }
.print-footer .ref { margin-top: 10px; font-size: 10px; color: #6b7280; }

@media print {
  body { background: #fff; }
  .topbar { display: none !important; }
  .print-wrap { box-shadow: none; margin: 0; padding: 0; max-width: none; }
}
