/* ============================================================================
   ib-chat.css — floating chat bubble (messenger) + notifications popup.
   Framework-agnostic, loaded on every surface alongside ib-nav (static pages via
   patch-nav, Next via app/layout). Dark is the default; overrides. Mounted/controlled by public/ib-chat.js.
   ============================================================================ */

:root { --ibc-teal: var(--ib-cta-accent); }

/* ── Floating action button (bubble) ── */
.ib-chat-fab {
  position: fixed; right: 14px; bottom: 26px; z-index: 320;
  width: 42px; height: 42px; border-radius: 10px; cursor: pointer;
  display: none; align-items: center; justify-content: center;
  /* Frosted teal glass */
  background: rgba(15,188,173,0.18); color: #0fbcad;
  border: 1px solid rgba(15,188,173,0.40);
  -webkit-backdrop-filter: blur(14px) saturate(140%); backdrop-filter: blur(14px) saturate(140%);
  box-shadow: 0 8px 26px rgba(0,0,0,0.32), inset 0 1px 0 rgba(255,255,255,0.22);
  transition: transform .18s ease, box-shadow .18s ease;
  -webkit-tap-highlight-color: transparent;
}
.ib-chat-fab.show { display: flex; }
/* Cycle plotter has no calculator sticky action bar (the reason the bubble is
   raised to 118px on mobile elsewhere), so drop it much lower on that page. */
body.ib-plotter-page .ib-chat-fab { bottom: 16px; }
.ib-chat-fab:hover { transform: translateY(-2px); box-shadow: 0 14px 34px rgba(0,0,0,0.5); }
.ib-chat-fab svg { width: 20px; height: 20px; }
.ib-chat-fab-badge {
  position: absolute; top: -3px; right: -3px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 999px; background: #ff5757; color: #fff; font: 700 11px/20px 'Inter',sans-serif;
  text-align: center; box-shadow: 0 0 0 2px #0e0e10; display: none;
}
.ib-chat-fab-badge.show { display: block; }

/* ── Backdrop behind the open chat (mobile) + hide the bubble while chatting ── */
.ib-chat-overlay { position: fixed; inset: 0; z-index: 325; display: none; background: rgba(0,0,0,0.45); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); }
body.ib-chat-open .ib-chat-fab { display: none !important; }
@media (max-width: 767px) { body.ib-chat-open .ib-chat-overlay { display: block; } }

/* ── Chat panel (messenger) ── */
.ib-chat-panel {
  position: fixed; right: 20px; bottom: 88px; z-index: 330;
  width: 380px; max-width: calc(100vw - 32px); height: 560px; max-height: calc(100dvh - 120px);
  display: none; flex-direction: column; overflow: hidden;
  background: rgba(16,17,20,0.70); border: 1px solid rgba(255,255,255,0.12); border-radius: 14px;
  box-shadow: 0 24px 60px rgba(0,0,0,0.55), inset 0 1px 0 rgba(255,255,255,0.08);
  -webkit-backdrop-filter: blur(18px); backdrop-filter: blur(18px);
}
.ib-chat-panel.open { display: flex; }
@media (max-width: 767px) {
  /* Raised on mobile so the bubble clears the calculators' sticky action bar
     ("See your result" / "Save Dosage"). */
  .ib-chat-fab { right: 16px; bottom: 118px; }
  .ib-chat-panel { bottom: 182px; }
}
@media (max-width: 480px) {
  .ib-chat-panel { right: 8px; left: 8px; bottom: 182px; width: auto; max-width: none; height: calc(100dvh - 202px); }
}
.ib-chat-head {
  display: flex; align-items: center; gap: 10px; padding: 13px 14px;
  border-bottom: 1px solid rgba(255,255,255,0.08); flex-shrink: 0;
}
.ib-chat-head-ib { display: flex; align-items: center; justify-content: center; width: 34px; height: 34px; border-radius: 50%; background: rgba(15,188,173,0.16); color: var(--ibc-teal); flex-shrink: 0; }
.ib-chat-head-ib svg { width: 19px; height: 19px; }
.ib-chat-head-t { font: 600 14.5px 'Inter',sans-serif; color: var(--ib-fg); }
.ib-chat-head-s { font: 400 11.5px 'Inter',sans-serif; color: rgba(var(--ib-fg-rgb),0.5); margin-top: 1px; }
.ib-chat-x { background: none; border: none; color: rgba(var(--ib-fg-rgb),0.5); cursor: pointer; padding: 6px; line-height: 0; border-radius: 6px; }
.ib-chat-x:hover { color: var(--ib-fg); background: rgba(255,255,255,0.06); }
.ib-chat-x svg { width: 18px; height: 18px; }

.ib-chat-body { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; overscroll-behavior: contain; }
.ib-chat-empty { margin: auto; text-align: center; color: rgba(var(--ib-fg-rgb),0.4); font: 400 13.5px 'Inter',sans-serif; padding: 24px; }

/* messenger rows: avatar + bubble; mine (user) right, them (InjectBuddy) left */
.ib-cmsg { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; }
.ib-cmsg.mine { flex-direction: row-reverse; }
.ib-cmsg-av { width: 26px; height: 26px; border-radius: 50%; overflow: hidden; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.ib-cmsg-av img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ib-cmsg-av.ib-ib { background: rgba(15,188,173,0.16); color: var(--ibc-teal); }
.ib-cmsg-av.ib-ib svg { width: 15px; height: 15px; }
.ib-cmsg-av.ib-initials { background: rgba(15,188,173,0.16); color: var(--ibc-teal); font: 600 10px 'Inter',sans-serif; }
.ib-cbubble {
  max-width: 75%; padding: 8px 12px; border-radius: 14px; font: 400 14px/1.45 'Inter',sans-serif;
  white-space: pre-wrap; word-break: break-word;
}
.ib-cmsg.them .ib-cbubble { background: rgba(255,255,255,0.07); color: var(--ib-fg); border-bottom-left-radius: 4px; }
.ib-cmsg.mine .ib-cbubble { background: linear-gradient(140deg, rgba(15,188,173,0.95), rgba(15,188,173,0.78)); color: #04211d; border-bottom-right-radius: 4px; }

.ib-chat-foot { flex-shrink: 0; display: flex; gap: 8px; align-items: flex-end; padding: 10px; border-top: 1px solid rgba(255,255,255,0.08); }
.ib-chat-input {
  flex: 1; resize: none; max-height: 110px; min-height: 40px; box-sizing: border-box;
  background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.12); border-radius: 10px;
  padding: 10px 12px; color: var(--ib-fg); outline: none;
  /* 16px: iOS auto-zooms any focused input under 16px, which jerks the panel. */
  font: 400 16px/1.4 'Inter',sans-serif;
}
.ib-chat-input::placeholder { color: rgba(var(--ib-fg-rgb),0.4); }
.ib-chat-send {
  flex-shrink: 0; width: 40px; height: 40px; border-radius: 10px; border: none; cursor: pointer;
  background: linear-gradient(145deg, #12d0bf, #0c9e91); color: #04211d; display: flex; align-items: center; justify-content: center;
}
.ib-chat-send:disabled { opacity: 0.5; cursor: default; }
.ib-chat-send svg { width: 19px; height: 19px; }

/* ── Notifications popup (dropdown style) ── */
.ib-notify-panel {
  position: fixed; top: 56px; right: 16px; z-index: 420; width: 340px; max-width: calc(100vw - 24px);
  max-height: min(72vh, 560px); display: none; flex-direction: column; overflow: hidden;
  /* Shared frosted-popover surface (tokens in ib-nav.css :root); only the
     border-radius differs from .ib-dropdown-menu (8 vs 5). */
  background: var(--ib-popover-bg); border: var(--ib-popover-border); border-radius: 8px;
  box-shadow: var(--ib-popover-shadow);
  -webkit-backdrop-filter: var(--ib-popover-blur); backdrop-filter: var(--ib-popover-blur);
}
.ib-notify-panel.open { display: flex; }
.ib-notify-top { display: flex; align-items: center; padding: 12px 14px; border-bottom: 1px solid rgba(255,255,255,0.08); }
.ib-notify-top-t { font: 700 14px 'Inter',sans-serif; color: var(--ib-fg); }
.ib-notify-x { background: none; border: none; color: rgba(var(--ib-fg-rgb),0.5); cursor: pointer; padding: 4px; line-height: 0; }
.ib-notify-x:hover { color: var(--ib-fg); }
.ib-notify-x svg { width: 16px; height: 16px; }
.ib-notify-list { overflow-y: auto; padding: 6px; }
.ib-notify-empty { text-align: center; color: rgba(var(--ib-fg-rgb),0.4); font: 400 13px 'Inter',sans-serif; padding: 28px 16px; }
.ib-notify-item { display: block; width: 100%; text-align: left; padding: 10px 12px; border-radius: 6px; text-decoration: none; background: none; border: none; cursor: pointer; }
.ib-notify-item:hover { background: rgba(255,255,255,0.05); }
.ib-notify-item.unread { background: rgba(15,188,173,0.07); }
.ib-notify-row { display: flex; align-items: center; gap: 7px; }
.ib-notify-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ibc-teal); flex-shrink: 0; }
.ib-notify-kind { font: 700 9.5px 'Inter',sans-serif; letter-spacing: 0.05em; text-transform: uppercase; color: var(--ibc-teal); }
.ib-notify-when { margin-left: auto; font: 400 11px 'Inter',sans-serif; color: rgba(var(--ib-fg-rgb),0.4); }
.ib-notify-title { font: 600 13.5px 'Inter',sans-serif; color: var(--ib-fg); margin-top: 3px; }
.ib-notify-body { font: 400 12.5px/1.4 'Inter',sans-serif; color: rgba(var(--ib-fg-rgb),0.6); margin-top: 2px; white-space: pre-wrap; word-break: break-word; }

/* ── Badges on the nav (count chip in the menu item + avatar dot) ── */
.ib-notify-badge {
  display: none; margin-left: auto; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  background: #ff5757; color: #fff; font: 700 10.5px/18px 'Inter',sans-serif; text-align: center;
}
.ib-notify-badge.show { display: inline-block; }
/* the Notifications menu item is a flex row so the badge sits at the right edge */
.ib-dropdown-item[data-ib-notifications], .ib-rail-link[data-ib-notifications] { display: flex; align-items: center; gap: 8px; }

/* Suggestions is being phased out — hide its legacy floating bubble everywhere
   (the chat bubble replaces it). */
#ib-suggest-bubble { display: none !important; }
.ib-nav-pfp, .ib-rail-pfp { position: relative; }
.ib-avatar-btn.ib-has-notif::after, .ib-nav-pfp.ib-has-notif::after, .ib-rail-pfp.ib-has-notif::after {
  content: ''; position: absolute; top: -1px; right: -1px; width: 10px; height: 10px; border-radius: 50%;
  background: #ff5757; border: 2px solid #0e0e10;
}

@media (prefers-reduced-motion: reduce) { .ib-chat-fab { transition: none; } }

/* ── Light theme ── */
.ib-chat-panel { background: rgba(248,248,252,0.70); border-color: rgba(0,0,0,0.10); }
.ib-chat-head { border-bottom-color: rgba(0,0,0,0.08); }
.ib-chat-head-t { color: #1a1a1e; }
.ib-chat-head-s { color: rgba(20,20,28,0.5); }
.ib-chat-x { color: rgba(20,20,28,0.5); }
.ib-chat-x:hover { color: #1a1a1e; background: rgba(0,0,0,0.05); }
.ib-cmsg.them .ib-cbubble { background: rgba(0,0,0,0.05); color: #1a1a1e; }
.ib-chat-input { background: rgba(0,0,0,0.04); border-color: rgba(0,0,0,0.12); color: #1a1a1e; }
.ib-chat-input::placeholder { color: rgba(20,20,28,0.4); }
.ib-chat-foot { border-top-color: rgba(0,0,0,0.08); }
.ib-chat-fab { background: rgba(15,188,173,0.16); border-color: rgba(15,188,173,0.42); color: #0c8479; box-shadow: 0 8px 26px rgba(31,38,55,0.18), inset 0 1px 0 rgba(255,255,255,0.6); }
.ib-chat-fab-badge { box-shadow: 0 0 0 2px #f4f4f6; }
.ib-notify-panel { background: var(--ib-popover-bg); border-color: rgba(0,0,0,0.10); box-shadow: var(--ib-popover-shadow); }
.ib-notify-top { border-bottom-color: rgba(0,0,0,0.08); }
.ib-notify-top-t { color: #1a1a1e; }
.ib-notify-item:hover { background: rgba(0,0,0,0.04); }
.ib-notify-title { color: #1a1a1e; }
.ib-notify-body { color: rgba(20,20,28,0.6); }
.ib-avatar-btn.ib-has-notif::after, .ib-nav-pfp.ib-has-notif::after, .ib-rail-pfp.ib-has-notif::after { border-color: #f4f4f6; }

/* ── Desktop nav dropdowns: blur + dim the page behind an open menu ───────────
   Pure CSS via :has() — no JS, no markup change. Reuses the always-present
   .ib-rail-overlay element (built by nav-markup.js; display:none on desktop,
   where the rail is the fixed .ib-uni-rail). Hosted here rather than in its
   natural home ib-nav.css only because the nav files were under concurrent edit.
   The scrim sits at z-index 150: above page content (#ib-app z:1) and below the
   nav (.ib-nav z:200) and the open menu (.ib-dropdown-menu z:300), so the nav and
   the menu stay sharp while the page behind blurs. pointer-events:none preserves
   the existing hover / move-away-to-close behaviour. Desktop only (>=1024px). */
@media (min-width: 1024px) {
  ib-nav .ib-rail-overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 150;
    background: rgba(0, 0, 0, 0.18);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.16s ease, visibility 0s linear 0.16s;
  }
  ib-nav:has(.ib-dropdown-wrap.open) .ib-rail-overlay {
    opacity: 1;
    visibility: visible;
    transition: opacity 0.16s ease;
  }
}
