/* CFM UI kit. Rules and components: docs/ui.md. Style guide: /admin/_ui (dev only).
   Every size below comes from these tokens; don't add one-off values. */

:root {
  /* colours */
  --bg: #f5f5f3;
  --panel: #ffffff;
  --text: #1c1c1a;
  --muted: #6f6f69;
  --border: #e0e0da;
  --accent: #2a55c9;
  --accent-text: #ffffff;
  --danger: #b8322a;
  --danger-text: #ffffff;
  --ok: #2c7a4b;
  --warn: #a8670d;
  --hover: rgba(127, 127, 127, 0.07);
  --sidebar: #1f2430;
  --sidebar-text: #c9cdd6;
  --sidebar-active: #ffffff;

  /* spacing scale */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 24px;
  --s6: 32px;

  /* shape and controls */
  --radius: 10px;
  --radius-sm: 6px;
  --control-h: 36px;
  --control-h-sm: 28px;
  --font-sm: 0.85rem;
  --font-xs: 0.8rem;

  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141414;
    --panel: #1d1d1c;
    --text: #ebebe6;
    --muted: #9b9b94;
    --border: #32322f;
    --accent: #7d9cf2;
    --accent-text: #10131a;
    --danger: #ef7d74;
    --danger-text: #1a0f0e;
    --ok: #6cc896;
    --warn: #e6ad55;
    --sidebar: #0f1116;
  }
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; line-height: 1.25; margin: 0; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: 0.95rem; margin: 0 0 var(--s2); }
p { margin: 0 0 var(--s3); }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
pre {
  white-space: pre-wrap; word-break: break-word; font-size: var(--font-xs);
  background: var(--bg); padding: var(--s3); border-radius: var(--radius-sm); max-height: 300px; overflow: auto;
}
details > summary { cursor: pointer; }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
legend { padding: 0; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
/* The sidebar is pinned to the viewport: the links scroll inside it when they don't fit, and the
   signed-in user with "Wyloguj" always stays visible at the bottom. */
.sidebar {
  position: sticky; top: 0; height: 100vh; height: 100dvh;
  background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; padding: var(--s5) var(--s3) var(--s4);
}
.brand { color: var(--sidebar-active); font-weight: 700; font-size: 1.1rem; padding: 0 var(--s3) var(--s4); flex: none; }
.nav {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  display: flex; flex-direction: column; gap: var(--s4); margin: 0 calc(-1 * var(--s1)); padding: 0 var(--s1);
  scrollbar-width: thin; scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
  /* Shadows at the top or bottom edge appear only while there is more to scroll to. */
  background:
    linear-gradient(var(--sidebar) 30%, transparent) center top / 100% var(--s5) no-repeat local,
    linear-gradient(transparent, var(--sidebar) 70%) center bottom / 100% var(--s5) no-repeat local,
    linear-gradient(rgba(0, 0, 0, 0.45), transparent) center top / 100% var(--s3) no-repeat scroll,
    linear-gradient(transparent, rgba(0, 0, 0, 0.45)) center bottom / 100% var(--s3) no-repeat scroll;
}
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-heading {
  padding: 0 var(--s3) var(--s1); font-size: var(--font-xs); font-weight: 600;
  letter-spacing: 0.04em; text-transform: uppercase; color: rgba(255, 255, 255, 0.45);
}
.sidebar a {
  color: var(--sidebar-text); text-decoration: none; padding: var(--s2) var(--s3); border-radius: var(--radius-sm);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sidebar a:hover { background: rgba(255, 255, 255, 0.06); }
.sidebar a.active { color: var(--sidebar-active); background: rgba(255, 255, 255, 0.1); }
.sidebar .user {
  flex: none; margin-top: var(--s3); padding-top: var(--s3); border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: grid; gap: var(--s1); font-size: 0.9rem;
}
.sidebar .user .btn-link { color: var(--sidebar-text); padding: 0 var(--s3); }
.content { padding: var(--s6) var(--s6) 64px; max-width: 1240px; width: 100%; }
/* Short windows: tighter spacing so more links fit before the list scrolls. */
@media (max-height: 640px) and (min-width: 761px) {
  .sidebar { padding-top: var(--s4); }
  .brand { padding-bottom: var(--s2); }
  .nav { gap: var(--s2); }
  .sidebar a { padding: var(--s1) var(--s3); }
}
/* Phones: a top bar (brand and user) with the links in one horizontally scrolling row. */
@media (max-width: 760px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center;
    padding: var(--s2) var(--s3); gap: var(--s2);
  }
  .brand { padding: 0 var(--s3) 0 0; }
  .sidebar .user { order: 2; margin: 0 0 0 auto; padding: 0; border: 0; display: flex; align-items: center; }
  .nav {
    order: 3; flex: 1 0 100%; flex-direction: row; gap: var(--s3); overflow-x: auto; overflow-y: hidden;
    margin: 0; padding: 0 0 var(--s1);
  }
  .nav-group { flex-direction: row; flex: none; }
  .nav-heading { display: none; }
  .nav { background: none; }
  .nav-group + .nav-group { padding-left: var(--s3); border-left: 1px solid rgba(255, 255, 255, 0.1); }
  .content { padding: var(--s5) var(--s4) 48px; }
}

/* ---------- page header ---------- */
.page-header {
  display: flex; justify-content: space-between; align-items: flex-end; gap: var(--s4);
  flex-wrap: wrap; margin-bottom: var(--s5);
}
.page-header-main { display: grid; gap: var(--s1); min-width: 0; }
.page-back { font-size: var(--font-sm); text-decoration: none; }
.page-subtitle { color: var(--muted); margin: 0; }
.page-badges { display: flex; gap: var(--s2); flex-wrap: wrap; margin-top: var(--s1); }
.page-actions { display: flex; gap: var(--s2); flex-wrap: wrap; }

/* ---------- cards and layout ---------- */
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--s4) var(--s5) var(--s5); margin-bottom: var(--s5); min-width: 0;
}
.card-header {
  display: flex; justify-content: space-between; align-items: center; gap: var(--s3);
  min-height: var(--control-h-sm); margin-bottom: var(--s4);
}
.card-subtitle { color: var(--muted); font-size: var(--font-sm); margin: var(--s1) 0 0; }
.card-actions { display: flex; gap: var(--s2); flex-wrap: wrap; }
.section { margin-top: var(--s5); }
.section:first-child { margin-top: 0; }
.columns, .grid { display: grid; gap: var(--s5); margin-bottom: var(--s5); }
.columns { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
.grid { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.columns > .card, .grid > .card { margin-bottom: 0; }
@media (max-width: 960px) { .columns { grid-template-columns: minmax(0, 1fr); } }

/* ---------- buttons: two fixed heights, four variants ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  height: var(--control-h); padding: 0 var(--s4);
  font: inherit; font-weight: 500; line-height: 1; white-space: nowrap; text-decoration: none;
  border: 1px solid transparent; border-radius: var(--radius-sm); cursor: pointer;
}
.btn-small { height: var(--control-h-sm); padding: 0 var(--s3); font-size: var(--font-sm); }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-secondary { background: transparent; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--hover); }
.btn-danger { background: var(--danger); color: var(--danger-text); }
.btn-link { background: none; border: 0; height: auto; padding: 0; color: var(--accent); font-weight: 400; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.disclosure > summary { list-style: none; }
.disclosure > summary::-webkit-details-marker { display: none; }
.disclosure[open] > summary { margin-bottom: var(--s4); }
.disclosure-body { border-top: 1px solid var(--border); padding-top: var(--s4); }

/* ---------- forms ---------- */
.form { display: grid; gap: var(--s4); max-width: 760px; }
.field-row { display: grid; gap: var(--s4) var(--s5); grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.field { display: grid; gap: var(--s1); align-content: start; }
.field .label { font-size: var(--font-sm); color: var(--muted); }
.field .req { color: var(--danger); margin-left: 2px; }
.field .error { color: var(--danger); font-size: var(--font-sm); }
.field .hint { color: var(--muted); font-size: var(--font-sm); }
input, select, textarea {
  font: inherit; color: inherit; width: 100%;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 0 var(--s3);
}
input, select { height: var(--control-h); }
textarea { padding: var(--s2) var(--s3); resize: vertical; }
input[type=file] { height: auto; padding: var(--s2) 0; border: 0; background: none; }
input.invalid, select.invalid, textarea.invalid { border-color: var(--danger); }
.form-actions { display: flex; gap: var(--s2); flex-wrap: wrap; padding-top: var(--s1); }
.checkbox-list { display: flex; flex-wrap: wrap; gap: var(--s2) var(--s5); }
.check { display: inline-flex; gap: var(--s2); align-items: center; color: var(--text); }
.check input { width: auto; height: auto; }
.copy-field { display: flex; gap: var(--s2); align-items: center; flex-wrap: wrap; }
.copy-field input { flex: 1; min-width: 220px; height: var(--control-h-sm); font-size: var(--font-sm); }

/* ---------- lists: toolbar, tables, pagination ---------- */
.toolbar { display: flex; gap: var(--s3); align-items: center; flex-wrap: wrap; margin-bottom: var(--s4); }
.toolbar .search { max-width: 340px; }
.toolbar .filter { width: auto; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: var(--s2) var(--s3); border-bottom: 1px solid var(--border); vertical-align: top; }
.table th {
  font-size: var(--font-xs); color: var(--muted); font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.03em; white-space: nowrap;
}
.table tbody tr:hover { background: var(--hover); }
.table .num { text-align: right; white-space: nowrap; }
.table .empty { color: var(--muted); padding: var(--s5) var(--s3); text-align: center; }
.table td .muted { font-size: var(--font-sm); }
.row-actions { width: 1%; white-space: nowrap; }
.row-actions-inner { display: flex; gap: var(--s2); justify-content: flex-end; }
.pagination { display: flex; gap: var(--s3); align-items: center; justify-content: flex-end; margin-top: var(--s3); color: var(--muted); }

/* ---------- data display ---------- */
.facts { display: grid; grid-template-columns: max-content 1fr; gap: var(--s2) var(--s5); margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.badge {
  display: inline-flex; align-items: center; height: 22px; padding: 0 var(--s2);
  border-radius: 999px; border: 1px solid currentColor; font-size: var(--font-xs); white-space: nowrap;
}
.badge-ok { color: var(--ok); }
.badge-warn { color: var(--warn); }
.badge-bad { color: var(--danger); }
.badge-neutral { color: var(--muted); }
.alert { padding: var(--s3) var(--s4); border-radius: var(--radius-sm); margin-bottom: var(--s4); border: 1px solid; }
.alert-ok { color: var(--ok); border-color: var(--ok); }
.alert-warn { color: var(--warn); border-color: var(--warn); }
.alert-bad { color: var(--danger); border-color: var(--danger); }
.alert-neutral { color: var(--muted); border-color: var(--border); }
.status-list { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--s2); }
.status-list li { display: flex; gap: var(--s3); align-items: baseline; flex-wrap: wrap; }
.history { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--s3); font-size: 0.9rem; }
.history .diff { color: var(--muted); font-size: var(--font-sm); word-break: break-word; }

/* ---------- auth pages ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--s4); }
.auth { width: 100%; max-width: 400px; }
.brand-large { font-weight: 700; font-size: 1.4rem; text-align: center; margin-bottom: var(--s4); }
.auth .form { max-width: none; }
.auth-links { margin: var(--s4) 0 0; font-size: var(--font-sm); }
.steps { padding-left: var(--s5); display: grid; gap: var(--s3); margin: 0 0 var(--s4); }
.secret { font-size: 1.05rem; letter-spacing: 0.04em; margin-top: var(--s2); user-select: all; }

/* ---------- System page ---------- */
.spark { width: 100%; height: 48px; display: block; }
.spark polyline { fill: none; stroke: var(--accent); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.chart-value { font-size: 1.4rem; font-weight: 600; margin-bottom: var(--s2); }
.job-desc summary { color: var(--accent); font-size: var(--font-sm); }
.job-desc p { max-width: 520px; margin: var(--s2) 0 var(--s1); font-size: 0.9rem; }
.job-result { font-size: var(--font-sm); max-width: 420px; overflow-wrap: anywhere; }

/* ---------- style guide ---------- */
.swatches { display: flex; gap: var(--s3); flex-wrap: wrap; align-items: center; }
