:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #1f2933;
  --muted: #6b7785;
  --line: #e3e8ef;
  --brand: #1f6f54;
  --brand-dark: #155742;
  --good: #1f7a4d;
  --good-bg: #e7f6ed;
  --ok: #8a6d00;
  --ok-bg: #fdf6e3;
  --bad: #a3261d;
  --bad-bg: #fbeae8;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
}

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.hidden { display: none; }

/* ---------- Login ---------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}
.login-card {
  background: var(--card);
  padding: 2rem;
  border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0,0,0,0.08);
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.login-card h1 { margin: 0 0 0.25rem; font-size: 1.3rem; }
.login-card form { margin-top: 1.25rem; text-align: left; }
.login-card label { display: block; font-weight: 600; margin-bottom: 0.35rem; }
.login-card input {
  width: 100%;
  padding: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 1rem;
}
.login-card button { width: 100%; margin-top: 1rem; }

/* ---------- Top bar ---------- */
.topbar {
  background: var(--brand);
  color: #fff;
  padding: 0.85rem 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.brand { font-weight: 700; }
.logout { color: #d7efe6; text-decoration: none; font-size: 0.9rem; }
.logout:hover { text-decoration: underline; }

/* ---------- Layout ---------- */
.wrap { max-width: 1000px; margin: 1.5rem auto; padding: 0 1rem; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.25rem;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}
.card h2 { margin: 0 0 1rem; font-size: 1.1rem; color: var(--brand-dark); }
.card h3 { font-size: 1rem; margin: 1.25rem 0 0.5rem; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.9rem 1.2rem;
}
.grid label { display: flex; flex-direction: column; font-size: 0.85rem; font-weight: 600; color: #41505f; gap: 0.3rem; }
.grid input {
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 400;
}
.grid input:focus { outline: 2px solid var(--brand); border-color: var(--brand); }

/* ---------- Buttons ---------- */
.actions { display: flex; gap: 0.75rem; flex-wrap: wrap; margin-top: 1rem; }
button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 0.7rem 1.2rem;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
}
button:hover { background: #f0f3f6; }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
button.primary:hover { background: var(--brand-dark); }
button:disabled { opacity: 0.6; cursor: default; }

/* ---------- KPIs ---------- */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.85rem;
  margin-bottom: 1.25rem;
}
.kpi {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.kpi-label { font-size: 0.78rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.kpi-value { font-size: 1.35rem; font-weight: 700; }
.kpi.good { background: var(--good-bg); border-color: #bce3cd; }
.kpi.good .kpi-value { color: var(--good); }
.kpi.ok { background: var(--ok-bg); border-color: #ecdca6; }
.kpi.ok .kpi-value { color: var(--ok); }
.kpi.bad { background: var(--bad-bg); border-color: #f0c4be; }
.kpi.bad .kpi-value { color: var(--bad); }

/* ---------- Tables ---------- */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
@media (max-width: 700px) { .two-col { grid-template-columns: 1fr; } }

table.lines { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
table.lines td { padding: 0.4rem 0; border-bottom: 1px solid var(--line); }
table.lines td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
table.lines tr.subtotal td { font-weight: 600; }
table.lines tr.total td { font-weight: 700; border-top: 2px solid var(--ink); border-bottom: none; padding-top: 0.5rem; }

.table-scroll { overflow-x: auto; }
table.projection { width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-top: 0.5rem; }
table.projection th, table.projection td { padding: 0.45rem 0.6rem; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
table.projection th:first-child, table.projection td:first-child { text-align: center; }
table.projection thead th { background: #eef2f6; color: #41505f; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.02em; }
table.projection td.pos { color: var(--good); }
table.projection td.neg { color: var(--bad); }

/* ---------- Analysis ---------- */
.analysis { margin-top: 1.5rem; border-top: 2px dashed var(--line); padding-top: 1rem; }
.analysis h3 { font-size: 1.05rem; }
#analysis-body h3, #analysis-body h2 { color: var(--brand-dark); margin: 1rem 0 0.4rem; font-size: 1rem; }
#analysis-body ul, #analysis-body ol { margin: 0.3rem 0 0.8rem 1.2rem; padding: 0; }
#analysis-body li { margin: 0.2rem 0; }
#analysis-body p { margin: 0.5rem 0; }
#analysis-body code { background: #eef2f6; padding: 0.1rem 0.3rem; border-radius: 4px; font-size: 0.9em; }

.alert {
  background: var(--bad-bg);
  color: var(--bad);
  border: 1px solid #f0c4be;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin: 0.5rem 0;
}

/* ---------- Nav ---------- */
.topnav { display: flex; gap: 1.1rem; align-items: center; }
.topnav a { color: #d7efe6; text-decoration: none; font-size: 0.9rem; }
.topnav a:hover { text-decoration: underline; }
.topnav a.active { color: #fff; font-weight: 700; }

.page-title { font-size: 1.3rem; margin: 0 0 1rem; }

/* Full-width form fields (address, listing link) */
.grid label.span-2 { grid-column: 1 / -1; }

/* ---------- Results header + realtor link ---------- */
.results-head { display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; gap: 0.5rem; }
.realtor-link {
  display: inline-block;
  color: var(--brand);
  font-weight: 600;
  text-decoration: none;
  font-size: 0.9rem;
}
.realtor-link:hover { text-decoration: underline; }

.loaded-note {
  background: #eef5ff;
  border: 1px solid #cfe0f7;
  color: #234;
  padding: 0.7rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.92rem;
}

/* ---------- Listing fetch ---------- */
.listing-fetch { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
input[readonly] { background: #f1f4f8; color: #41505f; }
.paste-listing summary { cursor: pointer; font-size: 0.85rem; font-weight: 600; color: var(--brand); }
.paste-listing summary:hover { text-decoration: underline; }
.paste-listing textarea {
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 0.9rem;
  resize: vertical;
}

/* ---------- Model picker ---------- */
.ai-bar { margin-top: 1.25rem; }
.model-pick { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.8rem; font-weight: 600; color: #41505f; max-width: 360px; }
.model-pick select {
  padding: 0.55rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.95rem;
  background: #fff;
}

/* ---------- Save bar ---------- */
.save-bar {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}
.save-bar .saveby { display: flex; flex-direction: column; font-size: 0.8rem; font-weight: 600; color: #41505f; gap: 0.3rem; }
.save-bar .saveby input {
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.95rem;
}
.save-err { color: var(--bad); }

/* ---------- Saved properties page ---------- */
.btn-link {
  display: inline-block;
  text-decoration: none;
  padding: 0.4rem 0.8rem;
  border-radius: 7px;
  border: 1px solid var(--line);
  color: var(--ink);
  font-weight: 600;
  font-size: 0.88rem;
}
.btn-link:hover { background: #f0f3f6; }
.btn-link.primary { background: var(--brand); border-color: var(--brand); color: #fff; }

.saved-group .saved-head { display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 0.6rem; }
.saved-group h2 { margin: 0; font-size: 1.05rem; color: var(--brand-dark); }

table.saved-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.saved-table th, table.saved-table td { padding: 0.5rem 0.6rem; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.saved-table th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.02em; color: #41505f; background: #eef2f6; }
table.saved-table td.pos { color: var(--good); font-weight: 600; }
table.saved-table td.neg { color: var(--bad); font-weight: 600; }
.row-actions { display: flex; gap: 0.5rem; align-items: center; }
.row-actions form { margin: 0; }
.link-danger {
  background: none;
  border: none;
  color: var(--bad);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.88rem;
  padding: 0.4rem 0.4rem;
}
.link-danger:hover { text-decoration: underline; background: none; }

/* ---------- Print ---------- */
@media print {
  .topbar, .actions, #proj-form { display: none; }
  body { background: #fff; }
  .card { box-shadow: none; border: 1px solid #ccc; }
}
