.ai-chat {
    box-sizing: border-box;
    width: min(100%, 1040px);
    max-width: 100%;
    margin: 0 auto;
    padding: 1rem;
    overflow-x: hidden;
}

.ai-chat *,
.ai-chat *::before,
.ai-chat *::after {
    box-sizing: border-box;
}

.leben-page-chat #body-wrapper.section {
    padding-top: 1.5rem;
    padding-bottom: 1.25rem;
}

.leben-page-chat #footer.section {
    padding-top: 1rem;
    padding-bottom: 1rem;
}

.ai-chat__header {
    margin-bottom: 1rem;
    min-width: 0;
}

.ai-chat h1 {
    margin-top: 0;
    margin-bottom: 0.75rem;
}

.ai-chat__intro {
    color: #555;
    margin-bottom: 0;
    overflow-wrap: anywhere;
}

.ai-chat__avatar-label {
    display: block;
    color: #155d27;
    font-weight: 700;
    line-height: 1.15;
    text-align: center;
}

.ai-chat__reset {
    border-color: #155d27;
    color: #155d27;
    background: #fff;
}

.ai-chat__reset[disabled] {
    opacity: 0.6;
    cursor: not-allowed;
}

.ai-chat__messages {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    min-height: 4rem;
    overflow: visible;
    padding: 1rem;
    border: 1px solid #155d27;
    border-radius: 8px;
    background: #fafafa;
}

.ai-chat__messages:empty::before {
    content: attr(data-empty);
    color: #68758a;
    line-height: 1.45;
}

.ai-chat__messages:has(.ai-chat__message--assistant:not(.ai-chat__message--loading)) {
    min-height: 16rem;
}

.ai-chat__message {
    max-width: 85%;
    padding: 0.6rem 0.9rem;
    border-radius: 12px;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    line-height: 1.45;
}

@media (min-width: 960px) {
    .ai-chat {
        padding: 1.5rem 2rem;
    }

    .ai-chat__messages {
        min-height: 4rem;
    }

    .ai-chat__messages:has(.ai-chat__message--assistant:not(.ai-chat__message--loading)) {
        min-height: 22rem;
    }

    .ai-chat__message--assistant {
        max-width: 92%;
    }

    .ai-chat__message--user {
        max-width: 78%;
    }
}

.ai-chat__message--user {
    align-self: flex-end;
    background: #155d27;
    color: #fff;
    border-bottom-right-radius: 4px;
}

.ai-chat__message--assistant {
    align-self: flex-start;
    background: #fff;
    border: 1px solid #7da66e;
    border-bottom-left-radius: 4px;
    white-space: normal;
}

.ai-chat__message--assistant strong {
    font-weight: 700;
}

.leben-page-chat .ai-chat .ai-chat__message--assistant a,
.leben-page-chat .ai-chat .ai-chat__message--assistant a:visited {
    color: #155d27 !important;
    text-decoration: underline;
    text-decoration-thickness: 0.08em;
    text-underline-offset: 0.16em;
    word-break: break-all;
}

.leben-page-chat .ai-chat .ai-chat__message--assistant a:hover,
.leben-page-chat .ai-chat .ai-chat__message--assistant a:focus {
    color: #0b4f24 !important;
    text-decoration-thickness: 0.12em;
}

.ai-chat__md-heading {
    margin: 0.75rem 0 0.35rem;
    font-weight: 700;
}

.ai-chat__md-heading:first-child {
    margin-top: 0;
}

.ai-chat__md-heading--1,
.ai-chat__md-heading--2,
.ai-chat__md-heading--3 {
    font-size: 1.05em;
}

.ai-chat__md-paragraph {
    margin: 0.35rem 0;
}

.ai-chat__md-list-item {
    margin: 0.2rem 0;
}

.ai-chat__md-rule {
    border: 0;
    border-top: 1px solid #7da66e;
    margin: 0.8rem 0;
}

.ai-chat__message--loading {
    color: #155d27;
    font-weight: 700;
    white-space: pre-wrap;
}

.ai-chat__source {
    margin-top: 0.55rem;
}

.ai-chat__source-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.08rem 0.45rem;
    border: 1px solid #b8b8b8;
    border-radius: 999px;
    background: #f1f1f1;
    color: #555;
    font: inherit;
    font-size: 0.72rem;
    line-height: 1.35;
    cursor: pointer;
}

.ai-chat__source-button:hover,
.ai-chat__source-button:focus-visible {
    border-color: #8a8a8a;
    background: #e6e6e6;
    color: #333;
}

.ai-chat__source-json {
    max-height: 18rem;
    margin: 0.45rem 0 0;
    padding: 0.65rem;
    overflow: auto;
    border: 1px solid #c9c9c9;
    border-radius: 6px;
    background: #f7f7f7;
    color: #2f2f2f;
    font-size: 0.72rem;
    line-height: 1.4;
    white-space: pre-wrap;
}

.ai-chat__error {
    margin-top: 0.75rem;
    padding: 0.6rem 0.9rem;
    border: 1px solid #d93025;
    border-radius: 8px;
    background: #fdecea;
    color: #a50e0e;
}

.ai-chat__composer {
    --ai-chat-desktop-avatar-size: 9.5rem;
    display: grid;
    grid-template-columns: 10rem minmax(0, 1fr) 8.5rem;
    grid-template-areas:
        "prompt input actions"
        "avatar input actions"
        "status status status";
    gap: 1rem;
    align-items: center;
    margin-top: 0.75rem;
}

.ai-chat__avatar-prompt {
    display: grid;
    grid-area: prompt;
    gap: 0.35rem;
    justify-items: center;
    align-self: end;
}

.ai-chat__avatar-wrap {
    grid-area: avatar;
    position: relative;
    width: var(--ai-chat-desktop-avatar-size);
    aspect-ratio: 1;
    justify-self: center;
    align-self: start;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(220, 252, 231, 0.78), rgba(240, 253, 244, 0.2) 68%, transparent 72%);
}

.ai-chat__avatar {
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
    transform-origin: center 68%;
    transition: filter 180ms ease;
    --ai-chat-avatar-image: url("avatar/avatar-elf-idle-transparent.png");
}

.ai-chat__avatar-base {
    position: absolute;
    inset: -10%;
    background: var(--ai-chat-avatar-image) center / contain no-repeat;
    filter: drop-shadow(0 0.75rem 1.25rem rgba(47, 107, 63, 0.16));
}

.ai-chat__avatar-ring {
    position: absolute;
    inset: 2%;
    z-index: 0;
    border: 0.32rem solid transparent;
    border-radius: 50%;
    pointer-events: none;
    transition: background 180ms ease, border-color 180ms ease;
}

.ai-chat__avatar-mouth {
    position: absolute;
    top: 67%;
    left: 52%;
    z-index: 2;
    width: 22%;
    height: 8%;
    transform: translateX(-50%);
    opacity: 0;
    pointer-events: none;
}

.ai-chat__avatar-mouth img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: fill;
}

.ai-chat__avatar.idle {
    --ai-chat-avatar-image: url("avatar/avatar-elf-idle-transparent.png");
    animation: aiChatAvatarBreathe 3.1s ease-in-out infinite;
}

.ai-chat__avatar.listening {
    --ai-chat-avatar-image: url("avatar/avatar-elf-listening-transparent.png");
    filter: drop-shadow(0 0 0.75rem rgba(34, 197, 94, 0.28));
}

.ai-chat__avatar.listening .ai-chat__avatar-ring {
    background: rgba(187, 247, 208, 0.42);
    border-color: rgba(34, 197, 94, 0.84);
    box-shadow: 0 0 0 0.38rem rgba(187, 247, 208, 0.2);
    animation: aiChatAvatarListenPulse 1.25s ease-out infinite;
}

.ai-chat__avatar.thinking {
    --ai-chat-avatar-image: url("avatar/avatar-elf-thinking-transparent.png");
    animation: aiChatAvatarThinking 1.25s ease-in-out infinite;
}

.ai-chat__avatar.speaking {
    --ai-chat-avatar-image: url("avatar/avatar-elf-speaking-base-transparent.png");
    animation: aiChatAvatarSpeaking 520ms ease-in-out infinite;
    filter: drop-shadow(0 0 0.8rem rgba(124, 58, 237, 0.2));
}

.ai-chat__avatar.speaking .ai-chat__avatar-mouth {
    opacity: 1;
}

.ai-chat__avatar.speaking.speech-paused .ai-chat__avatar-mouth img {
    opacity: 0;
}

@keyframes aiChatAvatarBreathe {
    0%,
    100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.035) translateY(-0.2rem);
    }
}

@keyframes aiChatAvatarListenPulse {
    0% {
        opacity: 0.95;
        transform: scale(0.9);
    }
    55% {
        opacity: 0.58;
    }
    100% {
        opacity: 0.2;
        transform: scale(1.08);
    }
}

@keyframes aiChatAvatarThinking {
    0%,
    100% {
        transform: rotate(-2deg);
    }
    50% {
        transform: rotate(3deg) translateY(-0.15rem);
    }
}

@keyframes aiChatAvatarSpeaking {
    0%,
    100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-0.15rem);
    }
}

.ai-chat__form {
    display: contents;
    gap: 0;
    margin-top: 0;
    align-items: stretch;
    width: 100%;
    max-width: 100%;
}

.ai-chat__input {
    grid-area: input;
    display: block;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    resize: vertical;
    overflow-x: hidden;
    padding: 0.6rem;
    border: 1px solid #155d27;
    border-radius: 8px;
    font-family: inherit;
    font-size: 16px;
    line-height: 1.4;
    min-height: var(--ai-chat-desktop-avatar-size);
}

.ai-chat__actions {
    grid-area: actions;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: repeat(4, 1fr);
    gap: 0.45rem;
    align-items: stretch;
    height: var(--ai-chat-desktop-avatar-size);
}

.ai-chat__actions button {
    display: inline-flex;
    gap: 0.35rem;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 0;
    padding: 0 0.6rem;
    border-radius: 999px;
    border-color: #155d27;
    color: #155d27;
    white-space: nowrap;
}

.ai-chat__actions button::before {
    font-size: 1.1rem;
    font-weight: 700;
    line-height: 1;
}

.ai-chat__reset::before {
    content: "\21BB";
}

.ai-chat__speak-latest::before {
    content: "\1F50A";
}

/* Langer Druck liest die ganze Antwort: kein Kontextmenue, keine Textauswahl. */
.ai-chat__speak-latest {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

/* Haltedauer erreicht: Loslassen liest die ganze Antwort. */
.ai-chat__speak-latest--full {
    border-color: #0b4f24;
    background: #e7f3ea;
    box-shadow: 0 0 0 3px rgba(21, 93, 39, 0.18);
}

.ai-chat__mic::before {
    content: "\1F399";
}

.ai-chat__send::before {
    content: "\27A4";
}

.ai-chat__mic {
    border-color: #155d27;
    color: #155d27;
    background: #fff;
}

.ai-chat__mic--recording {
    border-color: #d93025;
    color: #a50e0e;
    background: #fdecea;
}

.ai-chat__mic--busy {
    opacity: 0.75;
}

.ai-chat__send {
    font-weight: 700;
}

.ai-chat__input:focus,
.ai-chat__actions button:focus-visible {
    border-color: #0b4f24;
    outline: 2px solid rgba(21, 93, 39, 0.24);
    outline-offset: 2px;
}

.ai-chat__speak-latest[aria-pressed="true"],
.ai-chat__send--ready,
.ai-chat__mic--hint:not(.ai-chat__mic--recording):not(.ai-chat__mic--busy) {
    animation: aiChatActionPulse 1.15s ease-in-out infinite;
}

@keyframes aiChatActionPulse {
    0%,
    100% {
        border-color: #155d27;
        box-shadow: 0 0 0 0 rgba(21, 93, 39, 0);
    }
    50% {
        border-color: #0b4f24;
        box-shadow: 0 0 0 0.22rem rgba(21, 93, 39, 0.18);
    }
}

.ai-chat__send[disabled] {
    opacity: 0.6;
    cursor: not-allowed;
}

.ai-chat__hint {
    margin-top: 0.5rem;
    font-size: 0.8rem;
    color: #777;
}

/* --- Datenschutzhinweise -------------------------------------------------
   Der Startdialog liegt ueber der ganzen Seite: bis "Chat starten" geklickt
   ist, geht nichts an einen externen Dienst. Der Mikrofonhinweis benutzt
   dieselben Bausteine. */

.ai-chat__intro-link {
    white-space: nowrap;
}

/* Eigene Zeile unter dem TTS-Haken: in der Intro-Zeile selbst gilt
   white-space: nowrap, dort wuerde der Satz die Breite sprengen. */
.ai-chat__mode-note {
    margin: 0.25rem 0 0;
    color: #777;
    font-size: 0.78rem;
    line-height: 1.35;
}

.ai-chat__gate {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: rgba(12, 40, 20, 0.55);
    overflow-y: auto;
}

.ai-chat__gate[hidden] {
    display: none;
}

.ai-chat__gate-box {
    width: min(100%, 32rem);
    margin: auto;
    padding: 1.35rem 1.4rem 1.2rem;
    border: 1px solid #d3e6d6;
    border-radius: 0.9rem;
    background: #fff;
    box-shadow: 0 1.2rem 2.4rem rgba(0, 0, 0, 0.22);
}

.ai-chat__gate-title {
    margin: 0 0 0.6rem;
    color: #155d27;
    font-size: 1.35rem;
    line-height: 1.2;
}

.ai-chat__gate-text {
    margin: 0 0 1.1rem;
    color: #333;
    font-size: 0.98rem;
    line-height: 1.45;
}

.ai-chat__gate-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.55rem;
    align-items: stretch;
    justify-content: flex-end;
}

.ai-chat__gate-actions .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 2.4rem;
    padding: 0.35rem 1rem;
    border: 1px solid #155d27;
    border-radius: 999px;
    font-size: 0.95rem;
    line-height: 1.2;
    text-decoration: none;
    white-space: nowrap;
}

.ai-chat__gate-link,
.ai-chat__gate-cancel {
    color: #155d27;
    background: #fff;
}

.ai-chat__gate-start {
    color: #fff;
    background: #155d27;
    font-weight: 700;
}

.ai-chat__gate-actions .button:focus-visible {
    outline: 2px solid rgba(21, 93, 39, 0.45);
    outline-offset: 2px;
}

@media (max-width: 640px) {
    .ai-chat__gate {
        padding: 0.75rem;
        /* Auf kleinen Geraeten ist der Dialog oben besser erreichbar als
           mittig - die Schaltflaechen liegen dann im Daumenbereich. */
        align-items: flex-start;
    }

    .ai-chat__gate-box {
        margin-top: 3.5rem;
        padding: 1.1rem 1rem 1rem;
    }

    .ai-chat__gate-actions {
        justify-content: stretch;
    }

    .ai-chat__gate-actions .button {
        flex: 1 1 9rem;
    }
}

.ai-chat__speech-status {
    grid-area: status;
    min-height: 1.2em;
    color: #666;
    font-size: 0.82rem;
}

@media (max-width: 640px) {
    .leben-page-chat #body-wrapper.section {
        padding-top: 0.45rem;
        padding-bottom: 0.35rem;
    }

    .leben-page-chat #footer.section {
        padding-top: 0.75rem;
        padding-bottom: 0.75rem;
    }

    .leben-page-chat #footer p {
        margin: 0.25rem 0;
    }

    .ai-chat {
        display: flex;
        flex-direction: column;
        gap: 0.55rem;
        min-height: calc(100vh - 11.5rem);
        padding: 0.45rem 0;
        width: 100%;
    }

    .ai-chat h1 {
        margin-bottom: 0.2rem;
        font-size: clamp(1.95rem, 8.2vw, 2.55rem);
        line-height: 1.05;
        white-space: nowrap;
    }

    .ai-chat__header {
        margin-bottom: 0.2rem;
    }

    .ai-chat__intro {
        margin-bottom: 0;
        font-size: 0.82rem;
        line-height: 1.35;
    }

    .ai-chat__messages {
        flex: 0 1 auto;
        min-height: 3.9rem;
        max-height: none;
        padding: 0.55rem;
    }

    .ai-chat__messages:has(.ai-chat__message--assistant:not(.ai-chat__message--loading)) {
        flex: 1 1 auto;
        min-height: 10rem;
    }

    .ai-chat__message {
        max-width: none;
        width: 100%;
    }

    .ai-chat__composer {
        --ai-chat-mobile-avatar-size: clamp(7.8rem, 32vw, 9.8rem);
        position: sticky;
        bottom: 0;
        z-index: 2;
        grid-template-columns: minmax(5rem, 1fr) var(--ai-chat-mobile-avatar-size) minmax(5.8rem, 1fr);
        grid-template-areas:
            "prompt avatar actions"
            "input input input"
            "status status status";
        gap: 0.45rem 0.55rem;
        align-items: center;
        margin-top: 0;
        padding-top: 0.35rem;
        background: linear-gradient(to bottom, rgba(249, 255, 246, 0.86), #f9fff6 28%);
    }

    .ai-chat__avatar-prompt {
        display: grid;
        grid-area: prompt;
        gap: 0.35rem;
        align-self: end;
        align-content: end;
        justify-items: end;
    }

    .ai-chat__avatar-label {
        display: block;
        color: #2f6b3f;
        font-size: clamp(0.95rem, 4.3vw, 1.25rem);
        font-weight: 700;
        line-height: 1.15;
        text-align: right;
    }

    .ai-chat__avatar-wrap {
        grid-area: avatar;
        width: var(--ai-chat-mobile-avatar-size);
        justify-self: center;
    }

    .ai-chat__form {
        display: contents;
    }

    .ai-chat__input {
        grid-area: input;
        width: 100%;
        min-height: 3.9rem;
    }

    .ai-chat__actions {
        grid-area: actions;
        display: grid;
        grid-template-columns: 1fr;
        grid-template-rows: repeat(4, 1fr);
        gap: clamp(0.22rem, 1vw, 0.34rem);
        height: var(--ai-chat-mobile-avatar-size);
        justify-content: end;
        align-items: stretch;
    }

    .ai-chat__actions button {
        display: inline-flex;
        gap: 0.28rem;
        align-items: center;
        justify-content: center;
        width: 100%;
        min-width: 0;
        height: 100%;
        min-height: 0;
        padding: 0 0.42rem;
        border-radius: 999px;
        color: #155d27;
        font-size: clamp(0.62rem, 2.45vw, 0.8rem);
        line-height: 1;
        white-space: nowrap;
    }

    .ai-chat__actions button::before {
        display: inline-block;
        color: #155d27;
        font-size: 1.15rem;
        font-weight: 700;
        line-height: 1;
    }

    .ai-chat__reset::before {
        content: "↺";
    }

    .ai-chat__mic::before {
        content: "◉";
    }

    .ai-chat__send::before {
        content: "➤";
    }

    .ai-chat__reset::before {
        content: "\21BB";
    }

    .ai-chat__speak-latest::before {
        content: "\1F50A";
    }

    .ai-chat__mic::before {
        content: "\1F399";
    }

    .ai-chat__send::before {
        content: "\27A4";
    }

    .ai-chat__send {
        align-self: center;
    }

    .ai-chat__hint {
        margin-bottom: 0;
    }

    .ai-chat__speech-status {
        grid-area: status;
        min-height: 1rem;
        font-size: 0.72rem;
        text-align: center;
    }
}

.visuallyhidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

/* Details der Antwort: aufklappbar wie die Quelle darunter. Zugeklappt bleibt
   die Zusammenfassung stehen - das ist die Antwort; die Fundstellen sind der
   Beleg dazu und werden geholt, wer ihn braucht. */
.ai-chat__details {
    margin: 0.5rem 0 0;
}

.ai-chat__details-summary {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    padding: 0.2rem 0.7rem;
    border: 1px solid #b8b8b8;
    border-radius: 999px;
    background: #f1f1f1;
    color: #555;
    font: inherit;
    font-size: 0.78rem;
    cursor: pointer;
    list-style: none;          /* eigenes Zeichen statt Systemdreieck */
    touch-action: manipulation;
}

.ai-chat__details-summary::-webkit-details-marker { display: none; }

.ai-chat__details-summary::after {
    content: "\25BE";
    transition: transform 0.15s ease;
}

.ai-chat__details[open] > .ai-chat__details-summary::after {
    transform: rotate(180deg);
}

.ai-chat__details[open] > .ai-chat__details-summary {
    margin-bottom: 0.45rem;
}

.ai-chat__details-summary:hover,
.ai-chat__details-summary:focus-visible {
    border-color: #8a8a8a;
    background: #e6e6e6;
    color: #333;
}

/* Die erste Linkzeile steht ausserhalb des Aufklappers und ist damit der
   sichtbare Weg von der Antwort zur Quelle - etwas Luft davor genuegt. */
.ai-chat__erster-link {
    margin-top: 0.5rem;
}
