:root {
  --bg: #f3f5f7;
  --paper: #ffffff;
  --ink: #1e2a32;
  --muted: #6b7a86;
  --line: #dfe5ea;
  --accent: #1f9d6b;
  --accent-dark: #178257;
  --accent-soft: #e7f6ef;
  --head: #2f3c46;
  --warn: #b7791f;
  --bad: #c0392b;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(20, 30, 40, .08), 0 8px 24px rgba(20, 30, 40, .06);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); line-height: 1.45; }
a { color: var(--accent-dark); }
code { background: #eef2f5; padding: 1px 6px; border-radius: 4px; font-size: .92em; }
h1 { font-size: 1.6rem; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: 1rem; margin: 1.4rem 0 .5rem; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }
.center { text-align: center; }
.ok { color: var(--accent-dark); }
.warn { color: var(--warn); }
.bad { color: var(--bad); }
.inline { display: inline; }
main { padding: 24px 16px 60px; }

/* top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px; background: #fff; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.1rem; color: var(--ink); text-decoration: none; }
.brand-mark { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: #fff; font-size: .95rem; }
.nav-links { display: flex; align-items: center; gap: 18px; }
.nav-links a:not(.btn) { color: var(--ink); text-decoration: none; font-weight: 500; }
.nav-links a:not(.btn):hover { color: var(--accent-dark); }
.link-btn { background: none; border: 0; padding: 0; color: var(--muted); cursor: pointer; font: inherit; }
.link-btn:hover { color: var(--bad); }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border-radius: 8px; border: 1px solid transparent;
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { background: #fff; color: var(--ink); border-color: var(--line); }
.btn-ghost:hover { border-color: #b9c4cc; }
.btn-danger { background: #fff; color: var(--bad); border-color: #f0c9c4; }
.btn-danger:hover { background: #fdf1ef; }
.btn-sm { padding: 5px 10px; font-size: .88rem; }
.btn-lg { padding: 12px 22px; font-size: 1.05rem; }
.btn-block { width: 100%; }
.btn.is-busy { opacity: .7; cursor: progress; }
.btn-add { background: none; color: var(--accent-dark); border: 1px dashed var(--accent); margin-top: 10px; }
.btn-add:hover { background: var(--accent-soft); }

/* forms */
input, textarea, select {
  font: inherit; color: inherit; width: 100%;
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 7px; background: #fff;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid rgba(31, 157, 107, .35); border-color: var(--accent); }
label { display: block; font-weight: 600; font-size: .92rem; }
label input, label textarea, label select { margin-top: 5px; font-weight: 400; }
label small { font-weight: 400; }
.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; }
.check input { width: auto; margin: 0; }
.stack > * + * { margin-top: 14px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid-2 + label, label + .grid-2 { margin-top: 14px; }
section.card > label, section.card > .image-field { margin-top: 14px; }

/* cards & layout */
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 20px 22px; box-shadow: var(--shadow); }
.card + .card, .card + form, form + .card { margin-top: 18px; }
.narrow { max-width: 520px; margin: 40px auto; }
.page { max-width: 1180px; margin: 0 auto; }
.narrow-page { max-width: 820px; margin: 0 auto; }
.narrow-page form .card + .card { margin-top: 18px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 4px 0 0; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; }

.flash { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; font-weight: 500; }
.flash-ok { background: var(--accent-soft); color: #11603f; border: 1px solid #bfe6d3; }
.flash-error { background: #fdecea; color: #8e2a1f; border: 1px solid #f4c2bb; }
.callout { background: #fff8e6; border: 1px solid #f3dca2; border-radius: 8px; padding: 12px 16px; margin-bottom: 18px; }
.attention { border-color: #f3dca2; }

/* landing */
.hero { max-width: 760px; margin: 50px auto 30px; text-align: center; }
.hero h1 { font-size: clamp(2rem, 5vw, 3rem); line-height: 1.15; }
.hero p { font-size: 1.15rem; color: var(--muted); }
.hero-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-top: 24px; }
.steps { max-width: 1000px; margin: 40px auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; }
.step { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; }
.step span { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-dark); font-weight: 700; }
.step p { color: var(--muted); margin: 0; }

/* auth */
.auth-card { max-width: 420px; margin: 40px auto; }

/* dashboard */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 18px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.stat small { display: block; color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: .72rem; letter-spacing: .04em; }
.stat strong { display: block; font-size: 1.45rem; margin-top: 4px; }
.stat em { font-style: normal; font-size: .85rem; font-weight: 600; }
.list-toolbar { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab { padding: 7px 14px; border-radius: 999px; text-decoration: none; color: var(--muted); font-weight: 600; }
.tab:hover { background: #eef2f5; }
.tab.active { background: var(--accent-soft); color: var(--accent-dark); }
.search input { min-width: 260px; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--line); }
.table td { padding: 12px; border-bottom: 1px solid #eef2f5; vertical-align: top; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover td { background: #f8faf9; }
.table .actions { white-space: nowrap; text-align: right; }
.table a { text-decoration: none; }
.num, .table th.num, .doc-items th.num { text-align: right; white-space: nowrap; }
.empty { text-align: center; padding: 40px 10px; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .78rem; font-weight: 700; vertical-align: middle; }
.badge-draft { background: #eef2f5; color: #55656f; }
.badge-sent { background: #e8f0fb; color: #2a5a9e; }
.badge-partial { background: #fff3dc; color: #8a5a00; }
.badge-paid { background: var(--accent-soft); color: #11603f; }
.badge-overdue { background: #fdecea; color: #a12c1e; }

/* paper (editor + document) */
.paper { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 36px 40px; position: relative; }
.paper-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; margin-bottom: 28px; }
.title-block { text-align: right; }
.doc-title { font-size: 2.4rem; font-weight: 300; letter-spacing: .02em; margin: 0; }
.number-field { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; margin-top: 6px; }
.number-field span { padding: 0 10px; color: var(--muted); background: #f6f8fa; align-self: stretch; display: grid; place-items: center; }
.number-field input { border: 0; margin: 0; width: 170px; text-align: right; border-radius: 0; }
.doc-number { color: var(--muted); font-size: 1.05rem; margin-top: 4px; }

.logo-drop { width: 220px; min-height: 110px; position: relative; }
.logo-placeholder {
  display: grid; place-items: center; height: 110px; border: 2px dashed var(--line); border-radius: 8px;
  color: var(--muted); font-weight: 600; cursor: pointer; background: #fafbfc;
}
.logo-placeholder:hover { border-color: var(--accent); color: var(--accent-dark); }
.logo-drop img, .doc-logo img { max-width: 220px; max-height: 110px; object-fit: contain; display: block; }
.logo-remove { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px 0; font-size: .85rem; }

.paper-parties { display: grid; grid-template-columns: 1fr 300px; gap: 30px; }
.parties .from { margin-bottom: 14px; }
.parties .two { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.picker { margin-bottom: 6px; font-size: .9rem; background: #f8faf9; }
.meta label { display: grid; grid-template-columns: 120px 1fr; align-items: center; gap: 10px; margin-bottom: 8px; font-weight: 500; color: var(--muted); text-align: right; }
.meta label input { margin: 0; color: var(--ink); }
.doc-meta > div { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 4px 0; }
.doc-meta span { color: var(--muted); text-align: right; }
.doc-meta strong { font-weight: 600; text-align: right; overflow-wrap: anywhere; }
.balance-chip { background: #f3f5f7; border-radius: 7px; padding: 10px 12px !important; margin-top: 6px; }
.balance-chip span { color: var(--ink) !important; font-weight: 700; }
.label { display: block; font-weight: 700; color: var(--muted); font-size: .85rem; margin-bottom: 3px; }

.items { margin-top: 28px; }
.items-head, .item-row { display: grid; grid-template-columns: 1fr 100px 150px 140px 32px; gap: 10px; align-items: start; }
.items-head { background: var(--head); color: #fff; border-radius: 7px; padding: 9px 12px; font-weight: 600; font-size: .9rem; }
.items-head span:nth-child(n+2) { text-align: right; }
.item-row { padding: 8px 12px; border-bottom: 1px solid #eef2f5; }
.item-row textarea { min-height: 40px; overflow: hidden; }
.item-row .qty { text-align: right; }
.money-input { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 7px; background: #fff; }
.money-input:focus-within { outline: 2px solid rgba(31, 157, 107, .35); border-color: var(--accent); }
.money-input span { padding-left: 10px; color: var(--muted); font-size: .9rem; }
.money-input input { border: 0; outline: 0; text-align: right; }
.item-row .amount { text-align: right; padding-top: 9px; font-weight: 500; white-space: nowrap; }
.remove-item { background: none; border: 0; font-size: 1.4rem; line-height: 1; color: #b5c0c8; cursor: pointer; padding: 6px 0; }
.remove-item:hover { color: var(--bad); }

.paper-foot { display: grid; grid-template-columns: 1fr 340px; gap: 30px; margin-top: 30px; }
.notes > * + * { margin-top: 14px; }
.notes p { margin: 0; white-space: normal; }
.totals .row { display: grid; grid-template-columns: 1fr 170px; align-items: center; gap: 10px; padding: 6px 0; }
.totals .row > span:first-child { color: var(--muted); text-align: right; }
.totals output, .doc-totals .row > span:last-child { text-align: right; font-weight: 500; }
.totals .total { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 10px; font-weight: 700; }
.totals .total > *, .totals .balance > * { font-weight: 700; color: var(--ink) !important; }
.totals .balance { background: #f3f5f7; border-radius: 7px; padding: 10px 12px; margin-top: 6px; font-size: 1.05rem; }
.with-toggle { display: flex; align-items: center; gap: 6px; }
.with-toggle input { text-align: right; }
.unit { color: var(--muted); min-width: 32px; text-align: center; }
.unit-toggle { border: 1px solid var(--line); background: #fff; border-radius: 6px; min-width: 32px; padding: 7px 6px; cursor: pointer; font-weight: 700; color: var(--accent-dark); }

.doc-items { width: 100%; border-collapse: collapse; margin-top: 28px; }
.doc-items th { background: var(--head); color: #fff; text-align: left; padding: 9px 12px; font-size: .9rem; }
.doc-items th:first-child { border-radius: 7px 0 0 7px; }
.doc-items th:last-child { border-radius: 0 7px 7px 0; }
.doc-items td { padding: 11px 12px; border-bottom: 1px solid #eef2f5; vertical-align: top; }
.stamp {
  position: absolute; top: 120px; right: 50%; transform: translateX(50%) rotate(-14deg);
  border: 4px solid rgba(31, 157, 107, .55); color: rgba(31, 157, 107, .6); font-size: 3rem; font-weight: 800;
  padding: 4px 26px; border-radius: 10px; letter-spacing: .12em; pointer-events: none;
}

/* editor layout */
.editor { display: grid; grid-template-columns: 1fr 260px; gap: 22px; align-items: start; }
.sidebar { position: sticky; top: 78px; display: flex; flex-direction: column; gap: 12px; }
.sidebar label { margin-top: 6px; }
.ref-box { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.ref-box small { display: block; color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.ref-box code { font-size: 1rem; background: none; padding: 0; font-weight: 700; }
.sidebar p { margin: 0; }

/* owner invoice page */
.share { display: flex; justify-content: space-between; gap: 18px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.share p { margin: 4px 0 0; }
.share-row { display: flex; gap: 8px; flex: 1; min-width: 320px; max-width: 620px; }
.share-row input { margin: 0; }
.page > .document + .card { margin-top: 18px; }

/* public invoice */
.public-grid { display: grid; grid-template-columns: 1fr 340px; gap: 22px; align-items: start; }
.pay-card { position: sticky; top: 78px; }
.pay-card .due { font-size: 1.8rem; font-weight: 700; margin: 4px 0 14px; }
.pay-card .due small { display: block; font-size: .9rem; font-weight: 500; color: var(--muted); }
.pay-card form + a, .pay-card form + form { margin-top: 10px; }
.paid-card h2 { color: var(--accent-dark); }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .82rem; font-weight: 600; text-transform: uppercase; margin: 18px 0 12px; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--line); }
.qr { display: flex; justify-content: center; }
.qr svg, .qr img { width: 220px; height: auto; max-width: 100%; border: 1px solid var(--line); border-radius: 8px; }
.bank { background: #f6f8fa; border-radius: 8px; padding: 10px 12px; margin-top: 10px; }
.bank > div { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0; align-items: center; flex-wrap: wrap; }
.bank span { color: var(--muted); font-size: .88rem; }
.bank .ref { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 9px; }
.bank .ref span { width: 100%; }
.bank .ref strong { font-size: 1.15rem; letter-spacing: .03em; }
.claim { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 12px; }
.claim summary { cursor: pointer; font-weight: 700; color: var(--accent-dark); }
.claim form { margin-top: 12px; }

/* settings */
.image-field .image-box { display: flex; align-items: center; gap: 14px; margin: 6px 0; }
.image-field img { max-width: 160px; max-height: 110px; border: 1px solid var(--line); border-radius: 8px; padding: 4px; background: #fff; }
.image-actions { display: flex; gap: 8px; }
.image-actions label { font-weight: 600; }
.sticky-save { position: sticky; bottom: 0; padding: 12px 0; background: linear-gradient(transparent, var(--bg) 35%); }
#payments h2 { display: flex; align-items: center; gap: 10px; }

/* responsive */
.editor > *, .public-grid > *, .paper-parties > *, .paper-foot > * { min-width: 0; }
.pending-note { background: #e8f0fb; color: #2a5a9e; border-radius: 8px; padding: 10px 12px; font-size: .9rem; margin-bottom: 12px; }
@media (max-width: 980px) {
  .editor, .public-grid { grid-template-columns: 1fr; }
  .sidebar, .pay-card { position: static; }
  .paper-parties, .paper-foot { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  main { padding: 16px 10px 40px; }
  .topbar { padding: 10px 12px; flex-wrap: wrap; gap: 8px; }
  .nav-links { gap: 10px; flex-wrap: wrap; }
  .page-head h1 { font-size: 1.3rem; }
  .doc-title { font-size: 1.9rem; }
  .doc-items th, .doc-items td { padding: 8px 5px; font-size: .82rem; }
  .doc-meta > div { grid-template-columns: auto 1fr; }
  .totals .row { grid-template-columns: 1fr auto; }
  .paper { padding: 20px 16px; }
  .paper-head { flex-direction: column-reverse; }
  .title-block { text-align: left; }
  .parties .two, .grid-2, .steps { grid-template-columns: 1fr; }
  .items-head { display: none; }
  .item-row { grid-template-columns: 1fr 1fr; }
  .item-row textarea { grid-column: 1 / -1; }
  .item-row .amount { text-align: left; }
  .meta label { grid-template-columns: 1fr; text-align: left; }
  .search input { min-width: 0; }
  .share-row { min-width: 0; flex-wrap: wrap; }
}

/* print / PDF: only the invoice itself */
@media print {
  @page { margin: 12mm; }
  body { background: #fff; }
  main { padding: 0; }
  .topbar, .no-print, .flash, .callout { display: none !important; }
  .public-grid { display: block; }
  .paper { border: 0; box-shadow: none; padding: 0; }
  .doc-items th, .balance-chip, .totals .balance { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
