/* ============================================================
   Support Tickets – Chat Widget
   Token-driven: uses CSS variables set by widget.php config.
   ============================================================ */

/* ── Design tokens (light-mode defaults) ───────────────────── */
:root {
    --w-bg:              #ffffff;
    --w-surface:         #f8f7f4;
    --w-text:            #374151;
    --w-text-muted:      #9ca3af;
    --w-border:          rgba(0,0,0,.07);
    --w-input-bg:        #ffffff;
    --w-bot-bubble:      #ffffff;
    --w-shadow:          0 8px 32px rgba(0,0,0,.12);
}

/* ── Container ─────────────────────────────────────────────── */
#iw-st-widget {
    --w-accent:          #0d9488;
    --w-accent-dark:     #0b7c73;
    --w-accent-bg:       #f0fdf9;
    --w-radius:          20px;
    --w-font:            -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    /* Semantic tokens with fallback to global theme tokens */
    --w-text-primary:    var(--text-primary,   #374151);
    --w-text-secondary:  var(--text-secondary, #6b7280);
    --w-text-muted:      var(--text-muted,     #9ca3af);
    --w-danger:          var(--feedback-danger, #ef4444);
    --w-danger-bg:       var(--feedback-danger-bg, #fef2f2);
    --w-danger-border:   var(--feedback-danger-border, #fecaca);
    --w-success:         var(--feedback-success, #4ade80);

    position: fixed;
    z-index: 99999;
    bottom: 88px;
    right: 28px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 12px;
    font-family: var(--w-font);
}
#iw-st-widget.iw-st-pos-bl {
    right: auto;
    left: 28px;
    align-items: flex-start;
}

/* ── Toggle button ──────────────────────────────────────────── */
#iw-st-toggle {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--w-accent);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 28px rgba(13,148,136,.38), 0 2px 8px rgba(0,0,0,.12);
    transition: transform .2s cubic-bezier(.34,1.56,.64,1), box-shadow .2s;
    position: relative;
    flex-shrink: 0;
}
#iw-st-toggle:hover {
    transform: scale(1.08);
    box-shadow: 0 12px 36px rgba(13,148,136,.46), 0 2px 8px rgba(0,0,0,.14);
}
#iw-st-toggle svg { color: #fff; display: block; }

/* Unread badge */
#iw-st-badge {
    position: absolute;
    top: 2px; right: 2px;
    min-width: 18px; height: 18px;
    border-radius: 9px;
    background: var(--w-danger);
    font-size: .6rem; font-weight: 700; color: #fff;
    display: flex; align-items: center; justify-content: center;
    padding: 0 4px;
    border: 2px solid #fff;
    line-height: 1;
}
#iw-st-badge[hidden] { display: none; }

/* ── Chat window ────────────────────────────────────────────── */
#iw-st-chat {
    width: 360px;
    background: rgba(255,255,255,.92);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border-radius: var(--w-radius);
    border: 1px solid rgba(255,255,255,.9);
    box-shadow:
        0 0 0 1px rgba(0,0,0,.04),
        0 4px 6px rgba(0,0,0,.03),
        0 20px 60px rgba(0,0,0,.12),
        0 40px 80px rgba(13,148,136,.05);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    max-height: 520px;
}
#iw-st-chat[hidden] { display: none; }

@media (max-width: 480px) {
    #iw-st-widget {
        bottom: 0; right: 0; left: 0;
        align-items: stretch;
        gap: 0;
    }
    #iw-st-widget.iw-st-pos-bl {
        right: 0; left: 0;
    }
    #iw-st-toggle {
        position: fixed;
        bottom: 20px; right: 20px;
    }
    #iw-st-widget.iw-st-pos-bl #iw-st-toggle {
        right: auto; left: 20px;
    }
    #iw-st-chat {
        position: fixed;
        inset: 0;
        width: 100%;
        max-height: 100%;
        border-radius: 0;
        border: none;
    }
}

/* ── Header ─────────────────────────────────────────────────── */
#iw-st-head {
    background: linear-gradient(135deg, var(--w-accent) 0%, var(--w-accent-dark) 100%);
    padding: 1rem 1.25rem;
    display: flex;
    align-items: center;
    gap: .875rem;
    flex-shrink: 0;
}
.iw-st-head-av {
    width: 38px; height: 38px;
    border-radius: 50%;
    background: rgba(255,255,255,.2);
    display: flex; align-items: center; justify-content: center;
    border: 2px solid rgba(255,255,255,.3);
    flex-shrink: 0;
    overflow: hidden;
}
.iw-st-head-av img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.iw-st-head-av svg { color: #fff; }
.iw-st-head-info { flex: 1; min-width: 0; }
.iw-st-head-name {
    font-size: .875rem; font-weight: 600; color: #fff;
    letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.iw-st-head-status {
    font-size: .7rem; color: rgba(255,255,255,.75);
    display: flex; align-items: center; gap: .3rem; margin-top: 2px;
}
.iw-st-head-status::before {
    content: '';
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--w-success);
    box-shadow: 0 0 6px rgba(74,222,128,.8);
    flex-shrink: 0;
}
#iw-st-close {
    background: rgba(255,255,255,.15);
    border: none; cursor: pointer;
    width: 28px; height: 28px; border-radius: 50%;
    color: rgba(255,255,255,.85);
    display: flex; align-items: center; justify-content: center;
    font-size: .875rem; line-height: 1;
    transition: background .15s; flex-shrink: 0;
}
#iw-st-close:hover { background: rgba(255,255,255,.28); color: #fff; }

/* ── Messages body ──────────────────────────────────────────── */
#iw-st-body {
    flex: 1;
    overflow-y: auto;
    padding: 1.125rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: .75rem;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}
#iw-st-body::-webkit-scrollbar { width: 4px; }
#iw-st-body::-webkit-scrollbar-track { background: transparent; }
#iw-st-body::-webkit-scrollbar-thumb { background: rgba(0,0,0,.12); border-radius: 2px; }

/* ── Message bubbles ────────────────────────────────────────── */
.iw-st-msg { display: flex; gap: .625rem; align-items: flex-end; }
.iw-st-msg-av {
    width: 28px; height: 28px; border-radius: 50%;
    background: linear-gradient(135deg, var(--w-accent), var(--w-accent-dark));
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; overflow: hidden;
}
.iw-st-msg-av img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.iw-st-msg-av svg { color: #fff; }

.iw-st-bubble {
    background: #fff;
    border: 1px solid rgba(0,0,0,.07);
    border-radius: 16px 16px 16px 4px;
    padding: .7rem 1rem;
    font-size: .8125rem; color: var(--w-text-primary); line-height: 1.55;
    box-shadow: 0 1px 4px rgba(0,0,0,.06);
    max-width: 240px;
    word-wrap: break-word;
}

/* Typing indicator */
.iw-st-typing .iw-st-bubble {
    padding: .7rem .875rem;
    display: flex; align-items: center; gap: 4px;
}
.iw-st-dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--w-text-muted);
    animation: iw-st-bounce .9s ease infinite;
}
.iw-st-dot:nth-child(2) { animation-delay: .15s; }
.iw-st-dot:nth-child(3) { animation-delay: .30s; }
@keyframes iw-st-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .5; }
    30%            { transform: translateY(-5px); opacity: 1; }
}

/* Appear animation */
.iw-st-msg {
    animation: iw-st-msg-in .22s ease;
}
@keyframes iw-st-msg-in {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ── Choices ────────────────────────────────────────────────── */
.iw-st-choices {
    display: flex; flex-direction: column; gap: .45rem;
    padding-left: calc(28px + .625rem); /* align with bubble */
    animation: iw-st-msg-in .28s ease;
}
.iw-st-choice {
    background: #fff;
    border: 1.5px solid rgba(13,148,136,.22);
    border-radius: 100px;
    padding: .55rem 1.1rem;
    font-family: var(--w-font);
    font-size: .78rem; font-weight: 500;
    color: var(--w-accent);
    cursor: pointer; text-align: left;
    transition: background .15s, border-color .15s, transform .12s, box-shadow .15s;
    box-shadow: 0 1px 3px rgba(13,148,136,.08);
}
.iw-st-choice:hover {
    background: var(--w-accent-bg);
    border-color: var(--w-accent);
    transform: translateX(3px);
    box-shadow: 0 4px 12px rgba(13,148,136,.14);
}
.iw-st-choice:disabled {
    opacity: .45; pointer-events: none;
}

/* ── Divider ────────────────────────────────────────────────── */
.iw-st-divider {
    display: flex; align-items: center; gap: .75rem;
    padding: .25rem 0;
}
.iw-st-divider span {
    font-size: .68rem; color: var(--w-text-muted); white-space: nowrap;
}
.iw-st-divider::before, .iw-st-divider::after {
    content: ''; flex: 1; height: 1px; background: var(--border-light, #efefef);
}

/* ── Contact form ───────────────────────────────────────────── */
.iw-st-form {
    display: flex; flex-direction: column; gap: .6rem;
    padding-left: calc(28px + .625rem);
    animation: iw-st-msg-in .28s ease;
}
.iw-st-field {
    display: flex; flex-direction: column; gap: .25rem;
}
.iw-st-field label {
    font-size: .7rem; font-weight: 600; color: var(--w-text-secondary, #374151);
    letter-spacing: .04em; text-transform: uppercase;
}
.iw-st-field input {
    background: #fff;
    border: 1.5px solid rgba(0,0,0,.1);
    border-radius: 10px;
    padding: .55rem .75rem;
    font-family: var(--w-font);
    font-size: .8125rem; color: var(--w-text-primary);
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}
.iw-st-field input:focus {
    border-color: var(--w-accent);
    box-shadow: 0 0 0 3px rgba(13,148,136,.12);
}
.iw-st-field input.iw-st-error {
    border-color: var(--w-danger);
    box-shadow: 0 0 0 3px rgba(239,68,68,.1);
}
.iw-st-field-error {
    font-size: .7rem; color: var(--w-danger);
}
.iw-st-form-submit {
    background: var(--w-accent); color: #fff;
    border: none; border-radius: 100px;
    padding: .625rem 1.25rem;
    font-family: var(--w-font); font-size: .8125rem; font-weight: 600;
    cursor: pointer; align-self: flex-start;
    transition: background .15s, transform .12s, box-shadow .15s;
    box-shadow: 0 4px 12px rgba(13,148,136,.25);
}
.iw-st-form-submit:hover {
    background: var(--w-accent-dark);
    transform: translateY(-1px);
    box-shadow: 0 6px 16px rgba(13,148,136,.32);
}
.iw-st-form-submit:disabled { opacity: .6; pointer-events: none; }

/* ── Success / error state ──────────────────────────────────── */
.iw-st-success {
    display: flex; flex-direction: column; align-items: center;
    padding: 1.5rem 1.25rem; text-align: center; gap: .75rem;
    animation: iw-st-msg-in .3s ease;
}
.iw-st-success-icon {
    width: 48px; height: 48px; border-radius: 50%;
    background: linear-gradient(135deg, var(--w-accent), var(--w-accent-dark));
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 6px 20px rgba(13,148,136,.3);
}
.iw-st-success-icon svg { color: #fff; }
.iw-st-success h3 { font-size: .9rem; font-weight: 700; color: var(--w-text-primary); }
.iw-st-success p  { font-size: .8rem; color: var(--w-text-secondary); line-height: 1.55; }

/* ── Footer / input bar ─────────────────────────────────────── */
#iw-st-footer {
    border-top: 1px solid rgba(0,0,0,.05);
    padding: .75rem 1.25rem;
    display: flex; gap: .5rem; align-items: center;
    background: rgba(248,247,244,.65);
    flex-shrink: 0;
}
.iw-st-input-wrap {
    flex: 1; min-width: 0;
    background: rgba(0,0,0,.04);
    border: 1px solid rgba(0,0,0,.08);
    border-radius: 14px;
    padding: .45rem .875rem;
    display: flex; align-items: flex-end;
    transition: border-color .15s, box-shadow .15s;
}
.iw-st-input-wrap:focus-within {
    border-color: var(--w-accent);
    box-shadow: 0 0 0 3px rgba(13,148,136,.1);
    background: #fff;
}
#iw-st-text-input {
    flex: 1; background: none; border: none; outline: none;
    font-family: var(--w-font); font-size: .8rem; color: var(--w-text-primary);
    resize: none; overflow: hidden; min-height: 1.25rem; max-height: 100px;
    line-height: 1.4; padding: 0; margin: 0; display: block;
}
#iw-st-text-input::placeholder { color: var(--w-text-muted); }
#iw-st-text-input:disabled { cursor: not-allowed; }
#iw-st-send {
    width: 32px; height: 32px; border-radius: 50%;
    background: var(--w-accent); border: none;
    cursor: pointer; display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    transition: background .15s, transform .12s, box-shadow .15s;
    box-shadow: 0 2px 8px rgba(13,148,136,.28);
}
#iw-st-send:hover { background: var(--w-accent-dark); transform: scale(1.08); }
#iw-st-send:disabled { opacity: .4; pointer-events: none; }
#iw-st-send svg { color: #fff; display: block; }

/* ── Dark mode ──────────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
    :root {
        --w-bg:         #1e293b;
        --w-surface:    #0f172a;
        --w-text:       #f1f5f9;
        --w-text-muted: #94a3b8;
        --w-border:     #334155;
        --w-input-bg:   #1e293b;
        --w-bot-bubble: #1e3a5f;
        --w-shadow:     0 8px 32px rgba(0,0,0,.4);
    }
    #iw-st-widget {
        --w-text-primary:   var(--text-primary,   #e5e7eb);
        --w-text-secondary: var(--text-secondary, #9ca3af);
        --w-text-muted:     var(--text-muted,     #6b7280);
        --w-danger:         var(--feedback-danger, #f87171);
        --w-danger-bg:      var(--feedback-danger-bg, #450a0a);
        --w-danger-border:  var(--feedback-danger-border, #991b1b);
        --w-success:        var(--feedback-success, #34d399);
    }
    #iw-st-chat {
        background: rgba(17,24,39,.96);
        border-color: rgba(255,255,255,.08);
    }
    .iw-st-bubble {
        background: rgba(31,41,55,.95);
        border-color: rgba(255,255,255,.08);
    }
    .iw-st-choice {
        background: rgba(31,41,55,.9);
        border-color: rgba(13,148,136,.3);
    }
    .iw-st-choice:hover {
        background: rgba(13,148,136,.15);
    }
    .iw-st-field label {
        color: #c9d1db;
    }
    .iw-st-field input {
        background: rgba(31,41,55,.9);
        border-color: rgba(255,255,255,.12);
        color: var(--w-text-primary);
    }
    #iw-st-footer {
        background: rgba(17,24,39,.8);
        border-top-color: rgba(255,255,255,.06);
    }
    .iw-st-input-wrap {
        background: rgba(255,255,255,.05);
        border-color: rgba(255,255,255,.1);
    }
    .iw-st-input-wrap:focus-within {
        background: rgba(255,255,255,.08);
    }
    .iw-st-divider::before, .iw-st-divider::after {
        background: rgba(255,255,255,.08);
    }
}

/* ── Mobile keyboard fix (#6) ───────────────────────────────── */
@media (max-width: 768px) {
    .iw-st-widget {
        max-height: calc(100vh - env(safe-area-inset-bottom, 0px) - 60px);
        height: calc(100dvh - env(safe-area-inset-bottom, 0px) - 60px);
    }
    .iw-st-chat {
        max-height: 65vh;
    }
}

/* ── Loading spinner (#7) ────────────────────────────────────── */
.iw-st-btn-send.loading::after {
    content: '';
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255,255,255,.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: iw-spin .6s linear infinite;
    margin-left: 4px;
    vertical-align: middle;
}
@keyframes iw-spin { to { transform: rotate(360deg); } }

/* ── Offline banner + toast (#8) ─────────────────────────────── */
.iw-st-offline-banner {
    background: var(--feedback-danger, #ef4444);
    color: #fff;
    padding: .5rem 1rem;
    font-size: .8125rem;
    text-align: center;
    border-radius: var(--radius-sm, 4px);
    margin-bottom: .5rem;
}
.iw-st-toast {
    position: fixed;
    bottom: 80px;
    right: 16px;
    padding: .625rem 1.25rem;
    border-radius: var(--radius-sm, 6px);
    font-size: .8125rem;
    font-weight: 500;
    z-index: 99999;
    animation: iw-fadein .2s ease;
    background: #1f2937;
    color: #f3f4f6;
    box-shadow: 0 4px 16px rgba(0,0,0,.22);
    max-width: 320px;
}
.iw-st-toast--success {
    background: var(--feedback-success, #16a34a);
    color: #fff;
}
@keyframes iw-fadein { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; } }

/* ── Session-resume banner (#3) ──────────────────────────────── */
.iw-st-resume-banner {
    background: var(--w-accent-bg, #f0fdf9);
    border: 1.5px solid rgba(13,148,136,.22);
    border-radius: var(--radius-sm, 8px);
    padding: .75rem 1rem;
    font-size: .8125rem;
    color: var(--w-text-primary);
    display: flex;
    flex-direction: column;
    gap: .5rem;
    animation: iw-st-msg-in .25s ease;
}
.iw-st-resume-banner__btns {
    display: flex;
    gap: .5rem;
}
.iw-st-resume-btn {
    border: 1.5px solid rgba(13,148,136,.35);
    border-radius: 100px;
    padding: .4rem .9rem;
    font-family: var(--w-font);
    font-size: .75rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, border-color .15s;
}
.iw-st-resume-btn--yes {
    background: var(--w-accent, #0d9488);
    color: #fff;
    border-color: var(--w-accent, #0d9488);
}
.iw-st-resume-btn--yes:hover { background: var(--w-accent-dark, #0b7c73); }
.iw-st-resume-btn--no {
    background: transparent;
    color: var(--w-text-primary, #374151);
}
.iw-st-resume-btn--no:hover { background: rgba(0,0,0,.04); }

/* ── CSAT rating (#14) ───────────────────────────────────────── */
.iw-st-csat { text-align: center; padding: 1rem; }
.iw-st-csat__stars {
    font-size: 1.75rem;
    cursor: pointer;
    display: flex;
    justify-content: center;
    gap: .25rem;
}
.iw-st-csat__star {
    color: var(--w-text-muted, #9ca3af);
    transition: color .15s;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
}
.iw-st-csat__star.active { color: #f59e0b; }
.iw-st-csat__star:hover,
.iw-st-csat__stars:hover .iw-st-csat__star { color: #f59e0b; }
.iw-st-csat__stars .iw-st-csat__star:hover ~ .iw-st-csat__star { color: var(--w-text-muted, #9ca3af); }
.iw-st-csat__thanks {
    color: var(--feedback-success, #16a34a);
    font-size: .875rem;
    margin-top: .5rem;
}

/* ── Accessibility ──────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ── File Attachments ────────────────────────────────────────────────── */
.iw-st-attach-area {
  margin: .5rem 0 .75rem;
  border: 1.5px dashed var(--w-border);
  border-radius: var(--w-radius);
  padding: .625rem .75rem;
  cursor: pointer;
  transition: border-color .2s, background .2s;
  position: relative;
}
.iw-st-attach-area:hover,
.iw-st-attach-area.drag-over {
  border-color: var(--w-accent);
  background: color-mix(in srgb, var(--w-accent) 6%, transparent);
}
.iw-st-attach-area input[type="file"] {
  /* Komplett aus dem Layout/Hover-Tree raus, Klick wird programmatisch via JS ausgelöst.
     Vorher: opacity:0 + position:absolute zeigte Browser-Tooltip „Keine ausgewählt". */
  display: none !important;
}
.iw-st-attach-area:focus-visible {
  outline: 2px solid var(--w-accent);
  outline-offset: 2px;
}
.iw-st-attach-hint {
  display: flex;
  align-items: center;
  gap: .5rem;
  color: var(--w-text-secondary);
  font-size: .75rem;
  pointer-events: none;
}
.iw-st-attach-hint svg {
  flex-shrink: 0;
  stroke: var(--w-text-secondary);
}
.iw-st-attach-list {
  list-style: none;
  margin: .25rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: .25rem;
}
.iw-st-attach-item {
  display: flex;
  align-items: center;
  gap: .5rem;
  background: var(--w-bg-bubble-staff);
  border-radius: calc(var(--w-radius) - 2px);
  padding: .35rem .6rem;
  font-size: .75rem;
  color: var(--w-text-primary);
}
.iw-st-attach-item__name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.iw-st-attach-item__size {
  color: var(--w-text-secondary);
  white-space: nowrap;
  flex-shrink: 0;
}
.iw-st-attach-item__remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--w-text-secondary);
  padding: 0 2px;
  line-height: 1;
  font-size: .9rem;
  flex-shrink: 0;
}
.iw-st-attach-item__remove:hover { color: #ef4444; }
.iw-st-attach-item--uploading .iw-st-attach-item__name::after {
  content: ' (' attr(data-status) ')';
  color: var(--w-text-secondary);
}
.iw-st-attach-progress {
  height: 3px;
  background: var(--w-border);
  border-radius: 2px;
  margin-top: 4px;
  overflow: hidden;
}
.iw-st-attach-progress__bar {
  height: 100%;
  background: var(--w-accent);
  border-radius: 2px;
  transition: width .2s;
  width: 0%;
}
