/* ========================================
   CSS Variables & Base Styles
   ======================================== */

:root {
    /* Colors - Light Professional Theme */
    --color-bg: #f8f9fc;
    --color-surface: #ffffff;
    --color-surface-elevated: #ffffff;
    --color-border: #e2e8f0;
    --color-border-light: #f1f5f9;
    
    --color-text-primary: #1e293b;
    --color-text-secondary: #64748b;
    --color-text-muted: #94a3b8;
    
    --color-accent: #3b82f6;
    --color-accent-hover: #2563eb;
    --color-accent-light: #eff6ff;
    
    --color-success: #10b981;
    --color-success-light: #d1fae5;
    --color-warning: #f59e0b;
    --color-warning-light: #fef3c7;
    --color-danger: #ef4444;
    --color-danger-light: #fee2e2;
    
    /* Unreached indicator */
    --color-unreached: #dc2626;
    --color-reached: #059669;
    
    /* Typography */
    --font-sans: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-mono: 'DM Mono', 'SF Mono', Monaco, 'Cascadia Code', monospace;
    
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.25rem;
    --text-2xl: 1.5rem;
    --text-3xl: 1.875rem;
    
    /* Spacing */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.25rem;
    --space-6: 1.5rem;
    --space-8: 2rem;
    --space-10: 2.5rem;
    --space-12: 3rem;
    
    /* Border Radius */
    --radius-sm: 0.25rem;
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
    --radius-xl: 1rem;
    --radius-full: 9999px;
    
    /* Shadows */
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
    
    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-base: 200ms ease;
    --transition-slow: 300ms ease;
    
    /* Layout */
    --header-height: 64px;
    --table-panel-width: 55%;
}

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

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

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.5;
    color: var(--color-text-primary);
    background-color: var(--color-bg);
    min-height: 100vh;
    overflow: hidden;
}

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

.header {
    height: var(--header-height);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-6);
    box-shadow: var(--shadow-sm);
}

.header__title {
    font-size: var(--text-2xl);
    font-weight: 600;
    color: var(--color-text-primary);
    letter-spacing: -0.02em;
}

.header__right {
    display: flex;
    align-items: center;
    gap: var(--space-4);
}

.header__church {
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--color-accent);
    background: var(--color-accent-light);
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-full);
}

/* ========================================
   Dashboard Layout
   ======================================== */

.dashboard {
    display: flex;
    height: calc(100vh - var(--header-height));
    overflow: hidden;
}

.panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel--table {
    width: var(--table-panel-width);
    border-right: 1px solid var(--color-border);
    background: var(--color-surface);
}

.panel--visuals {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--color-bg);
}

/* ========================================
   Common UI Elements
   ======================================== */

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    font-family: inherit;
    font-size: var(--text-sm);
    font-weight: 500;
    line-height: 1;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.btn--primary {
    background: var(--color-accent);
    color: white;
}

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

.btn--outline {
    background: transparent;
    color: var(--color-accent);
    border: 1px solid var(--color-accent);
}

.btn--outline:hover {
    background: var(--color-accent-light);
}

.btn--small {
    padding: var(--space-1) var(--space-2);
    font-size: var(--text-xs);
}

/* Form Controls */
.input {
    width: 100%;
    padding: var(--space-2) var(--space-3);
    font-family: inherit;
    font-size: var(--text-sm);
    color: var(--color-text-primary);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.input:focus {
    outline: none;
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px var(--color-accent-light);
}

.input::placeholder {
    color: var(--color-text-muted);
}

.select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-3) center;
    padding-right: var(--space-10);
}

/* Badges */
.badge {
    display: inline-flex;
    align-items: center;
    padding: var(--space-1) var(--space-2);
    font-size: var(--text-xs);
    font-weight: 500;
    border-radius: var(--radius-full);
    white-space: nowrap;
    background: var(--color-bg);
    color: var(--color-text-secondary);
}

.badge--unreached {
    background: var(--color-danger-light);
    color: var(--color-unreached);
}

.badge--reached {
    background: var(--color-success-light);
    color: var(--color-reached);
}

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

/* Loading State */
.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-12);
    color: var(--color-text-muted);
}

.loading__spinner {
    width: 24px;
    height: 24px;
    border: 2px solid var(--color-border);
    border-top-color: var(--color-accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ========================================
   Filters Section
   ======================================== */

.filters {
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--color-border);
    background: var(--color-surface);
}

.filters__row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-4);
}

.filters__search {
    flex: 1;
    min-width: 200px;
    position: relative;
}

.filters__search-icon {
    position: absolute;
    left: var(--space-3);
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    color: var(--color-text-muted);
    pointer-events: none;
}

.filters__search .input {
    padding-left: var(--space-10);
}

.filters__group {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.filters__label {
    font-size: var(--text-sm);
    color: var(--color-text-secondary);
    white-space: nowrap;
}

.filters__select {
    min-width: 140px;
}

/* ========================================
   Table Container
   ======================================== */

.table-container {
    flex: 1;
    overflow: auto;
    padding: var(--space-4) var(--space-5);
}

.table-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-3);
}

.table-info__count {
    font-size: var(--text-sm);
    color: var(--color-text-secondary);
}

.table-info__count strong {
    color: var(--color-text-primary);
    font-weight: 600;
}

/* ========================================
   Data Table
   ======================================== */

.data-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: var(--text-sm);
}

.data-table th,
.data-table td {
    text-align: left;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border-light);
}

.data-table th {
    position: sticky;
    top: 0;
    background: var(--color-bg);
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    font-size: var(--text-xs);
    letter-spacing: 0.05em;
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    transition: color var(--transition-fast);
    z-index: 10;
}

.data-table th:hover {
    color: var(--color-text-primary);
}

.data-table th.sorted {
    color: var(--color-accent);
}

.data-table th .sort-icon {
    display: inline-block;
    margin-left: var(--space-1);
    opacity: 0.5;
}

.data-table th.sorted .sort-icon {
    opacity: 1;
}

.data-table tbody tr {
    transition: background var(--transition-fast);
    cursor: pointer;
}

.data-table tbody tr:hover {
    background: var(--color-accent-light);
}

.data-table tbody tr.selected {
    background: var(--color-accent-light);
}

.data-table td {
    color: var(--color-text-primary);
    vertical-align: middle;
}

/* Table Cell Types */
.cell-name {
    font-weight: 500;
}

.cell-country {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.cell-country__flag {
    font-size: 1.2em;
}

.cell-unreached {
    text-align: center;
}

.cell-unreached .indicator {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
}

.cell-unreached .indicator--yes {
    background: var(--color-danger-light);
    color: var(--color-unreached);
}

.cell-unreached .indicator--no {
    background: var(--color-success-light);
    color: var(--color-reached);
}

.cell-org {
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Empty State */
.table-empty {
    text-align: center;
    padding: var(--space-12);
    color: var(--color-text-muted);
}

.table-empty__icon {
    font-size: 3rem;
    margin-bottom: var(--space-4);
    opacity: 0.5;
}

.table-empty__title {
    font-size: var(--text-lg);
    font-weight: 500;
    color: var(--color-text-secondary);
    margin-bottom: var(--space-2);
}

.table-empty__text {
    font-size: var(--text-sm);
}

/* ========================================
   Map Container
   ======================================== */

.map-container {
    flex: 1;
    min-height: 200px;
    position: relative;
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    margin: var(--space-4);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

#map {
    width: 100%;
    height: 100%;
    z-index: 1;
}

/* ========================================
   Leaflet Customizations
   ======================================== */

.leaflet-container {
    font-family: var(--font-sans);
    background: #e8f4f8;
}

.leaflet-control-zoom {
    border: none !important;
    box-shadow: var(--shadow-md) !important;
    border-radius: var(--radius-lg) !important;
    overflow: hidden;
}

.leaflet-control-zoom a {
    background: var(--color-surface) !important;
    color: var(--color-text-primary) !important;
    border: none !important;
    width: 32px !important;
    height: 32px !important;
    line-height: 32px !important;
    font-size: var(--text-lg) !important;
    transition: background var(--transition-fast);
}

.leaflet-control-zoom a:hover {
    background: var(--color-bg) !important;
}

.leaflet-control-zoom a:first-child {
    border-bottom: 1px solid var(--color-border) !important;
}

.leaflet-control-attribution {
    background: rgba(255, 255, 255, 0.9) !important;
    padding: var(--space-1) var(--space-2) !important;
    font-size: var(--text-xs) !important;
    border-radius: var(--radius-md) !important;
    margin: var(--space-2) !important;
}

/* ========================================
   Custom Map Markers
   ======================================== */

.missionary-marker {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: var(--color-accent);
    border: 3px solid white;
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-md);
    font-size: var(--text-xs);
    font-weight: 600;
    color: white;
    cursor: pointer;
    transition: transform var(--transition-fast);
}

.missionary-marker:hover {
    transform: scale(1.15);
}

.missionary-marker--unreached {
    background: var(--color-unreached);
}

.missionary-marker--reached {
    background: var(--color-reached);
}

.missionary-marker--cluster {
    width: 36px;
    height: 36px;
    font-size: var(--text-sm);
}

/* ========================================
   Map Popup
   ======================================== */

.leaflet-popup-content-wrapper {
    border-radius: var(--radius-lg) !important;
    box-shadow: var(--shadow-lg) !important;
    padding: 0 !important;
}

.leaflet-popup-content {
    margin: 0 !important;
    min-width: 200px;
}

.leaflet-popup-tip {
    box-shadow: none !important;
}

.map-popup {
    padding: var(--space-4);
}

.map-popup__header {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    margin-bottom: var(--space-3);
}

.map-popup__avatar {
    width: 40px;
    height: 40px;
    background: var(--color-accent-light);
    color: var(--color-accent);
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: var(--text-sm);
    flex-shrink: 0;
}

.map-popup__info {
    flex: 1;
    min-width: 0;
}

.map-popup__name {
    font-weight: 600;
    color: var(--color-text-primary);
    font-size: var(--text-sm);
    margin-bottom: var(--space-1);
}

.map-popup__org {
    font-size: var(--text-xs);
    color: var(--color-text-secondary);
}

.map-popup__details {
    display: grid;
    gap: var(--space-2);
}

.map-popup__detail {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-xs);
    color: var(--color-text-secondary);
}

.map-popup__btn {
    width: 100%;
    padding: var(--space-2);
    font-size: var(--text-xs);
    font-weight: 500;
    color: var(--color-accent);
    background: var(--color-accent-light);
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.map-popup__btn:hover {
    background: var(--color-accent);
    color: white;
}

/* ========================================
   Map Legend
   ======================================== */

.map-legend {
    position: absolute;
    bottom: var(--space-4);
    left: var(--space-4);
    z-index: 1000;
    background: var(--color-surface);
    padding: var(--space-3);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    font-size: var(--text-xs);
}

.map-legend__title {
    font-weight: 600;
    color: var(--color-text-secondary);
    margin-bottom: var(--space-2);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.map-legend__items {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.map-legend__item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.map-legend__dot {
    width: 12px;
    height: 12px;
    border-radius: var(--radius-full);
    border: 2px solid white;
    box-shadow: var(--shadow-sm);
}

.map-legend__dot--unreached {
    background: var(--color-unreached);
}

.map-legend__dot--reached {
    background: var(--color-reached);
}

.map-legend__label {
    color: var(--color-text-secondary);
}

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

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-6);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--transition-base), visibility var(--transition-base);
}

.modal-overlay:not([hidden]) {
    opacity: 1;
    visibility: visible;
}

.modal {
    position: relative;
    width: 100%;
    max-width: 600px;
    max-height: 80vh;
    background: var(--color-surface);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transform: scale(0.95);
    transition: transform var(--transition-base);
}

.modal--wide {
    max-width: 800px;
}

.modal-overlay:not([hidden]) .modal {
    transform: scale(1);
}

.modal__close {
    position: absolute;
    top: var(--space-4);
    right: var(--space-4);
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--text-xl);
    color: var(--color-text-muted);
    background: var(--color-bg);
    border: none;
    border-radius: var(--radius-full);
    cursor: pointer;
    transition: all var(--transition-fast);
    z-index: 1;
}

.modal__close:hover {
    color: var(--color-text-primary);
    background: var(--color-border);
}

.modal__content {
    padding: var(--space-6);
    overflow-y: auto;
}

.modal__header {
    margin-bottom: var(--space-6);
}

.modal__title {
    font-size: var(--text-xl);
    font-weight: 600;
    color: var(--color-text-primary);
    margin-bottom: var(--space-1);
}

.modal__subtitle {
    font-size: var(--text-sm);
    color: var(--color-text-secondary);
}

.modal__section {
    margin-bottom: var(--space-6);
}

.modal__section:last-child {
    margin-bottom: 0;
}

.modal__section-title {
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: var(--space-3);
}

.modal__grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-4);
}

.modal__field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.modal__field--full {
    grid-column: 1 / -1;
}

.modal__label {
    font-size: var(--text-xs);
    font-weight: 500;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.modal__value {
    font-size: var(--text-sm);
    color: var(--color-text-primary);
}

/* ========================================
   Missionary Photo & Lightbox
   ======================================== */

.modal__avatar-wrapper {
    position: relative;
    width: 56px;
    height: 56px;
    flex-shrink: 0;
}

.modal__photo {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    object-fit: cover;
    cursor: pointer;
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.modal__photo:hover {
    opacity: 0.85;
    transform: scale(1.05);
}

.modal__photo-fallback {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--color-accent-light);
    color: var(--color-accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 600;
}

/* Lightbox overlay */
.lightbox {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    cursor: pointer;
}

.lightbox[hidden] {
    display: none;
}

.lightbox__img {
    max-width: 90vw;
    max-height: 90vh;
    object-fit: contain;
    border-radius: var(--radius-lg);
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
    cursor: default;
}

.lightbox__close {
    position: absolute;
    top: 20px;
    right: 24px;
    background: none;
    border: none;
    color: white;
    font-size: 36px;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s ease;
    line-height: 1;
    padding: 4px 8px;
}

.lightbox__close:hover {
    opacity: 1;
}

/* ========================================
   Newsletter List (inside modal)
   ======================================== */

.newsletter-list {
    display: flex;
    flex-direction: column;
    max-height: 70vh;
    overflow-y: auto;
}

.newsletter-list__loading,
.newsletter-list__empty {
    padding: 2rem;
    text-align: center;
    color: var(--color-text-secondary);
    font-size: 0.875rem;
}

.newsletter-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--color-border);
    text-decoration: none;
    color: var(--color-text-primary);
    transition: background 0.12s ease;
    cursor: pointer;
    min-height: 44px;
}

.newsletter-row:last-child {
    border-bottom: none;
}

.newsletter-row:hover {
    background: var(--color-accent-light);
}

.newsletter-row__missionary {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 200px;
    flex-shrink: 0;
}

.newsletter-row__name {
    font-size: 0.8125rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 160px;
}

.newsletter-row__date {
    font-size: 0.8125rem;
    color: var(--color-text-secondary);
    white-space: nowrap;
    min-width: 100px;
}

.newsletter-row__file {
    font-size: 0.75rem;
    color: var(--color-text-secondary);
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.newsletter-row__dl {
    flex-shrink: 0;
    color: var(--color-text-secondary);
    display: flex;
    align-items: center;
}

.newsletter-row:hover .newsletter-row__dl {
    color: var(--color-accent);
}

.newsletter-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.newsletter-avatar--fallback {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-border);
    color: var(--color-text-secondary);
    font-weight: 600;
    font-size: 11px;
    border-radius: 50%;
}

/* ========================================
   Responsive — Mobile-first adjustments
   ======================================== */

/* Tablet and below: stack layout, hide map */
@media (max-width: 767px) {
    .dashboard {
        flex-direction: column;
    }

    .panel--table {
        width: 100%;
        height: 100%;
        border-right: none;
    }

    .panel--visuals {
        display: none; /* Hide map entirely on mobile */
    }

    .header {
        padding: 0 var(--space-4);
    }
    
    .header__title {
        font-size: var(--text-lg);
    }

    .header__church {
        display: none; /* Hide church badge to save space */
    }

    .filters__row {
        flex-direction: column;
        align-items: stretch;
    }

    .filters__search {
        min-width: 100%;
    }

    .filters__group {
        justify-content: space-between;
    }

    .data-table {
        font-size: var(--text-xs);
    }

    .data-table th,
    .data-table td {
        padding: var(--space-2) var(--space-3);
    }

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

    .modal {
        max-height: 90vh;
    }

    .modal-overlay {
        padding: var(--space-3);
    }
}

/* Medium screens: side-by-side but smaller map */
@media (min-width: 768px) and (max-width: 1024px) {
    :root {
        --table-panel-width: 60%;
    }

    .map-container {
        margin: var(--space-2);
    }
    
    .map-legend {
        bottom: var(--space-2);
        left: var(--space-2);
        padding: var(--space-2);
    }
}
