:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1f2933;
  --muted: #6b7684;
  --line: #dde2e8;
  --brand: #1d4e89;
  --brand-dark: #153a66;
  --ok-bg: #e5f4ea;  --ok: #1e6b3a;
  --err-bg: #fbe9e9; --err: #9b1c1c;
  --warn-bg: #fff6dd;
  --radius: 6px;
}

* { box-sizing: border-box; }
html { font-size: 15px; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .6rem; }
small, .muted { color: var(--muted); }
.nowrap { white-space: nowrap; }

/* barra superiore */
.topbar {
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  background: #fff; color: var(--text); padding: .5rem 1.25rem;
  border-bottom: 3px solid var(--brand); box-shadow: 0 1px 3px rgba(0, 0, 0, .06);
}
.topbar a { color: var(--brand); }
.brand { display: block; line-height: 0; }
.brand img { height: 40px; width: auto; }
.menu { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.menu a { padding: .35rem .7rem; border-radius: var(--radius); font-weight: 500; }
.menu a:hover { background: #eef1f5; text-decoration: none; }
.menu a.on { background: var(--brand); color: #fff; }
.user { display: flex; align-items: center; gap: .9rem; }
.user form { margin: 0; }
button.link { background: none; border: 0; color: var(--brand); cursor: pointer; font: inherit; padding: 0; text-decoration: underline; }

/* pagina */
.page { max-width: 1200px; margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }
.page.narrow { max-width: 420px; padding-top: 12vh; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.25rem; margin-bottom: 1rem; }
.login h1 { text-align: center; color: var(--brand); margin-bottom: 1.4rem; }
.login .logo { display: block; width: 100%; height: auto; }
.login h2 { margin-top: 0; }
.login .dimenticata { float: right; margin-top: .45rem; font-size: .92rem; }

.head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.head h1 { margin: 0; }
.head .actions { margin: 0; }

/* messaggi */
.flash { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.flash.ok { background: var(--ok-bg); color: var(--ok); }
.flash.error { background: var(--err-bg); color: var(--err); }
.flash ul { margin: .3rem 0 0 1.1rem; padding: 0; }

/* pulsanti */
.btn {
  display: inline-block; border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: .45rem .9rem; border-radius: var(--radius); cursor: pointer; font: inherit; line-height: 1.3;
}
.btn:hover { background: #eef1f5; text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--err); border-color: #e6b8b8; }
.btn.small { padding: .2rem .55rem; font-size: .9rem; }
.actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-top: 1rem; }
.actions form { margin: 0; }
form.inline { display: inline; margin: 0; }

/* moduli */
.form label { display: block; margin-bottom: .8rem; font-weight: 600; font-size: .92rem; }
.login label { display: block; margin-bottom: .8rem; font-weight: 600; }
input, select, textarea {
  display: block; width: 100%; margin-top: .25rem; padding: .45rem .6rem;
  border: 1px solid #c5ccd4; border-radius: var(--radius); font: inherit; font-weight: 400; background: #fff;
}
textarea { min-height: 7rem; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9cbbe3; border-color: var(--brand); }
label.check { display: flex; align-items: center; gap: .5rem; font-weight: 600; }
label.check input { width: auto; margin: 0; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 1rem; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem .2rem; margin: 0 0 1rem; }
legend { font-weight: 700; padding: 0 .4rem; }

/* filtri */
.filters { display: flex; gap: .6rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: 1rem; }
.filters input, .filters select { width: auto; min-width: 12rem; margin: 0; }

/* tabelle */
table.list { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
table.list th, table.list td { text-align: left; padding: .5rem .7rem; border-bottom: 1px solid var(--line); vertical-align: top; }
table.list th { background: #eef1f5; font-size: .85rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
table.list tr:last-child td { border-bottom: 0; }
table.list tr.late td { background: var(--err-bg); }
table.list tr.soon td { background: var(--warn-bg); }
table.list tr.done td { color: var(--muted); text-decoration: line-through; }
table.list td.num, table.list th.num { text-align: right; }
.table-wrap { overflow-x: auto; }

.pager { display: flex; gap: 1rem; align-items: center; margin-top: .7rem; }

/* dettagli */
dl.dati { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1.2rem; margin: 0; }
dl.dati dt { color: var(--muted); }
dl.dati dd { margin: 0; }
.testo { white-space: pre-wrap; }

.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .8rem; background: #e6ebf1; color: var(--text); }
.badge.aperto, .badge.ammessa, .badge.erogata { background: var(--ok-bg); color: var(--ok); }
.badge.chiuso, .badge.non_ammessa, .badge.chiusa, .badge.inattivo { background: #eceff2; color: var(--muted); }
.badge.previsto, .badge.presentata, .badge.in_preparazione { background: #e3edfa; color: var(--brand); }

/* dashboard */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: .5rem; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; color: var(--text); }
.stat b { display: block; font-size: 1.8rem; color: var(--brand); }
.stat.alert b { color: var(--err); }
.stat:hover { text-decoration: none; border-color: var(--brand); }
.grid2 { display: grid; grid-template-columns: 3fr 2fr; gap: 1.5rem; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: .35rem 0; border-bottom: 1px solid var(--line); }

@media (max-width: 800px) {
  .grid2 { grid-template-columns: 1fr; }
  .topbar { gap: .6rem; }
  .brand img { height: 30px; }
  .filters input, .filters select { min-width: 0; width: 100%; }
}

/* Sezioni del vecchio gestionale (sola consultazione) */
details.archivio { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: .75rem; }
details.archivio > summary { cursor: pointer; padding: .7rem 1.1rem; font-weight: 600; }
details.archivio[open] > summary { border-bottom: 1px solid var(--line); }
details.archivio > :not(summary) { margin: .9rem 1.1rem; }
details.archivio table.list { border: 0; }
