:root {
  --paper:   #ffffff;
  --desk:    #e8eaed;
  --ink:     #1b232c;
  --muted:   #6c7683;
  --line:    #d3d8de;
  --pine:    #26645a;
  --pine-lt: #e3efec;
  --amber:   #a8681f;

  --mono:  "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
  --sans:  "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;

  --split: 50%;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--desk);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ---------- Kopfleiste ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 6px;
  padding: 6px 10px;
  font: inherit;
  cursor: pointer;
}
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.4; }
.icon-btn[aria-expanded="true"] { background: var(--pine-lt); border-color: var(--pine); color: var(--pine); }

.title-input {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 10px;
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 600;
  color: var(--ink);
  background: transparent;
}
.title-input:hover { border-color: var(--line); }
.title-input:focus { outline: none; border-color: var(--pine); background: var(--paper); }

.status {
  flex: 0 0 auto;
  font-size: 12.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.status[data-state="dirty"] { color: var(--amber); }
.status[data-state="saved"] { color: var(--pine); }

.actions { display: flex; gap: 6px; flex: 0 0 auto; }

.btn {
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 6px;
  padding: 7px 12px;
  font: inherit;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--pine); color: var(--pine); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn--primary { background: var(--pine); border-color: var(--pine); color: #fff; }
.btn--primary:hover:not(:disabled) { background: #1d4f47; color: #fff; }

/* Export-Menü */

.menu { position: relative; }

.menu__list {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 20;
  min-width: 210px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 5px;
  box-shadow: 0 10px 28px rgba(27, 35, 44, .16);
}
.menu__list button {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 14px;
  width: 100%;
  border: 0;
  background: none;
  font: inherit;
  text-align: left;
  padding: 9px 11px;
  border-radius: 5px;
  cursor: pointer;
  color: var(--ink);
}
.menu__list button:hover { background: var(--pine-lt); color: var(--pine); }
.menu__list button span { font-size: 12.5px; color: var(--muted); font-family: var(--mono); }
.menu__list button:hover span { color: var(--pine); }
.menu__list button:disabled { opacity: .5; cursor: progress; }

.menu__options {
  border-top: 1px solid var(--line);
  margin-top: 5px;
  padding: 9px 11px 5px;
}
.menu__options label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}
.menu__options input { accent-color: var(--pine); margin: 0; }

:where(button, input, textarea, .file):focus-visible {
  outline: 2px solid var(--pine);
  outline-offset: 2px;
}

/* ---------- Arbeitsfläche ---------- */

.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

.filelist {
  width: 0;
  flex: 0 0 auto;
  overflow: hidden auto;
  background: var(--paper);
  border-right: 1px solid transparent;
  transition: width .18s ease;
}
.filelist.is-open { width: 260px; border-right-color: var(--line); }
@media (prefers-reduced-motion: reduce) { .filelist { transition: none; } }

.filelist__search {
  padding: 12px 12px 8px;
  position: sticky;
  top: 0;
  background: var(--paper);
  z-index: 1;
}
.filelist__search input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 10px;
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--paper);
}
.filelist__search input:focus { outline: none; border-color: var(--pine); }

.filelist__group {
  margin: 10px 16px 4px;
  font-size: 12px;
  color: var(--muted);
}
.filelist__hint {
  margin: 0;
  padding: 10px 16px 20px;
  border-top: 1px solid var(--line);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--muted);
}
.filelist__hint code { font-family: var(--mono); font-size: 11.5px; }
.filelist__note, .filelist__empty {
  margin: 0;
  padding: 4px 16px 14px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
}
.filelist__note code { font-family: var(--mono); font-size: 12px; }

.filelist ul { list-style: none; margin: 0; padding: 0 8px 16px; }
.filelist li { display: flex; align-items: stretch; border-radius: 6px; }
.filelist li:hover { background: var(--pine-lt); }

.file {
  flex: 1 1 auto;
  min-width: 0;
  display: block;
  text-align: left;
  border: 0;
  background: none;
  font: inherit;
  padding: 9px 10px;
  border-radius: 6px;
  cursor: pointer;
  color: inherit;
}
.file[aria-current="true"] .file__title { color: var(--pine); }
.file__title { display: block; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file__meta { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; font-variant-numeric: tabular-nums; }
.file__snippet {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-top: 3px;
  line-height: 1.45;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.file__delete {
  flex: 0 0 auto;
  border: 0; background: none; cursor: pointer;
  color: var(--muted); font-size: 18px; line-height: 1;
  padding: 0 10px; border-radius: 6px;
}
.file__delete:hover { color: #9c2f2f; }

/* ---------- Bereiche ---------- */

.panes {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
}

.pane {
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--paper);
}
.pane--editor  { width: var(--split); border-right: 1px solid var(--line); }
.pane--preview { flex: 1 1 auto; overflow: auto; background: var(--desk); }

.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.toolbar button {
  border: 0;
  background: none;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  padding: 5px 9px;
  border-radius: 5px;
  cursor: pointer;
  min-width: 32px;
}
.toolbar button:hover { background: var(--pine-lt); color: var(--pine); }
.toolbar__sep { width: 1px; height: 18px; background: var(--line); margin: 0 6px; }

#editor {
  flex: 1 1 auto;
  width: 100%;
  border: 0;
  resize: none;
  padding: 20px 22px 60px;
  font-family: var(--mono);
  font-size: 14.5px;
  line-height: 1.7;
  color: var(--ink);
  background: var(--paper);
  tab-size: 2;
}
#editor:focus { outline: none; }
#editor::placeholder { color: #a5adb7; }

.resizer {
  flex: 0 0 auto;
  width: 9px;
  margin-left: -5px;
  margin-right: -4px;
  cursor: col-resize;
  background: none;
  z-index: 2;
}

.preview {
  max-width: 68ch;
  margin: 0 auto;
  padding: 44px 40px 90px;
  background: var(--paper);
  min-height: 100%;
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.68;
}
.preview > :first-child { margin-top: 0; }
.preview h1, .preview h2, .preview h3, .preview h4 {
  font-family: var(--serif);
  font-weight: 600;
  line-height: 1.25;
  margin: 1.9em 0 .55em;
}
.preview h1 { font-size: 2em; margin-top: 0; }
.preview h2 { font-size: 1.45em; }
.preview h3 { font-size: 1.18em; }
.preview p, .preview ul, .preview ol, .preview blockquote { margin: 0 0 1.05em; }
.preview a { color: var(--pine); text-underline-offset: 2px; }
.preview blockquote {
  border-left: 3px solid var(--pine);
  margin-left: 0;
  padding: 2px 0 2px 18px;
  color: #45505c;
}
.preview code {
  font-family: var(--mono);
  font-size: .86em;
  background: #f1f3f5;
  padding: .12em .35em;
  border-radius: 3px;
}
.preview pre {
  background: #f6f7f9;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 14px 16px;
  overflow: auto;
  line-height: 1.55;
}
.preview pre code { background: none; padding: 0; font-size: .82em; }
.preview table { border-collapse: collapse; width: 100%; font-size: .93em; margin-bottom: 1.05em; }
.preview th, .preview td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; }
.preview th { background: #f6f7f9; font-weight: 600; }
.preview img { max-width: 100%; height: auto; }
.preview hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }
.preview :is(ul, ol) { padding-left: 1.3em; }
.preview .fnref { font-size: .72em; vertical-align: super; line-height: 0; }
.preview .fnref a { text-decoration: none; }
.preview .fn-rule { margin-top: 2.4em; width: 30%; margin-left: 0; }
.preview .footnotes { font-size: .85em; color: #45505c; padding-left: 1.5em; }
.preview .footnotes li { margin-bottom: .35em; }
.preview li[data-task] { list-style: none; }
.preview input[type="checkbox"] { margin-right: .45em; accent-color: var(--pine); }
.preview li { margin-bottom: .3em; }

/* ---------- Umschalter für schmale Fenster ---------- */

.tabbar { display: none; }

/* ---------- Hinweis unten ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 7px;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(27, 35, 44, .22);
}
.toast[data-tone="error"] { background: #8c2a2a; }

/* ---------- Schmale Fenster ---------- */

@media (max-width: 820px) {
  .topbar { flex-wrap: wrap; }
  .title-input { order: 3; flex-basis: 100%; font-size: 17px; }
  .icon-btn__label { display: none; }
  .actions { margin-left: auto; }
  .btn { padding: 7px 10px; font-size: 13.5px; }

  .filelist.is-open { position: absolute; z-index: 5; height: calc(100% - 52px); box-shadow: 0 8px 28px rgba(27,35,44,.18); }

  .pane--editor { width: 100% !important; border-right: 0; }
  .resizer { display: none; }
  .pane--preview { width: 100%; }
  .panes.show-preview .pane--editor { display: none; }
  .panes:not(.show-preview) .pane--preview { display: none; }
  .preview { padding: 26px 20px 70px; }

  .tabbar { display: flex; border-top: 1px solid var(--line); background: var(--paper); }
  .tabbar button {
    flex: 1; border: 0; background: none; font: inherit;
    padding: 12px; color: var(--muted); cursor: pointer;
    border-top: 2px solid transparent; margin-top: -1px;
  }
  .tabbar button.is-active { color: var(--pine); border-top-color: var(--pine); }
}

/* ---------- Druck = PDF ---------- */

@page { margin: 20mm; }

@media print {
  .topbar, .filelist, .toolbar, .resizer, .tabbar, .toast, .pane--editor { display: none !important; }

  html, body, .app, .workspace, .panes, .pane--preview {
    display: block !important;
    height: auto !important;
    overflow: visible !important;
    background: #fff !important;
    width: auto !important;
  }

  .preview {
    max-width: none;
    margin: 0;
    padding: 0;
    font-size: 11.5pt;
    line-height: 1.55;
    color: #000;
  }
  .preview a { color: #000; text-decoration: underline; }
  .preview h1, .preview h2, .preview h3 { break-after: avoid-page; }
  .preview pre, .preview blockquote, .preview table, .preview img { break-inside: avoid; }
}

/* ---------- Dialog ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(27, 35, 44, .35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.dialog {
  background: var(--paper);
  border-radius: 9px;
  padding: 24px 26px 20px;
  max-width: 460px;
  box-shadow: 0 16px 44px rgba(27, 35, 44, .26);
}
.dialog h2 {
  margin: 0 0 8px;
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 600;
}
.dialog p {
  margin: 0 0 18px;
  font-size: 14.5px;
  line-height: 1.55;
  color: #45505c;
}
.dialog__actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}
