/* DealsList — 3×3 Card-Grid, helles Paper-Background. */
const DEALS_LIST_CSS = `
.dl-section { background: var(--v-paper-alt); padding: 120px 80px 140px; }
.dl-head { margin-bottom: 64px; }
.dl-head-left { max-width: 820px; }
.dl-eyebrow { font-family: var(--font-mono); font-size: 14px; letter-spacing: 3px; text-transform: uppercase; color: var(--v-ink-sub); display: inline-flex; align-items: center; gap: 14px; margin-bottom: 28px; font-weight: 400; }
.dl-eyebrow::before { content: ''; width: 36px; height: 1px; background: var(--v-bordeaux); }
.dl-title { font-family: var(--font-serif); font-weight: 300; font-size: 62px; line-height: 1.05; letter-spacing: -0.02em; color: var(--v-ink); margin: 0 0 20px; }
.dl-title em { font-style: italic; font-weight: 400; color: var(--v-bordeaux); }
.dl-lede { font-family: var(--font-serif); font-size: 19px; font-weight: 300; color: var(--v-ink-sub); line-height: 1.55; margin: 0; max-width: 620px; }
.dl-foot { display: flex; justify-content: center; margin-top: 64px; }
.dl-see-all { font-family: var(--font-mono); font-size: 13px; letter-spacing: 2px; text-transform: uppercase; color: var(--v-ink); text-decoration: none; display: inline-flex; align-items: center; gap: 12px; padding: 18px 30px; border: 1px solid var(--v-border-strong); background: transparent; transition: all 200ms ease; }
.dl-see-all:hover { background: var(--v-ink); color: var(--v-paper); border-color: var(--v-ink); }
.dl-see-all .arrow { transition: transform 200ms ease; }
.dl-see-all:hover .arrow { transform: translateX(4px); }

.dl-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; }
.dl-card { display: flex; flex-direction: column; background: var(--v-card); border: 1px solid var(--v-border-soft); text-decoration: none; color: inherit; cursor: pointer; transition: transform 280ms ease, box-shadow 280ms ease, border-color 280ms ease; }
.dl-card:hover { transform: translateY(-4px); box-shadow: 0 12px 36px rgba(20,18,16,0.10); border-color: var(--v-border); }

.dl-card-image { position: relative; aspect-ratio: 4 / 3; overflow: hidden; background: var(--v-paper-deep); }
.dl-card-image img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transition: transform 600ms ease; }
.dl-card:hover .dl-card-image img { transform: scale(1.05); }

.dl-card-yield { position: absolute; top: 18px; right: 22px; font-family: var(--font-mono); font-size: 12px; letter-spacing: 1.5px; text-transform: uppercase; padding: 8px 14px; background: rgba(20,18,16,0.78); color: var(--v-brass-300); backdrop-filter: blur(10px); display: inline-flex; align-items: center; gap: 8px; font-weight: 500; }
.dl-card-yield::before { content: ''; width: 6px; height: 6px; background: var(--v-brass-300); display: inline-block; }

.dl-card-body { display: flex; flex-direction: column; gap: 14px; padding: 26px 26px 24px; flex: 1; }
.dl-card-loc { font-family: var(--font-mono); font-size: 12px; letter-spacing: 2.2px; text-transform: uppercase; color: var(--v-ink-muted); display: inline-flex; align-items: center; gap: 8px; }
.dl-card-loc svg { width: 14px; height: 14px; flex-shrink: 0; color: var(--v-bordeaux); }
.dl-card-title { font-family: var(--font-serif); font-size: 24px; font-weight: 400; line-height: 1.2; letter-spacing: -0.01em; color: var(--v-ink); margin: 0; }
.dl-card-type { font-family: var(--font-mono); font-size: 11.5px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--v-ink-sub); }

.dl-card-boxes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: auto; padding-top: 14px; min-width: 0; }
.dl-box { padding: 14px 16px; background: var(--v-paper); border: 1px solid var(--v-border-soft); display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.dl-box.is-cf { background: var(--v-ink); border-color: var(--v-ink); justify-content: center; }
.dl-box-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.dl-box-row + .dl-box-row { padding-top: 6px; border-top: 1px solid var(--v-border-soft); }
.dl-box-l { font-family: var(--font-mono); font-size: 10.5px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--v-ink-muted); white-space: nowrap; }
.dl-box-v { font-family: var(--font-serif); font-size: 20px; font-weight: 400; color: var(--v-ink); letter-spacing: -0.01em; line-height: 1; white-space: nowrap; }
.dl-box.is-cf .dl-box-l { color: var(--v-paper); }
.dl-box.is-cf .dl-box-v { color: var(--v-brass); font-size: 26px; }
.dl-box.is-cf .dl-box-v .unit { font-family: var(--font-mono); font-size: 12px; color: rgba(201,166,107,0.6); margin-left: 4px; letter-spacing: 0.5px; }

@media (max-width: 1200px) {
  .dl-section { padding: 96px 48px 110px; }
  .dl-title { font-size: 48px; }
  .dl-grid { gap: 22px; }
  .dl-card-title { font-size: 21px; }
  .dl-box-v { font-size: 18px; }
  .dl-box.is-cf .dl-box-v { font-size: 23px; }
  .dl-card-body { padding: 22px 22px 20px; }
}

@media (max-width: 900px) {
  .dl-section { padding: 72px 28px 88px; }
  .dl-title { font-size: 38px; }
  .dl-head { margin-bottom: 44px; }
  .dl-grid { grid-template-columns: repeat(2, 1fr); gap: 18px; }
}

@media (max-width: 720px) {
  /* In a single-column layout the two stat boxes share the full card
     width; their nowrap labels + values (e.g. "EIGENKAPITAL 106.840 $"
     beside "CASHFLOW / MONAT 1.824 $") force the card wider than the
     viewport. Stack them vertically once we drop to 1 col. */
  .dl-grid { grid-template-columns: 1fr; gap: 18px; }
  .dl-card-boxes { grid-template-columns: 1fr; gap: 10px; }
}

@media (max-width: 560px) {
  .dl-section { padding: 56px 22px 64px; }
  .dl-title { font-size: 30px; }
  .dl-grid { grid-template-columns: 1fr; gap: 18px; }
  .dl-card-title { font-size: 20px; }
  .dl-box-v { font-size: 18px; }
  .dl-box.is-cf .dl-box-v { font-size: 22px; }
}
`;

const DEALS_LIST_ITEMS = [
  { img: './house-1.webp?v=2', city: 'Cleveland Heights · Ohio', title: 'Turnkey-SFR', kaufpreis: '134.000 $', ek: '46.900 $', cashflowMo: '153 $', type: 'Single-Family · 3 BR', yield: '6,63 %' },
  { img: './house-2.webp', city: 'Parma · Ohio', title: 'Duplex 2×2BR', kaufpreis: '172.000 $', ek: '60.200 $', cashflowMo: '545 $', type: 'Duplex · 2 × 2 BR', yield: '9,07 %' },
  { img: './house-3.webp', city: 'Lakewood · Ohio', title: 'Triplex Inner-Ring', kaufpreis: '258.000 $', ek: '90.300 $', cashflowMo: '865 $', type: 'Triplex · 3 × 1 BR', yield: '9,07 %' },
  { img: './cleveland-duplex.png', city: 'Euclid · Ohio', title: 'Quadplex 4 Streams', kaufpreis: '298.000 $', ek: '104.300 $', cashflowMo: '1.120 $', type: 'Quadplex · 4 × 1 BR', yield: '10,03 %' },
  { img: './house-1.webp?v=2', city: 'Shaker Heights · Ohio', title: 'Colonial 1928', kaufpreis: '195.000 $', ek: '68.250 $', cashflowMo: '370 $', type: 'SFR · 4 BR', yield: '6,56 %' },
  { img: './house-2.webp', city: 'Maple Heights · Ohio', title: 'Renoviertes SFR', kaufpreis: '128.000 $', ek: '44.800 $', cashflowMo: '190 $', type: 'Single-Family · 3 BR', yield: '7,06 %' },
  { img: './house-3.webp', city: 'Memphis · Tennessee', title: 'Cashflow-Duplex', kaufpreis: '182.000 $', ek: '63.700 $', cashflowMo: '610 $', type: 'Duplex · 2 × 2 BR', yield: '9,42 %' },
  { img: './cleveland-duplex.png', city: 'Birmingham · Alabama', title: 'Triplex Süd', kaufpreis: '245.000 $', ek: '85.750 $', cashflowMo: '780 $', type: 'Triplex · 3 × 1 BR', yield: '8,88 %' },
  { img: './house-1.webp?v=2', city: 'Louisville · Kentucky', title: 'SFR Walkable', kaufpreis: '148.000 $', ek: '51.800 $', cashflowMo: '240 $', type: 'Single-Family · 3 BR', yield: '6,84 %' },
];

const PinIcon = () => (
  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
    <path d="M8 14s5-4.5 5-8.5A5 5 0 0 0 3 5.5C3 9.5 8 14 8 14Z"/>
    <circle cx="8" cy="5.5" r="1.8"/>
  </svg>
);

const HouseIcon = () => (
  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
    <path d="M2 7.5 8 2.5l6 5"/><path d="M3.5 7v6h9V7"/>
  </svg>
);

const CashIcon = () => (
  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
    <rect x="2" y="4.5" width="12" height="7" rx="0.5"/><circle cx="8" cy="8" r="1.5"/>
  </svg>
);

// Map a Supabase-shape deal (from window.DEALS / loadDeals) into the
// fields the DealsList card UI expects. Falls back to the hardcoded
// item shape so DEALS_LIST_ITEMS still works as a last-resort.
const mapSupabaseDeal = (d) => {
  if (!d) return null;
  if (d.kaufpreis && d.cashflowMo) return d; // already mapped (hardcoded shape)
  const cityLine = [d.city, d.state].filter(Boolean).join(' · ');
  const titleParts = [d.neighborhood, d.type].filter(Boolean);
  return {
    id: d.id,
    img: d.imgPrimary || './house-1.webp?v=2',
    city: cityLine || (d.city || '—'),
    title: titleParts.length ? titleParts.join(' · ') : (d.type || 'Deal'),
    type: d.meta || d.type || '',
    kaufpreis: d.kaufpreis || '—',
    ek: d.ek || '—',
    cashflowMo: d.cfMonat || '—',
    yield: d.coc ? `${d.coc} %` : '—',
  };
};

const DealsList = () => {
  const initial = window.DEALS && window.DEALS.length ? window.DEALS : null;
  const [deals, setDeals] = React.useState(initial);

  React.useEffect(() => {
    if (deals) return;
    if (window.DEALS_READY && typeof window.DEALS_READY.then === 'function') {
      window.DEALS_READY.then(setDeals);
    }
  }, []);

  // Homepage shows up to 9 currently-available listings (status=verfuegbar).
  // Falls back to the hardcoded sample data if Supabase is unreachable.
  const source = deals && deals.length
    ? deals.filter(d => !d.status || d.status === 'verfuegbar').slice(0, 9)
    : DEALS_LIST_ITEMS;
  const items = source.map(mapSupabaseDeal).filter(Boolean);

  return (
  <>
    <style dangerouslySetInnerHTML={{ __html: DEALS_LIST_CSS }} />
    <section className="dl-section" id="inserate">
      <div className="dl-head">
        <div className="dl-head-left">
          <div className="dl-eyebrow">Aktuelle Inserate</div>
          <h2 className="dl-title">Entdecke unsere <em>aktuellen Deals</em>.</h2>
          <p className="dl-lede">Eine Auswahl aktueller Off-Market-Objekte, die wir aktiv für unsere Investoren strukturieren — Cashflow-Rentals in den US-Märkten mit den stärksten Fundamentaldaten.</p>
        </div>
      </div>
      <div className="dl-grid">
        {items.map((d, i) => (
          <a key={d.id || i} className="dl-card" href={d.id ? `/deal?id=${(window.dealHref ? window.dealHref(d) : d.id)}` : '/deals'}>
            <div className="dl-card-image">
              <img src={d.img} alt={d.title}/>
              <div className="dl-card-yield">{d.yield} EK-Rendite</div>
            </div>
            <div className="dl-card-body">
              <span className="dl-card-loc"><PinIcon/>{d.city}</span>
              <h3 className="dl-card-title">{d.title}</h3>
              <div className="dl-card-boxes">
                <div className="dl-box">
                  <div className="dl-box-row">
                    <span className="dl-box-l">Kaufpreis</span>
                    <span className="dl-box-v">{d.kaufpreis}</span>
                  </div>
                  <div className="dl-box-row">
                    <span className="dl-box-l">Eigenkapital</span>
                    <span className="dl-box-v">{d.ek}</span>
                  </div>
                </div>
                <div className="dl-box is-cf">
                  <span className="dl-box-l">Cashflow / Monat</span>
                  <span className="dl-box-v">{d.cashflowMo}</span>
                </div>
              </div>
            </div>
          </a>
        ))}
      </div>
      <div className="dl-foot">
        <a className="dl-see-all" href="/deals">Alle Inserate ansehen <span className="arrow">→</span></a>
      </div>
    </section>
  </>
  );
};

window.DealsList = DealsList;
