:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --card: #ffffff;
  --line: #e2e5ea;
  --text: #16191d;
  --muted: #6b7280;
  --accent: #1565c0;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --err: #b91c1c;
  --err-bg: #fee2e2;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101215;
    --card: #191c21;
    --line: #2a2f37;
    --text: #e8eaed;
    --muted: #9aa1ac;
    --ok: #86efac;
    --ok-bg: #14351f;
    --warn: #fcd34d;
    --warn-bg: #3a2a08;
    --err: #fca5a5;
    --err-bg: #3a1414;
  }
  .badge.duplicate { background: #2e1065; color: #c4b5fd; }
  .badge.archived { background: rgba(128,128,128,0.22); color: #9aa1ac; }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }

/* ── Układ ──────────────────────────────────────────────────────── */

header.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 14px max(16px, env(safe-area-inset-left)) 14px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: var(--card);
}
header.top h1 { flex: 1 1 auto; margin: 0; font-size: 17px; }
header.top nav { display: flex; gap: 8px; }

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px max(16px, env(safe-area-inset-left)) calc(40px + env(safe-area-inset-bottom));
}

.center {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
}

/* ── Formanty ───────────────────────────────────────────────────── */

label.field { display: block; margin-bottom: 14px; text-align: left; }
label.field > span { display: block; margin-bottom: 5px; font-size: 12px; color: var(--muted); }

input, select, button {
  font: inherit;
  color: var(--text);
}

input, select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input.bad, select.bad { border-color: var(--err); background: var(--err-bg); }

button {
  padding: 10px 16px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: default; }
button.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
  font-weight: 500;
}
button.danger { background: transparent; border: 1px solid var(--line); color: var(--err); font-weight: 500; }
button.big { width: 100%; padding: 15px; font-size: 17px; }

.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.grow { flex: 1 1 auto; }

/* ── Pasek filtrów panelu ───────────────────────────────────────── */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  margin-bottom: 16px;
}
.toolbar .field { margin: 0; }

/* Opcja miesiąca to całe zdanie („czerwiec 2026 — 230 do zaksięgowania,
   14 zaksięgowane"), więc pole musi być na nie dość szerokie. */
.toolbar .f-month { flex: 1 1 27rem; min-width: 16rem; max-width: 34rem; }
.toolbar .f-search { flex: 1 1 18rem; min-width: 14rem; max-width: 26rem; }
.toolbar .f-kind { flex: 0 0 10rem; }
.toolbar .f-currency { flex: 0 0 8.5rem; }

/* ── Komunikaty ─────────────────────────────────────────────────── */

.msg { padding: 10px 12px; border-radius: 10px; font-size: 13px; }
.msg.err { background: var(--err-bg); color: var(--err); }
.msg.ok { background: var(--ok-bg); color: var(--ok); }
.msg.warn { background: var(--warn-bg); color: var(--warn); }
.muted { color: var(--muted); font-size: 13px; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.3px;
  white-space: nowrap;
}
.badge.booked { background: var(--ok-bg); color: var(--ok); }
.badge.ready { background: var(--warn-bg); color: var(--warn); }
.badge.duplicate { background: #ede9fe; color: #6d28d9; }
.badge.pending { background: var(--line); color: var(--muted); }
/* Archiwum jest wyciszone celowo: to sprawa zamknieta, ma nie przyciagac oka. */
.badge.archived { background: var(--line); color: var(--muted); opacity: 0.85; }
/* Typ faktury — etykieta, nie status, więc celowo spokojniejsza kolorystycznie. */
.badge.kind { background: var(--line); color: var(--text); }
.badge.failed { background: var(--err-bg); color: var(--err); }

/* ── Tabela faktur ──────────────────────────────────────────────── */

/* Osiem kolumn z polami edycyjnymi nie zmieści się na telefonie — tabela
   przewija się w swoim kontenerze, żeby strona nie jechała w poziomie. */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
}

table.invoices {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}

table.invoices th {
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
}

table.invoices td {
  padding: 7px 8px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
table.invoices tbody tr:last-child td { border-bottom: none; }
table.invoices tr.is-booked td { opacity: 0.6; }

/* Wynik porównania z iFirmą, widoczny na pierwszy rzut oka: zielony pasek =
 * numer brzmi tam tak samo, pomarańczowy = trzeba było go dopasować mimo
 * różnicy w znakach. Brak paska znaczy „jeszcze nieporównane" — to trzeci
 * stan, nie odmiana zgodności, więc nie może wyglądać jak żaden z tamtych. */
table.invoices tr.match-same td:first-child { box-shadow: inset 3px 0 0 #15803d; }
table.invoices tr.match-fixed td:first-child { box-shadow: inset 3px 0 0 #d97706; }

/* Numer z iFirmy pod naszym — tylko gdy się różnią. */
.ifirma-number {
  display: block;
  margin-top: 3px;
  font-size: 11.5px;
  color: #b45309;
  word-break: break-all;
}
@media (prefers-color-scheme: dark) { .ifirma-number { color: #fcd34d; } }

table.invoices input {
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 13.5px;
}
table.invoices input[readonly] { color: var(--muted); background: transparent; }

/* Szerokości dopasowane do treści: numer faktury jest długi, kwota krótka. */
table.invoices .c-img { width: 46px; }
table.invoices .c-kind { width: 1%; white-space: nowrap; }
/* Numery faktur są zawsze wielkimi literami — widać to już przy wpisywaniu. */
table.invoices .c-number { min-width: 12rem; }
table.invoices .c-number input { text-transform: uppercase; }
table.invoices .c-vat input { text-transform: uppercase; }
table.invoices .c-date { min-width: 8.5rem; }
table.invoices .c-amount { min-width: 10.5rem; }
table.invoices .money { display: flex; gap: 5px; align-items: center; }
table.invoices .money input { flex: 1 1 auto; min-width: 5rem; }
table.invoices .money select {
  flex: 0 0 auto;
  width: auto;
  padding: 6px 4px;
  border-radius: 8px;
  font-size: 12.5px;
}
table.invoices .c-vat { min-width: 9.5rem; }
table.invoices .c-partner { min-width: 7.5rem; }
table.invoices .c-actions { white-space: nowrap; }
table.invoices .c-actions button {
  padding: 6px 9px;
  margin: 2px 3px 2px 0;
  font-size: 12px;
}

table.invoices .thumb {
  width: 40px;
  height: 52px;
  object-fit: cover;
  border-radius: 5px;
  background: var(--line);
  cursor: zoom-in;
  display: block;
}
table.invoices .thumb.none {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  color: var(--muted);
  cursor: default;
}

/* PDF nie ma miniaturki — zamiast niej odnośnik w tym samym prostokącie. */
table.invoices .thumb.pdf {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--err-bg);
  color: var(--err);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-decoration: none;
  cursor: pointer;
}

/* ── Ostrzeżenia ────────────────────────────────────────────────── */

/* Uwaga to jedna ikona przy przyciskach, nie akapit pod wierszem — treść
   pokazuje się w dymku po najechaniu (albo po kliknięciu na telefonie). */
.warn-icon {
  display: inline-block;
  margin-left: 4px;
  padding: 4px 6px;
  border-radius: 8px;
  background: var(--warn-bg);
  color: var(--warn);
  font-size: 14px;
  line-height: 1;
  cursor: help;
  user-select: none;
}
.warn-icon.bad { background: var(--err-bg); color: var(--err); }
.warn-icon:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Dymek siedzi w <body>, więc przewijana tabela go nie przycina. */
.tip {
  position: fixed;
  z-index: 50;
  max-width: min(30rem, calc(100vw - 16px));
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
  font-size: 12.5px;
  white-space: pre-line;
  pointer-events: none;   /* dymek nie może odbierać najechania ikonie */
  opacity: 0;
  visibility: hidden;
}
.tip.on { opacity: 1; visibility: visible; }

/* Powiększone zdjęcie */
dialog.lightbox {
  max-width: 96vw;
  max-height: 96vh;
  padding: 0;
  border: none;
  border-radius: 12px;
  background: #000;
}
dialog.lightbox img { display: block; max-width: 96vw; max-height: 96vh; }
dialog.lightbox::backdrop { background: rgba(0, 0, 0, 0.8); }

/* Ustawienia — rzeczy ustawiane raz i rzadko. Wąskie okno, bo to formularz
   z kilkoma polami, a nie widok danych. */
dialog.settings {
  width: min(30rem, 92vw);
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
}
dialog.settings h2 { margin: 0 0 16px; font-size: 16px; }
dialog.settings::backdrop { background: rgba(0, 0, 0, 0.45); }

/* ── Skaner ─────────────────────────────────────────────────────── */

.spinner {
  width: 38px;
  height: 38px;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
