/* =====================================================================
 * Jimmy — Research Experts' customer chat widget.
 *
 * Design language is pulled straight from the public storefront's token
 * system (re-theme/assets/css/index.css): the same palette, the same
 * Fraunces / Source Sans 3 / JetBrains Mono type roles, the same
 * radius / shadow / motion scale. The dashboard DROPS the theme CSS +
 * webfonts (re-theme/inc/enqueue.php returns early on is_my_account_page),
 * so this file is fully self-contained: it self-hosts the three brand
 * webfaces (latin subset) and re-declares the tokens it needs under a
 * private --jmy-* namespace so nothing here can collide with dashboard CSS.
 *
 * Everything is scoped under #jimmy-root. Prefix for classes: .jmy-*.
 * =================================================================== */

/* ---- Self-hosted brand webfaces (copied from re-theme, latin subset) ---- */
@font-face {
  font-family: 'Jimmy Fraunces';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('../fonts/fraunces-normal-400_700-latin.woff2') format('woff2');
}
@font-face {
  font-family: 'Jimmy Sans';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('../fonts/source-sans-3-normal-400_700-latin.woff2') format('woff2');
}
@font-face {
  font-family: 'Jimmy Mono';
  font-style: normal;
  font-weight: 500;
  font-display: swap;
  src: url('../fonts/jetbrains-mono-normal-500-latin.woff2') format('woff2');
}

/* ---- Token namespace (light-first) ------------------------------------ */
#jimmy-root {
  /* palette — mirrors re-theme core */
  --jmy-green:      #10C020;   /* logo exact */
  --jmy-green-deep: #0A6F17;
  --jmy-forest:     #063D0D;
  --jmy-ink:        #0B0E0C;
  --jmy-paper:      #FAFAF7;
  --jmy-elevated:   #FFFFFF;
  --jmy-bone:       #F2F1EC;
  --jmy-stone:      #E6E4DB;
  --jmy-ash:        #B4B2A7;
  --jmy-slate:      #5B5E57;
  --jmy-oxblood:    #8A1F2B;
  --jmy-parchment:  #E8DFC4;

  /* semantic surfaces */
  --jmy-surface:      var(--jmy-paper);
  --jmy-surface-2:    var(--jmy-elevated);
  --jmy-surface-mute: var(--jmy-bone);
  --jmy-border:       var(--jmy-stone);
  --jmy-border-soft:  rgba(11,14,12,0.07);
  --jmy-fg1:          var(--jmy-ink);
  --jmy-fg2:          var(--jmy-slate);
  --jmy-fg3:          var(--jmy-ash);
  --jmy-accent:       var(--jmy-green);
  --jmy-accent-hover: var(--jmy-green-deep);
  --jmy-accent-fg:    #FFFFFF;

  /* message bubbles — the human speaks in ink on the page,
     Jimmy answers in the paper's own voice with a green identity marker */
  --jmy-you-bg:   #16201A;
  --jmy-you-fg:   #FAFAF7;
  --jmy-jim-bg:   var(--jmy-elevated);
  --jmy-jim-fg:   var(--jmy-ink);
  --jmy-jim-brd:  var(--jmy-stone);

  /* type */
  --jmy-font-display: 'Jimmy Fraunces', 'Fraunces', Georgia, 'Times New Roman', serif;
  --jmy-font-sans:    'Jimmy Sans', 'Source Sans 3', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  --jmy-font-mono:    'Jimmy Mono', 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;

  /* radius / shadow / motion — re-theme scale */
  --jmy-r-sm:  4px;
  --jmy-r-md:  6px;
  --jmy-r-lg:  10px;
  --jmy-r-xl:  16px;
  --jmy-r-pill: 999px;
  --jmy-shadow-md: 0 4px 12px rgba(10,20,10,0.06), 0 1px 2px rgba(10,20,10,0.04);
  --jmy-shadow-xl: 0 24px 60px rgba(10,20,10,0.14), 0 6px 16px rgba(10,20,10,0.08);
  --jmy-shadow-focus: 0 0 0 3px rgba(16,192,32,0.35);
  --jmy-ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --jmy-dur-fast: 150ms;
  --jmy-dur-base: 250ms;

  --jmy-z: 2147483000; /* above dashboard chrome */

  position: fixed;
  inset: auto 0 0 auto;
  z-index: var(--jmy-z);
  font-family: var(--jmy-font-sans);
  color: var(--jmy-fg1);
  -webkit-font-smoothing: antialiased;
}

/* ---- Dark theme -------------------------------------------------------
   re-theme ships no dark mode, so this is derived from its ink/forest
   family. Honour OS preference AND an explicit [data-theme] stamp on the
   widget root, so a future dashboard theme toggle can win. */
@media (prefers-color-scheme: dark) {
  #jimmy-root:not([data-theme="light"]) {
    --jmy-green:        #16D42A;
    --jmy-surface:      #0C100B;
    --jmy-surface-2:    #141A12;
    --jmy-surface-mute: #1A2118;
    --jmy-border:       rgba(255,255,255,0.10);
    --jmy-border-soft:  rgba(255,255,255,0.07);
    --jmy-fg1:          #F2F1EC;
    --jmy-fg2:          #B4B2A7;
    --jmy-fg3:          #7C8079;
    --jmy-accent-fg:    #08120A;
    --jmy-you-bg:       #1F3D24;
    --jmy-you-fg:       #EAF7EC;
    --jmy-jim-bg:       #141A12;
    --jmy-jim-fg:       #F2F1EC;
    --jmy-jim-brd:      rgba(255,255,255,0.12);
  }
}
#jimmy-root[data-theme="dark"] {
  --jmy-green:        #16D42A;
  --jmy-surface:      #0C100B;
  --jmy-surface-2:    #141A12;
  --jmy-surface-mute: #1A2118;
  --jmy-border:       rgba(255,255,255,0.10);
  --jmy-border-soft:  rgba(255,255,255,0.07);
  --jmy-fg1:          #F2F1EC;
  --jmy-fg2:          #B4B2A7;
  --jmy-fg3:          #7C8079;
  --jmy-accent-fg:    #08120A;
  --jmy-you-bg:       #1F3D24;
  --jmy-you-fg:       #EAF7EC;
  --jmy-jim-bg:       #141A12;
  --jmy-jim-fg:       #F2F1EC;
  --jmy-jim-brd:      rgba(255,255,255,0.12);
}

#jimmy-root *,
#jimmy-root *::before,
#jimmy-root *::after { box-sizing: border-box; }

/* =====================================================================
 * Signature element — the wax-seal monogram.
 * A green radial "seal" carrying a Fraunces "J". Used as the launcher
 * face, the header identity, and the per-message author mark. It is the
 * one memorable, brand-specific object; everything else stays quiet.
 * =================================================================== */
.jmy-seal {
  --seal-size: 40px;
  position: relative;
  width: var(--seal-size);
  height: var(--seal-size);
  flex: 0 0 var(--seal-size);
  border-radius: var(--jmy-r-pill);
  display: grid;
  place-items: center;
  background:
    radial-gradient(120% 120% at 30% 25%, #3ee056 0%, var(--jmy-green) 45%, var(--jmy-green-deep) 100%);
  box-shadow:
    inset 0 1px 1px rgba(255,255,255,0.45),
    inset 0 -2px 4px rgba(6,61,13,0.5),
    0 2px 6px rgba(6,61,13,0.28);
  color: #fff;
  font-family: var(--jmy-font-display);
  font-weight: 600;
  line-height: 1;
  font-size: calc(var(--seal-size) * 0.52);
  letter-spacing: -0.02em;
  user-select: none;
}
.jmy-seal span { transform: translateY(1px); text-shadow: 0 1px 1px rgba(6,61,13,0.4); }

/* =====================================================================
 * Launcher — a seal that unfurls into a pill on hover/focus.
 * Not a generic round "chat" bubble: it reads "Ask Jimmy" and carries
 * the brand seal + a live presence dot.
 * =================================================================== */
.jmy-launcher {
  position: fixed;
  right: 24px;
  bottom: 24px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border: 1px solid var(--jmy-border);
  background: var(--jmy-surface-2);
  border-radius: var(--jmy-r-pill);
  box-shadow: var(--jmy-shadow-xl);
  cursor: pointer;
  color: var(--jmy-fg1);
  font-family: var(--jmy-font-sans);
  transition: transform var(--jmy-dur-base) var(--jmy-ease),
              box-shadow var(--jmy-dur-base) var(--jmy-ease),
              opacity var(--jmy-dur-base) var(--jmy-ease),
              padding var(--jmy-dur-base) var(--jmy-ease);
}
.jmy-launcher .jmy-seal { --seal-size: 46px; }
.jmy-launcher-label {
  max-width: 0;
  overflow: hidden;
  white-space: nowrap;
  opacity: 0;
  font-size: 15px;
  font-weight: 600;
  padding-right: 0;
  transition: max-width var(--jmy-dur-base) var(--jmy-ease),
              opacity var(--jmy-dur-base) var(--jmy-ease),
              padding-right var(--jmy-dur-base) var(--jmy-ease);
}
.jmy-launcher-label b { font-family: var(--jmy-font-display); font-weight: 600; }
.jmy-launcher:hover,
.jmy-launcher:focus-visible { transform: translateY(-2px); }
.jmy-launcher:hover .jmy-launcher-label,
.jmy-launcher:focus-visible .jmy-launcher-label {
  max-width: 160px;
  opacity: 1;
  padding-right: 10px;
}
.jmy-launcher:focus-visible { outline: none; box-shadow: var(--jmy-shadow-xl), var(--jmy-shadow-focus); }

/* live presence dot on the seal */
.jmy-presence {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #35d84a;
  border: 2.5px solid var(--jmy-surface-2);
  animation: jmy-pulse 2.4s var(--jmy-ease) infinite;
}
@keyframes jmy-pulse {
  0%   { box-shadow: 0 0 0 0 rgba(53,216,74,0.5); }
  70%  { box-shadow: 0 0 0 7px rgba(53,216,74,0); }
  100% { box-shadow: 0 0 0 0 rgba(53,216,74,0); }
}

/* launcher hidden while panel is open */
#jimmy-root.is-open .jmy-launcher {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.6) translateY(10px);
}

/* Slide-in drawers (RelSlide) set body.rel-si-lock while any panel is open.
   Jimmy sits at z-index 2147483000 — far above the drawer's 1090 — so the
   launcher covered the popup's bottom-right Update button. Stand down for as
   long as a drawer is up; the whole widget (open panel included) goes with it,
   since a chat panel over a form the user is filling is the same problem.

   visibility (not just opacity) so the launcher and any open chat inputs leave the
   tab order and the accessibility tree too — opacity:0 alone left a keyboard user
   tabbing onto an invisible, unclickable control, and a screen reader narrating the
   whole widget over the form. Delayed on the way out so the fade still plays. */
body.rel-si-lock #jimmy-root {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity var(--jmy-dur-base, 250ms) var(--jmy-ease, ease),
              visibility 0s linear var(--jmy-dur-base, 250ms);
}

/* =====================================================================
 * Panel
 * =================================================================== */
.jmy-panel {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 400px;
  max-width: calc(100vw - 32px);
  height: 620px;
  max-height: calc(100vh - 48px);
  display: flex;
  flex-direction: column;
  background: var(--jmy-surface);
  border: 1px solid var(--jmy-border);
  border-radius: var(--jmy-r-xl);
  box-shadow: var(--jmy-shadow-xl);
  overflow: hidden;
  transform-origin: bottom right;
  opacity: 0;
  transform: translateY(16px) scale(0.98);
  pointer-events: none;
  transition: opacity var(--jmy-dur-base) var(--jmy-ease),
              transform var(--jmy-dur-base) var(--jmy-ease);
}
#jimmy-root.is-open .jmy-panel {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* ---- Header ---- */
.jmy-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 16px 14px;
  background: var(--jmy-surface-2);
  border-bottom: 1px solid var(--jmy-border);
}
.jmy-header .jmy-seal { --seal-size: 42px; }
.jmy-id { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.jmy-id-name {
  font-family: var(--jmy-font-display);
  font-weight: 600;
  font-size: 21px;
  letter-spacing: -0.01em;
  line-height: 1.1;
  color: var(--jmy-fg1);
}
.jmy-id-sub {
  font-size: 12.5px;
  color: var(--jmy-fg2);
  display: flex;
  align-items: center;
  gap: 5px;
}
.jmy-id-sub .dot { width: 6px; height: 6px; border-radius: 50%; background: #35d84a; }
.jmy-close {
  flex: 0 0 auto;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 1px solid transparent;
  border-radius: var(--jmy-r-md);
  background: transparent;
  color: var(--jmy-fg2);
  cursor: pointer;
  transition: background var(--jmy-dur-fast) var(--jmy-ease), color var(--jmy-dur-fast) var(--jmy-ease);
}
.jmy-close:hover { background: var(--jmy-surface-mute); color: var(--jmy-fg1); }
.jmy-close:focus-visible { outline: none; box-shadow: var(--jmy-shadow-focus); }
.jmy-close svg { width: 16px; height: 16px; }

/* ---- Trust ribbon — the "grounded on your data" cue, set like a
       scholarly footnote label (mono, tracked-out, small). ---- */
.jmy-trust {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 16px;
  background: var(--jmy-surface);
  border-bottom: 1px solid var(--jmy-border-soft);
  font-family: var(--jmy-font-mono);
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--jmy-fg2);
}
.jmy-trust .diamond { color: var(--jmy-accent); font-size: 9px; }

/* ---- Message log ---- */
.jmy-log {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 18px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
  background:
    radial-gradient(60% 40% at 100% 0%, rgba(16,192,32,0.035), transparent 70%),
    var(--jmy-surface);
}
.jmy-log::-webkit-scrollbar { width: 8px; }
.jmy-log::-webkit-scrollbar-thumb { background: var(--jmy-border); border-radius: 8px; border: 2px solid var(--jmy-surface); }

.jmy-msg { display: flex; gap: 9px; max-width: 100%; animation: jmy-rise var(--jmy-dur-base) var(--jmy-ease) both; }
.jmy-msg-jimmy { align-self: flex-start; }
.jmy-msg-you   { align-self: flex-end; flex-direction: row-reverse; }
.jmy-msg .jmy-seal { --seal-size: 26px; margin-top: 2px; }
.jmy-msg-you .jmy-seal { display: none; }

.jmy-stack { display: flex; flex-direction: column; gap: 4px; min-width: 0; max-width: 300px; }
.jmy-bubble {
  padding: 10px 13px;
  border-radius: var(--jmy-r-lg);
  font-size: 14.5px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.jmy-msg-jimmy .jmy-bubble {
  background: var(--jmy-jim-bg);
  color: var(--jmy-jim-fg);
  border: 1px solid var(--jmy-jim-brd);
  border-top-left-radius: var(--jmy-r-sm);
}
.jmy-msg-you .jmy-bubble {
  background: var(--jmy-you-bg);
  color: var(--jmy-you-fg);
  border-top-right-radius: var(--jmy-r-sm);
}
.jmy-bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }

/* timestamp — mono footnote apparatus */
.jmy-time {
  font-family: var(--jmy-font-mono);
  font-size: 10px;
  letter-spacing: 0.04em;
  color: var(--jmy-fg3);
  padding: 0 4px;
}
.jmy-msg-you .jmy-time { text-align: right; }

/* ---- Typing indicator ---- */
.jmy-typing { display: flex; align-items: center; gap: 6px; padding: 11px 14px; }
.jmy-typing i {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--jmy-accent);
  opacity: 0.5;
  animation: jmy-blink 1.2s var(--jmy-ease) infinite;
}
.jmy-typing i:nth-child(2) { animation-delay: 0.18s; }
.jmy-typing i:nth-child(3) { animation-delay: 0.36s; }
@keyframes jmy-blink { 0%,100% { opacity: 0.25; transform: translateY(0); } 50% { opacity: 1; transform: translateY(-3px); } }

/* ---- Error notice + retry ---- */
.jmy-notice {
  align-self: stretch;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 13px;
  border-radius: var(--jmy-r-lg);
  background: color-mix(in srgb, var(--jmy-oxblood) 8%, var(--jmy-surface-2));
  border: 1px solid color-mix(in srgb, var(--jmy-oxblood) 30%, transparent);
  font-size: 13.5px;
  color: var(--jmy-fg1);
  animation: jmy-rise var(--jmy-dur-base) var(--jmy-ease) both;
}
.jmy-notice .ico { color: var(--jmy-oxblood); flex: 0 0 auto; margin-top: 1px; }
.jmy-notice-body { flex: 1; }
.jmy-retry {
  margin-top: 7px;
  display: inline-flex; align-items: center; gap: 6px;
  font-family: var(--jmy-font-sans);
  font-size: 13px; font-weight: 600;
  color: var(--jmy-accent-hover);
  background: none; border: none; padding: 0; cursor: pointer;
  text-decoration: underline; text-underline-offset: 2px;
}
.jmy-retry:focus-visible { outline: none; box-shadow: var(--jmy-shadow-focus); border-radius: 3px; }

/* ---- Escalation card — a distinct affordance, not a normal bubble ---- */
.jmy-escalate {
  align-self: stretch;
  border-radius: var(--jmy-r-lg);
  border: 1px solid color-mix(in srgb, var(--jmy-accent) 40%, var(--jmy-border));
  background:
    linear-gradient(180deg, color-mix(in srgb, var(--jmy-accent) 9%, var(--jmy-surface-2)), var(--jmy-surface-2));
  padding: 14px;
  animation: jmy-rise var(--jmy-dur-base) var(--jmy-ease) both;
}
.jmy-escalate-eyebrow {
  font-family: var(--jmy-font-mono);
  font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--jmy-accent-hover);
  display: flex; align-items: center; gap: 6px;
  margin-bottom: 6px;
}
.jmy-escalate-title { font-family: var(--jmy-font-display); font-size: 17px; font-weight: 600; color: var(--jmy-fg1); }
.jmy-escalate-text { font-size: 13.5px; color: var(--jmy-fg2); margin-top: 3px; line-height: 1.5; }
.jmy-escalate-btn {
  margin-top: 11px;
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 15px;
  border-radius: var(--jmy-r-pill);
  background: var(--jmy-accent);
  color: var(--jmy-accent-fg);
  font-weight: 600; font-size: 14px;
  text-decoration: none;
  border: none; cursor: pointer;
  transition: transform var(--jmy-dur-fast) var(--jmy-ease), background var(--jmy-dur-fast) var(--jmy-ease);
}
.jmy-escalate-btn:hover { transform: translateY(-1px); background: var(--jmy-accent-hover); }
.jmy-escalate-btn:focus-visible { outline: none; box-shadow: var(--jmy-shadow-focus); }
.jmy-escalate-btn svg { width: 16px; height: 16px; }

/* ---- Composer ---- */
.jmy-composer {
  border-top: 1px solid var(--jmy-border);
  background: var(--jmy-surface-2);
  padding: 12px 12px 13px;
}
.jmy-inputwrap {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--jmy-surface);
  border: 1px solid var(--jmy-border);
  border-radius: var(--jmy-r-lg);
  padding: 6px 6px 6px 12px;
  transition: border-color var(--jmy-dur-fast) var(--jmy-ease), box-shadow var(--jmy-dur-fast) var(--jmy-ease);
}
.jmy-inputwrap:focus-within { border-color: var(--jmy-accent); box-shadow: var(--jmy-shadow-focus); }
.jmy-input {
  flex: 1;
  border: none;
  background: none;
  resize: none;
  font-family: var(--jmy-font-sans);
  font-size: 14.5px;
  line-height: 1.45;
  color: var(--jmy-fg1);
  max-height: 120px;
  padding: 5px 0;
  outline: none;
}
.jmy-input::placeholder { color: var(--jmy-fg3); }
.jmy-send {
  flex: 0 0 auto;
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: var(--jmy-r-md);
  background: var(--jmy-you-bg);
  color: #fff;
  border: none;
  cursor: pointer;
  transition: transform var(--jmy-dur-fast) var(--jmy-ease), opacity var(--jmy-dur-fast) var(--jmy-ease), background var(--jmy-dur-fast) var(--jmy-ease);
}
.jmy-send:hover:not(:disabled) { transform: translateY(-1px); }
.jmy-send:focus-visible { outline: none; box-shadow: var(--jmy-shadow-focus); }
.jmy-send:disabled { opacity: 0.4; cursor: not-allowed; }
.jmy-send svg { width: 17px; height: 17px; }
.jmy-foot {
  margin: 8px 2px 0;
  font-family: var(--jmy-font-mono);
  font-size: 9.5px; letter-spacing: 0.05em;
  color: var(--jmy-fg3);
  text-align: center;
}

/* =====================================================================
 * Mobile — full-height sheet
 * =================================================================== */
@media (max-width: 520px) {
  .jmy-panel {
    right: 0; bottom: 0;
    width: 100vw; max-width: 100vw;
    height: 100dvh; max-height: 100dvh;
    border-radius: 0;
    border: none;
  }
  .jmy-launcher { right: 16px; bottom: 16px; }
}

/* =====================================================================
 * Reduced motion
 * =================================================================== */
@keyframes jmy-rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  #jimmy-root *,
  #jimmy-root *::before,
  #jimmy-root *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
