:root {
    --bg: #0f1923;
    --bg-card: #1a2733;
    --bg-hover: #243342;
    --border: #2d3f4f;
    --text: #e0e6ed;
    --text-dim: #8899aa;
    --accent: #4fc3f7;
    --accent-hover: #81d4fa;
    --green: #66bb6a;
    --orange: #ffa726;
    --red: #ef5350;
    --gold: #ffd54f;
    --radius: 8px;
}

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

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

header {
    background: linear-gradient(160deg, #0d2118 0%, #0f1923 55%, #111e2b 100%);
    border-top: 3px solid var(--accent);
    border-bottom: 1px solid var(--border);
    padding: 1.2rem 2rem 1.4rem;
}

.back-link {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    color: var(--text-dim);
    text-decoration: none;
    font-size: 0.7rem;
    font-weight: 500;
    letter-spacing: 0.6px;
    text-transform: uppercase;
    margin-bottom: 0.7rem;
    padding: 0.2rem 0.65rem 0.2rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 20px;
    background: rgba(255,255,255,0.03);
    transition: all 0.2s;
}

.back-link:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: rgba(79, 195, 247, 0.07);
}

.header-content h1 {
    display: flex;
    flex-direction: column;
    gap: 0.05rem;
    margin-bottom: 0.35rem;
}

.header-region {
    font-size: 2rem;
    font-weight: 800;
    color: #ffffff;
    letter-spacing: -1px;
    line-height: 1.1;
}

.header-tagline {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--accent);
    letter-spacing: 1.5px;
    text-transform: uppercase;
}

.subtitle {
    color: var(--text-dim);
    font-size: 0.8rem;
    margin-top: 0.1rem;
    max-width: 680px;
}

main {
    max-width: 1600px;
    margin: 0 auto;
    padding: 1rem 1.5rem;
}

/* Stats Bar */
.stats-bar {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
}

.stat {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1.25rem;
    flex: 1;
    text-align: center;
}

.stat-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--accent);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* Controls */
.controls {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
    align-items: flex-end;
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.filter-group label {
    font-size: 0.7rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.filter-group select,
.filter-group input {
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius);
    font-size: 0.85rem;
    min-width: 120px;
    max-width: 150px;
}

.filter-group input[type="number"] {
    width: 80px;
    min-width: 80px;
}

.filter-group input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    min-width: 110px;
    max-width: 140px;
    width: 120px;
    height: 4px;
    border-radius: 2px;
    background: var(--border);
    border: none;
    padding: 0;
    margin-top: 0.4rem;
    cursor: pointer;
}
.filter-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}
.filter-group input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    border: none;
    cursor: pointer;
}
.slider-value {
    font-size: 0.75rem;
    color: var(--accent);
    font-weight: 600;
    margin-top: 0.2rem;
}

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

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 72px;
    height: 34px;
    cursor: pointer;
    user-select: none;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
}

.toggle-slider {
    position: absolute;
    inset: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: all 0.2s;
}

.toggle-slider::before {
    content: '';
    position: absolute;
    height: 22px;
    width: 22px;
    left: 5px;
    bottom: 5px;
    background: var(--text-dim);
    border-radius: 4px;
    transition: all 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
    border-color: var(--orange);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(36px);
    background: var(--orange);
}

.toggle-text-off,
.toggle-text-on {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    pointer-events: none;
    transition: opacity 0.2s;
}

.toggle-text-off {
    right: 8px;
    color: var(--text-dim);
}

.toggle-text-on {
    left: 8px;
    color: var(--orange);
    opacity: 0;
}

.toggle-switch input:checked ~ .toggle-text-off { opacity: 0; }
.toggle-switch input:checked ~ .toggle-text-on { opacity: 1; }

.btn-reset {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-dim);
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.2s;
}

.btn-reset:hover {
    border-color: var(--accent);
    color: var(--accent);
}

/* Content Grid */
.content-grid {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
    gap: 1rem;
    margin-bottom: 1rem;
}

/* Map */
.map-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    height: 500px;
}

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

.leaflet-control-layers {
    background: var(--bg-card) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--radius) !important;
    color: var(--text) !important;
}

.leaflet-control-layers label {
    color: var(--text) !important;
}

.leaflet-control-layers-separator {
    border-top-color: var(--border) !important;
}

.homebase-marker {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: #ffd54f;
    text-shadow: 0 0 6px rgba(0,0,0,0.8), 0 1px 3px rgba(0,0,0,0.9);
    filter: drop-shadow(0 0 4px rgba(255, 213, 79, 0.6));
    cursor: default;
}

.map-legend {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 10px;
    min-width: 160px;
    font-size: 11px;
    color: var(--text);
    box-shadow: 0 2px 8px rgba(0,0,0,0.4);
}

.map-legend .legend-title {
    font-size: 10px;
    font-weight: 600;
    color: var(--text-muted);
    text-align: center;
    margin-bottom: 5px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.map-legend .legend-bar {
    height: 10px;
    border-radius: 5px;
    background: linear-gradient(to right,
        hsl(0,90%,52%),
        hsl(67,90%,52%),
        hsl(135,90%,52%),
        hsl(202,90%,52%),
        hsl(270,90%,52%));
    margin-bottom: 4px;
}

.map-legend .legend-ticks {
    display: flex;
    justify-content: space-between;
    color: var(--text-muted);
    font-size: 10px;
}

.leaflet-popup-content-wrapper {
    background: var(--bg-card) !important;
    color: var(--text) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--radius) !important;
}

.leaflet-popup-tip {
    background: var(--bg-card) !important;
}

.popup-stream {
    font-weight: 700;
    color: var(--accent);
    font-size: 1rem;
    margin-bottom: 0.25rem;
}

.popup-region {
    color: var(--text-dim);
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
}

.popup-stat {
    display: flex;
    justify-content: space-between;
    padding: 0.15rem 0;
    font-size: 0.85rem;
}

.popup-stat .label { color: var(--text-dim); }
.popup-stat .value { font-weight: 600; }

/* Table */
.table-container {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    display: flex;
    flex-direction: column;
}

.table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}

.table-header h2 {
    font-size: 1rem;
    font-weight: 600;
}

.table-header #filteredCount {
    color: var(--text-dim);
    font-weight: 400;
    font-size: 0.85rem;
}

.btn-export {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-dim);
    padding: 0.35rem 0.75rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.8rem;
    transition: all 0.2s;
}

.btn-export:hover {
    border-color: var(--green);
    color: var(--green);
}

.table-wrapper {
    overflow-y: auto;
    max-height: 450px;
    flex: 1;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

thead {
    position: sticky;
    top: 0;
    z-index: 10;
}

th {
    background: #15202b;
    color: var(--text-dim);
    padding: 0.5rem 0.5rem;
    text-align: left;
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    cursor: pointer;
    white-space: nowrap;
    border-bottom: 2px solid var(--border);
    user-select: none;
}

th:hover {
    color: var(--accent);
}

th.active {
    color: var(--accent);
}

th.active.asc::after { content: ' \25B2'; }
th.active.desc::after { content: ' \25BC'; }

td {
    padding: 0.4rem 0.5rem;
    border-bottom: 1px solid #1e2d3d;
    white-space: nowrap;
}

/* Truncate long stream names and region names */
#dataTable td:nth-child(2) {
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
}

#dataTable td:nth-child(4) {
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
}

tr:hover td {
    background: var(--bg-hover);
}

tr.highlighted td {
    background: rgba(79, 195, 247, 0.1);
    border-bottom-color: rgba(79, 195, 247, 0.2);
}

tr { cursor: pointer; }

/* Density coloring */
.density-high { color: var(--green); font-weight: 700; }
.density-med { color: var(--orange); font-weight: 600; }
.density-low { color: var(--text-dim); }

/* Estimated density styling */
.density-est { font-style: italic; opacity: 0.75; }

.est-badge {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--orange);
    margin-right: 1px;
    font-style: normal;
}

.est-badge-detail {
    display: inline-block;
    font-size: 0.55rem;
    background: rgba(255, 167, 38, 0.15);
    color: var(--orange);
    padding: 0.1rem 0.35rem;
    border-radius: 3px;
    vertical-align: middle;
    font-style: normal;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.rank-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    font-size: 0.7rem;
    font-weight: 700;
}

.rank-gold { background: rgba(255, 213, 79, 0.2); color: var(--gold); }
.rank-silver { background: rgba(176, 190, 197, 0.2); color: #b0bec5; }
.rank-bronze { background: rgba(255, 167, 38, 0.15); color: var(--orange); }

/* Stream Detail Panel */
.stream-detail {
    background: var(--bg-card);
    border: 1px solid var(--accent);
    border-radius: var(--radius);
    margin-bottom: 1rem;
    overflow: hidden;
}

.detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    background: rgba(79, 195, 247, 0.05);
    border-bottom: 1px solid var(--border);
}

.detail-header h2 {
    color: var(--accent);
    font-size: 1.1rem;
}

.btn-close {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 0.5rem;
}

.btn-close:hover { color: var(--text); }

.detail-body {
    padding: 1rem;
}

.detail-stats {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.detail-stat {
    background: var(--bg);
    border-radius: var(--radius);
    padding: 0.6rem 0.8rem;
}

.detail-stat .label {
    font-size: 0.7rem;
    color: var(--text-dim);
    text-transform: uppercase;
}

.detail-stat .value {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--accent);
}

.detail-sites {
    margin-bottom: 0.75rem;
}

.samples-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

.samples-table th {
    background: var(--bg);
    color: var(--text-dim);
    text-transform: uppercase;
    font-size: 0.7rem;
    padding: 0.35rem 0.6rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.samples-table td {
    padding: 0.3rem 0.6rem;
    border-bottom: 1px solid var(--border);
}

.samples-table tr:last-child td {
    border-bottom: none;
}

.samples-table tr:hover td {
    background: var(--bg-light);
}

.samples-heading-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 0 0.5rem;
}

.btn-expand-samples {
    background: none;
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-dim);
    cursor: pointer;
    padding: 0.2rem 0.4rem;
    font-size: 0.75rem;
    line-height: 1;
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.btn-expand-samples:hover {
    border-color: var(--accent);
    color: var(--accent);
}

/* Samples modal */
.samples-modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.75);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}

.samples-modal-overlay.open {
    display: flex;
}

.samples-modal {
    background: var(--bg-card);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    width: 92vw;
    max-width: 1100px;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.samples-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.85rem 1.2rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.samples-modal-header h2 {
    font-size: 1rem;
    margin: 0;
}

.samples-modal-body {
    overflow-y: auto;
    flex: 1;
    padding: 1rem 1.2rem;
}

.samples-modal .samples-table {
    font-size: 0.85rem;
}

.samples-modal .samples-table th,
.samples-modal .samples-table td {
    padding: 0.45rem 0.8rem;
}

.sites-heading {
    font-size: 0.85rem;
    color: var(--text);
    margin-bottom: 0.5rem;
    font-weight: 600;
}

.sites-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.78rem;
    margin-bottom: 0.5rem;
}

.sites-table th {
    background: var(--bg);
    cursor: default;
    border-bottom: 1px solid var(--border);
    padding: 0.35rem 0.5rem;
    font-size: 0.68rem;
}

.sites-table td {
    padding: 0.35rem 0.5rem;
    border-bottom: 1px solid rgba(45, 63, 79, 0.5);
}

.site-row {
    cursor: pointer;
}

.site-row:hover td {
    background: var(--bg-hover);
}

.coord-cell {
    font-family: monospace;
    font-size: 0.7rem;
    color: var(--text-dim);
}

.site-row-nocoord td {
    cursor: default;
}

.sites-note {
    font-size: 0.7rem;
    color: var(--text-dim);
    font-style: italic;
    margin-top: 0.25rem;
}

.best-site-tag {
    display: inline-block;
    font-size: 0.55rem;
    font-weight: 700;
    background: rgba(102, 187, 106, 0.15);
    color: var(--green);
    padding: 0.1rem 0.35rem;
    border-radius: 3px;
    letter-spacing: 0.5px;
}

.detail-notes {
    color: var(--text-dim);
    font-size: 0.85rem;
    font-style: italic;
}

/* Sources Section */
.sources-section {
    margin-top: 2rem;
}

.sources-section h2 {
    font-size: 1.1rem;
    margin-bottom: 0.75rem;
    color: var(--text);
}

.sources-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 0.75rem;
}

.source-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
}

.source-card h3 {
    font-size: 0.85rem;
    color: var(--accent);
    margin-bottom: 0.25rem;
}

.source-card p {
    font-size: 0.75rem;
    color: var(--text-dim);
}

.source-card a {
    color: var(--accent);
    text-decoration: none;
    font-size: 0.75rem;
}

.source-card a:hover { text-decoration: underline; }

/* Footer */
footer {
    border-top: 1px solid var(--border);
    padding: 1.5rem 2rem;
    text-align: center;
    color: var(--text-dim);
    font-size: 0.8rem;
    margin-top: 2rem;
}

.footer-note {
    margin-top: 0.5rem;
    font-style: italic;
    font-size: 0.75rem;
}

/* Responsive */
@media (max-width: 1200px) {
    .content-grid {
        grid-template-columns: 1fr;
    }
    .map-container {
        height: 400px;
    }
    #map {
        height: 400px;
    }
}

@media (max-width: 768px) {
    .stats-bar { flex-wrap: wrap; }
    .stat { flex: 1 1 calc(50% - 0.5rem); }
    .controls { flex-direction: column; }
    .filter-group select,
    .filter-group input { min-width: 100%; }
    header { padding: 1rem 1rem 1.2rem; }
    .header-region { font-size: 1.5rem; }
    .back-link { margin-bottom: 0.5rem; }
    main { padding: 0.75rem; }
}

/* Scrollbar styling */
.table-wrapper::-webkit-scrollbar {
    width: 6px;
}

.table-wrapper::-webkit-scrollbar-track {
    background: var(--bg);
}

.table-wrapper::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

.table-wrapper::-webkit-scrollbar-thumb:hover {
    background: var(--text-dim);
}

/* ── Size histogram ──────────────────────────────────────────────────────── */
.size-hist {
    margin-top: 1rem;
    padding: 0.75rem 1rem;
    background: rgba(255,255,255,0.03);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.size-hist-title {
    font-size: 0.75rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.6rem;
}

.size-hist-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.3rem;
}

.size-hist-label {
    font-size: 0.75rem;
    color: var(--text-dim);
    width: 3.2rem;
    flex-shrink: 0;
    text-align: right;
}

.size-hist-bar-wrap {
    flex: 1;
    height: 10px;
    background: rgba(255,255,255,0.05);
    border-radius: 3px;
    overflow: hidden;
}

.size-hist-bar {
    height: 100%;
    background: var(--border);
    border-radius: 3px;
    transition: width 0.3s;
}

.size-hist-bar.quality {
    background: var(--accent);
}

.size-hist-pct {
    font-size: 0.72rem;
    color: var(--text-dim);
    width: 2.5rem;
    flex-shrink: 0;
}

/* ── Checkbox filter toggle ──────────────────────────────────────────────── */
.checkbox-filter {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: var(--text);
    cursor: pointer;
    user-select: none;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.45rem 0.75rem;
    transition: border-color 0.2s;
}

.checkbox-filter:hover {
    border-color: var(--accent);
}

.checkbox-filter input[type="checkbox"] {
    accent-color: var(--accent);
    width: 15px !important;
    min-width: 15px !important;
    max-width: 15px !important;
    height: 15px;
    flex-shrink: 0;
    cursor: pointer;
    padding: 0;
    background: none;
    border: none;
}

/* ── Multi-select checkbox dropdown ──────────────────────────────────────── */
.multi-select-wrap {
    position: relative;
}

.multi-select-btn {
    width: 100%;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    padding: 0.45rem 0.75rem;
    font-size: 0.9rem;
    text-align: left;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: border-color 0.2s;
}

.multi-select-btn:hover,
.multi-select-btn.open {
    border-color: var(--accent);
    color: var(--accent);
}

.multi-select-panel {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    width: max-content;
    min-width: 100%;
    max-height: 280px;
    overflow-y: auto;
    background: var(--bg-card);
    border: 1px solid var(--accent);
    border-radius: var(--radius);
    z-index: 999;
    padding: 0.4rem 0;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.multi-select-panel.open {
    display: block;
}

.multi-select-panel label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.85rem;
    cursor: pointer;
    color: var(--text);
    transition: background 0.15s;
    text-transform: none;
    letter-spacing: 0;
    white-space: nowrap;
}

.multi-select-panel label:hover {
    background: var(--bg-hover);
}

.multi-select-panel input[type="checkbox"] {
    accent-color: var(--accent);
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

.multi-select-panel .ms-divider {
    border: none;
    border-top: 1px solid var(--border);
    margin: 0.3rem 0;
}
