/* Shared light/dark theme — toggled via html[data-theme] (?theme=dark|light) */

html[data-theme="light"] {
    color-scheme: light;
    --tk-bg: #f0f2f5;
    --tk-panel: #ffffff;
    --tk-panel-border: #e2e8f0;
    --tk-text: #2d3748;
    --tk-text-muted: #718096;
    --tk-heading: #333333;
    --tk-accent: #0077ff;
    --tk-surface: #f8f9fa;
    --tk-row-border: #eeeeee;
    --tk-row-hover: #f8f9fa;
    --tk-row-selected: #e3f2fd;
    --tk-group-bg: #f8f9fa;
    --tk-group-border: #e9ecef;
    --tk-group-row: #ffffff;
    --tk-gap-bg: linear-gradient(90deg, #e9ecef 0%, #dee2e6 50%, #e9ecef 100%);
    --tk-gap-text-bg: #ffffff;
    --tk-gap-text-color: #495057;
    --tk-gap-line: #6c757d;
    --tk-section-header-bg: #f1f3f5;
    --tk-section-header-color: #495057;
    --tk-card-bg: rgba(255, 255, 255, 0.95);
    --tk-card-border: rgba(255, 255, 255, 0.3);
    --tk-card-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    --tk-weather-bg: rgba(255, 255, 255, 0.92);
    --tk-weather-text: #4a5568;
    --tk-weather-value: #2d3748;
    --tk-weather-unit: #718096;
    --tk-sponsor-bg: rgba(255, 255, 255, 0.85);
    --tk-elapsed-bg: #f8f9fa;
    --tk-elapsed-text: #333333;
    --tk-elapsed-border: #dee2e6;
    --tk-slider-track: #dee2e6;
    --tk-timeline-bg: linear-gradient(180deg, #f8f9fa 0%, #eef1f5 100%);
    --tk-timeline-border: #dee2e6;
    --tk-control-bg: #ffffff;
    --tk-control-border: #dee2e6;
    --tk-btn-bg: transparent;
    --tk-btn-hover: rgba(0, 119, 255, 0.12);
    --tk-select-bg: #ffffff;
    --tk-splits-btn-bg: #ffffff;
    --tk-splits-btn-border: #cccccc;
    --tk-splits-btn-text: #333333;
    --tk-waiting-overlay: rgba(255, 255, 255, 0.85);
    --tk-waiting-text: #333333;
    --tk-waiting-box: rgba(255, 255, 255, 0.95);
    --tk-table-bg: #ffffff;
    --tk-table-head-bg: #f1f3f5;
    --tk-table-head-text: #4a5568;
    --tk-table-border: #e2e8f0;
    --tk-table-row-hover: #f8f9fa;
}

html[data-theme="dark"] {
    color-scheme: dark;
    --tk-bg: #0f1419;
    --tk-panel: #1a2332;
    --tk-panel-border: #2d3a4d;
    --tk-text: #e7ecf3;
    --tk-text-muted: #9aa8bc;
    --tk-heading: #e7ecf3;
    --tk-accent: #3b8bff;
    --tk-surface: #121820;
    --tk-row-border: #2d3a4d;
    --tk-row-hover: rgba(59, 139, 255, 0.08);
    --tk-row-selected: rgba(59, 139, 255, 0.15);
    --tk-group-bg: #1e2836;
    --tk-group-border: #2d3a4d;
    --tk-group-row: #1a2332;
    --tk-gap-bg: linear-gradient(90deg, #252f3f 0%, #2d3a4d 50%, #252f3f 100%);
    --tk-gap-text-bg: #1a2332;
    --tk-gap-text-color: #b8c5d6;
    --tk-gap-line: #6c757d;
    --tk-section-header-bg: #252f3f;
    --tk-section-header-color: #9aa8bc;
    --tk-card-bg: rgba(26, 35, 50, 0.95);
    --tk-card-border: #2d3a4d;
    --tk-card-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
    --tk-weather-bg: rgba(26, 35, 50, 0.92);
    --tk-weather-text: #b8c5d6;
    --tk-weather-value: #e7ecf3;
    --tk-weather-unit: #9aa8bc;
    --tk-sponsor-bg: rgba(26, 35, 50, 0.88);
    --tk-elapsed-bg: #121820;
    --tk-elapsed-text: #e7ecf3;
    --tk-elapsed-border: #2d3a4d;
    --tk-slider-track: #2a3548;
    --tk-timeline-bg: linear-gradient(180deg, rgba(26, 35, 50, 0.97) 0%, rgba(15, 20, 25, 0.98) 100%);
    --tk-timeline-border: #2d3a4d;
    --tk-control-bg: #121820;
    --tk-control-border: #2d3a4d;
    --tk-btn-bg: transparent;
    --tk-btn-hover: rgba(59, 139, 255, 0.2);
    --tk-select-bg: #121820;
    --tk-splits-btn-bg: #1a2332;
    --tk-splits-btn-border: #2d3a4d;
    --tk-splits-btn-text: #e7ecf3;
    --tk-waiting-overlay: rgba(15, 20, 25, 0.88);
    --tk-waiting-text: #e7ecf3;
    --tk-waiting-box: rgba(26, 35, 50, 0.95);
    --tk-table-bg: #151c27;
    --tk-table-head-bg: #1e2838;
    --tk-table-head-text: #b8c5d6;
    --tk-table-border: #2d3a4d;
    --tk-table-row-hover: #1a2433;
}

/* ── Base surfaces ── */
html[data-theme="dark"] body {
    background: var(--tk-bg);
    color: var(--tk-text);
}

html[data-theme="dark"] .info-panel {
    background: linear-gradient(180deg, #151c27 0%, #121820 100%);
    border-left: 1px solid var(--tk-panel-border);
}

html[data-theme="dark"] .info-panel h2,
html[data-theme="light"] .surface-playback .info-panel h2 {
    color: var(--tk-heading);
}

html[data-theme="dark"] .race-info-section,
html[data-theme="dark"] .leaderboard-section,
html[data-theme="light"] .surface-playback .leaderboard-section {
    background: var(--tk-panel);
    border: 1px solid var(--tk-panel-border);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

html[data-theme="dark"] .race-info,
html[data-theme="dark"] .race-name {
    color: var(--tk-text);
}

html[data-theme="dark"] .race-id {
    color: var(--tk-text-muted);
    background: rgba(154, 168, 188, 0.12);
    border-color: rgba(154, 168, 188, 0.25);
}

/* ── Leaderboard rows ── */
html[data-theme="dark"] .leaderboard-row {
    border-bottom-color: var(--tk-row-border);
}

html[data-theme="dark"] .leaderboard-row:hover {
    background-color: var(--tk-row-hover);
}

html[data-theme="dark"] .leaderboard-row.selected {
    background-color: var(--tk-row-selected);
    box-shadow: 0 2px 12px rgba(59, 139, 255, 0.2);
}

html[data-theme="dark"] .athlete-name {
    color: var(--tk-text);
}

html[data-theme="dark"] .athlete-team,
html[data-theme="dark"] .athlete-team-line,
html[data-theme="dark"] .athlete-stats,
html[data-theme="dark"] .athlete-details {
    color: var(--tk-text-muted);
}

html[data-theme="dark"] .athlete-meta .lb-data {
    color: var(--tk-text);
}

html[data-theme="dark"] .lb-penalty {
    color: #ff6b7a;
}

/* ── Groups ── */
html[data-theme="dark"] .group-container,
html[data-theme="light"] .surface-playback .group-container {
    background: var(--tk-group-bg);
    border-color: var(--tk-group-border);
}

html[data-theme="dark"] .group-athlete,
html[data-theme="light"] .surface-playback .group-athlete {
    background: var(--tk-group-row);
    border-bottom-color: var(--tk-group-border);
}

html[data-theme="dark"] .gap-indicator,
html[data-theme="light"] .surface-playback .gap-indicator {
    background: var(--tk-gap-bg);
}

html[data-theme="dark"] .gap-text,
html[data-theme="light"] .surface-playback .gap-text {
    background: var(--tk-gap-text-bg);
    color: var(--tk-gap-text-color);
    border-color: var(--tk-gap-line);
}

html[data-theme="dark"] .group-section-header,
html[data-theme="light"] .surface-playback .group-section-header {
    background: var(--tk-section-header-bg);
    color: var(--tk-section-header-color);
}

/* ── Weather card ── */
html[data-theme="dark"] .weather-card,
html[data-theme="light"] .surface-playback .weather-card {
    background: var(--tk-weather-bg);
    border: 1px solid var(--tk-card-border);
    color: var(--tk-weather-text);
    box-shadow: var(--tk-card-shadow);
}

html[data-theme="dark"] .weather-value,
html[data-theme="light"] .surface-playback .weather-value {
    color: var(--tk-weather-value);
}

html[data-theme="dark"] .weather-unit,
html[data-theme="light"] .surface-playback .weather-unit {
    color: var(--tk-weather-unit);
}

/* ── Event info card (playback) ── */
.event-info-card {
    position: absolute;
    top: 20px;
    left: 20px;
    right: 20px;
    z-index: 1000;
    min-width: 260px;
    max-width: 400px;
    padding: 18px 24px;
    border-radius: 10px;
    transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

html[data-theme="light"] .event-info-card {
    background: var(--tk-card-bg);
    box-shadow: var(--tk-card-shadow);
    color: var(--tk-text);
}

html[data-theme="dark"] .event-info-card {
    background: var(--tk-card-bg);
    border: 1px solid var(--tk-card-border);
    box-shadow: var(--tk-card-shadow);
    color: var(--tk-text);
}

.event-info-card__title {
    font-size: 1.5em;
    font-weight: bold;
    margin-bottom: 6px;
}

.event-info-card__subtitle {
    font-size: 0.9em;
    margin-bottom: 8px;
    color: var(--tk-text-muted);
}

/* ── Race status / elapsed ── */
html[data-theme="dark"] .elapsed-time,
html[data-theme="light"] .surface-playback .elapsed-time {
    color: var(--tk-elapsed-text);
    background: var(--tk-elapsed-bg);
    border-color: var(--tk-elapsed-border);
}

/* ── Sponsor banner ── */
html[data-theme="dark"] .sponsor-banner,
html[data-theme="light"] .surface-playback .sponsor-banner {
    background: var(--tk-sponsor-bg);
}

/* ── Splits toggle ── */
html[data-theme="dark"] .header-row .splits-toggle-btn,
html[data-theme="light"] .surface-playback .header-row .splits-toggle-btn {
    background: var(--tk-splits-btn-bg);
    border-color: var(--tk-splits-btn-border);
    color: var(--tk-splits-btn-text);
}

html[data-theme="dark"] .header-row .splits-toggle-btn:hover,
html[data-theme="light"] .surface-playback .header-row .splits-toggle-btn:hover {
    background: var(--tk-row-hover);
}

/* ── Waiting overlay (live) ── */
html[data-theme="dark"] .waiting-overlay {
    background: var(--tk-waiting-overlay);
}

html[data-theme="dark"] .waiting-text {
    color: var(--tk-waiting-text);
    background: var(--tk-waiting-box);
    border-color: #4a5568;
}

/* ── Athlete details (live) ── */
html[data-theme="dark"] .athlete-details-panel {
    background: linear-gradient(135deg, #1a2332 0%, #151c27 100%);
    border-color: var(--tk-panel-border);
}

html[data-theme="dark"] .athlete-info-label {
    color: var(--tk-text-muted);
}

html[data-theme="dark"] .athlete-info-value {
    color: var(--tk-text);
}

html[data-theme="dark"] .athlete-chart-section {
    background: var(--tk-panel);
}

html[data-theme="dark"] .chart-title {
    color: var(--tk-text-muted);
}

html[data-theme="dark"] .speed-chart-current {
    color: #4da3ff;
}

html[data-theme="dark"] .speed-chart {
    background: var(--tk-surface);
}

/* ── Chat (live) ── */
html[data-theme="dark"] .chat-container {
    border-color: var(--tk-panel-border);
}

html[data-theme="dark"] .chat-panel {
    background: rgba(26, 35, 50, 0.88);
}

html[data-theme="dark"] .chat-header h3,
html[data-theme="dark"] .chat-toggle {
    color: var(--tk-text);
}

html[data-theme="dark"] .message-text {
    color: var(--tk-text);
}

html[data-theme="dark"] .chat-input {
    background: transparent;
}

html[data-theme="dark"] .chat-input input,
html[data-theme="dark"] .chat-input textarea {
    background: var(--tk-surface);
    color: var(--tk-text);
    border-color: var(--tk-panel-border);
}

/* ── Playback: light theme overrides ── */
html[data-theme="light"].surface-playback body {
    background: var(--tk-bg);
    color: var(--tk-text);
}

html[data-theme="light"].surface-playback .info-panel {
    background: var(--tk-bg);
    border-left-color: var(--tk-panel-border);
}

html[data-theme="light"].surface-playback .leaderboard-section {
    background: var(--tk-panel);
    border-color: var(--tk-panel-border);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

html[data-theme="light"].surface-playback #timeline {
    background: var(--tk-timeline-bg);
    border-top-color: var(--tk-timeline-border);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.08);
}

html[data-theme="light"].surface-playback #timeDisplay,
html[data-theme="light"].surface-playback .control-buttons {
    background: var(--tk-control-bg);
    border-color: var(--tk-control-border);
    color: var(--tk-text);
}

html[data-theme="light"].surface-playback #timelineSlider {
    background: var(--tk-slider-track);
}

html[data-theme="light"].surface-playback #playBtn,
html[data-theme="light"].surface-playback #pauseBtn,
html[data-theme="light"].surface-playback #stopBtn {
    color: var(--tk-text);
}

html[data-theme="light"].surface-playback #playBtn:not(:disabled):hover,
html[data-theme="light"].surface-playback #pauseBtn:not(:disabled):hover,
html[data-theme="light"].surface-playback #stopBtn:not(:disabled):hover {
    background: var(--tk-btn-hover);
}

html[data-theme="light"].surface-playback #speedSelect {
    background-color: var(--tk-select-bg);
    color: var(--tk-text);
    border-color: var(--tk-control-border);
}

html[data-theme="light"].surface-playback .leaderboard-row {
    border-bottom-color: var(--tk-row-border);
}

html[data-theme="light"].surface-playback .leaderboard-row:hover {
    background-color: var(--tk-row-hover);
}

html[data-theme="light"].surface-playback .leaderboard-row.selected {
    background-color: var(--tk-row-selected);
}

html[data-theme="light"].surface-playback .athlete-name {
    color: var(--tk-heading);
}

html[data-theme="light"].surface-playback .athlete-team-line,
html[data-theme="light"].surface-playback .athlete-stats {
    color: var(--tk-text-muted);
}

html[data-theme="light"].surface-playback .athlete-meta .lb-data {
    color: var(--tk-text);
}

/* ── Splits overlay light theme ── */
html[data-theme="light"] .splits-overlay {
    background: rgba(240, 242, 245, 0.92);
}

html[data-theme="light"] .splits-overlay-panel {
    background: #ffffff;
    border-color: #dee2e6;
    color: #2d3748;
}

html[data-theme="light"] .splits-overlay-header {
    background: #f8f9fa;
    border-bottom-color: #dee2e6;
}

html[data-theme="light"] .splits-overlay-meta,
html[data-theme="light"] .splits-overlay-gate-info {
    color: #718096;
}

html[data-theme="light"] .splits-overlay-close {
    background: #e2e8f0;
    color: #2d3748;
}

html[data-theme="light"] .splits-overlay-close:hover {
    background: #cbd5e0;
}

html[data-theme="light"] .splits-overlay-table-wrap {
    background: #ffffff;
    border-color: #dee2e6;
}

html[data-theme="light"] .splits-placeholder {
    color: #718096;
}

html[data-theme="light"] .splits-overlay table.splits-table th,
html[data-theme="light"] .splits-overlay table.splits-table td,
html[data-theme="light"] .splits-overlay table.groups-table th,
html[data-theme="light"] .splits-overlay table.groups-table td {
    border-bottom-color: #e2e8f0;
}

html[data-theme="light"] .splits-overlay table.splits-table thead th,
html[data-theme="light"] .splits-overlay table.groups-table thead th {
    background: #f1f3f5;
    color: #4a5568;
}

html[data-theme="light"] .splits-overlay table.splits-table .sticky-col,
html[data-theme="light"] .splits-overlay table.splits-table .sticky-col-2,
html[data-theme="light"] .splits-overlay table.splits-table .sticky-col-3,
html[data-theme="light"] .splits-overlay table.splits-table .sticky-col-4,
html[data-theme="light"] .splits-overlay table.splits-table .sticky-col-5,
html[data-theme="light"] .splits-overlay table.splits-table .sticky-col-6 {
    background: #ffffff;
}

html[data-theme="light"] .splits-overlay table.splits-table thead .sticky-col,
html[data-theme="light"] .splits-overlay table.splits-table thead .sticky-col-2,
html[data-theme="light"] .splits-overlay table.splits-table thead .sticky-col-3,
html[data-theme="light"] .splits-overlay table.splits-table thead .sticky-col-4,
html[data-theme="light"] .splits-overlay table.splits-table thead .sticky-col-5,
html[data-theme="light"] .splits-overlay table.splits-table thead .sticky-col-6 {
    background: #f1f3f5;
}
