/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #252836;
  --border: #2e3147;
  --accent: #6c63ff;
  --accent-hover: #7c74ff;
  --text: #e8eaf0;
  --text-muted: #8b8fa8;
  --user-bubble: #2d3248;
  --bot-bubble: #1e2130;
  --error: #ff5c6a;
  --success: #4caf82;
  --radius: 12px;
  --radius-sm: 8px;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

/* ── Auth overlay ── */
.overlay {
  position: fixed; inset: 0;
  background: linear-gradient(135deg, #0a0c14 0%, #141726 100%);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}

.auth-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 2.5rem;
  width: min(420px, 92vw);
  text-align: center;
}

.auth-box .logo { font-size: 3rem; margin-bottom: 0.5rem; }
.auth-box h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 1.5rem; }
.auth-box h1 span { color: var(--accent); }
.auth-box h2 { font-size: 1.1rem; font-weight: 600; margin-bottom: 1.2rem; }

.auth-box input {
  width: 100%; padding: 0.75rem 1rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
  outline: none;
  transition: border-color 0.2s;
}
.auth-box input:focus { border-color: var(--accent); }

.auth-box button {
  width: 100%; padding: 0.8rem;
  background: var(--accent);
  border: none; border-radius: var(--radius-sm);
  color: #fff; font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: background 0.2s;
  margin-top: 0.25rem;
}
.auth-box button:hover { background: var(--accent-hover); }

.switch-link { margin-top: 1rem; color: var(--text-muted); font-size: 0.88rem; }
.switch-link a { color: var(--accent); text-decoration: none; }
.switch-link a:hover { text-decoration: underline; }

.error-msg { color: var(--error); font-size: 0.85rem; margin-top: 0.6rem; min-height: 1.2em; }

/* ── App layout ── */
#app { display: flex; height: 100vh; }

/* ── Sidebar ── */
.sidebar {
  width: 260px; flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}

.sidebar-header {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}
.logo-small { font-size: 1.4rem; }
.brand { font-weight: 700; flex: 1; font-size: 0.95rem; }

.new-chat-btn {
  background: none; border: none;
  color: var(--text-muted); font-size: 1.1rem; cursor: pointer;
  padding: 0.25rem; border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}
.new-chat-btn:hover { background: var(--surface2); color: var(--text); }

.session-list {
  flex: 1; overflow-y: auto; padding: 0.5rem;
}

.sidebar-footer {
  padding: 1rem;
  border-top: 1px solid var(--border);
}

.usage-bar-wrap { margin-bottom: 0.75rem; }
.usage-label { display: flex; justify-content: space-between; font-size: 0.75rem; color: var(--text-muted); margin-bottom: 0.35rem; }
.usage-bar { height: 6px; background: var(--surface2); border-radius: 3px; overflow: hidden; }
.usage-fill { height: 100%; background: var(--accent); border-radius: 3px; transition: width 0.4s; }
.usage-numbers { font-size: 0.73rem; color: var(--text-muted); margin-top: 0.3rem; }

.sidebar-actions {
  display: flex; gap: 0.4rem; flex-direction: column;
}

.logout-btn, .pw-change-btn {
  width: 100%; padding: 0.6rem;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: 0.85rem; cursor: pointer;
  transition: background 0.15s, color 0.15s;
  text-align: left;
}
.logout-btn:hover, .pw-change-btn:hover { background: var(--border); color: var(--text); }

.session-item {
  display: flex; align-items: center; gap: 0.25rem;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius-sm);
  cursor: pointer; font-size: 0.85rem;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}
.session-item:hover { background: var(--surface2); color: var(--text); }
.session-item.active { background: var(--surface2); color: var(--text); }

.session-label {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  cursor: pointer;
}

.session-delete-btn {
  background: none; border: none; cursor: pointer;
  color: var(--text-muted); font-size: 0.9rem; padding: 0 0.2rem;
  opacity: 0; transition: opacity 0.15s, color 0.15s;
  flex-shrink: 0;
}
.session-item:hover .session-delete-btn { opacity: 1; }
.session-delete-btn:hover { color: var(--error); }

.hint-text { font-size: 0.8rem; color: var(--text-muted); margin: 0.25rem 0 0.5rem; }

.secondary-btn {
  margin-top: 0.5rem !important;
  background: var(--surface2) !important;
  border: 1px solid var(--border) !important;
  color: var(--text-muted) !important;
}
.secondary-btn:hover { background: var(--border) !important; }

.success-msg { color: var(--success); font-size: 0.85rem; margin-top: 0.6rem; min-height: 1.2em; }

/* ── Chat area ── */
.chat-area {
  flex: 1; display: flex; flex-direction: column; min-width: 0;
}

.chat-header {
  padding: 0.85rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface);
}
.chat-header h2 { font-size: 1rem; font-weight: 600; }
.header-user { font-size: 0.8rem; color: var(--text-muted); }

/* ── Messages ── */
.messages {
  flex: 1; overflow-y: auto;
  padding: 1.5rem;
  display: flex; flex-direction: column; gap: 1.2rem;
}

.welcome-msg {
  text-align: center; margin: auto;
  max-width: 480px;
  color: var(--text-muted);
}
.welcome-icon { font-size: 3rem; margin-bottom: 1rem; }
.welcome-msg h3 { font-size: 1.1rem; color: var(--text); margin-bottom: 0.5rem; }
.welcome-msg p { font-size: 0.9rem; line-height: 1.6; }

.msg { display: flex; gap: 0.75rem; max-width: 800px; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.assistant { align-self: flex-start; }

.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem; flex-shrink: 0;
}
.msg.user .avatar { background: var(--accent); }
.msg.assistant .avatar { background: var(--success); }

.bubble {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  line-height: 1.65;
  font-size: 0.92rem;
  max-width: calc(100% - 48px);
}
.msg.user .bubble { background: var(--user-bubble); border-radius: var(--radius) 4px var(--radius) var(--radius); }
.msg.assistant .bubble { background: var(--bot-bubble); border-radius: 4px var(--radius) var(--radius) var(--radius); }

/* Markdown rendering in bubbles */
.bubble p { margin-bottom: 0.6rem; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 0.8rem 0 0.4rem; font-size: 1rem; }
.bubble ul, .bubble ol { padding-left: 1.4rem; margin-bottom: 0.6rem; }
.bubble li { margin-bottom: 0.25rem; }
.bubble code { background: var(--surface2); padding: 0.1em 0.35em; border-radius: 4px; font-size: 0.88em; }
.bubble pre { background: var(--surface2); padding: 0.75rem; border-radius: 8px; overflow-x: auto; margin-bottom: 0.6rem; }
.bubble pre code { background: none; padding: 0; }
.bubble strong { color: #fff; }
.bubble blockquote { border-left: 3px solid var(--accent); padding-left: 0.8rem; color: var(--text-muted); margin: 0.5rem 0; }

.typing-indicator { display: flex; gap: 4px; padding: 0.5rem 0; }
.typing-dot { width: 8px; height: 8px; background: var(--text-muted); border-radius: 50%; animation: bounce 1.2s infinite; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-8px); } }

/* ── Input area ── */
.input-area {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.file-preview {
  display: flex; flex-wrap: wrap; gap: 0.5rem;
  margin-bottom: 0.6rem;
}
.file-tag {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.3rem 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex; align-items: center; gap: 0.4rem;
}
.file-tag button { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 1rem; line-height: 1; padding: 0; }

.input-row {
  display: flex; align-items: flex-end; gap: 0.5rem;
}

.upload-btn {
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer; font-size: 1.2rem;
  flex-shrink: 0; transition: background 0.15s;
}
.upload-btn:hover { background: var(--border); }

textarea#message-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit; font-size: 0.95rem;
  padding: 0.65rem 0.9rem;
  resize: none; outline: none;
  max-height: 160px; overflow-y: auto;
  transition: border-color 0.2s;
  line-height: 1.5;
}
textarea#message-input:focus { border-color: var(--accent); }
textarea#message-input::placeholder { color: var(--text-muted); }

.send-btn {
  width: 40px; height: 40px;
  background: var(--accent);
  border: none; border-radius: var(--radius-sm);
  color: #fff; font-size: 1rem; cursor: pointer;
  flex-shrink: 0; transition: background 0.2s, opacity 0.2s;
  display: flex; align-items: center; justify-content: center;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.abort-btn {
  width: 40px; height: 40px;
  background: #ef4444;
  border: none; border-radius: var(--radius-sm);
  color: #fff; font-size: 1rem; cursor: pointer;
  flex-shrink: 0; transition: background 0.2s;
  display: flex; align-items: center; justify-content: center;
}
.abort-btn:hover { background: #dc2626; }

.stream-aborted {
  margin-top: 0.5rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
}

.disclaimer {
  font-size: 0.72rem; color: var(--text-muted);
  text-align: center; margin-top: 0.5rem;
}

/* ── Source badges ── */
.sources { margin-top: 0.6rem; display: flex; flex-wrap: wrap; gap: 0.4rem; }
.source-badge {
  display: inline-block;
  font-size: 0.75rem;
  padding: 0.15em 0.6em;
  border-radius: 12px;
  background: rgba(99,102,241,0.18);
  color: #a5b4fc;
  border: 1px solid rgba(99,102,241,0.35);
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.source-badge:hover {
  background: rgba(99,102,241,0.35);
  border-color: rgba(99,102,241,0.65);
}

/* ── Knowledge modal ── */
.km-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.65);
  z-index: 200;
  align-items: center; justify-content: center;
}
.km-backdrop.open { display: flex; }

.km-dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(820px, 94vw);
  max-height: 88vh;
  display: flex; flex-direction: column;
  box-shadow: 0 24px 64px rgba(0,0,0,0.5);
}

.km-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.85rem 1.2rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.km-title {
  font-size: 0.88rem; font-weight: 600;
  color: #a5b4fc;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.km-close {
  background: none; border: none;
  color: var(--text-muted); font-size: 1rem;
  cursor: pointer; padding: 0.2rem 0.4rem;
  border-radius: 4px; line-height: 1;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
.km-close:hover { background: var(--surface2); color: var(--text); }

.km-body {
  padding: 1.2rem 1.5rem;
  overflow-y: auto;
  flex: 1;
  font-size: 0.9rem;
  line-height: 1.7;
  color: var(--text);
}
.km-body p { margin-bottom: 0.6rem; }
.km-body p:last-child { margin-bottom: 0; }
.km-body h1, .km-body h2, .km-body h3 { margin: 0.9rem 0 0.4rem; }
.km-body ul, .km-body ol { padding-left: 1.4rem; margin-bottom: 0.6rem; }
.km-body li { margin-bottom: 0.25rem; }
.km-body code { background: var(--surface2); padding: 0.1em 0.35em; border-radius: 4px; font-size: 0.88em; }
.km-body pre { background: var(--surface2); padding: 0.75rem; border-radius: 8px; overflow-x: auto; margin-bottom: 0.6rem; }
.km-body pre code { background: none; padding: 0; }
.km-body strong { color: #fff; }
.km-body blockquote { border-left: 3px solid var(--accent); padding-left: 0.8rem; color: var(--text-muted); margin: 0.5rem 0; }
.km-loading { color: var(--text-muted); font-style: italic; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Hamburger (desktop: unsichtbar) ── */
#hamburger-btn {
  display: none;
}

/* ── Sidebar-Username (desktop: unsichtbar) ── */
.sidebar-user-name {
  display: none;
}

/* ── Sidebar backdrop ── */
#sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 199;
}
#sidebar-backdrop.visible { display: block; }

/* ── Responsive ── */
@media (max-width: 640px) {
  /* Off-canvas Drawer */
  .sidebar {
    position: fixed;
    left: -280px;
    top: 0; bottom: 0;
    width: 280px;
    z-index: 200;
    transition: left 0.25s ease;
    /* box-shadow erscheint nur wenn geöffnet */
  }
  .sidebar.open {
    left: 0;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.6);
  }

  /* Chat-Bereich füllt volle Breite */
  .chat-area {
    width: 100%;
    min-width: 0;
  }

  /* Hamburger-Button einblenden */
  #hamburger-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    background: none;
    border: none;
    color: var(--text);
    font-size: 1.3rem;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background 0.15s;
    margin-right: 0.25rem;
  }
  #hamburger-btn:hover { background: var(--surface2); }

  /* Username aus Header entfernen, im Menü anzeigen */
  #header-user { display: none; }
  .sidebar-user-name {
    display: block;
    font-size: 0.8rem;
    color: var(--text-muted);
    padding: 0 0 0.6rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.6rem;
  }

  /* Header kompakter, Titel einzeilig mit Ellipsis */
  .chat-header {
    padding: 0.45rem 0.6rem;
    min-height: 0;
  }
  .chat-header h2 {
    font-size: 0.78rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
  }

  /* Hamburger kleiner */
  #hamburger-btn {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }

  /* Buttons kompakter – touch-tauglich aber nicht riesig */
  .upload-btn,
  .send-btn,
  .abort-btn {
    width: 40px;
    height: 40px;
    font-size: 0.9rem;
  }

  /* Input-Area kompakter */
  .input-area {
    padding: 0.5rem 0.6rem;
  }

  textarea#message-input {
    padding: 0.45rem 0.65rem;
  }

  /* Nachrichten-Padding reduzieren */
  .messages {
    padding: 1rem 0.75rem;
    gap: 0.75rem;
  }

  /* Avatar ausblenden – Absender als Label oberhalb der Bubble */
  .avatar { display: none; }

  /* Alle Nachrichten linksbündig, eingerückt */
  .msg,
  .msg.user {
    flex-direction: column;
    align-self: unset;
    max-width: 100%;
    gap: 0.2rem;
    align-items: flex-start;
  }

  /* Absender-Label: linksbündig, mit gleichem Einzug wie die Bubble */
  .msg.assistant::before {
    content: "🏢 Beerbaum Coach";
    font-size: 0.72rem;
    color: var(--text-muted);
    padding-left: 0.25rem;
  }
  .msg.user::before {
    content: "👤 Du";
    font-size: 0.72rem;
    color: var(--text-muted);
    padding-left: calc(16px + 0.25rem);
  }

  /* Beide Bubbles mit 16px Einzug links, rechts etwas Luft */
  .msg.assistant .bubble {
    margin-left: 0;
    margin-right: 16px;
    border-radius: 4px var(--radius) var(--radius) var(--radius);
  }
  .msg.user .bubble {
    margin-left: 16px;
    margin-right: 0;
    border-radius: var(--radius) var(--radius) var(--radius) 4px;
  }

  /* Avatar ist weg → max-width korrigieren, 16px für den Einzug abziehen */
  .bubble {
    max-width: calc(100% - 16px);
    font-size: 0.62rem;
  }
  .bubble p, .bubble li, .bubble code,
  .bubble h1, .bubble h2, .bubble h3,
  .bubble blockquote {
    font-size: inherit;
  }

  /* Input-Row: Textarea oben, Buttons darunter */
  .input-row {
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  textarea#message-input {
    order: 1;
    flex: 1 1 100%;
    font-size: 16px;
  }
  .upload-btn {
    order: 2;
    flex: 0 0 auto;
  }
  .send-btn,
  .abort-btn {
    order: 2;
    margin-left: auto;
  }

  /* Source-Badges 33% kleiner */
  .source-badge {
    font-size: 0.50rem;
    padding: 0.1em 0.4em;
  }

  /* Disclaimer kleiner */
  .disclaimer { font-size: 0.68rem; }

  /* Knowledge-Modal: Vollbild auf Mobile */
  .km-dialog {
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    border: none;
  }

  /* Passwort-Modal: Auth-Box volle Breite */
  .auth-box {
    border-radius: 0;
    width: 100%;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}
