:root {
    --bg: #ffffff;
    --fg: #1d2330;
    --muted: #6b7280;
    --border: #d9dde5;
    --accent: #2563eb;
    --shadow: 0 6px 24px rgb(15 23 42 / 0.18);
    --chart-surface: #fcfcfb;
    --chart-ink: #0b0b0b;
    --chart-ink-2: #52514e;
    --chart-muted: #898781;
    --chart-grid: #e1e0d9;
    --chart-axis: #c3c2b7;
    --chart-series: #2a78d6;
    --chart-series-strong: #1c5cab;
    --chart-series-soft: #b7d3f6;
    --telemetry-height: 330px;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1b1f27;
        --fg: #e7e9ee;
        --muted: #9aa3b2;
        --border: #363c48;
        --accent: #4f8cff;
        --shadow: 0 6px 24px rgb(0 0 0 / 0.5);
        --chart-surface: #1a1a19;
        --chart-ink: #ffffff;
        --chart-ink-2: #c3c2b7;
        --chart-muted: #898781;
        --chart-grid: #2c2c2a;
        --chart-axis: #383835;
        --chart-series: #3987e5;
        --chart-series-strong: #86b6ef;
        --chart-series-soft: #184f95;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--fg);
}

#map {
    position: fixed;
    inset: 0;
}

.legend {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 1000;
    width: 270px;
    max-height: min(70vh, calc(100vh - 60px));
    overflow-y: auto;
    padding: 12px 14px;
    background: var(--bg);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

body.has-telemetry .legend { max-height: calc(100vh - var(--telemetry-height) - 36px); }

.legend__empty { padding: 4px 8px; color: var(--muted); font-size: 13px; }

.legend__header {
    display: flex;
    align-items: baseline;
    gap: 8px;
    cursor: pointer;
    list-style: none;
}

.legend__header::-webkit-details-marker { display: none; }

.legend__header::after {
    content: "▾";
    margin-left: auto;
    color: var(--muted);
    transition: transform 0.15s;
}

.legend:not([open]) .legend__header::after { transform: rotate(-90deg); }

.legend__title { font-size: 15px; font-weight: 600; }
.legend__count { color: var(--muted); font-size: 13px; }

.legend__list {
    display: grid;
    gap: 2px;
    margin: 10px 0 0;
    padding: 0;
    list-style: none;
}

.legend__row {
    display: flex;
    align-items: stretch;
    gap: 2px;
}

.legend__button {
    display: flex;
    flex: 1;
    align-items: center;
    gap: 10px;
    min-width: 0;
    padding: 6px 8px;
    border: 0;
    border-radius: 8px;
    background: none;
    color: var(--fg);
    font: inherit;
    text-align: left;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
}

.legend__button:hover,
.legend__expand:hover,
.legend__track-button:hover { background: rgb(127 127 127 / 0.12); }

.legend__button:focus-visible,
.legend__expand:focus-visible,
.legend__track-button:focus-visible { outline: 2px solid var(--accent); }

.legend__expand {
    flex: none;
    width: 30px;
    border: 0;
    border-radius: 8px;
    background: none;
    color: var(--muted);
    font: inherit;
    cursor: pointer;
}

.legend__expand::before {
    content: "▸";
    display: inline-block;
    transition: transform 0.15s;
}

.legend__item.is-open .legend__expand::before { transform: rotate(90deg); }

.legend__tracks {
    display: grid;
    gap: 1px;
    margin: 2px 0 6px 20px;
    padding: 0 0 0 10px;
    border-left: 2px solid var(--border);
    list-style: none;
}

.legend__tracks[hidden] { display: none; }

.legend__group {
    margin: 6px 0 2px;
    padding: 0 8px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.legend__track-button {
    display: grid;
    width: 100%;
    padding: 5px 8px;
    border: 0;
    border-radius: 6px;
    background: none;
    color: var(--fg);
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.legend__track.is-selected .legend__track-button { background: rgb(127 127 127 / 0.22); }

.legend__track-name {
    overflow: hidden;
    font-size: 13px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.legend__track-meta { color: var(--muted); font-size: 12px; }
.legend__item.is-selected .legend__button { background: rgb(127 127 127 / 0.2); }
.legend__item.is-dimmed .legend__button { opacity: 0.45; }

.legend__swatch {
    flex: none;
    width: 22px;
    height: 6px;
    border-radius: 3px;
}

.legend__item.is-selected .legend__swatch { height: 8px; }

.legend__text {
    display: grid;
    min-width: 0;
}

.legend__name {
    overflow: hidden;
    font-size: 14px;
    font-weight: 500;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.legend__meta { color: var(--muted); font-size: 12px; }

.legend__extras {
    margin-top: 6px;
    padding-top: 6px;
    border-top: 1px solid var(--border);
}

.legend__extras .legend__button[aria-pressed="false"] { opacity: 0.45; }

.charging-marker { background: none; border: 0; }

.charging-badge {
    display: grid;
    width: 30px;
    height: 30px;
    place-items: center;
    border: 2px solid #fff;
    border-radius: 50%;
    background: #ffc400;
    box-shadow: 0 2px 6px rgb(0 0 0 / 0.35);
    cursor: pointer;
}

.charging-badge::before {
    content: "";
    width: 14px;
    height: 18px;
    background: #1d2330;
    clip-path: polygon(58% 0, 8% 58%, 44% 58%, 34% 100%, 92% 38%, 55% 38%);
}

.charging-marker:hover .charging-badge { transform: scale(1.15); }

.charging-badge--small {
    flex: none;
    width: 22px;
    height: 22px;
    border-width: 0;
    box-shadow: none;
}

.charging-badge--small::before { width: 10px; height: 13px; }

.charging-popup { min-width: 200px; max-width: 260px; font-size: 13px; line-height: 1.45; }
.leaflet-popup-content .charging-popup__title { margin: 0; font-size: 15px; font-weight: 600; }
.leaflet-popup-content .charging-popup__place { margin: 0 0 6px; color: #6b7280; }
.leaflet-popup-content .charging-popup__text { margin: 0 0 6px; }
.leaflet-popup-content .charging-popup__meta { margin: 0; color: #6b7280; font-size: 12px; }
.charging-popup__link { display: inline-block; margin-top: 6px; font-weight: 600; }

.legend__reset {
    margin-top: 8px;
    padding: 4px 8px;
    border: 0;
    background: none;
    color: var(--accent);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}

.legend__reset[hidden] { display: none; }

.leaflet-container .leaflet-control-attribution {
    padding: 0 4px;
    background: rgb(255 255 255 / 0.5);
    color: #555;
    font-size: 9px;
    opacity: 0.7;
}

.leaflet-container .leaflet-control-attribution a { color: inherit; }

.photo-marker { background: none; border: 0; }

.photo-marker__inner {
    position: relative;
    display: block;
    width: 38px;
    height: 38px;
    overflow: hidden;
    border: 3px solid #fff;
    border-radius: 50%;
    background: #ddd;
    box-shadow: 0 2px 6px rgb(0 0 0 / 0.35);
    cursor: zoom-in;
    transition: transform 0.12s;
}

.photo-marker:hover .photo-marker__inner { transform: scale(1.25); }

.photo-marker__inner img,
.track-popup__photo img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.is-video::after {
    content: "▶";
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    background: rgb(0 0 0 / 0.3);
    color: #fff;
    font-size: 12px;
}

.track-popup { min-width: 180px; font-size: 13px; line-height: 1.45; }
.track-popup__title { margin: 0 0 4px; font-size: 15px; font-weight: 600; }
.track-popup__author { margin: 0 0 6px; }
.track-popup dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0; }
.track-popup dt { color: #6b7280; }
.track-popup dd { margin: 0; }

.track-popup__photos {
    display: grid;
    grid-template-columns: repeat(auto-fill, 52px);
    gap: 4px;
    max-width: 232px;
    margin-top: 10px;
}

.track-popup__photo {
    position: relative;
    width: 52px;
    height: 52px;
    padding: 0;
    overflow: hidden;
    border: 0;
    border-radius: 6px;
    background: #e5e7eb;
    color: #374151;
    font: inherit;
    font-weight: 600;
    cursor: zoom-in;
}

.track-popup__photo img { transition: transform 0.15s; }
.track-popup__photo:hover img { transform: scale(1.08); }

.track-popup__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
}

.share-button {
    padding: 6px 10px;
    border: 1px solid #c3c2b7;
    border-radius: 6px;
    background: #fff;
    color: #1d2330;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.share-button:hover { background: #f0efec; }
.share-button.is-done { border-color: #1f9d3a; color: #006300; }

.telemetry__header .share-button {
    margin-left: 8px;
    padding: 4px 10px;
    border-color: var(--chart-axis);
    background: none;
    color: var(--chart-ink);
    font-size: 12px;
}

.telemetry__header .share-button.is-done { border-color: #0ca30c; color: #0ca30c; }

.track-popup__telemetry {
    padding: 6px 10px;
    border: 0;
    border-radius: 6px;
    background: #2a78d6;
    color: #fff;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.map-tools {
    display: grid;
    overflow: hidden;
}

.draw-toggle {
    display: block;
    width: 100%;
    padding: 6px 10px;
    border: 0;
    border-radius: 0;
    text-align: left;
    background: #fff;
    color: #1d2330;
    font: 600 13px system-ui, -apple-system, sans-serif;
    cursor: pointer;
}

.draw-toggle:hover { background: #f4f4f4; }
.draw-toggle + .draw-toggle { border-top: 1px solid #ccc; }
.draw-toggle.is-active { background: #f07800; color: #fff; }
.draw-toggle.is-done { background: #e8f5e9; color: #006300; }

.is-drawing .leaflet-overlay-pane .leaflet-interactive,
.is-drawing .photo-marker,
.is-framing .leaflet-overlay-pane .leaflet-interactive,
.is-framing .photo-marker { pointer-events: none; }

.is-drawing .leaflet-container,
.is-framing .leaflet-container { cursor: crosshair; }

.draw-marker { background: none; border: 0; }

.draw-marker__dot {
    display: grid;
    width: 24px;
    height: 24px;
    place-items: center;
    border: 2px solid #fff;
    border-radius: 50%;
    background: #f07800;
    color: #fff;
    box-shadow: 0 1px 4px rgb(0 0 0 / 0.4);
    font: 700 11px system-ui, -apple-system, sans-serif;
    cursor: grab;
}

.draw-panel {
    position: absolute;
    top: 176px;
    left: 12px;
    z-index: 1000;
    width: 280px;
    padding: 12px 14px;
    background: var(--bg);
    color: var(--fg);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.draw-panel[hidden] { display: none; }

.draw-panel__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.draw-panel__title { margin: 0; font-size: 15px; }
.draw-panel__stats { margin: 6px 0 0; font-size: 20px; font-weight: 700; }
.draw-panel__status { min-height: 1.2em; margin: 2px 0 8px; color: var(--muted); font-size: 12px; }

.draw-panel__modes {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2px;
    padding: 2px;
    border-radius: 8px;
    background: rgb(127 127 127 / 0.15);
}

.draw-panel__mode {
    padding: 5px 6px;
    border: 0;
    border-radius: 6px;
    background: none;
    color: var(--fg);
    font: inherit;
    font-size: 12px;
    cursor: pointer;
}

.draw-panel__mode[aria-pressed="true"] { background: var(--bg); font-weight: 600; box-shadow: 0 1px 3px rgb(0 0 0 / 0.2); }

.draw-panel__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
}

.draw-panel__action {
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: none;
    color: var(--fg);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}

.draw-panel__action:disabled,
.draw-panel .share-button:disabled { opacity: 0.45; cursor: default; }

.draw-panel__hint { margin: 10px 0 0; color: var(--muted); font-size: 12px; line-height: 1.35; }

.telemetry {
    position: absolute;
    right: 12px;
    bottom: 20px;
    left: 12px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    height: var(--telemetry-height);
    padding: 10px 14px 12px;
    background: var(--chart-surface);
    color: var(--chart-ink);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.telemetry[hidden] { display: none; }

.telemetry__header {
    display: flex;
    align-items: center;
    gap: 16px;
}

.telemetry__title {
    margin: 0;
    overflow: hidden;
    font-size: 15px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.telemetry__scale {
    display: flex;
    align-items: center;
    gap: 2px;
    margin-left: auto;
    color: var(--chart-ink-2);
    font-size: 11px;
}

.telemetry__scale-step {
    display: grid;
    gap: 2px;
    min-width: 38px;
    text-align: center;
}

.telemetry__scale-step::before {
    content: "";
    display: block;
    height: 5px;
    border-radius: 3px;
    background: var(--step);
}

.telemetry__scale-step--gap { min-width: 62px; margin-left: 6px; }

.telemetry__scale-step--gap::before {
    background: repeating-linear-gradient(90deg, #7b8190 0 5px, transparent 5px 9px);
}

.telemetry__close {
    width: 30px;
    height: 30px;
    border: 0;
    border-radius: 50%;
    background: none;
    color: var(--chart-ink-2);
    font: inherit;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
}

.telemetry__close:hover { background: rgb(127 127 127 / 0.15); }

.telemetry__stats {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 22px;
    margin: 8px 0 6px;
}

.telemetry__stat { display: grid; }
.telemetry__stat dt { order: 2; color: var(--chart-muted); font-size: 11px; }
.telemetry__stat dd { order: 1; margin: 0; font-size: 15px; font-weight: 600; }
.telemetry__stat small { color: var(--chart-ink-2); font-size: 11px; font-weight: 400; }

.telemetry__body {
    display: grid;
    flex: 1;
    grid-template-columns: minmax(0, 1fr) 300px;
    gap: 20px;
    min-height: 0;
}

.telemetry__charts {
    display: grid;
    grid-template-rows: repeat(3, 1fr) auto;
    min-height: 0;
    min-width: 0;
}

.chart {
    position: relative;
    min-height: 0;
    margin: 0;
}

.chart__title {
    position: absolute;
    top: 0;
    left: 48px;
    z-index: 1;
    color: var(--chart-ink-2);
    font-size: 11px;
    pointer-events: none;
}

.chart__plot { height: 100%; }

.chart__readout {
    min-height: 1.3em;
    margin: 2px 0 0 48px;
    color: var(--chart-ink-2);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.chart__readout strong { color: var(--chart-ink); }

.uplot, .uplot * { font-family: inherit; }
.u-legend { display: none; }
.u-cursor-x { border-right: 1px solid var(--chart-muted) !important; }
.u-cursor-y { display: none; }

.efficiency {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow-y: auto;
}

.efficiency__title {
    margin: 0;
    color: var(--chart-ink-2);
    font-size: 12px;
    font-weight: 500;
}

.efficiency__verdict {
    margin: 4px 0 6px;
    font-size: 13px;
    line-height: 1.35;
}

.efficiency__bars {
    display: grid;
    gap: 2px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.efficiency__bar {
    display: grid;
    grid-template-columns: 78px 1fr 34px;
    align-items: center;
    gap: 6px;
    padding: 1px 2px;
    border-radius: 4px;
    font-size: 11px;
    font-variant-numeric: tabular-nums;
}

.efficiency__bar:hover,
.efficiency__bar:focus-visible { background: rgb(127 127 127 / 0.12); outline: none; }

.efficiency__label { color: var(--chart-ink-2); text-align: right; }
.efficiency__value { color: var(--chart-ink); }

.efficiency__track {
    height: 10px;
    border-radius: 0 4px 4px 0;
}

.efficiency__fill {
    height: 100%;
    border-radius: 0 4px 4px 0;
    background: var(--chart-series);
}

.efficiency__bar.is-best .efficiency__fill { background: var(--chart-series-strong); }
.efficiency__bar.is-best .efficiency__value { font-weight: 700; }

.efficiency__bar.is-thin .efficiency__fill {
    background: repeating-linear-gradient(135deg, var(--chart-series-soft) 0 3px, transparent 3px 6px);
}

.efficiency__bar.is-thin .efficiency__value { color: var(--chart-muted); }

.efficiency__note {
    margin: 6px 0 0;
    color: var(--chart-muted);
    font-size: 11px;
    line-height: 1.35;
}

.efficiency__tip {
    position: fixed;
    z-index: 6000;
    padding: 6px 8px;
    background: var(--chart-surface);
    color: var(--chart-ink-2);
    border: 1px solid var(--chart-axis);
    border-radius: 6px;
    box-shadow: var(--shadow);
    font-size: 12px;
    line-height: 1.4;
    pointer-events: none;
}

.efficiency__tip strong { color: var(--chart-ink); }

.lightbox {
    position: fixed;
    inset: 0;
    z-index: 5000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgb(10 12 16 / 0.92);
}

.lightbox[hidden] { display: none; }

body.has-lightbox { overflow: hidden; }

.lightbox__figure {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 48px 72px 20px;
}

.lightbox__image,
.lightbox__video {
    max-width: 100%;
    max-height: calc(100% - 40px);
    width: auto;
    height: auto;
    border-radius: 4px;
    object-fit: contain;
    box-shadow: 0 10px 40px rgb(0 0 0 / 0.5);
}

.lightbox__image[hidden],
.lightbox__video[hidden] { display: none; }

.lightbox__caption {
    color: #d1d5db;
    font-size: 14px;
    text-align: center;
}

.lightbox__close,
.lightbox__nav {
    position: absolute;
    z-index: 1;
    border: 0;
    border-radius: 50%;
    background: rgb(255 255 255 / 0.12);
    color: #fff;
    font: inherit;
    line-height: 1;
    cursor: pointer;
}

.lightbox__close:hover,
.lightbox__nav:hover { background: rgb(255 255 255 / 0.25); }

.lightbox__close:focus-visible,
.lightbox__nav:focus-visible { outline: 2px solid #fff; }

.lightbox__close {
    top: 12px;
    right: 12px;
    width: 40px;
    height: 40px;
    font-size: 28px;
}

.lightbox__nav {
    top: 50%;
    width: 48px;
    height: 48px;
    margin-top: -24px;
    font-size: 34px;
}

.lightbox__prev { left: 12px; }
.lightbox__next { right: 12px; }
.lightbox.is-single .lightbox__nav { display: none; }

@media (max-width: 900px) {
    :root { --telemetry-height: 62vh; }

    .telemetry__body {
        grid-template-columns: 1fr;
        overflow-y: auto;
    }

    .telemetry__charts { grid-template-rows: repeat(3, 110px) auto; }
    .telemetry__scale { display: none; }
}

@media (max-width: 600px) {
    .telemetry { right: 0; bottom: 0; left: 0; border-radius: 12px 12px 0 0; }

    .lightbox__figure { padding: 56px 8px 16px; }
    .lightbox__nav { top: auto; bottom: 16px; margin-top: 0; }

    .draw-panel { top: auto; right: 0; bottom: 0; left: 0; width: auto; border-radius: 12px 12px 0 0; }
    .draw-panel__hint { display: none; }

    .legend {
        left: 56px;
        width: auto;
        max-height: 40vh;
        padding: 10px 12px;
    }
}
