/*
 * Help chat (partials/chat-widget + public/js/chat-widget.js), Object UI.
 * A round moss launcher fixed bottom-right; a paper panel above it on desktop, as tall as its conversation
 * (400 to 620px) so a short chat has no empty stretch, and a full-screen sheet on phones. Surfaces, not lines: the
 * head and the composer are paper, the conversation sits on oat and every message is a slip. Customer = lichen
 * (right), assistant and people = paper (left), system notes centred and small. Martian Mono only for data inside
 * messages (tracking numbers, query codes, refs): prices, weights and times stay in the sentence's type, in
 * tabular figures. The launcher sits under the site's menus (z-menu) and over the page; the open panel over both.
 */

.cw[hidden], .cw [hidden] { display: none !important; }

/* ---------- Launcher ---------- */
.cw-launch {
  position: fixed; z-index: 25;
  right: max(20px, env(safe-area-inset-right));
  bottom: max(20px, env(safe-area-inset-bottom));
  display: grid; place-items: center;
  width: 56px; height: 56px; margin: 0; padding: 0;
  border: 0; border-radius: 50%;
  background: var(--moss); color: var(--oat);
  box-shadow: 0 10px 26px rgba(28, 35, 30, .22), 0 2px 5px rgba(28, 35, 30, .14);
  cursor: pointer;
  transition: background-color .15s, transform .15s;
}
.cw-launch:hover { background: var(--moss-deep); }
.cw-launch:active { transform: scale(.96); }
.cw-launch:focus-visible { outline: 3px solid var(--ink); outline-offset: 3px; }
.cw-launch svg { width: 28px; height: 28px; }
.cw-launch__close { display: none; }
.cw.is-open .cw-launch__open { display: none; }
.cw.is-open .cw-launch__close { display: block; }
.cw-launch__dot {
  position: absolute; top: 2px; right: 2px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--sprout); box-shadow: 0 0 0 3px var(--moss);
}

/* A person's reply while the panel is closed: a paper slip above the launcher, gone when read or after a few seconds */
.cw-peek {
  position: fixed; z-index: 25;
  right: max(20px, env(safe-area-inset-right));
  bottom: calc(max(20px, env(safe-area-inset-bottom)) + 56px + 10px);
  display: block; width: max-content; max-width: min(280px, calc(100vw - 40px)); margin: 0; padding: 10px 14px;
  border: 0; border-radius: 14px; border-bottom-right-radius: 4px;
  background: var(--slip); color: var(--ink);
  box-shadow: 0 10px 26px rgba(28, 35, 30, .18), 0 2px 5px rgba(28, 35, 30, .10);
  font: inherit; font-size: var(--xs-size); line-height: 1.4; text-align: left; overflow-wrap: anywhere;
  cursor: pointer; animation: cw-in .16s ease-out;
}
.cw-peek b { display: block; font-weight: 600; }
.cw-peek span { display: block; color: var(--ink-2); }
.cw-peek:hover { background: var(--lichen); }
.cw-peek:focus-visible { outline: 2px solid var(--moss); outline-offset: 2px; }

/* ---------- Panel ---------- */
.cw-panel {
  --surface: var(--slip); --inset: var(--oat); --field: var(--oat); --fg: var(--ink); --fg-2: var(--ink-2);
  position: fixed; z-index: 40;
  right: max(20px, env(safe-area-inset-right));
  bottom: calc(max(20px, env(safe-area-inset-bottom)) + 56px + 14px);
  display: flex; flex-direction: column;
  width: 392px; max-width: calc(100vw - 40px);
  min-height: min(400px, calc(100vh - 110px)); min-height: min(400px, calc(100dvh - 110px)); /* room for the launcher and a margin above */
  max-height: min(620px, calc(100vh - 110px)); max-height: min(620px, calc(100dvh - 110px));
  overflow: hidden;
  border-radius: var(--r-xl);
  background: var(--slip); color: var(--ink);
  box-shadow: 0 24px 60px rgba(28, 35, 30, .22), 0 3px 10px rgba(28, 35, 30, .10);
  font-family: var(--f-sans);
  transform-origin: 100% 100%;
  animation: cw-in .16s ease-out;
}
.cw-panel:focus { outline: none; }
.cw-panel.is-restored { animation: none; }
@keyframes cw-in { from { opacity: 0; transform: translateY(8px) scale(.985); } to { opacity: 1; transform: none; } }

.cw-head { flex: none; display: flex; align-items: center; gap: 4px; padding: 12px 10px 12px 20px; background: var(--slip); }
.cw-head__text { flex: 1 1 auto; min-width: 0; }
.cw-head__title { margin: 0; font-size: 1.125rem; font-weight: 600; line-height: 1.2; letter-spacing: -.015em; }
.cw-head__note { margin: 2px 0 0; color: var(--ink-2); font-size: var(--xs-size); line-height: 1.35; font-variant-numeric: tabular-nums; }
.cw-x, .cw-end {
  flex: none; margin: 0; border: 0; background: transparent; color: var(--ink); cursor: pointer; font: inherit;
}
.cw-x { display: grid; place-items: center; width: 40px; height: 40px; border-radius: var(--r-s); }
.cw-x svg { width: 20px; height: 20px; }
.cw-end { min-height: 40px; padding: 0 10px; border-radius: var(--r-s); color: var(--ink-2); font-size: var(--xs-size); font-weight: 600; white-space: nowrap; }
.cw-end.is-confirming { background: var(--tomato-2); color: var(--tomato); }
.cw-x:hover, .cw-end:hover { background: var(--oat); color: var(--ink); }
.cw-end.is-confirming:hover { background: var(--tomato-2); color: var(--tomato); }
.cw-x:focus-visible, .cw-end:focus-visible { outline: 2px solid var(--moss); outline-offset: 1px; }

/* Chat state (waiting for a person, ticket made): sticker + plain words, never a step count */
.cw-state {
  flex: none; display: flex; align-items: flex-start; gap: 10px;
  margin: 0 12px 10px; padding: 10px 12px; border-radius: var(--r);
  background: var(--oat-deep); font-size: var(--xs-size); line-height: 1.4; font-variant-numeric: tabular-nums;
}
.cw-state.is-live { background: var(--lichen); }
.cw-state .st__g { width: 18px; height: 18px; flex: none; margin-top: 1px; }
.cw-state__text { min-width: 0; }
.cw-state__title { display: block; font-weight: 600; }
.cw-state__sub { display: block; color: var(--ink-2); }
.cw-state a { color: var(--moss); font-weight: 600; }
.cw-state a:hover { color: var(--moss-deep); }

/* ---------- Conversation ---------- */
.cw-body { position: relative; flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; background: var(--oat); }
.cw-log {
  position: relative; flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  display: flex; flex-direction: column; gap: 12px;
  padding: 16px 14px;
}
.cw-log:focus-visible { outline: 2px solid var(--moss); outline-offset: -2px; }
.cw-log > :first-child { margin-top: auto; }

.cw-msg { flex: none; display: flex; flex-direction: column; max-width: 86%; }
.cw-msg--user { align-self: flex-end; align-items: flex-end; }
.cw-msg--bot, .cw-msg--agent { align-self: flex-start; }
.cw-bubble {
  padding: 10px 14px; border-radius: 14px;
  background: var(--slip); color: var(--ink);
  font-size: var(--small-size); line-height: 1.45; font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere; white-space: pre-line;
}
.cw-bubble p { margin: 0; }
.cw-bubble p + p, .cw-bubble p + .cw-list, .cw-bubble .cw-list + p { margin-top: 8px; }
.cw-list { margin: 0; padding: 0 0 0 1.1em; list-style: disc; }
.cw-list li { display: list-item; margin: 0; padding: 0; }
.cw-list li + li { margin-top: 2px; }
.cw-list li::marker { color: var(--ink-2); }
.cw-msg--bot .cw-bubble, .cw-msg--agent .cw-bubble { border-top-left-radius: 4px; }
.cw-msg--user .cw-bubble { background: var(--lichen); border-top-right-radius: 4px; }
.cw-msg.is-pending .cw-bubble { opacity: .7; }
.cw-msg.is-failed .cw-bubble { box-shadow: inset 0 0 0 2px var(--tomato); }
.cw-msg.is-grouped { margin-bottom: -7px; }
.cw-msg.is-untimed .cw-time { display: none; }
.cw-retry {
  align-self: flex-end; margin: 2px 2px 0; padding: 4px 0; border: 0; background: none; color: var(--tomato);
  font: inherit; font-size: var(--xs-size); font-weight: 600; cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}
.cw-who { margin: 0 0 4px 4px; font-size: var(--xs-size); color: var(--ink-2); }
.cw-who b { color: var(--ink); font-weight: 600; }
.cw-time { margin: 4px 4px 0; color: var(--ink-2); font-size: .75rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.cw .cw-data { font-family: var(--f-mono); font-size: .86em; font-weight: 400; letter-spacing: -.02em; font-feature-settings: "zero" 1; white-space: nowrap; }
.cw-inline { padding-block: 6px; color: var(--moss); font-weight: 600; text-underline-offset: 2px; white-space: nowrap; }
.cw-inline:hover { color: var(--moss-deep); }

.cw-links { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 6px 4px 0; }
.cw-links a {
  display: inline-flex; align-items: center; gap: 6px; color: var(--moss); font-size: var(--small-size); font-weight: 600;
  text-decoration: none;
}
.cw-links a::after {
  content: ""; width: 14px; height: 14px; background: currentColor;
  -webkit-mask: var(--arrow) center / contain no-repeat; mask: var(--arrow) center / contain no-repeat;
}
.cw-links a:hover { color: var(--moss-deep); text-decoration: underline; text-underline-offset: 3px; }

.cw-note, .cw-day {
  flex: none; align-self: center; max-width: 92%; margin: 0; padding: 0 8px;
  color: var(--ink-2); font-size: var(--xs-size); line-height: 1.4; text-align: center; font-variant-numeric: tabular-nums;
}
.cw-note p { margin: 0; }
.cw-day { font-weight: 600; }

/* Quick replies under the latest message: two even columns; an odd last one takes the whole row */
.cw-chips { flex: none; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.cw-chip {
  min-height: 40px; margin: 0; padding: 8px 12px;
  border: 2px solid transparent; border-radius: var(--r);
  background: var(--oat-deep); color: var(--ink);
  font: inherit; font-size: var(--ui-size-xs); font-weight: 600; line-height: 1.2; text-align: center;
  overflow-wrap: anywhere; cursor: pointer; transition: background-color .12s;
}
.cw-chip:last-child:nth-child(odd) { grid-column: 1 / -1; }
.cw .cw-chip .cw-data { white-space: normal; }
.cw-chip:hover { background: var(--lichen); }
.cw-chip:focus-visible { outline: none; border-color: var(--moss); }
.cw-chip:disabled { cursor: default; opacity: .55; }

/* Back to the latest message, when one arrives while the customer is reading further up */
.cw-jump {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%);
  min-height: 36px; margin: 0; padding: 0 14px;
  border: 0; border-radius: var(--r);
  background: var(--moss); color: var(--slip); box-shadow: 0 4px 12px rgba(28, 35, 30, .18);
  font: inherit; font-size: var(--ui-size-xs); font-weight: 600; white-space: nowrap; cursor: pointer;
}
.cw-jump:hover { background: var(--moss-deep); }
.cw-jump:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* Typing indicator (a paper slip with three dots) */
.cw-typing { flex: none; padding: 0 14px 12px; }
.cw-typing__dots { display: inline-flex; gap: 5px; padding: 13px 14px; border-radius: 14px; border-top-left-radius: 4px; background: var(--slip); }
.cw-typing__dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-2); animation: cw-dot 1.1s infinite ease-in-out; }
.cw-typing__dots span:nth-child(2) { animation-delay: .15s; }
.cw-typing__dots span:nth-child(3) { animation-delay: .3s; }
@keyframes cw-dot { 0%, 60%, 100% { opacity: .25; transform: none; } 30% { opacity: 1; transform: translateY(-2px); } }

.cw-error {
  flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin: 0; padding: 10px 16px 0; background: var(--slip); color: var(--tomato); font-size: var(--xs-size); font-weight: 600; line-height: 1.4;
}
.cw-error__retry {
  flex: none; margin: 0; padding: 4px 0; border: 0; background: none; color: inherit; font: inherit; cursor: pointer;
  text-decoration: underline; text-underline-offset: 2px;
}

/* ---------- Composer ---------- */
.cw-form { flex: none; display: flex; align-items: flex-end; gap: 8px; padding: 12px 12px 6px; background: var(--slip); }
.cw-input {
  flex: 1 1 auto; min-width: 0; min-height: 46px; max-height: 132px; margin: 0; padding: 11px 14px;
  border: 2px solid transparent; border-radius: var(--r);
  background: var(--oat); color: var(--ink);
  font: inherit; font-size: 1rem; line-height: 1.35;
  resize: none; field-sizing: content;
  transition: border-color .12s;
}
.cw-input::placeholder { color: var(--ink-2); opacity: 1; }
.cw-input:focus { outline: none; border-color: var(--moss); }
.cw-input:disabled { background: var(--oat-deep); color: var(--ink-2); }
.cw-send {
  flex: none; display: grid; place-items: center;
  width: 46px; height: 46px; margin: 0; padding: 0;
  border: 0; border-radius: var(--r);
  background: var(--moss); color: var(--slip); cursor: pointer;
  transition: background-color .12s;
}
.cw-send:hover { background: var(--moss-deep); }
.cw-send:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.cw-send:disabled { background: var(--oat-deep); color: var(--ink-2); cursor: default; }
.cw-send svg { width: 20px; height: 20px; }
.cw-count { flex: none; margin: 0; padding: 0 16px 2px; background: var(--slip); color: var(--ink-2); font-size: .75rem; font-variant-numeric: tabular-nums; }

.cw-ended { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 12px 6px 20px; background: var(--slip); }
.cw-ended p { margin: 0; color: var(--ink-2); font-size: var(--small-size); }
.cw-restart {
  flex: none; min-height: 44px; margin: 0; padding: 0 16px; border: 0; border-radius: var(--r);
  background: var(--moss); color: var(--slip); font: inherit; font-size: var(--ui-size-s); font-weight: 600; cursor: pointer;
}
.cw-restart:hover { background: var(--moss-deep); }
.cw-restart:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* A person, the phone or email: always one tap away, whatever the chat is doing */
.cw-reach {
  flex: none; display: flex; flex-wrap: wrap; align-items: center; gap: 0 16px;
  padding: 0 14px 8px; background: var(--slip); font-size: var(--ui-size-xs); font-variant-numeric: tabular-nums;
}
.cw-reach a, .cw-reach__talk {
  display: inline-flex; align-items: center; min-height: 32px; margin: 0; padding: 0; border: 0; background: none;
  color: var(--moss); font: inherit; font-weight: 600; white-space: nowrap; cursor: pointer;
  text-decoration: none;
}
.cw-reach__talk { margin-right: auto; }
.cw-reach a:hover, .cw-reach__talk:hover { color: var(--moss-deep); text-decoration: underline; text-underline-offset: 3px; }
.cw-reach__talk:disabled { color: var(--ink-2); cursor: default; }

/* ---------- Phones: a full-screen sheet ---------- */
@media (max-width: 640px) {
  .cw-launch { right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom)); }
  .cw-peek { right: max(16px, env(safe-area-inset-right)); bottom: calc(max(16px, env(safe-area-inset-bottom)) + 56px + 10px); max-width: min(280px, calc(100vw - 32px)); }
  .cw-panel {
    inset: 0; width: auto; max-width: none; height: auto; min-height: 0; max-height: none;
    border-radius: 0; box-shadow: none;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    animation-name: cw-sheet;
  }
  .cw-head { padding: 10px 6px 10px 16px; }
  .cw.is-open .cw-launch { display: none; }
  .cw-msg { max-width: min(90%, 460px); }
}
@keyframes cw-sheet { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
html.cw-locked, html.cw-locked body { overflow: hidden; }

/* Narrow cover screens: the note under the title takes its own line rather than squeezing the buttons */
@media (max-width: 380px) {
  .cw-log { padding-left: 12px; padding-right: 12px; }
  .cw-msg { max-width: 94%; }
  .cw-reach { gap: 0 12px; padding-left: 12px; padding-right: 12px; }
}

/* ---------- Phones held sideways (short screens): a full-height sheet on the right ----------
   A floating panel does not fit a 375–412px tall screen, so the panel takes the full height beside the page
   and the launcher steps aside while it is open. The composer is always the last thing but one in the sheet. */
@media (max-height: 500px) and (min-width: 641px) {
  .cw-launch { right: max(12px, env(safe-area-inset-right)); bottom: max(12px, env(safe-area-inset-bottom)); width: 48px; height: 48px; }
  .cw-launch svg { width: 24px; height: 24px; }
  .cw-peek { right: max(12px, env(safe-area-inset-right)); bottom: calc(max(12px, env(safe-area-inset-bottom)) + 48px + 8px); }
  .cw-panel {
    top: 0; right: 0; bottom: 0; left: auto;
    width: min(420px, 100%); max-width: none; height: auto; min-height: 0; max-height: none;
    border-radius: var(--r-xl) 0 0 var(--r-xl);
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) 0;
    animation-name: cw-sheet;
  }
  .cw.is-open .cw-launch { display: none; }
  .cw-head { padding: 6px 8px 6px 18px; }
  .cw-log { padding-top: 10px; padding-bottom: 10px; }
  .cw-form { padding: 8px 10px 4px; }
  .cw-reach { padding-bottom: 2px; }
}
/* While the keyboard is up, chat-widget.js sizes a sheet to the visible part of the screen (visualViewport), so the
   head, the conversation and the composer all stay in view above the keyboard; the keyboard already covers the safe
   area, and the row of other contacts gives its height back to the conversation until the keyboard goes. */
.cw-panel.is-fitted { bottom: auto; padding-bottom: 0; }
.cw-panel.is-fitted .cw-reach { display: none; }
.cw-panel.is-fitted .cw-form { padding-bottom: 8px; }
.cw-panel.is-tight .cw-head { padding-top: 2px; padding-bottom: 2px; }
.cw-panel.is-tight .cw-head__note, .cw-panel.is-tight .cw-state__sub { display: none; }
.cw-panel.is-tight .cw-state { margin-bottom: 4px; padding-top: 6px; padding-bottom: 6px; }
.cw-panel.is-tight .cw-log { padding-top: 8px; padding-bottom: 8px; }

/* Touch screens: every control in the panel is a 44px target */
@media (max-width: 960px), (pointer: coarse) {
  .cw-x { width: 44px; height: 44px; }
  .cw-end { min-height: 44px; }
  .cw-chip { min-height: 44px; }
  .cw-jump { min-height: 44px; }
  .cw-retry, .cw-error__retry { min-height: 44px; padding: 0; }
  .cw-links a { min-height: 44px; }
  .cw-links { margin-top: 0; }
  .cw-reach a, .cw-reach__talk { min-height: 44px; }
  .cw-reach { padding-bottom: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .cw-panel, .cw-peek { animation: none; }
  .cw-typing__dots span { animation: none; opacity: .6; }
}

@media print { .cw { display: none !important; } }
