:root {
  --bg: #f3f2ef;
  --panel: #ffffff;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #e2e0dc;
  --accent: #d6402b;
  --accent-ink: #ffffff;
  --sel: #2563eb;
  --word: #2b5797;
  --excel: #1e7145;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: .5; cursor: progress; }

/* ---------- barra superiore ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  height: 56px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; color: var(--ink); text-decoration: none; }
.logo { background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 800; padding: 5px 6px; border-radius: 5px; letter-spacing: .5px; }
.docname { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.spacer { flex: 1; }
.btn { border: 1px solid var(--line); background: var(--panel); padding: 8px 14px; border-radius: 8px; font-weight: 600; font-size: 14px; }
.btn:hover { background: #f7f6f4; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); background: var(--accent); }

/* ---------- home ---------- */
.wrap { max-width: 880px; margin: 0 auto; padding: 48px 16px 64px; }
h1 { font-size: clamp(26px, 4vw, 38px); margin: 0 0 10px; letter-spacing: -.02em; }
h2 { font-size: 18px; margin: 32px 0 14px; }
.lead { color: var(--muted); font-size: 17px; line-height: 1.5; margin: 0 0 28px; max-width: 640px; }
.alert { background: #fdecea; color: #8a1c10; border: 1px solid #f5c2bb; padding: 12px 14px; border-radius: 8px; margin-bottom: 20px; }

.dropzone { position: relative; }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.dropzone label {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 56px 16px; text-align: center; color: var(--muted);
  background: var(--panel); border: 2px dashed #cfccc6; border-radius: 16px; transition: .15s;
}
.dropzone label svg { color: var(--accent); }
.dropzone label strong { color: var(--ink); font-size: 18px; }
.dropzone.over label, .dropzone:hover label { border-color: var(--accent); background: #fff8f6; }

.filecard { display: flex; align-items: center; gap: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; }
.fileicon { width: 44px; height: 54px; border-radius: 6px; background: var(--accent); color: #fff; font-weight: 800; font-size: 12px; display: grid; place-items: center; flex: none; }
.fname { font-weight: 600; word-break: break-all; }
.fmeta { color: var(--muted); font-size: 14px; margin-top: 2px; }
.filecard .link { margin-left: auto; color: var(--accent); font-size: 14px; white-space: nowrap; }

.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 14px; }
.action {
  display: flex; flex-direction: column; gap: 6px; text-align: left; text-decoration: none; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; transition: .15s;
}
.action:hover:not(:disabled) { border-color: #bdb9b2; transform: translateY(-1px); box-shadow: 0 6px 20px rgba(0,0,0,.06); }
.action strong { font-size: 16px; }
.action span:last-child { color: var(--muted); font-size: 14px; line-height: 1.45; }
.aicon { width: 38px; height: 38px; border-radius: 9px; display: grid; place-items: center; color: #fff; font-weight: 800; margin-bottom: 6px; }
.aicon.edit { background: var(--accent); font-size: 20px; }
.aicon.word { background: var(--word); }
.aicon.excel { background: var(--excel); }
.status { margin-top: 18px; padding: 12px 14px; border-radius: 8px; background: #eef2ff; color: #1e3a8a; }
.status.ok { background: #e8f5ec; color: #14532d; }
.status.err { background: #fdecea; color: #8a1c10; }

/* ---------- editor ---------- */
body.editor { display: flex; flex-direction: column; overflow: hidden; }
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.toolbar .group { display: flex; align-items: center; gap: 4px; }
.toolbar button { border: 1px solid transparent; background: transparent; border-radius: 7px; padding: 6px 10px; min-width: 34px; font-size: 14px; }
.toolbar button:hover { background: #f1efec; }
.toolbar .tool.active { background: #fdecea; border-color: #f4c3bb; color: var(--accent); font-weight: 600; }
.toolbar .fmt.on { background: #e7ecfb; border-color: #c6d2f6; color: var(--sel); }
.toolbar select, .toolbar input[type=number] { border: 1px solid var(--line); border-radius: 7px; padding: 5px 6px; background: #fff; height: 32px; }
.toolbar input[type=number] { width: 64px; }
.color { position: relative; display: inline-grid; place-items: center; width: 34px; height: 32px; border-radius: 7px; cursor: pointer; font-weight: 700; }
.color:hover { background: #f1efec; }
.color input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
.color span { border-bottom: 4px solid currentColor; line-height: 1.1; pointer-events: none; }
.format.erase > :not(.bg) { opacity: .35; pointer-events: none; }
.zoom span { min-width: 48px; text-align: center; font-size: 13px; color: var(--muted); }
.hint { padding: 6px 16px; font-size: 13px; color: var(--muted); background: #faf9f7; border-bottom: 1px solid var(--line); }

.viewer { flex: 1; overflow: auto; padding: 24px; display: flex; flex-direction: column; align-items: flex-start; gap: 28px; outline: none; }
.viewer > .page, .viewer > .loading { margin-left: auto; margin-right: auto; }   /* centrate, ma una pagina più larga si scorre fino al bordo */
.loading { margin-top: 80px; color: var(--muted); }
.page { position: relative; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 8px 24px rgba(0,0,0,.06); flex: none; }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.pagenum { position: absolute; top: 100%; left: 0; right: 0; text-align: center; font-size: 12px; color: var(--muted); padding-top: 6px; }
.layer { position: absolute; inset: 0; overflow: hidden; }
body[data-tool=add] .layer { cursor: text; }
body[data-tool=erase] .layer { cursor: crosshair; }

/* scritte originali cliccabili */
.ti { position: absolute; border-radius: 2px; }
body[data-tool=edit] .ti { cursor: text; }
body[data-tool=edit] .ti:hover { background: rgba(37, 99, 235, .10); outline: 1px solid rgba(37, 99, 235, .55); }
body[data-tool=erase] .ti:hover { background: rgba(214, 64, 43, .10); outline: 1px dashed rgba(214, 64, 43, .7); }
body[data-tool=add] .ti, body[data-tool=move] .ti { pointer-events: none; }

/* oggetti inseriti */
.cover { position: absolute; pointer-events: none; }
.obj { position: absolute; }
.obj.edit, .obj.add { white-space: pre; }
.obj .content { outline: none; white-space: pre; min-height: 1em; padding: 0; margin: 0; cursor: text; caret-color: var(--sel); }
.obj.add .content:empty::before { content: "Scrivi qui…"; color: #9aa3b5; font-style: italic; }
.obj.edit:hover, .obj.add:hover { outline: 1px dashed rgba(37, 99, 235, .5); outline-offset: 1px; }
.obj.sel { outline: 1.5px solid var(--sel) !important; outline-offset: 1px; z-index: 5; }
.obj.erase { cursor: move; }
.obj.erase:hover { outline: 1px dashed rgba(214, 64, 43, .7); }
body[data-tool=move] .obj { cursor: move; }
.grip, .objdel, .resize { display: none; position: absolute; }
.obj.sel .grip, .obj.sel .objdel, .obj.sel .resize { display: grid; }
.grip {
  left: -22px; top: -2px; width: 18px; height: 20px; place-items: center; font-size: 13px;
  background: var(--sel); color: #fff; border-radius: 4px; cursor: move; user-select: none; touch-action: none;
}
.objdel {
  right: -10px; top: -10px; width: 18px; height: 18px; place-items: center; padding: 0;
  border: none; border-radius: 50%; background: #222; color: #fff; font-size: 13px; line-height: 1;
}
.resize { right: -5px; bottom: -5px; width: 10px; height: 10px; background: var(--sel); border: 2px solid #fff; border-radius: 2px; cursor: nwse-resize; touch-action: none; }
.rubber { position: absolute; border: 1.5px dashed var(--accent); background: rgba(214, 64, 43, .08); pointer-events: none; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 50;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); max-width: calc(100vw - 32px);
}
.toast.err { background: #8a1c10; }
.toast.ok { background: #14532d; }

@media (max-width: 760px) {
  .toolbar .tool span { display: none; }
  .docname { display: none; }
  .viewer { padding: 16px; }
  .btn { padding: 7px 10px; }
}

/* scritte riconosciute come filigrana: stanno sotto le altre e si evidenziano appena */
body[data-tool=edit] .ti.wm:hover { background: rgba(120, 120, 120, .06); outline: 1px dotted rgba(120, 120, 120, .5); }

/* barra OCR */
.ocrbar { display: flex; align-items: center; gap: 12px; padding: 8px 16px; background: #eef4ff; color: #1e3a8a; font-size: 13px; border-bottom: 1px solid #d5e1fb; }
.ocrbar[hidden] { display: none; }
.ocrbar.done { background: #e8f5ec; color: #14532d; border-color: #c9e6d2; }
.ocrbar.err { background: #fdecea; color: #8a1c10; border-color: #f5c2bb; }
.ocrbar-bar { width: 140px; height: 6px; border-radius: 3px; background: rgba(30, 58, 138, .15); overflow: hidden; flex: none; }
.ocrbar.done .ocrbar-bar, .ocrbar.err .ocrbar-bar { display: none; }
#ocrFill { height: 100%; width: 0; background: #2563eb; transition: width .3s; }
#ocrMsg { flex: 1; }
#ocrClose { border: none; background: transparent; font-size: 18px; line-height: 1; padding: 0 4px; color: inherit; }
.ti.ocr { }
body[data-tool=edit] .page.scanning .layer { cursor: progress; }

/* immagini spostabili */
.imghit { position: absolute; pointer-events: none; border-radius: 2px; }
body[data-tool=move] .imghit, body[data-tool=edit] .imghit { pointer-events: auto; cursor: move; touch-action: none; outline: 1px dashed rgba(37, 99, 235, .45); outline-offset: -1px; }
body[data-tool=edit] .imghit { outline-color: rgba(37, 99, 235, .25); }
body[data-tool=move] .imghit:hover, body[data-tool=edit] .imghit:hover { outline: 2px solid rgba(37, 99, 235, .8); background: rgba(37, 99, 235, .06); }
body[data-tool=erase] .imghit { pointer-events: auto; cursor: pointer; touch-action: none; }
/* su tablet/telefono il trascinamento col dito non deve far scorrere la pagina */
body[data-tool=move] .layer, body[data-tool=erase] .layer { touch-action: none; }
.obj.erase, .grip, .resize { touch-action: none; }
body[data-tool=erase] .imghit:hover { outline: 1px dashed rgba(214, 64, 43, .7); background: rgba(214, 64, 43, .06); }
.obj.img { cursor: move; touch-action: none; }
.obj.img .imgsrc { display: block; width: 100%; height: 100%; pointer-events: none; user-select: none; }
.obj.img:hover { outline: 1px dashed rgba(37, 99, 235, .6); }
.rubber.select { border-color: var(--sel); background: rgba(37, 99, 235, .08); }

/* ---------- pagine: colonna delle anteprime ---------- */
.workspace { flex: 1; display: flex; min-height: 0; position: relative; }
.workspace .viewer { flex: 1; min-width: 0; }
.sidebar { width: 168px; flex: none; display: flex; flex-direction: column; background: #faf9f7; border-right: 1px solid var(--line); min-height: 0; }
body.pages-hidden .sidebar { display: none; }
.side-actions { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; padding: 8px; border-bottom: 1px solid var(--line); }
.side-actions #pgAddPdf, .side-actions #pgBlank { grid-column: span 3; }
.side-actions #pgUp, .side-actions #pgDown, .side-actions #pgDelete { grid-column: span 2; }
.side-actions button { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 5px 6px; font-size: 12px; font-weight: 600; min-width: 30px; }
.side-actions button:hover:not(:disabled) { background: #f1efec; }
.side-actions button.danger:hover:not(:disabled) { background: #fdecea; border-color: #f4c3bb; color: var(--accent); }
.side-actions button:disabled { opacity: .4; cursor: default; }
.thumbs { position: relative; flex: 1; overflow-y: auto; padding: 12px 0 24px; display: flex; flex-direction: column; align-items: center; gap: 14px; outline: none; }
.thumb { position: relative; padding: 5px; border-radius: 6px; border: 2px solid transparent; cursor: pointer; user-select: none; }
.thumb canvas { display: block; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.18); }
.thumb:hover { background: rgba(0,0,0,.04); }
.thumb.current { border-color: #d5d2cc; }
.thumb.selected { border-color: var(--sel); background: rgba(37, 99, 235, .08); }
.thumbs:focus .thumb.selected { box-shadow: 0 0 0 2px rgba(37, 99, 235, .25); }
.tnum { display: block; text-align: center; font-size: 11px; color: var(--muted); margin-top: 4px; }
.thumb.selected .tnum { color: var(--sel); font-weight: 700; }
.thumb.edited .tnum::after { content: " ✎"; color: var(--accent); }
.thumb.dragging { opacity: .4; }
.drop-line { position: absolute; left: 14px; right: 14px; height: 4px; border-radius: 2px; background: var(--sel); box-shadow: 0 0 0 3px rgba(37, 99, 235, .2); pointer-events: none; z-index: 3; }
.drop-line[hidden] { display: none; }
.side-tip { font-size: 11px; line-height: 1.4; color: var(--muted); padding: 8px 10px; border-top: 1px solid var(--line); }
.viewer.dropping { outline: 3px dashed var(--sel); outline-offset: -8px; background: #eef2ff; }

/* filigrana in anteprima */
.wmark { position: absolute; left: 50%; top: 50%; white-space: nowrap; pointer-events: none; font-family: Helvetica, Arial, sans-serif; line-height: 1; z-index: 1; }
.wmark[hidden] { display: none; }
.page .layer { z-index: 2; }

/* finestra filigrana */
.dialog { border: none; border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); color: var(--ink); }
.dialog::backdrop { background: rgba(20, 20, 20, .35); }
.dialog form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.dialog h3 { margin: 0; font-size: 18px; }
.dialog .row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.dialog .field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.dialog .field.grow { flex: 1; min-width: 140px; }
.dialog .field input[type=text], .dialog .field input[type=number], .dialog select { border: 1px solid var(--line); border-radius: 7px; padding: 7px 8px; font-size: 14px; color: var(--ink); background: #fff; }
.dialog .field input[type=color] { width: 48px; height: 34px; padding: 2px; border: 1px solid var(--line); border-radius: 7px; background: #fff; }
.dialog .check { display: flex; gap: 6px; align-items: center; font-size: 14px; padding-bottom: 8px; }
.dialog .note { margin: 0; font-size: 12px; color: var(--muted); }
.dialog-actions { display: flex; gap: 8px; align-items: center; }
.dialog-actions .spacer { flex: 1; }
.btn.icon span { margin-left: 2px; }

@media (max-width: 760px) {
  .sidebar { position: absolute; left: 0; top: 0; bottom: 0; z-index: 20; box-shadow: 6px 0 24px rgba(0,0,0,.15); }
  .btn.icon span { display: none; }
}

.dialog [hidden] { display: none !important; }

/* filigrana: scelta testo / PDF */
.seg { display: inline-flex; background: #f1efec; border-radius: 9px; padding: 3px; gap: 2px; align-self: flex-start; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 6px 14px; border-radius: 7px; font-size: 14px; font-weight: 600; color: var(--muted); }
.seg input:checked + span { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg input:focus-visible + span { outline: 2px solid var(--sel); }
.wm-box { display: flex; flex-direction: column; gap: 12px; }
.fileinfo { font-size: 13px; color: var(--muted); word-break: break-all; }
.check.off { opacity: .4; pointer-events: none; }
.wmark img { display: block; }

/* ---------- schermata iniziale (nessun documento aperto) ---------- */
.start { max-width: 760px; width: 100%; margin: 6vh auto 48px; padding: 0 8px; display: flex; flex-direction: column; gap: 18px; }
.start h1 { margin: 0; }
.start .lead { margin: 0; }
.start[hidden] { display: none; }
.start .status { margin: 0; padding: 12px 14px; border-radius: 8px; background: #eef2ff; color: #1e3a8a; }
.start .status.err { background: #fdecea; color: #8a1c10; }
.privacy { background: #e8f5ec; color: #14532d; border: 1px solid #c9e6d2; border-radius: 12px; padding: 14px 16px; font-size: 14px; line-height: 1.5; }
.privacy strong { display: block; margin-bottom: 2px; }
.dropzone { position: relative; display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 56px 16px; text-align: center; color: var(--muted);
  background: var(--panel); border: 2px dashed #cfccc6; border-radius: 16px; transition: .15s; cursor: pointer; }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; }
.dropzone svg { color: var(--accent); }
.dropzone strong { color: var(--ink); font-size: 18px; }
.dropzone.over, .dropzone:hover { border-color: var(--accent); background: #fff8f6; }
body.empty .toolbar, body.empty .hint, body.empty .sidebar, body.empty #btnPages, body.empty #btnWm,
body.empty #btnDocx, body.empty #btnXlsx, body.empty #btnSave, body.empty #btnOpen { display: none; }
.start .dropzone { flex: none; }

/* ---------- filigrana: pannello laterale (non copre le pagine) ---------- */
.wmpanel { position: static; display: block; flex: none; width: 300px; margin: 0; border: none; border-right: 1px solid var(--line);
  border-radius: 0; box-shadow: none; background: var(--panel); overflow-y: auto; min-height: 0; }
.wmpanel[hidden] { display: none; }
.wmpanel form { padding: 16px; gap: 14px; }
.wmpanel .row { gap: 10px; }
.wmpanel .row .field { flex: 1 1 120px; }
.wmpanel .field select, .wmpanel .field input[type=text], .wmpanel .field input[type=number] { width: 100%; }
.wmpanel .dialog-actions { flex-wrap: wrap; position: sticky; bottom: -16px; background: var(--panel); padding: 10px 0 6px; margin-top: 4px; border-top: 1px solid var(--line); }
.wm-head { display: flex; align-items: center; gap: 8px; }
.wm-head h3 { flex: 1; font-size: 16px; }
.wm-x { border: none; background: transparent; font-size: 22px; line-height: 1; padding: 2px 6px; border-radius: 6px; color: var(--muted); }
.wm-x:hover { background: #f1efec; color: var(--ink); }
.btn.active { background: #fdecea; border-color: #f4c3bb; color: var(--accent); }

@media (max-width: 760px) {
  .wmpanel { position: absolute; left: 0; right: 0; bottom: 0; width: auto; max-height: 55vh; z-index: 30;
    border-right: none; border-top: 1px solid var(--line); box-shadow: 0 -8px 24px rgba(0,0,0,.15); border-radius: 14px 14px 0 0; }
  body.wm-open .viewer { padding-bottom: 56vh; }
}

.wmpanel .dialog-actions .spacer { display: none; }
.wmpanel #wmCancel, .wmpanel #wmApply { flex: 1 1 0; }
.wmpanel #wmRemove { order: 3; flex: 1 1 100%; border-color: transparent; color: var(--muted); font-weight: 500; }
.wmpanel #wmRemove:hover { color: var(--accent); background: #fdecea; }

/* ---- pagina iniziale: contenuti informativi ---- */
.start .info h2 { font-size: 20px; margin: 18px 0 10px; }
.features { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.features li { background: #fff; border: 1px solid var(--line, #e3e6ec); border-radius: 12px; padding: 12px 14px; font-size: 14px; line-height: 1.45; color: var(--muted, #555); }
.features li strong { display: block; color: var(--ink, #1b1f27); font-size: 15px; margin-bottom: 4px; }
.steps { margin: 0; padding-left: 22px; line-height: 1.6; }
.steps li { margin: 4px 0; }
.faq details { background: #fff; border: 1px solid var(--line, #e3e6ec); border-radius: 10px; margin: 0 0 8px; padding: 0 14px; }
.faq summary { cursor: pointer; padding: 12px 0; font-weight: 600; }
.faq details p { margin: 0 0 12px; line-height: 1.55; color: var(--muted, #555); }
.sitefoot { border-top: 1px solid var(--line, #e3e6ec); margin-top: 12px; padding-top: 14px; font-size: 12.5px; line-height: 1.55; color: var(--muted, #666); }
.sitefoot p { margin: 0 0 8px; }
.sitefoot a { color: inherit; }
