/* ============================================================================
   IntegrityShield — support assistant widget (.chatw)
   ----------------------------------------------------------------------------
   Loaded by views/components/support_chat.php on every surface: the public
   site (public.css) and the three signed-in portals (app.css). Both stylesheets
   declare the SAME design tokens, so every colour here is a token and the
   widget inherits whichever palette the host page is using. Nothing in this
   file redefines a token.

   Layering, against app.css's existing scale:
       mobile tab bar  120
       LAUNCHER        140   — above the tab bar, reachable
       modal scrim     250
       OPEN PANEL      240   — above everything a page owns, below a modal, so
                               a confirmation dialog is never buried by a chat
       toast stack     300

   Dark mode follows the platform's own switch, html[data-theme="dark"], which
   only the portal sets. The public site is light-only by design and simply
   never matches those rules.
   ========================================================================== */

.chatw {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 140;
  font-family: inherit;
  /* The root is only a positioning anchor — clicks must fall through to the
     page everywhere except on the launcher and the panel themselves. */
  pointer-events: none;
}
.chatw > * { pointer-events: auto; }
.chatw.is-open { z-index: 240; }

.chatw__sr {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* ── Launcher ─────────────────────────────────────────────────────────────
   An <a> to the grievance form until JS upgrades it, so the no-JS path is a
   working link rather than a dead button. */
.chatw__launch {
  /* Containing block for the ::after hint dot — without it the dot would
     resolve against .chatw, which spans the viewport at the phone breakpoint. */
  position: relative;
  display: inline-flex; align-items: center; gap: 9px;
  height: 52px; padding: 0 20px 0 17px;
  border: 0; border-radius: 26px; cursor: pointer;
  background: linear-gradient(160deg, var(--green-600) 0%, var(--primary) 100%);
  color: #fff; font-size: 14px; font-weight: 650; letter-spacing: -0.005em;
  text-decoration: none;
  box-shadow: 0 10px 26px -10px rgba(10, 92, 57, .62), 0 2px 6px rgba(7, 18, 25, .16);
  transition: transform .18s ease, box-shadow .18s ease, background .18s ease;
  -webkit-tap-highlight-color: transparent;
}
.chatw__launch:hover {
  transform: translateY(-2px);
  box-shadow: 0 16px 32px -12px rgba(10, 92, 57, .7), 0 2px 8px rgba(7, 18, 25, .2);
  color: #fff;
}
.chatw__launch:focus-visible {
  outline: 3px solid var(--gold-bright);
  outline-offset: 3px;
}
.chatw__launch-ic { display: inline-flex; }
.chatw__launch-ic--close { display: none; }
.chatw.is-open .chatw__launch-ic--open { display: none; }
.chatw.is-open .chatw__launch-ic--close { display: inline-flex; }
.chatw.is-open .chatw__launch { background: var(--slate-700); box-shadow: 0 6px 18px -8px rgba(7, 18, 25, .5); }
.chatw.is-open .chatw__launch-label { display: none; }
.chatw.is-open .chatw__launch { padding: 0; width: 52px; justify-content: center; }

/* An unread-style ping while a first-time visitor has never opened it. */
.chatw__launch::after {
  content: ""; position: absolute; top: 8px; right: 10px;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--gold-bright); border: 2px solid var(--primary);
  opacity: 0; transform: scale(.4); transition: opacity .3s ease, transform .3s ease;
}
.chatw.is-hinted .chatw__launch::after { opacity: 1; transform: scale(1); }
.chatw.is-open .chatw__launch::after { opacity: 0; }

/* ── Panel ──────────────────────────────────────────────────────────────── */
.chatw__panel {
  position: absolute; right: 0; bottom: 66px;
  width: 392px; max-width: calc(100vw - 32px);
  height: 604px; max-height: calc(100vh - 130px);
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 28px 64px -20px rgba(7, 18, 25, .38), 0 4px 14px rgba(7, 18, 25, .1);
  animation: chatw-rise .22s cubic-bezier(.22, 1, .36, 1);
}
/* display:flex above would otherwise beat the [hidden] default and the panel
   would never actually close. */
.chatw__panel[hidden] { display: none; }

@keyframes chatw-rise {
  from { opacity: 0; transform: translateY(14px) scale(.985); }
  to   { opacity: 1; transform: none; }
}

.chatw__head {
  display: flex; align-items: center; gap: 11px;
  padding: 13px 14px 13px 15px; flex: none;
  background: linear-gradient(135deg, var(--ink) 0%, var(--ink-2) 100%);
  color: #fff;
}
.chatw__mark { display: inline-flex; flex: none; }
.chatw__head-text { min-width: 0; flex: 1; }
.chatw__title { margin: 0; font-size: 14.5px; font-weight: 700; letter-spacing: -0.01em; }
.chatw__status {
  margin: 2px 0 0; font-size: 11.5px; line-height: 1.35;
  color: rgba(255, 255, 255, .62);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chatw__x {
  flex: none; width: 32px; height: 32px; border: 0; border-radius: 8px; cursor: pointer;
  background: rgba(255, 255, 255, .09); color: rgba(255, 255, 255, .82);
  display: grid; place-items: center; transition: background .15s ease, color .15s ease;
}
.chatw__x:hover { background: rgba(255, 255, 255, .17); color: #fff; }
.chatw__x:focus-visible { outline: 2px solid var(--gold-bright); outline-offset: 2px; }

/* ── Message log ────────────────────────────────────────────────────────── */
.chatw__log {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 14px;
  background: var(--surface-2);
  scrollbar-width: thin;
}
.chatw__log::-webkit-scrollbar { width: 8px; }
.chatw__log::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }

.chatw__boot { display: flex; flex-direction: column; gap: 9px; padding: 4px 2px; }
.chatw__bar {
  display: block; height: 12px; border-radius: 6px;
  background: linear-gradient(90deg, var(--slate-200) 0%, var(--slate-100) 50%, var(--slate-200) 100%);
  background-size: 200% 100%;
  animation: chatw-shimmer 1.5s ease-in-out infinite;
}
@keyframes chatw-shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.chatw__msg { display: flex; flex-direction: column; gap: 5px; max-width: 88%; }
.chatw__msg--user { align-self: flex-end; align-items: flex-end; max-width: 84%; }
.chatw__msg--bot { align-self: flex-start; }

.chatw__who {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 10.5px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-faint); padding: 0 3px;
}

.chatw__bubble {
  padding: 10px 13px; border-radius: 13px;
  font-size: 13.5px; line-height: 1.58; word-break: break-word;
  white-space: pre-wrap;   /* the service returns plain text with real newlines */
}
.chatw__msg--bot .chatw__bubble {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-bottom-left-radius: 4px;
}
.chatw__msg--user .chatw__bubble {
  background: var(--primary); color: #fff; border-bottom-right-radius: 4px;
}
/* Links the assistant mentions are turned into real anchors by chat.js, but
   only ever to same-origin paths — see chat.js linkify(). */
.chatw__bubble a { color: var(--green-600); font-weight: 600; text-decoration: underline; }
.chatw__msg--user .chatw__bubble a { color: #fff; }

/* Provenance chip: every reply says where it came from. */
.chatw__badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 7px; border-radius: 999px;
  font-size: 9.5px; font-weight: 800; letter-spacing: .05em; text-transform: uppercase;
}
.chatw__badge--kb { background: var(--green-100); color: var(--primary); }
.chatw__badge--ai { background: var(--info-bg); color: var(--info); }

.chatw__sources { display: flex; flex-wrap: wrap; gap: 6px; padding: 1px 2px; }
.chatw__source {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text-muted); font-size: 11.5px; font-weight: 600; text-decoration: none;
  transition: border-color .15s ease, color .15s ease;
}
.chatw__source:hover { border-color: var(--green-600); color: var(--primary); }

/* ── Was this helpful? ──────────────────────────────────────────────────── */
.chatw__rate { display: flex; align-items: center; gap: 6px; padding: 1px 2px; }
.chatw__rate-label { font-size: 11.5px; color: var(--text-faint); }
.chatw__rate-btn {
  display: inline-grid; place-items: center; width: 26px; height: 26px;
  border: 1px solid var(--border-strong); border-radius: 7px; background: var(--surface);
  color: var(--text-muted); cursor: pointer;
  transition: border-color .15s ease, color .15s ease, background .15s ease;
}
.chatw__rate-btn:hover { border-color: var(--green-600); color: var(--primary); background: var(--green-100); }
.chatw__rate-btn--down:hover { border-color: var(--danger); color: var(--danger); background: var(--danger-bg); }
.chatw__rate-btn:focus-visible { outline: 2px solid var(--green-600); outline-offset: 1px; }
.chatw__rate-done { font-size: 11.5px; font-weight: 600; color: var(--success); }
.chatw__rate-done--down { color: var(--text-muted); font-weight: 500; }

/* ── Starter chips ──────────────────────────────────────────────────────── */
.chatw__chips { display: flex; flex-wrap: wrap; gap: 7px; padding: 0 2px 2px; }
.chatw__chip {
  border: 1px solid var(--border-strong); border-radius: 999px;
  background: var(--surface); color: var(--text); cursor: pointer;
  padding: 6px 12px; font-size: 12.5px; font-weight: 550; line-height: 1.3;
  text-align: left; font-family: inherit;
  transition: border-color .15s ease, background .15s ease, color .15s ease;
}
.chatw__chip:hover { border-color: var(--green-600); background: var(--green-100); color: var(--primary); }
.chatw__chip:focus-visible { outline: 2px solid var(--green-600); outline-offset: 1px; }

/* ── Typing indicator ───────────────────────────────────────────────────── */
.chatw__typing {
  display: flex; align-items: center; gap: 9px; flex: none;
  padding: 6px 16px 10px; background: var(--surface-2);
  font-size: 11.5px; color: var(--text-faint);
}
.chatw__typing[hidden] { display: none; }
.chatw__dots { display: inline-flex; gap: 4px; }
.chatw__dots i {
  width: 6px; height: 6px; border-radius: 50%; background: var(--slate-400);
  animation: chatw-bounce 1.15s ease-in-out infinite;
}
.chatw__dots i:nth-child(2) { animation-delay: .16s; }
.chatw__dots i:nth-child(3) { animation-delay: .32s; }
@keyframes chatw-bounce {
  0%, 62%, 100% { transform: translateY(0); opacity: .45; }
  28%           { transform: translateY(-5px); opacity: 1; }
}

/* ── Error state ────────────────────────────────────────────────────────── */
.chatw__alert {
  display: flex; align-items: flex-start; gap: 8px; flex: none;
  margin: 0 12px 8px; padding: 9px 11px;
  background: var(--danger-bg); color: var(--danger);
  /* Literal rather than color-mix(): the alert is the surface a user sees when
     something already went wrong, so it must not depend on a newer CSS feature. */
  border: 1px solid rgba(179, 38, 30, .28);
  border-radius: 9px; font-size: 12.5px; line-height: 1.5;
}
.chatw__alert[hidden] { display: none; }
.chatw__alert-ic { flex: none; display: inline-flex; margin-top: 1px; }

/* ── Composer ───────────────────────────────────────────────────────────── */
.chatw__form {
  display: flex; align-items: flex-end; gap: 8px; flex: none;
  padding: 10px 12px 8px; border-top: 1px solid var(--border); background: var(--surface);
}
.chatw__input {
  flex: 1; min-width: 0; resize: none;
  min-height: 40px; max-height: 132px; padding: 10px 13px;
  border: 1px solid var(--border-strong); border-radius: 12px;
  background: var(--surface-2); color: var(--text);
  font-family: inherit; font-size: 13.5px; line-height: 1.45;
}
.chatw__input::placeholder { color: var(--text-faint); }
.chatw__input:focus {
  outline: none; border-color: var(--green-600); background: var(--surface);
  box-shadow: 0 0 0 3px var(--green-100);
}
.chatw__send {
  flex: none; width: 40px; height: 40px; border: 0; border-radius: 11px; cursor: pointer;
  background: var(--primary); color: #fff; display: grid; place-items: center;
  transition: background .15s ease, opacity .15s ease;
}
.chatw__send:hover:not(:disabled) { background: var(--green-600); }
.chatw__send:disabled { opacity: .42; cursor: not-allowed; }
.chatw__send:focus-visible { outline: 2px solid var(--gold-bright); outline-offset: 2px; }

.chatw__foot {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex: none;
  padding: 0 14px 11px; background: var(--surface);
}
.chatw__note { margin: 0; font-size: 10.5px; line-height: 1.4; color: var(--text-faint); }
.chatw__human {
  display: inline-flex; align-items: center; gap: 5px; flex: none;
  font-size: 11.5px; font-weight: 650; color: var(--text-muted); text-decoration: none;
  white-space: nowrap;
}
.chatw__human:hover { color: var(--primary); text-decoration: underline; }

/* ── Mobile: the panel becomes a sheet ──────────────────────────────────── */
.chatw__scrim {
  position: fixed; inset: 0; z-index: -1;
  background: rgba(7, 18, 25, .5);
  animation: chatw-fade .2s ease;
  display: none;
}
@keyframes chatw-fade { from { opacity: 0; } to { opacity: 1; } }

@media (max-width: 620px) {
  .chatw { right: 14px; bottom: 14px; left: 14px; pointer-events: none; }
  .chatw__launch { position: absolute; right: 0; bottom: 0; }

  .chatw.is-open .chatw__scrim { display: block; }
  .chatw__panel {
    position: fixed; inset: auto 0 0 0;
    width: 100%; max-width: none;
    height: 92dvh; max-height: 92dvh;
    border: 0; border-radius: 18px 18px 0 0;
    animation: chatw-sheet .26s cubic-bezier(.22, 1, .36, 1);
  }
  /* Older engines without dvh keep a usable, if slightly tall, sheet. */
  @supports not (height: 92dvh) {
    .chatw__panel { height: 88vh; max-height: 88vh; }
  }
  @keyframes chatw-sheet { from { transform: translateY(100%); } to { transform: translateY(0); } }

  /* The launcher collapses to a circle; the panel's own close button and the
     scrim are the ways out on a phone. */
  .chatw__launch-label { display: none; }
  .chatw__launch { width: 52px; padding: 0; justify-content: center; }
  .chatw.is-open .chatw__launch { display: none; }

  .chatw__form { padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px)); }
  .chatw__foot { padding-bottom: calc(11px + env(safe-area-inset-bottom, 0px)); }
  .chatw__msg { max-width: 92%; }
}

/* The portal's mobile tab bar owns the bottom edge below 1024px — lift the
   launcher clear of it. :has() is the only way to know the bar is there from
   a stylesheet the layout does not own; where it is unsupported the launcher
   simply keeps its default position. */
@media (max-width: 1024px) {
  body:has(.mobile-nav) .chatw__launch {
    bottom: calc(62px + env(safe-area-inset-bottom, 0px));
  }
}

/* ── Dark mode (portal only — the public site never sets data-theme) ─────── */
html[data-theme="dark"] .chatw__panel {
  border-color: var(--border);
  box-shadow: 0 28px 64px -18px rgba(0, 0, 0, .72), 0 0 0 1px rgba(255, 255, 255, .04);
}
html[data-theme="dark"] .chatw__head {
  background: linear-gradient(135deg, var(--slate-800) 0%, var(--ink-2) 100%);
}
html[data-theme="dark"] .chatw__bar {
  background: linear-gradient(90deg, var(--slate-800) 0%, var(--slate-700) 50%, var(--slate-800) 100%);
  background-size: 200% 100%;
}
html[data-theme="dark"] .chatw__msg--bot .chatw__bubble { background: var(--surface); }
html[data-theme="dark"] .chatw__msg--user .chatw__bubble { background: var(--green-600); color: #fff; }
html[data-theme="dark"] .chatw__badge--kb { background: rgba(18, 148, 92, .18); color: var(--green-500); }
html[data-theme="dark"] .chatw__badge--ai { background: rgba(29, 95, 191, .2); color: #7fb0f5; }
html[data-theme="dark"] .chatw__chip:hover,
html[data-theme="dark"] .chatw__rate-btn:hover { background: rgba(18, 148, 92, .16); color: var(--green-500); }
html[data-theme="dark"] .chatw__rate-btn--down:hover { background: rgba(179, 38, 30, .18); color: #f08c86; }
html[data-theme="dark"] .chatw__bubble a { color: var(--green-500); }
html[data-theme="dark"] .chatw__input:focus { box-shadow: 0 0 0 3px rgba(18, 148, 92, .22); }
html[data-theme="dark"] .chatw__source:hover { color: var(--green-500); }

/* ── Motion & print ─────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .chatw__panel, .chatw__scrim { animation: none; }
  .chatw__launch { transition: none; }
  .chatw__launch:hover { transform: none; }
  .chatw__bar, .chatw__dots i { animation: none; }
  .chatw__dots i { opacity: .6; }
}

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