:root {
  --bg: #f6f5f3; --card: #ffffff; --ink: #1d1b19; --muted: #6b6560; --line: #e3dfda;
  --brand: #16222c; --brand-ink: #ffffff; --brand-soft: #fdf0ea; --accent: #e4572e; --accent-ink: #b8401c;
  --ok: #1e7a3c; --ok-soft: #e3f4e8; --bad: #b3261e; --bad-soft: #fde8e6; --na: #5d6670; --na-soft: #eceff2;
  --warn: #9a6200; --warn-soft: #fff3d6;
  --radius: 12px; --tap: 48px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { font-size: 16px; line-height: 1.45; -webkit-text-size-adjust: 100%; padding-bottom: env(safe-area-inset-bottom); }
a { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* Górny pasek */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--card); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--ink); text-decoration: none; font-size: 18px; }
.hlogo { height: 38px; width: auto; display: block; }
.loginlogo { width: 180px; height: auto; margin: 4px auto 8px; display: block; }
.logo { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; background: var(--brand); color: #fff; font-weight: 800; }
.spacer { flex: 1; }
.iconbtn { display: inline-grid; place-items: center; min-width: var(--tap); height: 40px; border-radius: 10px; text-decoration: none; color: var(--ink); font-size: 22px; border: 1px solid var(--line); background: var(--card); }
.sync { display: flex; align-items: center; gap: 6px; height: 40px; padding: 0 12px; border-radius: 20px; border: 1px solid var(--line); background: var(--card); font-size: 14px; cursor: pointer; }
.sync .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--na); }
.sync.online .dot { background: var(--ok); }
.sync.pending .dot { background: var(--warn); }
.sync.offline .dot { background: var(--bad); }
.sync.busy .dot { animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

main { max-width: 860px; margin: 0 auto; padding: 16px; }
h1 { font-size: 22px; margin: 4px 0 12px; }
h2 { font-size: 18px; margin: 20px 0 8px; }
h3 { font-size: 16px; margin: 14px 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card.link { display: block; text-decoration: none; color: inherit; }
.card.link:active { background: #faf8f6; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row > .grow { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
@media (max-width: 560px) { .grid2 { grid-template-columns: 1fr; } }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-top: 8px; }

/* Przyciski */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: var(--tap); padding: 0 18px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); font-weight: 600; cursor: pointer; text-decoration: none; color: var(--ink); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--bad); border-color: #efc4bf; }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.bigbtns { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.bigbtn { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; padding: 16px; border-radius: var(--radius); border: 2px solid var(--accent); background: var(--brand-soft); color: var(--ink); text-decoration: none; cursor: pointer; text-align: left; }
.bigbtn b { font-size: 17px; }
.bigbtn .ic { font-size: 26px; }

/* Formularze */
label.f { display: block; }
label.f > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
label.f > span i { font-weight: 400; font-style: normal; opacity: .8; }
input[type=text], input[type=date], input[type=password], input[type=search], input[type=email], select, textarea {
  width: 100%; min-height: var(--tap); padding: 10px 12px; border-radius: 10px; border: 1px solid #cfc9c2; background: #fff; }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 3px solid #f2b8b2; border-color: var(--brand); }
input:disabled, select:disabled, textarea:disabled { background: #f2f0ed; color: #444; }
.check { display: flex; gap: 10px; align-items: flex-start; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; cursor: pointer; margin-bottom: 8px; }
.check input { width: 24px; height: 24px; flex: none; margin: 0; accent-color: var(--brand); }
.check.on { border-color: var(--brand); background: var(--brand-soft); }

/* Lista kontrolna */
.area { margin-top: 14px; }
.area > h3 { display: flex; justify-content: space-between; align-items: baseline; border-bottom: 2px solid var(--line); padding-bottom: 4px; }
.item { padding: 10px 0; border-bottom: 1px solid var(--line); }
.item .top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.item .name { flex: 1; min-width: 150px; font-weight: 600; }
.item .name small { display: block; font-weight: 400; color: var(--muted); }
.seg { display: inline-flex; border: 1px solid #cfc9c2; border-radius: 10px; overflow: hidden; }
.seg button { min-width: 52px; height: 44px; border: 0; background: #fff; font-weight: 700; cursor: pointer; border-right: 1px solid #cfc9c2; }
.seg button:last-child { border-right: 0; }
.seg button.on.Y { background: var(--ok); color: #fff; }
.seg button.on.N { background: var(--bad); color: #fff; }
.seg button.on.NA { background: var(--na); color: #fff; }
.item .more { display: grid; grid-template-columns: 1fr 120px; gap: 8px; margin-top: 8px; }
.item.bad { background: var(--bad-soft); margin: 0 -14px; padding: 10px 14px; }
@media (max-width: 560px) { .item .more { grid-template-columns: 1fr; } }

.photos { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; align-items: center; }
.thumb { position: relative; width: 76px; height: 76px; border-radius: 8px; overflow: hidden; background: #ddd; border: 1px solid var(--line); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.thumb .x { position: absolute; top: 2px; right: 2px; width: 26px; height: 26px; border-radius: 50%; border: 0; background: rgba(0,0,0,.65); color: #fff; font-size: 14px; cursor: pointer; }
.thumb.pending::after { content: "⏳"; position: absolute; bottom: 2px; left: 4px; font-size: 12px; }
.addphoto { width: 76px; height: 76px; border-radius: 8px; border: 2px dashed #bdb5ad; background: #fff; font-size: 26px; cursor: pointer; color: var(--muted); }

/* Etykiety statusu */
.badge { display: inline-block; padding: 2px 10px; border-radius: 20px; font-size: 12px; font-weight: 700; background: var(--na-soft); color: var(--na); white-space: nowrap; }
.badge.draft { background: var(--warn-soft); color: var(--warn); }
.badge.submitted { background: var(--ok-soft); color: var(--ok); }
.badge.dirty { background: #e8eefc; color: #2b4ea2; }
.badge.type { background: var(--brand-soft); color: var(--accent-ink); }
.topbar { border-bottom: 3px solid var(--accent); }
input:focus, select:focus, textarea:focus { outline-color: #f6c3b0 !important; }
.check.on { border-color: var(--accent) !important; }
.check input { accent-color: var(--accent) !important; }

.tabs { display: flex; gap: 6px; overflow-x: auto; margin: 0 -16px 12px; padding: 0 16px; scrollbar-width: none; }
.tabs a { flex: none; padding: 10px 14px; border-radius: 20px; background: var(--card); border: 1px solid var(--line); text-decoration: none; color: var(--ink); font-weight: 600; font-size: 14px; }
.tabs a.on { background: var(--ink); color: #fff; border-color: var(--ink); }

.progress { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 6px; }
.progress > i { display: block; height: 100%; background: var(--ok); }

.sigbox { border: 1px dashed #bdb5ad; border-radius: 10px; min-height: 90px; display: grid; place-items: center; background: #fff; cursor: pointer; padding: 6px; }
.sigbox img { max-height: 80px; max-width: 100%; }
.sigpad { width: 100%; height: 220px; border: 1px solid var(--line); border-radius: 10px; background: #fff; touch-action: none; }

.stickybar { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding: 16px 0 calc(8px + env(safe-area-inset-bottom)); display: flex; gap: 10px; }
.stickybar .btn { flex: 1; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(120px); background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 10px; transition: transform .25s; z-index: 100; max-width: 90vw; }
.toast.show { transform: translateX(-50%) translateY(0); }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 50; display: grid; place-items: center; padding: 16px; }
.modal .box { background: var(--card); border-radius: var(--radius); padding: 16px; width: 100%; max-width: 640px; max-height: 92vh; overflow: auto; }
.modal .camera { max-width: 860px; }
.modal .camera video { width: 100%; max-height: 65vh; background: #000; border-radius: 10px; margin-top: 10px; display: block; transition: opacity .1s; }
.modal img.full { max-width: 100%; max-height: 80vh; display: block; margin: 0 auto; }

.alert { padding: 12px 14px; border-radius: 10px; background: var(--warn-soft); color: #5a3a00; margin-bottom: 12px; }
.alert.err { background: var(--bad-soft); color: #7a1912; }
.alert.okk { background: var(--ok-soft); color: #134d26; }

table.list { width: 100%; border-collapse: collapse; background: var(--card); font-size: 14px; }
table.list th, table.list td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
table.list th { background: #f0ece8; }
.tablewrap { overflow-x: auto; }

.login { max-width: 420px; margin: 40px auto; }

/* ============ WYDRUK (formularze BM TRADA) ============ */
.doc { overflow-x: auto; background: #fff; color: #000; font-family: Arial, Helvetica, sans-serif; font-size: 10pt; }
.doc .page { padding: 12mm; margin: 0 auto 16px; background: #fff; max-width: 210mm; box-shadow: 0 1px 4px rgba(0,0,0,.15); }
.doc .page.wide { max-width: 297mm; }
.doc .dochead { display: flex; align-items: center; gap: 12px; border-bottom: 2px solid #e4572e; padding-bottom: 6px; margin-bottom: 10px; }
.doc .plogo { height: 52px; width: auto; flex: none; }
.doc .dh-text { flex: 1; text-align: center; }
.doc .dh-co { width: 90px; flex: none; font-size: 8pt; text-align: right; color: #444; }
.doc .scheme { font-weight: 700; font-size: 11pt; text-align: center; letter-spacing: .5px; }
.doc .title { font-weight: 700; font-size: 14pt; text-align: center; margin: 2px 0 0; }
.doc table { width: 100%; border-collapse: collapse; margin-bottom: 8px; page-break-inside: auto; }
.doc tr { page-break-inside: avoid; }
.doc th, .doc td { border: 1px solid #000; padding: 3px 5px; vertical-align: top; text-align: left; }
.doc th, .doc td.lbl { background: #e7e6e6; font-weight: 700; }
.doc td.c, .doc th.c { text-align: center; }
.doc .sig { max-height: 45px; max-width: 200px; }
.doc .ph { width: 80px; height: 60px; object-fit: cover; margin: 1px; border: 1px solid #999; }
.doc .phbig { width: 150px; height: 112px; object-fit: cover; margin: 2px; border: 1px solid #999; }
.doc .small { font-size: 8pt; }
.doc .box { display: inline-block; width: 12px; height: 12px; border: 1px solid #000; text-align: center; line-height: 11px; font-size: 10px; margin-right: 6px; }
.doc .meta { font-size: 8pt; color: #555; text-align: right; margin-top: 4px; }
.doc .avoid { page-break-inside: avoid; }
.doc.pdf-render { position: absolute; left: -20000px; top: 0; width: 1200px; overflow: visible; }
.doc.pdf-render .page { box-shadow: none; margin: 0 0 20px; padding: 2mm; }
/* Wybór nadawcy e-maila */
.viaopts { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 8px; }
.viaopt { display: flex; gap: 10px; align-items: center; text-align: left; padding: 10px 12px; border: 2px solid var(--line); border-radius: 10px; background: #fff; cursor: pointer; min-height: var(--tap); }
.viaopt .ic { font-size: 22px; }
.viaopt.on { border-color: var(--accent); background: var(--brand-soft); }

/* Okno odczytu AI */
.airow.warn { border-color: #e9c46a; background: var(--warn-soft); }
.airow.na { opacity: .75; }

/* Etykiety QR – 90×55 mm, 2 kolumny × 5 rzędów na A4 */
.qrsheet { display: grid; grid-template-columns: repeat(2, 90mm); gap: 4mm; justify-content: center; background: #fff; padding: 8px; }
.qrlabel { width: 90mm; height: 55mm; border: 1px dashed #999; border-radius: 3mm; display: flex; gap: 3mm; padding: 3mm; box-sizing: border-box; background: #fff; color: #000; page-break-inside: avoid; overflow: hidden; font-family: Arial, sans-serif; }
.qrlabel .qrcode { width: 38mm; height: 38mm; flex: none; align-self: center; }
.qrlabel .qrcode svg { width: 100%; height: 100%; display: block; }
.qrlabel .qrinfo { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1mm; }
.qrlabel .qrlogo { height: 10mm; width: auto; align-self: flex-start; }
.qrlabel .qrtitle { font-size: 7pt; font-weight: 700; letter-spacing: .3px; color: #b8401c; }
.qrlabel .qrref { font-size: 18pt; font-weight: 800; line-height: 1.05; word-break: break-word; }
.qrlabel .qrsub { font-size: 7.5pt; color: #333; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; }
.qrlabel .qrscan { margin-top: auto; font-size: 7pt; font-weight: 700; }
@media (max-width: 420px) { .qrsheet { grid-template-columns: 90mm; } }
@media print { .qrsheet { padding: 0; } }

.printbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }

@page { size: A4 portrait; margin: 8mm; }
@page wide { size: A4 landscape; margin: 8mm; }
@media print {
  body { background: #fff; }
  .no-print, .topbar, .printbar, .toast, .modal { display: none !important; }
  main { max-width: none; padding: 0; }
  .doc { overflow: visible; }
  .doc .page { box-shadow: none; margin: 0; padding: 0; max-width: none; page-break-after: always; }
  .doc .page:last-child { page-break-after: auto; }
  .doc .page.wide { page: wide; }
  .doc th, .doc td.lbl { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
