/* AP Chat Pro — Frontend Widget */

#apchat-root {
    --apchat-color: #29F9B5;
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 99999;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

#apchat-root.apchat-left {
    right: auto;
    left: 24px;
}

/* ── Toggle button ── */
#apchat-btn {
    min-width: 56px;
    height: 56px;
    border-radius: 28px;
    background: var(--apchat-color);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0 16px;
    box-shadow: 0 4px 16px rgba(0,0,0,.20);
    transition: transform 0.2s, box-shadow 0.2s, padding 0.2s;
    outline: none;
    position: relative;
    color: #000;
}

#apchat-root.apchat-circle #apchat-btn:not(.apchat-has-label *) {
    width: 56px;
    padding: 0;
}

#apchat-btn:hover {
    transform: scale(1.06);
    box-shadow: 0 6px 22px rgba(0,0,0,.24);
}

#apchat-btn svg {
    transition: opacity 0.15s, transform 0.15s;
    position: absolute;
    flex-shrink: 0;
}

/* Label mode: icons not absolute */
#apchat-root.apchat-has-label #apchat-btn svg {
    position: relative;
}

#apchat-btn .apchat-icon-chat  { opacity: 1;  transform: scale(1) rotate(0deg); }
#apchat-btn .apchat-icon-close { opacity: 0;  transform: scale(0.5) rotate(-45deg); }

#apchat-root.apchat-open #apchat-btn .apchat-icon-chat  { opacity: 0;  transform: scale(0.5) rotate(45deg); }
#apchat-root.apchat-open #apchat-btn .apchat-icon-close { opacity: 1;  transform: scale(1) rotate(0deg); position: absolute; }

/* ── Button label ── */
.apchat-btn-label {
    font-size: 14px;
    font-weight: 700;
    color: inherit;
    white-space: nowrap;
    transition: opacity 0.15s, max-width 0.2s;
    max-width: 120px;
    overflow: hidden;
}

#apchat-root.apchat-open .apchat-btn-label {
    opacity: 0;
    max-width: 0;
}

/* No label = true circle */
#apchat-root:not(.apchat-has-label) #apchat-btn {
    width: 56px;
    padding: 0;
    border-radius: 50%;
}

#apchat-root.apchat-rounded:not(.apchat-has-label) #apchat-btn {
    border-radius: 14px;
}

/* ── Attention bubble ── */
#apchat-bubble {
    position: absolute;
    bottom: 68px;
    right: 0;
    background: #fff;
    border-radius: 16px 16px 0 16px;
    box-shadow: 0 6px 24px rgba(0,0,0,.15);
    padding: 13px 40px 13px 16px;
    max-width: 240px;
    min-width: 160px;
    cursor: pointer;
    opacity: 0;
    transform: translateY(10px) scale(0.94);
    transition: opacity 0.25s ease, transform 0.25s ease;
    font-size: 13.5px;
    line-height: 1.55;
    color: #222;
    user-select: none;
}

#apchat-root.apchat-left #apchat-bubble {
    right: auto;
    left: 0;
    border-radius: 16px 16px 16px 0;
}

#apchat-bubble.apchat-bubble--visible {
    opacity: 1;
    transform: translateY(0) scale(1);
}

#apchat-bubble::after {
    content: '';
    position: absolute;
    bottom: -8px;
    right: 20px;
    border: 8px solid transparent;
    border-top-color: #fff;
    border-bottom: 0;
}

#apchat-root.apchat-left #apchat-bubble::after {
    right: auto;
    left: 20px;
}

.apchat-bubble-close {
    position: absolute;
    top: 7px;
    right: 9px;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 15px;
    color: #bbb;
    line-height: 1;
    padding: 3px 5px;
    border-radius: 4px;
    transition: color 0.15s, background 0.15s;
}
.apchat-bubble-close:hover { color: #555; background: #f5f5f5; }

/* ── Widget window ── */
#apchat-window {
    position: absolute;
    bottom: 70px;
    right: 0;
    width: 380px;
    background: #fff;
    border-radius: 18px;
    box-shadow: 0 10px 48px rgba(0,0,0,.18);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform-origin: bottom right;
    transform: scale(0.92) translateY(12px);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.22s cubic-bezier(.32,1.4,.64,1), opacity 0.16s;
}

#apchat-root.apchat-left #apchat-window {
    right: auto;
    left: 0;
    transform-origin: bottom left;
}

#apchat-root.apchat-open #apchat-window {
    transform: scale(1) translateY(0);
    opacity: 1;
    pointer-events: auto;
}

#apchat-root.apchat-sm #apchat-window { width: 320px; }
#apchat-root.apchat-md #apchat-window { width: 380px; }
#apchat-root.apchat-lg #apchat-window { width: 440px; }

/* ── Header ── */
#apchat-header {
    background: var(--apchat-color);
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-shrink: 0;
}

#apchat-title {
    font-weight: 700;
    font-size: 15px;
    color: #000;
    opacity: 0.88;
}

.apchat-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

#apchat-close-btn,
#apchat-reset-btn {
    background: rgba(0,0,0,.08);
    border: none;
    cursor: pointer;
    padding: 5px;
    display: flex;
    align-items: center;
    border-radius: 8px;
    color: #000;
    opacity: 0.6;
    transition: opacity 0.15s, background 0.15s;
}
#apchat-close-btn:hover,
#apchat-reset-btn:hover { opacity: 1; background: rgba(0,0,0,.14); }

/* ── Messages ── */
#apchat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-height: 220px;
    max-height: 360px;
    scroll-behavior: smooth;
}

.apchat-msg {
    display: flex;
    flex-direction: column;
    max-width: 82%;
}

.apchat-msg--bot  { align-self: flex-start; }
.apchat-msg--user { align-self: flex-end; }

.apchat-bubble {
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 13.5px;
    line-height: 1.65;
    word-break: break-word;
    white-space: pre-wrap;
}

.apchat-msg--bot .apchat-bubble {
    background: #f2f2f2;
    color: #1a1a1a;
    border-bottom-left-radius: 4px;
}

.apchat-msg--user .apchat-bubble {
    background: var(--apchat-color);
    color: #000;
    border-bottom-right-radius: 4px;
}

/* ── Timestamps ── */
.apchat-time {
    font-size: 10px;
    color: #bbb;
    margin-top: 3px;
    padding: 0 4px;
}
.apchat-msg--user .apchat-time { text-align: right; }
.apchat-msg--bot  .apchat-time { text-align: left; }

/* ── Error & retry ── */
.apchat-msg--error .apchat-bubble {
    background: #fff8f8;
    color: #888;
    border: 1px solid #f5d5d5;
}
.apchat-error-icon { color: #e88; }

.apchat-retry-btn {
    background: none;
    border: 1px solid #ddd;
    border-radius: 12px;
    padding: 2px 10px;
    font-size: 11.5px;
    cursor: pointer;
    margin-left: 8px;
    color: #666;
    font-family: inherit;
    transition: background 0.15s;
}
.apchat-retry-btn:hover { background: #f5f5f5; }

/* ── Markdown ── */
.apchat-bubble strong { font-weight: 700; }
.apchat-bubble code {
    background: rgba(0,0,0,.07);
    border-radius: 4px;
    padding: 1px 5px;
    font-size: 12px;
    font-family: 'SF Mono', Menlo, monospace;
}
.apchat-list {
    margin: 4px 0 0 0;
    padding-left: 18px;
}
.apchat-list li { margin-bottom: 2px; }

/* ── Suggested questions ── */
.apchat-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
    padding: 2px 0 10px;
    align-self: stretch;
}

.apchat-suggestion-chip {
    background: #fff;
    border: 1.5px solid var(--apchat-color);
    color: #333;
    border-radius: 20px;
    padding: 6px 15px;
    font-size: 12.5px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, transform 0.1s;
    white-space: nowrap;
    font-family: inherit;
    line-height: 1.4;
}

.apchat-suggestion-chip:hover {
    background: var(--apchat-color);
    color: #000;
    transform: translateY(-1px);
}

/* ── Typing indicator ── */
.apchat-typing .apchat-bubble { padding: 13px 16px; }

.apchat-dots {
    display: flex;
    gap: 5px;
    align-items: center;
}

.apchat-dots span {
    width: 7px;
    height: 7px;
    background: #bbb;
    border-radius: 50%;
    animation: apchat-bounce 1.3s infinite;
}
.apchat-dots span:nth-child(2) { animation-delay: 0.2s; }
.apchat-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes apchat-bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30%           { transform: translateY(-6px); }
}

/* ── Footer / Input ── */
#apchat-footer {
    border-top: 1px solid #ebebeb;
    padding: 10px 12px;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    flex-shrink: 0;
}

#apchat-input {
    flex: 1;
    border: 1.5px solid #e0e0e0;
    border-radius: 22px;
    padding: 8px 14px;
    font-size: 13.5px;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 100px;
    overflow-y: auto;
    font-family: inherit;
    transition: border-color 0.15s, background 0.15s;
    background: #fafafa;
    color: #1a1a1a;
}

#apchat-input:focus {
    border-color: var(--apchat-color);
    background: #fff;
}

#apchat-send-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--apchat-color);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #000;
    transition: opacity 0.15s, transform 0.15s;
}

#apchat-send-btn:hover    { opacity: 0.84; transform: scale(1.06); }
#apchat-send-btn:disabled { opacity: 0.35; cursor: default; transform: none; }

/* ── Mobile ── */
@media (max-width: 480px) {
    #apchat-root {
        bottom: 16px;
        right: 16px;
    }
    #apchat-root.apchat-left {
        left: 16px;
        right: auto;
    }
    #apchat-window {
        width: calc(100vw - 32px) !important;
        right: 0;
        left: auto;
        border-radius: 14px;
        max-height: 75vh;
    }
    #apchat-root.apchat-left #apchat-window {
        left: 0;
        right: auto;
    }
    #apchat-messages {
        max-height: calc(75vh - 140px);
    }
}
