/* 1. Design token */

:root {
  --background: #f3f5f8;
  --surface: #ffffff;
  --text: #172033;
  --text-secondary: #667085;
  --heading: #101828;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --success-action: #15803d;
  --success-text: #166534;
  --success-surface: #f0fdf4;
  --success-border: #86efac;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-text: #991b1b;
  --danger-surface: #fef2f2;
  --danger-border: #fca5a5;
  --border: #cbd5e1;
  --panel-border: #d9e0e8;
  --surface-muted: #f8fafc;
  --overlay: rgba(15, 23, 42, 0.52);

  --font: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --text-caption: 11px;
  --text-small: 12px;
  --text-body: 14px;
  --text-subheading: 18px;
  --text-section: 21px;
  --text-page: 25px;
  --weight-regular: 400;
  --weight-semibold: 600;
  --weight-bold: 700;

  --space-xs: 6px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 18px;
  --space-xl: 24px;
  --radius-small: 6px;
  --radius: 8px;
  --radius-panel: 12px;
  --page-width: 1400px;
  --button-height: 42px;
  --button-height-small: 36px;
  --input-height: 44px;
  --cell-padding: var(--space-md) 14px;
  --panel-shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 12px 30px rgba(15, 23, 42, 0.06);
  --overlay-shadow: 0 18px 45px rgba(15, 23, 42, 0.18);
  --transition: 150ms ease;
}

/* 2. Reset e tipografia */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html [hidden] {
  display: none;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background: var(--background);
  font: var(--weight-regular) var(--text-body)/1.5 var(--font);
}

button,
input,
select,
textarea {
  font: inherit;
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

h1,
h2,
h3 {
  margin: 0;
  color: var(--heading);
  font-weight: var(--weight-bold);
  line-height: 1.25;
  overflow-wrap: anywhere;
}

h1 {
  font-size: var(--text-page);
}

h2 {
  font-size: var(--text-section);
}

h3 {
  font-size: var(--text-subheading);
}

.caption,
th {
  color: var(--text-secondary);
  font-size: var(--text-caption);
  font-weight: var(--weight-bold);
  letter-spacing: 0.055em;
  text-transform: uppercase;
}

.text-center {
  text-align: center;
}

.empty-state {
  color: var(--text-secondary);
  font-style: italic;
  text-align: center;
}

/* 3. Layout generale */

.page-container {
  max-width: var(--page-width);
  margin-inline: auto;
  padding: var(--space-xl);
}

.split-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-md);
}

.actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: var(--space-xs);
}

/* 4. Header e toolbar */

.toolbar {
  margin-bottom: var(--space-lg);
}

.toolbar-filters {
  display: flex;
  align-items: flex-end;
  gap: var(--space-md);
}

.toolbar-filters > :where(.form-field) {
  flex: 1;
}

.back-link {
  display: inline-block;
  margin-bottom: var(--space-xs);
  color: var(--primary);
  font-weight: var(--weight-bold);
  text-decoration: none;
}

.back-link:hover {
  color: var(--primary-hover);
  text-decoration: underline;
}

/* 5. Pulsanti */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--button-height);
  padding: var(--space-sm) 16px;
  border: 1px solid var(--button-border, transparent);
  border-radius: var(--radius);
  color: var(--button-text, var(--surface));
  background: var(--button-background);
  font-weight: var(--weight-bold);
  line-height: 1.25;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color var(--transition);
}

.button:hover:not(:disabled) {
  background: var(--button-hover);
}

.button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.button-primary {
  --button-background: var(--primary);
  --button-hover: var(--primary-hover);
}

.button-secondary {
  --button-background: var(--surface);
  --button-hover: var(--background);
  --button-border: var(--border);
  --button-text: var(--text);
}

.button-success {
  --button-background: var(--success-action);
  --button-hover: var(--success-text);
}

.button-danger {
  --button-background: var(--danger);
  --button-hover: var(--danger-hover);
}

.button-small {
  min-height: var(--button-height-small);
  padding-inline: var(--space-md);
  border-radius: var(--radius-small);
  font-size: var(--text-small);
}

/* 6. Form e input */

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-lg);
}

.form-grid-four {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.form-grid + .actions {
  margin-top: var(--space-lg);
}

.form-field {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: var(--space-xs);
}

.form-field-wide {
  grid-column: 1 / -1;
}

label {
  font-size: var(--text-small);
  font-weight: var(--weight-bold);
}

.form-control {
  width: 100%;
  min-height: var(--input-height);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--state-border, var(--border));
  border-radius: var(--radius);
  color: var(--state-text, var(--text));
  background: var(--state-background, var(--surface));
  transition: border-color var(--transition);
}

.form-control:focus {
  border-color: var(--primary);
}

.form-control:user-invalid {
  border-color: var(--danger);
}

.form-control:disabled {
  color: var(--text-secondary);
  background: var(--background);
  cursor: not-allowed;
}

textarea.form-control {
  min-height: 110px;
  resize: vertical;
}

.checkbox-field {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: var(--text-body);
}

.checkbox-field input {
  width: var(--space-lg);
  height: var(--space-lg);
  margin: 0;
  accent-color: var(--primary);
}

.inline-form {
  display: flex;
  align-items: flex-end;
  gap: var(--space-lg);
}

.inline-form .form-control {
  flex: 1;
  min-width: 0;
}

.inline-form + .table-container {
  margin-top: var(--space-md);
}

.inline-editor-row {
  background: var(--surface-muted);
}

.validation-message {
  flex-basis: 100%;
  margin: var(--space-xs) 0 0;
  color: var(--danger-text);
  font-size: var(--text-small);
  font-weight: var(--weight-semibold);
}

/* 7. Pannelli */

.panel {
  min-width: 0;
  padding: var(--space-xl);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-panel);
  background: var(--surface);
  box-shadow: var(--panel-shadow);
}

.panel-flush {
  padding: 0;
  overflow: hidden;
}

.page-container > .panel + :is(.panel, .secondary-panels) {
  margin-top: var(--space-lg);
}

.panel-header {
  margin-bottom: var(--space-lg);
}

.panel-section + .panel-section {
  margin-top: var(--space-xl);
  padding-top: var(--space-xl);
  border-top: 1px solid var(--panel-border);
}

.panel-row {
  padding: var(--cell-padding);
}

.panel-row-heading {
  background: var(--surface-muted);
}

.panel-row + .panel-row,
.panel-row + .panel-rows:not(:empty) {
  border-top: 1px solid var(--panel-border);
}

/* 8. Tabelle */

.table-container {
  overflow-x: auto;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
}

th,
td {
  padding: var(--cell-padding);
  border-bottom: 1px solid var(--panel-border);
  text-align: left;
  vertical-align: middle;
}

th {
  background: var(--surface-muted);
  white-space: nowrap;
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr:not(.row-active):hover {
  background: var(--surface-muted);
}

.table-actions {
  width: 1%;
  text-align: right;
}

.table-actions .actions {
  min-width: 120px;
}

/* 9. Stati e indicatori */

.row-active {
  background: var(--success-surface);
}

.status-positive {
  --state-text: var(--success-text);
  --state-background: var(--success-surface);
  --state-border: var(--success-border);
  --state-accent: var(--success);
}

.status-negative {
  --state-text: var(--danger-text);
  --state-background: var(--danger-surface);
  --state-border: var(--danger-border);
  --state-accent: var(--danger);
}

.status-badge {
  display: inline-block;
  padding: 2px var(--space-sm);
  border-radius: var(--radius-small);
  color: var(--state-text, var(--text-secondary));
  background: var(--state-background, var(--background));
  font-size: var(--text-small);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
}

.status-field select {
  font-weight: var(--weight-semibold);
}

.status-neutral {
  --state-text: var(--text-secondary);
  --state-background: var(--surface-muted);
}

.debt-dot {
  display: inline-block;
  width: var(--space-md);
  height: var(--space-md);
  border-radius: 50%;
  background: var(--state-accent);
  vertical-align: middle;
}

/* 10. Modali e notifiche */

.modal {
  position: fixed;
  z-index: 1000;
  inset: 0;
  display: grid;
  place-items: center;
  padding: var(--space-lg);
  background: var(--overlay);
}

.modal-toggle:not(.is-open) {
  display: none;
}

.modal-content {
  width: min(100%, 520px);
  max-height: 100%;
  overflow: auto;
  box-shadow: var(--overlay-shadow);
}

.modal-content p {
  overflow-wrap: anywhere;
}

.modal-content-wide {
  width: min(100%, 1100px);
}

.modal-content > .actions:last-child {
  margin-top: var(--space-lg);
}

.application-notification {
  position: fixed;
  z-index: 1100;
  inset-inline: 0;
  bottom: max(var(--space-xl), env(safe-area-inset-bottom));
  width: max-content;
  max-width: min(420px, calc(100% - 2 * var(--space-xl)));
  margin-inline: auto;
  padding: var(--cell-padding);
  border: 1px solid var(--state-border, var(--success-border));
  border-radius: var(--radius);
  color: var(--state-text, var(--success-text));
  background: var(--state-background, var(--success-surface));
  box-shadow: var(--overlay-shadow);
  text-align: center;
  overflow-wrap: anywhere;
}

/* 11. Banner ambiente */

.test-environment-banner {
  position: sticky;
  z-index: 2000;
  top: 0;
  padding: var(--space-sm) var(--space-xl);
  border-bottom: 1px solid var(--danger-border);
  color: var(--danger-text);
  background: var(--danger-surface);
  font-weight: var(--weight-bold);
  text-align: center;
  letter-spacing: 0.055em;
}

/* 12. Layout specifici */

.filter-status {
  flex: 0 0 auto;
  width: 180px;
}

.client-grid {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.client-grid > .form-field {
  grid-column: span 2;
}

.client-grid > .form-field:where(:nth-child(-n + 2)) {
  grid-column: span 3;
}

.contact-methods-row {
  grid-template-columns: minmax(0, 2fr) repeat(2, minmax(0, 1fr));
}

.secondary-panels {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 0.8fr);
  gap: var(--space-lg);
}

.sites-list {
  display: grid;
  gap: var(--space-md);
}

.hierarchy-child {
  padding-inline-start: var(--space-xl);
}

.hierarchy-row > :first-child {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.printer-data {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--space-md);
}

.inline-printer-fields {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

/* 13. Responsive design */

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

  .secondary-panels {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 700px) {
  .page-container,
  .modal {
    padding: var(--space-md);
  }

  .panel:not(.panel-flush) {
    padding: var(--space-lg);
  }

  .toolbar-filters,
  .toolbar.split-row,
  .inline-form,
  .hierarchy-row {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-status {
    width: auto;
  }

  .form-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .client-grid > .form-field {
    grid-column: auto;
  }

  .toolbar > .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .toolbar > .actions .button-danger {
    grid-column: 1 / -1;
  }

  .hierarchy-child {
    padding-inline-start: var(--space-md);
  }

  .hierarchy-row .actions {
    justify-content: flex-start;
  }
}

/* Colonna Cliente - Log API */
table:has(#logsTable) th:nth-child(3),
#logsTable td:nth-child(3) {
  box-sizing: border-box;
  width: 320px;
  min-width: 320px;
  max-width: 320px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.home-panels {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
}
