/* =========================================================
   Free Invoice Generator — styles
   ========================================================= */
:root {
  --accent: #4f46e5;
  --accent-soft: color-mix(in srgb, var(--accent) 10%, white);
  --accent-dark: color-mix(in srgb, var(--accent) 80%, black);

  --bg: #f3f5f9;
  --surface: #ffffff;
  --ink: #0f172a;
  --ink-2: #334155;
  --muted: #64748b;
  --line: #e2e8f0;
  --line-2: #eef2f7;
  --field: #f8fafc;
  --danger: #e11d48;
  --success: #16a34a;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06);
  --shadow: 0 1px 3px rgba(15, 23, 42, .06), 0 10px 30px -10px rgba(15, 23, 42, .12);
  --shadow-lg: 0 25px 60px -15px rgba(15, 23, 42, .35);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.ta-r { text-align: right; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 38px; padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  font-weight: 600; font-size: 13.5px;
  white-space: nowrap; text-decoration: none;
  transition: background .15s, border-color .15s, color .15s, box-shadow .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .icon-btn:focus-visible, .tpl:focus-visible, .swatch:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 35%, transparent);
  outline-offset: 2px;
}
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 4px 14px -4px color-mix(in srgb, var(--accent) 60%, transparent); }
.btn-primary:hover { background: var(--accent-dark); }
.btn-outline { border-color: var(--line); background: var(--surface); color: var(--ink-2); }
.btn-outline:hover { border-color: #cbd5e1; background: var(--field); }
.btn-soft { background: var(--accent-soft); color: var(--accent-dark); }
.btn-soft:hover { background: color-mix(in srgb, var(--accent) 16%, white); }
.btn-ghost { color: var(--ink-2); }
.btn-ghost:hover { background: var(--line-2); }
.btn-danger-ghost { color: var(--danger); }
.btn-danger-ghost:hover { background: #fff1f2; }
.btn-block { width: 100%; }
.btn-lg { height: 46px; font-size: 15px; }
.btn-sm { height: 30px; padding: 0 10px; font-size: 12.5px; }

.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent;
  font-size: 22px; line-height: 1; color: var(--muted);
}
.icon-btn:hover { background: var(--line-2); color: var(--ink); }

.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent-soft); color: var(--accent-dark);
  font-size: 11.5px; display: inline-grid; place-items: center;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: rgba(255, 255, 255, .85);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1240px; margin: 0 auto; padding: 0 16px;
  height: 60px; display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); font-size: 17px; }
.brand strong { color: var(--accent); font-weight: 800; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  background: var(--accent); color: #fff;
}
.topbar-actions { display: flex; gap: 8px; }

/* ---------- Layout ---------- */
.layout {
  max-width: 1240px; margin: 0 auto; padding: 28px 16px 20px;
  display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 24px; align-items: start;
}

/* ---------- Paper (editor) ---------- */
.paper {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 40px 44px 44px;
  overflow: hidden;
}
.paper-accent { position: absolute; inset: 0 0 auto 0; height: 5px; background: var(--accent); }

/* Shared field styling: looks like plain text until hovered/focused */
.paper input:not([type=file]), .paper textarea, .select, .search {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  padding: 8px 10px;
  outline: none;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
.paper textarea { resize: none; overflow: hidden; min-height: 42px; }
.paper input:hover, .paper textarea:hover { border-color: #cbd5e1; }
.paper input:focus, .paper textarea:focus, .select:focus, .search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
}
.paper ::placeholder { color: #a0aec0; }
.paper input[type=number] { -moz-appearance: textfield; text-align: right; }
.paper input[type=number]::-webkit-inner-spin-button,
.paper input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field + .field { margin-top: 14px; }
.field label, .meta-row label {
  font-size: 11.5px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
}
.opt { text-transform: none; letter-spacing: 0; font-weight: 400; }

/* Header row */
.row-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; }
.logo-box {
  position: relative;
  width: 220px; height: 120px;
  border: 1.5px dashed #cbd5e1; border-radius: var(--radius);
  background: var(--field);
  display: grid; place-items: center; text-align: center;
  cursor: pointer; transition: border-color .15s, background .15s;
}
.logo-box:hover, .logo-box.dragover { border-color: var(--accent); background: var(--accent-soft); }
.logo-box.has-logo { border-style: solid; border-color: transparent; background: transparent; }
.logo-box.has-logo:hover { border-color: var(--line); }
.logo-empty { display: flex; flex-direction: column; align-items: center; gap: 4px; color: var(--muted); }
.logo-empty span { font-weight: 600; color: var(--ink-2); }
.logo-empty small { font-size: 11.5px; }
#logo-img { max-width: 100%; max-height: 100%; object-fit: contain; }
.logo-remove {
  position: absolute; top: -10px; right: -10px;
  width: 26px; height: 26px; border-radius: 50%; border: 0;
  background: var(--ink); color: #fff; font-size: 16px; line-height: 1;
  opacity: 0; transition: opacity .15s;
}
.logo-box:hover .logo-remove, .logo-remove:focus { opacity: 1; }

.title-box { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.paper .input-title {
  width: 280px; text-align: right;
  font-size: 34px; font-weight: 800; letter-spacing: .02em;
  border-color: transparent; padding: 2px 10px;
}
.number-wrap {
  display: flex; align-items: center; width: 200px;
  border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden;
}
.number-wrap:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent); }
.number-wrap span { padding: 0 10px; color: var(--muted); background: var(--field); align-self: stretch; display: grid; place-items: center; border-right: 1px solid var(--line); }
.paper .number-wrap input { border: 0; box-shadow: none; text-align: right; font-weight: 600; }

/* Parties & meta */
.row-parties {
  display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 32px;
  margin-top: 32px;
}
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 14px; }
.two-col .field + .field { margin-top: 0; }
.meta { display: flex; flex-direction: column; gap: 10px; }
.meta-row { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 10px; }
.meta-row label { text-align: right; }
.balance-chip {
  margin-top: 6px; padding: 12px 14px; border-radius: var(--radius-sm);
  background: var(--accent-soft);
  display: flex; justify-content: space-between; align-items: center;
}
.balance-chip span { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--accent-dark); }
.balance-chip strong { font-size: 18px; color: var(--accent-dark); }

/* Items */
.items { margin-top: 34px; }
.items-head, .item-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 90px 130px 130px 34px;
  gap: 10px; align-items: start;
}
.items-head {
  background: var(--accent); color: #fff;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 12px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase;
}
.item-row { padding: 10px 12px 0; animation: rowIn .2s ease; }
@keyframes rowIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.item-row + .item-row { border-top: 1px dashed var(--line-2); padding-top: 10px; }
.item-row textarea { min-height: 38px; padding-top: 8px; }
.it-amount { padding: 8px 2px; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.money-input { position: relative; }
.money-input .cur-sym { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; font-size: 13px; }
.paper .money-input input { padding-left: 26px; }
.it-del {
  width: 34px; height: 38px; border: 0; border-radius: var(--radius-sm); background: transparent;
  color: #94a3b8; font-size: 20px;
}
.it-del:hover { background: #fff1f2; color: var(--danger); }
.it-del:disabled { visibility: hidden; }
.item-label { display: none; }

.btn-add-line {
  margin: 14px 12px 0; display: inline-flex; align-items: center; gap: 6px;
  border: 1.5px dashed color-mix(in srgb, var(--accent) 45%, white);
  background: var(--accent-soft); color: var(--accent-dark);
  border-radius: var(--radius-sm); padding: 8px 14px; font-weight: 600;
  transition: background .15s;
}
.btn-add-line:hover { background: color-mix(in srgb, var(--accent) 16%, white); }

/* Bottom: notes + totals */
.row-bottom {
  display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 36px;
  margin-top: 34px; padding-top: 28px; border-top: 1px solid var(--line);
}
.totals { display: flex; flex-direction: column; gap: 6px; }
.t-row {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  min-height: 38px; color: var(--ink-2);
}
.t-row strong { font-variant-numeric: tabular-nums; }
.t-adj { display: none; }
.t-adj.on { display: flex; }
.adj-ctrl { display: flex; align-items: center; gap: 6px; }
.paper .adj-ctrl input { width: 110px; }
.type-toggle, .type-static {
  width: 38px; height: 38px; flex: none; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--field);
  display: grid; place-items: center; font-weight: 700; color: var(--ink-2); font-size: 13px;
}
.type-toggle:hover { border-color: var(--accent); color: var(--accent); }
.adj-remove {
  width: 24px; height: 24px; border: 0; border-radius: 50%; background: transparent; color: #94a3b8; font-size: 18px; line-height: 1;
}
.adj-remove:hover { color: var(--danger); background: #fff1f2; }
.t-row:not(.t-adj) .adj-ctrl { margin-right: 30px; }
.adj-links { display: flex; justify-content: flex-end; gap: 4px; flex-wrap: wrap; }
.adj-links button {
  border: 0; background: transparent; color: var(--accent); font-weight: 600; font-size: 13px;
  padding: 4px 8px; border-radius: 6px;
}
.adj-links button:hover { background: var(--accent-soft); }
.t-total { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 8px; color: var(--ink); font-size: 15px; }
.t-total strong { font-size: 17px; }
.t-balance {
  margin-top: 4px; padding: 12px 14px; border-radius: var(--radius-sm);
  background: var(--accent); color: #fff; font-weight: 600;
}
.t-balance strong { font-size: 20px; }

/* ---------- Sidebar ---------- */
.sidebar { position: sticky; top: 84px; display: flex; flex-direction: column; gap: 14px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-sm); border: 1px solid var(--line); padding: 16px; }
.card > .btn + .btn, .card > .btn-grid + .btn { margin-top: 10px; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.card-title { margin: 0 0 10px; font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.autosave { margin: 10px 0 0; font-size: 12px; color: var(--muted); text-align: center; display: flex; align-items: center; justify-content: center; gap: 6px; }
.autosave::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--success); }
.select, .search { height: 40px; padding: 0 10px; cursor: pointer; border: 1px solid var(--line); outline: none; border-radius: var(--radius-sm); }

.tpl-current {
  width: 100%; padding: 0; border: 1.5px solid var(--line); border-radius: var(--radius-sm);
  background: #e9edf3; overflow: hidden; display: block; margin-bottom: 10px; text-align: left;
  transition: border-color .15s, box-shadow .15s;
}
.tpl-current:hover { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 14%, transparent); }
.tpl-current .tpl-frame { height: 170px; }
.tpl-current-name { display: flex; justify-content: space-between; align-items: center; padding: 9px 12px; background: var(--surface); border-top: 1px solid var(--line); }
.tpl-current-name em { font-style: normal; color: var(--accent); font-weight: 600; font-size: 12.5px; }

/* A clipped window onto a scaled-down A4 render */
.tpl-frame { display: block; position: relative; overflow: hidden; background: #fff; }
.tpl-frame .inv { position: absolute; top: 0; left: 0; transform-origin: top left; pointer-events: none; }

.modal-sub { margin: 0 0 16px; color: var(--muted); }
.tpl-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 18px; }
.tpl-card {
  padding: 0; border: 2px solid var(--line); border-radius: 12px; background: var(--surface);
  overflow: hidden; text-align: left; position: relative;
  transition: border-color .15s, transform .15s, box-shadow .15s;
}
.tpl-card:hover { border-color: #cbd5e1; transform: translateY(-2px); box-shadow: var(--shadow); }
.tpl-card.active { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.tpl-card.active::after {
  content: "✓"; position: absolute; bottom: 16px; right: 12px; width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 14px;
}
.tpl-card .tpl-frame { aspect-ratio: 794 / 1123; border-bottom: 1px solid var(--line); }
.tpl-card-info { display: block; padding: 10px 44px 12px 12px; }
.tpl-card-info strong { display: block; font-size: 14px; }
.tpl-card-info span { display: block; font-size: 12px; color: var(--muted); }

.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
.swatch {
  aspect-ratio: 1; border-radius: 50%; border: 0; position: relative;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .08);
  transition: transform .12s;
}
.swatch:hover { transform: scale(1.1); }
.swatch.active::after {
  content: ""; position: absolute; inset: -4px; border-radius: 50%; border: 2px solid var(--ink);
}
.swatch-custom {
  background: conic-gradient(#f43f5e, #f59e0b, #22c55e, #06b6d4, #6366f1, #d946ef, #f43f5e);
  overflow: hidden;
}
.swatch-custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }

.card-tips ul { margin: 0; padding-left: 18px; color: var(--ink-2); font-size: 13px; }
.card-tips li + li { margin-top: 6px; }
kbd { font: 600 11px/1 "Inter", sans-serif; padding: 2px 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: var(--field); }

.site-footer { text-align: center; color: var(--muted); font-size: 12.5px; padding: 12px 16px 32px; }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, .55); animation: fade .2s; }
.modal-dialog {
  position: relative; width: 100%; min-width: 0; max-width: 720px; max-height: calc(100vh - 32px);
  background: var(--surface); border-radius: 16px; box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; animation: pop .2s ease;
}
.modal-wide { max-width: 900px; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.98); } }
.modal-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 16px 14px 22px; border-bottom: 1px solid var(--line); }
.modal-header h2 { margin: 0; font-size: 17px; }
.modal-header-actions { display: flex; align-items: center; gap: 8px; }
.modal-body { padding: 18px 22px 22px; overflow: auto; }
.preview-body { background: #e9edf3; padding: 24px; border-radius: 0 0 16px 16px; }
.preview-scale { margin: 0 auto; width: fit-content; box-shadow: var(--shadow); }
body.modal-open { overflow: hidden; }

/* Saved list */
.search { width: 100%; cursor: text; margin-bottom: 14px; }
.inv-list { display: flex; flex-direction: column; gap: 8px; }
.inv-item {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 14px; align-items: center;
  padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  transition: border-color .15s, background .15s;
}
.inv-item:hover { border-color: #cbd5e1; background: var(--field); }
.inv-item .who { min-width: 0; }
.inv-item .who strong { display: block; }
.inv-item .who span { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.inv-item .amt { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.inv-item .amt small { display: block; font-weight: 600; font-size: 11px; }
.inv-item .acts { display: flex; gap: 4px; }
.status-paid { color: var(--success); }
.status-due { color: #d97706; }
.empty-state { text-align: center; padding: 36px 10px; color: var(--muted); }
.empty-state svg { color: #cbd5e1; margin-bottom: 8px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 10px;
  font-weight: 500; box-shadow: var(--shadow-lg);
  transition: opacity .2s, transform .2s;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

/* =========================================================
   Static invoice render (PDF / print / preview)
   ========================================================= */
#render-wrap { position: fixed; left: -10000px; top: 0; }
.inv {
  --c: #4f46e5;
  width: 794px; min-height: 1123px; /* A4 @ 96dpi */
  background: #fff; color: #1e293b;
  font-family: "Inter", system-ui, sans-serif; font-size: 12.5px; line-height: 1.55;
  padding: 56px 60px;
  position: relative;
}
.inv * { box-sizing: border-box; }
.inv-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 30px; }
.inv-logo img { max-width: 200px; max-height: 90px; object-fit: contain; display: block; }
.inv-from-name { font-size: 18px; font-weight: 700; color: #0f172a; }
.inv-title-wrap { text-align: right; }
.inv-title { font-size: 34px; font-weight: 800; letter-spacing: .04em; color: var(--c); line-height: 1.1; margin: 0; }
.inv-no { margin-top: 6px; color: #64748b; font-weight: 600; font-size: 13px; }
.inv-label { font-size: 10.5px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: #64748b; margin-bottom: 4px; }
.inv-mid { display: flex; justify-content: space-between; gap: 30px; margin-top: 36px; }
.inv-parties { display: flex; gap: 36px; flex: 1; }
.inv-party { flex: 1; max-width: 220px; word-wrap: break-word; }
.inv-party .first { font-weight: 700; color: #0f172a; }
.inv-meta { border-collapse: collapse; align-self: flex-start; }
.inv-meta td { padding: 3px 0; }
.inv-meta td:first-child { color: #64748b; padding-right: 20px; text-align: right; }
.inv-meta td:last-child { text-align: right; font-weight: 600; color: #0f172a; }
.inv-meta .bal td { padding-top: 8px; }
.inv-meta .bal td:last-child { color: var(--c); font-size: 15px; font-weight: 800; }

.inv-items { width: 100%; border-collapse: separate; border-spacing: 0; margin-top: 34px; }
.inv-items th { text-align: left; font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; padding: 10px 12px; }
.inv-items td { padding: 12px; border-bottom: 1px solid #eef2f7; vertical-align: top; }
.inv-items tr { page-break-inside: avoid; }
.inv-items .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.inv-items .desc { white-space: pre-wrap; word-break: break-word; }
.inv-items tbody tr:nth-child(even) td { background: #fafbfc; }

.inv-bottom { display: flex; justify-content: space-between; gap: 40px; margin-top: 26px; page-break-inside: avoid; }
.inv-notes { flex: 1; }
.inv-notes > div + div { margin-top: 18px; }
.inv-notes p { margin: 0; white-space: pre-wrap; color: #334155; }
.inv-totals { width: 300px; flex: none; border-collapse: collapse; align-self: flex-start; }
.inv-totals td { padding: 6px 0; }
.inv-totals td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.inv-totals .total td { border-top: 1px solid #e2e8f0; padding-top: 10px; font-weight: 700; color: #0f172a; font-size: 14px; }
.inv-totals .balance td { background: var(--c); color: #fff; padding: 12px 14px; font-weight: 700; font-size: 15px; }
.inv-totals .balance td:first-child { border-radius: 6px 0 0 6px; }
.inv-totals .balance td:last-child { border-radius: 0 6px 6px 0; }
.inv-foot { margin-top: 50px; text-align: center; color: #94a3b8; font-size: 11px; }

.inv-bottom { position: relative; }
.inv-stamp {
  position: absolute; top: 30px; right: 340px;
  border: 4px solid #16a34a; color: #16a34a; border-radius: 10px;
  padding: 6px 20px; font-size: 30px; font-weight: 800; letter-spacing: .15em;
  transform: rotate(-14deg); opacity: .75;
}

/* Template: Modern — coloured header band */
.tpl-modern { padding-top: 0; }
.tpl-modern .inv-top { background: var(--c); color: #fff; margin: 0 -60px; padding: 40px 60px 34px; align-items: center; }
.tpl-modern .inv-title, .tpl-modern .inv-from-name { color: #fff; }
.tpl-modern .inv-no { color: rgba(255, 255, 255, .8); }
.tpl-modern .inv-logo img { background: #fff; padding: 8px; border-radius: 8px; }
.tpl-modern .inv-items th { background: var(--c); color: #fff; }
.tpl-modern .inv-items th:first-child { border-radius: 6px 0 0 6px; }
.tpl-modern .inv-items th:last-child { border-radius: 0 6px 6px 0; }

/* Template: Classic — top rule, tinted table head */
.tpl-classic { border-top: 10px solid var(--c); }
.tpl-classic .inv-top { padding-bottom: 24px; border-bottom: 1px solid #e2e8f0; }
.tpl-classic .inv-items th { background: var(--c-soft); color: var(--c-dark); border-bottom: 2px solid var(--c); }

/* Template: Minimal — monochrome with accent touches */
.tpl-minimal .inv-title { color: #0f172a; font-weight: 300; letter-spacing: .2em; }
.tpl-minimal .inv-items th { border-bottom: 2px solid #0f172a; color: #0f172a; padding-left: 0; }
.tpl-minimal .inv-items td { padding-left: 0; }
.tpl-minimal .inv-items th.num, .tpl-minimal .inv-items td.num { padding-right: 0; padding-left: 12px; }
.tpl-minimal .inv-items tbody tr:nth-child(even) td { background: none; }
.tpl-minimal .inv-totals .balance td { background: none; color: var(--c); border-top: 2px solid #0f172a; border-radius: 0; padding: 12px 0; font-size: 17px; }

/* Template: Corporate — side rule, dark table head, boxed meta */
.tpl-corporate { border-left: 16px solid var(--c); padding-left: 50px; }
.tpl-corporate .inv-title { color: #0f172a; }
.tpl-corporate .inv-no { color: var(--c); }
.tpl-corporate .inv-meta { background: var(--c-soft); border-collapse: separate; border-radius: 8px; padding: 10px 14px; }
.tpl-corporate .inv-items th { background: #0f172a; color: #fff; }
.tpl-corporate .inv-totals .balance td { background: #0f172a; }

/* Template: Elegant — serif, centred header, fine rules */
.tpl-elegant .inv-top { flex-direction: column; align-items: center; text-align: center; gap: 14px; padding-bottom: 22px; border-bottom: 3px double var(--c); }
.tpl-elegant .inv-title-wrap { text-align: center; }
.tpl-elegant .inv-title, .tpl-elegant .inv-from-name { font-family: "Playfair Display", Georgia, serif; }
.tpl-elegant .inv-title { color: #0f172a; font-weight: 500; letter-spacing: .3em; font-size: 30px; }
.tpl-elegant .inv-label { color: var(--c); }
.tpl-elegant .inv-items th { color: var(--c); border-top: 1px solid var(--c); border-bottom: 1px solid var(--c); }
.tpl-elegant .inv-items tbody tr:nth-child(even) td { background: none; }
.tpl-elegant .inv-totals .balance td { background: none; color: var(--c); border-top: 3px double var(--c); border-radius: 0; padding: 12px 0; font-family: "Playfair Display", Georgia, serif; font-size: 18px; }

/* Template: Bold — dark header band with accent highlights */
.tpl-bold { padding-top: 0; }
.tpl-bold .inv-top { background: #0f172a; color: #fff; margin: 0 -60px; padding: 44px 60px 38px; align-items: center; border-bottom: 8px solid var(--c); }
.tpl-bold .inv-title { color: var(--c); font-size: 40px; }
.tpl-bold .inv-from-name { color: #fff; }
.tpl-bold .inv-no { color: #cbd5e1; }
.tpl-bold .inv-logo img { background: #fff; padding: 8px; border-radius: 8px; }
.tpl-bold .inv-items th { background: #0f172a; color: #fff; border-bottom: 3px solid var(--c); }

/* Template: Creative — decorative corner circles, pill shapes */
.tpl-creative { overflow: hidden; }
.tpl-creative::before, .tpl-creative::after { content: ""; position: absolute; border-radius: 50%; }
.tpl-creative::before { width: 380px; height: 380px; top: -210px; right: -130px; background: var(--c-soft); }
.tpl-creative::after { width: 150px; height: 150px; top: -60px; right: 170px; background: var(--c); opacity: .15; }
.tpl-creative > * { position: relative; z-index: 1; }
.tpl-creative .inv-title { color: var(--c); }
.tpl-creative .inv-items th { background: var(--c); color: #fff; }
.tpl-creative .inv-items th:first-child { border-radius: 20px 0 0 20px; }
.tpl-creative .inv-items th:last-child { border-radius: 0 20px 20px 0; }
.tpl-creative .inv-items tbody tr:nth-child(even) td { background: var(--c-soft); }
.tpl-creative .inv-totals .balance td:first-child { border-radius: 24px 0 0 24px; }
.tpl-creative .inv-totals .balance td:last-child { border-radius: 0 24px 24px 0; }

/* Template: Compact — tight spacing, full grid table */
.tpl-compact { font-size: 11.5px; padding: 40px 44px; border-top: 5px solid var(--c); }
.tpl-compact .inv-title { font-size: 26px; color: #0f172a; }
.tpl-compact .inv-mid { margin-top: 24px; }
.tpl-compact .inv-items { margin-top: 22px; }
.tpl-compact .inv-items th, .tpl-compact .inv-items td { border: 1px solid #e2e8f0; padding: 7px 10px; }
.tpl-compact .inv-items th + th, .tpl-compact .inv-items td + td { border-left: 0; }
.tpl-compact .inv-items tbody td { border-top: 0; }
.tpl-compact .inv-items th { background: #f1f5f9; color: #0f172a; }
.tpl-compact .inv-items tbody tr:nth-child(even) td { background: none; }
.tpl-compact .inv-totals .balance td { background: var(--c-soft); color: var(--c-dark); border-radius: 0 !important; }

/* ---------- Responsive ---------- */
@media (max-width: 1080px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); order: -1; }
  .card-tips { display: none; }
}
@media (max-width: 760px) {
  .layout { padding-top: 16px; gap: 16px; }
  .paper { padding: 28px 18px 24px; }
  .row-head { flex-direction: column-reverse; align-items: stretch; }
  .title-box { align-items: stretch; }
  .paper .input-title, .number-wrap { width: 100%; }
  .paper .input-title { text-align: left; padding-left: 0; }
  .logo-box { width: 100%; }
  .row-parties, .row-bottom, .two-col { grid-template-columns: 1fr; gap: 20px; }
  .meta-row label { text-align: left; }

  .items-head { display: none; }
  .item-row {
    grid-template-columns: 1fr 1fr 34px;
    grid-template-areas: "desc desc del" "qty rate ." "amt amt .";
    border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 12px !important; margin-top: 10px;
  }
  .item-row + .item-row { border-top: 1px solid var(--line); }
  .item-row .c-desc { grid-area: desc; }
  .item-row .c-qty { grid-area: qty; }
  .item-row .c-rate { grid-area: rate; }
  .item-row .it-amount { grid-area: amt; display: flex; justify-content: space-between; border-top: 1px dashed var(--line); }
  .item-row .it-amount::before { content: "Amount"; color: var(--muted); font-weight: 500; }
  .item-row .it-del { grid-area: del; }
  .item-label { display: block; font-size: 11px; color: var(--muted); font-weight: 600; margin-bottom: 4px; text-transform: uppercase; letter-spacing: .05em; }
  .btn-add-line { margin-left: 0; width: 100%; justify-content: center; }

  .topbar-actions .btn-ghost svg ~ .label-txt { display: none; }
  .preview-body { padding: 12px; }
  .inv-item { grid-template-columns: 1fr auto; }
  .inv-item .acts { grid-column: 1 / -1; }
}
@media (max-width: 420px) {
  .brand > span:last-child { display: none; }
}


/* =========================================================
   History page
   ========================================================= */
.history { max-width: 1240px; margin: 0 auto; padding: 28px 16px 48px; }
.history-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; }
.history-head h1 { margin: 0; font-size: 26px; letter-spacing: -.01em; }
.history-head p { margin: 4px 0 0; color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow-sm); border-top: 3px solid var(--accent); }
.stat span { display: block; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.stat strong { display: block; font-size: 22px; margin-top: 4px; font-variant-numeric: tabular-nums; word-break: break-word; }
.stat small { color: var(--danger); font-weight: 600; font-size: 12px; }
.stat-good { border-top-color: var(--success); }
.stat-warn { border-top-color: #d97706; }

.history-card { padding: 0; overflow: hidden; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.toolbar .search { width: 280px; margin: 0; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 20px; padding: 6px 14px; font-weight: 600; font-size: 13px; color: var(--ink-2); }
.chip:hover { border-color: #cbd5e1; }
.chip.active { background: var(--ink); border-color: var(--ink); color: #fff; }

.table-wrap { overflow-x: auto; }
.h-table { width: 100%; border-collapse: collapse; }
.h-table th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; padding: 12px 16px; background: var(--field); border-bottom: 1px solid var(--line); white-space: nowrap; user-select: none; }
.h-table th[data-sort] { cursor: pointer; }
.h-table th[data-sort]:hover { color: var(--ink); }
.h-table th.sorted { color: var(--accent); }
.h-table th.asc::after { content: " 91"; }
.h-table th.desc::after { content: " 93"; }
.h-table td { padding: 12px 16px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.h-table tbody tr:hover td { background: #fafbfd; }
.h-table .num { text-align: right; }
.h-table td small { display: block; color: #d97706; font-size: 12px; font-weight: 600; }
.h-table .loading { text-align: center; color: var(--muted); padding: 32px; }
.inv-link { color: var(--accent); font-weight: 700; text-decoration: none; }
.inv-link:hover { text-decoration: underline; }
.acts-cell { white-space: nowrap; }
.acts-cell .btn + .btn { margin-left: 4px; }
.pill { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.pill-paid { background: #dcfce7; color: #15803d; }
.pill-unpaid { background: #fef3c7; color: #b45309; }
.pill-overdue { background: #ffe4e6; color: #be123c; }
.history-card .empty-state div { margin-top: 4px; }
code { background: var(--line-2); padding: 2px 6px; border-radius: 4px; font-size: 12.5px; }

@media (max-width: 900px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .toolbar .search { width: 100%; }
  .h-table thead { display: none; }
  .h-table, .h-table tbody, .h-table tr, .h-table td { display: block; width: 100%; }
  .h-table tr { border-bottom: 1px solid var(--line); padding: 8px 0; }
  .h-table td { border: 0; padding: 5px 16px; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  .h-table td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; text-align: left; }
  .h-table td.num { text-align: right; }
  .h-table td small { display: inline; margin-left: 6px; }
  .acts-cell { justify-content: flex-start !important; flex-wrap: wrap; padding-top: 8px !important; }
}

/* =========================================================
   Pickers (client / saved items) in the editor
   ========================================================= */
.label-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.label-acts { display: flex; gap: 2px; }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 600; font-size: 12px; padding: 2px 6px; border-radius: 5px; }
.link-btn:hover { background: var(--accent-soft); }
.paper #f-client-email { margin-top: 6px; font-size: 13px; }

.picker-anchor { position: relative; }
.picker-inline { display: inline-block; }
.picker {
  position: absolute; z-index: 30; top: 28px; left: 0; right: 0; min-width: 280px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg); padding: 8px; display: block;
  animation: pop .15s ease;
}
.picker-up { top: auto; bottom: calc(100% + 6px); left: 12px; right: auto; width: 360px; max-width: calc(100vw - 48px); }
.paper .picker .picker-search { height: 36px; margin: 0 0 6px; }
.picker-list { display: block; max-height: 260px; overflow-y: auto; }
.pick {
  display: flex; width: 100%; justify-content: space-between; align-items: center; gap: 10px;
  text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px;
}
.pick:hover, .pick:focus { background: var(--accent-soft); outline: none; }
.pick-main { display: flex; flex-direction: column; min-width: 0; font-weight: 600; }
.pick-main small { font-weight: 400; color: var(--muted); font-size: 12px; }
.pick strong { white-space: nowrap; font-variant-numeric: tabular-nums; }
.pick-empty { display: block; padding: 14px 10px; color: var(--muted); font-size: 13px; text-align: center; }
.pick-empty a, .pick-empty b { color: var(--accent); }
.btn-add-saved { margin-left: 6px; background: var(--surface); }

.btn-success-ghost { color: var(--success); }
.btn-success-ghost:hover { background: #dcfce7; }
.head-acts { display: flex; gap: 8px; flex-wrap: wrap; }

/* =========================================================
   Settings page
   ========================================================= */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 20px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0; background: none; padding: 10px 16px; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 8px;
}
.tab:hover { color: var(--ink); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.settings-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.muted { color: var(--muted); font-size: 13px; }

.fld { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; min-width: 0; }
.fld label { font-size: 12px; font-weight: 600; color: var(--ink-2); }
.fld input, .fld textarea, .fld .select {
  width: 100%; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface);
  padding: 9px 11px; outline: none; transition: border-color .15s, box-shadow .15s; resize: vertical;
}
.fld .select { height: 40px; padding: 0 10px; }
.fld input:focus, .fld textarea:focus, .fld .select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent); }
.fld .color-input { height: 40px; padding: 4px; cursor: pointer; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-row .fld-logo { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; }
.logo-sm { width: 200px; height: 90px; }
.logo-sm #s-logo-img { max-width: 100%; max-height: 100%; object-fit: contain; }

.split { display: grid; grid-template-columns: 380px minmax(0, 1fr); gap: 16px; align-items: start; }
.list-card .search { margin-bottom: 10px; }
.rec-list { display: flex; flex-direction: column; gap: 6px; }
.rec {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm);
}
.rec:hover { background: var(--field); }
.rec-main { min-width: 0; }
.rec-main strong { display: block; white-space: pre-line; }
.rec-main span { color: var(--muted); font-size: 12.5px; }
.rec-acts { display: flex; gap: 4px; flex: none; }

@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; }
  .settings-grid { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .form-row { grid-template-columns: 1fr; }
  .btn-add-saved { margin: 8px 0 0; }
  .picker-up { left: 0; }
}

/* ---------- Print: only the static render ---------- */
@media print {
  @page { size: A4; margin: 0; }
  body { background: #fff; }
  body > *:not(#render-wrap) { display: none !important; }
  #render-wrap { position: static; }
  .inv { min-height: auto; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
