:root {
  --bg: #f6f3ec;
  --bg-tint: #efeadf;
  --paper: #ffffff;
  --ink: #14110d;
  --ink-soft: #4a443b;
  --ink-mute: #837b6e;
  --line: #e2dccf;
  --line-soft: #ece6d9;
  --accent: #1f3a3d;
  --accent-soft: #2a5054;
  --code-bg: #f5f1e8;
  --shadow: 0 1px 0 rgba(20, 17, 13, 0.04),
    0 24px 48px -24px rgba(20, 17, 13, 0.18);

  --font-serif: "Fraunces", "Iowan Old Style", "Apple Garamond", Georgia,
    "Times New Roman", serif;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: var(--font-sans);
  font-size: 14px;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow: hidden;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  gap: 24px;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 9px;
  background: var(--ink);
  color: var(--bg);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.02em;
  flex-shrink: 0;
}

.brand-text h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 20px;
  letter-spacing: -0.01em;
  line-height: 1.1;
}

.brand-text h1 em {
  font-style: italic;
  font-weight: 400;
  color: var(--ink-mute);
}

.brand-text p {
  margin: 2px 0 0;
  color: var(--ink-mute);
  font-size: 12px;
  letter-spacing: 0.01em;
}

.actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.filename-field {
  display: inline-flex;
  align-items: center;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px 0 12px;
  height: 36px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.filename-field:focus-within {
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(20, 17, 13, 0.08);
}

.filename-field input {
  border: 0;
  outline: 0;
  background: transparent;
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  width: 140px;
  padding: 0;
}

.filename-suffix {
  color: var(--ink-mute);
  font-size: 13px;
  margin-left: 2px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.01em;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background-color 0.15s ease, border-color 0.15s ease,
    color 0.15s ease, transform 0.05s ease, box-shadow 0.15s ease;
  white-space: nowrap;
  user-select: none;
}

.btn:active {
  transform: translateY(1px);
}

.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
  border-color: var(--line);
}

.btn-ghost:hover {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--ink-mute);
}

.btn-primary {
  background: var(--ink);
  color: var(--bg);
  border-color: var(--ink);
}

.btn-primary:hover {
  background: var(--accent);
  border-color: var(--accent);
}

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

/* ---------- Workspace ---------- */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  min-height: 0;
}

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

.pane-editor {
  border-right: 1px solid var(--line);
  background: var(--bg);
}

.pane-preview {
  background: var(--bg-tint);
}

.pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line-soft);
  flex-shrink: 0;
}

.pane-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--ink-mute);
  font-weight: 600;
}

.pane-meta {
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--ink-mute);
}

#editor {
  flex: 1;
  width: 100%;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  padding: 32px 36px 64px;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.7;
  color: var(--ink);
  caret-color: var(--accent-soft);
  tab-size: 2;
}

#editor::placeholder {
  color: var(--ink-mute);
  opacity: 0.7;
}

.preview-scroll {
  flex: 1;
  overflow: auto;
  padding: 32px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

/* ---------- Page (preview + PDF) ---------- */
.page {
  background: var(--paper);
  width: 100%;
  max-width: 760px;
  min-height: 1040px;
  padding: 64px 72px;
  border-radius: 4px;
  box-shadow: var(--shadow);
  font-family: var(--font-serif);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.7;
  font-feature-settings: "kern", "liga", "onum";
}

.page:empty::before {
  content: "Your document will appear here.";
  color: var(--ink-mute);
  font-style: italic;
}

.page > *:first-child {
  margin-top: 0;
}

.page > *:last-child {
  margin-bottom: 0;
}

.page h1,
.page h2,
.page h3,
.page h4,
.page h5,
.page h6 {
  font-family: var(--font-serif);
  color: var(--ink);
  letter-spacing: -0.01em;
  line-height: 1.2;
  margin: 1.6em 0 0.5em;
  font-weight: 600;
  page-break-after: avoid;
  break-after: avoid;
}

.page h1 {
  font-size: 2.25em;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-top: 0;
  padding-bottom: 0.35em;
  border-bottom: 1px solid var(--line);
}

.page h2 {
  font-size: 1.55em;
  font-weight: 600;
}

.page h3 {
  font-size: 1.25em;
  font-weight: 600;
  font-style: italic;
  color: var(--ink-soft);
}

.page h4 {
  font-size: 1.05em;
  font-weight: 600;
}

.page h5,
.page h6 {
  font-size: 0.95em;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
}

.page p {
  margin: 0 0 1em;
  hyphens: auto;
  text-wrap: pretty;
}

.page p + p {
  margin-top: 0;
}

.page a {
  color: var(--accent-soft);
  text-decoration: none;
  background-image: linear-gradient(currentColor, currentColor);
  background-size: 100% 1px;
  background-position: 0 95%;
  background-repeat: no-repeat;
  padding-bottom: 1px;
}

.page a:hover {
  color: var(--accent);
}

.page strong {
  font-weight: 600;
}

.page em {
  font-style: italic;
}

.page small {
  font-size: 0.85em;
  color: var(--ink-soft);
}

.page del {
  color: var(--ink-mute);
}

.page ul,
.page ol {
  margin: 0 0 1em;
  padding-left: 1.5em;
}

.page li {
  margin-bottom: 0.35em;
}

.page li::marker {
  color: var(--ink-mute);
}

.page li > p {
  margin-bottom: 0.35em;
}

.page ul ul,
.page ol ol,
.page ul ol,
.page ol ul {
  margin: 0.35em 0 0.35em;
}

.page blockquote {
  margin: 1.25em 0;
  padding: 0.25em 0 0.25em 1.25em;
  border-left: 3px solid var(--line);
  color: var(--ink-soft);
  font-style: italic;
  page-break-inside: avoid;
  break-inside: avoid;
}

.page blockquote p:last-child {
  margin-bottom: 0;
}

.page hr {
  border: 0;
  height: 1px;
  background: var(--line);
  margin: 2em 0;
}

.page code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  border: 1px solid var(--line-soft);
}

.page pre {
  background: var(--code-bg);
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  padding: 1em 1.15em;
  overflow: auto;
  margin: 1.25em 0;
  font-size: 0.85em;
  line-height: 1.55;
  page-break-inside: avoid;
  break-inside: avoid;
}

.page pre code {
  background: transparent;
  border: 0;
  padding: 0;
  font-size: 1em;
  display: block;
  white-space: pre;
}

.page table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.25em 0;
  font-size: 0.95em;
  page-break-inside: avoid;
  break-inside: avoid;
}

.page thead th {
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid var(--ink);
  padding: 0.55em 0.75em;
}

.page tbody td {
  padding: 0.55em 0.75em;
  border-bottom: 1px solid var(--line);
}

.page tbody tr:last-child td {
  border-bottom: 0;
}

.page tbody tr:nth-child(even) td {
  background: var(--bg-tint);
}

.page img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin: 1em 0;
  display: block;
}

.page input[type="checkbox"] {
  margin-right: 0.4em;
  transform: translateY(1px);
}

.page .task-list-item {
  list-style: none;
  margin-left: -1.4em;
}

/* ---------- Footnote ---------- */
.footnote {
  text-align: center;
  padding: 14px;
  font-size: 11px;
  color: var(--ink-mute);
  border-top: 1px solid var(--line);
  letter-spacing: 0.04em;
  background: var(--bg);
  flex-shrink: 0;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 56px;
  transform: translate(-50%, 12px);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 12px;
  letter-spacing: 0.02em;
  box-shadow: 0 12px 32px -12px rgba(20, 17, 13, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 50;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- Scrollbars ---------- */
.preview-scroll::-webkit-scrollbar,
#editor::-webkit-scrollbar,
.page pre::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

.preview-scroll::-webkit-scrollbar-thumb,
#editor::-webkit-scrollbar-thumb,
.page pre::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: padding-box;
}

.preview-scroll::-webkit-scrollbar-thumb:hover,
#editor::-webkit-scrollbar-thumb:hover,
.page pre::-webkit-scrollbar-thumb:hover {
  background: var(--ink-mute);
  background-clip: padding-box;
  border: 2px solid transparent;
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  body {
    overflow: auto;
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
    padding: 16px;
  }

  .actions {
    justify-content: flex-start;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .pane-editor {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  #editor {
    min-height: 320px;
    padding: 20px;
  }

  .preview-scroll {
    padding: 20px;
  }

  .page {
    padding: 36px 28px;
    min-height: auto;
  }
}

/* ---------- Print / PDF ---------- */
@page {
  size: A4;
  margin: 18mm 18mm 20mm 18mm;
}

@media print {
  html,
  body {
    background: #ffffff !important;
    overflow: visible !important;
    height: auto !important;
    min-height: 0 !important;
    color: #000;
  }

  body {
    display: block !important;
  }

  .topbar,
  .footnote,
  .pane-editor,
  .pane-header,
  .toast {
    display: none !important;
  }

  .workspace {
    display: block !important;
    grid-template-columns: none !important;
  }

  .pane,
  .pane-preview {
    display: block !important;
    background: #ffffff !important;
    border: 0 !important;
    min-height: 0 !important;
  }

  .preview-scroll {
    display: block !important;
    overflow: visible !important;
    padding: 0 !important;
    margin: 0 !important;
  }

  .page {
    box-shadow: none !important;
    border: 0 !important;
    border-radius: 0 !important;
    width: 100% !important;
    max-width: none !important;
    min-height: 0 !important;
    padding: 0 !important;
    margin: 0 !important;
    background: #ffffff !important;
    color: #14110d !important;
    font-size: 11pt;
    line-height: 1.55;
  }

  .page h1,
  .page h2,
  .page h3,
  .page h4,
  .page h5,
  .page h6 {
    page-break-after: avoid;
    break-after: avoid;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .page p,
  .page li {
    orphans: 3;
    widows: 3;
  }

  .page pre,
  .page blockquote,
  .page table,
  .page figure,
  .page img {
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .page tr {
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .page thead {
    display: table-header-group;
  }

  .page a {
    color: #1f3a3d !important;
    text-decoration: underline;
    background: none !important;
  }

  .page pre,
  .page code {
    background: #f5f1e8 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .page tbody tr:nth-child(even) td {
    background: #f6f3ec !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}
