/* =============================================================================
   HOMESTEAD EXEMPTION MAP - Custom CSS
   Adapted from CPAL shiny-app-template for map-focused layout
   ============================================================================= */

/* =============================================================================
   CSS CUSTOM PROPERTIES (CPAL THEME)
   ============================================================================= */

:root {
  /* CPAL Brand Colors */
  --cpal-midnight: #004855;
  --cpal-deep-teal: #006878;
  --cpal-coral: #E86A50;
  --cpal-sage: #5A8A6F;
  --cpal-slate: #5C6B73;
  --cpal-warm-gray: #9BA8AB;

  /* Sequential Midnight (for choropleth) */
  --cpal-midnight-1: #E8F4F6;
  --cpal-midnight-2: #C2E4EA;
  --cpal-midnight-3: #8BCDD8;
  --cpal-midnight-4: #54B5C5;
  --cpal-midnight-5: #2D9AAD;
  --cpal-midnight-6: #006878;
  --cpal-midnight-7: #004855;
  --cpal-midnight-8: #002D38;

  /* App colors */
  --app-primary: var(--cpal-deep-teal);
  --app-primary-dark: var(--cpal-midnight);
  --app-accent: var(--cpal-coral);

  /* Neutral colors */
  --app-bg-light: #f8f9fa;
  --app-bg-dark: #1a1a2e;
  --app-text-light: #1a1a2e;
  --app-text-dark: #f8f9fa;
  --app-text-muted: #6c757d;

  /* Sidebar - let theme control colors */
  --sidebar-active-bg: var(--cpal-coral);

  /* Header */
  --header-bg: var(--cpal-midnight);
  --header-text: #ffffff;

  /* Spacing */
  --header-height: 56px;
  --mobile-tabbar-height: 60px;
}

/* =============================================================================
   DARK MODE OVERRIDES
   ============================================================================= */

/* Dark mode - let theme handle base colors, only override app-specific components */

/* =============================================================================
   HEADER STYLING
   ============================================================================= */

/* =============================================================================
   MAPBOX POPUP STYLING
   ============================================================================= */

.mapboxgl-popup-content {
  background: #ffffff;
  color: #1a1a2e;
  border-radius: 8px;
  padding: 0.75rem 1rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  font-size: 0.825rem;
  line-height: 1.5;
  border-left: 3px solid var(--cpal-deep-teal);
}

.mapboxgl-popup-content strong {
  color: var(--cpal-deep-teal);
}

.mapboxgl-popup-close-button {
  font-size: 1.1rem;
  color: var(--cpal-slate);
  padding: 0.25rem 0.5rem;
}

.mapboxgl-popup-close-button:hover {
  color: var(--cpal-midnight);
  background: transparent;
}

.mapboxgl-popup-tip {
  border-top-color: #ffffff;
}

[data-bs-theme="dark"] .mapboxgl-popup-content {
  background: #1e2a30;
  color: #e8e8e8;
  border-left-color: #8BCDD8;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

[data-bs-theme="dark"] .mapboxgl-popup-content strong {
  color: #8BCDD8;
}

[data-bs-theme="dark"] .mapboxgl-popup-close-button {
  color: rgba(255, 255, 255, 0.6);
}

[data-bs-theme="dark"] .mapboxgl-popup-close-button:hover {
  color: #ffffff;
}

[data-bs-theme="dark"] .mapboxgl-popup-tip {
  border-top-color: #1e2a30;
}

/* Header title - matches shiny-showcase styling */
.header-title {
  font-weight: 400;
  text-transform: uppercase;
  font-size: 0.675rem;
  letter-spacing: 0.075px;
  width: 10rem;
  white-space: normal;
  word-wrap: break-word;
}

/* Header buttons - adapt to theme */
.header-btn {
  color: var(--cpal-deep-teal);
  border-color: var(--cpal-deep-teal);
  background: transparent;
}

.header-btn:hover {
  color: #ffffff;
  background-color: var(--cpal-deep-teal);
  border-color: var(--cpal-deep-teal);
}

[data-bs-theme="dark"] .header-btn {
  color: rgba(255, 255, 255, 0.8);
  border-color: rgba(255, 255, 255, 0.4);
}

[data-bs-theme="dark"] .header-btn:hover {
  color: #ffffff;
  background-color: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.6);
}

/* App-specific dark mode overrides - theme handles base colors */
[data-bs-theme="dark"] .stats-overlay {
  background: rgba(26, 26, 26, 0.95);
}

[data-bs-theme="dark"] .stats-overlay strong {
  color: #8BCDD8;
}

/* =============================================================================
   FULL-SCREEN MAP LAYOUT
   ============================================================================= */

.map-container {
  position: relative;
  width: 100%;
  height: calc(100vh - var(--header-height));
}

.mapboxgl-map,
.mapgl-container {
  width: 100% !important;
  height: 100% !important;
}

/* Ensure map canvas fills container */
.mapboxgl-canvas {
  width: 100% !important;
  height: 100% !important;
}

/* Stats overlay on map - positioned top-right */
.stats-overlay {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  padding: 1rem;
  max-width: 320px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  font-size: 0.85rem;
}

.stats-overlay p {
  margin-bottom: 0.5rem;
}

.stats-overlay p:last-child {
  margin-bottom: 0;
}

.stats-overlay strong {
  color: var(--cpal-deep-teal);
}

/* =============================================================================
   SIDEBAR STYLING
   Let cpal_dashboard_theme() handle base colors, only add minimal overrides
   ============================================================================= */

.sidebar .form-label,
.sidebar h4,
.sidebar h5 {
  font-size: 0.9rem;
  font-weight: 600;
}

.sidebar hr {
  margin: 0.75rem 0;
}

/* Ensure form elements fit within sidebar */
.sidebar .shiny-input-container {
  width: 100% !important;
  max-width: 100%;
}

.sidebar .form-select,
.sidebar .form-control {
  width: 100%;
  max-width: 100%;
}

/* Slider container width fix */
.sidebar .irs {
  max-width: 100%;
}

/* Focus state accent */
.sidebar .form-control:focus,
.sidebar .form-select:focus {
  border-color: var(--cpal-coral);
  box-shadow: 0 0 0 0.25rem rgba(232, 106, 80, 0.25);
}

/* Primary button in sidebar */
/* Primary button accent color */
.sidebar .btn-primary {
  background-color: var(--cpal-coral);
  border-color: var(--cpal-coral);
}

.sidebar .btn-primary:hover {
  background-color: #d55a42;
  border-color: #d55a42;
}

.sidebar .btn-primary:focus {
  box-shadow: 0 0 0 0.25rem rgba(232, 106, 80, 0.5);
}

/* Checkbox and radio accent color */
.sidebar .form-check-input:checked {
  background-color: var(--cpal-coral);
  border-color: var(--cpal-coral);
}

.sidebar .form-check-input:focus {
  box-shadow: 0 0 0 0.25rem rgba(232, 106, 80, 0.25);
  border-color: var(--cpal-coral);
}

/* Radio buttons inline */
.sidebar .shiny-input-radiogroup .shiny-options-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* Slider accent color */
.sidebar .irs--shiny .irs-bar {
  background: var(--cpal-coral);
  border-top: 1px solid var(--cpal-coral);
  border-bottom: 1px solid var(--cpal-coral);
}

.sidebar .irs--shiny .irs-handle {
  background: var(--cpal-coral);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.sidebar .irs--shiny .irs-handle:hover {
  background: #d55a42;
}

.sidebar .irs--shiny .irs-single,
.sidebar .irs--shiny .irs-from,
.sidebar .irs--shiny .irs-to {
  background: var(--cpal-coral);
  color: #ffffff;
}

/* Hide default mapgl legend (custom legend in stats panel) */
.mapboxgl-ctrl-legend,
.mapgl-legend {
  display: none !important;
}

/* Custom quantile legend in stats panel */
.legend-title-text {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--cpal-deep-teal);
  margin-bottom: 0.3rem;
}

[data-bs-theme="dark"] .legend-title-text {
  color: #8BCDD8;
}

.legend-items {
  display: flex;
  gap: 0.15rem;
}

.legend-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-width: 0;
}

.legend-swatch {
  display: block;
  width: 100%;
  height: 10px;
  border-radius: 2px;
}

.legend-item:first-child .legend-swatch {
  border-radius: 4px 2px 2px 4px;
}

.legend-item:last-child .legend-swatch {
  border-radius: 2px 4px 4px 2px;
}

.legend-label {
  font-size: 0.6rem;
  color: var(--app-text-muted);
  margin-top: 0.2rem;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

[data-bs-theme="dark"] .legend-label {
  color: rgba(255, 255, 255, 0.55);
}

/* NA legend row - separate from quantile bins */
.legend-na-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.4rem;
  padding-top: 0.3rem;
}

.legend-swatch-inline {
  display: inline-block;
  width: 14px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

.legend-label-inline {
  font-size: 0.6rem;
  color: var(--app-text-muted);
}

[data-bs-theme="dark"] .legend-label-inline {
  color: rgba(255, 255, 255, 0.55);
}

/* Source box in sidebar */
.source-box {
  background: rgba(0, 72, 85, 0.06);
  border: 1px solid rgba(0, 72, 85, 0.12);
  border-radius: 6px;
  padding: 0.6rem 0.75rem;
  margin-top: 1rem;
  font-size: 0.75rem;
  line-height: 1.5;
}

.source-box p {
  margin-bottom: 0.2rem;
}

.source-box p:last-child {
  margin-bottom: 0;
}

.source-box .source-label {
  font-weight: 600;
  color: var(--cpal-deep-teal);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0.35rem;
}

[data-bs-theme="dark"] .source-box {
  background: rgba(139, 205, 216, 0.08);
  border-color: rgba(139, 205, 216, 0.15);
}

[data-bs-theme="dark"] .source-box .source-label {
  color: #8BCDD8;
}

/* Filter status text */
.filter-status-text {
  font-size: 0.8rem;
  text-align: center;
}

/* Contact card in glossary modal */
.contact-card {
  background: rgba(0, 72, 85, 0.06);
  border: 1px solid rgba(0, 72, 85, 0.12);
  border-radius: 6px;
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
}

.contact-card .contact-label {
  font-weight: 600;
  color: var(--cpal-deep-teal);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0.35rem;
}

.contact-card p:last-child {
  margin-bottom: 0;
}

.contact-card a {
  color: var(--cpal-coral);
}

.contact-card a:hover {
  color: #d55a42;
}

[data-bs-theme="dark"] .contact-card {
  background: rgba(139, 205, 216, 0.08);
  border-color: rgba(139, 205, 216, 0.15);
}

[data-bs-theme="dark"] .contact-card .contact-label {
  color: #8BCDD8;
}

/* =============================================================================
   MODALS
   ============================================================================= */

.modal-content {
  border-radius: 12px;
  border: none;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

.modal-header {
  background: var(--cpal-midnight);
  color: #ffffff;
  border-radius: 12px 12px 0 0;
  border-bottom: none;
  padding: 1rem 1.5rem;
}

.modal-header .btn-close {
  filter: invert(1);
  opacity: 0.8;
}

.modal-header .btn-close:hover {
  opacity: 1;
}

.modal-title {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.modal-body {
  padding: 1.5rem;
  max-height: 70vh;
  overflow-y: auto;
}

.modal-body h4 {
  color: var(--cpal-deep-teal);
  margin-top: 1.25rem;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.modal-body h4:first-child {
  margin-top: 0;
}

.modal-body p {
  margin-bottom: 0.75rem;
  line-height: 1.6;
}

.modal-footer {
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  padding: 1rem 1.5rem;
}

/* FAQ accordion in modal */
.accordion-item {
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-bottom: 0.5rem;
  border-radius: 8px !important;
  overflow: hidden;
}

.accordion-button {
  font-weight: 500;
  padding: 1rem 1.25rem;
}

.accordion-button:not(.collapsed) {
  background-color: rgba(0, 104, 120, 0.1);
  color: var(--cpal-deep-teal);
}

.accordion-button:focus {
  box-shadow: 0 0 0 0.25rem rgba(0, 104, 120, 0.25);
  border-color: var(--cpal-deep-teal);
}

.accordion-button::after {
  background-size: 1rem;
}

.accordion-body {
  padding: 1rem 1.25rem;
  line-height: 1.6;
}

/* PDF embed in modal */
.pdf-embed {
  width: 100%;
  height: 70vh;
  border: none;
  border-radius: 8px;
  background: #f8f9fa;
}

/* =============================================================================
   MOBILE BOTTOM TAB BAR
   ============================================================================= */

.mobile-tab-bar {
  display: none;
}

@media (max-width: 768px) {
  /* Hide header icon buttons on mobile (duplicated in tab bar) */
  .header-btn {
    display: none !important;
  }

  /* Hide sidebar on mobile */
  .bslib-sidebar-layout > .sidebar,
  .bslib-sidebar-layout > .collapse-toggle {
    display: none !important;
  }

  .bslib-sidebar-layout {
    --_sidebar-width: 0px !important;
  }

  /* Force main content to fill full width */
  .bslib-sidebar-layout > .main {
    width: 100% !important;
    max-width: 100% !important;
    margin-left: 0 !important;
    flex: 1 1 100% !important;
  }

  .bslib-sidebar-layout {
    display: flex !important;
    flex-direction: column !important;
  }

  .mapboxgl-map,
  .mapgl-container,
  .mapboxgl-canvas {
    width: 100% !important;
  }

  /* Show mobile tab bar */
  .mobile-tab-bar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1050;
    background: var(--header-bg);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    padding: 0.35rem 0;
    justify-content: space-around;
    align-items: center;
    height: var(--mobile-tabbar-height);
  }

  .tab-bar-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.15rem;
    color: rgba(255, 255, 255, 0.6);
    font-size: 0.65rem;
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    transition: all 0.15s ease;
    text-decoration: none;
    cursor: pointer;
    background: none;
    border: none;
  }

  .tab-bar-item .bi,
  .tab-bar-item i,
  .tab-bar-item .fa,
  .tab-bar-item .fas,
  .tab-bar-item .far {
    font-size: 1.2rem;
  }

  .tab-bar-item.active {
    color: var(--cpal-coral);
  }

  .tab-bar-item:hover,
  .tab-bar-item:focus {
    color: rgba(255, 255, 255, 0.9);
    background: rgba(255, 255, 255, 0.1);
  }

  .tab-bar-label {
    font-size: 0.6rem;
    letter-spacing: 0.3px;
    text-transform: uppercase;
  }

  /* Map container fills available space between header and tab bar */
  .map-container {
    height: calc(100vh - var(--header-height) - var(--mobile-tabbar-height));
    overflow: hidden;
    display: flex;
    flex-direction: column;
    width: 100% !important;
  }

  /* Map fills remaining space after stats panel */
  .map-container .mapgl-container,
  .map-container .mapboxgl-map {
    flex: 1;
    min-height: 0;
  }

  /* Override inline height on mobile */
  .map-container #map {
    height: 100% !important;
    flex: 1;
    min-height: 0;
  }

  /* Stats panel stacked above map on mobile */
  .stats-overlay {
    position: relative;
    top: auto;
    right: auto;
    left: auto;
    bottom: auto;
    max-width: none;
    border-radius: 0;
    box-shadow: none;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    padding: 0.75rem 1rem;
    font-size: 0.8rem;
    max-height: 35vh;
    overflow-y: auto;
  }

  [data-bs-theme="dark"] .stats-overlay {
    border-bottom-color: rgba(255, 255, 255, 0.1);
  }

  /* Mobile modal adjustments */
  .modal-dialog {
    margin: 0.5rem;
  }

  .modal-body {
    max-height: 60vh;
  }

  .pdf-embed {
    height: 50vh;
  }
}

/* =============================================================================
   MOBILE FILTER SHEET
   ============================================================================= */

.filter-sheet {
  display: none;
}

@media (max-width: 768px) {
  .filter-sheet {
    display: block;
    position: fixed;
    bottom: var(--mobile-tabbar-height);
    left: 0;
    right: 0;
    z-index: 1040;
    background: var(--sidebar-bg);
    border-radius: 16px 16px 0 0;
    padding: 1rem;
    transform: translateY(100%);
    transition: transform 0.3s ease;
    max-height: 70vh;
    overflow-y: auto;
  }

  .filter-sheet.open {
    transform: translateY(0);
  }

  .filter-sheet-handle {
    width: 40px;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    margin: 0 auto 1rem;
  }

  .filter-sheet-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  }

  .filter-sheet-title {
    color: #ffffff;
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
  }

  .filter-sheet-close {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.7);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    line-height: 1;
  }

  .filter-sheet-close:hover {
    color: #ffffff;
  }

  /* Filter sheet inherits sidebar form styling */
  .filter-sheet .form-label,
  .filter-sheet h5 {
    color: rgba(255, 255, 255, 0.9) !important;
    font-size: 0.85rem;
    font-weight: 600;
  }

  .filter-sheet .form-check-label {
    color: rgba(255, 255, 255, 0.85) !important;
  }

  .filter-sheet .form-control,
  .filter-sheet .form-select {
    background-color: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
    color: #ffffff;
  }

  .filter-sheet hr {
    border-color: rgba(255, 255, 255, 0.2);
    margin: 0.75rem 0;
  }

  /* Filter sheet backdrop */
  .filter-sheet-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: var(--mobile-tabbar-height);
    background: rgba(0, 0, 0, 0.5);
    z-index: 1039;
  }

  .filter-sheet-backdrop.show {
    display: block;
  }
}

/* =============================================================================
   NAVBAR ADJUSTMENTS
   ============================================================================= */

.navbar {
  padding: 0.5rem 1rem;
  min-height: var(--header-height);
}

.navbar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.navbar-brand img {
  height: 30px;
}

.navbar-brand span {
  font-weight: 600;
  white-space: nowrap;
}

.navbar .nav-link {
  color: rgba(255, 255, 255, 0.8) !important;
  padding: 0.5rem 0.75rem !important;
  transition: color 0.15s ease;
}

.navbar .nav-link:hover {
  color: #ffffff !important;
}

/* Icon buttons in navbar */
.navbar .btn-icon,
.btn-icon {
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.8);
  padding: 0.5rem;
  border-radius: 6px;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
}

.navbar .btn-icon:hover,
.btn-icon:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #ffffff;
}

.navbar .btn-icon:focus,
.btn-icon:focus {
  outline: none;
  box-shadow: 0 0 0 0.25rem rgba(255, 255, 255, 0.25);
}

/* Dark mode toggle button */
.navbar .bslib-input-dark-mode {
  margin-left: 0.25rem;
}

/* Responsive navbar */
@media (max-width: 576px) {
  .navbar-brand span {
    font-size: 0.9rem;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .navbar-brand img {
    height: 24px;
  }

  .navbar .btn-icon {
    width: 34px;
    height: 34px;
    padding: 0.35rem;
  }
}

/* =============================================================================
   UTILITY CLASSES
   ============================================================================= */

/* Text colors */
.text-cpal-midnight { color: var(--cpal-midnight); }
.text-cpal-teal { color: var(--cpal-deep-teal); }
.text-cpal-coral { color: var(--cpal-coral); }
.text-cpal-sage { color: var(--cpal-sage); }
.text-cpal-slate { color: var(--cpal-slate); }
.text-cpal-warm-gray { color: var(--cpal-warm-gray); }

/* Background colors */
.bg-cpal-midnight { background-color: var(--cpal-midnight); }
.bg-cpal-teal { background-color: var(--cpal-deep-teal); }
.bg-cpal-coral { background-color: var(--cpal-coral); }
.bg-cpal-sage { background-color: var(--cpal-sage); }
.bg-cpal-slate { background-color: var(--cpal-slate); }

/* Border colors */
.border-cpal-midnight { border-color: var(--cpal-midnight) !important; }
.border-cpal-teal { border-color: var(--cpal-deep-teal) !important; }
.border-cpal-coral { border-color: var(--cpal-coral) !important; }

/* Accent highlights */
.accent-underline {
  border-bottom: 3px solid var(--cpal-coral);
  padding-bottom: 0.25rem;
}

.accent-left-border {
  border-left: 4px solid var(--cpal-coral);
  padding-left: 1rem;
}

/* Visually hidden but accessible */
.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;
}

/* Focus visible for accessibility */
:focus-visible {
  outline: 2px solid var(--cpal-coral);
  outline-offset: 2px;
}

/* Smooth scrolling */
html {
  scroll-behavior: smooth;
}

/* Prevent text selection on interactive elements */
.tab-bar-item,
.btn-icon {
  user-select: none;
  -webkit-user-select: none;
}

/* Loading spinner overlay */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1001;
}

[data-bs-theme="dark"] .loading-overlay {
  background: rgba(26, 26, 26, 0.8);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(0, 104, 120, 0.2);
  border-top-color: var(--cpal-deep-teal);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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