/* Word documents and PDFs: the version bar, the page-like reading layout, the
   PDF viewer and its text layer, and the dialogs for versions. Colours come
   from styles.css tokens; the two below are the only new ones, each defined
   for both themes. */

:root {
  --office-word: #2F62C9;
  --office-word-tint: rgba(47, 98, 201, 0.10);
  --office-pdf: #B4451F;
  --office-pdf-tint: rgba(180, 69, 31, 0.10);
  --pdf-backdrop: #E9E6DF;
  --pdf-page-shadow: 0 1px 2px rgba(25, 24, 23, 0.10), 0 6px 18px rgba(25, 24, 23, 0.10);
  --pdf-hl: rgba(240, 190, 40, 0.42);
  --pdf-hl-line: rgba(196, 140, 10, 0.95);
  --pdf-hl-resolved: rgba(62, 142, 90, 0.26);
}
:root[data-theme="dark"] {
  --office-word: #8FB0F5;
  --office-word-tint: rgba(143, 176, 245, 0.14);
  --office-pdf: #F2A07E;
  --office-pdf-tint: rgba(242, 160, 126, 0.14);
  --pdf-backdrop: #151412;
  --pdf-page-shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.45);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --office-word: #8FB0F5;
    --office-word-tint: rgba(143, 176, 245, 0.14);
    --office-pdf: #F2A07E;
    --office-pdf-tint: rgba(242, 160, 126, 0.14);
    --pdf-backdrop: #151412;
    --pdf-page-shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.45);
  }
}

/* ---------- the tree ---------- */

.office-glyph {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 15px;
  height: 14px;
  padding: 0 3px;
  margin-right: 6px;
  border-radius: 3px;
  font: 700 8.5px/1 var(--font-ui);
  letter-spacing: 0.03em;
  vertical-align: 1px;
  flex: none;
}
.office-glyph.is-word { color: var(--office-word); background: var(--office-word-tint); }
.office-glyph.is-pdf { color: var(--office-pdf); background: var(--office-pdf-tint); }
.office-count {
  flex: none;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-quiet);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-pill);
  padding: 0 6px;
  line-height: 15px;
  white-space: nowrap;
}

/* ---------- the version bar ---------- */

.office-bar {
  box-sizing: border-box;
  width: 100%;
  max-width: 1000px;
  margin: 14px auto 0;
  padding: 0 24px;
  display: grid;
  gap: 10px;
  font-family: var(--font-ui);
}
#app.editing .office-bar, #app.editing .pdf-tools { display: none; }
.ob-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.ob-kind {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 650;
  padding: 4px 9px 4px 7px;
  border-radius: var(--r-pill);
}
.ob-kind .ic { width: 15px; height: 15px; }
.ob-kind.is-word { color: var(--office-word); background: var(--office-word-tint); }
.ob-kind.is-pdf { color: var(--office-pdf); background: var(--office-pdf-tint); }
.ob-current { font-size: 13px; color: var(--text-soft); }
.ob-spacer { flex: 1; }
.ob-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.ob-btn { font-size: 12.5px; padding: 5px 10px; text-decoration: none; color: var(--text); }
.ob-btn .ic { width: 15px; height: 15px; }
.ob-icon-only { padding: 5px 8px; }
.ob-n {
  font-size: 10.5px;
  font-weight: 700;
  color: var(--text-quiet);
  background: var(--surface-hover);
  border-radius: var(--r-xs);
  padding: 0 5px;
}

.ob-switch { position: relative; }
.ob-switch-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border-strong);
  background: var(--card);
  color: var(--text);
  border-radius: var(--r-sm);
  padding: 5px 8px 5px 11px;
  font: 650 13px var(--font-ui);
  cursor: pointer;
}
.ob-switch-btn:hover { border-color: var(--border-hover); }
.ob-switch-btn:focus-visible, .ob-item:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ob-switch-btn .ic { width: 14px; height: 14px; color: var(--text-soft); }
.ob-menu {
  position: absolute;
  z-index: 30;
  top: calc(100% + 6px);
  left: 0;
  width: min(330px, calc(100vw - 32px));
  max-height: min(60vh, 420px);
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--e4);
  padding: 6px;
  animation: pop-in 0.14s ease;
}
.ob-item {
  display: grid;
  gap: 2px;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  border-radius: var(--r-sm);
  padding: 8px 10px 8px 26px;
  font: inherit;
  cursor: pointer;
  position: relative;
}
.ob-item:hover { background: var(--surface-hover-card); }
.ob-item.on { background: var(--accent-wash); }
.ob-item.on::before {
  content: '';
  position: absolute;
  left: 10px;
  top: 14px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
}
.ob-item-title { font-size: 13.5px; font-weight: 650; }
.ob-item-sub { font-size: 12px; color: var(--text-soft); }
.ob-item-note { font-size: 12px; color: var(--text-quiet); font-style: italic; }
.ob-item-dim { color: var(--text-soft); font-weight: 500; }
.ob-latest {
  display: inline-block;
  margin-left: 4px;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ok);
  border: 1px solid currentColor;
  border-radius: var(--r-pill);
  padding: 0 6px;
  vertical-align: 1px;
}

.ob-banner {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--text-body);
  border-radius: var(--r-md);
  padding: 9px 12px;
  border: 1px solid var(--border-soft);
}
.ob-banner > .ic { width: 16px; height: 16px; flex: none; margin-top: 2px; color: var(--text-soft); }
.ob-banner .text-btn { display: inline-flex; gap: 4px; align-items: center; margin-left: 4px; }
.ob-banner .text-btn .ic { width: 13px; height: 13px; }
.ob-banner-actions { display: inline-flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 2px; }
.ob-banner.tone-warn { background: var(--attn-bg); border-color: var(--attn-border); color: var(--attn-text); }
.ob-banner.tone-warn > .ic { color: var(--attn-text); }
.ob-banner.tone-info { background: var(--accent-wash); border-color: transparent; }
.ob-banner.tone-info > .ic { color: var(--accent); }
.ob-banner.tone-note { background: var(--bg-soft); }
.ob-banner.tone-quiet { background: transparent; color: var(--text-quiet); }

/* ---------- a Word document reads like a page ---------- */

#article.office-docx {
  box-sizing: border-box;
  width: calc(100% - 48px);
  max-width: 860px;
  margin: 14px auto 120px;
  padding: 64px 76px 80px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: var(--e2);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.62;
}
#article.office-docx h1 { font-size: 30px; line-height: 1.2; margin-top: 0; }
/* A Word document is somebody else's typography: no drop cap, and no rule
   above every section — Word draws neither. */
#article.office-docx > h1:first-of-type + p::first-letter { float: none; font-size: inherit; line-height: inherit; margin: 0; font-weight: inherit; }
#article.office-docx h2 { border-top: 0; padding-top: 0; margin-top: 36px; }
#article.office-docx p { margin: 0 0 14px; }
#article.office-docx ul, #article.office-docx ol { margin: 0 0 14px; }
#article.office-docx li { margin-bottom: 4px; }
#article.office-docx h2 { font-size: 23px; }
#article.office-docx h3 { font-size: 19px; }
#article.office-docx table { border-collapse: collapse; width: 100%; font-size: 14.5px; }
#article.office-docx th, #article.office-docx td { border: 1px solid var(--border-strong); padding: 6px 10px; vertical-align: top; }
#article.office-docx th { background: var(--bg-soft); text-align: left; }
#article.office-docx th p, #article.office-docx td p { margin: 0; }
#article.office-docx img { max-width: 100%; height: auto; }
#article.office-docx hr.page-break {
  border: 0;
  border-top: 1px dashed var(--border-dashed);
  margin: 36px -76px;
  overflow: visible;
  position: relative;
}
#article.office-docx hr.page-break::after {
  content: 'Page break';
  position: absolute;
  left: 50%;
  top: -9px;
  transform: translateX(-50%);
  font: 600 10px var(--font-ui);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-soft);
  background: var(--card);
  padding: 0 8px;
}
#editor-wrap.office-docx-editing #md-diagram-btn,
#editor-wrap.office-docx-editing #md-sketch-btn,
#editor-wrap.office-docx-editing #editor-seg { display: none !important; }
#editor-wrap.office-docx-editing #rich-editor { font-family: var(--font-ui); font-size: 16px; line-height: 1.62; }
#editor-wrap.office-docx-editing #rich-editor > h1:first-of-type + p::first-letter { float: none; font-size: inherit; line-height: inherit; margin: 0; font-weight: inherit; }
#editor-wrap.office-docx-editing #rich-editor h1 { font-size: 30px; line-height: 1.2; }
#editor-wrap.office-docx-editing #rich-editor h2 { border-top: 0; padding-top: 0; margin-top: 32px; font-size: 23px; }
#editor-wrap.office-docx-editing #rich-editor h3 { font-size: 19px; }
#editor-wrap.office-docx-editing #rich-editor p { margin: 0 0 14px; }
#editor-wrap.office-docx-editing #rich-editor li { margin-bottom: 4px; }
#rich-editor th p, #rich-editor td p { margin: 0; }
#rich-editor hr.page-break { border: 0; border-top: 2px dashed var(--border-dashed); margin: 24px 0; }
#rich-editor table { border-collapse: collapse; width: 100%; margin: 12px 0; }
#rich-editor th, #rich-editor td { border: 1px solid var(--border-strong); padding: 6px 10px; min-width: 60px; vertical-align: top; }
#rich-editor th { background: var(--bg-soft); text-align: left; }

.office-card {
  max-width: 460px;
  margin: 40px auto;
  text-align: center;
  font-family: var(--font-ui);
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--card);
}
#article.office-docx .office-card { border: 0; margin: 0 auto; }
.office-card-icon .ic { width: 34px; height: 34px; color: var(--text-soft); }
.office-card h2 { font-size: 18px; margin: 10px 0 6px; font-family: var(--font-doc); }
.office-card p { font-size: 14px; color: var(--text-quiet); margin: 0 0 16px; line-height: 1.5; }
.office-card .ghost-btn { text-decoration: none; color: var(--text); }

/* ---------- the PDF viewer ---------- */

#article.office-pdf {
  max-width: none;
  margin: 14px 0 0;
  padding: 20px 8px 28px;
  background: var(--pdf-backdrop);
  font-family: var(--font-ui);
}
.pdf-loading { max-width: 520px; margin: 40px auto; }
.pdf-doc { display: flex; flex-direction: column; align-items: center; gap: 30px; }
.pdf-page {
  --user-unit: 1;
  --total-scale-factor: calc(var(--scale-factor) * var(--user-unit));
  --scale-round-x: 1px;
  --scale-round-y: 1px;
  position: relative;
  flex: none;
  /* Paper in either theme: a PDF's own colours are part of what it says. */
  background: #fff;
  box-shadow: var(--pdf-page-shadow);
  max-width: none;
}
.pdf-page > canvas { display: block; width: 100%; height: 100%; }
.pdf-page::after {
  content: attr(data-page-label);
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 7px);
  text-align: center;
  font-size: 11px;
  color: var(--text-soft);
  pointer-events: none;
}

/* The text layer: transparent words laid exactly over the painted page, so a
   selection lands on the right letters. Adapted from pdf.js's own viewer. */
.pdf-page .textLayer {
  color-scheme: only light;
  position: absolute;
  text-align: initial;
  inset: 0;
  overflow: clip;
  opacity: 1;
  line-height: 1;
  letter-spacing: normal;
  word-spacing: normal;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  forced-color-adjust: none;
  transform-origin: 0 0;
  caret-color: CanvasText;
  z-index: 0;
  --min-font-size: 1;
  --text-scale-factor: calc(var(--total-scale-factor) * var(--min-font-size));
  --min-font-size-inv: calc(1 / var(--min-font-size));
}
.pdf-page .textLayer :is(span, br) {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
  user-select: text;
}
.pdf-page .textLayer > :not(.markedContent),
.pdf-page .textLayer .markedContent span:not(.markedContent) {
  z-index: 1;
  --font-height: 0;
  font-size: calc(var(--text-scale-factor) * var(--font-height));
  --scale-x: 1;
  --rotate: 0deg;
  transform: rotate(var(--rotate)) scaleX(var(--scale-x)) scale(var(--min-font-size-inv));
}
.pdf-page .textLayer .markedContent { display: contents; }
.pdf-page .textLayer span[role="img"] { user-select: none; cursor: default; }
.pdf-page .textLayer ::selection { background: rgba(79, 70, 214, 0.28); color: transparent; }
.pdf-page .textLayer br::selection { background: transparent; }
.pdf-page .textLayer .endOfContent {
  display: block;
  position: absolute;
  inset: 100% 0 0;
  z-index: 0;
  cursor: default;
  user-select: none;
}
.pdf-page .textLayer.selecting .endOfContent { top: 0; }

/* Comment highlights on the page: tinted, not painted over — the words
   underneath are the PDF's own pixels, and must stay readable. */
.pdf-page .textLayer mark.mdc-hl {
  color: transparent;
  background: var(--pdf-hl);
  box-shadow: inset 0 -2px 0 var(--pdf-hl-line);
  padding: 0;
  border-radius: 1px;
  position: static;
}
.pdf-page .textLayer mark.mdc-hl:hover, .pdf-page .textLayer mark.mdc-hl.active { background: rgba(240, 190, 40, 0.6); }
.pdf-page .textLayer mark.mdc-hl.resolved { background: var(--pdf-hl-resolved); box-shadow: none; }

.pdf-tools {
  position: sticky;
  bottom: 16px;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 2px;
  width: max-content;
  max-width: calc(100% - 24px);
  margin: -40px auto 24px;
  padding: 4px 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  box-shadow: var(--e4);
  font: 13px var(--font-ui);
  color: var(--text);
}
.pdf-tools .icon-btn { width: 32px; height: 32px; border-radius: 50%; }
.pdf-tools .icon-btn:disabled { opacity: 0.35; cursor: default; }
.pdf-tools .icon-btn.on { color: var(--accent); background: var(--accent-tint); }
.pdf-tools .ic { width: 16px; height: 16px; }
.pdf-page-of { padding: 0 6px; white-space: nowrap; color: var(--text-quiet); }
.pdf-page-of strong { color: var(--text); }
.pdf-zoom { min-width: 44px; text-align: center; font-variant-numeric: tabular-nums; color: var(--text-quiet); }
.pdf-sep { width: 1px; height: 18px; background: var(--border); margin: 0 6px; }

/* ---------- threads from earlier versions ---------- */

.office-older { margin-top: 18px; border-top: 1px solid var(--border-soft); padding-top: 12px; }
.office-older-head {
  display: flex;
  flex-direction: column;
  gap: 1px;
  font: 700 11px var(--font-ui);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-quiet);
  margin: 10px 2px 8px;
}
.office-older-head span { font-weight: 500; text-transform: none; letter-spacing: 0; color: var(--text-soft); font-size: 11.5px; }
.office-older-row {
  display: grid;
  gap: 6px;
  width: 100%;
  text-align: left;
  border: 1px dashed var(--border-dashed);
  background: transparent;
  border-radius: var(--r-md);
  padding: 10px 12px;
  margin-bottom: 8px;
  font: inherit;
  color: var(--text);
  cursor: pointer;
}
.office-older-row:hover { background: var(--surface-hover-card); }
.office-older-quote { font-family: var(--font-doc); font-size: 13.5px; color: var(--text-quiet); }
.office-older-text { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; }

/* ---------- dialogs ---------- */

.office-dialog {
  box-sizing: border-box;
  width: min(480px, calc(100vw - 24px));
  left: 0;
  right: 0;
  margin-inline: auto;
  top: 10%;
  max-height: 82vh;
  overflow-y: auto;
  background: var(--card);
  color: var(--text);
  border-radius: var(--r-2xl, 16px);
  box-shadow: var(--e4), 0 0 0 1px var(--border);
  padding: 22px;
  animation: pop-in 0.16s ease;
  font-family: var(--font-ui);
}
.office-dialog h3 { margin: 0 0 3px; font-family: var(--font-doc); font-size: 19px; }
.office-dialog .form-sub { margin: 0 0 14px; font-size: 13.5px; color: var(--text-quiet); line-height: 1.5; }
.office-dialog textarea {
  box-sizing: border-box;
  width: 100%;
  font: inherit;
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--card);
  color: var(--text);
  resize: vertical;
}
.office-dialog textarea:focus { outline: none; border-color: var(--accent); }
.office-dialog .row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.office-drop {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1.5px dashed var(--border-dashed);
  border-radius: var(--r-md);
  padding: 16px;
  margin-bottom: 14px;
  cursor: pointer;
  color: var(--text-quiet);
  font-size: 13.5px;
}
.office-drop:hover, .office-drop:focus-within { border-color: var(--accent); color: var(--text); }
.office-drop .ic { width: 20px; height: 20px; }
.office-drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }

.ov-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.ov-row { display: flex; gap: 12px; padding: 12px 8px; border-radius: var(--r-md); }
.ov-row.on { background: var(--accent-wash); }
.ov-row + .ov-row { border-top: 1px solid var(--border-soft); }
.ov-num {
  flex: none;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--office-word-tint);
  color: var(--office-word);
  font-weight: 700;
  font-size: 12px;
}
.ov-body { min-width: 0; flex: 1; }
.ov-title { font-weight: 650; font-size: 14px; }
.ov-meta { font-size: 12.5px; color: var(--text-soft); margin-top: 1px; }
.ov-note { margin: 6px 0 0; font-size: 13.5px; color: var(--text-body); }
.ov-actions { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 6px; }
.ov-actions .text-btn { display: inline-flex; gap: 4px; align-items: center; text-decoration: none; font-size: 12.5px; }
.ov-actions .ic { width: 13px; height: 13px; }
.ov-diff { margin-top: 8px; font-size: 13px; }
.ov-diff-sum { margin: 0 0 6px; color: var(--text-quiet); }
.ov-diff-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; max-height: 260px; overflow-y: auto; }
.ov-diff-list li { padding: 4px 8px; border-radius: var(--r-xs); font-family: var(--font-doc); line-height: 1.4; overflow-wrap: anywhere; }
.ov-diff-list li.add { background: var(--success-bg); border-left: 3px solid var(--ok); }
.ov-diff-list li.del { background: var(--bg-soft); border-left: 3px solid var(--danger); text-decoration: line-through; text-decoration-color: var(--text-soft); color: var(--text-quiet); }

.office-rt .ic { width: 16px; height: 16px; }

/* ---------- small screens ---------- */

@media (max-width: 720px) {
  .office-bar { padding: 0 12px; margin-top: 10px; }
  .ob-btn > span:not(.ob-n) {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }
  .ob-btn { padding: 6px 8px; }
  .ob-spacer { display: none; }
  /* Under the button it opens from, the full width of the screen. */
  .ob-menu { position: fixed; top: auto; left: 12px; right: 12px; width: auto; margin-top: 6px; }
  .ob-actions { margin-left: auto; }
  #article.office-docx { width: calc(100% - 16px); margin: 10px 8px 100px; padding: 28px 20px 48px; border-radius: 4px; font-size: 15.5px; }
  #article.office-docx h1 { font-size: 25px; }
  #article.office-docx hr.page-break { margin: 28px -20px; }
  #article.office-docx table { display: block; overflow-x: auto; }
  #article.office-pdf { padding: 12px 0 24px; }
  .pdf-tools { bottom: calc(12px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  .ob-menu, .office-dialog { animation: none; }
}

/* ---------- the project library ---------- */

.format-word .project-format .ic { color: var(--office-word); background: var(--office-word-tint); }
.format-pdf .project-format .ic { color: var(--office-pdf); background: var(--office-pdf-tint); }
.project-format .office-count { margin-left: 4px; letter-spacing: 0; }
