/* ==========================================================================
   Clay FAB + popover — phablet-style chat reachable from anywhere
   ==========================================================================
   The FAB (#clay-fab) sits at the bottom-right corner over all page
   content. Clicking it toggles #clay-popover, which contains the
   .home-chat-frame (rounded card with header / messages / input).
   Inspired by Vercel's persistent toolbar pattern. */

/* --- FAB ---
   Draggable. Default position is bottom-right; user position is
   restored from localStorage. Inline `top`/`left` set by JS override
   the default `right`/`bottom`. */

.clay-fab {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--bg, #fff);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.16), 0 2px 4px rgba(0, 0, 0, 0.08);
  cursor: grab;
  z-index: 9000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: box-shadow 0.18s, opacity 0.18s, transform 0.18s cubic-bezier(.2,.7,.3,1.3);
  user-select: none;
  touch-action: none;
}
.clay-fab:hover {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2), 0 3px 6px rgba(0, 0, 0, 0.10);
}
.clay-fab.dragging {
  cursor: grabbing;
  transition: none;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.26), 0 4px 8px rgba(0, 0, 0, 0.14);
}
.clay-fab.open {
  /* Hide while popover is open — the popover X button is the close path */
  transform: scale(0.6);
  opacity: 0;
  pointer-events: none;
}

.clay-fab-icon {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  pointer-events: none;
}

/* Subtle pulse ring to draw the eye on first paint */
.clay-fab-pulse {
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  opacity: 0;
  pointer-events: none;
  animation: clay-fab-pulse 2.4s ease-out 1s 2;
}
@keyframes clay-fab-pulse {
  0%   { opacity: 0;    transform: scale(1);   }
  20%  { opacity: 0.55; }
  100% { opacity: 0;    transform: scale(1.6); }
}

/* --- Popover --- */

.clay-popover {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: 320px;
  height: 480px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 40px);
  z-index: 9001;
  display: flex;
  transform-origin: bottom right;
  animation: clay-popover-in 0.18s cubic-bezier(.2,.7,.3,1.05);
}
.clay-popover.hidden {
  display: none;
}
@keyframes clay-popover-in {
  from { transform: translateY(8px) scale(0.96); opacity: 0; }
  to   { transform: translateY(0)    scale(1);    opacity: 1; }
}

.clay-popover .home-chat-frame {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-alt, var(--bg));
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.22), 0 4px 10px rgba(0, 0, 0, 0.10);
  overflow: hidden;
}

/* --- Header --- */

.home-chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 12px 12px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.home-chat-avatar-wrap {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
}

.home-chat-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.home-chat-title-block {
  flex: 1;
  min-width: 0;
}

.home-chat-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.2;
}

.home-chat-subtitle {
  font-size: 11px;
  color: var(--text-muted, var(--text-dimmer));
  line-height: 1.2;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.home-chat-icon-btn {
  background: none;
  border: none;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-muted, var(--text-dimmer));
  flex-shrink: 0;
}
.home-chat-icon-btn:hover {
  background: var(--hover-bg, rgba(0,0,0,0.05));
  color: var(--text);
}
.home-chat-icon-btn .lucide {
  width: 16px;
  height: 16px;
}

/* --- Messages list --- */

.home-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--input-bg, var(--bg));
  scroll-behavior: smooth;
}

.home-chat-bubble {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: 16px;
  font-size: 13px;
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
  box-shadow: 0 1px 1px rgba(0,0,0,0.04);
}

.home-chat-bubble-user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.home-chat-bubble-clay {
  align-self: flex-start;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.home-chat-bubble-system {
  align-self: center;
  font-size: 11px;
  color: var(--text-muted, var(--text-dimmer));
  background: transparent;
  padding: 4px 10px;
  font-style: italic;
  text-align: center;
  max-width: 100%;
}

.home-chat-ref {
  display: inline-block;
  background: rgba(124, 58, 237, 0.08);
  color: var(--accent);
  padding: 1px 8px;
  border-radius: 10px;
  font-family: "Roboto Mono", "Courier New", monospace;
  font-size: 11px;
  cursor: pointer;
  margin: 0 2px;
  border: 1px solid rgba(124, 58, 237, 0.18);
}
.home-chat-ref:hover {
  background: rgba(124, 58, 237, 0.16);
}

/* --- Typing indicator --- */

.home-chat-typing {
  display: flex;
  gap: 4px;
  padding: 4px 16px 0;
  align-items: center;
}
.home-chat-typing.hidden { display: none; }

.home-chat-typing-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-dimmer);
  animation: home-chat-bounce 1.2s infinite ease-in-out;
}
.home-chat-typing-dot:nth-child(1) { animation-delay: 0s; }
.home-chat-typing-dot:nth-child(2) { animation-delay: 0.15s; }
.home-chat-typing-dot:nth-child(3) { animation-delay: 0.30s; }

@keyframes home-chat-bounce {
  0%, 80%, 100% { transform: translateY(0);   opacity: 0.4; }
  40%           { transform: translateY(-4px); opacity: 1;   }
}

/* --- Input row --- */

.home-chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.home-chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 9px 12px;
  background: var(--input-bg, var(--bg));
  color: var(--text);
  font-size: 13px;
  line-height: 1.5;
  font-family: inherit;
  resize: none;
  max-height: 120px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.home-chat-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.18);
}

.home-chat-send-btn {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.home-chat-send-btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.home-chat-send-btn:not(:disabled):hover { opacity: 0.9; }
.home-chat-send-btn:not(:disabled):active { transform: scale(0.95); }
.home-chat-send-btn .lucide {
  width: 16px;
  height: 16px;
}

/* --- Mobile / narrow ---
   On phones the popover fills the screen edge-to-edge with a small
   inset, and the FAB shrinks slightly. */

@media (max-width: 768px) {
  /* Lift the FAB above the mobile tab bar (56px + safe-bottom).
     Only applies when JS hasn't placed the FAB at a custom position
     (custom positions use inline top/left and override these defaults). */
  .clay-fab:not(.user-positioned) {
    right: 14px;
    bottom: calc(56px + var(--safe-bottom, 0px) + 14px);
  }
  .clay-fab {
    width: 44px;
    height: 44px;
  }
  .clay-fab-icon {
    width: 30px;
    height: 30px;
  }
}

@media (max-width: 600px) {
  .clay-popover {
    right: 8px;
    bottom: calc(56px + var(--safe-bottom, 0px) + 8px);
    left: 8px;
    width: auto;
    height: 70vh;
    max-height: calc(100vh - 80px - var(--safe-bottom, 0px));
  }
  .clay-popover .home-chat-frame {
    border-radius: 16px;
  }
}

/* When the mobile tab bar is hidden by the keyboard, drop the FAB back
   down so it doesn't float in dead space. */
@media (max-width: 768px) {
  #mobile-tab-bar.keyboard-hidden ~ .clay-fab,
  body:has(#mobile-tab-bar.keyboard-hidden) .clay-fab {
    bottom: 14px;
  }
}

/* Hide FAB while user is on auth/setup pages or any modal-heavy screens.
   Add the class .clay-fab-suppressed on <body> when needed. */
body.clay-fab-suppressed .clay-fab,
body.clay-fab-suppressed .clay-popover {
  display: none !important;
}
