/* ==========================================================================
   Formi, the assistant (assets/js/formi-chat.js): the docked Formi, the
   "Ask Formi" launcher and the chat panel. Loaded on every page by layout.php.
   ========================================================================== */

/* ---------- docked Formi (bottom corner of app pages) ---------- */
.fc-dock {
  --fm-s: 1; position: fixed; right: 14px; bottom: calc(10px + env(safe-area-inset-bottom)); z-index: 880; width: calc(84px * var(--fm-s)); height: calc(88px * var(--fm-s));
  cursor: pointer; -webkit-tap-highlight-color: transparent;
  animation: fcDockIn .7s var(--spring) both .5s;
}
[dir="rtl"] .fc-dock { right: auto; left: 14px; }
@keyframes fcDockIn { from { transform: translateY(130px) scale(.6); } }
.fc-dock .mascot { width: 100% !important; height: 100% !important; filter: drop-shadow(0 8px 14px rgba(19, 33, 44, .22)); }
.fc-dock .m-frame { overflow: visible !important; }
.fc-dock .m-bubble {
  left: auto; right: 92%; top: -14%; max-width: min(240px, 62vw); font-size: 12.5px;
  border-radius: 16px 16px 5px 16px; transform-origin: 100% 100%;
}
[dir="rtl"] .fc-dock .m-bubble { right: auto; left: 92%; border-radius: 16px 16px 16px 5px; transform-origin: 0 100%; }
.fc-hint {
  position: absolute; right: 78%; bottom: 8px; display: inline-flex; align-items: center; gap: 6px;
  height: 30px; padding: 0 12px; border-radius: var(--radius-full); white-space: nowrap;
  background: var(--surface); color: var(--text); border: 1.5px solid var(--line); box-shadow: 2px 2px 0 var(--line);
  font: 700 12.5px var(--font-display);
  opacity: 0; transform: translateX(10px) scale(.9); transform-origin: 100% 50%;
  transition: opacity .18s ease, transform .3s var(--spring); pointer-events: none;
}
.fc-hint .icon { color: var(--brand); }
[dir="rtl"] .fc-hint { right: auto; left: 78%; transform-origin: 0 50%; }
.fc-dock:hover .fc-hint, .fc-dock:focus-within .fc-hint, .fc-dock.is-hinting .fc-hint { opacity: 1; transform: none; }
body.fc-is-open .fc-hint { opacity: 0 !important; }
@media (hover: none) { .fc-hint { display: none; } }

/* keep page content clear of the dock */
body:has(> .fc-dock) :is(.dashboard, .admin-main, .acct, .ed-main, .lp) { padding-bottom: 96px; }

/* ---------- plain launcher (pages without a Formi on screen) ---------- */
.fc-launch {
  position: fixed; right: 16px; bottom: 16px; z-index: 880; display: inline-flex; align-items: center; gap: 8px;
  height: 44px; padding: 0 16px 0 8px; border-radius: var(--radius-full);
  background: var(--surface); color: var(--text); border: 1.5px solid var(--line); box-shadow: 3px 3px 0 var(--line);
  font: 700 13.5px var(--font-display); cursor: pointer;
  transition: transform .25s var(--spring), box-shadow .2s;
  animation: fcDockIn .6s var(--spring) both .4s;
}
.fc-launch:hover { transform: translate(-1px, -2px); box-shadow: 4px 5px 0 var(--line); }
[dir="rtl"] .fc-launch { right: auto; left: 16px; }
body.fc-is-open .fc-launch { opacity: 0; pointer-events: none; }

/* little Formi face used in the header and launcher */
.fc-avatar {
  position: relative; flex: none; width: 38px; height: 38px; border-radius: 11px;
  background: linear-gradient(160deg, #fffefa, #f1e7d2); border: 2px solid var(--ink);
  box-shadow: inset 0 -3px 0 rgba(19, 33, 44, .08);
}
.fc-avatar::before { content: ""; position: absolute; left: 50%; top: -6px; width: 16px; height: 7px; margin-left: -8px; border-radius: 3px 3px 0 0; background: var(--coral); border: 2px solid var(--ink); border-bottom: 0; }
.fc-avatar .fc-eye { position: absolute; top: 12px; width: 7px; height: 9px; border-radius: 50%; background: var(--ink); animation: fcBlink 4.5s infinite; }
.fc-avatar .fc-eye:first-child { left: 9px; } .fc-avatar .fc-eye:nth-child(2) { right: 9px; }
.fc-avatar .fc-smile { position: absolute; left: 50%; top: 23px; width: 12px; height: 6px; margin-left: -6px; border: 2.2px solid var(--ink); border-top: 0; border-radius: 0 0 8px 8px; }
.fc-avatar.sm { width: 28px; height: 28px; border-radius: 8px; border-width: 1.6px; }
.fc-avatar.sm::before { top: -5px; width: 12px; height: 5px; margin-left: -6px; border-width: 1.6px; }
.fc-avatar.sm .fc-eye { top: 9px; width: 5px; height: 6px; }
.fc-avatar.sm .fc-eye:first-child { left: 6px; } .fc-avatar.sm .fc-eye:nth-child(2) { right: 6px; }
.fc-avatar.sm .fc-smile { top: 16px; width: 9px; height: 4px; margin-left: -4.5px; border-width: 1.6px; }
@keyframes fcBlink { 0%, 94%, 100% { transform: scaleY(1); } 96% { transform: scaleY(.1); } }

/* ---------- the panel ---------- */
.fc-panel {
  position: fixed; right: 18px; bottom: 108px; z-index: 960;
  width: min(392px, calc(100vw - 36px)); height: min(600px, calc(100dvh - 132px));
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); color: var(--text);
  border: 2px solid var(--line); border-radius: 22px; box-shadow: 5px 6px 0 var(--line), var(--shadow-lg);
  opacity: 0; transform: translateY(18px) scale(.94); transform-origin: 92% 100%;
  transition: opacity .18s ease, transform .38s var(--spring);
}
.fc-panel.is-open { opacity: 1; transform: none; }
.fc-panel.is-still { transition: opacity .12s; transform: none; }
[dir="rtl"] .fc-panel { right: auto; left: 18px; transform-origin: 8% 100%; }
body:not(:has(> .fc-dock)) .fc-panel { bottom: 76px; height: min(600px, calc(100dvh - 100px)); }

.fc-head {
  display: flex; align-items: center; gap: 10px; padding: 14px 12px 12px 16px;
  background:
    radial-gradient(120% 140% at 0% 0%, rgba(15, 155, 142, .16), transparent 60%),
    radial-gradient(90% 120% at 100% 0%, rgba(255, 200, 87, .22), transparent 60%),
    var(--surface-2);
  border-bottom: 1.5px solid color-mix(in srgb, var(--line) 18%, transparent);
}
.fc-title { display: flex; flex-direction: column; min-width: 0; margin-right: auto; }
[dir="rtl"] .fc-title { margin-right: 0; margin-left: auto; }
.fc-title strong { font: 800 16px/1.15 var(--font-display); letter-spacing: -.01em; }
.fc-sub { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fc-dot { width: 7px; height: 7px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 3px rgba(34, 197, 94, .18); }
.fc-hbtn {
  width: 32px; height: 32px; display: grid; place-items: center; flex: none; border-radius: 10px;
  color: var(--text-2); background: transparent; border: 0; cursor: pointer; transition: background .15s, color .15s;
}
.fc-hbtn:hover { background: var(--surface-3); color: var(--text); }
.fc-hbtn:focus-visible, .fc-ibtn:focus-visible, .fc-chip:focus-visible, .fc-act:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.fc-hbtn .fc-off { display: none; }
.fc-hbtn.is-off .fc-on { display: none; } .fc-hbtn.is-off .fc-off { display: inline; }
.fc-hbtn.is-off { color: var(--text-3); }

/* messages */
.fc-log {
  flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 10px; scroll-behavior: smooth;
  background:
    radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--text) 7%, transparent) 1px, transparent 0) 0 0 / 18px 18px;
}
.fc-msg {
  max-width: 86%; padding: 10px 13px; border-radius: 16px; font-size: 14px; line-height: 1.5;
  overflow-wrap: anywhere; animation: fcMsgIn .32s var(--spring) both;
}
@keyframes fcMsgIn { from { opacity: 0; transform: translateY(8px) scale(.96); } }
.fc-msg p { margin: 0; } .fc-msg p + p, .fc-msg p + ul, .fc-msg p + ol, .fc-msg ul + p, .fc-msg ol + p { margin-top: 6px; }
.fc-msg ul, .fc-msg ol { margin: 4px 0 0; padding-inline-start: 20px; }
.fc-msg li + li { margin-top: 3px; }
.fc-msg code { font-size: 12.5px; padding: 1px 5px; border-radius: 5px; background: var(--surface-3); }
.fc-msg.is-formi {
  align-self: flex-start; background: var(--surface-2); color: var(--text);
  border: 1.5px solid color-mix(in srgb, var(--line) 22%, transparent); border-bottom-left-radius: 5px;
}
.fc-msg.is-user {
  align-self: flex-end; background: var(--brand); color: #fff; border-bottom-right-radius: 5px;
  box-shadow: 0 6px 14px -8px rgba(15, 155, 142, .8);
}
[dir="rtl"] .fc-msg.is-formi { border-bottom-left-radius: 16px; border-bottom-right-radius: 5px; }
[dir="rtl"] .fc-msg.is-user { border-bottom-right-radius: 16px; border-bottom-left-radius: 5px; }
.fc-msg.is-typing { display: inline-flex; gap: 5px; padding: 13px 15px; }
.fc-msg.is-typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); animation: fcDot 1.1s infinite ease-in-out; }
.fc-msg.is-typing i:nth-child(2) { animation-delay: .15s; } .fc-msg.is-typing i:nth-child(3) { animation-delay: .3s; }
/* form builder progress: "Reading your request…" → "Building the questions…" → … */
.fc-msg.is-typing.is-step { align-items: center; }
.fc-msg.is-typing.is-step::after { content: attr(data-step); margin-inline-start: 6px; font-size: 13px; font-weight: 600; color: var(--text-2); animation: fcStepIn .35s var(--ease, ease) both; }
@keyframes fcStepIn { from { opacity: 0; transform: translateY(3px); } }
/* build mode: the input invites a description of the form */
.fc-panel.is-building .fc-input { border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 18%, transparent); }
@keyframes fcDot { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }

.fc-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.fc-act {
  display: inline-flex; align-items: center; gap: 5px; min-height: 32px; padding: 5px 12px 5px 9px;
  border-radius: var(--radius-full); background: var(--surface); color: var(--text); text-decoration: none;
  border: 1.5px solid var(--line); box-shadow: 2px 2px 0 var(--line);
  font: 700 12.5px var(--font-display); cursor: pointer; transition: transform .2s var(--spring), box-shadow .15s;
}
.fc-act .icon { color: var(--brand); }
.fc-act:hover { transform: translate(-1px, -1px); box-shadow: 3px 3px 0 var(--line); }
.fc-act:active { transform: translate(1px, 1px); box-shadow: 0 0 0 var(--line); }

/* suggestions */
.fc-suggest { display: flex; gap: 6px; padding: 4px 14px 8px; overflow-x: auto; scrollbar-width: none; flex: none; }
.fc-suggest:empty { display: none; }
.fc-suggest::-webkit-scrollbar { display: none; }
.fc-chip {
  flex: none; height: 32px; padding: 0 12px; border-radius: var(--radius-full);
  background: color-mix(in srgb, var(--brand) 10%, var(--surface)); color: var(--text);
  border: 1.5px solid color-mix(in srgb, var(--brand) 40%, transparent);
  font: 600 12.5px var(--font); cursor: pointer; white-space: nowrap; transition: background .15s, transform .2s var(--spring);
}
.fc-chip:hover { background: color-mix(in srgb, var(--brand) 18%, var(--surface)); transform: translateY(-1px); }

/* input */
.fc-input {
  display: flex; align-items: flex-end; gap: 6px; margin: 0 10px 10px; padding: 6px 6px 6px 12px; flex: none;
  border-radius: 16px; background: var(--surface-2); border: 1.5px solid color-mix(in srgb, var(--line) 25%, transparent);
  transition: border-color .15s, box-shadow .15s;
}
[dir="rtl"] .fc-input { padding: 6px 12px 6px 6px; }
.fc-input:focus-within { border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 20%, transparent); }
.fc-text {
  flex: 1; min-width: 0; resize: none; border: 0; outline: 0; background: transparent; color: var(--text);
  font: 400 14.5px/1.45 var(--font); padding: 7px 0; max-height: 120px; box-shadow: none !important;
}
.fc-text::placeholder { color: var(--text-3); }
.fc-ibtn {
  width: 36px; height: 36px; display: grid; place-items: center; flex: none; border-radius: 12px; border: 0; cursor: pointer;
  background: transparent; color: var(--text-2); transition: background .15s, color .15s, transform .2s var(--spring);
}
.fc-ibtn:hover { background: var(--surface-3); color: var(--text); }
.fc-send { background: var(--brand); color: #fff; }
.fc-send:hover { background: var(--brand-deep); color: #fff; transform: scale(1.05); }
.fc-mic.is-live { background: var(--coral); color: #fff; animation: fcPulse 1.2s infinite; }
@keyframes fcPulse { 0% { box-shadow: 0 0 0 0 rgba(255, 122, 89, .55); } 100% { box-shadow: 0 0 0 12px rgba(255, 122, 89, 0); } }

/* Formi thinking (dock and form guide) */
.mascot.is-thinking .m-frame { animation: fcThink 1.4s ease-in-out infinite; }
@keyframes fcThink { 0%, 100% { transform: rotate(0); } 50% { transform: rotate(-4deg) translateY(-2px); } }

/* ---------- dark ---------- */
:root[data-theme="dark"] .fc-panel { box-shadow: 5px 6px 0 #000, var(--shadow-lg); border-color: #2a3c40; }
:root[data-theme="dark"] .fc-msg.is-formi { border-color: #24363a; }
:root[data-theme="dark"] .fc-act, :root[data-theme="dark"] .fc-hint { border-color: #2a3c40; }
:root[data-theme="dark"] .fc-launch { border-color: color-mix(in srgb, var(--brand) 70%, transparent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand) 14%, transparent), 3px 3px 0 #000; }

/* ---------- phones: a bottom sheet ---------- */
@media (max-width: 600px) {
  /* phones keep the whole Formi (move it or resize it from a corner: formi-drag.js) */
  body:has(> .fc-dock) :is(.dashboard, .admin-main, .acct, .ed-main, .lp) { padding-bottom: 80px; }
  .fc-launch { right: 10px; bottom: 10px; height: 42px; }
  .fc-panel, [dir="rtl"] .fc-panel, body:not(:has(> .fc-dock)) .fc-panel {
    left: 0; right: 0; bottom: 0; width: 100%; height: min(86dvh, 640px);
    border-radius: 22px 22px 0 0; border-width: 2px 0 0; box-shadow: 0 -18px 40px -12px rgba(0, 0, 0, .35);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .fc-panel { transform: translateY(40px); transform-origin: 50% 100%; }
  .fc-panel.is-open { transform: none; }
  .fc-head::before { content: ""; position: absolute; left: 50%; top: 6px; width: 38px; height: 4px; margin-left: -19px; border-radius: 3px; background: color-mix(in srgb, var(--text) 22%, transparent); }
  .fc-head { position: relative; padding-top: 18px; }
  .fc-text { font-size: 16px; } /* no zoom on iOS */
  body.fc-is-open .fc-dock { opacity: 0; pointer-events: none; }
}

@media print { .fc-dock, .fc-panel, .fc-launch { display: none !important; } }
@media (prefers-reduced-motion: reduce) {
  .fc-dock, .fc-launch, .fc-msg { animation: none; }
  .fc-avatar .fc-eye, .mascot.is-thinking .m-frame, .fc-msg.is-typing i { animation: none; }
}

/* voice reply state (formi-chat.js sets data-voice on the panel) */
.fc-panel[data-voice="loading"] .fc-dot { background: var(--butter); box-shadow: 0 0 0 3px rgba(255, 200, 87, .25); animation: fcDotPulse 1s ease-in-out infinite; }
.fc-panel[data-voice="speaking"] [data-fc="voice"] { color: var(--brand); background: color-mix(in srgb, var(--brand) 14%, transparent); }
.fc-panel[data-voice="speaking"] [data-fc="voice"] .icon { animation: fcDotPulse .9s ease-in-out infinite; }
@keyframes fcDotPulse { 50% { transform: scale(1.25); } }
@media (prefers-reduced-motion: reduce) { .fc-panel[data-voice] .fc-dot, .fc-panel[data-voice] [data-fc="voice"] .icon { animation: none; } }

/* reply-language switch (Auto / اردو / EN) */
.fc-hbtn.fc-lang { width: auto; min-width: 40px; padding: 0 9px; border-radius: 99px; font-size: 11.5px; font-weight: 800; letter-spacing: .02em; border: 1.5px solid var(--border); }
.fc-hbtn.fc-lang.is-ur { font-family: 'Noto Nastaliq Urdu', var(--font); font-size: 12px; line-height: 1; color: var(--brand); border-color: color-mix(in srgb, var(--brand) 45%, var(--border)); background: color-mix(in srgb, var(--brand) 10%, transparent); }
/* the Voice switch: a labelled pill, so it is clear whether Formi will talk */
.fc-hbtn[data-fc="voice"] { width: auto; padding: 0 10px 0 8px; gap: 5px; display: inline-flex; align-items: center; border-radius: 999px;
  background: color-mix(in srgb, var(--brand) 14%, transparent); color: var(--brand); font: 600 12px/1 var(--font); }
.fc-hbtn[data-fc="voice"].is-off { background: var(--surface-3); color: var(--text-2); }
.fc-vlabel { white-space: nowrap; }
@media (max-width: 380px) { .fc-vlabel { display: none; } .fc-hbtn[data-fc="voice"] { padding: 0 8px; } }
