/* OrbitPilot global chat widget — Workstream A (2026-07-25).
   A fixed, NON-blocking, movable/collapsible panel usable across the whole site.
   Scoped under .op-widget so it never collides with page styles. */

/* Border-box everywhere in the widget so the 1px border + padding count INSIDE
   the width — otherwise "width:100%/100vw + border" overflows the screen (the
   iPhone bug where the panel edge sat just past the screen). */
.op-widget-fab,
.op-widget,
.op-widget * { box-sizing: border-box; }

.op-widget-fab {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 1050;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  font-size: 26px;
  line-height: 1;
  color: #fff;
  background: radial-gradient(circle at 30% 30%, #6d5efc, #3b2fb0);
  box-shadow: 0 6px 20px rgba(59, 47, 176, .45);
  transition: transform .15s ease, box-shadow .15s ease;
}
.op-widget-fab:hover { transform: scale(1.06); box-shadow: 0 8px 26px rgba(59, 47, 176, .6); }
.op-widget-fab.op-hidden { display: none; }

/* ── Proactive attention nudge for new visitors (2026-07-28) ───────────────
   The FAB gently wobbles + a small "New here?" bubble appears a few seconds
   after landing; guests only, homepage only, once per browser (see JS). */
@keyframes op-fab-wobble {
  0%, 100% { transform: rotate(0) scale(1); }
  15% { transform: rotate(-12deg) scale(1.08); }
  30% { transform: rotate(10deg) scale(1.08); }
  45% { transform: rotate(-8deg) scale(1.06); }
  60% { transform: rotate(6deg) scale(1.04); }
  75% { transform: rotate(-3deg) scale(1.02); }
}
.op-widget-fab.op-nudge {
  animation: op-fab-wobble 1.1s ease-in-out infinite;
  box-shadow: 0 0 0 6px rgba(109, 94, 252, .18), 0 8px 26px rgba(59, 47, 176, .6);
}

.op-nudge-bubble {
  position: fixed;
  right: 22px;
  bottom: 88px;                 /* sits just above the 56px FAB */
  z-index: 1052;
  max-width: min(260px, calc(100vw - 44px));
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 12px 12px 14px;
  background: #fff;
  color: #2a2450;
  border: 1px solid #e2e0f0;
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(59, 47, 176, .28);
  font-size: 14px;
  line-height: 1.35;
  cursor: pointer;
  animation: op-nudge-pop .28s ease-out both;
}
.op-nudge-bubble[hidden] { display: none; }
.op-nudge-bubble::after {        /* tail pointing down toward the FAB */
  content: "";
  position: absolute;
  right: 24px;
  bottom: -7px;
  width: 14px; height: 14px;
  background: #fff;
  border-right: 1px solid #e2e0f0;
  border-bottom: 1px solid #e2e0f0;
  transform: rotate(45deg);
}
@keyframes op-nudge-pop {
  from { opacity: 0; transform: translateY(8px) scale(.96); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.op-nudge-text { flex: 1 1 auto; }
.op-nudge-x {
  flex: 0 0 auto;
  border: none;
  background: transparent;
  color: #9a95c0;
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 6px;
}
.op-nudge-x:hover { background: #f0eefb; color: #5a52b5; }

/* ── Guest sign-up CTA inside the widget (shown at the free-preview limit) ── */
.op-guest-cta {
  margin: 4px 12px 10px;
  padding: 12px 14px;
  background: linear-gradient(135deg, #f3f1ff, #eaf6f7);
  border: 1px solid #d9d5f5;
  border-radius: 12px;
  text-align: center;
}
.op-guest-cta[hidden] { display: none; }
.op-guest-cta-lead { margin: 0 0 8px; font-size: 13px; color: #4a4470; }
.op-guest-cta-btn {
  display: inline-block;
  padding: 8px 16px;
  background: radial-gradient(circle at 30% 30%, #6d5efc, #3b2fb0);
  color: #fff;
  text-decoration: none;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
}
.op-guest-cta-btn:hover { color: #fff; box-shadow: 0 6px 18px rgba(59, 47, 176, .45); }

@media (prefers-reduced-motion: reduce) {
  .op-widget-fab.op-nudge { animation: none; }
  .op-nudge-bubble { animation: none; }
}

.op-widget {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 1051;
  width: 380px;
  max-width: calc(100vw - 24px);
  height: 560px;
  max-height: calc(100vh - 48px);
  display: none;
  flex-direction: column;
  background: #fff;
  border: 1px solid #e2e0f0;
  border-radius: 14px;
  box-shadow: 0 12px 44px rgba(30, 20, 90, .28);
  overflow: hidden;
  font-size: 14px;
}
.op-widget.op-open { display: flex; }
.op-widget.op-collapsed { height: auto !important; }
.op-widget.op-collapsed .op-widget-body,
.op-widget.op-collapsed .op-widget-input,
.op-widget.op-collapsed .op-widget-toolbar-bottom { display: none; }

/* Header = drag handle */
.op-widget-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  color: #fff;
  background: linear-gradient(135deg, #4338ca, #6d28d9);
  cursor: grab;
  user-select: none;
}
.op-widget-header.op-dragging { cursor: grabbing; }
.op-widget-title { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op-widget-header button {
  background: rgba(255, 255, 255, .16);
  border: none; color: #fff; cursor: pointer;
  width: 26px; height: 26px; border-radius: 6px; font-size: 14px; line-height: 1;
}
.op-widget-header button:hover { background: rgba(255, 255, 255, .3); }
/* A− / A+ chat-font buttons: a hair wider so the two glyphs breathe. */
.op-widget-header .op-font-btn { width: auto; min-width: 26px; padding: 0 7px; font-size: 13px; font-weight: 700; }
#opWidgetMenu { font-size: 16px; }

/* Hamburger menu — overlays the panel (instructions · downloads · memory). */
.op-menu { position: absolute; inset: 0; z-index: 6; background: #fff; display: flex; flex-direction: column; }
.op-menu[hidden] { display: none; }
.op-menu-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; color: #fff; font-weight: 600;
  background: linear-gradient(135deg, #4338ca, #6d28d9);
}
.op-menu-head button {
  background: rgba(255, 255, 255, .16); border: none; color: #fff; cursor: pointer;
  width: 26px; height: 26px; border-radius: 6px; font-size: 14px; line-height: 1;
}
.op-menu-head button:hover { background: rgba(255, 255, 255, .3); }
.op-menu-body { flex: 1; overflow-y: auto; padding: 14px; background: #faf9ff; }
.op-menu-sec { margin-bottom: 18px; }
.op-menu-sec h4 { font-size: 14px; font-weight: 700; margin: 0 0 4px; color: #1f2937; }
.op-menu-hint { font-size: 12px; color: #6b7280; margin: 0 0 8px; line-height: 1.45; }
.op-menu-sec textarea {
  width: 100%; resize: vertical; min-height: 84px; border: 1px solid #d8d4f0;
  border-radius: 9px; padding: 8px 10px; font-size: 13px; font-family: inherit; line-height: 1.4;
}
.op-menu-actions { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.op-menu-btn {
  display: block; width: 100%; text-align: left; margin-bottom: 6px;
  border: 1px solid #d8d4f0; background: #fff; color: #4338ca; cursor: pointer;
  border-radius: 8px; padding: 8px 11px; font-size: 13px;
}
.op-menu-btn:hover { background: #efedfb; }
.op-menu-btn-go { width: auto; display: inline-block; margin: 0; background: #4338ca; color: #fff; border-color: #4338ca; }
.op-menu-btn-go:hover { background: #3730a3; }
.op-menu-btn-warn { color: #b45309; border-color: #fcd9a6; }
.op-menu-btn-warn:hover { background: #fff7ed; }
.op-menu-status { font-size: 12px; color: #6b7280; }
.op-menu-links { display: flex; flex-direction: column; gap: 6px; }
.op-menu-link {
  display: block; text-decoration: none; border: 1px solid #d8d4f0; background: #fff;
  color: #4338ca; border-radius: 8px; padding: 8px 11px; font-size: 13px;
}
.op-menu-link:hover { background: #efedfb; }
.op-mem-status { font-size: 12.5px; margin-bottom: 8px; padding: 7px 10px; border-radius: 8px; background: #f3f1fb; color: #4b5563; }
.op-mem-status.op-mem-ok { background: #e8f5e9; color: #256029; }
.op-mem-status.op-mem-busy { background: #fff7ed; color: #9a3412; }
.op-mem-status.op-mem-fail { background: #fef2f2; color: #991b1b; }
.op-menu-locked { font-size: 13px; color: #6b7280; background: #f3f1fb; border: 1px dashed #d8d4f0; border-radius: 9px; padding: 10px; }
.op-menu-locked[hidden] { display: none; }

.op-widget-toolbar {
  display: flex; gap: 6px; flex-wrap: wrap;
  padding: 6px 10px;
  background: #f6f5fc;
  border-bottom: 1px solid #eceafa;
}
.op-widget-toolbar-bottom { border-top: 1px solid #eceafa; border-bottom: none; }
.op-widget-toolbar button {
  font-size: 12px; padding: 3px 9px; border-radius: 6px;
  border: 1px solid #d8d4f0; background: #fff; color: #4338ca; cursor: pointer;
}
.op-widget-toolbar button:hover { background: #efedfb; }
.op-widget-toolbar button.op-stop { color: #b42318; border-color: #f3c5bf; display: none; }
.op-widget-toolbar button.op-stop.op-show { display: inline-block; }

.op-widget-body {
  flex: 1; overflow-y: auto; padding: 12px;
  background: #faf9ff;
  display: flex; flex-direction: column; gap: 10px;
  /* Chat font size is user-adjustable (A− / A+ in the header); the value is a
     CSS var set from localStorage in the JS. Messages inherit it from here. */
  font-size: var(--op-chat-font, 14px);
}
.op-msg { position: relative; max-width: 88%; padding: 8px 12px; border-radius: 12px; line-height: 1.45; word-wrap: break-word; }
.op-msg-user { align-self: flex-end; background: #4338ca; color: #fff; border-bottom-right-radius: 4px; }
.op-msg-ai { align-self: flex-start; background: #fff; color: #1f2937; border: 1px solid #ece9fb; border-bottom-left-radius: 4px; }
.op-msg-ai p:last-child, .op-msg-ai ul:last-child, .op-msg-ai ol:last-child { margin-bottom: 0; }
.op-msg-ai pre { white-space: pre-wrap; background: #f3f1fb; padding: 8px; border-radius: 6px; overflow-x: auto; }
.op-msg-copy {
  position: absolute; top: 4px; right: 4px;
  opacity: 0; transition: opacity .12s ease;
  background: rgba(0, 0, 0, .06); border: none; cursor: pointer;
  border-radius: 5px; width: 22px; height: 22px; font-size: 12px; line-height: 1;
}
.op-msg:hover .op-msg-copy { opacity: .8; }
.op-msg-user .op-msg-copy { background: rgba(255, 255, 255, .28); color: #fff; }

.op-actions-label { font-size: 11px; color: #8b8ba7; margin-top: 8px; margin-bottom: 3px; }
.op-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.op-actions a, .op-actions button {
  font-size: 12px; padding: 6px 11px; border-radius: 14px; min-height: 32px;
  border: 1px solid #d8d4f0; background: #fff; color: #4338ca; text-decoration: none; cursor: pointer;
}
.op-actions button:active { transform: scale(0.96); background: #ede9fe; }
.op-actions button.op-action-picked { background: #4338ca; border-color: #4338ca; color: #fff; }
.op-actions button:disabled:not(.op-action-picked) { opacity: 0.45; cursor: default; }

.op-typing { align-self: flex-start; color: #8b8ba7; font-style: italic; font-size: 13px; }

/* Answer feedback (👍/👎) row under each AI message — Phase 7 signal. */
.op-feedback { display: flex; align-items: center; gap: 4px; margin-top: 6px; }
.op-fb {
  border: 1px solid #e2e0f0; background: #fff; cursor: pointer;
  border-radius: 999px; width: 26px; height: 24px; font-size: 12px; line-height: 1; padding: 0;
  opacity: .65; transition: opacity .12s ease, background .12s ease;
}
.op-fb:hover { opacity: 1; background: #f3f1fb; }
.op-fb:disabled { cursor: default; }
.op-fb-picked { opacity: 1; background: #e8f5e9; border-color: #b9e2bd; }
.op-fb-thanks { font-size: 11px; color: #8b8ba7; margin-left: 4px; }

/* align-items:flex-end keeps Send aligned to the textarea's bottom as it grows;
   flex-wrap:nowrap keeps the row on one line (mic + Send stay beside the box). */
.op-widget-input { padding: 10px; border-top: 1px solid #eceafa; background: #fff; }
/* ChatGPT/Gemini-style composer: the mic + Send buttons sit INSIDE the text box
   (bottom-right), and the textarea reserves right padding so text never runs under
   them. min-width:0 keeps it shrinkable on a narrow phone. */
.op-composer { position: relative; }
.op-composer textarea {
  display: block; width: 100%; min-width: 0; box-sizing: border-box;
  resize: vertical; border: 1px solid #d8d4f0; border-radius: 14px;
  padding: 10px 84px 10px 13px; font-size: var(--op-chat-font, 14px); line-height: 1.4;
  min-height: 48px; max-height: 300px; font-family: inherit;
}
.op-composer-btns { position: absolute; right: 6px; bottom: 6px; display: flex; align-items: center; gap: 4px; }
.op-composer-btns button {
  border: none; background: #4338ca; color: #fff; border-radius: 50%;
  width: 34px; height: 34px; padding: 0; cursor: pointer; font-weight: 600;
  font-size: 15px; line-height: 1; display: flex; align-items: center; justify-content: center;
}
.op-composer-btns button:disabled { opacity: .5; cursor: default; }
.op-composer-btns button.op-mic { background: #efedfb; color: #4338ca; }
.op-composer-btns button.op-mic.op-mic-active { background: #b42318; color: #fff; animation: op-mic-pulse 1.1s ease-in-out infinite; }
@keyframes op-mic-pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(180,35,24,.5); } 50% { box-shadow: 0 0 0 6px rgba(180,35,24,0); } }

/* Maximize — fill (almost) the whole screen. */
.op-widget.op-maximized {
  width: calc(100vw - 20px); height: calc(100vh - 20px);
  left: 10px !important; top: 10px !important; right: auto; bottom: auto;
  max-width: none; max-height: none;
}
.op-widget.op-maximized .op-widget-header { cursor: default; }

/* Full-page mode (/orbitpilot/chat/) — the widget fills the whole screen and the
   floating bubble is hidden. Reuses the same markup/JS; only the layout changes. */
body.op-fullpage .op-widget {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
  max-width: none; max-height: none;
  left: 0 !important; top: 0 !important; right: 0 !important; bottom: 0 !important;
  border: 0; border-radius: 0;
  display: flex;
}
body.op-fullpage .op-widget-fab { display: none; }
body.op-fullpage .op-widget-header { cursor: default; }
body.op-fullpage .op-widget-body { max-width: 900px; width: 100%; margin: 0 auto; }
body.op-fullpage .op-widget-input,
body.op-fullpage .op-widget-toolbar { max-width: 900px; margin-left: auto; margin-right: auto; width: 100%; }

/* History list (rendered inside the message area). */
.op-history-head {
  font-weight: 600; color: #4338ca; padding: 2px 2px 8px;
  border-bottom: 1px solid #eceafa; display: flex; align-items: center; justify-content: space-between;
}
.op-history-head button { background: none; border: none; color: #4338ca; cursor: pointer; font-size: 12px; }
.op-history-item {
  display: block; width: 100%; text-align: left; background: #fff;
  border: 1px solid #ece9fb; border-radius: 9px; padding: 8px 10px; cursor: pointer;
}
.op-history-item:hover { background: #f3f1fb; }
.op-history-title { font-size: 13px; color: #1f2937; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op-history-date { font-size: 11px; color: #8b8ba7; margin-top: 2px; }
.op-history-empty { color: #8b8ba7; font-size: 13px; padding: 12px 2px; }

/* Mobile: dock to the bottom, no drag. Width comes from left/right (NOT 100vw,
   which includes the scrollbar gutter and overflowed). Safe-area insets keep it
   clear of the notch (landscape) and the home indicator; dvh keeps the iOS URL
   bar from clipping it. box-sizing:border-box (above) means the border stays in. */
@media (max-width: 575px) {
  .op-widget {
    left: env(safe-area-inset-left, 0px) !important;
    right: env(safe-area-inset-right, 0px) !important;
    bottom: 0 !important; top: auto !important;
    width: auto !important; max-width: none !important;
    height: 82vh;            /* fallback for browsers without dvh */
    height: 82dvh;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 14px 14px 0 0;
  }
  /* Maximized on a phone = edge-to-edge, using dynamic viewport height. */
  .op-widget.op-maximized {
    left: env(safe-area-inset-left, 0px) !important;
    right: env(safe-area-inset-right, 0px) !important;
    top: 0 !important; bottom: 0 !important;
    width: auto !important; height: auto !important;
    max-height: 100dvh; border-radius: 0;
  }
  /* Lift the composer above the home indicator so Send is always tappable. */
  .op-widget-input { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  .op-widget-header { cursor: default; }

  /* P1 fix (ledger widget-close-clipped-mobile, 2026-08): 8 fixed-width header
     controls overflowed narrow phones and pushed ✕ off-screen — users dragged
     sideways to find Close. Thin the nice-to-have controls on phones; ✕ always
     visible with a full 44px touch target. */
  .op-widget-header #opFontDown,
  .op-widget-header #opFontUp,
  .op-widget-header #opWidgetMax,
  .op-widget-header #opWidgetCollapse { display: none; }
  .op-widget-title { min-width: 0; }
  .op-widget-header #opWidgetClose {
    width: 44px;
    height: 44px;
    font-size: 18px;
    flex: 0 0 auto;
  }
}

/* Dark theme parity for viewers in dark mode. */
@media (prefers-color-scheme: dark) {
  .op-widget { background: #16151f; border-color: #2c2a3d; }
  .op-widget-body { background: #100f18; }
  .op-msg-ai { background: #1e1d2b; color: #e7e6f0; border-color: #2c2a3d; }
  .op-widget-input { background: #16151f; border-color: #2c2a3d; }
  .op-composer textarea { background: #1e1d2b; color: #e7e6f0; border-color: #2c2a3d; }
  .op-widget-toolbar { background: #1a1926; border-color: #2c2a3d; }
  .op-widget-toolbar button { background: #1e1d2b; color: #b9b2f5; border-color: #35324a; }
  .op-menu { background: #16151f; }
  .op-menu-body { background: #100f18; }
  .op-menu-sec h4 { color: #e7e6f0; }
  .op-menu-sec textarea { background: #1e1d2b; color: #e7e6f0; border-color: #2c2a3d; }
  .op-menu-btn { background: #1e1d2b; color: #b9b2f5; border-color: #35324a; }
  .op-menu-btn-go { background: #4338ca; color: #fff; }
  .op-menu-link { background: #1e1d2b; color: #b9b2f5; border-color: #35324a; }
  .op-mem-status { background: #1a1926; color: #b9b2f5; }
  .op-menu-locked { background: #1a1926; border-color: #35324a; color: #9aa0ac; }
}
