/* General styling and layout */
:root {
  --primary: #007bff; /* A nice blue for primary actions */
  --secondary: #6c757d; /* Grey for secondary actions */
  --success: #28a745; /* Green for success */
  --danger: #dc3545; /* Red for errors/danger */
  --info: #17a2b8; /* Light blue for info */
  --light: #f8f9fa; /* Light background */
  --dark: #343a40; /* Dark text/background */
  --text-color: #333;
  --bg-color: #f4f7f6;
  --card-bg: #fff;
  --border-color: #e0e0e0;
  --header-bg: #212529; /* Darker header */
  --footer-bg: #212529; /* Darker footer */
  --header-text: #fff;
  --line: #eee; /* Light line color for borders */
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  line-height: 1.6;
  color: var(--text-color);
  background-color: var(--bg-color);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.app-header {
  background-color: var(--header-bg);
  color: var(--header-text);
  padding: 1rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  flex-shrink: 0;
}

.app-header h1 {
  font-size: 1.5rem;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#status {
  font-size: 0.9rem;
  opacity: 0.8;
  transition: opacity 0.3s ease;
  min-width: 100px;
  text-align: right;
  color: var(--header-text); /* Ensure status text is visible on dark header */
}

#status.loading {
  opacity: 1;
  font-weight: bold;
  color: var(--info);
}

.app-main {
  flex: 1;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

/* Home page specific styles */
#home {
  width: 100%;
  padding-bottom: 2rem;
}

.home-title {
  text-align: center;
  margin-bottom: 2rem;
  color: var(--dark);
  font-size: 2rem;
}

#home-cards.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.5rem;
  width: 100%;
}

.card {
  background-color: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  padding: 1.2rem;
  text-decoration: none;
  color: var(--text-color);
  transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.card .thumb {
  font-size: 2.5rem;
  margin-right: 1rem;
  line-height: 1;
}

.card .stack {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card .badge {
  background-color: var(--primary);
  color: white;
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: bold;
  text-transform: uppercase;
  align-self: flex-start;
  margin-bottom: 0.4rem;
}

.card .title {
  font-size: 1.2rem;
  font-weight: 600;
}

.feat-progress {
  color: var(--secondary);
  font-size: 0.9rem;
}

/* Map page specific styles */
body[data-view="viewer"] .app-main {
  padding: 0;
  flex-direction: column; /* Always column for app-main: viewer-bar on top, map-and-panel-wrapper below */
  max-width: none;
  width: 100%;
  flex-grow: 1;
}

.viewer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background-color: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
  z-index: 1000;
  position: relative;
  flex-shrink: 0;
  flex-wrap: wrap; /* Allow items to wrap on smaller screens */
  gap: 0.8rem; /* Space between items */
}

#viewer-title {
  font-size: 1.2rem;
  font-weight: 600;
  flex-grow: 1;
  min-width: 0; /* Allow title to shrink */
}

.viewer-bar button, .viewer-bar summary {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border-color);
  border-radius: 5px;
  background-color: var(--card-bg);
  cursor: pointer;
  font-size: 0.9rem;
  margin-left: 0.8rem;
  transition: background-color 0.2s ease, border-color 0.2s ease;
  color: var(--text-color);
  flex-shrink: 0; /* Prevent buttons from shrinking */
}

.viewer-bar button:hover, .viewer-bar summary:hover {
  background-color: var(--light);
  border-color: var(--primary);
}

.map-and-panel-wrapper { /* New container for map and panel */
  display: flex;
  flex-direction: column; /* Default to column for map, as panel is now a modal */
  flex-grow: 1; /* Takes remaining vertical space */
  position: relative;
  width: 100%;
}

#map {
  flex-grow: 1;
  height: 100%; /* Take full height of wrapper */
  width: 100%;
  background-color: #ccc;
  z-index: 1;
}

/* NEW: Modal Backdrop Styles */
#modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6); /* Darker backdrop */
  z-index: 1999; /* Below modal, above map */
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

#modal-backdrop.visible {
  opacity: 1;
  visibility: visible;
}

/* NEW: Modal Panel Styles */
#panel {
  position: fixed;
  top: 50%;
  left: 1.5rem; /* Aligned to the left with a small margin */
  transform: translate(0, -50%) scale(0.95); /* Remove X-axis translation for left alignment */
  opacity: 0;
  visibility: hidden;
  background-color: var(--card-bg);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 2000; /* Above backdrop */
  max-width: min(600px, 90vw - 3rem); /* Adjust max-width to account for left margin and potential right margin if needed */
  max-height: 90vh; /* Max height relative to viewport */
  width: min(600px, 90vw - 3rem); /* Desired width, but not larger than (90vw - margins) */
  display: flex; /* For internal layout */
  flex-direction: column;
  padding: 1rem;
  overflow: hidden; /* Hide scrollbars until content area */
  transition: transform 0.3s ease-out, opacity 0.3s ease-out, visibility 0.3s ease-out;
  pointer-events: none; /* Block interaction when hidden */
}

#panel.open {
  transform: translate(0, -50%) scale(1); /* Remove X-axis translation for left alignment */
  opacity: 1;
  visibility: visible;
  pointer-events: all; /* Enable interaction when open */
}

#panel-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: none;
  border: none;
  font-size: 2rem;
  cursor: pointer;
  color: var(--secondary);
  line-height: 1;
  padding: 0;
  z-index: 2010;
  display: block; /* Always show close button for modal */
}

#panel-close:hover {
  color: var(--dark);
}

#panel-content {
  flex-grow: 1; /* Allows content to take available height */
  overflow-y: auto; /* Scrollable content area */
  padding-top: 2.5rem; /* Space for close button */
}

#feature-meta {
  font-size: 0.9rem;
  color: var(--secondary);
  margin-bottom: 1.5rem;
  border-bottom: 1px solid var(--line);
  padding-bottom: 1rem;
}

#props {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.prop {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  padding: 0.6rem 0;
  border-bottom: 1px dotted var(--line);
}

.prop:last-child {
  border-bottom: none;
}

.prop b {
  font-weight: 600;
  margin-right: 0.5rem;
  min-width: 100px;
  color: var(--dark);
}

.prop span {
  flex: 1;
  word-break: break-word;
}

/* NEW: Styles for single feature download section in the panel */
.prop-download-section {
  padding: 1rem 0;
  border-top: 1px solid var(--line);
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.8rem; /* Space between title and links, and between links */
}

.prop-download-section b {
  font-weight: 600;
  color: var(--dark);
}

.download-links {
  display: flex;
  flex-wrap: wrap; /* Allow links to wrap */
  gap: 0.6rem; /* Space between download links */
}

.download-links .button { /* General button style for download links */
  display: inline-block;
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--primary);
  border-radius: 5px;
  background-color: var(--primary);
  color: white;
  text-decoration: none;
  font-size: 0.85rem;
  transition: background-color 0.2s ease, border-color 0.2s ease;
  white-space: nowrap; /* Prevent breaking in the middle of text */
}

.download-links .button:hover {
  background-color: #0056b3;
  border-color: #0056b3;
}

.download-links .button.small-button {
  padding: 0.3rem 0.6rem; /* Slightly smaller for detail panel */
  font-size: 0.8rem;
}

/* NEW: Styles for overlapping features list in panel */
#overlapping-features-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1.5rem;
}

.overlapping-feature-item {
  background-color: var(--light);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  align-items: flex-start;
}

.overlapping-feature-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--dark);
  margin: 0;
  word-break: break-all; /* Ensure long codes wrap */
}

.panel-intro-text {
  font-size: 0.95rem;
  color: var(--secondary);
  margin-top: 0.5rem;
}

/* Download Dropdown */
.downloads {
  position: relative;
  display: inline-block;
  z-index: 100; /* Ensure dropdown is above other elements in viewer-bar */
  flex-shrink: 0;
}

.downloads summary {
  display: flex;
  align-items: center;
  user-select: none;
  list-style: none;
}

.downloads summary::-webkit-details-marker {
  display: none;
}

.downloads summary::after {
  content: '▼';
  font-size: 0.7em;
  margin-left: 0.5em;
  transition: transform 0.2s;
}

.downloads[open] summary::after {
  transform: rotate(180deg);
}

.downloads .menu {
  position: absolute;
  right: 0;
  margin-top: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  min-width: 180px;
  overflow: hidden;
  z-index: 1000; /* Ensure it's on top of map */
  display: flex;
  flex-direction: column;
}

.downloads .menu a {
  display: block;
  padding: 0.8rem 1rem;
  text-decoration: none;
  color: var(--text-color);
  transition: background-color 0.2s ease;
  white-space: nowrap;
}

.downloads .menu a:hover {
  background-color: var(--light);
}

/* Search Container */
.search-container {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto; /* Push to the right on larger screens */
  flex-shrink: 0;
}

#search-toggle {
  background-color: transparent;
  border: none;
  padding: 0.5rem;
  cursor: pointer;
  color: var(--text-color);
}

#search-toggle img {
  width: 24px;
  height: 24px;
  display: block;
}

#search-bar {
  display: flex;
  gap: 0.5rem;
  transition: max-width 0.3s ease-in-out, opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
  overflow: hidden;
  max-width: 0; /* Hidden by default */
  opacity: 0;
  visibility: hidden;
  white-space: nowrap;
}

#search-bar.visible {
  max-width: 300px; /* Adjust as needed */
  opacity: 1;
  visibility: visible;
}

#cod-imovel-input {
  padding: 0.5rem 0.8rem;
  border: 1px solid var(--border-color);
  border-radius: 5px;
  font-size: 0.9rem;
  flex-grow: 1;
  min-width: 120px; /* Ensure it's not too small when visible */
  color: var(--text-color);
  background-color: var(--card-bg);
}

#search-button {
  padding: 0.5rem 1rem;
  border: 1px solid var(--primary);
  border-radius: 5px;
  background-color: var(--primary);
  color: white;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background-color 0.2s ease, border-color 0.2s ease;
  flex-shrink: 0;
}

#search-button:hover {
  background-color: #0056b3;
  border-color: #0056b3;
}

/* Footer */
.footer {
  background-color: var(--footer-bg);
  color: var(--header-text);
  text-align: center;
  padding: 1rem 1.5rem;
  font-size: 0.85rem;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  flex-shrink: 0;
}

/* Media Queries for Responsiveness */
@media (min-width: 768px) {
  .app-header h1 {
    font-size: 1.8rem;
  }

  .home-title {
    font-size: 2.5rem;
  }

  /* No longer applies flex-direction: row to map-and-panel-wrapper as panel is a modal */

  .viewer-bar button#panel-open {
    /* No longer need to force display: block here, default button styling is fine */
  }

  .downloads {
    margin-left: 1.5rem; /* More space on desktop */
  }

  .search-container {
    margin-left: 1.5rem; /* Space between downloads and search on desktop */
    width: auto;
    order: unset; /* Reset order for desktop */
    margin-top: 0;
  }
}

@media (max-width: 767px) {
  .search-container {
    width: 100%;
    order: 3; /* Push to new line on small screens */
    margin-top: 0.5rem;
    margin-left: 0; /* Override margin-left auto */
  }
  #search-bar.visible {
    max-width: 100%;
    flex-grow: 1;
  }
  #cod-imovel-input {
    flex-grow: 1;
  }
  .viewer-bar {
    justify-content: flex-start; /* Align items to start when wrapping */
  }
  .viewer-bar #viewer-title {
    flex-basis: 100%; /* Take full width for title if wrapping */
    margin-bottom: 0.5rem;
  }
  .viewer-bar button, .viewer-bar details {
    margin-left: 0; /* No margin-left needed with gap */
  }
}