/**
 * SapplBot Chat Widget Styles
 *
 * CSS Custom Properties for theming:
 * --sapplbot-color: Primary brand color (set via JS from agent config)
 * --sapplbot-color-light: Light version for backgrounds
 */

/* =========================================================================
   CSS Variables / Defaults
   ========================================================================= */

:root {
    --sapplbot-color: #2a94d1;
    --sapplbot-color-light: rgba(42, 148, 209, 0.1);
    --sapplbot-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    --sapplbot-radius: 12px;
    --sapplbot-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
    --sapplbot-widget-width: 500px;
    --sapplbot-widget-height: 690px;
}

/* =========================================================================
   Container & Positioning
   ========================================================================= */

.sapplbot-container {
    position: fixed;
    z-index: 999999;
    font-family: var(--sapplbot-font);
    font-size: 14px;
    line-height: 1.5;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.sapplbot-container.sapplbot-bottom-left {
    align-items: flex-start;
}

.sapplbot-container.sapplbot-bottom-right {
    bottom: 20px;
    right: 20px;
}

.sapplbot-container.sapplbot-bottom-left {
    bottom: 20px;
    left: 20px;
}

/* iOS-Safari im Browser: Container über die untere Safari-Leiste heben.
   Diese Positionierung macht das Plugin selbst – kein externer Lift!
   Muss NACH den bottom-right/bottom-left-Regeln stehen (gleiche Spezifität). */
.sapplbot-container.sapplbot-ios-browser {
    bottom: calc(40px + env(safe-area-inset-bottom, 0px));
}

/* =========================================================================
   Bubble (Chat Trigger)
   ========================================================================= */

.sapplbot-bubble {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--sapplbot-color);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--sapplbot-shadow);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.sapplbot-bubble:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 30px rgba(0, 0, 0, 0.2);
}

/* Avatar-Bild als Chat-Button (statt des Brief-Symbols). */
.sapplbot-bubble.sapplbot-bubble-has-avatar {
    overflow: hidden;
}

.sapplbot-bubble-avatar {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    display: block;
}

/* =========================================================================
   Teaser (aufpoppende Begrüßungs-Sprechblase neben dem Chat-Symbol)
   ========================================================================= */

.sapplbot-teaser {
    position: relative;
    max-width: 320px;
    background: #fff;
    /* Explizite Schriftfarbe: der Teaser darf die Textfarbe NICHT von der
       Gastseite erben (Avada vererbt z. B. ein kontrastarmes Grau). */
    color: #1a1a1a;
    border: 1px solid #e2e4e7;
    border-radius: var(--sapplbot-radius);
    border-bottom-right-radius: 4px;
    box-shadow: var(--sapplbot-shadow);
    padding: 12px 16px;
    margin-bottom: 12px;
    cursor: pointer;
    animation: sapplbot-teaser-in 0.35s ease-out;
}

.sapplbot-container.sapplbot-bottom-left .sapplbot-teaser {
    border-bottom-right-radius: var(--sapplbot-radius);
    border-bottom-left-radius: 4px;
}

@keyframes sapplbot-teaser-in {
    from { opacity: 0; transform: translateY(14px); }
    to   { opacity: 1; transform: translateY(0); }
}

.sapplbot-teaser-close {
    position: absolute;
    top: -14px;
    right: 0;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    border: 1px solid #dcdcde;
    background: #fff;
    color: #666;
    font-size: 17px;
    line-height: 1;
    cursor: pointer;
    padding: 0;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

.sapplbot-teaser-close:hover {
    color: #000;
}

.sapplbot-teaser-content p:last-child {
    margin-bottom: 0;
}

@media (max-width: 480px) {
    .sapplbot-teaser {
        position: fixed;
        right: 16px;
        bottom: 84px;
        max-width: calc(100vw - 32px);
    }

    .sapplbot-container.sapplbot-bottom-left .sapplbot-teaser {
        right: auto;
        left: 16px;
    }
}

/* Einhüpf-Animation beim Laden der Seite (per Agent aktivierbar). */
@keyframes sapplbot-hop-in {
    0%   { transform: translateY(140px); opacity: 0; }
    60%  { transform: translateY(-12px); opacity: 1; }
    80%  { transform: translateY(5px); }
    100% { transform: translateY(0); }
}

.sapplbot-container.sapplbot-animate-in {
    animation: sapplbot-hop-in 0.6s ease-out;
}

@media (prefers-reduced-motion: reduce) {
    .sapplbot-container.sapplbot-animate-in {
        animation: none;
    }
}

/* =========================================================================
   Widget Window
   ========================================================================= */

.sapplbot-widget {
    width: var(--sapplbot-widget-width);
    height: var(--sapplbot-widget-height);
    max-height: calc(100vh - 100px);
    max-width: calc(100vw - 40px);
    background: #fff;
    border-radius: var(--sapplbot-radius);
    box-shadow: var(--sapplbot-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin-bottom: 12px;
}

/* =========================================================================
   Header
   ========================================================================= */

.sapplbot-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--sapplbot-color);
    color: #fff;
    flex-shrink: 0;
}

.sapplbot-header-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sapplbot-header-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 14px;
    color: #fff;
    flex-shrink: 0;
}

img.sapplbot-header-avatar {
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.sapplbot-header-title {
    font-weight: 600;
    font-size: 15px;
}

.sapplbot-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Beide Header-Buttons identisch aufgebaut (gleiche Fläche, Icon exakt
   zentriert), damit "Neuer Chat" und "X" perfekt auf einer Linie liegen. */
.sapplbot-close,
.sapplbot-new-chat {
    background: none;
    border: none;
    color: #fff;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0.8;
    transition: opacity 0.15s;
}

.sapplbot-close:hover,
.sapplbot-new-chat:hover {
    opacity: 1;
}

.sapplbot-close svg,
.sapplbot-new-chat svg {
    width: 20px;
    height: 20px;
    display: block;
}

/* =========================================================================
   Messages Area
   ========================================================================= */

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

/* Scrollbar styling */
.sapplbot-messages::-webkit-scrollbar {
    width: 6px;
}

.sapplbot-messages::-webkit-scrollbar-track {
    background: transparent;
}

.sapplbot-messages::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}

/* =========================================================================
   Message Bubbles
   ========================================================================= */

.sapplbot-msg {
    display: flex;
    gap: 8px;
    max-width: 90%;
    animation: sapplbot-fadeIn 0.2s ease;
}

@keyframes sapplbot-fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.sapplbot-msg-user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.sapplbot-msg-bot {
    align-self: flex-start;
}

.sapplbot-msg-content {
    padding: 10px 14px;
    border-radius: var(--sapplbot-radius);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.sapplbot-msg-user .sapplbot-msg-content {
    background: var(--sapplbot-user-bg, var(--sapplbot-color));
    border: 1px solid var(--sapplbot-user-border, var(--sapplbot-user-bg, var(--sapplbot-color)));
    color: var(--sapplbot-user-text, #fff);
    border-bottom-right-radius: 4px;
}

.sapplbot-msg-bot .sapplbot-msg-content {
    background: var(--sapplbot-bot-bg, #f0f2f5);
    border: 1px solid var(--sapplbot-bot-border, #000000);
    color: var(--sapplbot-bot-text, #1a1a1a);
    border-bottom-left-radius: 4px;
}

.sapplbot-msg-body {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
}

/* Avatar */
.sapplbot-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    margin-top: 2px;
}

.sapplbot-avatar-initials {
    background: var(--sapplbot-color);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 12px;
}

/* Status-Zwischenmeldung während Tool-Aufrufen */
.sapplbot-status {
    color: #888;
    font-size: 0.92em;
}

/* Typing cursor */
.sapplbot-typing-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: var(--sapplbot-color);
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: sapplbot-blink 0.8s infinite;
}

@keyframes sapplbot-blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* =========================================================================
   Markdown Content
   ========================================================================= */

.sapplbot-markdown p {
    margin: 0 0 8px 0;
}

.sapplbot-markdown p:last-child {
    margin-bottom: 0;
}

.sapplbot-markdown ul,
.sapplbot-markdown ol {
    margin: 4px 0 8px 0;
    padding-left: 20px;
}

.sapplbot-markdown li {
    margin-bottom: 2px;
}

.sapplbot-markdown strong {
    font-weight: 600;
}

.sapplbot-markdown a {
    color: var(--sapplbot-color);
    text-decoration: underline;
}

.sapplbot-markdown code {
    background: rgba(0, 0, 0, 0.06);
    padding: 1px 4px;
    border-radius: 3px;
    font-size: 0.9em;
    font-family: 'SF Mono', Monaco, Consolas, monospace;
}

.sapplbot-markdown pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 0.85em;
}

.sapplbot-markdown pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.sapplbot-markdown blockquote {
    border-left: 3px solid var(--sapplbot-color);
    margin: 8px 0;
    padding: 4px 12px;
    color: #555;
    background: var(--sapplbot-color-light);
    border-radius: 0 6px 6px 0;
}

.sapplbot-markdown h1,
.sapplbot-markdown h2,
.sapplbot-markdown h3,
.sapplbot-markdown h4 {
    margin: 12px 0 6px 0;
    font-weight: 600;
}

.sapplbot-markdown h1 { font-size: 1.3em; }
.sapplbot-markdown h2 { font-size: 1.15em; }
.sapplbot-markdown h3 { font-size: 1.05em; }

.sapplbot-markdown hr {
    border: none;
    border-top: 1px solid #ddd;
    margin: 12px 0;
}

/* =========================================================================
   Tables (critical per spec 10.3)
   ========================================================================= */

.sapplbot-markdown table {
    width: 100%;
    border-collapse: collapse;
    margin: 8px 0;
    font-size: 0.9em;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.sapplbot-markdown thead {
    background: var(--sapplbot-color);
    color: #fff;
}

.sapplbot-markdown th,
.sapplbot-markdown td {
    padding: 8px 10px;
    text-align: left;
    white-space: nowrap;
    border: 1px solid #e0e0e0;
}

.sapplbot-markdown th {
    font-weight: 600;
    border-color: rgba(255, 255, 255, 0.2);
}

.sapplbot-markdown tbody tr:nth-child(even) {
    background: #f8f9fa;
}

.sapplbot-markdown tbody tr:hover {
    background: var(--sapplbot-color-light);
}

/* =========================================================================
   Inline Images (EPC QR codes etc.)
   ========================================================================= */

.sapplbot-inline-image {
    max-width: 200px;
    border-radius: 8px;
    margin: 4px 0;
    display: block;
}

/* =========================================================================
   Message Actions (Copy / Like / Dislike)
   ========================================================================= */

.sapplbot-msg-actions {
    display: flex;
    gap: 2px;
    margin-top: 2px;
}

.sapplbot-action-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 3px 5px;
    color: #9aa0a6;
    border-radius: 4px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
}

.sapplbot-action-btn:hover {
    background: rgba(0, 0, 0, 0.06);
    color: #50575e;
}

.sapplbot-action-btn.is-active {
    color: var(--sapplbot-color);
}

.sapplbot-action-btn.sapplbot-action-dislike.is-active {
    color: #d63638;
}

.sapplbot-action-btn svg {
    width: 15px;
    height: 15px;
}

/* =========================================================================
   Sources (collapsible)
   ========================================================================= */

.sapplbot-sources {
    margin-top: 4px;
}

.sapplbot-sources-toggle {
    background: none;
    border: none;
    color: #666;
    font-size: 12px;
    cursor: pointer;
    padding: 2px 0;
    font-family: inherit;
}

.sapplbot-sources-toggle:hover {
    color: var(--sapplbot-color);
}

.sapplbot-sources-list {
    margin-top: 4px;
    padding-left: 8px;
    font-size: 12px;
    color: #666;
}

.sapplbot-source-item {
    padding: 2px 0;
}

.sapplbot-source-pool {
    color: #999;
    font-size: 11px;
}

/* =========================================================================
   Message Suggestions (Chips)
   ========================================================================= */

.sapplbot-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 4px 0;
}

.sapplbot-chip {
    background: #fff;
    border: 1px solid var(--sapplbot-color);
    color: var(--sapplbot-color);
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 13px;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, color 0.15s;
    white-space: nowrap;
}

.sapplbot-chip:hover {
    background: var(--sapplbot-color);
    color: #fff;
}

/* =========================================================================
   Input Area
   ========================================================================= */

.sapplbot-input-wrap {
    display: flex;
    align-items: flex-end;
    padding: 10px 12px;
    border-top: 1px solid #e8e8e8;
    background: #fff;
    gap: 8px;
    flex-shrink: 0;
}

.sapplbot-input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 8px 14px;
    font-family: inherit;
    font-size: 14px;
    resize: none;
    outline: none;
    max-height: 120px;
    line-height: 1.4;
    transition: border-color 0.15s;
}

.sapplbot-input:focus {
    border-color: var(--sapplbot-color);
}

.sapplbot-input::placeholder {
    color: #999;
}

.sapplbot-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--sapplbot-color);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity 0.15s;
}

.sapplbot-send:hover {
    opacity: 0.85;
}

.sapplbot-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* =========================================================================
   Disclaimer
   ========================================================================= */

.sapplbot-disclaimer {
    padding: 6px 16px;
    font-size: 11px;
    color: #999;
    text-align: center;
    border-top: 1px solid #f0f0f0;
    background: #fafafa;
    flex-shrink: 0;
}

.sapplbot-disclaimer p {
    margin: 0;
}

.sapplbot-disclaimer a {
    color: var(--sapplbot-color);
    text-decoration: underline;
}

/* =========================================================================
   Responsive – Mobile
   ========================================================================= */

@media (max-width: 480px) {
    .sapplbot-container {
        bottom: 0 !important;
        right: 0 !important;
        left: 0 !important;
    }

    .sapplbot-widget {
        width: 100vw;
        height: 100vh;
        height: 100dvh; /* passt sich Safaris dynamischer Leiste an */
        max-height: 100vh;
        max-height: 100dvh;
        max-width: 100vw;
        border-radius: 0;
        margin-bottom: 0;
    }

    /* iOS zoomt automatisch in Eingabefelder mit Schriftgröße < 16px –
       dabei wandert der Senden-Button aus dem Bild. 16px verhindert den
       Zoom komplett (gleicher Trick wie bei sissi). */
    .sapplbot-input {
        font-size: 16px;
    }

    /* Header-Buttons (Neuer Chat + Schließen) am Handy größer –
       bessere Sichtbarkeit und fingerfreundlichere Trefferfläche. */
    .sapplbot-close,
    .sapplbot-new-chat {
        width: 38px;
        height: 38px;
    }

    .sapplbot-close svg,
    .sapplbot-new-chat svg {
        width: 24px;
        height: 24px;
    }

    .sapplbot-bubble {
        position: fixed;
        bottom: 16px;
        right: 16px;
    }

    /* iOS-Safari (Browser): Symbol und Teaser über die untere Leiste heben. */
    .sapplbot-container.sapplbot-ios-browser .sapplbot-bubble {
        bottom: calc(32px + env(safe-area-inset-bottom, 0px));
    }

    .sapplbot-container.sapplbot-ios-browser .sapplbot-teaser {
        bottom: calc(100px + env(safe-area-inset-bottom, 0px));
    }

    /* Im Frame eingebettet (Schüler-Dashboard): env(safe-area-inset-bottom)
       liefert dort 0, die Safari-Leiste schnitt das Symbol unten leicht an –
       deshalb Symbol und Teaser ein paar Pixel höher. */
    .sapplbot-container.sapplbot-ios-browser.sapplbot-ios-frame .sapplbot-bubble {
        bottom: calc(42px + env(safe-area-inset-bottom, 0px));
    }

    .sapplbot-container.sapplbot-ios-browser.sapplbot-ios-frame .sapplbot-teaser {
        bottom: calc(110px + env(safe-area-inset-bottom, 0px));
    }

    .sapplbot-container.sapplbot-bottom-left .sapplbot-bubble {
        right: auto;
        left: 16px;
    }

    .sapplbot-header {
        border-radius: 0;
    }

    .sapplbot-msg {
        max-width: 95%;
    }
}
