:root {
  --bg: #f5f3f0;
  --card: #ffffff;
  --ink: #1f1b16;
  --muted: #6b645c;
  --line: #e4ded6;
  --accent: #9c2f5a;
  --accent-soft: #f6e8ee;
  --radius: 10px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
}

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.brand-logo { height: 32px; border-radius: 50%; }
.brand-client { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  padding: 8px 14px; border-radius: 8px; font: inherit; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-ghost { border-style: dashed; width: 100%; margin-top: 8px; }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 18px; }
.icon-btn:hover { color: #c0392b; }

/* Layout */
.layout {
  display: grid; grid-template-columns: minmax(320px, 440px) 1fr; gap: 16px;
  padding: 16px; max-width: 1400px; margin: 0 auto;
}
@media (max-width: 900px) { .layout { grid-template-columns: 1fr; } }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px;
}
.card h2 { font-size: 15px; margin: 0 0 10px; }
.card h2 small { font-weight: 400; color: var(--muted); font-size: 12px; }

label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 8px;
  border: 1px solid var(--line); border-radius: 6px; font: inherit; color: var(--ink); background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.seg { display: flex; gap: 8px; margin-bottom: 10px; }
.seg label {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin: 0; color: var(--ink); cursor: pointer;
}
.seg label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.seg input { width: auto; margin: 0; }

.hint { font-size: 12px; color: var(--muted); margin: 0 0 8px; }
.item-row {
  display: grid; gap: 6px; align-items: end;
  grid-template-columns: 56px 56px 1fr 84px 28px;
  grid-template-areas: "desc desc desc desc del" "qty unit price mode del";
  padding: 8px 0; border-bottom: 1px dashed var(--line);
}
.item-row label { margin: 0; font-size: 11px; }
.item-row input, .item-row select { margin-top: 2px; }
.item-row .i-desc-wrap { grid-area: desc; }
.item-row .i-qty-wrap { grid-area: qty; }
.item-row .i-unit-wrap { grid-area: unit; }
.item-row .i-price-wrap { grid-area: price; }
.item-row .i-mode-wrap { grid-area: mode; }
.item-row .icon-btn { grid-area: del; align-self: center; }
.item-row .i-days-wrap { display: none; grid-area: days; }
.item-row.is-day { grid-template-areas: "desc desc desc desc del" "qty unit price mode del" "days days . . del"; }
.item-row.is-day .i-days-wrap { display: block; }

.history { list-style: none; padding: 0; margin: 0; max-height: 240px; overflow: auto; }
.history li {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 6px 0; border-bottom: 1px solid var(--line);
}
.history li:last-child { border-bottom: 0; }
.history button.link { border: 0; background: none; color: var(--accent); cursor: pointer; text-align: left; font: inherit; padding: 0; }
.history .meta { color: var(--muted); font-size: 12px; }
.history .empty { color: var(--muted); }

.backup { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); }
.backup h3 { font-size: 13px; margin: 0 0 8px; }
.backup-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.backup-row .btn { flex: 1; }
.btn-sm { padding: 6px 10px; font-size: 13px; }
a.btn { text-decoration: none; display: inline-flex; align-items: center; }

body:not(.is-receipt) .receipt-only { display: none; }

/* Document preview (A4) */
.preview-wrap { overflow-x: auto; }
.doc {
  background: #fff; width: 210mm; min-height: 297mm; margin: 0 auto; padding: 18mm 16mm;
  box-shadow: 0 2px 12px rgba(0,0,0,.08); color: #222; font-size: 12.5px;
}
/* While html2pdf captures the page: no shadow, and no forced height that could spill onto a blank 2nd page. */
.doc.is-capturing { box-shadow: none; min-height: 0; }
.doc-head { display: flex; justify-content: space-between; gap: 16px; border-bottom: 3px solid var(--accent); padding-bottom: 12px; }
.doc-head img { height: 64px; }
.doc-company { font-size: 11.5px; color: #555; line-height: 1.5; }
.doc-company strong { display: block; font-size: 15px; color: #222; }
.doc-title { text-align: right; }
.doc-title h1 { margin: 0; font-size: 26px; letter-spacing: 2px; color: var(--accent); }
.doc-title div { color: #555; margin-top: 4px; }

.doc-parties { display: flex; justify-content: space-between; gap: 16px; margin: 18px 0; }
.doc-parties h3 { margin: 0 0 4px; font-size: 11px; text-transform: uppercase; color: #888; letter-spacing: 1px; }
.doc-parties p { margin: 0; line-height: 1.5; white-space: pre-line; }

.doc table { width: 100%; border-collapse: collapse; }
.doc th { background: var(--accent-soft); text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; padding: 8px; }
.doc td { padding: 8px; border-bottom: 1px solid #eee; vertical-align: top; }
.doc .num { text-align: right; white-space: nowrap; }

.doc-totals { margin-left: auto; width: 280px; margin-top: 12px; }
.doc-totals div { display: flex; justify-content: space-between; padding: 4px 8px; }
.doc-totals .grand { border-top: 2px solid #222; font-weight: 700; font-size: 14px; margin-top: 4px; padding-top: 8px; }
.doc-totals .balance { background: var(--accent-soft); font-weight: 700; }

.doc-foot { margin-top: 28px; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; font-size: 11.5px; color: #444; }
.doc-foot h3 { margin: 0 0 4px; font-size: 11px; text-transform: uppercase; color: #888; letter-spacing: 1px; }
.doc-foot p { margin: 0; line-height: 1.5; white-space: pre-line; }
.doc-foot .schedule { margin-top: 12px; }
.doc .muted { color: #888; font-size: 11px; }
/* Round rubber stamp: outer ring + inner ring, green when paid, amber when partly paid */
.stamp {
  --stamp: #1e8e3e;
  position: relative; width: 124px; height: 124px; margin-top: 12px; border-radius: 50%;
  border: 3px solid var(--stamp); color: var(--stamp);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  text-align: center; transform: rotate(-12deg); opacity: .9;
}
.stamp::before {
  content: ""; position: absolute; inset: 5px; border-radius: 50%; border: 1px solid var(--stamp);
}
.stamp.is-partial { --stamp: #b35c00; }
.stamp strong {
  font-size: 15px; letter-spacing: 1.5px; line-height: 1.15;
  border-top: 2px solid var(--stamp); border-bottom: 2px solid var(--stamp); padding: 3px 4px;
}
.stamp.is-partial strong { font-size: 13px; }
.stamp-ring { font-size: 8px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase; max-width: 84px; line-height: 1.2; }
.doc-thanks { margin-top: 32px; text-align: center; color: #888; font-size: 11px; }

/* Print: only the document */
@page { size: A4; margin: 0; }
@media print {
  body { background: #fff; }
  .no-print { display: none !important; }
  .layout { display: block; padding: 0; }
  .doc { box-shadow: none; margin: 0; width: auto; min-height: auto; }
  .doc th, .doc-totals .balance, .stamp { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* Dashboard (dashboard.html) */
.dash-main { max-width: 1200px; margin: 0 auto; padding: 16px; }
.dropzone {
  border: 2px dashed var(--line); text-align: center; padding: 40px 16px; cursor: pointer;
}
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dropzone.compact { padding: 14px; }
.dropzone.compact .hint { display: none; }
.dash-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.dash-head h2 { margin: 0; }
.file-list { list-style: none; padding: 0; margin: 0; }
.file-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.file-list li:last-child { border-bottom: 0; }
.file-list .meta { color: var(--muted); font-size: 12px; }
.file-list li.bad .meta { color: #c0392b; }

.check-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; margin-bottom: 10px; }
.check-stats div { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 12px; color: var(--muted); }
.check-stats span { display: block; font-size: 20px; font-weight: 700; color: var(--ink); }
.check-stats .warn { border-color: #b35c00; }
.check-stats .warn span { color: #b35c00; }

.filters { display: flex; flex-wrap: wrap; align-items: end; gap: 10px; }
.filters label { margin: 0; min-width: 150px; }
.filters .hint { margin: 0 0 10px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 12px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 2px; }
.kpi-label { font-size: 12px; color: var(--muted); }
.kpi strong { font-size: 22px; }
.kpi-sub { font-size: 12px; color: var(--muted); }

.dash-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 0 12px; }
.table-scroll { overflow-x: auto; }
.dtable { width: 100%; border-collapse: collapse; font-size: 13px; }
.dtable th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); padding: 6px 8px; border-bottom: 1px solid var(--line); }
.dtable td { padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.dtable .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.dtable .muted { color: var(--muted); font-size: 11px; }

.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.legend i, .tip i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.chart-box { position: relative; }
.chart svg { display: block; width: 100%; height: auto; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .hit { fill: transparent; }
.chart .hit.on { fill: rgba(0,0,0,.04); }
.tip {
  position: absolute; top: 0; transform: translateX(-50%); pointer-events: none;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 4px 14px rgba(0,0,0,.1);
  padding: 8px 10px; font-size: 12px; min-width: 170px;
}
.tip div { display: flex; align-items: center; margin-top: 4px; }
.tip span { margin-left: auto; padding-left: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.table-view { margin-top: 8px; font-size: 13px; }
.table-view summary { cursor: pointer; color: var(--accent); }
@media (max-width: 600px) { .dash-grid { grid-template-columns: 1fr; } }
