:root {
  --bg: #0f1a14; --bg2: #16241c; --card: #1d2f25; --line: #2b4234;
  --fg: #eef5f0; --muted: #93a99b; --acc: #34c77b; --acc2: #1f8f57; --warn: #f0b429; --err: #ef5350;
  --radius: 14px; --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); font: 16px/1.4 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; overscroll-behavior: none; }
body { display: flex; flex-direction: column; min-height: 100dvh; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; font-weight: 600; }
a { color: var(--acc); }
[hidden] { display: none !important; }

#topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 6px; padding: calc(8px + var(--sat)) 10px 8px; background: var(--bg2); border-bottom: 1px solid var(--line); }
#topbar h1 { font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#topbar .spacer { flex: 1; }
.icon-btn { background: none; border: 0; color: var(--fg); font-size: 24px; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; }
.icon-btn:active { background: var(--card); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #555; margin-right: 4px; }
.dot.ok { background: var(--acc); } .dot.busy { background: var(--warn); animation: pulse 1s infinite; } .dot.err { background: var(--err); }
@keyframes pulse { 50% { opacity: .3; } }

#view { flex: 1; display: flex; flex-direction: column; padding: 12px 12px calc(90px + var(--sab)); max-width: 900px; width: 100%; margin: 0 auto; }
#view.full { padding: 0; max-width: none; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.muted { color: var(--muted); font-size: 14px; }
.row { display: flex; align-items: center; gap: 10px; }
.btn { background: var(--acc2); color: #fff; border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn.primary { background: var(--acc); color: #062; }
.btn.ghost { background: transparent; border: 1px solid var(--line); color: var(--fg); }
.btn.danger { background: #5a1f1f; color: #ffd1d1; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
input[type=text], input[type=password], input[type=search], input[type=url], textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; color: var(--fg); outline: none;
}
input:focus { border-color: var(--acc2); }
label.field { display: block; margin: 10px 0; font-size: 14px; color: var(--muted); }
label.field input { margin-top: 4px; }

/* FAB */
#fab { position: fixed; right: 18px; bottom: calc(18px + var(--sab)); z-index: 9; width: 64px; height: 64px; border-radius: 50%; background: var(--acc); color: #042; border: 0; font-size: 30px; box-shadow: 0 8px 24px rgba(0,0,0,.45); }
#fab-menu { position: fixed; right: 18px; bottom: calc(92px + var(--sab)); z-index: 9; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
#fab-menu .btn { box-shadow: 0 6px 18px rgba(0,0,0,.4); }

/* Archivio */
.search { position: sticky; top: calc(57px + var(--sat)); z-index: 5; background: var(--bg); padding: 4px 0 8px; }
.day-head { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin: 14px 4px 6px; }
.doc { display: flex; gap: 12px; align-items: center; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; margin-bottom: 8px; cursor: pointer; }
.doc:active { background: var(--bg2); }
.doc img { width: 56px; height: 72px; object-fit: cover; border-radius: 6px; background: #fff; }
.doc .info { flex: 1; min-width: 0; }
.doc .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc .snip { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 10px; background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.badge.warn { color: var(--warn); border-color: var(--warn); }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; }
.empty .big { font-size: 56px; }

/* Crop / clean (schermo pieno) */
.stage { position: relative; flex: 1; background: #000; display: flex; align-items: center; justify-content: center; overflow: hidden; touch-action: none; min-height: 50vh; }
.stage canvas { max-width: 100%; max-height: 100%; display: block; }
#loupe { position: absolute; width: 120px; height: 120px; border-radius: 50%; border: 2px solid var(--acc); box-shadow: 0 4px 16px rgba(0,0,0,.6); pointer-events: none; display: none; background: #000; }
.toolbar { display: flex; gap: 8px; padding: 10px 12px calc(10px + var(--sab)); background: var(--bg2); border-top: 1px solid var(--line); align-items: center; }
.toolbar .btn { flex: 1; padding: 12px 8px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 12px 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: 0 0 auto; border: 1px solid var(--line); background: var(--card); color: var(--fg); border-radius: 20px; padding: 8px 14px; font-size: 14px; }
.chip.on { background: var(--acc); color: #042; border-color: var(--acc); font-weight: 600; }
.sliders { display: flex; gap: 14px; padding: 6px 14px; font-size: 13px; color: var(--muted); }
.sliders label { flex: 1; }
.sliders input[type=range] { width: 100%; accent-color: var(--acc); }

/* Documento */
.pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.page { position: relative; background: var(--card); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.page img { width: 100%; aspect-ratio: 3/4; object-fit: cover; display: block; background: #fff; }
.page .n { position: absolute; top: 6px; left: 6px; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; padding: 2px 7px; border-radius: 10px; }
.page .ocr { position: absolute; top: 6px; right: 6px; font-size: 12px; }
.page .acts { display: flex; }
.page .acts button { flex: 1; background: none; border: 0; border-top: 1px solid var(--line); padding: 7px 0; color: var(--muted); font-size: 16px; }
.page .acts button:active { color: var(--fg); }
.page.dragging { opacity: .4; }
.page.over { outline: 2px solid var(--acc); }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 14px 0; }
.actions .btn.wide { grid-column: 1 / -1; }

/* Busy / toast */
#busy { position: fixed; inset: 0; background: rgba(0,0,0,.7); z-index: 50; display: grid; place-items: center; }
#busy .card { min-width: 240px; text-align: center; }
#busy progress { width: 100%; margin-top: 10px; accent-color: var(--acc); }
.spin { width: 36px; height: 36px; border: 4px solid var(--line); border-top-color: var(--acc); border-radius: 50%; margin: 0 auto 10px; animation: rot .8s linear infinite; }
@keyframes rot { to { transform: rotate(360deg); } }
#toast { position: fixed; left: 50%; bottom: calc(100px + var(--sab)); transform: translateX(-50%); background: #000; color: #fff; padding: 10px 16px; border-radius: 20px; z-index: 60; max-width: 90vw; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.5); }

/* Viewer pagina */
.viewer { position: fixed; inset: 0; background: #000; z-index: 40; display: flex; flex-direction: column; }
.viewer .img { flex: 1; display: grid; place-items: center; overflow: auto; }
.viewer img { max-width: 100%; max-height: 100%; }

.share-help { font-size: 14px; color: var(--muted); margin-top: 8px; }
.share-help a { display: inline-block; margin-right: 12px; }
@media (min-width: 700px) { .pages { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); } }
