:root {
  --paper: #f5f1ea;
  --card: #fffdf9;
  --ink: #1c1a17;
  --ink-2: #4a4640;
  --muted: #8a847a;
  --line: #e6dfd3;
  --line-2: #d9d0c1;
  --accent: #6e1f2a;
  --accent-soft: #f1e3e1;
  --now: #b3261e;
  --now-soft: #f7e2df;
  --high: #a8650a;
  --high-soft: #f6ead6;
  --normal: #4b5e52;
  --normal-soft: #e5ebe5;
  --ref: #50607f;
  --ref-soft: #e3e7ef;
  --ok: #2f6b45;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", "Segoe UI", system-ui, sans-serif;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(40, 30, 20, 0.05), 0 6px 20px rgba(40, 30, 20, 0.05);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #151412;
    --card: #1e1c19;
    --ink: #eee8de;
    --ink-2: #c9c2b6;
    --muted: #8f887c;
    --line: #2d2a25;
    --line-2: #3a362f;
    --accent: #d9a0a6;
    --accent-soft: #3a2427;
    --now: #f08a80;
    --now-soft: #3d2220;
    --high: #e0a85a;
    --high-soft: #3a2d1b;
    --normal: #9fb7a6;
    --normal-soft: #24302a;
    --ref: #a8b6d6;
    --ref-soft: #252b38;
    --ok: #8cc79f;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; }
button, input, textarea, select { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Struttura ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 24px;
  padding: calc(var(--safe-t) + 14px) 20px 12px;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--line);
}
.brand { font-family: var(--serif); font-size: 21px; letter-spacing: 0.01em; text-decoration: none; font-weight: 600; }
.topnav { display: none; gap: 4px; margin-left: auto; }
.topnav a { text-decoration: none; padding: 6px 12px; border-radius: 999px; color: var(--ink-2); font-size: 15px; position: relative; }
.topnav a.active { background: var(--ink); color: var(--paper); }
.top-search { margin-left: auto; display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%; color: var(--ink-2); }
.top-search:hover { background: var(--line); }

main { max-width: 860px; margin: 0 auto; padding: 18px 16px calc(110px + var(--safe-b)); outline: none; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  padding: 6px 8px calc(var(--safe-b) + 6px);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 11px; position: relative; padding: 4px 0; }
.tabbar a.active { color: var(--ink); }
.tab-add {
  justify-self: center; width: 52px; height: 52px; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; display: grid; place-items: center; box-shadow: 0 6px 18px rgba(110, 31, 42, 0.3);
}
@media (prefers-color-scheme: dark) { .tab-add { color: #1e1c19; } }
.tab-add svg { width: 26px; height: 26px; stroke-width: 2; }
.dot-count:empty { display: none; }
.dot-count { position: absolute; top: 0; right: calc(50% - 22px); min-width: 17px; height: 17px; padding: 0 5px; border-radius: 9px; background: var(--now); color: #fff; font-size: 10.5px; line-height: 17px; text-align: center; font-weight: 600; }
.topnav .dot-count { top: -2px; right: -4px; }

@media (min-width: 820px) {
  .topnav { display: flex; }
  .top-search { display: none; }
  .tabbar { display: none; }
  main { padding-bottom: 60px; }
  .fab { display: grid !important; }
}
.fab {
  display: none; position: fixed; right: 28px; bottom: 28px; z-index: 15; place-items: center;
  width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; box-shadow: 0 8px 24px rgba(110, 31, 42, 0.3); cursor: pointer;
}

/* ---------- Tipografia ---------- */
h1, h2, h3 { font-family: var(--serif); font-weight: 600; letter-spacing: 0.005em; }
h1 { font-size: 30px; line-height: 1.15; margin: 6px 0 4px; }
h2 { font-size: 13px; font-family: var(--sans); text-transform: uppercase; letter-spacing: 0.09em; color: var(--muted); margin: 30px 0 10px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
h2 .count { color: var(--ink-2); }
h3 { font-size: 19px; margin: 0; }
.eyebrow { color: var(--muted); font-size: 14px; }
.eyebrow::first-letter { text-transform: uppercase; }
.muted { color: var(--muted); }
.small { font-size: 13.5px; }
.lead { color: var(--ink-2); margin: 4px 0 0; }

/* ---------- Componenti ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.stack { display: grid; gap: 10px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spacer { flex: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; padding: 0 16px;
  border-radius: 999px; border: 1px solid var(--line-2); background: var(--card); cursor: pointer; text-decoration: none; font-size: 15px; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn.accent { background: var(--accent); color: #fff; border-color: var(--accent); }
@media (prefers-color-scheme: dark) { .btn.accent { color: #1e1c19; } }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn.danger { color: var(--now); }
.btn.small { min-height: 32px; padding: 0 12px; font-size: 13.5px; }
.btn[disabled] { opacity: 0.5; pointer-events: none; }
.btn svg { width: 18px; height: 18px; }

input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="search"], textarea, select {
  width: 100%; border: 1px solid var(--line-2); background: var(--card); border-radius: 12px; padding: 11px 13px; font-size: 16px; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--ink-2); }
textarea { resize: vertical; min-height: 90px; line-height: 1.5; }
label.field { display: grid; gap: 6px; font-size: 14px; color: var(--ink-2); }

.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12.5px;
  background: var(--paper); border: 1px solid var(--line); color: var(--ink-2); white-space: nowrap; text-decoration: none;
}
.chip.proposed { border-style: dashed; }
.chip.on { background: var(--ink); color: var(--paper); border-color: var(--ink); }
button.chip { cursor: pointer; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.chips.scroll::-webkit-scrollbar { display: none; }

.prio { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; letter-spacing: 0.03em; text-transform: uppercase; }
.prio::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.p-now { color: var(--now); }
.p-high { color: var(--high); }
.p-normal { color: var(--normal); }
.p-reference { color: var(--ref); }

.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 8px; border-radius: 6px; background: var(--paper); color: var(--ink-2); border: 1px solid var(--line); }
.badge.warn { background: var(--now-soft); color: var(--now); border-color: transparent; }
.badge.info { background: var(--ref-soft); color: var(--ref); border-color: transparent; }
.badge.link { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge svg { width: 13px; height: 13px; }

/* ---------- Nota ---------- */
.note {
  position: relative; display: grid; gap: 8px; padding: 14px 16px 14px 18px; cursor: pointer; text-align: left; width: 100%;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.note:hover { border-color: var(--line-2); }
.note:active { transform: scale(0.995); }
.note::before { content: ""; position: absolute; left: 0; top: 14px; bottom: 14px; width: 3px; border-radius: 0 3px 3px 0; background: var(--line-2); }
.note.now::before { background: var(--now); }
.note.high::before { background: var(--high); }
.note.reference::before { background: var(--ref); opacity: 0.6; }
.note.pending { opacity: 0.85; }
.note-title { font-family: var(--serif); font-size: 18px; line-height: 1.3; font-weight: 600; margin: 0; }
.note-summary { color: var(--ink-2); font-size: 14.5px; line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; }
.note-usage { font-size: 13.5px; color: var(--accent); }
.note-meta { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.note-thumbs { display: flex; gap: 6px; }
.note-thumbs img, .note-thumbs .file { width: 52px; height: 52px; border-radius: 8px; object-fit: cover; background: var(--paper); border: 1px solid var(--line); }
.note-thumbs .file { display: grid; place-items: center; font-size: 10px; color: var(--muted); text-transform: uppercase; font-weight: 600; }
.note.compact { padding: 11px 14px 11px 16px; gap: 4px; }
.note.compact .note-title { font-size: 16px; }

.group-label { font-family: var(--serif); font-size: 16px; margin: 18px 0 8px; color: var(--ink-2); display: flex; gap: 8px; align-items: baseline; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 16px; }
.stat { padding: 12px; text-decoration: none; display: grid; gap: 2px; }
.stat b { font-family: var(--serif); font-size: 26px; line-height: 1; font-weight: 600; }
.stat span { font-size: 12px; color: var(--muted); }
.stat.now b { color: var(--now); }
.stat.high b { color: var(--high); }

.banner { display: flex; gap: 12px; align-items: flex-start; padding: 13px 15px; border-radius: 12px; margin-top: 12px; font-size: 14.5px; }
.banner.low { background: var(--high-soft); color: var(--ink); }
.banner.empty, .banner.error { background: var(--now-soft); color: var(--ink); }
.banner.info { background: var(--ref-soft); }
.banner b { display: block; }
.banner .btn { margin-top: 8px; }

.empty-state { text-align: center; color: var(--muted); padding: 30px 20px; font-size: 15px; }
.empty-state .serif { font-family: var(--serif); font-size: 19px; color: var(--ink-2); display: block; margin-bottom: 4px; }

/* ---------- Cattura ---------- */
.capture { padding: 12px; margin-top: 16px; }
.capture textarea { border: 0; padding: 4px 4px; min-height: 64px; background: transparent; resize: none; }
.capture-bar { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.attach-previews { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0 2px; }
.attach-previews .pv { position: relative; width: 64px; height: 64px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: var(--paper); display: grid; place-items: center; font-size: 10px; color: var(--muted); text-align: center; padding: 2px; word-break: break-all; }
.attach-previews img { width: 100%; height: 100%; object-fit: cover; position: absolute; inset: 0; }
.attach-previews .x { position: absolute; top: 2px; right: 2px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: rgba(0,0,0,0.6); color: #fff; font-size: 12px; line-height: 20px; cursor: pointer; z-index: 1; }

/* ---------- Ambiti ---------- */
.area-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 14px 16px; text-decoration: none; align-items: center; }
.area-row .name { font-family: var(--serif); font-size: 18px; font-weight: 600; }
.area-row .sub { font-size: 13px; color: var(--muted); grid-column: 1 / -1; display: flex; gap: 12px; flex-wrap: wrap; }
.area-row .nums { display: flex; gap: 6px; }
.area-children { display: grid; gap: 6px; margin: 6px 0 0 18px; }
.area-children .area-row { padding: 10px 14px; }
.area-children .area-row .name { font-size: 16px; }
.num { min-width: 28px; height: 24px; padding: 0 7px; border-radius: 7px; font-size: 13px; font-weight: 600; display: inline-grid; place-items: center; }
.num.now { background: var(--now-soft); color: var(--now); }
.num.high { background: var(--high-soft); color: var(--high); }
.num.all { background: var(--paper); color: var(--ink-2); border: 1px solid var(--line); }

/* ---------- Media ---------- */
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-top: 14px; }
.tile { position: relative; aspect-ratio: 1; border-radius: 12px; overflow: hidden; background: var(--card); border: 1px solid var(--line); cursor: pointer; padding: 0; }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile .cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 22px 9px 7px; font-size: 12px; line-height: 1.3; color: #fff; background: linear-gradient(transparent, rgba(0,0,0,0.72)); text-align: left; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.tile .file { display: grid; place-items: center; height: 100%; font-weight: 700; color: var(--muted); font-size: 14px; text-transform: uppercase; }
.media-strip { display: flex; gap: 8px; overflow-x: auto; margin: 12px -16px 0; padding: 0 16px 4px; scrollbar-width: none; }
.media-strip .tile { flex: 0 0 104px; }

/* ---------- Pannello (dettaglio, cattura, scelta) ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(20, 16, 12, 0.38); animation: fade 0.18s ease; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--paper); border-radius: 20px 20px 0 0; padding: 8px 18px calc(var(--safe-b) + 24px);
  animation: up 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: var(--line-2); margin: 4px auto 12px; }
@media (min-width: 820px) {
  .sheet-panel { left: 50%; right: auto; bottom: auto; top: 6vh; width: 680px; transform: translateX(-50%); border-radius: 20px; max-height: 88vh; animation: fade 0.18s ease; }
  .sheet-grip { display: none; }
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.detail-head { display: flex; gap: 8px; align-items: center; justify-content: space-between; position: sticky; top: -8px; background: var(--paper); padding: 6px 0 10px; z-index: 2; }
.seg { display: inline-flex; background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 11px; border-radius: 999px; font-size: 13px; cursor: pointer; color: var(--ink-2); }
.seg button.on.p-now { background: var(--now); color: #fff; }
.seg button.on.p-high { background: var(--high); color: #fff; }
.seg button.on.p-normal { background: var(--normal); color: #fff; }
.seg button.on.p-reference { background: var(--ref); color: #fff; }
.detail-title { font-family: var(--serif); font-size: 25px; line-height: 1.2; margin: 6px 0 10px; font-weight: 600; }
.detail-summary { font-size: 16px; line-height: 1.6; color: var(--ink); }
.detail-summary ul { margin: 6px 0; padding-left: 20px; }
.detail-summary p { margin: 0 0 8px; }
.detail-section { margin-top: 22px; }
.detail-section > h2 { margin-top: 0; }
.actions-list { display: grid; gap: 2px; }
.action { display: flex; gap: 10px; align-items: flex-start; padding: 8px 2px; cursor: pointer; font-size: 15.5px; }
.action input { width: 20px; height: 20px; margin-top: 2px; accent-color: var(--ok); flex: none; }
.action.done span { text-decoration: line-through; color: var(--muted); }
.gallery { display: grid; gap: 12px; }
.gallery figure { margin: 0; }
.gallery img { width: 100%; border-radius: 12px; border: 1px solid var(--line); display: block; background: var(--card); }
.gallery figcaption { font-size: 14px; color: var(--ink-2); margin-top: 6px; }
.gallery .filecard { display: flex; gap: 12px; align-items: center; padding: 12px 14px; }
.area-usage { display: grid; gap: 6px; }
.area-usage .item { display: grid; gap: 2px; justify-items: start; }
.area-usage .item .u { font-size: 14px; color: var(--ink-2); padding-left: 2px; }
details.raw { margin-top: 18px; }
details.raw summary { cursor: pointer; color: var(--muted); font-size: 14px; }
details.raw pre { white-space: pre-wrap; font: 14.5px/1.55 var(--sans); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin: 8px 0 0; }
.meta-line { font-size: 13px; color: var(--muted); margin-top: 18px; display: flex; gap: 12px; flex-wrap: wrap; }
.check-list { display: grid; gap: 8px; }
.check-item { display: grid; grid-template-columns: auto 1fr; gap: 6px 10px; align-items: center; padding: 10px 12px; }
.check-item input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
.check-item .usage { grid-column: 2; }
.check-item .usage input { padding: 7px 10px; font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 92px); transform: translate(-50%, 20px); z-index: 80;
  background: var(--ink); color: var(--paper); padding: 11px 18px; border-radius: 999px; font-size: 14.5px; opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease; max-width: calc(100vw - 40px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 820px) { .toast { bottom: 30px; } }

.menu-list { display: grid; }
.menu-list a, .menu-list button { display: flex; align-items: center; gap: 12px; padding: 15px 16px; text-decoration: none; border: 0; background: none; width: 100%; text-align: left; cursor: pointer; font-size: 16px; border-bottom: 1px solid var(--line); }
.menu-list > :last-child { border-bottom: 0; }
.menu-list .right { margin-left: auto; color: var(--muted); font-size: 14px; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login .card { width: min(380px, 100%); padding: 28px 24px; display: grid; gap: 14px; text-align: center; }
.login h1 { margin: 0; }

.progress { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width 0.3s ease; }
.code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; word-break: break-all; background: var(--card); border: 1px solid var(--line); padding: 10px 12px; border-radius: 10px; user-select: all; }
.settings-block { padding: 16px; display: grid; gap: 12px; }
.settings-block h3 { font-size: 18px; }
ol.steps { padding-left: 20px; margin: 0; display: grid; gap: 6px; font-size: 14.5px; color: var(--ink-2); }
.spinner { width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
body.logged-out .fab, body.logged-out .tabbar { display: none !important; }

.area-row .sub .desc { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; width: 100%; }
details.area-edit > summary { list-style: none; display: flex; justify-content: space-between; gap: 10px; padding: 14px 16px; cursor: pointer; font-family: var(--serif); font-size: 17px; font-weight: 600; }
details.area-edit > summary::-webkit-details-marker { display: none; }
details.area-edit[open] > summary { padding-bottom: 8px; }
