/* =====================================================================
   Floating live chat widget (scoped with .tbc-)
   Mobile: the panel is sized from window.visualViewport (set by chat.js
   as --tbc-h / --tbc-top) so the input always sits above the keyboard.
   ===================================================================== */
.tbc {
  --tbc-red: #e60000;
  --tbc-red-dark: #b80000;
  --tbc-red-soft: #fff1f0;
  --tbc-ink: #1a1a1a;
  --tbc-text: #2b2b2b;
  --tbc-muted: #7a7a7a;
  --tbc-line: #ececec;
  --tbc-bg: #f5f5f7;
  --tbc-radius: 20px;
  position: fixed; right: 24px; bottom: 24px; z-index: 1990;
  font-family: 'Poppins', system-ui, -apple-system, sans-serif; font-size: 14px; line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
.tbc *, .tbc *::before, .tbc *::after { box-sizing: border-box; }
.tbc [hidden] { display: none !important; }
.tbc button { font-family: inherit; }
.tbc-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Launcher ---------- */
.tbc-launcher {
  position: relative; width: 62px; height: 62px; border-radius: 50%; border: 0; cursor: pointer; padding: 0;
  background: linear-gradient(135deg, #ff1a1a, var(--tbc-red-dark)); color: #fff;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 12px 30px rgba(230,0,0,.35), 0 3px 8px rgba(0,0,0,.15);
  transition: transform .2s ease, box-shadow .2s ease;
}
.tbc-launcher:hover { transform: translateY(-2px) scale(1.04); box-shadow: 0 16px 36px rgba(230,0,0,.42); }
.tbc-launcher:focus-visible { outline: 3px solid rgba(230,0,0,.35); outline-offset: 4px; }
.tbc-launcher::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%; border: 2px solid var(--tbc-red);
  animation: tbcPulse 2.4s ease-out infinite; pointer-events: none;
}
.tbc.is-open .tbc-launcher::after, .tbc.has-opened .tbc-launcher::after { animation: none; opacity: 0; }
@keyframes tbcPulse { 0% { transform: scale(1); opacity: .65; } 100% { transform: scale(1.6); opacity: 0; } }

.tbc-launcher-icon { position: absolute; display: flex; transition: transform .25s ease, opacity .2s ease; }
.tbc-icon-close { opacity: 0; transform: rotate(-90deg) scale(.6); }
.tbc.is-open .tbc-icon-open { opacity: 0; transform: rotate(90deg) scale(.6); }
.tbc.is-open .tbc-icon-close { opacity: 1; transform: none; }

.tbc-badge {
  position: absolute; top: -3px; right: -3px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 999px;
  background: #fff; color: var(--tbc-red); font-size: 11px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(0,0,0,.25);
}

/* Small "Need help?" bubble next to the launcher (desktop, first visit) */
.tbc-hint {
  position: absolute; right: 74px; bottom: 12px; white-space: nowrap;
  background: #fff; color: var(--tbc-ink); font-size: 13px; font-weight: 600;
  padding: 9px 14px; border-radius: 14px 14px 4px 14px; box-shadow: 0 8px 24px rgba(0,0,0,.14);
  animation: tbcHint .35s ease .8s both; cursor: pointer;
}
.tbc.has-opened .tbc-hint { display: none; }
@keyframes tbcHint { from { opacity: 0; transform: translateX(8px); } to { opacity: 1; transform: none; } }

/* ---------- Panel ---------- */
.tbc-panel {
  position: absolute; right: 0; bottom: 80px;
  width: 380px; height: min(620px, calc(100vh - 130px)); height: min(620px, calc(100dvh - 130px));
  display: flex; flex-direction: column; background: #fff; border-radius: var(--tbc-radius); overflow: hidden;
  box-shadow: 0 28px 70px rgba(0,0,0,.22), 0 6px 16px rgba(0,0,0,.08);
  transform-origin: bottom right; animation: tbcIn .22s cubic-bezier(.2,.8,.2,1);
}
@keyframes tbcIn { from { opacity: 0; transform: translateY(14px) scale(.96); } to { opacity: 1; transform: none; } }

/* Header */
.tbc-head {
  flex: 0 0 auto; display: flex; align-items: center; gap: 12px; padding: 16px 14px 16px 18px; color: #fff;
  background: linear-gradient(120deg, #1a0000 0%, #7a0000 55%, var(--tbc-red) 100%);
}
.tbc-avatar {
  position: relative; flex: 0 0 44px; width: 44px; height: 44px; border-radius: 50%;
  background: #fff; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 0 2px rgba(255,255,255,.35), 0 2px 8px rgba(0,0,0,.25);
}
.tbc-avatar img { width: 64%; height: 64%; object-fit: contain; display: block; }
.tbc-online { position: absolute; right: 0; bottom: 1px; width: 12px; height: 12px; border-radius: 50%; background: #22c55e; border: 2px solid #6a0000; }
.tbc-online.is-off { background: #a3a3a3; }
.tbc-head-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.tbc-head-text strong { font-size: 15px; font-weight: 700; color: #fff; line-height: 1.3; }
.tbc-head-text span { font-size: 12px; color: rgba(255,255,255,.85); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tbc-close {
  flex: 0 0 38px; width: 38px; height: 38px; border-radius: 50%; border: 0; cursor: pointer; padding: 0;
  background: rgba(255,255,255,.14); color: #fff; display: flex; align-items: center; justify-content: center;
  transition: background .2s;
}
.tbc-close:hover { background: rgba(255,255,255,.28); }

/* Messages (min-height:0 is required so the flex child can scroll) */
.tbc-body {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden;
  padding: 18px 14px 10px; background: var(--tbc-bg);
  display: flex; flex-direction: column; gap: 4px;
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  scrollbar-width: thin; scrollbar-color: #d4d4d4 transparent;
}
.tbc-body::-webkit-scrollbar { width: 6px; }
.tbc-body::-webkit-scrollbar-thumb { background: #d4d4d4; border-radius: 6px; }
.tbc-day { align-self: center; font-size: 11px; font-weight: 600; color: var(--tbc-muted); background: #ebebee; padding: 3px 10px; border-radius: 999px; margin: 2px 0 10px; }

.tbc-msg { display: flex; flex-direction: column; max-width: 80%; margin-top: 6px; animation: tbcMsg .2s ease; }
.tbc-msg.is-grouped { margin-top: 0; }
@keyframes tbcMsg { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: none; } }
.tbc-bubble {
  padding: 10px 14px; border-radius: 18px; font-size: 14px; line-height: 1.5; color: var(--tbc-text);
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word;
}
.tbc-msg.from-bot, .tbc-msg.from-agent { align-self: flex-start; }
.tbc-msg.from-bot .tbc-bubble, .tbc-msg.from-agent .tbc-bubble {
  background: #fff; border-top-left-radius: 6px; box-shadow: 0 1px 2px rgba(0,0,0,.06);
}
.tbc-msg.from-agent .tbc-bubble { background: #fff; box-shadow: inset 3px 0 0 var(--tbc-red), 0 1px 2px rgba(0,0,0,.06); }
.tbc-msg.from-visitor { align-self: flex-end; align-items: flex-end; }
.tbc-msg.from-visitor .tbc-bubble {
  background: linear-gradient(135deg, #ff1a1a, var(--tbc-red)); color: #fff; border-top-right-radius: 6px;
  box-shadow: 0 2px 6px rgba(230,0,0,.18);
}
.tbc-meta { font-size: 10.5px; color: var(--tbc-muted); margin: 3px 8px 2px; }
.tbc-msg.is-grouped-next .tbc-meta { display: none; }
.tbc-msg.is-error .tbc-bubble { background: #fff4f4; color: #a40000; box-shadow: inset 0 0 0 1px #ffd2d2; }

.tbc-typing {
  align-self: flex-start; display: flex; gap: 4px; padding: 13px 15px; margin-top: 6px;
  background: #fff; border-radius: 18px; border-top-left-radius: 6px; box-shadow: 0 1px 2px rgba(0,0,0,.06);
}
.tbc-typing span { width: 7px; height: 7px; border-radius: 50%; background: #b9b9b9; animation: tbcDot 1.2s infinite ease-in-out; }
.tbc-typing span:nth-child(2) { animation-delay: .15s; }
.tbc-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes tbcDot { 0%, 80%, 100% { transform: translateY(0); opacity: .5; } 40% { transform: translateY(-4px); opacity: 1; } }

/* Links inside bot / staff messages */
.tbc-bubble a.tbc-link { color: var(--tbc-red); font-weight: 600; text-decoration: underline; text-underline-offset: 2px; word-break: break-all; }

/* Quick replies */
.tbc-quick { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 4px; justify-content: flex-end; }
.tbc-chip {
  border: 1px solid rgba(230,0,0,.35); background: #fff; color: var(--tbc-red); cursor: pointer;
  font-size: 13px; font-weight: 600; padding: 8px 14px; border-radius: 18px; line-height: 1.3; text-align: left; max-width: 100%;
  transition: background .15s, color .15s, border-color .15s;
}
.tbc-chip:hover, .tbc-chip:focus-visible { background: var(--tbc-red); border-color: var(--tbc-red); color: #fff; outline: none; }

.tbc-new {
  align-self: center; margin: 12px 0 6px; padding: 9px 18px; border-radius: 999px; border: 1px solid var(--tbc-red);
  background: #fff; color: var(--tbc-red); font-weight: 600; font-size: 13px; cursor: pointer;
}
.tbc-new:hover { background: var(--tbc-red); color: #fff; }

/* Scroll-to-latest button */
.tbc-jump {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 96px; z-index: 2;
  width: 36px; height: 36px; border-radius: 50%; border: 0; cursor: pointer; padding: 0;
  background: #fff; color: var(--tbc-ink); box-shadow: 0 4px 14px rgba(0,0,0,.18);
  display: flex; align-items: center; justify-content: center;
}

/* Input bar */
.tbc-form {
  position: relative;
  flex: 0 0 auto; display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--tbc-line); background: #fff;
}
.tbc-input {
  flex: 1; min-width: 0; min-height: 46px; max-height: 120px; padding: 12px 16px; resize: none; overflow-y: hidden;
  border-radius: 24px; border: 1px solid #e2e2e2; background: #f7f7f8;
  font: inherit; font-size: 15px; line-height: 1.4; color: var(--tbc-ink); outline: none;
  transition: border-color .15s, background .15s, box-shadow .15s;
}
.tbc-input::placeholder { color: #9a9a9a; }
.tbc-input:focus { border-color: var(--tbc-red); background: #fff; box-shadow: 0 0 0 3px rgba(230,0,0,.1); }
.tbc-input:disabled { opacity: .6; }
.tbc-send {
  flex: 0 0 46px; width: 46px; height: 46px; border-radius: 50%; border: 0; cursor: pointer; padding: 0;
  background: var(--tbc-red); color: #fff; display: flex; align-items: center; justify-content: center;
  transition: background .2s, transform .15s, opacity .2s;
}
.tbc-send:hover { background: var(--tbc-red-dark); }
.tbc-send:active { transform: scale(.92); }
.tbc-send:disabled { background: #d9d9d9; cursor: default; }
.tbc-count {
  position: absolute; right: 66px; top: -24px; font-size: 11px; font-weight: 600; color: var(--tbc-muted);
  background: #fff; padding: 2px 8px; border-radius: 999px; box-shadow: 0 1px 4px rgba(0,0,0,.12); pointer-events: none;
}
.tbc-count.is-max { color: var(--tbc-red); }
.tbc-foot { flex: 0 0 auto; margin: 0; padding: 0 12px 10px; font-size: 11px; color: var(--tbc-muted); text-align: center; background: #fff; }

/* ---------- Tablet / small laptop ---------- */
@media (max-width: 991.98px) {
  .tbc { right: 18px; bottom: 18px; }
}

/* ---------- Phone: full-screen sheet that follows the visible viewport ---------- */
@media (max-width: 575.98px) {
  .tbc { right: 14px; bottom: calc(14px + env(safe-area-inset-bottom)); }
  .tbc-launcher { width: 56px; height: 56px; }
  .tbc-hint { display: none; }
  .tbc.is-open .tbc-launcher { display: none; }

  .tbc-panel {
    position: fixed; left: 0; right: 0; bottom: auto;
    top: var(--tbc-top, 0px);
    width: 100%;
    height: 100vh; height: 100dvh; height: var(--tbc-h, 100dvh);
    border-radius: 0; box-shadow: none;
    animation: tbcUp .25s cubic-bezier(.2,.8,.2,1);
  }
  .tbc-head { padding: max(12px, env(safe-area-inset-top)) 12px 12px 14px; }
  .tbc-avatar { flex-basis: 40px; width: 40px; height: 40px; }
  .tbc-body { padding: 14px 12px 8px; }
  .tbc-msg { max-width: 85%; }
  .tbc-bubble { font-size: 15px; }
  .tbc-form { padding: 8px 10px; }
  .tbc.kb-closed .tbc-form { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .tbc-input { font-size: 16px; min-height: 44px; padding: 11px 15px; }  /* 16px stops iOS auto-zoom */
  .tbc-send { flex-basis: 44px; width: 44px; height: 44px; }
  .tbc-foot { display: none; }
  .tbc.kb-closed .tbc-foot { display: block; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .tbc-jump { bottom: 76px; }
}
@keyframes tbcUp { from { transform: translateY(100%); } to { transform: none; } }

/* Page behind the chat must not scroll on phones */
html.tbc-lock, html.tbc-lock body { overflow: hidden !important; overscroll-behavior: none; }
html.tbc-lock body { position: fixed; left: 0; right: 0; width: 100%; }

@media (prefers-reduced-motion: reduce) {
  .tbc-launcher::after, .tbc-panel, .tbc-msg, .tbc-typing span, .tbc-hint { animation: none !important; }
  .tbc-launcher, .tbc-launcher-icon { transition: none; }
}
@media print { .tbc { display: none !important; } }
