/* =============== Global Layout =============== */

:root {
    --bg-main: #05070b;
    --bg-card: #14161d;
    --bg-card-alt: #181b23;
    --border-subtle: #2c313d;
    --text-main: #f5f5f5;
    --text-muted: #a6abb7;
    --accent: #ffb347;
    --accent-soft: rgba(255, 179, 71, 0.16);
    --danger: #ff4b7d;
    --success: #00d66b;
    /* Global defaults for SVG canvas sizing (can be edited here) */
    --global-svg-width: 1800px;
    --global-svg-height: 600px;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text",
        "Segoe UI", sans-serif;
    background: radial-gradient(circle at top, #1c2333 0, #05070b 45%);
    color: var(--text-main);
}

#app {
    max-width: 1900px;
    margin: 0 auto;
    padding: 16px 24px 32px;
}

/* =============== Top Bar =============== */

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 4px 20px;
}

.top-title {
    font-size: 22px;
    font-weight: 700;
    letter-spacing: 0.03em;
}

.top-course {
    font-size: 12px;
    color: var(--text-muted);
    letter-spacing: 0.01em;
}

.accent-link {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
}

.accent-link:hover {
    text-decoration: underline;
}

/* Top-right area: author + size toggle */
.top-right {
    display: flex;
    align-items: center;
    gap: 14px;
}

/* Small accessible toggle to change global SVG sizing */
.size-toggle {
    width: 56px;
    height: 30px;
    background: rgba(255,255,255,0.04);
    border-radius: 999px;
    position: relative;
    padding: 4px;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.02);
    border: 1px solid rgba(255,255,255,0.05);
    cursor: pointer;
    transition: background 200ms ease, transform 120ms ease;
}
.size-toggle .knob {
    width: 22px;
    height: 22px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 3px 8px rgba(0,0,0,0.5);
    position: absolute;
    left: 4px;
    top: 4px;
    transition: left 220ms cubic-bezier(.2,.9,.2,1), transform 120ms ease;
}
.size-toggle[aria-checked="true"], .size-toggle.on {
    background: var(--accent);
}
.size-toggle[aria-checked="true"] .knob, .size-toggle.on .knob {
    left: 30px;
}

.size-toggle:active .knob { transform: scale(0.98); }

/* visually hidden helper text */
.sr-only { position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden; }

/* Smooth transitions when toggling width/height */
#top-panels, #plots { transition: max-width 360ms cubic-bezier(.2,.9,.2,1); }
/* allow svg width/height attribute adjustments without layout jump */
.plot-card svg { transition: height 360ms cubic-bezier(.2,.9,.2,1), transform 360ms cubic-bezier(.2,.9,.2,1); }

/* Subtle fade + hardware-accelerated hint while animating */
.animating #plots, .animating #top-panels { will-change: max-width, opacity; }
.animating .plot-card svg { will-change: height, transform, opacity; }

/* =============== Control Card =============== */

#control-card {
    background: var(--bg-card);
    border-radius: 18px;
    border: 1px solid var(--border-subtle);
    padding: 14px 18px 120px; /* increased bottom padding to reserve space for absolutely positioned session row */
    margin-bottom: 18px;
    box-shadow: 0 14px 26px rgba(0, 0, 0, 0.55);
    position: relative; /* allow absolute positioning of session row */
}

.control-row {
    display: grid;
    grid-template-columns: 100px 1fr;
    align-items: start; /* align labels to top so multi-line content doesn't overlap */
    margin-bottom: 18px; /* more vertical spacing between rows */
}

.control-label {
    font-size: 13px;
    color: var(--text-muted);
}

.control-content {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.pill-group {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.pill-group-scroll {
    /* Allow the race pill group to expand to show all races instead of scrolling */
    max-height: none;
    overflow: visible;
    padding-right: 0;
}

.pill {
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(255, 255, 255, 0.02);
    color: var(--text-main);
    font-size: 11px;
    padding: 4px 10px;
    cursor: pointer;
    white-space: nowrap;
}

.pill-race {
    font-size: 11px;
}

.pill:hover:not(:disabled) {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.pill:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    pointer-events: none;
}

.pill-active {
    background: var(--accent);
    color: #000;
    border-color: var(--accent);
}

.hidden-select {
    display: none;
}

.control-select {
    width: 100%;
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-card);
    color: var(--text-main);
    font-size: 13px;
    cursor: pointer;
    appearance: auto;
}

.control-select:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.control-content-right {
    align-items: flex-end;
}

.load-data-btn {
    padding: 6px 32px;
    border-radius: 8px;
    border: none;
    background: var(--accent);
    color: #000;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    width: auto;
}

.load-data-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.load-data-btn:not(:disabled):hover {
    filter: brightness(1.15);
}

.load-data-btn .spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid rgba(0,0,0,0.25);
    border-top-color: #000;
    border-radius: 50%;
    animation: btn-spin 0.6s linear infinite;
    vertical-align: middle;
    margin-right: 6px;
}

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

.loaded-label {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* =============== Driver Pills =============== */

#driver-pills-section {
    background: var(--bg-card);
    border-radius: 18px;
    border: 1px solid var(--border-subtle);
    padding: 10px 18px 12px;
    margin-bottom: 18px;
}

#driver-pills-section h2 {
    font-size: 15px;
    margin: 2px 0 4px;
}

#driver-pills-section .hint {
    margin: 0 0 8px;
    font-size: 11px;
    color: var(--text-muted);
}

/* driver pills grid/placement handled below */

/* ===== Top two-panel layout: controls + track preview ===== */
#top-panels {
    display: flex;
    gap: 18px;
    margin-bottom: 18px;
    /* constrain the top panels to match plot canvas width */
    max-width: var(--global-svg-width);
    margin-left: auto;
    margin-right: auto;
    align-items: stretch; /* make children same height */
}

.left-panel {
    flex: 1 1 50%; /* left column: 50% */
    display: flex;
    flex-direction: column;
    gap: 18px; /* match column gap so spacing between stacked panels equals inter-column gap */
}

.right-panel {
    flex: 1 1 50%; /* right column: 50% */
    display: flex;
}

/* make the control card expand vertically and keep the driver box compact */
.left-panel > #control-card {
    flex: 1 1 auto;
    margin-bottom: 0; /* avoid double spacing with flex gap */
}

.left-panel > #driver-pills-section {
    flex: 0 0 auto;
    margin-bottom: 0; /* avoid extra bottom margin inside left-panel */
}

.right-panel > #track-panel {
    flex: 1 1 auto;
}

/* track panel stretches to match the combined height of the left column */
#track-panel {
    padding: 12px;
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
}

#track-canvas {
    width: 100%;
    height: 100%; /* fill the track panel */
    min-height: 360px;
    background: linear-gradient(180deg, rgba(255,255,255,0.01), rgba(255,255,255,0.02));
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.03);
    display: block;
}

/* New layout for the Circuit Track panel to match design */
.track-container {
    display: flex;
    gap: 14px;
    align-items: stretch;
    width: 100%;
    height: 420px; /* keep similar visual size to previous canvas */
}

.track-preview {
    flex: 3 1 75%;
    display: flex;
}

.track-preview-inner {
    flex: 1;
    /* Remove inner inset so canvas can use the full preview area */
    background: transparent;
    border-radius: 0;
    padding: 0;
    border: none;
    box-shadow: none;
    display: flex;
    align-items: stretch;
    justify-content: stretch;
    position: relative;
}

.track-preview-inner canvas {
    width: 100%;
    height: 100%;
    border-radius: 12px;
    display: block;
    /* move the border/visual framing onto the canvas itself */
    background: linear-gradient(180deg, rgba(255,255,255,0.01), rgba(255,255,255,0.02));
    border: 1px solid rgba(255,255,255,0.03);
    box-shadow: inset 0 2px 0 rgba(255,255,255,0.02), 0 10px 20px rgba(0,0,0,0.45);
}

.track-empty {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-muted);
    font-size: 15px;
    text-align: center;
    pointer-events: none;
}

.track-info {
    flex: 1 1 25%;
    display: flex;
}

.track-info-card {
    background: linear-gradient(180deg, rgba(255,255,255,0.01), rgba(255,255,255,0.02));
    border-radius: 12px;
    padding: 18px;
    width: 100%;
    border: 1px solid rgba(255,255,255,0.03);
    box-shadow: 0 8px 18px rgba(0,0,0,0.45);
}

.track-info-card h3 {
    margin: 0 0 10px 0;
    font-size: 16px;
}

.info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    border-top: 1px solid rgba(255,255,255,0.02);
}

.info-row:first-of-type { border-top: none; padding-top: 6px; }

.info-label { color: var(--text-muted); font-size: 13px; }
.info-value {
    font-weight: 600;
    font-size: 13px;
    text-align: right;
    white-space: normal;
    overflow-wrap: anywhere;
    max-width: 65%;
}

/* Small driver box spacing: keep it under the control card */
.left-panel > #driver-pills-section {
    margin-top: 0; /* remove extra top margin so bottoms align precisely */
}

.driver-pill {
    border-radius: 999px;
    border: 1px solid #555;
    background: #0d0f15;
    color: #ccc;
    font-size: 11px;
    padding: 4px 10px;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

/* Arrange driver pills in two rows, flowing across columns */
#driver-pills {
    display: grid;
    grid-template-rows: repeat(2, auto);
    grid-auto-flow: column;
    gap: 6px;
    align-items: center;
}

/* Fallback for smaller screens: revert to flex wrap */
@media (max-width: 1100px) {
    #driver-pills {
        display: flex;
        flex-wrap: wrap;
    }
}

/* Unselected pills: dark background, team colour as text/border */
.driver-pill.unselected {
    background: #0d0f15;
    color: var(--pill-text-color, var(--team-color, #ccc));
    border-color: var(--pill-border-color, var(--team-color, #555));
}

/* Selected pills: filled with team colour and readable text */
.driver-pill.selected {
    background: var(--team-color, #ffffff);
    color: var(--team-text-color, #000);
    border-color: var(--pill-border-color, var(--team-color, #fff));
}

/* Make sure hover keeps clear contrast */
.driver-pill.unselected:hover {
    background: rgba(255,255,255,0.02);
}
.driver-pill.selected:hover {
    filter: brightness(0.95);
}

.driver-pill:hover {
    border-color: var(--accent);
}

/* =============== Plot Cards =============== */

#plots {
    display: flex;
    flex-direction: column;
    gap: 18px;
    max-width: var(--global-svg-width);
    margin-left: auto;
    margin-right: auto;
}

.plot-card {
    background: var(--bg-card);
    border-radius: 18px;
    border: 1px solid var(--border-subtle);
    padding: 12px 18px 16px;
    box-shadow: 0 14px 26px rgba(0, 0, 0, 0.55);
    position: relative;
}

.plot-card h2 {
    font-size: 15px;
    margin: 0 0 4px;
}

.plot-help {
    font-size: 11px;
    color: var(--text-muted);
    margin: 0 0 10px;
}

.plot-card svg {
    width: 100%;
    display: block;
}

/* Small save button for exporting plots */
.save-plot-btn {
    position: absolute;
    top: 10px;
    right: 12px;
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: rgba(255,255,255,0.04);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    cursor: pointer;
    border: 1px solid rgba(255,255,255,0.05);
    transition: background 120ms ease, transform 100ms ease, opacity 120ms;
    opacity: 0.9;
    z-index: 8;
}
.save-plot-btn:hover {
    background: rgba(255,255,255,0.09);
    transform: translateY(-1px);
}
.save-plot-btn:active { transform: translateY(0); }
.save-plot-btn .icon { pointer-events: none; }
.save-plot-btn .hint { display:none; }

/* =============== Tooltip =============== */

.tooltip {
    position: absolute;
    pointer-events: none;
    background: #10121a;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    padding: 8px 10px;
    font-size: 11px;
    color: #f5f5f5;
    box-shadow: 0 18px 35px rgba(0, 0, 0, 0.7);
    z-index: 20;
}

.tooltip strong {
    font-weight: 700;
}

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

.footer {
    margin-top: 36px;
    border-top: 1px solid var(--border-subtle);
    padding: 14px 0 10px;
    font-size: 12px;
    color: var(--text-muted);
    text-align: center;
    letter-spacing: 0.01em;
}

.footer-line {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* =============== Smoothing Control =============== */

.plot-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.plot-header-left {
    flex: 1 1 auto;
    min-width: 0;
}

.plot-header-left h2 {
    margin: 0;
}

.plot-header-left .plot-help {
    margin: 4px 0 0;
}

.smooth-control {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    margin-top: 2px;
    margin-right: 42px;
}

.smooth-label {
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.02em;
}

.segmented-control {
    display: inline-flex;
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    overflow: hidden;
    background: var(--bg-card);
}

.seg-btn {
    background: transparent;
    color: var(--text-muted);
    border: none;
    padding: 4px 12px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    line-height: 1.4;
}

.seg-btn:not(:last-child) {
    border-right: 1px solid var(--border-subtle);
}

.seg-btn:hover {
    background: rgba(255, 255, 255, 0.06);
    color: var(--text-main);
}

.seg-btn.active {
    background: rgba(255, 179, 71, 0.16);
    color: var(--accent);
}

/* =============== SVG + Axes =============== */

.axis text {
    fill: #eeeeee;
    font-size: 12px;
}

.axis path,
.axis line {
    stroke: #777777;
}

.grid line {
    stroke: #333333;
    stroke-opacity: 0.7;
    shape-rendering: crispEdges;
}

.grid path {
    stroke-width: 0;
}

/* Centralized styling for in-SVG exported titles. Applied to any <text>
   element with class `svg-plot-title`. This keeps the visual format in
   CSS (so plot modules only set position/content) and ensures consistent
   placement/contrast across plots. */
.svg-plot-title {
    fill: #ffffff !important;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
    font-size: 16px;
    font-weight: 600;
    pointer-events: none; /* title should not block interactions */
}

/* Slightly smaller variant (used by compact plots) */
.svg-plot-title.small {
    font-size: 13px;
    font-weight: 400;
}

/* Guide line in lap-time plot */
.guide-line {
    stroke: rgba(255, 255, 255, 0.35);
    stroke-width: 1;
    stroke-dasharray: 4 3;
}

/* Legend for lap-time plot */
.driver-legend text {
    font-size: 11px;
    fill: var(--text-main);
}

.driver-legend .legend-hit {
    cursor: pointer;
}

/* =============== Small screens (basic) =============== */

@media (max-width: 1100px) {
    #app {
        padding: 8px 6px 16px;
    }

    .plot-card {
        padding: 10px 10px 12px;
    }

    #control-card {
        padding: 10px 10px 8px;
    }

    .control-row {
        grid-template-columns: 66px 1fr;
    }

    /* Stack top panels vertically on smaller screens */
    #top-panels {
        flex-direction: column;
        gap: 12px;
    }

    .right-panel {
        width: 100%;
        flex: 1 1 100%;
    }
}