/*
 * QC n8n Chatbot — custom widget styles.
 *
 * Brand tokens (spec §6.1, V1 reference): #0072CE primary (QC blue),
 * #FFFFFF surface, #F1F4F8 visitor bubble, Montserrat for the header,
 * system sans for body. All selectors are scoped under .qc-chat to avoid
 * leaking into the host theme.
 *
 * This file is BOTH imported by src/main.js (so Vite bundles it into the
 * IIFE) AND copied verbatim to dist/qc-brand.css by the vite.config.js
 * writeBundle plugin so the PHP embed loader can enqueue it as a standalone
 * stylesheet handle (qc-n8n-chatbot-brand).
 */

.qc-chat {
  /* Brand tokens. */
  --qc-blue: #0072ce;
  --qc-blue-dark: #005a9e;
  --qc-blue-soft: #e8f1fb;
  --qc-text: #1a1a1a;
  --qc-text-muted: #5b6770;
  --qc-surface: #ffffff;
  --qc-surface-alt: #f5f7fa;
  --qc-visitor-bubble: #f1f4f8;
  --qc-border: #e1e5ea;
  --qc-error: #c0392b;
  --qc-radius: 12px;
  --qc-radius-sm: 8px;
  --qc-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  --qc-shadow-sm: 0 4px 12px rgba(0, 0, 0, 0.12);
  --qc-z: 2147483000;
  --qc-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  --qc-font-head: "Montserrat", "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;

  position: fixed;
  bottom: 0;
  right: 0;
  z-index: var(--qc-z);
  font-family: var(--qc-font);
  color: var(--qc-text);
  width: 0;
  height: 0;
}

.qc-chat * {
  box-sizing: border-box;
}

/* --- Launcher bubble ---------------------------------------------------- */
.qc-launcher {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  background: var(--qc-blue);
  color: #fff;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--qc-shadow-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: transform 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
  z-index: var(--qc-z);
}

.qc-launcher:hover,
.qc-launcher:focus-visible {
  background: var(--qc-blue-dark);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 90, 158, 0.35);
}

.qc-launcher:focus-visible {
  outline: 3px solid #ffbf47;
  outline-offset: 3px;
}

.qc-launcher--hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.8);
}

/* --- Panel frame -------------------------------------------------------- */
.qc-panel {
  position: fixed;
  right: 20px;
  bottom: 90px;
  width: 380px;
  max-width: calc(100vw - 40px);
  height: 520px;
  max-height: calc(100vh - 120px);
  background: var(--qc-surface);
  border-radius: var(--qc-radius);
  box-shadow: var(--qc-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.98);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease,
    width 0.2s ease, height 0.2s ease;
  z-index: var(--qc-z);
}

.qc-panel--open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* Expanded state (toggle via header expand button, persisted in sessionStorage). */
.qc-panel--expanded {
  width: min(620px, calc(100vw - 40px));
  height: min(700px, calc(100vh - 120px));
}

/* --- Header ------------------------------------------------------------- */
.qc-panel__header {
  background: var(--qc-blue);
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: 0 0 auto;
}

.qc-panel__heading {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1 1 auto;
}

.qc-panel__title {
  font-family: var(--qc-font-head);
  font-weight: 700;
  font-size: 16px;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.qc-panel__subtitle {
  font-size: 12px;
  opacity: 0.85;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Header toolbar: clear + expand + close icon buttons. */
.qc-panel__toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}

.qc-panel__btn {
  background: transparent;
  border: none;
  color: #fff;
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: var(--qc-radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s ease;
}

.qc-panel__btn:hover,
.qc-panel__btn:focus-visible {
  background: rgba(255, 255, 255, 0.18);
}

.qc-panel__btn:focus-visible {
  outline: 2px solid #ffbf47;
  outline-offset: 2px;
}

.qc-panel__btn svg {
  pointer-events: none;
}

/* --- Chat screen -------------------------------------------------------- */
.qc-chat-screen {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.qc-chat__messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--qc-surface-alt);
}

/* --- Message bubbles (asymmetric margins per spec §13.8) --------------- */
.qc-msg {
  display: flex;
  max-width: 80%;
}

.qc-msg--visitor {
  align-self: flex-end;
  justify-content: flex-end;
  margin-left: 15%;
}

.qc-msg--bot {
  align-self: flex-start;
  justify-content: flex-start;
  margin-right: 15%;
}

.qc-msg__bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.qc-msg--visitor .qc-msg__bubble {
  background: var(--qc-visitor-bubble);
  color: var(--qc-text);
  border: 1px solid var(--qc-border);
  border-bottom-right-radius: 4px;
}

.qc-msg--bot .qc-msg__bubble {
  background: var(--qc-blue);
  color: #fff;
  border-bottom-left-radius: 4px;
}

/* Markdown inside bot bubbles. */
.qc-msg--bot .qc-msg__bubble p {
  margin: 0 0 8px;
}
.qc-msg--bot .qc-msg__bubble p:last-child {
  margin-bottom: 0;
}
.qc-msg--bot .qc-msg__bubble a {
  color: #bbdefb;
  text-decoration: underline;
}
.qc-msg--bot .qc-msg__bubble a:hover {
  color: #e3f2fd;
}
.qc-msg--bot .qc-msg__bubble ul,
.qc-msg--bot .qc-msg__bubble ol {
  margin: 6px 0;
  padding-left: 20px;
}
.qc-msg--bot .qc-msg__bubble li {
  margin-bottom: 4px;
}
.qc-msg--bot .qc-msg__bubble code {
  background: rgba(0, 0, 0, 0.2);
  padding: 1px 4px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  color: #f1f1f1;
}
.qc-msg--bot .qc-msg__bubble strong {
  font-weight: 700;
}

/* Typing indicator (three bouncing dots). */
.qc-msg--typing .qc-msg__bubble {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 12px 14px;
}

.qc-typing__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #fff;
  opacity: 0.6;
  animation: qc-typing-bounce 1.2s infinite ease-in-out;
}

.qc-typing__dot:nth-child(2) {
  animation-delay: 0.15s;
}
.qc-typing__dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes qc-typing-bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* --- Composer ----------------------------------------------------------- */
.qc-composer {
  border-top: 1px solid var(--qc-border);
  background: var(--qc-surface);
  padding: 10px 12px;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.qc-composer__row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.qc-composer__input {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--qc-border);
  border-radius: var(--qc-radius-sm);
  padding: 10px 12px;
  font-family: var(--qc-font);
  font-size: 14px;
  line-height: 1.4;
  max-height: 96px;
  overflow-y: auto;
  background: var(--qc-surface);
  color: var(--qc-text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.qc-composer__input:focus {
  outline: none;
  border-color: var(--qc-blue);
  box-shadow: 0 0 0 3px var(--qc-blue-soft);
}

.qc-composer__input:disabled {
  background: var(--qc-surface-alt);
  color: var(--qc-text-muted);
  cursor: not-allowed;
}

.qc-composer__send {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--qc-blue);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, transform 0.15s ease;
}

.qc-composer__send:hover,
.qc-composer__send:focus-visible {
  background: var(--qc-blue-dark);
  transform: translateY(-1px);
}

.qc-composer__send:focus-visible {
  outline: 3px solid #ffbf47;
  outline-offset: 2px;
}

.qc-composer__send:disabled {
  background: var(--qc-text-muted);
  cursor: not-allowed;
  transform: none;
}

.qc-composer__send svg {
  pointer-events: none;
}

/* Spinner animation (shown in send button while in-flight). */
.qc-spinner {
  animation: qc-spin 0.6s linear infinite;
}

@keyframes qc-spin {
  to {
    transform: rotate(360deg);
  }
}

/* Composer footer: disclaimer (left) + validation + char counter (right),
   spread to opposite edges via space-between. */
.qc-composer__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 18px;
  padding: 0 2px;
}

.qc-composer__disclaimer {
  font-size: 11px;
  color: var(--qc-text-muted);
  line-height: 1.3;
  /* Truncate if the panel is very narrow (mobile). */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.qc-composer__validation {
  font-size: 12px;
  color: var(--qc-error);
  flex: 1 1 auto;
}

.qc-composer__validation[hidden] {
  display: none;
}

.qc-composer__counter {
  font-size: 11px;
  color: var(--qc-text-muted);
  flex: 0 0 auto;
}

.qc-composer__counter--over {
  color: var(--qc-error);
  font-weight: 600;
}

.qc-composer__counter[hidden] {
  display: none;
}

/* --- Clear-conversation confirmation dialog ----------------------------- */
.qc-confirm {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: calc(var(--qc-z) + 1);
  padding: 20px;
}

.qc-confirm[hidden] {
  display: none;
}

.qc-confirm__box {
  background: var(--qc-surface);
  border-radius: var(--qc-radius);
  box-shadow: var(--qc-shadow);
  padding: 20px;
  max-width: 320px;
  width: 100%;
}

.qc-confirm__title {
  font-family: var(--qc-font-head);
  font-weight: 700;
  font-size: 16px;
  margin-bottom: 8px;
  color: var(--qc-text);
}

.qc-confirm__body {
  font-size: 13px;
  line-height: 1.5;
  color: var(--qc-text-muted);
  margin-bottom: 16px;
}

.qc-confirm__actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.qc-confirm__btn {
  border: none;
  border-radius: var(--qc-radius-sm);
  padding: 8px 16px;
  font-family: var(--qc-font);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}

.qc-confirm__btn:focus-visible {
  outline: 3px solid #ffbf47;
  outline-offset: 2px;
}

.qc-confirm__btn--cancel {
  background: var(--qc-surface-alt);
  color: var(--qc-text);
  border: 1px solid var(--qc-border);
}

.qc-confirm__btn--cancel:hover {
  background: var(--qc-border);
}

.qc-confirm__btn--danger {
  background: var(--qc-error);
  color: #fff;
}

.qc-confirm__btn--danger:hover {
  background: #a93226;
}

/* --- Mobile: full-screen panel < 600px ---------------------------------- */
@media (max-width: 599px) {
  .qc-launcher {
    right: 14px;
    bottom: 14px;
    width: 54px;
    height: 54px;
    font-size: 24px;
  }

  .qc-panel,
  .qc-panel--open,
  .qc-panel--expanded {
    right: 0;
    bottom: 0;
    left: 0;
    top: 0;
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    transform: translateY(100%);
    transition: transform 0.22s ease;
  }

  .qc-panel--open {
    transform: translateY(0);
  }

  .qc-chat__messages {
    padding-bottom: 80px;
  }

  /* Bubbles take more width on narrow screens. */
  .qc-msg {
    max-width: 88%;
  }
  .qc-msg--visitor {
    margin-left: 8%;
  }
  .qc-msg--bot {
    margin-right: 8%;
  }
}

/* --- Reduced motion ----------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .qc-launcher,
  .qc-panel,
  .qc-composer__send,
  .qc-composer__input,
  .qc-typing__dot,
  .qc-spinner {
    transition: none !important;
    animation: none !important;
  }
}
