:root {
  --bg: #f3f5f7;
  --panel: #ffffff;
  --text: #111827;
  --muted: #5b6472;
  --line: #cfd6df;
  --head: #ffd824;
  --tax: #ffb6b0;
  --green: #92d050;
  --blue: #e2ebf1;
  --black: #050505;
  --accent: #1f4e5f;
  --planning-green: #76b947;
  --dash-section: #e2ebf1;
  --dash-tax: #ffb6b0;
  --dash-yellow: #ffff00;
  --dash-green: #92d050;
  --dash-cost: #ffb6b0;
  --status-planned: #e8f1ff;
  --status-confirmed: #fff4bf;
  --status-invoiced: #dbeafe;
  --status-done: #d9f2d0;
  --status-cancelled: #ffd6d6;
}

.modal-panel.pdf-invoice-panel {
  width: min(1120px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 24px));
  overflow: auto;
}

.pdf-invoice-panel .pdf-invoice-fields {
  grid-template-columns: repeat(12, minmax(0, 1fr));
  align-items: end;
}

.pdf-invoice-fields .pdf-field-sm {
  grid-column: span 2;
}

.pdf-invoice-fields .pdf-field-md {
  grid-column: span 3;
}

.pdf-invoice-fields .pdf-field-lg {
  grid-column: span 4;
}

.pdf-invoice-fields .pdf-field-wide {
  grid-column: span 6;
}

.pdf-invoice-fields input {
  width: 100%;
  height: 38px;
}

@media (max-width: 900px) {
  .pdf-invoice-panel .pdf-invoice-fields {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .pdf-invoice-fields .pdf-field-sm,
  .pdf-invoice-fields .pdf-field-md,
  .pdf-invoice-fields .pdf-field-lg,
  .pdf-invoice-fields .pdf-field-wide {
    grid-column: span 1;
  }
}

* { box-sizing: border-box; }

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Segoe UI, Arial, sans-serif;
  font-size: 15px;
}

.app-header {
  min-height: 72px;
  padding: 14px 20px;
  background: #fff7ed;
  border-bottom: 4px solid #f97316;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.test-env-badge {
  display: inline-block;
  margin-bottom: 4px;
  padding: 3px 9px;
  border-radius: 6px;
  background: #c2410c;
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: .04em;
}

.test-env-strip {
  padding: 7px 20px;
  background: #f97316;
  color: #111827;
  font-weight: 800;
  text-align: center;
  border-bottom: 1px solid #c2410c;
}

h1, h2, p { margin: 0; }
h1 { font-size: 24px; font-weight: 700; }
h2 { font-size: 18px; }
p, .muted { color: var(--muted); }

.header-actions, .archive-actions, .toolbar, .filters {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
}

button, select, input {
  font: inherit;
}

button {
  border: 1px solid #b9c2cc;
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
}

button:hover { background: #eef3f6; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 13px;
}

select, input {
  border: 1px solid #b9c2cc;
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
  min-height: 38px;
}

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  background: #e9edf1;
  border-bottom: 1px solid var(--line);
  padding: 0 10px;
}

.tab {
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 12px 14px;
  white-space: nowrap;
}

.tab.active {
  background: #fff;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  font-weight: 700;
}

main { padding: 18px; }
.view { display: none; }
.view.active { display: block; }

.toolbar, .filters, .archive-head, .stats, .cards, .dashboard-sheet, .table-wrap {
  margin-bottom: 14px;
}

.archive-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.archive-subnav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: -4px 0 10px;
}

.archive-subnav button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.quick-form {
  display: grid;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
  gap: 10px;
  align-items: end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 14px;
}

.archive-invoice-form {
  position: sticky;
  top: 0;
  z-index: 8;
  box-shadow: 0 2px 10px rgba(17, 24, 39, .08);
}

.quick-form button {
  min-height: 38px;
}

.invoice-form {
  grid-template-columns: repeat(14, minmax(58px, 1fr));
  gap: 8px;
}

.invoice-form label {
  min-width: 0;
}

.invoice-form input,
.invoice-form select {
  width: 100%;
}

.invoice-form label:has(#quickDataCompetenza) {
  grid-column: 1 / span 2;
  grid-row: 1;
}

.invoice-form label:has(#quickDataFattura) {
  grid-column: 3 / span 2;
  grid-row: 1;
}

.invoice-form label:has(#quickNumero) {
  grid-column: 5 / span 2;
  grid-row: 1;
}

.invoice-form label:has(#quickCliente) {
  grid-column: 7 / span 6;
  grid-row: 1;
}

.invoice-form label:has(#quickTipologia) {
  grid-column: 13 / span 2;
  grid-row: 1;
}

.invoice-form label:has(#quickImponibile) {
  grid-column: 1 / span 2;
  grid-row: 2;
}

.invoice-form label:has(#quickRivalsa) {
  grid-column: 3 / span 2;
  grid-row: 2;
}

.invoice-form label:has(#quickBollo) {
  grid-column: 5 / span 2;
  grid-row: 2;
}

.invoice-form label:has(#quickTotale) {
  grid-column: 7 / span 4;
  grid-row: 2;
}

.invoice-form label:has(#quickInps) {
  grid-column: 11 / span 2;
  grid-row: 2;
}

.invoice-form label:has(#quickIrpef) {
  grid-column: 13 / span 2;
  grid-row: 2;
}

.invoice-paid-field {
  grid-column: 1 / span 2;
  grid-row: 3;
}

.invoice-collection-date-field {
  grid-column: 3 / span 2;
  grid-row: 3;
}

.invoice-description-field {
  grid-column: 5 / span 5;
  grid-row: 3;
}

#invoiceSubmitBtn {
  grid-column: 13;
  grid-row: 3;
}

#cancelInvoiceEditBtn {
  grid-column: 14;
  grid-row: 3;
}

#invoiceQuickForm:not(:has(#cancelInvoiceEditBtn:not([hidden]))) #invoiceSubmitBtn {
  grid-column: 13 / span 2;
}

.invoice-form select:disabled {
  background: #eef2f6;
  color: var(--text);
  opacity: 1;
}

.cost-form {
  grid-template-columns: 120px 120px minmax(170px, 1fr) minmax(180px, 1fr) 105px 150px;
  gap: 8px;
}

.other-form {
  grid-template-columns: 120px 120px 120px minmax(220px, 1fr) 110px minmax(170px, 1fr) 130px;
  gap: 8px;
}

.investment-form {
  grid-template-columns: 125px 125px 95px 110px minmax(180px, 1fr) minmax(170px, 1fr) 120px;
  gap: 8px;
}

.investment-form label {
  min-width: 0;
}

.investment-form input,
.investment-form select {
  width: 100%;
}

.select-field {
  display: grid;
  grid-template-columns: 1fr 38px;
  gap: 4px;
}

.select-field input {
  min-width: 0;
}

.pick-btn {
  padding: 0;
  min-height: 38px;
}

.editable-wrap {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) 32px;
  gap: 4px;
  align-items: center;
}

.editable-wrap .editable {
  min-width: 0;
}

.cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 6px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  min-height: 58px;
}

.card .label {
  color: var(--muted);
  font-size: 12px;
}

.card .value {
  margin-top: 2px;
  text-align: right;
  font-size: 18px;
  font-weight: 800;
}

.card.status-planned { background: var(--status-planned); }
.card.status-confirmed { background: var(--status-confirmed); }
.card.status-invoiced { background: var(--status-invoiced); }
.card.status-done { background: var(--status-done); }
.card.status-cancelled { background: var(--status-cancelled); }

.dashboard-unpaid-card {
  cursor: pointer;
}

.dashboard-unpaid-card.is-valued {
  background: var(--tax);
  border-color: var(--tax);
}

.dashboard-unpaid-card.is-empty {
  background: var(--panel);
  border-color: var(--line);
}

.dashboard-unpaid-card:hover {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

#dashboardCards {
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: 5px;
}

#dashboardCards .card {
  min-width: 0;
  padding: 7px 7px;
}

#dashboardCards .card .label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#dashboardCards .card .value {
  font-size: 16px;
}

.invoice-planning-field {
  grid-column: 10 / span 3;
  grid-row: 3;
}

.invoice-planning-field button {
  min-height: 38px;
  width: 100%;
}

.field-hint {
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
}

.planning-link-wrap input[readonly] {
  background: #eef2f6;
}

.planning-link-panel {
  width: min(1050px, 100%);
}

.pdf-invoice-panel {
  width: min(1080px, 100%);
}

.pdf-invoice-fields {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 10px;
  margin: 12px 0 16px;
}

.pdf-invoice-fields label {
  display: grid;
  gap: 5px;
  color: var(--muted);
}

.pdf-invoice-fields input,
.pdf-invoice-fields select {
  min-width: 0;
}

.pdf-duplicate {
  border: 1px solid #dc2626;
  background: #fee2e2;
  color: #7f1d1d;
  padding: 10px 12px;
  border-radius: 6px;
  margin: 10px 0;
  font-weight: 700;
}

.planning-link-list {
  display: grid;
  gap: 6px;
  max-height: 55vh;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  background: var(--panel);
}

.planning-link-row {
  display: grid;
  grid-template-columns: 24px 1fr;
  gap: 8px;
  align-items: center;
  padding: 6px;
  border-bottom: 1px solid var(--line);
}

.planning-link-row:last-child {
  border-bottom: 0;
}

.planning-link-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 8px 0;
}

.planning-link-heading h3 {
  margin: 0;
  flex: 0 0 auto;
}

.planning-link-heading button {
  flex: 0 0 auto;
  white-space: nowrap;
}

.planning-link-balance {
  border: 1px solid var(--line);
  background: #f8fafc;
  padding: 10px 12px;
  border-radius: 6px;
  font-weight: 600;
  flex: 1;
  text-align: right;
}

.card .value span {
  font-size: 13px;
  font-weight: 500;
}

#planningSummary {
  grid-template-columns: repeat(6, minmax(105px, 1fr));
  gap: 6px;
}

#planningSummary .card {
  min-height: 44px;
  padding: 6px 10px;
}

#planningSummary .card .label {
  font-size: 11px;
}

#planningSummary .card .value {
  font-size: 16px;
  line-height: 1.15;
}

#planningSummary .card .value span {
  font-size: 12px;
}

.dashboard-sheet, .table-wrap {
  background: var(--panel);
  border: 1px solid var(--line);
  overflow: auto;
}

.dashboard-subnav,
.report-subnav {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.dashboard-subnav button.active,
.report-subnav button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.dashboard-panel {
  display: none;
}

.dashboard-panel.active {
  display: block;
}

#dashboardCostsPanel .cards,
#dashboardClientsPanel .cards,
#dashboardInvestmentsPanel .cards {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  max-width: 980px;
}

#archiveTable {
  max-height: calc(100vh - 335px);
}

#reportTable {
  max-height: calc(100vh - 210px);
}

#reportTable .total-row td {
  background: var(--dash-green);
  font-weight: 800;
}

.cost-dashboard-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(320px, 1fr));
  gap: 10px;
  margin-top: 10px;
}

.cost-dashboard-grid section {
  min-width: 0;
}

.cost-dashboard-grid h2 {
  margin: 4px 0 6px;
  font-size: 15px;
}

.cost-dashboard-grid .wide {
  grid-column: 1 / -1;
}

.client-dashboard-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-top: 10px;
}

.client-dashboard-grid h2 {
  margin: 4px 0 6px;
  font-size: 15px;
}

.client-chart {
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 10px;
  overflow: auto;
}

.client-chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 10px;
  font-weight: 700;
}

.client-chart-legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.client-chart-legend i {
  width: 16px;
  height: 16px;
  border: 1px solid #9aa6b2;
}

.client-chart-row {
  display: grid;
  grid-template-columns: minmax(190px, 280px) 1fr;
  gap: 10px;
  align-items: start;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}

.client-chart-row:last-child {
  border-bottom: 0;
}

.client-chart-label {
  font-weight: 700;
  overflow-wrap: anywhere;
}

.client-chart-bars {
  display: grid;
  gap: 4px;
  min-width: 320px;
}

.client-chart-bar-line {
  display: grid;
  grid-template-columns: minmax(40px, 1fr) 110px;
  gap: 8px;
  align-items: center;
}

.client-chart-bar {
  display: block;
  height: 16px;
  min-width: 2px;
  border-radius: 2px;
}

.client-chart-bar-line b {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.client-chart-bar-line b em {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-style: normal;
  font-weight: 700;
}

.client-column-chart {
  min-width: 760px;
  display: flex;
  align-items: end;
  gap: 14px;
  padding-top: 8px;
}

.client-column-group {
  min-width: 160px;
  display: grid;
  gap: 8px;
  justify-items: center;
}

.client-column-bars {
  min-height: 190px;
  display: flex;
  align-items: end;
  gap: 6px;
  border-bottom: 2px solid #9aa6b2;
  padding: 0 6px;
}

.client-column-item {
  width: 46px;
  display: grid;
  grid-template-rows: 132px auto auto auto;
  gap: 4px;
  align-items: end;
  justify-items: center;
  text-align: center;
}

.client-column-item span {
  display: block;
  width: 26px;
  border-radius: 3px 3px 0 0;
}

.client-column-item b {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.client-column-item em {
  color: var(--muted);
  font-size: 11px;
  font-style: normal;
  font-weight: 700;
  white-space: nowrap;
}

.client-column-item small {
  color: var(--muted);
  font-weight: 700;
}

.client-column-label {
  max-width: 160px;
  min-height: 38px;
  font-weight: 700;
  text-align: center;
  overflow-wrap: anywhere;
}

.client-trend-line-row {
  display: grid;
  grid-template-columns: minmax(190px, 280px) 1fr;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}

.client-trend-line-row:last-child {
  border-bottom: 0;
}

.client-trend-line {
  min-width: 420px;
  display: flex;
  align-items: end;
  gap: 10px;
}

.client-trend-mini {
  min-width: 92px;
  display: grid;
  grid-template-rows: 104px auto auto;
  gap: 4px;
  align-items: end;
  justify-items: center;
  text-align: center;
}

.client-trend-mini span {
  display: block;
  width: 34px;
  border-radius: 3px 3px 0 0;
}

.client-trend-mini b {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.client-trend-mini small {
  color: var(--muted);
  font-weight: 700;
}

.client-trend-chart {
  min-height: 260px;
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 12px;
  display: flex;
  align-items: end;
  gap: 16px;
  overflow-x: auto;
}

.client-trend-item {
  min-width: 150px;
  display: grid;
  grid-template-rows: auto 180px auto auto;
  gap: 6px;
  align-items: end;
  text-align: center;
}

.client-trend-value {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.client-trend-bar-wrap {
  height: 180px;
  display: flex;
  align-items: end;
  justify-content: center;
  border-bottom: 2px solid #9aa6b2;
}

.client-trend-bar {
  width: 72px;
  background: var(--accent);
  border: 1px solid #173b49;
  border-radius: 4px 4px 0 0;
}

.client-trend-year {
  font-weight: 800;
}

.client-trend-item .positive {
  color: #1f7a34;
  font-weight: 700;
}

.client-trend-item .negative {
  color: #b42318;
  font-weight: 700;
}

#dashboardInvestmentsPanel.comparison-mode .investment-summary-section {
  grid-column: 1 / -1;
}

#costMacroTable,
#costCategoryTable,
#costMonthlyTable,
#clientRevenueTable,
#investmentTypeTable,
#investmentLossTable,
#investmentInstrumentTable,
#investmentMonthlyTable,
#investmentReturnDetailTable,
#investmentLossDetailTable {
  max-height: 360px;
}

#costMacroTable,
#costCategoryTable,
#investmentTypeTable,
#investmentLossTable,
#investmentInstrumentTable {
  overflow-x: auto;
  overflow-y: auto;
}

#costMonthlyTable,
#clientRevenueTable,
#investmentMonthlyTable,
#investmentReturnDetailTable,
#investmentLossDetailTable {
  overflow-x: auto;
  overflow-y: auto;
}

#costMacroTable table,
#costCategoryTable table,
#costMonthlyTable table,
#clientRevenueTable table,
#investmentTypeTable table,
#investmentLossTable table,
#investmentInstrumentTable table,
#investmentMonthlyTable table,
#investmentReturnDetailTable table,
#investmentLossDetailTable table {
  min-width: 620px;
  table-layout: auto;
  width: 100%;
}

#costMacroTable th,
#costMacroTable td,
#costCategoryTable th,
#costCategoryTable td,
#costMonthlyTable th,
#costMonthlyTable td,
#clientRevenueTable th,
#clientRevenueTable td,
#investmentTypeTable th,
#investmentTypeTable td,
#investmentLossTable th,
#investmentLossTable td,
#investmentInstrumentTable th,
#investmentInstrumentTable td,
#investmentMonthlyTable th,
#investmentMonthlyTable td,
#investmentReturnDetailTable th,
#investmentReturnDetailTable td,
#investmentLossDetailTable th,
#investmentLossDetailTable td {
  padding: 6px 8px;
  font-size: 13px;
  line-height: 1.25;
}

#costMacroTable th:first-child,
#costMacroTable td:first-child,
#costCategoryTable th:first-child,
#costCategoryTable td:first-child,
#investmentTypeTable th:first-child,
#investmentTypeTable td:first-child,
#investmentLossTable th:first-child,
#investmentLossTable td:first-child,
#investmentInstrumentTable th:first-child,
#investmentInstrumentTable td:first-child {
  width: 250px;
  min-width: 250px;
  white-space: normal;
  overflow-wrap: anywhere;
}

#costMonthlyTable th:first-child,
#costMonthlyTable td:first-child,
#clientRevenueTable th:first-child,
#clientRevenueTable td:first-child,
#investmentMonthlyTable th:first-child,
#investmentMonthlyTable td:first-child,
#investmentReturnDetailTable th:first-child,
#investmentReturnDetailTable td:first-child,
#investmentLossDetailTable th:first-child,
#investmentLossDetailTable td:first-child {
  width: 190px;
  min-width: 190px;
  white-space: normal;
}

#costMacroTable th.num,
#costMacroTable td.num,
#costCategoryTable th.num,
#costCategoryTable td.num,
#costMonthlyTable th.num,
#costMonthlyTable td.num,
#clientRevenueTable th.num,
#clientRevenueTable td.num,
#investmentTypeTable th.num,
#investmentTypeTable td.num,
#investmentLossTable th.num,
#investmentLossTable td.num,
#investmentInstrumentTable th.num,
#investmentInstrumentTable td.num,
#investmentMonthlyTable th.num,
#investmentMonthlyTable td.num,
#investmentReturnDetailTable th.num,
#investmentReturnDetailTable td.num,
#investmentLossDetailTable th.num,
#investmentLossDetailTable td.num {
  min-width: 105px;
  text-align: right;
  white-space: nowrap;
  font-weight: 700;
}

#dashboardInvestmentsPanel.comparison-mode #investmentTypeTable th.num,
#dashboardInvestmentsPanel.comparison-mode #investmentTypeTable td.num,
#dashboardInvestmentsPanel.comparison-mode #investmentLossTable th.num,
#dashboardInvestmentsPanel.comparison-mode #investmentLossTable td.num,
#dashboardInvestmentsPanel.comparison-mode #investmentInstrumentTable th.num,
#dashboardInvestmentsPanel.comparison-mode #investmentInstrumentTable td.num,
#dashboardInvestmentsPanel.comparison-mode #investmentMonthlyTable th.num,
#dashboardInvestmentsPanel.comparison-mode #investmentMonthlyTable td.num {
  min-width: 82px;
  padding-left: 5px;
  padding-right: 5px;
}

#dashboardInvestmentsPanel.comparison-mode #investmentTypeTable th,
#dashboardInvestmentsPanel.comparison-mode #investmentLossTable th,
#dashboardInvestmentsPanel.comparison-mode #investmentInstrumentTable th,
#dashboardInvestmentsPanel.comparison-mode #investmentMonthlyTable th {
  white-space: normal;
  line-height: 1.1;
}

#costMacroTable,
#costCategoryTable {
  overflow-x: hidden;
}

#costMonthlyTable {
  overflow-x: hidden;
}

#costMacroTable table,
#costCategoryTable table,
#costMonthlyTable table {
  min-width: 0;
  table-layout: fixed;
}

#costMacroTable th,
#costMacroTable td,
#costCategoryTable th,
#costCategoryTable td,
#costMonthlyTable th,
#costMonthlyTable td {
  padding: 7px 9px;
  font-size: 14px;
}

#costMacroTable th:first-child,
#costMacroTable td:first-child,
#costCategoryTable th:first-child,
#costCategoryTable td:first-child {
  width: 56%;
  min-width: 0;
}

#costMonthlyTable th:first-child,
#costMonthlyTable td:first-child {
  width: 38%;
  min-width: 0;
}

#costMacroTable th.num,
#costMacroTable td.num,
#costCategoryTable th.num,
#costCategoryTable td.num,
#costMonthlyTable th.num,
#costMonthlyTable td.num {
  width: auto;
  min-width: 0;
}

#costMacroTable .total-row td,
#costCategoryTable .total-row td,
#costMonthlyTable .total-row td,
#clientRevenueTable .total-row td,
#investmentTypeTable .total-row td,
#investmentLossTable .total-row td,
#investmentInstrumentTable .total-row td,
#investmentMonthlyTable .total-row td,
#investmentReturnDetailTable .total-row td,
#investmentLossDetailTable .total-row td {
  background: var(--dash-green);
  font-weight: 800;
}

#planningTable {
  max-height: 34vh;
  border-color: #b9c4cf;
}

#planningTable table {
  min-width: 1900px;
  table-layout: fixed;
  font-size: 13px;
}

#planningTable .action-col { width: 82px; }
#planningTable .col-id { width: 58px; }
#planningTable .col-data { width: 125px; }
#planningTable .col-ora { width: 90px; }
#planningTable .col-cliente { width: 220px; }
#planningTable .col-localita { width: 180px; }
#planningTable .col-km,
#planningTable .col-persone { width: 95px; }
#planningTable .col-tempo-percorrenza { width: 135px; }
#planningTable .col-lordo-orario { width: 125px; }
#planningTable .col-attivita { width: 170px; }
#planningTable .col-descrizione { width: 230px; }
#planningTable .col-ore { width: 95px; }
#planningTable .col-importo { width: 110px; }
#planningTable .col-stato { width: 145px; }
#planningTable .col-note { width: 210px; }

#planningTable tbody tr.status-planned td { background: color-mix(in srgb, var(--status-planned) 72%, #fff); }
#planningTable tbody tr.status-confirmed td { background: color-mix(in srgb, var(--status-confirmed) 72%, #fff); }
#planningTable tbody tr.status-invoiced td { background: color-mix(in srgb, var(--status-invoiced) 72%, #fff); }
#planningTable tbody tr.status-done td { background: color-mix(in srgb, var(--status-done) 72%, #fff); }
#planningTable tbody tr.status-cancelled td { background: color-mix(in srgb, var(--status-cancelled) 72%, #fff); }

#planningTable .editable {
  background: rgba(255, 255, 255, .84);
  width: 100%;
  min-width: 0;
  min-height: 32px;
  padding: 4px 6px;
}

#planningTable .status-select {
  font-weight: 700;
}

#planningClientSummary {
  max-height: 42vh;
  border: 1px solid #9aa6b2;
  box-shadow: inset 0 1px 0 #fff;
}

.planning-toolbar {
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  align-items: end;
}

.planning-form {
  grid-template-columns: 130px 115px minmax(190px, 1.2fr) minmax(150px, 1fr) 90px 90px 135px 130px;
  gap: 8px;
}

.planning-form label {
  min-width: 0;
}

.planning-form input,
.planning-form select {
  width: 100%;
}

.planning-form label:nth-of-type(3) { grid-column: span 2; }
.planning-form label:nth-of-type(4) { grid-column: span 2; }
.planning-form label:nth-of-type(9) { grid-column: span 2; }
.planning-form label:nth-of-type(10) { grid-column: span 2; }
.planning-form label:nth-of-type(14) { grid-column: span 3; }
.planning-form button {
  grid-column: span 2;
  min-width: 150px;
}

table {
  border-collapse: collapse;
  width: 100%;
  min-width: 760px;
}

th, td {
  border: 1px solid var(--line);
  padding: 4px 6px;
  vertical-align: middle;
}

th {
  background: #eef2f6;
  text-align: left;
  position: sticky;
  top: 0;
  z-index: 3;
  box-shadow: 0 1px 0 var(--line);
}

#planningTable th,
#planningClientSummary th,
#archiveTable th {
  position: sticky;
  top: 0;
  z-index: 5;
}

#planningClientSummary tbody tr:last-child td {
  background: #eef2f6;
  font-weight: 800;
}

.planning-matrix {
  min-width: 2100px;
  table-layout: fixed;
  font-size: 10px;
  border-collapse: separate;
  border-spacing: 0;
}

.planning-matrix th,
.planning-matrix td {
  padding: 3px 4px;
  text-align: center;
  white-space: nowrap;
  border-color: #aab3bd;
}

.planning-matrix .left-head {
  background: #e32616;
  color: #fff;
  text-align: left;
  width: 150px;
  left: 0;
  z-index: 9;
}

.planning-matrix .company-head {
  background: #ffff00;
  color: #000;
  width: 92px;
  left: 150px;
  z-index: 9;
}

.planning-matrix .month-head,
.planning-matrix .week-head {
  background: #e32616;
  color: #fff;
  font-weight: 800;
}

.planning-matrix .week-head {
  width: 34px;
}

.planning-matrix .client-cell {
  text-align: left;
  font-weight: 700;
  background: #fff;
  position: sticky;
  left: 0;
  z-index: 4;
  width: 150px;
}

.planning-matrix .total-company {
  background: #fff;
  font-weight: 700;
  position: sticky;
  z-index: 4;
}

.planning-matrix .total-company { left: 150px; }

.planning-matrix .week-filled {
  background: #fff;
  color: #000;
  font-weight: 400;
}

.planning-matrix .planning-status-line {
  display: grid;
  grid-template-columns: 13px minmax(0, 1fr);
  gap: 2px;
  align-items: center;
  margin: 1px 0;
  padding: 2px;
  border-radius: 2px;
  text-align: right;
  line-height: 1.05;
  min-width: 34px;
}

.planning-matrix .planning-status-line.status-planned { background: var(--status-planned); }
.planning-matrix .planning-status-line.status-confirmed { background: var(--status-confirmed); }
.planning-matrix .planning-status-line.status-invoiced { background: var(--status-invoiced); }
.planning-matrix .planning-status-line.status-done { background: var(--status-done); }
.planning-matrix .planning-status-line.status-cancelled { background: var(--status-cancelled); }

.planning-matrix .planning-status-line span {
  font-size: 8px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
}

.planning-matrix .planning-status-line strong {
  display: block;
  font-weight: 800;
  font-size: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.planning-matrix .week-empty {
  background: #fff;
}

.planning-matrix .planning-click-cell {
  cursor: pointer;
}

.planning-matrix .planning-click-cell:hover {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.planning-matrix .planning-total-row td {
  background: #ffff00;
  font-weight: 800;
}

.planning-matrix .planning-total-row .client-cell,
.planning-matrix .planning-total-row .total-company {
  background: #ffff00;
}

td.num, th.num, input.num, .editable.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.dash-row-header td { background: var(--head); font-weight: 800; }
.dash-row-section td { background: var(--dash-section); font-weight: 800; }
.dash-row-tax td { background: var(--dash-tax); }
.dash-row-cost td { background: var(--dash-cost); }
.dash-row-yellow td { background: var(--dash-yellow); }
.dash-row-green td { background: var(--dash-green); }
.dash-row-total td, .dash-row-final td { font-weight: 800; }
.dash-row-spacer td { background: var(--black); height: 22px; padding: 0; border-color: var(--black); }

.dashboard-comparison {
  min-width: 860px;
  font-size: 14px;
}

.dashboard-comparison .row-label {
  min-width: 185px;
  position: sticky;
  left: 0;
  z-index: 2;
  background: inherit;
}

.dashboard-comparison .dash-row-header .row-label {
  z-index: 4;
}

.dashboard-comparison .year-head,
.dashboard-comparison .metric-head {
  text-align: center;
}

.dashboard-comparison td {
  height: 28px;
}

.dashboard-comparison .dash-row-header .metric-0,
.dashboard-comparison .dash-row-header .metric-1,
.dashboard-comparison .dash-row-header .metric-2,
.dashboard-comparison .dash-row-header .metric-3 {
  background: var(--head);
}

.dashboard-comparison .dash-row-section .metric-0 { background: var(--dash-section); }
.dashboard-comparison .dash-row-tax .metric-0 { background: var(--dash-tax); }
.dashboard-comparison .dash-row-cost .metric-0 { background: var(--dash-cost); }
.dashboard-comparison .dash-row-yellow .metric-0 { background: var(--dash-yellow); }
.dashboard-comparison .dash-row-green .metric-0 { background: var(--dash-green); }
}

.stats {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  line-height: 1.5;
}

.edit-hint {
  margin: 8px 0;
  color: var(--muted);
  font-size: 13px;
}

.editable {
  width: 100%;
  min-width: 90px;
  border: 1px solid #c9d3df;
  border-radius: 4px;
  background: #fff;
  padding: 4px 6px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.action-col {
  width: 72px;
  text-align: center;
}

.col-id {
  width: 58px;
  min-width: 58px;
  max-width: 64px;
}

.col-id .editable {
  min-width: 48px;
  width: 54px;
}

#archiveTable table {
  table-layout: auto;
}

#archiveTable .col-data-cassa,
#archiveTable .col-data-competenza,
#archiveTable .col-data-fattura,
#archiveTable .col-data-incasso,
#archiveTable .col-data-acquisto,
#archiveTable .col-data-vendita {
  min-width: 190px;
}

#archiveTable .col-categoria,
#archiveTable .col-cliente,
#archiveTable .col-nome-titolo,
#archiveTable .col-tipologia-titolo,
#archiveTable .col-tipo-documento,
#archiveTable .col-tipologia-cliente,
#archiveTable .col-macro-categoria,
#archiveTable .col-localita {
  min-width: 190px;
}

#archiveTable .col-descrizione,
#archiveTable .col-note {
  min-width: 230px;
}

#archiveTable .col-importo,
#archiveTable .col-lordo-orario,
#archiveTable .col-imponibile,
#archiveTable .col-rivalsa,
#archiveTable .col-valore,
#archiveTable .col-valore-vendita,
#archiveTable .col-spese-acquisto,
#archiveTable .col-spese-vendita,
#archiveTable .col-totale-fattura,
#archiveTable .col-inps-stimata,
#archiveTable .col-irpef-stimata,
#archiveTable .col-prezzo-unitario,
#archiveTable .col-prezzo-vendita,
#archiveTable .col-valore-attuale,
#archiveTable .col-rendimento {
  min-width: 130px;
}

#archiveTable .col-km,
#archiveTable .col-persone,
#archiveTable .col-tempo-percorrenza {
  min-width: 120px;
}

#archiveTable .col-anno-competenza,
#archiveTable .col-anno-cassa,
#archiveTable .col-anno-incasso {
  min-width: 125px;
}

#archiveTable .col-deducibile,
#archiveTable .col-incassata,
#archiveTable .col-venduto,
#archiveTable .col-stato,
#archiveTable .col-ambito {
  min-width: 115px;
}

.row-delete-btn,
.row-edit-btn,
.planning-row-delete-btn {
  padding: 4px 7px;
  min-height: 28px;
  font-size: 12px;
  width: 100%;
  margin: 1px 0;
}

.editable:hover {
  border-color: #88a9ba;
  background: #f8fcff;
}

.editable:focus {
  outline: 2px solid #5c8fa5;
  border-color: #3f7288;
  background: #fff;
}

.dirty #saveBtn,
.dirty #saveArchiveBtn,
.dirty #savePlanningBtn {
  background: #b45309;
  color: #fff;
  border-color: #92400e;
}

.select-col { width: 38px; text-align: center; }

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: min(420px, calc(100vw - 32px));
  background: #111827;
  color: #fff;
  padding: 12px 14px;
  border-radius: 8px;
  opacity: 0;
  transform: translateY(8px);
  transition: 180ms ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(17, 24, 39, .42);
  padding: 18px;
}

.modal[hidden] {
  display: none;
}

.modal-panel {
  width: min(620px, 100%);
  min-width: 0;
  max-height: min(720px, calc(100vh - 36px));
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 60px rgba(17, 24, 39, .25);
  padding: 14px;
  display: grid;
  gap: 10px;
  overflow: hidden;
}

.unpaid-panel {
  width: min(1100px, 100%);
}

.unpaid-date {
  min-width: 145px;
}

.modal-panel > * {
  min-width: 0;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.modal-list {
  overflow: auto;
  border: 1px solid var(--line);
  max-height: 520px;
}

.modal-item {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  text-align: left;
  padding: 10px 12px;
}

.modal-item strong {
  display: block;
}

.modal-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.modal-subtitle {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.import-map-panel {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  grid-template-rows: auto auto minmax(120px, 1fr) minmax(120px, .7fr) auto;
}

.import-map-help {
  color: var(--muted);
  font-size: 13px;
}

.import-map-grid {
  max-height: none;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  width: 100%;
}

.import-map-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 4px;
  width: 100%;
  border-bottom: 1px solid var(--line);
  padding: 9px;
  color: var(--muted);
}

.import-map-row strong {
  display: block;
  color: var(--text);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.import-map-row span {
  font-size: 11px;
  line-height: 1.1;
}

.import-map-row select {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  font-size: 13px;
  padding: 6px 8px;
  min-height: 34px;
}

.import-preview-section {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
}

.import-preview {
  max-height: 220px;
  min-height: 0;
  max-width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  display: grid;
  gap: 8px;
  padding-right: 4px;
}

.import-map-panel .modal-actions {
  position: sticky;
  bottom: 0;
  z-index: 3;
  background: #fff;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.import-preview-row {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  background: #fff;
  display: grid;
  gap: 4px;
  font-size: 12px;
}

.import-preview-row > strong {
  font-size: 13px;
}

.import-preview-row div {
  display: grid;
  grid-template-columns: minmax(0, 120px) minmax(0, 1fr);
  gap: 8px;
  min-width: 0;
}

.import-preview-row span {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.import-preview-row b {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.settings-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

.settings-subnav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 14px 0;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.settings-subtab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.settings-subtab.danger {
  border-color: #d66;
}

.settings-section {
  display: none;
}

.settings-section.active {
  display: block;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 14px;
  margin-top: 14px;
}

.settings-color-title {
  grid-column: 1 / -1;
  margin: 8px 0 -4px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  font-size: 15px;
}

.settings-color-title:first-child {
  border-top: 0;
  padding-top: 0;
}

.settings-grid input[type="color"] {
  width: 100%;
  height: 44px;
  padding: 3px;
}

.settings-actions {
  margin-top: 16px;
}

.backup-toolbar {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
  margin: 16px 0;
}

.backup-toolbar label {
  min-width: min(520px, 100%);
  flex: 1;
}

.backup-counts {
  margin-top: 10px;
  min-width: 0;
  max-width: 520px;
}

.backup-counts td:first-child {
  font-weight: 600;
}

.danger-box {
  margin-top: 16px;
  border: 1px solid #dc2626;
  background: #fff1f1;
  border-radius: 8px;
  padding: 14px;
  display: grid;
  gap: 12px;
}

.delete-archive-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.delete-archive-list {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 8px;
}

.delete-archive-item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  min-width: 0;
}

.delete-archive-item span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.delete-archive-item b {
  font-variant-numeric: tabular-nums;
}

.danger-button {
  background: #b91c1c;
  border-color: #991b1b;
  color: #fff;
  justify-self: start;
}

.danger-button:hover {
  background: #991b1b;
}

@media (max-width: 760px) {
  .app-header { align-items: stretch; flex-direction: column; }
  main { padding: 10px; }
  .cards { grid-template-columns: 1fr; }
  .archive-head { align-items: stretch; flex-direction: column; }
  .quick-form { grid-template-columns: 1fr; }
  .invoice-form { grid-template-columns: 1fr; }
  .settings-grid { grid-template-columns: 1fr; }
  .cost-dashboard-grid { grid-template-columns: 1fr; }
  .cost-dashboard-grid .wide { grid-column: auto; }
  .modal-actions { flex-direction: column; }
  .header-actions, .archive-actions, .toolbar, .filters, .backup-toolbar, .settings-subnav { align-items: stretch; }
  button, select, input { width: 100%; }
  .delete-archive-list { grid-template-columns: 1fr; }
  #archiveTable { max-height: calc(100vh - 440px); }
  #reportTable { max-height: calc(100vh - 260px); }
  #planningTable { max-height: calc(100vh - 420px); }
  table { min-width: 920px; }
}

@media print {
  @page {
    size: A4 landscape;
    margin: 10mm;
  }

  * {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  body {
    background: #fff;
    color: #000;
    font-size: 10px;
  }

  .app-header, .tabs, .toolbar, .archive-head, .filters, .stats, .toast, .quick-form, .edit-hint { display: none !important; }
  main { padding: 0; }
  .view { display: none !important; }
  .view.active { display: block !important; }
  .cards {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 4px;
    margin-bottom: 8px;
  }
  .card {
    border: 1px solid #777;
    box-shadow: none;
    padding: 6px;
    min-height: auto;
  }
  .card .label { font-size: 8px; color: #333; }
  .card .value { font-size: 13px; color: #000; }
  .dashboard-sheet, .table-wrap {
    border: 0;
    overflow: visible;
    max-height: none !important;
    margin-bottom: 8px;
  }
  .select-col { display: none; }
  input.editable {
    border: 0;
    padding: 0;
    color: #000;
    background: transparent;
    min-width: 0;
    width: 100%;
  }
  table {
    min-width: 0;
    width: 100%;
    font-size: 9px;
    page-break-inside: auto;
  }
  .planning-matrix {
    min-width: 0;
    table-layout: fixed;
    font-size: 5.5px;
  }
  .planning-matrix th,
  .planning-matrix td {
    padding: 1px;
    white-space: normal;
  }
  .planning-matrix .left-head,
  .planning-matrix .client-cell {
    width: 42px;
  }
  .planning-matrix .hours-head,
  .planning-matrix .company-head,
  .planning-matrix .week-head {
    width: auto;
  }
  .planning-matrix .week-filled span {
    font-size: 4.8px;
  }
  th, td {
    padding: 3px 4px;
    border: 1px solid #777;
  }
  th {
    position: static;
    background: #d9e2ea !important;
    color: #000;
    box-shadow: none;
  }
  tr {
    page-break-inside: avoid;
    page-break-after: auto;
  }
}
