/* === Reset & Variables === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Dark theme (default) */
:root {
  --bg: #0f1117;
  --bg-surface: #1a1d27;
  --bg-hover: #242736;
  --bg-selected: #2a2d3e;
  --border: #2d3044;
  --text: #e4e4e7;
  --text-muted: #8b8d9e;
  --text-dim: #6b6d7e;
  --green: #22c55e;
  --green-bg: #16351f;
  --blue: #3b82f6;
  --blue-bg: #1a2744;
  --amber: #f59e0b;
  --amber-bg: #3b2f0e;
  --red: #ef4444;
  --red-bg: #3b1616;
  --gray: #6b7280;
  --gray-bg: #242736;
  --radius: 6px;
  --radius-lg: 10px;
}

/* Light theme */
[data-theme="light"] {
  --bg: #f8f9fa;
  --bg-surface: #ffffff;
  --bg-hover: #f0f1f3;
  --bg-selected: #e8e9ec;
  --border: #d4d6dc;
  --text: #1a1d27;
  --text-muted: #5a5d6e;
  --text-dim: #8b8d9e;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --blue: #2563eb;
  --blue-bg: #dbeafe;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --gray: #6b7280;
  --gray-bg: #f0f1f3;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
}

/* === Header === */
header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
}
header h1 { font-size: 20px; font-weight: 600; }
.count { color: var(--text-muted); font-size: 13px; }
.header-right { margin-left: auto; }
.theme-toggle {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 16px;
  cursor: pointer;
  line-height: 1;
  transition: background 0.15s;
}
.theme-toggle:hover { background: var(--bg-hover); }
.view-toggle {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 16px;
  cursor: pointer;
  line-height: 1;
  color: var(--text);
  transition: background 0.15s;
}
.view-toggle:hover { background: var(--bg-hover); }

/* === Filters === */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}
.filter-group { display: flex; align-items: center; gap: 6px; }
.filter-group label { font-size: 12px; color: var(--text-muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px; }
.pill-group { display: flex; gap: 4px; flex-wrap: wrap; }
.pill-toggle {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}
.pill-toggle:hover { border-color: var(--text-dim); }
.pill-toggle.active { background: var(--blue); border-color: var(--blue); color: #fff; }
.filter-input {
  width: 80px;
  padding: 4px 8px;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-surface);
  color: var(--text);
}
.filter-input.score-input { width: 55px; }
.filter-sep { color: var(--text-dim); font-size: 12px; }

/* === Loading === */
.loading { padding: 60px 20px; text-align: center; color: var(--text-muted); }
.loading-sm { color: var(--text-muted); font-size: 13px; padding: 8px 0; }

/* === Table === */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead { position: sticky; top: 0; background: var(--bg); z-index: 5; }
th {
  text-align: left;
  padding: 8px 12px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  user-select: none;
}
th.sortable { cursor: pointer; }
th.sortable:hover { color: var(--text); }
th.num, td.num { text-align: right; }
td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  font-size: 13px;
}
.listing-row { cursor: pointer; transition: background 0.1s; }
.listing-row:hover { background: var(--bg-hover); }
.listing-row.selected { background: var(--bg-selected); }
.address-cell {
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.redfin-link { color: var(--blue); text-decoration: none; font-size: 16px; }

/* === Status Pills === */
.status-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}
.status-strong { background: var(--green-bg); color: var(--green); }
.status-look { background: var(--blue-bg); color: var(--blue); }
.status-reviewing { background: var(--amber-bg); color: var(--amber); }
.status-notfit, .status-pass { background: var(--gray-bg); color: var(--gray); }
.status-none { background: var(--gray-bg); color: var(--text-dim); }

/* === Score Badge === */
.score-badge {
  display: inline-block;
  min-width: 28px;
  text-align: center;
  padding: 1px 6px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
}
.score-badge.lg { font-size: 22px; padding: 4px 12px; border-radius: var(--radius-lg); }
.score-high { background: var(--green-bg); color: var(--green); }
.score-mid { background: var(--amber-bg); color: var(--amber); }
.score-low { background: var(--red-bg); color: var(--red); }

/* === Price Drop === */
.price-drop-icon { color: var(--red); font-size: 10px; margin-left: 4px; }

/* === Detail Panel === */
.detail-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 20;
}
.detail-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 440px;
  height: 100vh;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  z-index: 30;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.2s ease;
}
.detail-panel.open { transform: translateX(0); }
.detail-content { padding: 20px; }
.detail-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 28px;
  cursor: pointer;
  line-height: 1;
  z-index: 1;
}
.detail-close:hover { color: var(--text); }
.detail-photo {
  width: 100%;
  border-radius: var(--radius-lg);
  margin-bottom: 16px;
  object-fit: cover;
  max-height: 260px;
}
.detail-address { font-size: 18px; font-weight: 600; margin-bottom: 4px; padding-right: 40px; }
.detail-redfin-link { color: var(--blue); text-decoration: none; font-size: 13px; }
.detail-redfin-link:hover { text-decoration: underline; }

/* === Detail Sections === */
.detail-section { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }
.detail-section h3 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); margin-bottom: 10px; }
.detail-row { margin-bottom: 10px; }
.detail-row label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.status-select {
  width: 100%;
  padding: 8px 10px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
}
.redfin-status-active { border-color: var(--green); }
.redfin-status-pending { border-color: var(--amber); }
.redfin-status-sold { border-color: var(--red); }
.notes-input {
  width: 100%;
  min-height: 60px;
  padding: 8px 10px;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  resize: vertical;
}

/* === Stats Grid === */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.stats-grid.compact { grid-template-columns: repeat(2, 1fr); }
.stat {
  padding: 6px 8px;
  background: var(--bg);
  border-radius: var(--radius);
}
.stat-label { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-dim); }
.stat-value { font-size: 13px; font-weight: 500; }

/* === Evaluation === */
.eval-header { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.verdict { font-size: 16px; font-weight: 500; }
.eval-summary {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-muted);
  margin-bottom: 12px;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: var(--radius);
  border-left: 3px solid var(--blue);
}
.flags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.flag {
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 400;
}
.flag.green { background: var(--green-bg); color: var(--green); }
.flag.red { background: var(--red-bg); color: var(--red); }

/* === Sub Tables (drive times, schools, etc) === */
.sub-table { width: 100%; font-size: 13px; }
.sub-table th { font-size: 10px; padding: 4px 8px; border-bottom: 1px solid var(--border); }
.sub-table td { padding: 4px 8px; border-bottom: 1px solid var(--border); white-space: normal; }
.diff-better { color: var(--green); }
.diff-worse { color: var(--red); }
.school-rating { font-weight: 600; }
.school-rating.good { color: var(--green); }
.school-rating.ok { color: var(--amber); }
.school-rating.poor { color: var(--red); }

/* === Fiber === */
.fiber-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}
.fiber-badge.yes { background: var(--green-bg); color: var(--green); }
.fiber-badge.no { background: var(--red-bg); color: var(--red); }
.broadband-providers { font-size: 12px; color: var(--text-muted); margin-top: 6px; }

/* === Climate Risk === */
.risk-low { color: var(--green); }
.risk-mid { color: var(--amber); }
.risk-high { color: var(--red); }

/* === Description === */
.description { font-size: 13px; color: var(--text-muted); line-height: 1.6; }
.muted { color: var(--text-dim); font-size: 13px; }

/* === Sort Bar (card view) === */
.sort-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  border-bottom: 1px solid var(--border);
}
.sort-bar label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.sort-bar select {
  padding: 4px 8px;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-surface);
  color: var(--text);
}
.sort-dir-btn {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 4px 8px;
  cursor: pointer;
  font-size: 12px;
}
.sort-dir-btn:hover { background: var(--bg-hover); }

/* === Card View === */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
  padding: 16px 20px;
}
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.card:hover { border-color: var(--blue); box-shadow: 0 2px 12px rgba(0,0,0,0.2); }
.card.selected { border-color: var(--blue); }
.card-img-wrap { position: relative; height: 180px; background: var(--bg); overflow: hidden; }
.card-img { width: 100%; height: 100%; object-fit: cover; }
.card-img-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-dim);
  font-size: 13px;
}
.card-score {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 14px;
  padding: 2px 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.card-status {
  position: absolute;
  top: 8px;
  left: 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.card-body { padding: 12px; }
.card-price-row { display: flex; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.card-price { font-size: 18px; font-weight: 600; }
.card-monthly { font-size: 12px; color: var(--text-muted); margin-left: auto; }
.card-address { font-size: 13px; color: var(--text-muted); margin-bottom: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-stats {
  display: flex;
  gap: 10px;
  font-size: 12px;
  color: var(--text);
  margin-bottom: 6px;
}
.card-meta {
  display: flex;
  gap: 10px;
  font-size: 11px;
  color: var(--text-dim);
}
.card-fiber {
  color: var(--green);
  font-weight: 500;
}

/* === Mobile === */
@media (max-width: 768px) {
  header { padding: 12px 16px 10px; }
  .filters { padding: 10px 16px; gap: 8px; }
  .filter-group { flex-wrap: wrap; }
  .hide-mobile { display: none !important; }
  .address-cell { max-width: 160px; }
  td, th { padding: 8px; }
  .detail-panel { width: 100vw; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .cards-grid { grid-template-columns: 1fr; padding: 12px 16px; gap: 12px; }
  .sort-bar { padding: 8px 16px; }
}
