:root {
  --bg: #f5f4ee;
  --surface: #ffffff;
  --text: #2c2c2a;
  --text-muted: #5f5e5a;
  --border: #d3d1c7;
  --accent: #185fa5;
  --accent-bg: #e6f1fb;
  --success: #0f6e56;
  --warning: #854f0b;
  --danger: #a32d2d;
  --radius: 8px;
  --radius-lg: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
}

.container { max-width: 1100px; margin: 0 auto; padding: 0 1.5rem; }

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem 0;
  margin-bottom: 1.5rem;
}
.topbar .container { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.topbar h1 { font-size: 20px; font-weight: 500; margin: 0; }
.status { font-size: 13px; color: var(--text-muted); display: flex; align-items: center; gap: 12px; }
.status .admin-link {
  color: var(--text-muted); text-decoration: none; font-size: 12px;
  border: 1px solid var(--border); padding: 2px 8px; border-radius: 3px;
}
.status .admin-link:hover { background: #fff; color: var(--accent); }

.tabs {
  display: flex;
  gap: 0.25rem;
  margin-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0;
}
.tab {
  font: inherit;
  font-weight: 500;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-bottom: 3px solid transparent;
  padding: 0.55rem 1rem;
  margin-bottom: -1px;
  cursor: pointer;
  border-radius: 0;
}
.tab:hover { color: var(--text); background: transparent; opacity: 1; }
.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  background: transparent;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.25rem;
}
.card h2 { font-size: 17px; font-weight: 500; margin: 0 0 1rem; }
.card h3 { font-size: 15px; font-weight: 500; margin: 0 0 0.5rem; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 0.9rem 1rem;
  margin-bottom: 1rem;
}
label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 13px; color: var(--text-muted); }
label > span { font-weight: 500; color: var(--text); }
label > small { color: var(--text-muted); font-size: 11px; }

input[type="number"], select {
  font: inherit;
  padding: 0.5rem 0.7rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

.actions { display: flex; gap: 0.75rem; }
button {
  font: inherit;
  font-weight: 500;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius);
  border: 1px solid var(--accent);
  background: var(--accent);
  color: white;
  cursor: pointer;
}
button:hover { opacity: 0.9; }
button.secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.hidden { display: none !important; }

.riepilogo-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 1rem;
}
.riepilogo-row > div { display: flex; flex-direction: column; gap: 2px; }
.riepilogo-row .muted { font-size: 12px; color: var(--text-muted); }
.riepilogo-row strong { font-size: 22px; font-weight: 500; }

.muted { color: var(--text-muted); }

#risultati { display: flex; flex-direction: column; gap: 0.6rem; margin-bottom: 1.5rem; }

.offerta {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.9rem 1.1rem;
  display: grid;
  grid-template-columns: 40px 1fr auto auto;
  gap: 0.75rem 1rem;
  align-items: center;
  cursor: pointer;
  transition: border-color 0.15s;
}
.offerta:hover { border-color: var(--accent); }
.offerta.no-link { cursor: default; grid-template-columns: 40px 1fr auto; }
.offerta.no-link:hover { border-color: var(--border); }
.offerta .rank { font-weight: 500; font-size: 16px; color: var(--text-muted); text-align: center; }
.offerta .rank.top1 { color: var(--success); }
.offerta .titolo { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.offerta .titolo .nome { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.offerta .titolo .meta { font-size: 12px; color: var(--text-muted); display: flex; gap: 0.5rem; flex-wrap: wrap; }
.offerta .badge {
  display: inline-block;
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--text-muted);
}
.offerta .badge.fixed { background: var(--accent-bg); color: var(--accent); }
.offerta .badge.variable { background: #faeeda; color: var(--warning); }
.offerta .spesa { text-align: right; }
.offerta .spesa .valore { font-size: 20px; font-weight: 500; }
.offerta .spesa .label { font-size: 11px; color: var(--text-muted); }
.offerta .arrow { color: var(--text-muted); }

@media (max-width: 600px) {
  .offerta, .offerta.no-link { grid-template-columns: 30px 1fr auto; }
  .offerta .arrow { display: none; }
}

.note p { font-size: 13px; color: var(--text); margin: 0.5rem 0; }
.note p.muted { color: var(--text-muted); }

#dettaglio { position: relative; }
#dettaglio .close {
  position: absolute; top: 0.75rem; right: 0.75rem;
  background: transparent; color: var(--text-muted); border: none;
  font-size: 22px; cursor: pointer; padding: 0 0.35rem;
}
#dettaglio h2 { margin-top: 0; }
.dett-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
  margin: 1rem 0;
}
@media (max-width: 700px) { .dett-grid { grid-template-columns: 1fr; } }
.dett-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.dett-table td { padding: 0.4rem 0; border-bottom: 1px solid var(--border); }
.dett-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.dett-table tr.totale td { font-weight: 500; border-bottom: 2px solid var(--text); padding-top: 0.6rem; }
.dett-table tr.subtotale td { font-weight: 500; }
.dett-table tr.sub-row td { font-size: 12px; color: var(--text-muted); border-bottom: 1px dashed var(--border); padding: 0.25rem 0; }
.dett-table tr.muted-row td { font-size: 12px; color: var(--text-muted); }
.dett-table tr.debug-warn td { font-size: 12px; color: var(--warning); background: #faeeda; padding: 0.4rem 0.5rem; }
.dett-componenti li { font-size: 13px; padding: 0.25rem 0; }

/* Blocco debug del calcolo di comparazione (sezione "La tua offerta attuale") */
.bill-debug { margin-top: 1.25rem; border: 1px solid var(--border); border-radius: 8px; padding: 0 0.75rem; }
.bill-debug > summary { cursor: pointer; font-weight: 500; padding: 0.6rem 0; list-style: revert; }
.bill-debug > summary:hover { color: var(--accent); }
.bill-debug[open] > summary { border-bottom: 1px solid var(--border); margin-bottom: 0.5rem; }
.bill-debug h4 { margin: 1rem 0 0.4rem; font-size: 13px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); }
.bill-debug > *:last-child { margin-bottom: 0.75rem; }

.scheda-link {
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dotted var(--accent);
  padding-bottom: 1px;
}
.scheda-link:hover { background: var(--accent-bg); }

.scheda-h4 { font-size: 13px; font-weight: 500; margin: 1rem 0 0.4rem; color: var(--text-muted); }
.scheda-table { margin-bottom: 0.75rem; font-size: 13px; }
.scheda-table th { text-align: left; font-weight: 500; color: var(--text-muted); padding: 0.3rem 0; border-bottom: 1px solid var(--border); font-size: 12px; }
.scheda-desc { font-size: 12px; margin-top: 0.25rem; }
.scheda-table-wrap { overflow-x: auto; margin-bottom: 0.75rem; }
.scheda-matrice { min-width: 540px; font-variant-numeric: tabular-nums; }
.scheda-matrice th, .scheda-matrice td { padding: 0.35rem 0.6rem; }
.scheda-matrice th + th, .scheda-matrice td + td { text-align: right; }
.scheda-matrice tbody tr:hover { background: var(--bg); }
.dett-actions { display: flex; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }
.dett-actions a {
  text-decoration: none; color: var(--accent); padding: 0.4rem 0.8rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 13px; font-weight: 500;
}
.dett-actions a:hover { background: var(--accent-bg); border-color: var(--accent); }
.dett-actions a.cte-btn,
.dett-actions button.cte-btn {
  background: var(--accent); color: #fff; border-color: var(--accent);
  border-style: solid; border-width: 1px; border-radius: var(--radius);
  font-size: 13px; font-weight: 500; padding: 0.4rem 0.8rem;
  cursor: pointer; font-family: inherit;
}
.dett-actions a.cte-btn:hover,
.dett-actions button.cte-btn:hover { background: #134d8a; border-color: #134d8a; }
.dett-actions button.cte-btn:disabled { background: #888; border-color: #888; cursor: wait; }

/* Overlay full-screen mostrato durante la generazione del PDF della CTE */
#cte-pdf-overlay {
  position: fixed; inset: 0; background: rgba(20, 30, 50, 0.55);
  display: none; align-items: center; justify-content: center; z-index: 9999;
  backdrop-filter: blur(2px);
}
#cte-pdf-overlay.show { display: flex; }
#cte-pdf-overlay .cte-pdf-box {
  background: #fff; border: 1px solid #d0d0d0; border-radius: 8px;
  padding: 24px 32px; max-width: 90vw; min-width: 280px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25); text-align: center;
}
#cte-pdf-overlay .cte-pdf-spinner {
  width: 38px; height: 38px; border: 4px solid #e0e0e0;
  border-top-color: var(--accent, #0a66c2); border-radius: 50%;
  margin: 0 auto 14px; animation: cte-pdf-spin 0.9s linear infinite;
}
@keyframes cte-pdf-spin { to { transform: rotate(360deg); } }
#cte-pdf-overlay .cte-pdf-msg { font-size: 14px; color: #1a1a1a; margin: 0 0 4px; }
#cte-pdf-overlay .cte-pdf-sub { font-size: 12px; color: #666; }
#cte-pdf-overlay .cte-pdf-cancel {
  margin-top: 14px; padding: 6px 16px; background: #fff;
  border: 1px solid var(--border, #d0d0d0); border-radius: 4px;
  font-size: 13px; cursor: pointer; color: #1a1a1a;
}
#cte-pdf-overlay .cte-pdf-cancel:hover { background: #f4f4f4; }
#cte-pdf-overlay.error .cte-pdf-spinner { display: none; }
#cte-pdf-overlay.error .cte-pdf-msg { color: #a01515; }

.empty {
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
}
.loading { padding: 1rem; text-align: center; color: var(--text-muted); }

/* ============ Dropzone "Carica PDF" ============ */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 2.25rem 1.5rem;
  text-align: center;
  cursor: pointer;
  background: var(--bg);
  transition: background 0.15s, border-color 0.15s;
  outline: none;
}
.dropzone:hover, .dropzone:focus { border-color: var(--accent); background: var(--accent-bg); }
.dropzone.dragover { border-color: var(--accent); background: var(--accent-bg); }
.dropzone.disabled { cursor: wait; opacity: 0.7; }
.dropzone-icon { font-size: 36px; color: var(--accent); line-height: 1; margin-bottom: 0.5rem; }
.dropzone-msg { font-size: 16px; margin-bottom: 0.35rem; }
.dropzone-msg strong { color: var(--text); font-weight: 500; }
.dropzone-hint { font-size: 12px; }

.bill-status {
  margin-top: 1rem; padding: 0.7rem 1rem; border-radius: var(--radius);
  background: var(--accent-bg); color: var(--accent); font-size: 14px;
  display: flex; align-items: center; gap: 0.6rem;
}
.bill-status.error { background: #f9e2e2; color: var(--danger); }
.bill-status .spinner {
  width: 16px; height: 16px; border: 2px solid currentColor;
  border-top-color: transparent; border-radius: 50%;
  animation: cte-pdf-spin 0.9s linear infinite;
}

/* Risultati analisi bolletta */
.bill-extract-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem 1.25rem;
  margin-bottom: 0.5rem;
}
.bill-extract-grid .field {
  display: flex; flex-direction: column; gap: 2px;
  font-size: 14px;
}
.bill-extract-grid .field .label { font-size: 12px; color: var(--text-muted); }
.bill-extract-grid .field .value { font-weight: 500; word-break: break-all; }
.bill-extract-grid .field .value.missing { color: var(--text-muted); font-weight: normal; font-style: italic; }

.bill-saving-card {
  display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1rem;
  background: linear-gradient(135deg, #e6f1fb 0%, #ffffff 100%);
  border: 1px solid var(--accent); border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem; margin-bottom: 1.25rem;
}
.bill-saving-card > div { text-align: center; }
.bill-saving-card .stat-value { font-size: 22px; font-weight: 500; }
.bill-saving-card .stat-value.savings { color: var(--success); font-size: 28px; }
.bill-saving-card .stat-value.negative { color: var(--danger); }
.bill-saving-card .stat-label { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
@media (max-width: 600px) {
  .bill-saving-card { grid-template-columns: 1fr; }
}

.bill-match-card .match-row {
  display: grid; grid-template-columns: 1fr 1fr; gap: 1rem;
  margin-top: 0.5rem;
}
.bill-match-card .delta { font-size: 13px; color: var(--text-muted); margin-top: 0.5rem; }
.bill-match-card .delta.warn { color: var(--warning); }
.bill-match-card .delta.bad { color: var(--danger); }
.bill-match-card .delta.ok { color: var(--success); }
@media (max-width: 600px) {
  .bill-match-card .match-row { grid-template-columns: 1fr; }
}

.bill-warning-list {
  margin: 0.5rem 0 0; padding: 0; list-style: none; font-size: 13px;
}
.bill-warning-list li { padding: 4px 0; color: var(--warning); }
.bill-warning-list li::before { content: "⚠ "; }
