/* ========================================
   CSS Variables and Reset
   ======================================== */
:root {
    /* Colors */
    --primary-color: #e63946;
    --secondary-color: #1d3557;
    --accent-color: #457b9d;
    --background-color: #f1faee;
    --text-color: #1d3557;
    --border-color: #a8dadc;
    
    /* Office Level Colors */
    --federal-color: #e63946;
    --state-color: #f77f00;
    --county-color: #06a77d;
    --city-color: #457b9d;
    --town-color: #9b59b6;
    
    /* Spacing */
    --spacing-xs: 0.25rem;
    --spacing-sm: 0.5rem;
    --spacing-md: 1rem;
    --spacing-lg: 1.5rem;
    --spacing-xl: 2rem;
    
    /* Shadows */
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.1);
    --shadow-lg: 0 10px 15px rgba(0,0,0,0.1);
    
    /* Transitions */
    --transition-fast: 0.2s ease;
    --transition-normal: 0.3s ease;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
        'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
        sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: var(--text-color);
    background-color: var(--background-color);
    overflow: hidden;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

/* ========================================
   Header
   ======================================== */
header {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    padding: var(--spacing-lg);
    padding-top: max(var(--spacing-lg), env(safe-area-inset-top));
    padding-left: max(var(--spacing-lg), env(safe-area-inset-left));
    padding-right: max(var(--spacing-lg), env(safe-area-inset-right));
    text-align: center;
    box-shadow: var(--shadow-md);
    z-index: 1000;
    position: relative;
}

header h1 {
    font-size: 2rem;
    margin-bottom: var(--spacing-sm);
    font-weight: 700;
}

header p {
    font-size: 1rem;
    opacity: 0.9;
}

/* ========================================
   Main Layout
   ======================================== */
main {
    display: flex;
    height: calc(100vh - 120px);
    position: relative;
}

/* ========================================
   Filter Panel
   ======================================== */
.filter-panel {
    width: 320px;
    background: white;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    z-index: 900;
    transition: transform var(--transition-normal);
}

.filter-panel.collapsed {
    transform: translateX(-100%);
}

.filter-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--spacing-lg);
    background: var(--secondary-color);
    color: white;
}

.filter-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.toggle-btn {
    background: transparent;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: var(--spacing-sm);
    transition: transform var(--transition-fast);
}

.toggle-btn:hover {
    transform: scale(1.1);
}

.toggle-btn:focus {
    outline: 2px solid white;
    outline-offset: 2px;
}

.filter-content {
    overflow-y: auto;
    padding: var(--spacing-lg);
    flex: 1;
}

.filter-group {
    margin-bottom: var(--spacing-lg);
}

.filter-label {
    display: block;
    font-weight: 600;
    margin-bottom: var(--spacing-sm);
    color: var(--text-color);
    font-size: 0.95rem;
}

/* Form Elements */
.search-input,
.filter-select,
.year-input {
    width: 100%;
    padding: var(--spacing-sm) var(--spacing-md);
    border: 2px solid var(--border-color);
    border-radius: 4px;
    font-size: 1rem;
    min-height: 44px;
    transition: border-color var(--transition-fast);
}

.search-input:focus,
.filter-select:focus,
.year-input:focus {
    outline: none;
    border-color: var(--accent-color);
}

.checkbox-group {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    cursor: pointer;
    padding: var(--spacing-sm);
    border-radius: 4px;
    transition: background-color var(--transition-fast);
    min-height: 44px;
    position: relative;
}

.checkbox-label:hover {
    background-color: rgba(69, 123, 157, 0.1);
}

.checkbox-label input[type="checkbox"] {
    width: 24px;
    height: 24px;
    cursor: pointer;
    margin: 0;
    flex-shrink: 0;
}

/* Expanded touch area for better mobile interaction */
.checkbox-label::before {
    content: '';
    position: absolute;
    top: -4px;
    left: -4px;
    right: -4px;
    bottom: -4px;
    min-width: 44px;
    min-height: 44px;
}

.year-range {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
}

.year-input {
    width: auto;
    flex: 1;
}

.range-separator {
    color: var(--text-color);
    font-weight: 500;
}

.results-count {
    padding: var(--spacing-md);
    background: var(--background-color);
    border-radius: 4px;
    text-align: center;
    font-weight: 600;
    color: var(--text-color);
    margin-bottom: var(--spacing-md);
}

.btn-clear {
    width: 100%;
    padding: var(--spacing-md);
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color var(--transition-fast);
    min-height: 48px;
}

.btn-clear:hover {
    background: #d62839;
}

.btn-clear:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* ========================================
   Map Container
   ======================================== */
.map-container {
    flex: 1;
    position: relative;
    overflow: hidden;
}

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

/* View Mode Toggle */
.view-mode-toggle {
    position: absolute;
    top: var(--spacing-md);
    right: var(--spacing-md);
    z-index: 1000;
    background: white;
    border-radius: 8px;
    box-shadow: var(--shadow-md);
    display: flex;
    padding: 4px;
    gap: 4px;
}

.view-mode-btn {
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
    padding: var(--spacing-sm) var(--spacing-md);
    background: transparent;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-color);
    transition: all var(--transition-fast);
    min-width: 44px;
    min-height: 44px;
}

.view-mode-btn:hover {
    background: rgba(69, 123, 157, 0.1);
}

.view-mode-btn:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

.view-mode-btn.active {
    background: var(--accent-color);
    color: white;
}

.view-mode-btn.active:hover {
    background: var(--secondary-color);
}

.view-mode-icon {
    font-size: 1.2rem;
    line-height: 1;
}

.view-mode-label {
    font-weight: 600;
}

.loading-indicator {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: var(--spacing-xl);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    text-align: center;
    z-index: 1000;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto var(--spacing-md);
}

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

.loading-indicator p {
    font-weight: 600;
    color: var(--text-color);
}

.no-results {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: var(--spacing-xl);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    text-align: center;
    max-width: 400px;
}

.no-results p {
    margin-bottom: var(--spacing-sm);
    color: var(--text-color);
}

.no-results p:first-child {
    font-weight: 600;
    font-size: 1.1rem;
}

/* ========================================
   Leaflet Customizations
   ======================================== */
.leaflet-popup-content-wrapper {
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
}

.leaflet-popup-content {
    margin: 0;
    min-width: 300px;
    max-width: 400px;
}

/* Mobile popup optimization */
@media (max-width: 768px) {
    .leaflet-popup-content {
        min-width: 250px;
        max-width: 90vw;
        max-height: 70vh;
        overflow-y: auto;
    }
}

@media (max-width: 480px) {
    .leaflet-popup-content {
        min-width: 200px;
        max-width: 95vw;
        max-height: 75vh;
    }
}

/* Custom Popup Styles */
.popup-header {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    padding: var(--spacing-lg);
    margin: calc(-1 * var(--spacing-md));
    margin-bottom: var(--spacing-md);
    border-radius: 8px 8px 0 0;
}

.popup-photo {
    width: 100%;
    height: 200px;
    object-fit: cover;
    border-radius: 4px;
    margin-bottom: var(--spacing-md);
}

.popup-name {
    font-size: 1.4rem;
    font-weight: 700;
    margin-bottom: var(--spacing-xs);
}

.popup-position {
    font-size: 1rem;
    opacity: 0.95;
    margin-bottom: var(--spacing-sm);
}

.popup-badge {
    display: inline-block;
    padding: var(--spacing-xs) var(--spacing-sm);
    background: rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
}

.popup-body {
    padding: var(--spacing-md);
}

.popup-section {
    margin-bottom: var(--spacing-lg);
}

.popup-section:last-child {
    margin-bottom: 0;
}

.popup-section-title {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--secondary-color);
    margin-bottom: var(--spacing-sm);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.popup-bio {
    line-height: 1.6;
    color: var(--text-color);
    margin-bottom: var(--spacing-md);
}

.popup-info-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: var(--spacing-sm);
    font-size: 0.9rem;
}

.popup-info-label {
    font-weight: 600;
    color: var(--secondary-color);
}

.popup-info-value {
    color: var(--text-color);
}

.popup-contact-links {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
}

.popup-link {
    color: var(--accent-color);
    text-decoration: none;
    transition: color var(--transition-fast);
    word-break: break-all;
    padding: var(--spacing-sm);
    border-radius: 4px;
    min-height: 44px;
    display: flex;
    align-items: center;
}

.popup-link:hover {
    color: var(--primary-color);
    text-decoration: underline;
    background-color: rgba(69, 123, 157, 0.05);
}

.popup-social {
    display: flex;
    gap: var(--spacing-md);
    margin-top: var(--spacing-sm);
    flex-wrap: wrap;
}

.popup-social a {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 600;
    transition: color var(--transition-fast);
    padding: var(--spacing-sm);
    min-height: 44px;
    display: flex;
    align-items: center;
    border-radius: 4px;
}

.popup-social a:hover {
    color: var(--primary-color);
    background-color: rgba(69, 123, 157, 0.05);
}

.popup-committees {
    list-style: none;
}

.popup-committees li {
    padding: var(--spacing-xs) 0;
    color: var(--text-color);
}

.popup-committees li:before {
    content: "• ";
    color: var(--primary-color);
    font-weight: bold;
    margin-right: var(--spacing-xs);
}

.popup-location {
    padding: var(--spacing-md);
    background: var(--background-color);
    border-radius: 4px;
    font-size: 0.9rem;
    color: var(--text-color);
}

/* Custom Marker Icons */
.custom-marker {
    background: white;
    border-radius: 50%;
    border: 3px solid;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 12px;
    box-shadow: var(--shadow-md);
    cursor: pointer;
    transition: transform var(--transition-fast);
}

.custom-marker:hover {
    transform: scale(1.15);
    z-index: 1000;
}

/* Touch-friendly marker sizing on mobile */
@media (max-width: 768px) {
    .custom-marker {
        width: 36px;
        height: 36px;
        font-size: 14px;
    }
}

.marker-federal {
    border-color: var(--federal-color);
    background-color: var(--federal-color);
    color: white;
}

.marker-state {
    border-color: var(--state-color);
    background-color: var(--state-color);
    color: white;
}

.marker-county {
    border-color: var(--county-color);
    background-color: var(--county-color);
    color: white;
}

.marker-city {
    border-color: var(--city-color);
    background-color: var(--city-color);
    color: white;
}

.marker-town {
    border-color: var(--town-color);
    background-color: var(--town-color);
    color: white;
}

/* Cluster Icons */
.custom-cluster-icon {
    background: var(--primary-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
    cursor: pointer;
    transition: transform var(--transition-fast);
}

.custom-cluster-icon:hover {
    transform: scale(1.1);
}

.cluster-small {
    width: 40px;
    height: 40px;
}

.cluster-medium {
    width: 50px;
    height: 50px;
}

.cluster-large {
    width: 60px;
    height: 60px;
}

/* Larger cluster icons on mobile for better touch targets */
@media (max-width: 768px) {
    .cluster-small {
        width: 48px;
        height: 48px;
    }
    
    .cluster-medium {
        width: 56px;
        height: 56px;
    }
    
    .cluster-large {
        width: 64px;
        height: 64px;
    }
}

.cluster-count {
    color: white;
    font-weight: bold;
    font-size: 14px;
}

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

/* Tablet and small desktop breakpoint */
@media (max-width: 1024px) {
    .filter-panel {
        width: 280px;
    }
    
    header h1 {
        font-size: 1.75rem;
    }
    
    .view-mode-btn {
        padding: var(--spacing-sm);
    }
}

/* Tablet breakpoint */
@media (max-width: 768px) {
    header h1 {
        font-size: 1.5rem;
    }
    
    header p {
        font-size: 0.9rem;
    }
    
    main {
        flex-direction: column;
        height: calc(100vh - 100px);
    }
    
    .filter-panel {
        position: absolute;
        width: 100%;
        height: auto;
        max-height: 60%;
        bottom: 0;
        left: 0;
        transform: translateY(calc(100% - 60px));
        transition: transform var(--transition-normal);
        z-index: 1000;
    }
    
    .filter-panel.expanded {
        transform: translateY(0);
    }
    
    .filter-header {
        cursor: pointer;
    }
    
    .map-container {
        height: 100%;
    }
    
    .leaflet-popup-content {
        min-width: 250px;
        max-width: 90vw;
    }
    
    .popup-name {
        font-size: 1.2rem;
    }
    
    .view-mode-toggle {
        top: var(--spacing-sm);
        right: var(--spacing-sm);
    }
    
    .view-mode-label {
        display: none;
    }
    
    /* Timeline responsive design */
    .timeline-display {
        flex-direction: column;
        gap: var(--spacing-sm);
    }
    
    .timeline-controls {
        flex-wrap: wrap;
        gap: var(--spacing-xs);
    }
    
    .timeline-slider::-webkit-slider-thumb {
        width: 32px;
        height: 32px;
    }
    
    .timeline-slider::-moz-range-thumb {
        width: 32px;
        height: 32px;
    }
    
    .timeline-slider {
        height: 16px;
    }
}

/* Mobile breakpoint */
@media (max-width: 480px) {
    header h1 {
        font-size: 1.2rem;
    }
    
    header p {
        font-size: 0.85rem;
    }
    
    .filter-panel {
        transform: translateY(calc(100% - 50px));
        max-height: 70%;
    }
    
    .leaflet-popup-content {
        min-width: 200px;
        max-width: 95vw;
    }
    
    .popup-name {
        font-size: 1.1rem;
    }
    
    .popup-body {
        padding: var(--spacing-sm);
    }
    
    .filter-content {
        padding: var(--spacing-md);
    }
    
    .view-mode-btn {
        min-width: 48px;
        min-height: 48px;
        padding: var(--spacing-sm);
    }
}

/* Landscape orientation optimizations for mobile */
@media (max-width: 768px) and (orientation: landscape) and (max-height: 500px) {
    header {
        padding: var(--spacing-sm) var(--spacing-md);
    }
    
    header h1 {
        font-size: 1.2rem;
        margin-bottom: 0;
    }
    
    header p {
        display: none;
    }
    
    main {
        height: calc(100vh - 60px);
    }
    
    .filter-panel {
        width: 280px;
        height: 100%;
        max-height: 100%;
        bottom: auto;
        top: 0;
        left: 0;
        transform: translateX(-100%);
    }
    
    .filter-panel.expanded {
        transform: translateX(0);
    }
    
    .timeline-panel {
        display: none;
    }
    
    .leaflet-popup-content {
        max-height: 60vh;
        overflow-y: auto;
    }
}

/* ========================================
   Accessibility
   ======================================== */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

*:focus-visible {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    :root {
        --border-color: #000000;
    }
    
    .filter-panel,
    .popup-header {
        border: 2px solid black;
    }
}

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

/* ========================================
   Timeline Panel
   ======================================== */
.timeline-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    box-shadow: 0 -4px 10px rgba(0,0,0,0.1);
    z-index: 1000;
    transition: all var(--transition-normal);
    padding-bottom: env(safe-area-inset-bottom);
}

.timeline-toggle-btn {
    width: 100%;
    padding: var(--spacing-md);
    background: var(--secondary-color);
    color: white;
    border: none;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--spacing-sm);
}

.timeline-toggle-btn:hover {
    background: var(--accent-color);
}

.timeline-toggle-btn:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: -2px;
}

.timeline-content {
    padding: var(--spacing-lg);
    border-top: 1px solid var(--border-color);
}

.timeline-display {
    display: flex;
    justify-content: center;
    gap: var(--spacing-xl);
    margin-bottom: var(--spacing-lg);
}

.timeline-year-display,
.timeline-count-display {
    text-align: center;
}

.timeline-label {
    font-size: 0.85rem;
    color: #666;
    margin-right: var(--spacing-xs);
}

.timeline-year,
.timeline-count {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-color);
}

.timeline-slider-container {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    margin-bottom: var(--spacing-lg);
}

.timeline-label-year {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-color);
    min-width: 3rem;
    text-align: center;
}

.timeline-slider-wrapper {
    flex: 1;
    position: relative;
    height: 48px;
    display: flex;
    align-items: center;
}

.timeline-progress {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    height: 8px;
    background: var(--primary-color);
    border-radius: 4px;
    pointer-events: none;
    z-index: 1;
    transition: width 0.3s ease;
}

.timeline-slider {
    width: 100%;
    height: 12px;
    border-radius: 4px;
    background: var(--border-color);
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    position: relative;
    z-index: 2;
    cursor: pointer;
}

.timeline-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    transition: transform var(--transition-fast);
}

.timeline-slider::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

.timeline-slider::-moz-range-thumb {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    border: none;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    transition: transform var(--transition-fast);
}

.timeline-slider::-moz-range-thumb:hover {
    transform: scale(1.2);
}

.timeline-slider:focus::-webkit-slider-thumb {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

.timeline-slider:focus::-moz-range-thumb {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

.timeline-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: var(--spacing-sm);
}

.timeline-btn {
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 4px;
    padding: var(--spacing-sm) var(--spacing-md);
    font-size: 1.2rem;
    cursor: pointer;
    transition: all var(--transition-fast);
    min-width: 48px;
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.timeline-btn:hover {
    background: var(--secondary-color);
    transform: translateY(-2px);
}

.timeline-btn:active {
    transform: translateY(0);
}

.timeline-btn:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.timeline-btn-play {
    background: var(--primary-color);
    min-width: 56px;
}

.timeline-btn-play:hover {
    background: #d62839;
}

.timeline-speed-select {
    padding: var(--spacing-sm) var(--spacing-md);
    border: 2px solid var(--border-color);
    border-radius: 4px;
    font-size: 0.95rem;
    background: white;
    cursor: pointer;
    transition: border-color var(--transition-fast);
}

.timeline-speed-select:focus {
    outline: none;
    border-color: var(--accent-color);
}

/* Timeline responsive design */
@media (max-width: 768px) {
    .timeline-display {
        flex-direction: column;
        gap: var(--spacing-sm);
    }
    
    .timeline-controls {
        flex-wrap: wrap;
    }
    
    .timeline-btn {
        min-width: 40px;
        height: 40px;
        font-size: 1rem;
    }
}
