/* EventTracker - compléments à Bootstrap 6 */

/* Palette des graphiques (palette de référence validée daltonisme, clair / sombre). */
:root,
[data-bs-theme="light"] {
    --viz-font: system-ui, -apple-system, "Segoe UI", sans-serif;
    --viz-surface: #fcfcfb;
    --viz-tooltip-bg: #ffffff;
    --viz-ink: #0b0b0b;
    --viz-muted: #898781;
    --viz-grid: #e1e0d9;
    --viz-series-1: #2a78d6;
    --viz-series-2: #eb6834;
    --viz-series-3: #1baf7a;
    --viz-series-4: #eda100;
    --viz-track: #cde2fb;
    /* Séquentiel (bleu) : peu -> beaucoup */
    --viz-seq-1: #cde2fb;
    --viz-seq-2: #86b6ef;
    --viz-seq-3: #3987e5;
    --viz-seq-4: #256abf;
    --viz-seq-5: #104281;
    --viz-empty: #e1e0d9;
    /* Carte : dégradé jaune -> orange -> rouge -> bordeaux (peu -> beaucoup, de plus en plus foncé) */
    --map-seq-1: #fed976;
    --map-seq-2: #fd9f44;
    --map-seq-3: #fc4e2a;
    --map-seq-4: #cf0f23;
    --map-seq-5: #800026;
}

[data-bs-theme="dark"] {
    --viz-surface: #1a1a19;
    --viz-tooltip-bg: #262624;
    --viz-ink: #f0efec;
    --viz-muted: #898781;
    --viz-grid: #2c2c2a;
    --viz-series-1: #3987e5;
    --viz-series-2: #d95926;
    --viz-series-3: #199e70;
    --viz-series-4: #c98500;
    --viz-track: #184f95;
    /* Séquentiel sombre : les faibles valeurs se fondent dans le fond, les fortes ressortent */
    --viz-seq-1: #184f95;
    --viz-seq-2: #256abf;
    --viz-seq-3: #3987e5;
    --viz-seq-4: #6da7ec;
    --viz-seq-5: #b7d3f6;
    --viz-empty: #2c2c2a;
    /* Carte : même dégradé ; bordeaux un peu éclairci pour rester visible sur le fond sombre */
    --map-seq-1: #fed976;
    --map-seq-2: #fd9f44;
    --map-seq-3: #fc4e2a;
    --map-seq-4: #d7142a;
    --map-seq-5: #9e0a30;
}

body {
    background-color: var(--bs-bg-body);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* --- Mise en page ------------------------------------------------------- */
.app-navbar {
    border-bottom: 1px solid var(--bs-border-color);
    position: sticky;
    top: 0;
    z-index: 1020;
}

.app-navbar .navbar-brand .bi {
    color: var(--viz-series-1);
}

.app-navbar .navbar-nav .nav-link .bi {
    margin-inline-end: .25rem;
}

.navbar-user {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .5rem;
    margin-inline-start: auto;
    padding-top: 1rem;
}

/* Barre déployée à partir de 1280 px (en dessous : menu latéral). Jusqu'à 1700 px, les icônes du menu
   sont masquées pour que tout tienne sur une ligne. */
@media (width >= 1280px) {
    .navbar-user {
        flex-wrap: nowrap;
        padding-top: 0;
    }
}

@media (1280px <= width < 1700px) {
    .app-navbar .navbar-nav .nav-link > .bi,
    .search-trigger-label {
        display: none;
    }
}

.app-main {
    flex: 1;
    max-width: 1440px;
    margin-inline: auto;
    padding-block: 1.5rem 3rem;
}

.app-footer {
    max-width: 1440px;
    margin-inline: auto;
    padding-block: 1rem;
    border-top: 1px solid var(--bs-border-color);
}

.page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.page-header h1 {
    text-wrap: balance;
}

/* --- Tuiles statistiques ------------------------------------------------ */
.stat-tile .card-body {
    display: flex;
    flex-direction: column;
    gap: .25rem;
}

.stat-label .bi {
    margin-inline-end: .25rem;
}

.stat-value {
    font-size: clamp(1.5rem, 2.5vw, 2rem);
    font-weight: 600;
    line-height: 1.15;
}

.num {
    font-variant-numeric: tabular-nums;
}

/* --- Graphiques --------------------------------------------------------- */
.chart-figure {
    margin: 0;
}

.chart-box {
    position: relative;
    width: 100%;
}

.chart-table {
    margin-top: .75rem;
}

.chart-table summary {
    cursor: pointer;
}

.chart-table table {
    margin-top: .5rem;
}

/* Barre de proportion (communes) et jauge (appareils) */
.share-table .share-col {
    width: 35%;
    vertical-align: middle;
}

.share-bar,
.meter-fill {
    height: 8px;
    width: var(--share);
    min-width: 2px;
    border-radius: 0 4px 4px 0;
    background-color: var(--viz-series-1);
}

.meter {
    height: 8px;
    border-radius: 4px;
    background-color: var(--viz-track);
    margin: .25rem 0 .75rem;
    overflow: hidden;
}

.device-list li:last-child .meter {
    margin-bottom: 0;
}

/* --- Tableaux ----------------------------------------------------------- */
.events-table .event-desc {
    max-width: 48ch;
}

.table th {
    font-weight: 600;
    white-space: nowrap;
}

/* --- Fiche personne ----------------------------------------------------- */
.person-dl dt {
    font-weight: 500;
    font-size: var(--bs-font-size-sm);
    color: var(--bs-fg-2);
    margin-top: .75rem;
}

.person-dl dt:first-child {
    margin-top: 0;
}

.person-dl dd {
    margin-bottom: 0;
    overflow-wrap: anywhere;
}

.participation-message {
    max-width: 60ch;
    font-style: italic;
}

/* --- Formulaires -------------------------------------------------------- */
.form-card fieldset legend {
    margin-bottom: 0;
}

.narrow {
    max-width: 32rem;
}

/* --- Connexion ---------------------------------------------------------- */
.auth-body {
    justify-content: center;
    align-items: center;
    padding: 1rem;
}

.auth-main {
    width: 100%;
    max-width: 24rem;
}

/* reCAPTCHA v2 : la case Google (304 px) centrée dans le formulaire */
.recaptcha {
    display: flex;
    justify-content: center;
}

.auth-logo {
    font-size: 2.25rem;
    color: var(--viz-series-1);
    line-height: 1;
    margin-bottom: .5rem;
}

/* --- Erreurs ------------------------------------------------------------ */
.error-code {
    font-size: 4rem;
    font-weight: 700;
    line-height: 1;
}

.error-trace {
    max-height: 24rem;
    overflow: auto;
    padding: 1rem;
    background: var(--bs-bg-2);
    border-radius: .5rem;
}

/* --- Doublons ----------------------------------------------------------- */
.dup-row {
    display: grid;
    grid-template-columns: auto 1fr 1fr minmax(12rem, 16rem);
    gap: 1rem;
    align-items: start;
}

@media (width < 1024px) {
    .dup-row {
        grid-template-columns: auto 1fr;
    }
    .dup-row .dup-side + .dup-side,
    .dup-row .dup-why {
        grid-column: 2;
    }
}

.dup-person {
    min-width: 0;
}

.dup-score .meter {
    margin: .25rem 0 .5rem;
}

.compare-dl .shared,
.shared {
    font-weight: 600;
}

.compare-dl .shared::after {
    content: " · identique";
    font-weight: 400;
    color: var(--bs-fg-3);
    font-size: var(--bs-font-size-sm);
}

.compare-history li {
    padding-block: .25rem;
    border-bottom: 1px solid var(--bs-border-color);
}

.compare-history li:last-child {
    border-bottom: 0;
}

.decision-bar {
    position: sticky;
    bottom: 1rem;
    box-shadow: var(--bs-box-shadow-lg);
}

.merge-list li + li {
    margin-top: .75rem;
    padding-top: .75rem;
    border-top: 1px solid var(--bs-border-color);
}

/* --- Carte -------------------------------------------------------------- */
/* Ancre « #carte » : ne pas passer sous la barre de navigation fixe. */
#carte {
    scroll-margin-top: 5rem;
}

.map-box {
    height: clamp(360px, 62vh, 640px);
    border-radius: .5rem;
    background: var(--viz-surface);
}

.map-box.leaflet-container {
    font: inherit;
    background: var(--viz-surface);
}

/* Plan IGN en gris : ses couleurs (routes, forêts) ne se confondent pas avec celles des données. */
.map-tiles-plan {
    filter: grayscale(1) contrast(.9) brightness(1.05);
}

[data-bs-theme="dark"] .map-tiles-plan {
    filter: grayscale(1) invert(1) contrast(.85) brightness(.9);
}

[data-bs-theme="dark"] .map-tiles-photo,
[data-bs-theme="dark"] .map-tiles-color {
    filter: brightness(.8);
}

/* Pas de cadre de focus rectangulaire autour d'une zone cliquée (le survol la met déjà en évidence). */
.map-box path.leaflet-interactive:focus:not(:focus-visible) {
    outline: none;
}

.map-opacity .form-range {
    width: 8rem;
}

.map-opacity input:disabled {
    opacity: .4;
}

.map-legend {
    padding: .5rem .75rem;
    background: var(--viz-tooltip-bg);
    color: var(--viz-ink);
    border: 1px solid var(--viz-grid);
    border-radius: .5rem;
    font-size: var(--bs-font-size-sm);
    line-height: 1.6;
}

.map-legend-title {
    font-weight: 600;
    margin-bottom: .25rem;
}

.map-legend-swatch {
    display: inline-block;
    width: .875rem;
    height: .875rem;
    border-radius: 3px;
    margin-inline-end: .5rem;
    vertical-align: -2px;
    box-shadow: inset 0 0 0 1px rgb(0 0 0 / .25); /* liseré plus foncé, comme les zones */
}

.leaflet-tooltip.map-tooltip {
    background: var(--viz-tooltip-bg);
    color: var(--viz-ink);
    border: 1px solid var(--viz-grid);
    box-shadow: var(--bs-box-shadow-lg);
}

.map-ring-label {
    color: var(--viz-ink);
    font-weight: 600;
    text-shadow: 0 0 3px var(--viz-surface), 0 0 3px var(--viz-surface), 0 0 2px var(--viz-surface);
    font-size: .75rem;
    white-space: nowrap;
    transform: translate(-50%, -100%);
}

.leaflet-control-attribution {
    font-size: .6875rem;
}

.leaflet-control-attribution,
.leaflet-control-zoom a {
    background: var(--viz-tooltip-bg) !important;
    color: var(--viz-ink) !important;
}

/* Libellé affiché seulement dans le menu mobile (icône seule dans la barre). */
@media (width >= 1280px) {
    .label-mobile-only {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }
}

/* --- Comparaison -------------------------------------------------------- */
.compare-picker {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
    gap: 1rem 1.5rem;
}

.compare-picker fieldset .form-field {
    margin-bottom: .25rem;
}

.series-key {
    display: inline-block;
    width: .75rem;
    height: .75rem;
    border-radius: 3px;
    background: var(--key);
    margin-inline-end: .375rem;
    vertical-align: -1px;
}

.compare-table th[scope="row"] {
    white-space: nowrap;
}

/* --- Fidélité : carte de chaleur des cohortes ---------------------------- */
.cohort-table td.heat {
    min-width: 4.5rem;
    border: 2px solid var(--viz-surface);
}

.heat-1 { background: var(--viz-seq-1); color: var(--viz-ink); }
.heat-2 { background: var(--viz-seq-2); color: var(--viz-ink); }
.heat-3 { background: var(--viz-seq-3); color: #fff; }
.heat-4 { background: var(--viz-seq-4); color: #fff; }
.heat-5 { background: var(--viz-seq-5); color: #fff; }

[data-bs-theme="dark"] .heat-1,
[data-bs-theme="dark"] .heat-2,
[data-bs-theme="dark"] .heat-3 { color: #fff; }
[data-bs-theme="dark"] .heat-4,
[data-bs-theme="dark"] .heat-5 { color: #0b0b0b; }

.card-link {
    text-decoration: none;
    color: inherit;
    transition: border-color .15s;
}

.card-link:hover {
    border-color: var(--viz-series-1);
}

[hidden] {
    display: none !important;
}

/* --- Assistant d'import ------------------------------------------------- */
.import-steps {
    display: flex;
    flex-wrap: wrap;
    gap: .5rem 1.5rem;
    list-style: none;
    padding: 0;
    margin: 0 0 1.5rem;
}

.import-steps li {
    display: flex;
    align-items: center;
    gap: .5rem;
    color: var(--bs-fg-3);
    font-size: var(--bs-font-size-sm);
}

.import-steps .import-step-number {
    display: inline-grid;
    place-items: center;
    width: 1.75rem;
    height: 1.75rem;
    border-radius: 50%;
    border: 1px solid var(--bs-border-color);
    font-weight: 600;
}

.import-steps li.current {
    color: var(--bs-fg-1);
    font-weight: 600;
}

.import-steps li.current .import-step-number {
    background: var(--viz-series-1);
    border-color: var(--viz-series-1);
    color: #fff;
}

.import-steps li.done .import-step-number {
    border-color: var(--viz-series-1);
    color: var(--viz-series-1);
}

.mapping-grid {
    display: grid;
    grid-template-columns: minmax(10rem, 14rem) 1fr;
    gap: .75rem 1rem;
    align-items: start;
}

@media (width < 576px) {
    .mapping-grid {
        grid-template-columns: 1fr;
    }
}

.mapping-example {
    min-height: 1.25rem;
    margin-top: .125rem;
}

.preview-table td,
.preview-table th {
    white-space: nowrap;
}

/* --- Modification d'une fiche -------------------------------------------- */
.identifier-table td:not(:first-child),
.identifier-table th:not(:first-child) {
    width: 6rem;
}

.winner-toggle {
    opacity: .55;
}

tr:hover .winner-toggle,
.winner-toggle:focus-visible {
    opacity: 1;
}

/* --- Historique des actions ---------------------------------------------- */
.activity-list {
    padding: .25rem 1.25rem 1rem;
}

.activity-day {
    margin: 1rem 0 .25rem;
    padding-bottom: .25rem;
    border-bottom: 1px solid var(--bs-border-color);
}

.activity-item {
    display: grid;
    grid-template-columns: 3rem 1.25rem 1fr auto;
    gap: .5rem;
    align-items: baseline;
    padding: .375rem 0;
}

.activity-icon {
    color: var(--viz-series-1);
}

.activity-text {
    min-width: 0;
    overflow-wrap: anywhere;
}

@media (width < 576px) {
    .activity-item {
        grid-template-columns: 2.75rem 1.25rem 1fr;
    }
    .activity-ip {
        grid-column: 3;
    }
}

/* --- Questionnaires --------------------------------------------------------- */
.survey-filters > summary {
    cursor: pointer;
    list-style: none;
}

.survey-filters > summary::-webkit-details-marker {
    display: none;
}

.survey-filters > summary::after {
    content: "\F282"; /* bi-chevron-down */
    font-family: "bootstrap-icons";
    margin-inline-start: auto;
    color: var(--bs-fg-3);
    transition: transform .15s;
}

.survey-filters[open] > summary::after {
    transform: rotate(180deg);
}

.survey-filters:not([open]) > summary {
    border-bottom: 0;
}

.survey-quotes li + li {
    margin-top: .75rem;
}

.survey-quotes blockquote {
    margin: 0;
    padding-inline-start: .75rem;
    border-inline-start: 3px solid var(--bs-border-color);
}

.survey-comments li {
    padding: .875rem 1rem;
}

.survey-comments li + li {
    border-top: 1px solid var(--bs-border-color);
}

/* --- Recherche rapide ------------------------------------------------------ */
.search-dialog {
    --bs-dialog-width: 640px;
    margin-top: 10vh;
}

.search-dialog:not([open]) {
    display: none;
}

.search-form {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--bs-border-color);
}

.search-input {
    border: 0;
    box-shadow: none;
    padding-inline: 0;
    font-size: 1.0625rem;
}

.search-input:focus,
.search-input:focus-visible {
    box-shadow: none;
    outline: none;
}

/* La croix native du champ ferait doublon avec le bouton « Fermer ». */
.search-input::-webkit-search-cancel-button {
    display: none;
}

.search-results {
    max-height: min(60vh, 28rem);
    overflow-y: auto;
}

.search-results:empty {
    display: none;
}

.search-group {
    padding: .5rem 1rem .25rem;
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--bs-fg-3, var(--viz-muted));
}

.search-option {
    display: block;
    padding: .5rem 1rem;
    color: inherit;
    text-decoration: none;
}

.search-option small {
    display: block;
    color: var(--viz-muted);
}

.search-option:hover,
.search-option[aria-selected="true"] {
    background-color: var(--bs-secondary-bg, rgb(127 127 127 / .12));
}

.search-option[aria-selected="true"] {
    box-shadow: inset 3px 0 0 var(--viz-series-1);
}

.search-empty {
    padding: .75rem 1rem;
    margin: 0;
    color: var(--viz-muted);
}

.search-hint {
    padding: .5rem 1rem;
    border-top: 1px solid var(--bs-border-color);
}

/* Raccourcis clavier inutiles sur téléphone. */
@media (width < 768px) {
    .search-hint {
        display: none;
    }
}


/* --- Suggestions de communes (fiche participant) ----------------------------- */
.commune-field {
    position: relative;
}

.commune-suggestions {
    position: absolute;
    z-index: 1000;
    inset-inline: calc(var(--bs-gutter-x, 1.5rem) * .5);
    margin-top: .25rem;
    max-height: 18rem;
    overflow-y: auto;
    background-color: var(--bs-bg-body);
    border: 1px solid var(--bs-border-color);
    border-radius: .5rem;
    box-shadow: 0 .5rem 1.5rem rgb(0 0 0 / .15);
}

.commune-option {
    padding: .4rem .75rem;
    cursor: pointer;
}

.commune-option small {
    display: block;
    color: var(--viz-muted);
}

.commune-option:hover,
.commune-option[aria-selected="true"] {
    background-color: rgb(127 127 127 / .12);
}

.commune-option[aria-selected="true"] {
    box-shadow: inset 3px 0 0 var(--viz-series-1);
}

/* --- Tri des tableaux par clic sur le titre de colonne ------------------------ */
.sort-link {
    color: inherit;
    text-decoration: none;
    white-space: nowrap;
}

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

.sort-link .bi {
    font-size: .8em;
    color: var(--viz-muted);
}

th:not(.sorted) .sort-link .bi {
    opacity: .45;
}

th.sorted .sort-link .bi {
    color: var(--viz-series-1);
}
