/* ---------- Chat widget (AI assistant + live agent) ---------- */
.chatw { position: fixed; right: 20px; bottom: 20px; z-index: 60; font-family: var(--font); }
.chatw[hidden] { display: none; }
.back-to-top { bottom: 92px; }

.chatw__launcher { position: relative; display: grid; place-items: center; width: 60px; height: 60px; border: 0; border-radius: 999px; background: linear-gradient(135deg, var(--brand-700), var(--brand)); color: #fff; box-shadow: 0 14px 30px -10px rgba(19, 42, 85, .65); cursor: pointer; transition: transform .2s var(--ease), box-shadow .2s; }
.chatw__launcher:hover { transform: translateY(-2px) scale(1.03); box-shadow: 0 18px 36px -12px rgba(19, 42, 85, .75); }
.chatw__launcher:focus-visible { outline: none; box-shadow: var(--ring), 0 14px 30px -10px rgba(19, 42, 85, .65); }
.chatw__launcher .icon { width: 26px; height: 26px; }
.chatw__launcher-close { display: none; }
.chatw.is-open .chatw__launcher-open { display: none; }
.chatw.is-open .chatw__launcher-close { display: grid; }
.chatw__badge { position: absolute; top: -2px; right: -2px; min-width: 22px; height: 22px; padding: 0 6px; border: 2px solid #fff; border-radius: 999px; background: var(--accent); color: #fff; font-size: .72rem; font-weight: 800; line-height: 18px; text-align: center; }

.chatw__panel { position: absolute; right: 0; bottom: 76px; display: flex; flex-direction: column; width: min(390px, calc(100vw - 32px)); height: min(640px, calc(100vh - 120px)); border: 1px solid var(--line); border-radius: 22px; background: var(--bg); box-shadow: var(--shadow-lg); overflow: hidden; transform-origin: bottom right; animation: chatw-in .22s var(--ease); }
.chatw__panel[hidden] { display: none; }
@keyframes chatw-in { from { opacity: 0; transform: translateY(12px) scale(.97); } to { opacity: 1; transform: none; } }

.chatw__head { display: flex; align-items: center; gap: .75rem; padding: .9rem 1rem; background: var(--night-gradient); color: #fff; }
.chatw__avatar { display: grid; place-items: center; flex-shrink: 0; width: 40px; height: 40px; border-radius: 999px; background: rgba(255, 255, 255, .14); }
.chatw__avatar .icon { width: 20px; height: 20px; }
.chatw.is-agent .chatw__avatar { background: var(--success); }
.chatw__who { flex: 1; min-width: 0; }
.chatw__title { margin: 0; font-family: var(--font-heading); font-size: 1rem; font-weight: 700; color: #fff; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatw__status { display: flex; align-items: center; gap: .4rem; margin: .1rem 0 0; font-size: .75rem; color: rgba(255, 255, 255, .75); }
.chatw__dot { width: 8px; height: 8px; border-radius: 999px; background: #34d399; box-shadow: 0 0 0 3px rgba(52, 211, 153, .25); }
.chatw__dot.is-waiting { background: var(--amber); box-shadow: 0 0 0 3px rgba(255, 176, 32, .25); animation: chatw-pulse 1.4s infinite; }
@keyframes chatw-pulse { 50% { opacity: .35; } }
.chatw__icon-btn { display: grid; place-items: center; width: 34px; height: 34px; border: 0; border-radius: 10px; background: transparent; color: rgba(255, 255, 255, .8); cursor: pointer; }
.chatw__icon-btn:hover { background: rgba(255, 255, 255, .12); color: #fff; }
.chatw__icon-btn .icon { width: 18px; height: 18px; }
.chatw__icon-btn[hidden] { display: none; }

.chatw__body { flex: 1; min-height: 0; overflow-y: auto; padding: 1rem; background: var(--surface); overscroll-behavior: contain; scroll-behavior: smooth; }
.chatw__log, .chatw__intro { display: grid; gap: .6rem; }
.chatw__log { margin-top: .6rem; }
.chatw__msg { display: grid; justify-items: start; gap: .25rem; max-width: 86%; }
.chatw__bubble { display: block; padding: .6rem .85rem; border-radius: 16px; font-size: .9rem; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; text-align: start; }
.chatw__bubble[dir="rtl"] { line-height: 1.7; }
.chatw__bubble a { color: inherit; font-weight: 700; text-decoration: underline; text-underline-offset: 2px; }
.chatw__msg--bot .chatw__bubble, .chatw__msg--agent .chatw__bubble { background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 5px; color: var(--ink); }
.chatw__msg--bot .chatw__bubble a, .chatw__msg--agent .chatw__bubble a { color: var(--brand); }
.chatw__msg--customer { justify-self: end; justify-items: end; }
.chatw__msg--customer .chatw__bubble { background: var(--brand); color: #fff; border-bottom-right-radius: 5px; }
.chatw__msg--customer.is-pending .chatw__bubble { opacity: .7; }
.chatw__msg--customer.is-failed .chatw__bubble { background: var(--danger); }
.chatw__msg--system { justify-self: center; max-width: 94%; }
.chatw__msg--system .chatw__bubble { padding: .35rem .75rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: .76rem; text-align: center; }
.chatw__name { padding-left: .25rem; font-size: .7rem; font-weight: 700; color: var(--muted); }
.chatw__offer { display: inline-flex; align-items: center; gap: .35rem; padding: .45rem .85rem; border: 1.5px solid var(--brand); border-radius: 999px; background: #fff; color: var(--brand); font: inherit; font-size: .8rem; font-weight: 800; cursor: pointer; }
.chatw__offer:hover { background: var(--brand); color: #fff; }

.chatw__chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chatw__chips[hidden] { display: none; }
.chatw__chip { display: inline-flex; align-items: center; gap: .3rem; padding: .4rem .75rem; border: 1px solid var(--line); border-radius: 999px; background: #fff; color: var(--ink-2); font: inherit; font-size: .78rem; font-weight: 700; cursor: pointer; transition: border-color .15s, color .15s; }
.chatw__chip:hover { border-color: var(--brand); color: var(--brand); }
.chatw__chip .icon { width: 14px; height: 14px; }
.chatw__chip--agent { border-color: var(--brand-100); background: var(--brand-50); color: var(--brand); }

.chatw__typing { display: inline-flex; gap: 4px; margin-top: .6rem; padding: .7rem .9rem; border: 1px solid var(--line); border-radius: 16px; border-bottom-left-radius: 5px; background: #fff; }
.chatw__typing[hidden] { display: none; }
.chatw__typing span { width: 7px; height: 7px; border-radius: 999px; background: var(--subtle); animation: chatw-dot 1.2s infinite; }
.chatw__typing span:nth-child(2) { animation-delay: .15s; }
.chatw__typing span:nth-child(3) { animation-delay: .3s; }
@keyframes chatw-dot { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }

.chatw__agent-form { display: grid; gap: .6rem; margin-top: .75rem; padding: 1rem; border: 1px solid var(--brand-100); border-radius: 16px; background: #fff; box-shadow: var(--shadow-sm); }
.chatw__agent-form[hidden] { display: none; }
.chatw__agent-title { display: flex; align-items: center; gap: .4rem; margin: 0; font-family: var(--font-heading); font-weight: 700; color: var(--ink); }
.chatw__agent-title .icon { width: 18px; height: 18px; color: var(--brand); }
.chatw__agent-text { margin: 0; font-size: .8rem; color: var(--muted); }
.chatw__field { display: grid; gap: .25rem; font-size: .75rem; font-weight: 700; color: var(--ink-2); }
.chatw__field small { font-weight: 400; color: var(--muted); }
.chatw__field input { width: 100%; min-height: 40px; padding: .5rem .75rem; border: 1px solid var(--line); border-radius: 10px; background: #fff; font: inherit; font-size: .9rem; color: var(--ink); }
.chatw__field input:focus { outline: none; border-color: var(--brand); box-shadow: var(--ring); }
.chatw__error { margin: 0; font-size: .78rem; color: var(--danger); }
.chatw__error[hidden] { display: none; }
.chatw__agent-actions { display: flex; align-items: center; justify-content: flex-end; gap: .75rem; }

.chatw__ended { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .75rem 1rem; border-top: 1px solid var(--line); font-size: .85rem; color: var(--muted); }
.chatw__ended[hidden], .chatw__composer[hidden] { display: none; }
.chatw__composer { display: flex; align-items: flex-end; gap: .5rem; padding: .7rem .75rem; border-top: 1px solid var(--line); background: var(--bg); }
.chatw__composer textarea { flex: 1; min-height: 42px; max-height: 120px; padding: .6rem .85rem; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); font: inherit; font-size: .92rem; line-height: 1.4; color: var(--ink); resize: none; }
.chatw__composer textarea:focus { outline: none; border-color: var(--brand); background: #fff; box-shadow: var(--ring); }
.chatw__send { display: grid; place-items: center; flex-shrink: 0; width: 42px; height: 42px; border: 0; border-radius: 12px; background: var(--brand); color: #fff; cursor: pointer; transition: background-color .15s; }
.chatw__send:hover { background: var(--brand-600); }
.chatw__send:disabled { opacity: .55; cursor: default; }
.chatw__send .icon { width: 18px; height: 18px; }
.chatw__foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .25rem .75rem; margin: 0; padding: 0 1rem .65rem; background: var(--bg); font-size: .68rem; color: var(--subtle); }
.chatw__link { display: inline-flex; align-items: center; gap: .3rem; padding: 0; border: 0; background: none; color: var(--brand); font: inherit; font-weight: 800; font-size: .75rem; cursor: pointer; }
.chatw__link:hover { text-decoration: underline; }
.chatw__link .icon { width: 14px; height: 14px; }
.chatw__link[hidden] { display: none; }

@media (max-width: 1023px) {
    .chatw { right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); }
    .back-to-top { bottom: calc(148px + env(safe-area-inset-bottom)); }
    .chatw__launcher { width: 54px; height: 54px; }
}
@media (max-width: 639px) {
    .chatw.is-open { inset: 0; right: 0; bottom: 0; z-index: 80; }
    .chatw.is-open .chatw__launcher { display: none; }
    .chatw__panel { position: fixed; inset: 0; width: 100%; height: 100%; height: 100dvh; border: 0; border-radius: 0; }
    .chatw__head { padding-top: calc(.9rem + env(safe-area-inset-top)); }
    .chatw__foot { padding-bottom: calc(.65rem + env(safe-area-inset-bottom)); }
}
@media (prefers-reduced-motion: reduce) {
    .chatw__panel, .chatw__typing span, .chatw__dot.is-waiting { animation: none; }
}
