:root {
  /* Palette della nota spese MTS: rosso del logo e rosa antico del modulo Excel (#DA9694). */
  --bg: #f7f3f2;
  --card: #ffffff;
  --text: #1d1717;
  --muted: #776a6a;
  --line: #eee1e0;
  --accent: #c4261d;
  --accent-soft: #fbeceb;
  --rose: #da9694;
  --rose-text: #4a1d1b;
  --amount: #b0261f;
  --off: #f2dcdb;
  --off-text: #8e3b37;
  --on-accent: #ffffff;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(60, 20, 20, .06), 0 1px 3px rgba(60, 20, 20, .08);
  --radius: 14px;
  --tabbar-h: 58px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141011; --card: #201a1b; --text: #f3eded; --muted: #a99d9d; --line: #362c2d;
    --accent: #f0776d; --accent-soft: #3a2322; --rose: #8f5553; --rose-text: #fff1f0; --amount: #f39a92;
    --off: #43292a; --off-text: #f0b3ae; --on-accent: #2a0d0b; --danger: #ff8a80; --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 80px);
}
body.sheet-open { overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
svg.inline { width: 16px; height: 16px; vertical-align: -3px; color: var(--accent); }
h2, h3 { margin: 0; }

/* barra superiore */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(180%) blur(16px); -webkit-backdrop-filter: saturate(180%) blur(16px);
}
.topbar h1 { margin: 0; font-size: 26px; font-weight: 750; letter-spacing: -.02em; }
.chip { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-btn { display: flex; align-items: center; gap: 8px; max-width: 55%; min-width: 0; }

/* avatar operatore */
.avatar {
  flex: none; width: 96px; height: 96px; border-radius: 50%; overflow: hidden;
  display: grid; place-items: center; background: linear-gradient(135deg, var(--rose), var(--accent));
  color: var(--on-accent); font-weight: 700; font-size: 36px; letter-spacing: .02em; user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar svg { width: 55%; height: 55%; stroke-width: 1.6; }
.avatar.small { width: 34px; height: 34px; font-size: 13px; }
.profile-head { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 6px 0 4px; }
.profile-head h3 { margin: 0; font-size: 20px; text-align: center; }
.profile-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }

main { padding: 0 16px; max-width: 640px; margin: 0 auto; }

/* card */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 12px 0; }
.card h3 { font-size: 17px; margin-bottom: 10px; }
.hint { border-left: 4px solid var(--accent); }
.hint p { margin: 6px 0 10px; color: var(--muted); font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.empty { color: var(--muted); text-align: center; padding: 18px 8px; margin: 0; }

/* intestazione mese */
.month-head { display: flex; align-items: center; justify-content: space-between; margin: 6px 0 12px; }
.month-head h2 { font-size: 21px; font-weight: 700; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: var(--accent); background: var(--card); box-shadow: var(--shadow); }

/* riepilogo */
.summary { display: grid; grid-template-columns: 1fr 1fr 1.2fr; gap: 8px; }
.summary div { background: var(--card); border-radius: 12px; padding: 10px 12px; box-shadow: var(--shadow); min-width: 0; }
.summary span { display: block; font-size: 12px; color: var(--muted); }
.summary strong { font-size: 16px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.summary .net { background: var(--accent); color: var(--on-accent); }
.summary .net span { color: inherit; opacity: .85; }
.breakdown { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--muted); margin: 10px 2px 14px; }
.breakdown b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

/* calendario */
.calendar { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; background: var(--card); border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow); }
.calendar i { font-style: normal; text-align: center; font-size: 12px; font-weight: 600; color: var(--rose-text); background: var(--rose); border-radius: 6px; padding: 3px 0; margin-bottom: 2px; }
.day { aspect-ratio: 1 / 1.05; border-radius: 10px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; min-width: 0; }
.day b { font-weight: 500; font-size: 15px; }
.day small { font-size: 9.5px; font-weight: 600; color: var(--amount); font-variant-numeric: tabular-nums; max-width: 100%; overflow: hidden; }
.day.off { background: var(--off); }
.day.off b { color: var(--off-text); }
.day.has b { font-weight: 700; }
.day.today b { color: var(--on-accent); background: var(--accent); border-radius: 50%; width: 26px; height: 26px; display: grid; place-items: center; }
.day.sel { outline: 2px solid var(--accent); outline-offset: -2px; }

/* elenco movimenti */
.list-head { display: flex; align-items: baseline; justify-content: space-between; margin: 20px 2px 8px; gap: 8px; }
.list-head h3 { font-size: 17px; }
.tag { font-size: 12px; font-weight: 500; color: var(--off-text); background: var(--off); padding: 2px 8px; border-radius: 99px; vertical-align: 2px; }
.list { list-style: none; margin: 0 0 12px; padding: 0; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.item { width: 100%; display: flex; align-items: center; gap: 12px; padding: 11px 14px; text-align: left; }
.item:active { background: var(--bg); }
.date-badge { width: 42px; flex: none; text-align: center; font-size: 11px; color: var(--muted); text-transform: uppercase; line-height: 1.1; border-radius: 9px; padding: 4px 0; background: var(--bg); }
.date-badge b { display: block; font-size: 18px; color: var(--text); }
.date-badge.off { background: var(--off); }
.item-main { flex: 1; min-width: 0; }
.item-main strong, .item-main small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-main small { color: var(--muted); font-size: 13px; }
.item-amt { font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.cam { width: 17px; height: 17px; color: var(--muted); }

.export { margin-top: 22px; }
.export h3 { font-size: 15px; color: var(--muted); font-weight: 600; margin: 0 2px 8px; }

/* pulsanti */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 16px; border-radius: 12px;
  background: var(--card); color: var(--accent); font-weight: 600; box-shadow: var(--shadow);
  border: 1px solid var(--line); cursor: pointer; text-align: center;
}
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger-text { color: var(--danger); box-shadow: none; }
.btn.small { min-height: 38px; padding: 0 12px; font-size: 14px; }
.btn.block { display: flex; width: 100%; margin: 8px 0; }
.btn:disabled { opacity: .55; }
.link { color: var(--accent); font-weight: 500; padding: 6px 0; }
.link.strong { font-weight: 700; }

.fab {
  position: fixed; z-index: 6; right: 16px;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  display: flex; align-items: center; gap: 6px; padding: 0 18px 0 14px; height: 52px;
  border-radius: 26px; background: var(--accent); color: var(--on-accent); font-weight: 650;
  box-shadow: 0 6px 18px rgba(196, 38, 29, .32);
}
.fab svg { stroke-width: 2.4; }
.fab[hidden] { display: none; }

/* barra in basso */
.tabbar {
  position: fixed; z-index: 7; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--card) 88%, transparent);
  backdrop-filter: saturate(180%) blur(16px); -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid var(--line);
}
.tabbar button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; color: var(--muted); }
.tabbar button.active { color: var(--accent); }

/* storico */
.year h3 { font-size: 22px; }
.hist { list-style: none; margin: 0; padding: 0; }
.hist li + li { border-top: 1px solid var(--line); }
.hist button { width: 100%; display: flex; justify-content: space-between; align-items: center; padding: 10px 0; text-align: left; }
.hist small { display: block; color: var(--muted); font-size: 13px; }
.hist b, .year-total b { font-variant-numeric: tabular-nums; }
.year-total { display: flex; justify-content: space-between; border-top: 2px solid var(--line); margin-top: 4px; padding-top: 10px; font-weight: 600; }

/* campi */
.field { display: block; padding: 10px 0; }
.field + .field { border-top: 1px solid var(--line); }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field textarea {
  width: 100%; font: inherit; font-size: 17px; color: var(--text); background: transparent;
  border: 0; outline: none; padding: 4px 0; border-radius: 0; -webkit-appearance: none; appearance: none;
}
.field input[type="date"] { min-height: 32px; }
.field.money { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.field.money > span { margin: 0; font-size: 16px; color: var(--text); }
.field.money input { text-align: right; max-width: 50%; font-variant-numeric: tabular-nums; }
.field.km small { display: block; font-size: 13px; color: var(--muted); }
.field.total { display: flex; justify-content: space-between; align-items: center; }
.field.total > span { margin: 0; font-size: 16px; color: var(--text); font-weight: 600; }
.field.total strong { font-size: 19px; color: var(--accent); font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 8px; align-items: center; }
.row input { flex: 1; min-width: 0; }
.group { background: var(--card); border-radius: var(--radius); padding: 2px 16px; margin: 12px 0; box-shadow: var(--shadow); }
.card .field input { border-bottom: 1px solid var(--line); }

/* foglio modulo */
.sheet {
  position: fixed; inset: 0; z-index: 20; background: var(--bg); overflow-y: auto; -webkit-overflow-scrolling: touch;
  transform: translateY(100%); transition: transform .25s ease;
}
.sheet.open { transform: none; }
.sheet-bar {
  position: sticky; top: 0; z-index: 2; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px; background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); border-bottom: 1px solid var(--line);
}
.sheet-bar h2 { font-size: 17px; }
.sheet-bar .link:first-child { justify-self: start; }
.sheet-bar .link:last-child { justify-self: end; }
.sheet-body { padding: 8px 16px calc(env(safe-area-inset-bottom) + 40px); max-width: 640px; margin: 0 auto; }

.receipt { margin: 12px 0; }
.receipt img { display: block; width: 100%; max-height: 280px; object-fit: contain; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 10px; }
.receipt-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.receipt-actions .btn { flex: 1 1 140px; }
#ocr-status { margin: 10px 2px 0; }
.ocr-card { margin-top: 10px; background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; font-size: 15px; }
.ocr-card p { margin: 6px 0; }
.ocr-card dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 8px 0; }
.ocr-card dt { color: var(--muted); }
.ocr-card dd { margin: 0; font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-btn { padding: 7px 12px; border-radius: 99px; background: var(--card); color: var(--accent); font-weight: 600; font-size: 14px; border: 1px solid var(--line); }
.chip-btn.on { background: var(--accent); color: var(--on-accent); border-color: transparent; }

/* dialog e toast */
.dialog { border: 0; border-radius: 16px; padding: 20px; width: min(90vw, 360px); background: var(--card); color: var(--text); }
.dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dialog pre { white-space: pre-wrap; font: inherit; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }
.toast {
  position: fixed; z-index: 30; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 80px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: .2s;
  background: rgba(20, 22, 28, .92); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 15px; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
