/* ==========================================================================
   AI assistant widget
   Phones: full-screen sheet. Tablet and up: docked panel, bottom right.
   ========================================================================== */

#chat-launcher {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 950;
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  height: 54px;
  padding: 0 1.15rem;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--accent-deep), var(--accent-3));
  color: var(--on-accent);
  font: inherit;
  font-weight: 700;
  font-size: .92rem;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(238,0,0,.34);
  transition: transform .18s var(--ease), box-shadow .18s var(--ease);
}
#chat-launcher:hover  { transform: translateY(-2px); box-shadow: 0 14px 36px rgba(238,0,0,.44); }
#chat-launcher:active { transform: translateY(0); }
#chat-launcher i { font-size: 1.05rem; }
#chat-launcher .label { display: none; }
@media (min-width: 480px) { #chat-launcher .label { display: inline; } }

/* Hide the launcher while the panel is open so it never overlaps the sheet. */
body.chat-open #chat-launcher { display: none; }

/* --------------------------------------------------------------- the panel */
#chat-panel {
  position: fixed;
  inset: 0;                       /* full screen on phones */
  z-index: 960;
  display: none;
  flex-direction: column;
  background: var(--bg-elev);
  border: 0;
  overflow: hidden;
}
#chat-panel.is-open { display: flex; }

body.chat-open { overflow: hidden; }   /* stop the page scrolling behind */

@media (min-width: 640px) {
  #chat-panel {
    inset: auto 1rem 1rem auto;
    width: 400px;
    height: min(620px, calc(100dvh - 2rem));
    border: 1px solid var(--line-strong);
    border-radius: 18px;
    box-shadow: var(--shadow-lg);
  }
  body.chat-open { overflow: auto; }
}

/* ---------------------------------------------------------------- header */
.chat-head {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .6rem .75rem;
  background: linear-gradient(135deg, rgba(255,61,61,.14), rgba(204,0,0,.12));
  border-bottom: 1px solid var(--line);
  flex: none;
}

/* Only the full-screen phone layout sits under the notch, so only it needs
   the safe-area inset. Applying it at every width pushed the header down
   inside the docked panel, leaving dead space above it. */
@media (max-width: 639px) {
  .chat-head { padding-top: max(.6rem, env(safe-area-inset-top)); }
}

.chat-avatar {
  display: grid; place-items: center;
  width: 38px; height: 38px; flex: none;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--accent-deep), var(--accent-3));
  color: var(--on-accent);
}
.chat-title { flex: 1; min-width: 0; line-height: 1.25; }
.chat-title strong { display: block; font-size: .95rem; }
.chat-title span {
  display: flex; align-items: center; gap: .35rem;
  font-size: .75rem; color: var(--text-muted);
}
.chat-title span::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: #34d399; flex: none;
}

.chat-close {
  width: 40px; height: 40px; flex: none;
  display: grid; place-items: center;
  background: transparent; border: 0;
  color: var(--text-muted); font-size: 1.2rem; cursor: pointer;
  border-radius: 10px;
}
.chat-close:hover { background: rgba(255,255,255,.07); color: var(--text); }

/* --------------------------------------------------------------- messages */
.chat-log {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;

  /* Firefox and Chrome 121+: transparent track, faint thumb. */
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, .16) transparent;
}

/* WebKit/Blink: the default light scrollbar looks pasted on over a dark
   panel, so the track is removed entirely and the thumb kept minimal. */
.chat-log::-webkit-scrollbar { width: 6px; }
.chat-log::-webkit-scrollbar-track { background: transparent; }
.chat-log::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, .16);
  border-radius: 999px;
}
.chat-log::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, .28); }
/* Stops the corner square rendering as an opaque block. */
.chat-log::-webkit-scrollbar-corner { background: transparent; }

.msg { display: flex; gap: .55rem; max-width: 95%; }
.msg-bot  { align-self: flex-start; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; }

.msg-bubble {
  padding: .7rem .9rem;
  border-radius: 14px;
  font-size: .9rem;
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: anywhere;      /* long URLs must not widen the panel */
}
.msg-bot .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
  color: var(--text);
}
.msg-user .msg-bubble {
  background: linear-gradient(135deg, var(--accent-deep), var(--accent-3));
  color: var(--on-accent);
  font-weight: 500;
  border-bottom-right-radius: 4px;
}
.msg-bubble p { margin: 0 0 .5rem; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble a { text-decoration: underline; }
.msg-user .msg-bubble a { color: var(--on-accent); }
.msg-bubble ul { margin: .35rem 0; padding-left: 1.1rem; }
.msg-bubble code {
  font-family: var(--font-mono); font-size: .82em;
  background: rgba(0,0,0,.35); padding: 1px 5px; border-radius: 5px;
}

.msg-error .msg-bubble {
  background: rgba(248,113,113,.12);
  border-color: rgba(248,113,113,.4);
  color: #fca5a5;
}

/* typing indicator */
.typing { display: flex; gap: 4px; padding: .85rem .9rem; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.3s infinite ease-in-out both;
}
.typing span:nth-child(2) { animation-delay: .18s; }
.typing span:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: scale(.8); } 40% { opacity: 1; transform: scale(1); } }

/* ------------------------------------------------------------- suggestions */
.chat-chips {
  display: flex; flex-wrap: wrap; gap: .35rem;
  padding: 0 .85rem .6rem;
  flex: none;
}
.chat-chips button {
  padding: .45rem .75rem;
  border-radius: 999px;
  background: rgba(255,255,255,.04);
  border: 1px solid var(--line-strong);
  color: var(--text-muted);
  font: inherit; font-size: .78rem;
  cursor: pointer;
  transition: border-color .18s var(--ease), color .18s var(--ease);
}
.chat-chips button:hover { border-color: var(--accent); color: var(--accent); }

/* ------------------------------------------------------------------- input */
.chat-input {
  display: flex;
  gap: .5rem;
  align-items: flex-end;
  padding: .75rem 1rem;
  padding-bottom: max(.75rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
  flex: none;
}
.chat-input textarea {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  padding: .65rem .85rem;
  border-radius: 12px;
  resize: none;
  font-size: 16px;               /* prevents iOS zoom-on-focus */
  line-height: 1.4;
}
.chat-send {
  width: 44px; height: 44px; flex: none;
  display: grid; place-items: center;
  border: 0; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent-deep), var(--accent-3));
  color: var(--on-accent); cursor: pointer;
  transition: opacity .18s var(--ease);
}
.chat-send[disabled] { opacity: .45; cursor: not-allowed; }

.chat-foot {
  margin: 0;
  padding: 0 1rem .5rem;
  font-size: .66rem;
  color: var(--text-dim);
  text-align: center;
  flex: none;
}
