/* === Chat Widget — scoped to .cw-* === */

/* ---- Floating action button ---- */
.cw-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 1100;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: #2563EB;
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(37, 99, 235, 0.45), 0 2px 6px rgba(0,0,0,0.15);
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.15s;
  outline: none;
}
.cw-fab:hover { background: #1D4ED8; transform: scale(1.06); }
.cw-fab:active { transform: scale(0.96); }
.cw-fab.is-open { background: #1D4ED8; }
.cw-fab svg { transition: transform 0.25s ease; }
.cw-fab.is-open svg { transform: rotate(90deg); }

/* Unread dot */
.cw-fab-badge {
  position: absolute;
  top: 2px; right: 2px;
  width: 11px; height: 11px;
  border-radius: 50%;
  background: #F59E0B;
  border: 2px solid #fff;
}

/* ---- Panel ---- */
.cw-panel {
  position: fixed;
  bottom: 88px;
  right: 24px;
  z-index: 1099;
  width: 420px;
  max-width: calc(100vw - 32px);
  height: min(680px, calc(100vh - 120px));
  display: flex;
  flex-direction: column;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 24px 64px rgba(15, 23, 42, 0.18), 0 4px 16px rgba(15, 23, 42, 0.08);
  border: 1px solid rgba(226, 232, 240, 0.8);
  transform-origin: bottom right;
  transform: scale(0.94) translateY(12px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.22s cubic-bezier(0.34, 1.3, 0.64, 1),
              opacity 0.18s ease;
  overflow: hidden;
}
.cw-panel.is-open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: all;
}

/* ---- Panel header ---- */
.cw-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px 12px 16px;
  background: rgba(255,255,255,0.9);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid #E2E8F0;
  flex-shrink: 0;
}
.cw-header-logo {
  width: 28px; height: 28px; flex-shrink: 0;
  border-radius: 7px;
  background: linear-gradient(135deg, #2563EB 0%, #7C3AED 100%);
  display: flex; align-items: center; justify-content: center;
  color: white; font-weight: 700; font-size: 12px;
  box-shadow: 0 2px 6px rgba(37,99,235,0.3);
}
.cw-header-title {
  font-size: 13px; font-weight: 700; color: #0F172A;
  white-space: nowrap;
}
.cw-session-select {
  flex: 1; min-width: 0;
  font-size: 12px;
  border: 1px solid #E2E8F0;
  border-radius: 6px;
  padding: 4px 6px;
  background: #F8FAFC;
  color: #334155;
  cursor: pointer;
  outline: none;
  font-family: inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: border-color 0.15s;
}
.cw-session-select:focus { border-color: #2563EB; }
.cw-header-btn {
  flex-shrink: 0;
  width: 28px; height: 28px;
  border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; border: none;
  background: transparent; color: #64748B;
  transition: background 0.12s, color 0.12s;
}
.cw-header-btn:hover { background: #F1F5F9; color: #0F172A; }

/* ---- Messages area ---- */
.cw-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}
.cw-messages::-webkit-scrollbar { width: 4px; }
.cw-messages::-webkit-scrollbar-track { background: transparent; }
.cw-messages::-webkit-scrollbar-thumb { background: #CBD5E1; border-radius: 4px; }

.cw-empty {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 10px; color: #94A3B8; padding: 24px;
}
.cw-empty-icon { font-size: 32px; opacity: 0.5; }
.cw-empty-hint { font-size: 13px; text-align: center; line-height: 1.5; }

/* ---- Individual message ---- */
.cw-msg {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-width: 100%;
}
.cw-msg-role {
  font-size: 10px; font-weight: 700; letter-spacing: 0.06em;
  text-transform: uppercase; color: #94A3B8;
  padding: 0 2px;
}
.cw-msg.user .cw-msg-role { color: #2563EB; text-align: right; }

.cw-msg.user  { align-items: flex-end; }
.cw-msg.assistant { align-items: flex-start; width: 100%; }

.cw-msg-bubble {
  border-radius: 14px;
  padding: 10px 14px;
  font-size: 13.5px;
  line-height: 1.6;
  max-width: 100%;
  word-break: break-word;
  position: relative;
}
.cw-msg.user .cw-msg-bubble {
  background: #2563EB;
  color: #fff;
  border-bottom-right-radius: 4px;
  max-width: 85%;
}
.cw-msg.assistant .cw-msg-bubble {
  background: #F8FAFC;
  color: #0F172A;
  border: 1px solid #E2E8F0;
  border-bottom-left-radius: 4px;
  width: 100%;
}

/* Cursor blink while streaming */
.cw-cursor {
  display: inline-block; width: 2px; height: 1em;
  background: #2563EB; margin-left: 2px; vertical-align: text-bottom;
  animation: cw-blink 0.8s step-end infinite;
}
@keyframes cw-blink { 0%,100%{opacity:1} 50%{opacity:0} }

/* Tool calls */
.cw-tools {
  display: flex; flex-direction: column; gap: 3px;
  width: 100%; margin-bottom: 4px;
}
.cw-tool {
  display: flex; align-items: center; gap: 6px;
  padding: 3px 8px;
  background: #F1F5F9; border-radius: 6px;
  font-size: 11.5px; color: #64748B;
  font-family: ui-monospace, monospace;
  border-left: 2px solid #CBD5E1;
}
.cw-tool.done  { border-left-color: #059669; color: #047857; background: #F0FDF4; }
.cw-tool-spin  { animation: cw-spin 1s linear infinite; display: inline-block; font-style: normal; }
@keyframes cw-spin { to { transform: rotate(360deg); } }

/* Markdown inside bubbles */
.cw-msg-bubble p  { margin: 0 0 8px; }
.cw-msg-bubble p:last-child { margin-bottom: 0; }
.cw-msg-bubble h1,.cw-msg-bubble h2,.cw-msg-bubble h3 { margin: 10px 0 4px; font-size: 14px; }
.cw-msg-bubble ul,.cw-msg-bubble ol { padding-left: 16px; margin: 6px 0; }
.cw-msg-bubble li { margin: 2px 0; }
.cw-msg-bubble pre { background: rgba(0,0,0,0.04); border-radius: 7px; padding: 10px; overflow-x: auto; margin: 8px 0; }
.cw-msg.user .cw-msg-bubble pre { background: rgba(255,255,255,0.12); }
.cw-msg-bubble code { font-family: ui-monospace,monospace; font-size: 12px; background: rgba(0,0,0,0.06); padding: 1px 4px; border-radius: 3px; }
.cw-msg.user .cw-msg-bubble code { background: rgba(255,255,255,0.15); }
.cw-msg-bubble pre code { background: none; padding: 0; }
.cw-msg-bubble hr { border: none; border-top: 1px solid #E2E8F0; margin: 10px 0; }
.cw-msg-bubble a { color: #2563EB; text-decoration: underline; }
.cw-msg.user .cw-msg-bubble a { color: rgba(255,255,255,0.85); }

/* Scroll anchor */
.cw-anchor { height: 1px; }

/* ---- File chips (in composer) ---- */
.cw-file-chips {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 8px 12px 0;
}
.cw-chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 8px;
  background: #F1F5F9; border: 1px solid #E2E8F0;
  border-radius: 20px; font-size: 12px; color: #334155;
  max-width: 180px;
}
.cw-chip-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cw-chip-status { font-size: 10px; flex-shrink: 0; }
.cw-chip-status.loading { color: #D97706; }
.cw-chip-status.done    { color: #059669; }
.cw-chip-status.error   { color: #DC2626; }
.cw-chip-rm {
  background: none; border: none; cursor: pointer;
  color: #94A3B8; font-size: 14px; line-height: 1; padding: 0 2px;
  flex-shrink: 0;
}
.cw-chip-rm:hover { color: #DC2626; }

/* Drop zone highlight */
.cw-panel.drag-over { box-shadow: 0 0 0 2px #2563EB, 0 24px 64px rgba(37,99,235,0.2); }

/* ---- Composer ---- */
.cw-composer {
  padding: 10px 12px 12px;
  border-top: 1px solid #E2E8F0;
  flex-shrink: 0;
  background: #fff;
}
.cw-composer-box {
  border: 1.5px solid #E2E8F0;
  border-radius: 12px;
  background: #F8FAFC;
  transition: border-color 0.15s, box-shadow 0.15s;
  overflow: hidden;
}
.cw-composer-box:focus-within {
  border-color: #2563EB;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.10);
  background: #fff;
}
.cw-textarea {
  display: block; width: 100%;
  padding: 10px 12px 6px;
  border: none; outline: none;
  background: transparent;
  font-family: inherit; font-size: 13.5px;
  color: #0F172A; resize: none;
  min-height: 56px; max-height: 180px;
  line-height: 1.5;
  field-sizing: content; /* auto-grow where supported */
}
.cw-textarea::placeholder { color: #94A3B8; }
.cw-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 8px 8px;
}
.cw-toolbar-left { display: flex; align-items: center; gap: 4px; }
.cw-icon-btn {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border: none; cursor: pointer;
  background: transparent; color: #94A3B8;
  border-radius: 6px;
  transition: background 0.12s, color 0.12s;
}
.cw-icon-btn:hover { background: #F1F5F9; color: #334155; }
.cw-send-btn {
  padding: 6px 14px;
  background: #2563EB; color: #fff;
  border: none; border-radius: 8px;
  font-size: 13px; font-weight: 600;
  cursor: pointer; font-family: inherit;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.cw-send-btn:hover:not(:disabled) { background: #1D4ED8; }
.cw-send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.cw-hint { font-size: 11px; color: #94A3B8; }

/* ---- Error toast (inside panel) ---- */
.cw-err {
  margin: 0 16px 12px;
  padding: 8px 12px;
  background: #FEF2F2; border: 1px solid #FECACA;
  border-radius: 8px; color: #DC2626; font-size: 12.5px;
}

/* ---- Fullscreen mode (route-based view) ---- */
.cw-panel.cw-fullscreen {
  position: relative;
  inset: auto;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  height: calc(100vh - 90px);
  border-radius: 14px;
  transform: none;
  opacity: 1;
  pointer-events: all;
  transition: none;
}
.cw-panel.cw-fullscreen .cw-messages { padding: 24px 28px; }
.cw-panel.cw-fullscreen .cw-composer { padding: 12px 18px 16px; }

/* ---- Attach button (composer) ---- */
.cw-attach-btn {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 10px;
  border: 1px solid #E2E8F0;
  background: #fff;
  color: #475569;
  border-radius: 7px;
  font-size: 12px; font-weight: 600;
  cursor: pointer; font-family: inherit;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.cw-attach-btn:hover { background: #F1F5F9; border-color: #CBD5E1; color: #0F172A; }
.cw-attach-btn svg { display: block; }
.cw-file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

/* ---- Mobile ---- */
@media (max-width: 480px) {
  .cw-panel {
    right: 0; bottom: 0;
    width: 100%; max-width: 100%;
    height: calc(100dvh - 64px);
    border-radius: 20px 20px 0 0;
    border-bottom: none;
  }
  .cw-fab { bottom: 16px; right: 16px; }
}
