:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --primary: #0b1f3a;
  --primary-soft: #1f4e78;
  --accent: #2f5597;
  --border: #d7e1ee;
  --text: #102a43;
  --muted: #5b6f86;
  --danger: #d9534f;
  --warn: #f0ad4e;
  --ok: #2e9f4b;
  --radius: 14px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, "Segoe UI", Arial, sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, #f4f7fb 0%, #eef3fb 100%);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(11, 31, 58, 0.06);
}

.top-header {
  margin: 16px;
  padding: 16px 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 18px;
}

.brand-centered {
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 10px;
  width: 100%;
}

.brand img {
  height: 90px;
  max-width: min(100%, 520px);
  width: auto;
  border-radius: 10px;
}

.brand-centered h1 {
  margin: 0;
  color: var(--primary);
  font-size: 1.45rem;
}

.brand-centered p {
  margin: 0;
  color: var(--muted);
}

.brand h1 {
  margin: 0;
  color: var(--primary);
  font-size: 1.45rem;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
}

.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 16px;
  padding: 0 16px 16px;
  align-items: start;
}

.content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.report-area {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.report-title {
  margin: 0;
  color: var(--primary);
  font-size: 1.2rem;
  text-align: center;
}

.clients-panel {
  max-height: 42vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.sidebar, .clients-panel, .report-area > .card {
  padding: 14px;
}

.sidebar h2, .panel-head h2 {
  margin: 0 0 10px;
  color: var(--primary);
}

.field, .period-grid label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
  font-size: 0.9rem;
}

.period-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
}

.period-grid h3 {
  grid-column: 1 / -1;
  margin: 8px 0 0;
  color: var(--primary);
  font-size: 0.95rem;
}

#searchClient {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 0.92rem;
}

input[type="date"], input[type="text"], input[type="file"] {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 0.92rem;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0 12px;
}

.btn {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  font-weight: 600;
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  margin-bottom: 8px;
}

.btn-secondary {
  width: 100%;
  background: #fff;
  color: var(--primary);
}

.btn-light {
  background: #eef3fb;
  color: var(--primary);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.status, .muted {
  color: var(--muted);
  font-size: 0.86rem;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
}

.client-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.client-list {
  max-height: 360px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  background: #fbfdff;
}

.client-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 8px;
}

.client-item:hover { background: #f1f6fd; }
.client-item.active { background: #e8f0fb; }

.hidden { display: none; }

.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 12px;
}

.kpi {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  background: #fff;
  text-align: center;
}

.kpi .label { color: var(--muted); font-size: 0.82rem; font-weight: 600; }
.kpi .value { color: var(--primary); font-size: 1.1rem; font-weight: 700; margin-top: 4px; }

.charts-grid {
  display: block;
  margin-bottom: 12px;
  padding: 10px;
}

.chart-wrap {
  position: relative;
  width: 100%;
  min-height: 280px;
}

.chart-wrap canvas {
  width: 100% !important;
  height: 100% !important;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px;
  margin-bottom: 12px;
}

.tab {
  border: 1px solid var(--border);
  background: #eef3fb;
  border-radius: 10px 10px 0 0;
  padding: 8px 12px;
  cursor: pointer;
  font-weight: 600;
  flex: 1 1 auto;
  min-width: 140px;
  text-align: center;
}

.tab.active {
  background: #fff;
  color: var(--primary);
}

.table-wrap {
  overflow: auto;
  padding: 0;
  max-height: 55vh;
  min-height: 220px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th, td {
  border-bottom: 1px solid #edf2f8;
  padding: 8px 10px;
  text-align: center;
  vertical-align: middle;
}

th {
  background: #eef3fb;
  color: var(--primary);
  position: sticky;
  top: 0;
}

td.num, th.num { white-space: nowrap; }

.split-tables {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.table-section .table-wrap {
  margin-bottom: 0;
}

thead.th-persi th,
tr.th-persi th {
  background: #d9534f;
  color: #fff;
}

thead.th-calo th,
tr.th-calo th {
  background: #f0ad4e;
  color: #222;
}

tr.st-perso { background: #ffe8e8; }
tr.st-calo { background: #fff6e5; }
tr.st-nuovo, tr.st-crescita { background: #e9f8ec; }

.print-footer { display: none; }

@media print {
  @page {
    size: A4 landscape;
    margin: 8mm 10mm 22mm;
  }

  .sidebar,
  .clients-panel,
  .tabs,
  .client-actions,
  #searchClient,
  .btn,
  .charts-grid {
    display: none !important;
  }

  .split-tables,
  .table-wrap,
  .content,
  .report-area,
  #sectionPersi,
  #sectionCalo {
    max-height: none !important;
    overflow: visible !important;
  }

  #sectionPersi.hidden,
  #sectionCalo.hidden {
    display: block !important;
  }

  .layout { grid-template-columns: 1fr; }
  body { background: #fff; }
  .card { box-shadow: none; border: none; }

  .top-header {
    margin: 0 0 6px;
    padding: 6px 8px;
  }

  .brand img {
    height: 52px;
    max-width: 280px;
  }

  .brand-centered h1 {
    font-size: 1rem;
    margin: 0;
  }

  .brand-centered p {
    display: none;
  }

  .report-title {
    font-size: 1rem;
    margin: 0 0 6px;
  }

  .kpi-row {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    margin-bottom: 6px;
  }

  .kpi {
    padding: 6px;
  }

  .kpi .label { font-size: 0.72rem; }
  .kpi .value { font-size: 0.9rem; }

  table {
    font-size: 0.72rem;
    page-break-inside: auto;
  }

  thead {
    display: table-header-group;
  }

  th {
    position: static !important;
    top: auto !important;
    padding: 4px 5px;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  td {
    padding: 4px 5px;
  }

  tr.th-persi th,
  tr.th-calo th {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .report-area {
    padding-bottom: 4mm;
  }
}

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .clients-panel { max-height: none; }
  .client-list { max-height: 280px; }
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .top-header {
    margin: 8px;
    padding: 12px;
  }

  .brand img {
    height: 68px;
  }

  .brand-centered h1 {
    font-size: clamp(0.95rem, 4vw, 1.2rem);
  }

  .layout {
    padding: 0 8px 12px;
    gap: 12px;
  }

  .sidebar,
  .clients-panel,
  .report-area > .card {
    padding: 12px;
  }

  .panel-head {
    flex-direction: column;
    align-items: stretch;
  }

  .client-actions {
    flex-direction: column;
  }

  .client-actions .btn {
    width: 100%;
  }

  .period-grid {
    grid-template-columns: 1fr;
  }

  .kpi .value {
    font-size: 1rem;
  }

  .chart-wrap {
    min-height: 200px;
  }

  .table-wrap {
    max-height: none;
  }

  table {
    font-size: 0.82rem;
  }

  th, td {
    padding: 6px 5px;
  }

  .tab {
    min-width: 0;
    font-size: 0.85rem;
    border-radius: 10px;
  }
}

@media (max-width: 480px) {
  .kpi-row {
    grid-template-columns: 1fr;
  }

  .brand-centered p {
    font-size: 0.82rem;
  }
}
