:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #e3e6eb;
  --grid-line: #e6e8ec;
  --primary: #1f5eff;
  --primary-ink: #ffffff;
  --ok: #e7f6ec;
  --ok-ink: #17643a;
  --err: #fdecec;
  --err-ink: #9b1c1c;
  --info: #e8f0ff;
  --info-ink: #1d3f8f;
  --warn: #b45309;
  --edit-bg: #fffdf2;
  --calc-bg: #f8f9fb;
  --radius: 8px;
  --sidebar: 220px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color-scheme: light;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); font-weight: normal; }
.small { font-size: .85em; }
.mono { font-family: ui-monospace, Consolas, monospace; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.center { text-align: center; }
.warn { color: var(--warn); font-weight: 600; }
.text-err { color: var(--err-ink); }
code { font-family: ui-monospace, Consolas, monospace; font-size: .9em; background: var(--bg); padding: 0 .25rem; border-radius: 4px; }

/* Układ */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: 52px; z-index: 30;
  display: flex; align-items: center; gap: 1rem; padding: 0 1rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand span { color: var(--primary); }
.topbar-user { margin-left: auto; display: flex; align-items: center; gap: 1rem; }
.topbar-user form { margin: 0; }
.nav-toggle { display: none; background: none; border: 0; font-size: 1.3rem; cursor: pointer; }
.sidebar {
  position: fixed; top: 52px; bottom: 0; left: 0; width: var(--sidebar); z-index: 20;
  background: var(--surface); border-right: 1px solid var(--border); padding: 1rem .5rem; overflow-y: auto;
}
.sidebar a { display: block; padding: .5rem .8rem; border-radius: 6px; color: var(--text); }
.sidebar a:hover { background: var(--bg); text-decoration: none; }
.sidebar a.active { background: var(--info); color: var(--info-ink); font-weight: 600; }
.sidebar a.nav-sub { padding: .3rem .8rem .3rem 1.2rem; font-size: .92rem; }
.nav-group { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; padding: 1rem .8rem .3rem; }
.settings-tabs { display: flex; flex-wrap: wrap; align-items: center; gap: .25rem; margin-bottom: 1.25rem; padding-bottom: .75rem; border-bottom: 1px solid var(--border); }
.settings-tabs-title { font-weight: 700; margin-right: .75rem; }
.settings-tabs a { padding: .35rem .75rem; border-radius: 6px; color: var(--text); }
.settings-tabs a:hover { background: var(--bg); text-decoration: none; }
.settings-tabs a.active { background: var(--info); color: var(--info-ink); font-weight: 600; }
.content { margin-left: var(--sidebar); padding: 72px 1.5rem 2rem; max-width: 1500px; }
body.wide .content { max-width: none; }

.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem; margin-bottom: 1rem; overflow-x: auto; }
.narrow-card { max-width: 680px; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 1rem; align-items: start; }
.grid-2 > .card { margin-bottom: 0; }
.section-gap { margin-top: 1.25rem; }

/* Kafelki */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem 1rem; }
.tile-label { color: var(--muted); font-size: .85rem; }
.tile-value { font-size: 1.3rem; font-weight: 700; margin-top: .25rem; font-variant-numeric: tabular-nums; }
.tile-sub { color: var(--muted); font-size: .8rem; margin-top: .3rem; }
.tile-warn { border-color: #fdba74; background: #fff7ed; }
.tile-warn .tile-value { color: var(--warn); }
a.tile { color: inherit; display: block; }
a.tile:hover { border-color: var(--primary); text-decoration: none; }

/* Formularze */
input, select, textarea, button { font: inherit; color: inherit; }
input:not([type=checkbox]):not([type=radio]), select, textarea {
  width: 100%; padding: .4rem .5rem; border: 1px solid var(--border); border-radius: 6px; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 35%, transparent); border-color: var(--primary); }
input:disabled, select:disabled, textarea:disabled { background: var(--bg); }
label { display: flex; flex-direction: column; gap: .25rem; font-weight: 500; }
label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: normal; }
.stack { display: flex; flex-direction: column; gap: .8rem; }
.row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: end; }
.row > label { flex: 1 1 160px; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem 1rem; display: flex; flex-direction: column; gap: .75rem; }
legend { font-weight: 600; padding: 0 .3rem; }
.hint { color: var(--muted); font-size: .85rem; font-weight: normal; }

.btn {
  display: inline-flex; align-items: center; gap: .3rem; padding: .45rem .9rem; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: color-mix(in srgb, var(--primary) 85%, black); }
.btn-warn { background: #fff7ed; border-color: #fdba74; color: var(--warn); }
.btn-danger { background: var(--err); border-color: #f5b5b5; color: var(--err-ink); }
.btn-ghost { background: transparent; }
.btn-small { padding: .25rem .6rem; font-size: .85rem; }
button.link { background: none; border: 0; padding: 0; color: var(--primary); cursor: pointer; }
button.link.danger { color: var(--err-ink); }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.actions form { margin: 0; }
details > summary { cursor: pointer; }
details.card > summary { font-weight: 600; }

.period { display: flex; gap: .4rem; align-items: center; margin: 0; }
.period select { width: auto; }

.badge { display: inline-block; font-size: .75rem; padding: .1rem .45rem; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.badge-open { background: var(--ok); color: var(--ok-ink); }
.badge-closed { background: #f3f4f6; color: #374151; }
.badge-admin { background: #fff7ed; color: var(--warn); }

/* Komunikaty */
.alert { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.alert-success { background: var(--ok); color: var(--ok-ink); }
.alert-error { background: var(--err); color: var(--err-ink); }
.alert-info { background: var(--info); color: var(--info-ink); }
.alert ul { margin: .3rem 0 0; padding-left: 1.2rem; }

/* Tabele */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .45rem .5rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th.num, .table td.num { text-align: right; }
.table thead th { font-size: .78rem; text-transform: uppercase; letter-spacing: .02em; color: var(--muted); font-weight: 600; white-space: nowrap; }
.table.compact td, .table.compact th { padding: .25rem .35rem; }
.table tr.inactive td { opacity: .55; }

/* ---------- Siatka arkusza ---------- */
.grid-toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: .5rem; position: relative; }
.grid-search { max-width: 260px; }
.grid-groups { position: relative; }
.grid-groups > summary { list-style: none; }
.grid-groups > summary::-webkit-details-marker { display: none; }
.grid-groups-list {
  position: absolute; z-index: 25; top: 110%; left: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.12); padding: .6rem .8rem; display: grid; gap: .3rem; min-width: 240px;
}
.grid-status { color: var(--muted); font-size: .85rem; margin-left: auto; }
.grid-status.busy { color: var(--info-ink); }
.grid-status.good { color: var(--ok-ink); }
.grid-status.bad { color: var(--err-ink); font-weight: 600; }

.grid-wrap {
  overflow: auto; max-height: calc(100vh - 150px); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.grid { border-collapse: separate; border-spacing: 0; font-size: 13px; }
.grid th, .grid td {
  border-right: 1px solid var(--grid-line); border-bottom: 1px solid var(--grid-line);
  padding: 0 .4rem; height: 28px; white-space: nowrap; background: var(--surface);
}
.grid td { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.grid td[data-k="zakladka"] select.cell, .grid td[data-k="contract"] select.cell { min-width: 112px; }
.grid thead th {
  position: sticky; z-index: 3; background: #f3f5f8; font-weight: 600; font-size: 12px; color: #374151;
  white-space: normal; vertical-align: bottom; min-width: 76px; max-width: 140px; line-height: 1.2; padding: .35rem .4rem; text-align: left;
}
.grid thead tr.grp th { top: 0; height: 24px; text-align: left; background: #e9edf3; color: #1f2937; font-size: 11px; text-transform: uppercase; letter-spacing: .03em;
  border-left: 2px solid #cbd5e1; max-width: none; white-space: nowrap; }
.grid thead tr.grp th .grp-label { position: sticky; left: 200px; }
.grid thead tr.grp th.empty { background: #f3f5f8; }
.grid thead tr.lbl th { top: 24px; }
.grid thead:not(:has(tr.grp)) tr.lbl th { top: 0; }
.grid thead th.is-edit { background: #fff7d6; }
.grid tfoot td {
  position: sticky; bottom: 0; z-index: 3; background: #eef2f7; font-weight: 700; border-top: 2px solid #cbd5e1;
}
.grid .sticky { position: sticky; left: 0; z-index: 2; min-width: 190px; max-width: 240px; overflow: hidden; text-overflow: ellipsis; box-shadow: 1px 0 0 var(--grid-line); }
.grid thead th.sticky, .grid thead th.sticky-grp { left: 0; z-index: 5; }
.grid thead th.sticky-grp { position: sticky; }
.grid tfoot td.sticky { z-index: 5; }
.grid td.is-calc { background: var(--calc-bg); }
.grid td.is-edit { background: var(--edit-bg); padding: 0; }
.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid td.err { color: var(--err-ink); background: var(--err); font-weight: 600; }
.grid tbody tr:hover td { background-color: #f2f6ff; }
.grid tbody tr.focus td { background-color: #eaf1ff; }
.grid tbody tr.focus td.is-edit { background-color: #fff6cc; }
.grid tbody tr.inactive td { color: #9ca3af; }
.grid .cell {
  width: 100%; min-width: 64px; height: 27px; border: 0 !important; border-radius: 0 !important; background: transparent !important;
  padding: 0 .4rem !important; text-align: inherit; font-size: 13px;
}
.grid td.num .cell { text-align: right; }
.grid select.cell { padding: 0 .2rem !important; min-width: 56px; }
.grid .cell:focus { outline: 2px solid var(--primary) !important; outline-offset: -2px; background: #fff !important; }
.grid td.saving { box-shadow: inset 0 0 0 2px #93c5fd; }
.grid td.saved { box-shadow: inset 0 0 0 2px #4ade80; }
.grid td.err-input { box-shadow: inset 0 0 0 2px #ef4444; background: var(--err) !important; }
.grid td.changed { animation: flash 1.2s ease-out; }
@keyframes flash { from { background-color: #fde68a; } to { } }
.grid td a { color: inherit; }

.legend { display: flex; flex-wrap: wrap; gap: 1.2rem; align-items: center; font-size: .82rem; margin-top: .6rem; color: #374151; }
.sw { display: inline-block; width: 14px; height: 14px; border: 1px solid #d1d5db; vertical-align: -2px; margin-right: .3rem; border-radius: 3px; }
.sw-edit { background: var(--edit-bg); border-color: #facc15; }
.sw-calc { background: var(--calc-bg); }
.sw-err { background: var(--err); border-color: #f5b5b5; }
.notes { line-height: 1.6; white-space: normal; }

/* Konfiguracja kolumn działu */
.cols-editor td { vertical-align: top; padding: .3rem .25rem; }
.cols-editor input, .cols-editor select, .cols-editor textarea { font-size: 13px; padding: .25rem .35rem; }
.cols-editor textarea { font-family: ui-monospace, Consolas, monospace; min-height: 34px; resize: vertical; }
.cols-editor .w-key { width: 130px; }
.cols-editor .w-sort { width: 56px; }
.cols-editor .w-sm { width: 92px; }
.cols-editor tr.kind-input td { background: #fffdf2; }
.cols-editor tr.kind-formula td { background: #f5f8ff; }
.cols-editor tr.col-error td { background: var(--err); }
.cols-editor .col-err { color: var(--err-ink); font-size: .8rem; font-weight: 600; }
.cols-editor .xl { color: var(--muted); font-size: .75rem; }
.help-names { columns: 3 220px; font-size: .82rem; }
.help-names code { white-space: nowrap; }

/* Uprawnienia */
.perm-matrix .group-row th { background: var(--bg); text-transform: none; font-size: .9rem; color: var(--text); }
.perm-matrix td.center input { width: 18px; height: 18px; }
.inline-form { display: flex; gap: .4rem; align-items: center; margin: 0; }

/* Logowanie */
.auth-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-box { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 2rem; }
.auth-box h1 { margin-bottom: 1.25rem; }
.app-footer { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border); color: var(--muted); font-size: .85rem; }
.auth-box .app-footer { margin-top: 1.5rem; text-align: center; }

/* Weryfikacja dwuetapowa */
.qr { display: flex; justify-content: center; margin: .75rem 0; }
.qr svg { width: 220px; height: 220px; max-width: 100%; border-radius: 8px; background: #fff; }
.secret-key { text-align: center; padding: .5rem; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); overflow-wrap: anywhere; }
.secret-key code, .recovery-codes code { font: 600 15px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace; letter-spacing: .04em; background: none; }
.code-input { font: 600 20px/1.2 ui-monospace, SFMono-Regular, Consolas, monospace; letter-spacing: .15em; text-align: center; }
.recovery-codes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem 1.5rem; margin: 1rem 0; padding-left: 1.5rem; }

/* Telefon */
@media (max-width: 860px) {
  .nav-toggle { display: block; }
  .sidebar { transform: translateX(-100%); transition: transform .2s; }
  body.nav-open .sidebar { transform: none; box-shadow: 0 0 30px rgba(0,0,0,.15); }
  .content { margin-left: 0; padding: 68px 16px 2rem; }
  .grid-2 { grid-template-columns: 1fr; }
  .topbar-user a { display: none; }
  .grid .sticky { min-width: 130px; max-width: 150px; }
  .grid-wrap { max-height: calc(100vh - 120px); }
}

@media print {
  .topbar, .sidebar, .no-print, .alert, .app-footer { display: none !important; }
  .content { margin: 0; padding: 0; }
  .card { border: 0; padding: 0; }
  .grid-wrap { max-height: none; overflow: visible; border: 0; }
  .grid thead th, .grid tfoot td, .grid .sticky { position: static; }
  body { background: #fff; }
}
