/* Q4U Help Dialog - full styles (Task 12).
   Uses Bootstrap 5.3 + theme CSS variables. Dark mode via [data-bs-theme="dark"]. */

/* ------------------------------------------------------------------ */
/* Launcher                                                            */
/* Geometry, colours and label chip come from q4u_base's floating-      */
/* controls family (.q4u-fab, components/_floating-controls.scss):      */
/* slot 1 of the right "page" stack, back-to-top above it. The rules    */
/* below are the STANDALONE fallback for installs without q4u_base,     */
/* scoped to html:not([data-q4u-floating]).                             */
/* ------------------------------------------------------------------ */
html:not([data-q4u-floating]) .q4u-hd-launcher {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: 56px;
  height: 56px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
  background: var(--bs-primary, #756FBA);
  color: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .18);
  z-index: 990;
}

html:not([data-q4u-floating]) .q4u-hd-launcher .q4u-fab__label {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

html:not([data-q4u-floating]) .q4u-hd-launcher:focus-visible {
  outline: 3px solid var(--bs-emphasis-color, #000);
  outline-offset: 3px;
}

/* Glyph "?" — colour inherits from the disc (family role or fallback). */
.q4u-hd-launcher-glyph {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: calc(var(--q4u-fab-icon, 26px) * 1.15);
  font-weight: 700;
  line-height: 1;
  color: currentColor;
  /* Optical centering: a "?" sits a touch higher than its bounding box centre */
  transform: translateY(1px);
}

/* Pulse animation for first visit (help-dialog.js skips it under reduced motion).
   It rides a pseudo element, never the button's own box-shadow: the family draws the
   focus ring with box-shadow, and a running animation would override it for the whole
   pulse — a keyboard user would have no visible focus indicator. The ring rides
   --bs-primary-rgb so it follows the active theme; the literal fallback keeps the old
   value on installs without the variable. */
@keyframes q4u-hd-pulse-keyframes {
  0%   { box-shadow: 0 0 0 0 rgba(var(--bs-primary-rgb, 117, 111, 186), .55); }
  70%  { box-shadow: 0 0 0 14px rgba(var(--bs-primary-rgb, 117, 111, 186), 0); }
  100% { box-shadow: 0 0 0 0 rgba(var(--bs-primary-rgb, 117, 111, 186), 0); }
}

.q4u-hd-launcher.q4u-hd-pulse::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
  animation: q4u-hd-pulse-keyframes 1.6s ease-out 2;
}

/* ------------------------------------------------------------------ */
/* Dialog container                                                    */
/* ------------------------------------------------------------------ */
.q4u-hd-dialog {
  width: 380px;
  background: var(--bs-body-bg, #fff);
  color: var(--bs-body-color, #212529);
  border: 1px solid var(--bs-border-color, rgba(0, 0, 0, .15));
  border-radius: min(var(--bs-border-radius-xl, 16px), 20px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .18);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Standalone fallback (no q4u_base): family .q4u-fab-panel--end does this otherwise.
   Tablet and up only — below 576px the bottom-sheet rule further down owns the
   geometry, and this block would out-specify it. */
@media (min-width: 576px) {
  html:not([data-q4u-floating]) .q4u-hd-dialog {
    position: fixed;
    right: 16px;
    bottom: 84px;
    max-width: calc(100vw - 24px);
    max-height: min(640px, calc(100dvh - 180px));
    z-index: 995;
  }
}

.q4u-hd-dialog[hidden] {
  display: none;
}

/* ------------------------------------------------------------------ */
/* Header / Body / Footer                                              */
/* ------------------------------------------------------------------ */
.q4u-hd-header {
  /* .text-bg-primary on this element handles bg + contrast text per active theme */
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .625rem .875rem;
  border-bottom: 1px solid var(--bs-border-color, rgba(0, 0, 0, .08));
  flex: 0 0 auto;
}

.q4u-hd-brand {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  font-weight: 600;
  font-size: .9375rem;
}

.q4u-hd-brand-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  /* Tinted scrim using whatever the contrast colour is — works on white-on-pink AND black-on-pink */
  background: color-mix(in srgb, currentColor 18%, transparent);
  position: relative;
}
.q4u-hd-brand-avatar::before {
  content: "?";
  font-size: 1.125rem;
  font-weight: 700;
  color: currentColor;
}

.q4u-hd-brand-name {
  letter-spacing: .01em;
}

.q4u-hd-close {
  background: transparent;
  border: 0;
  color: currentColor;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background-color .15s ease;
}

.q4u-hd-close:hover,
.q4u-hd-close:focus-visible {
  background: color-mix(in srgb, currentColor 18%, transparent);
}

.q4u-hd-close:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

.q4u-hd-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 1rem;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: .625rem;
  scroll-behavior: smooth;
  background: var(--bs-body-bg, #fff);
}
@media (prefers-reduced-motion: reduce) {
  .q4u-hd-body { scroll-behavior: auto; }
}

/* footer + back button + breadcrumb removed —
   navigation now happens by clicking any prior options block (re-branching). */

/* ------------------------------------------------------------------ */
/* Options list (branch)                                               */
/* ------------------------------------------------------------------ */
.q4u-hd-options {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.q4u-hd-options li {
  margin: 0;
}

.q4u-hd-option {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .875rem 1rem;
  background: var(--bs-tertiary-bg, rgba(0, 0, 0, .02));
  border: 1px solid var(--bs-border-color, rgba(0, 0, 0, .1));
  border-radius: var(--bs-border-radius-lg, 12px);
  color: var(--bs-body-color, #212529);
  text-align: left;
  cursor: pointer;
  min-height: 44px;
  transition: background-color .15s ease, border-color .15s ease, transform .1s ease;
}

.q4u-hd-option:hover,
.q4u-hd-option:focus-visible {
  background: var(--bs-secondary-bg, rgba(117, 111, 186, .08));
  border-color: var(--bs-primary, #756FBA);
}

.q4u-hd-option:focus-visible {
  outline: 2px solid var(--bs-primary, #756FBA);
  outline-offset: 2px;
}

.q4u-hd-option:active {
  transform: scale(.99);
}

.q4u-hd-option-icon {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  color: var(--bs-primary, #756FBA);
}
.q4u-hd-option-icon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.q4u-hd-option-text {
  display: flex;
  flex-direction: column;
  gap: .125rem;
  min-width: 0;
  flex: 1 1 auto;
}

.q4u-hd-option-title {
  font-weight: 600;
  font-size: .9375rem;
}

.q4u-hd-option-subtitle {
  font-size: .8125rem;
  color: var(--bs-secondary-color, #6c757d);
}

.q4u-hd-option-chevron {
  color: var(--bs-secondary-color, #6c757d);
  font-size: 1.25rem;
  flex: 0 0 auto;
}

.q4u-hd-empty {
  color: var(--bs-secondary-color, #6c757d);
  text-align: center;
  padding: 1.5rem .5rem;
  margin: 0;
}

/* ------------------------------------------------------------------ */
/* Leaf view                                                           */
/* ------------------------------------------------------------------ */
.q4u-hd-leaf {
  display: flex;
  flex-direction: column;
  gap: .75rem;
}

.q4u-hd-leaf-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--bs-body-color, #212529);
}

.q4u-hd-leaf-answer {
  font-size: .9375rem;
  line-height: 1.55;
  color: var(--bs-body-color, #212529);
}

.q4u-hd-leaf-answer p:last-child {
  margin-bottom: 0;
}

.q4u-hd-leaf-answer a {
  color: var(--bs-primary, #756FBA);
}

.q4u-hd-ctas {
  display: flex;
  flex-direction: column;
  gap: .5rem;
  margin-top: .5rem;
}

.q4u-hd-cta {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: .625rem 1rem;
  border-radius: 999px;
  font-weight: 600;
  font-size: .9375rem;
  text-decoration: none;
  min-height: 44px;
  border: 1px solid transparent;
  transition: filter .15s ease, background-color .15s ease;
}

.q4u-hd-cta:focus-visible {
  outline: 2px solid var(--bs-primary, #756FBA);
  outline-offset: 2px;
}

/* Primary CTA also carries .text-bg-primary (added in JS) for theme-aware
   bg + contrast text colour. We only set the hover affordance here. */
.q4u-hd-cta-primary:hover {
  filter: brightness(.92);
}

.q4u-hd-cta-secondary {
  background: transparent;
  color: var(--bs-body-color, #212529);
  border-color: var(--bs-border-color, rgba(0, 0, 0, .2));
}

.q4u-hd-cta-secondary:hover {
  background: var(--bs-secondary-bg, rgba(0, 0, 0, .06));
  color: var(--bs-body-color, #212529);
}

/* ------------------------------------------------------------------ */
/* Restore hint                                                        */
/* ------------------------------------------------------------------ */
.q4u-hd-restore-hint {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: .5rem .875rem;
  margin: .5rem 1rem 0;
  background: var(--bs-info-bg-subtle, rgba(13, 202, 240, .1));
  color: var(--bs-info-text-emphasis, var(--bs-body-color, #212529));
  border: 1px solid var(--bs-info-border-subtle, rgba(13, 202, 240, .25));
  border-radius: 8px;
  font-size: .8125rem;
}

.q4u-hd-reset {
  background: transparent;
  border: 0;
  color: var(--bs-primary, #756FBA);
  font-weight: 600;
  cursor: pointer;
  padding: .25rem .5rem;
  font-size: .8125rem;
  text-decoration: underline;
}

.q4u-hd-reset:focus-visible {
  outline: 2px solid var(--bs-primary, #756FBA);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ------------------------------------------------------------------ */
/* Error                                                               */
/* ------------------------------------------------------------------ */
.q4u-hd-error {
  text-align: center;
  padding: 1.5rem .5rem;
  color: var(--bs-body-color, #212529);
}

.q4u-hd-error p {
  margin-bottom: 1rem;
  color: var(--bs-secondary-color, #6c757d);
}

/* .q4u-hd-retry is rendered with Bootstrap's btn btn-sm btn-outline-secondary
   utilities for theme-aware styling — no custom bg/color here. */
.q4u-hd-retry {
  margin-top: .25rem;
}

/* ------------------------------------------------------------------ */
/* Slide animations                                                    */
/* ------------------------------------------------------------------ */
@keyframes q4u-hd-out-left {
  from { transform: translateX(0); opacity: 1; }
  to   { transform: translateX(-16px); opacity: 0; }
}

@keyframes q4u-hd-out-right {
  from { transform: translateX(0); opacity: 1; }
  to   { transform: translateX(16px); opacity: 0; }
}

@keyframes q4u-hd-in-right {
  from { transform: translateX(16px); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

@keyframes q4u-hd-in-left {
  from { transform: translateX(-16px); opacity: 0; }
  to   { transform: translateX(0); opacity: 1; }
}

.q4u-hd-slide-out-left  { animation: q4u-hd-out-left  .18s ease-in forwards; }
.q4u-hd-slide-out-right { animation: q4u-hd-out-right .18s ease-in forwards; }
.q4u-hd-slide-in-right  { animation: q4u-hd-in-right  .22s ease-out; }
.q4u-hd-slide-in-left   { animation: q4u-hd-in-left   .22s ease-out; }

/* ------------------------------------------------------------------ */
/* Mobile: bottom sheet                                                */
/* ------------------------------------------------------------------ */
@media (max-width: 575.98px) {
  .q4u-hd-dialog {
    /* position + z-index: the family supplies these in integrated mode; the
       standalone fallback above is scoped to (min-width: 576px) and no longer
       reaches phones, so this rule must carry them itself. */
    position: fixed;
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    max-height: 85dvh;
    z-index: 995;
    padding-bottom: env(safe-area-inset-bottom);
    border-radius: min(var(--bs-border-radius-xl, 16px), 20px) min(var(--bs-border-radius-xl, 16px), 20px) 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
  }

  body.q4u-hd-locked {
    overflow: hidden;
  }
}

/* ------------------------------------------------------------------ */
/* Chat-style conversation: bubbles, typing, picked-state              */
/* ------------------------------------------------------------------ */
.q4u-hd-conversation { /* alias for .q4u-hd-body when in chat mode */ }

.q4u-hd-msg {
  display: flex;
  max-width: 88%;
  animation: q4u-hd-msg-in .22s ease-out both;
}
.q4u-hd-msg--instant { animation: none; }
@media (prefers-reduced-motion: reduce) {
  .q4u-hd-msg { animation: none; }
}

@keyframes q4u-hd-msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.q4u-hd-msg-bubble {
  padding: .625rem .875rem;
  border-radius: 14px;
  font-size: .9375rem;
  line-height: 1.45;
  word-wrap: break-word;
}

/* Bot bubble: left-aligned, neutral surface */
.q4u-hd-msg--bot {
  align-self: flex-start;
}
.q4u-hd-msg--bot .q4u-hd-msg-bubble {
  background: var(--bs-tertiary-bg, #f4f4f6);
  color: var(--bs-body-color, #212529);
  border-top-left-radius: 4px;
}

/* User bubble: right-aligned. .text-bg-primary on the inner bubble (added
   in JS) paints bg + theme-correct contrast colour in both light + dark. */
.q4u-hd-msg--user {
  align-self: flex-end;
}
.q4u-hd-msg--user .q4u-hd-msg-bubble {
  border-top-right-radius: 4px;
}

/* Greeting bubble may carry an avatar dot to break the wall-of-text feel */
.q4u-hd-msg--greeting .q4u-hd-msg-bubble {
  background: var(--bs-tertiary-bg, #f4f4f6);
}

/* Leaf answer bubble: a touch wider + RTE styles */
.q4u-hd-msg--leaf {
  max-width: 100%;
}
.q4u-hd-msg--leaf .q4u-hd-msg-bubble p:last-child { margin-bottom: 0; }
.q4u-hd-msg--leaf .q4u-hd-msg-bubble a {
  color: var(--bs-primary, #756FBA);
}

/* ─── Image gallery in leaf turn ─────────────────────────────────────
 * Always 1-column at full leaf width — the dialog body itself is narrow
 * (≤ 380px desktop, full sheet width on mobile) and side-by-side splits
 * make thumbnails too small to read. Stacked, 16:9, gallery bubble
 * shares the same outer width as the answer bubble below. */
.q4u-hd-msg--gallery {
  max-width: 100%;
  margin-top: .5rem;
}
.q4u-hd-gallery {
  display: grid;
  grid-template-columns: 1fr;
  gap: .5rem;
  max-width: 100%;
}
.q4u-hd-gallery-figure {
  margin: 0;
  background: var(--bs-tertiary-bg, #f8f9fa);
  border-radius: var(--bs-border-radius, .375rem);
  overflow: hidden;
  aspect-ratio: 16 / 9;
}
.q4u-hd-gallery-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Error bubble: subtle accent, retry button */
.q4u-hd-msg--error .q4u-hd-msg-bubble p { margin: 0 0 .5rem; }
.q4u-hd-msg--error .q4u-hd-retry { margin-top: .25rem; }

/* Typing indicator: three pulsing dots in a bot bubble */
.q4u-hd-typing .q4u-hd-msg-bubble {
  display: inline-flex;
  align-items: center;
  gap: .25rem;
  padding: .75rem .875rem;
  min-height: 2.25rem;
}
.q4u-hd-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--bs-secondary-color, #6c757d);
  opacity: .35;
  animation: q4u-hd-dot 1.2s infinite ease-in-out both;
}
.q4u-hd-dot:nth-child(2) { animation-delay: .15s; }
.q4u-hd-dot:nth-child(3) { animation-delay: .3s; }
@keyframes q4u-hd-dot {
  0%, 80%, 100% { opacity: .35; transform: scale(.8); }
  40%           { opacity: 1;   transform: scale(1.1); }
}
@media (prefers-reduced-motion: reduce) {
  .q4u-hd-dot { animation: none; opacity: .65; }
}

/* Options after a bot bubble — render inline (no extra wrapping) */
.q4u-hd-options { margin-top: .125rem; }

/* Picked option (highlighted) — block stays clickable so user can re-branch
   from any prior turn by clicking a different (or same) option. */
.q4u-hd-option--picked {
  border-color: var(--bs-primary, #756FBA);
  background: var(--bs-secondary-bg, rgba(117, 111, 186, .12));
}
.q4u-hd-option--picked .q4u-hd-option-title {
  color: var(--bs-primary, #756FBA);
}

/* "Zur Übersicht" inline link below leaf bubble */
.q4u-hd-overview-link {
  align-self: flex-start;
  margin-top: -.25rem;
}
.q4u-hd-link {
  background: transparent;
  border: 0;
  padding: .25rem 0;
  color: var(--bs-primary, #756FBA);
  font-size: .8125rem;
  cursor: pointer;
  text-decoration: underline;
}
.q4u-hd-link:hover,
.q4u-hd-link:focus-visible {
  color: var(--bs-body-color, #212529);
}
.q4u-hd-link:focus-visible {
  outline: 2px solid var(--bs-primary, #756FBA);
  outline-offset: 2px;
}

/* Restore-from-session hint sits at the very top of the log */
.q4u-hd-restore-hint {
  font-size: .8125rem;
  color: var(--bs-secondary-color, #6c757d);
  background: var(--bs-tertiary-bg, rgba(0, 0, 0, .03));
  border-radius: 999px;
  padding: .25rem .625rem;
  align-self: center;
  display: inline-flex;
  align-items: center;
  gap: .25rem;
}
.q4u-hd-restore-hint .q4u-hd-reset {
  background: transparent;
  border: 0;
  color: var(--bs-primary, #756FBA);
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
  font: inherit;
}

/* CTA row sits below the leaf bubble, full-width */
.q4u-hd-ctas {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-self: stretch;
  max-width: 100%;
}

/* ------------------------------------------------------------------ */
/* Dark mode (Bootstrap 5.3 [data-bs-theme="dark"])                    */
/* ------------------------------------------------------------------ */
[data-bs-theme="dark"] .q4u-hd-dialog {
  background: var(--bs-body-bg);
  color: var(--bs-body-color);
  border-color: var(--bs-border-color);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .55);
}

/* Header keeps its theme-primary background even in dark mode (brand bar) */

[data-bs-theme="dark"] .q4u-hd-msg--bot .q4u-hd-msg-bubble {
  background: var(--bs-tertiary-bg);
  color: var(--bs-body-color);
}
[data-bs-theme="dark"] .q4u-hd-restore-hint {
  background: var(--bs-tertiary-bg);
}

[data-bs-theme="dark"] .q4u-hd-option {
  background: var(--bs-tertiary-bg);
  border-color: var(--bs-border-color);
  color: var(--bs-body-color);
}

[data-bs-theme="dark"] .q4u-hd-option:hover,
[data-bs-theme="dark"] .q4u-hd-option:focus-visible {
  background: var(--bs-secondary-bg);
  border-color: var(--bs-primary, #756FBA);
}

/* Bootstrap-style SVG <img> icons render dark-on-transparent — invert in
   dark mode so they stay legible on the dark bubble surface. */
[data-bs-theme="dark"] .q4u-hd-option-icon img {
  filter: brightness(0) invert(1);
}

[data-bs-theme="dark"] .q4u-hd-cta-secondary {
  color: var(--bs-body-color);
  border-color: var(--bs-border-color);
}

[data-bs-theme="dark"] .q4u-hd-cta-secondary:hover {
  background: var(--bs-secondary-bg);
  color: var(--bs-body-color);
}

/* No close-button dark-mode override needed:
   .text-bg-primary on the header sets the right contrast colour in
   both light and dark modes; close inherits via currentColor. */

/* ------------------------------------------------------------------ */
/* Reduced motion                                                      */
/* ------------------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
  .q4u-hd-launcher,
  .q4u-hd-option,
  .q4u-hd-cta,
  .q4u-hd-close {
    transition: none;
  }

  .q4u-hd-launcher.q4u-hd-pulse::after {
    animation: none;
  }

  .q4u-hd-slide-out-left,
  .q4u-hd-slide-out-right,
  .q4u-hd-slide-in-right,
  .q4u-hd-slide-in-left {
    animation: none;
  }
}
