/*
  KI-Assistent — Widget und Vollansicht (ADR-0049). Eine Datei für alle Themes.

  ★ FARBEN SIND EIGENSTÄNDIG UND KONTRASTSICHER. Der Mandant setzt seine
    Markenfarbe über --color-primary; hier trägt sie nur Akzente (Ring,
    Linien, getönte Flächen mit dunklem Text). Kein Text steht auf der
    Markenfarbe — so kann kein Branding-Wert die Lesbarkeit zerstören, und
    AT-17 bleibt unberührt (kein Theme-Stylesheet).
  ★ prefers-reduced-motion: keine Bewegung. Fokusringe sichtbar. Mindestgröße
    der Ziele 44 px.
*/
.ai-root {
  --ai-ink: #0d1117;
  --ai-ink-2: #2b3440;
  --ai-muted: #5b6672;
  --ai-line: #e3e8ee;
  --ai-surface: #ffffff;
  --ai-tint: color-mix(in srgb, var(--color-primary, #1f3a5f) 12%, #ffffff);
  --ai-tint-strong: color-mix(in srgb, var(--color-primary, #1f3a5f) 22%, #ffffff);
  --ai-accent: var(--color-primary, #1f3a5f);
  --ai-radius: calc(16px * var(--scale-radius, 1));
  /* Sichtbare Viewporthoehe. Grundwert 100vh, wo vorhanden 100dvh; das Skript
     ueberschreibt sie mit der TATSAECHLICH sichtbaren Hoehe (visualViewport),
     damit eine eingeblendete Bildschirmtastatur das Eingabefeld nicht verdeckt. */
  --ai-vh: 100vh;
  --ai-edge: 1.25rem;
  --ai-shadow: 0 24px 60px rgba(13, 17, 23, .22), 0 2px 8px rgba(13, 17, 23, .08);
  font-family: var(--font-body, system-ui, sans-serif);
  color: var(--ai-ink);
  line-height: 1.5;
}
@supports (height: 100dvh) { .ai-root { --ai-vh: 100dvh; } }
.ai-root *, .ai-root *::before, .ai-root *::after { box-sizing: border-box; }
/* Seitenscrollen hinter der Vollbildoberflaeche (nur mobil, nur solange offen) */
html.ai-lock, html.ai-lock body { overflow: hidden !important; touch-action: none; }
.ai-root[hidden] { display: none !important; }

/* Launcher — unten rechts, immer erreichbar, nie über der Navigation */
.ai-launcher {
  position: fixed; inset-inline-end: 1.25rem; inset-block-end: calc(1.25rem + var(--ai-offset, 0px)); z-index: 60;
  display: inline-flex; align-items: center; gap: .6rem;
  min-height: 3.25rem; padding: .7rem 1.1rem .7rem .9rem;
  border: 0; border-radius: 999px; cursor: pointer;
  background: var(--ai-ink); color: #ffffff;
  font: inherit; font-weight: 600; letter-spacing: .01em;
  box-shadow: 0 10px 30px rgba(13, 17, 23, .28);
  transition: transform .2s ease, box-shadow .2s ease, inset-block-end .15s ease;
}
.ai-launcher:hover { transform: translateY(-2px); box-shadow: 0 14px 34px rgba(13, 17, 23, .32); }
.ai-launcher:focus-visible, .ai-btn:focus-visible, .ai-iconbtn:focus-visible, .ai-linkbtn:focus-visible, .ai-form__input:focus-visible, .ai-msg__source:focus-visible, .ai-actions__link:focus-visible {
  outline: 3px solid var(--ai-accent); outline-offset: 2px;
}
.ai-launcher__dot { width: .7rem; height: .7rem; border-radius: 50%; background: var(--ai-accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ai-accent) 30%, transparent); }
.ai-root--open .ai-launcher { box-shadow: 0 0 0 3px var(--ai-accent), 0 10px 30px rgba(13, 17, 23, .28); }

/* Panel */
.ai-panel {
  position: fixed; inset-inline-end: var(--ai-edge); inset-block-end: calc(5.25rem + var(--ai-offset, 0px)); z-index: 61;
  width: min(27rem, calc(100vw - 2.5rem));
  /* Die Hoehe waechst mit dem Gespraech - bis hoechstens 85 % des sichtbaren
     Viewports und nie ueber den Platz zwischen Launcher und oberem Rand hinaus.
     Ein kurzer Verlauf erzeugt damit KEINE Leerflaeche, ein langer laeuft nicht
     aus dem Bild: gescrollt wird ausschliesslich innen, im Nachrichtenbereich. */
  /* ★ SOFORT DIE VOLLE HOEHE. Vorher wuchs das Fenster mit dem Gespraech -
     beim Oeffnen war es klein, das Projektprofil lag ausserhalb des Bildes und
     der Verlauf bekam erst spaeter Platz. Jetzt ist die Zielhoehe die HOEHE,
     nicht die Obergrenze: eine Zeile, drei Grenzen (Zielmass, 85 % des
     sichtbaren Viewports, verfuegbarer Platz ueber dem Launcher/Banner). */
  height: min(56rem, calc(var(--ai-vh) * 0.85), calc(var(--ai-vh) - 7rem - var(--ai-offset, 0px)));
  max-height: min(56rem, calc(var(--ai-vh) * 0.85), calc(var(--ai-vh) - 7rem - var(--ai-offset, 0px)));
  display: flex; flex-direction: column;
  background: var(--ai-surface); color: var(--ai-ink);
  border-radius: var(--ai-radius); box-shadow: var(--ai-shadow); border: 1px solid var(--ai-line);
  overflow: hidden;
  transform-origin: bottom right;
  animation: ai-pop .22s ease-out;
}
.ai-panel[hidden] { display: none; }
@keyframes ai-pop { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
/* Mobil: kein schwebendes Kaertchen, sondern eine Vollbildoberflaeche.
   Hoehe aus --ai-vh (dvh bzw. visualViewport), Raender ohne Abstand, unter dem
   Chat ist von der Website nichts mehr zu sehen, und der Launcher weicht. */
@media (max-width: 34rem) {
  .ai-launcher__label { display: none; }
  .ai-launcher { padding: .9rem; }
}
/* ★ Die Vollbildoberflaeche haengt NICHT nur an der Breite. Sie gilt, sobald
   der sichtbare Platz zu klein fuer ein schwebendes Fenster wird - also auch
   bei 200 % Zoom oder wenn ein hoher Einwilligungsbanner fast alles nimmt.
   Die Klasse setzt das Skript aus gemessener Hoehe und Breite. */
.ai-root--sheet:not(.ai-root--page) {
  --ai-sheet: 1;
}
.ai-root--sheet:not(.ai-root--page) .ai-panel {
    position: fixed; inset: 0; inset-block-end: auto;
    width: 100%; height: var(--ai-vh);
    min-height: 0; max-height: none;
    border: 0; border-radius: 0; box-shadow: none;
    transform-origin: bottom center; animation: ai-rise .2s ease-out;
}
.ai-root--sheet.ai-root--open:not(.ai-root--page) .ai-launcher { display: none; }
.ai-root--sheet:not(.ai-root--page) .ai-panel__head { padding-block-start: max(1rem, env(safe-area-inset-top, 0px)); }
.ai-root--sheet:not(.ai-root--page) .ai-panel__foot { padding-block-end: max(.7rem, env(safe-area-inset-bottom, 0px)); }
.ai-root--sheet:not(.ai-root--page) .ai-form { padding-inline: 1rem; }
@keyframes ai-rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

/* ★ ENG HEISST SPARSAM, NICHT STUMM. Bleibt wenig Platz (kleiner Bildschirm
   oder ein hoher Einwilligungsbanner davor), schrumpfen ABSTAENDE - keine
   Aussage verschwindet: Testbetriebshinweis, Aktionen und Fusszeile bleiben
   sichtbar, damit der Verlauf trotzdem lesbar viel Raum behaelt.
   Die Klasse setzt das Skript aus der tatsaechlich verfuegbaren Hoehe;
   eine reine Medienabfrage kennt den Banner nicht. */
/* Wird es eng, weicht auch der Launcher, solange das Fenster offen ist: Er
   ist nur der Weg hinein, das Kreuz ist der Weg hinaus - und sein Band sind
   84 px, die dem Gespraech fehlen. */
.ai-root--tight.ai-root--open .ai-launcher { display: none; }
.ai-root--tight .ai-panel {
  inset-block-end: calc(1.25rem + var(--ai-offset, 0px));
  height: min(56rem, calc(var(--ai-vh) * 0.85), calc(var(--ai-vh) - 3.5rem - var(--ai-offset, 0px)));
  max-height: min(56rem, calc(var(--ai-vh) * 0.85), calc(var(--ai-vh) - 3.5rem - var(--ai-offset, 0px)));
}
.ai-root--tight .ai-panel__head { padding: .55rem .8rem .5rem .9rem; }
.ai-root--tight .ai-panel__eyebrow { display: none; }
.ai-root--tight .ai-panel__title { font-size: 1rem; }
.ai-root--tight .ai-iconbtn { width: 2.25rem; height: 2.25rem; }
.ai-root--tight .ai-panel__notice { padding: .35rem .9rem; font-size: .72rem; }
.ai-root--tight .ai-messages { padding: .6rem .9rem .35rem; }
.ai-root--tight .ai-msg { margin-bottom: .55rem; }
.ai-root--tight .ai-actions { padding: .35rem .9rem; }
.ai-root--tight .ai-form { padding: .45rem .9rem .5rem; }
.ai-root--tight .ai-form__label { display: none; }
.ai-root--tight .ai-form__input { min-height: 2.5rem; }
.ai-root--tight .ai-panel__foot { padding: .35rem .9rem .45rem; font-size: .65rem; }
.ai-panel__head { flex: 0 0 auto; display: flex; align-items: flex-start; justify-content: space-between; gap: .75rem; padding: 1rem 1rem .75rem 1.15rem; background: var(--ai-ink); color: #ffffff; }
.ai-panel__eyebrow { margin: 0; font-size: .72rem; letter-spacing: .12em; text-transform: uppercase; color: #b8c4d0; }
.ai-panel__title { margin: .1rem 0 0; font-size: 1.1rem; font-weight: 700; color: #ffffff; line-height: 1.25; }
.ai-panel__tools { display: flex; gap: .25rem; }
.ai-iconbtn { width: 2.5rem; height: 2.5rem; border: 0; border-radius: 50%; background: rgba(255,255,255,.08); color: #ffffff; font-size: 1.25rem; line-height: 1; cursor: pointer; }
.ai-iconbtn:hover { background: rgba(255,255,255,.18); }
.ai-panel__notice { flex: 0 0 auto; margin: 0; padding: .5rem 1.15rem; font-size: .78rem; background: var(--ai-tint); color: var(--ai-ink-2); border-bottom: 1px solid var(--ai-line); }
/* Der Koerper traegt jetzt zwei Ansichten uebereinander: den Verlauf und -
   auf Wunsch - das Projektprofil. Deshalb ein Bezugsrahmen. */
.ai-panel__body { position: relative; display: flex; flex-direction: column; min-height: 0; flex: 1 1 auto; overflow: hidden; }

/* Die schmale Bedienzeile: Fortschritt und Weg ins Profil. Sie nimmt dem
   Gespraech knapp zwei Zeilen Hoehe - mehr nicht. */
.ai-profilebar { flex: 0 0 auto; border-bottom: 1px solid var(--ai-line); background: #fbfcfd; }
.ai-profilebar[hidden] { display: none; }
.ai-profilebar__toggle {
  width: 100%; display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  padding: .45rem 1.15rem; min-height: 2.25rem;
  border: 0; background: none; cursor: pointer;
  font: inherit; font-size: .82rem; font-weight: 600; color: var(--ai-ink-2);
}
.ai-profilebar__toggle:hover { background: #f4f6f8; }
.ai-profilebar__toggle:focus-visible { outline: 3px solid var(--ai-accent); outline-offset: -3px; }
.ai-profilebar__chevron { color: var(--ai-muted); }
.ai-root--profile .ai-profilebar__chevron { transform: rotate(180deg); display: inline-block; }
.ai-messages { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 1rem 1.15rem .5rem; scroll-behavior: smooth; overscroll-behavior: contain; }
.ai-msg { display: flex; flex-direction: column; margin: 0 0 .85rem; }
.ai-msg--visitor { align-items: flex-end; }
.ai-msg__bubble { max-width: 92%; padding: .65rem .9rem; border-radius: 14px; font-size: .95rem; overflow-wrap: anywhere; }
.ai-msg--assistant .ai-msg__bubble { background: #f4f6f8; color: var(--ai-ink); border-bottom-left-radius: 4px; }
.ai-msg--visitor .ai-msg__bubble { background: var(--ai-tint-strong); color: var(--ai-ink); border-bottom-right-radius: 4px; }
.ai-msg__bubble p { margin: 0 0 .5rem; }
.ai-msg__bubble p:last-child { margin: 0; }
.ai-msg__bubble a { color: var(--ai-ink); text-decoration: underline; text-underline-offset: 2px; }
.ai-msg__meta { margin-top: .3rem; font-size: .72rem; color: var(--ai-muted); display: flex; flex-wrap: wrap; gap: .5rem; }
.ai-msg__who { font-weight: 600; }
.ai-msg__source { color: var(--ai-ink-2); text-decoration: underline; text-underline-offset: 2px; }
.ai-msg--typing .ai-msg__bubble { display: inline-flex; gap: .3rem; align-items: center; }
.ai-dot { width: .45rem; height: .45rem; border-radius: 50%; background: var(--ai-muted); animation: ai-blink 1.1s infinite ease-in-out; }
.ai-dot:nth-child(2) { animation-delay: .18s; } .ai-dot:nth-child(3) { animation-delay: .36s; }
@keyframes ai-blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.ai-suggest { margin: 0 0 .85rem; padding: .6rem .8rem; border-left: 3px solid var(--ai-accent); background: var(--ai-tint); font-size: .88rem; border-radius: 6px; }
.ai-linkbtn { border: 0; background: none; padding: 0; margin-inline-start: .35rem; font: inherit; font-weight: 600; color: var(--ai-ink); text-decoration: underline; text-underline-offset: 2px; cursor: pointer; }

/* Aktionen INNERHALB einer Nachricht - Antwortmoeglichkeiten und Schritte.
   Sie kommen aus dem serverseitigen Katalog; die Beschriftung ist Text. */
.ai-msg__actions { display: flex; flex-wrap: wrap; gap: .4rem; margin: .5rem 0 .2rem; }
.ai-chip {
  font: inherit; font-size: .85rem; line-height: 1.2;
  min-height: 2.25rem; padding: .45rem .85rem;
  border: 1px solid #c9d2dc; border-radius: 999px;
  background: #ffffff; color: var(--ai-ink); cursor: pointer;
  transition: background-color .15s ease, border-color .15s ease;
}
.ai-chip:hover { background: #f4f6f8; border-color: var(--ai-accent); }
.ai-chip:focus-visible { outline: 3px solid var(--ai-accent); outline-offset: 2px; }
.ai-chip:disabled { opacity: .5; cursor: not-allowed; }
/* Ein Schritt (Auswertung, Beratung, Erstanalyse) traegt mehr Gewicht als eine
   blosse Antwortmoeglichkeit - aber immer noch kein Verkaufsbanner. */
.ai-chip--do { background: var(--ai-ink); color: #ffffff; border-color: var(--ai-ink); font-weight: 600; }
.ai-chip--do:hover { background: var(--ai-ink-2); border-color: var(--ai-ink-2); }

/* Projektprofil */
/* ★ DAS PROFIL IST EINE ZWEITE ANSICHT DESSELBEN FENSTERS - kein zweiter
   Bereich unter dem Chat und kein neues Fenster: Es legt sich UEBER den
   Verlauf, in derselben Aussenflaeche, und gibt ihn beim Schliessen
   unveraendert wieder frei (auch den Scrollstand). */
/* ★ Der Kopf des Profils gehoert NICHT in den Scrollbereich: Titel und
   Schliessen muessen immer erreichbar sein - gemessen, vorher scrollten sie weg. */
.ai-profile {
  position: absolute; inset: 0; z-index: 2;
  display: flex; flex-direction: column;
  padding: .75rem 1.15rem 1rem; background: var(--ai-surface);
  overflow: hidden; overscroll-behavior: contain;
}
.ai-profile[hidden] { display: none; }
.ai-profile__head { flex: 0 0 auto; display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: .25rem; }
.ai-profile .ai-profile__hint { flex: 0 0 auto; }
.ai-profile__list { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.ai-profile__title { margin: 0; font-size: .82rem; letter-spacing: .08em; text-transform: uppercase; color: var(--ai-ink-2); }
.ai-iconbtn--quiet { background: #eef1f5; color: var(--ai-ink); width: 2.25rem; height: 2.25rem; }
.ai-iconbtn--quiet:hover { background: #e2e7ee; }
.ai-profile__hint { margin: .15rem 0 .5rem; font-size: .75rem; color: var(--ai-muted); }
.ai-profile__list { margin: 0; display: grid; grid-template-columns: minmax(6rem, 35%) 1fr; gap: .25rem .75rem; font-size: .85rem; }
.ai-fact__label { color: var(--ai-muted); }
.ai-fact__value { margin: 0; display: flex; flex-wrap: wrap; gap: .35rem; align-items: baseline; }
.ai-fact__origin { color: var(--ai-muted); font-size: .75rem; }
.ai-fact__editor { display: grid; gap: .35rem; width: 100%; }

/* Aktionen */
.ai-actions { flex: 0 0 auto; display: flex; flex-wrap: wrap; gap: .5rem; padding: .5rem 1.15rem; border-top: 1px solid var(--ai-line); }
.ai-actions:empty { display: none; }
.ai-actions__note { margin: 0; font-size: .82rem; color: var(--ai-ink-2); }
.ai-actions__link { font-size: .82rem; color: var(--ai-ink-2); text-decoration: underline; text-underline-offset: 2px; align-self: center; }

/* Formulare */
.ai-form { flex: 0 0 auto; display: grid; grid-template-columns: 1fr auto; gap: .5rem; align-items: end; padding: .75rem 1.15rem 1rem; border-top: 1px solid var(--ai-line); background: var(--ai-surface); }
.ai-form[hidden] { display: none; }
.ai-form--contact { grid-template-columns: 1fr; }
.ai-form__label { grid-column: 1 / -1; font-size: .78rem; color: var(--ai-muted); }
.ai-form__intro { grid-column: 1 / -1; margin: 0; font-size: .82rem; color: var(--ai-ink-2); }
.ai-form__input { width: 100%; min-height: 2.75rem; padding: .6rem .75rem; border: 1px solid #c9d2dc; border-radius: 10px; font: inherit; color: var(--ai-ink); background: #ffffff; resize: vertical; }
.ai-form__input:disabled { background: #f4f6f8; color: var(--ai-muted); }
.ai-form__row { display: flex; gap: .5rem; flex-wrap: wrap; }
.ai-btn { min-height: 2.75rem; padding: .6rem 1rem; border-radius: 10px; border: 1px solid var(--ai-ink); background: var(--ai-ink); color: #ffffff; font: inherit; font-weight: 600; cursor: pointer; transition: transform .15s ease, box-shadow .15s ease; }
.ai-btn:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(13,17,23,.18); }
.ai-btn:disabled { opacity: .55; cursor: not-allowed; transform: none; box-shadow: none; }
.ai-btn--ghost { background: transparent; color: var(--ai-ink); }
.ai-btn--small { min-height: 2.25rem; padding: .4rem .8rem; font-size: .85rem; }
.ai-status { flex: 0 0 auto; margin: 0; padding: 0 1.15rem .5rem; font-size: .8rem; color: var(--ai-ink-2); min-height: 1.2rem; }
.ai-status:empty { display: none; }
.ai-status--error { color: #8a1c1c; }
.ai-panel__foot { flex: 0 0 auto; display: flex; justify-content: space-between; gap: .75rem; padding: .5rem 1.15rem .7rem; font-size: .7rem; color: var(--ai-muted); border-top: 1px solid var(--ai-line); background: #fbfcfd; }
.ai-panel__foot a { color: var(--ai-ink-2); }
.ai-root--busy .ai-btn--primary { opacity: .7; }

/* Vollansicht (Seite /ki-erstanalyse)
   ★ DIESELBE BAUFORM WIE IM WIDGET. Vorher stand das Profil hier als zweite
     Spalte neben dem Verlauf; seit es eine zweite ANSICHT ist, gilt das auch
     auf der Seite - eine Oberflaeche, ein Ort fuer das Profil, ein Verhalten. */
.ai-root--page .ai-panel { position: static; width: 100%; max-width: 62rem; height: auto; max-height: none; min-height: 34rem; margin: 0 auto; animation: none; box-shadow: 0 10px 40px rgba(13,17,23,.10); }
.ai-root--page .ai-panel__body { flex-direction: column; min-height: 30rem; }
.ai-root--page .ai-messages { flex: 1 1 auto; min-height: 20rem; max-height: 60vh; }
.ai-page__noscript { padding: 1rem; border: 1px solid var(--ai-line, #e3e8ee); border-radius: 12px; }

@media (prefers-reduced-motion: reduce) {
  .ai-panel, .ai-dot, .ai-launcher, .ai-btn { animation: none !important; transition: none !important; }
  .ai-messages { scroll-behavior: auto; }
  .ai-launcher:hover, .ai-btn:hover { transform: none; }
}
