/* 2VISION Finanse — własny, lekki arkusz. Tokeny → komponenty → układ. Bez CDN. */

:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-2: #475569;
  --text-3: #94a3b8;
  --brand: #0f766e;
  --brand-strong: #115e59;
  --brand-soft: #ccfbf1;
  --accent: #2563eb;
  --ok: #15803d;      --ok-soft: #dcfce7;
  --warn: #b45309;    --warn-soft: #fef3c7;
  --danger: #b91c1c;  --danger-soft: #fee2e2;
  --info: #1d4ed8;    --info-soft: #dbeafe;
  --muted-soft: #e2e8f0;
  --focus: #2563eb;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 6px rgba(15, 23, 42, .04);

  --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px; --s-5: 24px; --s-6: 32px; --s-7: 48px;
  --radius: 10px; --radius-sm: 6px;
  --nav-w: 232px;
  --touch: 44px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220; --surface: #121a2b; --surface-2: #172034; --border: #243049; --border-strong: #334155;
    --text: #e5e7eb; --text-2: #a3b0c2; --text-3: #6b7a90; --brand: #2dd4bf; --brand-strong: #5eead4;
    --brand-soft: #134e4a; --accent: #60a5fa; --ok: #4ade80; --ok-soft: #14532d; --warn: #fbbf24;
    --warn-soft: #78350f; --danger: #f87171; --danger-soft: #7f1d1d; --info: #93c5fd; --info-soft: #1e3a8a;
    --muted-soft: #1f2937; --focus: #93c5fd; --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1220; --surface: #121a2b; --surface-2: #172034; --border: #243049; --border-strong: #334155;
  --text: #e5e7eb; --text-2: #a3b0c2; --text-3: #6b7a90; --brand: #2dd4bf; --brand-strong: #5eead4;
  --brand-soft: #134e4a; --accent: #60a5fa; --ok: #4ade80; --ok-soft: #14532d; --warn: #fbbf24;
  --warn-soft: #78350f; --danger: #f87171; --danger-soft: #7f1d1d; --info: #93c5fd; --info-soft: #1e3a8a;
  --muted-soft: #1f2937; --focus: #93c5fd; --shadow: none;
}

*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font-family: var(--font); color: var(--text); background: var(--bg);
  line-height: 1.5; min-height: 100dvh;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 var(--s-3); line-height: 1.25; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1rem; }
p { margin: 0 0 var(--s-3); }
small, .muted { color: var(--text-2); }
.hint { color: var(--text-3); font-size: .875rem; }
code, .mono { font-family: var(--mono); font-size: .9em; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: var(--radius-sm); }

/* ---- Układ --------------------------------------------------------------- */
.app { display: grid; grid-template-columns: var(--nav-w) 1fr; min-height: 100dvh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border); padding: var(--s-4);
  display: flex; flex-direction: column; gap: var(--s-4); position: sticky; top: 0; height: 100dvh;
}
.brand { display: flex; align-items: center; gap: var(--s-2); font-weight: 700; font-size: 1.05rem; color: var(--text); }
.brand__mark { width: 28px; height: 28px; border-radius: 8px; background: var(--brand); color: #fff; display: grid; place-items: center; font-size: .8rem; }
.brand small { display: block; font-weight: 500; font-size: .75rem; color: var(--text-3); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: var(--s-3); padding: 10px var(--s-3); border-radius: var(--radius-sm);
  color: var(--text-2); font-weight: 500; min-height: var(--touch);
}
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a[aria-current="page"] { background: var(--brand-soft); color: var(--brand-strong); }
.nav svg { width: 20px; height: 20px; flex: none; }
.sidebar__footer { margin-top: auto; font-size: .85rem; color: var(--text-2); display: flex; flex-direction: column; gap: var(--s-2); }
.main { min-width: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s-3);
  padding: var(--s-3) var(--s-5); background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.content { padding: var(--s-5); max-width: 1280px; }
.page-header { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: var(--s-3); margin-bottom: var(--s-5); }
.page-header p { color: var(--text-2); margin: 0; }

/* Przełącznik firmy */
.company-switch { display: flex; align-items: center; gap: var(--s-2); }
.company-switch form { display: inline; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-2); font-size: .85rem;
  cursor: pointer; min-height: 36px; font-family: inherit;
}
.chip span.code { display: none; }
.chip[aria-pressed="true"], .chip--active { background: var(--brand); color: #fff; border-color: var(--brand); }

/* Mobile */
.btn.menu-btn { display: none; }
.scrim { position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 15; display: none; }
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(84vw, 300px); transform: translateX(-100%); transition: transform .2s; z-index: 20; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .sidebar.is-open { transform: none; }
  .scrim.is-open { display: block; }
  .btn.menu-btn { display: inline-flex; }
  .content { padding: var(--s-4); }
  .topbar { padding: var(--s-2) var(--s-4); }
  .company-switch .chip span.label { display: none; }
  .company-switch .chip span.code { display: inline; }
  nav.tabbar { display: grid; }
  .hide-sm { display: none !important; }
  .topbar .btn--primary { padding: 10px 12px; }
  body { padding-bottom: 64px; }
}
.tabbar {
  display: none; position: fixed; bottom: 0; left: 0; right: 0; grid-auto-flow: column; background: var(--surface);
  border-top: 1px solid var(--border); z-index: 10; padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; font-size: .7rem; color: var(--text-2); min-height: 56px; justify-content: center; }
.tabbar a[aria-current="page"] { color: var(--brand-strong); }
.tabbar svg { width: 22px; height: 22px; }

/* ---- Komponenty ---------------------------------------------------------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--s-4); box-shadow: var(--shadow); }
.card + .card { margin-top: var(--s-4); }
.grid > .card, .grid > .card + .card { margin-top: 0; } /* w siatce odstęp daje `gap` — margines psuł wyrównanie kart w wierszu */
.card__head { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); margin-bottom: var(--s-3); }
.card__head h2 { margin: 0; }
.grid { display: grid; gap: var(--s-4); }
.grid--kpi { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid--2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.grid > * { min-width: 0; }
.grid--dash { grid-template-columns: 1fr; }
@media (min-width: 1100px) { .grid--dash { grid-template-columns: 2fr 1fr; align-items: start; } .grid--dash .card--tall { grid-row: span 2; } }
.nowrap { white-space: nowrap; }

.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--s-4); box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 4px; min-height: 104px; }
.kpi__label { font-size: .8rem; color: var(--text-2); font-weight: 500; text-transform: uppercase; letter-spacing: .04em; }
.kpi__value { font-size: 1.75rem; font-weight: 700; letter-spacing: -.02em; line-height: 1.1; }
.kpi__sub { font-size: .85rem; color: var(--text-2); }
.kpi--warn { border-left: 4px solid var(--warn); }
.kpi--danger { border-left: 4px solid var(--danger); }
.kpi--ok { border-left: 4px solid var(--ok); }
a.kpi:hover { text-decoration: none; border-color: var(--border-strong); }

.status { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status--new, .status--draft, .status--open, .status--pending { background: var(--info-soft); color: var(--info); }
.status--needs_info, .status--in_progress, .status--awaiting, .status--generated, .status--billed, .status--partial { background: var(--warn-soft); color: var(--warn); }
.status--ready, .status--done, .status--paid, .status--synced, .status--answered, .status--signed, .status--accepted, .status--matched { background: var(--ok-soft); color: var(--ok); }
.status--packaged, .status--closed, .status--cancelled, .status--rejected { background: var(--muted-soft); color: var(--text-2); }
.status--overdue, .status--error, .status--failed { background: var(--danger-soft); color: var(--danger); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2); padding: 10px 16px; min-height: var(--touch);
  border-radius: var(--radius-sm); border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn--primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn--primary:hover { background: var(--brand-strong); border-color: var(--brand-strong); }
.btn--danger { color: var(--danger); border-color: var(--danger); }
.btn--ghost { border-color: transparent; background: transparent; }
.btn--block { width: 100%; }
.btn--lg { padding: 14px 20px; font-size: 1.05rem; min-height: 56px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; }

.form { display: grid; gap: var(--s-4); }
.field { display: grid; gap: 6px; }
.field label, .label { font-weight: 600; font-size: .9rem; }
.field .hint { margin: 0; }
.input, select, textarea {
  font: inherit; padding: 10px 12px; min-height: var(--touch); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--surface); color: var(--text); width: 100%;
}
.input:focus, select:focus, textarea:focus { border-color: var(--focus); outline: 3px solid color-mix(in srgb, var(--focus) 30%, transparent); outline-offset: 0; }
.input--otp { letter-spacing: .4em; font-size: 1.4rem; text-align: center; font-family: var(--mono); }
.field--error .input, .field--error select { border-color: var(--danger); }
.errorlist { list-style: none; margin: 0; padding: 0; color: var(--danger); font-size: .875rem; }
.form__actions { display: flex; gap: var(--s-3); flex-wrap: wrap; }
.check { display: flex; align-items: center; gap: var(--s-2); min-height: var(--touch); }
.check input { width: 20px; height: 20px; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-2); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; }
.table tr:hover td { background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table a.row-link { color: inherit; font-weight: 600; }
.filters { display: flex; flex-wrap: wrap; gap: var(--s-2); align-items: end; margin-bottom: var(--s-4); }
.filters .field { min-width: 160px; }

.progress { height: 10px; background: var(--muted-soft); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--brand); border-radius: 999px; transition: width .3s; }
.progress--warn > span { background: var(--warn); }
.progress--ok > span { background: var(--ok); }
.checklist { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.checklist li { display: flex; align-items: center; gap: var(--s-3); padding: 8px 0; border-bottom: 1px solid var(--border); }
.checklist li:last-child { border-bottom: 0; }
.checklist .tick { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border-strong); display: grid; place-items: center; flex: none; font-size: .8rem; }
.checklist .is-done .tick { background: var(--ok); border-color: var(--ok); color: #fff; }

.empty { text-align: center; padding: var(--s-6) var(--s-4); color: var(--text-2); display: grid; gap: var(--s-3); justify-items: center; }
.empty svg { width: 40px; height: 40px; color: var(--text-3); }
.empty h3 { margin: 0; color: var(--text); }

.alert { padding: var(--s-3) var(--s-4); border-radius: var(--radius-sm); border: 1px solid; display: flex; gap: var(--s-3); align-items: start; }
.alert--info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.alert--success { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.alert--warning { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.alert--error, .alert--danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.messages { display: grid; gap: var(--s-2); margin-bottom: var(--s-4); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; gap: var(--s-3); padding: 10px 0; border-bottom: 1px solid var(--border); align-items: center; }
.list li:last-child { border-bottom: 0; }
.dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px var(--s-4); margin: 0; }
.dl dt { color: var(--text-2); } .dl dd { margin: 0; }

/* Logowanie */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: var(--s-4); }
.auth .card { width: 100%; max-width: 400px; padding: var(--s-6); }
.auth .brand { justify-content: center; margin-bottom: var(--s-5); }

/* PWA — Dodaj paragon */
.capture { display: grid; gap: var(--s-4); max-width: 520px; margin: 0 auto; }
.capture__drop { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: var(--s-6); text-align: center; display: grid; gap: var(--s-2); justify-items: center; background: var(--surface); }
.capture__drop input { display: none; }
.capture__preview { max-width: 100%; border-radius: var(--radius); display: none; }

/* UI library */
.swatches { display: flex; flex-wrap: wrap; gap: var(--s-2); }
.swatch { width: 96px; padding: var(--s-2); border-radius: var(--radius-sm); border: 1px solid var(--border); font-size: .7rem; }
.swatch span { display: block; height: 32px; border-radius: 4px; margin-bottom: 4px; }
.htmx-indicator { opacity: 0; transition: opacity .2s; } .htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }

@media (max-width: 480px) { .hide-xs { display: none !important; } }

/* === integracja: spójne odstępy między blokami i polami (ekrany modułów A–F) === */
.grid + .card, .grid + .grid, .card + .grid, .grid + .form__actions, .card + .form__actions, .grid + .section-title { margin-top: var(--s-4); }
.card > .field + .field, .card > .field + details, .card > details + .field, .card > .field + .check, .card > .check + .field,
.card > fieldset + .field, .card > .field + fieldset { margin-top: var(--s-3); }
.table tfoot th { text-transform: none; letter-spacing: 0; font-size: .9rem; color: var(--text); position: static; }
.table tfoot th.num { font-variant-numeric: tabular-nums; }
.list--stack li { display: block; overflow-wrap: anywhere; }
@media (max-width: 480px) { .btn { white-space: normal; max-width: 100%; text-align: center; } .card { overflow-wrap: anywhere; } }
.inline-form { display: flex; gap: var(--s-2); flex-wrap: wrap; align-items: end; margin: 0; }
