:root {
  --bg: #f4f4f1; --panel: #ffffff; --ink: #18181b; --muted: #6b6b73; --line: #e3e3de;
  --brand: #f5c518; --brand-ink: #18181b; --ok: #1a7f4b; --warn: #a15c00; --err: #b42318; --info: #2754a3;
  --radius: 10px; color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.45 Inter, system-ui, -apple-system, sans-serif; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0 0 .5em; line-height: 1.2; }
h2 { font-size: 20px; } h3 { font-size: 15px; }
a { color: inherit; }
.hint { color: var(--muted); font-size: 12px; font-weight: 400; }
.grow { flex: 1; }

/* controls */
label { display: block; font-size: 12px; font-weight: 500; color: #3f3f46; margin: 0 0 10px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; color: var(--ink); font: inherit; font-size: 14px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: #d4a900; }
input[type=color] { padding: 2px; height: 38px; }
textarea { resize: vertical; }
label.chk { display: flex; align-items: center; gap: 8px; font-weight: 400; font-size: 13px; }
label.chk input { width: auto; margin: 0; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: #fff; color: var(--ink); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #bdbdb5; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(.96); }
.btn.dark { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--err); }
.btn.sm { padding: 6px 10px; font-size: 13px; }
.pick { display: flex; gap: 6px; align-items: stretch; margin-top: 4px; }
.pick select, .pick input { margin-top: 0; flex: 1; }
.row2, .row3 { display: grid; gap: 0 12px; }
.row2 { grid-template-columns: 1fr 1fr; }
.row3 { grid-template-columns: repeat(3, 1fr); }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px 4px; margin: 6px 0 14px; }
legend { font-weight: 600; font-size: 13px; padding: 0 6px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: #fff; padding: 32px; border-radius: 16px; width: min(360px, 100%); box-shadow: 0 10px 40px rgba(0,0,0,.08); text-align: center; }
.login-card label { text-align: left; }
.login-card .btn { width: 100%; }
.err { color: var(--err); min-height: 1.2em; }

/* top bar */
.top { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 10px 18px; background: #18181b; color: #fff; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.brand img { border-radius: 6px; background: #fff; }
.tabs { display: flex; gap: 4px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs button { background: transparent; color: #c9c9cf; border: 0; padding: 7px 12px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; }
.tabs button.on, .tabs button:hover { background: #2c2c31; color: #fff; }
.tabs button.on { box-shadow: inset 0 -2px 0 var(--brand); }
.top-right { display: flex; align-items: center; gap: 10px; }
.top-right .btn { color: #fff; border-color: #3a3a40; }
.sync { font-size: 12px; color: #a1a1aa; }

/* editor */
.editor { display: grid; grid-template-columns: minmax(380px, 520px) 1fr; gap: 18px; padding: 18px; align-items: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 14px 4px; margin-bottom: 12px; }
.svc { display: none; }
.form[data-svc=date] .svc-date, .form[data-svc=period] .svc-period, .form[data-svc=month] .svc-month { display: block; }
.cn-only { display: none; }
.form[data-doc=credit_note] .cn-only { display: grid; }
.lines-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; }
.lines-head h3 { margin: 0; }
.lines-head .pick { margin: 0; max-width: 280px; flex: 1; }
.line { border: 1px solid var(--line); border-radius: 8px; padding: 10px 10px 0; margin-bottom: 8px; position: relative; background: #fcfcfa; }
.line .l1 { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.line .l2 { display: grid; grid-template-columns: 1fr 1fr 1.2fr 1fr; gap: 8px; }
.line .l2.disc { grid-template-columns: .9fr 1fr 1.1fr .8fr .9fr; }
.line label { margin-bottom: 8px; }
.line-tools { display: flex; gap: 4px; align-items: flex-end; padding-bottom: 8px; }
.line-tools button { border: 1px solid var(--line); background: #fff; border-radius: 6px; width: 30px; height: 34px; cursor: pointer; font-size: 14px; }
.line-sum { font-size: 12px; color: var(--muted); text-align: right; margin: -4px 0 8px; }
.lines-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.lines-actions .chk { margin: 0; }
.checks { padding-bottom: 10px; }
.checks h3 { display: flex; align-items: center; gap: 8px; }
.check { display: flex; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); font-size: 13px; }
.check:first-of-type { border-top: 0; }
.check .dot { flex: none; width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; }
.check.error .dot { background: var(--err); } .check.warn .dot { background: #e69500; } .check.info .dot { background: var(--info); }
.check .law { display: block; color: var(--muted); font-size: 11px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: #eee; }
.badge.ok { background: #dcf3e6; color: var(--ok); } .badge.err { background: #fde3e1; color: var(--err); } .badge.warn { background: #fff0d6; color: var(--warn); }
.badge.draft { background: #ececec; color: #555; } .badge.issued { background: #e2ebff; color: var(--info); } .badge.paid { background: #dcf3e6; color: var(--ok); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.actions.sticky { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding: 14px 0 12px; }

.preview-wrap { position: sticky; top: 70px; }
.preview-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-weight: 600; }
.preview-scroll { max-height: calc(100vh - 110px); overflow: auto; border-radius: 6px; }
#sheetScale { transform-origin: top left; }
.sheet { width: 210mm; min-height: 297mm; background: #fff; box-shadow: 0 6px 30px rgba(0,0,0,.12); }
.empty-sheet { padding: 40mm 20mm; color: var(--muted); text-align: center; font-size: 16px; }

/* lists */
.list { padding: 22px 18px; max-width: 1200px; margin: 0 auto; }
.list-head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.list-head h2 { margin: 0 auto 0 0; }
.list-head input { max-width: 320px; margin: 0; }
.list-head select { max-width: 140px; margin: 0; }
.table-wrap { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th, table.grid td { padding: 9px 12px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
table.grid th { background: #fafaf8; font-weight: 600; font-size: 12px; color: #52525b; }
table.grid td.num, table.grid th.num { text-align: right; }
table.grid tr:last-child td { border-bottom: 0; }
table.grid td .btn { margin-left: 4px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.pcard { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; cursor: pointer; display: flex; gap: 12px; }
.pcard:hover { border-color: #c8c8c0; }
.pcard .plogo { width: 54px; height: 54px; flex: none; border-radius: 8px; border: 1px solid var(--line); display: grid; place-items: center; overflow: hidden; background: #fafafa; font-weight: 700; color: #999; }
.pcard .plogo img { max-width: 100%; max-height: 100%; }
.pcard .pmeta { font-size: 12px; color: var(--muted); }
.pcard h3 { margin: 0 0 2px; font-size: 14px; }

/* dialogs */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(820px, calc(100vw - 24px)); max-height: calc(100vh - 24px); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(20,20,25,.45); }
.dlg { padding: 20px 22px; }
.dlg.narrow { max-width: 560px; }
#itemDlg { width: min(560px, calc(100vw - 24px)); }
.dlg .actions { position: sticky; bottom: -20px; background: #fff; padding: 12px 0 4px; border-top: 1px solid var(--line); margin-top: 8px; }
.pt-only, .de-only, .freelancer-only, .company-only, .biz-only { display: none; }
.dlg[data-country=PT] .pt-only, .dlg[data-country=DE] .de-only, .dlg[data-other] .other-only { display: block; }
.dlg[data-kind=freelancer] .freelancer-only { display: flex; }
.dlg[data-kind=freelancer] label.freelancer-only.dob { display: block; }
.dlg[data-kind=company] .company-only { display: block; }
.dlg[data-kind=company] .row3.company-only { display: grid; }
.dlg:not([data-kind=private]) .biz-only { display: block; }
.logo-row { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; }
.logo-box { width: 180px; height: 70px; border: 1px dashed #c9c9c0; border-radius: 8px; display: grid; place-items: center; color: var(--muted); font-size: 12px; background: #fafafa; overflow: hidden; }
.logo-box img { max-width: 100%; max-height: 100%; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(80px); background: #18181b; color: #fff; padding: 10px 16px; border-radius: 10px; transition: transform .25s; z-index: 100; max-width: calc(100vw - 32px); }
.toast.on { transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--err); }

/* ---------------- invoice sheet ---------------- */
.iv { position: relative; width: 210mm; min-height: 297mm; padding: 14mm 17mm 10mm 20mm; display: flex; flex-direction: column; font: 9.4pt/1.4 Inter, Helvetica, Arial, sans-serif; color: #1d1d1f; }
.iv-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10mm; min-height: 20mm; }
.iv-logo { max-height: 19mm; max-width: 72mm; object-fit: contain; }
.iv-logotext { font-size: 17pt; font-weight: 700; letter-spacing: -.01em; max-width: 95mm; }
.iv-title { text-align: right; }
.iv-title h1 { margin: 0; font-size: 21pt; font-weight: 700; letter-spacing: .02em; text-transform: uppercase; color: #1d1d1f; line-height: 1.05; }
.iv-title h1.long { font-size: 15pt; max-width: 105mm; }
.iv-titleno { color: #6b6b73; margin-top: 1.5mm; }
.iv-bar { height: 1.4mm; background: var(--accent); margin: 5mm 0 7mm; border-radius: 1px; }
.iv-top { display: flex; justify-content: space-between; gap: 10mm; min-height: 40mm; }
.iv-addr { width: 85mm; }
.iv-return { font-size: 6.8pt; color: #6b6b73; border-bottom: .2mm solid #c9c9c9; padding-bottom: 1mm; margin-bottom: 2.5mm; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.iv-ids { margin-top: 1.8mm; font-size: 8.4pt; color: #3f3f46; }
.iv-meta { border-collapse: collapse; font-size: 8.8pt; align-self: flex-start; }
.iv-meta td { padding: .7mm 0; vertical-align: top; }
.iv-meta td:first-child { color: #6b6b73; padding-right: 6mm; }
.iv-meta td:last-child { text-align: right; }
.iv-sb { display: grid; grid-template-columns: 1fr 1fr; gap: 8mm; font-size: 8.4pt; margin: 2mm 0 4mm; padding: 3mm 4mm; background: #f6f6f3; border-radius: 1.5mm; }
.iv-lbl { font-size: 7.4pt; text-transform: uppercase; letter-spacing: .06em; color: #6b6b73; font-weight: 600; margin-bottom: 1mm; }
.iv-intro { margin: 2mm 0 5mm; }
.iv-lines { width: 100%; border-collapse: collapse; margin-top: 3mm; }
.iv-lines th { text-align: left; font-size: 7.6pt; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: #52525b; padding: 2mm 1.6mm; border-bottom: .5mm solid var(--accent); }
.iv-lines td { padding: 2.4mm 1.6mm; border-bottom: .2mm solid #e4e4e0; vertical-align: top; }
.iv-lines tr { break-inside: avoid; }
.iv-lines .c-pos { width: 8mm; color: #6b6b73; }
.iv-lines .c-num { text-align: right; white-space: nowrap; }
.iv-details { color: #6b6b73; font-size: 8.4pt; margin-top: .6mm; }
.iv-code { color: #6b6b73; font-size: 8pt; }
.iv-none { text-align: center; color: #aaa; }
.iv-sum { display: flex; justify-content: space-between; gap: 10mm; margin-top: 4mm; break-inside: avoid; }
.iv-notes { flex: 1; font-size: 8.2pt; color: #3f3f46; }
.iv-notes p { margin: 0 0 1.8mm; }
.iv-totals { border-collapse: collapse; min-width: 78mm; font-size: 9pt; }
.iv-totals td { padding: 1.2mm 0; }
.iv-totals td:last-child { text-align: right; padding-left: 6mm; white-space: nowrap; }
.iv-totals .iv-grand td { font-weight: 700; font-size: 11pt; border-top: .5mm solid var(--accent); padding-top: 2.2mm; }
.iv-pay { margin-top: 6mm; padding: 3.5mm 4mm; border: .25mm solid #e4e4e0; border-left: 1.2mm solid var(--accent); border-radius: 1.5mm; font-size: 8.6pt; break-inside: avoid; }
.iv-pay table { border-collapse: collapse; margin: 1.5mm 0; }
.iv-pay td { padding: .3mm 0; }
.iv-pay td:first-child { color: #6b6b73; padding-right: 6mm; }
.iv-due { font-weight: 600; }
.iv-closing { margin-top: 6mm; }
.iv-spacer { flex: 1; min-height: 8mm; }
.iv-foot { display: grid; grid-template-columns: 1.15fr 1fr 1.25fr 1.15fr; gap: 4mm; border-top: .25mm solid #d4d4d0; padding-top: 3mm; font-size: 6.9pt; line-height: 1.45; color: #52525b; break-inside: avoid; }
.iv-watermark { position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%) rotate(-28deg); font-size: 64pt; font-weight: 800; color: rgba(0,0,0,.05); letter-spacing: .08em; pointer-events: none; white-space: nowrap; }

/* responsive */
@media (max-width: 1100px) {
  .editor { grid-template-columns: minmax(0, 1fr); }
  .form, .preview-wrap { min-width: 0; }
  .preview-wrap { position: static; }
  .preview-scroll { max-height: none; }
}
@media (max-width: 640px) {
  .top { flex-wrap: wrap; padding: 10px 16px; }
  .tabs { order: 3; flex-basis: 100%; }
  .editor, .list { padding: 12px 16px; }
  .row2, .row3 { grid-template-columns: 1fr; }
  .line .l2, .line .l2.disc { grid-template-columns: 1fr 1fr; }
  .editor { min-width: 0; }
}

/* print: only the sheet */
@page { size: A4; margin: 0; }
@media print {
  html, body { background: #fff; }
  body > *:not(#app) { display: none !important; }
  .top, .form, .preview-bar, .view:not(#view-editor) { display: none !important; }
  .editor { display: block; padding: 0; }
  .preview-wrap, .preview-scroll { position: static; max-height: none; overflow: visible; }
  #sheetScale { transform: none !important; height: auto !important; }
  .sheet { box-shadow: none; width: 210mm; }
  .iv { min-height: 296mm; }
}
.dlg:not([data-kind=freelancer]) .freelancer-only, .dlg:not([data-kind=company]) .company-only, .dlg[data-kind=private] .biz-only { display: none !important; }
