/* ---------- Widget shell ---------- */
#chatbudgie-widget {
    all: revert;
    --chatbudgie-accent: #3f7cf5;
    --chatbudgie-accent-strong: #2f6ee8;
    --chatbudgie-surface: #ffffff;
    --chatbudgie-surface-soft: #f5f8ff;
    --chatbudgie-border: #e6ebf5;
    --chatbudgie-text: #182233;
    --chatbudgie-muted: #748099;
    --chatbudgie-danger-bg: #fdecec;
    --chatbudgie-danger: #e53935;
    position: fixed;
    right: 24px;
    bottom: 24px;
    z-index: 999999;
    width: min(420px, calc(100vw - 24px));
    max-height: calc(100vh - 24px);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    line-height: 1.5;
    font-size: 16px;
    font-weight: 400;
}

#chatbudgie-widget *,
#chatbudgie-widget *::before,
#chatbudgie-widget *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
    text-transform: none;
    letter-spacing: normal;
    word-spacing: normal;
    text-shadow: none;
    transition: none;
    text-decoration: none;
    list-style: none;
    text-align: left;
}

#chatbudgie-widget.is-open {
    background: var(--chatbudgie-surface);
    border: 1px solid rgba(225, 232, 244, 0.9);
    border-radius: 14px;
    box-shadow:
        0 28px 64px rgba(24, 39, 75, 0.16),
        0 8px 24px rgba(24, 39, 75, 0.08);
    overflow: hidden;
    max-height: min(640px, calc(100vh - 24px));
}

#chatbudgie-toggle {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--chatbudgie-accent-strong);
    background: linear-gradient(180deg, var(--chatbudgie-accent) 0%, var(--chatbudgie-accent-strong) 100%);
    color: #ffffff;
    box-shadow:
        0 16px 30px rgba(47, 110, 232, 0.28),
        0 4px 14px rgba(24, 39, 75, 0.16);
    cursor: pointer;
    transition: transform 0.18s ease, box-shadow 0.18s ease, opacity 0.18s ease;
}

#chatbudgie-toggle:hover {
    transform: translateY(-2px);
    box-shadow:
        0 20px 34px rgba(47, 110, 232, 0.32),
        0 8px 18px rgba(24, 39, 75, 0.18);
}

#chatbudgie-toggle:active {
    transform: translateY(0);
}

#chatbudgie-toggle svg,
#chatbudgie-toggle img {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: block;
}

#chatbudgie-widget:not(.is-open) #chatbudgie-header,
#chatbudgie-widget:not(.is-open) #chatbudgie-messages,
#chatbudgie-widget:not(.is-open) #chatbudgie-form {
    display: none;
}

#chatbudgie-widget.is-open #chatbudgie-toggle {
    opacity: 0;
    pointer-events: none;
    position: absolute;
    right: 18px;
    bottom: 18px;
    transform: scale(0.9);
}

/* ---------- Header ---------- */
#chatbudgie-header {
    width: 100%;
    background: var(--chatbudgie-accent-strong);
    background: linear-gradient(180deg, var(--chatbudgie-accent) 0%, var(--chatbudgie-accent-strong) 100%);
    color: #ffffff;
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

#chatbudgie-header .chatbudgie-header__brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

#chatbudgie-header .chatbudgie-header__avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

#chatbudgie-header .chatbudgie-header__title {
    margin: 0;
    font-size: 19px;
    font-weight: 700;
    line-height: 1.1;
    letter-spacing: 0.01em;
    color: white;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
            Arial, sans-serif;
}

#chatbudgie-close-btn {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: #ffffff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.16s ease, transform 0.16s ease;
}

#chatbudgie-close-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

#chatbudgie-close-btn:active {
    transform: scale(0.96);
}

/* ---------- Messages ---------- */
#chatbudgie-messages {
    width: 100%;
    height: min(560px, calc(100vh - 196px));
    min-height: 360px;
    max-height: calc(100vh - 196px);
    padding: 18px 16px 14px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow-y: auto;
    background:
        radial-gradient(circle at top left, rgba(99, 146, 255, 0.14), transparent 32%),
        linear-gradient(180deg, #ffffff 0%, #fbfcff 100%);
}

#chatbudgie-messages .chatbudgie-msg {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
}

#chatbudgie-messages .chatbudgie-msg--user {
    justify-content: flex-end;
}

#chatbudgie-messages .chatbudgie-msg--bot {
    justify-content: flex-start;
}

#chatbudgie-messages .chatbudgie-bot-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

#chatbudgie-messages .chatbudgie-bubble {
    max-width: min(78%, 300px);
    padding: 12px 15px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.5;
    color: var(--chatbudgie-text);
    word-break: break-word;
}

#chatbudgie-messages .chatbudgie-bubble--user {
    background: #eff3fb;
    border-bottom-right-radius: 6px;
}

#chatbudgie-messages .chatbudgie-bubble--bot {
    background: #eaf2ff;
    border-bottom-left-radius: 6px;
}

/* ---------- Markdown Content ---------- */
#chatbudgie-messages .chatbudgie-bubble--bot > *:first-child { margin-top: 0; }
#chatbudgie-messages .chatbudgie-bubble--bot > *:last-child { margin-bottom: 0; }

#chatbudgie-messages .chatbudgie-bubble--bot p {
    margin: 0 0 12px;
}

#chatbudgie-messages .chatbudgie-bubble--bot ul, 
#chatbudgie-messages .chatbudgie-bubble--bot ol {
    margin: 0 0 12px 20px;
    padding: 0;
}

#chatbudgie-messages .chatbudgie-bubble--bot li {
    margin-bottom: 4px;
}

#chatbudgie-messages .chatbudgie-bubble--bot code {
    background: rgba(0, 0, 0, 0.06);
    padding: 2px 5px;
    border-radius: 4px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.9em;
}

#chatbudgie-messages .chatbudgie-bubble--bot pre {
    background: rgba(0, 0, 0, 0.06);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 12px 0;
}

#chatbudgie-messages .chatbudgie-bubble--bot pre code {
    background: none;
    padding: 0;
    display: block;
    line-height: 1.4;
    white-space: pre;
}

#chatbudgie-messages .chatbudgie-bubble--bot h1, 
#chatbudgie-messages .chatbudgie-bubble--bot h2, 
#chatbudgie-messages .chatbudgie-bubble--bot h3 {
    margin: 16px 0 8px;
    font-size: 1.1em;
    font-weight: 600;
}

#chatbudgie-messages .chatbudgie-bubble--bot blockquote {
    margin: 12px 0;
    padding-left: 12px;
    border-left: 3px solid rgba(0, 0, 0, 0.1);
    color: rgba(0, 0, 0, 0.7);
}

#chatbudgie-messages .chatbudgie-bubble--loading {
    min-width: 72px;
    padding: 14px 16px;
}

#chatbudgie-messages .chatbudgie-typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

#chatbudgie-messages .chatbudgie-typing-indicator span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--chatbudgie-accent);
    opacity: 0.85;
    animation: chatbudgie-typing 1.1s infinite ease-in-out;
}

#chatbudgie-messages .chatbudgie-typing-indicator span:nth-child(2) {
    animation-delay: 0.16s;
}

#chatbudgie-messages .chatbudgie-typing-indicator span:nth-child(3) {
    animation-delay: 0.32s;
}

@keyframes chatbudgie-typing {
    0%, 80%, 100% {
        transform: translateY(0);
        opacity: 0.45;
    }

    40% {
        transform: translateY(-3px);
        opacity: 1;
    }
}

/* ---------- Error banner ---------- */
#chatbudgie-error-banner {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    background: var(--chatbudgie-danger-bg);
    border: 1px solid rgba(229, 57, 53, 0.08);
    border-radius: 16px;
    padding: 14px;
}

#chatbudgie-error-banner[hidden] {
    display: none;
}

#chatbudgie-error-banner .chatbudgie-error-banner__icon {
    width: 32px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

#chatbudgie-error-banner .chatbudgie-error-banner__content {
    flex: 1;
    min-width: 0;
}

#chatbudgie-error-banner .chatbudgie-error-banner__title {
    margin: 0 0 4px;
    color: var(--chatbudgie-danger);
    font-size: 14px;
    font-weight: 700;
}

#chatbudgie-error-banner .chatbudgie-error-banner__text {
    margin: 0;
    color: var(--chatbudgie-text);
    font-size: 13px;
    line-height: 1.45;
}

#chatbudgie-retry-btn {
    flex-shrink: 0;
    min-width: 82px;
    border: 1px solid color-mix(in srgb, var(--chatbudgie-accent), transparent 86%);
    border-radius: 12px;
    background: #ffffff;
    color: var(--chatbudgie-accent);
    padding: 8px 10px;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 700;
    transition: transform 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

#chatbudgie-retry-btn:hover {
    background: #f6f9ff;
    transform: translateY(-1px);
    box-shadow: 0 6px 14px color-mix(in srgb, var(--chatbudgie-accent), transparent 88%);
}

#chatbudgie-retry-btn:active {
    transform: translateY(0);
}

/* ---------- Input ---------- */
#chatbudgie-form {
    width: 100%;
    padding: 14px 16px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: #ffffff;
    border-top: 1px solid var(--chatbudgie-border);
}

#chatbudgie-input-field {
    flex: 1;
    height: 48px;
    border: 1px solid transparent;
    border-radius: 14px;
    outline: none;
    background: #f3f6fb;
    color: var(--chatbudgie-text);
    padding: 0 18px;
    font-size: 14px;
    transition: border-color 0.16s ease, background 0.16s ease, box-shadow 0.16s ease;
}

#chatbudgie-input-field::placeholder {
    color: #97a2b6;
}

#chatbudgie-input-field:focus {
    background: #eef4ff;
    border-color: color-mix(in srgb, var(--chatbudgie-accent), transparent 82%);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--chatbudgie-accent), transparent 88%);
}

#chatbudgie-input-field:disabled {
    cursor: not-allowed;
    opacity: 0.75;
}

#chatbudgie-send-btn,
#chatbudgie-stop-btn {
    width: 46px;
    height: 46px;
    border: none;
    border-radius: 50%;
    background: #edf3ff;
    color: var(--chatbudgie-accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.16s ease, background 0.16s ease, opacity 0.16s ease;
}

#chatbudgie-send-btn[hidden],
#chatbudgie-stop-btn[hidden] {
    display: none !important;
}

#chatbudgie-send-btn:hover,
#chatbudgie-stop-btn:hover {
    background: #dfebff;
}

#chatbudgie-send-btn:active,
#chatbudgie-stop-btn:active {
    transform: scale(0.95);
}

#chatbudgie-send-btn:disabled,
#chatbudgie-stop-btn:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

#chatbudgie-send-btn svg,
#chatbudgie-stop-btn svg {
    display: block;
}

/* ---------- Scrollbar ---------- */
#chatbudgie-messages::-webkit-scrollbar {
    width: 6px;
}

#chatbudgie-messages::-webkit-scrollbar-thumb {
    background: #d6dceb;
    border-radius: 999px;
}

#chatbudgie-messages::-webkit-scrollbar-track {
    background: transparent;
}

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
    #chatbudgie-widget {
        right: 12px;
        bottom: 12px;
        width: calc(100vw - 24px);
        max-height: calc(100vh - 24px);
    }

    #chatbudgie-widget.is-open {
        max-height: calc(100vh - 24px);
    }

    #chatbudgie-messages {
        height: calc(100vh - 204px);
        min-height: 300px;
    }
}

@media (max-height: 640px) {
    #chatbudgie-widget {
        right: 10px;
        bottom: 10px;
        width: min(420px, calc(100vw - 20px));
        max-height: calc(100vh - 20px);
    }

    #chatbudgie-widget.is-open {
        border-radius: 20px;
        max-height: calc(100vh - 20px);
    }

    #chatbudgie-header {
        padding: 14px 14px 12px;
    }

    #chatbudgie-messages {
        height: calc(100vh - 164px);
        min-height: 180px;
        max-height: calc(100vh - 164px);
        padding: 14px 14px 10px;
        gap: 12px;
    }

    #chatbudgie-form {
        padding: 10px 14px 14px;
    }

    #chatbudgie-input-field,
    #chatbudgie-send-btn {
        height: 42px;
    }

    #chatbudgie-send-btn {
        width: 42px;
    }
}
