/* Responsive design improvements */
@media (max-width: 768px) {
    /* Layer control panel adjustments for mobile */
    #layer-panel {
        top: 10px;
        right: 10px;
        left: 70px; /* Leave space for zoom controls */
        max-width: none;
        padding: 12px;
    }

    /* Ensure touch targets are at least 44px on mobile */
    #layer-panel input[type="checkbox"] {
        width: 18px;
        height: 18px;
        min-width: 18px;
        min-height: 18px;
    }

    #layer-panel label {
        padding: 8px 0;
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    /* No data panel mobile adjustments */
    #no-data .max-w-md {
        max-width: 90vw;
        margin: 0 20px;
    }

    /* Mobile legend accordion styling */
    #legend-accordion-toggle {
        min-height: 44px;
        touch-action: manipulation;
    }

    #legend-accordion-content {
        max-height: 250px;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    /* Smooth scrollbar for legend on mobile */
    #legend-accordion-content::-webkit-scrollbar {
        width: 4px;
    }

    #legend-accordion-content::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 2px;
    }

    #legend-accordion-content::-webkit-scrollbar-thumb {
        background: #cbd5e1;
        border-radius: 2px;
    }

    #legend-accordion-content::-webkit-scrollbar-thumb:hover {
        background: #94a3b8;
    }
}

/* Desktop improvements - better positioning and sizing */
@media (min-width: 769px) {
    #layer-panel {
        top: 20px;
        right: 20px;
        max-width: 260px;
        padding: 8px;
    }
    
    #layer-panel input[type="checkbox"] {
        width: 16px;
        height: 16px;
    }
    
    #layer-panel label {
        padding: 4px 0;
        min-height: auto;
    }
}

/* Custom tooltip styling */
.custom-tooltip {
    background: rgba(0, 0, 0, 0.9) !important;
    color: white !important;
    border: none !important;
    border-radius: 6px !important;
    padding: 8px 12px !important;
    font-size: 13px !important;
    line-height: 1.4 !important;
    max-width: 250px !important;
    word-wrap: break-word !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3) !important;
}

.custom-tooltip::before {
    border-top-color: rgba(0, 0, 0, 0.9) !important;
}

/* Custom popup styling */
.custom-popup .leaflet-popup-content-wrapper {
    background: white;
    border-radius: 12px;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
    padding: 0;
    overflow: hidden;
    border: none;
}

.custom-popup .leaflet-popup-content {
    margin: 0;
    line-height: 1.4;
    font-size: 14px;
    width: auto !important;
}

.custom-popup .leaflet-popup-tip {
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Compact popup design */
.popup-container {
    padding: 16px;
    min-width: 200px;
    max-width: 260px;
}

/* Outlier badge styling */
.popup-outlier-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #fef3c7;
    color: #92400e;
    font-size: 11px;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 12px;
    margin-bottom: 10px;
    border: 1px solid #fde68a;
}

.popup-outlier-icon {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

.popup-header {
    font-size: 16px;
    font-weight: 700;
    color: #1f2937;
    margin-bottom: 4px;
    line-height: 1.3;
}

.popup-location {
    font-size: 13px;
    color: #6b7280;
    margin-bottom: 12px;
}

.popup-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    gap: 12px;
}

.popup-households {
    font-size: 13px;
    color: #374151;
}

.popup-households strong {
    color: #2563eb;
    font-weight: 600;
}

.popup-status {
    font-size: 11px;
    font-weight: 500;
    padding: 4px 8px;
    border-radius: 12px;
    text-transform: capitalize;
    white-space: nowrap;
}

/* Status color classes - enhanced and more robust system */
.popup-status {
    font-size: 11px;
    font-weight: 500;
    padding: 4px 8px;
    border-radius: 12px;
    text-transform: capitalize;
    white-space: nowrap;
}

/* Green status */
.popup-status-green {
    background: #dcfce7;
    color: #166534;
}

/* Blue status */
.popup-status-blue {
    background: #dbeafe;
    color: #1d4ed8;
}

/* Amber status */
.popup-status-amber {
    background: #fef3c7;
    color: #92400e;
}

/* Orange status */
.popup-status-orange {
    background: #fed7aa;
    color: #c2410c;
}

/* Red status */
.popup-status-red {
    background: #fee2e2;
    color: #b91c1c;
}

/* Purple status */
.popup-status-purple {
    background: #f3e8ff;
    color: #7e22ce;
}

/* Teal status */
.popup-status-teal {
    background: #ccfbf1;
    color: #0f766e;
}

/* Pink status */
.popup-status-pink {
    background: #fce7f3;
    color: #be185d;
}

/* Indigo status */
.popup-status-indigo {
    background: #e0e7ff;
    color: #4338ca;
}

/* Gray status */
.popup-status-gray {
    background: #f3f4f6;
    color: #4b5563;
}

/* Original status mappings for backward compatibility */
.popup-status-homesPassed {
    background: #dcfce7;
    color: #166534;
}

.popup-status-connected {
    background: #dbeafe;
    color: #1d4ed8;
}

.popup-status-planned {
    background: #fef3c7;
    color: #92400e;
}

.popup-status-inProgress {
    background: #fed7aa;
    color: #c2410c;
}

.popup-button {
    display: block;
    width: 100%;
    background: #2563eb !important;
    color: white !important;
    text-decoration: none;
    padding: 10px 16px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    text-align: center;
    transition: background-color 0.2s ease;
    border: none;
    cursor: pointer;
}

.popup-button:hover {
    background: #1d4ed8 !important;
    color: white !important;
    text-decoration: none;
}

/* Custom properties section styling */
.popup-custom-section {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #e5e7eb;
}

.popup-custom-separator {
    font-size: 11px;
    color: #9ca3af;
    text-align: center;
    margin-bottom: 10px;
    font-weight: 500;
    letter-spacing: 0.5px;
}

.popup-custom-item {
    margin-bottom: 6px;
    font-size: 12px;
    line-height: 1.4;
    word-break: break-word;
    overflow-wrap: break-word;
}

.popup-custom-key {
    color: #6b7280;
    font-weight: 500;
    display: block;
    margin-bottom: 2px;
}

.popup-custom-value {
    color: #374151;
    font-weight: 600;
    display: block;
    padding-left: 8px;
    border-left: 3px solid #e5e7eb;
}

/* Handle very long property names gracefully */
.popup-custom-key {
    hyphens: auto;
    -webkit-hyphens: auto;
    -ms-hyphens: auto;
}

/* Responsive adjustments for custom properties */
@media (max-width: 768px) {
    .popup-custom-item {
        font-size: 11px;
    }
    
    .popup-custom-separator {
        font-size: 10px;
        margin-bottom: 8px;
    }
    
    .popup-custom-value {
        padding-left: 6px;
        border-left-width: 2px;
    }
}

/* Mobile popup adjustments */
@media (max-width: 768px) {
    .custom-popup .leaflet-popup-content-wrapper {
        max-width: calc(100vw - 40px) !important;
        min-width: 250px;
    }
    
    .custom-popup .leaflet-popup-content {
        margin: 12px;
        font-size: 13px;
    }
    
    /* Ensure Google Maps button is touch-friendly */
    .custom-popup a {
        min-height: 44px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        text-decoration: none !important;
        font-size: 14px !important;
    }
}

/* Leaflet control adjustments for mobile only */
@media (max-width: 768px) {
    .leaflet-control-zoom a {
        width: 44px !important;
        height: 44px !important;
        line-height: 44px !important;
        font-size: 18px !important;
    }
}

/* Loading spinner improvements */
#loading {
    backdrop-filter: blur(2px);
}

/* Smooth transitions */
.leaflet-popup-content-wrapper,
.custom-tooltip,
#layer-panel,
#no-data {
    transition: all 0.2s ease-in-out;
}

/* Legend accordion animations */
#legend-accordion-arrow {
    transition: transform 0.3s ease;
}

#legend-accordion-content {
    transition: all 0.3s ease-in-out;
}

#legend-accordion-toggle {
    transition: background-color 0.2s ease;
}

#legend-accordion-toggle:active {
    transform: scale(0.98);
}

/* Focus styles for accessibility */
#layer-panel input[type="checkbox"]:focus {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

#legend-accordion-toggle:focus {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .custom-tooltip {
        background: black !important;
        border: 2px solid white !important;
    }
    
    #layer-panel {
        border: 2px solid #000;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    .leaflet-popup-content-wrapper,
    .custom-tooltip,
    #layer-panel,
    #no-data,
    .animate-spin {
        transition: none !important;
        animation: none !important;
    }
}

/* Print styles */
@media print {
    #layer-panel,
    #no-data,
    #loading {
        display: none !important;
    }
    
    #map {
        height: 80vh !important;
        width: 100% !important;
    }
}


/* Ensure proper z-index stacking */
.leaflet-popup-pane {
    z-index: 700;
}

.leaflet-tooltip-pane {
    z-index: 650;
}

#layer-panel {
    z-index: 1000;
}

#loading,
#no-data {
    z-index: 2000;
}

/* Center view button styling */
#center-view-btn {
    transition: all 0.2s ease-in-out;
}

#center-view-btn:hover {
    background-color: #e5e7eb !important;
}

#center-view-btn svg {
    transition: transform 0.3s ease;
}

#center-view-btn.centering svg {
    transform: rotate(360deg);
}

/* Information icon and modal styles */
#info-icon {
    transition: all 0.2s ease-in-out;
    z-index: 1001 !important;
    position: fixed !important;
    bottom: 16px !important;
    left: 16px !important;
}

#info-icon:hover {
    background-color: rgba(255, 255, 255, 1) !important;
    transform: scale(1.1);
}

/* Location button styles */
#location-btn {
    transition: all 0.2s ease-in-out;
    z-index: 1001 !important;
    bottom: calc(1rem + 15px) !important;
}

#location-btn:hover {
    background-color: rgba(255, 255, 255, 1) !important;
    transform: scale(1.1);
}

#location-btn.location-active {
    background-color: #3b82f6 !important;
}

#location-btn.location-active svg {
    color: white !important;
}

/* Custom location marker styles */
.custom-location-marker {
    background: transparent;
    border: none;
}

.location-marker-inner {
    width: 20px;
    height: 20px;
    background-color: #3b82f6;
    border: 3px solid white;
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
    animation: pulse-location 2s infinite;
}

@keyframes pulse-location {
    0% {
        box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.7);
    }
    50% {
        box-shadow: 0 0 0 10px rgba(59, 130, 246, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(59, 130, 246, 0);
    }
}

/* Modal styles */
#info-modal,
#gps-guide-modal {
    backdrop-filter: blur(4px);
    animation: fadeIn 0.2s ease-in-out;
    z-index: 2500 !important; /* Higher than layer panel */
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

#info-modal > div,
#gps-guide-modal > div {
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Status badges */
#status-badge {
    display: flex;
    justify-content: flex-start;
}

/* Metadata cards hover effect */
#metadata-content .bg-gray-50 {
    transition: all 0.2s ease;
}

#metadata-content .bg-gray-50:hover {
    background-color: #f9fafb;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* Mobile optimizations */
@media (max-width: 768px) {
    #info-icon {
        bottom: 10px !important;
        left: 10px !important;
    }

    #location-btn {
        bottom: calc(10px + 15px) !important;
        right: 10px !important;
    }

    #info-modal > div {
        max-height: 95vh !important;
        margin: 0.5rem;
    }

    #info-modal .p-6 {
        padding: 1rem;
    }

    #info-modal .grid-cols-2 {
        grid-template-columns: 1fr;
    }

    #status-badge > div {
        font-size: 0.875rem;
        padding: 0.5rem 0.75rem;
    }
}

/* Accessibility improvements */
#close-modal:focus,
#close-gps-guide:focus,
#info-icon:focus,
#location-btn:focus,
#start-gps-btn:focus {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

/* GPS Guide Modal specific styles */
#gps-guide-modal .bg-gradient-to-r {
    background: linear-gradient(to right, #eff6ff, #dbeafe);
}

#start-gps-btn:active {
    transform: scale(0.98);
}

#platform-tips {
    border-left: 3px solid #3b82f6;
    padding-left: 1rem;
}

/* Loading spinner in modal */
#metadata-loading .animate-spin {
    animation: spin 1s linear infinite;
}

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

/* Deletion warning animation */
#deletion-warning {
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from {
        opacity: 0;
        max-height: 0;
        margin-bottom: 0;
    }
    to {
        opacity: 1;
        max-height: 200px;
        margin-bottom: 1.5rem;
    }
}

/* Smooth transitions */
#metadata-content,
#metadata-loading {
    transition: opacity 0.2s ease;
}

/* Print styles for modal */
@media print {
    #info-modal,
    #gps-guide-modal {
        display: none !important;
    }
}

/* GPS Guide Modal - Scrollable content (all platforms) */
#gps-guide-modal .overflow-y-auto {
    -webkit-overflow-scrolling: touch; /* iOS smooth scrolling */
    overscroll-behavior: contain; /* Prevent background scroll */
}

/* GPS Guide Modal - Size and spacing with safe area (all platforms) */
#gps-guide-modal {
    padding-top: calc(env(safe-area-inset-top) + 10px);
    padding-bottom: calc(env(safe-area-inset-bottom) + 10px);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

#gps-guide-modal > div {
    max-height: calc(90vh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 20px);
}

/* Ensure proper content height (all platforms) */
#gps-guide-modal .overflow-y-auto {
    max-height: calc(90vh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 180px); /* Subtract safe areas, header (~80px) and footer (~80px) */
}

/* Mobile-specific adjustments */
@media (max-width: 768px) {
    #gps-guide-modal > div {
        margin: 1rem;
    }

    /* Adjust content height for mobile header/footer */
    #gps-guide-modal .overflow-y-auto {
        max-height: calc(90vh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 160px); /* Smaller header/footer on mobile */
    }
}

/* Desktop-specific adjustments */
@media (min-width: 769px) {
    #gps-guide-modal > div {
        margin: 2rem;
    }
}
