:root {
  /* KOMPLETT DARKMODE */
  --ink:        #e0e4eb;
  --ink-soft:   #9fa6b2;
  --paper:      #1a1d24;
  --paper-warm: #14161b;
  --rail:       #111216;
  --rail-line:  #272b33;
  --rail-text:  #b6bcc8;
  --accent:     #8c2f39;
  --accent-dim: #b9636b;
  --line:       #2e323b;

  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: inherit; }

/* FIX FÜR HEADER UND SCROLLEN: Body darf nicht scrollen, nur die Nachrichten! */
html, body { height: 100%; height: 100dvh; margin: 0; overflow: hidden; font-size: 16px; }
body {
  font-family: var(--sans);
  color: var(--ink);
  background: var(--paper-warm);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent-dim); outline-offset: 2px; }
.is-hidden { display: none !important; }

#app { display: grid; grid-template-columns: 290px 1fr; height: 100%; height: 100dvh; overflow: hidden; }

/* ---------- Rail ---------- */
#rail {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--rail); color: var(--rail-text);
  border-right: 1px solid var(--rail-line);
}
.rail-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: .5rem; padding: 1.15rem 1.1rem .8rem;
}
.rail-head h1 {
  margin: 0; font-family: var(--serif); font-size: 1.22rem;
  font-weight: 600; letter-spacing: .01em; color: #fff;
}
.status { font-size: .7rem; color: var(--ink-soft); }
.status.on  { color: #6fbf8b; }
.status.off { color: var(--accent-dim); }

.tabs { display: flex; gap: 1.1rem; padding: 0 1.1rem; border-bottom: 1px solid var(--rail-line); }
.tab {
  background: none; border: 0; padding: 0 0 .6rem; color: var(--ink-soft);
  font-size: .82rem; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab.is-active { color: #fff; border-bottom-color: var(--accent-dim); }

.panel { flex: 1; min-height: 0; overflow-y: auto; padding: .9rem .75rem; }
.list { list-style: none; margin: 0; padding: 0; }
.list li {
  padding: .6rem .6rem; border-radius: 6px; cursor: pointer;
  display: flex; gap: .65rem; align-items: flex-start; position: relative;
}
.list li:hover { background: #1d2026; }
.list li.is-active { background: #232730; }
.list .avatar { font-size: 1.15rem; line-height: 1.3; }
.list .meta { min-width: 0; flex: 1; }
.list .meta b { display: block; font-size: .86rem; font-weight: 550; color: #eceff4; }
.list .meta span {
  display: block; font-size: .74rem; color: var(--ink-soft);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.chat-del-btn {
  position: absolute; right: 5px; top: 10px; background: none; border: none;
  color: var(--accent); opacity: 0; font-size: 1.1rem; padding: 5px;
}
.list li:hover .chat-del-btn { opacity: 0.6; }
.chat-del-btn:hover { opacity: 1 !important; transform: scale(1.1); }

.ghost-btn {
  width: 100%; background: none; color: var(--rail-text);
  border: 1px solid var(--rail-line); border-radius: 6px;
  padding: .5rem; font-size: .8rem; margin-bottom: .6rem;
}
.ghost-btn:hover { border-color: #3a404b; color: #fff; }
.rail-foot { padding: .75rem; border-top: 1px solid var(--rail-line); }
.rail-foot .ghost-btn { margin: 0; }

/* ---------- Stage ---------- */
#stage { min-width: 0; display: flex; flex-direction: column; background: var(--paper); height: 100%; }
#chat, .empty { flex: 1; display: flex; flex-direction: column; min-height: 0; height: 100%; }
.empty { align-items: center; justify-content: center; color: var(--ink-soft); text-align: center; padding: 2rem; }
.empty-mark { font-family: var(--serif); font-size: 3rem; color: var(--line); margin: 0 0 .5rem; }
.empty p { margin: 0; max-width: 28ch; }

.chat-head {
  display: flex; align-items: center; gap: .7rem; position: relative;
  padding: .6rem 0.8rem; border-bottom: 1px solid var(--line); background: var(--paper);
  flex-wrap: nowrap; flex-shrink: 0; z-index: 100;
}
.chat-emoji { font-size: 1.2rem; }
.chat-titles { flex: 1; min-width: 0; line-height: 1.2; }
.chat-titles strong { display: block; font-family: var(--serif); font-size: 0.95rem; }
.chat-titles span { display: block; font-size: .7rem; color: var(--ink-soft); }

.chat-controls { display: flex; gap: 0.4rem; align-items: center; margin-right: 0.2rem; }
.chat-controls select {
  padding: 0.3rem 0.4rem; border-radius: 6px; background: var(--paper-warm);
  border: 1px solid var(--line); color: var(--ink); font-size: 0.75rem; cursor: pointer;
}
.chat-controls select:focus { border-color: var(--accent-dim); outline: none; }

.icon-btn { background: none; border: 0; font-size: 1.1rem; color: var(--ink-soft); padding: .2rem .45rem; border-radius: 6px; }
.icon-btn:hover { background: var(--paper-warm); color: var(--ink); }
#back { display: none; }

.menu {
  position: absolute; top: 100%; right: .8rem; z-index: 20;
  background: var(--paper-warm); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 10px 28px rgba(0,0,0,.5); padding: .25rem; min-width: 210px;
}
.menu button {
  display: block; width: 100%; text-align: left; background: none; border: 0;
  padding: .5rem .6rem; border-radius: 5px; font-size: .85rem;
}
.menu button:hover { background: var(--rail-line); }
.menu .danger, .danger-text { color: var(--accent); }

/* ---------- Nachrichten ---------- */
.messages { flex: 1; overflow-y: auto; padding: 1.2rem 1rem 2rem; -webkit-overflow-scrolling: touch; }
.msg { max-width: 68ch; margin: 0 auto 1.2rem; }
.msg .who {
  font-size: .65rem; color: var(--ink-soft); margin-bottom: .2rem;
  font-variant-caps: normal; text-transform: uppercase; letter-spacing: 0.5px;
}
.msg .body {
  font-family: var(--serif); font-size: 0.9rem; line-height: 1.5; /* SCHRIFT KLEINER */
  white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .body { border-left: 2px solid var(--line); padding-left: 1rem; }
.msg.assistant em { color: var(--ink-soft); font-style: italic; }
.msg.user .body {
  background: var(--paper-warm); padding: .65rem .8rem; border-radius: 10px 10px 10px 2px;
  border: 1px solid var(--line);
}
.msg .tools { margin-top: .35rem; display: flex; gap: .75rem; opacity: 0; transition: opacity .12s; }
.msg:hover .tools, .msg:focus-within .tools { opacity: 1; }
.msg .tools button { background: none; border: 0; padding: 0; font-size: .7rem; color: var(--ink-soft); }
.msg .tools button:hover { color: var(--accent); }
.msg .body[contenteditable="true"] {
  outline: 2px solid var(--accent-dim); outline-offset: 4px; border-radius: 4px;
  background: var(--paper-warm);
}
.caret::after {
  content: "▍"; color: var(--accent-dim); animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .caret::after { animation: none; } }

.notice {
  max-width: 68ch; margin: 0 auto 1.5rem; font-size: .76rem; color: var(--ink-soft);
  text-align: center; border-top: 1px solid var(--line); padding-top: .5rem;
}
.notice.error { color: var(--accent); border-top-color: var(--accent-dim); }

/* ---------- Composer ---------- */
.composer {
  display: flex; gap: .6rem; align-items: flex-end;
  padding: .8rem 1rem 1rem; border-top: 1px solid var(--line);
  width: 100%; max-width: calc(68ch + 2rem); margin: 0 auto;
  background: var(--paper); flex-shrink: 0; z-index: 100;
}
.composer textarea {
  flex: 1; resize: none; max-height: 30vh; padding: .65rem .8rem;
  border: 1px solid var(--line); border-radius: 10px; background: var(--paper-warm);
  font-family: var(--serif); line-height: 1.4; color: var(--ink); font-size: 0.9rem;
}
.composer textarea:focus { border-color: var(--accent-dim); outline: none; }
.composer button {
  border: 0; border-radius: 9px; padding: .6rem 1rem;
  background: var(--accent); color: #fff; font-size: .85rem;
}
.composer button:disabled { background: var(--rail-line); color: var(--ink-soft); }
#stop { background: var(--ink-soft); color: var(--paper-warm); }

/* ---------- Dialoge ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 12px; padding: 1.5rem; width: min(560px, 92vw);
  background: var(--paper-warm); color: var(--ink);
  box-shadow: 0 24px 60px rgba(0,0,0,.6);
}
dialog::backdrop { background: rgba(0,0,0,.7); }
dialog h2 { font-family: var(--serif); font-weight: 600; font-size: 1.1rem; margin: 0 0 1.1rem; }
dialog label { display: block; font-size: .78rem; color: var(--ink-soft); margin-bottom: .85rem; }
dialog input, dialog textarea, dialog select {
  display: block; width: 100%; margin-top: .3rem; padding: .5rem .6rem;
  border: 1px solid var(--line); border-radius: 7px; background: var(--paper);
  font-size: .85rem; color: var(--ink);
}
dialog textarea { resize: vertical; font-family: var(--serif); line-height: 1.5; }
dialog input:focus, dialog textarea:focus, dialog select:focus { border-color: var(--accent-dim); outline: none; }
.row.two { display: grid; grid-template-columns: 1fr 1fr; gap: .9rem; }
.hint { font-size: .74rem; color: var(--ink-soft); margin: -.4rem 0 1rem; line-height: 1.45; }
.dialog-actions { display: flex; gap: .6rem; align-items: center; margin: .5rem 0 0; padding: 0; }
.dialog-actions .spacer { flex: 1; }
.dialog-actions .ghost-btn { width: auto; border-color: var(--line); color: var(--ink-soft); margin: 0; }
.danger-text { background: none; border: 0; font-size: .8rem; padding: .5rem 0; }
.primary-btn { background: var(--accent); color: #fff; border: 0; border-radius: 7px; padding: .55rem 1.1rem; font-size: .85rem; }

/* ---------- Mobil ---------- */
@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #rail { display: flex; height: 100dvh; }
  #stage { display: none; height: 100dvh; }
  body.viewing-chat #rail { display: none; }
  body.viewing-chat #stage { display: flex; }
  #back { display: block; }
  .msg .tools { opacity: 1; }
  .composer { padding-bottom: calc(0.8rem + env(safe-area-inset-bottom, 0px)); }
  .chat-head { padding: 0.5rem 0.5rem; }
  .chat-controls { display: flex; margin-left: auto; }
  .chat-controls select { max-width: 75px; font-size: 0.7rem; padding: 0.2rem; }
}