/* e-Signature Chat Widget — v1.0.0
   Couleurs principales : violet #667eea / bleu-violet #764ba2
   Conforme WCAG AA. Responsive mobile. */

:root {
    --esig-primary:     #667eea;
    --esig-primary-d:   #5a6fd8;
    --esig-secondary:   #764ba2;
    --esig-gradient:    linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --esig-bg:          #ffffff;
    --esig-surface:     #f8f9ff;
    --esig-border:      #e8eaf6;
    --esig-text:        #2d2d44;
    --esig-text-light:  #6b7280;
    --esig-bot-bg:      #f0f1ff;
    --esig-user-bg:     var(--esig-gradient);
    --esig-shadow:      0 8px 32px rgba(102, 126, 234, 0.25);
    --esig-radius:      16px;
    --esig-radius-msg:  12px;
    --esig-z:           99999;
    --esig-font:        -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ── Root container ────────────────────────────────────────────────────── */
#esig-chat-root {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: var(--esig-z);
    font-family: var(--esig-font);
    font-size: 15px;
    line-height: 1.5;
    color: var(--esig-text);
}

/* ── Bubble button ─────────────────────────────────────────────────────── */
#esig-chat-bubble {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--esig-gradient);
    color: #fff;
    border: none;
    border-radius: 50px;
    padding: 14px 20px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    font-family: var(--esig-font);
    box-shadow: var(--esig-shadow);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    white-space: nowrap;
}
#esig-chat-bubble:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 40px rgba(102, 126, 234, 0.35);
}
#esig-chat-bubble:active { transform: translateY(0); }

#esig-chat-bubble svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}
.esig-bubble-label { line-height: 1; }

/* ── Chat window ───────────────────────────────────────────────────────── */
#esig-chat-window {
    position: absolute;
    bottom: calc(100% + 12px);
    right: 0;
    width: 360px;
    max-height: 560px;
    background: var(--esig-bg);
    border-radius: var(--esig-radius);
    box-shadow: var(--esig-shadow), 0 2px 8px rgba(0,0,0,0.08);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--esig-border);
    /* Animation */
    transform-origin: bottom right;
    animation: esig-appear 0.22s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
#esig-chat-window[hidden] { display: none; }

@keyframes esig-appear {
    from { opacity: 0; transform: scale(0.85) translateY(8px); }
    to   { opacity: 1; transform: scale(1)    translateY(0);   }
}

/* ── Header ────────────────────────────────────────────────────────────── */
#esig-chat-header {
    background: var(--esig-gradient);
    color: #fff;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    flex-shrink: 0;
}
.esig-header-avatar {
    width: 40px;
    height: 40px;
    background: rgba(255,255,255,0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
}
.esig-header-text {
    flex: 1;
    min-width: 0;
}
.esig-header-text strong {
    display: block;
    font-size: 15px;
    font-weight: 700;
}
.esig-header-text span {
    display: block;
    font-size: 11.5px;
    opacity: 0.85;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.esig-close-btn {
    background: none;
    border: none;
    color: rgba(255,255,255,0.8);
    font-size: 18px;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    transition: color 0.15s, background 0.15s;
    line-height: 1;
    flex-shrink: 0;
}
.esig-close-btn:hover { color: #fff; background: rgba(255,255,255,0.15); }

/* ── Messages area ─────────────────────────────────────────────────────── */
#esig-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    background: var(--esig-surface);
}
#esig-chat-messages::-webkit-scrollbar { width: 5px; }
#esig-chat-messages::-webkit-scrollbar-track { background: transparent; }
#esig-chat-messages::-webkit-scrollbar-thumb {
    background: var(--esig-border);
    border-radius: 3px;
}

/* ── Message bubbles ───────────────────────────────────────────────────── */
.esig-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: var(--esig-radius-msg);
    font-size: 14px;
    line-height: 1.55;
    word-wrap: break-word;
}
.esig-msg.bot {
    background: var(--esig-bot-bg);
    color: var(--esig-text);
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    border: 1px solid var(--esig-border);
}
.esig-msg.user {
    background: var(--esig-gradient);
    color: #fff;
    border-bottom-right-radius: 4px;
    align-self: flex-end;
}

/* Formatage markdown simple dans les réponses bot */
.esig-msg.bot strong { font-weight: 700; }
.esig-msg.bot em { font-style: italic; }
.esig-msg.bot a { color: var(--esig-primary); text-decoration: underline; }

/* ── Thinking indicator ────────────────────────────────────────────────── */
.esig-thinking {
    display: flex;
    gap: 5px;
    align-items: center;
    padding: 12px 14px;
    background: var(--esig-bot-bg);
    border-radius: var(--esig-radius-msg);
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    border: 1px solid var(--esig-border);
}
.esig-thinking span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--esig-primary);
    animation: esig-dot 1.2s infinite ease-in-out;
}
.esig-thinking span:nth-child(2) { animation-delay: 0.2s; }
.esig-thinking span:nth-child(3) { animation-delay: 0.4s; }
@keyframes esig-dot {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40%           { transform: scale(1);   opacity: 1;   }
}

/* ── Suggested questions ────────────────────────────────────────────────── */
#esig-chat-suggestions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 12px 14px 4px;
    background: var(--esig-surface);
}
.esig-suggestion {
    background: var(--esig-bg);
    border: 1.5px solid var(--esig-primary);
    color: var(--esig-primary);
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 13px;
    font-family: var(--esig-font);
    font-weight: 500;
    cursor: pointer;
    text-align: left;
    transition: background 0.15s, color 0.15s;
    line-height: 1.4;
}
.esig-suggestion:hover {
    background: var(--esig-primary);
    color: #fff;
}

/* ── Input area ────────────────────────────────────────────────────────── */
#esig-chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid var(--esig-border);
    background: var(--esig-bg);
    flex-shrink: 0;
}
#esig-chat-input {
    flex: 1;
    resize: none;
    border: 1.5px solid var(--esig-border);
    border-radius: 10px;
    padding: 9px 12px;
    font-family: var(--esig-font);
    font-size: 14px;
    color: var(--esig-text);
    background: var(--esig-surface);
    outline: none;
    transition: border-color 0.2s;
    min-height: 38px;
    max-height: 100px;
    overflow-y: auto;
    line-height: 1.4;
}
#esig-chat-input:focus { border-color: var(--esig-primary); }
#esig-chat-input::placeholder { color: var(--esig-text-light); }

#esig-chat-send {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: var(--esig-gradient);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity 0.2s, transform 0.15s;
}
#esig-chat-send:hover { opacity: 0.9; transform: scale(1.05); }
#esig-chat-send:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
#esig-chat-send svg {
    width: 16px;
    height: 16px;
    stroke: #fff;
}

/* ── Footer ────────────────────────────────────────────────────────────── */
.esig-chat-footer {
    text-align: center;
    font-size: 11px;
    color: var(--esig-text-light);
    padding: 6px 0 8px;
    margin: 0;
    background: var(--esig-bg);
    border-top: 1px solid var(--esig-border);
}
.esig-chat-footer a { color: var(--esig-primary); text-decoration: none; }
.esig-chat-footer a:hover { text-decoration: underline; }

/* ── Position gauche (optionnel) ───────────────────────────────────────── */
#esig-chat-root.esig-left {
    right: auto;
    left: 24px;
}
#esig-chat-root.esig-left #esig-chat-window {
    right: auto;
    left: 0;
    transform-origin: bottom left;
}

/* ── Responsive mobile ─────────────────────────────────────────────────── */
@media (max-width: 480px) {
    #esig-chat-root {
        bottom: 16px;
        right: 16px;
    }
    #esig-chat-window {
        width: calc(100vw - 32px);
        max-height: 75vh;
        bottom: calc(100% + 8px);
        right: 0;
    }
    #esig-chat-root.esig-left {
        left: 16px;
    }
    #esig-chat-root.esig-left #esig-chat-window {
        left: 0;
        width: calc(100vw - 32px);
    }
}

/* ── Reduced motion ────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    #esig-chat-window { animation: none; }
    .esig-thinking span { animation: none; opacity: 0.6; }
}

/* ── Menu ⋯ ─────────────────────────────────────────────────────────────── */
#esig-menu-btn {
    background: none;
    border: none;
    color: rgba(255,255,255,0.85);
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    line-height: 1;
    letter-spacing: 2px;
    transition: color 0.15s, background 0.15s;
    margin-right: 4px;
}
#esig-menu-btn:hover { color: #fff; background: rgba(255,255,255,0.15); }

#esig-chat-menu {
    position: absolute;
    top: 60px;
    right: 12px;
    background: #fff;
    border: 1px solid var(--esig-border);
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.12);
    z-index: 10;
    min-width: 200px;
    overflow: hidden;
}
#esig-chat-menu[hidden] { display: none; }

.esig-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 12px 16px;
    background: none;
    border: none;
    font-family: var(--esig-font);
    font-size: 14px;
    color: var(--esig-text);
    cursor: pointer;
    text-align: left;
    transition: background 0.15s;
}
.esig-menu-item:hover { background: var(--esig-surface); }
.esig-menu-item + .esig-menu-item { border-top: 1px solid var(--esig-border); }

/* ── Panel chats récents ─────────────────────────────────────────────────── */
#esig-recent-panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}
#esig-recent-panel[hidden] { display: none; }

#esig-recent-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--esig-border);
    background: var(--esig-surface);
}
#esig-recent-back {
    background: none;
    border: none;
    font-size: 13px;
    color: var(--esig-primary);
    cursor: pointer;
    font-family: var(--esig-font);
    padding: 0;
}
#esig-recent-header strong { font-size: 14px; }

#esig-recent-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.esig-recent-item {
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s;
    border: 1px solid var(--esig-border);
    margin-bottom: 6px;
}
.esig-recent-item:hover { background: var(--esig-surface); }
.esig-recent-item-date { font-size: 11px; color: var(--esig-text-light); margin-bottom: 3px; }
.esig-recent-item-preview { font-size: 13px; color: var(--esig-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.esig-no-recent { text-align: center; color: var(--esig-text-light); font-size: 13px; padding: 24px; }

/* ── Links in messages ───────────────────────────────────────────────────── */
.esig-msg.bot a {
    color: var(--esig-primary);
    text-decoration: underline;
    word-break: break-word;
    font-size: 0.95em;
}
.esig-msg.bot a:hover { opacity: 0.8; }
.esig-msg.user a { color: rgba(255,255,255,0.9); text-decoration: underline; }
