:root {
  /* Hell (Standard) */
  --bg: #ffffff;
  --panel: #f3f4f6;
  --border: #dde1e6;
  --text: #16181d;
  --muted: #667085;
  --accent: #3457d5;
  --danger: #c92a2a;
  --ok: #15803d;
  --teaser: #92600f;
  color-scheme: light dark;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --panel: #171a21;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --muted: #8b93a1;
    --accent: #5b8cff;
    --danger: #ff5b5b;
    --ok: #4ade80;
    --teaser: #e0af68;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --muted: #8b93a1;
  --accent: #5b8cff;
  --danger: #ff5b5b;
  --ok: #4ade80;
  --teaser: #e0af68;
}

* { box-sizing: border-box; }

html, body { height: 100%; overscroll-behavior: none; }

body {
  display: flex;
  flex-direction: column;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  /* Sicherheitsnetz: ohne das kann ein zu breites Flex-Kind (siehe .editorbar) die ganze Seite
     horizontal aufreissen, statt selbst umzubrechen — auf schmalen/mobilen Bildschirmen sonst
     nicht mehr bedienbar (Buttons ausserhalb des sichtbaren Bereichs). */
  overflow-x: hidden;
}

.hidden { display: none !important; }

button, .filebtn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
}
button:hover, .filebtn:hover { border-color: var(--accent); }
.danger:hover { border-color: var(--danger); color: var(--danger); }

.icon-btn { padding: 8px 10px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; position: relative; }
.icon { width: 16px; height: 16px; display: block; }

.badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 14px;
  height: 14px;
  padding: 0 3px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.6rem;
  font-weight: 700;
  line-height: 14px;
  text-align: center;
}

#trackChangesBtn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-with-shortcut {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  line-height: 1.15;
}
/* Bei ausreichend breiten Buttons (z.B. "Speichern") steht das Kürzel neben statt unter dem
   Label — nur die schmalen Toolbar-Buttons (B/I/H1/H2/T) brauchen den zweizeiligen Stack. */
.btn-with-shortcut.inline {
  flex-direction: row;
  align-items: baseline;
  gap: 6px;
}
.shortcut-hint {
  font-size: 0.6rem;
  font-weight: 400;
  opacity: 0.55;
  letter-spacing: 0.02em;
}

#saveBtn.save-confirmed {
  border-color: var(--ok);
  color: var(--ok);
}

button:disabled, input:disabled, textarea:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
button:disabled:hover { border-color: var(--border); }
.danger:disabled:hover { border-color: var(--border); color: var(--text); }

.status { font-size: 0.85rem; color: var(--muted); }
.status.ok { color: var(--ok); }

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}
.status-dot.ok { background: var(--ok); }

main {
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  margin-right: 0;
  transition: margin-right 0.2s ease;
}
/* Material-Spalte hat bewusst keinen Backdrop (bleibt neben dem Editor offen) — damit sie die
   rechten Editorbar-Buttons (Speichern etc.) nicht einfach überdeckt, weicht der Inhalt aus. */
body.material-open main { margin-right: 300px; }

/* Dateiliste als Overlay-Drawer, nicht permanent im Layout */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
}
.sidebar-backdrop.hidden { display: none; }

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 85vw;
  background: var(--bg);
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  z-index: 50;
}
.sidebar.open { transform: translateX(0); }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.sidebar-header h2 { font-size: 0.9rem; color: var(--muted); margin: 0; }

.sidebar-author {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 16px;
}
.sidebar-author input {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.85rem;
}

.newfile { display: flex; gap: 6px; margin-bottom: 12px; }
.newfile input {
  flex: 1;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.85rem;
}

#fileList { list-style: none; padding: 0; margin: 0; }
#fileList li {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text);
}
#fileList li:hover { background: var(--panel); }

.editorpane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
.editorbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  row-gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  color: var(--muted);
}
.editorbar-left,
.editorbar-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}
/* Ohne Leerzeichen (Dateinamen sind kebab-case) kann Text hier nicht umbrechen — ohne
   Kuerzung wuerde ein langer Name die ganze Editorbar in die Breite zwingen (siehe .editorbar
   flex-wrap: das haette den Umbruch nicht verhindert, nur den langen Namen selbst nicht). */
#currentFileLabel {
  min-width: 0;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* Trennt die Dokument-Aktionen (Import/Export/Speichern/Löschen) optisch vom Schlüssel-/
   Einstellungs-Cluster am Rand — beide bleiben unabhängig vom Datei-/Sperrzustand bedienbar. */
.editorbar-divider {
  width: 1px;
  height: 20px;
  background: var(--border);
  flex: 0 0 auto;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  row-gap: 6px;
  gap: 6px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
}
.toolbar button {
  min-width: 36px;
  padding: 6px 10px;
}

.suggestion-btn { font-weight: 700; }
.suggestion-btn-ins { color: var(--ok); }
.suggestion-btn-del { color: var(--danger); }
.suggestion-btn-comment { color: var(--accent); }

/* Hinweis, wenn eine andere Sitzung (per Heartbeat erkannt) dieselbe Datei gerade bearbeitet —
   siehe applyReadOnlyLock in app.js. --teaser ist bereits die im Theme vorhandene Warnfarbe. */
.readonly-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  font-size: 0.8rem;
  color: var(--teaser);
  background: color-mix(in srgb, var(--teaser) 12%, transparent);
  border-bottom: 1px solid var(--border);
}
.readonly-banner .icon { width: 14px; height: 14px; flex: 0 0 auto; }

.editor-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
}
.editor-highlight,
.editor-input {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 65ch;
  margin: 0;
  padding: 20px;
  font-size: 0.95rem;
  font-family: "SF Mono", Consolas, monospace;
  /* Etwas grosszuegiger als frueher (1.5) — die Material-Anker-Pins schweben ueber ihrer Zeile
     (siehe .material-anchor-pin) und brauchen dafuer etwas Platz zur Zeile darueber. Gilt fuer
     Overlay UND Textarea gleichermassen, sonst laufen beide auseinander. */
  line-height: 1.7;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow: auto;
  border: none;
}
.editor-highlight {
  color: var(--text);
  pointer-events: none;
  background: transparent;
  /* Liegt bewusst ÜBER der Textarea (z-index), damit einzelne Kind-Elemente (die
     Annehmen/Ablehnen-Icons) per pointer-events:auto klickbar sein können — der Rest des
     Overlays bleibt dank pointer-events:none hier am Container weiterhin klickdurchlässig,
     Klicks/Tippen erreichen also normal die Textarea darunter. */
  z-index: 2;
}
.editor-input {
  background: transparent;
  color: transparent;
  caret-color: var(--text);
  -webkit-text-fill-color: transparent;
  resize: none;
  outline: none;
  z-index: 1;
}
.editor-input::placeholder {
  color: var(--muted);
  -webkit-text-fill-color: var(--muted);
}

.md-marker { opacity: 0.4; }
/* Kein font-size hier: jede Abweichung von der Basisgröße ändert die Zeilenhöhe
   dieser Zeile und bringt Overlay und Textarea (die dieselbe Zeilenhöhe für den
   gesamten Text hat) für alle folgenden Zeilen aus dem Gleichgewicht. */
.md-line.md-h1 { font-weight: 700; color: var(--accent); }
.md-line.md-h2 { font-weight: 700; text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 3px; }
.md-line.md-teaser { font-style: italic; color: var(--teaser); }

.suggestion-ins { color: var(--ok); text-decoration: underline; text-decoration-style: solid; }
.suggestion-del { color: var(--danger); text-decoration: line-through; }
.suggestion-comment { color: var(--accent); font-style: italic; }
/* opacity statt display:none — {++/++} etc. bleiben im Rohtext, brauchen im Overlay aber
   exakt so viel Platz wie in der Textarea darunter, sonst driftet der Geist-Cursor auseinander. */
.suggestion-marker { opacity: 0; }

/* Klickbare Annehmen/Ablehnen-Icons genau an der Stelle der (unsichtbaren) {++/++} etc.
   Zeichen — visibility:hidden statt opacity auf dem Text, damit das Icon-Kind unabhängig
   davon sichtbar bleiben kann (opacity würde auch Kinder mit ausblenden). */
.suggestion-marker-action {
  position: relative;
  display: inline-block;
  pointer-events: auto;
  cursor: pointer;
}
.suggestion-marker-text { visibility: hidden; }
.suggestion-action-icon {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 12px;
  height: 12px;
  pointer-events: none;
}
.suggestion-action-accept .suggestion-action-icon { color: var(--ok); }
.suggestion-action-reject .suggestion-action-icon { color: var(--danger); }
.suggestion-marker-action:hover .suggestion-action-icon { opacity: 0.65; }

.char-counts {
  flex: 0 0 auto;
  padding: 6px 20px;
  font-size: 0.75rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}
.para-over { color: var(--danger); opacity: 0.55; }
.para-under { color: var(--ok); opacity: 0.55; }
.para-count.para-active { opacity: 1; font-weight: 600; }

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  max-width: 90vw;
  text-align: center;
}
.modal-card h2 { margin-top: 0; font-size: 1.1rem; }
.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 50vh;
  min-width: 240px;
  overflow-y: auto;
  text-align: left;
}
.history-list li {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}
.history-list li:hover { background: var(--panel); }
.history-list li.history-empty {
  color: var(--muted);
  cursor: default;
  text-align: center;
}
.history-list li.history-empty:hover { background: transparent; }

.suggestions-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: default;
}
.suggestions-list li:hover { background: transparent; }
.suggestion-item-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.suggestion-item-ins .suggestion-item-text { color: var(--ok); }
.suggestion-item-del .suggestion-item-text { color: var(--danger); }
.suggestion-item-comment .suggestion-item-text { color: var(--accent); }
.suggestion-item-actions {
  display: flex;
  gap: 6px;
  flex: 0 0 auto;
}
.suggestion-action-btn { padding: 6px; }
.suggestion-action-btn .icon { width: 14px; height: 14px; }
.suggestion-action-accept { color: var(--ok); }
.suggestion-action-reject { color: var(--danger); }
#scanVideo {
  width: 320px;
  max-width: 80vw;
  border-radius: 8px;
  background: #000;
}
.modal-actions {
  margin-top: 16px;
  display: flex;
  gap: 10px;
  justify-content: center;
}

/* Material-Spalte: Referenzen fürs Voice-Over/Motion-Design, unabhängig vom Markdown-Text.
   Bewusst ohne Backdrop (anders als .sidebar) — soll parallel zum Schreiben offen bleiben. */
.material-column {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 300px;
  max-width: 85vw;
  background: var(--bg);
  border-left: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.2s ease, background 0.15s ease;
  z-index: 45;
  display: flex;
  flex-direction: column;
}
.material-column.open { transform: translateX(0); }
.material-column.drag-over { background: var(--panel); }

.material-add-row { display: flex; gap: 6px; margin-bottom: 12px; }

.material-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; flex: 1 1 auto; }

.material-block {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font-size: 0.85rem;
  cursor: pointer; /* Klick auf den Block hebt den zugehörigen Anker im Text hervor */
}
.material-block.drag-over { border-color: var(--accent); }
.material-block.material-block-highlight { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.material-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 6px;
}
.material-block-type {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}
.material-block-tag {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 0.75rem;
}
.material-block-actions { display: flex; gap: 4px; }
.material-block-actions button { padding: 4px 6px; }
.material-block-actions .icon { width: 14px; height: 14px; }
.material-block textarea {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  resize: vertical;
  min-height: 44px;
  padding: 0;
}
.material-block textarea:focus { outline: none; }
.material-block img { max-width: 100%; border-radius: 6px; display: block; }
.material-block a { color: var(--accent); word-break: break-all; font-size: 0.8rem; }
.material-block-anchor {
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  color: var(--muted);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}
.material-block-anchor:hover { color: var(--accent); }
.material-block-anchor.linked { color: var(--accent); }
.material-block-anchor .icon { width: 12px; height: 12px; }

.material-empty {
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
  margin-top: 20px;
}
.material-empty.hidden { display: none; }

/* Anker-Marker im Editor-Overlay: der Anker selbst besteht aus echten Zero-Width-Unicode-
   Zeichen (siehe MATERIAL_ANCHOR_* in app.js) und rendert dadurch bereits von sich aus mit 0px
   Breite, in Overlay UND Textarea gleichermaßen — keine reservierte Lücke wie bei sichtbaren
   Zeichen nötig. Das klickbare Icon sitzt trotzdem an exakt dieser Stelle. */
.material-anchor-action {
  position: relative;
  display: inline-block;
  /* Der Anker-Text ist jetzt 0px breit (siehe oben) — diese Box hat dadurch selbst keine
     eigene Fläche mehr. Pointer-events/cursor sitzen deshalb direkt am Icon (siehe unten),
     nicht hier: ein 0×0-Element kann keine Klicks/Hover empfangen, egal was CSS dazu sagt. */
}
/* visibility:hidden nur als zusätzliches Sicherheitsnetz, falls eine Schrift/Plattform die
   Zero-Width-Zeichen doch einmal nicht mit 0px rendert. */
.material-anchor-text { visibility: hidden; }

/* Pin-Form (klassischer CSS-"Map-Pin"-Trick): ein Quadrat mit drei runden Ecken und einer
   spitzen Ecke, um 45° gedreht — die Spitze zeigt dadurch exakt nach unten auf die Ankerstelle,
   der runde Kopf schwebt etwas über der Zeile (siehe bottom/transform), damit der Marker gut
   sichtbar bleibt statt in der Zeile unterzugehen. */
.material-anchor-pin {
  position: absolute;
  left: 0;
  bottom: 75%;
  width: 18px;
  height: 18px;
  transform: translate(-50%, -3px) rotate(-45deg);
  transform-origin: center;
  border-radius: 50% 50% 50% 0;
  background: var(--accent);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease, transform 0.15s ease;
}
.material-anchor-pin .material-anchor-icon {
  width: 10px;
  height: 10px;
  color: #fff;
  transform: rotate(45deg); /* Gegenrotation zum Pin, damit das Icon selbst aufrecht bleibt */
}
/* Pins nur sichtbar UND klickbar, während die Material-Spalte offen ist (siehe
   body.material-open, von JS beim Öffnen/Schließen der Spalte gesetzt) — geschlossen ist vom
   Material im Fließtext nichts zu sehen, dann fallen Klicks normal auf die Textarea durch. */
body.material-open .material-anchor-pin {
  opacity: 1;
  pointer-events: auto;
  cursor: pointer;
}
body.material-open .material-anchor-pin:hover {
  transform: translate(-50%, -5px) rotate(-45deg);
}
/* Kurzes Hervorheben: von der Material-Spalte aus angestossen (Klick auf einen Block, siehe
   highlightAnchorInText) oder umgekehrt (Klick auf den Pin hebt den Block hervor). */
@keyframes material-anchor-pulse {
  0%, 100% { transform: translate(-50%, -3px) rotate(-45deg) scale(1); }
  50% { transform: translate(-50%, -7px) rotate(-45deg) scale(1.35); }
}
.material-anchor-pin.material-anchor-pin-highlight {
  animation: material-anchor-pulse 0.6s ease;
}

@media (max-width: 720px) {
  /* Sobald die Editorbar umbricht, trennt der Zeilenumbruch die Gruppen schon optisch —
     der Trenner würde sonst allein am Ende einer Zeile "schweben". */
  .editorbar-divider { display: none; }
  button, .filebtn { padding: 10px 14px; }
  .toolbar button { padding: 8px 12px; }
  .sidebar { width: 260px; }
  /* Auf schmalen Bildschirmen bliebe neben einer 260px-Spalte kaum noch nutzbarer Platz für den
     Editor — hier überlagert die Material-Spalte den Inhalt stattdessen vollständig (wie die
     Dateiliste), statt ihn schmal an den Rand zu drücken. */
  /* Auf dem Handy bewusst reduzierter Funktionsumfang — Material-Spalte, Versionsverlauf,
     Vorschlagsliste, .docx-Import/-Export und der Datei-Upload für den Schlüssel brauchen alle
     entweder viel Raum oder eine Desktop-typische Interaktion (Drag&Drop, Datei-Dialog). Auf
     Mobilgeräten bleibt nur der QR-Scan als Weg, einen Schlüssel zu laden. */
  .material-column { display: none; }
  #historyBtn,
  #suggestionsBtn,
  #materialToggleBtn,
  #importDocxBtn,
  #exportDocxBtn,
  #keyFileLabel {
    display: none;
  }
  body.material-open main { margin-right: 0; }
}
