/* ---------- Variables (clair / sombre) ---------- */
:root {
    color-scheme: light dark;
    --bg: #f4f3ef;
    --surface: #ffffff;
    --surface-2: #f8f7f4;
    --text: #1f2933;
    --muted: #66727d;
    --line: #e3e1da;
    --accent: #0f766e;
    --accent-ink: #ffffff;
    --accent-soft: #d8eeeb;
    --danger: #b42318;
    --danger-soft: #fde9e7;
    --warn: #96560a;
    --warn-soft: #fcefd3;
    --ok: #1b7340;
    --ok-soft: #dcf3e4;
    --client: #e8e6df;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(20, 30, 30, .06), 0 6px 20px rgba(20, 30, 30, .05);
}

:root[data-theme="dark"] {
    --bg: #14181a;
    --surface: #1d2326;
    --surface-2: #232a2d;
    --text: #e7ecee;
    --muted: #93a1a8;
    --line: #2d363a;
    --accent: #3bb8a9;
    --accent-ink: #06211e;
    --accent-soft: #17312e;
    --danger: #ff8b80;
    --danger-soft: #3a1f1c;
    --warn: #f0b95c;
    --warn-soft: #382c14;
    --ok: #6fd69a;
    --ok-soft: #173223;
    --client: #2a3236;
    --shadow: none;
}

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

html { -webkit-text-size-adjust: 100%; }

/* L'attribut hidden doit toujours gagner sur un display: flex. */
[hidden] { display: none !important; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
p { margin: 0 0 .8em; }
a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
small, .muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }

/* ---------- Boutons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4em;
    padding: .55em 1em; border-radius: 9px; border: 1px solid var(--line);
    background: var(--surface); color: var(--text); font: inherit; font-weight: 600; font-size: .92rem;
    text-decoration: none; cursor: pointer; line-height: 1.2;
}
.btn:hover { border-color: var(--muted); }
.btn:focus-visible, .input:focus-visible, .nav a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn--primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.btn--danger { color: var(--danger); }
.btn--sm { padding: .35em .7em; font-size: .84rem; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
form.inline { display: inline; margin: 0; }

/* ---------- Formulaires ---------- */
.field { margin-bottom: 1.1rem; }
.field > label, .label { display: block; font-weight: 600; margin-bottom: .3em; font-size: .92rem; }
.help { color: var(--muted); font-size: .85rem; margin: .3em 0 0; }
.input {
    width: 100%; padding: .6em .75em; border: 1px solid var(--line); border-radius: 9px;
    background: var(--surface); color: var(--text); font: inherit;
}
textarea.input { min-height: 7em; resize: vertical; }
select.input { padding-right: 2em; }
.check { display: flex; gap: .6em; align-items: flex-start; padding: .5em 0; }
.check input { margin-top: .25em; accent-color: var(--accent); }
.check strong { display: block; }
.row { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.row > .grow { flex: 1 1 14rem; }
.actions { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: 1.4rem; }

/* ---------- Pastilles ---------- */
.badge {
    display: inline-block; padding: .12em .6em; border-radius: 99px; font-size: .78rem; font-weight: 600;
    background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); white-space: nowrap;
}
.badge--ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge--warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge--danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge--accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* ---------- Administration WhatsApp ---------- */
.tile[data-status="connected"] .tile__value { color: var(--ok); }
.tile[data-status="error"] .tile__value { color: var(--danger); }
.tile[data-status="awaiting_qr"] .tile__value,
.tile[data-status="qr_available"] .tile__value,
.tile[data-status="connecting"] .tile__value { color: var(--warn); }
.whatsapp-qr {
    display: flex; align-items: flex-start; gap: 1.4rem; flex-wrap: wrap;
    padding: 1rem; margin: 1rem 0; border: 1px solid var(--line);
    border-radius: var(--radius); background: var(--surface-2);
}
.whatsapp-qr img {
    width: 220px; height: 220px; object-fit: contain; flex: 0 0 auto;
    border-radius: 10px; border: 1px solid var(--line); background: #fff; padding: 8px;
}
.whatsapp-qr > div { flex: 1 1 18rem; }
.hint { color: var(--muted); font-size: .88rem; }
.tile__value--sm { font-size: 1.25rem; overflow-wrap: anywhere; }
.profile-card {
    display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
    padding: .9rem 1rem; margin-bottom: 1rem; border-radius: var(--radius);
    background: var(--surface-2); border: 1px solid var(--line);
}
.profile-card__avatar {
    width: 64px; height: 64px; border-radius: 50%; object-fit: cover;
    border: 2px solid var(--accent); background: var(--surface); flex: 0 0 auto;
}
.profile-card__avatar--empty {
    display: grid; place-items: center; color: var(--accent);
    font-size: 1.8rem; font-weight: 700; background: var(--accent-soft);
}
.profile-card small { display: block; margin-top: .2rem; }
.card--danger { border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.card--danger > h2 { color: var(--danger); }

/* ---------- Messages flash ---------- */
.flash { padding: .7em 1em; border-radius: 10px; margin-bottom: 1rem; border: 1px solid transparent; }
.flash--success { background: var(--ok-soft); color: var(--ok); }
.flash--error { background: var(--danger-soft); color: var(--danger); }

/* ---------- Mise en page du back-office ---------- */
.app { display: grid; grid-template-columns: minmax(0, 250px) minmax(0, 1fr); min-height: 100vh; }
.side {
    background: var(--surface); border-right: 1px solid var(--line); padding: 1.2rem 1rem;
    display: flex; flex-direction: column; gap: 1.2rem; position: sticky; top: 0; height: 100vh; min-width: 0;
}
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.05rem; text-decoration: none; color: var(--text); }
.brand__mark {
    width: 34px; height: 34px; border-radius: 10px; background: var(--accent); color: var(--accent-ink);
    display: grid; place-items: center; font-size: 1.1rem;
}
.nav { display: flex; flex-direction: column; gap: .15rem; }
.nav a {
    display: flex; justify-content: space-between; align-items: center; gap: .5rem;
    padding: .55em .8em; border-radius: 9px; color: var(--text); text-decoration: none; font-weight: 550;
}
.nav a:hover { background: var(--surface-2); }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.nav .count {
    background: var(--danger); color: #fff; border-radius: 99px; font-size: .74rem; padding: 0 .55em; font-weight: 700; line-height: 1.6;
}
.nav .count--soft { background: var(--warn-soft); color: var(--warn); }
.side__foot { margin-top: auto; font-size: .88rem; border-top: 1px solid var(--line); padding-top: 1rem; }
.side__foot .roles { display: flex; flex-wrap: wrap; gap: .3rem; margin: .4rem 0 .8rem; }
.main { padding: 0 2rem 3rem; width: 100%; min-width: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.4rem; }
.page-head h1 { margin: 0; }
.page-head p { margin: .2em 0 0; color: var(--muted); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.2rem 1.3rem; box-shadow: var(--shadow); margin-bottom: 1.2rem; }
.card > h2:first-child { margin-top: 0; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 1.2rem; align-items: start; }
.grid-2 > .card { margin-bottom: 0; }
.stack > * + * { margin-top: 1.2rem; }

/* ---------- Tableaux ---------- */
.table-wrap { overflow-x: auto; max-width: 100%; }
table.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: .5em .7em; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: .7em; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--surface-2); }
.table .title { font-weight: 600; }
.table .preview { color: var(--muted); max-width: 32em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table .cell-actions { white-space: nowrap; text-align: right; }
.empty { text-align: center; color: var(--muted); padding: 2.5rem 1rem; }

/* ---------- Tableau de bord ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(190px, 100%), 1fr)); gap: 1rem; margin-bottom: 1.2rem; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.2rem; box-shadow: var(--shadow); }
.tile__label { color: var(--muted); font-size: .85rem; }
.tile__value { font-size: 2rem; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.tile__note { color: var(--muted); font-size: .82rem; }
.tile--alert { border-color: var(--danger); }
.tile--alert .tile__value { color: var(--danger); }
.bars { display: flex; align-items: flex-end; gap: 6px; height: 160px; padding-top: .5rem; }
.bar { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; min-width: 0; }
.bar span { display: block; width: 100%; background: var(--accent); border-radius: 5px 5px 0 0; min-height: 2px; opacity: .85; }
.bar small { font-size: .68rem; margin-top: .4em; white-space: nowrap; }
.rank { list-style: none; margin: 0; padding: 0; }
.rank li { display: flex; justify-content: space-between; gap: 1rem; padding: .5em 0; border-bottom: 1px solid var(--line); }
.rank li:last-child { border-bottom: 0; }
.meter { height: 8px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 99px; overflow: hidden; min-width: 90px; }
.meter span { display: block; height: 100%; background: var(--accent); }

/* ---------- Fil de conversation ---------- */
.thread { display: flex; flex-direction: column; gap: .7rem; padding: 1rem; max-height: 62vh; overflow-y: auto; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); }
.msg { display: flex; flex-direction: column; max-width: 78%; }
.msg--client { align-self: flex-start; }
.msg--bot, .msg--agent { align-self: flex-end; align-items: flex-end; }
.bubble { padding: .6em .9em; border-radius: 14px; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--client); }
.msg--bot .bubble { background: var(--surface); border: 1px solid var(--line); }
.msg--agent .bubble { background: var(--accent); color: var(--accent-ink); }
.msg__meta { font-size: .75rem; color: var(--muted); margin-top: .25em; display: flex; gap: .5em; flex-wrap: wrap; }
.msg__meta .flag { color: var(--danger); font-weight: 600; }
.msg__meta .ok { color: var(--ok); }
.msg__author { display: inline-flex; align-items: center; padding: .05em .5em; border: 1px solid var(--line); border-radius: 99px; background: var(--surface); font-weight: 650; }
.msg__author--bot { color: var(--accent); border-color: var(--accent-soft); background: var(--accent-soft); }
.msg__author--agent { color: var(--ok); border-color: var(--ok-soft); background: var(--ok-soft); }
.msg__author--client { color: var(--muted); }
.reply-form textarea { min-height: 5.5em; }
.kv { display: grid; grid-template-columns: 8.5em 1fr; gap: .45em .8em; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.quote { border-left: 3px solid var(--accent); background: var(--accent-soft); padding: .6em .9em; border-radius: 0 9px 9px 0; margin-bottom: 1rem; }

.tabs { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: 1rem; }
.tabs a { padding: .4em .9em; border-radius: 99px; text-decoration: none; color: var(--text); border: 1px solid var(--line); background: var(--surface); font-weight: 550; font-size: .9rem; }
.tabs a[aria-current="page"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.pager { display: flex; gap: .5rem; align-items: center; justify-content: center; margin-top: 1rem; }

/* ---------- Connexion ---------- */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login { width: 100%; max-width: 400px; }
.login .brand { justify-content: center; margin-bottom: 1.2rem; font-size: 1.2rem; }

/* ---------- Chat public ---------- */
.chat-page { height: 100dvh; display: flex; justify-content: center; background: var(--bg); }
.chat { width: 100%; max-width: 720px; height: 100%; display: flex; flex-direction: column; background: var(--surface); border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
.chat__head { display: flex; align-items: center; gap: .8rem; padding: .9rem 1.1rem; background: var(--accent); color: var(--accent-ink); }
.chat__head .brand__mark { background: rgba(255, 255, 255, .2); color: inherit; }
.chat__head strong { display: block; line-height: 1.2; }
.chat__head small { color: inherit; opacity: .85; }
.chat__head .btn { margin-left: auto; background: transparent; color: inherit; border-color: rgba(255, 255, 255, .45); }
.chat__messages { flex: 1; overflow-y: auto; padding: 1.1rem; display: flex; flex-direction: column; gap: .7rem; background: var(--surface-2); }
.chat__messages .msg { max-width: 85%; }
.chat__messages .msg--client { align-self: flex-end; align-items: flex-end; }
.chat__messages .msg--client .bubble { background: var(--accent); color: var(--accent-ink); }
.chat__messages .msg--bot, .chat__messages .msg--agent { align-self: flex-start; align-items: flex-start; }
.chat__messages .msg--agent .bubble { background: var(--ok-soft); color: var(--text); border: 1px solid var(--ok); }
.chat__messages .msg--system { align-self: center; }
.chat__messages .msg--system .bubble { background: var(--danger-soft); color: var(--danger); font-size: .88rem; }
.chat__chips { display: flex; flex-wrap: wrap; gap: .5rem; padding: 0 1.1rem .8rem; background: var(--surface-2); }
.chip { border: 1px solid var(--accent); color: var(--accent); background: var(--surface); border-radius: 99px; padding: .35em .9em; font: inherit; font-size: .88rem; cursor: pointer; }
.chip:hover { background: var(--accent-soft); }
.typing { color: var(--muted); font-size: .85rem; padding: 0 1.1rem .5rem; background: var(--surface-2); min-height: 1.6em; }
.chat__form { display: flex; gap: .6rem; padding: .8rem 1rem; border-top: 1px solid var(--line); align-items: flex-end; background: var(--surface); }
.chat__form textarea { min-height: 2.8em; max-height: 8em; resize: none; }
.chat__foot { text-align: center; font-size: .75rem; color: var(--muted); padding: 0 1rem .6rem; background: var(--surface); }

/* ---------- Interface moderne ---------- */
.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.icon-btn {
    width: 38px; height: 38px; flex: 0 0 auto; display: grid; place-items: center;
    border: 1px solid var(--line); border-radius: 11px; background: var(--surface);
    color: var(--text); font-size: 1.05rem; cursor: pointer; position: relative;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
.chat__head .icon-btn { background: rgba(255, 255, 255, .16); border-color: rgba(255, 255, 255, .4); color: inherit; }
.chat__head .icon-btn:hover { background: rgba(255, 255, 255, .26); color: inherit; }

.topbar {
    position: sticky; top: 0; z-index: 25; margin: 0 -2rem 1.5rem;
    padding: .75rem 2rem; display: flex; align-items: center; gap: .8rem;
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    border-bottom: 1px solid var(--line); backdrop-filter: blur(12px);
}
.topbar__menu { display: none; }
.topbar__title { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.topbar__title strong { font-size: 1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar__eyebrow { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.topbar__actions { margin-left: auto; display: flex; align-items: center; gap: .5rem; }
.topbar__chat { white-space: nowrap; }
.topbar__avatar {
    width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center;
    background: var(--accent); color: var(--accent-ink); font-weight: 700; text-decoration: none;
}

.notifications { position: relative; }
.notifications__badge {
    position: absolute; top: -6px; right: -6px; min-width: 19px; height: 19px;
    padding: 0 .35em; border-radius: 99px; display: grid; place-items: center;
    background: var(--danger); color: #fff; font-size: .68rem; font-weight: 700;
    border: 2px solid var(--surface); line-height: 1;
}
.notifications__panel {
    position: absolute; right: 0; top: calc(100% + .55rem); z-index: 40;
    width: min(370px, calc(100vw - 2rem)); background: var(--surface);
    border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
    padding: .6rem;
}
.notifications__head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .2rem .4rem .6rem; }
.notifications__head strong { font-size: .9rem; }
.notifications__list { display: flex; flex-direction: column; gap: .25rem; max-height: 340px; overflow-y: auto; }
.notifications__empty { margin: 0; padding: 1.2rem .6rem; text-align: center; color: var(--muted); font-size: .88rem; }
.notification {
    display: grid; grid-template-columns: 10px minmax(0, 1fr); gap: .65rem;
    padding: .6rem; border-radius: 10px; text-decoration: none; color: var(--text);
}
.notification:hover { background: var(--surface-2); }
.notification__dot { width: 8px; height: 8px; margin-top: .45em; border-radius: 50%; background: var(--accent); }
.notification[data-tone="warn"] .notification__dot { background: var(--warn); }
.notification[data-tone="danger"] .notification__dot { background: var(--danger); }
.notification[data-tone="ok"] .notification__dot { background: var(--ok); }
.notification__body { display: flex; flex-direction: column; min-width: 0; }
.notification__body strong { font-size: .88rem; }
.notification__body small { font-size: .78rem; color: var(--muted); overflow-wrap: anywhere; }
.notification__time { opacity: .75; }

.side-scrim { display: none; }
.login__top { display: flex; align-items: center; justify-content: center; gap: .6rem; margin-bottom: 1.2rem; }
.login__top .brand { margin-bottom: 0; }

/* ---------- Modale de confirmation ---------- */
body.modal-open { overflow: hidden; }
.modal-backdrop {
    position: fixed; inset: 0; z-index: 100; display: grid; place-items: center;
    padding: 1rem; background: rgba(8, 18, 18, .55); backdrop-filter: blur(3px);
    animation: modal-fade .16s ease-out;
}
.modal {
    position: relative; width: min(440px, 100%); padding: 1.5rem;
    background: var(--surface); color: var(--text); border: 1px solid var(--line);
    border-radius: 16px; box-shadow: 0 18px 60px rgba(0, 0, 0, .28);
    animation: modal-rise .18s ease-out;
}
.modal__close {
    position: absolute; top: .6rem; right: .7rem; width: 32px; height: 32px;
    border: 0; border-radius: 9px; background: transparent; color: var(--muted);
    font-size: 1.4rem; line-height: 1; cursor: pointer;
}
.modal__close:hover { background: var(--surface-2); color: var(--text); }
.modal__icon {
    width: 42px; height: 42px; margin-bottom: 1rem; border-radius: 12px;
    display: grid; place-items: center; font-weight: 800; font-size: 1.2rem;
    background: var(--accent-soft); color: var(--accent);
}
.modal[data-tone="danger"] .modal__icon { background: var(--danger-soft); color: var(--danger); }
.modal[data-tone="danger"] .modal__confirm { background: var(--danger); border-color: var(--danger); color: #fff; }
.modal[data-tone="warn"] .modal__icon { background: var(--warn-soft); color: var(--warn); }
.modal[data-tone="warn"] .modal__confirm { background: var(--warn); border-color: var(--warn); color: #fff; }
.modal__title { margin: 0 0 .4rem; padding-right: 2rem; font-size: 1.2rem; }
.modal__message { color: var(--muted); margin: 0 0 1.4rem; overflow-wrap: anywhere; }
.modal__actions { display: flex; justify-content: flex-end; gap: .6rem; flex-wrap: wrap; }
@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-rise { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }

/* ---------- Petits écrans ---------- */
@media (max-width: 900px) {
    .app { display: block; }
    .topbar { margin: 0 -1rem 1rem; padding: .7rem 1rem; }
    .topbar__menu { display: grid; }
    .main { padding: 0 1rem 2.5rem; }
    .side {
        position: fixed; inset: 0 auto 0 0; z-index: 50; width: min(82vw, 290px);
        height: 100dvh; transform: translateX(-102%); transition: transform .22s ease;
        box-shadow: 12px 0 34px rgba(0, 0, 0, .18);
    }
    .app.nav-open .side { transform: translateX(0); }
    .app.nav-open .side-scrim {
        display: block; position: fixed; inset: 0; z-index: 45; border: 0;
        width: 100%; height: 100%; background: rgba(8, 18, 18, .45); cursor: default;
    }
    body.nav-open { overflow: hidden; }
    .nav { flex-direction: column; }
    .side__foot { margin-top: auto; }
    .grid-2 { grid-template-columns: minmax(0, 1fr); }
    .msg { max-width: 92%; }
    .kv { grid-template-columns: 1fr; }
    .kv dt { margin-top: .4em; }
    .chat { border: 0; }
}

@media (max-width: 560px) {
    .topbar__chat, .topbar__eyebrow { display: none; }
    .notifications__panel {
        position: fixed; top: 4.25rem; left: 1rem; right: 1rem;
        width: auto; max-width: none; z-index: 55;
        max-height: min(70vh, 520px); overflow: hidden;
    }
    .notifications__list { max-height: min(52vh, 340px); }
    .chat__head .btn { font-size: .78rem; }
    .table .preview { max-width: 14em; }
}
