:root {
    --bg: #0b0f14;
    --bg-panel: #151b23;
    --bg-card: #1d2530;
    --bg-hover: #242d3a;
    --border: #2a3341;
    --border-strong: #3d4a5c;
    --text: #e6edf3;
    --text-dim: #8b95a3;
    --text-faint: #5e6773;
    --accent: #4a9eff;
    --accent-hover: #6cb0ff;
    --accent-soft: rgba(74, 158, 255, 0.12);

    --gain: #22c55e;
    --loss: #f87171;
    --selection: #fbbf24;

    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.hidden {
    display: none !important;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}

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

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 1000;
    gap: 20px;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    color: var(--text);
    text-decoration: none;
    font-size: 17px;
    font-weight: 600;
    letter-spacing: -0.3px;
}

.brand:hover {
    color: var(--text);
    text-decoration: none;
}

.track-name {
    color: var(--text-dim);
    font-size: 13px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
    padding-left: 14px;
    border-left: 1px solid var(--border);
}

.track-name:empty {
    display: none;
}

/* ===== Buttons ===== */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--bg-card);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, transform 0.05s;
    text-decoration: none;
    user-select: none;
    font-family: inherit;
}

.btn:hover {
    background: var(--bg-hover);
    border-color: var(--border-strong);
    text-decoration: none;
}

.btn:active {
    transform: translateY(1px);
}

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

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

.btn-small {
    padding: 5px 10px;
    font-size: 12px;
}

/* ===== Drop zone ===== */

.drop-zone {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    padding: 32px;
    transition: background 0.2s;
}

.drop-zone.dragover {
    background: var(--accent-soft);
}

.drop-zone-inner {
    text-align: center;
    padding: 60px 70px;
    border: 2px dashed var(--border);
    border-radius: 16px;
    background: var(--bg-panel);
    max-width: 520px;
    color: var(--text);
}

.drop-zone.dragover .drop-zone-inner {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.drop-icon {
    color: var(--text-dim);
    margin-bottom: 16px;
}

.drop-zone-title {
    margin: 0 0 8px;
    font-size: 20px;
    font-weight: 600;
}

.drop-zone-hint {
    margin: 0;
    color: var(--text-dim);
    font-size: 14px;
}

.drop-zone-note {
    margin: 20px 0 0;
    padding-top: 20px;
    border-top: 1px solid var(--border);
    color: var(--text-faint);
    font-size: 12px;
    line-height: 1.5;
}

/* ===== Loading overlay ===== */

.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(11, 15, 20, 0.75);
    backdrop-filter: blur(3px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    z-index: 2000;
    color: var(--text);
    font-size: 14px;
    font-weight: 500;
}

.loading-spinner {
    width: 42px;
    height: 42px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ===== Dashboard layout ===== */

.dashboard {
    padding: 20px 24px 30px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    max-width: 1600px;
    margin: 0 auto;
    width: 100%;
    flex: 1;
}

/* ===== HERO STATS ===== */

.hero-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 14px;
}

.hero-card {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    position: relative;
    overflow: hidden;
}

.hero-card::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: var(--accent);
}

.hero-card-gain::before { background: var(--gain); }
.hero-card-loss::before { background: var(--loss); }

.hero-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--text-dim);
    font-weight: 600;
}

.hero-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.5px;
}

.hero-sub {
    font-size: 12px;
    color: var(--text-faint);
}

/* ===== Rows ===== */

.row {
    display: grid;
    gap: 18px;
}

.row-map {
    grid-template-columns: minmax(0, 2fr) minmax(300px, 1fr);
}

.row-chart {
    grid-template-columns: minmax(0, 2fr) minmax(300px, 1fr);
}

/* ===== Panel ===== */

.panel {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    gap: 14px;
    flex-wrap: wrap;
    min-height: 52px;
}

.panel-header h2 {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: -0.1px;
}

.panel-hint {
    font-size: 12px;
    color: var(--text-dim);
}

.panel-controls {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
}

.inline-control {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--text-dim);
    font-weight: 500;
}

.inline-control select,
.inline-control input {
    padding: 5px 8px;
    background: var(--bg-card);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: inherit;
    font-weight: 500;
}

.inline-control input[type=number] {
    width: 64px;
}

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

.inline-control .unit {
    color: var(--text-faint);
    font-weight: 400;
}

/* ===== Map panel ===== */

.panel-map {
    min-height: 480px;
}

#map {
    flex: 1;
    min-height: 420px;
    background: var(--bg-card);
}

.map-style-switcher {
    display: inline-flex;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.style-btn {
    padding: 5px 12px;
    background: transparent;
    color: var(--text-dim);
    border: none;
    border-right: 1px solid var(--border);
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    transition: background 0.15s, color 0.15s;
}

.style-btn:last-child { border-right: none; }

.style-btn:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.style-btn.active {
    background: var(--accent);
    color: #fff;
}

.map-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 10px;
    padding: 10px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg-card);
    font-size: 11px;
}

.legend-item {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    color: var(--text-dim);
    white-space: nowrap;
}

.legend-swatch {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    display: inline-block;
    flex-shrink: 0;
}

/* ===== Stats detail panel (right of map) ===== */

.panel-stats-detail {
    min-height: 480px;
}

.stats-detail-body {
    display: flex;
    flex-direction: column;
    padding: 4px 18px;
}

.sd-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 11px 0;
    border-bottom: 1px solid var(--border);
    gap: 10px;
}

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

.sd-label {
    font-size: 12px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.4px;
    font-weight: 500;
}

.sd-value {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    text-align: right;
}

/* ===== Chart panel ===== */

.panel-chart {
    min-height: 420px;
}

.chart {
    flex: 1;
    min-height: 340px;
}

.chart-hint {
    padding: 8px 18px 10px;
    border-top: 1px solid var(--border);
    color: var(--text-faint);
    font-size: 11px;
    text-align: center;
}

/* ===== Selection panel (right of chart) ===== */

.panel-selection {
    min-height: 420px;
}

.selection-body {
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 14px 18px;
    flex: 1;
}

.selection-hint {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 14px;
    background: var(--accent-soft);
    border: 1px solid rgba(74, 158, 255, 0.25);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 12px;
    line-height: 1.45;
}

.selection-hint svg {
    color: var(--accent);
    flex-shrink: 0;
    margin-top: 1px;
}

.selection-stats {
    display: flex;
    flex-direction: column;
}

.sel-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 9px 0;
    border-bottom: 1px solid var(--border);
    gap: 10px;
}

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

.sel-label {
    font-size: 12px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.4px;
    font-weight: 500;
}

.sel-value {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.sel-gain { color: var(--gain); }
.sel-loss { color: var(--loss); }

/* ===== Climbs ===== */

.panel-climbs {
    min-height: 150px;
}

.climbs-wrapper {
    padding: 0;
    overflow-x: auto;
}

.climbs-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}

.climbs-table th,
.climbs-table td {
    padding: 11px 16px;
    text-align: right;
    border-bottom: 1px solid var(--border);
}

.climbs-table th:first-child,
.climbs-table td:first-child {
    text-align: left;
    width: 40px;
}

.climbs-table th {
    background: var(--bg-card);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-dim);
}

.climbs-table tbody tr {
    cursor: pointer;
    transition: background 0.12s;
}

.climbs-table tbody tr:hover {
    background: var(--accent-soft);
}

.climbs-table tbody tr.active {
    background: var(--accent-soft);
    box-shadow: inset 3px 0 0 var(--accent);
}

.climbs-table .grade-cell {
    font-weight: 700;
}

.climbs-empty {
    padding: 28px;
    text-align: center;
    color: var(--text-dim);
    font-size: 13px;
}

/* ===== Advanced panel ===== */

.advanced-panel {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.advanced-panel summary {
    padding: 12px 18px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-dim);
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 8px;
}

.advanced-panel summary::-webkit-details-marker {
    display: none;
}

.advanced-panel summary::before {
    content: '▸';
    transition: transform 0.15s;
    font-size: 10px;
}

.advanced-panel[open] summary::before {
    transform: rotate(90deg);
}

.advanced-panel[open] summary {
    color: var(--text);
    border-bottom: 1px solid var(--border);
}

.advanced-body {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    padding: 16px 18px;
    align-items: center;
}

.advanced-hint {
    width: 100%;
    margin: 0;
    font-size: 12px;
    color: var(--text-faint);
    line-height: 1.5;
}

/* ===== Footer ===== */

.app-footer {
    padding: 14px 24px;
    border-top: 1px solid var(--border);
    background: var(--bg-panel);
    margin-top: auto;
}

.footer-inner {
    max-width: 1600px;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    color: var(--text-faint);
    font-size: 11px;
}

.footer-sep {
    color: var(--border-strong);
}

/* ===== Legal / Privacy page ===== */

.legal-page {
    flex: 1;
    padding: 40px 24px;
    display: flex;
    justify-content: center;
}

.legal-content {
    max-width: 760px;
    width: 100%;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 36px 44px;
    line-height: 1.65;
}

.legal-content h1 {
    margin: 0 0 6px;
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.4px;
}

.legal-content h2 {
    margin: 32px 0 10px;
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
    border-bottom: 1px solid var(--border);
    padding-bottom: 8px;
}

.legal-content h3 {
    margin: 20px 0 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.4px;
}

.legal-content p {
    margin: 0 0 12px;
    color: var(--text);
    font-size: 14px;
}

.legal-content ul {
    margin: 0 0 12px;
    padding-left: 22px;
    color: var(--text);
}

.legal-content li {
    margin-bottom: 6px;
}

.legal-content code {
    background: var(--bg-card);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 12px;
    font-family: 'Consolas', 'Monaco', monospace;
    color: var(--accent);
}

.legal-content abbr[title] {
    border-bottom: 1px dotted var(--text-dim);
    text-decoration: none;
    cursor: help;
}

.legal-updated {
    color: var(--text-faint);
    font-size: 12px;
    margin-bottom: 24px;
}

@media (max-width: 720px) {
    .legal-page {
        padding: 20px 14px;
    }
    .legal-content {
        padding: 24px 20px;
    }
}

/* ===== Leaflet dark theme tweaks ===== */

.leaflet-container {
    background: var(--bg-card);
    font-family: inherit;
}

.leaflet-popup-content-wrapper,
.leaflet-tooltip {
    background: var(--bg-panel);
    color: var(--text);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.leaflet-popup-tip {
    background: var(--bg-panel);
}

.leaflet-tooltip {
    padding: 6px 10px;
    font-size: 12px;
    font-weight: 500;
}

.leaflet-tooltip-top:before {
    border-top-color: var(--border);
}

.leaflet-control-attribution {
    background: rgba(21, 27, 35, 0.85) !important;
    color: var(--text-faint) !important;
    font-size: 10px !important;
}

.leaflet-control-attribution a {
    color: var(--text-dim) !important;
}

/* ===== Plotly dark tweaks ===== */

.js-plotly-plot .plotly .modebar {
    background: transparent !important;
}

.js-plotly-plot .plotly .modebar-btn path {
    fill: var(--text-dim) !important;
}

.js-plotly-plot .plotly .modebar-btn:hover path {
    fill: var(--text) !important;
}

.js-plotly-plot .plotly .modebar-btn.active path {
    fill: var(--accent) !important;
}

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

@media (max-width: 1100px) {
    .row-map,
    .row-chart {
        grid-template-columns: 1fr;
    }
    .panel-map,
    .panel-stats-detail,
    .panel-chart,
    .panel-selection {
        min-height: auto;
    }
    #map {
        min-height: 360px;
    }
    .chart {
        min-height: 300px;
    }
}

@media (max-width: 720px) {
    .dashboard {
        padding: 14px 14px 30px;
    }
    .hero-stats {
        grid-template-columns: repeat(2, 1fr);
    }
    .hero-value {
        font-size: 22px;
    }
    .app-header {
        padding: 12px 16px;
    }
    .track-name {
        display: none;
    }
    .panel-header {
        padding: 12px 14px;
    }
    .panel-controls {
        gap: 8px;
    }
}
