/* ChatWidget — v2, dialog + operator-mode polling + rich replies (buttons, forms, iframes)
 * All selectors are prefixed with .cw- to avoid host-page collisions.
 * Apply once on the host page: <link rel="stylesheet" href="chat-widget.css">
 */

chat-widget {
  display: block;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  color: #333;
  --cw-w: 380px;
  --cw-h: 560px;
  /* Color scheme — overridable via the skin/primary-color/header-bg/etc.
   * attributes (see chat-widget.js _applyTheme). --cw-header-bg and
   * --cw-header-text fall back to the accent so a bare accent color still
   * looks right without a full skin. */
  --cw-accent: #e30613;
  --cw-header-bg: var(--cw-accent);
  --cw-header-text: #fff;
  --cw-body-bg: #fff;
  --cw-bubble-bg: #f5f5f5;
  --cw-bubble-text: #333;
}

.cw-root {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 320px;
  background: var(--cw-body-bg);
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
}

/* ── Floating mode ───────────────────────────────────────────────────────
 * Activated via <chat-widget floating> attribute.
 * Renders a FAB circle bottom-right (or bottom-left with position="left");
 * clicking expands it into a panel of fixed size; a header button toggles
 * fullscreen.
 */
chat-widget[floating] {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 2147483000;
}
chat-widget[floating][data-cw-position="left"] {
  right: auto;
  left: 24px;
}
chat-widget[floating].cw-max {
  top: 0; right: 0; bottom: 0; left: 0;
}

.cw-fab {
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--cw-accent);
  color: #fff;
  border: none;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,.25);
  transition: transform .15s ease, box-shadow .15s ease;
}
.cw-fab:hover { transform: scale(1.05); box-shadow: 0 6px 18px rgba(0,0,0,.3); }
.cw-fab svg { width: 26px; height: 26px; }

.cw-panel {
  width: var(--cw-w);
  height: var(--cw-h);
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 32px);
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 12px 36px rgba(0,0,0,.22);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
chat-widget[floating].cw-max .cw-panel {
  width: 100vw;
  height: 100vh;
  max-width: 100vw;
  max-height: 100vh;
  border-radius: 0;
}

.cw-panel-head {
  display: flex; align-items: center;
  padding: 10px 12px;
  background: var(--cw-header-bg);
  color: var(--cw-header-text);
  flex-shrink: 0;
}
.cw-panel-title { flex: 1; font-size: 14px; font-weight: 600; }
.cw-panel-btn {
  background: transparent;
  color: #fff;
  border: none;
  padding: 4px 8px;
  margin-left: 2px;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
}
.cw-panel-btn:hover { background: rgba(255,255,255,.18); }
.cw-panel-btn svg { width: 16px; height: 16px; }

/* In floating mode, the inner root should not duplicate the panel chrome. */
chat-widget[floating] .cw-root {
  border: none;
  border-radius: 0;
  flex: 1;
  min-height: 0;
}

.cw-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 13px;
  text-align: center;
  padding: 24px;
}

.cw-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.cw-msg { display: flex; flex-direction: column; gap: 4px; width: 100%; }
.cw-msg-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--cw-accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; flex-shrink: 0;
}
.cw-msg.cw-user .cw-msg-avatar { background: #555; align-self: flex-end; }
.cw-msg-bubble {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.cw-msg.cw-bot .cw-msg-bubble { background: var(--cw-bubble-bg); color: var(--cw-bubble-text); border-top-left-radius: 3px; }
.cw-msg.cw-user .cw-msg-bubble { background: var(--cw-accent); color: #fff; border-top-right-radius: 3px; }
.cw-msg.cw-operator .cw-msg-avatar { background: #2563eb; }
.cw-msg.cw-operator .cw-msg-bubble {
  background: #eff5ff; color: #1e3a8a; border: 1px solid #bfdbfe;
  border-top-left-radius: 3px;
}
.cw-msg.cw-system .cw-msg-bubble {
  background: #fff7e6; color: #8a5a00; border: 1px dashed #e6c170;
  font-style: italic;
}

/* Typing indicator shown while waiting for the server reply */
.cw-msg.cw-typing .cw-msg-bubble {
  background: var(--cw-bubble-bg);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 12px 14px;
  width: auto;
  align-self: flex-start;
}
.cw-dots { display: inline-flex; gap: 4px; align-items: center; }
.cw-dots span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #888;
  display: inline-block;
  animation: cw-bounce 1.4s infinite ease-in-out both;
}
.cw-dots span:nth-child(2) { animation-delay: 0.16s; }
.cw-dots span:nth-child(3) { animation-delay: 0.32s; }
@keyframes cw-bounce {
  0%, 80%, 100% { opacity: 0.2; transform: scale(0.7); }
  40%           { opacity: 1;   transform: scale(1); }
}

.cw-input-row {
  border-top: 1px solid #f0f0f0;
  padding: 12px;
  display: flex; gap: 8px; align-items: center;
}
.cw-input {
  flex: 1;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
  resize: none;
  min-height: 36px;
  max-height: 120px;
  font-family: inherit;
}
.cw-input:focus { border-color: var(--cw-accent); }
.cw-send {
  width: 36px; height: 36px;
  background: var(--cw-accent); color: #fff;
  border: none; border-radius: 8px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: filter .2s;
  flex-shrink: 0;
}
.cw-send:hover { filter: brightness(0.9); }
.cw-send:disabled { background: #ccc; cursor: not-allowed; }

.cw-session-bar {
  font-size: 10px;
  color: #bbb;
  padding: 2px 10px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  user-select: all;
  cursor: text;
  border-top: 1px solid #f0f0f0;
}

/* Rich replies: raw HTML / form messages (json.state, json.variables.html_form) */
.cw-msg-bubble.cw-html-result {
  background: #f0f7ff;
  border: 1px solid #cce0f5;
  border-top-left-radius: 3px;
  overflow-x: auto;
}
.cw-msg-bubble.cw-html-result table { border-collapse: collapse; width: 100%; }
.cw-msg-bubble.cw-html-result td,
.cw-msg-bubble.cw-html-result th { border: 1px solid #cce0f5; padding: 3px 6px; }
.cw-msg-bubble.cw-html-result th { background: #ddeeff; font-weight: 600; }
.cw-msg-bubble.cw-html-result ul,
.cw-msg-bubble.cw-html-result ol { margin-left: 16px; padding-left: 20px; }
.cw-msg-bubble.cw-html-result li { margin-left: 4px; }

/* Quick-reply buttons attached to a form message (json.variables.html_form) */
.cw-button-container { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.cw-tool-btn {
  background: var(--cw-accent); color: #fff; border: none; border-radius: 4px;
  padding: 6px 14px; cursor: pointer; font-size: 12px;
}
.cw-tool-btn:hover { filter: brightness(0.9); }

/* Full-page iframe forms (json.htmlFrame) — calendar, car-carousel, etc. */
.cw-iframe-wrap { width: 100%; }
.cw-iframe-wrap .cw-frame {
  display: block;
  width: 100%;
  height: auto;
  min-height: 420px;
  border: none;
}
