/* TUM Chatbot - Link Styling */

/* Chainlit copilot loading indicator.
   BlinkingCursor renders <span class="inline-block loading-cursor" />, and the
   rule for it lives in frontend/src/index.css, which is not inlined into the
   copilot bundle's shadow-DOM stylesheet — so without this the cursor has no
   size, colour or animation in copilot mode. customCssUrl is the only hook that
   crosses the shadow boundary.

   The bundle renders this component in two places, distinguished only by the
   `ml-2` class it adds when used inline:
     .loading-cursor          alone, before the answer starts — the wait
     .loading-cursor.ml-2     inside the markdown, trailing the streamed text
   Only the first is a typing indicator; the second has to stay a caret, or the
   words below land in the middle of a sentence as it is being written.

   Three dots because that is the shape people read as "typing". The waiting
   text changes once, which is what makes a pause feel like work rather than a
   stall. It is a fixed script on a timer, not a report of what the model is
   doing. */
.loading-cursor:not(.ml-2) {
  position: relative;
  display: inline-block;
  width: 8px;
  height: 8px;
  margin: 0 0 2px 4px;
  border-radius: 50%;
  background: transparent;
  color: hsl(212, 30%, 45%);
  animation: tumi-typing 1.2s ease-in-out infinite;
}

@keyframes tumi-typing {
  0%, 100% { box-shadow: 0 0 0 0 currentColor,
                        14px 0 0 0 rgba(90, 120, 150, .3),
                        28px 0 0 0 rgba(90, 120, 150, .3); }
  33%      { box-shadow: 0 0 0 0 rgba(90, 120, 150, .3),
                        14px 0 0 0 currentColor,
                        28px 0 0 0 rgba(90, 120, 150, .3); }
  66%      { box-shadow: 0 0 0 0 rgba(90, 120, 150, .3),
                        14px 0 0 0 rgba(90, 120, 150, .3),
                        28px 0 0 0 currentColor; }
}

/* Two labels crossfading in place. Two, because an element has exactly two
   pseudo-elements and `content` cannot be animated portably. */
.loading-cursor:not(.ml-2)::before,
.loading-cursor:not(.ml-2)::after {
  position: absolute;
  left: 46px;
  top: 50%;
  transform: translateY(-50%);
  white-space: nowrap;
  font-size: .87rem;
  color: hsl(212, 20%, 48%);
}

.loading-cursor:not(.ml-2)::before {
  content: "Thinking…";
  animation: tumi-stage-out 8s ease forwards;
}

.loading-cursor:not(.ml-2)::after {
  content: "Looking it up on lll.tum.de…";
  opacity: 0;
  animation: tumi-stage-in 8s ease forwards;
}

:host(:lang(de)) .loading-cursor:not(.ml-2)::before { content: "Einen Moment…"; }
:host(:lang(de)) .loading-cursor:not(.ml-2)::after  { content: "Ich sehe auf lll.tum.de nach…"; }

/* The handover sits at ~2s: long enough that a fast answer never shows the
   second line at all, short enough that a slow one does not feel stuck. */
@keyframes tumi-stage-out { 0%, 22% { opacity: 1; } 28%, 100% { opacity: 0; } }
@keyframes tumi-stage-in  { 0%, 25% { opacity: 0; } 31%, 100% { opacity: 1; } }

/* The streaming caret: a text cursor trailing the words, nothing more. */
.loading-cursor.ml-2 {
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-left: 3px;
  vertical-align: -0.16em;
  border-radius: 1px;
  background: hsl(212, 30%, 45%);
  animation: tumi-caret 1.05s steps(2, jump-none) infinite;
}

.loading-cursor.ml-2::before,
.loading-cursor.ml-2::after { content: none; }

@keyframes tumi-caret { 0%, 45% { opacity: 1; } 55%, 100% { opacity: .15; } }

@media (prefers-reduced-motion: reduce) {
  .loading-cursor:not(.ml-2) { animation: none; box-shadow: 0 0 0 0 currentColor,
    14px 0 0 0 currentColor, 28px 0 0 0 currentColor; }
  .loading-cursor:not(.ml-2)::before { animation: none; }
  .loading-cursor:not(.ml-2)::after { animation: none; opacity: 0; }
  .loading-cursor.ml-2 { animation: none; }
}

a,
.markdown-body a,
.prose a,
.cl-message a,
[class*="message"] a,
[class*="Message"] a,
.step-content a,
[data-testid="step"] a,
[data-role="assistant"] a {
  color: hsl(212, 79%, 35%) !important;
  text-decoration: underline !important;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  font-weight: 500;
}

a:hover,
.markdown-body a:hover,
.prose a:hover,
.cl-message a:hover,
[class*="message"] a:hover,
[class*="Message"] a:hover,
.step-content a:hover,
[data-testid="step"] a:hover,
[data-role="assistant"] a:hover {
  color: hsl(212, 79%, 25%) !important;
  text-decoration: none !important;
}

a:visited,
.markdown-body a:visited,
.prose a:visited,
.cl-message a:visited,
[class*="message"] a:visited,
[class*="Message"] a:visited,
.step-content a:visited,
[data-testid="step"] a:visited,
[data-role="assistant"] a:visited {
  color: hsl(212, 50%, 40%) !important;
}

a:focus,
.markdown-body a:focus,
.prose a:focus,
.cl-message a:focus,
[class*="message"] a:focus,
[class*="Message"] a:focus,
.step-content a:focus,
[data-testid="step"] a:focus,
[data-role="assistant"] a:focus {
  outline: none;
}

a:focus-visible,
.markdown-body a:focus-visible,
.prose a:focus-visible,
.cl-message a:focus-visible,
[class*="message"] a:focus-visible,
[class*="Message"] a:focus-visible,
.step-content a:focus-visible,
[data-testid="step"] a:focus-visible,
[data-role="assistant"] a:focus-visible {
  outline: none;
}

/* ============================================================
   Launcher — the closed state of the copilot on lll.tum.de
   ============================================================
   customCssUrl is the only hook that crosses the copilot's shadow boundary
   (config.toml's custom_css/custom_js go into the main app's index.html, which
   the copilot bundle never loads), so everything here is CSS and the embed
   snippet on lll.tum.de stays untouched.

   Structure this relies on, from the copilot bundle:
     #chainlit-copilot         the shadow host, in the page
       #cl-shadow-root         wrapper inside the shadow root
         #chainlit-copilot-button   the launcher, fixed bottom-right
           > div                      holds the icon or configured image
   The button is replaced by the chat panel when open, so anything that must
   only exist alongside it is scoped with :has(). */

/* Consent must stay reachable. Borlabs Cookie draws its dialog at 999999 and
   its backdrop at 999998; the embed snippet gives the widget 999999 too, and
   on a tie the later element in the document wins — always the widget, which is
   appended to <body> at document-end. This selector outranks the snippet's bare
   :host, so load order cannot undo it. */
:host(#chainlit-copilot) {
  z-index: 999990 !important;
}

:host {
  --tumi-navy: #072140;          /* lll.tum.de palette colour 1, and its text colour */
  --tumi-size: 54px;
  --tumi-open: 138px;
  --tumi-inset: 32px;
  --tumi-expanded-max: 760px;   /* the expanded panel; see the note below */
  --tumi-ease-rise: cubic-bezier(.34, .68, .24, 1);
  --tumi-ease-open: cubic-bezier(.16, 1, .3, 1);
}

:host(:lang(de)) {
  --tumi-open: 158px;
}

#chainlit-copilot-button {
  /* Anchored right with the content flushed right, so opening moves only the
     left edge: the mark holds still and the label is uncovered in place.

     width and height carry no !important on purpose: an important author
     declaration outranks a CSS animation, so marking width important disables
     the opening animation. The id selector already beats the bundle's utility
     classes without it. */
  display: flex !important;
  align-items: center;
  justify-content: flex-end;
  overflow: hidden;
  right: var(--tumi-inset) !important;
  bottom: var(--tumi-inset) !important;
  width: var(--tumi-open);
  height: var(--tumi-size);
  padding: 0 !important;
  border: 2px solid #fff !important;
  border-radius: 999px !important;
  background: var(--tumi-navy) !important;
  box-shadow: 0 4px 18px rgba(7, 33, 64, .30), 0 1px 3px rgba(7, 33, 64, .18);
  transition: transform .28s var(--tumi-ease-open),
              box-shadow .28s var(--tumi-ease-open);
  /* Arrive as a circle, settle, then open. tumi-open holds its first keyframe
     through its delay, which is what keeps the capsule circular until then. */
  animation: tumi-rise .8s var(--tumi-ease-rise) .3s backwards,
             tumi-open .6s var(--tumi-ease-open) 1.35s backwards;
}

/* Roboto is what lll.tum.de sets, and the page self-hosts it — a @font-face
   registered in the outer document is usable inside a shadow tree, so this
   matches the site without our own webfont request. Chainlit's theme would
   otherwise supply Open Sans. */
#chainlit-copilot-button::before {
  content: "Ask TUMi";
  flex: 1 1 auto;               /* absorbs the slack, so the text stays centred
                                   whatever the language or font metrics */
  min-width: 0;
  overflow: hidden;
  text-align: center;
  white-space: nowrap;
  font-family: Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: .005em;
  line-height: 1;
  color: #fff;
  padding-left: 14px;
  animation: tumi-label .5s var(--tumi-ease-open) 1.35s backwards;
}

/* :lang() reads the language inherited from <html lang="de-DE">, so the label
   follows the page with no script. */
:host(:lang(de)) #chainlit-copilot-button::before {
  content: "TUMi fragen";
}

/* The mark, subtracted from the fill rather than placed on it. */
#chainlit-copilot-button > div {
  flex: 0 0 auto;
  width: 30px !important;
  height: 38px !important;
  margin-right: 9px;
  background: url("avatars/tumi-knockout.png") center / contain no-repeat;
}

#chainlit-copilot-button > div > * {
  display: none !important;
}

/* Radix flips data-state between "closed" and "open" as its tooltip shows, so
   both are needed for the avatar to hold its size while hovered. The tooltip
   reads the author name, which config.toml sets to TUMi.

   Only these two values: Radix writes data-state on radio and checkbox
   indicators too ("checked"/"unchecked"), and sizing those pulls the
   display-mode picker's circles out of line with their labels. */
#cl-shadow-root span[data-state="closed"],
#cl-shadow-root span[data-state="open"] {
  width: 36px !important;
  height: 36px !important;
}

/* Expanded, the panel fills whatever width the viewport offers, which on a wide
   monitor is far more than a conversation needs. Cap it just above the reading
   measure below, so the text keeps margins rather than acres.

   Constrain width rather than max-width: a transition interpolates the computed
   width, and max-width leaves that at the viewport while clamping only what is
   drawn, so the collapse would start from a size never shown. Unlike animations,
   transitions are not overridden by !important, so this still animates.

   Both states are pinned to the right edge. Laid out from its left inset the
   panel shrinks away from the launcher it belongs to; anchored right, the right
   edge holds and the left edge travels in. */
#cl-shadow-root .copilot-container-expanded,
#cl-shadow-root .copilot-container-collapsed {
  left: auto !important;
  right: var(--tumi-inset) !important;
}

#cl-shadow-root .copilot-container-expanded {
  width: min(100vw - (var(--tumi-inset) * 2), var(--tumi-expanded-max)) !important;
}

/* On a wide screen the panel stretches and the conversation becomes a thin
   ribbon of text adrift in it. Cap the reading measure on the list that holds
   the messages — never on the messages themselves, which carry their own
   alignment: a user bubble sits right, and centring it breaks that. */
#cl-shadow-root *:not([data-step-type]):has(> [data-step-type]) {
  max-width: 68ch;
  margin-inline: auto;
  width: 100%;
}

/* Status light. It sits on the capsule's outline, which the button cannot draw
   itself: overflow:hidden — which the label reveal depends on — would clip it
   into a crescent against the rounded end. Positioned against the same inset
   the button uses, and scoped so it disappears with the button when the chat
   opens. */
#cl-shadow-root:has(#chainlit-copilot-button)::after {
  content: "";
  position: fixed;
  right: calc(var(--tumi-inset) + 1px);
  bottom: calc(var(--tumi-inset) + var(--tumi-size) - 15px);
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #25b36b;
  border: 2.5px solid #fff;
  box-shadow: 0 1px 3px rgba(7, 33, 64, .3);
  pointer-events: none;
  z-index: 21;
  animation: tumi-dot .45s var(--tumi-ease-open) 1.1s backwards,
             tumi-live 2.8s ease-in-out 1.9s infinite;
}

/* The ring is a box-shadow because overflow:hidden would clip a pseudo-element
   drawn outside the box, and the white gap continues the capsule's own rim. */
#chainlit-copilot-button:hover,
#chainlit-copilot-button:focus-visible {
  transform: translateY(-3px);
  box-shadow: 0 10px 30px rgba(7, 33, 64, .38),
              0 0 0 5px #fff,
              0 0 0 7px rgba(7, 33, 64, .34);
}

@keyframes tumi-rise {
  0%   { opacity: 0; transform: translateY(12px) scale(.975); }
  50%  { opacity: 1; }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes tumi-open { from { width: var(--tumi-size); } }
@keyframes tumi-dot  { from { opacity: 0; transform: scale(.4); } }

/* Opacity lands early so the tail of the reveal plays on solid text. */
@keyframes tumi-label { 0%, 10% { opacity: 0; } 60%, 100% { opacity: 1; } }

@keyframes tumi-live {
  0%, 100% { box-shadow: 0 0 0 0 rgba(37, 179, 107, .5), 0 1px 3px rgba(7, 33, 64, .3); }
  70%      { box-shadow: 0 0 0 7px rgba(37, 179, 107, 0), 0 1px 3px rgba(7, 33, 64, .3); }
}

@media (prefers-reduced-motion: reduce) {
  #chainlit-copilot-button,
  #chainlit-copilot-button::before,
  #cl-shadow-root:has(#chainlit-copilot-button)::after {
    animation: none !important;
    transition: none !important;
  }
}

/* --- Panel footer and header -------------------------------------------- */

/* The Institute has to show a usage notice, and it carries a link, so the
   element itself is built in copilot-enhancements.js; only its appearance
   belongs here. Chainlit's own mark occupies the same slot and is hidden
   rather than removed, so a re-render cannot leave the footer empty. */
#cl-shadow-root .watermark {
  display: none !important;
}

#cl-shadow-root #tumi-usage-notice {
  text-align: center;
  font-size: 11px;
  line-height: 1.2;
  color: #888;
  margin-top: 8px;
  padding: 4px 8px;
}

#cl-shadow-root #tumi-usage-notice a {
  color: #4ea1ff;
}

/* Sized by the bundle for a full-page header, where there is room for it; in
   the panel it has to share a line with the title. */
#cl-shadow-root img.logo {
  height: 26px !important;
  width: auto !important;
}
