/* ---------- Grundlayout ---------- */

html, body {
  height: 100%;
}

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  transition: background 0.3s, color 0.3s;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
}

/* Titel und Status stehen beieinander: der Zustand des Dokuments gehört zum
   Dokument, nicht zu den Werkzeugen. */
.topbar-doc {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.doc-status {
  font-size: 12px;
  color: #6a737d;
  white-space: nowrap;
}

.doc-status.doc-status-warn {
  color: #b88600;
}

.container {
  display: flex;
  gap: 20px;
  flex: 1;
  min-height: 0;
}

/* Schrift, Zeilenabstand und Polsterung des Editors: gelten für das Textfeld
   und die Farbebene darunter gleichermaßen (siehe editor-highlight.css). Ein
   Zeilenabstand von 1.8 lässt Markdown-Zeilen atmen — Tabellenzeilen sind sonst
   kaum auseinanderzuhalten. */
body {
  --editor-font-family: Consolas, "SF Mono", Menlo, monospace;
  --editor-font-size: 15px;
  --editor-line-height: 1.8;
  --editor-padding: 12px;
}

textarea {
  width: 100%;
  height: 100%;
  padding: var(--editor-padding);
  font-size: var(--editor-font-size);
  font-family: var(--editor-font-family);
  line-height: var(--editor-line-height);
  transition: background 0.3s, color 0.3s;
  box-sizing: border-box;
}

.tabs {
  display: flex;
  margin-bottom: 8px;
}

.tab {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 14px;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: #6a737d;
  cursor: pointer;
  margin-right: 2px;
  border-radius: 4px 4px 0 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

/* Das Icon nimmt die Farbe des Tabs an — es wird mit ihm hell und dunkel,
   aktiv und ruhend, ohne eigene Regel. */
.tab-icon {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

.tab:hover {
  background: rgba(128, 128, 128, 0.1);
  color: var(--text);
}

.tab.active {
  background: transparent;
  color: var(--text);
  font-weight: 600;
  border-bottom-color: var(--accent);
}

.preview-wrapper {
  display: flex;
  flex-direction: column;
  height: 460px;
  border: 1px solid var(--border);
}

.preview {
  flex: 1;
  padding: 10px;
  overflow-y: auto;
  white-space: pre-wrap;
  font-family: Consolas, monospace;
  font-size: 13px;
  transition: background 0.3s, color 0.3s;
}

.lint-panel {
  border-top: 1px solid var(--border);
  padding: 6px 10px;
  font-size: 12px;
  background: rgba(255, 255, 0, 0.05);
  overflow-y: auto;
  max-height: 140px;
}

.lint-ok {
  color: #0a7f3f;
}

.lint-warning {
  color: #b88600;
}

.lint-error {
  color: #b00020;
}

.hidden {
  display: none !important;
}

/* Kurze Rückmeldung für Aktionen ohne eigenen Dialog — etwa eine abgelehnte
   Datei, die sonst spurlos im Nichts verschwände. */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  z-index: 3000;
  max-width: 520px;
  padding: 10px 16px;
  border-radius: 6px;
  background: #24292f;
  color: #ffffff;
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
}

iframe.preview {
  background: white;
  border: 1px solid var(--border);
  height: 500px;
  width: 100%;
}

/* ---------- Light Theme ---------- */

body.light {
  --bg: #ffffff;
  --text: #222;
  --border: #ccc;
  --tab-bg: #eee;
  --tab-active: #ddd;
  --preview-bg: #fafafa;
  --editor-bg: #ffffff;
  --editor-text: #222;
  --accent: #2563c9;
  --accent-hover: #1d4ea3;
}

body.light {
  background: var(--bg);
  color: var(--text);
}

body.light textarea {
  background: #fff;
  color: #222;
}

body.light .preview {
  background: var(--preview-bg);
  color: #222;
}

/* ---------- Dark Theme ---------- */

body.dark {
  --bg: #1e1e1e;
  --text: #e0e0e0;
  --border: #555;
  --tab-bg: #333;
  --tab-active: #444;
  --preview-bg: #2a2a2a;
  --editor-bg: #2a2a2a;
  --editor-text: #e0e0e0;
  --accent: #4a90e2;
  --accent-hover: #6aa5ea;
  color-scheme: dark;
}

body.light {
  color-scheme: light;
}

body.dark {
  background: var(--bg);
  color: var(--text);
}

body.dark textarea {
  background: #2a2a2a;
  color: #e0e0e0;
}

body.dark .preview {
  background: var(--preview-bg);
  color: #e0e0e0;
}

/* ---------- Buttons ---------- */

button {
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--tab-bg);
  color: var(--text);
  transition: background 0.3s, color 0.3s;
}

/* ---------- Toolbar-Hierarchie (Issue #33) ----------
   Eine Zeile, drei Ränge: Exportieren ist die Aktion, auf die alles zuläuft und
   trägt deshalb als einzige Farbe. Add MD, Tabelle und Stil-Editor sind
   gleichrangige Werkzeuge und stehen als Gruppe zusammen. Hilfe und
   Einstellungen begleiten nur — sie kommen ohne Text aus. */

.topbar-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn-group {
  display: flex;
  align-items: center;
  gap: 1px;
  padding: 2px;
  background: rgba(128, 128, 128, 0.1);
  border-radius: 6px;
  margin-right: 4px;
}

.btn-secondary {
  border: none;
  background: transparent;
  color: var(--text);
  border-radius: 4px;
  padding: 6px 12px;
  transition: background 0.15s, box-shadow 0.15s;
}

.btn-secondary:hover {
  background: var(--bg);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.16);
}

.btn-primary {
  border: 1px solid transparent;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  border-radius: 6px;
  padding: 8px 16px;
  transition: background 0.15s, box-shadow 0.15s;
}

.btn-primary:hover {
  background: var(--accent-hover);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  line-height: 1;
  transition: background 0.15s;
}

.btn-icon:hover {
  background: rgba(128, 128, 128, 0.18);
}

.btn-primary:focus-visible,
.btn-secondary:focus-visible,
.btn-icon:focus-visible,
.tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Settings Panel ---------- */

.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 260px;
  height: 100%;
  background: var(--preview-bg);
  border-left: 1px solid var(--border);
  padding: 20px;
  box-shadow: -3px 0 8px rgba(0,0,0,0.2);
  transition: transform 0.3s;
  z-index: 100;
  overflow-y: auto;
}

.settings-panel.hidden {
  transform: translateX(100%);
}

.setting-label {
  display: block;
  margin-top: 20px;
  margin-bottom: 6px;
}

.close-btn {
  margin-top: 30px;
  width: 100%;
}

/* ---------- Editor Tabs ---------- */

/* Editor und Vorschau sind zwei Sichten auf dieselbe Fläche — die Umschaltung
   sitzt deshalb am unteren Rand dieser Fläche, nicht in der Toolbar. */
.editor-tabs {
  display: flex;
  align-items: stretch;
  margin-top: 0;
  margin-bottom: 0;
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 4px 4px;
  padding: 0;
  background: var(--preview-bg);
}

/* Der Gutter läuft bis ganz nach unten durch: die Leiste setzt seine Randspalte
   fort, und die Tabs beginnen erst rechts davon — über der Editorfläche, zu der
   sie gehören. */
.editor-tabs::before {
  content: "";
  width: 44px;
  min-width: 44px;
  flex-shrink: 0;
  background: var(--gutter-bg);
  border-right: 1px solid var(--border);
}

.editor-tabs .tab {
  margin: 4px 2px;
}

.editor-tabs .tab:first-of-type {
  margin-left: 8px;
}

/* Die Warnung über zu lange Codezeilen sitzt am anderen Ende derselben Leiste:
   sie gehört zum Text, nicht zur Toolbar, und drängt sich nicht auf. */
.long-line-warning {
  margin-left: auto;
  margin-right: 10px;
  align-self: center;
  padding: 3px 8px;
  border: none;
  border-radius: 4px;
  background: none;
  color: #b88600;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
}

.long-line-warning:hover {
  background: rgba(184, 134, 0, 0.12);
  text-decoration: underline;
}

body.dark .long-line-warning {
  color: #e4c368;
}

body.dark .long-line-warning:hover {
  background: rgba(228, 195, 104, 0.14);
}


/* ---------- Markdown Preview Panel (token-based) ---------- */

.md-preview {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  padding: 12px 16px;
  overflow-y: auto;
  /* Die Vorschau liegt über der Editorfläche und deckt deren Rahmen ab — sie
     muss ihn also selbst führen, und zwar denselben: rundherum, unten offen,
     weil die Tab-Leiste den unteren Rand trägt. */
  border: 1px solid var(--border);
  border-bottom: none;
  z-index: 1;
  font-family: var(--md-global-font-family);
  font-size: var(--md-global-font-size);
  line-height: var(--md-global-line-height);
  color: var(--md-global-text-color);
  background: var(--md-global-bg-color);
  text-align: var(--md-global-text-align);
  transition: background 0.3s, color 0.3s;
}

.md-preview > * {
  max-width: var(--md-global-content-width);
}

.md-preview ::selection {
  background: var(--md-global-selection-color);
}

.editor-area.preview-mode #mdPreview {
  display: block;
}

.md-preview h1 {
  font-family: var(--md-h1-font-family);
  font-size: var(--md-h1-font-size);
  color: var(--md-h1-color);
  font-weight: var(--md-h1-bold);
  font-style: var(--md-h1-italic);
  text-decoration: var(--md-h1-underline);
  line-height: var(--md-h1-line-height);
  margin-top: var(--md-h1-margin-top);
  margin-bottom: var(--md-h1-margin-bottom);
}

.md-preview h2 {
  font-family: var(--md-h2-font-family);
  font-size: var(--md-h2-font-size);
  color: var(--md-h2-color);
  font-weight: var(--md-h2-bold);
  font-style: var(--md-h2-italic);
  text-decoration: var(--md-h2-underline);
  line-height: var(--md-h2-line-height);
  margin-top: var(--md-h2-margin-top);
  margin-bottom: var(--md-h2-margin-bottom);
}

.md-preview h3 {
  font-family: var(--md-h3-font-family);
  font-size: var(--md-h3-font-size);
  color: var(--md-h3-color);
  font-weight: var(--md-h3-bold);
  font-style: var(--md-h3-italic);
  text-decoration: var(--md-h3-underline);
  line-height: var(--md-h3-line-height);
  margin-top: var(--md-h3-margin-top);
  margin-bottom: var(--md-h3-margin-bottom);
}

.md-preview h4 {
  font-family: var(--md-h4-font-family);
  font-size: var(--md-h4-font-size);
  color: var(--md-h4-color);
  font-weight: var(--md-h4-bold);
  font-style: var(--md-h4-italic);
  text-decoration: var(--md-h4-underline);
  line-height: var(--md-h4-line-height);
  margin-top: var(--md-h4-margin-top);
  margin-bottom: var(--md-h4-margin-bottom);
}

.md-preview h5 {
  font-family: var(--md-h5-font-family);
  font-size: var(--md-h5-font-size);
  color: var(--md-h5-color);
  font-weight: var(--md-h5-bold);
  font-style: var(--md-h5-italic);
  text-decoration: var(--md-h5-underline);
  line-height: var(--md-h5-line-height);
  margin-top: var(--md-h5-margin-top);
  margin-bottom: var(--md-h5-margin-bottom);
}

.md-preview h6 {
  font-family: var(--md-h6-font-family);
  font-size: var(--md-h6-font-size);
  color: var(--md-h6-color);
  font-weight: var(--md-h6-bold);
  font-style: var(--md-h6-italic);
  text-decoration: var(--md-h6-underline);
  line-height: var(--md-h6-line-height);
  margin-top: var(--md-h6-margin-top);
  margin-bottom: var(--md-h6-margin-bottom);
}

.md-preview p {
  font-family: var(--md-paragraph-font-family);
  font-size: var(--md-paragraph-font-size);
  color: var(--md-paragraph-color);
  line-height: var(--md-paragraph-line-height);
  margin: var(--md-paragraph-margin-top) 0 var(--md-paragraph-margin-bottom);
}

/* The first block has nothing above it to be spaced from. */
.md-preview > *:first-child {
  margin-top: 0;
}

.md-preview a {
  color: var(--md-link-color, var(--md-global-link-color));
  text-decoration: var(--md-link-underline);
  font-weight: var(--md-link-bold);
  font-style: var(--md-link-italic);
}

.md-preview a:hover {
  color: var(--md-link-hover-color);
}

.md-preview ul,
.md-preview ol {
  font-size: var(--md-list-font-size);
  color: var(--md-list-color);
  padding-left: var(--md-list-indent);
  margin: var(--md-global-paragraph-spacing) 0 0;
}

.md-preview li {
  margin-bottom: var(--md-list-item-spacing);
}

.md-preview blockquote {
  background: var(--md-blockquote-bg-color);
  border-left: 4px solid var(--md-blockquote-border-color);
  color: var(--md-blockquote-text-color);
  font-style: var(--md-blockquote-font-style);
  padding: 8px 14px;
  margin: var(--md-global-paragraph-spacing) 0 0;
}

.md-preview code {
  background: var(--md-inline-code-bg-color);
  color: var(--md-inline-code-text-color);
  padding: 2px 5px;
  border-radius: var(--md-inline-code-border-radius);
  font-family: var(--md-inline-code-font-family);
  font-size: var(--md-inline-code-font-size);
}

.md-preview pre {
  background: var(--md-code-block-bg-color);
  border: 1px solid var(--md-code-block-border-color);
  color: var(--md-code-block-text-color);
  padding: 12px;
  border-radius: 4px;
  overflow-x: auto;
  margin: var(--md-global-paragraph-spacing) 0 0;
  font-family: var(--md-code-block-font-family);
  font-size: var(--md-code-block-font-size);
}

.md-preview pre code {
  background: none;
  color: inherit;
  padding: 0;
  font-family: inherit;
  font-size: inherit;
  border-radius: 0;
}

.md-preview pre code [data-tok="kw"]  { color: #d12f6b; font-weight: bold; }
.md-preview pre code [data-tok="fn"]  { color: #0a7f3f; }
.md-preview pre code [data-tok="str"] { color: #a67f00; }
.md-preview pre code [data-tok="num"] { color: #5a3ec8; }

.md-preview[style*="--md-code-block-syntax-theme: github-dark"] pre code [data-tok="kw"]  { color: #ff7aa8; }
.md-preview[style*="--md-code-block-syntax-theme: github-dark"] pre code [data-tok="fn"]  { color: #6ec48a; }
.md-preview[style*="--md-code-block-syntax-theme: github-dark"] pre code [data-tok="str"] { color: #e4c368; }
.md-preview[style*="--md-code-block-syntax-theme: github-dark"] pre code [data-tok="num"] { color: #b39dff; }

.md-preview table {
  border-collapse: collapse;
  width: auto;
  max-width: 100%;
  margin: var(--md-global-paragraph-spacing) 0 0;
  font-size: var(--md-table-font-size);
}

.md-preview th,
.md-preview td {
  border: 1px solid var(--md-table-border-color);
  padding: 6px 10px;
  text-align: left;
}

.md-preview th {
  background: var(--md-table-header-bg);
  font-weight: bold;
}

.md-preview tr:nth-child(even) td {
  background: var(--md-table-row-stripe-bg);
}

.md-preview img {
  max-width: var(--md-image-max-width);
  height: auto;
  border: 1px solid var(--md-image-border-color);
  border-radius: var(--md-image-border-radius);
  box-shadow: var(--md-image-shadow);
}

.md-preview hr {
  border: 0;
  border-top: var(--md-hr-thickness) solid var(--md-hr-color);
  margin: var(--md-hr-spacing) 0 0;
}

/* ---------- Help Panel ---------- */

.settings-panel h3 {
  margin-top: 18px;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--text);
}

.settings-panel p,
.settings-panel ul {
  font-size: 12px;
  line-height: 1.5;
  margin: 0 0 6px;
  color: var(--text);
}

.settings-panel ul {
  padding-left: 18px;
}

.settings-panel li {
  margin-bottom: 3px;
}

.md-preview .section-number,
.section-number {
  color: #888;
  font-weight: normal;
  margin-right: 0.25em;
}

.md-preview-error {
  color: #b00020;
  padding: 12px;
  border: 1px solid #b00020;
  border-radius: 4px;
  background: rgba(176, 0, 32, 0.05);
}

/* ---------- Style Editor Modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal.hidden {
  display: none !important;
}

.modal-content {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-content.style-editor {
  width: 92vw;
  height: 90vh;
  max-width: 1400px;
}

.style-editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--tab-bg);
  flex-shrink: 0;
}

.style-editor-header h2 {
  margin: 0;
  font-size: 18px;
}

.close-btn-x {
  background: transparent;
  border: none;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
  padding: 4px 10px;
}

.close-btn-x:hover {
  background: rgba(128, 128, 128, 0.15);
  border-radius: 4px;
}

.style-editor-body {
  display: flex;
  flex: 1;
  min-height: 0;
}

.style-editor-left {
  width: 45%;
  min-width: 380px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  min-height: 0;
}

.style-editor-toolbar {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--preview-bg);
  flex-shrink: 0;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.toolbar-row:last-child {
  margin-bottom: 0;
}

.toolbar-row label {
  min-width: 60px;
  font-size: 13px;
}

.toolbar-row select {
  flex: 1;
  padding: 4px 6px;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}

.toolbar-buttons {
  flex-wrap: wrap;
}

.toolbar-buttons button {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
  font-size: 12px;
}

.style-active-label {
  font-size: 12px;
  color: #888;
  margin-top: 6px;
  font-style: italic;
  min-height: 16px;
}

.style-editor-form {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
}

.style-editor-form details {
  border: 1px solid var(--border);
  border-radius: 4px;
  margin-bottom: 8px;
  background: var(--bg);
}

.style-editor-form summary {
  padding: 8px 12px;
  cursor: pointer;
  font-weight: bold;
  font-size: 13px;
  user-select: none;
  background: var(--tab-bg);
  border-radius: 4px;
}

.style-editor-form details[open] summary {
  border-bottom: 1px solid var(--border);
  border-radius: 4px 4px 0 0;
}

.style-editor-form .field-grid {
  padding: 10px 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
}

.style-editor-form .field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.style-editor-form .field.field-full {
  grid-column: 1 / -1;
}

.style-editor-form .field label {
  font-size: 11px;
  color: #666;
  text-transform: capitalize;
}

.style-editor-form .field input[type="text"],
.style-editor-form .field input[type="number"],
.style-editor-form .field select {
  padding: 4px 6px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 3px;
  width: 100%;
  box-sizing: border-box;
}

.style-editor-form .field input[type="color"] {
  width: 100%;
  height: 28px;
  padding: 2px;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 3px;
  cursor: pointer;
}

.style-editor-form .field.field-checkbox {
  flex-direction: row;
  align-items: center;
  gap: 6px;
}

.style-editor-form .field.field-checkbox label {
  font-size: 12px;
  text-transform: none;
  color: var(--text);
}

.style-editor-form .color-row {
  display: flex;
  gap: 6px;
  align-items: center;
}

.style-editor-form .color-row input[type="color"] {
  width: 36px;
  height: 28px;
  flex-shrink: 0;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 3px;
  cursor: pointer;
}

.style-editor-form .color-row input[type="text"] {
  flex: 1;
}

.style-editor-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--preview-bg);
  flex-shrink: 0;
}

.style-editor-footer button {
  width: 100%;
}

.style-editor-right {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  min-height: 0;
}

.style-editor-right h3 {
  margin: 0 0 8px;
  font-size: 14px;
  color: var(--text);
}

.style-editor-preview {
  position: relative !important;
  display: block !important;
  top: auto !important;
  right: auto !important;
  bottom: auto !important;
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 4px;
  min-height: 0;
}

/* -----------------------------------------------------------
   SPALTENAUSRICHTUNG (Tabelleneditor + Outline-Kontextmenü)
----------------------------------------------------------- */

.align-bar {
  display: flex;
  gap: 2px;
  padding: 2px;
  background: var(--preview-bg);
  border: 1px solid var(--border);
  border-radius: 3px;
}

.align-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 0;
  border: none;
  border-radius: 2px;
  background: transparent;
  color: var(--text);
  opacity: 0.5;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.align-btn:hover {
  opacity: 1;
  background: var(--tab-active);
}

.align-btn:focus-visible {
  outline: 2px solid #4a90e2;
  outline-offset: 1px;
  opacity: 1;
}

.align-btn.active {
  opacity: 1;
  background: #4a90e2;
  color: #fff;
}
