/* TMB Analysekarte – Prototyp. Mobile-first, WCAG 2.2 AA, Print-fähig. */
:root {
  --c-primary: #16435d;
  --c-primary-light: #1f5f84;
  --c-accent: #d7191c;
  --c-bg: #ffffff;
  --c-panel: rgba(255, 255, 255, 0.96);
  --c-border: #c9d4db;
  --c-text: #1c2b33;
  --c-text-dim: #4a5e6a;
  --shadow: 0 2px 10px rgba(10, 30, 45, 0.25);
  --radius: 10px;
  --focus: 3px solid #f5a623;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--c-text);
  display: flex;
  flex-direction: column;
  background: var(--c-bg);
}

:focus-visible { outline: var(--focus); outline-offset: 1px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px;
  clip-path: inset(50%); overflow: hidden; white-space: nowrap;
}
.skip-link {
  position: absolute; left: -999px; top: 0; z-index: 2000;
  background: var(--c-primary); color: #fff; padding: 8px 14px;
}
.skip-link:focus { left: 8px; }

/* ---------- Kopfzeile ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 14px;
  background: var(--c-primary); color: #fff;
  z-index: 1100;
}
.topbar-title h1 { font-size: 1.02rem; margin: 0; line-height: 1.2; }
.subtitle { margin: 0; font-size: 0.72rem; opacity: 0.85; }
.topbar-actions { display: flex; gap: 8px; margin-left: auto; align-items: center; flex-wrap: wrap; }

.btn {
  background: var(--c-primary-light); color: #fff;
  border: 1px solid rgba(255,255,255,0.35); border-radius: 7px;
  font-size: 0.85rem; padding: 7px 12px; cursor: pointer;
  min-height: 40px;
}
.btn:hover { background: #2a7099; }
.btn-small { min-height: 34px; padding: 5px 10px; background: var(--c-primary); }

/* Suche */
.search-wrap { position: relative; }
#search-input {
  min-width: 200px; width: 230px; min-height: 40px;
  border-radius: 7px; border: 1px solid rgba(255,255,255,0.4);
  padding: 7px 10px; font-size: 0.9rem;
}
#search-results {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 1500;
  background: #fff; color: var(--c-text);
  border: 1px solid var(--c-border); border-radius: 0 0 8px 8px;
  margin: 2px 0 0; padding: 0; list-style: none;
  box-shadow: var(--shadow); max-height: 260px; overflow-y: auto;
}
#search-results li { padding: 9px 10px; cursor: pointer; font-size: 0.85rem; }
#search-results li:hover, #search-results li[aria-selected="true"] {
  background: var(--c-primary); color: #fff;
}

/* Download-Menü */
.menu-wrap { position: relative; }
.dropdown {
  position: absolute; right: 0; top: 100%; z-index: 1500;
  background: #fff; color: var(--c-text); min-width: 270px;
  border: 1px solid var(--c-border); border-radius: 8px;
  box-shadow: var(--shadow); padding: 8px; margin-top: 4px;
  max-height: 70vh; overflow-y: auto;
}
.dropdown h3 { font-size: 0.78rem; margin: 8px 4px 4px; color: var(--c-text-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.dl-row { display: flex; align-items: center; gap: 6px; padding: 4px; font-size: 0.85rem; }
.dl-row .dl-name { flex: 1; }
.dl-row a {
  color: var(--c-primary); border: 1px solid var(--c-border); border-radius: 5px;
  text-decoration: none; font-size: 0.74rem; padding: 3px 7px; min-height: 28px; display: inline-flex; align-items: center;
}
.dl-row a:hover { background: var(--c-primary); color: #fff; }

/* ---------- Hauptbereich ---------- */
#app { position: relative; flex: 1; min-height: 0; }
#map { position: absolute; inset: 0; background: #d9e7ee; }
#map-placeholder {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; z-index: 1; /* unter den Leaflet-Panes (z-index >= 100) */
  transition: opacity 0.4s; pointer-events: none;
}
#map-placeholder.fade { opacity: 0; }

.panel {
  position: absolute; z-index: 1000;
  background: var(--c-panel); border: 1px solid var(--c-border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  max-width: min(86vw, 320px);
}
.panel-left { top: 12px; left: 12px; }
.panel-right { top: 12px; right: 12px; }
.panel-legend {
  bottom: 28px; left: 12px;
  max-height: 42vh; overflow-y: auto; max-width: min(80vw, 290px);
}
.panel-legend .panel-body { max-height: 34vh; }
.panel-h { margin: 0; font-size: inherit; }
.panel-title { font-size: 0.92rem; margin: 0; }
.panel-head {
  display: flex; justify-content: space-between; align-items: center; gap: 14px;
  width: 100%; background: none; border: 0; cursor: pointer;
  font: inherit; font-weight: 600; font-size: 0.92rem;
  padding: 10px 12px; min-height: 44px; color: var(--c-text);
}
.icon-btn {
  border: 0; background: #eef2f5; border-radius: 50%;
  width: 34px; height: 34px; font-size: 1.1rem; cursor: pointer; flex: 0 0 auto;
}
.icon-btn:hover { background: #dbe4ea; }
.btn-link { text-decoration: none; display: inline-flex; align-items: center; }
.panel-head .chev { transition: transform 0.15s; }
.panel-head[aria-expanded="false"] .chev { transform: rotate(-90deg); }
.panel-body { padding: 0 12px 12px; max-height: 56vh; overflow-y: auto; }
/* Disclosure: Button sitzt im h2, der Body ist Geschwister des h2 */
.panel-h:has(.panel-head[aria-expanded="false"]) + .panel-body { display: none; }

.field { margin: 0 0 12px; border: 0; padding: 0; }
.field label, .field legend { font-size: 0.8rem; font-weight: 600; display: block; margin-bottom: 4px; }
.field select {
  width: 100%; min-height: 40px; font-size: 0.88rem;
  border: 1px solid var(--c-border); border-radius: 6px; padding: 6px;
  background: #fff;
}
.checks { display: flex; flex-direction: column; gap: 2px; }
.checks label {
  display: flex; align-items: center; gap: 8px;
  font-size: 0.85rem; font-weight: 400; padding: 5px 2px; min-height: 32px;
  cursor: pointer;
}
.checks input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--c-primary); }

/* Layer-Liste */
#layer-list { display: flex; flex-direction: column; gap: 2px; }
#layer-list .layer-group { margin-top: 8px; font-size: 0.72rem; font-weight: 700; color: var(--c-text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
#layer-list label {
  display: flex; align-items: center; gap: 8px; font-size: 0.86rem;
  padding: 5px 2px; min-height: 34px; cursor: pointer;
}
#layer-list input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--c-primary); }
.layer-swatch { width: 14px; height: 14px; border-radius: 3px; flex: 0 0 auto; border: 1px solid rgba(0,0,0,0.25); }
.layer-swatch.line { height: 4px; border-radius: 2px; }
.layer-hint { font-size: 0.72rem; color: var(--c-text-dim); margin: 2px 0 0 27px; }

/* Legende */
.legend-title { font-size: 0.85rem; margin: 0 0 6px; }
.legend-section { margin-bottom: 10px; }
.legend-section h3 { font-size: 0.76rem; margin: 0 0 4px; }
.legend-row { display: flex; align-items: center; gap: 7px; font-size: 0.76rem; padding: 1px 0; }
.legend-row .sw { width: 16px; height: 12px; border: 1px solid rgba(0,0,0,0.2); flex: 0 0 auto; }
.legend-row .sw.line { height: 4px; border: 0; }
.legend-row .sw.dot { width: 11px; height: 11px; border-radius: 50%; }
.legend-empty { font-size: 0.76rem; color: var(--c-text-dim); }

/* Status-Toast */
.status {
  position: absolute; bottom: 28px; left: 50%; transform: translateX(-50%);
  z-index: 1200; background: rgba(22, 67, 93, 0.92); color: #fff;
  border-radius: 20px; padding: 7px 16px; font-size: 0.82rem;
  opacity: 0; pointer-events: none; transition: opacity 0.25s;
  max-width: 86vw; text-align: center;
}
.status.show { opacity: 1; }

/* Popups */
.leaflet-popup-content { font-size: 0.84rem; line-height: 1.45; margin: 12px 14px; }
.popup-title { font-weight: 700; font-size: 0.95rem; margin-bottom: 2px; }
.popup-sub { color: var(--c-text-dim); font-size: 0.76rem; margin-bottom: 7px; }
.popup-table { border-collapse: collapse; width: 100%; margin: 4px 0; }
.popup-table td { padding: 2px 6px 2px 0; vertical-align: top; }
.popup-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.popup-src { font-size: 0.7rem; color: var(--c-text-dim); margin-top: 7px; border-top: 1px solid #e3e9ed; padding-top: 5px; }
.badge {
  display: inline-block; border-radius: 4px; padding: 1px 7px; font-size: 0.72rem;
  font-weight: 700; color: #fff; background: var(--c-accent);
}
.badge.ok { background: #1a9641; }
.vi-chip { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; border: 1px solid rgba(0,0,0,0.2); }

/* Bottom-Sheet (Mobile) */
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 1300;
  background: #fff; border-radius: 14px 14px 0 0;
  box-shadow: 0 -4px 18px rgba(0,0,0,0.3);
  padding: 14px 16px 18px; max-height: 55%; overflow-y: auto;
  font-size: 0.88rem;
}
.sheet-close {
  position: absolute; top: 6px; right: 8px; border: 0; background: #eef2f5;
  border-radius: 50%; width: 36px; height: 36px; font-size: 1.2rem; cursor: pointer;
}

/* Fußzeile */
.footbar {
  display: flex; flex-wrap: wrap; gap: 4px 14px; justify-content: space-between;
  font-size: 0.68rem; color: var(--c-text-dim);
  padding: 5px 12px; background: #f2f5f7; border-top: 1px solid var(--c-border);
  z-index: 1100;
}
.footbar a { color: var(--c-primary); }

/* Cluster-Farbe */
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large { background-color: rgba(31, 95, 132, 0.25); }
.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background-color: rgba(22, 67, 93, 0.85); color: #fff; font-weight: 600;
}

/* Gastro-Punkt-Icons */
.gastro-icon {
  display: flex; align-items: center; justify-content: center;
  border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.4);
  width: 26px; height: 26px;
}
.gastro-icon span { transform: rotate(45deg); font-size: 13px; }

#print-header, #print-table, #print-method { display: none; }

/* ---------- Detail-Sidebar ---------- */
.detail {
  position: absolute; top: 12px; right: 12px; bottom: 28px; z-index: 1250;
  width: min(92vw, 400px);
  background: var(--c-panel); border: 1px solid var(--c-border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px; overflow-y: auto;
}
.detail-close { position: absolute; top: 8px; right: 10px; }
.detail h2 { font-size: 1.1rem; margin: 0 38px 2px 0; }
.detail .detail-sub { color: var(--c-text-dim); font-size: 0.78rem; margin: 0 0 10px; }
.detail h3 { font-size: 0.82rem; margin: 14px 0 5px; color: var(--c-primary); }
.detail .popup-table { font-size: 0.82rem; }
.detail-badges { margin: 4px 0 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.chart-svg { width: 100%; height: auto; display: block; }
.empfehlung-box {
  background: #fdf3e7; border-left: 4px solid #e67e22; border-radius: 4px;
  padding: 8px 10px; font-size: 0.82rem; line-height: 1.45;
}
.detail-list { list-style: none; margin: 0; padding: 0; font-size: 0.82rem; }
.detail-list li { padding: 3px 0; border-bottom: 1px solid #eef2f5; display: flex; justify-content: space-between; gap: 8px; }
.detail-list li span:last-child { color: var(--c-text-dim); white-space: nowrap; }
.wiki-box { font-size: 0.8rem; line-height: 1.5; color: var(--c-text); }
.wiki-box a { color: var(--c-primary); }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.detail-actions .btn { background: var(--c-primary); }

/* ---------- Ranking-Tabelle ---------- */
.panel-ranking {
  top: 12px; left: 12px; bottom: 28px; width: min(94vw, 430px);
  display: flex; flex-direction: column; z-index: 1260;
}
.panel-ranking[hidden] { display: none; }
.ranking-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px 4px; }
.ranking-scroll { overflow: auto; flex: 1; padding: 0 6px 8px; }
#ranking-table { border-collapse: collapse; width: 100%; font-size: 0.78rem; }
#ranking-table th {
  position: sticky; top: 0; background: var(--c-primary); color: #fff;
  padding: 6px 6px; text-align: left; cursor: pointer; white-space: nowrap;
  user-select: none;
}
#ranking-table th:hover { background: var(--c-primary-light); }
#ranking-table td { padding: 4px 6px; border-bottom: 1px solid #e8edf1; }
#ranking-table tbody tr { cursor: pointer; }
#ranking-table tbody tr:hover, #ranking-table tbody tr:focus-within { background: #e8f1f7; }
#ranking-table td:nth-child(n+3) { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Vergleich ---------- */
.compare {
  position: absolute; bottom: 28px; left: 50%; transform: translateX(-50%);
  z-index: 1270; width: min(96vw, 760px);
  background: var(--c-panel); border: 1px solid var(--c-border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px 14px 12px;
  max-height: 46vh; overflow-y: auto;
}
.compare-head { display: flex; justify-content: space-between; align-items: center; }
.compare-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.compare-card { border: 1px solid var(--c-border); border-radius: 8px; padding: 8px 10px; font-size: 0.8rem; position: relative; }
.compare-card h3 { margin: 0 18px 4px 0; font-size: 0.88rem; }
.compare-card .icon-btn { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; font-size: 0.9rem; }
.compare-bar { height: 10px; border-radius: 5px; background: #e8edf1; overflow: hidden; margin: 2px 0 6px; }
.compare-bar i { display: block; height: 100%; }

/* ---------- Tour ---------- */
.tour {
  position: absolute; bottom: 70px; left: 50%; transform: translateX(-50%);
  z-index: 1280; width: min(94vw, 520px);
  background: var(--c-panel); border: 2px solid var(--c-primary);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px;
}
.tour h2 { font-size: 1rem; margin: 0 0 6px; color: var(--c-primary); }
.tour p { font-size: 0.86rem; line-height: 1.5; margin: 0 0 10px; }
.tour-nav { display: flex; gap: 8px; align-items: center; }
.tour-nav span { font-size: 0.78rem; color: var(--c-text-dim); margin-right: auto; }

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  .topbar { padding: 6px 8px; gap: 6px; }
  .topbar-title h1 { font-size: 0.9rem; }
  .subtitle { display: none; }
  .topbar-actions { flex-wrap: nowrap; width: 100%; overflow-x: auto; }
  .search-wrap { flex: 1; min-width: 110px; }
  #search-input { width: 100%; min-width: 0; }
  .btn { padding: 7px 9px; font-size: 0.78rem; white-space: nowrap; }
  /* Detail-Sidebar wird zum Bottom-Sheet */
  .detail {
    top: auto; left: 0; right: 0; bottom: 0; width: auto;
    max-height: 58%; border-radius: 14px 14px 0 0;
  }
  .panel-ranking { left: 0; right: 0; top: 0; bottom: 0; width: auto; border-radius: 0; }
  .compare { width: 96vw; max-height: 40vh; }
  .panel { max-width: 78vw; }
  .panel-legend { bottom: 8px; max-height: 30vh; max-width: 62vw; }
  .panel-body { max-height: 46vh; }
  .footbar { display: none; }
  .leaflet-control-scale { margin-bottom: 4px !important; }
}

/* ---------- Druck (A4 quer, 3 Seiten) ---------- */
@media print {
  @page { size: A4 landscape; margin: 8mm; }
  body { display: block; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .topbar, .footbar, .panel-left, .panel-right, .panel-ranking, .status, .sheet,
  .detail, .compare, .tour, .leaflet-control-zoom, .skip-link { display: none !important; }
  #app { position: static; }
  /* Seite 1: Karte + Legende */
  #map {
    position: relative !important; inset: auto;
    width: 100%; height: 158mm !important;
    page-break-inside: avoid;
  }
  .panel-legend {
    position: relative; left: 0; bottom: 0; margin-top: 3mm;
    box-shadow: none; max-height: none; max-width: none; overflow: visible;
    border: 1px solid #999; display: block;
    columns: 3; column-gap: 8mm;
  }
  /* Legende im Druck immer ausgeklappt, Toggle-Pfeil ausblenden */
  .panel-legend .panel-body { display: block !important; max-height: none; overflow: visible; }
  .panel-legend .chev { display: none; }
  .legend-section { break-inside: avoid; }
  #print-header {
    display: flex; justify-content: space-between; align-items: center; gap: 6mm;
    font-size: 11pt; margin-bottom: 3mm;
  }
  #print-logo-slot {
    display: inline-block; width: 34mm; height: 10mm;
    border: 1px dashed #bbb; border-radius: 2mm;
    flex: 0 0 auto;
  }
  #print-meta { font-size: 7.5pt; color: #555; }
  /* Seite 2: Tabelle der gefilterten Gemeinden */
  #print-table { display: block; page-break-before: always; }
  #print-table h2 { font-size: 12pt; }
  #print-table table { border-collapse: collapse; width: 100%; font-size: 8.5pt; }
  #print-table th, #print-table td { border: 0.3pt solid #999; padding: 1.2mm 2mm; text-align: left; }
  #print-table th { background: #16435d; color: #fff; }
  #print-table td:nth-child(n+3) { text-align: right; }
  /* Seite 3: Methodik-Hinweis */
  #print-method { display: block; page-break-before: always; font-size: 9.5pt; line-height: 1.5; }
  #print-method h2 { font-size: 12pt; }
}
