:root {
  --bg-0: #0a1626;
  --bg-1: #0f1d33;
  --bg-2: #16263f;
  --bg-3: #1d3253;
  --border: rgba(255,255,255,0.10);
  --border-strong: rgba(255,255,255,0.22);
  --text: #e8eef7;
  --text-dim: rgba(232,238,247,0.65);
  --text-muted: rgba(232,238,247,0.45);
  --accent: #4ea3ff;
  --accent-2: #2479d8;
  --user: #2479d8;
  --assistant-bg: rgba(255,255,255,0.05);
  --danger: #ff6b6b;
  --ok: #4caf50;
  --warn: #ffb547;
  --radius: 12px;
  --shadow: 0 12px 30px rgba(0,0,0,0.35);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100vh;
  height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(135deg, #1a2540 0%, #003056 50%, #0a1626 100%);
  background-attachment: fixed;
  color: var(--text);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
body {
  display: flex;
  flex-direction: row;
  min-height: 100%;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button, select, textarea, input {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

/* ---------- Sidebar ---------- */
#sidebar {
  width: 280px;
  flex-shrink: 0;
  background: rgba(10,22,38,0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  z-index: 50;
}
.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(18px + env(safe-area-inset-top, 0px)) 18px 12px;
  border-bottom: 1px solid var(--border);
}
.brand {
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand i { color: var(--accent); }
.side-section {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.side-list-wrap { flex: 1; min-height: 0; border-bottom: none; }
.side-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-weight: 600;
}
#projectSelect {
  width: 100%;
  padding: 9px 12px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23ffffffaa'><path d='M4 6l4 4 4-4'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 14px;
  padding-right: 32px;
}
#projectSelect:focus { outline: 2px solid var(--accent); outline-offset: 0; }

.primary-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none;
  color: white;
  padding: 9px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 0.1s, filter 0.15s;
}
.primary-btn:hover:not(:disabled) { filter: brightness(1.1); }
.primary-btn:active:not(:disabled) { transform: translateY(1px); }
.primary-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.session-list {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-bottom: 8px;
}
.session-item {
  padding: 10px 12px;
  background: rgba(255,255,255,0.02);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
}
.session-menu-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s;
}
.session-item:hover .session-menu-btn,
.session-item.active .session-menu-btn { opacity: 1; }
.session-menu-btn:hover { background: rgba(255,255,255,0.1); color: var(--text); }
@media (max-width: 800px) { .session-menu-btn { opacity: 1; } }

.session-group-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin: 8px 0 4px 4px;
  font-weight: 600;
}
.session-group-label:first-child { margin-top: 0; }
.session-item:hover { background: rgba(255,255,255,0.06); border-color: var(--border-strong); }
.session-item.active { background: rgba(78,163,255,0.15); border-color: var(--accent); }
.session-item .s-title {
  font-size: 0.88rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}
.session-item .s-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.session-empty {
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 12px;
  text-align: center;
}
.side-foot {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  font-size: 0.82rem;
}
.muted-link { color: var(--text-dim); }
.muted-link:hover { color: var(--text); text-decoration: none; }

/* ---------- Main / Chat ---------- */
#main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}
.chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top, 0px)) 22px 14px;
  border-bottom: 1px solid var(--border);
  background: rgba(10,22,38,0.5);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.chat-title { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
#chatTitle {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#chatSub { font-size: 0.78rem; }
.muted { color: var(--text-muted); }

.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
  transition: background 0.2s;
}
.status-dot.active { background: var(--accent); box-shadow: 0 0 8px var(--accent); animation: pulse 1.2s infinite; }
.status-dot.error  { background: var(--danger); }
.status-dot.ok     { background: var(--ok); }
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.4; }
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
}
.empty-state i { margin-bottom: 12px; color: var(--accent); opacity: 0.6; }

.msg {
  max-width: 760px;
  width: fit-content;
  border-radius: 14px;
  padding: 11px 15px;
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
  font-size: 0.95rem;
}
.msg.user {
  align-self: flex-end;
  background: var(--user);
  color: white;
  border-bottom-right-radius: 4px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--assistant-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.assistant .tool {
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: 0.78rem;
  color: var(--text-muted);
  margin: 6px 0 0;
  padding: 6px 10px;
  background: rgba(0,0,0,0.25);
  border-radius: 6px;
  border-left: 2px solid var(--accent);
}
.msg.assistant .tool-result {
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin: 4px 0 0;
  padding: 6px 10px;
  background: rgba(0,0,0,0.15);
  border-radius: 6px;
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
}
.msg.assistant .tool-result.err { color: var(--danger); border-left: 2px solid var(--danger); }
.msg.assistant .meta-info {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 8px;
  text-align: right;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
}
.msg.assistant code {
  background: rgba(0,0,0,0.3);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: 0.9em;
}
.msg.assistant pre {
  background: rgba(0,0,0,0.35);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 0.85em;
}
.msg.assistant pre code { background: transparent; padding: 0; }
.msg.assistant ul, .msg.assistant ol { margin: 6px 0 6px 22px; padding: 0; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3, .msg.assistant h4 {
  margin: 12px 0 6px; font-weight: 600;
}
.msg.assistant h1 { font-size: 1.25em; }
.msg.assistant h2 { font-size: 1.15em; }
.msg.assistant h3 { font-size: 1.05em; }
.msg.assistant blockquote {
  border-left: 3px solid var(--accent);
  margin: 8px 0;
  padding: 2px 0 2px 10px;
  color: var(--text-dim);
}
.msg.assistant a { color: var(--accent); text-decoration: underline; }
.msg.assistant table {
  border-collapse: collapse; margin: 8px 0; font-size: 0.88em;
}
.msg.assistant th, .msg.assistant td {
  border: 1px solid var(--border); padding: 4px 8px;
}
.msg.assistant p { margin: 6px 0; }

.msg .typing {
  display: inline-block;
  width: 8px; height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  margin-left: 2px;
  animation: blink 1s infinite;
}
@keyframes blink { 0%, 50%, 100% { opacity: 1; } 25%, 75% { opacity: 0.2; } }

.msg .file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(0,0,0,0.25);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.82rem;
  margin: 4px 6px 0 0;
}

/* ---------- Composer ---------- */
.composer {
  border-top: 1px solid var(--border);
  background: rgba(10,22,38,0.55);
  backdrop-filter: blur(8px);
  padding: 12px 16px env(safe-area-inset-bottom, 12px);
}
.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 6px 8px;
  transition: border-color 0.15s;
}
.composer-row:focus-within { border-color: var(--accent); }
#input {
  flex: 1;
  resize: none;
  background: transparent;
  border: none;
  outline: none;
  padding: 8px 6px;
  min-height: 24px;
  max-height: 220px;
  line-height: 1.4;
}
.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  width: 38px; height: 38px;
  border-radius: 50%;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: rgba(255,255,255,0.08); color: var(--text); }
.file-btn { position: relative; }

.model-select {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 0.78rem;
  padding: 4px 4px;
  cursor: pointer;
  border-radius: 6px;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  flex-shrink: 0;
}
.model-select:hover { color: var(--text); background: rgba(255,255,255,0.05); }
.model-select option { background: var(--bg-2); color: var(--text); }
@media (max-width: 800px) {
  .model-select { font-size: 0.7rem; padding: 4px 2px; }
}

.mic-btn { position: relative; transition: color 0.15s, background 0.15s; }
.mic-btn.listening { color: #fff; background: var(--danger); }
#ttsBtn.on { color: var(--accent); }
#ttsBtn.speaking { color: var(--accent); animation: pulse 1.2s infinite; }
#pushBtn.on { color: var(--accent); }

/* Modal-Sheet (TTS-Settings etc.) */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal {
  background: var(--bg-1);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  max-width: 440px;
  width: 100%;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 1.05rem; font-weight: 600; }
.modal-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 10px; }
.modal-label { font-size: 0.78rem; color: var(--text-muted); margin-top: 6px; }
.modal-select {
  width: 100%;
  padding: 9px 12px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}
.modal-range { width: 100%; }
.modal-actions { display: flex; gap: 8px; margin-top: 14px; justify-content: flex-end; }
.mic-btn.listening::after {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 2px solid var(--danger);
  opacity: 0.6;
  animation: mic-ring 1.2s ease-out infinite;
}
@keyframes mic-ring {
  0%   { transform: scale(0.85); opacity: 0.7; }
  100% { transform: scale(1.6);  opacity: 0; }
}
#input.interim { color: var(--text-dim); font-style: italic; }

.send-btn {
  width: 38px; height: 38px;
  padding: 0;
  border-radius: 50%;
  flex-shrink: 0;
}
.send-btn.stop {
  background: var(--danger);
}
.send-btn.stop:hover:not(:disabled) { filter: brightness(1.1); }

.file-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  max-height: 140px;
  overflow-y: auto;
  padding: 2px;
}
.file-preview.hidden { display: none; }
.file-tag {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 4px 10px 4px 12px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
}
.file-tag .x {
  cursor: pointer;
  color: var(--text-muted);
  padding: 2px 4px;
}
.file-tag .x:hover { color: var(--danger); }
.file-tag.uploading { opacity: 0.6; }
.file-tag-img {
  padding: 4px 8px 4px 4px;
  align-items: center;
}
.file-tag-img img {
  width: 36px; height: 36px;
  object-fit: cover;
  border-radius: 6px;
  display: block;
}
.file-tag-img .file-tag-name {
  max-width: 110px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Mobile ---------- */
.mobile-only { display: none; }
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 40;
}
.overlay.show { display: block; }

@media (max-width: 800px) {
  .mobile-only { display: inline-flex; }
  #sidebar {
    position: fixed;
    left: 0; top: 0;
    height: 100vh;
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform 0.2s;
    width: min(86vw, 320px);
    box-shadow: var(--shadow);
  }
  #sidebar.open { transform: translateX(0); }
  #main { width: 100%; }
  .messages { padding: 14px; }
  .msg { max-width: 92%; font-size: 0.93rem; }
}

/* Sprache: Freisprech-Modus + Mic-Zustände */
#handsFreeBtn.on { color: #43a047; }
#handsFreeBtn.on i { animation: pulse 1.4s infinite; }
.mic-btn.processing { color: var(--accent); }
.mic-btn.processing i { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Scroll-zum-Ende-Button (schwebend) */
#main { position: relative; }
.scroll-bottom-btn {
  position: absolute;
  right: 18px;
  bottom: 92px;            /* über dem Eingabefeld */
  bottom: calc(92px + env(safe-area-inset-bottom, 0px));
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--bg-3);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  z-index: 30;
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
  opacity: 0.95;
  transition: transform 0.12s, opacity 0.15s;
}
.scroll-bottom-btn:hover { transform: translateY(-2px); background: var(--accent-2); }
.scroll-bottom-btn:active { transform: translateY(0); }
