/**
 * KR Chat Widget – Frontend Styles
 * Kunze & Ritter & Schwarzwald IT
 */

:root {
    --kr-chat-primary: #E30613;
    --kr-chat-accent: #B8050F;
    --kr-chat-primary-contrast: #FFFFFF;
    --kr-chat-bg: #FFFFFF;
    --kr-chat-surface: #F8FAFC;
    --kr-chat-surface-hover: #F1F5F9;
    --kr-chat-text: #0F172A;
    --kr-chat-muted: #64748B;
    --kr-chat-border: #E2E8F0;
    --kr-chat-radius: 16px;
    --kr-chat-radius-sm: 8px;
    --kr-chat-shadow: 0 12px 32px -4px rgba(15, 23, 42, 0.18), 0 4px 12px -2px rgba(15, 23, 42, 0.08);
    --kr-chat-z-index: 999990;
    --kr-chat-font: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* ── Container ────────────────────────────────────────────── */
.kr-chat-root {
    position: fixed;
    bottom: 24px;
    z-index: var(--kr-chat-z-index);
    font-family: var(--kr-chat-font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--kr-chat-text);
    box-sizing: border-box;
}

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

.kr-chat-pos-bottom-right {
    right: 24px;
}

.kr-chat-pos-bottom-left {
    left: 24px;
}

/* ── Floating Action Button (Trigger) ──────────────────────── */
.kr-chat-fab {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background-color: var(--kr-chat-primary);
    color: var(--kr-chat-primary-contrast);
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), background-color 0.2s ease, box-shadow 0.2s ease;
    outline: none;
    position: relative;
    padding: 0;
}

.kr-chat-fab:hover {
    transform: scale(1.06);
    background-color: var(--kr-chat-accent);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.kr-chat-fab:focus-visible {
    box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--kr-chat-primary);
}

.kr-chat-fab svg {
    width: 28px;
    height: 28px;
    fill: currentColor;
    transition: transform 0.25s ease;
}

.kr-chat-fab.is-open .kr-chat-icon-bubble {
    display: none;
}

.kr-chat-fab:not(.is-open) .kr-chat-icon-close {
    display: none;
}

.kr-chat-fab-badge {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background-color: #22C55E;
    border: 2px solid #FFFFFF;
}

/* ── Chat Window ───────────────────────────────────────────── */
.kr-chat-window {
    position: absolute;
    bottom: 74px;
    width: 390px;
    max-width: calc(100vw - 32px);
    height: 620px;
    max-height: calc(100dvh - 110px);
    background-color: var(--kr-chat-bg);
    border-radius: var(--kr-chat-radius);
    box-shadow: var(--kr-chat-shadow);
    border: 1px solid var(--kr-chat-border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(16px) scale(0.96);
    transform-origin: bottom right;
    transition: opacity 0.22s ease, transform 0.22s cubic-bezier(0.16, 1, 0.3, 1), visibility 0.22s;
}

.kr-chat-pos-bottom-left .kr-chat-window {
    transform-origin: bottom left;
    left: 0;
}

.kr-chat-pos-bottom-right .kr-chat-window {
    right: 0;
}

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

/* ── Header ────────────────────────────────────────────────── */
.kr-chat-header {
    background-color: var(--kr-chat-primary);
    color: var(--kr-chat-primary-contrast);
    padding: 16px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.kr-chat-header-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.kr-chat-header-title {
    font-weight: 700;
    font-size: 15px;
    letter-spacing: -0.01em;
    margin: 0;
    line-height: 1.2;
}

.kr-chat-header-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    opacity: 0.92;
}

.kr-chat-status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background-color: #4ADE80;
    display: inline-block;
}

.kr-chat-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.kr-chat-btn-header {
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: #FFFFFF;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.15s ease;
    padding: 0;
}

.kr-chat-btn-header:hover {
    background: rgba(255, 255, 255, 0.28);
}

.kr-chat-btn-header svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}

/* ── Body (Scrollbereich) ──────────────────────────────────── */
.kr-chat-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background-color: var(--kr-chat-surface);
    overscroll-behavior: contain;
}

/* ── Nachrichtenblasen ─────────────────────────────────────── */
.kr-chat-msg {
    display: flex;
    flex-direction: column;
    max-width: 84%;
    animation: krChatFadeIn 0.2s ease-out;
}

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

.kr-chat-msg--visitor {
    align-self: flex-end;
}

.kr-chat-msg--visitor .kr-chat-msg-bubble {
    background-color: var(--kr-chat-primary);
    color: var(--kr-chat-primary-contrast);
    border-bottom-right-radius: 4px;
}

.kr-chat-msg--assistant,
.kr-chat-msg--agent {
    align-self: flex-start;
}

.kr-chat-msg--assistant .kr-chat-msg-bubble,
.kr-chat-msg--agent .kr-chat-msg-bubble {
    background-color: #FFFFFF;
    color: var(--kr-chat-text);
    border: 1px solid var(--kr-chat-border);
    border-bottom-left-radius: 4px;
}

.kr-chat-msg-bubble {
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.45;
    word-break: break-word;
    white-space: pre-wrap;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.kr-chat-msg-author {
    font-size: 11px;
    font-weight: 600;
    color: var(--kr-chat-muted);
    margin-bottom: 3px;
    padding-left: 2px;
}

.kr-chat-msg-time {
    font-size: 10px;
    color: var(--kr-chat-muted);
    margin-top: 3px;
    align-self: flex-end;
}

/* ── Quick-Topics / Schnellauswahl ─────────────────────────── */
.kr-chat-topics-wrap {
    margin-top: 4px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.kr-chat-topic-btn {
    background-color: #FFFFFF;
    border: 1px solid var(--kr-chat-border);
    border-radius: var(--kr-chat-radius-sm);
    padding: 8px 12px;
    font-size: 12.5px;
    font-weight: 500;
    text-align: left;
    color: var(--kr-chat-text);
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.kr-chat-topic-btn:hover {
    border-color: var(--kr-chat-primary);
    background-color: var(--kr-chat-surface-hover);
    color: var(--kr-chat-primary);
    transform: translateX(2px);
}

/* ── Ticket / Call Banner ──────────────────────────────────── */
.kr-chat-ticket-card {
    background: #ECFDF5;
    border: 1px solid #A7F3D0;
    border-radius: var(--kr-chat-radius-sm);
    padding: 12px;
    font-size: 12.5px;
    color: #065F46;
}

.kr-chat-ticket-card strong {
    font-size: 14px;
    color: #047857;
    display: block;
    margin-bottom: 2px;
}

/* ── Tipp-Indikator (Typing) ───────────────────────────────── */
.kr-chat-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 10px 14px;
    background: #FFFFFF;
    border: 1px solid var(--kr-chat-border);
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    width: fit-content;
    align-self: flex-start;
}

.kr-chat-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background-color: var(--kr-chat-muted);
    animation: krChatDot 1.4s infinite ease-in-out both;
}

.kr-chat-typing span:nth-child(1) { animation-delay: -0.32s; }
.kr-chat-typing span:nth-child(2) { animation-delay: -0.16s; }

@keyframes krChatDot {
    0%, 80%, 100% { transform: scale(0); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* ── Kontakt-Eskalation / Modal Form ───────────────────────── */
.kr-chat-contact-box {
    background: #FFFFFF;
    border: 1px solid var(--kr-chat-border);
    border-radius: var(--kr-chat-radius-sm);
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 4px;
}

.kr-chat-contact-box label {
    font-size: 11.5px;
    font-weight: 600;
    color: var(--kr-chat-muted);
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.kr-chat-contact-box input {
    border: 1px solid var(--kr-chat-border);
    border-radius: 6px;
    padding: 6px 8px;
    font-size: 13px;
    color: var(--kr-chat-text);
}

.kr-chat-contact-box input:focus {
    outline: none;
    border-color: var(--kr-chat-primary);
}

.kr-chat-btn-submit-contact {
    background-color: var(--kr-chat-primary);
    color: #FFFFFF;
    border: none;
    border-radius: 6px;
    padding: 8px 12px;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 4px;
}

.kr-chat-btn-submit-contact:hover {
    background-color: var(--kr-chat-accent);
}

/* ── Footer / Input ────────────────────────────────────────── */
.kr-chat-footer {
    padding: 12px 14px;
    background-color: var(--kr-chat-bg);
    border-top: 1px solid var(--kr-chat-border);
    flex-shrink: 0;
}

.kr-chat-input-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.kr-chat-input {
    flex: 1;
    border: 1px solid var(--kr-chat-border);
    border-radius: 20px;
    padding: 9px 14px;
    font-size: 13.5px;
    font-family: inherit;
    color: var(--kr-chat-text);
    background-color: var(--kr-chat-surface);
    outline: none;
    transition: border-color 0.15s ease, background-color 0.15s ease;
}

.kr-chat-input:focus {
    border-color: var(--kr-chat-primary);
    background-color: #FFFFFF;
}

.kr-chat-btn-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background-color: var(--kr-chat-primary);
    color: #FFFFFF;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.15s ease, background-color 0.15s ease;
    flex-shrink: 0;
    padding: 0;
}

.kr-chat-btn-send:hover {
    transform: scale(1.05);
    background-color: var(--kr-chat-accent);
}

.kr-chat-btn-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.kr-chat-btn-send svg {
    width: 17px;
    height: 17px;
    fill: currentColor;
    margin-left: 2px;
}

.kr-chat-privacy-footer {
    margin-top: 6px;
    font-size: 10.5px;
    color: var(--kr-chat-muted);
    text-align: center;
    line-height: 1.3;
}

.kr-chat-privacy-footer a {
    color: var(--kr-chat-primary);
    text-decoration: underline;
}

/* ── Mobile Responsive ─────────────────────────────────────── */
@media (max-width: 640px) {
    .kr-chat-root {
        bottom: 16px;
    }
    .kr-chat-pos-bottom-right {
        right: 16px;
    }
    .kr-chat-pos-bottom-left {
        left: 16px;
    }
    .kr-chat-window {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        max-width: 100vw;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
        border: none;
        transform: translateY(100%);
    }
    .kr-chat-window.is-visible {
        transform: translateY(0);
    }
}

/* ── Barrierefreiheit / Motion-Safe ────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    .kr-chat-fab,
    .kr-chat-window,
    .kr-chat-msg,
    .kr-chat-typing span {
        transition: none !important;
        animation: none !important;
        transform: none !important;
    }
}
