/*
 * AI assistant component (module ai-assistant). Uses the host's theme variables (PrimeFaces + vw3 tokens) with
 * fallbacks, so it follows light/dark mode of Verwaltung 3 and still works on a page without them.
 * Desktop: docked side panel (pushes the page on wide screens). Phone: full-screen sheet.
 */
.ai-root, .ai-panel {
  --ai-accent: var(--primary-color, #2563eb);
  --ai-accent-text: var(--primary-color-text, #fff);
  --ai-surface: var(--surface-card, #fff);
  --ai-ground: var(--surface-ground, #f6f7f9);
  --ai-text: var(--text-color, #18181b);
  --ai-muted: var(--text-color-secondary, #6b7280);
  --ai-border: var(--vw3-hairline-strong, rgba(24, 24, 27, .14));
  --ai-hairline: var(--vw3-hairline, rgba(24, 24, 27, .08));
  --ai-radius: var(--vw3-radius, 8px);
  --ai-radius-lg: var(--vw3-radius-lg, 12px);
  --ai-shadow: var(--vw3-shadow-pop, 0 12px 32px -8px rgba(24, 24, 27, .25));
  --ai-success: var(--vw3-success, #15803d);
  --ai-success-bg: var(--vw3-success-bg, #ecfdf3);
  --ai-warn: var(--vw3-warn, #a15c07);
  --ai-warn-bg: var(--vw3-warn-bg, #fff7e8);
  --ai-danger: var(--vw3-danger, #b42318);
  --ai-touch: 2.75rem;
}
/* touch: no grey/blue tap flash, and 16px inputs so iOS does not zoom on focus */
.ai-panel button, .ai-launch, .ai-chip { -webkit-tap-highlight-color: transparent; }
/* the hidden attribute must win over the display of the component classes */
.ai-panel [hidden], .ai-root [hidden] { display: none !important; }
/* on :root: the page (body) uses it too, to make room for the docked panel */
:root { --ai-panel-w: 26rem; }

/* ---------- launcher ---------- */
.ai-launch {
  display: inline-flex; align-items: center; gap: .5rem;
  min-height: 2.5rem; padding: .5rem 1rem;
  border: 1px solid var(--ai-border); border-radius: var(--ai-radius);
  background: var(--ai-surface); color: var(--ai-text);
  font: inherit; font-weight: 600; cursor: pointer;
  transition: background-color .15s, border-color .15s;
}
.ai-launch:hover { border-color: var(--ai-accent); }
.ai-launch:focus-visible, .ai-panel :focus-visible { outline: 2px solid var(--ai-accent); outline-offset: 2px; }
.ai-launch[aria-expanded="true"] { border-color: var(--ai-accent); color: var(--ai-accent); }
/* full-width launcher, e.g. in a sidebar */
.ai-root--block { display: block; }
.ai-root--block .ai-launch { display: flex; width: 100%; justify-content: flex-start; }
.ai-icon { width: 1.125rem; height: 1.125rem; flex: none; }
.ai-launch .ai-icon { color: var(--ai-accent); }

/* ---------- panel ---------- */
.ai-panel {
  position: fixed; z-index: 1100; top: 0; right: 0; bottom: 0;
  width: min(var(--ai-panel-w), 100vw);
  display: flex; flex-direction: column;
  background: var(--ai-surface); color: var(--ai-text);
  box-shadow: var(--ai-shadow); border-left: 1px solid var(--ai-hairline);
  transform: translateX(100%); visibility: hidden;
  transition: transform .2s ease, visibility 0s linear .2s;
  font-size: .9375rem; line-height: 1.5;
}
.ai-panel.is-open { transform: none; visibility: visible; transition: transform .2s ease; }
@media (prefers-reduced-motion: reduce) { .ai-panel, .ai-panel.is-open { transition: none; } }
/* wide screens: the panel is docked, the page gets narrower instead of being covered */
@media (min-width: 1280px) { html.ai-docked body { padding-right: var(--ai-panel-w); } }
/* phones: full-screen sheet, safe areas */
@media (max-width: 767px) {
  .ai-panel { width: 100vw; border-left: 0; padding-bottom: env(safe-area-inset-bottom, 0); }
  html.ai-sheet-open, html.ai-sheet-open body { overflow: hidden; }
}

.ai-panel__header {
  position: relative; display: flex; align-items: center; gap: .5rem;
  padding: .75rem .75rem .75rem 1rem; padding-top: max(.75rem, env(safe-area-inset-top, 0));
  border-bottom: 1px solid var(--ai-hairline);
}
.ai-panel__title { display: flex; align-items: center; gap: .5rem; min-width: 0; flex: 1; }
.ai-panel__title h2 { margin: 0; font-size: 1rem; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-panel__title .ai-icon { color: var(--ai-accent); }
.ai-iconbtn {
  display: inline-grid; place-items: center; width: var(--ai-touch); height: var(--ai-touch);
  border: 0; border-radius: var(--ai-radius); background: transparent; color: var(--ai-muted); cursor: pointer;
}
.ai-iconbtn:hover { background: var(--ai-ground); color: var(--ai-text); }
@media (pointer: fine) { .ai-iconbtn { width: 2.25rem; height: 2.25rem; } }

.ai-history {
  position: absolute; z-index: 2; top: calc(100% - .25rem); right: .75rem; left: .75rem; max-height: 60vh; overflow: auto;
  margin: 0; padding: .375rem; list-style: none;
  background: var(--ai-surface); border: 1px solid var(--ai-border); border-radius: var(--ai-radius); box-shadow: var(--ai-shadow);
}
.ai-history[hidden] { display: none; }
.ai-history li { display: flex; align-items: center; gap: .25rem; }
.ai-history button.ai-history__open {
  flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: .125rem;
  padding: .5rem .625rem; border: 0; border-radius: calc(var(--ai-radius) - 2px); background: transparent; color: inherit;
  font: inherit; text-align: left; cursor: pointer;
}
.ai-history button.ai-history__open:hover, .ai-history button.ai-history__open[aria-current="true"] { background: var(--ai-ground); }
.ai-history__title { width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-history__date, .ai-history__empty { font-size: .8125rem; color: var(--ai-muted); }
.ai-history__empty { padding: .5rem .625rem; }

.ai-panel__body { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 1rem; scroll-padding-bottom: 1rem; }

/* ---------- welcome ---------- */
.ai-welcome { display: flex; flex-direction: column; gap: .75rem; padding: .5rem 0 1rem; }
.ai-welcome p { margin: 0; color: var(--ai-muted); }
.ai-starters { display: flex; flex-direction: column; gap: .5rem; margin: 0; padding: 0; list-style: none; }
.ai-starter {
  width: 100%; min-height: var(--ai-touch); padding: .5rem .75rem; text-align: left;
  border: 1px solid var(--ai-border); border-radius: var(--ai-radius); background: var(--ai-surface); color: var(--ai-text);
  font: inherit; cursor: pointer;
}
.ai-starter:hover { border-color: var(--ai-accent); }

/* ---------- messages ---------- */
.ai-messages { display: flex; flex-direction: column; gap: 1rem; margin: 0; padding: 0; list-style: none; }
.ai-msg { display: flex; flex-direction: column; gap: .5rem; min-width: 0; }
.ai-msg--user { align-items: flex-end; }
.ai-bubble {
  max-width: 88%; padding: .5rem .75rem; border-radius: var(--ai-radius-lg) var(--ai-radius-lg) .25rem var(--ai-radius-lg);
  background: var(--ai-accent); color: var(--ai-accent-text); white-space: pre-wrap; overflow-wrap: anywhere;
}
.ai-bubble--answers { background: var(--ai-ground); color: var(--ai-text); border: 1px solid var(--ai-hairline); white-space: normal; }
.ai-bubble--answers dl { margin: 0; display: grid; gap: .25rem; }
.ai-bubble--answers dt { font-size: .8125rem; color: var(--ai-muted); }
.ai-bubble--answers dd { margin: 0 0 .25rem; overflow-wrap: anywhere; }
.ai-q__item legend { max-width: 100%; white-space: normal; overflow-wrap: anywhere; }
.ai-text { overflow-wrap: anywhere; }
.ai-inline-link { color: var(--ai-accent); font-weight: 600; text-decoration: underline; text-underline-offset: 2px; }
.ai-inline-link:focus-visible { outline: 2px solid var(--ai-accent); outline-offset: 2px; border-radius: 2px; }
.ai-text p { margin: 0 0 .5rem; }
.ai-text p:last-child, .ai-text ul:last-child, .ai-text ol:last-child { margin-bottom: 0; }
.ai-text ul, .ai-text ol { margin: 0 0 .5rem; padding-left: 1.25rem; }
.ai-text code { padding: .05rem .3rem; border-radius: 4px; background: var(--ai-ground); font-size: .875em; }
.ai-msg--interrupted .ai-text::after { content: " …"; color: var(--ai-muted); }
.ai-msg__meta { font-size: .8125rem; color: var(--ai-muted); }

.ai-steps { display: flex; flex-wrap: wrap; gap: .375rem; margin: 0; padding: 0; list-style: none; }
.ai-step {
  display: inline-flex; align-items: center; gap: .375rem; padding: .125rem .5rem;
  border-radius: 999px; background: var(--ai-ground); color: var(--ai-muted); font-size: .8125rem;
}
.ai-step::before { content: ""; width: .5rem; height: .5rem; border-radius: 50%; background: currentColor; opacity: .5; }
.ai-step.is-running::before { animation: ai-pulse 1s ease-in-out infinite; background: var(--ai-accent); opacity: 1; }
.ai-step.is-done::before { background: var(--ai-success); opacity: 1; }
.ai-step.is-failed::before { background: var(--ai-danger); opacity: 1; }
.ai-typing { display: inline-flex; gap: .25rem; padding: .25rem 0; }
.ai-typing span { width: .375rem; height: .375rem; border-radius: 50%; background: var(--ai-muted); animation: ai-pulse 1.2s ease-in-out infinite; }
.ai-typing span:nth-child(2) { animation-delay: .15s; }
.ai-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes ai-pulse { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .ai-typing span, .ai-step.is-running::before { animation: none; } }

.ai-notice { margin: 0; padding: .5rem .75rem; border-radius: var(--ai-radius); font-size: .875rem; background: var(--ai-ground); }
.ai-notice--warn { background: var(--ai-warn-bg); color: var(--ai-warn); }
.ai-notice--error { background: color-mix(in srgb, var(--ai-danger) 12%, transparent); color: var(--ai-danger); }
.ai-fallback { margin: 0; color: var(--ai-muted); white-space: pre-wrap; }

/* ---------- cards (questions, proposal) ---------- */
.ai-card {
  display: flex; flex-direction: column; gap: .75rem; padding: .875rem;
  border: 1px solid var(--ai-border); border-radius: var(--ai-radius-lg); background: var(--ai-surface);
}
.ai-card__head { display: flex; align-items: flex-start; justify-content: space-between; gap: .5rem; }
.ai-card__title { margin: 0; font-weight: 650; }
.ai-card__summary { margin: 0; color: var(--ai-muted); }
.ai-card__actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.ai-card__outcome { margin: 0; font-size: .875rem; }
.ai-badge { flex: none; padding: .0625rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 600; background: var(--ai-ground); color: var(--ai-muted); }
.ai-badge--applied { background: var(--ai-success-bg); color: var(--ai-success); }
.ai-badge--conflict { background: var(--ai-warn-bg); color: var(--ai-warn); }

.ai-q__item { margin: 0; padding: 0; border: 0; min-width: 0; display: flex; flex-direction: column; gap: .5rem; }
.ai-q__item legend { padding: 0; margin-bottom: .5rem; font-weight: 600; }
.ai-q__options { display: flex; flex-wrap: wrap; gap: .5rem; }
.ai-chip { position: relative; display: inline-flex; max-width: 100%; min-width: 0; }
.ai-chip input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.ai-chip span {
  display: inline-flex; align-items: center; min-height: var(--ai-touch); padding: .375rem .875rem;
  border: 1px solid var(--ai-border); border-radius: 999px; background: var(--ai-surface); cursor: pointer;
  /* long answer options wrap inside the chip instead of widening the panel */
  max-width: 100%; white-space: normal; overflow-wrap: anywhere; text-align: start; line-height: 1.3;
}
@media (pointer: fine) { .ai-chip span { min-height: 2.25rem; } }
.ai-chip input:checked + span { border-color: var(--ai-accent); background: color-mix(in srgb, var(--ai-accent) 12%, transparent); color: var(--ai-accent); font-weight: 600; }
.ai-chip input:focus-visible + span { outline: 2px solid var(--ai-accent); outline-offset: 2px; }
.ai-chip input:disabled + span { opacity: .6; cursor: default; }
.ai-input, .ai-composer textarea {
  width: 100%; box-sizing: border-box; min-height: var(--ai-touch); padding: .5rem .75rem;
  border: 1px solid var(--ai-border); border-radius: var(--ai-radius); background: var(--ai-surface); color: var(--ai-text);
  font: inherit;
}
/* iOS Safari zooms into inputs below 16px on focus (and stays zoomed): touch devices and phones get 16px.
   After the rule above – its "font: inherit" reset the size to 15px before. */
@media (pointer: coarse), (max-width: 767px) { .ai-input, .ai-composer textarea { font-size: 16px; } }
.ai-input:focus, .ai-composer textarea:focus { outline: none; border-color: var(--ai-accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ai-accent) 20%, transparent); }
.ai-card[data-status]:not([data-status="open"]):not([data-status="pending"]) { background: var(--ai-ground); }

.ai-diff { width: 100%; border-collapse: collapse; font-size: .875rem; }
.ai-diff th { text-align: left; font-weight: 600; color: var(--ai-muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .03em; padding: 0 .5rem .375rem 0; }
.ai-diff td { padding: .375rem .5rem .375rem 0; border-top: 1px solid var(--ai-hairline); vertical-align: top; overflow-wrap: anywhere; }
.ai-diff td.ai-diff__old { color: var(--ai-muted); text-decoration: line-through; text-decoration-color: color-mix(in srgb, currentColor 40%, transparent); }
.ai-diff td.ai-diff__new { font-weight: 600; }
.ai-diff { table-layout: fixed; }
.ai-diff__value { white-space: normal; overflow-wrap: anywhere; }
/* texts (Webseiten-Inhalte): wrap, keep line breaks, long ones clamped */
.ai-diff__value--text { white-space: pre-line; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 8; overflow: hidden; }
.ai-diff__auto { display: block; font-size: .75rem; font-weight: 400; color: var(--ai-muted); }
.ai-swatch { display: inline-block; width: .875rem; height: .875rem; margin-right: .375rem; vertical-align: -.125rem; border-radius: 3px; border: 1px solid var(--ai-border); }
@container ai-panel (max-width: 22rem) { .ai-diff thead { display: none; } }

/* ---------- links to pages ---------- */
.ai-link {
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  min-height: var(--ai-touch); padding: .5rem .75rem; margin: .25rem 0;
  border: 1px solid var(--ai-border); border-radius: var(--ai-radius);
  background: var(--ai-surface); color: var(--ai-text); text-decoration: none;
  transition: border-color .15s, background-color .15s;
}
.ai-link:hover { border-color: var(--ai-accent); }
.ai-link:focus-visible { outline: 2px solid var(--ai-accent); outline-offset: 2px; }
.ai-link__text { display: flex; flex-direction: column; min-width: 0; }
.ai-link__text small { font-size: .75rem; color: var(--ai-muted); }
.ai-link__arrow { color: var(--ai-accent); font-weight: 600; }
.ai-link__note { margin: 0 0 .5rem; font-size: .8125rem; color: var(--ai-muted); }

/* ---------- buttons ---------- */
.ai-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .375rem;
  min-height: var(--ai-touch); padding: .5rem 1rem; border-radius: var(--ai-radius);
  border: 1px solid var(--ai-border); background: var(--ai-surface); color: var(--ai-text);
  font: inherit; font-weight: 600; cursor: pointer;
}
@media (pointer: fine) { .ai-btn { min-height: 2.375rem; } }
.ai-btn--primary { border-color: var(--ai-accent); background: var(--ai-accent); color: var(--ai-accent-text); }
.ai-btn--primary:hover { filter: brightness(1.06); }
.ai-btn:disabled { opacity: .55; cursor: default; filter: none; }

/* ---------- banner + composer ---------- */
.ai-banner { display: flex; align-items: center; gap: .5rem; margin: 0 1rem .5rem; padding: .5rem .75rem; border-radius: var(--ai-radius);
  background: color-mix(in srgb, var(--ai-danger) 10%, var(--ai-surface)); color: var(--ai-danger); font-size: .875rem; }
.ai-banner[hidden] { display: none; }
.ai-banner.ai-banner--ok { background: var(--ai-success-bg); color: var(--ai-success); }
.ai-banner span { flex: 1; }
.ai-composer { display: flex; align-items: flex-end; gap: .5rem; padding: .75rem 1rem 0; border-top: 1px solid var(--ai-hairline); }
.ai-composer textarea { resize: none; max-height: 10rem; line-height: 1.4; }
.ai-composer .ai-btn { flex: none; }
.ai-disclaimer { margin: 0; padding: .375rem 1rem .75rem; font-size: .75rem; color: var(--ai-muted); }
.ai-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ai-panel { container: ai-panel / inline-size; }
@media (forced-colors: active) { .ai-chip input:checked + span, .ai-btn--primary { border: 2px solid CanvasText; } }
