:root {
  --bg: #ffffff; --bg-soft: #f7f7f5; --card: #ffffff;
  --hover: rgba(55,53,47,.06); --active: rgba(55,53,47,.1);
  --border: rgba(55,53,47,.12); --border-strong: rgba(55,53,47,.22);
  --text: #37352f; --text-2: rgba(55,53,47,.68); --text-3: rgba(55,53,47,.45);
  --accent: #2383e2; --accent-soft: rgba(35,131,226,.12);
  --danger: #e03e3e; --ok: #30a46c; --warn: #f76b15;
  --callout: #f1f1ef; --code-bg: rgba(135,131,120,.15);
  --p1: #0090ff; --p2: #f76b15; --p3: #e5484d;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, monospace;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  --tab-h: 56px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191919; --bg-soft: #202020; --card: #232323;
    --hover: rgba(255,255,255,.06); --active: rgba(255,255,255,.1);
    --border: rgba(255,255,255,.1); --border-strong: rgba(255,255,255,.18);
    --text: rgba(255,255,255,.88); --text-2: rgba(255,255,255,.62); --text-3: rgba(255,255,255,.4);
    --accent: #529cca; --accent-soft: rgba(82,156,202,.2);
    --callout: #262626; --code-bg: rgba(135,131,120,.22);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg-soft); color: var(--text); font: 16px/1.45 var(--font); -webkit-text-size-adjust: 100%; }
body { overscroll-behavior-y: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea, select { font: inherit; color: var(--text); font-size: 16px; }
a { color: var(--accent); }
.muted { color: var(--text-2); } .small { font-size: 13px; } .faint { color: var(--text-3); }
.hidden { display: none !important; }

/* ---------- struttura ---------- */
#root { height: 100%; }
.app { display: flex; flex-direction: column; height: 100%; }
.topbar { position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--bg-soft) 88%, transparent); backdrop-filter: saturate(1.6) blur(14px); -webkit-backdrop-filter: saturate(1.6) blur(14px); padding: calc(var(--safe-t) + 8px) 16px 8px; display: flex; align-items: center; gap: 10px; min-height: calc(var(--safe-t) + 52px); border-bottom: 1px solid transparent; }
.topbar.scrolled { border-bottom-color: var(--border); }
.topbar h1 { font-size: 22px; font-weight: 700; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; letter-spacing: -.01em; }
.topbar .back { font-size: 17px; color: var(--accent); display: flex; align-items: center; gap: 2px; margin-left: -6px; padding: 6px; }
.top-btn { width: 38px; height: 38px; border-radius: 19px; display: grid; place-items: center; color: var(--accent); }
.top-btn:active { background: var(--hover); }
.top-btn svg, .tab svg, .ico { width: 22px; height: 22px; }
.main { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 16px calc(var(--tab-h) + var(--safe-b) + 24px); }
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: grid; grid-template-columns: repeat(5, 1fr); height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b); background: color-mix(in srgb, var(--bg) 90%, transparent); backdrop-filter: saturate(1.6) blur(14px); -webkit-backdrop-filter: saturate(1.6) blur(14px); border-top: 1px solid var(--border); }
.tab { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 10.5px; color: var(--text-3); position: relative; }
.tab.active { color: var(--accent); }
.tab .badge { position: absolute; top: 5px; left: calc(50% + 6px); background: var(--danger); color: #fff; font-size: 10px; font-weight: 700; min-width: 17px; height: 17px; border-radius: 9px; display: grid; place-items: center; padding: 0 4px; }
.sync-dot { font-size: 12px; color: var(--text-3); display: flex; align-items: center; gap: 5px; white-space: nowrap; }
.sync-dot.err { color: var(--danger); }
.sync-dot .spin { animation: spin 1s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- elementi comuni ---------- */
.card { background: var(--card); border-radius: 14px; padding: 4px 0; margin: 10px 0 16px; box-shadow: 0 0 0 1px var(--border); overflow: hidden; }
.card-pad { padding: 14px 16px; }
.sec-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--text-3); margin: 22px 4px 6px; display: flex; justify-content: space-between; align-items: baseline; }
.sec-title button { text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--accent); font-size: 14px; }
.row { display: flex; align-items: center; gap: 12px; padding: 11px 16px; min-height: 48px; position: relative; width: 100%; text-align: left; }
.row + .row::before { content: ''; position: absolute; top: 0; left: 16px; right: 0; border-top: 1px solid var(--border); }
.row.indent + .row::before, .row + .row.indent::before { left: 50px; }
button.row:active, .row.tap:active { background: var(--hover); }
.row-main { flex: 1; min-width: 0; }
.row-title { overflow-wrap: anywhere; }
.row-sub { font-size: 13px; color: var(--text-2); margin-top: 1px; display: flex; flex-wrap: wrap; gap: 4px 10px; }
.row-sub .late { color: var(--danger); }
.chev { color: var(--text-3); font-size: 20px; line-height: 1; }
.empty { padding: 22px 16px; text-align: center; color: var(--text-3); font-size: 14px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 1px 7px; border-radius: 6px; background: var(--hover); color: var(--text-2); }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; display: inline-block; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 11px 16px; border-radius: 12px; background: var(--hover); font-weight: 500; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; }
.check { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--border-strong); flex: none; display: grid; place-items: center; color: #fff; transition: background .15s, border-color .15s; }
.check.p1 { border-color: var(--p1); } .check.p2 { border-color: var(--p2); } .check.p3 { border-color: var(--p3); }
.check.on { background: var(--ok); border-color: var(--ok); }
.check.on::after { content: '✓'; font-size: 14px; font-weight: 700; }
.done .row-title { color: var(--text-3); text-decoration: line-through; }

/* ---------- campi ---------- */
.quick { display: flex; gap: 8px; margin: 8px 0 4px; }
.quick input { flex: 1; min-width: 0; border: 0; border-radius: 12px; padding: 12px 14px; background: var(--card); box-shadow: 0 0 0 1px var(--border); outline: none; }
.quick input:focus { box-shadow: 0 0 0 2px var(--accent); }
.quick button { width: 46px; border-radius: 12px; background: var(--accent); color: #fff; font-size: 24px; line-height: 1; }
.form { display: flex; flex-direction: column; gap: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-2); margin: 0 0 5px 2px; }
.field input, .field textarea, .field select { width: 100%; border: 0; border-radius: 11px; padding: 11px 13px; background: var(--bg-soft); box-shadow: inset 0 0 0 1px var(--border); outline: none; -webkit-appearance: none; appearance: none; }
.field select { background-image: linear-gradient(45deg, transparent 50%, var(--text-3) 50%), linear-gradient(135deg, var(--text-3) 50%, transparent 50%); background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
.field input:focus, .field textarea:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.field textarea { min-height: 90px; resize: vertical; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field input[type=date], .field input[type=time] { min-height: 44px; }
.seg { display: flex; background: var(--hover); border-radius: 10px; padding: 2px; }
.seg button { flex: 1; padding: 7px 6px; border-radius: 8px; font-size: 14px; color: var(--text-2); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); font-weight: 500; }
.switch-row { display: flex; align-items: center; justify-content: space-between; }
.toggle { width: 51px; height: 31px; border-radius: 16px; background: var(--border-strong); position: relative; flex: none; transition: background .2s; }
.toggle::after { content: ''; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s; }
.toggle.on { background: var(--ok); } .toggle.on::after { transform: translateX(20px); }

/* ---------- foglio (sheet) dal basso ---------- */
.sheet-back { position: fixed; inset: 0; background: rgba(0,0,0,.32); z-index: 50; opacity: 0; transition: opacity .2s; }
.sheet-back.show { opacity: 1; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; background: var(--card); border-radius: 16px 16px 0 0; max-height: calc(100% - var(--safe-t) - 30px); display: flex; flex-direction: column; transform: translateY(100%); transition: transform .26s cubic-bezier(.2,.8,.2,1); box-shadow: 0 -4px 30px rgba(0,0,0,.18); }
.sheet.show { transform: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.sheet-head h2 { font-size: 17px; margin: 0; font-weight: 600; flex: 1; text-align: center; }
.sheet-head button { color: var(--accent); font-size: 17px; min-width: 70px; }
.sheet-head button:last-child { text-align: right; font-weight: 600; }
.sheet-head button:first-child { text-align: left; }
.sheet-body { overflow-y: auto; padding: 16px 16px calc(20px + var(--safe-b)); }

/* ---------- avvisi ---------- */
#toasts { position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + var(--safe-b) + 12px); z-index: 80; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast { background: rgba(30,30,30,.92); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: calc(100% - 32px); opacity: 0; transform: translateY(8px); transition: all .2s; pointer-events: auto; display: flex; gap: 12px; align-items: center; }
.toast.show { opacity: 1; transform: none; }
.toast.error { background: #c4302b; }
.toast button { color: #8cc8ff; font-weight: 600; }

/* ---------- accesso ---------- */
.login { min-height: 100%; display: flex; flex-direction: column; justify-content: center; padding: calc(var(--safe-t) + 30px) 24px calc(var(--safe-b) + 30px); max-width: 420px; margin: 0 auto; }
.login img { width: 84px; height: 84px; margin: 0 auto 14px; display: block; }
.login h1 { text-align: center; margin: 0 0 6px; font-size: 28px; }
.login p { text-align: center; color: var(--text-2); margin: 0 0 22px; }
.login .err { color: var(--danger); text-align: center; font-size: 14px; min-height: 20px; margin-top: 10px; }
.login .note { font-size: 13px; color: var(--text-3); text-align: left; margin-top: 26px; }

/* ---------- oggi ---------- */
.today-head { margin: 4px 4px 4px; }
.today-head .date { font-size: 15px; color: var(--text-2); }
.today-head .summary { font-size: 17px; margin-top: 4px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 14px 0 4px; }
.stat { background: var(--card); border-radius: 14px; padding: 12px; box-shadow: 0 0 0 1px var(--border); text-align: left; }
.stat strong { display: block; font-size: 22px; font-weight: 700; letter-spacing: -.02em; }
.stat span { font-size: 12px; color: var(--text-2); }
.stat.warn strong { color: var(--danger); }
.time-col { width: 50px; flex: none; font-size: 13px; color: var(--text-2); font-variant-numeric: tabular-nums; line-height: 1.25; }
.time-col small { display: block; color: var(--text-3); }
.bar { width: 3px; align-self: stretch; border-radius: 2px; flex: none; }

/* ---------- calendario ---------- */
.week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; margin: 4px 0 8px; }
.week button { display: flex; flex-direction: column; align-items: center; padding: 6px 0; border-radius: 12px; }
.week small { font-size: 11px; color: var(--text-3); text-transform: uppercase; }
.week b { font-size: 18px; font-weight: 600; width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%; margin-top: 2px; }
.week button.today b { color: var(--danger); }
.week button.sel b { background: var(--text); color: var(--bg); }
.week button.today.sel b { background: var(--danger); color: #fff; }
.week i { width: 5px; height: 5px; border-radius: 50%; background: var(--text-3); margin-top: 3px; opacity: 0; }
.week i.on { opacity: 1; }
.cal-nav { display: flex; align-items: center; justify-content: space-between; margin: 0 4px; }
.cal-nav h2 { font-size: 17px; margin: 0; text-transform: capitalize; }
.cal-nav button { color: var(--accent); padding: 6px 10px; font-size: 15px; }
.day-head { font-size: 14px; font-weight: 600; margin: 20px 4px 4px; text-transform: capitalize; display: flex; gap: 8px; align-items: baseline; }
.day-head small { color: var(--text-3); font-weight: 400; }
.day-head.is-today { color: var(--danger); }

/* ---------- documenti ---------- */
.doc { background: var(--card); border-radius: 14px; padding: 18px 18px 28px; box-shadow: 0 0 0 1px var(--border); margin-top: 8px; }
.doc-title { font-size: 26px; font-weight: 700; margin: 0 0 12px; outline: none; letter-spacing: -.01em; overflow-wrap: anywhere; }
.doc-title:empty::before { content: 'Senza titolo'; color: var(--text-3); }
.blk { margin: 3px 0; min-height: 1.45em; overflow-wrap: anywhere; outline: none; position: relative; padding-left: calc(var(--ind, 0) * 22px); }
.blk[contenteditable]:empty::before { content: attr(data-ph); color: var(--text-3); }
.blk.h1 { font-size: 24px; font-weight: 700; margin-top: 18px; }
.blk.h2 { font-size: 20px; font-weight: 650; margin-top: 16px; }
.blk.h3 { font-size: 17px; font-weight: 600; margin-top: 12px; }
.blk-wrap { display: flex; gap: 8px; align-items: flex-start; padding-left: calc(var(--ind, 0) * 22px); margin: 3px 0; }
.blk-wrap .blk { flex: 1; padding-left: 0; margin: 0; }
.blk-wrap .mark { flex: none; width: 18px; text-align: center; color: var(--text-2); line-height: 1.45; }
.blk-wrap .check { width: 20px; height: 20px; margin-top: 1px; border-radius: 5px; }
.blk-wrap .check.on::after { font-size: 12px; }
.blk-wrap.checked .blk { color: var(--text-3); text-decoration: line-through; }
.blk.quote { border-left: 3px solid var(--text); padding-left: 12px; }
.callout { display: flex; gap: 10px; background: var(--callout); border-radius: 8px; padding: 12px 14px; margin: 6px 0; }
.blk.code { font: 14px/1.5 var(--mono); background: var(--code-bg); border-radius: 8px; padding: 12px; white-space: pre-wrap; }
.blk code { font-family: var(--mono); font-size: .9em; background: var(--code-bg); padding: 1px 4px; border-radius: 4px; color: #eb5757; }
.blk mark { background: rgba(255,212,0,.35); color: inherit; }
hr.div { border: 0; border-top: 1px solid var(--border-strong); margin: 14px 0; }
.page-link { display: flex; gap: 8px; align-items: center; padding: 6px 4px; border-radius: 8px; text-decoration: underline; text-decoration-color: var(--border-strong); text-underline-offset: 3px; color: var(--text); font-weight: 500; width: 100%; text-align: left; }
.placeholder-blk { display: flex; gap: 10px; align-items: center; background: var(--bg-soft); border-radius: 10px; padding: 12px; color: var(--text-2); font-size: 14px; margin: 6px 0; }
.doc-tools { position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + var(--safe-b)); z-index: 9; display: flex; gap: 8px; overflow-x: auto; padding: 8px 12px; background: color-mix(in srgb, var(--bg-soft) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-top: 1px solid var(--border); scrollbar-width: none; }
.doc-tools::-webkit-scrollbar { display: none; }
.doc-tools button { flex: none; background: var(--card); box-shadow: 0 0 0 1px var(--border); padding: 8px 12px; border-radius: 10px; font-size: 14px; white-space: nowrap; }
.doc.editing { margin-bottom: 60px; }
.row select { border: 0; background: var(--hover); border-radius: 8px; padding: 6px 10px; -webkit-appearance: none; appearance: none; color: var(--accent); font-size: 15px; text-align: right; }
.doc.editing .blk[contenteditable] { -webkit-user-select: text; user-select: text; }
.blk:focus { background: var(--accent-soft); border-radius: 4px; }
.crumbs { font-size: 13px; color: var(--text-3); margin: 6px 4px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- progetti ---------- */
.proj-card { display: flex; gap: 12px; align-items: center; }
.proj-color { width: 10px; align-self: stretch; border-radius: 5px; flex: none; }
.kpi { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.money { font-variant-numeric: tabular-nums; white-space: nowrap; }
.money.paid { color: var(--ok); }
.money.late { color: var(--danger); }
.notes-box { white-space: pre-wrap; font-size: 15px; }
.progress { height: 6px; border-radius: 3px; background: var(--hover); overflow: hidden; margin-top: 8px; }
.progress i { display: block; height: 100%; background: var(--accent); }

body.kb-open .tabbar { display: none; }
body.kb-open .doc-tools { bottom: var(--kb, 0px); }
body.kb-open #toasts { bottom: calc(var(--kb, 0px) + 60px); }

/* ---------- vista giorno a fasce orarie ---------- */
.cal-mode { margin: 4px 0 2px; }
.allday { display: flex; flex-direction: column; gap: 4px; margin: 6px 0 8px; }
.ad-item { text-align: left; font-size: 14px; padding: 6px 10px; border-radius: 8px; background: color-mix(in srgb, var(--c) 16%, var(--card)); border-left: 3px solid var(--c); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ad-item.done { opacity: .55; text-decoration: line-through; }
.grid { position: relative; background: var(--card); border-radius: 14px; box-shadow: 0 0 0 1px var(--border); margin-top: 6px; overflow: hidden; }
.hour { position: absolute; left: 0; right: 0; border-top: 1px solid var(--border); pointer-events: none; }
.hour:first-child { border-top: 0; }
.hour span { position: absolute; left: 8px; top: -8px; font-size: 11px; color: var(--text-3); background: var(--card); padding: 0 4px; font-variant-numeric: tabular-nums; }
.ev { position: absolute; text-align: left; border-radius: 7px; padding: 3px 6px; overflow: hidden; background: color-mix(in srgb, var(--c) 20%, var(--card)); border-left: 3px solid var(--c); display: flex; flex-direction: column; gap: 1px; line-height: 1.2; }
.ev b { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.ev small { font-size: 11px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev.short { flex-direction: row; align-items: center; padding-top: 1px; padding-bottom: 1px; }
.ev.short b { -webkit-line-clamp: 1; font-size: 12px; }
.ev.done { opacity: .55; }
.ev.done b { text-decoration: line-through; }
.ev:active { filter: brightness(.95); }
.now { position: absolute; left: 48px; right: 0; border-top: 2px solid var(--danger); pointer-events: none; z-index: 2; }
.now::before { content: ''; position: absolute; left: -5px; top: -6px; width: 10px; height: 10px; border-radius: 50%; background: var(--danger); }

.proj-money { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 8px; font-size: 14px; font-variant-numeric: tabular-nums; }
.proj-money span { display: flex; flex-direction: column; }
.proj-money small { font-size: 11px; color: var(--text-3); }
.proj-money .paid { color: var(--ok); }
.proj-money .warnish, .stat.warnish strong { color: var(--warn); }
.proj-money .late { color: var(--danger); }

.ptr { height: 0; overflow: hidden; display: flex; align-items: flex-end; justify-content: center; gap: 8px; font-size: 13px; color: var(--text-3); padding-bottom: 6px; margin: 0 -16px; }
.ptr-ic { display: inline-block; transition: transform .2s; font-size: 16px; }
.ptr.ready .ptr-ic { transform: rotate(180deg); color: var(--accent); }
.ptr.loading .ptr-ic { animation: spin 1s linear infinite; }
.ptr.loading .ptr-ic::before { content: ''; }
