/* =====================================================================
   Designsystem "Halbleiter" – vollständig, mobil-optimiert
   ===================================================================== */
:root {
  --bg: #0d1117; --surface: #161b22; --surface-2: #1c2430;
  --border: #2a3441; --text: #e6edf3; --muted: #8b98a8;
  --accent: #2dd4bf; --accent-ink: #04211d; --error: #f87171;
  --radius: 12px; --mono: ui-monospace, "JetBrains Mono", "Fira Code", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", sans-serif;
  /* Safe-Area-Kurzformen */
  --sa-l: env(safe-area-inset-left); --sa-r: env(safe-area-inset-right);
  --sa-t: env(safe-area-inset-top);  --sa-b: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text); font-family: var(--sans);
  overscroll-behavior-y: none;   /* kein Gummiband-Effekt der ganzen Seite */
}
[hidden] { display: none !important; }

.screen { min-height: 100dvh; }
.center { display: grid; place-items: center; padding: 24px;
  padding-left: max(24px, var(--sa-l)); padding-right: max(24px, var(--sa-r)); }
.center-text { text-align: center; }
.muted { color: var(--muted); } .small { font-size: .85rem; }

/* ---- Formularelemente: 16px verhindert iOS-Auto-Zoom ---- */
input, select, textarea { font-size: 16px; font-family: inherit; }

/* ---- Karten (Login / TOTP / Enrollment) ---- */
.card {
  width: 100%; max-width: 380px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.card h1 { margin: 0; font-size: 1.5rem; letter-spacing: -.02em; }
.dot { color: var(--accent); }
label { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; }
input[type=email], input[type=password], input[type=text],
#totp-code, #enroll-code {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 12px; color: var(--text); font-family: var(--mono);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button {
  background: var(--accent); color: var(--accent-ink); border: none;
  border-radius: 8px; padding: 12px; font-weight: 600; font-size: 1rem;
  cursor: pointer; min-height: 44px; touch-action: manipulation;
}
button:active { transform: translateY(1px); }
button.ghost { background: transparent; color: var(--muted);
  border: 1px solid var(--border); padding: 8px 12px; font-size: .9rem; min-height: 40px; }

.error { color: var(--error); font-size: .85rem; min-height: 1em; margin: 0; }
.qr { width: 200px; height: 200px; align-self: center; border-radius: 8px; background: #fff; padding: 8px; }
.secret { font-family: var(--mono); background: var(--surface-2); padding: 8px 10px;
  border-radius: 6px; text-align: center; letter-spacing: .1em; user-select: all; word-break: break-all; }
.backup { font-family: var(--mono); columns: 2; gap: 12px; list-style: none; padding: 0; }
.backup li { padding: 4px 0; }

.spinner { width: 36px; height: 36px; border: 3px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- App-Shell ---- */
#screen-app { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  flex-wrap: nowrap;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, var(--sa-t));
  padding-left: max(16px, var(--sa-l)); padding-right: max(16px, var(--sa-r));
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; flex-shrink: 0; z-index: 20;
}
.brand { font-weight: 700; } .spacer { flex: 1; }

/* ---- Chat ---- */
.chat-root { flex: 1; display: flex; flex-direction: column; padding: 0; min-height: 0; }
.chat-toolbar {
  display: flex; gap: 8px; padding: 10px 16px;
  padding-left: max(16px, var(--sa-l)); padding-right: max(16px, var(--sa-r));
  background: var(--surface); border-bottom: 1px solid var(--border);
  flex-wrap: wrap; flex-shrink: 0;
}
.select {
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-family: var(--mono);
  min-width: 0;   /* erlaubt Schrumpfen statt Überlaufen */
}
#chat-model { flex: 1 1 auto; }
#chat-web.active { color: #5b8def; opacity: 1; }

.messages {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  padding-left: max(16px, var(--sa-l)); padding-right: max(16px, var(--sa-r));
  display: flex; flex-direction: column; gap: 14px;
}
.msg { display: flex; flex-direction: column; gap: 6px; max-width: 780px; width: 100%; align-self: center; }
.bubble { padding: 12px 14px; border-radius: var(--radius); }
.msg-user .bubble { background: var(--surface-2); border: 1px solid var(--border);
  align-self: flex-end; max-width: 85%; }
.bubble-content { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.55; }
.bubble-content p { margin: .5em 0; }
.bubble-content pre { position: relative; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.bubble-content code { font-family: var(--mono); font-size: .88rem; }
.bubble-content :not(pre) > code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.copy-btn { position: absolute; top: 6px; right: 6px; background: var(--surface); color: var(--muted);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 10px; font-size: .72rem;
  cursor: pointer; min-height: 0; touch-action: manipulation; }

.think details { background: var(--surface); border: 1px dashed var(--border);
  border-radius: 8px; padding: 4px 10px; }
.think summary { cursor: pointer; color: var(--muted); font-size: .85rem; }
.think-content { white-space: pre-wrap; color: var(--muted); margin-top: 6px;
  font-family: var(--mono); font-size: .8rem; }

.composer {
  display: flex; gap: 8px; padding: 12px 16px;
  padding-bottom: max(12px, var(--sa-b));
  padding-left: max(16px, var(--sa-l)); padding-right: max(16px, var(--sa-r));
  background: var(--surface); border-top: 1px solid var(--border); flex-shrink: 0;
}
.composer textarea {
  flex: 1; resize: none; max-height: 180px; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px; line-height: 1.4;
}
.composer button { padding: 0 18px; flex-shrink: 0; }
.composer button.stop { background: var(--error); color: #2a0a0a; }

/* ---- Schmale Displays ---- */
@media (max-width: 600px) {
  .center { padding: 16px; padding-bottom: max(16px, var(--sa-b)); }
  .card { padding: 22px; }
  .messages { padding: 12px; gap: 12px; }
  .chat-toolbar { padding: 8px 12px; }
  #chat-model { flex-basis: 100%; }      /* Modellname bekommt eigene Zeile */
  .msg-user .bubble { max-width: 92%; }
  .composer { padding: 10px 12px; padding-bottom: max(10px, var(--sa-b)); }
}

/* ---- Bewegung reduzieren (Systemeinstellung) ---- */
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2s; }
  button:active { transform: none; }
}
/* ---- Vision-Anhänge ---- */
.icon-btn { background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; min-width: 44px; padding: 0; font-size: 1.2rem; flex-shrink: 0; }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 16px 8px;
  padding-left: max(16px, var(--sa-l)); padding-right: max(16px, var(--sa-r)); }
.attach-chip { position: relative; width: 64px; height: 64px; border-radius: 8px;
  overflow: hidden; border: 1px solid var(--border); }
.attach-chip img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attach-remove { position: absolute; top: 2px; right: 2px; width: 20px; height: 20px;
  min-height: 0; padding: 0; border-radius: 50%; background: rgba(0,0,0,.6); color: #fff;
  font-size: 14px; line-height: 1; border: none; }
.bubble-images { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.bubble-images img { max-width: 220px; max-height: 220px; border-radius: 8px; display: block; }
/* ---- Seitenleiste ---- */
.icon-only { padding: 8px 10px; font-size: 1.1rem; }
.drawer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; }
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: 82%; max-width: 320px;
  background: var(--surface); border-right: 1px solid var(--border);
  transform: translateX(-100%); transition: transform .22s ease; z-index: 21;
  display: flex; flex-direction: column;
  padding-top: max(0px, var(--sa-t)); padding-left: max(0px, var(--sa-l));
}
.drawer.open { transform: translateX(0); }
.drawer-head { display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border); font-weight: 600; }
.drawer-list { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 8px; }
.chat-item { display: flex; align-items: center; border-radius: 8px; }
.chat-item:hover, .chat-item.active { background: var(--surface-2); }
.chat-item.active { outline: 1px solid var(--border); }
.chat-item-title { flex: 1; min-width: 0; text-align: left; background: none; border: none;
  color: var(--text); padding: 10px 12px; font-size: .9rem; min-height: 40px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-actions { display: flex; gap: 2px; padding-right: 6px; }
.chat-item-actions button { background: none; border: none; color: var(--muted);
  font-size: .95rem; padding: 6px; min-height: 32px; cursor: pointer; }
.chat-item-actions button:hover { color: var(--text); }
@media (prefers-reduced-motion: reduce) { .drawer { transition: none; } }
/* ---- Suche ---- */
.drawer-search { padding: 8px; border-bottom: 1px solid var(--border); }
.drawer-search input { width: 100%; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; }
.search-result { display: block; width: 100%; text-align: left; background: none;
  border: none; border-radius: 8px; padding: 10px 12px; cursor: pointer; color: var(--text); min-height: 0; }
.search-result:hover { background: var(--surface-2); }
.search-result-title { font-size: .85rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-result-snippet { font-size: .8rem; color: var(--muted); margin-top: 2px;
  display: -webkit-box; -webkit-box-orient: vertical;
  -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; }
.search-result-snippet mark { background: var(--accent); color: var(--accent-ink);
  border-radius: 3px; padding: 0 2px; }
/* angesprungene Nachricht kurz hervorheben */
.msg.flash .bubble { animation: flash 1.5s ease; }
@keyframes flash { 0% { box-shadow: 0 0 0 2px var(--accent); } 100% { box-shadow: 0 0 0 2px transparent; } }
/* ---- Export-Menü ---- */
.export-wrap { position: relative; display: flex; align-items: center; flex-shrink: 0; }
.menu { position: absolute; right: 0; top: 100%; margin-top: 6px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 4px; z-index: 22;
  display: flex; flex-direction: column; min-width: 140px; }
.menu button { background: none; border: none; color: var(--text); text-align: left;
  padding: 9px 12px; border-radius: 6px; font-size: .9rem; min-height: 40px; cursor: pointer; }
.menu button:hover { background: var(--surface-2); }
#app-user {
  visibility: hidden;
  width: 0;
  height: 0;
}

/* ---- Druck / PDF ---- */
@media print {
  .topbar, .chat-toolbar, .composer, .composer-wrap, .drawer, .drawer-overlay,
  #export-menu, .copy-btn, .think { display: none !important; }
  #screen-app, .chat-root, .messages {
    display: block !important; height: auto !important; overflow: visible !important; }
  body { background: #fff; }
  .messages { padding: 0; }
  .msg { max-width: 100%; margin-bottom: 16px; page-break-inside: avoid; }
  .bubble { background: #fff !important; border: none !important; }
  .msg-user .bubble { background: #f3f4f6 !important; }
  .msg .bubble::before { display: block; font-weight: 700; font-size: .72rem;
    text-transform: uppercase; letter-spacing: .05em; color: #555; margin-bottom: 4px; }
  .msg-user .bubble::before { content: "Du"; }
  .msg-assistant .bubble::before { content: "Assistent"; }
  .bubble-content, .bubble-content * { color: #000 !important; }
  .bubble-content pre { background: #f6f8fa !important; border: 1px solid #ddd !important; }
}
/* ---- Web-Tools ---- */
.toggle.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.tool-status { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.tool-status .chip { font-size: .8rem; color: var(--muted); background: var(--surface);
  border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; align-self: flex-start;
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  /* ---- Kontingent-Anzeige ---- */
#usage { font-variant-numeric: tabular-nums; white-space: nowrap; }
#usage.over { color: var(--error); font-weight: 600; }
/* ---- Light-Theme ---- */
:root[data-theme="light"] {
  --bg: #f7f8fa; --surface: #ffffff; --surface-2: #eef1f5;
  --border: #d4d9e0; --text: #1a1f27; --muted: #5b6673;
  --accent: #0d9488; --accent-ink: #ffffff; --error: #dc2626;
}
/* Code-Blöcke bleiben auch im Light-Modus dunkel, damit das
   github-dark-Highlighting lesbar bleibt. */
:root[data-theme="light"] .bubble-content pre {
  background: #0d1117; border-color: #2a3441;
}
:root[data-theme="light"] .bubble-content pre code { color: #e6edf3; }

/* Auf schmalen Displays die E-Mail ausblenden, damit die Topbar-Knöpfe passen */
@media (max-width: 600px) { #app-user { display: none; } }
/* ---- Modell-Knopf ---- */
.model-wrap { position: relative; display: inline-flex; min-width: 0; }
.model-btn { display: inline-flex; align-items: center; gap: 6px; max-width: 220px; padding: 6px 10px; border-radius: 8px; }
.model-btn-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; font-weight: 600; }
.model-btn-caret { flex-shrink: 0; opacity: .55; font-size: .8rem; }
#chat-model { display: none; }   /* versteckter Wertspeicher */

/* ---- Denk-Knopf ---- */
.think-wrap { position: relative; display: inline-flex; }
.think-btn { font-size: 1.05rem; opacity: .55; }
.think-btn.active { opacity: 1; color: #5b8def; }
#think-menu { min-width: 130px; }
#think-menu button.sel { color: #5b8def; font-weight: 600; }

/* native Select-Pfeile abschalten (falls irgendwo noch ein <select> sichtbar ist) */
.select { -webkit-appearance: none; appearance: none; }

/* ---- Modell-Modal ---- */
.modal-overlay { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0,0,0,.5); }
.modal-overlay[hidden] { display: none; }
.modal-card { display: flex; flex-direction: column; width: 100%; max-width: 460px; max-height: min(70vh, 560px); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; flex: 1; font-size: 1rem; }
.modal-search { margin: 10px 14px 4px; padding: 8px 10px; background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; }
.model-list { overflow-y: auto; padding: 6px; }
.model-empty { padding: 16px; text-align: center; font-size: .85rem; }
.model-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px 12px; border: 1px solid transparent; border-radius: 10px; background: none; color: var(--text); text-align: left; cursor: pointer; }
.model-item:hover { background: var(--surface-2); }
.model-item.sel { border-color: #5b8def; }
.model-item-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }

/* ---- Fähigkeits-Tags ---- */
.cap-tags { display: flex; flex-wrap: wrap; gap: 5px; flex-shrink: 0; justify-content: flex-end; }
.cap { display: inline-flex; align-items: center; gap: 3px; padding: 3px 7px; font-size: .68rem; line-height: 1; white-space: nowrap; border: 1px solid var(--border); border-radius: 999px; color: var(--muted); }
.cap-tools  { color: #c98a1e; border-color: #c98a1e55; }
.cap-vision { color: #2f8fd0; border-color: #2f8fd055; }
.cap-think  { color: #9a63d6; border-color: #9a63d655; }
.cap-audio  { color: #37a97d; border-color: #37a97d55; }
/* Modell-Name dezent oben links im Chatfenster */
#chat-root { position: relative; }
.model-indicator {
  position: absolute; top: 8px; left: 12px; z-index: 5;
  font-size: .75rem; opacity: .75; color: var(--muted);
  pointer-events: none; max-width: 60%; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}

/* Modal-Tags: eigene Zeile unter dem Namen, nur Symbole */
.model-item { flex-direction: column; align-items: flex-start; gap: 6px; }
.model-item-name { width: 100%; }
.cap-tags { justify-content: flex-start; }
.cap { padding: 4px; }                 /* icon-only, kein Text mehr */
.cap svg { width: 15px; height: 15px; display: block; }

/* Code-Snippet-Aktionen: Kopieren + Download */
.bubble-content pre { position: relative; }
.code-actions { position: absolute; top: 6px; right: 6px; display: flex; gap: 6px; }
.code-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; padding: 0; min-height: 0;
  background: var(--surface); color: var(--muted);
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
}
.code-btn:hover { color: var(--text); }
.code-btn.ok { color: #37a97d; border-color: #37a97d66; }
.code-btn svg { width: 15px; height: 15px; }
/* ---- Composer: Werkzeugleiste unter der Eingabe ---- */
.composer { flex-direction: column; gap: 8px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer-row textarea { flex: 1; }
.composer-tools { display: flex; align-items: center; gap: 4px; }

.send-btn, .stop-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; padding: 0; min-height: 0; flex-shrink: 0; border-radius: 10px;
}
.stop-btn { background: var(--error); color: #2a0a0a; }

/* Popover nach oben aufklappen */
.menu-up { bottom: calc(100% + 6px); top: auto; }

/* ---- Drawer: Logout unten ---- */
.drawer { display: flex; flex-direction: column; }
.drawer-list { flex: 1; overflow-y: auto; }
.drawer-foot { border-top: 1px solid var(--border); padding: 10px; padding-bottom: max(10px, var(--sa-b)); }
.drawer-logout { display: flex; align-items: center; gap: 10px; width: 100%; justify-content: flex-start; }

/* ---- Seitenmenü: auf Mobilgeräten Vollbild ---- */
@media (max-width: 600px) {
  .drawer { width: 100%; max-width: none; }
}

/* ---- Tabellen lesbar machen ---- */
.bubble-content table {
  width: 100%; border-collapse: collapse; margin: 10px 0; font-size: .88rem;
  display: block; overflow-x: auto; white-space: nowrap;
}
.bubble-content th, .bubble-content td {
  border: 1px solid var(--border); padding: 7px 10px; text-align: left;
}
.bubble-content th { background: var(--surface-2); font-weight: 600; }
.bubble-content tbody tr:nth-child(even) { background: rgba(127,127,127,.06); }

@media (max-width: 600px) {
  /* Kartenlayout: jede Zeile wird ein Block, Spaltennamen als Beschriftung */
  .bubble-content table { display: block; white-space: normal; }
  .bubble-content thead { display: none; }
  .bubble-content tbody, .bubble-content tr, .bubble-content td { display: block; width: 100%; }
  .bubble-content tr {
    border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; overflow: hidden;
  }
  .bubble-content td { border: none; border-bottom: 1px solid var(--border); }
  .bubble-content tr td:last-child { border-bottom: none; }
  .bubble-content td::before {
    content: attr(data-label); display: block;
    font-size: .72rem; color: var(--muted); margin-bottom: 2px;
  }
}
.drawer-search { padding: 8px 10px; border-bottom: 1px solid var(--border); }
.drawer-search input {
  width: 100%; padding: 8px 10px; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
}