/* ============================================================
   CHATBOT WIDGET — v2 (fully responsive, production-ready)
   ------------------------------------------------------------
   Compatible with chatbot.js v2 (typing animation) + v3
   (keyboard detection + fullscreen toggle).
   ============================================================ */

/* ---------- Design tokens ---------- */
:root {
  --pcb-primary:       #7DD3C0;
  --pcb-primary-hover: #5FC4AE;
  --pcb-surface:       #20242F;
  --pcb-surface-soft:  #1C2029;
  --pcb-bg:            #15171E;
  --pcb-bg-elevated:   #1A1D26;
  --pcb-text:          #E8EBF0;
  --pcb-text-soft:     #B8BFCB;
  --pcb-text-muted:    #8A92A3;
  --pcb-border:        #2A2F3C;
  --pcb-border-strong: #363D4D;
  --pcb-radius:        12px;
  --pcb-radius-lg:     16px;

  /* Filled by JS on load — used for keyboard offset & layout */
  --pcb-topbar:        44px;
  --pcb-dock:          68px;
  --pcb-keyboard-offset: 0px;
}

/* ============================================================
   TOGGLE BUTTON
   ============================================================ */
.pcb-toggle {
  position: fixed;
  right: max(22px, env(safe-area-inset-right, 0px) + 8px);
  bottom: calc(var(--pcb-dock) + 22px);
  z-index: 9990;

  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: 1px solid var(--pcb-primary);
  background: var(--pcb-primary);
  color: var(--pcb-bg);
  font-size: 22px;

  display: flex;
  align-items: center;
  justify-content: center;

  cursor: pointer;
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.45),
    0 0 0 6px rgba(125, 211, 192, 0.14);

  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.pcb-toggle:hover {
  transform: translateY(-3px) scale(1.03);
  box-shadow:
    0 12px 32px rgba(0, 0, 0, 0.5),
    0 0 0 8px rgba(125, 211, 192, 0.2);
}

.pcb-toggle.pcb-active i::before { content: "\f62a"; } /* bi-x-lg */

.pcb-toggle-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #E8A0A8;
  color: #15171E;
  font-size: 10px;
  font-weight: 700;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--pcb-bg);
}

/* ============================================================
   PANEL
   ============================================================ */
.pcb-panel {
  position: fixed;
  right: max(22px, env(safe-area-inset-right, 0px) + 8px);
  bottom: calc(var(--pcb-dock) + 22px + 54px + 14px);
  z-index: 9989;

  width: min(380px, calc(100vw - 32px));
  height: min(560px, calc(100vh - 200px));
  height: min(560px, calc(100dvh - var(--pcb-topbar) - var(--pcb-dock) - 100px));

  background: var(--pcb-bg-elevated);
  border: 1px solid var(--pcb-border-strong);
  border-radius: var(--pcb-radius-lg);

  display: flex;
  flex-direction: column;
  overflow: hidden;

  box-shadow:
    0 24px 64px rgba(0, 0, 0, 0.55),
    0 0 0 1px rgba(125, 211, 192, 0.06);

  opacity: 0;
  transform: translateY(16px) scale(0.98);
  visibility: hidden;
  pointer-events: none;

  transition: opacity 0.22s ease, transform 0.22s ease, visibility 0.22s;

  /* iOS safe-area bottom padding */
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.pcb-panel.pcb-open {
  opacity: 1;
  transform: translateY(0) scale(1);
  visibility: visible;
  pointer-events: auto;
}

/* Fallback for very old browsers that ignore dvh */
@supports not (height: 100dvh) {
  .pcb-panel {
    height: min(560px, calc(100vh - 200px));
  }
}

/* ============================================================
   HEADER
   ============================================================ */
.pcb-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--pcb-border);
  background: var(--pcb-surface);
  flex-shrink: 0;
  gap: 8px;
}

.pcb-header-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0; /* allow text truncation */
}

.pcb-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(125, 211, 192, 0.15);
  border: 1px solid rgba(125, 211, 192, 0.4);
  color: var(--pcb-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}

.pcb-header-info > div {
  min-width: 0;
}

.pcb-header-info strong {
  display: block;
  color: var(--pcb-text);
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pcb-status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.7rem;
  color: #86C89A;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
  margin-top: 2px;
}

.pcb-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #86C89A;
  box-shadow: 0 0 6px #86C89A;
}

/* Header action buttons (close, expand) */
.pcb-close {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--pcb-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease, border-color 0.18s ease;
  flex-shrink: 0;
}

.pcb-close:hover {
  background: var(--pcb-surface-soft);
  border-color: var(--pcb-border);
  color: var(--pcb-text);
}

/* ============================================================
   MESSAGES AREA
   ============================================================ */
.pcb-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background:
    radial-gradient(600px 300px at 0% 0%, rgba(125, 211, 192, 0.04), transparent 60%),
    var(--pcb-bg);
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
}

.pcb-msg {
  display: flex;
  flex-wrap: wrap;
}

.pcb-msg-bot  { justify-content: flex-start; }
.pcb-msg-user { justify-content: flex-end; }

/* ============================================================
   BUBBLES
   ============================================================ */
.pcb-bubble {
  max-width: 82%;
  padding: 11px 14px;
  border-radius: 14px;
  font-size: 0.88rem;
  line-height: 1.55;
  overflow-wrap: anywhere;
  word-break: break-word;
  animation: pcbFadeIn 0.25s ease;
}

.pcb-msg-bot .pcb-bubble {
  background: var(--pcb-surface);
  color: var(--pcb-text-soft);
  border: 1px solid var(--pcb-border);
  border-bottom-left-radius: 4px;
}

.pcb-msg-user .pcb-bubble {
  background: var(--pcb-primary);
  color: #15171E;
  font-weight: 500;
  border-bottom-right-radius: 4px;
}

@keyframes pcbFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ============================================================
   TYPING DOTS (before reply arrives)
   ============================================================ */
.pcb-typing {
  transition: opacity 0.15s ease;
}

.pcb-typing .pcb-bubble {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 14px 16px;
}

.pcb-typing .pcb-bubble span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--pcb-text-muted);
  animation: pcbDot 1.2s ease-in-out infinite;
}

.pcb-typing .pcb-bubble span:nth-child(2) { animation-delay: 0.15s; }
.pcb-typing .pcb-bubble span:nth-child(3) { animation-delay: 0.3s; }

@keyframes pcbDot {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30%           { opacity: 1;    transform: translateY(-3px); }
}

/* ============================================================
   TYPING ANIMATION (caret + meta fade-in)
   ============================================================ */
.pcb-caret {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--pcb-primary);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: pcbCaretBlink 1s step-end infinite;
  border-radius: 1px;
}

@keyframes pcbCaretBlink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0; }
}

.pcb-meta {
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}

.pcb-meta.pcb-meta-in {
  opacity: 1;
  transform: translateY(0);
}

/* ============================================================
   LINKS
   ============================================================ */
.pcb-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
  padding-left: 4px;
  width: 100%;
}

.pcb-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 10px;
  background: rgba(125, 211, 192, 0.12);
  border: 1px solid rgba(125, 211, 192, 0.35);
  color: var(--pcb-primary);
  font-size: 0.78rem;
  font-weight: 600;
  text-decoration: none;
  transition: background 0.18s ease, transform 0.18s ease;
  overflow-wrap: anywhere;
}

.pcb-link:hover {
  background: rgba(125, 211, 192, 0.2);
  transform: translateY(-1px);
}

/* ============================================================
   RELATED (projects + posts)
   ============================================================ */
.pcb-related {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--pcb-border);
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}

.pcb-related-title {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--pcb-text-muted);
  font-weight: 700;
  margin-bottom: 2px;
}

.pcb-related-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--pcb-surface-soft);
  border: 1px solid var(--pcb-border);
  color: var(--pcb-text-soft);
  text-decoration: none;
  transition: border-color 0.18s ease, background 0.18s ease;
  overflow-wrap: anywhere;
}

.pcb-related-item:hover {
  border-color: rgba(125, 211, 192, 0.4);
  background: var(--pcb-surface);
}

.pcb-related-item > i {
  color: var(--pcb-primary);
  margin-top: 2px;
  font-size: 0.95rem;
  flex-shrink: 0;
}

.pcb-related-item > div {
  min-width: 0;
  flex: 1;
}

.pcb-related-item strong {
  display: block;
  color: var(--pcb-text);
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.pcb-related-item small {
  display: block;
  color: var(--pcb-text-muted);
  font-size: 0.7rem;
  margin-top: 2px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

/* ============================================================
   SUGGESTION CHIPS
   ============================================================ */
.pcb-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  width: 100%;
}

.pcb-chip {
  padding: 6px 12px;
  border-radius: 14px;
  background: transparent;
  border: 1px solid var(--pcb-border);
  color: var(--pcb-text-soft);
  font-size: 0.76rem;
  cursor: pointer;
  font-family: inherit;
  transition: border-color 0.18s ease, color 0.18s ease, background 0.18s ease;
  overflow-wrap: anywhere;
  text-align: left;
}

.pcb-chip:hover {
  border-color: var(--pcb-primary);
  color: var(--pcb-primary);
  background: rgba(125, 211, 192, 0.06);
}

/* ============================================================
   INPUT FORM
   ============================================================ */
.pcb-form {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
  border-top: 1px solid var(--pcb-border);
  background: var(--pcb-surface);
  flex-shrink: 0;
}

.pcb-input {
  flex: 1;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--pcb-border);
  background: var(--pcb-bg);
  color: var(--pcb-text);
  font-family: inherit;
  font-size: 0.88rem;
  outline: none;
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
  min-width: 0;
}

.pcb-input:focus {
  border-color: var(--pcb-primary);
  box-shadow: 0 0 0 3px rgba(125, 211, 192, 0.15);
}

.pcb-input::placeholder { color: var(--pcb-text-muted); }

.pcb-send {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--pcb-primary);
  background: var(--pcb-primary);
  color: var(--pcb-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.95rem;
  transition: background 0.18s ease, transform 0.15s ease;
  flex-shrink: 0;
}

.pcb-send:hover:not(:disabled) {
  background: var(--pcb-primary-hover);
  transform: translateY(-1px);
}

.pcb-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ============================================================
   FULLSCREEN MODE (.pcb-expanded)
   ------------------------------------------------------------
   Added by the JS when the user clicks the expand button.
   Desktop: fills ~80% of the viewport.
   Mobile:  goes truly fullscreen (edge-to-edge).
   ============================================================ */
.pcb-panel.pcb-expanded {
  right: 12px !important;
  left: 12px !important;
  bottom: calc(var(--pcb-dock) + 12px) !important;
  top: calc(var(--pcb-topbar) + 12px) !important;
  width: auto !important;
  height: auto !important;
  max-height: none !important;
  border-radius: 14px;
}

@media (max-width: 600px) {
  .pcb-panel.pcb-expanded {
    right: 0 !important;
    left: 0 !important;
    top: calc(var(--pcb-topbar) + env(safe-area-inset-top, 0px)) !important;
    bottom: 0 !important;
    border-radius: 0;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .pcb-panel.pcb-expanded .pcb-header {
    border-radius: 0;
  }
}

/* ============================================================
   RESPONSIVE — TABLET (601px – 900px)
   ============================================================ */
@media (min-width: 601px) and (max-width: 900px) {
  .pcb-toggle {
    right: max(20px, env(safe-area-inset-right, 0px) + 6px);
    bottom: calc(var(--pcb-dock) + 20px);
    width: 52px;
    height: 52px;
    font-size: 21px;
  }

  .pcb-panel {
    right: max(20px, env(safe-area-inset-right, 0px) + 6px);
    bottom: calc(var(--pcb-dock) + 20px + 52px + 12px);
    width: min(420px, calc(100vw - 40px));
    height: min(600px, calc(100dvh - 220px));
  }

  .pcb-bubble {
    max-width: 85%;
    font-size: 0.9rem;
  }
}

/* ============================================================
   RESPONSIVE — MOBILE (≤ 600px)
   ============================================================ */
@media (max-width: 600px) {
  .pcb-toggle {
    right: max(14px, env(safe-area-inset-right, 0px) + 4px);
    bottom: calc(var(--pcb-dock) + 14px + env(safe-area-inset-bottom, 0px));
    width: 50px;
    height: 50px;
    font-size: 20px;
  }

  .pcb-panel {
    right: max(10px, env(safe-area-inset-right, 0px) + 4px);
    left:  max(10px, env(safe-area-inset-left, 0px) + 4px);
    bottom: calc(var(--pcb-dock) + 14px + 50px + 10px + env(safe-area-inset-bottom, 0px));
    width: auto;
    height: min(70dvh, 560px);
    border-radius: 16px;
  }

  .pcb-header {
    padding: 12px 14px;
    padding-top: max(12px, env(safe-area-inset-top, 0) + 6px);
  }

  .pcb-close {
    width: 44px;
    height: 44px;
    font-size: 1.15rem;
    margin: -6px -6px -6px 0;
  }

  .pcb-messages {
    padding: 14px 12px;
    gap: 10px;
  }

  .pcb-bubble {
    max-width: 88%;
    padding: 10px 13px;
    font-size: 0.9rem;
    line-height: 1.55;
  }

  .pcb-links,
  .pcb-related,
  .pcb-suggestions {
    gap: 8px;
  }

  .pcb-link {
    padding: 10px 14px;
    min-height: 40px;
    font-size: 0.82rem;
  }

  .pcb-related-item {
    padding: 10px 12px;
    min-height: 44px;
  }

  .pcb-related-item strong { font-size: 0.85rem; }
  .pcb-related-item small  { font-size: 0.74rem; }

  .pcb-chip {
    padding: 9px 14px;
    min-height: 40px;
    font-size: 0.8rem;
    border-radius: 20px;
  }

  .pcb-form {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0));
    gap: 8px;
  }

  /* 16px prevents iOS auto-zoom on focus */
  .pcb-input {
    padding: 12px 14px;
    font-size: 16px;
    border-radius: 12px;
  }

  .pcb-send {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    font-size: 1rem;
  }

  /* Keyboard-open state (added by JS) */
  .pcb-panel.pcb-keyboard-open {
    bottom: var(--pcb-keyboard-offset, 0px);
    height: min(55dvh, 480px);
    max-height: calc(100dvh - var(--pcb-keyboard-offset, 0px) - 20px);
  }

  .pcb-panel.pcb-keyboard-open .pcb-messages {
    padding-bottom: 8px;
  }
}

/* ============================================================
   RESPONSIVE — SMALL PHONES (≤ 400px)
   ============================================================ */
@media (max-width: 400px) {
  .pcb-toggle {
    width: 46px;
    height: 46px;
    font-size: 18px;
    right: max(12px, env(safe-area-inset-right, 0px) + 4px);
    bottom: calc(var(--pcb-dock) + 12px + env(safe-area-inset-bottom, 0px));
  }

  .pcb-panel {
    right: 6px;
    left: 6px;
    bottom: calc(var(--pcb-dock) + 12px + 48px + 8px + env(safe-area-inset-bottom, 0px));
    height: min(72dvh, calc(100dvh - 130px));
  }

  .pcb-header {
    padding: 10px 12px;
    padding-top: max(10px, env(safe-area-inset-top, 0) + 4px);
  }

  .pcb-avatar {
    width: 32px;
    height: 32px;
    font-size: 0.9rem;
  }

  .pcb-header-info strong { font-size: 0.82rem; }
  .pcb-status             { font-size: 0.66rem; }

  .pcb-bubble {
    font-size: 0.86rem;
    padding: 9px 12px;
  }

  .pcb-messages {
    padding: 12px 10px;
  }

  .pcb-chip {
    font-size: 0.76rem;
    padding: 8px 12px;
    min-height: 36px;
  }

  .pcb-input {
    padding: 11px 12px;
    font-size: 16px;
  }

  .pcb-send {
    width: 42px;
    height: 42px;
  }
}

/* ============================================================
   RESPONSIVE — TINY (≤ 340px)
   ============================================================ */
@media (max-width: 340px) {
  .pcb-toggle {
    width: 42px;
    height: 42px;
    font-size: 17px;
    right: max(10px, env(safe-area-inset-right, 0px) + 4px);
    bottom: calc(var(--pcb-dock) + 10px + env(safe-area-inset-bottom, 0px));
  }

  .pcb-panel {
    right: 4px;
    left: 4px;
    bottom: calc(var(--pcb-dock) + 10px + 44px + 6px + env(safe-area-inset-bottom, 0px));
    border-radius: 12px;
    height: min(74dvh, calc(100dvh - 120px));
  }

  .pcb-header { padding: 8px 10px; }
  .pcb-bubble { font-size: 0.84rem; padding: 8px 11px; }
  .pcb-form   { padding: 8px 10px; }
  .pcb-input  { font-size: 16px; padding: 10px 11px; }
  .pcb-send   { width: 40px; height: 40px; }
  .pcb-chip   { font-size: 0.74rem; padding: 7px 11px; }
}

/* ============================================================
   RESPONSIVE — LANDSCAPE PHONE (low height)
   ============================================================ */
@media (max-height: 500px) and (orientation: landscape) {
  .pcb-toggle {
    right: 12px;
    bottom: calc(var(--pcb-dock) + 12px);
    width: 44px;
    height: 44px;
    font-size: 18px;
  }

  .pcb-panel {
    right: 8px;
    left: auto;
    bottom: calc(var(--pcb-dock) + 8px + 48px + 6px);
    width: min(500px, calc(100vw - 24px));
    height: min(calc(100dvh - 100px), 380px);
    border-radius: 12px;
  }

  .pcb-header {
    padding: 8px 12px;
    padding-top: max(8px, env(safe-area-inset-top, 0) + 4px);
  }

  .pcb-messages { padding: 10px 12px; }
  .pcb-bubble   { font-size: 0.85rem; padding: 8px 12px; }
  .pcb-input    { padding: 9px 12px; font-size: 16px; }
  .pcb-send     { width: 40px; height: 40px; }
  .pcb-form     { padding: 8px 10px; }
}

/* ============================================================
   COARSE POINTER (touch devices) — 44px tap targets
   ============================================================ */
@media (hover: none) and (pointer: coarse) {
  .pcb-close,
  .pcb-send {
    min-width: 44px;
    min-height: 44px;
  }

  .pcb-link,
  .pcb-chip,
  .pcb-related-item {
    min-height: 44px;
  }

  .pcb-toggle {
    min-width: 46px;
    min-height: 46px;
  }

  /* Remove hover-only effects that feel wrong on touch */
  .pcb-link:hover,
  .pcb-related-item:hover,
  .pcb-chip:hover,
  .pcb-toggle:hover {
    transform: none;
  }
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .pcb-toggle,
  .pcb-panel,
  .pcb-bubble {
    transition: none;
    animation: none;
  }

  .pcb-typing .pcb-bubble span {
    animation: none;
    opacity: 0.6;
  }

  .pcb-caret {
    animation: none;
    opacity: 0.6;
  }

  .pcb-meta,
  .pcb-meta.pcb-meta-in {
    transition: none;
    opacity: 1;
    transform: none;
  }

  .pcb-typing {
    transition: none;
  }
}

/* ============================================================
   PRINT — hide the widget entirely
   ============================================================ */
@media print {
  .pcb-toggle,
  .pcb-panel {
    display: none !important;
  }
}


/* ============================================================
   MOBILE-ONLY TAP HINT (appears once on first open)
   ============================================================ */
.pcb-tap-hint {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  align-self: center;
  margin-top: 16px;
  padding: 8px 14px;
  border-radius: 20px;
  background: rgba(125, 211, 192, 0.08);
  border: 1px dashed rgba(125, 211, 192, 0.35);
  color: var(--pcb-primary);
  font-size: 0.78rem;
  font-weight: 500;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.pcb-tap-hint.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.pcb-tap-hint i {
  font-size: 0.9rem;
}

/* Hide hint on desktop — it's only for touch devices */
@media (hover: hover) and (pointer: fine) {
  .pcb-tap-hint { display: none; }
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  .pcb-tap-hint {
    transition: none;
    opacity: 1;
    transform: none;
  }
}