/* ==========================================================================
   Design tokens — "Precision Instrument" system. Dark is the default theme;
   :root[data-theme="light"] below overrides for light mode. The toggle in
   the header flips data-theme on <html> (see index.html's no-flash inline
   script and toggleTheme() in app.js).

   Legacy token names (--primary, --success, --danger, --border, --text,
   --text-muted, --primary-dark, --danger-dark, --accent-race) are kept as
   theme-aware aliases so the ~20 existing declarations using them keep
   working without a hunt-and-replace pass; all new rules should use the
   --surface-, --accent, --status- and --text- names directly.
   ========================================================================== */
:root,
:root[data-theme="dark"] {
    /* Surfaces */
    --surface-0: #0B0D10;
    --surface-1: #14171B;
    --surface-2: #1C2027;
    --surface-3: #262B33;
    --surface-hover: #2C323C;
    --border-hairline: rgba(255,255,255,0.08);
    --border-hairline-strong: rgba(255,255,255,0.14);

    /* Text */
    --text-primary: #EDEFF2;
    --text-secondary: #9BA3AF;
    --text-tertiary: #6B7280;
    --text-on-accent: #06201C;

    /* Brand / interactive */
    --accent: #2DD4BF;
    --accent-hover: #5EEAD4;
    --accent-active: #14B8A6;
    --accent-wash: rgba(45, 212, 191, 0.12);
    --accent-wash-strong: rgba(45, 212, 191, 0.22);

    /* Semantic status */
    --status-success: #34D399;
    --status-success-wash: rgba(52, 211, 153, 0.14);
    --status-success-strong: #059669;
    --status-warning: #FBBF24;
    --status-warning-wash: rgba(251, 191, 36, 0.14);
    --status-danger: #F87171;
    --status-danger-wash: rgba(248, 113, 113, 0.14);
    --status-danger-strong: #DC2626;
    --status-info: #60A5FA;

    /* Elevation */
    --shadow-elevated: 0 8px 40px rgba(0,0,0,0.55), 0 2px 8px rgba(0,0,0,0.35);
    --shadow-popover: 0 4px 20px rgba(0,0,0,0.45);

    /* Legacy aliases */
    --primary: var(--accent);
    --primary-dark: var(--accent-active);
    --success: var(--status-success);
    --danger: var(--status-danger);
    --danger-dark: var(--status-danger-strong);
    --border: var(--border-hairline-strong);
    --text: var(--text-primary);
    --text-muted: var(--text-secondary);
    --accent-race: var(--accent);
}

:root[data-theme="light"] {
    --surface-0: #F5F6F8;
    --surface-1: #FFFFFF;
    --surface-2: #FFFFFF;
    --surface-3: #FFFFFF;
    --surface-hover: #EEF0F3;
    --border-hairline: rgba(15,23,42,0.08);
    --border-hairline-strong: rgba(15,23,42,0.14);

    --text-primary: #14171B;
    --text-secondary: #565E6B;
    --text-tertiary: #8A93A1;
    --text-on-accent: #06201C;

    --accent: #0D9488;
    --accent-hover: #0F766E;
    --accent-active: #115E59;
    --accent-wash: rgba(13, 148, 136, 0.08);
    --accent-wash-strong: rgba(13, 148, 136, 0.16);

    --status-success: #059669;
    --status-success-wash: rgba(5, 150, 105, 0.10);
    --status-success-strong: #047857;
    --status-warning: #D97706;
    --status-warning-wash: rgba(217, 119, 6, 0.10);
    --status-danger: #DC2626;
    --status-danger-wash: rgba(220, 38, 38, 0.10);
    --status-danger-strong: #B91C1C;
    --status-info: #2563EB;

    --shadow-elevated: 0 12px 32px rgba(15,23,42,0.14), 0 2px 8px rgba(15,23,42,0.08);
    --shadow-popover: 0 8px 24px rgba(15,23,42,0.12);

    --primary: var(--accent);
    --primary-dark: var(--accent-active);
    --success: var(--status-success);
    --danger: var(--status-danger);
    --danger-dark: var(--status-danger-strong);
    --border: var(--border-hairline-strong);
    --text: var(--text-primary);
    --text-muted: var(--text-secondary);
    --accent-race: var(--accent);
}

:root {
    /* Spacing / radius / motion — same in both themes */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;
    --space-8: 64px;

    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 10px;
    --radius-full: 999px;

    --ease-standard: cubic-bezier(0.2, 0, 0, 1);
    --duration-fast: 120ms;
    --duration-base: 180ms;
    --duration-modal: 220ms;

    --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
    --font-mono: 'IBM Plex Mono', ui-monospace, Menlo, monospace;
}

/* Theme transition: applies to the handful of surfaces most visible when
   toggling, kept short and property-scoped (not `all`) to avoid jank on
   data-heavy screens. */
body, #header, #sidebar, .file-item, .analysis-modal .deviation-modal-content,
.toast, .cmp-btn {
    transition: background-color var(--duration-base) var(--ease-standard),
                border-color var(--duration-base) var(--ease-standard),
                color var(--duration-base) var(--ease-standard);
}

/* Icon sprite usage: <svg class="icon"><use href="#icon-name"/></svg> */
.icon {
    width: 16px;
    height: 16px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.75;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex-shrink: 0;
}
.icon--lg { width: 20px; height: 20px; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: transparent;
    border: 1px solid var(--border-hairline-strong);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
}
.icon-btn:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
}
.icon-btn .icon { width: 18px; height: 18px; }

/* Theme toggle: shows the sun in dark mode (click to go light) and the moon
   in light mode (click to go dark), matching the common sun/moon convention
   of "icon shown = what you'll switch to." */
#themeToggle .icon--moon { display: none; }
:root[data-theme="light"] #themeToggle .icon--sun { display: none; }
:root[data-theme="light"] #themeToggle .icon--moon { display: inline-flex; }
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    font-family: var(--font-sans);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--surface-0);
    color: var(--text-primary);
}
#header {
    background: var(--surface-1);
    color: var(--text-primary);
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-hairline);
    flex-shrink: 0;
}
#header h1 {
    font: 600 18px/1.2 var(--font-sans);
    color: var(--text-primary);
}
#mainContainer {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
}
#sidebar {
    width: 320px;
    background: var(--surface-1);
    border-right: 1px solid var(--border-hairline);
    display: flex;
    flex-direction: column;
    transition: margin-left 0.3s ease;
    position: relative;
    z-index: 100;
}
#sidebar.collapsed {
    margin-left: -320px;
}
#sidebarToggle {
    position: absolute;
    right: -40px;
    top: 80px;
    width: 40px;
    height: 40px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 0 8px 8px 0;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 2px 2px 4px rgba(0,0,0,0.2);
    z-index: 101;
}
#sidebarToggle:hover {
    background: var(--accent-active);
}
#sidebarHeader {
    padding: 15px;
    background: var(--surface-1);
    border-bottom: 1px solid var(--border-hairline-strong);
    flex-shrink: 0;
}
#sidebarHeader h2 {
    font-size: 16px;
    color: var(--text-primary);
    margin-bottom: 10px;
}
/* Prominent primary action — hidden until 2+ routes are selected (see updateUI). */
#compareBtn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 12px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 15px;
    width: 100%;
    box-shadow: 0 2px 6px var(--accent-wash-strong);
    transition: all 0.2s;
    display: none;
}
#compareBtn.visible {
    display: block;
}
#compareBtn:hover {
    background: var(--accent-active);
}
#compareHint {
    margin-top: 10px;
    font-size: 12px;
    color: var(--text-secondary);
    text-align: center;
    display: none;
}
#compareHint.visible {
    display: block;
}
#compactDropZone {
    border: 2px dashed var(--border-hairline-strong);
    border-radius: 6px;
    padding: 12px 10px;
    text-align: center;
    background: var(--surface-2);
    cursor: pointer;
    transition: all 0.2s;
}
#compactDropZone.dragover {
    border-color: var(--accent);
    background: var(--accent-wash);
}
#compactDropZone p {
    color: var(--text-secondary);
    font-size: 12px;
    margin: 0;
}
#compactDropZone input {
    display: none; 
}
#compactDropZone.hidden {
    display: none;
}
#dropZone {
    border: 2px dashed var(--border-hairline-strong);
    border-radius: 8px;
    padding: 30px 20px;
    margin: 15px;
    text-align: center;
    background: var(--surface-1);
    cursor: pointer;
    transition: all 0.3s;
}
#dropZone.dragover {
    border-color: var(--accent);
    background: var(--accent-wash);
}
#dropZone p {
    color: var(--text-secondary);
    font-size: 14px;
    margin-bottom: 8px;
}
#dropZone input {
    display: none;
}
#fileList {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
#fileList::-webkit-scrollbar {
    width: 8px;
}
#fileList::-webkit-scrollbar-track {
    background: var(--surface-2);
}
#fileList::-webkit-scrollbar-thumb {
    background: var(--text-tertiary);
    border-radius: 4px;
}
#fileList::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}
.file-item {
    display: flex;
    flex-direction: column;
    padding: 12px;
    background: var(--surface-1);
    border-radius: 8px;
    border: 2px solid var(--border-hairline-strong);
    transition: all 0.2s;
    cursor: pointer;
    position: relative;
}
.file-item:hover {
    border-color: var(--accent);
    box-shadow: 0 2px 8px var(--accent-wash);
}
.file-item.highlighted {
    border-color: var(--accent);
    background: var(--accent-wash);
    box-shadow: 0 0 0 2px var(--accent-wash-strong);
}
/* Included in the comparison (the default state for every loaded route). */
.file-item.selected {
    border-color: var(--accent);
    background: var(--accent-wash);
}
/* Excluded from the comparison — dimmed so the on/off state reads at a glance. */
.file-item:not(.selected) {
    opacity: 0.6;
    border-style: dashed;
}
.compare-checkbox {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 20px;
    height: 20px;
    cursor: pointer;
    accent-color: var(--accent);
    display: block;
}
.file-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.file-number {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--surface-1);
    border: 2px solid var(--border-hairline-strong);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: bold;
    flex-shrink: 0;
}
.file-color {
    width: 24px;
    height: 24px;
    border-radius: 4px;
    border: 2px solid var(--border-hairline-strong);
    flex-shrink: 0;
}
.file-name-container {
    flex: 1;
    margin-bottom: 8px;
}
.file-display-name {
    font-size: 14px;
    font-weight: 600;
    padding: 4px 6px;
    border-radius: 4px;
    width: 100%;
    border: 1px solid transparent;
    background: var(--surface-2);
    word-wrap: break-word;
    min-height: 28px;
    display: flex;
    align-items: center;
}
.file-display-name:hover {
    border-color: var(--border-hairline-strong);
}
.file-display-name:focus {
    outline: none;
    border-color: var(--accent);
    background: var(--surface-1);
}
.file-original-name {
    font-size: 10px;
    color: var(--text-secondary);
    padding: 2px 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.file-stats {
    background: var(--surface-2);
    border-radius: 4px;
    padding: 6px;
    margin-bottom: 8px;
    font-size: 11px;
}
.stat-row {
    display: flex;
    justify-content: space-between;
    padding: 2px 0;
    border-bottom: 1px solid var(--border-hairline);
}
.stat-row:last-child {
    border-bottom: none;
}
.stat-label {
    color: var(--text-secondary);
    font-weight: 500;
}
.stat-value {
    font-family: var(--font-mono);
    font-weight: 600;
    color: var(--accent);
}
.file-actions {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}
.file-play {
    background: var(--accent);
    color: white;
    border: none;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
    flex: 1;
    font-weight: 500;
}
.file-play:hover {
    background: var(--accent-active);
}
.file-play.playing {
    background: var(--status-danger);
}
.file-toggle {
    background: var(--status-success);
    color: white;
    border: none;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
    flex: 1;
    font-weight: 500;
}
.file-toggle.hidden-route {
    background: var(--text-tertiary);
}
.file-toggle:hover {
    opacity: 0.8;
}
.file-remove {
    background: var(--status-danger);
    color: white;
    border: none;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
    flex: 1;
    font-weight: 500;
}
.file-remove:hover {
    background: var(--status-danger-strong);
}
#mapContainer {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
}
#map {
    flex: 1;
    width: 100%;
}
.hidden {
    display: none !important;
}
#routeTooltip {
    position: absolute;
    background: var(--surface-2);
    color: var(--text-primary);
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 12px;
    pointer-events: none;
    z-index: 1000;
    display: none;
    white-space: nowrap;
    box-shadow: var(--shadow-popover);
    backdrop-filter: blur(10px);
    border: 1px solid var(--accent-wash);
    line-height: 1.6;
}
#routeTooltip strong {
    color: var(--accent);
    font-weight: 600;
}
#chartCrosshair {
    position: fixed;
    background: var(--surface-2);
    color: var(--text-primary);
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 12px;
    pointer-events: none;
    z-index: 9999;
    display: none;
    white-space: nowrap;
    box-shadow: var(--shadow-popover);
    backdrop-filter: blur(10px);
    border: 1px solid var(--accent-wash);
    max-width: 300px;
}
#chartCrosshair .crosshair-distance {
    font-weight: 600;
    margin-bottom: 6px;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border-hairline-strong);
    color: var(--accent);
}
#chartCrosshair .crosshair-route {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 4px 0;
}
#chartCrosshair .crosshair-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
    flex-shrink: 0;
}
#chartCrosshair .crosshair-value {
    font-weight: 600;
}
.wayback-control {
    margin: 10px;
    display: flex;
    gap: 6px;
}
.wayback-select {
    padding: 8px 12px;
    border-radius: 4px;
    border: none;
    background: white;
    color: #333;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

#playbackControls {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: var(--surface-1);
    padding: 12px 16px;
    border-radius: 8px;
    box-shadow: var(--shadow-elevated);
    z-index: 210; /* above the comparison panel (200) so it can't be covered */
    display: none;
    align-items: center;
    gap: 10px;
}
#playbackControls.show {
    display: flex;
}
#stopPlaybackBtn {
    background: var(--danger);
    color: white;
    border: none;
    padding: 6px 14px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    transition: background 0.15s;
}
#stopPlaybackBtn:hover {
    background: var(--danger-dark);
}
.speed-label {
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 500;
}
#comparisonPanel {
    position: fixed;
    bottom: 0;
    left: 320px;
    right: 0;
    background: var(--surface-1);
    border-top: 2px solid var(--accent);
    box-shadow: var(--shadow-popover);
    z-index: 200;
    max-height: 50vh;
    display: flex;
    flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.3s ease, left 0.3s ease;
}
body:has(#sidebar.collapsed) #comparisonPanel {
    left: 0;
}
#comparisonPanel.show {
    transform: translateY(0);
}
.comparison-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border-hairline-strong);
    flex-shrink: 0;
}
.comparison-header h3 {
    color: var(--accent);
    font-size: 16px;
    font-weight: 600;
}
.comparison-actions {
    display: flex;
    align-items: flex-end;
    gap: 16px;
    flex-wrap: wrap;
}
/* Buttons are grouped (Charts / Analyse / Actions) with a small caption so the
   row reads as three clusters rather than twelve competing controls. */
.cmp-group {
    display: flex;
    align-items: center;
    gap: 4px;
    position: relative;
}
.cmp-group--actions {
    margin-left: auto;
}
.cmp-group-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-right: 4px;
    align-self: center;
}
/* Charts/Analyse/Validate collapse into popover menus (see
   setupComparisonMenubar in app.js) so the toolbar reads as three clear
   triggers instead of 17 competing buttons, and never overflows narrow
   viewports. Race/Export/Close stay inline — they're single actions, not
   groups. */
.cmp-menu-trigger {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--border-hairline-strong);
    background: var(--surface-1);
    color: var(--text-primary);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    transition: all 0.15s;
}
.cmp-menu-trigger .icon {
    width: 14px;
    height: 14px;
    transition: transform 0.15s var(--ease-standard);
}
.cmp-menu-trigger:hover {
    border-color: var(--accent);
    color: var(--accent);
}
.cmp-group--menu.open .cmp-menu-trigger {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-wash);
}
.cmp-group--menu.open .cmp-menu-trigger .icon {
    transform: rotate(180deg);
}
.cmp-menu-panel {
    display: none;
    position: absolute;
    bottom: calc(100% + 6px);
    left: 0;
    flex-direction: column;
    gap: 4px;
    min-width: 200px;
    padding: 8px;
    background: var(--surface-2);
    border: 1px solid var(--border-hairline-strong);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-popover);
    z-index: 20;
}
.cmp-group--menu.open .cmp-menu-panel {
    display: flex;
}
.cmp-menu-panel .cmp-btn {
    width: 100%;
    text-align: left;
}
/* One coherent button; variants change only color/emphasis, not shape. */
.cmp-btn {
    border: 1px solid transparent;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    transition: all 0.15s;
    line-height: 1.4;
}
.cmp-btn:disabled {
    background: var(--surface-2);
    color: var(--text-tertiary);
    border-color: transparent;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}
/* Charts — the six metric buttons, a flat primary segment group. */
.cmp-btn--metric {
    background: var(--primary);
    color: white;
}
.cmp-btn--metric:hover:not(:disabled) {
    background: var(--primary-dark);
}
/* Analyse — neutral secondary. */
.cmp-btn--analyse {
    background: var(--surface-1);
    color: var(--text);
    border-color: var(--border);
}
.cmp-btn--analyse:hover:not(:disabled) {
    border-color: var(--primary);
    color: var(--primary);
}
/* The single colourful moment: Race. */
.cmp-btn--primary {
    background: var(--accent-race);
    color: white;
}
.cmp-btn--primary:hover:not(:disabled) {
    filter: brightness(0.95);
    box-shadow: 0 2px 8px var(--accent-wash-strong);
}
/* Export — quiet ghost button. */
.cmp-btn--ghost {
    background: transparent;
    color: var(--text-muted);
}
.cmp-btn--ghost:hover:not(:disabled) {
    background: var(--surface-2);
    color: var(--text);
}
/* Close — a neutral icon, not a red destructive-looking button. */
.cmp-btn--icon {
    background: transparent;
    color: var(--text-muted);
    font-size: 20px;
    line-height: 1;
    padding: 4px 8px;
}
.cmp-btn--icon:hover:not(:disabled) {
    background: var(--surface-2);
    color: var(--text);
}
.comparison-content {
    flex: 1;
    overflow-y: auto;
    padding: 15px 20px;
}
.comparison-content::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}
.comparison-content::-webkit-scrollbar-track {
    background: var(--surface-2);
}
.comparison-content::-webkit-scrollbar-thumb {
    background: var(--text-tertiary);
    border-radius: 4px;
}
.comparison-content::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}
th, td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid var(--border-hairline-strong);
}
th {
    background: var(--surface-2);
    font-weight: 600;
    color: var(--text-secondary);
    position: sticky;
    top: 0;
}
tr:hover {
    background: var(--surface-2);
}
.route-name-cell {
    display: flex;
    align-items: center;
    gap: 10px;
}
.route-color-indicator {
    width: 16px;
    height: 16px;
    border-radius: 3px;
    flex-shrink: 0;
}
#elevationModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
#elevationModal.show {
    display: flex;
}
.modal-content {
    background: var(--surface-1);
    border-radius: 8px;
    padding: 20px;
    max-width: 90%;
    width: 1000px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: var(--shadow-elevated);
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--border-hairline-strong);
}
.modal-header h2 {
    font-size: 18px;
    color: var(--accent);
    font-weight: 600;
}
.modal-close {
    background: var(--status-danger);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 500;
}
.modal-close:hover {
    background: var(--status-danger-strong);
}
#elevationChart {
    width: 100%;
    height: 400px;
    border: 1px solid var(--border-hairline-strong);
    border-radius: 4px;
    cursor: crosshair;
}
#elevationChart.drag-mode {
    cursor: grab;
}
#elevationChart.dragging {
    cursor: grabbing;
}
.chart-controls {
    margin-top: 10px;
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}
.chart-control-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
}
.chart-control-btn:hover {
    background: var(--accent-active);
}
.chart-control-btn:disabled {
    background: var(--border-hairline-strong);
    cursor: not-allowed;
}
.chart-control-btn.active {
    background: var(--status-success);
}
.chart-control-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    background: var(--surface-2);
    border: 1px solid var(--border-hairline);
}
.chart-control-toggle:hover {
    background: var(--surface-3);
}
.chart-control-toggle input[type="checkbox"] {
    margin: 0;
}
.chart-legend {
    margin-top: 15px;
    padding: 10px;
    background: var(--surface-2);
    border-radius: 4px;
}
.legend-item {
    display: inline-flex;
    align-items: center;
    margin-right: 20px;
    margin-bottom: 5px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    transition: background 0.2s;
}
.legend-item:hover {
    background: var(--accent-wash);
}
.legend-item.active {
    background: var(--accent-wash);
    box-shadow: 0 0 0 2px var(--accent);
}
.legend-color {
    width: 30px;
    height: 3px;
    margin-right: 8px;
}
.legend-label {
    font-size: 13px;
    color: var(--text-primary);
}
.legend-offset {
    font-size: 11px;
    color: var(--text-secondary);
    margin-left: 8px;
    font-style: italic;
}
.chart-control-slider {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 500;
}
.chart-control-slider input[type="range"] {
    width: 80px;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border-hairline-strong);
    border-radius: 2px;
    cursor: pointer;
}
.chart-control-slider input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}
.battery-stats {
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-secondary);
}
.range-stats-panel {
    margin-top: 8px;
    padding: 10px;
    background: var(--surface-2);
    border-radius: 4px;
    border: 1px solid var(--accent-wash);
}
.range-stats-grid {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.range-stats-route {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 4px 8px;
    font-size: 12px;
    color: var(--text-primary);
}
.range-stats-route strong {
    font-size: 13px;
}
.range-stats-route span {
    color: var(--text-secondary);
}
#speedSlider {
    -webkit-appearance: none;
    appearance: none;
    height: 6px;
    border-radius: 3px;
    background: linear-gradient(to right, var(--border-hairline-strong) 0%, var(--accent) 0%);
    outline: none;
}
#speedSlider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
#speedSlider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* Insights Modal Styles */
#insightsModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
#insightsModal.show {
    display: flex;
}
.insights-modal-content {
    background: var(--surface-1);
    border-radius: 8px;
    padding: 20px;
    max-width: 95%;
    width: 1100px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-elevated);
}
.insights-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--border-hairline-strong);
}
.insights-modal-header h2 {
    font-size: 18px;
    color: var(--accent);
    font-weight: 600;
}
.insights-tabs {
    display: flex;
    gap: 5px;
    margin-bottom: 20px;
    border-bottom: 2px solid var(--border-hairline-strong);
    overflow-x: auto;
    flex-wrap: wrap;
}
.insights-tab {
    background: transparent;
    border: none;
    padding: 10px 16px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    border-bottom: 3px solid transparent;
    transition: all 0.2s;
    white-space: nowrap;
}
.insights-tab:hover {
    background: var(--surface-2);
    color: var(--accent);
}
.insights-tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}
.insights-content {
    min-height: 500px;
    max-height: 500px;
    overflow-y: auto;
}
.insights-tab-panel {
    display: none;
    height: 100%;
    overflow-y: auto;
}
.insights-tab-panel.active {
    display: block;
}
.insights-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 15px;
    margin-top: 10px;
    padding-bottom: 10px;
}
.insight-card {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 8px;
    padding: 16px;
    color: white;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    transition: transform 0.2s, box-shadow 0.2s;
}
.insight-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.insight-card.positive {
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
}
.insight-card.negative {
    background: linear-gradient(135deg, #ee0979 0%, #ff6a00 100%);
}
.insight-card.neutral {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}
.insight-card.warning {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}
.insight-icon {
    font-size: 28px;
    margin-bottom: 8px;
}
.insight-title {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 6px;
    opacity: 0.95;
}
.insight-value {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 6px;
}
.insight-description {
    font-size: 13px;
    opacity: 0.9;
    line-height: 1.4;
}

/* Zone Bar Styles */
.zone-card .insight-value {
    font-size: 16px;
}
.zone-bar {
    display: flex;
    height: 24px;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 6px;
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
}
.zone-segment {
    transition: all 0.2s;
}
.zone-segment:hover {
    filter: brightness(1.1);
}
.zone-labels {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    font-weight: 600;
}
.zone-label {
    text-shadow: 0 1px 2px rgba(0,0,0,0.3);
}

/* Effort Score Card */
.effort-card {
    text-align: center;
}
.effort-score-display {
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 8px;
}
.effort-number {
    font-size: 42px;
    font-weight: 800;
    line-height: 1;
}
.effort-category {
    font-size: 18px;
    font-weight: 600;
    opacity: 0.9;
}

.metric-insights-text {
    background: var(--surface-2);
    border-radius: 8px;
    padding: 20px 24px;
    margin-top: 20px;
    line-height: 1.8;
    color: var(--text-primary);
    border-left: 4px solid var(--accent);
}

.metric-insights-text p {
    margin: 0 0 16px 0;
    font-size: 14px;
}

.metric-insights-text p:last-child {
    margin-bottom: 0;
}

.metric-insights-text strong {
    color: var(--accent);
    font-weight: 600;
}

.metric-insights-text em {
    color: var(--text-secondary);
    font-style: normal;
    font-size: 13px;
}

#insightsChart {
    width: 100%;
    height: 400px;
    border: 1px solid var(--border-hairline-strong);
    border-radius: 4px;
    cursor: crosshair;
}
#insightsChart.drag-mode {
    cursor: grab;
}
#insightsChart.dragging {
    cursor: grabbing;
}
#insightsCrosshair {
    position: fixed;
    background: var(--surface-2);
    color: var(--text-primary);
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 12px;
    pointer-events: none;
    z-index: 9999;
    display: none;
    white-space: nowrap;
    box-shadow: var(--shadow-popover);
    backdrop-filter: blur(10px);
    border: 1px solid var(--accent-wash);
    max-width: 300px;
}
#insightsCrosshair .crosshair-distance {
    font-weight: 600;
    margin-bottom: 6px;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border-hairline-strong);
    color: var(--accent);
}
#insightsCrosshair .crosshair-route {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 4px 0;
}
#insightsCrosshair .crosshair-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
    flex-shrink: 0;
}
#insightsCrosshair .crosshair-value {
    font-weight: 600;
}
.insights-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}
.insights-btn:hover {
    background: var(--accent-active);
}
.heatmap-btn {
    background: linear-gradient(135deg, #34A853 0%, #FBBC04 50%, #EA4335 100%);
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
    text-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.heatmap-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.heatmap-btn.active {
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
    transform: translateY(1px);
}

/* Firebase Authentication UI */
#authContainer {
    display: flex;
    align-items: center;
    gap: 15px;
    margin-left: auto;
}
#signInBtn {
    background: var(--surface-1);
    color: var(--accent);
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s;
}
#signInBtn:hover {
    background: var(--surface-hover);
}
#userInfo {
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--text-primary);
}
#userAvatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid var(--border-hairline-strong);
}
#userName {
    font-size: 14px;
    font-weight: 500;
}
#signOutBtn {
    background: var(--surface-hover);
    color: var(--text-primary);
    border: 1px solid var(--border-hairline-strong);
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: all 0.2s;
}
#signOutBtn:hover {
    background: var(--surface-3);
}
.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
}

#saveCurrentBtn {
    background: var(--status-success);
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    width: 100%;
    margin-top: 10px;
    transition: all 0.2s;
    display: none;
}
#saveCurrentBtn.visible {
    display: block;
}
#saveCurrentBtn:hover {
    background: var(--status-success-strong);
}
#saveCurrentBtn:disabled {
    background: var(--border-hairline-strong);
    cursor: not-allowed;
}

/* --- Session buttons (Save / Load) sit side by side --- */
#sessionButtons {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}
#sessionButtons button {
    flex: 1;
    margin-top: 0;
}
#loadSessionBtn {
    background: var(--surface-1);
    color: var(--accent);
    border: 1px solid var(--accent);
    padding: 8px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    transition: all 0.2s;
    display: none;
}
#loadSessionBtn.visible {
    display: block;
}
#loadSessionBtn:hover {
    background: var(--accent-wash);
}

/* --- Saved Sessions modal --- */
#sessionsModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
#sessionsModal.show {
    display: flex;
}
.sessions-modal-content {
    background: var(--surface-1);
    border-radius: 8px;
    padding: 20px;
    max-width: 95%;
    width: 480px;
    max-height: 80vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-elevated);
}
.sessions-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
}
.sessions-modal-header h2 {
    font-size: 18px;
    color: var(--text-primary);
}
#savedSessionsList {
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
#sessionsEmptyState {
    color: var(--text-secondary);
    font-size: 14px;
    text-align: center;
    padding: 20px 0;
}
.saved-session-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--border-hairline-strong);
    border-radius: 8px;
    transition: all 0.2s;
}
.saved-session-item:hover {
    border-color: var(--accent);
    background: var(--accent-wash);
}
.session-info {
    min-width: 0;
}
.session-name {
    font-weight: 500;
    color: var(--text-primary);
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.session-meta {
    color: var(--text-secondary);
    font-size: 12px;
    margin-top: 2px;
}
.session-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}
.session-load-btn,
.session-share-btn,
.session-delete-btn {
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    border: none;
    transition: all 0.2s;
}
.session-load-btn {
    background: var(--accent);
    color: white;
}
.session-load-btn:hover {
    background: var(--accent-active);
}
.session-share-btn {
    background: var(--surface-hover);
    color: var(--text-primary);
}
.session-share-btn:hover {
    background: var(--surface-2);
}
.session-delete-btn {
    background: var(--surface-hover);
    color: var(--status-danger);
}
.session-delete-btn:hover {
    background: var(--status-danger-wash);
}
.loading-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid var(--surface-2);
    border-top: 2px solid var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}
    

/* Analysis Modal Styles — shared by every data-table modal (Deviation,
   Distance Drift, Running Dynamics, Session Self-Check, HR/Cadence
   Validation, Dropout Diagnostics). Add the "analysis-modal" class to a new
   modal's outer <div> and "analysis-table" to its <table> to get all of this
   for free — deliberately class-based, not #id-based, so a new modal can't
   silently end up unstyled or with a CSS-specificity collision the way the
   REF badge once did. */
.analysis-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
.analysis-modal.show {
    display: flex;
}
.deviation-modal-content {
    background: var(--surface-1);
    border-radius: 8px;
    padding: 20px;
    max-width: 95%;
    width: 900px;
    max-height: 85vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-elevated);
}
.deviation-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--border-hairline-strong);
    flex-shrink: 0;
}
.deviation-modal-header h2 {
    font-size: 18px;
    color: var(--accent);
    font-weight: 600;
    margin: 0;
}
.deviation-modal-hint {
    font-size: 13px;
    color: var(--text-secondary);
    margin: 0 0 15px;
}
.table-caption {
    caption-side: top;
    text-align: left;
    font-size: 12px;
    line-height: 1.5;
    color: var(--text-secondary);
    padding: 0 0 10px;
}
.deviation-table-container {
    flex: 1;
    overflow: auto;
}
.analysis-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}
.analysis-table th,
.analysis-table td {
    padding: 10px 12px;
    text-align: center;
    border-bottom: 1px solid var(--border-hairline-strong);
    white-space: nowrap;
}
.analysis-table .route-header-content {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}
.analysis-table .route-color-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}
.analysis-table .value-positive,
.analysis-table .drift-positive {
    font-family: var(--font-mono);
    color: var(--status-warning);
}
.analysis-table .value-negative,
.analysis-table .drift-negative {
    font-family: var(--font-mono);
    color: var(--accent);
}
.analysis-table .confidence-badge {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    border-radius: 4px;
    padding: 1px 5px;
}
.analysis-table .confidence-high {
    color: var(--status-success);
    border: 1px solid var(--status-success);
    background: var(--status-success-wash);
}
.analysis-table .confidence-medium {
    color: var(--status-warning);
    border: 1px solid var(--status-warning);
    background: var(--status-warning-wash);
}
.analysis-table .confidence-low {
    color: var(--status-danger);
    border: 1px solid var(--status-danger);
    background: var(--status-danger-wash);
}
.analysis-table .deviation-heatmap-toggle {
    font-size: 12px;
    padding: 4px 10px;
    border-radius: 4px;
    border: 1px solid var(--border-hairline-strong);
    background: var(--surface-1);
    cursor: pointer;
}
.analysis-table .deviation-heatmap-toggle.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Splits Modal Styles */
#splitsModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
#splitsModal.show {
    display: flex;
}
.splits-modal-content {
    background: var(--surface-1);
    border-radius: 8px;
    padding: 20px;
    max-width: 95%;
    width: 900px;
    max-height: 85vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-elevated);
}
.splits-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--border-hairline-strong);
    flex-shrink: 0;
}
.splits-modal-header h2 {
    font-size: 18px;
    color: var(--accent);
    font-weight: 600;
    margin: 0;
}
.splits-table-container {
    flex: 1;
    overflow: auto;
}
#splitsTable {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
#splitsTable th,
#splitsTable td {
    padding: 10px 12px;
    text-align: center;
    border-bottom: 1px solid var(--border-hairline-strong);
    white-space: nowrap;
}
#splitsTable thead {
    position: sticky;
    top: 0;
    background: var(--surface-1);
    z-index: 10;
}
#splitsTable th {
    background: var(--surface-2);
    font-weight: 600;
    color: var(--text-secondary);
}
.split-number-header {
    background: var(--accent) !important;
    color: white !important;
    min-width: 70px;
}
.route-header {
    background: var(--accent-wash) !important;
    border-left: 2px solid var(--border-hairline-strong);
}
.route-header-content {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
.route-color-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}
.metric-header {
    font-size: 11px;
    color: var(--text-secondary) !important;
    background: var(--surface-2) !important;
}
.split-number {
    font-family: var(--font-mono);
    font-weight: 600;
    color: var(--accent);
    background: var(--surface-2);
    position: sticky;
    left: 0;
    z-index: 5;
}
.split-pace {
    font-family: var(--font-mono);
    color: var(--accent);
    font-weight: 500;
}
.split-elev {
    font-family: var(--font-mono);
    color: var(--status-success);
}
.split-hr {
    font-family: var(--font-mono);
    color: var(--status-danger);
}
.split-na {
    color: var(--text-tertiary);
}
.split-time {
    font-family: var(--font-mono);
    color: var(--text-secondary);
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}
.split-gap {
    font-family: var(--font-mono);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}
.split-gap-ahead {
    color: var(--status-success);
    background: var(--status-success-wash);
}
.split-gap-behind {
    color: var(--status-danger);
    background: var(--status-danger-wash);
}
.split-gap-even {
    color: var(--text-secondary);
}
.time-header {
    color: var(--text-secondary) !important;
}
.gap-header {
    color: var(--accent) !important;
    font-weight: 600 !important;
}
.reference-badge {
    display: inline-block;
    background: var(--accent);
    color: white;
    font-size: 9px;
    font-weight: 600;
    padding: 2px 5px;
    border-radius: 3px;
    margin-left: 6px;
    vertical-align: middle;
}
.partial-split {
    font-style: italic;
    opacity: 0.7;
}
.totals-row {
    background: var(--accent-wash) !important;
    border-top: 2px solid var(--accent);
}
.totals-row td {
    background: var(--accent-wash);
}
#splitsTable tbody tr:hover {
    background: var(--surface-2);
}
#splitsTable tbody tr:hover .split-number {
    background: var(--accent-wash);
}

/* Segment Modal Styles */
#segmentModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
#segmentModal.show {
    display: flex;
}
.segment-modal-content {
    background: var(--surface-1);
    border-radius: 8px;
    padding: 20px;
    max-width: 95%;
    width: 700px;
    max-height: 85vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-elevated);
}
.segment-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--border-hairline-strong);
    flex-shrink: 0;
}
.segment-modal-header h2 {
    font-size: 18px;
    color: var(--accent);
    font-weight: 600;
    margin: 0;
}
.segment-input-section {
    background: var(--surface-2);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
}
.segment-inputs {
    display: flex;
    gap: 20px;
    margin-bottom: 15px;
    flex-wrap: wrap;
}
.segment-inputs label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--text-primary);
}
.segment-inputs label span {
    font-weight: 500;
}
.segment-inputs input {
    width: 80px;
    padding: 8px 12px;
    border: 2px solid var(--border-hairline-strong);
    border-radius: 6px;
    font-size: 14px;
    text-align: center;
}
.segment-inputs input:focus {
    outline: none;
    border-color: var(--accent);
}
.segment-inputs .unit {
    color: var(--text-secondary);
    font-weight: normal;
}
.analyze-segment-btn {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: white;
    border: none;
    padding: 10px 24px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    transition: all 0.2s;
}
.analyze-segment-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(245, 87, 108, 0.4);
}
.segment-results {
    flex: 1;
    overflow: auto;
}
.segment-header-info {
    margin-bottom: 15px;
    font-size: 16px;
    color: var(--text-primary);
}
.segment-header-info strong {
    color: var(--accent);
}
.segment-distance {
    color: var(--text-secondary);
    margin-left: 8px;
}
.segment-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}
.segment-table th,
.segment-table td {
    padding: 12px 15px;
    text-align: center;
    border-bottom: 1px solid var(--border-hairline-strong);
}
.segment-table th {
    background: var(--surface-2);
    font-weight: 600;
    color: var(--text-secondary);
}
.segment-table .route-cell {
    display: flex;
    align-items: center;
    gap: 10px;
    text-align: left;
}
.segment-table .time-cell {
    font-family: var(--font-mono);
    color: var(--accent);
    font-weight: 600;
}
.segment-table .pace-cell {
    font-family: var(--font-mono);
    color: var(--accent);
}
.segment-table .elev-cell {
    font-family: var(--font-mono);
    color: var(--status-success);
}
.segment-table .hr-cell {
    font-family: var(--font-mono);
    color: var(--status-danger);
}
.segment-table .diff-row {
    background: var(--accent-wash);
    border-top: 2px solid var(--accent);
}
.segment-table .diff-row td {
    font-weight: 500;
}
.segment-table .faster {
    color: var(--status-success) !important;
}
.segment-table .slower {
    color: var(--status-danger) !important;
}

/* --- Toast notifications (replaces blocking alert()) --- */
#toastContainer {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    z-index: 3000;
    pointer-events: none;
    width: max-content;
    max-width: 90vw;
}
.toast {
    pointer-events: auto;
    cursor: pointer;
    background: #323232;
    color: white;
    padding: 12px 18px;
    border-radius: 8px;
    font-size: 14px;
    line-height: 1.4;
    box-shadow: 0 4px 16px rgba(0,0,0,0.25);
    max-width: 90vw;
    opacity: 0;
    transform: translateY(12px);
    transition: opacity 0.2s ease, transform 0.2s ease;
    border-left: 4px solid var(--primary);
    /* Multi-route summaries (e.g. Auto-Align) use \n between entries instead
       of growing one line wider per route. */
    white-space: pre-line;
}
.toast--visible {
    opacity: 1;
    transform: translateY(0);
}
.toast--success { border-left-color: var(--success); }
.toast--error   { border-left-color: var(--danger); }
.toast--warning { border-left-color: var(--status-warning); }
.toast--info    { border-left-color: var(--primary); }

/* --- Accessibility: visible keyboard focus everywhere --- */
:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

/* ============================================================
   Responsive — phones/tablets (<=768px). Desktop is unaffected.
   ============================================================ */
@media (max-width: 768px) {
    body { height: 100dvh; }

    /* Compact single-line header */
    #header { padding: 8px 12px; }
    .header-content { gap: 10px; }
    #header h1 {
        font-size: 15px;
        flex: 1;
        min-width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    #authContainer { gap: 8px; flex-shrink: 0; }
    #signInBtn { padding: 6px 10px; font-size: 13px; white-space: nowrap; }
    #userName { display: none; }

    /* Sidebar: full-width sheet over the map */
    #sidebar {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
        max-width: none;
        border-right: none;
        z-index: 300;
    }
    #sidebar.collapsed { margin-left: -100vw; }

    /* Open: toggle becomes a Close button inside the sheet header */
    #sidebarToggle {
        position: absolute;
        top: 12px;
        right: 12px;
        left: auto;
        width: auto;
        height: 36px;
        padding: 0 14px;
        font-size: 13px;
        font-weight: 600;
        border-radius: 8px;
        box-shadow: none;
        z-index: 301;
    }
    /* Closed: floating "Routes" pill below the header */
    #sidebar.collapsed #sidebarToggle {
        position: fixed;
        top: 56px;
        left: 12px;
        right: auto;
        height: 44px;
        border-radius: 999px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.35);
    }

    /* Comparison panel: compact so the map stays visible */
    #comparisonPanel {
        left: 0;
        max-height: 45vh;
        padding-bottom: env(safe-area-inset-bottom);
    }
    .comparison-header {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 10px 12px;
    }
    .comparison-actions { gap: 8px; }
    .cmp-group--actions { margin-left: 0; }

    /* Touch targets — nothing below ~40px. */
    .cmp-btn,
    .file-actions button,
    .session-load-btn,
    .session-share-btn,
    .session-delete-btn,
    #stopPlaybackBtn,
    .chart-control-btn { min-height: 40px; }
    .cmp-btn--icon { min-width: 40px; }

    /* Modals fill the screen with tighter padding. */
    .modal-content,
    .insights-modal-content,
    .splits-modal-content,
    .segment-modal-content,
    .sessions-modal-content {
        width: 100%;
        max-width: 100%;
        max-height: 92vh;
        padding: 15px;
    }

    /* Wide data tables scroll instead of overflowing the page. */
    .comparison-content,
    .splits-table-container { overflow-x: auto; }

    #playbackControls { left: 20px; right: 20px; bottom: 12px; justify-content: center; }
}

/* --- Drop zone first-run polish (Fable L4) --- */
#dropZone .dropzone-icon { font-size: 32px; margin-bottom: 8px; }
#dropZone .dropzone-title { color: var(--text); font-weight: 600; font-size: 15px; margin-bottom: 4px; }
#dropZone .dropzone-sub { color: var(--text-muted); font-size: 13px; margin-bottom: 10px; }
#dropZone .dropzone-note { color: var(--text-muted); font-size: 11px; }
/* Rename affordance: the display name is editable — show a text caret. */
.file-display-name { cursor: text; }

/* --- Legibility bumps (Fable L2) + sessions loading state --- */
.file-original-name { font-size: 11px; }
.file-stats { font-size: 12px; }
.stat-value { font-variant-numeric: tabular-nums; }
.sessions-loading {
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
    padding: 24px 0;
}

/* Photo section */
#photoSection {
    margin-top: 12px;
    padding: 0 8px;
}
#photoDropZone {
    border: 1px dashed var(--border-hairline-strong);
    border-radius: 6px;
    padding: 10px;
    text-align: center;
    cursor: pointer;
    font-size: 12px;
    color: var(--text-secondary);
    transition: border-color 0.2s;
}
#photoDropZone:hover, #photoDropZone.drag-over {
    border-color: var(--accent);
    color: var(--accent);
}
#photoDropZone input { display: none; }
#photoGallery {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}
.photo-thumb {
    position: relative;
    width: 56px;
    height: 56px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
}
.photo-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.photo-remove {
    position: absolute;
    top: 1px;
    right: 1px;
    width: 18px;
    height: 18px;
    border: none;
    background: rgba(0,0,0,0.6);
    color: white;
    font-size: 12px;
    line-height: 18px;
    text-align: center;
    border-radius: 50%;
    cursor: pointer;
    padding: 0;
    display: none;
}
.photo-thumb:hover .photo-remove { display: block; }

/* Photo lightbox */
.photo-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.85);
    z-index: 3000;
    display: flex;
    align-items: center;
    justify-content: center;
}
.photo-lightbox-content {
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    align-items: center;
}
.photo-lightbox-content img {
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
    border-radius: 4px;
}
.photo-lightbox-nav {
    margin-top: 12px;
    display: flex;
    align-items: center;
    gap: 16px;
    color: white;
    font-size: 13px;
}
.photo-nav-btn {
    background: rgba(255,255,255,0.15);
    border: none;
    color: white;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    font-size: 18px;
    cursor: pointer;
}
.photo-nav-btn:hover { background: rgba(255,255,255,0.3); }

/* Session tags */
.session-tag {
    display: inline-block;
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 10px;
    background: var(--accent);
    color: white;
    margin-right: 4px;
    margin-top: 3px;
}
.session-tags { margin-top: 2px; }

/* Library tabs */
.library-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 12px;
    overflow-x: auto;
    flex-shrink: 0;
}
.library-tab {
    padding: 4px 10px;
    border: 1px solid var(--border-hairline);
    border-radius: 14px;
    background: var(--surface-2);
    color: var(--text-secondary);
    font-size: 12px;
    cursor: pointer;
    white-space: nowrap;
}
.library-tab:hover { background: var(--surface-3); }
.library-tab.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Session search */
.session-search {
    flex: 1;
    min-width: 0;
    margin: 0 10px;
    padding: 5px 10px;
    border: 1px solid var(--border-hairline);
    border-radius: 4px;
    font-size: 13px;
    background: var(--surface-2);
    color: var(--text-primary);
}

/* Save session modal */
#saveSessionModal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}
.save-session-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.save-session-form label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13px;
    color: var(--text-secondary);
}
.save-session-form input,
.save-session-form textarea {
    padding: 6px 10px;
    border: 1px solid var(--border-hairline);
    border-radius: 4px;
    font-size: 13px;
    background: var(--surface-2);
    color: var(--text-primary);
    font-family: inherit;
}
.save-session-form textarea { resize: vertical; }
