:root {
  --md-primary: #1b5e4a;
  --md-primary-dark: #143f34;
  --md-primary-container: #d4efe4;
  --md-secondary: #c4a35a;
  --md-secondary-container: #f3e9d2;
  --md-on-primary: #ffffff;
  --md-surface: #f6f3ee;
  --md-surface-card: #fffcf8;
  --md-outline: #c9c2b8;
  --md-text: #1c1b19;
  --md-muted: #5f5b55;
  --md-danger: #9b2c2c;
  --md-warning: #8a5a00;
  --md-radius: 12px;
  --md-shadow: 0 1px 2px rgba(28, 27, 25, 0.06), 0 8px 24px rgba(28, 27, 25, 0.06);
  --font: "Source Sans 3", "Segoe UI", sans-serif;
  --font-display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--md-text);
  background:
    radial-gradient(1200px 600px at 10% -10%, var(--md-primary-container) 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, var(--md-secondary-container) 0%, transparent 50%),
    var(--md-surface);
  min-height: 100vh;
}

a { color: var(--md-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell { display: grid; grid-template-columns: 260px minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  background: linear-gradient(
    180deg,
    var(--md-primary-dark) 0%,
    var(--md-primary) 55%,
    color-mix(in srgb, var(--md-primary) 70%, var(--md-secondary)) 100%
  );
  color: #f4faf7;
  padding: 1.5rem 1rem 2rem;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand {
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  margin: 0 0 0.15rem;
}
.brand-sub { opacity: 0.75; font-size: 0.9rem; margin-bottom: 1.75rem; }
.nav a {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: #e8f5ef;
  padding: 0.65rem 0.8rem;
  border-radius: 10px;
  margin-bottom: 0.25rem;
  text-decoration: none;
}
.nav a:hover, .nav a.active { background: rgba(255,255,255,0.12); text-decoration: none; }
.nav .material-symbols-outlined { font-size: 1.25rem; }
.nav-sub {
  margin: 0.15rem 0 0.75rem 0.85rem;
  padding-left: 0.65rem;
  border-left: 2px solid rgba(255,255,255,0.22);
}
.nav-sub a {
  display: block;
  color: #e8f5ef;
  padding: 0.4rem 0.65rem;
  border-radius: 8px;
  margin-bottom: 0.15rem;
  font-size: 0.92rem;
  text-decoration: none;
  opacity: 0.9;
}
.nav-sub a:hover, .nav-sub a.active { background: rgba(255,255,255,0.12); opacity: 1; }

.settings-toc {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  margin-bottom: 1rem;
}
.settings-toc a { font-weight: 600; }
.settings-section {
  margin-bottom: 1.25rem;
  scroll-margin-top: 1rem;
}
.settings-section h2 { margin-top: 0; font-family: var(--font-display); }
.sidebar-footer { margin-top: 2rem; font-size: 0.85rem; opacity: 0.8; }

.chart-wrap { position: relative; height: 220px; max-width: 520px; }

.main { padding: 1.5rem 2rem 3rem; min-width: 0; }
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
}
.page-title {
  font-family: var(--font-display);
  font-size: 1.85rem;
  margin: 0;
  font-weight: 600;
}
.page-lead { color: var(--md-muted); margin: 0.35rem 0 0; }

.card {
  background: var(--md-surface-card);
  border: 1px solid rgba(201, 194, 184, 0.55);
  border-radius: var(--md-radius);
  box-shadow: var(--md-shadow);
  padding: 1.15rem 1.25rem;
}
.grid { display: grid; gap: 1rem; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid > * { min-width: 0; }
.stat .label { color: var(--md-muted); font-size: 0.85rem; }
.stat .value { font-size: 1.8rem; font-weight: 650; margin-top: 0.2rem; }

.chance-summary { margin-bottom: 0.75rem; }
.chance-summary-main {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
}
.chance-summary-value {
  font-family: var(--font-display);
  font-size: 1.85rem;
  font-weight: 650;
  letter-spacing: -0.02em;
}
.chance-list { display: flex; flex-direction: column; gap: 0.65rem; }
.chance-row {
  display: block;
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  background: color-mix(in srgb, var(--md-primary) 6%, transparent);
  color: inherit;
  text-decoration: none;
}
.chance-row:hover {
  background: color-mix(in srgb, var(--md-primary) 12%, transparent);
  text-decoration: none;
}
.chance-row-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
}
.chance-pct {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--md-primary);
  white-space: nowrap;
}
.chance-meter {
  height: 0.45rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--md-outline) 45%, #fff);
  overflow: hidden;
}
.chance-meter-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(
    90deg,
    color-mix(in srgb, var(--md-primary) 75%, #2f6b4f),
    var(--md-primary)
  );
}
.chance-row-meta { margin-top: 0.3rem; font-size: 0.88rem; }
.chance-block .chance-row-top { margin-bottom: 0.4rem; }

.discover-list { display: flex; flex-direction: column; gap: 0.85rem; }
.discover-item {
  padding: 0.85rem 0.9rem;
  border-radius: 10px;
  background: color-mix(in srgb, var(--md-primary) 5%, transparent);
  border: 1px solid color-mix(in srgb, var(--md-outline) 55%, transparent);
}
.discover-item-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border: none;
  border-radius: 999px;
  padding: 0.55rem 1rem;
  font: inherit;
  cursor: pointer;
  background: var(--md-primary);
  color: var(--md-on-primary);
  text-decoration: none;
}
.btn:hover { filter: brightness(1.05); text-decoration: none; }
.btn-secondary { background: var(--md-secondary-container); color: color-mix(in srgb, var(--md-secondary) 70%, #1c1b19); }
.btn-ghost { background: transparent; color: var(--md-primary); border: 1px solid var(--md-outline); }
.btn-danger { background: var(--md-danger); color: #fff; }
.btn-sm { padding: 0.35rem 0.75rem; font-size: 0.9rem; }

.md-color {
  width: 3.25rem;
  height: 2.5rem;
  padding: 0.2rem;
  border: 1px solid var(--md-outline);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
}
.color-row {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
  align-items: end;
}
.color-field label { margin-top: 0; }

.md-input, select.md-input, textarea.md-input {
  width: 100%;
  border: 1px solid var(--md-outline);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  font: inherit;
  background: #fff;
}
label { display: block; font-weight: 600; margin: 0.75rem 0 0.35rem; font-size: 0.92rem; }
.form-actions { margin-top: 1.25rem; display: flex; gap: 0.6rem; flex-wrap: wrap; }
.helptext, .form-row .help { color: var(--md-muted); font-size: 0.85rem; }

.ai-loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: rgba(28, 27, 25, 0.45);
  backdrop-filter: blur(2px);
}
.ai-loading-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  max-width: 22rem;
  padding: 1.5rem 1.75rem;
  border-radius: var(--md-radius);
  background: var(--md-surface-card);
  box-shadow: var(--md-shadow);
  text-align: center;
}
.ai-loading-spinner {
  width: 2.25rem;
  height: 2.25rem;
  border: 3px solid color-mix(in srgb, var(--md-primary) 25%, transparent);
  border-top-color: var(--md-primary);
  border-radius: 50%;
  animation: ai-spin 0.8s linear infinite;
}
.ai-loading-text {
  margin: 0;
  font-weight: 600;
  color: var(--md-text);
}
.ai-loading-hint {
  margin: 0;
  font-size: 0.9rem;
  color: var(--md-muted);
}
.btn.is-loading {
  opacity: 0.85;
  cursor: wait;
  pointer-events: none;
}
@keyframes ai-spin {
  to { transform: rotate(360deg); }
}
body.area-housing .ai-loading-spinner {
  border-color: color-mix(in srgb, var(--haus-accent) 25%, transparent);
  border-top-color: var(--haus-accent);
}

.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  text-align: left;
  padding: 0.75rem 0.5rem;
  border-bottom: 1px solid rgba(201,194,184,0.55);
  vertical-align: top;
  overflow-wrap: anywhere;
}
.table th { color: var(--md-muted); font-weight: 600; font-size: 0.85rem; }
.table th a.sort-link {
  color: inherit;
  text-decoration: none;
  white-space: nowrap;
}
.table th a.sort-link:hover { color: var(--md-primary); text-decoration: underline; }

.badge {
  display: inline-block;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: var(--md-primary-container);
  color: #0f3b2e;
  font-size: 0.8rem;
  font-weight: 600;
}
.badge-warn { background: #f3e2c0; color: var(--md-warning); }
.badge-danger { background: #f5d6d6; color: var(--md-danger); }
.badge-ok { background: #d7ecd9; color: #1f5a2c; }

/* Statusfarben Gesuchsvorgang */
.badge-status-new { background: #e3e8ef; color: #334155; }
.badge-status-fit_check { background: #d8f3ef; color: #0f5c52; }
.badge-status-prepare { background: #e3e0f7; color: #3f3480; }
.badge-status-sent { background: #dbeafe; color: #1e40af; }
.badge-status-follow_up { background: #ffedd5; color: #9a3412; }
.badge-status-in_review { background: #f3e8ff; color: #6b21a8; }
.badge-status-clarification { background: #cffafe; color: #0e7490; }
.badge-status-approved { background: #dcfce7; color: #166534; }
.badge-status-rejected { background: #fee2e2; color: #991b1b; }
.badge-status-defer { background: #f5e6d3; color: #7c4a1a; }
.badge-status-not_relevant { background: #eceff1; color: #546e7a; }

.messages { list-style: none; padding: 0; margin: 0 0 1rem; }
.messages li {
  padding: 0.75rem 1rem;
  border-radius: 10px;
  margin-bottom: 0.5rem;
  background: var(--md-primary-container);
}
.messages li.error { background: #f5d6d6; color: var(--md-danger); }

.filters { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-bottom: 1rem; align-items: end; }
.filters .field { min-width: 180px; flex: 1; }

.timeline { position: relative; padding-left: 1.25rem; }
.timeline::before {
  content: "";
  position: absolute;
  left: 0.35rem;
  top: 0.2rem;
  bottom: 0.2rem;
  width: 2px;
  background: var(--md-outline);
}
.timeline-item {
  position: relative;
  margin-bottom: 1rem;
  padding: 1rem;
}
.timeline-item::before {
  content: "";
  position: absolute;
  left: -1.05rem;
  top: 1.25rem;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--md-primary);
  border: 2px solid #fff;
}
.timeline-meta { color: var(--md-muted); font-size: 0.85rem; margin-bottom: 0.35rem; }
.timeline-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.attachments { margin-top: 0.6rem; display: flex; gap: 0.5rem; flex-wrap: wrap; }
.attachments a {
  background: #efeae2;
  padding: 0.25rem 0.55rem;
  border-radius: 8px;
  font-size: 0.85rem;
}

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
}
.login-card { width: min(420px, 100%); }
.login-card h1 { font-family: var(--font-display); margin-top: 0; }

.split {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 0.8fr);
  gap: 1rem;
  align-items: start;
}
.split > * { min-width: 0; }
.muted { color: var(--md-muted); }
.stack > * + * { margin-top: 1rem; }
.row-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }

/* Bereichstrennung Fundraising vs Haus */
.nav-group { margin: 0.85rem 0 0.35rem; }
.nav-group-label {
  margin: 0 0.8rem 0.4rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.65;
}
.nav-group-fundraising {
  border-left: 3px solid color-mix(in srgb, #fff 55%, transparent);
  margin-left: 0.35rem;
  padding-left: 0.15rem;
}
.nav-group-housing {
  border-left: 3px solid #c4d7ea;
  margin-left: 0.35rem;
  padding-left: 0.15rem;
}
.area-eyebrow {
  margin: 0 0 0.2rem;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.area-eyebrow.fundraising { color: var(--md-primary); }
.area-eyebrow.housing { color: var(--haus-accent); }

body.area-housing .sidebar {
  background: linear-gradient(
    180deg,
    var(--haus-accent-dark) 0%,
    var(--haus-accent) 55%,
    color-mix(in srgb, var(--haus-accent) 75%, var(--haus-warm)) 100%
  );
  color: #ffffff;
}
body.area-housing .sidebar .brand,
body.area-housing .sidebar .brand-sub,
body.area-housing .sidebar .nav-group-label,
body.area-housing .sidebar-footer,
body.area-housing .sidebar .nav a,
body.area-housing .sidebar a {
  color: #ffffff;
}
body.area-housing .sidebar .nav a:hover,
body.area-housing .sidebar .nav a.active {
  background: rgba(255, 255, 255, 0.16);
  color: #ffffff;
}
body.area-housing .sidebar .btn-ghost {
  color: #ffffff;
  border-color: rgba(255, 255, 255, 0.45);
}
body.area-housing {
  background:
    radial-gradient(1100px 520px at 8% -8%, var(--haus-accent-soft) 0%, transparent 55%),
    radial-gradient(900px 480px at 100% 0%, color-mix(in srgb, var(--haus-warm) 22%, #fff) 0%, transparent 50%),
    var(--md-surface);
}
/* Links/Buttons nur im Hauptbereich umfärben – Sidebar bleibt weiß */
body.area-housing .main a:not(.btn) { color: var(--haus-accent); }
body.area-housing .main .btn {
  background: var(--haus-accent);
  color: #ffffff;
}
body.area-housing .main .btn-secondary {
  background: var(--haus-accent-soft);
  color: var(--haus-accent-dark);
}
body.area-housing .main .btn-ghost {
  color: var(--haus-accent);
  background: transparent;
}
body.area-housing .main .btn-danger { color: #ffffff; }
body.area-housing .timeline-item::before { background: var(--haus-accent); }
body.area-housing .housing-stat { border-top: 3px solid var(--haus-accent); }

body.area-fundraising .sidebar {
  /* bleibt bei Vereins-Primärfarbe */
}
.area-badge-fundraising,
.area-badge-housing {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 0.15rem 0.45rem;
  border-radius: 6px;
}
.area-badge-fundraising { background: var(--md-primary-container); color: #0f3b2e; }
.area-badge-housing { background: var(--haus-accent-soft); color: var(--haus-accent-dark); }

.filter-row {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  align-items: end;
}
.filter-row .md-input { min-width: 160px; flex: 1; }
.settings-toc .toc-housing { color: var(--haus-accent); }
.settings-section.housing-section {
  border-top: 3px solid var(--haus-accent);
}
.settings-section.fundraising-section {
  border-top: 3px solid var(--md-primary);
}

/* —— Responsive shell —— */
.mobile-bar,
.nav-backdrop,
.nav-close { display: none; }

.sidebar-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.25rem;
}
.sidebar-head .brand-sub { margin-bottom: 1.5rem; }

.nav-toggle,
.nav-close {
  appearance: none;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.1);
  color: inherit;
  border-radius: 10px;
  width: 2.5rem;
  height: 2.5rem;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}
.nav-toggle .material-symbols-outlined,
.nav-close .material-symbols-outlined { font-size: 1.35rem; line-height: 1; }

.mobile-bar {
  position: sticky;
  top: 0;
  z-index: 40;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.9rem;
  background: linear-gradient(
    90deg,
    var(--md-primary-dark) 0%,
    var(--md-primary) 70%
  );
  color: #f4faf7;
  box-shadow: 0 1px 0 rgba(28, 27, 25, 0.08);
}
.mobile-brand {
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.02em;
}
.mobile-bar-spacer { flex: 1; }
.mobile-bar .nav-toggle {
  display: inline-flex;
  color: #f4faf7;
}

body.area-housing .mobile-bar {
  background: linear-gradient(
    90deg,
    var(--haus-accent-dark) 0%,
    var(--haus-accent) 70%
  );
  color: #fff;
}
body.area-housing .mobile-bar .nav-toggle { color: #fff; }

body.nav-locked { overflow: hidden; }

.card:has(> .table),
.card:has(> table.table) {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Large desktop */
@media (min-width: 1400px) {
  .main { padding: 1.75rem 2.5rem 3.5rem; }
}

/* Tablet / iPad landscape: keep sidebar, soften dense grids */
@media (max-width: 1100px) {
  .app-shell { grid-template-columns: 220px minmax(0, 1fr); }
  .sidebar { padding: 1.25rem 0.85rem 1.75rem; }
  .brand { font-size: 1.4rem; }
  .main { padding: 1.25rem 1.35rem 2.5rem; }
  .page-title { font-size: 1.65rem; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .split { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters .field,
  .filter-row .md-input { min-width: 140px; }
}

/* iPad portrait / small tablet: drawer nav, 2-col stats */
@media (max-width: 900px) {
  .mobile-bar { display: flex; }

  .nav-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 90;
    background: rgba(28, 27, 25, 0.45);
  }

  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 100;
    width: min(300px, 88vw);
    height: 100vh;
    height: 100dvh;
    overflow-y: auto;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: none;
  }
  .sidebar.is-open {
    transform: translateX(0);
    box-shadow: 8px 0 32px rgba(28, 27, 25, 0.22);
  }
  .nav-close {
    display: inline-flex;
    flex-shrink: 0;
    color: #e8f5ef;
  }
  body.area-housing .nav-close { color: #fff; }

  .main { padding: 1rem 1.1rem 2.25rem; }
  .topbar {
    flex-wrap: wrap;
    align-items: flex-start;
    margin-bottom: 1.15rem;
  }
  .topbar .row-actions { width: 100%; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
  .grid-2,
  .split { grid-template-columns: 1fr; }
  .stat .value { font-size: 1.55rem; }
  .chart-wrap { max-width: none; height: 200px; }
  .table { font-size: 0.94rem; }
  .table th,
  .table td { padding: 0.65rem 0.45rem; }
  .table th a.sort-link { white-space: normal; }
}

/* Phones */
@media (max-width: 600px) {
  .mobile-bar { padding: 0.55rem 0.75rem; }
  .main { padding: 0.85rem 0.75rem 2rem; }
  .page-title { font-size: 1.4rem; }
  .page-lead { font-size: 0.95rem; }
  .card { padding: 0.95rem 1rem; }
  .grid-4,
  .grid-3 { grid-template-columns: 1fr; }
  .filters,
  .filter-row { flex-direction: column; align-items: stretch; }
  .filters .field,
  .filter-row .md-input {
    min-width: 0;
    width: 100%;
    flex: none;
  }
  .btn { justify-content: center; }
  .row-actions { width: 100%; }
  .row-actions .btn { flex: 1 1 auto; }
  .login-wrap { padding: 1.25rem; }
  .chance-summary-value { font-size: 1.55rem; }
  .discover-item-head { flex-direction: column; }
}

/* Very narrow */
@media (max-width: 380px) {
  .page-title { font-size: 1.25rem; }
  .stat .value { font-size: 1.35rem; }
}
