/* The live chat widget.
 *
 * Every colour is a --cw-* custom property set on .cw, so a product page can
 * re-theme the widget from its own palette without this file knowing anything
 * about that page (Trove is forest, InnVentory is cold navy). Never add
 * page-specific rules here, and never put widget styles in a page's <style> —
 * a config test fails if either happens.
 *
 * Mobile first: below 560px the panel is the screen, because that is where this
 * widget is actually used.
 */

.cw {
  position: fixed; right: 20px; z-index: 90;
  /* env(safe-area-inset-bottom), not a bare 20px. On an iPhone, Safari's
   * bottom toolbar and the home indicator overlay fixed content, so a plain
   * offset puts the launcher behind them — invisible on the device, perfect in
   * every headless test, because headless Chromium has no browser chrome.
   * The 0px fallback keeps it identical everywhere else. */
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; align-items: flex-end; gap: 12px;

  --cw-ink: #1A2433;
  --cw-muted: #4E5E72;
  --cw-line: #DDD6C8;
  --cw-panel: #FFFFFF;
  --cw-bg: #F7F4EE;
  --cw-head: linear-gradient(150deg, #002E5D 0%, #001F40 100%);
  --cw-head-ink: #FFFFFF;
  --cw-head-sub: #E7D3AC;
  --cw-accent: #D4A85C;
  --cw-accent-ink: #001F40;
  --cw-accent-hover: #E7D3AC;
  --cw-me: #002E5D;
  --cw-me-ink: #FFFFFF;
  --cw-chip-ink: #002E5D;
  --cw-chip-line: rgba(184, 134, 59, .35);
  --cw-send: #002E5D;
  --cw-send-hover: #00417F;
  --cw-link: #8A6024;
  --cw-live: #2E9E5B;
  --cw-away: #B8863B;
  --cw-seen: #1F6FAF;   /* 4.84:1 on the panel ground. #4FA3E3 measured 2.49 */
  --cw-font: 'Inter', system-ui, -apple-system, sans-serif;
  --cw-display: 'Fraunces', Georgia, serif;
  --cw-shadow: rgba(0, 20, 45, .28);
}

/* The browser's own [hidden] loses to any later display rule. Four 7Agi
 * projects have shipped a screen that would not hide because of it. */
.cw [hidden] { display: none !important; }

/* --------------------------------------------------------------- launcher -- */

.cw-launch {
  order: 3; position: relative;
  display: inline-flex; align-items: center; gap: 10px;
  cursor: pointer; background: var(--cw-accent); color: var(--cw-accent-ink);
  border: none; border-radius: 999px; padding: 13px 20px 13px 17px;
  font-family: var(--cw-font); font-size: 14px; font-weight: 600;
  box-shadow: 0 12px 30px var(--cw-shadow);
  transition: transform .18s ease, background .2s ease;
}
.cw-launch:hover { background: var(--cw-accent-hover); transform: translateY(-2px); }
.cw-launch svg { width: 21px; height: 21px; flex-shrink: 0; }
.cw-ico-close { display: none; }
.cw.open .cw-ico-chat { display: none; }
.cw.open .cw-ico-close { display: block; }
.cw.open .cw-launch-label { display: none; }
.cw.open .cw-launch { padding: 13px; border-radius: 50%; }

/* Unread badge. Sits on the launcher because that is the only thing on screen
 * when the panel is shut. */
.cw-badge-count {
  position: absolute; top: -5px; right: -3px; min-width: 20px; height: 20px;
  padding: 0 6px; border-radius: 999px; background: #C4342B; color: #fff;
  font-size: 11.5px; font-weight: 700; line-height: 20px; text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .3);
}
.cw.open .cw-badge-count { display: none !important; }

/* ------------------------------------------------------------------ nudge -- */

.cw-nudge {
  order: 1; position: relative; max-width: 252px;
  background: var(--cw-panel); color: var(--cw-ink);
  border: 1px solid var(--cw-line); border-radius: 14px 14px 4px 14px;
  padding: 13px 30px 13px 15px;
  font-family: var(--cw-font); font-size: 13.5px; line-height: 1.5;
  box-shadow: 0 14px 34px var(--cw-shadow); cursor: pointer;
}
.cw-nudge-x {
  position: absolute; top: 6px; right: 7px; width: 22px; height: 22px;
  cursor: pointer; background: none; border: none; color: var(--cw-muted);
  font-size: 17px; line-height: 1; border-radius: 6px;
}
.cw-nudge-x:hover { color: var(--cw-ink); background: var(--cw-bg); }

/* ------------------------------------------------------------------ panel -- */

.cw-panel {
  order: 2; display: flex; flex-direction: column;
  width: 370px;
  height: min(560px, calc(100vh - 128px));
  /* dvh follows the viewport as the browser bars slide away; vh does not, and
   * stays behind for the old browsers that have never heard of it. */
  height: min(560px, calc(100dvh - 128px));
  background: var(--cw-bg); border: 1px solid var(--cw-line);
  border-radius: 18px; overflow: hidden;
  box-shadow: 0 26px 60px var(--cw-shadow); font-family: var(--cw-font);
}

.cw-head {
  display: flex; align-items: center; gap: 11px;
  padding: 14px 15px; background: var(--cw-head); color: var(--cw-head-ink);
}
.cw-head .cw-mark {
  width: 34px; height: 34px; flex-shrink: 0; border-radius: 50%;
  display: grid; place-items: center; border: 1.5px solid var(--cw-accent);
  font-family: var(--cw-display); font-size: 13px; color: var(--cw-accent);
}
.cw-head-txt { flex: 1; min-width: 0; line-height: 1.25; }
.cw-title { font-family: var(--cw-display); font-size: 15px; font-weight: 600; }
.cw-sub {
  display: flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--cw-head-sub); margin-top: 2px;
  /* One line. A wrapping status line makes the header grow and shrink as the
   * status changes, which reads as the panel twitching. */
  white-space: nowrap; overflow: hidden;
}
.cw-sub > span:last-child { overflow: hidden; text-overflow: ellipsis; }
.cw-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--cw-live); flex-shrink: 0;
}
.cw-sub[data-status="away"] .cw-dot { background: var(--cw-away); }
.cw-sub[data-status="offline"] .cw-dot { background: var(--cw-muted); }

.cw-headbtn {
  width: 30px; height: 30px; flex-shrink: 0; cursor: pointer;
  background: rgba(255, 255, 255, .12); border: none; border-radius: 8px;
  color: var(--cw-head-ink); font-size: 17px; line-height: 1;
  display: grid; place-items: center;
}
.cw-headbtn:hover { background: rgba(255, 255, 255, .24); }
.cw-headbtn svg { width: 16px; height: 16px; }
.cw-mute[aria-pressed="true"] { opacity: .55; }

/* -------------------------------------------------------------------- log -- */

.cw-log {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 15px; display: flex; flex-direction: column; gap: 9px;
}
.cw-row { display: flex; flex-direction: column; max-width: 84%; }
.cw-row.me { align-self: flex-end; align-items: flex-end; }
.cw-row.them { align-self: flex-start; align-items: flex-start; }

.cw-msg {
  font-size: 13.7px; line-height: 1.58; padding: 10px 13px;
  border-radius: 14px; white-space: pre-wrap; word-break: break-word;
  color: var(--cw-ink);
}
.cw-msg a { color: var(--cw-link); font-weight: 600; }
.cw-row.them .cw-msg {
  background: var(--cw-panel); border: 1px solid var(--cw-line);
  border-bottom-left-radius: 4px;
}
.cw-row.me .cw-msg {
  background: var(--cw-me); color: var(--cw-me-ink);
  border-bottom-right-radius: 4px;
}
.cw-row.me .cw-msg a { color: var(--cw-accent-hover); }

/* Who said it. An assistant answer is labelled, always — a visitor is owed the
 * difference between a person answering and a machine standing in for one. */
.cw-who {
  font-size: 10.5px; letter-spacing: .04em; text-transform: uppercase;
  color: var(--cw-muted); margin: 0 3px 3px;
}

.cw-meta {
  display: flex; align-items: center; gap: 4px;
  font-size: 10.5px; color: var(--cw-muted); margin: 3px 3px 0;
}
.cw-ticks { display: inline-flex; align-items: center; }
.cw-ticks svg { width: 15px; height: 10px; }
.cw-ticks[data-state="seen"] { color: var(--cw-seen); }

.cw-note {
  align-self: center; text-align: center; max-width: 92%;
  font-size: 11.5px; line-height: 1.5; color: var(--cw-muted);
  padding: 6px 10px;
}

.cw-typing {
  align-self: flex-start; display: flex; gap: 4px; padding: 13px 14px;
  background: var(--cw-panel); border: 1px solid var(--cw-line);
  border-radius: 14px; border-bottom-left-radius: 4px;
}
.cw-typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--cw-line); animation: cwBlink 1.3s ease-in-out infinite;
}
.cw-typing span:nth-child(2) { animation-delay: .18s; }
.cw-typing span:nth-child(3) { animation-delay: .36s; }
@keyframes cwBlink { 0%, 60%, 100% { opacity: .35; } 30% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .cw-typing span { animation: none; opacity: .6; }
  .cw-launch { transition: none; }
}

/* ------------------------------------------------------- chips & composer -- */

.cw-chips {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 0 15px 10px;
}
.cw-chips:empty { display: none; }
.cw-chip {
  cursor: pointer; background: var(--cw-panel); color: var(--cw-chip-ink);
  border: 1px solid var(--cw-chip-line); border-radius: 999px;
  padding: 7px 12px; font-family: var(--cw-font); font-size: 12.5px;
}
.cw-chip:hover { border-color: var(--cw-accent); }

.cw-ask {
  margin: 0 15px 10px; padding: 11px 12px; background: var(--cw-panel);
  border: 1px solid var(--cw-line); border-radius: 12px;
}
.cw-ask p { font-size: 12.5px; color: var(--cw-muted); margin: 0 0 8px; }
.cw-ask-row { display: flex; gap: 6px; }
.cw-ask input {
  flex: 1; min-width: 0; border: 1px solid var(--cw-line); border-radius: 8px;
  padding: 8px 10px; font-family: var(--cw-font); font-size: 13px;
  color: var(--cw-ink); background: var(--cw-bg);
}
.cw-ask button {
  cursor: pointer; border: none; border-radius: 8px; padding: 8px 12px;
  background: var(--cw-send); color: #fff; font-family: var(--cw-font);
  font-size: 12.5px; font-weight: 600;
}
.cw-ask .cw-skip {
  background: none; color: var(--cw-muted); padding: 8px 6px; font-weight: 500;
}

.cw-form {
  display: flex; gap: 8px; padding: 11px 12px;
  border-top: 1px solid var(--cw-line); background: var(--cw-panel);
}
.cw-form input {
  flex: 1; min-width: 0; border: 1px solid var(--cw-line); border-radius: 999px;
  padding: 10px 14px; font-family: var(--cw-font); font-size: 13.5px;
  color: var(--cw-ink); background: var(--cw-bg);
}
.cw-form input:focus { outline: 2px solid var(--cw-accent); outline-offset: 1px; }
.cw-send {
  width: 40px; height: 40px; flex-shrink: 0; cursor: pointer; border: none;
  border-radius: 50%; background: var(--cw-send); color: #fff;
  display: grid; place-items: center;
}
.cw-send:hover { background: var(--cw-send-hover); }
.cw-send svg { width: 17px; height: 17px; }

.cw-foot {
  padding: 0 15px 11px; font-size: 10.5px; color: var(--cw-muted);
  text-align: center; background: var(--cw-panel);
}

/* ----------------------------------------------------------------- mobile -- */

@media (max-width: 560px) {
  .cw { right: 12px; left: 12px; align-items: flex-end;
        bottom: calc(12px + env(safe-area-inset-bottom, 0px)); }
  .cw-panel {
    position: fixed; inset: 0; width: auto;
    height: 100%; height: 100dvh;
    border-radius: 0; border: none;
  }
  /* The composer is the bottom-most thing in a fullscreen panel, so it is what
   * the home indicator sits on top of. */
  .cw-form { padding-bottom: calc(11px + env(safe-area-inset-bottom, 0px)); }
  .cw-nudge { max-width: min(252px, calc(100vw - 24px)); }

  /* 16px, and this is not a taste decision: iOS Safari zooms the whole page
   * when you focus an input whose font-size is under 16px, and there is no way
   * to undo the zoom afterwards. The alternative fix — user-scalable=no on the
   * viewport meta — breaks pinch-zoom for everyone and is banned by the
   * pre-deploy checklist, so the type size is what gives. */
  .cw-form input, .cw-ask input { font-size: 16px; }

  /* The panel tracks the VISUAL viewport, set from JS. iOS shrinks the visual
   * viewport when the keyboard opens but leaves the layout viewport alone, so
   * a fullscreen panel keeps its full height and the composer ends up behind
   * the keyboard. The fallback is dvh for anything without visualViewport. */
  .cw-panel {
    top: var(--cw-vvtop, 0px);
    height: var(--cw-vvh, 100dvh);
    bottom: auto;
  }

  /* Thumbs, not cursors. 44px is the floor, and the header is tall enough to
   * take it without the title moving. */
  .cw-headbtn { width: 44px; height: 44px; }
  .cw-send { width: 44px; height: 44px; }
  .cw-nudge-x { width: 32px; height: 32px; }
  /* An expanded hit area rather than a bigger button: the × should stay small
   * on a nudge that is only 252px wide. */
  .cw-nudge-x::after {
    content: ''; position: absolute; inset: -6px;
  }

  /* The launcher goes away while the panel is open, and this is not cosmetic.
   * Flex `order` also reorders PAINTING, and the launcher is order 3 against
   * the panel's order 2 — so the round button paints ON TOP of a fullscreen
   * panel and lands exactly over the Send button. Tapping Send closed the chat
   * instead. The header's own close button is what shuts it on a phone. */
  .cw.open .cw-launch { display: none; }
}
