:root {
  color-scheme: light;
  --desk: #e3e6ea;
  --surface: #ffffff;
  --surface-2: #f3f5f7;
  --ink: #17202b;
  --muted: #5a6575;
  --line: #d2d8df;
  --accent: #2346a0;
  --accent-ink: #ffffff;
  --accent-soft: #e2e8f7;
  --danger: #b42318;
  --brand-fold: #ffd23f;
  --focus: #2f6fed;
  --shadow-page: 0 1px 2px rgba(20, 30, 45, .18), 0 6px 18px rgba(20, 30, 45, .10);
  --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --radius: 6px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --desk: #11151b;
    --surface: #1a2028;
    --surface-2: #222a34;
    --ink: #e5e9ef;
    --muted: #9aa5b5;
    --line: #313b48;
    --accent: #86a3f2;
    --accent-ink: #0d1526;
    --accent-soft: #24304b;
    --danger: #ff8a80;
    --brand-fold: #ffd23f;
    --focus: #9db8ff;
    --shadow-page: 0 1px 2px rgba(0, 0, 0, .5), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 var(--font); color: var(--ink); background: var(--desk); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
h1, h2 { margin: 0; line-height: 1.2; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 22px; }
h2 { font-size: 15px; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.spacer { flex: 1; }

.ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* Layout */
.app { display: grid; grid-template-columns: 216px 1fr; height: 100vh; height: 100dvh; }
.rail { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 18px 12px; gap: 22px; min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; padding: 0 8px; }
.brand-mark { width: 30px; height: 30px; color: var(--accent); }
.brand-name { font-size: 19px; font-weight: 700; letter-spacing: -.02em; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--radius); color: var(--muted); text-decoration: none; font-weight: 550; }
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }
.rail-foot { margin-top: auto; font-size: 12px; color: var(--muted); padding: 0 8px; display: grid; gap: 8px; }
.offline-state { display: inline-flex; align-items: center; gap: 6px; }
.offline-state::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: #2e9d5b; }

.stage { min-width: 0; min-height: 0; position: relative; }
.view { display: none; height: 100%; flex-direction: column; min-height: 0; }
.view.active { display: flex; }
.view-head { padding: 26px 32px 6px; display: grid; gap: 6px; max-width: 980px; }
.view-head p { color: var(--muted); max-width: 70ch; }
.panel-body { padding: 18px 32px 40px; overflow: auto; display: grid; gap: 18px; align-content: start; max-width: 1180px; width: 100%; }
#view-merge, #view-organise, #view-convert { overflow: auto; }
#view-merge .panel-body, #view-organise .panel-body, #view-convert .panel-body { overflow: visible; }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 7px; padding: 7px 13px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 550; white-space: nowrap; }
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled, .icon-btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.danger-text { color: var(--danger); }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: var(--radius); border: 1px solid transparent; background: transparent; cursor: pointer; color: var(--ink); }
.icon-btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--line); }
.icon-btn.danger { color: var(--danger); }
.action-btns { display: flex; flex-wrap: wrap; gap: 8px; }

/* Editor toolbar */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.tb-group { display: flex; align-items: center; gap: 6px; }
.tb-end { margin-left: auto; }
.tb-sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.tools { background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 3px; gap: 2px; }
.tool { width: 36px; height: 34px; display: grid; place-items: center; border-radius: 6px; border: 0; background: transparent; cursor: pointer; color: var(--muted); }
.tool:hover { color: var(--ink); background: var(--surface); }
.tool[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); }
.tool:disabled { opacity: .4; cursor: not-allowed; }
.swatches { display: flex; gap: 5px; }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.swatch[aria-checked="true"] { box-shadow: 0 0 0 2px var(--ink); }
.colour-pick { display: inline-grid; width: 26px; height: 26px; border-radius: 50%; overflow: hidden; box-shadow: 0 0 0 1px var(--line); cursor: pointer; background: conic-gradient(#e63946, #f4a261, #ffd23f, #2a9d8f, #2346a0, #8e44ad, #e63946); }
.colour-pick input { opacity: 0; width: 100%; height: 100%; cursor: pointer; border: 0; padding: 0; }
.size-ctl { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); margin-left: 6px; }
.size-ctl input { width: 96px; accent-color: var(--accent); }
.size-ctl output { min-width: 2ch; color: var(--ink); font-variant-numeric: tabular-nums; }
select { padding: 6px 8px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); }

/* Editor desk */
.desk { flex: 1; overflow: auto; position: relative; min-height: 0; overscroll-behavior: contain; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 22px; padding: 26px 20px 60px; width: max-content; min-width: 100%; }
.page { position: relative; background: #fff; box-shadow: var(--shadow-page); flex: none; }
.page canvas { position: absolute; inset: 0; display: block; }
.page .overlay { touch-action: none; }
.page-num { position: absolute; left: -2px; top: 100%; margin-top: 4px; font-size: 11.5px; color: var(--muted); }
.desk[data-tool="select"] .overlay { cursor: default; }
.desk[data-tool="pen"] .overlay, .desk[data-tool="highlight"] .overlay, .desk[data-tool="rect"] .overlay, .desk[data-tool="cover"] .overlay { cursor: crosshair; }
.desk[data-tool="text"] .overlay { cursor: text; }
.desk[data-tool="comment"] .overlay, .desk[data-tool="image"] .overlay { cursor: copy; }
.desk[data-tool="eraser"] .overlay { cursor: cell; }
.overlay.hover-move { cursor: move !important; }
.overlay.hover-resize { cursor: nwse-resize !important; }
.text-edit { position: absolute; z-index: 5; margin: 0; padding: 0; border: 1px dashed var(--accent); background: rgba(255, 255, 255, .85); outline: none; resize: none; overflow: hidden; line-height: 1.2; font-family: Helvetica, Arial, sans-serif; white-space: pre; min-width: 40px; color-scheme: light; }
.statusbar { display: flex; gap: 18px; padding: 6px 16px; font-size: 12.5px; color: var(--muted); background: var(--surface); border-top: 1px solid var(--line); min-height: 30px; }
.statusbar .hint { margin-left: auto; }
.tooltip { position: fixed; z-index: 30; max-width: 280px; padding: 8px 10px; border-radius: var(--radius); background: var(--ink); color: var(--surface); font-size: 13px; white-space: pre-wrap; pointer-events: none; box-shadow: 0 6px 18px rgba(0, 0, 0, .25); }

/* Drop zones */
.dropzone { border: 1.5px dashed var(--line); border-radius: 10px; padding: 22px; display: flex; align-items: center; justify-content: center; gap: 14px; flex-wrap: wrap; background: var(--surface); color: var(--muted); text-align: center; }
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); color: var(--ink); }
.dropzone.big { position: absolute; inset: 40px; max-width: 620px; max-height: 360px; margin: auto; flex-direction: column; gap: 12px; padding: 40px; }
.dropzone.big h1 { color: var(--ink); font-size: 22px; }
.dropzone.big p { max-width: 46ch; }
.dz-ic { width: 52px; height: 52px; color: var(--accent); }

/* Lists */
.file-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.file-list:empty { display: none; }
.file-item { display: grid; grid-template-columns: auto 44px 1fr auto auto; align-items: center; gap: 12px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.file-item.dragging { opacity: .5; }
.file-item.drop-before { box-shadow: 0 -3px 0 var(--accent); }
.file-item .grip { cursor: grab; color: var(--muted); display: grid; }
.file-item .thumb { width: 44px; height: 44px; border-radius: 4px; background: var(--surface-2); object-fit: cover; display: grid; place-items: center; font-size: 11px; font-weight: 700; color: var(--muted); }
.file-item .meta { min-width: 0; }
.file-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-item .sub { font-size: 12.5px; color: var(--muted); }
.file-item .rng { width: 150px; }
.file-item .row-btns { display: flex; gap: 2px; }
input[type="text"], input[type="number"], textarea { padding: 6px 9px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); }
.field { display: grid; gap: 4px; font-size: 12.5px; color: var(--muted); }
.field input, .field select { color: var(--ink); min-width: 180px; }
.action-bar { display: flex; align-items: end; justify-content: space-between; gap: 14px; flex-wrap: wrap; padding-top: 4px; }
.opts { display: flex; gap: 16px; flex-wrap: wrap; }
.note { max-width: 80ch; }

/* Organise */
.og-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 0; position: sticky; top: 0; background: var(--desk); z-index: 2; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.thumb-card { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 8px; display: grid; gap: 6px; cursor: pointer; user-select: none; position: relative; }
.thumb-card[aria-selected="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.thumb-card.dragging { opacity: .4; }
.thumb-card.drop-before { box-shadow: -4px 0 0 var(--accent); }
.thumb-box { height: 170px; display: grid; place-items: center; overflow: hidden; background: var(--surface-2); border-radius: 4px; }
.thumb-box canvas { max-width: 100%; max-height: 100%; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); background: #fff; transition: transform .2s; }
.thumb-foot { display: flex; align-items: center; gap: 2px; }
.thumb-foot .pn { font-weight: 650; font-variant-numeric: tabular-nums; margin-right: 4px; }
.thumb-foot .orig { font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.thumb-foot .icon-btn { width: 26px; height: 26px; }
.thumb-foot .icon-btn .ic { width: 15px; height: 15px; }
.og-out { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; margin-top: 8px; }
.out-block { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 16px; display: grid; gap: 10px; align-content: start; }
.radio-col { display: grid; gap: 8px; }
.radio-col label { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.num { width: 64px; }
.rng { width: 180px; }

/* Convert */
.tabs { display: flex; gap: 4px; padding: 10px 32px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tab { border: 0; background: transparent; padding: 9px 12px; border-bottom: 2px solid transparent; cursor: pointer; color: var(--muted); font-weight: 550; margin-bottom: -1px; }
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
.paste summary { cursor: pointer; color: var(--muted); }
.paste textarea { width: 100%; margin: 8px 0; display: block; }
.txt-out { width: 100%; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.doc-preview { display: flex; flex-direction: column; align-items: center; gap: 18px; overflow-x: auto; padding: 12px 0; }
.doc-preview:empty { display: none; }

/* Pages rendered from documents (these are captured to images, so keep plain colours) */
.doc-page { background: #ffffff; color: #111111; box-shadow: var(--shadow-page); flex: none; overflow: hidden; color-scheme: light; }
.doc-content { overflow: hidden; font-family: Calibri, Carlito, "Segoe UI", Arial, sans-serif; font-size: 11pt; line-height: 1.3; }
.doc-content p { margin: 0 0 6pt; min-height: 1em; }
.doc-content h1, .doc-content h2, .doc-content h3, .doc-content h4, .doc-content h5, .doc-content h6 { margin: 10pt 0 6pt; line-height: 1.2; color: #1f3763; }
.doc-content h1 { font-size: 20pt; } .doc-content h2 { font-size: 16pt; } .doc-content h3 { font-size: 13pt; }
.doc-content h4, .doc-content h5, .doc-content h6 { font-size: 11pt; }
.doc-content .title { font-size: 26pt; color: #111111; font-weight: 400; }
.doc-content table { border-collapse: collapse; width: 100%; margin: 0 0 8pt; }
.doc-content td, .doc-content th { border: 1px solid #9aa0a6; padding: 3pt 5pt; vertical-align: top; }
.doc-content td p, .doc-content th p { margin: 0; }
.doc-content td.vm { border-top-color: transparent; }
.doc-content img { max-width: 100%; height: auto; }
.doc-content a { color: #1155cc; }
.doc-content .li { display: flex; gap: 6pt; }
.doc-content .li-mark { flex: none; min-width: 14pt; }
.doc-content .txt-line { margin: 0; white-space: pre-wrap; font-family: Consolas, "Courier New", monospace; font-size: 10pt; line-height: 1.35; min-height: 1.35em; overflow-wrap: anywhere; }
.doc-content pre { white-space: pre-wrap; }

/* Dialog, busy, toasts */
.dlg { border: 1px solid var(--line); border-radius: 12px; padding: 20px; width: min(440px, 92vw); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.dlg::backdrop { background: rgba(15, 20, 30, .45); }
.dlg form { display: grid; gap: 12px; }
.dlg textarea { width: 100%; }
.dlg-msg { white-space: pre-line; color: var(--muted); }
.dlg { width: min(520px, 92vw); }
.dlg-btns { display: flex; gap: 8px; align-items: center; }
.busy { position: fixed; inset: 0; background: rgba(15, 20, 30, .35); display: grid; place-items: center; z-index: 50; }
.busy-card { background: var(--surface); padding: 16px 22px; border-radius: 10px; display: flex; align-items: center; gap: 12px; box-shadow: 0 10px 40px rgba(0, 0, 0, .3); font-weight: 550; }
.spinner { width: 18px; height: 18px; border: 2.5px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toasts { position: fixed; right: 18px; bottom: 18px; display: grid; gap: 8px; z-index: 60; max-width: min(420px, calc(100vw - 36px)); }
.toast { background: var(--ink); color: var(--surface); padding: 10px 14px; border-radius: 8px; box-shadow: 0 8px 26px rgba(0, 0, 0, .25); font-size: 13.5px; }
.toast.error { background: var(--danger); color: #fff; }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.5s; } .thumb-box canvas { transition: none; } }

#print-root { display: none; }

/* Small screens */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .rail { flex-direction: row; align-items: center; padding: 8px 10px; gap: 10px; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
  .brand-name, .rail-foot { display: none; }
  .nav { flex-direction: row; }
  .nav a { padding: 7px 9px; white-space: nowrap; }
  .nav a span { font-size: 13px; }
  .view-head, .panel-body { padding-left: 16px; padding-right: 16px; }
  .tabs { padding: 8px 16px 0; }
  .toolbar { padding: 8px 10px; gap: 8px; }
  .tb-end { margin-left: 0; }
  .btn span { display: none; }
  .btn.primary span, .action-btns .btn span, .dropzone .btn span { display: inline; }
  .file-item { grid-template-columns: auto 1fr auto; }
  .file-item .thumb, .file-item .grip { display: none; }
  .file-item .rng { width: 110px; }
  .statusbar .hint { display: none; }
}

/* Print: only the converted document pages */
@media print {
  body.print-doc > *:not(#print-root) { display: none !important; }
  body.print-doc { background: #fff; }
  body.print-doc #print-root { display: block; }
  #print-root .doc-page { box-shadow: none; margin: 0; break-after: page; page-break-after: always; }
  #print-root .doc-page:last-child { break-after: auto; page-break-after: auto; }
}
