/**
 * Suniar Operator Assistant — global glass popup (FAB + draggable/resizable panel)
 */

:root {
  --pi-op-z: 10060;
  --pi-op-fab: 3.6rem;
  --pi-op-radius: 1.35rem;
  --pi-op-ink: color-mix(in srgb, #14233F 92%, transparent);
  --pi-op-muted: color-mix(in srgb, #14233F 58%, transparent);
  --pi-op-accent: #204098;
  --pi-op-gold: #F8B810;
  --pi-op-user-bg: color-mix(in srgb, #204098 82%, #0b1426);
  --pi-op-bot-bg: color-mix(in srgb, #ffffff 72%, transparent);
  --pi-op-border: color-mix(in srgb, #204098 12%, rgba(255, 255, 255, 0.45));
}

html.dark {
  --pi-op-ink: rgba(236, 242, 255, 0.94);
  --pi-op-muted: rgba(200, 214, 240, 0.62);
  --pi-op-user-bg: color-mix(in srgb, #204098 70%, #0b1426);
  --pi-op-bot-bg: color-mix(in srgb, #162044 78%, transparent);
  --pi-op-border: rgba(255, 255, 255, 0.14);
}

.pi-op {
  position: fixed;
  inset: 0;
  z-index: var(--pi-op-z);
  pointer-events: none;
}

.pi-op[hidden] {
  display: none !important;
}

.pi-op__fab,
.pi-op__panel {
  pointer-events: auto;
}

/* Header AI chip (next to live metrics) */
.dashboard-topbar-ops-chip--ai {
  appearance: none;
  border: 0;
  cursor: pointer;
  font: inherit;
  text-align: start;
  color: inherit;
  flex-shrink: 0;
  justify-items: stretch !important;
  padding: 0.35rem 0.7rem 0.35rem 0.4rem !important;
  min-width: 0;
  background:
    linear-gradient(135deg, color-mix(in srgb, #F8B810 16%, transparent), transparent 42%),
    color-mix(in srgb, #ffffff 55%, transparent);
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, #F8B810 34%, transparent),
    0 6px 16px rgba(20, 35, 63, 0.1);
  transition: transform 160ms ease, box-shadow 160ms ease, filter 160ms ease;
}

html.dark .dashboard-topbar-ops-chip--ai {
  background:
    linear-gradient(135deg, color-mix(in srgb, #F8B810 18%, transparent), transparent 45%),
    color-mix(in srgb, #162044 70%, transparent);
}

.dashboard-topbar-ops-chip--ai:hover {
  transform: translateY(-1px);
  filter: brightness(1.02);
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, #F8B810 48%, transparent),
    0 10px 20px rgba(20, 35, 63, 0.14);
}

.dashboard-topbar-ops-chip--ai.is-pi-op-active {
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, #F8B810 62%, transparent),
    0 0 0 3px color-mix(in srgb, #F8B810 22%, transparent),
    0 10px 22px rgba(20, 35, 63, 0.16);
}

.dashboard-topbar-ops-chip--ai.is-pi-op-hinting {
  animation: pi-op-chip-pulse 1.55s ease-in-out infinite;
}

.pi-op-chip-face {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.35rem;
  max-width: 9.5rem;
}

.pi-op-chip-mark {
  width: 2.05rem;
  height: 2.05rem;
  border-radius: 0.72rem;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  background:
    radial-gradient(circle at 30% 25%, rgba(255, 255, 255, 0.28), transparent 55%),
    #204098;
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, #F8B810 40%, transparent),
    0 4px 10px rgba(32, 64, 152, 0.28);
}

.pi-op-chip-logo {
  width: 1.2rem;
  height: 1.2rem;
  object-fit: contain;
  border-radius: 0.28rem;
}

.pi-op-chip-name {
  font-size: 0.82rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  color: #14233F;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
  min-width: 0;
}

html[lang="fa"] .pi-op-chip-name,
html[dir="rtl"] .pi-op-chip-name {
  font-size: 0.78rem;
  font-weight: 750;
  max-width: 7.2rem;
}

html.dark .pi-op-chip-name {
  color: rgba(236, 242, 255, 0.96);
}

html[dir="rtl"] .dashboard-topbar-ops-chip--ai {
  padding: 0.35rem 0.4rem 0.35rem 0.65rem !important;
}

@keyframes pi-op-chip-pulse {
  0%,
  100% {
    box-shadow:
      inset 0 0 0 1px color-mix(in srgb, #F8B810 34%, transparent),
      0 0 0 0 color-mix(in srgb, #F8B810 0%, transparent),
      0 6px 16px rgba(20, 35, 63, 0.1);
  }
  50% {
    box-shadow:
      inset 0 0 0 1px color-mix(in srgb, #F8B810 55%, transparent),
      0 0 0 7px color-mix(in srgb, #F8B810 16%, transparent),
      0 10px 20px rgba(20, 35, 63, 0.14);
  }
}

@media (prefers-reduced-motion: reduce) {
  .dashboard-topbar-ops-chip--ai.is-pi-op-hinting {
    animation: none;
  }
}

/* ── FAB (Suniar logo) ── */
.pi-op__fab {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right));
  bottom: calc(1.1rem + env(safe-area-inset-bottom) + var(--app-nav-clearance, 4.75rem));
  width: var(--pi-op-fab);
  height: var(--pi-op-fab);
  border: 0;
  border-radius: 999px;
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  box-shadow:
    0 10px 28px rgba(20, 35, 63, 0.22),
    0 0 0 1px color-mix(in srgb, var(--pi-op-gold) 35%, transparent);
  transition: transform 220ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 220ms ease;
}

body:not(.app-mobile-shell) .pi-op__fab {
  bottom: max(1.25rem, env(safe-area-inset-bottom));
}

.pi-op__fab:hover {
  transform: translateY(-2px) scale(1.03);
  box-shadow:
    0 14px 34px rgba(20, 35, 63, 0.28),
    0 0 0 1px color-mix(in srgb, var(--pi-op-gold) 50%, transparent);
}

.pi-op__fab:focus-visible {
  outline: 2px solid var(--pi-op-gold);
  outline-offset: 3px;
}

.pi-op__fab.liquidGlass-wrapper > .liquidGlass-tint {
  background: var(--lg-tint-fab, color-mix(in srgb, #F8B810 58%, rgba(255, 255, 255, 0.34))) !important;
}

.pi-op__fab-core {
  position: relative;
  z-index: 2;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
}

.pi-op__fab-core img {
  width: 1.85rem;
  height: 1.85rem;
  object-fit: contain;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.18));
}

.pi-op.is-open .pi-op__fab {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.85);
}

/* ── Panel ──
   CRITICAL: .liquidGlass-wrapper forces position:relative !important.
   In RTL that pins the panel to the physical right edge, ignoring our
   viewport left/top math. Keep this panel viewport-fixed. */
.pi-op__panel,
.pi-op__panel.liquidGlass-wrapper {
  position: fixed !important;
  left: 8px;
  top: 72px;
  right: auto !important;
  bottom: auto !important;
  width: min(420px, calc(100vw - 1.5rem));
  height: min(560px, calc(100vh - 5rem));
  min-width: 300px;
  min-height: 360px;
  max-width: min(720px, calc(100vw - 0.75rem));
  max-height: calc(100vh - 1rem);
  display: flex;
  flex-direction: column;
  border-radius: var(--pi-op-radius) !important;
  overflow: hidden !important;
  border: 1px solid var(--pi-op-border);
  box-shadow:
    0 24px 60px rgba(12, 22, 48, 0.28),
    0 2px 0 rgba(255, 255, 255, 0.35) inset;
  transform-origin: top center;
  animation: pi-op-rise 320ms cubic-bezier(0.22, 1, 0.36, 1);
  /* Beat liquid-glass transparent chrome defaults for readability */
  isolation: isolate;
}

body:not(.app-mobile-shell) .pi-op__panel {
  bottom: auto !important;
}

.pi-op__panel.is-positioned {
  right: auto !important;
  bottom: auto !important;
}

.pi-op__panel[hidden] {
  display: none !important;
}

.pi-op__panel.liquidGlass-wrapper > .liquidGlass-tint {
  background: var(--lg-tint-sheet, rgba(255, 255, 255, 0.28)) !important;
}

.pi-op__panel > .liquidGlass-effect,
.pi-op__panel > .liquidGlass-tint,
.pi-op__panel > .liquidGlass-shine {
  border-radius: inherit;
}

.pi-op__chrome,
.pi-op__messages,
.pi-op__composer {
  position: relative;
  z-index: 2;
}

.pi-op__chrome {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 0.9rem 0.7rem;
  cursor: grab;
  user-select: none;
  touch-action: none;
  border-bottom: 1px solid color-mix(in srgb, var(--pi-op-border) 70%, transparent);
  background: color-mix(in srgb, #ffffff 18%, transparent);
}

html.dark .pi-op__chrome {
  background: color-mix(in srgb, #0b1426 35%, transparent);
}

.pi-op__chrome:active {
  cursor: grabbing;
}

.pi-op__brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.pi-op__brand-logo {
  width: 1.75rem;
  height: 1.75rem;
  object-fit: contain;
  flex-shrink: 0;
  border-radius: 0.45rem;
}

.pi-op__brand-copy {
  min-width: 0;
}

.pi-op__title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--pi-op-ink);
  line-height: 1.25;
}

.pi-op__subtitle {
  margin: 0.15rem 0 0;
  font-size: 0.72rem;
  color: var(--pi-op-muted);
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 16rem;
}

.pi-op__actions {
  display: flex;
  gap: 0.25rem;
  flex-shrink: 0;
}

.pi-op__icon-btn {
  width: 2rem;
  height: 2rem;
  border: 0;
  border-radius: 0.65rem;
  background: color-mix(in srgb, #ffffff 35%, transparent);
  color: var(--pi-op-ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 160ms ease, transform 160ms ease;
}

html.dark .pi-op__icon-btn {
  background: color-mix(in srgb, #ffffff 8%, transparent);
}

.pi-op__icon-btn:hover {
  background: color-mix(in srgb, var(--pi-op-gold) 28%, transparent);
  transform: translateY(-1px);
}

.pi-op__messages {
  flex: 1 1 auto;
  overflow: auto;
  padding: 0.85rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  scroll-behavior: smooth;
}

.pi-op__bubble {
  max-width: 92%;
  padding: 0.7rem 0.85rem;
  border-radius: 1rem;
  font-size: 0.86rem;
  line-height: 1.55;
  color: var(--pi-op-ink);
  white-space: pre-wrap;
  word-break: break-word;
}

.pi-op__bubble--user {
  align-self: flex-end;
  background: var(--pi-op-user-bg);
  color: #f7f9ff;
  border-bottom-right-radius: 0.35rem;
}

.pi-op__bubble--assistant {
  align-self: flex-start;
  background: var(--pi-op-bot-bg);
  border: 1px solid color-mix(in srgb, var(--pi-op-border) 80%, transparent);
  border-bottom-left-radius: 0.35rem;
  backdrop-filter: blur(8px);
}

.pi-op__bubble--meta {
  align-self: center;
  max-width: 100%;
  background: transparent;
  border: 0;
  color: var(--pi-op-muted);
  font-size: 0.75rem;
  text-align: center;
  padding: 0.2rem 0.4rem;
}

.pi-op__bubble strong {
  font-weight: 700;
}

.pi-op__h {
  font-weight: 700;
  margin: 0.15rem 0 0.35rem;
  color: var(--pi-op-ink);
}

.pi-op__tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.45rem;
}

.pi-op__tool-chip {
  font-size: 0.65rem;
  padding: 0.12rem 0.4rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--pi-op-accent) 12%, transparent);
  color: var(--pi-op-muted);
  border: 1px solid color-mix(in srgb, var(--pi-op-accent) 18%, transparent);
}

.pi-op__composer {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  padding: 0.7rem 0.85rem calc(0.75rem + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid color-mix(in srgb, var(--pi-op-border) 70%, transparent);
  background: color-mix(in srgb, #ffffff 22%, transparent);
}

html.dark .pi-op__composer {
  background: color-mix(in srgb, #0b1426 40%, transparent);
}

.pi-op__input {
  flex: 1 1 auto;
  min-height: 2.5rem;
  max-height: 7.5rem;
  resize: none;
  border-radius: 0.95rem;
  border: 1px solid color-mix(in srgb, var(--pi-op-border) 85%, transparent);
  background: color-mix(in srgb, #ffffff 55%, transparent);
  color: var(--pi-op-ink);
  padding: 0.65rem 0.8rem;
  font: inherit;
  font-size: 0.88rem;
  line-height: 1.4;
  outline: none;
}

html.dark .pi-op__input {
  background: color-mix(in srgb, #162044 70%, transparent);
}

.pi-op__input:focus {
  border-color: color-mix(in srgb, var(--pi-op-gold) 55%, transparent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--pi-op-gold) 22%, transparent);
}

.pi-op__send {
  flex: 0 0 auto;
  height: 2.5rem;
  min-width: 4.4rem;
  border: 0;
  border-radius: 0.95rem;
  padding: 0 0.95rem;
  background: linear-gradient(145deg, color-mix(in srgb, var(--pi-op-gold) 88%, #fff), var(--pi-op-gold));
  color: #14233f;
  font-weight: 700;
  font-size: 0.84rem;
  cursor: pointer;
  box-shadow: 0 6px 16px color-mix(in srgb, var(--pi-op-gold) 35%, transparent);
  transition: transform 160ms ease, filter 160ms ease;
}

.pi-op__send:hover:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.03);
}

.pi-op__send:disabled {
  opacity: 0.55;
  cursor: wait;
}

.pi-op__resize {
  position: absolute;
  z-index: 3;
  width: 1.1rem;
  height: 1.1rem;
  right: 0.2rem;
  bottom: 0.2rem;
  cursor: nwse-resize;
  touch-action: none;
  background:
    linear-gradient(135deg, transparent 45%, color-mix(in srgb, var(--pi-op-ink) 35%, transparent) 46%, transparent 54%),
    linear-gradient(135deg, transparent 60%, color-mix(in srgb, var(--pi-op-ink) 35%, transparent) 61%, transparent 69%);
  opacity: 0.55;
}

html[dir="rtl"] .pi-op__fab {
  right: auto;
  left: max(1rem, env(safe-area-inset-left));
}

html[dir="rtl"] .pi-op__panel {
  /* Position comes from JS under the header chip — do not pin to a corner. */
  right: auto;
}

html[dir="rtl"] .pi-op__resize {
  right: auto;
  left: 0.2rem;
  cursor: nesw-resize;
}

html[dir="rtl"] .pi-op__bubble--user {
  align-self: flex-start;
  border-bottom-right-radius: 1rem;
  border-bottom-left-radius: 0.35rem;
}

html[dir="rtl"] .pi-op__bubble--assistant {
  align-self: flex-end;
  border-bottom-left-radius: 1rem;
  border-bottom-right-radius: 0.35rem;
}

@keyframes pi-op-rise {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@media (max-width: 640px) {
  .pi-op__panel {
    width: calc(100vw - 0.9rem);
    height: min(72vh, calc(100vh - 5.5rem));
    max-width: none;
  }

  .pi-op__panel:not(.is-positioned) {
    right: 0.45rem;
    left: 0.45rem;
  }

  .pi-op__subtitle {
    max-width: 11rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .pi-op__panel,
  .pi-op__fab,
  .pi-op__send,
  .pi-op__icon-btn,
  .pi-op__coach-card {
    animation: none !important;
    transition: none !important;
  }
}

/* ── First-visit coach mark ── */
.pi-op__coach {
  position: fixed;
  z-index: calc(var(--pi-op-z) + 2);
  pointer-events: none;
  max-width: min(22rem, calc(100vw - 1.25rem));
}

.pi-op__coach:not([hidden]) {
  pointer-events: auto;
}

.pi-op__coach-card {
  position: relative;
  overflow: hidden;
  border-radius: 1.1rem;
  border: 1px solid var(--pi-op-border);
  box-shadow:
    0 18px 42px rgba(12, 22, 48, 0.28),
    0 1px 0 rgba(255, 255, 255, 0.4) inset;
  animation: pi-op-rise 320ms cubic-bezier(0.22, 1, 0.36, 1);
}

.pi-op__coach-card.liquidGlass-wrapper > .liquidGlass-tint {
  background: var(--lg-tint-sheet, rgba(255, 255, 255, 0.34)) !important;
}

.pi-op__coach-body {
  position: relative;
  z-index: 2;
  padding: 0.95rem 1rem 1rem;
}

.pi-op__coach-title {
  margin: 0;
  font-size: 0.92rem;
  font-weight: 750;
  color: var(--pi-op-ink);
  letter-spacing: -0.01em;
}

.pi-op__coach-text {
  margin: 0.4rem 0 0;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--pi-op-muted);
}

.pi-op__coach-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-top: 0.85rem;
}

.pi-op__coach-primary,
.pi-op__coach-secondary {
  border: 0;
  border-radius: 0.8rem;
  padding: 0.5rem 0.85rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
}

.pi-op__coach-primary {
  background: linear-gradient(145deg, color-mix(in srgb, var(--pi-op-gold) 88%, #fff), var(--pi-op-gold));
  color: #14233f;
  box-shadow: 0 6px 14px color-mix(in srgb, var(--pi-op-gold) 30%, transparent);
}

.pi-op__coach-secondary {
  background: color-mix(in srgb, #ffffff 42%, transparent);
  color: var(--pi-op-ink);
}

html.dark .pi-op__coach-secondary {
  background: color-mix(in srgb, #ffffff 10%, transparent);
}
