/* ------------------------------------------------------------
   THEME VARIABLES
------------------------------------------------------------ */
:root {
  /* Brand / Primary */
  --tw-primary: #f28c83;
  --tw-primary-text: #ffffff;

  /* Panel + Surfaces */
  --tw-panel-bg: #ffffff;
  --tw-body-bg: #fafafa;
  --tw-input-bg: #ffffff;

  /* Borders */
  --tw-border: #ddd;
  --tw-input-border: #ccc;

  /* Messages */
  --tw-user-msg-bg: var(--tw-primary);
  --tw-user-msg-text: #ffffff;

  --tw-bot-msg-bg: #e0e0e0;
  --tw-bot-msg-text: #333333;

  --tw-status-msg-notification-bg: #383838a1;
  --tw-status-msg-notification-text: #f7f7f7;

  --tw-status-msg-note-bg: #d9ffcd;
  --tw-status-msg-note-text: #5c5c5c;

  --tw-status-msg-warning-bg: #fff3cd;
  --tw-status-msg-warning-text: #5c5c5c;

  --tw-status-msg-alert-bg: #ff616e;
  --tw-status-msg-alert-text: #ffffff;

  /* Status Bar */
  --tw-status-offline-bg: #ffcccc;
  --tw-status-offline-text: #900;

  --tw-status-away-bg: #fff3cd;
  --tw-status-away-text: #856404;

  --tw-status-brb-bg: #cce5ff;
  --tw-status-brb-text: #004085;
}

/* ------------------------------------------------------------
   LAUNCHER BUTTON — base + interaction
   (Keep base rules early so panel/position helpers can layer)
------------------------------------------------------------ */
#tw-chat-button {
  position: fixed;
  background: var(--tw-primary);
  color: var(--tw-primary-text);
  padding: 14px 20px;
  border-radius: 50px;
  cursor: pointer;
  font-family: sans-serif;
  font-size: 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  z-index: 9999;
}

#tw-chat-button:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 18px rgba(0,0,0,0.25);
}

/* ------------------------------------------------------------
   POSITION HELPERS (must remain before panel-attached rules)
------------------------------------------------------------ */
.tw-bottom-right { bottom: 20px; right: 20px; }
.tw-bottom-left  { bottom: 20px; left: 20px; }
.tw-inline       { position: static !important; }

/* ------------------------------------------------------------
   LAUNCHER VARIANTS — pill then icon
   (Order: pill first, icon last — icon rules are more specific)
------------------------------------------------------------ */
/* pill */
.tw-pill {
  padding: 12px 18px;
  background: #333;
  color: white;
  border-radius: 999px;
}

/* icon (kept after pill so it reliably overrides) */
.tw-icon {
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: 50%;
  background: #333;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* explicit button state for icon mode (important to preserve circle) */
#tw-chat-button.tw-icon {
  width: 64px !important;
  height: 64px !important;
  padding: 6px !important;
  border-radius: 50% !important;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* small SVG tweak */
#tw-chat-button.tw-icon svg {
  transform: translateY(2px);
}

/* icon sub-elements */
.tw-icon-bg { fill: currentColor; }
.tw-icon-eye { fill: rgba(0, 0, 0, 0.514); }
.tw-icon-smile {
  stroke: rgba(0, 0, 0, 0.514);
  stroke-width: 1.4;
  stroke-linecap: round;
  fill: none;
}

/* ------------------------------------------------------------
   BOTTOM BAR MODE (explicit overrides only when used)
------------------------------------------------------------ */
.tw-bottom {
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  text-align: center;
  justify-content: center;
}

/* Remove rounded corners in bottom mode — only applies when .tw-bottom is present */
#tw-chat-button.tw-bottom.tw-pill,
#tw-chat-button.tw-bottom.tw-icon {
  border-radius: 0 !important;
}

/* special bottom icon placement (centered) */
#tw-chat-button.tw-icon.tw-bottom {
  width: 64px !important;
  height: 64px !important;
  padding: 6px !important;
  border-radius: 50% !important;

  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
}

/* ------------------------------------------------------------
   CHAT PANEL (container)
------------------------------------------------------------ */
#tw-chat-panel {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 320px;
  height: 420px;
  background: var(--tw-panel-bg);
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  display: none;
  flex-direction: column;
  overflow: hidden;
  z-index: 9999;
  font-family: sans-serif;
}

#tw-chat-panel.open {
  display: flex;
}

#tw-chat-panel label, #tw-chat-panel h3, #tw-chat-panel .tw-entry-or {
  user-select: none;
}

/* floating panel variant */
#tw-chat-panel.tw-panel-floating {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: 80vh;
  max-width: 400px;
  width: 90%;
  border-radius: 12px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.25);
}

/* attached panel base (kept after position helpers) */
#tw-chat-panel.tw-panel-attached {
  position: fixed;
  bottom: 20px;
  right: 20px;
  left: auto;
}

/* attached panel when button is bottom-right */
.tw-bottom-right + #tw-chat-panel.tw-panel-attached,
#tw-chat-panel.tw-panel-attached.tw-bottom-right {
  bottom: 20px;
  right: 20px;
  left: auto;
}

/* attached panel when button is bottom-left */
.tw-bottom-left + #tw-chat-panel.tw-panel-attached,
#tw-chat-panel.tw-panel-attached.tw-bottom-left {
  bottom: 20px;
  left: 20px;
  right: auto;
}

/* attached panel when button is bottom (centered above bar) */
#tw-chat-panel.tw-panel-attached.tw-bottom {
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
}

#tw-chat-header {
  user-select: none;
}

/* Only show grab cursor when panel is explicitly draggable (controlled by JS) */
#tw-chat-panel.tw-draggable #tw-chat-header {
  cursor: grab;
}

/* ------------------------------------------------------------
   HEADER
------------------------------------------------------------ */
#tw-chat-header {
  background: var(--tw-primary);
  color: var(--tw-primary-text);
  padding: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
}

#tw-chat-close {
  background: transparent;
  border: none;
  color: var(--tw-primary-text);
  font-size: 20px;
  cursor: pointer;
}

#tw-chat-dock-toggle {
  background: transparent;
  border: none;
  color: var(--tw-primary-text);
  font-size: 20px;
  cursor: pointer;
  display: none; /* Hidden by default */
  line-height: 1;
}

#tw-chat-dock-toggle svg {
  pointer-events: none;
}

#tw-chat-panel.tw-hide-close-btn #tw-chat-close {
  display: none !important;
}

#tw-chat-panel.tw-docked.tw-docked-left svg {
  transform: rotate(180deg);
}

#tw-chat-panel.tw-docked.tw-collapsed {
  user-select: none;
}

/* ------------------------------------------------------------
   CHAT BODY
------------------------------------------------------------ */
#tw-chat-body {
  flex: 1;
  padding: 12px;
  overflow-y: auto;
  background: var(--tw-body-bg);
  display: flex;
  flex-direction: column;
}

/* Contact Mode — overrides everything (kept near body rules) */
#tw-chat-body.contact-mode {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* no scroll */
  padding: 10px;
  background: var(--tw-body-bg);
}

/* ------------------------------------------------------------
   INPUT AREA
------------------------------------------------------------ */
#tw-chat-input-area {
  display: flex;
  border-top: 1px solid var(--tw-border);
  padding: 8px;
  background: var(--tw-input-bg);
}

#tw-chat-input {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--tw-input-border);
  border-radius: 6px;
}

#tw-chat-send, #tw-chat-success-close {
  margin-left: 8px;
  padding: 8px 12px;
  background: var(--tw-primary);
  color: var(--tw-primary-text);
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

/* ------------------------------------------------------------
   MESSAGES
------------------------------------------------------------ */
.tw-msg {
  margin-bottom: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  max-width: 80%;
  line-height: 1.4;
  font-size: 14px;
  box-sizing: border-box;
}

.tw-msg.tw-msg-full {
  max-width: 100%;
  width: 100%;
}

.tw-msg-user {
  background: var(--tw-user-msg-bg);
  color: var(--tw-user-msg-text);
  margin-left: auto;
  text-align: right;
}

.tw-msg-bot {
  background: var(--tw-bot-msg-bg);
  color: var(--tw-bot-msg-text);
  margin-right: auto;
  text-align: left;
}

.tw-msg-bot.notification {
  background: var(--tw-status-msg-notification-bg);
  color: var(--tw-status-msg-notification-text);
  text-align: center;
}

.tw-msg-bot.warning {
  background: var(--tw-status-msg-warning-bg);
  color: var(--tw-status-msg-warning-text);
  text-align: center;
}

.tw-msg-bot.alert {
  background: var(--tw-status-msg-alert-bg);
  color: var(--tw-status-msg-alert-text);
  text-align: center;
}

.tw-msg-bot.note {
  background: var(--tw-status-msg-note-bg);
  color: var(--tw-status-msg-note-text);
  text-align: center;
}

/* ------------------------------------------------------------
   CHAT CONTACT FORM (namespaced)
------------------------------------------------------------ */
#tw-chat-contact-form {
  display: flex;
  flex-direction: column;
  flex: 1; /* fill available space */
  gap: 6px;
}

#tw-chat-contact-form input {
  flex: 0 0 auto;
  padding: 8px;
  border-radius: 6px;
  border: 1px solid var(--tw-input-border);
  background: white;
}

#tw-chat-contact-form textarea {
  flex: 1 1 auto; /* grow to fill */
  padding: 8px;
  border-radius: 6px;
  border: 1px solid var(--tw-input-border);
  resize: none;
  min-height: 60px;
  background: white;
}

#tw-chat-contact-send {
  flex: 0 0 auto;
  padding: 10px;
  background: var(--tw-primary);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  margin-top: 6px;
}

/* ------------------------------------------------------------
   ENTRY SCREEN
------------------------------------------------------------ */
#tw-entry-screen {
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#tw-entry-screen h3 {
  margin: 0;
  font-size: 16px;
}

#tw-entry-screen label {
  font-size: 14px;
  margin-bottom: 2px;
}

#tw-entry-screen input {
  padding: 8px;
  border: 1px solid var(--tw-input-border);
  border-radius: 6px;
  background: white;
  font-size: 14px;
}

#tw-entry-screen button {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

#tw-start-chat {
  background: var(--tw-primary);
  color: white;
}

#tw-send-message {
  background: #555;
  color: white;
}

.tw-entry-or {
  text-align: center;
  opacity: 0.6;
  margin: 6px 0;
  font-size: 13px;
}

/* ------------------------------------------------------------
   STATUS BAR
------------------------------------------------------------ */
#tw-status-bar {
  width: 100%;
  padding: 10px;
  text-align: center;
  font-size: 14px;
  position: absolute;
  bottom: 50px;
  left: 0;
  z-index: 9999;
  border-radius: 0px 0px 0 0;
  box-sizing: border-box;
}

#tw-status-bar.offline {
  background: var(--tw-status-offline-bg);
  color: var(--tw-status-offline-text);
}

#tw-status-bar.away {
  background: var(--tw-status-away-bg);
  color: var(--tw-status-away-text);
}

#tw-status-bar.brb {
  background: var(--tw-status-brb-bg);
  color: var(--tw-status-brb-text);
}

/* ------------------------------------------------------------
   TYPING INDICATOR
------------------------------------------------------------ */
.tw-typing-bubble {
  padding: 8px 12px;       /* match bot bubble */
  line-height: 1.4;        /* match bot bubble */
  height: 20px;            /* same as single-line bubble */
  display: flex;
  align-items: center;
  opacity: 0.7;
}

.tw-typing-dots {
  display: flex;
  gap: 4px;
}

.tw-typing-dots span {
  width: 6px;
  height: 6px;
  background: #ccc;
  border-radius: 50%;
  animation: twTyping 1.4s infinite ease-in-out both;
}

.tw-typing-dots span:nth-child(1) { animation-delay: 0s; }
.tw-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.tw-typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes twTyping {
  0%, 80%, 100% { transform: scale(0.4); }
  40% { transform: scale(1); }
}

/* ------------------------------------------------------------
   INLINE ICONS (for link triggers)
------------------------------------------------------------ */
.tw-chat-inline-icon {
  display: inline-block;
  vertical-align: middle;
  position: relative;
  top: -1px;   /* nudges icon upward */
  fill: currentColor;
  stroke: currentColor;
  stroke-width: 0px;
}

.tw-chat-link {
  color: inherit;
}

.tw-chat-link:hover {
  color: var(--tw-primary);
}

/* ------------------------------------------------------------
   UTILITIES
------------------------------------------------------------ */
.tw-hidden { display: none; }
.tw-prehide { display: none; }

.tw-chat-status.online {
  color: #2ecc71; /* green */
  font-weight: 600;
}

.tw-chat-status.offline {
  color: #e74c3c; /* red */
  font-weight: 600;
}

.tw-envelope-icon path {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
}

#tw-chat { visibility: hidden; }

/* Docked mode container */
#tw-chat-dock-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 100vw;
  display: flex;
  pointer-events: none; /* floating mode ignores wrapper */
  z-index: 9998;
}

/* Chat panel in docked mode */
#tw-chat-panel.tw-docked {
  position: relative !important;
  top: 0 !important;
  bottom: 0 !important;
  left: 0 !important;
  right: 0 !important;
  height: 100vh !important;
  border-radius: 0 !important;
  pointer-events: auto;
  transition: width 0.3s ease;
  justify-content: flex-start !important;
}

/* When docked, hide the regular close button and show the dock toggle button */
#tw-chat-panel.tw-docked #tw-chat-close {
  display: none;
}

#tw-chat-panel.tw-docked.tw-collapsed #tw-status-bar {
  display: none;
}

#tw-chat-panel.tw-docked #tw-chat-dock-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

/* Collapsed state for docked panel */
#tw-chat-panel.tw-docked.tw-collapsed {
  width: 44px !important; /* Override docked width */
  // box-shadow: none !important;
}

/* Hide content when collapsed */
#tw-chat-panel.tw-docked.tw-collapsed #tw-chat-body,
#tw-chat-panel.tw-docked.tw-collapsed #tw-chat-input-area,
#tw-chat-panel.tw-docked.tw-collapsed #tw-chat-header > span {
  display: none !important;
}

/* Adjust header for collapsed state */
#tw-chat-panel.tw-docked.tw-collapsed #tw-chat-header {
  justify-content: center; /* Center the toggle button */
  padding: 12px 0;
}

/* Notification icon in collapsed state */
#tw-chat-dock-notification {
  display: none;
  position: absolute;
  bottom: 16px;
  left: 0;
  right: 0;
  text-align: center;
  cursor: pointer;
  color: var(--tw-primary);
  z-index: 100;
}

#tw-chat-dock-notification.tw-show {
  display: block;
  animation: tw-notif-bounce 1s infinite;
}

@keyframes tw-notif-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

/* Dock left */
#tw-chat-panel.tw-docked-left {
  width: 360px;
}

/* Dock right */
#tw-chat-panel.tw-docked-right {
  width: 360px;
  margin-left: auto;
}

/* Iframe for page content */
#tw-chat-iframe {
  flex: 1;
  border: none;
  height: 100vh;
  pointer-events: auto;
  background: #ffffff; /* ensure iframe area is opaque white */
}

/* When docked, lock the background page to prevent scrolling/jank */
body.tw-docked-active {
  overflow: hidden !important;
  touch-action: none !important;
}