:root {
  --bg: #0b0d10;
  --surface: #14171b;
  --surface-hover: #1b1f24;
  --border: #23272d;
  --text: #e8e6e1;
  --text-dim: #8b8f96;
  --accent: #1b9973;
  --accent-dim: #12523d;
  --on-accent: #ffffff;
  --online: #6fcf97;
  --danger: #e07070;
  --font-ui: 'IBM Plex Sans', -apple-system, sans-serif;
  --font-mono: 'IBM Plex Mono', monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

/* Auth-Screen */
.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 32px 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.auth-logo {
  height: 26px;
  width: auto;
  margin: 0 auto 4px;
}

.auth-sub {
  margin: 0 0 8px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  font-family: var(--font-mono);
}

.auth-card input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  padding: 11px 12px;
  outline: none;
}
.auth-card input:focus { border-color: var(--accent-dim); }

.auth-submit-btn {
  background: var(--accent);
  border: none;
  color: var(--on-accent);
  font-family: var(--font-ui);
  font-size: 14.5px;
  font-weight: 500;
  padding: 11px;
  border-radius: 8px;
  cursor: pointer;
  margin-top: 4px;
}
.auth-submit-btn:disabled { opacity: 0.5; cursor: default; }

.auth-error {
  margin: 0;
  min-height: 16px;
  color: var(--danger);
  font-size: 12.5px;
  text-align: center;
}

.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 280px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.25s ease;
  z-index: 20;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(16px + env(safe-area-inset-top)) 16px 16px;
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.02em;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }

.new-chat-btn {
  margin: 12px;
  padding: 10px 14px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s;
}
.new-chat-btn:hover { border-color: var(--accent-dim); }

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
}

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 10px 8px 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text-dim);
  margin-bottom: 2px;
}
.conv-item:hover { background: var(--surface-hover); color: var(--text); }
.conv-item.active { background: var(--surface-hover); color: var(--text); border-left: 2px solid var(--accent); }

.conv-item-icon {
  flex-shrink: 0;
  font-size: 12px;
  opacity: 0.7;
}

.conv-item-title {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-delete-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}
.conv-item:hover .conv-delete-btn { opacity: 1; }
.conv-delete-btn:hover { background: rgba(217, 74, 74, 0.15); color: var(--danger); }

.conv-rename-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 12.5px;
  line-height: 1;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}
.conv-item:hover .conv-rename-btn { opacity: 1; }
.conv-rename-btn:hover { background: var(--surface-hover); color: var(--text); }

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}

.sidebar-user {
  flex: 1;
  min-width: 0;
  font-size: 12.5px;
  font-family: var(--font-mono);
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.push-active {
  color: var(--accent);
  opacity: 1 !important;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 15;
}

/* Main */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar #openSidebar { display: none; }

.status {
  display: flex;
  align-items: center;
  gap: 7px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--online);
  box-shadow: 0 0 0 0 rgba(111,207,151,0.6);
}
.status-dot.thinking {
  background: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(27,153,115,0.55); }
  70%  { box-shadow: 0 0 0 8px rgba(27,153,115,0); }
  100% { box-shadow: 0 0 0 0 rgba(27,153,115,0); }
}

.status-label {
  font-size: 13px;
  color: var(--text-dim);
  font-family: var(--font-mono);
}

.mode-badge {
  font-size: 10.5px;
  font-family: var(--font-mono);
  color: var(--accent);
  border: 1px solid var(--accent-dim);
  border-radius: 4px;
  padding: 2px 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.conv-title {
  margin-left: auto;
  font-size: 13px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
}
.empty-sub { font-size: 13px; margin-top: 4px; }

.msg {
  max-width: 640px;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--on-accent);
}

.msg.assistant {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
}

.msg.failed {
  align-self: flex-start;
  background: transparent;
  border: 1px solid #7a2d2d;
  color: #d98080;
  font-family: var(--font-mono);
  font-size: 13px;
}

.msg-time {
  display: block;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-dim);
  margin-top: 4px;
  opacity: 0.7;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.composer textarea {
  flex: 1;
  resize: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  padding: 10px 12px;
  max-height: 140px;
  outline: none;
}
.composer textarea:focus { border-color: var(--accent-dim); }

.send-btn {
  background: var(--accent);
  border: none;
  color: var(--on-accent);
  width: 38px;
  height: 38px;
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:disabled { opacity: 0.4; cursor: default; }

/* Konto-/Nutzerverwaltungs-Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 30;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 500;
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.modal-section h3 {
  margin: 0 0 12px;
  font-size: 13px;
  font-family: var(--font-mono);
  color: var(--text-dim);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.section-hint {
  margin: -6px 0 12px;
  font-size: 12.5px;
  color: var(--text-dim);
  line-height: 1.4;
}

.modal-section form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-section input[type="text"],
.modal-section input[type="password"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  padding: 10px 12px;
  outline: none;
}
.modal-section input:focus { border-color: var(--accent-dim); }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  color: var(--text-dim);
  cursor: pointer;
}

.form-msg {
  margin: 0;
  min-height: 16px;
  font-size: 12.5px;
}
.form-msg.success { color: var(--online); }
.form-msg.error { color: var(--danger); }

.user-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 14px;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg);
  font-size: 13.5px;
}
.user-row .conv-delete-btn { opacity: 1; }

/* Mobile */
@media (max-width: 640px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    width: 85vw;
    max-width: 320px;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar.open ~ .sidebar-overlay { display: block; }
  .topbar #openSidebar { display: inline-block; }
  .msg { max-width: 85%; }
}

/* iPad Portrait: schmalere Sidebar statt komplett verstecken - bleibt sichtbar,
   Chat bekommt trotzdem genug Platz */
@media (min-width: 641px) and (max-width: 1024px) {
  .sidebar {
    width: 240px;
  }
  .msg { max-width: 75%; }
}

.brand-logo {
  height: 20px;
  width: auto;
  display: block;
}

/* Foto-Upload */
.photo-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  align-self: flex-end;
}
.photo-btn:disabled { opacity: 0.4; cursor: default; }

.upload-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  margin: 0 16px 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
  color: var(--text-dim);
}
.upload-preview img {
  width: 32px;
  height: 32px;
  object-fit: cover;
  border-radius: 6px;
}
.upload-preview .upload-status {
  flex: 1;
}
.upload-preview .upload-cancel {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
}

.messages.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: -8px;
  background: var(--surface);
}
