/* Widget d'assistance publique (messagerie) : bulle flottante + panneau de dialogue. */

.msgw {
    position: relative;
    z-index: 900;
}

.msgw .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Bulle flottante ------------------------------------------------------- */

.msgw-toggle {
    position: fixed;
    right: clamp(14px, 3vw, 28px);
    bottom: clamp(14px, 3vw, 28px);
    z-index: 901;
    display: grid;
    place-items: center;
    width: 60px;
    height: 60px;
    border: none;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--ds-brand-800), var(--ds-brand-500));
    color: #fff;
    box-shadow: var(--ds-shadow-md);
    cursor: pointer;
    transition: transform var(--ds-transition), box-shadow var(--ds-transition);
}

.msgw-toggle::before {
    position: absolute;
    inset: -5px;
    border: 2px solid rgba(14, 165, 233, .38);
    border-radius: inherit;
    pointer-events: none;
    content: '';
    animation: msgw-assistant-pulse 2.8s ease-out infinite;
}

.msgw-toggle:hover,
.msgw-toggle:focus-visible {
    transform: translateY(-3px);
    box-shadow: var(--ds-shadow-lg);
}

.msgw-toggle:focus-visible {
    outline: none;
    box-shadow: var(--ds-shadow-lg), var(--ds-ring);
}

.msgw-toggle .nav-icon {
    width: 30px;
    height: 30px;
    transform-origin: 50% 65%;
    animation: msgw-assistant-greeting 3.8s ease-in-out infinite;
}

.msgw-toggle[aria-expanded="true"] {
    display: none;
}

/* Panneau de dialogue ----------------------------------------------------- */

.msgw-panel {
    position: fixed;
    right: clamp(10px, 3vw, 28px);
    bottom: clamp(10px, 3vw, 28px);
    z-index: 902;
    display: flex;
    flex-direction: column;
    width: min(392px, calc(100vw - 20px));
    max-height: min(640px, calc(100vh - 40px));
    overflow: hidden;
    background: var(--ds-surface);
    border: 1px solid var(--ds-line);
    border-radius: var(--ds-radius-xl);
    box-shadow: var(--ds-shadow-lg);
}

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

.msgw-panel__header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--ds-space-3);
    padding: var(--ds-space-4) var(--ds-space-5);
    background: linear-gradient(135deg, var(--ds-brand-800), var(--ds-brand-500));
    color: #fff;
}

.msgw-panel__heading {
    display: flex;
    align-items: center;
    gap: var(--ds-space-3);
}

.msgw-panel__icon {
    display: grid;
    flex: 0 0 auto;
    place-items: center;
    width: 38px;
    height: 38px;
    border-radius: var(--ds-radius-md);
    background: rgba(255, 255, 255, .18);
}

.msgw-panel__icon .nav-icon {
    animation: msgw-assistant-listening 2.6s ease-in-out infinite;
}

@keyframes msgw-assistant-pulse {
    0% {
        opacity: .85;
        transform: scale(.92);
    }
    70%,
    100% {
        opacity: 0;
        transform: scale(1.28);
    }
}

@keyframes msgw-assistant-greeting {
    0%,
    62%,
    100% {
        transform: translateY(0) rotate(0deg);
    }
    70% {
        transform: translateY(-2px) rotate(-6deg);
    }
    78% {
        transform: translateY(-2px) rotate(6deg);
    }
    86% {
        transform: translateY(-1px) rotate(-3deg);
    }
    94% {
        transform: translateY(0) rotate(0deg);
    }
}

@keyframes msgw-assistant-listening {
    0%,
    100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-1.5px);
    }
}

@media (prefers-reduced-motion: reduce) {
    .msgw-toggle::before,
    .msgw-toggle .nav-icon,
    .msgw-panel__icon .nav-icon {
        animation: none;
    }
}

.msgw-panel__header h2 {
    margin: 0;
    font-size: var(--ds-font-size-lg);
}

.msgw-panel__header p {
    margin: 2px 0 0;
    font-size: var(--ds-font-size-sm);
    color: rgba(255, 255, 255, .85);
}

.msgw-panel__close {
    display: grid;
    flex: 0 0 auto;
    place-items: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: none;
    border-radius: var(--ds-radius-sm);
    background: rgba(255, 255, 255, .16);
    color: #fff;
    cursor: pointer;
    transition: background-color var(--ds-transition);
}

.msgw-panel__close:hover,
.msgw-panel__close:focus-visible {
    background: rgba(255, 255, 255, .3);
}

.msgw-panel__close .nav-icon {
    width: 16px;
    height: 16px;
}

.msgw-panel__body {
    position: relative;
    padding: var(--ds-space-5);
    overflow-x: hidden;
    overflow-y: auto;
    flex: 1;
    min-width: 0;
}

/* Formulaire multi-étapes -------------------------------------------------- */

.msgw-form {
    display: grid;
    gap: var(--ds-space-4);
    min-width: 0;
}

.msgw-step {
    display: grid;
    gap: var(--ds-space-3);
    min-width: 0;
}

.msgw-step[hidden] {
    display: none;
}

.msgw-step__title {
    margin: 0;
    color: var(--ds-ink);
    font-weight: 750;
}

.msgw-field {
    display: grid;
    gap: .35rem;
    min-width: 0;
}

.msgw-field label {
    color: var(--ds-text);
    font-size: var(--ds-font-size-sm);
    font-weight: 650;
}

.msgw-field label em {
    color: var(--ds-danger-600);
    font-style: normal;
}

.msgw-field label small {
    color: var(--ds-muted);
    font-weight: 500;
}

.msgw-field input,
.msgw-field select,
.msgw-field textarea {
    width: 100%;
    min-width: 0;
    min-height: 42px;
    padding: 0 var(--ds-space-3);
    border: 1px solid var(--ds-line);
    border-radius: var(--ds-radius-sm);
    background: var(--ds-surface-soft);
    color: var(--ds-ink);
    font: inherit;
    transition: border-color var(--ds-transition), box-shadow var(--ds-transition);
}

.msgw-field textarea {
    min-height: 92px;
    padding-block: var(--ds-space-2);
    resize: vertical;
}

.msgw-field input:focus,
.msgw-field select:focus,
.msgw-field textarea:focus {
    outline: none;
    border-color: var(--ds-brand-500);
    box-shadow: var(--ds-ring);
}

.msgw-field select:disabled {
    color: var(--ds-muted);
    background: var(--ds-page);
    cursor: not-allowed;
}

.msgw-field-group {
    display: grid;
    gap: var(--ds-space-3);
    min-width: 0;
}

.msgw-choice {
    display: flex;
    align-items: center;
    gap: var(--ds-space-2);
    padding: var(--ds-space-2) var(--ds-space-3);
    border: 1px solid var(--ds-line);
    border-radius: var(--ds-radius-sm);
    cursor: pointer;
    transition: border-color var(--ds-transition), background-color var(--ds-transition);
}

.msgw-choice:has(input:checked) {
    border-color: var(--ds-brand-500);
    background: var(--ds-brand-50);
}

.msgw-choice input {
    accent-color: var(--ds-brand-600);
}

.msgw-choice .nav-icon {
    width: 18px;
    height: 18px;
    color: var(--ds-brand-700);
}

.msgw-step__actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--ds-space-2);
}

.msgw-step__actions--split {
    justify-content: space-between;
}

.msgw-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--ds-space-2);
    min-height: 42px;
    padding: 0 var(--ds-space-4);
    border: 1px solid transparent;
    border-radius: var(--ds-radius-sm);
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    transition: transform var(--ds-transition), box-shadow var(--ds-transition), background-color var(--ds-transition);
}

.msgw-button .nav-icon {
    width: 16px;
    height: 16px;
}

.msgw-button--primary {
    background: linear-gradient(135deg, var(--ds-brand-800), var(--ds-brand-500));
    color: #fff;
    box-shadow: var(--ds-shadow-sm);
}

.msgw-button--primary:hover,
.msgw-button--primary:focus-visible {
    box-shadow: var(--ds-shadow-md);
    transform: translateY(-1px);
}

.msgw-button--ghost {
    background: var(--ds-surface);
    border-color: var(--ds-line-strong);
    color: var(--ds-text);
}

.msgw-button--ghost:hover,
.msgw-button--ghost:focus-visible {
    border-color: var(--ds-brand-500);
    color: var(--ds-brand-800);
}

.msgw-button:focus-visible {
    outline: none;
    box-shadow: var(--ds-ring);
}

.msgw-button--wide {
    width: 100%;
}

.msgw-result__edit-actions {
    margin-top: var(--ds-space-1);
}

.msgw-hint {
    display: flex;
    align-items: flex-start;
    gap: var(--ds-space-2);
    margin: 0;
    padding: var(--ds-space-3);
    border-radius: var(--ds-radius-sm);
    background: var(--ds-brand-50);
    color: var(--ds-brand-800);
    font-size: var(--ds-font-size-sm);
}

.msgw-hint .nav-icon {
    flex: 0 0 auto;
    width: 16px;
    height: 16px;
    margin-top: 2px;
}

.msgw-hint--muted {
    background: var(--ds-surface-soft);
    color: var(--ds-muted);
}

.msgw-alert {
    margin: 0;
    padding: var(--ds-space-3);
    border: 1px solid var(--ds-danger-600);
    border-radius: var(--ds-radius-sm);
    background: var(--ds-danger-100);
    color: var(--ds-danger-700);
    font-weight: 650;
}

/* Résultats (réponse automatique / transmission humaine) ------------------ */

.msgw-result {
    display: grid;
    gap: var(--ds-space-3);
}

.msgw-badge {
    display: inline-flex;
    align-items: center;
    gap: var(--ds-space-2);
    align-self: start;
    padding: var(--ds-space-1) var(--ds-space-3);
    border-radius: var(--ds-radius-sm);
    font-size: var(--ds-font-size-sm);
    font-weight: 750;
}

.msgw-badge .nav-icon {
    width: 14px;
    height: 14px;
}

.msgw-badge--auto {
    background: var(--ds-brand-100);
    color: var(--ds-brand-800);
}

.msgw-badge--human {
    background: var(--ds-warning-100);
    color: var(--ds-warning-700);
}

.msgw-result__text {
    margin: 0;
    color: var(--ds-ink);
    line-height: var(--ds-line-height);
}

.msgw-confirm {
    display: grid;
    gap: var(--ds-space-2);
    padding: var(--ds-space-3);
    border: 1px solid var(--ds-line);
    border-radius: var(--ds-radius-md);
    background: var(--ds-surface-soft);
}

.msgw-confirm__question {
    margin: 0;
    color: var(--ds-text);
    font-weight: 650;
}

.msgw-confirm__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--ds-space-2);
}

.msgw-confirm__actions form {
    display: contents;
}

.msgw-confirm__thanks {
    display: flex;
    align-items: center;
    gap: var(--ds-space-2);
    margin: 0;
    color: var(--ds-success-700);
    font-weight: 650;
}

.msgw-phone-link {
    color: inherit;
    font-weight: 750;
    text-decoration-thickness: 2px;
    text-underline-offset: 2px;
}

.msgw-conseillers {
    display: flex;
    flex-direction: column;
    gap: var(--ds-space-2);
}

.msgw-conseillers__list {
    display: flex;
    flex-direction: column;
    gap: var(--ds-space-1);
    margin: 0;
    padding: 0;
    list-style: none;
}

.msgw-conseillers__list li {
    display: flex;
    align-items: center;
    gap: var(--ds-space-2);
    padding: var(--ds-space-2) var(--ds-space-3);
    border-radius: var(--ds-radius-sm);
    background: var(--ds-surface-soft);
    font-size: var(--ds-font-size-sm);
}

.msgw-conseillers__list li .nav-icon {
    flex: 0 0 auto;
    width: 14px;
    height: 14px;
    color: var(--ds-muted);
}

.msgw-conseillers__list li span {
    flex: 1 1 auto;
    color: var(--ds-text);
}

.msgw-lookup {
    border: 1px dashed var(--ds-line-strong);
    border-radius: var(--ds-radius-md);
    padding: var(--ds-space-3);
}

.msgw-lookup summary {
    color: var(--ds-brand-800);
    font-weight: 700;
    cursor: pointer;
}

.msgw-lookup .msgw-form {
    margin-top: var(--ds-space-3);
}

/* État « envoi en cours » : Turbo place un attribut busy sur le turbo-frame. */

.msgw-panel__body turbo-frame[busy] {
    position: relative;
    pointer-events: none;
    opacity: .55;
}

.msgw-panel__body turbo-frame[busy]::after {
    content: '';
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    animation: msgw-fade-in .15s ease;
}

.msgw-panel__body turbo-frame[busy]::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 1;
    width: 26px;
    height: 26px;
    margin: -13px 0 0 -13px;
    border: 3px solid var(--ds-brand-100);
    border-top-color: var(--ds-brand-600);
    border-radius: 50%;
    animation: msgw-spin .7s linear infinite;
}

@keyframes msgw-spin {
    to { transform: rotate(360deg); }
}

@keyframes msgw-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

@media (max-width: 480px) {
    .msgw-panel {
        right: 8px;
        bottom: 8px;
        left: 8px;
        width: auto;
        max-height: calc(100vh - 24px);
    }

    .msgw-toggle {
        right: 14px;
        bottom: 14px;
    }
}
