/* ============================================================================
   CSS Variables
   ============================================================================ */

:root {
  --color-primary: #2563eb;
  --color-primary-dark: #1d4ed8;
  --color-success: #059669;
  --color-warning: #d97706;
  --color-danger: #dc2626;
  --color-bg: #f8fafc;
  --color-surface: #ffffff;
  --color-text: #1e293b;
  --color-text-muted: #64748b;
  --color-border: #e2e8f0;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  --font-sans:
    system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ============================================================================
   Reset & Base
   ============================================================================ */

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
}

img {
  max-width: 100%;
  height: auto;
  display: block;
}

button {
  font-family: inherit;
  cursor: pointer;
}

/* ============================================================================
   Utilities
   ============================================================================ */

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

[hidden] {
  display: none !important;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

/* ============================================================================
   Header
   ============================================================================ */

.site-header {
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%);
  padding: 2rem 0;
  margin-bottom: 2rem;
}

.site-header h1 {
  font-size: 1.875rem;
  font-weight: 700;
  color: #fff;
}

.site-header .subtitle {
  color: rgba(255, 255, 255, 0.85);
  margin-top: 0.25rem;
}

.last-run {
  color: rgba(255, 255, 255, 0.7);
  font-size: 0.8125rem;
  margin-top: 0.25rem;
}

/* ============================================================================
   Loading & Error States
   ============================================================================ */

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4rem 1rem;
  color: var(--color-text-muted);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.error-message {
  background-color: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius-md);
  padding: 1.5rem;
  text-align: center;
  color: var(--color-danger);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  border: none;
  transition: background-color 0.15s ease;
}

.btn-secondary {
  background-color: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

.btn-secondary:hover {
  background-color: var(--color-bg);
}

/* ============================================================================
   Property Grid
   ============================================================================ */

.property-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
  padding-bottom: 2rem;
}

/* ============================================================================
   Property Card
   ============================================================================ */

.property-card {
  background-color: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
  overflow: hidden;
  transition:
    box-shadow 0.2s ease,
    transform 0.2s ease;
  cursor: pointer;
}

.property-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.property-card.is-new-find {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1), var(--shadow-sm);
  position: relative;
}

.property-card.is-new-find:hover {
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15), var(--shadow-md);
}

.property-card:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.property-card-media {
  position: relative;
  overflow: hidden;
}

.property-card-media .badge-score {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  box-shadow: var(--shadow-sm);
}

.property-card-image {
  width: 100%;
  height: 180px;
  object-fit: cover;
  background-color: var(--color-bg);
  transition: transform 0.3s ease;
}

.property-card:hover .property-card-image {
  transform: scale(1.04);
}

.property-card-image-placeholder {
  width: 100%;
  height: 180px;
  background-color: var(--color-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  font-size: 0.875rem;
}

.property-card-content {
  padding: 1rem;
}

.property-card-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 0.5rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.property-card-price {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--color-primary);
  margin-bottom: 0.5rem;
}

.property-card-address {
  font-size: 0.875rem;
  color: var(--color-text-muted);
  margin-bottom: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.property-card-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
}

.badge-score {
  background-color: var(--color-bg);
  color: var(--color-text);
}

.badge-score.excellent {
  background-color: #dcfce7;
  color: #166534;
}
.badge-score.good {
  background-color: #dbeafe;
  color: #1e40af;
}
.badge-score.average {
  background-color: #fef3c7;
  color: #92400e;
}
.badge-score.below-average {
  background-color: #fee2e2;
  color: #991b1b;
}

.badge-energy {
  background-color: #f0fdf4;
  color: #166534;
}

.badge-energy.unknown {
  background-color: var(--color-bg);
  color: var(--color-text-muted);
}

/* Last seen badge */
.badge-last-seen {
  background-color: #f0fdf4;
  color: #166534;
}

.badge-online {
  background-color: #dbeafe; /* blue-100 */
  color: #1e40af; /* blue-800 */
}

.badge-new {
  background-color: var(--color-primary);
  color: white;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.025em;
}

.badge-not-available {
  background-color: #fee2e2; /* red-100 */
  color: #991b1b; /* red-800 */
  font-weight: 500;
  text-transform: none;
}

/* ============================================================================
   Filter Bar
   ============================================================================ */

.filter-bar {
  position: sticky;
  top: 0.5rem;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
  padding: 1rem;
  background-color: var(--color-surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  margin-bottom: 1rem;
}

.filter-panel {
  display: contents;
}

.filter-toggle {
  display: none;
}

.filter-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 0.25rem;
  margin-left: 0.375rem;
  border-radius: 999px;
  background: var(--color-primary);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
}

.filter-search {
  flex: 1;
  min-width: 200px;
}

.filter-search-input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background-color: var(--color-surface);
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease;
}

.filter-search-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.filter-label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--color-text-muted);
}

.filter-switch-container {
  height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 0.5rem;
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
}

.filter-checkbox {
  width: 1.25rem;
  height: 1.25rem;
  cursor: pointer;
}

.filter-select {
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  font-size: 0.875rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background-color: var(--color-surface);
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M2 4l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.5rem center;
  transition: border-color 0.15s ease;
}

.filter-select:focus {
  outline: none;
  border-color: var(--color-primary);
}

.filter-group-toggle {
  justify-content: flex-end;
}

.btn-icon {
  width: 2.5rem;
  height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  transition: background-color 0.15s ease;
}

.btn-icon:hover {
  background-color: var(--color-bg);
}

.sort-controls {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
}

.sort-controls .filter-select {
  flex: 1;
  min-width: 0;
}

.btn-icon.sort-ascending {
  transform: rotate(180deg);
}

.btn-icon:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ============================================================================
   Results Summary
   ============================================================================ */

.results-summary {
  font-size: 0.875rem;
  color: var(--color-text-muted);
  margin-bottom: 1rem;
}

.results-summary:empty {
  display: none;
}

/* ============================================================================
   Empty State
   ============================================================================ */

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--color-text-muted);
}

.empty-state p {
  margin-bottom: 1rem;
}

/* ============================================================================
   Modal
   ============================================================================ */

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  background-color: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  max-width: 700px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 0.5rem 0.625rem 1.25rem;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  flex-shrink: 0;
}

.modal-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.3;
  color: var(--color-text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.modal-scroll {
  overflow-y: auto;
}

.modal-close {
  width: 2.75rem;
  height: 2.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 50%;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.modal-close:hover {
  background-color: var(--color-bg);
}

.modal-loading,
.modal-error {
  padding: 4rem 2rem;
  text-align: center;
}

.modal-body {
  padding: 2rem;
}

/* ============================================================================
   Image Lightbox
   ============================================================================ */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
  background-color: rgba(0, 0, 0, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
}

.lightbox[hidden] {
  display: none;
}

.lightbox-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 3rem;
  height: 3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  transition: background-color 0.15s ease;
  z-index: 201;
}

.lightbox-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

.lightbox-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 3rem;
  height: 3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 1.25rem;
  cursor: pointer;
  transition: background-color 0.15s ease;
  z-index: 201;
}

.lightbox-nav:hover {
  background: rgba(255, 255, 255, 0.2);
}

.lightbox-prev {
  left: 1rem;
}

.lightbox-next {
  right: 1rem;
}

.lightbox-content {
  max-width: 90vw;
  max-height: 90vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.lightbox-content img {
  max-width: 100%;
  max-height: 90vh;
  object-fit: contain;
  border-radius: var(--radius-sm);
}

.lightbox-counter {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.875rem;
}

/* ============================================================================
   Detail View
   ============================================================================ */

.detail-header {
  margin-bottom: 1.5rem;
}

.detail-address {
  color: var(--color-text-muted);
  font-size: 0.875rem;
}

.detail-price-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--color-border);
}

.detail-price {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-primary);
}

.detail-price-per-sqm {
  color: var(--color-text-muted);
  font-size: 0.875rem;
}

.detail-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.detail-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.detail-gallery img:first-child {
  grid-column: 1 / -1;
  height: 200px;
}

.detail-gallery img {
  width: 100%;
  height: 80px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.detail-gallery img:hover {
  opacity: 0.8;
}

.detail-gallery img:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.detail-attributes {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.detail-attribute {
  display: flex;
  flex-direction: column;
}

.detail-attribute-label {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.detail-attribute-value {
  font-size: 1rem;
  font-weight: 500;
  color: var(--color-text);
}

.detail-score {
  background-color: var(--color-bg);
  border-radius: var(--radius-md);
  padding: 1rem;
  margin-bottom: 1.5rem;
}

.detail-score-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.detail-score-title {
  font-size: 0.875rem;
  font-weight: 600;
}

.detail-score-value {
  font-size: 1.25rem;
  font-weight: 700;
}

.detail-score-value.excellent {
  color: #166534;
}
.detail-score-value.good {
  color: #1e40af;
}
.detail-score-value.average {
  color: #92400e;
}
.detail-score-value.below-average {
  color: #991b1b;
}

.detail-score-bar {
  height: 6px;
  background: var(--color-border);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 0.75rem;
}

.detail-score-bar span {
  display: block;
  height: 100%;
  background: var(--color-primary);
}

.detail-score-bar span.excellent {
  background: #16a34a;
}
.detail-score-bar span.good {
  background: #2563eb;
}
.detail-score-bar span.average {
  background: #d97706;
}
.detail-score-bar span.below-average {
  background: #dc2626;
}

.detail-score-tier {
  font-size: 0.75rem;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm);
}

.detail-score-tier.excellent {
  background-color: #dcfce7;
  color: #166534;
}
.detail-score-tier.good {
  background-color: #dbeafe;
  color: #1e40af;
}
.detail-score-tier.average {
  background-color: #fef3c7;
  color: #92400e;
}
.detail-score-tier.below-average {
  background-color: #fee2e2;
  color: #991b1b;
}

.detail-score-breakdown {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.detail-score-item {
  display: flex;
  justify-content: space-between;
  font-size: 0.875rem;
}

.detail-score-item-label {
  color: var(--color-text-muted);
}

.detail-agent {
  background-color: var(--color-bg);
  border-radius: var(--radius-md);
  padding: 1rem;
  margin-bottom: 1.5rem;
}

.detail-agent-company {
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.detail-agent-name {
  font-size: 0.875rem;
  color: var(--color-text-muted);
}

.detail-agent-rating {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.875rem;
  color: var(--color-warning);
}

.detail-link {
  display: block;
  width: 100%;
  padding: 0.75rem;
  background-color: var(--color-primary);
  color: white;
  text-align: center;
  text-decoration: none;
  border-radius: var(--radius-sm);
  font-weight: 500;
  transition: background-color 0.15s ease;
}

.detail-link:hover {
  background-color: var(--color-primary-dark);
}

/* ============================================================================
   Responsive
   ============================================================================ */

@media (max-width: 640px) {
  .site-header {
    padding: 1.25rem 0;
    margin-bottom: 1rem;
  }

  .site-header h1 {
    font-size: 1.5rem;
  }

  .property-grid {
    grid-template-columns: 1fr;
  }

  .filter-bar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
  }

  .filter-search {
    flex: 1 1 auto;
    min-width: 0;
  }

  .filter-toggle {
    display: inline-flex;
    flex-shrink: 0;
  }

  .filter-panel {
    display: none;
    flex-basis: 100%;
  }

  .filter-bar.filter-open .filter-panel {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--color-border);
  }

  .filter-group {
    width: 100%;
  }

  .filter-select {
    width: 100%;
  }

  .filter-group-toggle {
    flex-direction: row;
    justify-content: flex-end;
  }

  .modal {
    padding: 0;
  }

  .modal-content {
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }

  .detail-score-breakdown {
    grid-template-columns: 1fr;
  }
}

/* ============================================================================
   Reduced Motion
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ============================================================================
   Statistics View
   ============================================================================ */

.view-tabs {
  display: flex;
  gap: 0.25rem;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid var(--color-border);
}

.view-tab {
  padding: 0.5rem 1rem;
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--color-text-muted);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  cursor: pointer;
}

.view-tab.active {
  color: var(--color-primary);
  border-bottom-color: var(--color-primary);
  font-weight: 600;
}

.view-tab:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.5rem;
  padding-bottom: 2rem;
}

.stats-card {
  background-color: var(--color-surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  padding: 1.25rem;
}

.stats-card.wide {
  grid-column: 1 / -1;
}

.stats-card h3 {
  font-size: 1rem;
  margin-bottom: 0.75rem;
}

.stats-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.stats-card-header h3 {
  margin-bottom: 0;
}

.stats-card-footnote,
.stats-progress-label {
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.stats-card-footnote {
  margin-top: 0.5rem;
}

.stats-no-data {
  color: var(--color-text-muted);
  font-size: 0.875rem;
  padding: 2rem 0;
  text-align: center;
}

.stats-progress {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.stats-progress-bar {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--color-border);
  overflow: hidden;
}

.stats-progress-bar span {
  display: block;
  height: 100%;
  background: var(--color-primary);
  border-radius: 999px;
  transition: width 0.2s ease;
  width: 0;
}

.stats-card svg {
  width: 100%;
  height: auto;
  display: block;
}

@media (max-width: 640px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
}
