:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #9097a6;
  --accent: #4f8cff;
  --accent-2: #3a74e6;
  --danger: #ff5d5d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
[hidden] { display: none !important; }

/* ------------------------------------------------------------ top bar */
.topbar {
  display: flex; align-items: center; gap: 18px; height: 52px; padding: 0 18px;
  border-bottom: 1px solid var(--line); background: var(--panel); flex: none;
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; }
.brand:hover { color: var(--text-strong); }
.logo {
  display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 7px;
  background: linear-gradient(135deg, #4f8cff, #8b5cf6); font-size: 14px;
}
.app-name { font-weight: 600; color: var(--muted); border-left: 1px solid var(--line); padding-left: 18px; }
.topbar-note { margin-left: auto; color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------ layout */
.workspace { flex: 1; display: flex; min-height: 0; }
.stage { position: relative; flex: 1; min-width: 0; overflow: hidden; }
.panel {
  width: 310px; flex: none; overflow-y: auto; background: var(--panel);
  border-left: 1px solid var(--line); padding: 8px 16px 24px;
}
.group { padding: 14px 0; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.group:last-child { border-bottom: 0; }
.group h2 { margin: 0; font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); font-weight: 700; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.meta { margin: 0; color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------ controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  font: inherit; font-weight: 600; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover:not(:disabled) { border-color: var(--line-strong); background: var(--hover); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-2); border-color: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }
select, .text, .num {
  font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
select, .text.wide { width: 100%; }
.num { width: 64px; }
.text { width: 110px; }
.slider { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.slider span { display: flex; justify-content: space-between; color: var(--muted); }
.slider output { color: var(--text); font-variant-numeric: tabular-nums; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.check, .radio { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; color: var(--text); }
.radio + .radio { margin-top: 8px; }
.check input, .radio input { accent-color: var(--accent); width: 16px; height: 16px; }

/* ------------------------------------------------------------ file list */
.files { display: flex; flex-direction: column; gap: 6px; max-height: 190px; overflow-y: auto; }
.file {
  display: flex; align-items: center; gap: 8px; font-size: 12.5px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px;
}
.file .dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.file .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file .info { color: var(--muted); font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ page grid */
.dropzone {
  position: absolute; inset: 20px; border: 2px dashed var(--line-strong); border-radius: 16px;
  display: grid; place-items: center; text-align: center; transition: border-color .15s, background .15s;
}
body.dragging .dropzone { border-color: var(--accent); background: rgba(79, 140, 255, .06); }
.drop-inner { max-width: 460px; padding: 20px; }
.drop-inner h1 { margin: 10px 0 6px; font-size: 22px; }
.drop-inner p { color: var(--muted); margin: 6px 0 16px; }
.drop-inner .hint { font-size: 12px; margin-top: 16px; }
.drop-icon {
  margin: 0 auto; width: 56px; height: 56px; border-radius: 16px; display: grid; place-items: center;
  font-size: 24px; background: var(--panel-2); color: var(--accent);
}

.pages {
  position: absolute; inset: 0; overflow-y: auto; padding: 18px 18px 90px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; align-content: start;
}
.page {
  position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px; cursor: pointer; user-select: none; transition: border-color .12s, box-shadow .12s, transform .12s;
}
.page:hover { border-color: var(--line-hover); }
.page.selected { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(79, 140, 255, .35); }
.page.dragging { opacity: .35; }
.page.drop-before::before, .page.drop-after::after {
  content: ""; position: absolute; top: 6px; bottom: 6px; width: 3px; border-radius: 2px; background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}
.page.drop-before::before { left: -9px; }
.page.drop-after::after { right: -9px; }
.thumb-box {
  position: relative; display: grid; place-items: center; background: var(--well); border-radius: 6px;
  aspect-ratio: 1 / 1.35; overflow: hidden;
}
.thumb-box img {
  max-width: 100%; max-height: 100%; display: block; background: #fff;
  transition: transform .18s; box-shadow: 0 2px 10px rgba(0, 0, 0, .5);
}
.page .badge-row { display: flex; align-items: center; gap: 6px; margin-top: 7px; font-size: 11.5px; color: var(--muted); }
.page .src-dot { width: 9px; height: 9px; border-radius: 3px; flex: none; }
.page .page-no { font-weight: 700; color: var(--text); min-width: 14px; }
.page .src { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toolbar {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 4px; padding: 6px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 12px;
  backdrop-filter: blur(8px); box-shadow: 0 8px 30px rgba(0, 0, 0, .45);
}
.tool {
  border: 0; background: transparent; color: var(--text); font: inherit; font-size: 15px;
  padding: 6px 10px; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 6px;
}
.tool span { font-size: 12px; font-weight: 600; }
.tool:hover { background: var(--panel-2); }
.tool:disabled { opacity: .4; cursor: default; }
.count { font-size: 12px; color: var(--muted); padding: 0 8px; white-space: nowrap; }
.sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }

.busy {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: rgba(15, 17, 21, .6); backdrop-filter: blur(2px); z-index: 5;
}
.busy-text { font-weight: 600; text-align: center; max-width: 360px; }
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .15); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%); z-index: 6;
  background: var(--panel-2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .4); max-width: 80%;
}
.toast.error { border-color: var(--danger); color: var(--danger-text); }

@media (max-width: 820px) {
  .workspace { flex-direction: column; }
  .panel { width: auto; border-left: 0; border-top: 1px solid var(--line); max-height: 45vh; }
  .topbar-note { display: none; }
}

/* ------------------------------------------------------------ live stamp preview */
.stamp-layer { position: absolute; pointer-events: none; overflow: hidden; }
.wm {
  position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center;
  align-items: center; gap: .35em; font-weight: 700; white-space: nowrap; line-height: 1.1;
  transform: rotate(var(--angle, -45deg));
}
.wm.tiled { justify-content: space-around; }
.wm.tiled span { display: block; }
.num-stamp { position: absolute; color: #333; font-weight: 500; line-height: 1; }
.num-stamp.bottom { bottom: 4%; }
.num-stamp.top { top: 4%; }
.num-stamp.center { left: 50%; transform: translateX(-50%); }
.num-stamp.left { left: 5%; }
.num-stamp.right { right: 5%; }

/* ------------------------------------------------------------ panel extras */
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.field span { color: var(--muted); }
.colour { width: 44px; height: 36px; padding: 2px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; flex: none; }
details > summary { cursor: pointer; padding: 8px 0; font-size: 13px; font-weight: 600; color: var(--text-2); list-style: none; }
details > summary::before { content: "▸ "; color: var(--muted); }
details[open] > summary::before { content: "▾ "; }
details { border: 1px solid var(--line); border-radius: 9px; padding: 0 10px; display: flex; flex-direction: column; }
details[open] { padding-bottom: 10px; gap: 9px; }
details.on > summary { color: var(--accent); }
details.on { border-color: rgba(79, 140, 255, .5); }
.error-text { color: var(--danger-text); }

dialog {
  border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text);
  padding: 22px; width: min(380px, 92vw); box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}
dialog::backdrop { background: rgba(8, 10, 14, .65); }
dialog h3 { margin: 0 0 6px; font-size: 17px; }
dialog form { display: flex; flex-direction: column; gap: 12px; }

/* ------------------------------------------------------------ redact / blur editor */
.mark-btn {
  position: absolute; top: 12px; right: 12px; z-index: 2; opacity: 0; transition: opacity .12s;
  border: 1px solid var(--line); background: var(--glass); color: var(--text);
  border-radius: 7px; height: 26px; padding: 0 8px; cursor: pointer; font-size: 12px; font-weight: 600; line-height: 1;
}
.mark-btn { opacity: 1; }
.page { cursor: pointer; }
.mark-btn.has-marks { border-color: #f43f5e; color: #ffb3c1; }
#markDialog { width: min(920px, 94vw); }
.mark-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font: inherit;
  font-size: 12.5px; padding: 6px 10px; border-radius: 999px; cursor: pointer;
}
.chip.active { border-color: var(--accent); background: rgba(79, 140, 255, .18); }
.mark-stage {
  position: relative; display: inline-block; max-height: 62vh; overflow: hidden; border-radius: 8px;
  background: var(--well); cursor: crosshair; user-select: none; touch-action: none;
}
.mark-stage img { display: block; max-width: 100%; max-height: 62vh; }
.mark-boxes { position: absolute; inset: 0; }
.mark-box { position: absolute; border: 1px solid rgba(255, 255, 255, .55); cursor: pointer; }
.mark-box.black { background: #000; }
.mark-box.white { background: #fff; }
.mark-box.blur { background: rgba(140, 170, 210, .75); backdrop-filter: blur(6px); }
.mark-box.pixelate { background: repeating-conic-gradient(#8aa 0 25%, #556 0 50%) 0 0/10px 10px; }
.mark-box:hover { outline: 2px solid #ff5d5d; }
.stamp-layer .mark-hint { position: absolute; background: rgba(0, 0, 0, .78); border: 1px solid rgba(255,255,255,.35); }
.stamp-layer .mark-hint.blur, .stamp-layer .mark-hint.pixelate { background: rgba(150, 175, 210, .8); }
.stamp-layer .mark-hint.white { background: rgba(255, 255, 255, .9); }

/* ------------------------------------------------------------ page editor items */
.chip-sep { width: 1px; align-self: stretch; background: var(--line); margin: 0 2px; }
.text-props { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.text-props select { width: auto; }
.slider.inline { flex-direction: row; align-items: center; gap: 8px; min-width: 180px; }
.slider.inline span { gap: 6px; }
.edit-item { position: absolute; cursor: move; background: none; border: 0; padding: 0; width: auto; }
.edit-item.selected { outline: 2px solid var(--accent); }
.edit-item .ei-body {
  width: 100%; height: 100%; overflow: hidden; outline: none; line-height: 1.25;
  white-space: pre-wrap; word-break: break-word;
}
.edit-item.kind-image .ei-body { display: grid; place-items: center; }
.edit-item.kind-image img { max-width: 100%; max-height: 100%; }
.edit-item .ei-handle {
  position: absolute; right: -6px; bottom: -6px; width: 13px; height: 13px; border-radius: 3px;
  background: var(--accent); border: 2px solid #fff; cursor: nwse-resize;
}
.edit-item .ei-del {
  position: absolute; right: -9px; top: -9px; width: 19px; height: 19px; border-radius: 50%;
  border: 0; background: #ef4444; color: #fff; font-size: 12px; line-height: 1; cursor: pointer; padding: 0;
}
.edit-item:not(.selected) .ei-handle, .edit-item:not(.selected) .ei-del { display: none; }
.edit-cover, .text-target, .pic-target, .form-field { position: absolute; }
.text-target { cursor: text; border-radius: 2px; }
.text-target:hover { background: rgba(79, 140, 255, .16); outline: 1px solid rgba(79, 140, 255, .85); }
.pic-target { cursor: pointer; }
.pic-target:hover { outline: 2px solid #4f8cff; background: rgba(79, 140, 255, .12); }
.form-field input[type=text], .form-field select {
  width: 100%; height: 100%; padding: 0 3px; border: 1px solid rgba(79, 140, 255, .7); border-radius: 2px;
  background: rgba(219, 234, 254, .85); color: #111; font: 12px Helvetica, Arial, sans-serif; color-scheme: light;
}
.form-field input[type=checkbox] { width: 100%; height: 100%; margin: 0; accent-color: #1d4ed8; }
/* Close matches for common fonts, used when a PDF doesn't store a letter that was typed */
@font-face { font-family: "Carlito"; src: url(/vendor/fonts/Carlito-Regular.ttf); font-weight: 400; font-style: normal; }
@font-face { font-family: "Carlito"; src: url(/vendor/fonts/Carlito-Bold.ttf); font-weight: 700; font-style: normal; }
@font-face { font-family: "Carlito"; src: url(/vendor/fonts/Carlito-Italic.ttf); font-weight: 400; font-style: italic; }
@font-face { font-family: "Carlito"; src: url(/vendor/fonts/Carlito-BoldItalic.ttf); font-weight: 700; font-style: italic; }
@font-face { font-family: "Caladea"; src: url(/vendor/fonts/Caladea-Regular.ttf); font-weight: 400; font-style: normal; }
@font-face { font-family: "Caladea"; src: url(/vendor/fonts/Caladea-Bold.ttf); font-weight: 700; font-style: normal; }
@font-face { font-family: "Caladea"; src: url(/vendor/fonts/Caladea-Italic.ttf); font-weight: 400; font-style: italic; }
@font-face { font-family: "Caladea"; src: url(/vendor/fonts/Caladea-BoldItalic.ttf); font-weight: 700; font-style: italic; }
@font-face { font-family: "LiberationSans"; src: url(/vendor/fonts/LiberationSans-Regular.ttf); font-weight: 400; font-style: normal; }
@font-face { font-family: "LiberationSans"; src: url(/vendor/fonts/LiberationSans-Bold.ttf); font-weight: 700; font-style: normal; }
@font-face { font-family: "LiberationSans"; src: url(/vendor/fonts/LiberationSans-Italic.ttf); font-weight: 400; font-style: italic; }
@font-face { font-family: "LiberationSans"; src: url(/vendor/fonts/LiberationSans-BoldItalic.ttf); font-weight: 700; font-style: italic; }
@font-face { font-family: "LiberationSerif"; src: url(/vendor/fonts/LiberationSerif-Regular.ttf); font-weight: 400; font-style: normal; }
@font-face { font-family: "LiberationSerif"; src: url(/vendor/fonts/LiberationSerif-Bold.ttf); font-weight: 700; font-style: normal; }
@font-face { font-family: "LiberationSerif"; src: url(/vendor/fonts/LiberationSerif-Italic.ttf); font-weight: 400; font-style: italic; }
@font-face { font-family: "LiberationSerif"; src: url(/vendor/fonts/LiberationSerif-BoldItalic.ttf); font-weight: 700; font-style: italic; }
@font-face { font-family: "LiberationMono"; src: url(/vendor/fonts/LiberationMono-Regular.ttf); font-weight: 400; font-style: normal; }
@font-face { font-family: "LiberationMono"; src: url(/vendor/fonts/LiberationMono-Bold.ttf); font-weight: 700; font-style: normal; }
@font-face { font-family: "LiberationMono"; src: url(/vendor/fonts/LiberationMono-Italic.ttf); font-weight: 400; font-style: italic; }
@font-face { font-family: "LiberationMono"; src: url(/vendor/fonts/LiberationMono-BoldItalic.ttf); font-weight: 700; font-style: italic; }
.props-slot { min-height: 46px; }   /* the page must not jump when these controls appear */
.font-note { font-size: 12px; color: #86efac; }
.font-note.warn { color: #fcd34d; }
.edit-item .ei-body[contenteditable] { -webkit-user-select: text; user-select: text; }
.edit-item.kind-retext .ei-body { white-space: pre; overflow: visible; min-width: 12px; cursor: text; }
.edit-item.kind-retext { min-width: 14px; }
.edit-item.deleted { border: 1px dashed #ef4444; }
.edit-item.deleted .ei-undo { display: block; }
.edit-item.kind-repic img { display: block; width: 100%; height: 100%; max-height: none; }
.edit-item .ei-move {
  position: absolute; left: -9px; top: -9px; width: 19px; height: 19px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-size: 11px; line-height: 1; cursor: move; touch-action: none;
}
.edit-item .ei-undo {
  position: absolute; right: 13px; top: -9px; width: 19px; height: 19px; border-radius: 50%;
  border: 0; background: #475569; color: #fff; font-size: 12px; line-height: 1; cursor: pointer; padding: 0;
}
.edit-item:not(.selected) .ei-move, .edit-item:not(.selected):not(.deleted) .ei-undo { display: none; }
.edit-item.kind-shape::after, .edit-item.kind-pen::after { content: ""; position: absolute; inset: -6px; z-index: -1; }
.edit-item .ei-body:has(.ei-shape) { overflow: visible; }
.ei-shape { display: block; width: 100%; height: 100%; overflow: visible; }
.pen-ghost { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.pen-ghost polyline { fill: none; stroke-width: 2; vector-effect: non-scaling-stroke; }
.stamp-layer .item-hint { position: absolute; overflow: hidden; color: #111; line-height: 1.2; }
.stamp-layer .item-hint img { width: 100%; height: 100%; object-fit: contain; }

/* ------------------------------------------------------------ full-screen editor */
#markDialog { width: 100vw; height: 100vh; max-width: none; max-height: none; margin: 0; padding: 0; border: 0; border-radius: 0; }
#markDialog[open] { display: flex; flex-direction: column; }
.ed-top { display: flex; align-items: center; gap: 14px; padding: 9px 14px; border-bottom: 1px solid var(--line); background: var(--panel); }
.ed-top strong { white-space: nowrap; }
.ed-top .chips { flex: 1; }
.ed-top #markDone { flex: none; padding: 8px 26px; }
.props-slot { display: flex; align-items: center; min-height: 48px; padding: 6px 14px; background: var(--panel-2); border-bottom: 1px solid var(--line); }
.props-slot .text-props { margin: 0; }
.fmt, .colour-box {
  width: 34px; height: 34px; display: inline-grid; place-items: center; border: 1px solid var(--line); border-radius: 7px;
  background: var(--panel); color: var(--text); font: 15px Georgia, serif; cursor: pointer; position: relative; padding: 0;
}
.fmt.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.colour-box { width: 42px; font-weight: 700; font-family: inherit; }
.colour-box input { position: absolute; left: 3px; right: 3px; bottom: 3px; width: calc(100% - 6px); height: 9px; padding: 0; border: 0; background: none; cursor: pointer; }
.size-box .num { width: 72px; }
.ed-main { flex: 1; min-height: 0; display: flex; }
.ed-strip { width: 118px; flex: none; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 10px; background: var(--panel); border-right: 1px solid var(--line); }
.strip-page { position: relative; border: 2px solid transparent; border-radius: 6px; padding: 0; background: none; cursor: pointer; color: var(--muted); }
.strip-page img { display: block; width: 100%; background: #fff; border-radius: 4px; }
.strip-page span { display: block; padding: 2px 0; font-size: 11.5px; }
.strip-page.current { border-color: var(--accent); color: var(--text); }
.ed-scroll { flex: 1; min-width: 0; overflow: auto; background: var(--well); padding: 20px; }
#markDialog .mark-stage { display: block; width: min(100%, 980px); max-height: none; margin: 0 auto; border-radius: 0; overflow: hidden; box-shadow: 0 8px 30px rgba(0, 0, 0, .5); }
#markDialog .mark-stage > img { width: 100%; max-width: none; max-height: none; background: #fff; }
.ed-foot { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-top: 1px solid var(--line); background: var(--panel); }
.ed-foot .meta { flex: 1; }
.ed-foot .btn { padding: 6px 12px; }
@media (max-width: 820px) {
  .ed-strip { display: none; } .ed-scroll { padding: 6px; }
  /* phones: tools in one swipeable row, so the page keeps the screen */
  .ed-top { gap: 8px; padding: 7px 8px; }
  .ed-top strong { display: none; }
  .ed-top .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; min-width: 0; }
  .ed-top .chips::-webkit-scrollbar { display: none; }
  .ed-top .chip { flex: none; }
  .ed-top #markDone { padding: 8px 14px; }
  .text-props { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .text-props > * { flex: none; }
  .text-props .font-note { white-space: nowrap; }
  #markDialog .meta#markHint { font-size: 12px; }
}

#signDialog { width: min(680px, 94vw); }
#signPad { width: 100%; background: #fff; border-radius: 10px; border: 1px solid var(--line); touch-action: none; cursor: crosshair; }
#signDialog h3 { margin: 0 0 10px; }
