/* KI-Kundenservice-Chat - modernes Messenger-Design
 * Theming ueber --ddfchat-color (wird per JS aus --category-color abgeleitet)
 * Design-System: Spacing/Radius/Farben als CSS-Variablen auf #ddfchat-root
 */

#ddfchat-root {
    --ddfchat-color: #af4882;
    --chat-radius: 16px;
    --chat-radius-bubble: 18px;
    --chat-gap: 12px;
    --chat-gap-sm: 6px;
    --chat-bg: #ffffff;
    --chat-body-bg: #f4f6f9;
    --chat-bubble-bg: #ffffff;
    --chat-bubble-border: #e6e9ee;
    --chat-agent-bg: #eaf0f9;
    --chat-agent-border: #d5e0f0;
    --chat-text: #2b3038;
    --chat-meta: #9aa1ab;
    --chat-live: #2f3b4c;
    --chat-shadow: 0 12px 40px rgba(23, 28, 38, 0.22);
    font-family: inherit;
    font-size: 14.5px;
    line-height: 1.5;
    color: var(--chat-text);
    text-align: left; /* Site-CSS zentriert teils global - Widget immer linksbuendig */
}

#ddfchat-root *,
#ddfchat-root *::before,
#ddfchat-root *::after {
    box-sizing: border-box;
}

#ddfchat-root button {
    font-family: inherit;
}

/* ==================== FAB ==================== */

#ddfchat-fab {
    position: fixed;
    right: 20px;
    bottom: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: var(--ddfchat-color);
    color: #fff;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(23, 28, 38, 0.28);
    z-index: 24000;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.22s ease, opacity 0.22s ease, box-shadow 0.22s ease;
    animation: ddfchat-fab-in 0.35s ease backwards;
}

@keyframes ddfchat-fab-in {
    from { transform: scale(0.4); opacity: 0; }
    to   { transform: scale(1);   opacity: 1; }
}

#ddfchat-fab:hover {
    transform: scale(1.08);
    box-shadow: 0 8px 26px rgba(23, 28, 38, 0.34);
}

#ddfchat-fab:active {
    transform: scale(0.98);
}

#ddfchat-fab.ddfchat-fab-scrolled {
    transform: translateY(96px);
    opacity: 0;
    pointer-events: none;
}

#ddfchat-fab.ddfchat-fab-hidden {
    display: none;
}

/* Badge fuer ungelesene Agent-Nachrichten am FAB */
.ddfchat-fab-badge {
    position: absolute;
    top: -3px;
    right: -3px;
    min-width: 19px;
    height: 19px;
    border-radius: 999px;
    background: #e02020;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    border: 2px solid #fff;
    box-sizing: border-box;
    pointer-events: none;
}

/* ==================== Fenster ==================== */

#ddfchat-window {
    position: fixed;
    right: 20px;
    bottom: 20px;
    width: 384px;
    max-width: calc(100vw - 40px);
    height: 580px;
    max-height: calc(100vh - 56px);
    background: var(--chat-bg);
    border-radius: var(--chat-radius);
    box-shadow: var(--chat-shadow);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 24001;
    transform-origin: bottom right;
}

#ddfchat-window.ddfchat-open {
    display: flex;
    animation: ddfchat-window-in 0.24s cubic-bezier(0.2, 0.9, 0.3, 1.05) backwards;
}

#ddfchat-window.ddfchat-closing {
    animation: ddfchat-window-out 0.18s ease forwards;
}

@keyframes ddfchat-window-in {
    from { transform: scale(0.86) translateY(14px); opacity: 0; }
    to   { transform: scale(1) translateY(0);       opacity: 1; }
}

@keyframes ddfchat-window-out {
    from { transform: scale(1) translateY(0);       opacity: 1; }
    to   { transform: scale(0.88) translateY(12px); opacity: 0; }
}

/* ==================== Header ==================== */

.ddfchat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 14px;
    background-color: var(--ddfchat-color);
    background-image: linear-gradient(135deg, rgba(255, 255, 255, 0.12), rgba(0, 0, 0, 0.10));
    color: #fff;
    flex-shrink: 0;
    transition: background-color 0.3s ease;
}

#ddfchat-window.ddfchat--live .ddfchat-header {
    background-color: var(--chat-live);
}

#ddfchat-window.ddfchat--connecting .ddfchat-header {
    background-color: #5a6675;
}

.ddfchat-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    background: rgba(255, 255, 255, 0.9);
    border: 2px solid rgba(255, 255, 255, 0.35);
    flex-shrink: 0;
}

/* Initialen-Badge im Header (Fallback wenn Zammad kein Avatar-Bild liefert) */
.ddfchat-avatar--initials {
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
    font-weight: 600;
    font-size: 13px;
    letter-spacing: 0.5px;
}

.ddfchat-avatar--initials svg {
    width: 18px;
    height: 18px;
}

.ddfchat-headtext {
    flex: 1;
    min-width: 0;
}

.ddfchat-title {
    font-weight: 600;
    font-size: 15px;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ddfchat-status {
    font-size: 12px;
    line-height: 1.3;
    opacity: 0.85;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: 5px;
}

.ddfchat-status::before {
    content: "";
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #7ce38b;
    flex-shrink: 0;
}

#ddfchat-window.ddfchat--connecting .ddfchat-status::before {
    background: #ffd479;
}

.ddfchat-btn {
    border: 1px solid rgba(255, 255, 255, 0.55);
    background: rgba(255, 255, 255, 0.10);
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 999px;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s ease;
}

.ddfchat-btn:hover {
    background: rgba(255, 255, 255, 0.28);
}

/* dezente Variante (z.B. "Nachricht hinterlassen" wenn kein Agent online) */
.ddfchat-btn--subtle {
    border-color: rgba(255, 255, 255, 0.35);
    background: none;
    font-weight: 400;
}

.ddfchat-close {
    border: none;
    background: none;
    color: #fff;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    opacity: 0.9;
    transition: background 0.15s ease;
    flex-shrink: 0;
}

.ddfchat-close:hover {
    background: rgba(255, 255, 255, 0.18);
    opacity: 1;
}

/* ==================== Nachrichtenbereich ==================== */

.ddfchat-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px 14px 10px 14px;
    background: var(--chat-body-bg);
    scrollbar-width: thin;
    scrollbar-color: #c9cfd8 transparent;
}

.ddfchat-body::-webkit-scrollbar {
    width: 6px;
}

.ddfchat-body::-webkit-scrollbar-track {
    background: transparent;
}

.ddfchat-body::-webkit-scrollbar-thumb {
    background: #c9cfd8;
    border-radius: 999px;
}

.ddfchat-body::-webkit-scrollbar-thumb:hover {
    background: #aeb6c2;
}

/* ==================== Nachrichten-Zeilen ==================== */

.ddfchat-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    margin-bottom: var(--chat-gap);
    animation: ddfchat-msg-in 0.18s ease backwards;
}

@keyframes ddfchat-msg-in {
    from { transform: translateY(6px); opacity: 0; }
    to   { transform: translateY(0);   opacity: 1; }
}

/* aufeinanderfolgende Nachrichten desselben Absenders ruecken zusammen */
.ddfchat-row.ddfchat-row--grouped {
    margin-bottom: var(--chat-gap-sm);
}

.ddfchat-row--user {
    justify-content: flex-end;
}

.ddfchat-msgavatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    flex-shrink: 0;
    overflow: hidden;
    background: var(--ddfchat-color);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px; /* auf Bubble-Hoehe (Meta-Zeile ausgleichen) */
}

.ddfchat-msgavatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.ddfchat-msgavatar svg {
    width: 16px;
    height: 16px;
}

.ddfchat-row--agent .ddfchat-msgavatar {
    background: var(--chat-live);
}

/* Initialen-Badge an Agent-Nachrichten (Fallback ohne Avatar-Bild) -
   Selektor bewusst spezifischer als die .ddfchat-row--agent-Hintergrundregel */
.ddfchat-row .ddfchat-msgavatar--initials {
    background: var(--ddfchat-color);
    color: #fff;
    font-weight: 600;
    font-size: 11.5px;
    letter-spacing: 0.4px;
}

/* im Gruppen-Verbund traegt nur die letzte Zeile den Avatar */
.ddfchat-row--grouped .ddfchat-msgavatar {
    visibility: hidden;
    margin-bottom: 0;
}

.ddfchat-msgcol {
    display: flex;
    flex-direction: column;
    max-width: 80%;
    min-width: 0;
}

.ddfchat-row--user .ddfchat-msgcol {
    align-items: flex-end;
}

/* ==================== Bubbles ==================== */

.ddfchat-bubble {
    padding: 9px 13px;
    border-radius: var(--chat-radius-bubble);
    word-wrap: break-word;
    overflow-wrap: break-word;
    font-size: 14.5px;
    line-height: 1.5;
}

.ddfchat-row--user .ddfchat-bubble {
    background: var(--ddfchat-color);
    color: #fff;
    border-bottom-right-radius: 5px;
}

.ddfchat-row--bot .ddfchat-bubble {
    background: var(--chat-bubble-bg);
    color: var(--chat-text);
    border: 1px solid var(--chat-bubble-border);
    border-bottom-left-radius: 5px;
    box-shadow: 0 1px 2px rgba(23, 28, 38, 0.05);
}

.ddfchat-row--agent .ddfchat-bubble {
    background: var(--chat-agent-bg);
    color: #24344d;
    border: 1px solid var(--chat-agent-border);
    border-bottom-left-radius: 5px;
}

/* gruppierte Zwischen-Bubbles: durchgehende Kante */
.ddfchat-row--grouped.ddfchat-row--bot .ddfchat-bubble,
.ddfchat-row--grouped.ddfchat-row--agent .ddfchat-bubble {
    border-bottom-left-radius: var(--chat-radius-bubble);
}

.ddfchat-row--grouped.ddfchat-row--user .ddfchat-bubble {
    border-bottom-right-radius: var(--chat-radius-bubble);
}

/* Meta-Zeile (Zeit) - nur an der letzten Nachricht einer Gruppe */
.ddfchat-meta {
    font-size: 11px;
    color: var(--chat-meta);
    margin: 3px 6px 0 6px;
}

.ddfchat-row--grouped .ddfchat-meta {
    display: none;
}

/* ==================== System-Nachrichten ==================== */

.ddfchat-row--system {
    justify-content: center;
    margin: 10px 0;
}

.ddfchat-row--system .ddfchat-msgcol {
    max-width: 95%;
    align-items: center;
}

.ddfchat-row--system .ddfchat-bubble {
    background: none;
    border: none;
    color: var(--chat-meta);
    font-size: 12px;
    text-align: center;
    padding: 0 8px;
    box-shadow: none;
}

.ddfchat-row--system .ddfchat-meta {
    display: none;
}

/* ==================== Links & Code in Bubbles ==================== */

.ddfchat-bubble a {
    color: var(--ddfchat-color);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.ddfchat-row--user .ddfchat-bubble a {
    color: #fff;
}

.ddfchat-bubble code {
    background: #eef0f3;
    border-radius: 4px;
    padding: 1px 5px;
    font-size: 13px;
}

.ddfchat-row--user .ddfchat-bubble code {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

/* ==================== Typing-Indicator ==================== */

.ddfchat-typing {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 2px;
}

.ddfchat-typing i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #b6bdc7;
    animation: ddfchat-typing-bounce 1.2s infinite ease-in-out;
}

.ddfchat-typing i:nth-child(2) { animation-delay: 0.15s; }
.ddfchat-typing i:nth-child(3) { animation-delay: 0.30s; }

@keyframes ddfchat-typing-bounce {
    0%, 60%, 100% { transform: translateY(0);    opacity: 0.5; }
    30%           { transform: translateY(-4px); opacity: 1; }
}

/* ==================== Ticket-Formular ==================== */

.ddfchat-row--form {
    display: block;
}

.ddfchat-ticketform {
    background: var(--chat-bubble-bg);
    border: 1px solid var(--chat-bubble-border);
    border-radius: 12px;
    padding: 12px;
    box-shadow: 0 1px 2px rgba(23, 28, 38, 0.05);
}

.ddfchat-ticketform__title {
    font-weight: 600;
    font-size: 13.5px;
    margin-bottom: 8px;
}

.ddfchat-ticketform input,
.ddfchat-ticketform textarea {
    width: 100%;
    border: 1.5px solid #d6dae0;
    border-radius: 8px;
    padding: 7px 10px;
    font-family: inherit;
    font-size: 13.5px;
    color: var(--chat-text);
    background: #f8f9fb;
    outline: none;
    margin-bottom: 7px;
    resize: vertical;
}

.ddfchat-ticketform input:focus,
.ddfchat-ticketform textarea:focus {
    border-color: var(--ddfchat-color);
    background: #fff;
}

.ddfchat-tf-error {
    color: #c0392b;
    font-size: 12px;
    margin-bottom: 7px;
}

.ddfchat-tf-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.ddfchat-tf-cancel {
    border: none;
    background: none;
    color: var(--chat-meta);
    font-size: 12.5px;
    cursor: pointer;
    padding: 7px 8px;
}

.ddfchat-tf-cancel:hover {
    color: var(--chat-text);
}

.ddfchat-tf-send {
    border: none;
    background: var(--ddfchat-color);
    color: #fff;
    font-size: 12.5px;
    font-weight: 600;
    padding: 8px 14px;
    border-radius: 999px;
    cursor: pointer;
}

.ddfchat-tf-send:hover:not(:disabled) {
    filter: brightness(1.08);
}

.ddfchat-tf-send:disabled {
    opacity: 0.55;
    cursor: default;
}

/* ==================== Quick Replies ==================== */

.ddfchat-quick {
    padding: 4px 14px 8px 14px;
    background: var(--chat-body-bg);
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    flex-shrink: 0;
}

.ddfchat-quickbtn {
    border: 1.5px solid var(--ddfchat-color);
    color: var(--ddfchat-color);
    background: var(--chat-bg);
    border-radius: 999px;
    font-size: 12.5px;
    font-weight: 500;
    padding: 6px 13px;
    cursor: pointer;
    text-align: left;
    transition: background 0.15s ease, color 0.15s ease;
}

.ddfchat-quickbtn:hover {
    background: var(--ddfchat-color);
    color: #fff;
}

/* ==================== Eingabezeile ==================== */

.ddfchat-inputrow {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    background: var(--chat-bg);
    border-top: 1px solid var(--chat-bubble-border);
    flex-shrink: 0;
}

.ddfchat-input {
    flex: 1;
    border: 1.5px solid #d6dae0;
    border-radius: 22px;
    padding: 9px 15px;
    font-family: inherit;
    font-size: 14.5px;
    line-height: 1.4;
    color: var(--chat-text);
    background: #f8f9fb;
    resize: none;
    outline: none;
    max-height: 96px;
    min-height: 20px;
    transition: border-color 0.15s ease, background 0.15s ease;
}

.ddfchat-input:focus {
    border-color: var(--ddfchat-color);
    background: #fff;
}

.ddfchat-input::placeholder {
    color: #a6adb7;
}

.ddfchat-send {
    border: none;
    background: var(--ddfchat-color);
    color: #fff;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.15s ease, opacity 0.15s ease, box-shadow 0.15s ease;
}

.ddfchat-send:hover:not(:disabled) {
    transform: scale(1.07);
    box-shadow: 0 3px 10px rgba(23, 28, 38, 0.22);
}

.ddfchat-send:disabled {
    opacity: 0.45;
    cursor: default;
}

.ddfchat-send svg {
    margin-left: -2px;
}

/* ==================== Fusszeile ==================== */

.ddfchat-foot {
    font-size: 11px;
    color: var(--chat-meta);
    text-align: center;
    padding: 0 8px 8px 8px;
    background: var(--chat-bg);
    flex-shrink: 0;
}

.ddfchat-foot a {
    color: var(--chat-meta);
    text-decoration: underline;
}

/* ==================== Mobile ==================== */

@media only screen and (max-width: 480px) {
    #ddfchat-window {
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        max-width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    #ddfchat-fab {
        right: 14px;
        bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    }
}
