:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --text: #17211b;
  --muted: #5e6b63;
  --line: #d9e0db;
  --brand: #1f6f4a;
  --brand-ink: #ffffff;
  --accent: #e8f3ed;
  --warn-bg: #fff6e5;
  --warn-ink: #7a4b00;
  --err-bg: #fdecec;
  --err-ink: #8a1f1f;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 4px 14px rgba(0, 0, 0, 0.05);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111614;
    --surface: #1a211d;
    --text: #e8ede9;
    --muted: #9aa69f;
    --line: #2d3733;
    --brand: #3fa374;
    --brand-ink: #06130c;
    --accent: #1f2d26;
    --warn-bg: #3a2a0c;
    --warn-ink: #ffd79a;
    --err-bg: #3c1b1b;
    --err-ink: #ffb3b3;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.wrap { max-width: 1100px; margin: 0 auto; padding: 0 16px; }
a { color: var(--brand); }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.25rem; margin: 2rem 0 0.75rem; }
h3 { margin: 0; font-size: 1.05rem; }
.muted { color: var(--muted); }

.site-header { background: var(--surface); border-bottom: 1px solid var(--line); padding: 18px 0; }
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  width: 44px; height: 44px; border-radius: 10px; display: grid; place-items: center;
  background: var(--brand); color: var(--brand-ink); font-size: 1.5rem;
}
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 0.95rem; }

.search-card {
  margin: 24px 0 16px; padding: 18px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: grid; grid-template-columns: 1fr auto auto; gap: 12px; align-items: end;
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 0.85rem; color: var(--muted); font-weight: 600; }
input, select {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 12px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
#category { min-width: 260px; }
.btn {
  font: inherit; font-weight: 600; border-radius: 8px; padding: 11px 18px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.primary:disabled { opacity: 0.6; cursor: wait; }
.btn.ghost { background: transparent; }
.household { grid-column: 1 / -1; }
.household summary { cursor: pointer; font-weight: 600; font-size: 0.95rem; }
.household-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-top: 12px; }

.status { padding: 12px 16px; border-radius: 8px; background: var(--accent); margin-bottom: 16px; }
.status.error { background: var(--err-bg); color: var(--err-ink); }
.notice { padding: 10px 14px; border-radius: 8px; background: var(--warn-bg); color: var(--warn-ink); margin: 8px 0; }
.notice.subtle { background: var(--accent); color: var(--text); }
.empty { padding: 16px; border: 1px dashed var(--line); border-radius: var(--radius); color: var(--muted); }
.empty.error { background: var(--err-bg); color: var(--err-ink); border-style: solid; }
.empty.working::before { content: ""; display: inline-block; width: 12px; height: 12px; margin-right: 8px; border-radius: 50%; border: 2px solid var(--brand); border-right-color: transparent; animation: spin 0.8s linear infinite; vertical-align: -1px; }
@keyframes spin { to { transform: rotate(360deg); } }

.results-toolbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.location-card { padding: 4px 0; }
.loc-main { font-weight: 700; font-size: 1.1rem; }
.loc-sub { font-size: 0.95rem; }

.summary { display: flex; flex-wrap: wrap; gap: 10px; margin: 14px 0; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; min-width: 90px; }
.stat-n { font-size: 1.4rem; font-weight: 700; line-height: 1.1; }
.stat-l { font-size: 0.8rem; color: var(--muted); }

.category-hints { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 8px; font-size: 0.9rem; }
.chip-btn { font: inherit; font-size: 0.85rem; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer; }
.chip-btn:hover { border-color: var(--brand); }

.level { margin-bottom: 20px; }
.level-title { margin: 10px 0 8px; font-size: 1rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px;
}
.card-head { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.amount { font-weight: 700; color: var(--brand); white-space: nowrap; }
.desc { margin: 0; font-size: 0.95rem; }
.meta { font-size: 0.82rem; color: var(--muted); }
.link { font-size: 0.9rem; font-weight: 600; margin-top: auto; }
.badges, .items { display: flex; flex-wrap: wrap; gap: 6px; }
.badge { font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; padding: 2px 8px; border-radius: 999px; background: var(--accent); }
.badge.pay { background: transparent; border: 1px solid var(--line); }
.badge.warn { background: var(--warn-bg); color: var(--warn-ink); }
.badge.ai, .badge.conf { background: #ece7fb; color: #3d2a8a; }
@media (prefers-color-scheme: dark) { .badge.ai, .badge.conf { background: #2b2545; color: #cbbfff; } }
.badge.lvl-federal { background: #e3ecfa; color: #1c3f7a; }
.badge.lvl-state { background: #e8f3ed; color: #1f6f4a; }
.badge.lvl-county { background: #fbeedd; color: #7a4b00; }
.badge.lvl-city { background: #fde7ef; color: #8a1f4f; }
.badge.lvl-utility, .badge.lvl-gas_utility { background: #fff3cd; color: #6b5300; }
.badge.lvl-loan { background: #e3ecfa; color: #1c3f7a; }
@media (prefers-color-scheme: dark) {
  .badge.lvl-federal, .badge.lvl-loan { background: #1c2d4a; color: #b7cdf5; }
  .badge.lvl-state { background: #1f2d26; color: #9fe0c0; }
  .badge.lvl-county { background: #3a2a0c; color: #ffd79a; }
  .badge.lvl-city { background: #3d1a2a; color: #ffb3d1; }
  .badge.lvl-utility, .badge.lvl-gas_utility { background: #3a3206; color: #ffe58a; }
}
.chip { font-size: 0.78rem; padding: 2px 8px; border-radius: 6px; background: var(--bg); border: 1px solid var(--line); }
.card.conf-low { opacity: 0.85; }
.card.resource .program { font-weight: 600; text-decoration: none; }
.card.resource .program:hover { text-decoration: underline; }
.results-footer { margin: 32px 0 48px; font-size: 0.85rem; color: var(--muted); border-top: 1px solid var(--line); padding-top: 12px; }

@media (max-width: 760px) {
  .search-card { grid-template-columns: 1fr; }
  #category { min-width: 0; }
  .household-grid { grid-template-columns: 1fr 1fr; }
  .results-toolbar { flex-direction: column; }
}
@media print {
  .site-header, .search-card, .status, #print-btn, .category-hints { display: none !important; }
  body { background: #fff; color: #000; }
  .card { box-shadow: none; break-inside: avoid; }
  .link::after { content: " (" attr(href) ")"; font-weight: 400; font-size: 0.8em; }
}

/* New York edition additions */
.state-tag { font-size: 0.8rem; font-weight: 700; vertical-align: middle; margin-left: 6px; padding: 2px 8px; border-radius: 999px; background: var(--accent); color: var(--brand); }
.section-note { margin: -4px 0 10px; font-size: 0.9rem; }
.badge.curated { background: var(--brand); color: var(--brand-ink); }
.badge.inc-income_qualified, .badge.inc-tiered { background: var(--warn-bg); color: var(--warn-ink); }
.badge.inc-any { background: transparent; border: 1px solid var(--line); }
.badge.lvl-nonprofit { background: #fde7ef; color: #8a1f4f; }
.badge.lvl-lender { background: #e3ecfa; color: #1c3f7a; }
.for-utility { font-size: 0.82rem; font-weight: 600; color: var(--warn-ink); background: var(--warn-bg); padding: 4px 8px; border-radius: 6px; }
.link-btn { font: inherit; font-size: 0.85rem; color: var(--brand); background: none; border: none; padding: 0; cursor: pointer; text-decoration: underline; }
.card.src-ai { border-style: dashed; }
@media (prefers-color-scheme: dark) {
  .badge.lvl-nonprofit { background: #3d1a2a; color: #ffb3d1; }
  .badge.lvl-lender { background: #1c2d4a; color: #b7cdf5; }
}

/* Card layout: program name on its own line, amount wraps underneath */
.card-head { flex-direction: column; align-items: stretch; gap: 4px; }
.card-head .program { line-height: 1.3; }
.amount { white-space: normal; overflow-wrap: anywhere; font-size: 0.95rem; line-height: 1.4; }
.household-grid { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 760px) { .household-grid { grid-template-columns: 1fr 1fr; } }

/* Compact form: six detail fields on one row on wide screens */
.household-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; }
.household-grid .field:nth-child(2) { grid-column: span 1; }
.household-grid select, .household-grid input { padding: 8px 10px; }
@media (max-width: 1000px) { .household-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 640px) { .household-grid { grid-template-columns: 1fr 1fr; } }

/* Property record and building department cards */
.property-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 12px; margin: 12px 0; }
.facts { display: grid; grid-template-columns: 1fr; gap: 2px; font-size: 0.9rem; }
.fact { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border-bottom: 1px dashed var(--line); }
.fact-l { color: var(--muted); }
.fact-v { text-align: right; }
.links { display: flex; flex-direction: column; gap: 4px; }
.link-primary { font-size: 1rem; padding: 6px 10px; border: 1px solid var(--brand); border-radius: 8px; display: inline-block; width: fit-content; }

/* Address suggestions dropdown */
.address-field { position: relative; }
.suggestions {
  position: absolute; left: 0; right: 0; top: 100%; z-index: 20; margin: 4px 0 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  max-height: 280px; overflow-y: auto;
}
.suggestions li { padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 0.95rem; }
.suggestions li:hover, .suggestions li[aria-selected="true"] { background: var(--accent); }
.suggestions li .src { float: right; font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.dept-address { font-size: 1rem; font-weight: 600; margin: 2px 0; }
.dept-phone { font-size: 0.95rem; }
.suggest-status { font-size: 0.82rem; margin-top: 4px; }

/* Clickable cards and the details panel */
.card.clickable { cursor: pointer; transition: box-shadow 0.15s ease, border-color 0.15s ease, transform 0.15s ease; }
.card.clickable:hover, .card.clickable:focus-visible {
  border-color: var(--brand); box-shadow: 0 0 0 2px var(--accent), 0 10px 24px rgba(0, 0, 0, 0.12); transform: translateY(-2px); outline: none;
}
.card-more { margin-top: auto; font-size: 0.85rem; font-weight: 600; color: var(--brand); }
body.modal-open { overflow: hidden; }
.modal {
  position: fixed; inset: 0; z-index: 100; background: rgba(15, 23, 20, 0.55);
  display: flex; align-items: center; justify-content: center; padding: 24px 16px;
}
.modal[hidden] { display: none; }
.modal-box {
  position: relative; background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 14px;
  width: min(50vw, 720px); min-width: min(100%, 340px); max-height: 85vh; overflow-y: auto; padding: 28px 28px 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
@media (max-width: 900px) { .modal-box { width: 92vw; } }
.modal-close {
  position: absolute; top: 10px; right: 12px; width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font-size: 1.4rem; line-height: 1; cursor: pointer;
}
.modal-close:hover, .modal-close:focus-visible { border-color: var(--brand); color: var(--brand); outline: none; }
.modal-title { font-size: 1.3rem; margin: 0 36px 6px 0; line-height: 1.3; }
.modal-amount { font-size: 1.05rem; margin-bottom: 8px; }
.modal-body .badges { margin: 6px 0 10px; }
.details { display: grid; grid-template-columns: 1fr; gap: 10px; margin: 14px 0 18px; }
.detail { display: grid; grid-template-columns: 150px 1fr; gap: 12px; padding-top: 10px; border-top: 1px solid var(--line); }
.detail-l { color: var(--muted); font-size: 0.85rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; padding-top: 2px; }
.detail-v { font-size: 0.97rem; }
@media (max-width: 560px) { .detail { grid-template-columns: 1fr; gap: 4px; } }
.modal-link { display: inline-block; text-decoration: none; margin-top: 4px; }
.modal-url { margin-top: 6px; word-break: break-all; }
@media print { .modal { display: none !important; } .card-more { display: none; } }

/* Details panel, styled like an image preview: dark backdrop, centered content, round close at the screen's top-right */
.modal { background: rgba(0, 0, 0, 0.82); backdrop-filter: blur(2px); padding: 56px 16px 24px; }
.modal-box {
  border: none; border-radius: 16px; box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
  width: min(52vw, 760px); max-height: 82vh; padding: 30px 32px 28px;
}
@media (max-width: 900px) { .modal-box { width: 94vw; } }
.modal-close {
  position: fixed; top: 16px; right: 20px; width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.35); background: rgba(255, 255, 255, 0.12); color: #fff; font-size: 1.5rem;
}
.modal-close:hover, .modal-close:focus-visible { background: rgba(255, 255, 255, 0.28); border-color: #fff; color: #fff; }
.modal-title { margin-right: 0; }
.inline-btn { margin-left: 10px; vertical-align: middle; padding: 8px 14px; }
.settings-form { display: flex; flex-direction: column; gap: 14px; margin: 16px 0 8px; }
.settings-form input { width: 100%; }
.settings-actions { display: flex; gap: 10px; align-items: center; }
.settings-msg { min-height: 1.2em; }
.settings-link { display: block; margin-top: 8px; font-size: 0.85rem; }

/* Clickable summary tiles */
.stat { cursor: pointer; transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease; text-align: left; font: inherit; color: inherit; }
.stat:hover, .stat:focus-visible { border-color: var(--brand); box-shadow: 0 0 0 2px var(--accent); transform: translateY(-1px); outline: none; }
.stat.active { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.stat.active .stat-l { color: var(--brand-ink); opacity: 0.85; }
.filter-note { font-size: 0.9rem; margin: -4px 0 10px; }

/* Status line under the address box must not change the row height */
.suggest-status { position: absolute; left: 2px; top: 100%; margin-top: 3px; z-index: 5; pointer-events: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.suggestions { z-index: 20; }

/* Phone layout: nothing may exceed the screen width */
html, body { max-width: 100%; overflow-x: hidden; }
select, input { width: 100%; max-width: 100%; box-sizing: border-box; }
.search-card { grid-template-columns: minmax(0, 1fr) auto auto; }
.household-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); }
@media (max-width: 1000px) { .household-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 760px) {
  .wrap { padding: 0 12px; }
  .search-card { grid-template-columns: minmax(0, 1fr); padding: 14px; }
  .search-card .field { min-width: 0; }
  #category { min-width: 0; width: 100%; }
  #search-btn { width: 100%; }
  .household-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .results-toolbar { flex-direction: column; align-items: stretch; }
  .results-toolbar .btn { width: 100%; }
  .summary .stat { flex: 1 1 calc(50% - 10px); min-width: 0; }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .card { padding: 12px 14px; }
  .modal-box { width: 94vw; padding: 22px 16px 20px; }
  .modal-close { top: 10px; right: 10px; }
  .detail { grid-template-columns: 1fr; gap: 4px; }
  h1 { font-size: 1.25rem; }
  .brand-mark { width: 36px; height: 36px; font-size: 1.2rem; }
}
@media (max-width: 420px) {
  .household-grid { grid-template-columns: minmax(0, 1fr); }
  .summary .stat { flex: 1 1 calc(50% - 10px); }
}

/* Reserve a strip under the address box for the status line so it never overlaps or shifts anything */
.address-field { padding-bottom: 22px; }
.suggest-status { top: auto; bottom: 2px; height: 18px; line-height: 18px; margin-top: 0; }
.suggestions { top: calc(100% - 22px); }
.search-card { align-items: start; }
#search-btn { margin-top: 1.55rem; }
@media (max-width: 760px) {
  #search-btn { margin-top: 0; }
  .address-field { padding-bottom: 24px; }
}
