:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6eb;
  --border-strong: #cdd2da;
  --text: #1b2230;
  --text-2: #5b6474;
  --text-3: #8a93a3;
  --primary: #2f5bea;
  --primary-hover: #2449c7;
  --primary-soft: #edf1fe;
  --on-primary: #ffffff;
  --red: #c4320a;
  --red-bg: #fdecea;
  --orange: #b54708;
  --orange-bg: #fef0e1;
  --yellow: #8a6100;
  --yellow-bg: #fdf6d8;
  --green: #067647;
  --green-bg: #e5f6ec;
  --gray: #5b6474;
  --gray-bg: #eef0f3;
  --blue: #2449c7;
  --blue-bg: #edf1fe;
  --sidebar: #151a26;
  --sidebar-text: #a9b1c1;
  --sidebar-hover: #1f2635;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131a;
    --surface: #171c25;
    --surface-2: #1c222d;
    --border: #272e3b;
    --border-strong: #3a4353;
    --text: #e6e9ef;
    --text-2: #a3abba;
    --text-3: #7a8394;
    --primary: #5b7ff5;
    --primary-hover: #7390f7;
    --primary-soft: #1f2a47;
    --red: #f97066;
    --red-bg: #3a1d1b;
    --orange: #fdb022;
    --orange-bg: #3a2a12;
    --yellow: #eac54f;
    --yellow-bg: #2f2a14;
    --green: #47cd89;
    --green-bg: #133024;
    --gray: #a3abba;
    --gray-bg: #252b36;
    --blue: #8aa4fa;
    --blue-bg: #1f2a47;
    --sidebar: #0b0e14;
    --sidebar-hover: #171c25;
    --shadow: 0 1px 2px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
p { margin: 0 0 .75em; }
p:last-child { margin-bottom: 0; }

.icon { width: 18px; height: 18px; flex: none; }
.muted { color: var(--text-3); }
.nowrap { white-space: nowrap; }
.prewrap { white-space: pre-wrap; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.link { font-weight: 500; }
.link-quiet { color: inherit; }

/* ---------- Układ ---------- */

.app { display: flex; min-height: 100vh; }

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 232px;
  display: flex;
  flex-direction: column;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  color: var(--sidebar-text);
  padding: 18px 12px;
  z-index: 30;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 22px;
  color: #fff;
  font-weight: 700;
  font-size: 16px;
  text-decoration: none !important;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: 15px;
}

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-foot { margin-top: auto; padding-top: 12px; border-top: 1px solid rgba(255, 255, 255, .08); }
.nav form { margin: 0; }

.nav a, .nav button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  text-decoration: none;
}
.nav a span, .nav button span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav a:hover, .nav button:hover { background: var(--sidebar-hover); color: #fff; }
.nav a.active { background: var(--sidebar-hover); color: #fff; box-shadow: inset 3px 0 var(--primary); }

.main { flex: 1; min-width: 0; margin-left: 232px; }

.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 16px;
  padding: 22px 32px 6px;
}
.page-title { flex: 1; min-width: 0; }
.page-title h1 { font-size: 22px; font-weight: 650; overflow-wrap: anywhere; }
.page-title p { margin: 2px 0 0; color: var(--text-2); }
.page-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.nav-toggle { display: none; }
.backdrop { display: none; }

.content { padding: 16px 32px 48px; max-width: 1500px; }

@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); transition: transform .2s ease; }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 20; }
  .main { margin-left: 0; }
  .nav-toggle {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
  }
  .topbar { padding: 14px 16px 4px; }
  .page-actions { width: 100%; }
  .content { padding: 12px 16px 40px; }
}

/* ---------- Karty i sekcje ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card-title { font-size: 15px; font-weight: 600; margin-bottom: 14px; }

.section { margin-top: 28px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.section-head h2 { font-size: 16px; font-weight: 600; }
.section-head h2 .muted { font-weight: 400; font-size: 14px; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; align-items: start; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

.empty { text-align: center; padding: 32px 20px; color: var(--text-2); }
.empty h2 { font-size: 18px; color: var(--text); margin-bottom: 8px; }
.empty .btn, .empty .button-row { margin-top: 12px; }

.button-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: inherit; }
.empty .button-row { justify-content: center; }

.list { padding: 4px 0; }
.list-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 16px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
}
.list-row:last-child { border-bottom: 0; }

.details { margin: 0; display: grid; gap: 10px; }
.details > div { display: grid; grid-template-columns: 150px 1fr; gap: 12px; }
.details dt { color: var(--text-2); }
.details dd { margin: 0; overflow-wrap: anywhere; }
.details .badge { margin: 0 4px 4px 0; }
@media (max-width: 520px) { .details > div { grid-template-columns: 1fr; gap: 0; } }

.steps { margin: 0 0 12px; padding-left: 20px; }
.steps li { margin-bottom: 4px; }

/* ---------- Kafelki ---------- */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }

.tile {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
  text-decoration: none !important;
  border-top: 3px solid var(--tile-accent, var(--border-strong));
  transition: transform .12s ease, box-shadow .12s ease;
}
.tile:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(16, 24, 40, .08); }
.tile-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--tile-accent, var(--text)); }
.tile-label { color: var(--text-2); }
.tile-expired { --tile-accent: var(--red); }
.tile-warn { --tile-accent: var(--orange); }
.tile-notice { --tile-accent: var(--yellow); }
.tile-ok { --tile-accent: var(--green); }
.tile.is-zero .tile-value { color: var(--text-3); }
@media (max-width: 520px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .tile { padding: 12px 14px; }
  .tile-value { font-size: 24px; }
  .tile-label { font-size: 13px; line-height: 1.3; }
}

/* ---------- Tabele ---------- */

.table-wrap {
  position: relative;
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 9px 10px; text-align: left; vertical-align: middle; border-bottom: 1px solid var(--border); }
.table th {
  background: var(--surface-2);
  color: var(--text-2);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
  white-space: nowrap;
}
.table th a { color: inherit; }
.table th a.is-sorted { color: var(--text); }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: var(--surface-2); }
.table .num { text-align: right; }
.table .actions { text-align: right; width: 1%; white-space: nowrap; }
.table .notes { color: var(--text-2); max-width: 260px; }
.table .cell-main { font-weight: 550; min-width: 180px; }
.table .cell-main a { color: var(--text); }
.table .cell-main a:hover { color: var(--primary); }

.table tr.state-expired td:first-child { box-shadow: inset 3px 0 var(--red); }
.table tr.state-warn td:first-child { box-shadow: inset 3px 0 var(--orange); }
.table tr.state-notice td:first-child { box-shadow: inset 3px 0 var(--yellow); }
.table tr.state-inactive td { color: var(--text-3); }
.table tr.state-inactive .cell-main a { color: var(--text-3); }

.days {
  display: inline-block;
  min-width: 3em;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: center;
}
.days-expired { background: var(--red-bg); color: var(--red); }
.days-warn { background: var(--orange-bg); color: var(--orange); }
.days-notice { background: var(--yellow-bg); color: var(--yellow); }
.days-ok { background: var(--green-bg); color: var(--green); }
.days-inactive, .days-none { background: var(--gray-bg); color: var(--gray); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 550;
  white-space: nowrap;
}
.badge-ok { background: var(--green-bg); color: var(--green); }
.badge-info { background: var(--blue-bg); color: var(--blue); }
.badge-muted { background: var(--gray-bg); color: var(--gray); }

.result-count { color: var(--text-2); margin: 14px 2px 8px; }

/* ---------- Przyciski ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  line-height: 1.3;
  white-space: nowrap;
  cursor: pointer;
  text-decoration: none !important;
}
.btn:hover { background: var(--surface-2); }
.btn .icon { width: 16px; height: 16px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--gray-bg); }
.btn-danger { color: var(--red); border-color: var(--red); background: transparent; }
.btn-danger:hover { background: var(--red-bg); }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-icon { padding: 5px 7px; }
.btn-block { width: 100%; }

.danger-zone { padding-top: 20px; border-top: 1px dashed var(--border-strong); }

/* ---------- Formularze ---------- */

input[type=text], input[type=email], input[type=password], input[type=search], input[type=tel],
input[type=date], input[type=file], select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
  border-color: var(--primary);
}

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px 20px; }
.span-2 { grid-column: span 2; }
@media (max-width: 700px) {
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
}

.form .field + .field { margin-top: 14px; }
.form-grid .field + .field { margin-top: 0; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.label { font-weight: 550; }
.label em { color: var(--red); font-style: normal; }
.hint { display: block; color: var(--text-3); font-size: 13px; }
.field-error { color: var(--red); font-size: 13px; }
.has-error input, .has-error select, .has-error textarea { border-color: var(--red); }
.form-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px; }
.filters select, .filters input { width: auto; }
.filters .filter-search { flex: 1 1 240px; }
.filters select { flex: 0 1 200px; min-width: 0; }
@media (max-width: 700px) {
  .filters select, .filters .filter-search { flex: 1 1 100%; }
}

/* ---------- Komunikaty ---------- */

.alert {
  padding: 12px 16px;
  margin-bottom: 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
}
.alert ul { margin: 6px 0 0; padding-left: 20px; }
.alert-success { background: var(--green-bg); color: var(--green); border-color: color-mix(in srgb, var(--green) 25%, transparent); }
.alert-error { background: var(--red-bg); color: var(--red); border-color: color-mix(in srgb, var(--red) 25%, transparent); }
.card .alert { margin: 16px 0; }

/* ---------- Logowanie ---------- */

body.auth { display: grid; place-items: center; min-height: 100vh; padding: 24px 16px; }
.auth-box { width: 100%; max-width: 400px; }
.brand-auth { color: var(--text); justify-content: center; padding-bottom: 18px; }
.auth-card h1 { font-size: 20px; margin-bottom: 16px; }
.auth-card .field { margin-bottom: 14px; }
.auth-card .btn { margin-top: 6px; }
