:root {
  --bg: #f6f5f2; --panel: #ffffff; --panel2: #efeee9; --ink: #1c1c1e; --mut: #8a8a8e;
  --line: rgba(60, 60, 67, .14); --accent: #1f6f68; --accent-ink: #ffffff; --accent-soft: rgba(31, 111, 104, .12);
  --other: #b5562f; --other-soft: rgba(181, 86, 47, .12); --danger: #c0392b; --note: #a26a00;
  --bubble-me: #1f6f68; --bubble-me-ink: #fff; --bubble-other: #ffffff;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 8px 24px rgba(0, 0, 0, .06);
  --vh: 100dvh;
  --sat: env(safe-area-inset-top); --sab: env(safe-area-inset-bottom);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111214; --panel: #1c1d20; --panel2: #26272b; --ink: #f2f2f4; --mut: #8e8e93;
    --line: rgba(255, 255, 255, .1); --accent: #4fb3a9; --accent-ink: #0b1514; --accent-soft: rgba(79, 179, 169, .16);
    --other: #e08a5c; --other-soft: rgba(224, 138, 92, .16); --danger: #ff6b5e; --note: #e3b341;
    --bubble-me: #2b7f77; --bubble-me-ink: #fff; --bubble-other: #26272b;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font: 22px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none; }
body { overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea, select { font: inherit; font-size: 22px; color: var(--ink); }
svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }
h1, h2, h3 { margin: 0; }

/* ------------------------------------------------ schermate di accesso */
.screen { position: fixed; inset: 0; overflow-y: auto; padding: calc(var(--sat) + 24px) 20px calc(var(--sab) + 28px);
  display: flex; flex-direction: column; gap: 16px; }
.hero { text-align: center; margin: 0 0 8px; }
/* Schermate di accesso: contenuto centrato in verticale, azioni subito sotto */
#scr-install, #scr-lock { justify-content: center; gap: 22px; }
#scr-install .steps { padding: 18px 18px 18px 16px; }
#scr-install .note { text-align: center; font-size: 16px; padding: 0 12px; }
#scr-lock .stack { margin-top: 18px; }
#scr-lock .msg { min-height: 0; }
.hero-icon { width: 96px; height: 96px; border-radius: 22px; box-shadow: var(--shadow); }
.hero h1 { font-size: 39px; letter-spacing: -.02em; margin-top: 14px; }
.lead { color: var(--mut); margin: 6px 0 0; font-size: 21px; }
.title { font-size: 42px; letter-spacing: -.02em; margin-top: 8px; }
.screen-head { min-height: 28px; }
.stack { display: flex; flex-direction: column; gap: 10px; margin-top: auto; }
.card { background: var(--panel); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); }
.card p { margin: 0 0 8px; } .card p:last-child { margin-bottom: 0; }
.steps .step { display: flex; gap: 12px; align-items: flex-start; margin: 0 0 12px; }
.steps .step:last-child { margin-bottom: 0; }
.steps .n { flex: 0 0 34px; height: 34px; border-radius: 17px; margin-top: -1px; background: var(--accent-soft); color: var(--accent);
  font-weight: 600; font-size: 18px; display: grid; place-items: center; }
.note { color: var(--mut); font-size: 18px; margin: 0; }
.label { font-size: 17px; color: var(--mut); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.recovery { font: 600 22px/1.7 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: .04em; word-spacing: 4px;
  user-select: all; -webkit-user-select: all; }
.code-box { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.code { font: 700 52px/1.1 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: .08em; color: var(--accent); margin: 4px 0 8px !important; }
.status { color: var(--mut); font-size: 20px; margin-top: 12px !important; }
.check { display: flex; gap: 12px; align-items: center; font-size: 21px; }
.check input { width: 28px; height: 28px; accent-color: var(--accent); }
.msg { min-height: 20px; margin: 0; font-size: 20px; text-align: center; }
.msg.err { color: var(--danger); }

.btn { border-radius: 14px; padding: 15px 18px; font-weight: 600; text-align: center; width: 100%; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:disabled { opacity: .4; }
.btn.secondary { background: var(--panel); color: var(--accent); box-shadow: var(--shadow); }
.btn.plain { color: var(--accent); font-weight: 500; padding: 10px; }
.btn.danger { background: var(--panel); color: var(--danger); box-shadow: var(--shadow); }
.btn.big { font-size: 23px; }
.btn:active, .row.action:active { opacity: .6; }
.link.back { font-size: 40px; line-height: 1; padding: 0 12px 4px 0; font-weight: 300; }
.link { white-space: nowrap; color: var(--accent); font-size: 22px; padding: 8px 0; }
.link.light { color: #fff; } .link.danger { color: #ff8a80; }

.form { display: flex; flex-direction: column; gap: 0; padding: 0 16px; }
.field { display: flex; flex-direction: column; gap: 4px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.field:last-child { border-bottom: 0; }
.field > span { font-size: 17px; color: var(--mut); }
.field input, .field textarea { border: 0; outline: 0; background: transparent; padding: 2px 0; width: 100%; resize: none; }
.field textarea { font-family: inherit; }
#act-code, #rec-code { font-family: ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: .06em; }
.field.inline { flex-direction: row; justify-content: space-between; align-items: center; gap: 12px; }
.field.inline > span { white-space: nowrap; font-size: 22px; color: var(--ink); }
.field.inline input[type="date"], .field.inline input[type="time"] { width: auto; text-align: right; color: var(--accent); }
.switch { appearance: none; -webkit-appearance: none; width: 51px; height: 31px; border-radius: 16px; background: rgba(120, 120, 128, .32);
  position: relative; transition: background .2s; flex: 0 0 auto; }
.switch::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; }
.field .switch, .field input.switch { width: 51px; padding: 0; }
.switch:checked { background: var(--accent); } .switch:checked::after { transform: translateX(20px); }

/* ------------------------------------------------ struttura dell'app */
#app { position: fixed; left: 0; right: 0; top: var(--vt, 0px); height: var(--vh); display: flex; flex-direction: column; }
.nav { padding: calc(var(--sat) + 6px) 16px 8px; display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: center;
  background: color-mix(in srgb, var(--bg) 86%, transparent); backdrop-filter: saturate(1.6) blur(20px);
  -webkit-backdrop-filter: saturate(1.6) blur(20px); border-bottom: 1px solid var(--line); z-index: 5; }
.nav h2 { font-size: 22px; font-weight: 600; max-width: 62vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-side.right { display: flex; justify-content: flex-end; }
.icon-btn { width: 46px; height: 46px; display: grid; place-items: center; color: var(--accent); border-radius: 20px; }
.icon-btn:active { background: var(--accent-soft); }
#view { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.tab { padding: 8px 16px 24px; }
.tabbar { display: grid; grid-template-columns: repeat(3, 1fr); padding: 6px 8px calc(var(--sab) + 4px);
  background: color-mix(in srgb, var(--bg) 86%, transparent); backdrop-filter: saturate(1.6) blur(20px);
  -webkit-backdrop-filter: saturate(1.6) blur(20px); border-top: 1px solid var(--line); }
.tabbar button { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 14px; color: var(--mut);
  padding: 4px 0; position: relative; font-weight: 500; }
.tabbar button.active { color: var(--accent); }
.badge { position: absolute; top: 0; left: calc(50% + 6px); min-width: 22px; height: 22px; border-radius: 11px; padding: 0 6px;
  background: var(--danger); color: #fff; font: 600 15px/22px -apple-system, system-ui; font-style: normal; }
body.typing .tabbar, body.in-thread .tabbar { display: none; }
body.in-thread:not(.typing) .composer { padding-bottom: calc(var(--sab) + 8px); }

/* ------------------------------------------------ agenda */
.month-head { display: flex; align-items: center; justify-content: space-between; margin: 4px -8px 6px; }
.month-head h3 { font-size: 29px; font-weight: 700; letter-spacing: -.01em; text-transform: capitalize; }
.dow, .grid { display: grid; grid-template-columns: repeat(7, 1fr); text-align: center; }
.dow span { font-size: 16px; color: var(--mut); font-weight: 600; padding: 4px 0; }
.grid { row-gap: 2px; }
.day { aspect-ratio: 1 / 1.02; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; }
.day .num { width: 44px; height: 44px; border-radius: 22px; display: grid; place-items: center; font-size: 22px; }
.day.out .num { color: var(--mut); opacity: .45; }
.day.today .num { color: var(--accent); font-weight: 700; }
.day.sel .num { background: var(--ink); color: var(--bg); font-weight: 600; }
.day.sel.today .num { background: var(--accent); color: var(--accent-ink); }
.dots { display: flex; gap: 3px; height: 7px; }
.dots i { width: 7px; height: 7px; border-radius: 4px; background: var(--accent); }
.dots i.o { background: var(--other); }
.day-head { margin: 16px 2px 8px; }
.day-head h3 { font-size: 20px; color: var(--mut); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.list { display: flex; flex-direction: column; gap: 8px; }
.ev { background: var(--panel); border-radius: 14px; padding: 12px 14px 12px 16px; display: grid; grid-template-columns: 96px 1fr;
  gap: 10px; box-shadow: var(--shadow); position: relative; text-align: left; width: 100%; }
.ev::before { content: ""; position: absolute; left: 0; top: 12px; bottom: 12px; width: 4px; border-radius: 0 4px 4px 0; background: var(--accent); }
.ev.o::before { background: var(--other); }
.ev .t { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.ev .t.all { font-size: 17px; line-height: 1.25; color: var(--mut); }
.ev .t small { display: block; font-weight: 400; color: var(--mut); }
.ev .b b { display: block; font-weight: 600; }
.ev .b span { display: block; color: var(--mut); font-size: 18px; }
.empty { text-align: center; color: var(--mut); padding: 28px 0; font-size: 20px; }

/* ------------------------------------------------ messaggi */
#tab-chat { padding: 8px 12px 12px; min-height: 100%; display: flex; flex-direction: column; justify-content: flex-end; }
.thread { display: flex; flex-direction: column; gap: 3px; }
.sep { text-align: center; font-size: 16px; color: var(--mut); margin: 14px 0 6px; font-weight: 600; }
.bubble { max-width: 78%; padding: 8px 12px; border-radius: 18px; font-size: 22px; line-height: 1.32; word-wrap: break-word;
  white-space: pre-wrap; background: var(--bubble-other); align-self: flex-start; box-shadow: var(--shadow); position: relative; }
.bubble.me { background: var(--bubble-me); color: var(--bubble-me-ink); align-self: flex-end; box-shadow: none; }
.bubble .time { display: block; font-size: 14px; opacity: .6; margin-top: 2px; text-align: right; }
.bubble.media { padding: 4px; overflow: hidden; }
.bubble.media img { display: block; max-width: 100%; border-radius: 14px; min-width: 120px; min-height: 80px; background: var(--panel2); }
.bubble.media .time { padding: 0 8px 2px; }
.bubble .file { display: flex; gap: 10px; align-items: center; padding: 4px 2px; }
.bubble .file .ic { width: 38px; height: 38px; border-radius: 10px; background: rgba(127, 127, 127, .18); display: grid; place-items: center; flex: 0 0 auto; }
.bubble .file b { display: block; font-weight: 600; font-size: 20px; }
.bubble .file small { opacity: .7; font-size: 17px; }
.bubble .evcard { display: block; text-align: left; }
.bubble .evcard small { display: block; opacity: .7; font-size: 16px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.bubble .evcard b { display: block; }
.bubble.pending { opacity: .55; }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; background: var(--bg); border-top: 1px solid var(--line); }
.input-wrap { flex: 1; position: relative; display: flex; min-width: 0; }
.composer textarea { flex: 1; border: 1px solid var(--line); border-radius: 22px; padding: 9px 44px 9px 16px; background: var(--panel);
  outline: 0; resize: none; max-height: 180px; line-height: 1.3; min-width: 0; }
.expand { position: absolute; right: 4px; bottom: 4px; width: 38px; height: 38px; border-radius: 19px; display: grid; place-items: center; color: var(--mut); }
.expand svg { width: 20px; height: 20px; }
.composer.expanded { align-items: stretch; }
.composer.expanded textarea { height: calc(var(--vh) * .55) !important; max-height: none; border-radius: 16px; padding-right: 16px; padding-top: 44px; }
.composer.expanded .expand { top: 4px; bottom: auto; color: var(--accent); }
.composer.expanded .send, .composer.expanded #btn-attach { align-self: flex-end; }
.send { width: 44px; height: 44px; border-radius: 22px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; flex: 0 0 auto; margin-bottom: 1px; }
.send svg { width: 20px; height: 20px; stroke-width: 2.4; }
.send:disabled { opacity: .35; }

/* ------------------------------------------------ elenco conversazioni */
#tab-chat:has(.convs:not(.hidden)) { justify-content: flex-start; }
.conv { background: var(--panel); width: 100%; text-align: left; padding: 12px 16px; border-bottom: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 2px; }
.group .conv:first-of-type { border-radius: 12px 12px 0 0; }
.group .conv:last-child { border-radius: 0 0 12px 12px; border-bottom: 0; }
.group .conv:first-of-type:last-child { border-radius: 12px; }
.conv:active { background: var(--panel2); }
.conv-top, .conv-bottom { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.conv-top b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { color: var(--mut); font-size: 18px; flex: 0 0 auto; }
.conv-prev { color: var(--mut); font-size: 20px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-badge { min-width: 26px; height: 26px; border-radius: 13px; padding: 0 6px; background: var(--accent); color: var(--accent-ink);
  font: 600 16px/26px -apple-system, system-ui; font-style: normal; text-align: center; flex: 0 0 auto; }

/* ------------------------------------------------ impostazioni */
.group { margin: 12px 0 22px; }
.group-title { font-size: 17px; color: var(--mut); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; margin: 0 4px 6px; }
.row { background: var(--panel); display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 13px 16px;
  width: 100%; text-align: left; border-bottom: 1px solid var(--line); }
.group .row:first-of-type { border-radius: 12px 12px 0 0; }
.group .row:last-child { border-radius: 0 0 12px 12px; border-bottom: 0; }
.group .row:first-of-type:last-child { border-radius: 12px; }
.row.action { color: var(--accent); }
.row.danger { color: var(--danger); }
.mut { color: var(--mut); text-align: right; flex: 0 1 auto; max-width: 60%; }
.row > span:first-child { flex: 1 0 auto; max-width: 55%; }
.foot { color: var(--mut); font-size: 17px; margin: 0 4px; }

/* ------------------------------------------------ pannello modale */
#sheet-wrap { position: fixed; left: 0; right: 0; top: var(--vt, 0px); height: var(--vh); z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .35); animation: fade .2s; }
.sheet { position: absolute; left: 0; right: 0; bottom: 0; max-height: calc(var(--vh) - var(--sat) - 12px); overflow-y: auto;
  background: var(--bg); border-radius: 14px 14px 0 0; padding: 8px 16px calc(var(--sab) + 20px); animation: up .25s ease-out;
  display: flex; flex-direction: column; gap: 14px; }
.sheet-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; position: sticky; top: -8px;
  background: var(--bg); padding: 8px 0 4px; z-index: 1; }
.sheet-head h3 { font-size: 22px; font-weight: 600; text-align: center; }
.sheet-head .right { text-align: right; }
.sheet-head .link.strong { font-weight: 600; }
.sheet .stack { margin-top: 0; }
.ev-view h2 { font-size: 34px; letter-spacing: -.01em; }
.ev-view .when { color: var(--mut); margin-top: 4px; }
.ev-view .meta { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.ev-view .who { font-size: 17px; color: var(--mut); margin-top: 10px; }
.pend { display: flex; justify-content: space-between; align-items: center; width: 100%; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

/* ------------------------------------------------ visualizzatore */
#viewer { position: fixed; inset: 0; background: #000; z-index: 30; display: flex; flex-direction: column; }
.viewer-bar { display: flex; justify-content: space-between; align-items: center; padding: calc(var(--sat) + 6px) 16px 8px; color: #fff; gap: 10px; }
.viewer-bar span { font-size: 20px; opacity: .8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-body { flex: 1; display: grid; place-items: center; overflow: auto; color: #fff; }
.viewer-body img, .viewer-body video { max-width: 100%; max-height: 100%; }
.viewer-foot { padding: 8px 16px calc(var(--sab) + 8px); text-align: center; min-height: 40px; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, .25); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#toast { position: fixed; left: 50%; transform: translateX(-50%); top: calc(var(--sat) + 10px); z-index: 40;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 22px; font-size: 20px; max-width: 90vw;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .2); animation: fade .2s; }

/* evento: data e ora di inizio e di fine */
.field.inline.when { flex-direction: column; align-items: stretch; gap: 6px; }
.when-inputs { display: flex; gap: 12px; align-items: center; justify-content: space-between; }
.field .when-inputs input { width: auto; min-width: 0; flex: 0 1 auto; color: var(--accent); background: var(--panel2);
  border-radius: 8px; padding: 6px 10px; }
.field .when-inputs input[type=date] { flex: 1 1 auto; }
.ev .t.lab small { display: block; font-weight: 400; color: var(--mut); font-size: 15px; }

/* impegno con note */
.ev .b .has-notes { color: var(--note); font-size: 15px; font-weight: 600; margin-top: 2px; }
