/* ── Reset & Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface2: #f0f2f5;
    --border: #e2e6ec;
    --border-light: #edf0f5;
    --text: #1a1f2e;
    --text-secondary: #4b5563;
    --text-muted: #8b95a6;
    --primary: #2563eb;
    --primary-light: #eff4ff;
    --primary-hover: #1d4ed8;
    --accent: #0891b2;
    --gold: #d97706;
    --green: #059669;
    --red: #dc2626;
    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.03);
    --transition: 0.15s ease;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ── Navigation ──────────────────────────────────────────────────────────── */
.nav {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 24px;
    display: flex;
    align-items: center;
    height: 60px;
    gap: 32px;
}

.nav-brand {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--primary);
    text-decoration: none;
    letter-spacing: -0.02em;
    display: flex;
    align-items: center;
    gap: 8px;
}

.nav-brand::before {
    content: "🏃";
    font-size: 1.2rem;
}

.nav-links {
    display: flex;
    gap: 2px;
}

.nav-links a {
    color: var(--text-muted);
    text-decoration: none;
    padding: 8px 18px;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    font-weight: 500;
    transition: all var(--transition);
}

.nav-links a:hover { color: var(--text-secondary); background: var(--surface2); }
.nav-links a.active { color: var(--primary); background: var(--primary-light); font-weight: 600; }

/* ── Main ────────────────────────────────────────────────────────────────── */
.main {
    max-width: 1200px;
    margin: 0 auto;
    padding: 28px 24px;
}

.loading {
    text-align: center;
    padding: 80px 20px;
    color: var(--text-muted);
    font-size: 1rem;
}

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: var(--shadow);
}

.card-header {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ── Stats Grid ──────────────────────────────────────────────────────────── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 28px;
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 22px 20px;
    text-align: center;
    box-shadow: var(--shadow);
    transition: transform var(--transition), box-shadow var(--transition);
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary);
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
}

.stat-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-top: 6px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table-wrapper {
    overflow-x: auto;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
}

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

thead th {
    background: var(--surface2);
    padding: 12px 16px;
    text-align: left;
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
}

.sortable-th {
    cursor: pointer;
    user-select: none;
    transition: color var(--transition);
}

.sortable-th:hover {
    color: var(--text-secondary);
}

.sortable-th.sort-active {
    color: var(--primary);
}

.sort-arrow {
    display: inline-block;
    margin-left: 6px;
    font-size: 0.7rem;
    opacity: 0.25;
    transition: opacity var(--transition);
}

.sortable-th:hover .sort-arrow {
    opacity: 0.6;
}

.sort-arrow.active {
    opacity: 1;
    color: var(--primary);
}

tbody td {
    padding: 11px 16px;
    border-bottom: 1px solid var(--border-light);
    white-space: nowrap;
}

tbody tr:hover { background: var(--primary-light); }
tbody tr:last-child td { border-bottom: none; }

/* Alternate row striping for readability */
tbody tr:nth-child(even) {
    background: #fafbfc;
}
tbody tr:nth-child(even):hover {
    background: var(--primary-light);
}

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

.rank-1 { background: #fef3c7; color: #d97706; }
.rank-2 { background: #f1f5f9; color: #64748b; }
.rank-3 { background: #fef2e6; color: #b45309; }
.rank-other { background: var(--surface2); color: var(--text-muted); }

.mark { font-weight: 600; font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace; letter-spacing: -0.01em; }
.mark-good { color: var(--green); }
.mark-great { color: var(--primary); }
.mark-elite { color: var(--gold); }

.flag { font-size: 1.1rem; }

.athlete-link {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    cursor: pointer;
    transition: color var(--transition);
}
.athlete-link:hover { color: var(--primary); text-decoration: underline; }

.score-bar {
    display: inline-block;
    width: 60px;
    height: 6px;
    background: var(--surface2);
    border-radius: 3px;
    overflow: hidden;
    vertical-align: middle;
    margin-right: 6px;
}
.score-bar-fill {
    height: 100%;
    background: var(--primary);
    border-radius: 3px;
    transition: width 0.3s;
}

/* ── Search & Filters ────────────────────────────────────────────────────── */
.filters {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    align-items: center;
}

.search-input {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    flex: 1;
    min-width: 200px;
    outline: none;
    transition: border-color var(--transition), box-shadow var(--transition);
    box-shadow: var(--shadow);
}
.search-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.1); }
.search-input::placeholder { color: var(--text-muted); }

.select-input {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    outline: none;
    cursor: pointer;
    min-width: 130px;
    box-shadow: var(--shadow);
    transition: border-color var(--transition);
}
.select-input:focus { border-color: var(--primary); }

/* ── Autocomplete ───────────────────────────────────────────────────────── */
.autocomplete-wrap {
    position: relative;
    flex: 1;
    min-width: 180px;
}

.autocomplete-wrap .search-input {
    width: 100%;
    padding-right: 36px;
}

.autocomplete-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    max-height: 280px;
    overflow-y: auto;
    z-index: 50;
    margin-top: 4px;
    box-shadow: var(--shadow-md);
}

.autocomplete-dropdown.open {
    display: block;
}

.autocomplete-item {
    padding: 9px 14px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text);
    transition: background 0.1s;
}

.autocomplete-item:hover {
    background: var(--primary-light);
}

.autocomplete-item .text-muted {
    color: var(--text-muted);
    font-size: 0.8rem;
}

.clear-btn {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 4px 8px;
    line-height: 1;
    border-radius: 4px;
    transition: all var(--transition);
    z-index: 2;
}

.clear-btn:hover {
    color: var(--text);
    background: var(--surface2);
}

/* ── Nav Filters ────────────────────────────────────────────────────────── */
.nav-filters {
    display: flex;
    gap: 8px;
    margin-left: auto;
    align-items: center;
}

.nav-select {
    background: var(--surface2);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 7px 12px;
    border-radius: var(--radius-sm);
    font-size: 0.8rem;
    outline: none;
    cursor: pointer;
    min-width: 110px;
    transition: border-color var(--transition);
}

.nav-select:focus {
    border-color: var(--primary);
    background: var(--surface);
}

.filter-badge {
    display: inline-block;
    background: var(--primary-light);
    color: var(--primary);
    padding: 4px 14px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.02em;
}

/* ── Pagination ──────────────────────────────────────────────────────────── */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    margin-top: 24px;
    padding: 16px 0;
    flex-wrap: wrap;
}

.pagination button {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.85rem;
    transition: all var(--transition);
    box-shadow: var(--shadow);
}
.pagination button:hover:not(:disabled) { background: var(--primary); border-color: var(--primary); color: white; }
.pagination button:disabled { opacity: 0.4; cursor: not-allowed; }
.pagination button.active { background: var(--primary); border-color: var(--primary); color: white; font-weight: 600; }
.pagination .page-info { color: var(--text-muted); font-size: 0.85rem; padding: 0 4px; }

/* ── Athlete Profile ─────────────────────────────────────────────────────── */
.profile-header {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 28px;
    flex-wrap: wrap;
}

.profile-avatar {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 700;
    color: white;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(37,99,235,0.2);
}

.profile-info h1 {
    font-size: 1.75rem;
    font-weight: 700;
    line-height: 1.3;
}

.profile-info .meta {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-top: 4px;
}

.profile-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 28px;
}

.profile-stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 16px;
    text-align: center;
    box-shadow: var(--shadow);
}

.profile-stat .value {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--primary);
    font-variant-numeric: tabular-nums;
}

.profile-stat .label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
    margin-top: 4px;
}

.back-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 8px 18px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    transition: all var(--transition);
    margin-bottom: 20px;
    box-shadow: var(--shadow);
}
.back-btn:hover { background: var(--primary); border-color: var(--primary); color: white; }
.back-btn::before { content: "← "; }

/* ── Progression Chart ───────────────────────────────────────────────────── */
.progression-chart {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    height: 130px;
    padding: 16px 8px 0;
    margin-bottom: 4px;
    position: relative;
}

.chart-bar {
    flex: 1;
    background: linear-gradient(to top, var(--primary), #60a5fa);
    border-radius: 4px 4px 0 0;
    min-height: 4px;
    position: relative;
    transition: height 0.3s;
    cursor: pointer;
    max-width: 40px;
}

.chart-bar:hover { opacity: 0.85; }

.chart-bar .tooltip {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text);
    color: white;
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 0.75rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s;
    margin-bottom: 6px;
    font-weight: 500;
    z-index: 5;
}

.chart-bar:hover .tooltip { opacity: 1; }

/* Chart year labels */
.progression-chart + div[style] {
    margin-top: 0;
}

/* ── Section Titles ──────────────────────────────────────────────────────── */
.section-title {
    font-size: 1.15rem;
    font-weight: 700;
    margin: 28px 0 16px;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ── Dashboard heading ───────────────────────────────────────────────────── */
.dash-heading {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    flex-wrap: wrap;
    gap: 8px;
}

.dash-heading h2 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text);
}

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
    .nav-inner { gap: 12px; flex-wrap: wrap; height: auto; padding: 10px 16px; }
    .nav-links { width: 100%; }
    .nav-links a { padding: 8px 12px; font-size: 0.8rem; flex: 1; text-align: center; }
    .nav-filters { margin-left: 0; width: 100%; }
    .nav-select { flex: 1; min-width: 0; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .profile-stats { grid-template-columns: repeat(2, 1fr); }
    .filters { flex-direction: column; }
    .search-input { min-width: 100%; }
}

/* ── Toast / Errors ──────────────────────────────────────────────────────── */
.error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: var(--red);
    padding: 14px 18px;
    border-radius: var(--radius);
    margin-bottom: 16px;
    font-size: 0.9rem;
}

.empty {
    text-align: center;
    padding: 50px 20px;
    color: var(--text-muted);
    font-size: 0.95rem;
}

/* ── Year cards ──────────────────────────────────────────────────────────── */
.year-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 14px;
}

.year-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 22px 20px;
    text-align: center;
    cursor: pointer;
    transition: all var(--transition);
    box-shadow: var(--shadow);
}
.year-card:hover { border-color: var(--primary); transform: translateY(-2px); box-shadow: var(--shadow-md); }
.year-card .year { font-size: 1.5rem; font-weight: 700; color: var(--primary); }
.year-card .count { color: var(--text-muted); font-size: 0.85rem; margin-top: 4px; }

/* ── Event Groups ─────────────────────────────────────────────────────────── */
.event-group {
    margin-bottom: 20px;
}

.event-group:last-child {
    margin-bottom: 0;
}

.event-group-header {
    display: flex;
    align-items: baseline;
    gap: 12px;
    padding: 12px 16px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-bottom: none;
    border-radius: var(--radius) var(--radius) 0 0;
}

.event-group-title {
    font-weight: 700;
    font-size: 1rem;
    color: var(--primary);
}

.event-group-meta {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.event-group .table-wrapper {
    border-radius: 0 0 var(--radius) var(--radius);
    border-top: none;
}

/* ── Admin Page ──────────────────────────────────────────────────────────── */
.admin-form-group {
    margin-bottom: 20px;
}

.admin-label {
    display: block;
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.admin-checkbox-group {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 8px;
}

.admin-checkbox {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
    transition: all var(--transition);
    user-select: none;
}

.admin-checkbox:hover {
    border-color: var(--primary);
    background: var(--primary-light);
}

.admin-checkbox input[type="checkbox"] {
    accent-color: var(--primary);
    width: 16px;
    height: 16px;
    cursor: pointer;
}

.admin-checkbox-actions {
    display: flex;
    gap: 8px;
    margin-top: 4px;
}

.admin-btn-sm {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 4px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.8rem;
    transition: all var(--transition);
}

.admin-btn-sm:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.admin-year-input-wrap {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.admin-year-input-wrap .search-input {
    max-width: 360px;
}

.admin-hint {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.admin-form-actions {
    display: flex;
    align-items: center;
    margin-top: 8px;
    flex-wrap: wrap;
    gap: 8px;
}

.admin-btn {
    padding: 10px 28px;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: all var(--transition);
}

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

.admin-btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.admin-btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.admin-btn-import {
    background: var(--green);
    color: white;
}

.admin-btn-import:hover:not(:disabled) {
    background: #047857;
}

.admin-btn-import:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.admin-combo-section {
    margin-bottom: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border-light);
}

.admin-combo-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.admin-combo-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    flex-wrap: wrap;
    gap: 8px;
}

.admin-combo-title {
    font-weight: 700;
    font-size: 0.95rem;
    color: var(--primary);
}

.admin-combo-count {
    font-size: 0.85rem;
    color: var(--text-muted);
    font-weight: 500;
}

.admin-error-text {
    color: var(--red);
}

.admin-sample-note {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-style: italic;
    padding: 4px 0 8px;
}