:root {
  --paper: #f4efe4;
  --paper-2: #ece5d6;
  --ink: #1c1a17;
  --ink-soft: #4a463e;
  --carbon: #181614;
  --carbon-2: #211e1b;
  --line: #cfc6b3;
  --accent: #d83a14; /* proofreader's vermilion */
  --accent-deep: #b22f0f;
  --green: #2c7a3f;
  --amber: #d99114;
  --code-bg: #f6f8fa;
  --code-border: #d0d7de;
  --shadow: 0 22px 50px -18px rgba(28, 26, 23, 0.45);
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "Hanken Grotesk", system-ui, sans-serif;
  --serif: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 15% -10%, #fbf7ee 0%, transparent 55%),
    radial-gradient(1000px 700px at 110% 120%, #efe7d6 0%, transparent 50%),
    var(--paper);
  min-height: 100vh;
  overflow-x: hidden;
}

/* The app occupies exactly the first screen (its own flex column with internal
   scroll); the SEO content flows below it and the page scrolls normally. */
.app-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* film grain overlay */
.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9;
  opacity: 0.04;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ---- top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 26px;
  border-bottom: 1px solid var(--line);
  background: rgba(244, 239, 228, 0.7);
  backdrop-filter: blur(6px);
  z-index: 5;
}
.brand { display: flex; align-items: baseline; gap: 16px; }
.brand-mark {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 22px;
  letter-spacing: -0.5px;
  color: var(--ink);
}
.brand-mark .arrow { color: var(--accent); margin: 0 1px; }
.brand-tag {
  font-family: var(--serif);
  font-style: italic;
  font-size: 15px;
  color: var(--ink-soft);
}
.topbar-meta { display: flex; align-items: center; gap: 18px; }
.stamp {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 2px;
  color: var(--accent);
  border: 1.5px solid var(--accent);
  padding: 4px 9px;
  border-radius: 3px;
  transform: rotate(-3deg);
  opacity: 0.85;
}
.ghost-link {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--ink-soft);
  text-decoration: none;
  border-bottom: 1px dotted var(--ink-soft);
}
.ghost-link:hover { color: var(--accent); }

/* ---- stage layout ---- */
.stage {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 200px 1fr;
  min-height: 0;
  padding: 22px;
  gap: 18px;
}
.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-radius: 10px;
  overflow: hidden;
  animation: rise 0.6s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}
.editor-panel { animation-delay: 0.05s; }
.preview-panel { animation-delay: 0.18s; }
@keyframes rise {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: translateY(0); }
}

.panel-head {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 14px;
  font-family: var(--mono);
  font-size: 12px;
}
.editor-panel .panel-head { background: var(--carbon-2); color: #cfc6b3; }
.preview-panel .panel-head {
  background: var(--paper-2);
  color: var(--ink-soft);
  border: 1px solid var(--line);
  border-bottom: none;
}
.panel-title { letter-spacing: 0.5px; }
.dot { width: 11px; height: 11px; border-radius: 50%; display: inline-block; }
.dot-r { background: #ff5f56; } .dot-y { background: #ffbd2e; } .dot-g { background: #27c93f; }
.panel-actions { margin-left: auto; display: flex; gap: 6px; }
.mini {
  font-family: var(--mono);
  font-size: 11px;
  color: #cfc6b3;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 5px;
  padding: 4px 8px;
  cursor: pointer;
  transition: all 0.15s;
}
.mini:hover { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---- editor ---- */
.editor {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  padding: 20px 22px;
  background: var(--carbon);
  color: #e8e2d4;
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.7;
  tab-size: 2;
}
.editor::placeholder { color: #6b655a; }
.editor::selection { background: rgba(216, 58, 20, 0.35); }
.editor-foot {
  display: flex;
  justify-content: space-between;
  padding: 8px 14px;
  background: var(--carbon-2);
  font-family: var(--mono);
  font-size: 11px;
}
.muted { color: #8a8377; }
.editor-foot .muted { color: #6f6a5e; }

/* ---- spine / controls ---- */
.spine {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-top: 6px;
}
.convert {
  position: relative;
  font-family: var(--sans);
  font-weight: 700;
  font-size: 16px;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 9px;
  padding: 16px 14px;
  cursor: pointer;
  box-shadow: 0 10px 24px -10px rgba(216, 58, 20, 0.8);
  transition: transform 0.12s, box-shadow 0.2s, background 0.2s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.convert:hover { background: var(--accent-deep); transform: translateY(-1px); }
.convert:active { transform: translateY(1px); }
.convert.busy { background: var(--ink-soft); cursor: progress; }
.convert-sub { font-family: var(--mono); font-size: 11px; opacity: 0.8; font-weight: 500; }
.opt { display: flex; flex-direction: column; gap: 6px; }
.opt > label {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.opt input[type="text"] {
  font-family: var(--sans);
  font-size: 14px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.4);
  color: var(--ink);
  outline: none;
}
.opt input[type="text"]:focus { border-color: var(--accent); }
.seg {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.35);
}
.seg button {
  flex: 1;
  font-family: var(--sans);
  font-size: 13px;
  padding: 8px 0;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  transition: all 0.15s;
}
.seg button.active { background: var(--ink); color: var(--paper); }
.spine-note {
  font-family: var(--serif);
  font-style: italic;
  font-size: 13px;
  line-height: 1.5;
  color: var(--ink-soft);
  margin: 4px 0 0;
}

/* ---- usage meter (freemium) ---- */
.usage {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  padding: 9px 11px;
  margin-top: -4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.4);
}
.usage-label {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-soft);
}
.usage-label.spent { color: var(--accent); font-weight: 700; }
.usage-label.premium { color: var(--green); font-weight: 700; }
.usage-upgrade {
  margin-left: auto;
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  transition: background 0.15s, transform 0.12s;
}
.usage-upgrade:hover { background: var(--accent-deep); transform: translateY(-1px); }
.usage-link {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-soft);
  background: none;
  border: none;
  cursor: pointer;
  border-bottom: 1px dotted var(--ink-soft);
}
.usage-link:hover { color: var(--accent); }

/* ---- upgrade modal ---- */
.upgrade-modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
/* CRITICAL: without this, `display:flex` overrides the [hidden] attribute and the
   (invisible, opacity:0) modal covers the whole page and eats every click/scroll. */
.upgrade-modal[hidden] {
  display: none;
}
.upgrade-scrim {
  position: absolute;
  inset: 0;
  background: rgba(24, 22, 20, 0.55);
  opacity: 0;
  transition: opacity 0.2s;
}
.upgrade-modal.show .upgrade-scrim { opacity: 1; }
.upgrade-card {
  position: relative;
  width: min(380px, 92vw);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 26px 26px 22px;
  text-align: center;
  transform: translateY(10px) scale(0.98);
  opacity: 0;
  transition: transform 0.2s, opacity 0.2s;
}
.upgrade-modal.show .upgrade-card { transform: translateY(0) scale(1); opacity: 1; }
.upgrade-badge {
  display: inline-block;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 1px;
  font-weight: 700;
  color: var(--accent);
  border: 1.5px solid var(--accent);
  border-radius: 999px;
  padding: 3px 12px;
  margin-bottom: 12px;
}
.upgrade-card h3 {
  font-family: var(--serif);
  font-size: 24px;
  font-weight: 900;
  margin: 0 0 8px;
  color: var(--ink);
}
.upgrade-msg { font-size: 14px; color: var(--ink-soft); margin: 0 0 12px; line-height: 1.5; }
.upgrade-price { font-family: var(--mono); font-size: 13px; color: var(--ink); margin: 0 0 18px; }
.upgrade-price strong { font-size: 20px; color: var(--accent); }
.upgrade-actions { display: flex; flex-direction: column; gap: 10px; }
.upgrade-cta { padding: 13px 14px; }
.upgrade-cta[hidden] { display: none; }

/* ---- preview / paper ---- */
.preview-panel { background: transparent; }
.paper-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-top: none;
  border-radius: 0 0 10px 10px;
  background: #fffdf8;
  padding: 30px;
}
.paper {
  max-width: 720px;
  margin: 0 auto;
  background: #fff;
  padding: 48px 56px;
  box-shadow: var(--shadow);
  border-radius: 2px;
  min-height: 100%;
}
.placeholder { color: #b4ac9b; font-style: italic; }

/* GitHub-ish markdown styling for the preview */
.markdown-body { color: #24292e; font-size: 15px; line-height: 1.65; }
.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  font-family: var(--serif);
  color: #1b1f23;
  line-height: 1.25;
  margin: 1.4em 0 0.5em;
  font-weight: 600;
}
.markdown-body h1 { font-size: 2em; }
.markdown-body h2 { font-size: 1.5em; border-bottom: 1px solid #eaecef; padding-bottom: 0.3em; }
.markdown-body h3 { font-size: 1.25em; }
.markdown-body a { color: #0366d6; text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }
.markdown-body code {
  font-family: var(--mono);
  font-size: 0.85em;
  background: #eff1f3;
  padding: 0.2em 0.4em;
  border-radius: 4px;
}
.markdown-body pre {
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: 6px;
  padding: 14px 16px;
  overflow-x: auto;
}
.markdown-body pre code { background: none; padding: 0; font-size: 0.82em; line-height: 1.5; }
.markdown-body pre.hljs { color: #24292e; }

/* highlight.js token colors — identical palette to the .docx (theme.ts github) */
.hljs-keyword, .hljs-type, .hljs-doctag, .hljs-meta-keyword { color: #d73a49; }
.hljs-tag, .hljs-name, .hljs-selector-tag, .hljs-template-tag { color: #22863a; }
.hljs-built_in, .hljs-builtin-name, .hljs-symbol, .hljs-variable,
.hljs-template-variable { color: #e36209; }
.hljs-string, .hljs-regexp, .hljs-meta .hljs-string, .hljs-link, .hljs-quote { color: #032f62; }
.hljs-number, .hljs-literal, .hljs-section, .hljs-attr, .hljs-attribute,
.hljs-selector-attr, .hljs-selector-pseudo, .hljs-property { color: #005cc5; }
.hljs-title, .hljs-class .hljs-title, .hljs-function .hljs-title,
.hljs-selector-id, .hljs-selector-class { color: #6f42c1; }
.hljs-comment, .hljs-meta { color: #6a737d; font-style: italic; }
.hljs-addition { color: #22863a; background: #f0fff4; }
.hljs-deletion { color: #b31d28; background: #ffeef0; }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 600; }
.markdown-body blockquote {
  margin: 0 0 1em;
  padding: 0 1em;
  color: #57606a;
  border-left: 4px solid #d0d7de;
  background: #f6f8fa;
}
.markdown-body table { border-collapse: collapse; width: 100%; margin: 0 0 1em; font-size: 0.92em; }
.markdown-body th, .markdown-body td { border: 1px solid #d0d7de; padding: 7px 12px; }
.markdown-body th { background: #f0f3f6; text-align: left; }
.markdown-body hr { border: none; border-top: 1px solid #d0d7de; margin: 1.5em 0; }
.markdown-body ul, .markdown-body ol { padding-left: 1.6em; }
.markdown-body img { max-width: 100%; }

/* Theme/Page reflected in the preview (mirrors the exported .docx) */
.markdown-body.mono [class*="hljs-"] { color: #1a1a1a !important; }
.markdown-body.mono .hljs-comment,
.markdown-body.mono .hljs-quote { color: #888 !important; font-style: italic; }
.markdown-body.mono a { color: #1a1a1a; text-decoration: underline; }
#preview[data-page="A4"] { max-width: 680px; } /* A4 is narrower than US Letter */
#preview[data-page="Letter"] { max-width: 720px; }

/* ---- SEO / marketing content band ---- */
.seo {
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(255, 253, 248, 0.6), var(--paper));
  padding: 56px 26px 72px;
}
.seo-inner { max-width: 760px; margin: 0 auto; }
.seo h2 {
  font-family: var(--serif);
  font-size: 28px;
  line-height: 1.2;
  color: var(--ink);
  margin: 0 0 14px;
}
.seo h3 {
  font-family: var(--sans);
  font-size: 18px;
  font-weight: 700;
  color: var(--ink);
  margin: 30px 0 10px;
}
.seo p { font-size: 15.5px; line-height: 1.7; color: var(--ink-soft); margin: 0 0 12px; }
.seo ul { margin: 0 0 8px; padding-left: 1.3em; }
.seo li { font-size: 15px; line-height: 1.7; color: var(--ink-soft); margin-bottom: 4px; }
.seo code {
  font-family: var(--mono);
  font-size: 0.85em;
  background: var(--inlineCodeBg, #eff1f3);
  padding: 0.12em 0.4em;
  border-radius: 4px;
  color: var(--ink);
}
.seo .faq { margin: 6px 0 0; }
.seo .faq dt { font-weight: 700; color: var(--ink); margin-top: 14px; font-size: 15px; }
.seo .faq dd { margin: 4px 0 0; font-size: 15px; line-height: 1.65; color: var(--ink-soft); }
.seo-foot {
  margin-top: 34px;
  font-family: var(--serif);
  font-style: italic;
  color: var(--ink-soft);
}

/* ---- toast ---- */
.toast {
  position: fixed;
  bottom: 26px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: var(--paper);
  font-family: var(--mono);
  font-size: 13px;
  padding: 11px 18px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
  z-index: 20;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--accent-deep); }

.small { font-size: 11px; }

/* ---- account + top-bar buttons ---- */
.topbtn {
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.45);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 7px 12px;
  cursor: pointer;
  transition: all 0.15s;
}
.topbtn:hover { border-color: var(--accent); color: var(--accent); }
#account { display: flex; align-items: center; gap: 10px; }
.signin {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 7px;
  padding: 8px 13px;
  cursor: pointer;
  box-shadow: 0 8px 18px -10px rgba(216, 58, 20, 0.8);
  transition: background 0.15s, transform 0.12s;
}
.signin:hover { background: var(--accent-deep); transform: translateY(-1px); }
.signin svg { width: 16px; height: 16px; background: #fff; border-radius: 2px; padding: 1px; }
.user-chip { display: inline-flex; align-items: center; gap: 9px; }
.user-chip img { width: 28px; height: 28px; border-radius: 50%; border: 1px solid var(--line); }
.user-chip .uname { font-size: 13px; font-weight: 600; color: var(--ink); max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip .logout {
  font-family: var(--mono); font-size: 11px; color: var(--ink-soft);
  background: none; border: none; cursor: pointer; border-bottom: 1px dotted var(--ink-soft);
}
.user-chip .logout:hover { color: var(--accent); }

/* ---- my-documents drawer ---- */
.drawer-scrim {
  position: fixed; inset: 0; background: rgba(28, 26, 23, 0.4);
  z-index: 24; opacity: 0; transition: opacity 0.25s;
}
.drawer-scrim.show { opacity: 1; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: min(440px, 92vw);
  background: var(--paper); border-left: 1px solid var(--line);
  box-shadow: -20px 0 50px -20px rgba(28, 26, 23, 0.5);
  z-index: 25; transform: translateX(100%); transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
  display: flex; flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px; border-bottom: 1px solid var(--line);
}
.drawer-title { font-family: var(--serif); font-size: 20px; font-weight: 600; }
.history-list { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 12px; }
.hist-item {
  border: 1px solid var(--line); border-radius: 9px; padding: 14px 15px;
  background: rgba(255, 255, 255, 0.5); transition: border-color 0.15s, box-shadow 0.15s;
}
.hist-item:hover { border-color: var(--accent); box-shadow: 0 6px 16px -10px rgba(28,26,23,0.4); }
.hist-title { font-weight: 600; font-size: 15px; color: var(--ink); margin-bottom: 3px; }
.hist-meta { font-family: var(--mono); font-size: 11px; color: var(--ink-soft); margin-bottom: 10px; }
.hist-actions { display: flex; gap: 8px; }
.hist-actions button {
  font-family: var(--sans); font-size: 12px; font-weight: 600; cursor: pointer;
  border-radius: 6px; padding: 6px 10px; border: 1px solid var(--line); background: #fff; color: var(--ink);
  transition: all 0.15s;
}
.hist-actions .primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.hist-actions button:hover { border-color: var(--accent); color: var(--accent); }
.hist-actions .primary:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.api-access {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 14px 20px; border-bottom: 1px solid var(--line);
}
.api-token-box {
  flex-basis: 100%; margin: 4px 0 0; max-height: 220px; overflow: auto;
  background: var(--code-bg); border: 1px solid var(--code-border); border-radius: 8px;
  padding: 12px 14px; font-family: var(--mono); font-size: 11.5px; line-height: 1.5;
  color: #24292e; white-space: pre-wrap; word-break: break-all; cursor: copy;
}

@media (max-width: 980px) {
  body { overflow: auto; }
  .app-shell { height: auto; }
  .stage { grid-template-columns: 1fr; }
  .spine { flex-direction: row; flex-wrap: wrap; align-items: flex-end; }
  .spine .convert { order: 5; flex: 1 1 100%; }
  .panel { min-height: 360px; }
}
