/* ---------------------------------------------------------------- tokens */
:root {
  color-scheme: light;
  --surface-0: #f4f3f0;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #dddcd7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6e6d68;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --focus: #2a78d6;
  --neutral-fill: #e4e3df;

  /* categorical slots (fixed order) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;

  /* sequential blue */
  --q0: #e4e3df; --q1: #cde2fb; --q2: #86b6ef; --q3: #3987e5; --q4: #1c5cab; --q5: #0d366b;

  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 16px rgb(0 0 0 / .06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --header-h: 56px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-0: #121211; --surface-1: #1a1a19; --surface-2: #242422; --border: #383835;
    --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #a3a29a;
    --accent: #3987e5; --accent-ink: #86b6ef; --focus: #6da7ec; --neutral-fill: #383835;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --q0: #383835; --q1: #184f95; --q2: #256abf; --q3: #3987e5; --q4: #6da7ec; --q5: #b7d3f6;
    --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 4px 16px rgb(0 0 0 / .3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211; --surface-1: #1a1a19; --surface-2: #242422; --border: #383835;
  --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #a3a29a;
  --accent: #3987e5; --accent-ink: #86b6ef; --focus: #6da7ec; --neutral-fill: #383835;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --q0: #383835; --q1: #184f95; --q2: #256abf; --q3: #3987e5; --q4: #6da7ec; --q5: #b7d3f6;
  --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 4px 16px rgb(0 0 0 / .3);
}

/* ---------------------------------------------------------------- base */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-ink); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.15rem; margin-top: 2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75em; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.skip { position: absolute; left: -999px; top: 0; background: var(--surface-1); padding: 8px 12px; z-index: 2000; }
.skip:focus { left: 8px; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- header */
.top {
  position: sticky; top: 0; z-index: 1000;
  height: var(--header-h);
  display: flex; align-items: center; gap: 16px;
  padding: 0 16px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text-primary); text-decoration: none; min-width: 0; }
.brand svg { width: 26px; height: 26px; fill: var(--accent); flex: none; }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tabs { display: flex; gap: 4px; margin-left: auto; }
.tabs a {
  padding: 6px 12px; border-radius: 999px; text-decoration: none;
  color: var(--text-secondary); font-weight: 500; white-space: nowrap;
}
.tabs a:hover { background: var(--surface-2); color: var(--text-primary); }
.tabs a[aria-current="page"] { background: var(--text-primary); color: var(--surface-1); }
.theme { border: 1px solid var(--border); background: var(--surface-1); border-radius: 999px; width: 34px; height: 34px; display: grid; place-items: center; cursor: pointer; flex: none; }
.theme svg { width: 16px; height: 16px; fill: var(--text-secondary); }

/* ---------------------------------------------------------------- map view */
.view-mapa {
  display: grid;
  grid-template-columns: minmax(340px, 400px) 1fr;
  height: calc(100vh - var(--header-h));
  height: calc(100dvh - var(--header-h));
}
.panel { background: var(--surface-1); border-right: 1px solid var(--border); overflow-y: auto; min-height: 0; }
.panel-list, .panel-detail { padding: 16px; }
.intro h1 { font-size: 1.25rem; }
.intro p { color: var(--text-secondary); font-size: .93rem; }
.map-wrap { position: relative; min-height: 0; }
#mapa { position: absolute; inset: 0; background: var(--surface-2); }

.accuracy.discrepancy { border-left: 3px solid var(--s4); }

.credit { margin: 20px 0 4px; padding-top: 12px; border-top: 1px solid var(--border); font-size: .8rem; color: var(--text-muted); }
.credit a { color: var(--text-secondary); }

/* data dates */
.stan { display: grid; gap: 2px; margin: 10px 0 0; font-size: .82rem; color: var(--text-secondary); }
.stan div { display: flex; flex-wrap: wrap; gap: 0 6px; }
.stan dt { font-weight: 600; }
.stan dt::after { content: ":"; }
.stan dd { margin: 0; color: var(--text-primary); }
.stan .opis { color: var(--text-muted); }
.dates-table td:first-child { white-space: normal; }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; margin: 12px 0 16px; }
.tile { background: var(--surface-2); border-radius: var(--radius); padding: 10px 12px; }
.tile b { display: block; font-size: 1.15rem; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.tile span { display: block; font-size: .78rem; color: var(--text-secondary); line-height: 1.3; }

/* filters */
.filters { display: grid; gap: 12px; padding: 12px 0 16px; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.field { display: grid; gap: 4px; }
.field > span, .chips legend { font-size: .78rem; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; }
select, input[type="search"] {
  width: 100%; min-height: 38px; padding: 6px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1);
}
.chips { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chips legend { margin-bottom: 6px; padding: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px 4px 4px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-1);
  cursor: pointer; font-size: .85rem; line-height: 1.2;
}
.chip[aria-pressed="false"] { opacity: .45; }
.chip[aria-pressed="false"] .glyph { background: var(--neutral-fill) !important; }
.chip small { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.checks { display: grid; gap: 4px; font-size: .9rem; color: var(--text-secondary); }
.checks label, .check-inline { display: flex; gap: 8px; align-items: center; cursor: pointer; }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }

/* category glyph */
.glyph { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; flex: none; }
.glyph svg { width: 13px; height: 13px; fill: #fff; }

/* list */
.results-head { margin: 14px 0 8px; font-size: .9rem; color: var(--text-secondary); }
.results-head b { color: var(--text-primary); }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.list button {
  width: 100%; display: grid; grid-template-columns: 22px 1fr auto; gap: 10px; align-items: start;
  text-align: left; border: 0; background: none; padding: 8px; border-radius: 8px; cursor: pointer;
}
.list button:hover, .list button:focus-visible { background: var(--surface-2); }
.list .name { font-weight: 500; font-size: .92rem; line-height: 1.3; }
.list .meta { display: block; font-size: .8rem; color: var(--text-muted); margin-top: 2px; }
.list .amount { font-variant-numeric: tabular-nums; font-size: .9rem; font-weight: 600; white-space: nowrap; }
.badge {
  display: inline-block; font-size: .7rem; font-weight: 600; line-height: 1;
  padding: 3px 6px; border-radius: 4px; background: var(--surface-2); color: var(--text-secondary);
  vertical-align: 1px; margin-left: 4px; white-space: nowrap;
}
.badge.fs { background: color-mix(in srgb, var(--s6) 16%, transparent); color: var(--text-primary); }
.badge.plan { background: transparent; border: 1px dashed var(--text-muted); }

/* detail */
.back { display: inline-flex; gap: 6px; align-items: center; border: 0; background: none; color: var(--accent-ink); padding: 4px 0; cursor: pointer; font-weight: 500; margin-bottom: 8px; }
.detail-cat { display: flex; gap: 8px; align-items: center; font-size: .82rem; color: var(--text-secondary); margin-bottom: 6px; }
.detail h2 { font-size: 1.15rem; margin: 0 0 8px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 12px 0; font-size: .9rem; }
.kv dt { color: var(--text-secondary); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }
.section-label { font-size: .78rem; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; margin: 18px 0 6px; }
.funding { display: grid; gap: 4px; font-size: .9rem; }
.funding div { display: flex; justify-content: space-between; gap: 12px; }
.funding .sub { padding-left: 14px; color: var(--text-secondary); font-size: .85rem; }
.sources { margin: 0; padding-left: 18px; font-size: .88rem; }
.sources li { margin-bottom: 4px; }
.accuracy { font-size: .85rem; color: var(--text-secondary); background: var(--surface-2); border-radius: 8px; padding: 8px 10px; margin: 10px 0; }
.fs-tasks { list-style: none; padding: 0; margin: 0; font-size: .88rem; display: grid; gap: 6px; }
.fs-tasks li { display: grid; grid-template-columns: 10px 1fr auto; gap: 8px; align-items: baseline; }
.fs-tasks .dot { width: 10px; height: 10px; border-radius: 3px; transform: translateY(1px); }
.fs-tasks .amt { font-variant-numeric: tabular-nums; white-space: nowrap; }
.report { display: inline-block; margin-top: 16px; font-size: .88rem; }

/* year bars (detail) */
.yearbars { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; align-items: end; height: 96px; margin-top: 6px; }
.yearbars .yb { display: grid; grid-template-rows: 1fr auto; height: 100%; gap: 4px; }
.yearbars .yb i { align-self: end; display: block; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 0; }
.yearbars .yb i.zero { background: none; border-bottom: 1px solid var(--border); border-radius: 0; height: 1px !important; }
.yearbars .yb span { font-size: .66rem; color: var(--text-muted); text-align: center; font-variant-numeric: tabular-nums; }
.yearbars .yb:hover i:not(.zero) { filter: brightness(1.15); }
/* execution (wykonanie): spent amounts are dark, planned ones light */
.yearbars .yb.done i:not(.zero) { background: var(--accent-ink); }
.yearbars .yb.done span { color: var(--text-primary); font-weight: 600; }
.yb-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; margin: 6px 0 0; font-size: .75rem; color: var(--text-muted); }
.yb-legend .sw { width: 10px; height: 10px; border-radius: 2px; background: var(--accent); display: inline-block; margin-left: 6px; }
.yb-legend .sw:first-child { margin-left: 0; }
.yb-legend .sw.done { background: var(--accent-ink); }
.exec-bar { height: 8px; border-radius: 4px; background: color-mix(in srgb, var(--accent) 22%, var(--surface-1)); overflow: hidden; margin: 2px 0 8px; }
.exec-bar i { display: block; height: 100%; background: var(--accent-ink); border-radius: 4px; }
.funding b small { font-weight: 400; color: var(--text-secondary); }
.exec-opis { margin: 10px 0 0; padding: 8px 12px; border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 0 8px 8px 0; font-size: .86rem; color: var(--text-secondary); }
.exec-opis p { margin: 0 0 6px; color: var(--text-primary); }
.exec-opis cite { display: block; font-style: normal; font-size: .78rem; }
.fs-tasks .amt small { color: var(--text-muted); font-weight: 400; }
.tiles.cztery { grid-template-columns: repeat(2, minmax(0, 1fr)); }

/* legend (map) */
.legend {
  position: absolute; right: 12px; bottom: 24px; z-index: 500;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 10px 12px; font-size: .8rem; max-width: 260px;
}
.legend h4 { margin: 0 0 6px; font-size: .78rem; color: var(--text-secondary); font-weight: 600; }
.legend .row { display: flex; align-items: center; gap: 8px; margin: 3px 0; }
.legend .sw { width: 18px; height: 12px; border-radius: 3px; flex: none; border: 1px solid rgb(0 0 0 / .08); }
.legend .mk { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.legend .mk.filled { background: var(--text-secondary); }
.legend .mk.hollow { border: 2px solid var(--text-secondary); background: var(--surface-1); }
.legend .mk.approx { border: 2px dashed var(--text-secondary); background: var(--surface-1); }
.legend-toggle { display: none; }

/* map markers */
.mk-icon { background: none; border: 0; }
.mk {
  display: grid; place-items: center; width: 100%; height: 100%; border-radius: 50%;
  box-shadow: 0 0 0 2px var(--surface-1), 0 1px 4px rgb(0 0 0 / .35);
  transition: transform .12s;
}
.mk svg { width: 55%; height: 55%; fill: #fff; }
.mk.hollow { background: var(--surface-1) !important; border: 2.5px solid var(--c); }
.mk.hollow svg { fill: var(--c); }
.mk.approx { outline: 2px dashed var(--text-secondary); outline-offset: 2px; }
.mk.fs::after {
  content: ""; position: absolute; right: -2px; top: -2px; width: 9px; height: 9px; border-radius: 50%;
  background: var(--s6); box-shadow: 0 0 0 2px var(--surface-1);
}
.mk-icon:hover .mk, .mk.active { transform: scale(1.18); }
.mk.active { box-shadow: 0 0 0 3px var(--surface-1), 0 0 0 5px var(--text-primary); }
.rejon-label { background: none; border: 0; box-shadow: none; color: var(--text-secondary); font-size: 11px; font-weight: 600; text-shadow: 0 0 3px var(--surface-1), 0 0 3px var(--surface-1), 0 0 3px var(--surface-1); padding: 0; }
.rejon-label::before { display: none; }

/* leaflet theming */
.leaflet-container { font: inherit; background: var(--surface-2); }
.leaflet-tooltip { background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: 8px; }
.leaflet-tooltip-top::before { border-top-color: var(--surface-1); }
.leaflet-bar a, .leaflet-control-layers { background: var(--surface-1); color: var(--text-primary); border-color: var(--border); }
.leaflet-control-attribution { background: color-mix(in srgb, var(--surface-1) 85%, transparent) !important; color: var(--text-secondary); }
.leaflet-control-attribution a { color: var(--accent-ink); }
.tiles-osm { filter: grayscale(.85) contrast(.92) brightness(1.04); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tiles-osm { filter: invert(1) hue-rotate(180deg) grayscale(.8) brightness(.85) contrast(.9); }
}
:root[data-theme="dark"] .tiles-osm { filter: invert(1) hue-rotate(180deg) grayscale(.8) brightness(.85) contrast(.9); }

/* ---------------------------------------------------------------- pages */
.page { overflow-y: auto; }
.page-inner { max-width: 980px; margin: 0 auto; padding: 24px 16px 64px; }
.lead { color: var(--text-secondary); max-width: 72ch; }
.controls-row { display: flex; flex-wrap: wrap; gap: 12px 16px; align-items: end; margin: 16px 0; }
.controls-row .field { min-width: 200px; }
.check-inline { font-size: .9rem; color: var(--text-secondary); min-height: 38px; }
.chart { margin: 8px 0 16px; background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.chart figcaption { font-weight: 600; margin-bottom: 12px; }
.note { font-size: .85rem; color: var(--text-secondary); margin: 12px 0 0; }

/* horizontal bars */
.bars { display: grid; gap: 4px; position: relative; padding-top: 24px; }
.bar-row { display: grid; grid-template-columns: minmax(110px, 220px) 1fr; gap: 12px; align-items: center; font-size: .88rem; }
.bar-row .lbl { text-align: right; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row .track { position: relative; height: 22px; display: flex; align-items: center; gap: 6px; }
.bar-row .fill { height: 16px; background: var(--accent); border-radius: 0 4px 4px 0; min-width: 0; }
.bar-row .fill.vs-plan { background: linear-gradient(to right, var(--accent-ink) var(--done, 100%), color-mix(in srgb, var(--accent) 28%, var(--surface-1)) var(--done, 100%)); }
.bar-row .val { font-variant-numeric: tabular-nums; font-size: .82rem; color: var(--text-primary); white-space: nowrap; }
.bar-row:hover .lbl { color: var(--text-primary); }
.bar-row:hover .fill { filter: brightness(1.12); }
.bar-row.zero .val { color: var(--text-muted); }
.avg-line { position: absolute; top: 20px; bottom: 0; border-left: 2px dashed var(--text-secondary); pointer-events: none; }
.avg-line span { position: absolute; top: -20px; left: 4px; font-size: .75rem; color: var(--text-secondary); white-space: nowrap; background: var(--surface-1); padding: 0 4px; }

/* stacked bars */
.legend-row { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: .82rem; margin-bottom: 12px; color: var(--text-secondary); }
.legend-row span { display: inline-flex; align-items: center; gap: 6px; }
.legend-row i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.stack { display: grid; gap: 4px; }
.stack-row { display: grid; grid-template-columns: minmax(110px, 180px) 1fr auto; gap: 12px; align-items: center; font-size: .86rem; }
.stack-row .lbl { text-align: right; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stack-row .segs { display: flex; gap: 2px; height: 16px; }
.stack-row .segs i { display: block; height: 100%; min-width: 2px; }
.stack-row .segs i:first-child { border-radius: 4px 0 0 4px; }
.stack-row .segs i:last-child { border-radius: 0 4px 4px 0; }
.stack-row .segs i:only-child { border-radius: 4px; }
.stack-row .segs i:hover { filter: brightness(1.15); }
.stack-row .tot { font-variant-numeric: tabular-nums; font-size: .82rem; white-space: nowrap; }

/* table */
.table-wrap { margin: 0 0 24px; }
.table-wrap summary { cursor: pointer; color: var(--accent-ink); font-weight: 500; margin-bottom: 8px; }
.scroll-x { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: .86rem; background: var(--surface-1); }
th, td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
th:first-child, td:first-child { text-align: left; white-space: normal; }
th { font-size: .78rem; color: var(--text-secondary); cursor: pointer; user-select: none; }
th[aria-sort]::after { content: " ↕"; color: var(--text-muted); }
th[aria-sort="descending"]::after { content: " ↓"; color: var(--text-primary); }
th[aria-sort="ascending"]::after { content: " ↑"; color: var(--text-primary); }

/* prose */
.prose { max-width: 760px; }
.prose li { margin-bottom: .4em; }
.prose .callout { background: var(--surface-1); border: 1px solid var(--border); border-left: 4px solid var(--accent); border-radius: 8px; padding: 12px 16px; margin: 16px 0; }
.acc-list { display: grid; gap: 6px; padding: 0; list-style: none; }
.acc-list li { display: grid; grid-template-columns: 140px 1fr; gap: 12px; }

/* tooltip */
.tooltip {
  position: fixed; z-index: 3000; pointer-events: none;
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  padding: 8px 10px; font-size: .82rem; max-width: 280px;
}
.tooltip b { font-variant-numeric: tabular-nums; }
.tooltip .muted { color: var(--text-secondary); }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 900px) {
  .view-mapa { display: flex; flex-direction: column-reverse; height: auto; }
  .map-wrap { height: 62vh; height: 62dvh; flex: none; }
  .panel { border-right: 0; border-top: 1px solid var(--border); overflow: visible; }
  .legend { right: 8px; bottom: 20px; max-width: 200px; padding: 8px 10px; }
  .legend.collapsed .legend-body { display: none; }
  .legend-toggle { display: block; border: 0; background: none; padding: 0; font-weight: 600; font-size: .78rem; color: var(--text-secondary); cursor: pointer; }
}
@media (max-width: 560px) {
  .top { gap: 8px; padding: 0 12px; }
  .brand span { display: none; }
  .tabs a { padding: 6px 10px; }
  .bar-row { grid-template-columns: 96px 1fr; gap: 8px; }
  .stack-row { grid-template-columns: 90px 1fr; }
  .stack-row .tot { grid-column: 2; }
  .acc-list li { grid-template-columns: 1fr; gap: 0; }
}
