.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.ai-chat-widget {
    position: fixed;
    right: max(22px, env(safe-area-inset-right));
    bottom: max(22px, env(safe-area-inset-bottom));
    z-index: 2400;
    font-family: "Poppins", sans-serif;
}

body.menu-open .ai-chat-widget {
    display: none;
}

.ai-chat-toggle {
    position: relative;
    width: 56px;
    height: 56px;
    display: grid;
    place-items: center;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    color: var(--text-primary);
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow:
        0 4px 24px var(--shadow-color),
        inset 0 1px 0 var(--border-color);
    cursor: pointer;
    transition:
        transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
        box-shadow 0.3s ease,
        background 0.3s ease,
        color 0.3s ease;
}

.ai-chat-toggle:hover,
.ai-chat-toggle:focus-visible {
    transform: scale(1.08);
    color: var(--text-primary);
    background: var(--glass-hover);
    box-shadow:
        0 8px 32px var(--shadow-color),
        0 0 0 6px rgba(168, 85, 247, 0.12),
        inset 0 1px 0 var(--border-color);
    outline: none;
}

.ai-chat-toggle i {
    position: relative;
    z-index: 2;
    font-size: 1.25rem;
    filter: drop-shadow(0 1px 2px var(--shadow-color));
}

.ai-chat-pulse {
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    pointer-events: none;
}
.ai-chat-panel {
    position: absolute;
    right: 0;
    bottom: 78px;
    width: min(460px, calc(100vw - 32px));
    height: min(700px, calc(100vh - 120px));
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: 22px;
    background:
        linear-gradient(180deg, var(--glass-bg), var(--surface));
    box-shadow:
        0 28px 80px var(--shadow-color),
        0 0 36px rgba(147, 51, 234, 0.18);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(18px) scale(0.96);
    transform-origin: bottom right;
    transition:
        opacity 0.25s ease,
        transform 0.25s ease,
        visibility 0.25s ease;
}

.ai-chat-widget.is-open .ai-chat-panel {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateY(0) scale(1);
}

.ai-chat-widget.is-open .ai-chat-toggle {
    transform: scale(0.94);
    opacity: 0.5;
}

.ai-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 20px 20px 16px;
    border-bottom: 1px solid var(--border-color);
    background: linear-gradient(
        135deg,
        rgba(147, 51, 234, 0.24),
        var(--input-bg)
    );
}

.ai-chat-eyebrow {
    margin: 0 0 2px;
    color: var(--primary-light);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.ai-chat-header h2 {
    margin: 0;
    font-size: 1.12rem;
    line-height: 1.2;
}

.ai-chat-close {
    width: 38px;
    height: 38px;
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    color: var(--text-primary);
    background: var(--input-bg);
    cursor: pointer;
    transition:
        background 0.2s ease,
        transform 0.2s ease;
}

.ai-chat-close:hover,
.ai-chat-close:focus-visible {
    background: var(--glass-bg);
    transform: translateY(-1px);
    outline: none;
}

.ai-chat-messages {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 18px;
    overflow-y: auto;
    scroll-behavior: smooth;
}

.ai-chat-message {
    max-width: 84%;
    animation: aiMessageIn 0.22s ease both;
}

.ai-chat-message-content,
.ai-chat-message > p {
    margin: 0;
    overflow-wrap: anywhere;
    line-height: 1.55;
    font-size: 0.92rem;
}

.ai-chat-message-bot {
    align-self: flex-start;
}

.ai-chat-message-bot .ai-chat-message-content,
.ai-chat-message-bot > p {
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 16px 16px 16px 6px;
    padding: 12px 14px;
    background: var(--input-bg);
}

.ai-chat-message-user {
    align-self: flex-end;
}

.ai-chat-message-user .ai-chat-message-content,
.ai-chat-message-user > p {
    white-space: pre-wrap;
    color: var(--text-primary);
    border-radius: 16px 16px 6px 16px;
    padding: 12px 14px;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    box-shadow: 0 10px 24px rgba(147, 51, 234, 0.22);
}

.ai-chat-message.is-streaming .ai-chat-message-content::after,
.ai-chat-message.is-streaming > p::after {
    content: "";
    display: inline-block;
    width: 7px;
    height: 1em;
    margin-left: 4px;
    vertical-align: -0.15em;
    border-radius: 999px;
    background: var(--primary-light);
    animation: aiCursor 0.8s steps(2, start) infinite;
}

.ai-chat-message.has-error .ai-chat-message-content,
.ai-chat-message.has-error > p {
    color: #fecaca;
    border-color: rgba(248, 113, 113, 0.35);
    background: rgba(127, 29, 29, 0.2);
}

/* --- Markdown-Elemente in Bot-Nachrichten --- */
.ai-chat-message-bot .ai-chat-message-content p {
    margin: 0;
}

.ai-chat-message-bot .ai-chat-message-content p + p,
.ai-chat-message-bot .ai-chat-message-content p + ul,
.ai-chat-message-bot .ai-chat-message-content p + ol,
.ai-chat-message-bot .ai-chat-message-content ul + p,
.ai-chat-message-bot .ai-chat-message-content ol + p,
.ai-chat-message-bot .ai-chat-message-content pre + p {
    margin-top: 10px;
}

.ai-chat-message-bot .ai-chat-message-content code,
.ai-chat-message-bot > p code {
    padding: 2px 6px;
    border-radius: 5px;
    font-family: "Fira Code", "Cascadia Code", Consolas, monospace;
    font-size: 0.84rem;
    background: var(--tag-bg);
    color: #e2b8ff;
}

.ai-chat-message-bot .ai-chat-message-content pre,
.ai-chat-message-bot > p pre {
    margin: 8px 0;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--tag-bg);
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
}

.ai-chat-message-bot .ai-chat-message-content pre code,
.ai-chat-message-bot > p pre code {
    padding: 0;
    background: none;
    color: #e2b8ff;
    font-size: 0.82rem;
}

.ai-chat-message-bot .ai-chat-message-content strong,
.ai-chat-message-bot > p strong {
    font-weight: 700;
    color: var(--text-primary);
}

.ai-chat-message-bot .ai-chat-message-content em,
.ai-chat-message-bot > p em {
    font-style: italic;
    color: var(--text-secondary);
}

.ai-chat-message-bot .ai-chat-message-content a,
.ai-chat-message-bot > p a {
    color: var(--primary-light);
    text-decoration: underline;
    text-underline-offset: 2px;
    transition: color 0.2s ease;
}

.ai-chat-message-bot .ai-chat-message-content a:hover,
.ai-chat-message-bot > p a:hover {
    color: #c084fc;
}

.ai-chat-message-bot .ai-chat-message-content h2,
.ai-chat-message-bot .ai-chat-message-content h3,
.ai-chat-message-bot .ai-chat-message-content h4,
.ai-chat-message-bot > p h2,
.ai-chat-message-bot > p h3,
.ai-chat-message-bot > p h4 {
    margin: 8px 0 4px;
    font-weight: 700;
    line-height: 1.3;
    color: var(--text-primary);
}

.ai-chat-message-bot .ai-chat-message-content h2,
.ai-chat-message-bot > p h2 {
    font-size: 1.08rem;
}

.ai-chat-message-bot .ai-chat-message-content h3,
.ai-chat-message-bot > p h3 {
    font-size: 1rem;
}

.ai-chat-message-bot .ai-chat-message-content h4,
.ai-chat-message-bot > p h4 {
    font-size: 0.94rem;
}

.ai-chat-message-bot .ai-chat-message-content ul,
.ai-chat-message-bot .ai-chat-message-content ol,
.ai-chat-message-bot > p ul,
.ai-chat-message-bot > p ol {
    margin: 6px 0;
    padding-left: 18px;
}

.ai-chat-message-bot .ai-chat-message-content li,
.ai-chat-message-bot > p li {
    margin-bottom: 2px;
    line-height: 1.5;
}

.md-table {
    width: 100%;
    margin: 8px 0;
    border-collapse: collapse;
    font-size: 0.82rem;
    border-radius: 8px;
    overflow: hidden;
}

.md-table th,
.md-table td {
    padding: 8px 10px;
    border: 1px solid var(--border-color);
}

.md-table th {
    background: rgba(168, 85, 247, 0.15);
    color: var(--text-primary);
    font-weight: 600;
}

.md-table td {
    background: var(--input-bg);
    color: var(--text-secondary);
}

.ai-chat-form {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 14px;
    border-top: 1px solid var(--border-color);
    background: var(--glass-bg);
}

.ai-chat-composer {
    position: relative;
    padding-top: 38px;
}

.ai-chat-quickstart {
    position: absolute;
    left: 12px;
    right: 12px;
    top: -16px;
    display: flex;
    align-items: center;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 4px 4px 10px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    touch-action: pan-x;
    cursor: grab;
}

.ai-chat-quickstart::-webkit-scrollbar {
    display: none;
}

.ai-chat-quickstart-btn {
    flex: 0 0 auto;
    white-space: nowrap;
    border: 1px solid var(--border-color);
    border-radius: 999px;
    background: color-mix(in srgb, var(--surface) 78%, black 22%);
    color: var(--text-primary);
    font: inherit;
    font-size: 0.8rem;
    line-height: 1;
    padding: 10px 14px;
    cursor: pointer;
    box-shadow: 0 8px 22px -18px var(--shadow-color);
    transition: transform 0.2s ease, border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
}

.ai-chat-quickstart-btn:hover,
.ai-chat-quickstart-btn:focus-visible {
    transform: translateY(-1px);
    border-color: var(--primary-light);
    background: color-mix(in srgb, var(--surface) 58%, var(--primary) 42%);
    box-shadow: 0 10px 26px -20px rgba(168, 85, 247, 0.62);
    outline: none;
}

.ai-chat-quickstart-btn:disabled {
    cursor: wait;
    opacity: 0.6;
}

.ai-chat-quickstart.is-pointer-down {
    cursor: grabbing;
}

[data-theme="light"] .ai-chat-quickstart-btn {
    background: #f3f6fd;
    border-color: rgba(48, 68, 97, 0.24);
    color: #1f2b40;
}

[data-theme="light"] .ai-chat-toggle {
    box-shadow:
        0 4px 24px rgba(0, 0, 0, 0.15),
        inset 0 1px 0 rgba(0, 0, 0, 0.1);
}

[data-theme="light"] .ai-chat-toggle:hover,
[data-theme="light"] .ai-chat-toggle:focus-visible {
    box-shadow:
        0 8px 32px rgba(0, 0, 0, 0.2),
        0 0 0 6px rgba(168, 85, 247, 0.12),
        inset 0 1px 0 rgba(0, 0, 0, 0.1);
}

[data-theme="light"] .ai-chat-toggle i {
    filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.15));
}

[data-theme="light"] .ai-chat-pulse {
    border-color: rgba(0, 0, 0, 0.15);
}

[data-theme="light"] .ai-chat-quickstart-btn:hover,
[data-theme="light"] .ai-chat-quickstart-btn:focus-visible {
    background: #e8eefc;
    border-color: rgba(113, 61, 222, 0.38);
    box-shadow: 0 10px 24px -18px rgba(89, 109, 152, 0.4);
}

.ai-chat-form textarea {
    flex: 1;
    min-height: 46px;
    max-height: 120px;
    resize: none;
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 12px 14px;
    color: var(--text-primary);
    background: var(--input-bg);
    font: inherit;
    line-height: 1.4;
    outline: none;
    transition:
        border-color 0.2s ease,
        box-shadow 0.2s ease;
}

.ai-chat-form textarea:focus {
    border-color: var(--primary-light);
    box-shadow: 0 0 0 3px rgba(168, 85, 247, 0.18);
}

.ai-chat-form button {
    width: 46px;
    height: 46px;
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    border: 0;
    border-radius: 14px;
    color: #fff;
    background: linear-gradient(
        135deg,
        var(--primary-light),
        var(--primary-dark)
    );
    cursor: pointer;
    transition:
        transform 0.2s ease,
        opacity 0.2s ease;
}

.ai-chat-form button:hover,
.ai-chat-form button:focus-visible {
    transform: translateY(-2px);
    outline: none;
}

.ai-chat-form button:disabled,
.ai-chat-form textarea:disabled {
    cursor: wait;
    opacity: 0.68;
}


@keyframes aiMessageIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes aiCursor {
    50% {
        opacity: 0;
    }
}


/* --- AI Chat Speech Bubble (Sprechblase) --- */
.ai-chat-bubble {
    position: absolute;
    bottom: 74px;
    right: 0;
    width: 290px;
    padding: 14px 34px 14px 14px;
    border: 1px solid var(--border-color);
    border-radius: 16px;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow:
        0 10px 30px var(--shadow-color),
        0 0 20px rgba(168, 85, 247, 0.08);
    z-index: 2300;
    opacity: 0;
    visibility: hidden;
    transform: translateY(12px) scale(0.95);
    transform-origin: bottom right;
    transition:
        opacity 0.3s ease,
        transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
        visibility 0.3s ease;
    cursor: pointer;
}

.ai-chat-bubble.is-visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

.ai-chat-bubble-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 20px;
    height: 20px;
    display: grid;
    place-items: center;
    border: 0;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.8rem;
    cursor: pointer;
    transition: color 0.2s ease, transform 0.2s ease;
}

.ai-chat-bubble-close:hover,
.ai-chat-bubble-close:focus-visible {
    color: var(--text-primary);
    transform: scale(1.15);
    outline: none;
}

.ai-chat-bubble-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.ai-chat-bubble-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-light), var(--primary-dark));
    color: #ffffff;
    display: grid;
    place-items: center;
    font-size: 0.95rem;
    flex-shrink: 0;
    box-shadow: 0 4px 12px rgba(168, 85, 247, 0.25);
}

.ai-chat-bubble-text {
    color: var(--text-primary);
    font-size: 0.82rem;
    line-height: 1.4;
    font-weight: 500;
}

.ai-chat-bubble-arrow {
    position: absolute;
    bottom: -6px;
    right: 23px;
    width: 10px;
    height: 10px;
    background: var(--surface);
    border-right: 1px solid var(--border-color);
    border-bottom: 1px solid var(--border-color);
    transform: rotate(45deg);
}

/* Light Theme overrides */
[data-theme="light"] .ai-chat-bubble {
    background: rgba(255, 255, 255, 0.92);
    border-color: rgba(48, 68, 97, 0.16);
    box-shadow:
        0 10px 30px rgba(30, 41, 59, 0.12),
        0 0 20px rgba(168, 85, 247, 0.04);
}

[data-theme="light"] .ai-chat-bubble-arrow {
    background: #ffffff;
    border-color: rgba(48, 68, 97, 0.16);
}

[data-theme="light"] .ai-chat-bubble-text {
    color: #1f2b40;
}

[data-theme="light"] .ai-chat-bubble-avatar {
    box-shadow: 0 4px 12px rgba(168, 85, 247, 0.15);
}

@media (max-width: 768px) {
    html.ai-chat-open,
    body.ai-chat-open {
        overflow: hidden;
        height: 100%;
    }

    .ai-chat-widget {
        inset: auto 0 0 0;
    }

    .ai-chat-toggle {
        position: fixed;
        right: max(16px, env(safe-area-inset-right));
        bottom: max(16px, env(safe-area-inset-bottom));
        width: 58px;
        height: 58px;
        border-radius: 50%;
    }

    .ai-chat-panel {
        position: fixed;
        inset: 0;
        width: 100vw;
        height: 100dvh;
        max-height: none;
        border-radius: 0;
        background: var(--surface);
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        box-shadow: none;
        border: 0;
        transform: translateY(100%);
        transform-origin: bottom center;
        overscroll-behavior: contain;
    }

    .ai-chat-messages {
        overscroll-behavior-y: contain;
    }

    .ai-chat-widget.is-open .ai-chat-panel {
        transform: translateY(0);
    }

    .ai-chat-header {
        padding: max(18px, env(safe-area-inset-top)) 18px 14px;
        background: var(--surface);
    }

    .ai-chat-messages {
        padding: 16px;
    }

    .ai-chat-message {
        max-width: 90%;
    }

    .ai-chat-form {
        padding: 12px 12px max(12px, env(safe-area-inset-bottom));
        background: var(--surface);
    }

    .ai-chat-composer {
        padding-top: 42px;
    }

    .ai-chat-quickstart {
        left: 10px;
        right: 10px;
        top: -14px;
        padding-bottom: 8px;
    }

    .ai-chat-quickstart-btn {
        font-size: 0.78rem;
        padding: 9px 13px;
    }

    .ai-chat-bubble {
        position: absolute;
        bottom: 74px;
        right: max(16px, env(safe-area-inset-right));
        width: calc(100vw - 32px);
        max-width: 320px;
        transform-origin: bottom right;
    }

    .ai-chat-bubble-arrow {
        right: 24px;
    }
}
