/* ============================================================
   Sleek Academia — floating chat widget ("Sleekie")
   Single-accent neumorphism, preserving Sleekie's original calm cyan identity.
   Mobile uses a dedicated bottom-sheet composition rather than scaling desktop.

   Everything is scoped under #sa-chat-root and declares its own tokens,
   because the widget ships on every public page and must not inherit
   whichever page stylesheet happens to be in scope.

   All motion is transform/opacity only so a pinned, animated launcher never
   costs a scroll frame.
   ============================================================ */

#sa-chat-root {
  --sa-surface: #e7e4f1;
  --sa-shadow: #c3bdd8;
  --sa-highlight: #ffffff;
  --sa-txt: #1b1f24;
  --sa-muted: #52606d;
  --sa-brand: #009fe3;
  --sa-brand-deep: #006b99;
  --sa-raised: 10px 10px 24px var(--sa-shadow), -10px -10px 24px var(--sa-highlight);
  --sa-raised-sm: 6px 6px 14px var(--sa-shadow), -6px -6px 14px var(--sa-highlight);
  --sa-pressed: inset 6px 6px 12px var(--sa-shadow), inset -6px -6px 12px var(--sa-highlight);
  --sa-pressed-sm: inset 4px 4px 9px var(--sa-shadow), inset -4px -4px 9px var(--sa-highlight);

  --sa-gap: 22px;
  --sa-ease: cubic-bezier(0.2, 0.9, 0.25, 1);
  --sa-spring: cubic-bezier(0.24, 1.24, 0.36, 1);

  position: fixed;
  right: max(var(--sa-gap), env(safe-area-inset-right));
  bottom: max(var(--sa-gap), env(safe-area-inset-bottom));
  z-index: 2147483000; /* above the liquid nav, cookie bars and Clerk portals */
  font-family: "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--sa-txt);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- launcher ---------- */

.sa-launcher {
  position: relative;
  display: grid;
  place-items: center;
  width: 64px;
  height: 64px;
  margin-left: auto;
  padding: 0;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  background: var(--sa-surface);
  box-shadow: var(--sa-raised);
  animation: sa-float 5.5s var(--sa-ease) infinite;
  transition: box-shadow 0.3s var(--sa-ease), transform 0.3s var(--sa-spring);
}

.sa-launcher:hover {
  transform: translateY(-4px) scale(1.04);
  box-shadow: 15px 15px 32px var(--sa-shadow), -14px -14px 30px var(--sa-highlight);
}

.sa-launcher:active {
  transform: translateY(0) scale(0.96);
  box-shadow: var(--sa-pressed);
}

.sa-launcher:focus-visible {
  outline: none;
  box-shadow: var(--sa-raised), 0 0 0 3px rgba(0, 159, 227, 0.35);
}

.sa-launcher img {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: contain;
  pointer-events: none;
  transition: transform 0.45s var(--sa-spring);
}

.sa-launcher .sa-close-glyph {
  position: absolute;
  font-size: 26px;
  font-weight: 300;
  line-height: 1;
  color: var(--sa-brand);
  opacity: 0;
  transform: rotate(-90deg) scale(0.5);
  transition: opacity 0.28s var(--sa-ease), transform 0.4s var(--sa-spring);
}

/* One cyan pulse keeps Sleekie discoverable without visual noise. */
.sa-launcher::before {
  content: "";
  position: absolute;
  inset: -4px;
  border: 2px solid rgba(0, 159, 227, 0.35);
  border-radius: 50%;
  opacity: 0;
  pointer-events: none;
  animation: sa-pulse 3.2s ease-out infinite;
}

/* unread nudge dot, shown until the panel is opened once */
.sa-launcher::after {
  content: "";
  position: absolute;
  top: 6px;
  right: 6px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--sa-brand);
  box-shadow: 0 0 0 3px var(--sa-surface), 0 0 12px rgba(0, 159, 227, 0.6);
  transform: scale(0);
  transition: transform 0.4s var(--sa-spring);
  z-index: 2;
}

#sa-chat-root[data-nudge="true"] .sa-launcher::after {
  transform: scale(1);
}

#sa-chat-root[data-open="true"] .sa-launcher {
  animation: none;
}

#sa-chat-root[data-open="true"] .sa-launcher::before {
  animation: none;
}

#sa-chat-root[data-open="true"] .sa-launcher img {
  transform: rotate(90deg) scale(0);
}

#sa-chat-root[data-open="true"] .sa-launcher .sa-close-glyph {
  opacity: 1;
  transform: rotate(0) scale(1);
}

@keyframes sa-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-7px); }
}

@keyframes sa-pulse {
  0% { opacity: 0.5; transform: scale(1); }
  70%, 100% { opacity: 0; transform: scale(1.35); }
}

/* ---------- panel ---------- */

.sa-panel {
  position: absolute;
  right: 0;
  bottom: 78px;
  display: flex;
  flex-direction: column;
  width: 374px;
  height: min(560px, calc(100vh - 140px));
  border-radius: 28px;
  background: var(--sa-surface);
  box-shadow: var(--sa-raised);
  overflow: hidden;
  transform-origin: 100% 100%;
  opacity: 0;
  transform: translateY(18px) scale(0.82);
  pointer-events: none;
  visibility: hidden;
  transition:
    opacity 0.26s var(--sa-ease),
    transform 0.44s var(--sa-spring),
    visibility 0s linear 0.44s;
}

#sa-chat-root[data-open="true"] .sa-panel {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
  visibility: visible;
  transition-delay: 0s;
}

/* ---------- header ---------- */

.sa-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 18px 14px;
  flex: none;
  border-bottom: 1px solid rgba(92, 109, 125, 0.12);
}

.sa-head-avatar {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: none;
  border-radius: 50%;
  box-shadow: var(--sa-pressed-sm);
}

.sa-head-avatar img {
  width: 26px;
  height: 26px;
  object-fit: contain;
}

.sa-head-text { min-width: 0; }

.sa-head-text strong {
  display: block;
  font-family: "Plus Jakarta Sans", system-ui, sans-serif;
  font-weight: 800;
  font-size: 15px;
  letter-spacing: -0.01em;
  color: var(--sa-txt);
}

.sa-head-text span {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--sa-muted);
}

.sa-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--sa-brand);
  box-shadow: 0 0 9px rgba(0, 159, 227, 0.6);
  animation: sa-blink 2.2s ease-in-out infinite;
}

@keyframes sa-blink {
  50% { opacity: 0.3; }
}

.sa-head-close {
  margin-left: auto;
  width: 34px;
  height: 34px;
  flex: none;
  display: grid;
  place-items: center;
  padding: 0;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  color: var(--sa-muted);
  background: var(--sa-surface);
  box-shadow: 4px 4px 9px var(--sa-shadow), -4px -4px 9px var(--sa-highlight);
  transition: box-shadow 0.22s, color 0.22s, transform 0.22s var(--sa-spring);
}

.sa-head-close:hover { color: var(--sa-brand); transform: scale(1.06); }
.sa-head-close:active { box-shadow: var(--sa-pressed-sm); transform: scale(0.94); }

/* ---------- message log ---------- */

.sa-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
  scrollbar-color: var(--sa-shadow) transparent;
}

.sa-log::-webkit-scrollbar { width: 6px; }
.sa-log::-webkit-scrollbar-thumb { background: var(--sa-shadow); border-radius: 6px; }

.sa-msg-group {
  display: flex;
  flex-direction: column;
  max-width: 86%;
  animation: sa-msg-in 0.4s var(--sa-ease) both;
}

.sa-msg-group-ai { align-self: flex-start; }
.sa-msg-group-user { align-self: flex-end; align-items: flex-end; }

.sa-msg-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--sa-muted);
  margin-bottom: 4px;
  padding: 0 4px;
}

.sa-msg {
  padding: 11px 15px;
  border-radius: 18px;
  font-size: 13.6px;
  word-wrap: break-word;
}

.sa-msg a { color: var(--sa-brand-deep); font-weight: 700; }

.sa-msg-ai {
  border-bottom-left-radius: 7px;
  background: var(--sa-surface);
  box-shadow: var(--sa-raised-sm);
}

.sa-msg-user {
  border-bottom-right-radius: 7px;
  color: #fff;
  background: var(--sa-brand-deep);
  box-shadow: 6px 6px 16px rgba(0, 107, 153, 0.28), -5px -5px 12px var(--sa-highlight);
}

.sa-msg-note {
  align-self: center;
  max-width: 100%;
  font-size: 12px;
  color: var(--sa-muted);
  text-align: center;
  padding: 8px 16px;
  border-radius: 30px;
  background: var(--sa-surface);
  box-shadow: var(--sa-pressed-sm);
  animation: sa-msg-in 0.4s var(--sa-ease) both;
}

@keyframes sa-msg-in {
  from { opacity: 0; transform: translateY(10px) scale(0.97); }
  to { opacity: 1; transform: none; }
}

/* typing indicator */

.sa-typing {
  align-self: flex-start;
  display: flex;
  gap: 5px;
  padding: 14px 16px;
  border-radius: 18px 18px 18px 7px;
  background: var(--sa-surface);
  box-shadow: var(--sa-raised-sm);
  animation: sa-msg-in 0.3s var(--sa-ease) both;
}

.sa-typing i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--sa-brand);
  animation: sa-bounce 1.3s ease-in-out infinite;
}

.sa-typing i:nth-child(2) { animation-delay: 0.16s; }
.sa-typing i:nth-child(3) { animation-delay: 0.32s; }

@keyframes sa-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ---------- suggestion + action chips ---------- */

.sa-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 18px 12px;
  flex: none;
}

.sa-chip {
  padding: 8px 14px;
  border: none;
  border-radius: 30px;
  cursor: pointer;
  font-family: inherit;
  font-size: 12.4px;
  font-weight: 600;
  color: var(--sa-muted);
  background: var(--sa-surface);
  box-shadow: 4px 4px 9px var(--sa-shadow), -4px -4px 9px var(--sa-highlight);
  transition: color 0.22s, box-shadow 0.22s, transform 0.22s var(--sa-spring);
  animation: sa-msg-in 0.4s var(--sa-ease) both;
}

.sa-chip:hover { color: var(--sa-brand-deep); transform: translateY(-2px); }
.sa-chip:active { box-shadow: var(--sa-pressed-sm); transform: translateY(0) scale(0.97); }

.sa-chip.sa-chip-primary {
  color: #fff;
  background: var(--sa-brand-deep);
  box-shadow: 6px 6px 16px rgba(0, 107, 153, 0.3), -5px -5px 12px var(--sa-highlight);
}

/* ---------- composer ---------- */

.sa-composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 18px max(16px, env(safe-area-inset-bottom));
  flex: none;
}

.sa-input {
  flex: 1 1 auto;
  max-height: 92px;
  padding: 12px 16px;
  border: none;
  border-radius: 18px;
  resize: none;
  font-family: inherit;
  font-size: 13.6px;
  line-height: 1.5;
  color: var(--sa-txt);
  background: var(--sa-surface);
  box-shadow: var(--sa-pressed);
  transition: box-shadow 0.25s;
}

.sa-input::placeholder { color: var(--sa-muted); }
.sa-input:focus { outline: none; box-shadow: var(--sa-pressed), 0 0 0 2px rgba(0, 159, 227, 0.3); }
.sa-input:disabled { opacity: 0.6; }

.sa-send {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  padding: 0;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  color: #fff;
  background: var(--sa-brand-deep);
  box-shadow: 6px 6px 16px rgba(0, 107, 153, 0.3), -5px -5px 12px var(--sa-highlight);
  transition: transform 0.25s var(--sa-spring), box-shadow 0.25s, opacity 0.25s;
}

.sa-send svg {
  width: 19px;
  height: 19px;
  fill: currentColor;
}

.sa-send:hover { transform: translateY(-2px) scale(1.05); }
.sa-send:active { transform: scale(0.94); box-shadow: var(--sa-pressed-sm); }
.sa-send:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.sa-legal {
  padding: 0 18px 14px;
  flex: none;
  font-size: 10.5px;
  text-align: center;
  color: var(--sa-muted);
  opacity: 1;
}

/* ---------- mobile: bottom sheet ---------- */

@media (max-width: 560px) {
  #sa-chat-root {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
    left: max(12px, env(safe-area-inset-left));
    pointer-events: none;
  }

  #sa-chat-root > * { pointer-events: auto; }

  .sa-launcher {
    width: 56px;
    height: 56px;
    transition: opacity 0.2s var(--sa-ease), transform 0.25s var(--sa-spring), visibility 0s linear 0s;
  }

  .sa-launcher img { width: 32px; height: 32px; }

  #sa-chat-root[data-open="true"] .sa-launcher {
    opacity: 0;
    transform: translateY(8px) scale(0.86);
    pointer-events: none;
    visibility: hidden;
    transition: opacity 0.2s var(--sa-ease), transform 0.25s var(--sa-spring), visibility 0s linear 0.2s;
  }

  .sa-panel {
    left: 0;
    right: 0;
    bottom: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto auto auto;
    width: auto;
    height: min(78dvh, 42rem);
    max-height: calc(100dvh - max(24px, env(safe-area-inset-top)) - max(12px, env(safe-area-inset-bottom)));
    border-radius: 24px;
    transform-origin: 50% 100%;
    transform: translateY(18px) scale(0.98);
  }

  .sa-head {
    gap: 10px;
    min-height: 62px;
    padding: 10px 12px 9px;
  }

  .sa-head-avatar {
    width: 38px;
    height: 38px;
  }

  .sa-head-avatar img { width: 24px; height: 24px; }
  .sa-head-text strong { font-size: 14px; }
  .sa-head-text span { font-size: 11px; }

  .sa-head-close {
    min-width: 44px;
    min-height: 44px;
    width: 44px;
    height: 44px;
  }

  .sa-log {
    padding: 12px 14px 16px;
    gap: 12px;
    scroll-padding-block: 12px;
    -webkit-overflow-scrolling: touch;
  }

  .sa-msg-group-ai { max-width: 92%; }
  .sa-msg-group-user { max-width: 78%; }

  .sa-msg-label {
    margin-bottom: 3px;
    font-size: 10px;
  }

  .sa-msg {
    padding: 10px 12px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
  }

  .sa-msg-ai { border-bottom-left-radius: 6px; }
  .sa-msg-user { border-bottom-right-radius: 6px; }

  .sa-chips {
    flex-wrap: nowrap;
    gap: 8px;
    padding: 3px 14px 11px;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    scrollbar-width: none;
  }

  .sa-chips::-webkit-scrollbar { display: none; }

  .sa-chip {
    flex: 0 0 auto;
    min-height: 40px;
    padding-inline: 14px;
  }

  .sa-composer {
    gap: 9px;
    padding: 10px 12px 9px;
    border-top: 1px solid rgba(92, 109, 125, 0.12);
    background: var(--sa-surface);
    box-shadow: 0 -10px 22px rgba(195, 189, 216, 0.24);
  }

  .sa-input {
    min-height: 48px;
    padding: 13px 14px;
    font-size: 16px;
  }

  .sa-send {
    width: 48px;
    height: 48px;
  }

  .sa-legal {
    padding: 0 14px 10px;
    font-size: 9.5px;
    line-height: 1.35;
  }
}

/* ---------- reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  #sa-chat-root *,
  #sa-chat-root *::before,
  #sa-chat-root *::after {
    animation: none !important;
    transition-duration: 0.01ms !important;
  }

  .sa-panel { transform: none; }
  #sa-chat-root[data-open="true"] .sa-panel { transform: none; }
}
