/* ============================================
   RAFAEL — Comic Neobrutalism Chat Widget
   Ported from stitch: mangacomicke_chat_widget_animated
   ============================================ */

/* ── Fonts ──
   No @import here. Bangers and Inter already arrive with styles.css (and the
   per-page <link>s), so this only duplicated them — and it dragged in Material
   Symbols Outlined, 361KB of woff2 for two icons, behind a render-blocking
   import chain on every page. Those icons are inline SVG now (see rafael.js). */

/* ── Tokens ── */
:root {
  --r-red:       #bb0018;
  --r-red-vivid: #ff3e3e;
  --r-yellow:    #facc15;
  --r-black:     #000000;
  --r-white:     #ffffff;
  --r-surface:   #f3f4f6;
  --r-surface-lo:#f8f9fb;
  --r-grey:      #edeef0;
  --r-text:      #191c1e;
  --r-muted:     #5c5c5c;
}

/* ── Shadows ── */
.r-shadow    { box-shadow: 4px 4px 0px 0px var(--r-black); }
.r-shadow-sm { box-shadow: 2px 2px 0px 0px var(--r-black); }

/* ── Root container ── */
#rafael-root {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 9999;
  width: 60px;
  height: 60px;
  font-family: 'Inter', sans-serif;
}

/* ══════════════════════════════════════════
   TRIGGER BUTTON
══════════════════════════════════════════ */
#rafael-trigger {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--r-red);
  border: 2px solid var(--r-black);
  box-shadow: 4px 4px 0px 0px var(--r-black);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  animation: rafael-float 3s ease-in-out infinite;
  transition: transform 0.1s, box-shadow 0.1s;
}

#rafael-trigger:active {
  transform: translate(2px, 2px) !important;
  box-shadow: 2px 2px 0px 0px var(--r-black) !important;
  animation: none;
}

/* Launcher is hidden while the chat panel is open (JS toggles this class) so it
   never covers the composer's Send button or other bottom-corner controls. */
#rafael-trigger.rafael-hidden {
  opacity: 0 !important;
  visibility: hidden;
  pointer-events: none;
  transform: scale(0.9);
  animation: none !important;
  transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s ease;
}

@keyframes rafael-float {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-5px); }
}

#rafael-trigger-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--r-black);
  background: var(--r-white);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#rafael-trigger-avatar img,
#rafael-trigger-avatar svg {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

/* Badge */
#rafael-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 22px;
  height: 22px;
  background: var(--r-yellow);
  border: 2px solid var(--r-black);
  border-radius: 50%;
  font-family: 'Bangers', cursive;
  font-size: 11px;
  color: var(--r-black);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
}

#rafael-badge.visible { opacity: 1; }

/* ══════════════════════════════════════════
   CHAT PANEL
══════════════════════════════════════════ */
#rafael-panel {
  position: absolute;
  bottom: 72px;
  right: 0;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: 540px;
  max-height: calc(100vh - 100px);
  background: var(--r-surface);
  border: 2px solid var(--r-black);
  box-shadow: 4px 4px 0px 0px var(--r-black);
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transform: scale(0.95);
  transform-origin: bottom right;
  transition: opacity 0.18s ease-out, transform 0.18s ease-out;
}

#rafael-panel.open {
  opacity: 1;
  pointer-events: all;
  transform: scale(1);
}

/* ── Header ── */
#rafael-header {
  background: var(--r-black);
  color: var(--r-white);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 2px solid var(--r-black);
  flex-shrink: 0;
}

#rafael-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

#rafael-header-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}

#rafael-header-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 2px solid var(--r-red-vivid);
  background: var(--r-white);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#rafael-header-avatar img,
#rafael-header-avatar svg {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#rafael-online-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 12px;
  height: 12px;
  background: #22c55e;
  border: 2px solid var(--r-black);
  border-radius: 50%;
}

#rafael-name {
  font-family: 'Bangers', cursive;
  font-size: 20px;
  letter-spacing: 2px;
  color: var(--r-red-vivid);
  line-height: 1;
}

#rafael-subtitle {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #9ca3af;
  margin-top: 2px;
}

#rafael-close {
  background: none;
  border: none;
  color: var(--r-white);
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 2px;
  transition: background 0.15s;
  font-size: 0;
}

#rafael-close:hover { background: #27272a; }

#rafael-close .r-icon {
  width: 20px;
  height: 20px;
  color: var(--r-white);
}

/* ── Messages area ── */
#rafael-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  background: var(--r-surface);
}

#rafael-messages::-webkit-scrollbar { width: 4px; }
#rafael-messages::-webkit-scrollbar-track { background: transparent; }
#rafael-messages::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 2px; }

/* Bubble wrappers — avatar + content sit side by side */
.rafael-msg {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 88%;
}

.rafael-msg.rafael { align-self: flex-start; }
.rafael-msg.user   { align-self: flex-end; flex-direction: row-reverse; }

/* Per-message avatar (Raphael, or the user's own profile pic / default) */
.rafael-msg-avatar {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 2px solid var(--r-black);
  background: var(--r-white);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.rafael-msg-avatar img,
.rafael-msg-avatar svg {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.rafael-msg-content {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.rafael-msg.rafael .rafael-msg-content { align-items: flex-start; }
.rafael-msg.user   .rafael-msg-content { align-items: flex-end; }

/* Sender label */
.rafael-label {
  font-family: 'Bangers', cursive;
  font-size: 10px;
  letter-spacing: 1px;
  color: var(--r-muted);
  margin-bottom: 3px;
}

.rafael-msg.user .rafael-label { color: var(--r-red); }

/* Bubbles */
.rafael-bubble {
  padding: 10px 13px;
  border: 2px solid var(--r-black);
  border-radius: 4px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
}

.rafael-msg.rafael .rafael-bubble {
  background: var(--r-white);
  color: var(--r-text);
  box-shadow: 2px 2px 0px 0px var(--r-black);
}

.rafael-msg.user .rafael-bubble {
  background: var(--r-red-vivid);
  color: var(--r-white);
  font-weight: 500;
  box-shadow: 2px 2px 0px 0px var(--r-black);
}

/* Bubble entrance animations */
.rafael-msg.rafael { animation: slide-in-left 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; }
.rafael-msg.user   { animation: slide-in-right 0.3s ease-out forwards; }

@keyframes slide-in-left {
  0%   { opacity: 0; transform: translateX(-20px) scale(0.95); }
  70%  { transform: translateX(4px) scale(1.01); }
  100% { opacity: 1; transform: translateX(0) scale(1); }
}

@keyframes slide-in-right {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* Typing indicator */
#rafael-typing-wrap {
  align-self: flex-start;
}

.rafael-typing-dots {
  background: var(--r-white);
  border: 2px solid var(--r-black);
  box-shadow: 2px 2px 0px 0px var(--r-black);
  border-radius: 999px;
  padding: 9px 14px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.rafael-typing-dots span {
  width: 6px;
  height: 6px;
  background: var(--r-red-vivid);
  border-radius: 50%;
  animation: dot-pulse 0.6s infinite ease-in-out;
}

.rafael-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.rafael-typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dot-pulse {
  0%, 100% { opacity: 0.3; transform: scale(0.8); }
  50%       { opacity: 1;   transform: scale(1.2); }
}

/* ── Quick chips ── */
#rafael-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  padding: 4px 16px 12px;
  background: var(--r-surface);
  flex-shrink: 0;
}

.rafael-chip {
  padding: 5px 14px;
  background: var(--r-yellow);
  border: 2px solid var(--r-black);
  border-radius: 999px;
  box-shadow: 2px 2px 0px 0px var(--r-black);
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  cursor: pointer;
  color: var(--r-black);
  transition: transform 0.15s, box-shadow 0.15s;
}

.rafael-chip:hover {
  transform: scale(1.05);
  box-shadow: 4px 4px 0px 0px var(--r-black);
}

.rafael-chip:active {
  transform: translate(2px, 2px);
  box-shadow: 0px 0px 0px 0px var(--r-black);
}

/* ── Input row ── */
#rafael-input-row {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 2px solid var(--r-black);
  background: var(--r-white);
  flex-shrink: 0;
  align-items: center;
}

#rafael-input-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: var(--r-surface);
  border: 2px solid var(--r-black);
  box-shadow: 2px 2px 0px 0px var(--r-black);
  border-radius: 2px;
  transition: transform 0.1s, box-shadow 0.1s;
}

#rafael-input-wrap:focus-within {
  transform: translate(1px, 1px);
  box-shadow: 1px 1px 0px 0px var(--r-black);
}

#rafael-input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  font-family: 'Inter', sans-serif;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--r-text);
  line-height: 1.4;
}

#rafael-input::placeholder { color: #9ca3af; }

#rafael-send {
  width: 38px;
  height: 38px;
  background: var(--r-red);
  border: 2px solid var(--r-black);
  border-radius: 2px;
  box-shadow: 2px 2px 0px 0px var(--r-black);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, box-shadow 0.1s;
  flex-shrink: 0;
  font-size: 0;
}

#rafael-send:hover:not(:disabled) {
  box-shadow: 3px 3px 0px 0px var(--r-black);
  transform: translate(-1px, -1px);
}

#rafael-send:active:not(:disabled) {
  transform: translate(2px, 2px);
  box-shadow: 0px 0px 0px 0px var(--r-black);
}

#rafael-send:disabled { opacity: 0.45; cursor: not-allowed; }

#rafael-send .r-icon {
  width: 18px;
  height: 18px;
  color: var(--r-white);
}

/* ── Header status text (typing) ── */
#rafael-subtitle.typing { color: var(--r-yellow); }

/* ── Mobile ──
   Breakpoint matches the site's mobile styles (820px, see styles.css).
   The panel is anchored to the VIEWPORT, not the trigger: the trigger gets
   lifted above the bottom nav (body.has-mobile-bottom-nav #rafael-root in
   styles.css), and a trigger-relative panel would overflow off the top of
   the screen. */
@media (max-width: 820px) {
  #rafael-root { bottom: 14px; right: 12px; }

  #rafael-panel {
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 12px);
    left: 12px;
    right: 12px;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 84px);
    width: auto;
    max-width: none;
    height: auto;
    max-height: none;
  }

  /* With the bottom nav present the panel sits just above it,
     covering the trigger (the panel has its own close button). */
  body.has-mobile-bottom-nav #rafael-panel {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 72px);
  }
}
