/* add-tsg-ai-assistant-ui: the TSG AI Assistant floating widget -- loaded on every page (both
   base.html-extending pages and the standalone home.html, which duplicates its own head/scripts
   and doesn't extend base.html, same precedent as _language_selector.html/the navbar already
   being duplicated there). Deliberately its own dedicated stylesheet rather than appended to
   style.css or home-preview.css, matching this project's own established pattern for a global,
   fixed-position UI feature (see static/css/bottom-nav.css, static/css/champions-corner.css) --
   and specifically NOT using Tailwind utility classes in the widget's markup, since home.html
   never loads tailwind.css at all and a class only compiled for base.html pages would silently
   fail to render there.

   Uses the --tsg-* custom properties already declared globally by style.css's/home-preview.css's
   own :root blocks (red/teal/bg/surface/white/muted/border/danger/warning) -- no colors are
   redefined here, only referenced, per this feature's own "don't duplicate the design system"
   requirement. */

.tsg-ai-widget {
  position: fixed;
  z-index: 999; /* above bottom-nav.css's 50 and shared-navbar.css's 101 */
}

/* ---------- Floating launcher button ---------- */

.tsg-ai-launcher {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: 56px;
  height: 56px;
  border-radius: 999px;
  border: none;
  background: var(--tsg-red);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.45);
  z-index: 999;
  transition: box-shadow 0.2s ease, background 0.2s ease;
}

.tsg-ai-launcher:hover {
  box-shadow: 0 0 22px rgba(231, 0, 0, 0.55), 0 4px 18px rgba(0, 0, 0, 0.45);
}

.tsg-ai-launcher:focus-visible {
  outline: 2px solid var(--tsg-teal);
  outline-offset: 3px;
}

@media (prefers-reduced-motion: no-preference) {
  .tsg-ai-launcher:active {
    transform: scale(0.94);
  }
}

/* Keep the launcher clear of the mobile bottom-nav bar (bottom-nav.css: fixed, 62px tall). */
@media (max-width: 767px) {
  .tsg-ai-launcher {
    bottom: calc(62px + 14px + env(safe-area-inset-bottom, 0px));
    right: 16px;
  }
}

.tsg-ai-launcher .tsg-ai-launcher-icon-open,
.tsg-ai-launcher .tsg-ai-launcher-icon-close {
  position: absolute;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.tsg-ai-launcher .tsg-ai-launcher-icon-close {
  opacity: 0;
  transform: scale(0.6);
}

.tsg-ai-widget.is-open .tsg-ai-launcher .tsg-ai-launcher-icon-open {
  opacity: 0;
  transform: scale(0.6);
}

.tsg-ai-widget.is-open .tsg-ai-launcher .tsg-ai-launcher-icon-close {
  opacity: 1;
  transform: scale(1);
}

/* ---------- Chat panel: desktop (anchored bottom-right, doesn't cover nav) ---------- */

.tsg-ai-panel {
  position: fixed;
  right: 20px;
  bottom: 88px;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: min(620px, calc(100vh - 120px));
  background: var(--tsg-surface);
  border: 1px solid var(--tsg-border);
  border-radius: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 998;
}

/* Hidden by default; JS toggles .is-open on .tsg-ai-widget. No display:none transition needed --
   a chat panel opening instantly reads as responsive, not janky, same as this app's existing
   modals (_push_prompt_modal.html has no open animation either). */
.tsg-ai-panel {
  display: none;
}

.tsg-ai-widget.is-open .tsg-ai-panel {
  display: flex;
}

/* ---------- Chat panel: mobile (full-screen sheet) ---------- */

@media (max-width: 640px) {
  .tsg-ai-panel {
    right: 0;
    bottom: 0;
    left: 0;
    top: 0;
    width: 100%;
    max-width: 100%;
    height: 100dvh; /* not 100vh -- keeps the input above the on-screen keyboard, see .tsg-ai-input-row */
    border-radius: 0;
    border: none;
  }

  /* The full-screen panel would otherwise sit *under* the launcher (launcher is z-index 999,
     panel 998) at this same bottom-right corner -- the header's own close (X) button is the one
     close affordance on mobile instead. Desktop keeps the launcher's open/close icon-morph since
     its panel is anchored above the button with a gap, never overlapping it. */
  .tsg-ai-widget.is-open .tsg-ai-launcher {
    display: none;
  }
}

/* ---------- Header ---------- */

.tsg-ai-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 14px 14px 16px;
  border-bottom: 1px solid var(--tsg-border);
  flex-shrink: 0;
}

.tsg-ai-header-avatar {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  object-fit: contain;
}

.tsg-ai-header-text {
  flex: 1;
  min-width: 0;
}

.tsg-ai-header-title {
  color: var(--tsg-white);
  font-family: 'Rajdhani', sans-serif;
  font-weight: 700;
  font-size: 15px;
  line-height: 1.2;
}

.tsg-ai-header-subtitle {
  color: var(--tsg-muted);
  font-size: 11px;
  line-height: 1.2;
}

.tsg-ai-header-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--tsg-muted);
  font-size: 17px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}

.tsg-ai-header-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--tsg-white);
}

.tsg-ai-header-btn:focus-visible {
  outline: 2px solid var(--tsg-teal);
  outline-offset: 1px;
}

/* ---------- Mode tabs (add-tsg-ai-voice) ---------- */
/* Kept here, not in the lazy-loaded ai-voice.css, since the tabs themselves are always visible
   from first paint -- only clicking "Voice" triggers the lazy load of ai_voice.js/ai-voice.css.
   #tsg-ai-text-panel/#tsg-ai-voice-panel need to be flex containers (not just visible/hidden) so
   everything already inside #tsg-ai-text-panel (messages/suggestions/input row) keeps exactly the
   same layout it had before this wrapper existed. */

.tsg-ai-mode-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 14px 0;
  border-bottom: 1px solid var(--tsg-border);
  flex-shrink: 0;
}

.tsg-ai-mode-tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  background: transparent;
  color: var(--tsg-muted);
  font-size: 12.5px;
  font-weight: 600;
  padding: 9px 10px;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.tsg-ai-mode-tab:hover {
  color: var(--tsg-white);
}

.tsg-ai-mode-tab.is-active {
  color: var(--tsg-teal);
  border-bottom-color: var(--tsg-teal);
}

.tsg-ai-mode-tab:focus-visible {
  outline: 2px solid var(--tsg-teal);
  outline-offset: -2px;
}

.tsg-ai-text-panel,
.tsg-ai-voice-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.tsg-ai-text-panel[hidden],
.tsg-ai-voice-panel[hidden] {
  display: none;
}

/* ---------- Message list ---------- */

.tsg-ai-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overscroll-behavior: contain;
}

.tsg-ai-row {
  display: flex;
  gap: 8px;
  max-width: 100%;
}

.tsg-ai-row.tsg-ai-row-user {
  flex-direction: row-reverse;
}

.tsg-ai-row-avatar {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  object-fit: contain;
  margin-top: 4px;
}

.tsg-ai-bubble {
  max-width: 78%;
  padding: 9px 12px;
  border-radius: 14px;
  font-size: 13.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
}

.tsg-ai-row-assistant .tsg-ai-bubble {
  background: var(--tsg-bg);
  border: 1px solid var(--tsg-border);
  color: var(--tsg-white);
  border-bottom-left-radius: 4px;
}

.tsg-ai-row-user .tsg-ai-bubble {
  background: var(--tsg-red);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.tsg-ai-row-error .tsg-ai-bubble {
  background: rgba(255, 107, 53, 0.12);
  border: 1px solid rgba(255, 107, 53, 0.4);
  color: var(--tsg-warning);
  border-bottom-left-radius: 4px;
}

.tsg-ai-retry-btn {
  display: block;
  margin-top: 6px;
  border: none;
  background: transparent;
  color: var(--tsg-teal);
  font-size: 12px;
  font-weight: 700;
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
}

.tsg-ai-retry-btn:hover {
  color: var(--tsg-white);
}

.tsg-ai-retry-btn:focus-visible {
  outline: 2px solid var(--tsg-teal);
  outline-offset: 2px;
}

/* ---------- Typing indicator ---------- */

.tsg-ai-typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 0;
}

.tsg-ai-typing-dots span {
  width: 5px;
  height: 5px;
  border-radius: 999px;
  background: var(--tsg-muted);
  opacity: 0.5;
}

@media (prefers-reduced-motion: no-preference) {
  .tsg-ai-typing-dots span {
    animation: tsg-ai-typing-bounce 1.1s infinite ease-in-out;
  }
  .tsg-ai-typing-dots span:nth-child(2) { animation-delay: 0.15s; }
  .tsg-ai-typing-dots span:nth-child(3) { animation-delay: 0.3s; }
}

@keyframes tsg-ai-typing-bounce {
  0%, 60%, 100% { opacity: 0.4; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* ---------- Suggested questions ---------- */

.tsg-ai-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 14px 12px;
  flex-shrink: 0;
}

.tsg-ai-suggestion-chip {
  border: 1px solid var(--tsg-border);
  background: transparent;
  color: var(--tsg-teal);
  font-size: 12px;
  padding: 6px 11px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
  text-align: left;
}

.tsg-ai-suggestion-chip:hover {
  background: rgba(91, 185, 195, 0.1);
  border-color: var(--tsg-teal);
}

.tsg-ai-suggestion-chip:focus-visible {
  outline: 2px solid var(--tsg-teal);
  outline-offset: 1px;
}

/* ---------- Input row ---------- */

.tsg-ai-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--tsg-border);
  flex-shrink: 0;
  /* iOS home-indicator clearance on the full-screen mobile sheet. */
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
}

.tsg-ai-input {
  flex: 1;
  resize: none;
  max-height: 100px;
  background: var(--tsg-bg);
  border: 1px solid var(--tsg-border);
  color: var(--tsg-white);
  border-radius: 12px;
  padding: 9px 12px;
  font-size: 13.5px;
  font-family: 'Inter', sans-serif;
  line-height: 1.4;
}

.tsg-ai-input:focus-visible {
  outline: none;
  border-color: var(--tsg-teal);
}

.tsg-ai-input:disabled {
  opacity: 0.6;
}

.tsg-ai-send-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border: none;
  border-radius: 10px;
  background: var(--tsg-red);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.tsg-ai-send-btn:hover:not(:disabled) {
  background: var(--tsg-red-dark);
}

.tsg-ai-send-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.tsg-ai-send-btn:focus-visible {
  outline: 2px solid var(--tsg-teal);
  outline-offset: 2px;
}
