:root {
  --bg: #f4f6f9; --card: #fff; --ink: #1d2433; --muted: #6b7385; --line: #e3e7ee;
  --brand: #c1121f; --brand-ink: #fff; --danger: #c62d2d; --ok: #1d8a4e; --warn: #b26a00;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; color: var(--ink); background: var(--bg); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* layout */
.topbar { position: sticky; top: 0; z-index: 20; height: 52px; display: flex; align-items: center; gap: 12px; padding: 0 16px; background: var(--ink); color: #fff; }
.topbar a { color: #fff; }
.brand { font-weight: 700; font-size: 16px; }
.top-right { margin-left: auto; display: flex; gap: 16px; align-items: center; font-size: 13px; }
.top-right .muted { color: #aeb6c6; }
.menu-btn { display: none; background: none; border: 0; color: #fff; font-size: 20px; cursor: pointer; }
.sidebar { position: fixed; top: 52px; left: 0; bottom: 0; width: 210px; padding: 12px 8px; background: #fff; border-right: 1px solid var(--line); overflow-y: auto; }
.sidebar a { display: block; padding: 9px 12px; border-radius: 8px; color: var(--ink); font-weight: 500; }
.sidebar a:hover { background: var(--bg); text-decoration: none; }
.sidebar a.active { background: #fdecee; color: var(--brand); }
.content { margin-left: 210px; padding: 24px; max-width: 1280px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

@media (max-width: 860px) {
  .menu-btn { display: block; }
  .top-right .muted { display: none; }
  .sidebar { transform: translateX(-100%); transition: transform .2s; z-index: 15; box-shadow: 0 0 20px rgba(0,0,0,.15); }
  body.nav-open .sidebar { transform: none; }
  .content { margin-left: 0; padding: 16px; }
}

/* components */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.btn { display: inline-block; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--ink); font: inherit; font-weight: 500; cursor: pointer; text-align: center; }
.btn:hover { text-decoration: none; border-color: #c5ccd8; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { color: var(--danger); }
.btn.block { display: block; width: 100%; margin-bottom: 8px; }
.icon { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 14px; padding: 4px 8px; }
.icon:hover { color: var(--danger); }
.linkbtn { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0; text-decoration: underline; }
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; background: #e6f5ec; color: var(--ok); border: 1px solid #bfe3cd; }
.flash-err { background: #fdecec; color: var(--danger); border-color: #f3c4c4; }

input, select, textarea { width: 100%; padding: 8px 10px; border: 1px solid #cfd5df; border-radius: 7px; font: inherit; background: #fff; color: var(--ink); }
input:focus, select:focus, textarea:focus { outline: 2px solid #f5c2c6; border-color: var(--brand); }
textarea { resize: vertical; }
label { display: block; font-weight: 500; font-size: 13px; }
label > input, label > select, label > textarea { margin-top: 4px; font-weight: 400; }
label.inline { display: inline-flex; gap: 6px; align-items: center; }
label.inline input { width: auto; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 18px; }
.grid.narrow { max-width: 640px; }
.span2 { grid-column: 1 / -1; }
.stack { display: grid; gap: 8px; margin-top: 12px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-bottom: 16px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 700px) { .grid, .two-col { grid-template-columns: 1fr; } }

.filters { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.filters input[name=q] { max-width: 280px; }
.filters select { width: auto; }
.filters label { display: flex; gap: 6px; align-items: center; }

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
table.list th, table.list td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.list th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
table.list tbody tr { cursor: pointer; }
table.list tbody tr:hover { background: #f8fafc; }
table.list tfoot td { border-bottom: 0; color: var(--muted); }
.r { text-align: right !important; }
.c { text-align: center; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; background: #eef0f4; color: var(--muted); }
.b-paid, .b-accepted, .b-delivered, .b-received { background: #e3f4ea; color: var(--ok); }
.b-partial, .b-sent { background: #e8effc; color: #1f5fd1; }
.b-overdue, .b-rejected { background: #fdecec; color: var(--danger); }
.b-unpaid, .b-issued { background: #fff4e0; color: var(--warn); }
.b-converted { background: #efe8fb; color: #6a3fc1; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.k-l { color: var(--muted); font-size: 12px; }
.k-v { font-size: 20px; font-weight: 700; margin-top: 4px; }
.k-s { color: var(--muted); font-size: 12px; }

.bars { display: flex; gap: 6px; height: 180px; align-items: stretch; }
.bar-col { flex: 1; display: flex; flex-direction: column; }
.bar-pair { flex: 1; display: flex; gap: 2px; align-items: flex-end; border-bottom: 1px solid var(--line); }
.bar { flex: 1; border-radius: 3px 3px 0 0; min-height: 1px; }
.bar.inv, .sw.inv { background: #9db8ec; }
.bar.col, .sw.col { background: var(--brand); }
.bar-l { text-align: center; font-size: 11px; color: var(--muted); padding-top: 4px; }
.legend { margin-top: 10px; font-size: 12px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-left: 8px; }

/* line editor */
.lines-edit th { font-size: 12px; color: var(--muted); text-align: left; padding: 6px; font-weight: 600; }
.lines-edit td { padding: 4px; vertical-align: top; }
.lines-edit td:first-child { min-width: 240px; }
.lines-edit textarea { margin-top: 4px; font-size: 13px; }
.lines-edit .n { width: 90px; text-align: right; }
.lines-edit .unit { width: 70px; }
.lines-edit .amt { padding-top: 12px; white-space: nowrap; font-weight: 600; }
.no-price .pc { display: none; }
.edit-totals { width: 300px; margin: 12px 0 0 auto; }
#add-line { margin-top: 8px; }

/* document view */
.doc-layout { display: grid; grid-template-columns: minmax(0, 1fr) 260px; gap: 16px; align-items: start; }
@media (max-width: 1000px) { .doc-layout { grid-template-columns: 1fr; } }
.doc-side input { margin-bottom: 8px; font-size: 12px; }
.pay-row { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.status-form { margin: 8px 0; }

.doc { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 40px; color: #222; font-size: 13px; max-width: 900px; margin: 0 auto; }
.doc-head { display: flex; justify-content: space-between; gap: 24px; padding-bottom: 18px; border-bottom: 2px solid var(--ink); position: relative; }
.biz { display: flex; flex-direction: column; gap: 10px; align-items: flex-start; max-width: 60%; }
.biz .logo { max-height: 56px; max-width: 280px; object-fit: contain; }
.biz-name { font-size: 17px; font-weight: 700; margin-bottom: 2px; }
.biz-tagline { font-style: italic; font-weight: 600; color: #333; margin-bottom: 2px; }
.doc-footer { margin-top: 28px; padding-top: 10px; border-top: 1px solid #e3e7ee; text-align: center; font-size: 11px; color: #666; letter-spacing: .02em; }
.biz div { line-height: 1.45; }
.doc-meta { text-align: right; }
.doc-title { font-size: 22px; font-weight: 800; letter-spacing: .04em; color: var(--brand); margin-bottom: 8px; }
.doc-title .ms { font-weight: 600; font-size: 15px; color: var(--muted); }
.doc-meta table { margin-left: auto; width: auto; }
.doc-meta th { text-align: left; padding: 1px 12px 1px 0; color: #555; font-weight: 600; }
.doc-meta td { text-align: right; padding: 1px 0; }
.ms { color: #666; font-weight: 400; }
.stamp { position: absolute; right: 0; bottom: -46px; border: 3px solid; padding: 2px 12px; font-weight: 800; font-size: 18px; transform: rotate(-8deg); border-radius: 6px; opacity: .8; }
.stamp-paid { color: var(--ok); }
.stamp-cancelled { color: var(--danger); }
.parties { display: flex; gap: 24px; margin: 20px 0; }
.party { max-width: 60%; }
.lbl { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: #666; font-weight: 700; margin-bottom: 4px; }
.lbl .ms { text-transform: none; }
.party-name { font-weight: 700; font-size: 14px; }
.subject { margin-bottom: 14px; }
.lines th { background: #f1f4f9; padding: 8px; text-align: left; font-size: 12px; border-bottom: 1px solid #d5dbe5; }
.lines th .ms { display: block; font-size: 11px; }
.lines td { padding: 8px; border-bottom: 1px solid #e7eaf0; vertical-align: top; }
.iname { font-weight: 600; }
.idesc { color: #555; font-size: 12px; }
.totals-wrap { display: flex; justify-content: space-between; gap: 24px; margin-top: 12px; align-items: flex-start; }
.words { font-style: italic; color: #444; max-width: 50%; padding-top: 6px; }
.totals { width: 320px; }
.totals th { text-align: left; padding: 5px 8px; font-weight: 500; }
.totals td { text-align: right; padding: 5px 8px; white-space: nowrap; }
.totals .grand th, .totals .grand td { font-weight: 800; font-size: 15px; border-top: 2px solid var(--ink); }
.block { margin-top: 18px; white-space: normal; }
.sign { display: flex; justify-content: space-between; gap: 40px; margin-top: 48px; }
.sig { flex: 1; max-width: 260px; font-size: 12px; color: #555; }
.sig-line { border-bottom: 1px solid #333; height: 50px; margin-bottom: 6px; }
.cgen { font-size: 11px; color: #777; text-align: center; width: 100%; }
@media (max-width: 700px) {
  .doc { padding: 18px; }
  .doc-head, .totals-wrap { flex-direction: column; }
  .biz, .party, .words { max-width: 100%; }
  .doc-meta { text-align: left; }
  .doc-meta table { margin-left: 0; }
  .totals { width: 100%; }
}

/* simple pages (login, share) */
body.simple { padding: 24px 16px; }
.auth { max-width: 400px; margin: 8vh auto; display: grid; gap: 12px; }
.auth h1 { font-size: 20px; }
.share-bar { max-width: 900px; margin: 0 auto 12px; text-align: right; }

@media print {
  @page { size: A4; margin: 12mm; }
  body { background: #fff; padding: 0 !important; }
  .topbar, .sidebar, .no-print, .doc-side, .flash { display: none !important; }
  .content { margin: 0; padding: 0; max-width: none; }
  .doc-layout { display: block; }
  .doc { border: 0; padding: 0; max-width: none; border-radius: 0; }
  .lines th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .doc-title { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  tr { page-break-inside: avoid; }
}
