/* Agent-Seite (/agent): Chat mit Kontextleiste, Live-Anzeige und Dokumenten.
   Ergaenzt style.css und uebernimmt dessen Variablen. */

.chatapp {
  display: grid;
  grid-template-columns: 268px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "kopf  kopf"
    "seite verlauf"
    "seite eingabe";
  height: var(--vh, 100vh);
  background: var(--bg);
  color: var(--fg);
  overflow: hidden;
}

.chatapp .topbar { grid-area: kopf; }
.chatapp .topbar .spacer { flex: 1; }
.chat-side       { grid-area: seite; }
.chat-main       { grid-area: verlauf; }
.composer-bereich { grid-area: eingabe; }

.klein { font-size: 11.5px; }

/* ========================== Seitenleiste ========================== */

.chat-side {
  background: var(--bg-1);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding-bottom: 20px;
}

.side-block { border-bottom: 1px solid var(--line-soft); padding: 12px 13px 14px; }
.side-block:last-child { border-bottom: 0; }

.side-kopf {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 9px;
}
.side-kopf h3 {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--fg-mute);
  flex: 1;
}

.chatliste, .liveliste, .dokliste { list-style: none; margin: 0; padding: 0; }

.chatliste li {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 9px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 12.5px;
  color: var(--fg-dim);
}
.chatliste li:hover { background: var(--bg-2); }
.chatliste li[aria-selected="true"] {
  background: var(--accent-bg);
  color: var(--fg);
  box-shadow: inset 2px 0 0 var(--accent);
}
.chatliste .titel { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatliste .anzahl { font-family: var(--mono); font-size: 11px; color: var(--fg-mute); }

.liveliste li {
  padding: 6px 0;
  font-size: 12px;
  color: var(--fg-dim);
  border-bottom: 1px solid var(--line-soft);
}
.liveliste li:last-child { border-bottom: 0; }
.liveliste .lauf-titel { color: var(--fg); font-family: var(--mono); font-size: 11.5px; }
.liveliste .lauf-meta { display: block; font-size: 11px; color: var(--fg-mute); margin-top: 2px; }
.liveliste .puls {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--run); margin-right: 6px;
  animation: pulsieren 1.6s ease-in-out infinite;
}
@keyframes pulsieren { 0%,100% { opacity: 1 } 50% { opacity: .28 } }

.dokliste li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 0;
  font-size: 12px;
  border-bottom: 1px solid var(--line-soft);
}
.dokliste li:last-child { border-bottom: 0; }
.dokliste .dok-name {
  flex: 1; min-width: 0;
  font-family: var(--mono); font-size: 11.5px; color: var(--fg-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dokliste .dok-name[data-beigelegt="1"] { color: var(--accent); }

/* ============================ Verlauf ============================ */

.chat-main { position: relative; overflow: hidden; }

.chatstream {
  height: 100%;
  overflow-y: auto;
  padding: 22px 0 12px;
  scroll-behavior: smooth;
}

.leerzustand { text-align: center; padding: 60px 20px; }
.leerzustand h2 { margin: 0 0 10px; font-size: 17px; font-weight: 600; }
.leerzustand p { margin: 0; font-size: 13px; line-height: 1.7; }

.nachricht {
  width: min(760px, 100%);
  margin: 0 auto 20px;
  padding: 0 22px;
}
.nachricht .rolle {
  font-size: 10.5px;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--fg-mute);
  margin-bottom: 6px;
}
.nachricht .inhalt {
  font-size: 13.5px;
  line-height: 1.68;
  white-space: pre-wrap;
  word-break: break-word;
}
.nachricht[data-rolle="user"] .inhalt {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px 14px;
  color: var(--fg);
}
.nachricht[data-rolle="assistant"] .inhalt { color: var(--fg-dim); }
.nachricht[data-rolle="assistant"] .rolle { color: var(--accent); }

/* Kontextbloecke, die der Server an die Nachricht haengt, klein halten. */
.nachricht .kontext-fold {
  margin-top: 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
}
.nachricht .kontext-fold summary {
  cursor: pointer;
  padding: 6px 11px;
  font-size: 11.5px;
  color: var(--fg-mute);
  list-style: none;
}
.nachricht .kontext-fold summary::-webkit-details-marker { display: none; }
.nachricht .kontext-fold pre {
  margin: 0;
  padding: 10px 12px;
  border-top: 1px solid var(--line-soft);
  font-size: 11px;
  line-height: 1.5;
  white-space: pre-wrap;
  color: var(--fg-mute);
  max-height: 260px;
  overflow-y: auto;
}

/* Werkzeuge und Subagenten */
.werkzeuge { margin-top: 9px; display: flex; flex-direction: column; gap: 4px; }
.werkzeug {
  display: flex; align-items: baseline; gap: 8px;
  font-family: var(--mono); font-size: 11px;
  color: var(--fg-mute);
  padding: 3px 9px;
  background: var(--bg-1);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
}
.werkzeug .wz-name { color: var(--fg-dim); flex: none; }
.werkzeug .wz-arg { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.werkzeug[data-sub="1"] {
  border-color: var(--accent-soft);
  background: var(--accent-bg);
}
.werkzeug[data-sub="1"] .wz-name { color: var(--accent); }

.tippt { display: inline-flex; gap: 3px; padding: 4px 0; }
.tippt i {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--fg-mute);
  animation: tippen 1.3s ease-in-out infinite;
}
.tippt i:nth-child(2) { animation-delay: .18s }
.tippt i:nth-child(3) { animation-delay: .36s }
@keyframes tippen { 0%,60%,100% { opacity: .25 } 30% { opacity: 1 } }

.scroll-down { position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); }

/* ============================ Eingabe ============================ */

.composer-bereich {
  border-top: 1px solid var(--line);
  background: var(--bg-1);
  padding: 10px 22px 12px;
}

.kontextleiste {
  width: min(760px, 100%);
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  gap: 11px;
  flex-wrap: wrap;
}
.kontext-feld { display: flex; align-items: center; gap: 7px; }
.kontext-feld > span { font-size: 11px; color: var(--fg-mute); }
.kontext-feld select {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--fg-dim);
  font: inherit;
  font-size: 12px;
  padding: 4px 8px;
  max-width: 340px;
}
.kontext-feld select:focus { outline: none; border-color: var(--accent-soft); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px; font-family: var(--mono);
  padding: 3px 6px 3px 9px;
  border-radius: 999px;
  background: var(--accent-bg);
  border: 1px solid var(--accent-soft);
  color: var(--accent);
}
.chip button {
  background: none; border: 0; cursor: pointer;
  color: inherit; opacity: .7; font-size: 13px; line-height: 1; padding: 0 2px;
}
.chip button:hover { opacity: 1; }

.chat-composer {
  width: min(760px, 100%);
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 9px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 9px 8px 12px;
}
.chat-composer:focus-within { border-color: var(--accent-soft); }
.chat-composer textarea {
  flex: 1;
  background: none;
  border: 0;
  color: var(--fg);
  font: inherit;
  font-size: 13.5px;
  line-height: 1.55;
  resize: none;
  max-height: 40vh;
  padding: 3px 0;
}
.chat-composer textarea:focus { outline: none; }
.composer-knoepfe { display: flex; align-items: center; gap: 6px; flex: none; }

#btnMic[data-an="1"] {
  background: rgba(224,101,111,.16);
  border-color: rgba(224,101,111,.5);
  color: var(--err);
  animation: pulsieren 1.4s ease-in-out infinite;
}

.composer-fuss {
  width: min(760px, 100%);
  margin: 6px auto 0;
  display: flex;
  gap: 12px;
  min-height: 15px;
}

/* ============================ Dialoge ============================ */

.zeile { display: flex; align-items: center; gap: 9px; margin: 0 0 11px; flex-wrap: wrap; }

.segmente {
  list-style: none; margin: 0 0 4px; padding: 0;
  border: 1px solid var(--line); border-radius: var(--radius);
  max-height: 320px; overflow-y: auto;
}
.segmente li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 11px;
  border-bottom: 1px solid var(--line-soft);
  background: var(--bg);
}
.segmente li:last-child { border-bottom: 0; }
.segmente li:hover { background: var(--bg-2); }
.segmente input[type="checkbox"] { accent-color: var(--accent); flex: none; }
.seg-titel {
  font-family: var(--mono); font-size: 12px; color: var(--fg);
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.seg-mass { font-family: var(--mono); font-size: 11px; color: var(--fg-mute); flex: none; }

.status {
  margin: 10px 0 0; font-size: 12px;
  padding: 7px 10px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--bg); color: var(--fg-dim);
}
.status[data-art="ok"]   { border-color: rgba(76,174,127,.4);  color: var(--ok); }
.status[data-art="err"]  { border-color: rgba(224,101,111,.4); color: var(--err); }
.status[data-art="warn"] { border-color: rgba(211,163,74,.4);  color: var(--warn); }
.warn-zeile { margin: 10px 0 0; font-size: 12px; color: var(--warn); }

.dlg-fuss { display: flex; justify-content: flex-end; gap: 9px; margin-top: 16px; }

.dlg.breit { width: min(860px, 94vw); max-height: 86vh; padding: 0; }
.dlg-kopf {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 15px; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; background: var(--bg-1);
}
.dlg-kopf h2 { margin: 0; font-size: 13.5px; font-family: var(--mono); flex: 1; }
.segment-text {
  margin: 0; padding: 15px;
  font-size: 11.5px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-word;
  overflow-y: auto; max-height: calc(86vh - 50px);
  color: var(--fg-dim);
}

/* =========================== Schmal =========================== */

/* Abdunklung hinter der ausgeklappten Leiste. Im Breitband nie sichtbar. */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 30;
  opacity: 0;
  transition: opacity .16s ease;
}
.scrim[data-an="1"] { opacity: 1; }

.side-zu { display: none; }

@media (min-width: 861px) {
  #btnSidebar { display: none; }
  .scrim { display: none; }
}

@media (max-width: 860px) {
  .chatapp {
    grid-template-columns: 1fr;
    grid-template-areas: "kopf" "verlauf" "eingabe";
  }

  /* Die Leiste beginnt UNTER der 40px hohen Topbar. Sonst deckt sie den
     Hamburger-Knopf zu und laesst sich nicht mehr zuklappen - genau der
     Fehler, den diese Regel behebt. */
  .chat-side {
    position: fixed;
    /* Hoehe der Topbar wird im JS gemessen und hier eingesetzt: sie haengt an
       Schriftgroesse und Knopfhoehe und ist nicht konstant 40px. Ein fester
       Wert liess die Leiste unter die Topbar rutschen. */
    top: var(--topbar-h, 40px);
    bottom: 0;
    left: 0;
    width: min(300px, 84vw);
    z-index: 35;
    transform: translateX(-100%);
    transition: transform .18s ease;
    box-shadow: 3px 0 20px rgba(0, 0, 0, .5);
    padding-left: env(safe-area-inset-left);
    overscroll-behavior: contain;
  }
  .chat-side[data-offen="1"] { transform: none; }

  /* Topbar ueber die Leiste legen, damit der Knopf immer klickbar bleibt. */
  .chatapp .topbar { position: relative; z-index: 36; }

  .side-zu {
    display: block;
    width: calc(100% - 26px);
    margin: 11px 13px 2px;
    text-align: left;
  }

  .nachricht { padding-left: 14px; padding-right: 14px; }
  .kontextleiste, .chat-composer, .composer-fuss { width: 100%; }
  .composer-bereich {
    padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  }

  /* Auf schmalen Geraeten darf die Queue-Auswahl die Zeile fuellen. */
  .kontext-feld { width: 100%; }
  .kontext-feld select { flex: 1; max-width: none; min-width: 0; }

  /* Knoepfe nicht umbrechen lassen, das Textfeld gibt nach. */
  .chat-composer { align-items: flex-end; }
  .composer-knoepfe { flex-wrap: nowrap; }
  .chat-composer textarea { min-width: 0; }

  .dlg { width: 94vw; max-width: none; }
  .segmente { max-height: 46vh; }
}

/* style.css blendet die Wortmarke unter 1100px aus - dort frisst sie im
   Konsolen-Kopf den Platz des Projektnamens. Auf dieser Seite steht im Kopf
   aber sonst nichts, was verraet, wo man ist. Deshalb hier zurueckgeholt. */
@media (max-width: 1099.98px) {
  .chatapp .brand { display: flex; flex: none; }
}

/* Sehr schmal: Beschriftungen weichen, Symbole bleiben. */
@media (max-width: 640px) {
  .chatapp .topbar .conn { display: none; }   /* Verbindungszustand weicht dem Titel */

  /* Dateiname auf eigene Zeile, Knoepfe darunter: sonst bleibt vom Namen nur
     "AP-65-Pr…" uebrig, waehrend drei Wortknoepfe die Breite fressen. */
  .dokliste li { flex-wrap: wrap; row-gap: 4px; }
  .dokliste .dok-name { flex-basis: 100%; font-size: 12px; }
  .dokliste .btn { padding: 2px 8px; }
}

@media (max-width: 400px) {
  .brand .muted { display: none; }
  .kontext-feld > span { display: none; }
  #btnSend { padding-left: 12px; padding-right: 12px; }
}
