:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #1b2430;
  --muted: #6b7686;
  --border: #dde2ea;
  --primary: #1e3a5f;
  --primary-2: #2f6fb3;
  --accent: #e8a33d;
  --danger: #c0392b;
  --ok: #2e8b57;
  --warn: #d98e04;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(20, 30, 50, .08), 0 4px 14px rgba(20, 30, 50, .05);
  color-scheme: light;
}
:root[data-tema="scuro"] {
  --bg: #11161d; --card: #1a212b; --text: #e7ebf1; --muted: #93a0b2; --border: #2a3441;
  --primary: #4f8fd6; --primary-2: #6aa6e8; --shadow: none; color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-tema="chiaro"]) {
    --bg: #11161d; --card: #1a212b; --text: #e7ebf1; --muted: #93a0b2; --border: #2a3441;
    --primary: #4f8fd6; --primary-2: #6aa6e8; --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }

.sticky-head { position: sticky; top: 0; z-index: 10; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: #1e3a5f; color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 8px; }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: .2px; }
.brand .sub { font-size: 12px; opacity: .8; text-transform: capitalize; }

.tabs {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; padding: 8px;
  background: var(--card); border-bottom: 1px solid var(--border);
}
.tab {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600;
  font-size: clamp(12px, 3.4vw, 15px); padding: 8px 2px; border-radius: 999px; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0;
}
@media (min-width: 700px) { .tabs { display: flex; padding: 8px 12px; } .tab { padding: 8px 16px; } }
.tab.active { background: var(--primary); color: #fff; }

main { max-width: 900px; margin: 0 auto; padding: 14px 12px 40px; }
.view { display: none; }
.view.active { display: block; animation: fade .15s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 6px; font-size: 16px; }
.section-title { font-size: 14px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 20px 4px 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; padding: 9px 14px; border-radius: 10px; cursor: pointer;
  text-decoration: none; min-height: 40px;
}
.btn:hover { filter: brightness(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #1b2430; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn.ghost { border-color: transparent; background: transparent; font-size: 20px; padding: 4px 12px; }
.btn.ghost.small { font-size: 14px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px; width: 100%;
  min-height: 40px;
}
input[type="color"] { padding: 2px; width: 48px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-2); outline-offset: 0; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.stat {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow); cursor: pointer; border-left: 4px solid var(--border);
}
.stat .n { font-size: 26px; font-weight: 750; line-height: 1.1; }
.stat .l { font-size: 12.5px; color: var(--muted); }
.stat .e { font-size: 12.5px; font-weight: 600; margin-top: 2px; }
.stat.red { border-left-color: var(--danger); } .stat.red .n { color: var(--danger); }
.stat.orange { border-left-color: var(--warn); } .stat.orange .n { color: var(--warn); }
.stat.blue { border-left-color: var(--primary-2); }
.stat.green { border-left-color: var(--ok); }

.install { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; }

/* List */
.list { display: flex; flex-direction: column; gap: 8px; }
.empty { padding: 18px; text-align: center; color: var(--muted); border: 1px dashed var(--border); border-radius: var(--radius); }
.item {
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: center;
  background: var(--card); border: 1px solid var(--border); border-left: 5px solid var(--cat, var(--border));
  border-radius: var(--radius); padding: 10px 12px; cursor: pointer; box-shadow: var(--shadow);
}
.item.done { opacity: .6; }
.item.done .t { text-decoration: line-through; }
.item .date {
  width: 52px; text-align: center; border-radius: 9px; padding: 4px 0; background: var(--bg);
}
.item .date .d { font-size: 20px; font-weight: 750; line-height: 1; }
.item .date .m { font-size: 11px; text-transform: uppercase; color: var(--muted); }
.item .t { font-weight: 650; overflow-wrap: anywhere; }
.item .meta { font-size: 12.5px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 2px; }
.item .right { text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.item .amt { font-weight: 700; white-space: nowrap; }
.badge { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge.red { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }
.badge.orange { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.blue { background: color-mix(in srgb, var(--primary-2) 15%, transparent); color: var(--primary-2); }
.badge.green { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.chk {
  width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--border); background: transparent;
  cursor: pointer; font-size: 15px; color: #fff; display: grid; place-items: center; padding: 0;
}
.chk.on { background: var(--ok); border-color: var(--ok); }

/* Filters */
.filters { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr; gap: 8px; }
@media (max-width: 680px) { .filters { grid-template-columns: 1fr 1fr; } .filters input { grid-column: 1 / -1; } }
.totali { margin: 0 4px 8px; font-size: 13px; }

/* Calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; text-transform: capitalize; }
.cal-head strong { flex: 1; text-align: center; font-size: 17px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow { text-align: center; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 0; }
.cal-day {
  min-height: 64px; background: var(--card); border: 1px solid var(--border); border-radius: 9px;
  padding: 4px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; overflow: hidden;
  font: inherit; color: inherit; text-align: left;
}
.cal-day.out { opacity: .4; }
.cal-day.today { outline: 2px solid var(--accent); }
.cal-day.sel { background: color-mix(in srgb, var(--primary-2) 14%, var(--card)); }
.cal-day.festivo .num { color: var(--danger); }
.cal-day .num { font-size: 13px; font-weight: 700; }
.cal-day .dots { display: flex; flex-wrap: wrap; gap: 3px; }
.cal-day .dot { width: 8px; height: 8px; border-radius: 50%; }
.cal-day .lbl { font-size: 10.5px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 1px 3px; border-radius: 4px; color: #fff; }
@media (max-width: 600px) { .cal-day .lbl { display: none; } .cal-day { min-height: 50px; } }
@media (min-width: 601px) { .cal-day .dots { display: none; } }

/* Categories */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 10px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 6px 4px 10px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); font-size: 13px; }
.chip i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.chip button { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 15px; padding: 0 4px; }
.inline-form { display: flex; gap: 8px; }
.inline-form input:first-child { flex: 1; }

/* Dialog */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(560px, 96vw); max-height: 92vh;
  background: var(--card); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
dialog::backdrop { background: rgba(10, 20, 35, .55); }
dialog form { padding: 18px; display: flex; flex-direction: column; gap: 10px; }
dialog h2 { margin: 0 0 4px; font-size: 19px; }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; color: var(--muted); }
dialog label.check { flex-direction: row; align-items: center; gap: 8px; padding-top: 22px; color: var(--text); font-size: 15px; }
dialog label.check input { width: 20px; height: 20px; min-height: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 420px) { .grid2 { grid-template-columns: 1fr; } dialog label.check { padding-top: 0; } }
.dlg-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.spacer { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(120px);
  background: #1b2430; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  transition: transform .25s ease; z-index: 100; max-width: 92vw; box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
.toast.show { transform: translateX(-50%) translateY(0); }

@media print {
  .topbar, .tabs, .filters, .btn, .chk { display: none !important; }
  .view { display: none !important; }
  #view-elenco { display: block !important; }
  .item { box-shadow: none; break-inside: avoid; }
}
.lbl-sm { display: none; }
@media (max-width: 359px) { .lbl-lg { display: none; } .lbl-sm { display: inline; } }
