:root {
    --bg: #f2f4f6;
    --surface: #ffffff;
    --surface-sunk: #f7f9fa;
    --ink: #16202a;
    --muted: #5a6876;
    --line: #dce2e8;
    --accent: #0f766e;
    --accent-ink: #ffffff;
    --accent-soft: #e3f3f1;
    --danger: #b42318;
    --danger-soft: #fdecea;

    --s2: #15803d;
    --s3: #1d4ed8;
    --s4: #b45309;
    --s5: #b42318;

    --m-get: #0f766e;
    --m-post: #1d4ed8;
    --m-put: #a16207;
    --m-patch: #7e22ce;
    --m-delete: #b42318;

    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    --radius: 10px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0e1318;
        --surface: #151c23;
        --surface-sunk: #11171d;
        --ink: #e3e9ef;
        --muted: #93a2b1;
        --line: #263240;
        --accent: #2dd4bf;
        --accent-ink: #062b27;
        --accent-soft: #123532;
        --danger: #f97066;
        --danger-soft: #3a1714;
        --s2: #4ade80;
        --s3: #7aa2ff;
        --s4: #fbbf24;
        --s5: #f97066;
        --m-get: #2dd4bf;
        --m-post: #7aa2ff;
        --m-put: #fbbf24;
        --m-patch: #c084fc;
        --m-delete: #f97066;
        color-scheme: dark;
    }
}

:root[data-theme="dark"] {
    --bg: #0e1318;
    --surface: #151c23;
    --surface-sunk: #11171d;
    --ink: #e3e9ef;
    --muted: #93a2b1;
    --line: #263240;
    --accent: #2dd4bf;
    --accent-ink: #062b27;
    --accent-soft: #123532;
    --danger: #f97066;
    --danger-soft: #3a1714;
    --s2: #4ade80;
    --s3: #7aa2ff;
    --s4: #fbbf24;
    --s5: #f97066;
    --m-get: #2dd4bf;
    --m-post: #7aa2ff;
    --m-put: #fbbf24;
    --m-patch: #c084fc;
    --m-delete: #f97066;
    color-scheme: dark;
}

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

/* L'attribut hidden l'emporte sur tout « display » posé par une classe. */
[hidden] {
    display: none !important;
}

/* Filet de sécurité : sur téléphone, rien ne doit pouvoir faire glisser la page de côté. */
html, body {
    overflow-x: clip;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font: 15px/1.5 var(--font);
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p {
    margin: 0;
}

a {
    color: var(--accent);
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

/* ─── En-tête ─────────────────────────────────────────────────────── */

.topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: 12px 32px;
    padding: 20px 24px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}

.eyebrow {
    font: 600 12px/1.4 var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--accent);
}

h1 {
    font-size: 26px;
    font-weight: 750;
    letter-spacing: -0.02em;
}

.doc-links {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 20px;
    font-weight: 600;
}

.notice {
    padding: 8px 24px;
    background: var(--accent-soft);
    border-bottom: 1px solid var(--line);
    font-size: 14px;
}

/* ─── Mise en page : toute la largeur ─────────────────────────────── */

.layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(360px, 440px);
    gap: 20px;
    align-items: start;
    padding: 20px 24px 32px;
}

.side {
    display: grid;
    gap: 20px;
}

@media (max-width: 1100px) {
    .layout {
        grid-template-columns: minmax(0, 1fr);
    }
}

@media (max-width: 640px) {
    .topbar, .layout, .notice {
        padding-inline: 16px;
    }
}

.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px 20px;
    display: grid;
    gap: 14px;
    min-width: 0;
}

.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

h2 {
    font-size: 17px;
    font-weight: 700;
    text-wrap: balance;
}

.count {
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.hint {
    color: var(--muted);
    font-size: 13px;
}

/* ─── Boutons et champs ───────────────────────────────────────────── */

.btn {
    font: inherit;
    font-weight: 600;
    border-radius: 8px;
    padding: 8px 14px;
    border: 1px solid transparent;
    cursor: pointer;
}

.btn:disabled {
    opacity: 0.45;
    cursor: default;
}

.btn-primary {
    background: var(--accent);
    color: var(--accent-ink);
}

.btn-ghost {
    background: transparent;
    color: var(--ink);
    border-color: var(--line);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--surface-sunk);
}

.btn-small {
    padding: 4px 10px;
    font-size: 13px;
}

.btn-link {
    font: inherit;
    font-weight: 600;
    font-size: 13px;
    background: none;
    border: 0;
    padding: 4px 6px;
    color: var(--accent);
    cursor: pointer;
    border-radius: 6px;
}

.btn-link.danger {
    color: var(--danger);
}

.btn-link:hover {
    background: var(--surface-sunk);
}

/* 16 px au moins : en dessous, Safari sur iPhone zoome sur le champ touché, et la page se déplace. */
input, select {
    font: inherit;
    font-size: 16px;
    color: inherit;
    background: var(--surface-sunk);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px 10px;
    width: 100%;
    min-width: 0;
}

input[aria-invalid="true"] {
    border-color: var(--danger);
}

.field {
    display: grid;
    gap: 4px;
}

.field label {
    font-weight: 600;
    font-size: 14px;
}

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.field-error {
    color: var(--danger);
    font-size: 13px;
}

.field-error:empty {
    display: none;
}

#user-form {
    display: grid;
    gap: 12px;
}

.form-actions {
    display: flex;
    gap: 10px;
    padding-top: 4px;
}

.alert {
    background: var(--danger-soft);
    color: var(--ink);
    border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
    border-radius: 8px;
    padding: 10px 12px;
    display: grid;
    gap: 8px;
    justify-items: start;
}

.etag {
    font: 600 12px/1 var(--mono);
    color: var(--accent);
    background: var(--accent-soft);
    border-radius: 999px;
    padding: 5px 10px;
    white-space: nowrap;
}

/* ─── Liste ───────────────────────────────────────────────────────── */

.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: center;
}

.search {
    flex: 1 1 260px;
}

.page-size {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: 14px;
    white-space: nowrap;
}

.page-size select, .sort-mobile select {
    width: auto;
}

.sort-mobile {
    display: none;
}

.table-wrap {
    /* Le repère des éléments positionnés du tableau (libellés masqués) : sans lui, ils
       échapperaient au défilement du tableau et élargiraient toute la page. */
    position: relative;
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

th, td {
    text-align: left;
    padding: 9px 12px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}

tbody tr:last-child td {
    border-bottom: 0;
}

tbody tr.editing {
    background: var(--accent-soft);
}

th {
    background: var(--surface-sunk);
    font-weight: 600;
    color: var(--muted);
    padding: 0;
}

th > button, th > span {
    display: block;
    width: 100%;
    font: inherit;
    color: inherit;
    text-align: inherit;
    background: none;
    border: 0;
    padding: 9px 12px;
    cursor: pointer;
}

th[aria-sort] > button {
    color: var(--ink);
}

th[aria-sort="ascending"] > button::after {
    content: " ↑";
}

th[aria-sort="descending"] > button::after {
    content: " ↓";
}

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

td.muted {
    color: var(--muted);
}

td.actions {
    text-align: right;
    width: 1%;
}

td.empty {
    text-align: center;
    color: var(--muted);
    padding: 28px 12px;
    white-space: normal;
}

.pager {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    color: var(--muted);
    font-size: 14px;
    font-variant-numeric: tabular-nums;
}

.pager-buttons {
    display: flex;
    gap: 8px;
}

/* ─── Sur téléphone : une carte par utilisateur ──────────────────── */

@media (max-width: 640px) {
    .sort-mobile, .page-size {
        display: flex;
        align-items: center;
        gap: 8px;
        color: var(--muted);
        font-size: 14px;
    }

    .search {
        flex-basis: 100%;
    }

    .table-wrap {
        border: 0;
        border-radius: 0;
        overflow: visible;
    }

    .table-wrap table, .table-wrap tbody, .table-wrap tr, .table-wrap td {
        display: block;
    }

    .table-wrap thead {
        display: none;
    }

    .table-wrap tbody {
        display: grid;
        gap: 10px;
    }

    .table-wrap tbody tr {
        display: flex;
        flex-wrap: wrap;
        align-items: baseline;
        gap: 2px 14px;
        padding: 12px 14px;
        border: 1px solid var(--line);
        border-radius: 8px;
    }

    .table-wrap td {
        padding: 0;
        border: 0;
        white-space: normal;
        text-align: left;
    }

    td.name {
        flex-basis: 100%;
        font-weight: 650;
        font-size: 15px;
    }

    td.email {
        flex-basis: 100%;
        color: var(--muted);
        overflow-wrap: anywhere;
    }

    td.meta {
        font-size: 13px;
        color: var(--muted);
    }

    td.meta.muted:not(.created) {
        display: none;
    }

    td.age::after {
        content: " ans";
    }

    td.created::before {
        content: "créé le ";
    }

    td.actions {
        flex-basis: 100%;
        display: flex;
        justify-content: flex-end;
        gap: 8px;
        margin-top: 8px;
    }

    td.actions .btn-link {
        padding: 8px 14px;
        font-size: 14px;
        border: 1px solid var(--line);
    }

    td.empty {
        border: 1px dashed var(--line);
        border-radius: 8px;
    }
}

/* ─── Journal des appels ──────────────────────────────────────────── */

.calls {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    gap: 6px;
    max-height: 520px;
    overflow-y: auto;
}

.calls:empty::before {
    content: "Aucun appel pour l'instant.";
    color: var(--muted);
    font-size: 14px;
}

.call summary {
    display: grid;
    grid-template-columns: 64px minmax(0, 1fr) auto auto;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    cursor: pointer;
    font: 13px/1.4 var(--mono);
    list-style: none;
}

.call summary::-webkit-details-marker {
    display: none;
}

.call[open] summary {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    background: var(--surface-sunk);
}

.method {
    font-weight: 700;
}

.method.get { color: var(--m-get); }
.method.post { color: var(--m-post); }
.method.put { color: var(--m-put); }
.method.patch { color: var(--m-patch); }
.method.delete { color: var(--m-delete); }

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

.status {
    font-weight: 700;
    padding: 1px 7px;
    border-radius: 999px;
    border: 1px solid currentColor;
}

.status.s2 { color: var(--s2); }
.status.s3 { color: var(--s3); }
.status.s4 { color: var(--s4); }
.status.s5, .status.s0 { color: var(--s5); }

.ms {
    color: var(--muted);
    min-width: 52px;
    text-align: right;
}

.call-body {
    border: 1px solid var(--line);
    border-top: 0;
    border-radius: 0 0 8px 8px;
    padding: 10px;
    display: grid;
    gap: 6px;
}

.call-body h3 {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
}

.call-body pre {
    margin: 0;
    padding: 8px 10px;
    background: var(--surface-sunk);
    border-radius: 6px;
    font: 12px/1.5 var(--mono);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    max-height: 260px;
    overflow-y: auto;
}

/* ─── Notification ────────────────────────────────────────────────── */

.toast {
    position: fixed;
    right: 24px;
    bottom: 24px;
    max-width: min(420px, calc(100vw - 32px));
    background: var(--ink);
    color: var(--bg);
    padding: 10px 16px;
    border-radius: 8px;
    font-weight: 600;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 160ms ease, transform 160ms ease;
    pointer-events: none;
}

.toast.visible {
    opacity: 1;
    transform: none;
}

@media (max-width: 640px) {
    .toast {
        right: 16px;
        left: 16px;
        bottom: 16px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .toast {
        transition: none;
    }
}
