:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --canvas: #e8e8ed;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --accent: #ff4c00;
  --accent-hover: #0077ed;
  --danger: #ff3b30;
  --border: rgba(0, 0, 0, .09);
  --hover: rgba(0, 0, 0, .06);
  --glass: rgba(255, 255, 255, .72);
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 8px 28px rgba(0, 0, 0, .10);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Segoe UI Variable Display", "Segoe UI", Inter, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --canvas: #161617;
    --text: #f5f5f7;
    --muted: #a1a1a6;
    --accent: #2997ff;
    --accent-hover: #47a6ff;
    --border: rgba(255, 255, 255, .12);
    --hover: rgba(255, 255, 255, .09);
    --glass: rgba(29, 29, 31, .72);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 28px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  letter-spacing: -.01em;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; padding: 0; }
svg.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; display: block; }

.glass {
  background: var(--glass);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 44px; padding: 0 22px; border-radius: 980px;
  font-size: 16px; font-weight: 500; white-space: nowrap;
  transition: background .15s, transform .1s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn.small { height: 32px; padding: 0 14px; font-size: 13px; }
.btn.small svg.ic { width: 16px; height: 16px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { color: var(--accent); }
.btn.ghost:hover { background: var(--hover); }
.icon-btn {
  width: 32px; height: 32px; border-radius: 8px; flex: none;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .12s;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn:disabled { opacity: .3; pointer-events: none; }
.icon-btn.on { background: var(--accent); color: #fff; }

/* ---------- App shell ---------- */
#app {
  height: 100%; display: grid;
  grid-template: "bar bar" 52px "side view" minmax(0, 1fr) / 172px minmax(0, 1fr);
}
.toolbar {
  grid-area: bar; z-index: 20; display: flex; align-items: center; gap: 12px;
  padding: 0 12px; border-bottom: 1px solid var(--border);
}
.tb-left, .tb-right { display: flex; align-items: center; gap: 4px; flex: 1 1 0; min-width: 0; }
.tb-right { justify-content: flex-end; }
.filename {
  min-width: 40px; max-width: 220px; padding: 4px 8px; border-radius: 6px;
  font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; outline: none;
}
.filename:hover, .filename:focus { background: var(--hover); }
.filename:focus { text-overflow: clip; }
.tools { display: flex; gap: 2px; padding: 3px; border-radius: 11px; background: var(--hover); overflow-x: auto; scrollbar-width: none; flex: 0 1 auto; }
.tools .sep { width: 1px; margin: 6px 4px; background: var(--border); flex: none; }
.tools .icon-btn.on { background: var(--surface); color: var(--accent); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
.zoom { display: flex; align-items: center; margin: 0 6px; }
.zoom-label { width: 50px; height: 32px; border-radius: 8px; font-size: 12px; font-variant-numeric: tabular-nums; color: var(--muted); }
.zoom-label:hover { background: var(--hover); }

.menu {
  position: fixed; top: 56px; left: 10px; z-index: 40; width: 250px; padding: 6px;
  border-radius: 14px; border: 1px solid var(--border); box-shadow: var(--shadow);
}
.menu button { display: block; width: 100%; text-align: left; padding: 8px 12px; border-radius: 8px; }
.menu button:hover { background: var(--accent); color: #fff; }
.menu button.danger { color: var(--danger); }
.menu button.danger:hover { background: var(--danger); color: #fff; }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 5px 8px; }

/* ---------- Sidebar ---------- */
.sidebar { grid-area: side; display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--border); }
.thumbs { flex: 1; overflow-y: auto; padding: 14px 0; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.thumb { padding: 8px 10px 4px; border-radius: 12px; text-align: center; cursor: pointer; position: relative; }
.thumb .frame { width: 112px; background: #fff; border-radius: 4px; box-shadow: 0 0 0 1px var(--border), 0 2px 6px rgba(0, 0, 0, .12); overflow: hidden; }
.thumb canvas { width: 100%; height: 100%; display: block; }
.thumb span { display: block; margin-top: 5px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.thumb.sel { background: var(--hover); }
.thumb.sel .frame { box-shadow: 0 0 0 2.5px var(--accent); }
.thumb.sel span { color: var(--accent); font-weight: 600; }
.thumb.dragging { opacity: .35; }
.thumb.before::before, .thumb.after::after {
  content: ""; position: absolute; left: 14px; right: 14px; height: 3px; border-radius: 2px; background: var(--accent);
}
.thumb.before::before { top: -3px; }
.thumb.after::after { bottom: -3px; }
.page-actions { display: flex; justify-content: space-around; padding: 8px 6px; border-top: 1px solid var(--border); }
.page-actions .icon-btn { width: 28px; height: 28px; }
.page-actions svg.ic { width: 17px; height: 17px; }

/* ---------- Viewer ---------- */
.viewer { grid-area: view; overflow: auto; background: var(--canvas); position: relative; }
#pages { display: flex; flex-direction: column; align-items: center; gap: 20px; padding: 72px 32px 80px; min-width: min-content; }
.page { position: relative; background: #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .06), 0 6px 24px rgba(0, 0, 0, .14); flex: none; }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.textlayer, .overlay { position: absolute; inset: 0; overflow: hidden; }
.overlay { pointer-events: none; }
.titem { position: absolute; border-radius: 2px; cursor: text; outline: 1px dashed rgba(255, 76, 0, .45); }
.titem:hover { background: rgba(255, 76, 0, .16); outline-style: solid; }

#pages.creating .page { cursor: crosshair; touch-action: none; }
#pages.t-text .page { cursor: text; }
#pages.t-eraser .page { cursor: var(--eraser-cur, crosshair); }
#pages.interactive .obj { pointer-events: auto; cursor: move; touch-action: none; }
#pages.interactive .obj.line, #pages.interactive .obj.arrow, #pages.interactive .obj.draw { pointer-events: none; }
#pages.interactive .obj .hit { pointer-events: stroke; cursor: move; touch-action: none; }

.obj { position: absolute; user-select: none; -webkit-user-select: none; transform-origin: 0 0; }
.obj svg { position: absolute; left: 0; top: 0; width: 1px; height: 1px; overflow: visible; }
.obj .hit { stroke: transparent; fill: none; }
.obj.text { white-space: pre; line-height: 1.2; padding: 0; outline-offset: 2px; }
.obj.text[contenteditable] { user-select: text; -webkit-user-select: text; cursor: text !important; outline: 2px solid #ff4c00 !important; }
.obj.highlight, .obj.draw.hl { mix-blend-mode: multiply; opacity: .4; }
.obj.image img { width: 100%; height: 100%; display: block; pointer-events: none; }
.obj.sel.text, .obj.sel.draw, .obj.sel.image, .obj.sel.rect, .obj.sel.ellipse, .obj.sel.highlight { outline: 1.5px solid #ff4c00; outline-offset: 2px; }
.obj.sel.highlight, .obj.sel.draw.hl { opacity: .6; }
.handle {
  position: absolute; width: 11px; height: 11px; margin: -5.5px; border-radius: 50%;
  background: #fff; border: 1.5px solid #ff4c00; box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  pointer-events: auto; touch-action: none; z-index: 2;
}
.handle[data-h="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.handle[data-h="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
.handle[data-h="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
.handle[data-h="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
.handle[data-h="a"], .handle[data-h="b"] { cursor: crosshair; }

.page-indicator {
  position: sticky; bottom: 16px; left: 50%; transform: translateX(-50%); width: max-content;
  padding: 6px 14px; border-radius: 980px; font-size: 12px; font-variant-numeric: tabular-nums;
  border: 1px solid var(--border); box-shadow: var(--shadow); pointer-events: none; margin-top: -32px;
}

/* ---------- Inspector ---------- */
.inspector {
  position: sticky; top: 12px; left: 0; z-index: 10; width: max-content; max-width: calc(100% - 24px);
  margin: 0 auto -48px; height: 44px; padding: 0 10px; transform: translateY(0);
  display: flex; align-items: center; gap: 10px; overflow-x: auto; scrollbar-width: none;
  border-radius: 14px; border: 1px solid var(--border); box-shadow: var(--shadow);
}
.inspector .grp { display: flex; align-items: center; gap: 5px; flex: none; }
.inspector .grp + .grp { padding-left: 10px; border-left: 1px solid var(--border); }
.inspector label { font-size: 11px; color: var(--muted); }
.swatch {
  width: 20px; height: 20px; border-radius: 50%; background: var(--c); flex: none;
  box-shadow: inset 0 0 0 1px rgba(128, 128, 128, .45);
}
.swatch.on { box-shadow: inset 0 0 0 1px rgba(128, 128, 128, .45), 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }
.swatch.none { background: linear-gradient(to top right, #fff 46%, #ff3b30 46%, #ff3b30 54%, #fff 54%); }
.swatch.custom { background: conic-gradient(#ff3b30, #ffd60a, #34c759, #2997ff, #af52de, #ff3b30); position: relative; overflow: hidden; }
.swatch.custom input { position: absolute; inset: -6px; width: 40px; height: 40px; opacity: 0; cursor: pointer; }
.inspector select, .inspector input[type=number] {
  height: 28px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 12px; padding: 0 6px;
}
.inspector input[type=number] { width: 56px; }
.inspector input[type=range] { width: 84px; accent-color: var(--accent); }
.inspector .icon-btn { width: 28px; height: 28px; font-weight: 700; font-size: 14px; font-family: Georgia, serif; }
.inspector .icon-btn svg.ic { width: 17px; height: 17px; }

/* ---------- Dialogs, toasts, busy ---------- */
dialog.sheet {
  border: 1px solid var(--border); border-radius: 20px; padding: 22px; width: min(560px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog.sheet.narrow { width: min(380px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, .35); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px); }
.sheet h3 { margin: 0 0 4px; font-size: 19px; font-weight: 600; letter-spacing: -.02em; }
.muted { color: var(--muted); margin: 0 0 14px; }
#signPad { width: 100%; aspect-ratio: 5 / 2; background: #fff; border-radius: 12px; border: 1px solid var(--border); touch-action: none; cursor: crosshair; display: block; }
.sheet-row { display: flex; align-items: center; gap: 6px; margin-top: 16px; }
.ink { display: flex; gap: 10px; padding-left: 4px; }
.spacer { flex: 1; }
#askInput {
  width: 100%; height: 38px; margin-top: 10px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit; font-size: 15px; outline: none;
}
#askInput:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }

#toasts { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  padding: 10px 18px; border-radius: 980px; background: rgba(29, 29, 31, .92); color: #fff; font-size: 13px;
  box-shadow: var(--shadow); animation: pop .25s ease-out; max-width: 90vw;
}
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.96); } }
#busy {
  position: fixed; inset: 0; z-index: 90; background: rgba(0, 0, 0, .25);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; color: #fff; font-weight: 500;
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, .35); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#dragveil { position: fixed; inset: 0; z-index: 80; background: color-mix(in srgb, var(--accent) 18%, transparent); display: grid; place-items: center; pointer-events: none; }
#dragveil div { padding: 14px 26px; border-radius: 980px; background: var(--accent); color: #fff; font-size: 17px; font-weight: 600; }

@media (max-width: 760px) {
  #app { grid-template: "bar" 52px "tools" 46px "view" minmax(0, 1fr) / minmax(0, 1fr); }
  .sidebar { display: none; }
  .toolbar { display: contents; }
  .tb-left, .tb-right { grid-area: bar; background: var(--surface); height: 52px; padding: 0 8px; border-bottom: 1px solid var(--border); }
  .tb-left { margin-right: 50%; } .tb-right { margin-left: 30%; }
  .tools { grid-area: tools; border-radius: 0; align-items: center; padding: 0 8px; background: var(--surface); border-bottom: 1px solid var(--border); }
  .filename { max-width: 110px; }
  .zoom { display: none; }
  #pages { padding: 64px 10px 80px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ===== Tema "copisteria": scuro, squadrato, monospace, accento arancione ===== */
:root, :root:root {
  --bg: #0b0504; --surface: #141211; --canvas: #1d1a18; --text: #f4efe6; --muted: #a39a8d;
  --accent: #ff4c00; --accent-hover: #ff6a2b; --danger: #ff3b30;
  --border: rgba(244, 239, 230, .16); --hover: rgba(244, 239, 230, .1); --glass: rgba(16, 13, 12, .86);
  --shadow: 0 0 0 1px rgba(244, 239, 230, .1), 0 10px 30px rgba(0, 0, 0, .6);
  --font: "Geist Mono", ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
  color-scheme: dark;
}
*, *::before, *::after { border-radius: 0 !important; }
.spinner { border-radius: 50% !important; }
body { font-size: 13px; letter-spacing: 0; }
.btn { text-transform: uppercase; letter-spacing: .06em; font-weight: 600; font-size: 13px; }
.btn.small { font-size: 11px; }
.btn.primary { color: #fff; }
.btn.ghost { color: var(--text); border: 1px solid var(--border); }
.toolbar { border-bottom: 1px solid var(--border); }
.tools { background: transparent; border: 1px solid var(--border); padding: 2px; }
.tools .icon-btn.on { background: var(--accent); color: #fff; box-shadow: none; }
.icon-btn.on { background: var(--accent); color: #fff; }
.filename { text-transform: uppercase; letter-spacing: .05em; font-size: 12px; }
.menu button, .inspector label, .thumb span, .page-indicator, .zoom-label { text-transform: uppercase; letter-spacing: .05em; font-size: 11px; }
.thumb.sel .frame { box-shadow: 0 0 0 2px var(--accent); }
.page { box-shadow: 0 0 0 1px rgba(0, 0, 0, .5), 0 10px 30px rgba(0, 0, 0, .55); }
.toast { background: var(--accent); color: #fff; text-transform: uppercase; letter-spacing: .05em; font-size: 11px; font-weight: 600; }
.handle { border-radius: 0 !important; }
dialog.sheet h3 { text-transform: uppercase; letter-spacing: .05em; font-size: 14px; }
#dragveil div { text-transform: uppercase; letter-spacing: .08em; font-size: 14px; }

.titem { outline: 1px dashed rgba(255, 76, 0, .3); }
.titem:hover { background: rgba(255, 76, 0, .22); outline: 1.5px solid #ff4c00; }
.cover { position: absolute; }

/* ---------- Moduli compilabili ---------- */
.formlayer, .findlayer { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
#pages.interactive .formlayer .ff { pointer-events: auto; }
.ff {
  position: absolute; margin: 0; padding: 0 2px; border: 0; outline: 0; box-sizing: border-box;
  background: rgba(0, 113, 227, .13); color: #000; font-family: Helvetica, Arial, sans-serif; line-height: 1.15;
  color-scheme: light;
}
.ff:hover { background: rgba(0, 113, 227, .2); }
.ff:focus { background: #fff; outline: 1.5px solid #ff4c00; }
.ff:disabled { background: transparent; color: #000; opacity: 1; }
textarea.ff { resize: none; padding: 2px; }
input.ff[type=checkbox], input.ff[type=radio] { appearance: none; -webkit-appearance: none; padding: 0; cursor: pointer; }
input.ff[type=checkbox]:checked { background: rgba(0, 113, 227, .13) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M4 13l5 5L20 6' fill='none' stroke='%23000' stroke-width='3.2'/%3E%3C/svg%3E") center / 86% no-repeat; }
input.ff[type=radio]:checked { background: radial-gradient(circle, #000 0 34%, rgba(0, 113, 227, .13) 38%); }

/* ---------- Cerca ---------- */
.findlayer i { position: absolute; background: rgba(255, 214, 10, .45); mix-blend-mode: multiply; }
.findlayer i.cur { background: rgba(255, 76, 0, .55); outline: 1.5px solid #ff4c00; }
.findbar {
  position: fixed; top: 64px; right: 20px; z-index: 30; height: 44px; padding: 0 6px 0 10px;
  display: flex; align-items: center; gap: 4px; border: 1px solid var(--border); box-shadow: var(--shadow);
}
.findbar input {
  width: 210px; height: 30px; padding: 0 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 12px; outline: none;
}
.findbar input:focus { border-color: var(--accent); }
#findCount { min-width: 62px; padding: 0 6px; text-align: center; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.findbar .icon-btn { width: 28px; height: 28px; }
.findbar svg.ic { width: 17px; height: 17px; }

/* ---------- Ritaglio, timbri, firme salvate ---------- */
.croprect { position: absolute; outline: 1.5px dashed #ff4c00; box-shadow: 0 0 0 9999px rgba(0, 0, 0, .5); }
#stampMenu { width: 250px; }
.saved-signs { display: flex; gap: 8px; margin: 0 0 12px; overflow-x: auto; padding-bottom: 2px; }
.ssign { position: relative; flex: none; width: 124px; height: 58px; background: #fff; border: 1px solid var(--border); cursor: pointer; }
.ssign:hover { border-color: var(--accent); }
.ssign img { width: 100%; height: 100%; object-fit: contain; padding: 5px; display: block; }
.ssign button { position: absolute; top: 0; right: 0; width: 20px; height: 20px; display: grid; place-items: center; background: rgba(0, 0, 0, .7); color: #fff; opacity: 0; }
.ssign:hover button, .ssign button:focus-visible { opacity: 1; }
.ssign button:hover { background: var(--danger); }
.ssign button svg.ic { width: 12px; height: 12px; }
label.check { display: inline-flex; align-items: center; gap: 6px; margin-left: 14px; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; cursor: pointer; }
label.check input { accent-color: var(--accent); margin: 0; }

@media (max-width: 760px) {
  .findbar { top: 106px; right: 8px; left: 8px; }
  .findbar input { flex: 1; width: auto; min-width: 0; }
}
