:root {
  --bg: #f4f6f4;
  --panel: #ffffff;
  --text: #1c2230;
  --text-muted: #6b7280;
  --border: #e2e7e1;
  --accent: #1e7a3d;
  --accent-dark: #145c2c;
  --accent-light: #e7f4ea;
  --row-hover: #eaf5ec;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(20, 25, 40, 0.08);
  --sidebar-w: 220px;
  --topbar-h: 56px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }

/* ---------- Login ---------- */
.login-body {
  display: flex; align-items: center; justify-content: center; min-height: 100vh;
  background: linear-gradient(160deg, #0f4a24, #1e7a3d 60%, #3fa15e);
}
.login-card {
  background: var(--panel); border-radius: var(--radius); padding: 2rem;
  width: 100%; max-width: 360px; box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.login-hint { color: var(--text-muted); font-size: .9rem; margin-top: -.5rem; }
.login-form { display: flex; flex-direction: column; gap: .6rem; margin-top: 1rem; }
.login-form label { font-size: .85rem; color: var(--text-muted); }
.login-form input {
  padding: .6rem .7rem; border: 1px solid var(--border); border-radius: 8px; font-size: 1rem;
}
.login-form button {
  margin-top: .5rem; padding: .7rem; border: none; border-radius: 8px;
  background: var(--accent); color: #fff; font-size: 1rem; cursor: pointer;
}
.login-form button:hover { background: var(--accent-dark); }
.login-footnote { font-size: .8rem; color: var(--text-muted); margin-top: 1rem; }
.alert { padding: .6rem .8rem; border-radius: 8px; font-size: .9rem; margin: .5rem 0; }
.alert-error { background: #fdecea; color: #a12a1e; }

/* ---------- App-Shell ---------- */
.app-shell { min-height: 100vh; }
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h);
  background: var(--accent); color: #fff; display: flex; align-items: center;
  gap: 1rem; padding: 0 1rem; z-index: 30; box-shadow: var(--shadow);
}
.topbar-title { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-toggle { display: none; background: none; border: none; color: #fff; font-size: 1.3rem; cursor: pointer; }
.jahr-switch select {
  padding: .3rem .5rem; border-radius: 6px; border: none; font-size: .9rem;
}
.topbar-user { display: flex; align-items: center; gap: .7rem; font-size: .9rem; white-space: nowrap; }
.topbar-user a { color: #dce8fb; }

.sidebar {
  position: fixed; top: var(--topbar-h); left: 0; bottom: 0; width: var(--sidebar-w);
  background: #101828; overflow-y: auto; z-index: 20;
}
.sidebar ul { list-style: none; margin: 0; padding: .75rem 0; }
.sidebar li a {
  display: block; padding: .6rem 1.2rem; color: #c9d2e3; font-size: .92rem;
}
.sidebar li a:hover { background: #1a2438; text-decoration: none; }
.sidebar li a.active { background: var(--accent); color: #fff; font-weight: 600; }
.sidebar li.nav-sep {
  margin-top: .6rem; padding: .5rem 1.2rem .3rem; font-size: .72rem; text-transform: uppercase;
  letter-spacing: .05em; color: #6b7690; border-top: 1px solid #1e2740;
}

.content {
  margin-left: var(--sidebar-w); margin-top: var(--topbar-h); padding: 1.25rem 1.5rem 3rem;
  max-width: 1400px;
}

@media (max-width: 900px) {
  .nav-toggle { display: block; }
  .sidebar { transform: translateX(-100%); transition: transform .2s ease; width: 240px; }
  .sidebar.open { transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,.3); }
  .content { margin-left: 0; padding: 1rem; }
  .topbar-title { display: none; }
}

/* ---------- Bausteine ---------- */
.panel {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 1rem 1.1rem; margin-bottom: 1.1rem;
}
.panel h2, .panel h3 { margin-top: 0; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

.kpi { text-align: center; padding: .8rem; }
.kpi .value { font-size: 1.7rem; font-weight: 700; }
.kpi .label { color: var(--text-muted); font-size: .82rem; }

table { border-collapse: collapse; width: max-content; min-width: 100%; font-size: .92rem; }
th, td { padding: .45rem .6rem; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--text-muted); font-weight: 600; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; }
tbody tr { transition: background-color .1s ease; }
tbody tr:hover td { background: var(--row-hover); }
/* width:100% wuerde den Browser dazu bringen, Spalten zusammenzuquetschen statt
   die Tabelle breiter als den Container werden zu lassen -- mit max-content
   greift stattdessen das overflow-x:auto unten und es erscheint eine
   Scrollleiste, sobald (z. B. durch die "Alle Ergebnisse"-Spalten) mehr
   Platz gebraucht wird, als der Bildschirm hergibt. */
.table-wrap { overflow-x: auto; max-width: 100%; }

/* Fuer sehr lange/breite Tabellen (z. B. mit eingeblendeten "Alle
   Parteiergebnisse"-Spalten, oder die Kandidatentabelle): Kopfzeile bleibt
   beim vertikalen Scrollen sichtbar, und die horizontale Scrollleiste ist
   immer erreichbar, weil der Rahmen selbst eine begrenzte Hoehe hat statt
   beliebig mitzuwachsen. */
.table-wrap.scroll-fixed { max-height: 68vh; overflow: auto; }
/* Muss table.sortable th[data-sort-key] { position: relative } (hoehere
   Spezifitaet) ueberstimmen, daher der zusammengesetzte Selektor. */
.scroll-fixed thead th,
.scroll-fixed table.sortable thead th[data-sort-key] {
  position: sticky; top: 0; z-index: 2; background: var(--panel);
  box-shadow: 0 1px 0 var(--border);
}
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Sortierbare Tabellen ---------- */
table.sortable th[data-sort-key] { cursor: pointer; user-select: none; position: relative; padding-right: 1.3rem; }
table.sortable th[data-sort-key]:hover { color: var(--text); }
table.sortable th[data-sort-key]::after { content: '↕'; position: absolute; right: .4rem; opacity: .35; font-size: .85em; }
table.sortable th[data-sort-key].sort-asc::after { content: '↑'; opacity: 1; color: var(--accent); }
table.sortable th[data-sort-key].sort-desc::after { content: '↓'; opacity: 1; color: var(--accent); }

/* ---------- Erweiterbare Tabellenzeilen ---------- */
tr.row-expandable { cursor: pointer; }
tr.row-expandable td:first-child::before { content: '▸'; display: inline-block; width: 1em; color: var(--text-muted); transition: transform .15s ease; }
tr.row-expandable.open td:first-child::before { transform: rotate(90deg); }
tr.detail-row { background: var(--bg); }
tr.detail-row > td { padding: .8rem 1rem 1rem 2rem; border-bottom: 1px solid var(--border); }
tr.detail-row table { font-size: .88rem; }
tr.detail-row .loading { color: var(--text-muted); font-size: .85rem; }

.party-chip {
  display: inline-flex; align-items: center; gap: .4rem; padding: .15rem .5rem;
  border-radius: 999px; font-size: .82rem; color: #fff;
}
.party-dot { width: .7rem; height: .7rem; border-radius: 50%; display: inline-block; flex: none; }
.bar-row { display: flex; align-items: center; gap: .6rem; margin: .35rem 0; }
.bar-label { width: 130px; flex: none; font-size: .85rem; }
.bar-track { flex: 1; background: #eef0f3; border-radius: 6px; height: 14px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 6px; }
.bar-value { width: 60px; flex: none; text-align: right; font-size: .85rem; font-variant-numeric: tabular-nums; }

.filters { display: flex; flex-wrap: wrap; gap: .6rem; margin-bottom: 1rem; align-items: end; }
.filters label { display: block; font-size: .78rem; color: var(--text-muted); margin-bottom: .2rem; }
.filters select, .filters input {
  padding: .45rem .55rem; border: 1px solid var(--border); border-radius: 6px; font-size: .9rem;
}

.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .72rem; font-weight: 600; }
.badge-exakt { background: #e3f6e8; color: #1c7a3c; }
.badge-hoch { background: #eaf1fb; color: #1a4d8f; }
.badge-mittel { background: #f3eefb; color: #5b3a94; }
.badge-niedrig { background: #fdf3e0; color: #9a6414; }

.diff-pos { color: #1c7a3c; font-weight: 600; }
.diff-neg { color: #b3261e; font-weight: 600; }

.map-container { height: 560px; border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }

/* Chart.js skaliert ohne festen Container sonst per Standard-Seitenverhaeltnis
   auf die volle Panel-Breite -- bei einem vollbreiten Panel (z. B. "Vergleich
   Vorjahr", "Entwicklung Zweitstimmenanteil") wird der Chart dadurch sehr
   hoch und wirkt wie ein grosser Weissraum. Fester Container + responsive:true,
   maintainAspectRatio:false (siehe jeweiliges Chart.js new Chart(...)) halten
   die Hoehe konstant. */
.chart-box { position: relative; height: 260px; }
.chart-box.chart-box-sm { height: 220px; }
.legend { background: #fff; padding: .5rem .7rem; border-radius: 8px; box-shadow: var(--shadow); font-size: .82rem; }
.legend div { display: flex; align-items: center; gap: .4rem; margin: .15rem 0; }

.pill-tabs { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: 1rem; }
.pill-tabs a {
  padding: .35rem .8rem; border-radius: 999px; background: #eef0f3; color: var(--text); font-size: .85rem;
}
.pill-tabs a.active { background: var(--accent); color: #fff; }

.muted { color: var(--text-muted); }
.small { font-size: .82rem; }
.text-right { text-align: right; }

form.inline { display: inline; }
.btn {
  display: inline-block; padding: .45rem .9rem; border-radius: 6px; border: 1px solid var(--border);
  background: #fff; cursor: pointer; font-size: .88rem;
}
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-danger { background: #fdecea; color: #a12a1e; border-color: #f2c6c1; }

th.col-erweitert, td.col-erweitert { background: var(--accent-light); }
td.col-erweitert.diff-pos { background: #d9f0df; }
td.col-erweitert.diff-neg { background: #fbe1de; }

/* ---------- Ergaenzungen: Ergebnistabellen, Potenzial, Empfehlungen ---------- */
.filters.panel { align-items: flex-start; }
.filters-break { flex-basis: 100%; }
.check-group { display: flex; flex-wrap: wrap; gap: .25rem .8rem; align-items: center; }
.check { display: inline-flex !important; align-items: center; gap: .3rem; font-size: .85rem !important; color: var(--text) !important; margin: 0 !important; white-space: nowrap; }
.btn-sm { padding: .15rem .5rem; font-size: .78rem; }
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; }
.panel-head h2 { margin-bottom: .3rem; }
.legend-swatch { padding: 0 .35rem; border-radius: 4px; color: #1c2230; }
.heat-table td.heat { font-weight: 600; }
.heat-table th.th-party { color: var(--text); }
tr.row-ref td { background: #f1f3f1; font-weight: 600; border-top: 2px solid var(--border); }
tr.row-land td { background: #e7ece7; }
.scroll-fixed tfoot td { position: sticky; bottom: 0; z-index: 1; }
.scroll-fixed tfoot tr.row-ref:not(.row-land) td { bottom: 2.1rem; }

.badge-hochburg { background: #d9f0df; color: #145c2c; }
.badge-wachstum { background: #e3efff; color: #1a4d8f; }
.badge-mobilisierung { background: #fff1d6; color: #8a5a00; }
.badge-aufbau { background: #f1f1f1; color: #555; }

.kpi .delta { font-size: .85rem; font-weight: 600; }
.kpi .delta.pos { color: #1c7a3c; }
.kpi .delta.neg { color: #b3261e; }
.chart-box.chart-box-lg { height: 360px; }
.chart-box.chart-box-half { height: 240px; }

.callout { border-left: 4px solid var(--accent); background: var(--accent-light); padding: .8rem 1rem; border-radius: 0 8px 8px 0; margin: .8rem 0; }
.callout.warn { border-color: #d98e04; background: #fff6e0; }
.callout.info { border-color: #1a4d8f; background: #eaf1fb; }
.callout p:last-child, .callout ul:last-child { margin-bottom: 0; }
.empf h3 { margin-top: 1.2rem; }
.empf li { margin-bottom: .35rem; }
.empf .prio { display: inline-block; min-width: 5.5rem; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--accent-dark); }
.timeline { list-style: none; padding: 0; margin: 0; border-left: 3px solid var(--accent); }
.timeline li { position: relative; padding: .2rem 0 .9rem 1.2rem; }
.timeline li::before { content: ''; position: absolute; left: -8px; top: .45rem; width: 13px; height: 13px; border-radius: 50%; background: #fff; border: 3px solid var(--accent); }
.timeline .when { font-weight: 700; color: var(--accent-dark); }
.toc { display: flex; flex-wrap: wrap; gap: .4rem; }
.toc a { font-size: .85rem; padding: .25rem .7rem; border-radius: 999px; background: #eef0f3; color: var(--text); }

.topbar .btn-praes { background: rgba(255,255,255,.15); color: #fff; border: 1px solid rgba(255,255,255,.35); border-radius: 6px; padding: .25rem .6rem; cursor: pointer; font-size: .85rem; }
#btnPraesentationEnde { display: none; }

/* Praesentationsmodus */
body.praesentation .sidebar, body.praesentation .topbar { display: none; }
body.praesentation .content { margin: 0 auto; padding: 1.5rem 2.5rem; max-width: 1600px; }
body.praesentation { font-size: 17px; }
body.praesentation h1 { font-size: 2rem; }
body.praesentation .kpi .value { font-size: 2.3rem; }
body.praesentation .no-print, body.praesentation .filters { display: none !important; }
body.praesentation #btnPraesentationEnde { display: block; position: fixed; right: 1rem; top: 1rem; z-index: 50; }

@media print {
  .sidebar, .topbar, .no-print, .filters, .btn, button, .pill-tabs { display: none !important; }
  .content { margin: 0; padding: 0; max-width: none; }
  body { background: #fff; font-size: 11px; }
  .panel { box-shadow: none; border: 1px solid #ddd; break-inside: avoid; }
  .table-wrap.scroll-fixed { max-height: none; overflow: visible; }
  .scroll-fixed thead th, .scroll-fixed tfoot td { position: static !important; }
  th, td { padding: .15rem .3rem; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  @page { size: A4 landscape; margin: 10mm; }
}

/* ---------- Strukturdaten ---------- */
.struktur-kacheln { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: .5rem; }
.struktur-kachel { border: 1px solid var(--border); border-radius: 8px; padding: .5rem .65rem; }
.struktur-kachel .label { font-size: .75rem; color: var(--text-muted); line-height: 1.2; min-height: 1.8em; }
.struktur-kachel .value { font-size: 1.15rem; font-weight: 700; }

/* ---------- Navigation: Gruppen, Werkzeuge, Hervorhebung ---------- */
.sidebar li.nav-sep:first-child { margin-top: 0; border-top: none; }
.sidebar li a { padding: .45rem 1.2rem; }
.nav-badge { font-size: .62rem; font-weight: 700; text-transform: uppercase; background: #3fa15e; color: #fff; border-radius: 999px; padding: .05rem .4rem; margin-left: .3rem; vertical-align: middle; }
.sidebar li.nav-highlight a { color: #b9f0c8; font-weight: 600; }
.sidebar li.nav-highlight a::before { content: ''; display: inline-block; width: .5rem; height: .5rem; border-radius: 50%; background: #46962B; margin-right: .5rem; vertical-align: middle; }
.sidebar li.nav-highlight a.active::before { background: #fff; }
.sidebar li.nav-sep-tool { color: #56617a; }
.sidebar li.nav-tool a { font-size: .84rem; color: #8f9ab0; margin: .15rem .8rem; padding: .35rem .6rem; border: 1px dashed #2c3752; border-radius: 6px; }
.sidebar li.nav-tool a:hover { color: #c9d2e3; border-color: #4a5775; }
.sidebar li.nav-tool a.active { border-style: solid; }
.nav-icon { display: inline-block; width: 1.1em; opacity: .75; }
.hinweis-rohdaten { display: flex; gap: .6rem; align-items: flex-start; border: 1px dashed var(--border); background: #fafbfa; border-radius: 8px; padding: .6rem .8rem; margin-bottom: 1rem; font-size: .88rem; }
