/* KartStats — feuille de style unique.
   Les couleurs de données sont définies par rôle (--series-N) : les valeurs
   claires et sombres sont choisies pour leur surface respective, et non
   inversées automatiquement. */

:root {
    color-scheme: light;
    --surface-0: #f4f3f0;
    --surface-1: #fcfcfb;
    --surface-2: #eeede9;
    --border: #dddbd4;
    --border-strong: #c4c1b7;
    --text-primary: #0b0b0b;
    --text-secondary: #52514e;
    --text-muted: #7d7b74;
    --series-1: #2a78d6;
    --series-2: #eb6834;
    --series-3: #1baf7a;
    --series-4: #eda100;
    --series-5: #e87ba4;
    --good: #008300;
    --grid: #e6e4de;
    --accent-soft: #e8f0fc;
    --radius: 10px;
    --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Menlo", monospace;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --surface-0: #121211;
        --surface-1: #1a1a19;
        --surface-2: #232322;
        --border: #35352f;
        --border-strong: #4a4a43;
        --text-primary: #ffffff;
        --text-secondary: #c3c2b7;
        --text-muted: #918f85;
        --series-1: #3987e5;
        --series-2: #d95926;
        --series-3: #199e70;
        --series-4: #c98500;
        --series-5: #d55181;
        --good: #3fa93f;
        --grid: #2b2b28;
        --accent-soft: #1d2937;
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232322;
    --border: #35352f;
    --border-strong: #4a4a43;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #918f85;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --good: #3fa93f;
    --grid: #2b2b28;
    --accent-soft: #1d2937;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--surface-0);
    color: var(--text-primary);
    font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--series-1); text-decoration: none; }
a:hover { text-decoration: underline; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 20px 64px; }

/* --- en-tête ----------------------------------------------------------- */
header.top {
    background: var(--surface-1);
    border-bottom: 1px solid var(--border);
    margin-bottom: 28px;
}
header.top .wrap { display: flex; align-items: center; gap: 24px; padding: 14px 20px; }
.brand { font-weight: 650; font-size: 17px; letter-spacing: -0.01em; color: var(--text-primary); }
.brand span { color: var(--series-2); }
nav.main { display: flex; gap: 18px; flex: 1; }
nav.main a { color: var(--text-secondary); font-size: 14px; }
nav.main a.active { color: var(--text-primary); font-weight: 600; }
nav.main a .badge {
    display: inline-block; margin-left: 5px; padding: 0 6px;
    font-size: 11px; font-weight: 700; line-height: 17px;
    border-radius: 999px; background: var(--series-2); color: #fff;
}

button.theme {
    background: var(--surface-2); border: 1px solid var(--border);
    color: var(--text-secondary); border-radius: 8px;
    padding: 5px 10px; font-size: 13px; cursor: pointer;
}

/* --- titres ------------------------------------------------------------ */
h1 { font-size: 26px; letter-spacing: -0.02em; margin: 0 0 4px; }
h2 { font-size: 17px; letter-spacing: -0.01em; margin: 32px 0 12px; }
.subtitle { color: var(--text-secondary); font-size: 14px; margin: 0 0 24px; }

/* --- tuiles de chiffres ------------------------------------------------ */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.tile {
    background: var(--surface-1); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 14px 16px;
}
.tile .label { font-size: 12px; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.04em; }
.tile .value { font-size: 24px; font-weight: 600; letter-spacing: -0.02em; margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile .value.mono { font-family: var(--mono); }
.tile .hint { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* --- cartes et tableaux ------------------------------------------------ */
.card {
    background: var(--surface-1); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px;
}
.card > h3 { margin: 0 0 2px; font-size: 15px; font-weight: 600; }
.card > .cap { margin: 0 0 14px; font-size: 13px; color: var(--text-secondary); }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th {
    text-align: left; font-weight: 600; font-size: 12px; color: var(--text-secondary);
    text-transform: uppercase; letter-spacing: 0.04em;
    padding: 6px 10px; border-bottom: 1px solid var(--border-strong);
}
td { padding: 7px 10px; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.time { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.rank { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.best { color: var(--good); font-weight: 600; }

.pill {
    display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 999px;
    background: var(--surface-2); color: var(--text-secondary); border: 1px solid var(--border);
}
.pill.live { background: var(--accent-soft); color: var(--series-1); border-color: var(--series-1); }

.empty { color: var(--text-muted); font-size: 14px; padding: 24px 0; text-align: center; }

/* --- recherche --------------------------------------------------------- */
form.search { display: flex; gap: 8px; margin-bottom: 20px; }
form.search input {
    flex: 1; padding: 9px 12px; font-size: 15px;
    background: var(--surface-1); color: var(--text-primary);
    border: 1px solid var(--border-strong); border-radius: 8px;
}
form.search button {
    padding: 9px 18px; font-size: 14px; font-weight: 600; cursor: pointer;
    background: var(--series-1); color: #fff; border: none; border-radius: 8px;
}

/* --- graphiques -------------------------------------------------------- */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-text { fill: var(--text-muted); font-size: 11px; font-family: inherit; }
.chart .axis-line { stroke: var(--border-strong); stroke-width: 1; }
.chart .crosshair { stroke: var(--border-strong); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .marker { stroke: var(--surface-1); stroke-width: 2; }

.legend { display: flex; flex-wrap: wrap; gap: 16px; margin: 0 0 10px; font-size: 13px; color: var(--text-secondary); }
.legend .item { display: flex; align-items: center; gap: 6px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 3px; }

.tooltip {
    position: absolute; pointer-events: none; z-index: 20;
    background: var(--surface-1); border: 1px solid var(--border-strong);
    border-radius: 8px; padding: 8px 10px; font-size: 13px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, .14); white-space: nowrap;
    opacity: 0; transition: opacity .1s;
}
.tooltip .t-title { font-weight: 600; margin-bottom: 4px; color: var(--text-primary); }
.tooltip .t-row { display: flex; align-items: center; gap: 7px; color: var(--text-secondary); }
.tooltip .t-row b { color: var(--text-primary); font-family: var(--mono); font-weight: 600; }

details.table-view { margin-top: 12px; }
details.table-view summary {
    cursor: pointer; font-size: 13px; color: var(--text-secondary); user-select: none;
}
details.table-view[open] summary { margin-bottom: 10px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

/* --- sous-navigation d'une piste ---------------------------------------- */
nav.subnav {
    display: flex; gap: 4px; margin: 0 0 22px;
    border-bottom: 1px solid var(--border);
}
nav.subnav a {
    padding: 8px 14px; font-size: 14px; color: var(--text-secondary);
    border-bottom: 2px solid transparent; margin-bottom: -1px;
}
nav.subnav a:hover { color: var(--text-primary); text-decoration: none; }
nav.subnav a.active {
    color: var(--text-primary); font-weight: 600;
    border-bottom-color: var(--series-1);
}
nav.subnav .count {
    display: inline-block; margin-left: 5px; padding: 0 6px;
    font-size: 11px; font-weight: 600; line-height: 17px;
    border-radius: 999px; background: var(--surface-2);
    color: var(--text-secondary); border: 1px solid var(--border);
}

/* Une tuile peut être un lien : elle doit alors le montrer. */
a.tile { display: block; color: inherit; }
a.tile:hover {
    border-color: var(--series-1); text-decoration: none;
}
a.tile:hover .hint { color: var(--series-1); }

/* --- barre de filtres serveur et pagination ----------------------------- */
form.filter-bar {
    display: flex; flex-wrap: wrap; align-items: center; gap: 14px;
    font-size: 14px; color: var(--text-secondary);
}
form.filter-bar label { display: flex; align-items: center; gap: 7px; }
form.filter-bar input, form.filter-bar select {
    padding: 6px 9px; font-size: 14px;
    background: var(--surface-2); color: var(--text-primary);
    border: 1px solid var(--border-strong); border-radius: 7px;
}
form.filter-bar button {
    padding: 7px 16px; font-size: 14px; font-weight: 600; cursor: pointer;
    background: var(--series-1); color: #fff; border: none; border-radius: 7px;
}

nav.pager {
    display: flex; align-items: center; justify-content: center; gap: 20px;
    font-size: 14px; color: var(--text-secondary); margin-top: 4px;
}
nav.pager .disabled { color: var(--text-muted); opacity: .5; }

/* --- filtrage et tri des tableaux --------------------------------------- */
th.sortable { cursor: pointer; user-select: none; position: relative; padding-right: 16px; }
th.sortable:hover { color: var(--text-primary); }
th.sortable::after {
    content: "↕"; position: absolute; right: 4px; opacity: .3; font-size: 10px;
}
th.sortable[data-sort="asc"]::after { content: "↑"; opacity: 1; color: var(--series-1); }
th.sortable[data-sort="desc"]::after { content: "↓"; opacity: 1; color: var(--series-1); }

tr.filter-row td { padding: 4px 6px 8px; border-bottom: 1px solid var(--border-strong); }
tr.filter-row input {
    width: 100%; min-width: 0; padding: 4px 7px; font-size: 12px;
    background: var(--surface-2); color: var(--text-primary);
    border: 1px solid var(--border); border-radius: 6px;
}
tr.filter-row input:focus {
    outline: none; border-color: var(--series-1);
    background: var(--surface-1);
}

.table-status {
    display: flex; align-items: center; gap: 12px;
    margin-top: 10px; font-size: 12px; color: var(--text-muted);
}
.table-status.empty-result { color: var(--series-2); }

/* --- endurance ---------------------------------------------------------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-secondary); }
.form-grid input, .form-grid select, section[data-panel] td select, section[data-panel] td input,
.form-row input, .form-row select {
    padding: 6px 8px; font-size: 14px; background: var(--surface-2); color: var(--text-primary);
    border: 1px solid var(--border-strong); border-radius: 7px;
}
.form-row button.secondary, button.secondary {
    background: var(--surface-2); color: var(--text-primary); border: 1px solid var(--border-strong);
    margin-left: 0; padding: 8px 14px; font-size: 14px; font-weight: 500; border-radius: 8px; cursor: pointer;
}
.small-muted { font-size: 12px; color: var(--text-muted); }
.driver-tag {
    display: inline-block; padding: 1px 7px; border-radius: 5px; color: #fff;
    font-size: 11px; font-weight: 700; letter-spacing: .02em;
}
.cell-good { color: var(--good); font-weight: 600; }
.cell-bad { color: var(--series-2); font-weight: 600; }
input.cell-bad { border-color: var(--series-2) !important; }
tr.me { background: var(--accent-soft); }
tr.me:hover { background: var(--accent-soft); }

/* --- formulaires d'administration -------------------------------------- */
textarea {
    width: 100%; padding: 10px 12px; font-size: 14px;
    font-family: var(--mono); line-height: 1.5; resize: vertical;
    background: var(--surface-2); color: var(--text-primary);
    border: 1px solid var(--border-strong); border-radius: 8px;
}
.form-row { display: flex; align-items: center; gap: 16px; margin-top: 12px; }
.form-row label { font-size: 14px; color: var(--text-secondary); display: flex; align-items: center; gap: 7px; }
.form-row button {
    margin-left: auto; padding: 9px 18px; font-size: 14px; font-weight: 600;
    cursor: pointer; background: var(--series-1); color: #fff;
    border: none; border-radius: 8px;
}

td.actions { white-space: nowrap; }
td.actions form { display: inline; }
button.link {
    background: none; border: none; padding: 0 8px 0 0; cursor: pointer;
    font: inherit; font-size: 13px; color: var(--series-1);
}
button.link:hover { text-decoration: underline; }
button.link.danger { color: var(--series-2); }
details.rename { display: inline; }
details.rename summary {
    display: inline; list-style: none; cursor: pointer;
    font-size: 13px; color: var(--series-1); padding-right: 8px;
}
details.rename summary::-webkit-details-marker { display: none; }
details.rename summary:hover { text-decoration: underline; }
details.rename[open] form { display: flex; gap: 8px; margin-top: 8px; }
details.rename input {
    flex: 1; min-width: 180px; padding: 6px 8px; font-size: 14px;
    background: var(--surface-2); color: var(--text-primary);
    border: 1px solid var(--border-strong); border-radius: 7px;
}
details.rename button.secondary { padding: 6px 12px; font-size: 13px; }

.notice {
    background: var(--accent-soft); border: 1px solid var(--series-1);
    border-radius: var(--radius); padding: 12px 16px; font-size: 14px;
    color: var(--text-secondary); margin-bottom: 20px;
}

/* --- carte en direct ---------------------------------------------------- */
.track-map .map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; margin-bottom: 12px; }
.track-map .map-canvas { position: relative; width: 100%; }
@media (max-width: 900px) { .track-map .map-layout { grid-template-columns: 1fr; } }
.track-map .map-kart { cursor: pointer; outline: none; }
.track-map .map-kart:focus-visible circle { stroke-width: 4; }
.track-map svg { display: block; width: 100%; height: auto; max-height: 460px; }
.track-map .map-image { opacity: .55; }
.track-map .map-track path { fill: none; stroke: #c0c0c0; stroke-width: 7; stroke-linejoin: round; }
.track-map .map-track path[data-type="out"] { opacity: 0; }
.track-map .map-track circle[data-type="sf"] { display: none; }
.track-map .map-track rect[data-type="sf"] { fill: var(--series-1); }
.track-map .map-kart circle {
    fill: var(--surface-1); stroke: var(--series-1); stroke-width: 2;
}
.track-map .map-kart text {
    font: 700 9px var(--mono); fill: var(--text-primary); pointer-events: none;
}
.track-map .map-kart.leader circle { fill: var(--series-4); stroke: var(--series-2); }
.track-map .map-kart.leader text { fill: #000; }
.track-map .map-kart.stale { opacity: .45; }
.track-map .map-kart:hover circle { stroke-width: 4; }

.stats-btn {
    display: inline-block; margin-left: 8px; padding: 1px 8px; vertical-align: 1px;
    font-size: 11px; font-weight: 600; letter-spacing: .02em; text-decoration: none;
    color: var(--series-1); border: 1px solid var(--series-1); border-radius: 999px;
}
.stats-btn:hover { background: var(--accent-soft); text-decoration: none; }

/* --- karts mis en évidence ------------------------------------------------ */
th.follow-col, td.follow-col { width: 1%; padding-right: 0; }
.follow-btn {
    background: none; border: none; padding: 0 2px; cursor: pointer;
    font-size: 17px; line-height: 1; color: var(--text-muted);
}
.follow-btn[aria-pressed="true"] { color: var(--follow-color, var(--series-4)); }
.follow-btn[aria-pressed="false"]:hover { color: var(--text-primary); }
tr.followed, tr.followed:hover { background: color-mix(in srgb, var(--follow-color, var(--series-1)) 16%, transparent); }
tr.followed td:first-child { box-shadow: inset 4px 0 0 var(--follow-color, var(--series-4)); }
.track-map svg.has-followed .map-kart:not(.followed) { opacity: .35; }
.track-map .map-kart.followed circle { r: 12px; stroke: #fff; stroke-width: 2.5; fill: var(--follow-color, var(--series-1)); }
.track-map .map-kart.followed text { fill: var(--follow-text, #fff); font-size: 11px; }
/* Kart ordinaire qui chevauche un kart suivi : visible par-dessus. */
.track-map svg.has-followed .map-kart.overlap { opacity: 1; }
.track-map .map-kart.overlap circle { stroke: var(--text-primary); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* --- fiches des karts suivis, à côté de la carte ----------------------------- */
.map-side { display: grid; gap: 10px; align-content: start; max-height: 460px; overflow-y: auto; }
.map-side-hint { margin: 0; font-size: 13px; color: var(--text-muted); }
.map-card {
    padding: 10px 12px; border-radius: 8px; font-size: 13px;
    background: var(--surface-2); border-left: 4px solid var(--follow-color, var(--series-1));
}
.map-card header { display: flex; align-items: center; gap: 8px; }
.map-card-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.map-card-close {
    background: none; border: none; cursor: pointer; font-size: 18px; line-height: 1;
    color: var(--text-muted); padding: 0 2px;
}
.map-card-close:hover { color: var(--text-primary); }
.map-card-sub { font-size: 12px; color: var(--text-secondary); }
.map-card-sub:empty { display: none; }
.map-card dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 8px 0 6px; }
.map-card dt { color: var(--text-secondary); }
.map-card dd { margin: 0; text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
/* Voisins sur la piste : libellé puis valeur sur leur propre ligne. */
.map-card dt.wide, .map-card dd.wide { grid-column: 1 / -1; }
.map-card dd.wide { text-align: left; font-family: inherit; margin-bottom: 2px; }
.map-card dt.wide { margin-top: 4px; }
.map-card a { font-size: 12px; }
.map-card-links { display: flex; justify-content: space-between; gap: 8px; }
.map-follow-chip {
    min-width: 26px; padding: 2px 6px; border-radius: 999px; text-align: center;
    font: 700 12px var(--mono);
    background: var(--follow-color, var(--series-1)); color: var(--follow-text, #fff);
}

/* Voisins dans une fiche : numéro de kart et position bien visibles. */
.map-card .nb { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.nb-kart {
    min-width: 28px; padding: 1px 7px; border-radius: 999px; text-align: center;
    font: 700 13px var(--mono);
    background: var(--surface-1); color: var(--text-primary); border: 1.5px solid var(--border-strong);
}
.nb-kart.is-followed {
    background: var(--follow-color); color: var(--follow-text); border-color: var(--follow-color);
}
.nb-pos {
    padding: 1px 6px; border-radius: 5px; font: 700 12px var(--mono);
    background: var(--series-1); color: #fff;
}
.nb-gap { font-family: var(--mono); font-size: 14px; }
.nb-note { font-size: 12px; color: var(--series-4); font-weight: 600; }
.nb-laps { display: block; font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.map-card-title .nb-pos { margin-right: 4px; vertical-align: 1px; }

/* --- partage vers un téléphone ------------------------------------------------ */
.map-cards { display: grid; gap: 10px; }
.share-btn {
    justify-self: start; padding: 6px 12px; font-size: 13px; cursor: pointer;
    background: var(--surface-2); color: var(--text-primary);
    border: 1px solid var(--border-strong); border-radius: 8px;
}
.share-btn:hover { border-color: var(--series-1); }
.share-dialog {
    max-width: min(420px, calc(100vw - 32px)); padding: 20px; border-radius: var(--radius);
    background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border-strong);
}
.share-dialog::backdrop { background: rgba(0, 0, 0, .5); }
.share-dialog h3 { margin: 0 0 6px; }
.share-qr { display: block; margin: 12px auto; background: #fff; border-radius: 8px; }
.share-link input {
    width: 100%; padding: 6px 8px; font: 13px var(--mono);
    background: var(--surface-2); color: var(--text-primary);
    border: 1px solid var(--border-strong); border-radius: 7px;
}
.share-warn { color: var(--series-2); }
.share-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.share-actions button {
    padding: 7px 14px; font-size: 14px; cursor: pointer; border-radius: 8px;
    background: var(--series-1); color: #fff; border: none;
}
.share-actions button.secondary { background: var(--surface-2); color: var(--text-primary); border: 1px solid var(--border-strong); }

/* Page de suivi (téléphone) : fiches d'abord, carte ensuite. */
.share-head h1 { margin-bottom: 2px; }
.share-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.track-map.share-view .map-side { max-height: none; overflow: visible; }
@media (max-width: 900px) {
    .track-map.share-view .map-side { order: -1; }
}

/* --- en-tête sur téléphone ----------------------------------------------------- */
/* La navigation passe sous le logo et défile horizontalement, au lieu de
   pousser la page hors de l'écran. */
@media (max-width: 640px) {
    header.top .wrap { flex-wrap: wrap; gap: 8px 16px; padding: 10px 16px; }
    header.top button.theme { margin-left: auto; }
    nav.main {
        order: 3; flex: 1 0 100%; gap: 16px; overflow-x: auto; white-space: nowrap;
        scrollbar-width: none; padding-bottom: 2px;
    }
    nav.main::-webkit-scrollbar { display: none; }
    nav.main a { flex: none; }
}

/* bouton d'aspect lien (actions secondaires dans un tableau) */
.btn-link { background: none; border: 0; padding: 0; color: var(--text-muted); cursor: pointer; text-decoration: underline; font: inherit; }
.btn-link:hover { color: var(--text-primary); }
