:root {
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #1d4ed8;
  --danger: #dc2626;
  --ok: #16a34a;
  --bg: #f3f4f6;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  font-size: 14px;
}

/* ---------- Toolbar ---------- */
.toolbar {
  max-width: 880px;
  margin: 16px auto 0;
  padding: 0 8px;
  display: flex;
  gap: 8px;
  justify-content: space-between;
  flex-wrap: wrap;
}
.toolbar-group { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar button {
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
}
.toolbar button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.toolbar button:hover { filter: brightness(0.95); }
.toolbar select {
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  font-size: 13px;
  max-width: 340px;
}
.inline-form { display: inline; margin: 0; }
.status { font-size: 13px; color: var(--ok); }
.status.error { color: var(--danger); }

/* ---------- Page ---------- */
.page {
  max-width: 880px;
  margin: 12px auto 48px;
  background: #fff;
  padding: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

input, textarea, select {
  font: inherit;
  color: inherit;
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 6px 8px;
  background: transparent;
  width: 100%;
}
.page input:hover, .page textarea:hover { border-color: var(--line); }
.page input:focus, .page textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
}
textarea { resize: vertical; min-height: 56px; }
::placeholder { color: #9ca3af; }

/* ---------- Header ---------- */
.header {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 28px;
}
.logo-box {
  width: 180px;
  height: 90px;
  border: 2px dashed var(--line);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--muted);
  text-align: center;
  overflow: hidden;
  flex-shrink: 0;
  position: relative;
}
.logo-box img { max-width: 100%; max-height: 100%; object-fit: contain; }
.logo-box.has-logo { border-style: solid; border-color: transparent; }
.logo-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  display: none;
  background: rgba(255,255,255,0.9);
  border: 1px solid var(--line);
  border-radius: 4px;
  cursor: pointer;
  font-size: 11px;
  padding: 1px 5px;
  color: var(--danger);
}
.logo-box.has-logo:hover .logo-remove { display: block; }

.biz { flex: 1; max-width: 300px; margin-top: 10px; }
.biz input { margin-bottom: 2px; }
.biz .biz-name { font-size: 17px; font-weight: 600; }

.invoice-meta { width: 280px; text-align: right; }
.invoice-meta h1 {
  margin: 0 0 4px;
  font-size: 34px;
  font-weight: 300;
  letter-spacing: 2px;
}
.invoice-number {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 14px;
}
.invoice-number span { color: var(--muted); font-size: 16px; }
.invoice-number input { width: 110px; text-align: left; }
.meta-row {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.meta-row label { color: var(--muted); white-space: nowrap; }
.meta-row input { width: 150px; }

/* ---------- Bill to / Ship to ---------- */
.parties {
  display: flex;
  gap: 24px;
  margin-bottom: 28px;
}
.party { flex: 1; max-width: 300px; }
.party .label, .notes-terms .label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  margin-bottom: 4px;
}
.label-note { font-weight: 400; text-transform: none; }

/* ---------- Items table ---------- */
table.items { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
table.items thead th {
  background: var(--ink);
  color: #fff;
  font-weight: 500;
  text-align: left;
  padding: 8px 10px;
  font-size: 13px;
}
table.items thead th.num, table.items td.num { text-align: right; }
table.items th.col-item { width: 50%; border-radius: 4px 0 0 4px; }
table.items th.col-qty { width: 12%; }
table.items th.col-rate { width: 17%; }
table.items th.col-amount { width: 17%; border-radius: 0 4px 4px 0; }
table.items th.col-del { width: 4%; background: transparent; }
table.items tbody td { padding: 2px 2px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.items td.num input { text-align: right; }
td.amount-cell { text-align: right; padding-right: 10px !important; white-space: nowrap; }
.row-del {
  border: none;
  background: none;
  color: var(--danger);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 6px;
  line-height: 1;
  visibility: hidden;
}
tr:hover .row-del { visibility: visible; }

.add-item {
  border: 1px dashed var(--accent);
  color: var(--accent);
  background: none;
  border-radius: 6px;
  padding: 7px 14px;
  cursor: pointer;
  font-size: 14px;
  margin-bottom: 24px;
}
.add-item:hover { background: #eff6ff; }

/* ---------- Bottom: notes + totals ---------- */
.bottom { display: flex; gap: 32px; align-items: flex-start; }
.notes-terms { flex: 1; }
.notes-terms .label { margin: 12px 0 2px; }
.notes-terms .label:first-child { margin-top: 0; }

.totals { width: 320px; flex-shrink: 0; }
.total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 0;
  gap: 8px;
}
.total-row .t-label { color: var(--muted); white-space: nowrap; }
.total-row .t-value { text-align: right; white-space: nowrap; }
.total-row input { width: 90px; text-align: right; }
.total-row .with-unit { display: flex; align-items: center; gap: 4px; }
.pct-toggle {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 4px;
  cursor: pointer;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--muted);
}
.row-remove {
  border: none;
  background: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 14px;
  padding: 0 2px;
}
.grand {
  border-top: 2px solid var(--ink);
  margin-top: 6px;
  padding-top: 10px;
  font-size: 17px;
  font-weight: 600;
}
.grand .t-label { color: var(--ink); }
.balance {
  background: var(--bg);
  border-radius: 6px;
  padding: 8px 10px;
  font-weight: 600;
  margin-top: 6px;
}
.add-links { margin-top: 6px; display: flex; gap: 14px; justify-content: flex-end; }
.add-links button {
  border: none;
  background: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 13px;
  padding: 0;
}
.add-links button:hover { text-decoration: underline; }
.hidden { display: none !important; }

.hint {
  max-width: 880px;
  margin: 0 auto 32px;
  padding: 0 8px;
  color: var(--muted);
  font-size: 12.5px;
  text-align: center;
}

/* ---------- Auth pages (login / setup / reset) ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.auth-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 32px;
  width: 100%;
  max-width: 380px;
}
.auth-card h1 { font-size: 22px; margin: 0 0 16px; font-weight: 600; }
.auth-card label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.auth-card input { border: 1px solid var(--line); background: #fff; }
.auth-card input:focus { outline: none; border-color: var(--accent); }
.auth-card button[type="submit"] {
  width: 100%;
  margin-top: 18px;
  padding: 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 15px;
  cursor: pointer;
}
.auth-card button[type="submit"]:hover { filter: brightness(1.1); }
.auth-error {
  background: #fef2f2;
  color: var(--danger);
  border: 1px solid #fecaca;
  padding: 8px 10px;
  border-radius: 6px;
  margin-bottom: 8px;
  font-size: 13px;
}
.auth-ok {
  background: #f0fdf4;
  color: var(--ok);
  border: 1px solid #bbf7d0;
  padding: 8px 10px;
  border-radius: 6px;
  margin-bottom: 8px;
  font-size: 13px;
}
.auth-note { color: var(--muted); font-size: 12.5px; margin-top: 14px; }
.auth-note a { color: var(--accent); }

/* ---------- Print ---------- */
@media print {
  body { background: #fff; }
  .toolbar, .hint, .add-item, .row-del, th.col-del, td.del-cell,
  .add-links, .pct-toggle, .row-remove, .logo-remove, noscript { display: none !important; }
  .page {
    max-width: none;
    margin: 0;
    border: none;
    border-radius: 0;
    padding: 24px 16px;
  }
  input, textarea { border-color: transparent !important; }
  .logo-box { border: none; }
  .logo-box:not(.has-logo) { display: none; }
  textarea { overflow: hidden; }
  @page { margin: 12mm; }
}
