/* ============================================================
   RENT VS BUY — Cape Town Calculator
   Art direction: Financial precision, cool slate + teal accent
   ============================================================ */

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

:root, [data-theme="light"] {
  --font-body: 'General Sans', 'Inter', sans-serif;
  --font-display: 'Cabinet Grotesk', 'General Sans', sans-serif;

  --color-bg: #f5f6f8;
  --color-surface: #ffffff;
  --color-surface-2: #f0f1f4;
  --color-border: #dde0e6;
  --color-divider: #e8eaee;

  --color-text: #1a1d26;
  --color-text-muted: #6b7080;
  --color-text-faint: #a0a4b0;

  --color-primary: #0c7c84;
  --color-primary-hover: #0a5f66;
  --color-primary-light: #e0f4f4;
  --color-accent: #f59e0b;

  --color-buy: #0c7c84;
  --color-buy-light: rgba(12, 124, 132, 0.12);
  --color-rent: #e67e22;
  --color-rent-light: rgba(230, 126, 34, 0.12);

  --color-success: #16a34a;
  --color-warning: #d97706;
  --color-error: #dc2626;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.12);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  --transition: 200ms cubic-bezier(0.16, 1, 0.3, 1);

  --panel-width: 480px;
}

[data-theme="dark"] {
  --color-bg: #0f1117;
  --color-surface: #181b24;
  --color-surface-2: #1e2130;
  --color-border: #2a2e3a;
  --color-divider: #252836;

  --color-text: #e2e4ea;
  --color-text-muted: #8a8e9c;
  --color-text-faint: #555968;

  --color-primary: #2ec4b6;
  --color-primary-hover: #22a99d;
  --color-primary-light: rgba(46, 196, 182, 0.12);

  --color-buy: #2ec4b6;
  --color-buy-light: rgba(46, 196, 182, 0.10);
  --color-rent: #f0a050;
  --color-rent-light: rgba(240, 160, 80, 0.10);

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.5);
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  font-size: 16px;
}

body {
  font-family: var(--font-body);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.5;
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
}

a, button, input, select, textarea {
  transition: color var(--transition), background var(--transition), border-color var(--transition), box-shadow var(--transition), opacity var(--transition);
}

/* --- Header --- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 52px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  z-index: 100;
  position: relative;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo { color: var(--color-primary); flex-shrink: 0; }

.header-title {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.02em;
}

.header-subtitle {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.header-right {
  display: flex;
  gap: 4px;
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  cursor: pointer;
  border: none;
  background: none;
}
.btn-icon:hover { background: var(--color-surface-2); color: var(--color-text); }
.btn-icon svg { width: 18px; height: 18px; }

/* --- Main Layout --- */
.app-main {
  display: flex;
  height: calc(100vh - 52px);
  height: calc(100dvh - 52px);
  position: relative;
}

/* --- Map --- */
.map-container {
  flex: 1;
  position: relative;
  min-width: 0;
}

#map {
  width: 100%;
  height: 100%;
}

.maplibregl-canvas { outline: none; }

/* Map Legend */
.map-legend {
  position: absolute;
  bottom: 24px;
  left: 16px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  font-size: 0.75rem;
  box-shadow: var(--shadow-md);
  z-index: 10;
  min-width: 200px;
}

.legend-title {
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--color-text);
}

.legend-scale {
  height: 10px;
  border-radius: 5px;
  background: linear-gradient(to right, #2ec4b6, #f0e68c, #e67e22, #dc2626);
  margin-bottom: 6px;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  color: var(--color-text-muted);
  font-size: 0.7rem;
}

/* Map Tooltip */
.map-tooltip {
  display: none;
  position: absolute;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 0.8rem;
  box-shadow: var(--shadow-md);
  z-index: 20;
  pointer-events: none;
  max-width: 240px;
}
.map-tooltip.visible { display: block; }
.map-tooltip .tt-zip { font-weight: 600; margin-bottom: 2px; }
.map-tooltip .tt-neighborhood { color: var(--color-text-muted); font-size: 0.72rem; margin-bottom: 4px; }
.map-tooltip .tt-row { display: flex; justify-content: space-between; gap: 16px; }
.map-tooltip .tt-label { color: var(--color-text-muted); }
.map-tooltip .tt-value { font-weight: 600; font-variant-numeric: tabular-nums; }

/* Mobile Search */
.mobile-search {
  display: block;
  position: absolute;
  top: 12px;
  left: 12px;
  right: 60px;
  z-index: 50;
}

.search-input-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 0 12px;
  box-shadow: var(--shadow-md);
}
.search-input-wrap svg { width: 16px; height: 16px; color: var(--color-text-muted); flex-shrink: 0; }
.search-input-wrap input {
  flex: 1;
  border: none;
  background: none;
  padding: 10px 0;
  font-size: 0.9rem;
  color: var(--color-text);
  outline: none;
}
.search-input-wrap input::placeholder { color: var(--color-text-faint); }
.search-clear { display: none; }
.search-clear.visible { display: flex; }

.search-results {
  margin-top: 6px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  max-height: 300px;
  overflow-y: auto;
  display: none;
}
.search-results.visible { display: block; }
.search-result-item {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--color-divider);
  font-size: 0.85rem;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--color-surface-2); }
.search-result-item .sr-zip { font-weight: 600; }
.search-result-item .sr-name { color: var(--color-text-muted); margin-left: 8px; }
.search-result-item .sr-price { float: right; font-variant-numeric: tabular-nums; color: var(--color-text-muted); font-size: 0.78rem; }
.search-no-results { padding: 12px 14px; font-size: 0.85rem; color: var(--color-text-muted); }

/* --- Popular Neighborhoods --- */
.popular-zips {
  margin-top: 8px;
}
.popular-title {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 700;
  margin: 0 0 4px;
}
.popular-subtitle {
  font-size: 0.8rem;
  color: var(--color-text-muted);
  margin: 0 0 16px;
}
.popular-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.popular-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  text-align: left;
  font-family: var(--font-body);
  font-size: 0.82rem;
  color: var(--color-text);
}
.popular-item:hover {
  background: var(--color-surface);
  border-color: var(--color-accent);
}
.pi-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 8px;
}
.pi-price {
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
  font-size: 0.78rem;
  flex-shrink: 0;
}

/* --- Side Panel --- */
.side-panel {
  width: var(--panel-width);
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  overflow-y: auto;
  overflow-x: hidden;
  flex-shrink: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

.panel-content {
  padding: 20px;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.panel-zip-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-back { display: none; }
.btn-back.visible { display: flex; }

.zip-name {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.zip-code-label {
  font-size: 0.78rem;
  color: var(--color-text-muted);
}

.zip-band {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.zip-band.premium { background: rgba(139, 92, 246, 0.15); color: #8b5cf6; }
.zip-band.high { background: rgba(14, 165, 233, 0.15); color: #0ea5e9; }
.zip-band.mid { background: rgba(34, 197, 94, 0.15); color: #22c55e; }
.zip-band.affordable { background: rgba(245, 158, 11, 0.15); color: #f59e0b; }

/* Breakeven */
.breakeven-section {
  text-align: center;
  padding: 20px 0;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--color-divider);
}

.breakeven-label {
  font-size: 0.72rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}

.breakeven-year {
  font-family: var(--font-display);
  font-size: clamp(2.5rem, 5vw, 3.5rem);
  font-weight: 800;
  line-height: 1.1;
  color: var(--color-primary);
  margin: 4px 0;
  transition: all 400ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

.breakeven-ci {
  font-size: 0.78rem;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

.breakeven-verdict {
  margin-top: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  display: inline-block;
}
.breakeven-verdict.buy { background: var(--color-buy-light); color: var(--color-buy); }
.breakeven-verdict.rent { background: var(--color-rent-light); color: var(--color-rent); }

/* Quick Stats */
.quick-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--color-surface-2);
  border-radius: var(--radius-sm);
  padding: 12px;
}

.stat-label {
  display: block;
  font-size: 0.7rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 1.05rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

/* Charts */
.chart-section, .sensitivity-section {
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--color-divider);
}

.section-title {
  font-size: 0.9rem;
  font-weight: 700;
  margin-bottom: 2px;
}

.chart-subtitle {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  margin-bottom: 12px;
}

canvas {
  width: 100% !important;
  height: auto !important;
  border-radius: var(--radius-sm);
}

.chart-legend-row {
  display: flex;
  gap: 16px;
  margin-top: 8px;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.legend-item { display: flex; align-items: center; gap: 5px; }
.legend-swatch {
  width: 12px;
  height: 3px;
  border-radius: 2px;
  display: inline-block;
}

/* Inputs */
.inputs-section {
  padding-top: 4px;
}

.input-group {
  margin-bottom: 14px;
}

.input-group label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.82rem;
  font-weight: 500;
  margin-bottom: 5px;
  gap: 8px;
}

.input-val {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--color-primary);
  min-width: 40px;
  text-align: right;
}

/* Range Slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: var(--color-surface-2);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform 150ms ease;
}
input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}
input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-primary);
  border: 2px solid var(--color-surface);
  cursor: pointer;
}
input[type="range"]:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Select */
select {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
  color: var(--color-text);
  font-size: 0.85rem;
  cursor: pointer;
  outline: none;
}
select:focus { border-color: var(--color-primary); }

/* Checkbox */
input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--color-primary);
  cursor: pointer;
  margin-right: 4px;
}

.refi-row label { flex-wrap: wrap; gap: 4px; justify-content: flex-start; }
.refi-row input[type="range"] { margin-top: 4px; }

.salt-info {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  background: var(--color-surface-2);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  margin-top: 8px;
}
.salt-info svg { width: 14px; height: 14px; flex-shrink: 0; margin-top: 1px; }

/* Sources */
.sources-section {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--color-divider);
}
.sources-text {
  font-size: 0.72rem;
  color: var(--color-text-faint);
  line-height: 1.6;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 24px;
  max-width: 480px;
  width: 100%;
  box-shadow: var(--shadow-lg);
}
.modal-card h3 {
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 8px;
}
.modal-card p {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  margin-bottom: 14px;
}

.share-url-row {
  display: flex;
  gap: 8px;
}
.share-url-row input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
  color: var(--color-text);
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
}

.btn-primary {
  padding: 8px 18px;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
}
.btn-primary:hover { background: var(--color-primary-hover); }

.btn-text {
  display: block;
  margin-top: 12px;
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-size: 0.85rem;
  cursor: pointer;
  text-align: center;
  width: 100%;
}
.btn-text:hover { color: var(--color-text); }

/* --- Responsive --- */
@media (max-width: 860px) {
  .app-main {
    flex-direction: column;
  }

  .map-container {
    height: 45vh;
    height: 45dvh;
    flex: none;
  }

  .side-panel {
    width: 100%;
    height: 55vh;
    height: 55dvh;
    border-left: none;
    border-top: 1px solid var(--color-border);
  }

  .mobile-search { display: block; }
  .btn-back { display: flex; }

  .map-legend {
    bottom: 12px;
    left: 8px;
    padding: 8px 10px;
    font-size: 0.68rem;
    min-width: 160px;
  }

  .panel-content { padding: 16px; }
  .quick-stats { grid-template-columns: 1fr 1fr; gap: 8px; }
  
  --panel-width: 100%;
}

@media (max-width: 480px) {
  .map-container { height: 38vh; height: 38dvh; }
  .side-panel { height: 62vh; height: 62dvh; }
  .quick-stats { gap: 6px; }
  .stat-card { padding: 10px; }
  .stat-value { font-size: 0.95rem; }
}

/* Scrollbar */
.side-panel::-webkit-scrollbar { width: 6px; }
.side-panel::-webkit-scrollbar-track { background: transparent; }
.side-panel::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 3px; }

/* Animation utility */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}
.animate-in { animation: fadeInUp 400ms cubic-bezier(0.16, 1, 0.3, 1) forwards; }

/* Counter animation for breakeven */
@keyframes countPulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.08); }
  100% { transform: scale(1); }
}
.count-pulse { animation: countPulse 300ms cubic-bezier(0.34, 1.56, 0.64, 1); }
