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

:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f3460;
    --accent: #e94560;
    --accent-hover: #ff6b6b;
    --text-primary: #ffffff;
    --text-secondary: #a0a0a0;
    --success: #4ade80;
    --warning: #fbbf24;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
}

#app {
    max-width: 480px;
    margin: 0 auto;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.header {
    padding: 1rem;
    text-align: center;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--bg-tertiary);
}

.header h1 {
    font-size: 1.5rem;
    font-weight: 600;
}

.nav {
    display: flex;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--bg-tertiary);
}

.nav-btn {
    flex: 1;
    padding: 0.75rem;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
    border-bottom: 2px solid transparent;
}

.nav-btn.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.view {
    display: none;
    flex: 1;
    flex-direction: column;
}

.view.active {
    display: flex;
}

/* Map */
#map {
    flex: 1;
    min-height: 300px;
    background: var(--bg-tertiary);
}

/* Stats Panel */
.stats-panel {
    display: flex;
    justify-content: space-around;
    padding: 0.6rem 0.5rem;
    background: var(--bg-secondary);
    border-top: 1px solid var(--bg-tertiary);
}

.stat {
    text-align: center;
}

.stat-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

.stat-label {
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

/* Controls */
.controls {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-secondary);
}

.control-btn {
    flex: 1;
    padding: 0.65rem;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

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

.control-btn.start:hover {
    background: var(--accent-hover);
}

.control-btn.pause {
    background: var(--warning);
    color: #000;
}

.control-btn.resume {
    background: var(--success);
    color: #000;
}

.control-btn.stop {
    background: var(--bg-tertiary);
    color: white;
    border: 2px solid var(--accent);
}

.hidden {
    display: none !important;
}

/* GPS Status */
.gps-status {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.35rem;
    background: var(--bg-tertiary);
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.gps-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--warning);
    animation: pulse 1.5s infinite;
}

.gps-dot.active {
    background: var(--success);
}

.gps-dot.error {
    background: var(--accent);
    animation: none;
}

.track-queue-status {
    text-align: center;
    padding: 0.5rem 0.75rem 0.75rem;
    background: var(--bg-tertiary);
    color: var(--warning);
    font-size: 0.8rem;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* History View */
.history-toolbar {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.75rem 1rem 0;
}

.history-toolbar label {
    font-size: 0.78rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

#history-sort {
    flex: 1;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--bg-primary);
    border-radius: 8px;
    padding: 0.55rem 0.7rem;
    font-size: 0.9rem;
}

.upload-queue-panel {
    margin: 0.75rem 1rem 0;
    border-radius: 10px;
    padding: 0.75rem;
    background: var(--bg-secondary);
    border: 1px solid rgba(251, 191, 36, 0.25);
}

.upload-queue-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    justify-content: space-between;
}

#upload-queue-summary {
    font-size: 0.8rem;
    color: var(--warning);
    line-height: 1.3;
}

#retry-upload-btn {
    border: none;
    border-radius: 6px;
    background: var(--warning);
    color: #111;
    padding: 0.4rem 0.7rem;
    font-size: 0.78rem;
    font-weight: 700;
    cursor: pointer;
}

#retry-upload-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.upload-queue-list {
    margin-top: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.queued-run-row {
    background: var(--bg-tertiary);
    border-radius: 8px;
    padding: 0.4rem 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.queued-run-primary {
    font-size: 0.78rem;
    color: var(--text-primary);
}

.queued-run-meta {
    font-size: 0.73rem;
    color: var(--text-secondary);
    word-break: break-word;
}

.queued-run-more {
    font-size: 0.74rem;
    color: var(--text-secondary);
}

.runs-list {
    padding: 0.75rem 1rem 1rem;
    overflow-y: auto;
}

.loading {
    text-align: center;
    color: var(--text-secondary);
    padding: 2rem;
}

.no-runs {
    text-align: center;
    color: var(--text-secondary);
    padding: 3rem 1rem;
}

.run-card {
    background: var(--bg-secondary);
    border-radius: 12px;
    padding: 1rem;
    margin-bottom: 1rem;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.run-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.run-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.run-date {
    font-weight: 600;
}

.run-time {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.run-card-stats {
    display: flex;
    justify-content: space-between;
    align-items: stretch;
    gap: 0.3rem;
}

.run-stat {
    display: flex;
    flex-direction: column;
    flex: 1;
    text-align: center;
}

.run-stat-value {
    font-size: 1.25rem;
    font-weight: 600;
}

.run-stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* Analysis Controls */
.analysis-controls {
    padding: 1rem;
    border-bottom: 1px solid var(--bg-tertiary);
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.analysis-row {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.analysis-row label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

#analysis-view-select,
#analysis-process-select {
    width: 100%;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--bg-primary);
    border-radius: 8px;
    padding: 0.6rem 0.7rem;
    font-size: 0.95rem;
}

.analysis-actions {
    display: flex;
    gap: 0.5rem;
}

.analysis-actions select {
    flex: 1;
}

.process-btn {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 0 1rem;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
}

.process-btn:disabled {
    opacity: 0.7;
    cursor: wait;
}

.analysis-meta {
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.35;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    z-index: 1000;
}

.modal-content {
    background: var(--bg-secondary);
    border-radius: 16px;
    width: 100%;
    max-width: 480px;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
}

.modal-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.5rem;
    cursor: pointer;
    z-index: 10;
}

.modal-content h2 {
    padding: 1rem;
    text-align: center;
    border-bottom: 1px solid var(--bg-tertiary);
}

.detail-map {
    height: 250px;
    background: var(--bg-tertiary);
}

.detail-stats {
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 1rem;
    margin: 1rem;
    background: var(--bg-tertiary);
    border-radius: 10px;
}

.detail-stat {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 0.55rem 0.25rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.detail-stat:last-child {
    border-bottom: none;
}

.detail-label {
    display: inline-block;
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.detail-value {
    font-size: 1.05rem;
    font-weight: 600;
    text-align: right;
}

.splits-panel {
    margin: 0 1rem 1rem;
    padding: 0.8rem;
    background: var(--bg-tertiary);
    border-radius: 10px;
}

.splits-title {
    margin: 0 0 0.55rem;
    font-size: 0.78rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.splits-bar-chart {
    width: 100%;
    margin-bottom: 0.55rem;
}

.split-bar-svg {
    width: 100%;
    height: 150px;
    display: block;
}

.split-bar-axis,
.split-bar-grid {
    stroke: rgba(255, 255, 255, 0.25);
    stroke-width: 1;
}

.split-bar-grid {
    stroke: rgba(255, 255, 255, 0.1);
}

.split-bar-rect {
    fill: var(--accent);
}

.split-bar-x-label,
.split-bar-y-label {
    fill: var(--text-secondary);
    font-size: 10px;
}

.split-bar-caption {
    margin-top: 0.2rem;
    font-size: 0.72rem;
    color: var(--text-secondary);
}

.split-bar-empty {
    font-size: 0.78rem;
    color: var(--text-secondary);
    padding: 0.35rem 0;
}

.pace-chart {
    width: 100%;
    min-height: 170px;
}

.pace-chart-svg {
    width: 100%;
    height: 170px;
    display: block;
}

.pace-axis,
.pace-grid {
    stroke: rgba(255, 255, 255, 0.25);
    stroke-width: 1;
}

.pace-grid {
    stroke: rgba(255, 255, 255, 0.1);
}

.pace-line {
    fill: none;
    stroke: var(--accent);
    stroke-width: 2.5;
}

.pace-point {
    fill: #4ade80;
    stroke: #ffffff;
    stroke-width: 1.2;
}

.pace-label {
    fill: var(--text-secondary);
    font-size: 10px;
}

.pace-caption {
    margin-top: 0.25rem;
    font-size: 0.72rem;
    color: var(--text-secondary);
}

.splits-list {
    margin-top: 0.6rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.split-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 0.6rem;
    align-items: center;
    padding: 0.45rem 0.5rem;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.16);
}

.split-label {
    font-size: 0.78rem;
    color: var(--text-secondary);
}

.split-time {
    font-size: 0.82rem;
    font-variant-numeric: tabular-nums;
    color: var(--text-primary);
}

.split-pace {
    font-size: 0.82rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--text-primary);
}

.splits-empty {
    font-size: 0.78rem;
    color: var(--text-secondary);
    padding: 0.35rem 0;
}

.delete-btn {
    display: block;
    width: calc(100% - 2rem);
    margin: 0 1rem 1rem;
    padding: 0.75rem;
    background: transparent;
    border: 2px solid var(--accent);
    border-radius: 8px;
    color: var(--accent);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

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

/* Leaflet overrides for dark theme */
.leaflet-container {
    background: var(--bg-tertiary);
}

.leaflet-control-attribution {
    background: var(--bg-secondary) !important;
    color: var(--text-secondary) !important;
}

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

@media (max-width: 480px) {
    .header {
        padding: 0.75rem 0.75rem 0.7rem;
    }

    .header h1 {
        font-size: 1.35rem;
    }

    .nav-btn {
        padding: 0.62rem;
        font-size: 0.96rem;
    }

    .stats-panel {
        padding: 0.5rem 0.4rem;
    }

    .stat-value {
        font-size: 1.35rem;
    }

    .controls {
        gap: 0.4rem;
        padding: 0.4rem;
    }

    .control-btn {
        padding: 0.55rem;
        border-radius: 8px;
        font-size: 0.9rem;
    }

    .track-queue-status {
        padding: 0.42rem 0.65rem 0.65rem;
        font-size: 0.74rem;
    }

    .history-toolbar {
        padding: 0.6rem 0.75rem 0;
    }

    .upload-queue-panel {
        margin: 0.6rem 0.75rem 0;
        border-radius: 8px;
        padding: 0.62rem;
    }

    #retry-upload-btn {
        border-radius: 5px;
        padding: 0.36rem 0.58rem;
        font-size: 0.74rem;
    }

    .queued-run-row {
        border-radius: 6px;
        padding: 0.35rem 0.42rem;
    }

    .queued-run-primary {
        font-size: 0.74rem;
    }

    .queued-run-meta,
    .queued-run-more {
        font-size: 0.7rem;
    }

    .runs-list {
        padding: 0.55rem 0.75rem 0.75rem;
    }

    .run-card {
        border-radius: 8px;
        padding: 0.82rem;
        margin-bottom: 0.72rem;
    }

    .run-card-header {
        margin-bottom: 0.62rem;
    }

    .modal {
        padding: 0.55rem;
    }

    .modal-content {
        border-radius: 10px;
        max-height: 94vh;
    }

    .modal-content h2 {
        padding: 0.78rem;
    }

    .modal-close {
        top: 0.72rem;
        right: 0.72rem;
    }

    .analysis-controls {
        padding: 0.75rem;
        gap: 0.6rem;
    }

    #analysis-view-select,
    #analysis-process-select,
    #history-sort {
        border-radius: 6px;
        padding: 0.5rem 0.6rem;
    }

    .process-btn {
        border-radius: 6px;
    }

    .detail-map {
        height: 220px;
    }

    .detail-stats {
        margin: 0.72rem;
        padding: 0.72rem;
        border-radius: 8px;
    }

    .detail-stat {
        padding: 0.45rem 0.2rem;
    }

    .splits-panel {
        margin: 0 0.72rem 0.72rem;
        padding: 0.65rem;
        border-radius: 8px;
    }

    .split-row {
        border-radius: 6px;
        padding: 0.4rem 0.45rem;
    }

    .delete-btn {
        width: calc(100% - 1.44rem);
        margin: 0 0.72rem 0.72rem;
        border-radius: 6px;
        padding: 0.68rem;
    }
}