/* Archivio Documenti — stesso linguaggio visivo della contabilità, tema chiaro/scuro automatico. */
:root {
  color-scheme: light dark;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --surface-3: #eef2f6;
  --input-bg: #ffffff;
  --border: #dde3ea;
  --border-subtle: #eef1f4;
  --text: #1f2933;
  --text-2: #4a5663;
  --muted: #6b7885;
  --faint: #8e9aa6;
  --primary: #4a90e2;
  --primary-hover: #357abd;
  --on-primary: #ffffff;
  --link: #2f6fb8;
  --success-bg: #e6f4ea; --success-text: #1e6b34; --success-border: #b7e0c4;
  --danger-bg: #fdecea; --danger-text: #a4302e; --danger-border: #f5c2c0; --danger: #c0392b;
  --warning-bg: #fff8e6; --warning-text: #8a6100; --warning-border: #ffe0a3;
  --info-bg: #eef6ff; --info-text: #1f5c99; --info-border: #c3ddf7;
  --shadow: 0 4px 16px rgba(0, 0, 0, .06);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --surface: #1b1e23;
    --surface-2: #22262c;
    --surface-3: #2b3037;
    --input-bg: #15181c;
    --border: #414851;
    --border-subtle: #2b3037;
    --text: #e6e8eb;
    --text-2: #c4cad1;
    --muted: #9da6b0;
    --faint: #8b949f;
    --primary: #4a90e2;
    --primary-hover: #5b9ce8;
    --link: #7db3f3;
    --success-bg: #14301e; --success-text: #7fd69b; --success-border: #2a5b3b;
    --danger-bg: #3b1c1f; --danger-text: #ff938d; --danger-border: #6f3035; --danger: #d9534f;
    --warning-bg: #322910; --warning-text: #f0c46c; --warning-border: #6d571f;
    --info-bg: #172433; --info-text: #7db3f3; --info-border: #2c4a6a;
    --shadow: 0 1px 0 rgba(255, 255, 255, .03);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
p { margin: 0 0 .75rem; }
.sub { color: var(--muted); font-size: .92rem; }
[hidden] { display: none !important; }

/* --- barra in alto ------------------------------------------------------ */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border-subtle);
}
.brand { font-weight: 700; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.topnav { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.topnav::-webkit-scrollbar { display: none; }
.topnav a { padding: 7px 10px; border-radius: 8px; color: var(--text-2); font-size: .93rem; white-space: nowrap; }
.topnav a:hover { background: var(--surface-3); text-decoration: none; }
.topnav a[aria-current="page"] { background: var(--surface-3); color: var(--text); font-weight: 600; }
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; }
  .topnav { order: 3; flex-basis: 100%; }
  .brand { flex: 1; }
}

.wrap { max-width: 1040px; margin: 0 auto; padding: 16px 16px 96px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}

/* --- pulsanti e campi --------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px; padding: 9px 16px;
  border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: .95rem; font-weight: 500;
  cursor: pointer; text-decoration: none; white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); font-weight: 600; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-text { color: var(--danger-text); }
.btn.small { min-height: 34px; padding: 5px 11px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn.big { min-height: 64px; font-size: 1.08rem; flex: 1; }

input[type=text], input[type=search], input[type=password], input[type=date], select, textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border); border-radius: 9px;
  background: var(--input-bg); color: var(--text);
  font: inherit; font-size: 16px; /* 16px: iOS non zooma entrando nel campo */
}
textarea { resize: vertical; min-height: 80px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(74, 144, 226, .18); }
::placeholder { color: var(--faint); opacity: 1; }
label { color: var(--text-2); }
.field { margin-bottom: 14px; }
.field > label, .field > .label { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.check { display: inline-flex; align-items: center; gap: 7px; font-size: .95rem; cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--primary); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 16px; }
.actions.end { justify-content: flex-end; }
.actions .btn.primary { margin-left: auto; }
.inline-form { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin: 12px 0 16px; }
.inline-form input[type=text], .inline-form select { flex: 1 1 200px; width: auto; }
.stack-form { display: grid; gap: 10px; max-width: 420px; }

/* --- chip --------------------------------------------------------------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 13px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-2);
  font-size: .93rem; cursor: pointer; user-select: none;
}
.chip input { display: none; }
.chip:has(input:checked), .chip.on, .chips.pick .chip[aria-pressed="true"] {
  background: var(--primary); border-color: var(--primary); color: #fff;
}
.chip.cat { background: var(--surface-3); color: var(--text-2); }
.chip.suggested { border-style: dashed; border-color: var(--primary); }
.chips.static .chip { cursor: default; }
a.chip:hover { text-decoration: none; filter: brightness(1.05); }
.chips.pick .chip { min-height: 40px; }

/* --- avvisi ------------------------------------------------------------- */
.notice { padding: 11px 14px; border-radius: 10px; margin-bottom: 14px; border: 1px solid var(--info-border); background: var(--info-bg); color: var(--info-text); }
.notice.success { background: var(--success-bg); color: var(--success-text); border-color: var(--success-border); }
.notice.error { background: var(--danger-bg); color: var(--danger-text); border-color: var(--danger-border); }
.notice.warning { background: var(--warning-bg); color: var(--warning-text); border-color: var(--warning-border); }
.notice .btn { margin-left: 8px; }
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: min(92vw, 520px); padding: 12px 16px; border-radius: 10px;
  background: var(--text); color: var(--bg); box-shadow: 0 8px 24px rgba(0,0,0,.2); z-index: 50; font-size: .95rem;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.warn { background: #b07a00; color: #fff; }

/* --- archivio ----------------------------------------------------------- */
.search .nl-form { display: flex; gap: 10px; }
.search .nl-form input { flex: 1; }
.filters { margin-top: 12px; }
.filters summary { cursor: pointer; color: var(--muted); font-size: .92rem; }
.filter-form { margin-top: 12px; display: grid; gap: 12px; }
.filter-row { display: flex; flex-wrap: wrap; gap: 10px; }
.filter-row select, .filter-row input[type=search] { flex: 1 1 180px; width: auto; }
.filter-row .inline { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; color: var(--muted); }
.filter-row .inline input { width: auto; }
.result-head { margin: 4px 2px 12px; color: var(--text-2); }
.result-head .clear { margin-left: 6px; font-size: .9rem; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); gap: 14px; }
.tile { display: flex; flex-direction: column; color: var(--text); min-width: 0; }
.tile:hover { text-decoration: none; }
.tile .thumb {
  position: relative; display: flex; align-items: center; justify-content: center;
  aspect-ratio: 3 / 4; overflow: hidden;
  background: var(--surface-3); border: 1px solid var(--border-subtle); border-radius: 10px;
}
.tile .thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; }
.tile:hover .thumb { border-color: var(--primary); }
.pages-badge {
  position: absolute; right: 6px; bottom: 6px; padding: 2px 7px; border-radius: 6px;
  background: rgba(0, 0, 0, .62); color: #fff; font-size: .75rem;
}
.tile-title {
  margin-top: 7px; font-weight: 600; font-size: .93rem; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.tile-sub { font-size: .8rem; color: var(--muted); margin-top: 2px; }
.file-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 64px; height: 80px; padding: 0 10px;
  border: 2px solid var(--border); border-radius: 6px 18px 6px 6px;
  background: var(--surface); color: var(--muted); font-weight: 700; letter-spacing: .04em;
}
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; margin: 22px 0; color: var(--muted); }
.empty { text-align: center; color: var(--text-2); }
.big-emoji { font-size: 2.6rem; margin: 6px 0; }
.fab {
  position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 20;
  width: 62px; height: 62px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--primary); color: #fff; font-size: 1.7rem;
  box-shadow: 0 8px 22px rgba(74, 144, 226, .45);
}
.fab:hover { text-decoration: none; background: var(--primary-hover); }
@media (min-width: 900px) { .fab { display: none; } }
@media (max-width: 640px) { .new-btn { display: none; } }

/* --- nuovo documento ---------------------------------------------------- */
.capture { display: flex; gap: 12px; flex-wrap: wrap; margin: 16px 0; }
.pages { list-style: none; margin: 0 0 12px; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 12px; }
.pages .page {
  position: relative; display: flex; flex-direction: column; gap: 6px;
  padding: 8px; border: 1px solid var(--border-subtle); border-radius: 10px; background: var(--surface-2);
}
.pages .page .pthumb {
  display: flex; align-items: center; justify-content: center; aspect-ratio: 3 / 4;
  background: var(--surface-3); border-radius: 6px; overflow: hidden;
}
.pages .page img { width: 100%; height: 100%; object-fit: cover; object-position: top; }
.pages .page .pname { font-size: .78rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pages .page .pacts { display: flex; gap: 4px; }
.pages .page .pacts button {
  flex: 1; min-height: 32px; border: 1px solid var(--border); border-radius: 7px;
  background: var(--surface); color: var(--text-2); font-size: .9rem; cursor: pointer;
}
.pages .page .pacts button:disabled { opacity: .35; }
.pages .page .pnum {
  position: absolute; left: 12px; top: 12px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: rgba(0,0,0,.62); color: #fff; font-size: .75rem; display: flex; align-items: center; justify-content: center;
}
.pages.compact { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
.progress { height: 6px; background: var(--surface-3); border-radius: 3px; overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--primary); transition: width .2s; }
.ai-status { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; padding: 10px 12px; border-radius: 9px; background: var(--info-bg); color: var(--info-text); font-size: .93rem; margin: 14px 0; }
.ai-status.ok { background: var(--success-bg); color: var(--success-text); }
.ai-status.err { background: var(--danger-bg); color: var(--danger-text); }
.spinner { width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; flex-shrink: 0; }
@keyframes spin { to { transform: rotate(360deg); } }
.add-person { margin-top: 14px; }
.add-person summary { cursor: pointer; color: var(--link); }
.add-person-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 10px; }
.add-person-form input[type=text] { flex: 1 1 200px; width: auto; }

/* --- documento ---------------------------------------------------------- */
.doc-head .back { font-size: .92rem; }
.doc-head h1 { margin-top: 8px; }
.doc-head .chips { margin: 10px 0 4px; }
.doc-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.desc { white-space: normal; color: var(--text); }
.save-state { color: var(--muted); font-size: .88rem; margin-right: auto; }
.pages-view { display: grid; gap: 16px; }
.page-view { padding: 10px; margin: 0; }
.page-view img { display: block; width: 100%; height: auto; border-radius: 6px; background: #fff; }
.page-open { display: block; width: 100%; padding: 0; border: 0; background: none; cursor: zoom-in; -webkit-tap-highlight-color: transparent; }
.linklike { padding: 0; border: 0; background: none; color: var(--link); font: inherit; cursor: pointer; }
.linklike:hover { text-decoration: underline; }
.page-view figcaption { margin-top: 8px; color: var(--muted); font-size: .88rem; text-align: center; }
.file-card { display: flex; align-items: center; gap: 14px; margin: 0; }
.file-card > div { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.file-card .file-badge { height: 56px; min-width: 48px; font-size: .8rem; }

/* --- elenchi (persone, categorie, cestino) ------------------------------ */
.rows { list-style: none; margin: 0; padding: 0; }
.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 0; border-top: 1px solid var(--border-subtle); }
.row .name { flex: 1 1 180px; width: auto; }
.row .count { font-size: .88rem; white-space: nowrap; }
.row .grow { flex: 1 1 200px; font-weight: 600; }
.row.muted { opacity: .6; }

/* --- accesso ------------------------------------------------------------ */
.login { max-width: 380px; margin: 12vh auto 0; }
.login h1 { text-align: center; margin-bottom: 18px; }
.login label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 6px; }
.login .btn { margin-top: 14px; }
.uploads { display: grid; gap: 8px; margin-bottom: 12px; }
.uploads .page { display: grid; gap: 6px; padding: 10px 12px; border: 1px solid var(--border-subtle); border-radius: 10px; background: var(--surface-2); }
.uploads .pname { font-size: .85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- visore delle pagine (sempre scuro: si legge la carta, non l'interfaccia) --- */
html.viewer-open, html.viewer-open body { overflow: hidden; }
.viewer {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column;
  background: #0b0c0e; color: #fff;
}
.viewer-bar {
  position: relative; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right)) 8px calc(10px + env(safe-area-inset-left));
  background: rgba(0, 0, 0, .6);
}
.viewer-btn {
  min-height: 44px; padding: 8px 14px; border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .28); background: rgba(255, 255, 255, .1); color: #fff;
  font: inherit; font-size: .95rem; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.viewer-btn:disabled { opacity: .5; }
.viewer-count { font-size: .92rem; opacity: .85; text-align: center; }
.viewer-stage { position: relative; flex: 1; overflow: hidden; touch-action: none; cursor: grab; }
.viewer-stage:active { cursor: grabbing; }
.viewer-img {
  position: absolute; left: 0; top: 0; max-width: none;
  transform-origin: 0 0; background: #fff;
  user-select: none; -webkit-user-select: none; -webkit-user-drag: none; -webkit-touch-callout: none;
}
.viewer-nav {
  position: absolute; top: 50%; z-index: 2; transform: translateY(-50%);
  width: 46px; height: 72px; border: 0; border-radius: 12px;
  background: rgba(0, 0, 0, .45); color: #fff; font-size: 2rem; line-height: 1; cursor: pointer;
}
.viewer-nav.prev { left: calc(8px + env(safe-area-inset-left)); }
.viewer-nav.next { right: calc(8px + env(safe-area-inset-right)); }
.viewer-nav:disabled { display: none; }
@media (hover: none) { .viewer-nav { display: none; } } /* sul telefono si scorre col dito */
.viewer-hint {
  position: absolute; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 2;
  transform: translateX(-50%); margin: 0; padding: 7px 12px; border-radius: 9px;
  background: rgba(0, 0, 0, .6); color: #fff; font-size: .85rem; white-space: nowrap; pointer-events: none;
}
