:root {
  --agent-bg: #1a1b1e;
  --agent-surface: #232428;
  --agent-surface-2: #2c2d32;
  --agent-border: #35363c;
  --agent-text: #e9e9ee;
  --agent-text-dim: #9a9ba3;
  --agent-accent: #6c5ce7;
  --agent-accent-hover: #7d70eb;
  --agent-error: #ff6b6b;
  --agent-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--agent-bg);
  color: var(--agent-text);
  font-family: var(--agent-font);
  overscroll-behavior: none;
}

#app {
  position: fixed;
  inset: 0;
  height: 100dvh;
  width: 100vw;
  display: flex;
  flex-direction: column;
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}

.screen.hidden {
  display: none;
}

/* Login screen */
#login-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: calc(24px + env(safe-area-inset-top));
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

.login-form {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-title {
  margin: 0;
  text-align: center;
  font-size: 24px;
  font-weight: 700;
}

.login-hint {
  margin: 0 0 4px;
  text-align: center;
  color: var(--agent-text-dim);
  font-size: 14px;
}

.login-form input[type="password"] {
  background: var(--agent-surface);
  border: 1px solid var(--agent-border);
  color: var(--agent-text);
  padding: 14px 16px;
  border-radius: 12px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.15s ease;
}

.login-form input[type="password"]:focus {
  border-color: var(--agent-accent);
}

.remember-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--agent-text-dim);
  cursor: pointer;
  user-select: none;
}

.remember-label input[type="checkbox"] {
  accent-color: var(--agent-accent);
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.primary-btn {
  background: var(--agent-accent);
  color: #fff;
  border: none;
  padding: 14px 16px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}

.primary-btn:hover {
  background: var(--agent-accent-hover);
}

.primary-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.login-error {
  margin: 0;
  color: var(--agent-error);
  font-size: 13px;
  text-align: center;
}

.login-error.hidden {
  display: none;
}

/* Chat screen: sidebar + main, side by side */
#chat-screen {
  height: 100%;
  flex-direction: row;
  position: relative;
  overflow: hidden;
}

.sidebar {
  width: 270px;
  flex-shrink: 0;
  background: var(--agent-surface);
  border-right: 1px solid var(--agent-border);
  display: flex;
  flex-direction: column;
  padding: 12px;
  padding-top: calc(12px + env(safe-area-inset-top));
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  gap: 10px;
  z-index: 20;
}

.sidebar-new-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--agent-accent);
  color: #fff;
  border: none;
  padding: 11px 14px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease;
}

.sidebar-new-btn:hover {
  background: var(--agent-accent-hover);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-list-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--agent-text-dim);
  font-size: 14px;
}

.chat-list-item:hover {
  background: var(--agent-surface-2);
  color: var(--agent-text);
}

.chat-list-item.active {
  background: var(--agent-surface-2);
  color: var(--agent-text);
}

.chat-list-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-list-item-delete {
  background: transparent;
  border: none;
  color: var(--agent-text-dim);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  flex-shrink: 0;
}

.chat-list-item:hover .chat-list-item-delete {
  display: flex;
}

.chat-list-item-delete:hover {
  background: var(--agent-border);
  color: var(--agent-error);
}

.sidebar-logout-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  color: var(--agent-text-dim);
  border: 1px solid var(--agent-border);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease;
}

.sidebar-logout-btn:hover {
  background: var(--agent-surface-2);
  color: var(--agent-text);
}

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

.sidebar-overlay.hidden {
  display: none;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

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

.chat-title {
  font-weight: 600;
  font-size: 16px;
}

.model-select {
  margin-left: auto;
  background: var(--agent-surface-2);
  color: var(--agent-text);
  border: 1px solid var(--agent-border);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  outline: none;
  cursor: pointer;
  max-width: 55%;
}

.sidebar-toggle-btn {
  display: none;
}

.scope-select-wrapper {
  position: relative;
}

.scope-select-btn {
  margin-left: 0;
  white-space: nowrap;
  max-width: none;
}

.scope-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--agent-surface-2);
  border: 1px solid var(--agent-border);
  border-radius: 10px;
  padding: 8px;
  min-width: 200px;
  max-height: 260px;
  overflow-y: auto;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 2px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.scope-panel.hidden {
  display: none;
}

.scope-option {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
}

.scope-option:hover {
  background: var(--agent-surface);
}

.scope-suboption {
  padding-left: 28px;
  color: var(--agent-text-dim);
}

.scope-option input[type="checkbox"] {
  accent-color: var(--agent-accent);
  width: 15px;
  height: 15px;
  cursor: pointer;
  flex-shrink: 0;
}

.scope-folder-list {
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--agent-border);
  margin-top: 4px;
  padding-top: 4px;
}

.scope-empty-hint {
  padding: 8px;
  font-size: 12px;
  color: var(--agent-text-dim);
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--agent-text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  transition: background 0.15s ease, color 0.15s ease;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--agent-surface-2);
  color: var(--agent-text);
}

/* Mobile: sidebar becomes an off-canvas drawer */
@media (max-width: 899px) {
  .sidebar-toggle-btn {
    display: flex;
  }

  .sidebar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.3);
  }

  .sidebar.open {
    transform: translateX(0);
  }
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-message {
  max-width: 80%;
  padding: 11px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.chat-message.user {
  align-self: flex-end;
  background: var(--agent-accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-message.agent {
  align-self: flex-start;
  background: var(--agent-surface);
  color: var(--agent-text);
  border-bottom-left-radius: 4px;
}

.chat-message.pending {
  color: var(--agent-text-dim);
  font-style: italic;
}

/* Markdown/HTML-Inhalte innerhalb einer Chat-Bubble */
.chat-message :first-child {
  margin-top: 0;
}

.chat-message :last-child {
  margin-bottom: 0;
}

.chat-message p {
  margin: 0 0 0.6em;
}

.chat-message ul,
.chat-message ol {
  margin: 0 0 0.6em;
  padding-left: 1.3em;
}

.chat-message h1,
.chat-message h2,
.chat-message h3 {
  margin: 0.8em 0 0.4em;
  line-height: 1.3;
}

.chat-message a {
  color: inherit;
  text-decoration: underline;
}

.chat-message code {
  background: rgba(0, 0, 0, 0.25);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}

.chat-message pre {
  background: rgba(0, 0, 0, 0.3);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 0 0 0.6em;
}

.chat-message pre code {
  background: none;
  padding: 0;
}

.chat-message blockquote {
  margin: 0 0 0.6em;
  padding-left: 10px;
  border-left: 3px solid currentColor;
  opacity: 0.85;
}

.chat-message table {
  border-collapse: collapse;
  margin-bottom: 0.6em;
}

.chat-message th,
.chat-message td {
  border: 1px solid currentColor;
  padding: 4px 8px;
  opacity: 0.9;
}

.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--agent-border);
  background: var(--agent-surface);
  flex-shrink: 0;
}

.chat-form textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  background: var(--agent-bg);
  border: 1px solid var(--agent-border);
  color: var(--agent-text);
  padding: 12px 14px;
  border-radius: 14px;
  font-size: 16px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease;
}

.chat-form textarea:focus {
  border-color: var(--agent-accent);
}

.send-btn {
  background: var(--agent-accent);
  color: #fff;
  border: none;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease;
}

.send-btn:hover {
  background: var(--agent-accent-hover);
}

.send-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Desktop: center a chat column inside .main so lines don't stretch edge-to-edge */
@media (min-width: 900px) {
  .chat-messages {
    padding: 24px 16px;
  }

  .chat-messages > * {
    align-self: center;
  }

  .chat-messages {
    align-items: center;
  }

  .chat-message {
    max-width: 640px;
  }

  .chat-message.user {
    align-self: flex-end;
    margin-right: calc(50% - 320px);
  }

  .chat-message.agent {
    align-self: flex-start;
    margin-left: calc(50% - 320px);
  }

  .chat-form {
    padding-left: calc(50% - 380px);
    padding-right: calc(50% - 380px);
  }
}

/* Mobile-Feinschliff (Smartphones) */
html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

@media (max-width: 899px) {
  .sidebar {
    width: min(85vw, 300px);
  }

  /* Kopfzeile: Titel ausblenden, Auswahlfelder teilen sich den Platz */
  .chat-header {
    position: relative;
    gap: 6px;
    padding: 10px 12px;
    padding-top: calc(10px + env(safe-area-inset-top));
  }

  .chat-header > .chat-title {
    display: none;
  }

  .chat-header .model-select {
    flex: 1 1 0;
    min-width: 0;
    max-width: none;
    margin-left: 0;
    font-size: 16px; /* verhindert Auto-Zoom auf iOS */
    text-overflow: ellipsis;
  }

  .chat-header .scope-select-btn {
    flex: 0 1 auto;
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 14px;
  }

  .icon-btn {
    width: 40px;
    height: 40px;
  }

  /* Ordner-Auswahl über die volle Breite unter der Kopfzeile */
  .scope-select-wrapper {
    position: static;
    flex: 0 1 auto;
    min-width: 0;
  }

  .scope-panel {
    left: 12px;
    right: 12px;
    top: calc(100% + 4px);
    min-width: 0;
    max-height: 50vh;
  }

  .scope-option {
    padding: 11px 8px;
    font-size: 15px;
  }

  .chat-messages {
    padding: 12px;
  }

  .chat-message {
    max-width: 92%;
    font-size: 16px;
    overflow-wrap: anywhere;
  }

  .chat-message table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
  }

  .chat-form {
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  /* Auf Touch-Geräten gibt es kein Hover: Löschen-Button immer zeigen */
  .chat-list-item {
    padding: 13px 10px;
  }

  .chat-list-item-delete {
    display: flex;
    width: 34px;
    height: 34px;
  }

  .file-card-delete {
    width: 36px;
    height: 36px;
    font-size: 22px;
  }

  .files-modal-header {
    padding: 10px 12px;
    padding-top: calc(10px + env(safe-area-inset-top));
  }

  .files-list {
    padding: 12px;
  }

  .modal-overlay {
    padding: 16px;
  }

  .modal-box {
    max-height: calc(100dvh - 32px);
    overflow-y: auto;
  }

  .settings-field textarea,
  .settings-field input[type="number"] {
    font-size: 16px;
  }

  .modal-btn-secondary,
  .modal-btn-danger {
    padding: 12px 16px;
  }
}

/* Scrollbar styling */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--agent-border);
  border-radius: 4px;
}

/* Dateien-Modal: Ordner zuerst, per Klick rein in die Dateien */
#files-modal {
  padding: 0;
  z-index: 9500;
}

.files-modal-box {
  background: var(--agent-bg);
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.files-modal-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px;
  padding-top: calc(14px + env(safe-area-inset-top));
  background: var(--agent-surface);
  border-bottom: 1px solid var(--agent-border);
  flex-shrink: 0;
}

.files-modal-header .chat-title {
  flex: 1;
}

.upload-pending-bar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 16px;
  background: var(--agent-surface-2);
  border-bottom: 1px solid var(--agent-border);
  flex-shrink: 0;
}

.upload-pending-bar.hidden {
  display: none;
}

.upload-pending-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 140px;
  overflow-y: auto;
}

.upload-pending-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--agent-text);
}

.upload-pending-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-pending-item-status {
  flex-shrink: 0;
  font-size: 12px;
  color: var(--agent-text-dim);
}

.upload-pending-item.done .upload-pending-item-status {
  color: #6bcf7f;
}

.upload-pending-item.error .upload-pending-item-status {
  color: var(--agent-error);
}

.upload-pending-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

.upload-confirm-btn {
  padding: 8px 14px;
  font-size: 13px;
}

.files-modal-body {
  flex: 1;
  min-height: 0;
  padding: 12px;
}

.files-modal-body.hidden {
  display: none;
}

/* Desktop: Dateien-Modal nimmt nur einen Teil des Bildschirms ein */
@media (min-width: 900px) {
  #files-modal {
    padding: 24px;
  }

  .files-modal-box {
    width: 100%;
    max-width: 640px;
    height: 80vh;
    max-height: 720px;
    margin: 0 auto;
    border-radius: 16px;
    border: 1px solid var(--agent-border);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
    overflow: hidden;
  }
}

.files-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.files-empty-hint {
  margin: auto;
  color: var(--agent-text-dim);
  font-size: 14px;
  text-align: center;
  padding: 24px;
}

.file-card {
  background: var(--agent-surface);
  border: 1px solid var(--agent-border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.file-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.file-card-name {
  font-weight: 600;
  font-size: 14px;
  word-break: break-word;
}

.file-card-summary {
  color: var(--agent-text-dim);
  font-size: 13px;
  line-height: 1.4;
}

.file-card-function {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--agent-accent);
  background: var(--agent-surface-2);
  padding: 3px 8px;
  border-radius: 6px;
  width: fit-content;
}

.file-card-warning {
  color: var(--agent-error);
  font-size: 12px;
}

.file-card-delete {
  background: transparent;
  border: none;
  color: var(--agent-text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  flex-shrink: 0;
}

.file-card-delete:hover {
  background: var(--agent-border);
  color: var(--agent-error);
}

/* Ordner-Liste: Ordner-Icon links neben dem Namen */
.folder-list-item-icon {
  display: flex;
  flex-shrink: 0;
  color: var(--agent-text-dim);
}

/* Bestätigungs-Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  padding: 24px;
}

.modal-overlay.hidden {
  display: none;
}

.modal-box {
  background: var(--agent-surface);
  border: 1px solid var(--agent-border);
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-box p {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
}

.modal-box input[type="text"] {
  background: var(--agent-bg);
  border: 1px solid var(--agent-border);
  color: var(--agent-text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 16px;
  outline: none;
}

.modal-box input[type="text"]:focus {
  border-color: var(--agent-accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-btn-secondary,
.modal-btn-danger {
  border: none;
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.modal-btn-secondary {
  background: var(--agent-surface-2);
  color: var(--agent-text);
}

.modal-btn-secondary:hover {
  background: var(--agent-border);
}

.modal-btn-danger {
  background: var(--agent-error);
  color: #fff;
}

.modal-btn-danger:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Modell-Einstellungen */
.settings-modal-box {
  max-width: 420px;
  gap: 16px;
}

.settings-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.settings-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  color: var(--agent-text);
}

.settings-value {
  font-weight: 400;
  color: var(--agent-text-dim);
  font-variant-numeric: tabular-nums;
}

.settings-hint {
  font-size: 12px;
  color: var(--agent-text-dim);
}

.settings-field textarea {
  background: var(--agent-bg);
  border: 1px solid var(--agent-border);
  color: var(--agent-text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  outline: none;
}

.settings-field textarea:focus {
  border-color: var(--agent-accent);
}

.settings-field input[type="number"] {
  background: var(--agent-bg);
  border: 1px solid var(--agent-border);
  color: var(--agent-text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
}

.settings-field input[type="number"]:focus {
  border-color: var(--agent-accent);
}

.settings-field input[type="range"] {
  accent-color: var(--agent-accent);
  width: 100%;
  cursor: pointer;
}
