:root {
  --bg-app: #e8ecf1;
  --bg-toolbar: #1e3a5f;
  --bg-toolbar-hover: #2a4d7a;
  --bg-page: #ffffff;
  --text-primary: #1a1a2e;
  --text-muted: #64748b;
  --border: #d1d9e6;
  --accent: #3b82f6;
  --shadow-page: 0 2px 12px rgba(0, 0, 0, 0.08), 0 8px 32px rgba(0, 0, 0, 0.06);
  --font-ui: "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-doc: "Georgia", "Times New Roman", serif;
  --page-width: 210mm;
  --page-min-height: 297mm;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-ui);
  background: var(--bg-app);
  color: var(--text-primary);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Toolbar ── */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: var(--bg-toolbar);
  color: #fff;
  flex-shrink: 0;
  z-index: 10;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 2px;
}

.toolbar-brand {
  margin-right: 8px;
}

.logo {
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.doc-title {
  margin-left: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.85rem;
  opacity: 0.9;
  min-width: 120px;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  outline: none;
}

.doc-title:hover,
.doc-title:focus {
  background: rgba(255, 255, 255, 0.1);
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  background: rgba(255, 255, 255, 0.2);
  margin: 0 4px;
}

.toolbar-spacer {
  flex: 1;
}

.toolbar button,
.toolbar select {
  background: transparent;
  border: none;
  color: #fff;
  padding: 6px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  font-family: inherit;
  line-height: 1;
  min-height: 32px;
}

.toolbar button:hover,
.toolbar select:hover {
  background: var(--bg-toolbar-hover);
}

.toolbar button.is-active {
  background: var(--accent);
}

.toolbar select {
  padding-right: 4px;
}

/* ── Workspace ── */
.workspace {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.panel {
  flex: 1;
  overflow: auto;
}

.editor-panel {
  display: flex;
  justify-content: center;
  padding: 32px 24px 64px;
  background: var(--bg-app);
}

.page {
  width: var(--page-width);
  min-height: var(--page-min-height);
  background: var(--bg-page);
  box-shadow: var(--shadow-page);
  padding: 25mm 20mm;
}

#editor {
  min-height: calc(var(--page-min-height) - 50mm);
  cursor: text;
}

#editor .ProseMirror {
  min-height: calc(var(--page-min-height) - 50mm);
  outline: none;
}

.md-panel {
  display: flex;
  flex-direction: column;
  max-width: 40%;
  border-left: 1px solid var(--border);
  background: #f8fafc;
}

.md-panel.hidden {
  display: none;
}

.md-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: #eef2f7;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 600;
}

.md-panel-header button {
  padding: 4px 12px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
}

#md-source {
  flex: 1;
  border: none;
  resize: none;
  padding: 16px;
  font-family: "Cascadia Code", "Consolas", monospace;
  font-size: 0.85rem;
  line-height: 1.6;
  background: #f8fafc;
  color: var(--text-primary);
  outline: none;
}

/* ── Status bar ── */
.statusbar {
  display: flex;
  justify-content: space-between;
  padding: 4px 16px;
  background: #f1f5f9;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* ── TipTap / ProseMirror content ── */
.ProseMirror {
  font-family: var(--font-doc);
  font-size: 12pt;
  line-height: 1.6;
  color: var(--text-primary);
  outline: none;
}

.ProseMirror > * + * {
  margin-top: 0.75em;
}

.ProseMirror h1 {
  font-size: 2em;
  font-weight: 700;
  margin-top: 1.2em;
  margin-bottom: 0.4em;
  line-height: 1.2;
}

.ProseMirror h2 {
  font-size: 1.5em;
  font-weight: 700;
  margin-top: 1em;
  margin-bottom: 0.35em;
}

.ProseMirror h3 {
  font-size: 1.25em;
  font-weight: 600;
  margin-top: 0.8em;
  margin-bottom: 0.3em;
}

.ProseMirror p {
  margin: 0;
}

.ProseMirror ul,
.ProseMirror ol {
  padding-left: 1.5em;
}

.ProseMirror blockquote {
  border-left: 4px solid var(--accent);
  padding-left: 1em;
  color: var(--text-muted);
  font-style: italic;
}

.ProseMirror pre {
  background: #f1f5f9;
  border-radius: 6px;
  padding: 12px 16px;
  font-family: "Cascadia Code", "Consolas", monospace;
  font-size: 0.9em;
  overflow-x: auto;
}

.ProseMirror code {
  background: #f1f5f9;
  border-radius: 3px;
  padding: 0.15em 0.35em;
  font-family: "Cascadia Code", "Consolas", monospace;
  font-size: 0.9em;
}

.ProseMirror pre code {
  background: none;
  padding: 0;
}

.ProseMirror hr {
  border: none;
  border-top: 2px solid var(--border);
  margin: 1.5em 0;
}

.ProseMirror a {
  color: var(--accent);
  text-decoration: underline;
}

.ProseMirror img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  display: block;
  margin: 0.5em auto;
}

.ProseMirror img.ProseMirror-selectednode {
  outline: 3px solid var(--accent);
}

/* Tables */
.ProseMirror table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
  overflow: hidden;
  table-layout: fixed;
}

.ProseMirror td,
.ProseMirror th {
  border: 1px solid var(--border);
  padding: 8px 12px;
  vertical-align: top;
  position: relative;
  min-width: 60px;
}

.ProseMirror th {
  background: #f1f5f9;
  font-weight: 600;
  text-align: left;
}

.ProseMirror .selectedCell::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(59, 130, 246, 0.15);
  pointer-events: none;
}

.ProseMirror .column-resize-handle {
  position: absolute;
  right: -2px;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--accent);
  cursor: col-resize;
}

.ProseMirror.resize-cursor {
  cursor: col-resize;
}

/* Placeholder */
.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: #adb5bd;
  pointer-events: none;
  height: 0;
}

/* ── Error banner ── */
.error-banner {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 90%;
  padding: 12px 20px;
  background: #dc2626;
  color: #fff;
  border-radius: 8px;
  font-size: 0.9rem;
  z-index: 100;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.error-banner.hidden {
  display: none;
}

/* ── Print / PDF ── */
@media print {
  body {
    background: #fff;
  }

  .toolbar,
  .statusbar,
  .md-panel {
    display: none !important;
  }

  .workspace,
  .editor-panel {
    padding: 0;
    overflow: visible;
  }

  .page {
    box-shadow: none;
    width: 100%;
    min-height: auto;
    padding: 0;
    margin: 0;
  }
}
