/* Hamman AI Chatbot — widget stylesheet.
 * Loaded twice on purpose: once as a normal wp_enqueue_style() <link> in the
 * host page (so it's a real, cacheable, versioned asset per WordPress
 * convention), and a second time via a <link> the widget script creates
 * inside its own Shadow DOM root — that second load is what actually
 * isolates these rules from the host theme's CSS (and isolates the host
 * theme's CSS from these rules). The browser serves the second load from
 * cache, so this costs nothing extra over the network.
 *
 * :host{} rules below target the shadow *host* element (the plain div the
 * script attaches the shadow root to) from inside the shadow tree — that's
 * the standard way to style the widget's own outer box from its own
 * stylesheet.
 */

/* Which corner the widget occupies is controlled by :host's own
 * data-position attribute (set from widget_config in hamman-widget.js),
 * never by `dir` — text direction and on-page position are independent
 * settings. A Persian chatbot must be able to stay bottom-right (the
 * common convention) and vice versa. Physical `right`/`left` (not
 * inset-inline-*) so the corner doesn't silently flip when dir changes. */
:host {
    all: initial;
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 2147483000;
    --hm-primary: #1B3A6B;
    --hm-primary-contrast: #ffffff;
    --hm-font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    font-family: var(--hm-font-family);
    display: block;
}

:host([data-position="bottom-left"]) {
    right: auto;
    left: 24px;
}

:host * {
    box-sizing: border-box;
}

/* align-items: flex-end/flex-start here is a physical choice (which side
 * of :host the button/window hug), not a text-direction one — it must
 * follow data-position, not #hm-w's own `dir` attribute, or the button
 * would drift to the wrong side whenever dir and position disagree. */
#hm-w {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

:host([data-position="bottom-left"]) #hm-w {
    align-items: flex-start;
}

#hm-box {
    display: none;
    flex-direction: column;
    width: 360px;
    height: 520px;
    max-height: 80vh;
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, .18);
    overflow: hidden;
    margin-bottom: 12px;
}

#hm-box.hm-open {
    display: flex;
}

#hm-hdr {
    background: var(--hm-primary);
    color: var(--hm-primary-contrast);
    padding: 14px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

#hm-hdr h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
}

#hm-hdr span {
    font-size: 11px;
    opacity: .75;
}

#hm-close {
    background: none;
    border: none;
    color: inherit;
    font-size: 20px;
    cursor: pointer;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
}

#hm-close:hover, #hm-close:focus-visible {
    background: rgba(255, 255, 255, .15);
}

#hm-msgs {
    flex: 1;
    overflow-y: auto;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    position: relative;
}

.hm-msg {
    max-width: 82%;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
    position: relative;
}

.hm-msg.user {
    background: var(--hm-primary);
    color: var(--hm-primary-contrast);
    align-self: flex-end;
}

.hm-msg.bot {
    background: #F1F5F9;
    color: #0F172A;
    align-self: flex-start;
    padding-inline-end: 34px;
}

.hm-msg-copy {
    position: absolute;
    top: 6px;
    inset-inline-end: 6px;
    width: 22px;
    height: 22px;
    padding: 0;
    border: none;
    background: transparent;
    color: #64748B;
    cursor: pointer;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: .6;
}

.hm-msg-copy:hover, .hm-msg-copy:focus-visible {
    opacity: 1;
    background: rgba(15, 23, 42, .08);
}

/* Typing indicator: three animated dots shown while waiting for a response. */
.hm-typing {
    align-self: flex-start;
    background: #F1F5F9;
    border-radius: 12px;
    padding: 10px 14px;
    display: flex;
    gap: 4px;
    align-items: center;
}

.hm-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #94A3B8;
    animation: hm-typing-bounce 1.2s infinite ease-in-out;
}

.hm-typing span:nth-child(2) { animation-delay: .15s; }
.hm-typing span:nth-child(3) { animation-delay: .3s; }

@keyframes hm-typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .5; }
    30% { transform: translateY(-4px); opacity: 1; }
}

#hm-scroll-bottom {
    position: absolute;
    bottom: 10px;
    inset-inline-end: 14px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid #E2E8F0;
    background: #fff;
    color: var(--hm-primary);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .12);
}

#hm-scroll-bottom.hm-visible {
    display: flex;
}

#hm-qq {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 0 14px 10px;
    flex-shrink: 0;
}

#hm-qq button {
    background: #fff;
    border: 1px solid var(--hm-primary);
    color: var(--hm-primary);
    border-radius: 10px;
    padding: 7px 10px;
    font-size: 12.5px;
    cursor: pointer;
    text-align: start;
    min-height: 44px;
}

#hm-in-row {
    padding: 10px 12px;
    border-top: 1px solid #E2E8F0;
    display: flex;
    gap: 8px;
    align-items: flex-end;
    flex-shrink: 0;
}

#hm-in {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid #CBD5E1;
    border-radius: 10px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
    resize: none;
    max-height: calc(1.4em * 4 + 20px);
    line-height: 1.4;
}

#hm-in:focus-visible {
    border-color: var(--hm-primary);
}

#hm-send {
    background: var(--hm-primary);
    color: var(--hm-primary-contrast);
    border: none;
    border-radius: 10px;
    padding: 10px 16px;
    cursor: pointer;
    font-size: 14px;
    min-width: 44px;
    min-height: 44px;
}

#hm-send:disabled {
    opacity: .5;
    cursor: default;
}

#hm-powered {
    text-align: center;
    font-size: 11px;
    color: #94A3B8;
    padding: 6px;
    background: #F8FAFC;
    flex-shrink: 0;
}

#hm-powered a {
    color: var(--hm-primary);
    text-decoration: none;
}

#hm-btn {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--hm-primary);
    color: var(--hm-primary-contrast);
    border: none;
    cursor: pointer;
    font-size: 24px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, .25);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

#hm-btn:focus-visible {
    outline: 2px solid var(--hm-primary);
    outline-offset: 3px;
}

#hm-unread-dot {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #EF4444;
    border: 2px solid #fff;
    display: none;
}

:host([data-position="bottom-left"]) #hm-unread-dot {
    right: auto;
    left: 2px;
}

#hm-unread-dot.hm-visible {
    display: block;
}

/* ── Mobile: full-screen sheet below 480px ─────────────────────────── */
@media (max-width: 480px) {
    /* Scoped to .hm-mobile-open (set on the host itself by openWidget()/
     * closeWidget() in hamman-widget.js) — NOT a bare :host. An
     * unconditional :host here would size the shadow host to the full
     * viewport at all times below 480px, and since a plain box (no
     * pointer-events:none) captures clicks across its whole bounding
     * rect regardless of visible content, that silently made the entire
     * page unclickable on mobile even while the widget was closed. */
    :host(.hm-mobile-open) {
        bottom: 0;
        inset-inline-end: 0;
        inset-inline-start: 0;
        top: 0;
    }

    #hm-w {
        align-items: stretch;
        height: 100%;
    }

    #hm-box {
        display: none;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        max-height: none;
        border-radius: 0;
        margin-bottom: 0;
        position: fixed;
        inset: 0;
    }

    #hm-box.hm-open {
        display: flex;
    }

    #hm-hdr {
        padding-top: calc(14px + env(safe-area-inset-top, 0px));
    }

    #hm-in-row {
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }

    #hm-btn {
        position: fixed;
        bottom: calc(24px + env(safe-area-inset-bottom, 0px));
        right: calc(24px + env(safe-area-inset-right, 0px));
    }

    :host([data-position="bottom-left"]) #hm-btn {
        right: auto;
        left: calc(24px + env(safe-area-inset-left, 0px));
    }

    /* Widget open on mobile: the floating button sits behind the fullscreen
       sheet anyway, but hiding it avoids it flashing above the keyboard. */
    #hm-w.hm-mobile-open #hm-btn {
        display: none;
    }
}
