/* Stile grafico: FeedLoop Design System
   content-first, spaziatura compatta, card uniformi, niente ombre pesanti, touch target >= 44px */
:root {
  color-scheme: light;
  /* FeedLoop */
  --primary: #2563EB;      /* interazioni, link, CTA */
  --secondary: #6B7280;    /* metadati */
  --tertiary: #F9FAFB;     /* tinte di sfondo */
  --background: #F9FAFB;   /* sfondo del feed */
  --surface: #FFFFFF;      /* card */
  --success: #16A34A;
  --warning: #D97706;
  --error: #DC2626;
  --info: #2563EB;
  /* derivati */
  --ink: #111827;
  --ink-2: #374151;
  --line: #E5E7EB;
  --hover: #F3F4F6;
  --primary-soft: #EFF6FF;
  --primary-line: #BFDBFE;
  --radius: 12px;
  --radius-s: 8px;
  --pad: 12px;
  --s-statale: #2563EB;
  --s-paritaria: #EA580C;
  --s-privata: #0D9488;
  --star: #D97706;
  --good: #16A34A;
  --shadow: 0 1px 2px rgba(17, 24, 39, .05);
  /* alias usati da app.js */
  --accent: var(--primary);
  --muted: var(--secondary);
  --page: var(--background);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --primary: #3B82F6; --secondary: #9CA3AF; --tertiary: #1F2937;
    --background: #0B1120; --surface: #111827;
    --success: #22C55E; --warning: #F59E0B; --error: #F87171; --info: #3B82F6;
    --ink: #F9FAFB; --ink-2: #D1D5DB; --line: #1F2937; --hover: #1F2937;
    --primary-soft: #172554; --primary-line: #1E40AF;
    --s-statale: #3B82F6; --s-paritaria: #F97316; --s-privata: #14B8A6;
    --star: #F59E0B; --good: #22C55E;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --primary: #3B82F6; --secondary: #9CA3AF; --tertiary: #1F2937;
  --background: #0B1120; --surface: #111827;
  --success: #22C55E; --warning: #F59E0B; --error: #F87171; --info: #3B82F6;
  --ink: #F9FAFB; --ink-2: #D1D5DB; --line: #1F2937; --hover: #1F2937;
  --primary-soft: #172554; --primary-line: #1E40AF;
  --s-statale: #3B82F6; --s-paritaria: #F97316; --s-privata: #14B8A6;
  --star: #F59E0B; --good: #22C55E;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--background); color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1 { font-size: 18px; margin: 0; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 17px; margin: 0 0 2px; font-weight: 700; line-height: 1.3; }
h3 { font-size: 12px; margin: 16px 0 6px; text-transform: uppercase; letter-spacing: .05em; color: var(--secondary); font-weight: 600; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
kbd { font: 11px ui-monospace, monospace; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }

/* ---------- barra in alto ---------- */
.top { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 1000; }
.top-inner { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 20px; max-width: 1600px; margin: 0 auto; }
.sub { margin: 1px 0 0; color: var(--secondary); font-size: 12px; }
.top-actions { display: flex; gap: 8px; }

/* ---------- impaginazione ---------- */
.layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 16px; padding: 16px 20px; max-width: 1600px; margin: 0 auto; }
.main { min-width: 0; }

/* card uniformi: stesso bordo, raggio e padding ovunque */
.filters, .tile, .banner, .table-wrap, #mappa, .feed-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}

/* ---------- filtri ---------- */
.filters {
  padding: 0 var(--pad) var(--pad); align-self: start; position: sticky; top: 68px;
  max-height: calc(100vh - 84px); overflow: auto;
}
.f-group { padding: 12px 0; border-bottom: 1px solid var(--line); }
.f-group:last-child { border-bottom: 0; }
.f-title { font-weight: 600; font-size: 13px; margin-bottom: 8px; display: block; color: var(--ink); }
.f-sub { display: block; color: var(--secondary); font-size: 12px; margin: 10px 0 4px; }
.f-sub.inline { display: inline; margin: 0 4px 0 0; }
.hint { color: var(--secondary); font-size: 12px; margin: 6px 0 0; }
.hint.inline { font-weight: 400; }
input[type=search], input[type=text], input[type=number], select {
  width: 100%; min-height: 36px; padding: 6px 10px; border-radius: var(--radius-s); border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font: inherit;
}
input:focus, select:focus { outline: 2px solid var(--primary-line); outline-offset: 0; border-color: var(--primary); }
.f-sub input { margin-top: 4px; }
input[type=range] { width: 100%; accent-color: var(--primary); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.small { margin-top: 6px; }
.row.two > * { flex: 1; }
#form-casa input { flex: 1; min-width: 0; }

/* ---------- pulsanti ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; border: 1px solid var(--primary); background: var(--primary); color: #fff;
  border-radius: var(--radius-s); padding: 6px 12px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { filter: brightness(1.06); text-decoration: none; }
.btn.ghost { background: var(--surface); color: var(--ink); border-color: var(--line); font-weight: 500; }
.btn.ghost:hover { background: var(--hover); filter: none; }
.btn.small { min-height: 30px; padding: 4px 10px; font-size: 12px; }
.btn.wide { width: 100%; margin-top: 10px; }
.btn:focus-visible, .chip:focus-visible, .tab:focus-visible, .star:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 32px; border: 1px solid var(--line); background: var(--surface); color: var(--ink-2);
  border-radius: 999px; padding: 4px 12px; cursor: pointer; font: inherit; font-size: 13px;
}
.chip:hover { background: var(--hover); }
.chip[aria-pressed="true"] { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }
.chip .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.checks { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 8px; }
.check { display: flex; gap: 8px; align-items: center; cursor: pointer; font-size: 13px; min-height: 28px; color: var(--ink-2); }
.check input { accent-color: var(--primary); width: 16px; height: 16px; margin: 0; flex: none; }
.pill { display: inline-block; min-width: 18px; padding: 0 6px; border-radius: 999px; background: var(--primary); color: #fff; font-size: 11px; font-weight: 600; text-align: center; line-height: 18px; }
.pill:empty { display: none; }

/* ---------- banner iscrizioni ---------- */
.banner {
  display: flex; gap: 8px 12px; align-items: center; flex-wrap: wrap;
  border-left: 4px solid var(--primary); padding: 10px var(--pad); margin-bottom: 12px;
}
.banner .big { font-size: 15px; font-weight: 700; }
.banner .hint { margin: 0; flex: 1 1 320px; }

/* ---------- riquadri numeri ---------- */
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.tile { padding: 10px var(--pad); }
.tile .lbl { color: var(--secondary); font-size: 12px; }
.tile .val { font-size: 24px; font-weight: 700; line-height: 1.2; margin-top: 2px; letter-spacing: -.01em; }
.tile .det { color: var(--secondary); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- barra strumenti e schede ---------- */
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.toolbar select { width: auto; }
.tabs { display: flex; gap: 2px; background: var(--surface); border: 1px solid var(--line); padding: 3px; border-radius: 10px; }
.tab { min-height: 32px; border: 0; background: transparent; color: var(--secondary); padding: 4px 14px; border-radius: 7px; cursor: pointer; font: inherit; font-weight: 500; }
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

/* ---------- mappa ---------- */
#mappa { height: min(68vh, 720px); overflow: hidden; }
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9); } }
.leaflet-container { font: inherit; background: var(--tertiary); }
.leaflet-tooltip { background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: var(--radius-s); box-shadow: var(--shadow); }
.leaflet-bar a { color: var(--ink); background: var(--surface); }
.pop-t { font-weight: 600; }
.pop-s { color: var(--secondary); font-size: 12px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; color: var(--secondary); font-size: 12px; margin-top: 8px; }
.legend span::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; background: var(--c); }
.legend .casa::before { border-radius: 2px; transform: rotate(45deg); }

/* ---------- elenco ---------- */
.table-wrap { overflow: auto; max-height: 75vh; }
.tbl { border-collapse: collapse; width: 100%; font-size: 13px; }
.tbl th, .tbl td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.tbl tbody tr:last-child td { border-bottom: 0; }
.tbl th { position: sticky; top: 0; background: var(--surface); color: var(--secondary); font-weight: 600; font-size: 12px; z-index: 1; }
.tbl td.num, .tbl th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl tbody tr { cursor: pointer; }
.tbl tbody tr:hover { background: var(--hover); }
.tbl .nome { font-weight: 600; }
#tabella td:nth-child(3) { white-space: nowrap; }
.tbl .com { color: var(--secondary); font-size: 12px; }
.confronto th:first-child, .confronto td:first-child { position: sticky; left: 0; background: var(--surface); color: var(--secondary); font-weight: 600; min-width: 150px; }
.confronto tbody tr { cursor: default; }
.confronto tbody tr:hover { background: transparent; }
.confronto td { min-width: 170px; }
.confronto .best { color: var(--good); font-weight: 600; }

.badge { display: inline-block; padding: 1px 8px; margin: 1px 2px 1px 0; border-radius: 999px; font-size: 11px; font-weight: 500; background: var(--tertiary); color: var(--ink-2); border: 1px solid var(--line); white-space: nowrap; }
.tipo-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.star { border: 0; background: none; cursor: pointer; font-size: 18px; line-height: 1; color: var(--secondary); padding: 0 4px; min-width: 28px; min-height: 28px; border-radius: var(--radius-s); }
.star:hover { background: var(--hover); }
.star[aria-pressed="true"] { color: var(--star); }

/* ---------- scheda scuola ---------- */
.drawer-bg { position: fixed; inset: 0; background: rgba(17, 24, 39, .35); z-index: 1500; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(480px, 100vw); z-index: 1600;
  background: var(--surface); border-left: 1px solid var(--line);
  transform: translateX(100%); transition: transform .2s ease; overflow: auto; padding: 16px 20px 32px;
}
.drawer.open { transform: none; }
.d-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.d-links { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 0; margin: 0; }
.kv dt, .kv dd { padding: 6px 0; border-bottom: 1px solid var(--line); }
.kv dt { color: var(--secondary); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.yn-si { color: var(--success); font-weight: 600; }
.yn-no { color: var(--secondary); }
.corsi { margin: 4px 0 0; padding-left: 18px; color: var(--ink-2); font-size: 13px; }
.bar { height: 6px; border-radius: 3px; background: var(--tertiary); border: 1px solid var(--line); overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; background: var(--primary); }
.chips-att { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0; }
.prog { margin: 4px 0 0; padding-left: 18px; color: var(--ink-2); font-size: 13px; }
.prog li { margin: 2px 0; }
details > summary { cursor: pointer; color: var(--primary); font-size: 13px; font-weight: 500; margin-top: 8px; min-height: 28px; display: list-item; }
.ist-note { color: var(--secondary); font-size: 11px; font-weight: 400; text-transform: none; letter-spacing: 0; }

.box-scuola { background: var(--tertiary); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--pad); margin: 8px 0 12px; }
.box-scuola .kv dt, .box-scuola .kv dd { border-color: var(--line); }
.kv dd.lungo { text-align: right; max-width: 62%; justify-self: end; }

/* ---------- richiesta e storico ---------- */
.rich { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid; }
.rich::before { margin-right: 4px; }
.rich-alta { color: var(--error); border-color: currentColor; }
.rich-alta::before { content: "▲"; }
.rich-media { color: var(--warning); border-color: currentColor; }
.rich-media::before { content: "●"; }
.rich-bassa { color: var(--success); border-color: currentColor; }
.rich-bassa::before { content: "✓"; }
.storico { display: flex; gap: 10px; align-items: flex-end; height: 96px; padding: 6px 0 2px; }
.st-col { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 2px; min-width: 34px; height: 100%; }
.st-col i { display: block; width: 22px; background: var(--primary); border-radius: 4px 4px 0 0; }
.st-val { font-size: 11px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.st-a { font-size: 10px; color: var(--secondary); }

/* ---------- stampa: scheda open day ---------- */
#stampa { display: none; }
@media print {
  body > *:not(#stampa) { display: none !important; }
  #stampa { display: block; color: #000; font-size: 11pt; }
  #stampa .foglio { page-break-after: always; }
  #stampa .foglio:last-child { page-break-after: auto; }
  #stampa h1 { font-size: 16pt; margin: 0 0 2px; }
  #stampa h2 { font-size: 12pt; margin: 12px 0 4px; border-bottom: 1px solid #999; }
  #stampa table { width: 100%; border-collapse: collapse; font-size: 9.5pt; }
  #stampa td { border-bottom: 1px solid #ddd; padding: 3px 4px; vertical-align: top; }
  #stampa td:first-child { width: 34%; color: #444; }
  #stampa .domande li { margin: 3px 0; list-style: none; }
  #stampa .domande li::before { content: "☐ "; }
  #stampa .righe div { border-bottom: 1px solid #aaa; height: 22px; }
  #stampa .meta { color: #444; font-size: 9pt; }
}

/* ---------- punteggio ---------- */
.punteggio-sum { cursor: pointer; display: list-item; margin: 0; color: var(--primary); }
.peso { display: grid; grid-template-columns: 1fr 92px; gap: 0 8px; align-items: center; font-size: 13px; color: var(--ink-2); margin-top: 8px; }
.peso > span { grid-column: 1 / 3; }
.peso input { width: 100%; }
.peso-val { font-size: 11px; font-weight: 600; color: var(--primary); text-align: right; }
.peso-lingua { margin-top: 4px; }
.punt {
  display: inline-flex; align-items: center; justify-content: center; min-width: 36px; height: 24px; padding: 0 6px;
  border-radius: 999px; font-weight: 700; font-size: 12px; font-variant-numeric: tabular-nums;
  color: var(--primary); background: var(--primary-soft); border: 1px solid var(--primary-line);
}

.foot { color: var(--secondary); font-size: 12px; margin-top: 16px; }
.only-mobile { display: none; }
.m-only { display: none; }

/* ---------- telefono: feed di card, touch target >= 44px ---------- */
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; gap: 12px; }
  .top-inner { padding: 8px 16px; }
  .only-mobile { display: inline-flex; }
  .m-only { display: inline-block; }
  .filters { position: static; max-height: none; display: none; }
  .filters.open { display: block; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .tile .val { font-size: 20px; }
  #mappa { height: 60vh; }

  .btn, .btn.small, .tab, .chip, input[type=search], input[type=text], input[type=number], select { min-height: 44px; }
  .check { min-height: 44px; }
  .star { min-width: 44px; min-height: 44px; }
  details > summary { min-height: 44px; line-height: 44px; }

  /* l'elenco diventa un feed di card uniformi */
  .table-wrap.feed { background: transparent; border: 0; box-shadow: none; max-height: none; overflow: visible; }
  .feed .tbl thead { display: none; }
  .feed .tbl, .feed .tbl tbody { display: block; }
  .feed .tbl tbody tr {
    display: block; position: relative;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    padding: var(--pad) var(--pad) var(--pad) 56px; margin-bottom: 8px;
  }
  .feed .tbl tbody tr:hover { background: var(--surface); }
  .feed .tbl td { display: block; border: 0; padding: 0; }
  .feed .tbl td.c-star { position: absolute; left: 6px; top: 6px; }
  .feed .tbl td.num { display: inline-block; margin: 4px 12px 0 0; text-align: left; color: var(--secondary); font-size: 12px; }
  .feed .tbl td.num::before { content: attr(data-l) " "; }
  .feed .tbl td.c-badge { margin-top: 6px; }
  .feed .tbl td.c-punt { position: absolute; right: var(--pad); top: var(--pad); }
  .feed .tbl td.c-nome { padding-right: 48px; }
  .peso input { min-height: 44px; }
  .feed .tbl .hide-m { display: none; }
  .feed .tbl td.c-badge.hide-m { display: block; }
}

/* ---------- note di famiglia, prove del PTOF, IA ---------- */
.note-famiglia { border: 1px solid var(--primary-line); background: var(--primary-soft); border-radius: var(--radius); padding: var(--pad); margin: 0 0 14px; }
.note-famiglia h3 { margin-top: 0; }
.nota-altro { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s); padding: 8px 10px; margin-bottom: 8px; }
.nota-altro p { margin: 4px 0 0; white-space: pre-wrap; }
.voto { color: var(--star); letter-spacing: 1px; }
.nota-mia { display: grid; gap: 8px; }
.nota-mia textarea, .nota-mia input, .nota-mia select, .dlg-ia textarea { width: 100%; box-sizing: border-box; font: inherit; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius-s); background: var(--surface); color: var(--ink); }
.nota-mia .btn { justify-self: start; }
.evidenze details { margin: 4px 0; }
.prove { list-style: none; padding-left: 0; margin: 6px 0; }
.prove li { margin-bottom: 8px; }
.prove blockquote { margin: 4px 0 0; padding: 4px 10px; border-left: 3px solid var(--line); color: var(--ink-2); font-size: .92em; }
.ev { display: inline-block; font-size: .75em; font-weight: 600; padding: 1px 7px; border-radius: 999px; border: 1px solid currentColor; }
.ev-verificato { color: var(--success); }
.ev-probabile { color: var(--warning); }
.ev-citato { color: var(--secondary); }
.avviso { position: fixed; left: 50%; bottom: 18px; transform: translate(-50%, 30px); opacity: 0; pointer-events: none; background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: var(--radius); box-shadow: 0 4px 16px rgba(0,0,0,.2); transition: opacity .2s, transform .2s; z-index: 3000; max-width: 90vw; }
.avviso.on { opacity: 1; transform: translate(-50%, 0); }
.avviso.errore { background: var(--error); color: #fff; }
.dlg-ia { width: min(640px, 94vw); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; background: var(--surface); color: var(--ink); }
.dlg-ia::backdrop { background: rgba(17, 24, 39, .45); }
.dlg-testa { display: flex; justify-content: space-between; align-items: center; margin: 0; }
.dlg-testa h2 { margin: 0; font-size: 1.15rem; }
#form-ia { display: grid; gap: 8px; margin: 8px 0; }
#form-ia .btn { justify-self: start; }
.risposta-ia { white-space: pre-wrap; line-height: 1.5; max-height: 50vh; overflow: auto; }
.risposta-ia:empty { display: none; }

[hidden] { display: none !important; }
