:root {
  --bg: #0f1117;
  --surface: #161b26;
  --surface-2: #1c2333;
  --border: #2a3348;
  --text: #e8ecf4;
  --text-muted: #8b95a8;
  --accent: #5e9fd4;
  --accent-hover: #7eb3e0;
  --accent-glow: rgba(94, 159, 212, 0.25);
  --success: #6fcf97;
  --danger: #e06c75;
  --radius: 10px;
  --font: "DM Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-icon {
  font-size: 1.5rem;
  color: var(--accent);
  filter: drop-shadow(0 0 8px var(--accent-glow));
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.brand-sub {
  margin: 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.toolbar {
  display: flex;
  gap: 0.5rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  border: none;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

.btn:active {
  transform: scale(0.98);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

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

.btn-primary {
  background: linear-gradient(135deg, #4a8fc9, #5e9fd4);
  color: #0f1117;
  box-shadow: 0 2px 12px var(--accent-glow);
}

.btn-primary:hover {
  background: linear-gradient(135deg, #5a9fd4, #7eb3e0);
  box-shadow: 0 4px 20px var(--accent-glow);
}

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

.btn-icon {
  font-size: 1rem;
  line-height: 1;
}

/* Workspace */
.workspace {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.pane-editor {
  flex: 1 1 50%;
  background: var(--surface);
  display: flex;
  flex-direction: column;
}

.pane-preview {
  flex: 1 1 50%;
  background: #fafbfd;
  color: #1a1f2e;
}

.pane-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  flex-shrink: 0;
}

.pane-editor .pane-header {
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}

.pane-preview .pane-header {
  color: #6b7280;
  border-bottom: 1px solid #e5e9f0;
  background: #f4f6fa;
}

.pane-hint {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  opacity: 0.7;
}

.resizer {
  width: 5px;
  cursor: col-resize;
  background: var(--border);
  flex-shrink: 0;
  transition: background 0.15s;
}

.resizer:hover,
.resizer.active {
  background: var(--accent);
}

/* Uploads panel */
.uploads-panel {
  flex-shrink: 0;
  max-height: 140px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.uploads-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0.75rem;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.uploads-hint code {
  font-family: var(--mono);
  font-size: 0.65rem;
  color: var(--accent);
  background: transparent;
}

.uploads-list {
  list-style: none;
  margin: 0;
  padding: 0 0.5rem 0.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.upload-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.5rem;
  background: var(--surface);
  border-radius: 6px;
  border: 1px solid var(--border);
}

.upload-thumb {
  width: 36px;
  height: 36px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
}

.upload-meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.upload-id {
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--accent);
}

.upload-syntax {
  font-family: var(--mono);
  font-size: 0.65rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: transparent;
}

.upload-actions {
  display: flex;
  gap: 0.25rem;
  flex-shrink: 0;
}

.btn-mini {
  padding: 0.2rem 0.45rem;
  font-size: 0.65rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: pointer;
}

.btn-mini:hover {
  color: var(--text);
  border-color: var(--accent);
}

/* CodeMirror */
.CodeMirror {
  flex: 1;
  min-height: 0;
  height: auto !important;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
}

.pane-editor .CodeMirror-scroll {
  min-height: 100%;
}

/* PDF preview pane */
.preview-viewport {
  flex: 1;
  min-height: 0;
  position: relative;
  background: #525659;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-empty {
  color: #c5cad3;
  font-size: 0.9rem;
  text-align: center;
  padding: 2rem;
}

.preview-pane-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  background: rgba(30, 36, 48, 0.85);
  color: #e8ecf4;
  font-size: 0.875rem;
  z-index: 2;
}

.preview-pane-loading[hidden] {
  display: none !important;
}

.preview-frame {
  width: 100%;
  height: 100%;
  border: none;
  background: #525659;
}

.preview-frame.is-loading {
  opacity: 0.35;
}

.preview-frame[hidden],
.preview-embed[hidden] {
  display: none !important;
}

.preview-embed {
  width: 100%;
  height: 100%;
  border: none;
  background: #525659;
}

/* Legacy preview content (unused) */
.preview-content {
  flex: 1;
  overflow: auto;
  padding: 1.5rem 2rem 3rem;
  max-width: 52rem;
  line-height: 1.65;
}

.preview-content h1,
.preview-content h2,
.preview-content h3 {
  color: #111827;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-top: 1.5em;
  margin-bottom: 0.5em;
}

.preview-content h1 { font-size: 1.75rem; border-bottom: 2px solid #e5e9f0; padding-bottom: 0.3em; }
.preview-content h2 { font-size: 1.35rem; }
.preview-content h3 { font-size: 1.1rem; }

.preview-content p { margin: 0.75em 0; }

.preview-content a {
  color: #2563eb;
  text-decoration: none;
}

.preview-content a:hover { text-decoration: underline; }

.preview-content code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: #eef1f6;
  padding: 0.15em 0.4em;
  border-radius: 4px;
  color: #374151;
}

.preview-content pre {
  background: #1e2433;
  color: #e8ecf4;
  padding: 1rem 1.15rem;
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.5;
}

.preview-content pre code {
  background: none;
  padding: 0;
  color: inherit;
}

.preview-content blockquote {
  margin: 1em 0;
  padding: 0.6em 1em;
  border-left: 4px solid #5e9fd4;
  background: #f0f6fc;
  color: #374151;
  border-radius: 0 6px 6px 0;
}

.preview-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 1em 0;
  font-size: 0.9rem;
}

.preview-content th,
.preview-content td {
  border: 1px solid #e5e9f0;
  padding: 0.5em 0.75em;
  text-align: left;
}

.preview-content th {
  background: #f4f6fa;
  font-weight: 600;
}

.preview-content tr:nth-child(even) td {
  background: #fafbfd;
}

.preview-content img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1em auto;
}

.preview-content .mermaid {
  margin: 1.5em 0;
  text-align: center;
}

.preview-content .preview-error {
  color: #cf222e;
  padding: 1rem;
}

/* PDF-like title page & TOC in preview */
.preview-titlepage {
  position: relative;
  background: var(--tp-bg, #2e3440);
  color: var(--tp-text, #eceff4);
  border-radius: 8px;
  min-height: 280px;
  margin: 0 0 2rem;
  padding: 2.5rem 2rem 2rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
}

.preview-titlepage-inner {
  max-width: 100%;
}

.preview-titlepage-rule {
  width: 4rem;
  height: 3px;
  background: var(--tp-rule, #88c0d0);
  margin-bottom: 1.25rem;
  border-radius: 2px;
}

.preview-titlepage-title {
  margin: 0 0 0.5rem !important;
  padding: 0 !important;
  border: none !important;
  font-size: 1.65rem !important;
  font-weight: 700 !important;
  color: var(--tp-text, #eceff4) !important;
  line-height: 1.25;
}

.preview-titlepage-subtitle {
  margin: 0 0 1.25rem;
  font-size: 1.05rem;
  opacity: 0.9;
}

.preview-titlepage-author,
.preview-titlepage-date {
  margin: 0.35rem 0 0;
  font-size: 0.9rem;
  opacity: 0.85;
}

.preview-page-label {
  position: absolute;
  top: 0.5rem;
  right: 0.75rem;
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.45;
  font-weight: 600;
}

.preview-toc {
  position: relative;
  margin: 0 0 2rem;
  padding: 1.25rem 1.5rem 1.5rem;
  background: #f4f6fa;
  border: 1px solid #e5e9f0;
  border-radius: 8px;
}

.preview-toc-own-page {
  min-height: 200px;
  margin-bottom: 2.5rem;
  page-break-after: always;
}

.preview-toc-heading {
  margin: 0 0 1rem !important;
  padding: 0 0 0.5rem !important;
  border: none !important;
  font-size: 1.25rem !important;
  color: #111827 !important;
  border-bottom: 2px solid #d8dee9 !important;
}

.preview-toc-list {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: none;
}

.preview-toc-list li {
  margin: 0.35rem 0;
  line-height: 1.4;
}

.preview-toc-list a {
  color: #374151;
  text-decoration: none;
  display: inline-flex;
  gap: 0.35rem;
  align-items: baseline;
}

.preview-toc-list a:hover {
  color: #2563eb;
  text-decoration: underline;
}

.preview-toc-list .toc-num {
  font-variant-numeric: tabular-nums;
  color: #6b7280;
  min-width: 1.5rem;
}

.preview-toc-list .toc-level-1 {
  font-weight: 600;
  margin-top: 0.5rem;
}

.preview-toc-list .toc-level-2 {
  padding-left: 1.25rem;
  font-size: 0.95rem;
}

.preview-toc-list .toc-level-3 {
  padding-left: 2.5rem;
  font-size: 0.9rem;
  color: #6b7280;
}

.preview-toc .preview-page-label {
  color: #9ca3af;
}

/* GitHub alerts in preview */
.preview-content .markdown-alert {
  padding: 0.75em 1em;
  margin: 1em 0;
  border-radius: 6px;
  border-left: 4px solid;
}

.preview-content .markdown-alert-note {
  background: #ddf4ff;
  border-color: #0969da;
}

.preview-content .markdown-alert-tip {
  background: #dafbe1;
  border-color: #1a7f37;
}

.preview-content .markdown-alert-important {
  background: #fbefff;
  border-color: #8250df;
}

.preview-content .markdown-alert-warning {
  background: #fff8c5;
  border-color: #9a6700;
}

.preview-content .markdown-alert-caution {
  background: #ffebe9;
  border-color: #cf222e;
}

/* Toast & loading – [hidden] darf nicht von display:flex überschrieben werden */
.toast[hidden],
.loading-overlay[hidden] {
  display: none !important;
}

.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  z-index: 100;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  max-width: 90vw;
}

.toast.error {
  background: #3d1f24;
  color: #f5a8ae;
  border: 1px solid var(--danger);
}

.toast.success {
  background: #1a2e24;
  color: var(--success);
  border: 1px solid var(--success);
}

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 23, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  backdrop-filter: blur(4px);
}

.loading-overlay p {
  margin: 1rem 0 0.25rem;
  font-weight: 500;
}

.loading-overlay small {
  color: var(--text-muted);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 768px) {
  .workspace { flex-direction: column; }
  .resizer { width: 100%; height: 5px; cursor: row-resize; }
  .pane-editor, .pane-preview { flex: 1 1 45%; }
}
