/* ==========================================================================
   DM Assistant — floating AI chat widget
   Every single selector below is scoped under #dm-assistant-root so it can
   NEVER leak onto the rest of the site, no matter what class names other
   templates use. Colours/fonts inherit the active theme via its own CSS
   variables (with safe fallbacks), so it matches automatically.
   ========================================================================== */

#dm-assistant-root {
    --dmai-primary:   var(--dm-primary, #E60023);
    --dmai-primary-h: var(--dm-primary-hover, #CC001F);
    --dmai-bg:        var(--dm-bg, #f7f7f8);
    --dmai-surface:   var(--dm-surface, #ffffff);
    --dmai-text:      var(--dm-text, #111111);
    --dmai-text-sec:  var(--dm-text-secondary, #6b7280);
    --dmai-text-mut:  var(--dm-text-muted, #9ca3af);
    --dmai-border:    var(--dm-border, #e5e7eb);
    --dmai-border-lt: var(--dm-border-light, #f3f4f6);
    --dmai-radius:    var(--dm-radius, 16px);
    --dmai-radius-sm: var(--dm-radius-sm, 10px);
    --dmai-pill:      var(--dm-radius-pill, 999px);
    --dmai-shadow-md: var(--dm-shadow-md, 0 4px 16px rgba(0,0,0,.1));
    --dmai-shadow-xl: var(--dm-shadow-xl, 0 24px 56px rgba(0,0,0,.16));
    --dmai-font:      var(--dm-font, 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif);
    --dmai-success:   var(--dm-success, #10b981);
    --dmai-trans:     var(--dm-transition, all .18s cubic-bezier(.4,0,.2,1));

    position: fixed;
    z-index: 2147483000;
    font-family: var(--dmai-font);
}

#dm-assistant-root * { box-sizing: border-box; }
#dm-assistant-root :focus-visible { outline: 2px solid var(--dmai-primary); outline-offset: 2px; }

/* ── Launcher bubble — docked to the right edge as a half-pill "tab" ───
   Pops fully visible on load; after a short pause it tucks itself about
   half off the edge (the "peek") until tapped. Same mechanism on both
   desktop and mobile. ──────────────────────────────────────────────── */

#dm-assistant-root .dmai-launcher {
    position: fixed;
    right: calc(env(safe-area-inset-right, 0px) + 20px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 24px);
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--dmai-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: none;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(0,0,0,.28), 0 0 0 5px rgba(230,0,35,.1);
    transition: transform .3s cubic-bezier(.34,1.25,.64,1), box-shadow .2s, opacity .2s;
    z-index: 2147483000;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}
#dm-assistant-root .dmai-launcher:hover  { box-shadow: 0 10px 28px rgba(0,0,0,.34), 0 0 0 6px rgba(230,0,35,.14); transform: translateY(-1px); }
#dm-assistant-root .dmai-launcher:active { transform: scale(.95); }
#dm-assistant-root .dmai-launcher svg { width: 26px; height: 26px; margin: 0; }
/* Custom uploaded icon fills the whole circular button */
#dm-assistant-root .dmai-launcher.has-custom-icon { background: var(--dmai-surface); }
#dm-assistant-root .dmai-launcher.has-custom-icon:hover { background: var(--dmai-surface); }
#dm-assistant-root .dmai-launcher-img { width: 100%; height: 100%; object-fit: cover; display: block; border-radius: 50%; }
#dm-assistant-root .dmai-launcher .dmai-icon-close { display: none; }

/* Peek: a subtle "here I am" nudge that keeps the button fully round and on-screen. */
#dm-assistant-root .dmai-launcher.is-peek { transform: none; }
#dm-assistant-root .dmai-launcher.is-peek:active { transform: scale(.95); }

/* Hidden (faded + shrunk) while the panel is open — the header's own
   close button is the only close action needed while chatting. */
#dm-assistant-root .dmai-launcher.is-open { opacity: 0; pointer-events: none; transform: scale(.6); }

#dm-assistant-root .dmai-launcher-badge {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--dmai-success);
    border: 2px solid var(--dmai-primary);
}
#dm-assistant-root .dmai-launcher.has-custom-icon .dmai-launcher-badge { border-color: var(--dmai-surface); }

@media (max-width: 768px) {
    /* Sit clear of the fixed mobile bottom nav (~72px tall) so they don't overlap */
    #dm-assistant-root .dmai-launcher {
        width: 54px;
        height: 54px;
        right: calc(env(safe-area-inset-right, 0px) + 16px);
        bottom: calc(env(safe-area-inset-bottom, 0px) + 84px);
    }
}

/* ── Panel ───────────────────────────────────────────────────────────── */

#dm-assistant-root .dmai-panel {
    position: fixed;
    right: 20px;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 96px);
    width: 392px;
    max-width: calc(100vw - 32px);
    height: min(640px, calc(100vh - 130px));
    background: var(--dmai-surface);
    border-radius: var(--dmai-radius);
    box-shadow: var(--dmai-shadow-xl);
    border: 1px solid var(--dmai-border);
    display: none;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(16px) scale(.97);
    transition: opacity .2s ease, transform .2s cubic-bezier(.34,1.56,.64,1);
}
#dm-assistant-root .dmai-panel.is-open { display: flex; opacity: 1; transform: translateY(0) scale(1); }

@media (max-width: 640px) {
    #dm-assistant-root .dmai-panel {
        right: 0;
        left: 0;
        bottom: 0;
        width: 100%;
        max-width: 100%;
        height: 88dvh;
        max-height: 88dvh;
        border-radius: 20px 20px 0 0;
        transform: translateY(100%);
    }
    #dm-assistant-root .dmai-panel.is-open { transform: translateY(0); }
}

/* ── Header ──────────────────────────────────────────────────────────── */

#dm-assistant-root .dmai-header {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 15px 16px;
    background: linear-gradient(180deg, color-mix(in srgb, var(--dmai-primary) 12%, transparent), transparent);
    border-bottom: 1px solid var(--dmai-border);
    color: var(--dmai-text);
    flex-shrink: 0;
}
#dm-assistant-root .dmai-header-logo {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--dmai-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
}
#dm-assistant-root .dmai-header-logo img { width: 100%; height: 100%; object-fit: cover; }
#dm-assistant-root .dmai-header-logo svg { width: 19px; height: 19px; }
#dm-assistant-root .dmai-header-title { font-size: 15px; font-weight: 800; letter-spacing: -.2px; line-height: 1.2; color: var(--dmai-text); }
#dm-assistant-root .dmai-header-sub { font-size: 11.5px; color: var(--dmai-text-sec); display: flex; align-items: center; gap: 5px; margin-top: 1px; }
#dm-assistant-root .dmai-header-sub-dot { width: 6px; height: 6px; border-radius: 50%; background: #22c55e; flex-shrink: 0; }
#dm-assistant-root .dmai-header-close {
    margin-left: auto;
    background: transparent;
    border: none;
    color: var(--dmai-text-mut);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background .15s, color .15s;
}
#dm-assistant-root .dmai-header-close:hover { background: var(--dmai-border-lt); color: var(--dmai-text); }
#dm-assistant-root .dmai-header-close svg { width: 16px; height: 16px; }

#dm-assistant-root .dmai-header-reset {
    margin-left: auto;
    background: transparent;
    border: none;
    color: var(--dmai-text-mut);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background .15s, color .15s;
}
#dm-assistant-root .dmai-header-reset:hover { background: var(--dmai-border-lt); color: var(--dmai-text); }
#dm-assistant-root .dmai-header-reset svg { width: 16px; height: 16px; }
#dm-assistant-root .dmai-header-close { margin-left: 2px; }

/* Starter suggestion chips under the greeting */
#dm-assistant-root .dmai-starters {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 12px 0 4px;
}
#dm-assistant-root .dmai-chip-suggest {
    background: var(--dmai-surface);
    border: 1px solid var(--dmai-border);
    color: var(--dmai-text);
    font-family: inherit;
    font-size: 12.5px;
    font-weight: 600;
    line-height: 1.2;
    padding: 8px 12px;
    border-radius: var(--dmai-pill);
    cursor: pointer;
    transition: var(--dmai-trans);
    text-align: left;
}
#dm-assistant-root .dmai-chip-suggest:hover {
    background: rgba(230,0,35,.06);
    border-color: var(--dmai-primary);
    color: var(--dmai-primary);
}

/* ── Message body ────────────────────────────────────────────────────── */

#dm-assistant-root .dmai-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--dmai-bg);
    -webkit-overflow-scrolling: touch;
}
#dm-assistant-root .dmai-body::-webkit-scrollbar { width: 5px; }
#dm-assistant-root .dmai-body::-webkit-scrollbar-thumb { background: var(--dmai-border); border-radius: 4px; }

#dm-assistant-root .dmai-row { display: flex; gap: 8px; max-width: 100%; }
#dm-assistant-root .dmai-row.user { justify-content: flex-end; }

#dm-assistant-root .dmai-bubble {
    max-width: 88%;
    padding: 11px 14px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.55;
    word-wrap: break-word;
}
#dm-assistant-root .dmai-row.bot .dmai-bubble {
    background: var(--dmai-surface);
    color: var(--dmai-text);
    border: 1px solid var(--dmai-border);
    border-top-left-radius: 4px;
    width: 100%;
}
#dm-assistant-root .dmai-row.user .dmai-bubble {
    background: var(--dmai-primary);
    color: #fff;
    border-top-right-radius: 4px;
}
#dm-assistant-root .dmai-bubble p { margin: 0 0 8px; }
#dm-assistant-root .dmai-bubble p:last-child { margin-bottom: 0; }
#dm-assistant-root .dmai-bubble strong { font-weight: 700; }
#dm-assistant-root .dmai-bubble a { color: var(--dmai-primary); text-decoration: underline; }
#dm-assistant-root .dmai-row.user .dmai-bubble a { color: #fff; }

#dm-assistant-root .dmai-bubble pre {
    background: rgba(0,0,0,.05);
    border: 1px solid var(--dmai-border);
    border-radius: 10px;
    padding: 10px 12px;
    font-size: 12.5px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    margin: 8px 0;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
[data-theme=dark] #dm-assistant-root .dmai-bubble pre { background: rgba(255,255,255,.06); }

#dm-assistant-root .dmai-copy-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-top: 6px;
    background: var(--dmai-primary);
    color: #fff;
    border: none;
    border-radius: var(--dmai-pill);
    padding: 6px 13px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    font-family: var(--dmai-font);
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-copy-btn:hover { background: var(--dmai-primary-h); }
#dm-assistant-root .dmai-copy-btn.copied { background: var(--dmai-success); }

/* ── Typing indicator ────────────────────────────────────────────────── */

#dm-assistant-root .dmai-typing { display: flex; gap: 4px; padding: 4px 2px; }
#dm-assistant-root .dmai-typing span {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--dmai-text-mut);
    animation: dmai-bounce 1.2s infinite ease-in-out;
}
#dm-assistant-root .dmai-typing span:nth-child(2) { animation-delay: .15s; }
#dm-assistant-root .dmai-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes dmai-bounce { 0%,80%,100% { transform: translateY(0); opacity:.5; } 40% { transform: translateY(-4px); opacity:1; } }

/* ── Prompt / trending cards — image LEFT, text RIGHT, stacked rows ────
   (deliberately a vertical list of full-width rows, not a horizontal
   scroller of narrow tiles — far more legible at chat-panel widths). ── */

#dm-assistant-root .dmai-card-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
}

#dm-assistant-root .dmai-card {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--dmai-surface);
    border: 1px solid var(--dmai-border);
    border-radius: var(--dmai-radius-sm);
    padding: 7px;
    text-decoration: none;
    color: inherit;
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-card:hover { border-color: var(--dmai-primary); box-shadow: var(--dmai-shadow-md); transform: translateY(-1px); }

#dm-assistant-root .dmai-card-img {
    width: 52px;
    height: 52px;
    flex-shrink: 0;
    border-radius: 9px;
    object-fit: cover;
    background: var(--dmai-border-lt);
    display: block;
}
#dm-assistant-root .dmai-card-img-ph {
    width: 52px;
    height: 52px;
    flex-shrink: 0;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--dmai-border-lt);
    font-size: 20px;
}

#dm-assistant-root .dmai-card-body { min-width: 0; flex: 1; }
#dm-assistant-root .dmai-card-title {
    font-size: 12.5px;
    font-weight: 700;
    color: var(--dmai-text);
    line-height: 1.35;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
#dm-assistant-root .dmai-card-excerpt {
    font-size: 11px;
    color: var(--dmai-text-sec);
    line-height: 1.3;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
#dm-assistant-root .dmai-card-arrow {
    flex-shrink: 0;
    color: var(--dmai-text-mut);
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-card:hover .dmai-card-arrow { color: var(--dmai-primary); transform: translateX(2px); }

/* Category pills */
#dm-assistant-root .dmai-chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
#dm-assistant-root .dmai-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--dmai-surface);
    border: 1.5px solid var(--dmai-border);
    border-radius: var(--dmai-pill);
    padding: 7px 13px;
    font-size: 12px;
    font-weight: 600;
    color: var(--dmai-text-sec);
    text-decoration: none;
    transition: var(--dmai-trans);
    white-space: nowrap;
}
#dm-assistant-root .dmai-chip:hover { border-color: var(--dmai-primary); color: var(--dmai-primary); background: rgba(230,0,35,.06); }

/* Site-page recommendation — visually distinct (left accent bar) from prompt cards */
#dm-assistant-root .dmai-page-card {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--dmai-surface);
    border: 1px solid var(--dmai-border);
    border-left: 3px solid var(--dmai-primary);
    border-radius: var(--dmai-radius-sm);
    padding: 10px 12px;
    text-decoration: none;
    margin-top: 8px;
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-page-card:hover { box-shadow: var(--dmai-shadow-md); transform: translateY(-1px); }
#dm-assistant-root .dmai-page-card-icon {
    flex-shrink: 0;
    width: 30px; height: 30px;
    border-radius: 50%;
    background: var(--dmai-primary);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
}
#dm-assistant-root .dmai-page-card-body { min-width: 0; }
#dm-assistant-root .dmai-page-card-title { font-size: 12.5px; font-weight: 700; color: var(--dmai-text); line-height: 1.3; }
#dm-assistant-root .dmai-page-card-desc { font-size: 11px; color: var(--dmai-text-sec); line-height: 1.3; margin-top: 1px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
#dm-assistant-root .dmai-page-card-arrow { margin-left: auto; flex-shrink: 0; color: var(--dmai-text-mut); }

#dm-assistant-root .dmai-section-label {
    font-size: 10.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--dmai-text-mut);
    margin: 10px 0 4px;
}
#dm-assistant-root .dmai-section-label:first-child { margin-top: 2px; }

/* ── Limit / login / upgrade CTA ─────────────────────────────────────── */

#dm-assistant-root .dmai-cta-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 10px;
    background: var(--dmai-primary);
    color: #fff !important;
    border-radius: var(--dmai-pill);
    padding: 9px 16px;
    font-size: 12.5px;
    font-weight: 700;
    text-decoration: none !important;
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-cta-btn:hover { background: var(--dmai-primary-h); }

#dm-assistant-root .dmai-discover-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 10px;
    background: transparent;
    color: var(--dmai-primary);
    border: 1.5px solid var(--dmai-primary);
    border-radius: var(--dmai-pill);
    padding: 8px 15px;
    font-size: 12.5px;
    font-weight: 700;
    font-family: var(--dmai-font);
    cursor: pointer;
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-discover-btn:hover { background: rgba(230,0,35,.07); }
#dm-assistant-root .dmai-discover-btn:disabled { opacity: .5; cursor: default; background: transparent; }

/* ── Composer ────────────────────────────────────────────────────────── */

#dm-assistant-root .dmai-composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--dmai-border);
    background: var(--dmai-surface);
    flex-shrink: 0;
}
#dm-assistant-root .dmai-input {
    flex: 1;
    resize: none;
    border: 1.5px solid var(--dmai-border);
    background: var(--dmai-bg);
    color: var(--dmai-text);
    border-radius: 14px;
    padding: 9px 13px;
    font-size: 13.5px;
    font-family: var(--dmai-font);
    max-height: 92px;
    outline: 0;
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-input:focus { border-color: var(--dmai-primary); background: var(--dmai-surface); }
#dm-assistant-root .dmai-input::placeholder { color: var(--dmai-text-mut); }

#dm-assistant-root .dmai-send-btn {
    flex-shrink: 0;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--dmai-primary);
    color: #fff;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--dmai-trans);
}
#dm-assistant-root .dmai-send-btn:hover { background: var(--dmai-primary-h); }
#dm-assistant-root .dmai-send-btn:disabled { opacity: .45; cursor: not-allowed; }
#dm-assistant-root .dmai-send-btn svg { width: 16px; height: 16px; }

#dm-assistant-root .dmai-footer-note {
    text-align: center;
    font-size: 10.5px;
    color: var(--dmai-text-mut);
    padding: 0 12px 10px;
    background: var(--dmai-surface);
}

/* ── Dark mode (mirrors the theme's own pattern exactly) ───────────────── */

[data-theme=dark] #dm-assistant-root .dmai-bubble pre { background: rgba(255,255,255,.07); }
[data-theme=dark] #dm-assistant-root .dmai-chip:hover,
[data-theme=dark] #dm-assistant-root .dmai-card:hover { background: rgba(230,0,35,.1); }
@media (prefers-color-scheme: dark) {
    html:not([data-theme=light]) #dm-assistant-root .dmai-bubble pre { background: rgba(255,255,255,.07); }
}

/* ── Reduced motion ──────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    #dm-assistant-root * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
