/* ========================================
   DESIGN TOKENS
   ======================================== */
:root {
  --bg: #f5f6f8;
  --card-bg: #ffffff;
  --text: #1a1a2e;
  --muted: #5a5a6e;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --error: #dc2626;
  --error-bg: #fef2f2;
  --error-text: #7f1d1d;
  --error-border: #fecaca;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --success-text: #14532d;
  --warning: #d97706;
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.12);
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

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

body {
  margin: 0;
  padding: 0;
  font-family:
    "Inter",
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ========================================
   HEADER
   ======================================== */
.app-header {
  background: var(--card-bg);
  padding: 14px 24px;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-header .logo {
  height: 56px;
  width: auto;
}

.app-header .logo-text {
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--text);
  letter-spacing: -0.02em;
}

/* ========================================
   LAYOUT
   ======================================== */
.container {
  width: 92%;
  max-width: 960px;
  margin: 28px auto 48px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.card {
  background: var(--card-bg);
  padding: 28px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid rgba(0, 0, 0, 0.04);
  transition:
    transform var(--transition),
    box-shadow var(--transition);
}

.card:hover {
  box-shadow: var(--shadow-lg);
}

.card h2 {
  margin: 0 0 20px;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.result-card h2 {
  margin: 0 0 16px;
}

.result-card h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  color: var(--muted);
}

.sub-section {
  position: relative;
  margin-top: 16px;
}

/* ========================================
   FORM ELEMENTS
   ======================================== */
.form-group {
  margin-bottom: 20px;
}

label {
  font-weight: 600;
  margin-bottom: 6px;
  display: block;
  font-size: 0.9rem;
  color: var(--text);
}

input[type="text"],
input[type="password"],
input[type="file"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid #d1d5db;
  font-size: 0.95rem;
  font-family: inherit;
  background: #fff;
  color: var(--text);
  transition:
    border-color var(--transition),
    box-shadow var(--transition);
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
  outline: none;
}

textarea {
  resize: vertical;
  min-height: 120px;
  line-height: 1.6;
}

.field-hint {
  display: block;
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}

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

/* ========================================
   SFTP TOGGLE
   ======================================== */
.sftp-toggle {
  margin-top: 12px;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-weight: 500;
  margin-bottom: 4px;
}

.toggle-label input[type="checkbox"] {
  display: none;
}

.toggle-slider {
  position: relative;
  width: 44px;
  height: 24px;
  background: #d1d5db;
  border-radius: 24px;
  transition: background var(--transition);
  flex-shrink: 0;
}

.toggle-slider::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background: #fff;
  border-radius: 50%;
  transition: transform var(--transition);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.toggle-label input:checked + .toggle-slider {
  background: var(--primary);
}

.toggle-label input:checked + .toggle-slider::before {
  transform: translateX(20px);
}

.toggle-text {
  font-size: 0.95rem;
  color: var(--text);
}

.sftp-toggle .field-hint {
  margin-left: 56px;
  margin-top: 2px;
}

/* ========================================
   BUTTONS
   ======================================== */
.primary-btn,
.secondary-btn,
.report-btn,
.copy-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 600;
  font-size: 0.95rem;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition:
    background-color var(--transition),
    transform 0.1s,
    box-shadow var(--transition);
}

.primary-btn {
  background: var(--primary);
  color: #fff;
}

.primary-btn:hover {
  background: var(--primary-hover);
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.25);
}

.primary-btn:active {
  transform: scale(0.98);
}

.primary-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.secondary-btn {
  background: #f3f4f6;
  color: #374151;
  border: 1px solid #e5e7eb;
}

.secondary-btn:hover {
  background: #e5e7eb;
}

.secondary-btn:active {
  transform: scale(0.98);
}

.report-btn {
  background: var(--primary-soft);
  color: var(--primary);
  border: 1px solid #bfdbfe;
  font-size: 0.85rem;
  padding: 8px 14px;
}

.report-btn:hover {
  background: #bfdbfe;
}

.copy-btn {
  background: rgba(255, 255, 255, 0.5);
  color: var(--error-text);
  border: 1px solid var(--error-border);
  font-size: 0.85rem;
  padding: 8px 14px;
}

.copy-btn:hover {
  background: rgba(255, 255, 255, 0.8);
}

.copy-btn.success {
  background: var(--success-bg);
  border-color: #86efac;
  color: var(--success-text);
}

/* ========================================
   DROPZONE
   ======================================== */
.dropzone {
  background: #f9fafb;
  border: 2px dashed #d1d5db;
  padding: 32px 24px;
  border-radius: var(--radius);
  text-align: center;
  cursor: pointer;
  transition:
    background var(--transition),
    border-color var(--transition),
    transform var(--transition);
}

.dropzone:hover,
.dropzone:focus-visible {
  background: #eff6ff;
  border-color: var(--primary);
  outline: none;
}

.dropzone.dragover {
  background: #dbeafe;
  border-color: var(--primary);
  transform: scale(1.01);
}

.dropzone.has-file {
  background: #f0fdf4;
  border-color: var(--success);
  border-style: solid;
}

.dropzone-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.dropzone-icon {
  color: var(--muted);
  transition: color var(--transition);
}

.dropzone:hover .dropzone-icon,
.dropzone.dragover .dropzone-icon {
  color: var(--primary);
}

.dropzone.has-file .dropzone-icon {
  color: var(--success);
}

.file-name {
  font-size: 0.9rem;
  color: var(--muted);
  word-break: break-all;
}

.dropzone.has-file .file-name {
  color: var(--success-text);
  font-weight: 600;
}

/* ========================================
   LOADER
   ======================================== */
.loader {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 1rem;
  font-weight: 500;
  color: var(--muted);
  padding: 24px;
}

.spinner {
  width: 22px;
  height: 22px;
  border: 3px solid #e5e7eb;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ========================================
   ERROR BOX
   ======================================== */
.error-box {
  background-color: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.error-header {
  background: linear-gradient(135deg, #fecaca 0%, #fee2e2 100%);
  padding: 14px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--error-border);
  gap: 12px;
  flex-wrap: wrap;
}

.error-header h3 {
  color: var(--error-text);
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.error-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.error-body {
  font-family:
    "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
  font-size: 0.88rem;
  padding: 18px;
  word-break: break-word;
  line-height: 1.65;
  background: rgba(255, 255, 255, 0.5);
}

.error-body pre {
  margin: 0;
  white-space: pre-wrap;
  overflow-x: auto;
}

/* ========================================
   PREVIEW
   ======================================== */
.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  flex-wrap: wrap;
  gap: 10px;
}

/* ========================================
   PREVIEW TOOLBAR
   ======================================== */
.preview-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  gap: 12px;
  flex-wrap: wrap;
}

.search-wrapper {
  position: relative;
  flex: 1 1 260px;
  max-width: 380px;
}

.search-wrapper .search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

.search-wrapper input {
  width: 100%;
  padding: 9px 12px 9px 38px;
  border-radius: var(--radius-sm);
  border: 1px solid #d1d5db;
  font-size: 0.9rem;
  font-family: inherit;
  background: #fff;
  color: var(--text);
  transition:
    border-color var(--transition),
    box-shadow var(--transition);
}

.search-wrapper input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
  outline: none;
}

.result-meta {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}

.download-wrapper {
  margin-top: 24px;
  padding-top: 8px;
}

/* ========================================
   HANDSONTABLE STYLES
   ======================================== */

/* HOT cells need content-box so padding+border don't shrink the inline-calculated
   cell dimensions; wrappers (wtHolder, wtHider, etc.) must keep their original model. */
.handsontable td,
.handsontable th {
  box-sizing: content-box;
}

/* Ensure clone wrappers fill empty space near scrollbar with header color */
.handsontable .ht_clone_top,
.handsontable .ht_clone_left,
.handsontable .ht_clone_top_left_corner {
  background: #f8f9fa;
}

/* Container */
.table-container {
  margin-top: 12px;
  height: 450px;
  overflow: hidden;
  border-radius: var(--radius-sm);
  background: #f8f9fa;
  /*  border: 1px solid #e5e7eb;é*/
}

.handsontable {
  font-family: "Inter", Arial, sans-serif;
  font-size: 13px;
  color: var(--text);
  z-index: 1 !important;
}

/* Celle dati */
.handsontable td {
  padding: 8px 10px !important;
  border-right: 1px solid #e5e7eb !important;
  border-bottom: 1px solid #e5e7eb !important;
  background: #fff !important;
}

.handsontable tbody tr:nth-child(even) td {
  background: #fafbfc !important;
}

.handsontable tbody tr:hover td {
  background: #f0f7ff !important;
}

/* Header colonne */
.handsontable thead th {
  background: #f8f9fa !important;
  color: var(--text) !important;
  font-weight: 600 !important;
  border-bottom: 1px solid #e5e7eb !important;
  border-right: 1px solid #e5e7eb !important;
  padding: 10px 8px !important;
  text-align: left !important;
}

/* Header righe — nessun bordo destro spesso */
/* Header righe */
.handsontable .ht_clone_left th {
  background: #f8f9fa !important;
  font-weight: 600 !important;
  color: var(--muted) !important;
  border-right: 1px solid #e5e7eb !important;
  border-bottom: 1px solid #e5e7eb !important;
}

/* Corner top-left */
.handsontable .ht_clone_top_left_corner th {
  background: #f8f9fa !important;
  border-right: 1px solid #e5e7eb !important;
  border-bottom: 1px solid #e5e7eb !important;
}

/* Ricerca */
.handsontable td.htSearchResult,
.handsontable tbody tr:nth-child(even) td.htSearchResult {
  background-color: #dbeafe !important;
  color: var(--primary) !important;
  font-weight: 700 !important;
}

.handsontable td.htReadOnly {
  cursor: text;
  color: #333;
}

.handsontable td.current,
.handsontable td.area {
  background-color: rgba(37, 99, 235, 0.12) !important;
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.handsontable .wtHolder {
  -webkit-user-select: text;
  -ms-user-select: text;
  user-select: text;
}

/* Scrollbar: HOT handles its own overflow internally.
   Removing !important overrides lets clones stay aligned to scrollbar track. */
.handsontable .ht_clone_top,
.handsontable .ht_clone_top_left_corner,
.handsontable .ht_clone_left,
.handsontable .ht_clone_bottom,
.handsontable .ht_clone_bottom_left_corner,
.handsontable .ht_clone_top .wtHolder,
.handsontable .ht_clone_top_left_corner .wtHolder,
.handsontable .ht_clone_left .wtHolder,
.handsontable .ht_clone_bottom .wtHolder,
.handsontable .ht_clone_bottom_left_corner .wtHolder {
  overflow: hidden;
}

.handsontable .ht_master .wtHolder {
  overflow: auto;
}

.handsontable .ht_master .wtHolder::-webkit-scrollbar {
  height: 8px;
  width: 8px;
}

.handsontable .ht_master .wtHolder::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 4px;
  border: 1px solid #f1f5f9;
}

.handsontable .ht_master .wtHolder::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

.handsontable .ht_master .wtHolder::-webkit-scrollbar-track {
  background: #f8fafc;
  border-radius: 4px;
}

.handsontable .ht_master .wtHolder::-webkit-scrollbar-corner {
  background: #f8fafc;
}

/* ========================================
   TOAST NOTIFICATIONS
   ======================================== */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  background: var(--card-bg);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius-sm);
  padding: 14px 18px;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
  min-width: 260px;
  max-width: 420px;
  animation: toastIn 0.35s cubic-bezier(0.16, 1, 0.3, 1);
  transition:
    opacity 0.3s,
    transform 0.3s;
}

.toast.toast-exit {
  opacity: 0;
  transform: translateX(120%);
}

.toast.toast-error {
  border-left-color: var(--error);
}

.toast.toast-success {
  border-left-color: var(--success);
}

.toast.toast-warning {
  border-left-color: var(--warning);
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(120%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* ========================================
   MODAL
   ======================================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: 150;
  opacity: 1;
  transition: opacity 0.25s ease;
}

.modal-backdrop.hidden {
  display: none;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(1);
  z-index: 160;
  width: 92%;
  max-width: 560px;
  max-height: 90vh;
  overflow: hidden;
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  opacity: 1;
  transition:
    opacity 0.25s ease,
    transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal.hidden {
  display: none;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px 0;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  padding: 6px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    background var(--transition),
    color var(--transition);
}

.modal-close:hover {
  background: #f3f4f6;
  color: var(--text);
}

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

.modal-content {
  padding: 0 24px 24px;
  overflow-y: auto;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
  padding-top: 16px;
  border-top: 1px solid #f3f4f6;
}

.attachments-list {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.attachment-chip {
  background: #f3f4f6;
  border: 1px solid #e5e7eb;
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 0.8rem;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* ========================================
   INFO BOX (report modal hint)
   ======================================== */
.info-box {
  background: #f8f9fa;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 12px 14px;
  border-left: 4px solid var(--primary);
}

.info-box p {
  margin: 0;
  font-size: 0.9rem;
  color: #374151;
  line-height: 1.5;
  font-weight: 500;
}

.info-box .info-box-detail {
  margin-top: 6px;
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.45;
  font-weight: 400;
}

.info-box code {
  background: #e5e7eb;
  padding: 1px 4px;
  border-radius: 4px;
  font-size: 0.8rem;
}

/* ========================================
   FOOTER
   ======================================== */
.app-footer {
  text-align: center;
  padding: 24px 16px;
  margin-top: 8px;
}

.contact-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.9rem;
  text-decoration: none;
  transition: color 0.2s ease;
  vertical-align: middle;
}

.contact-link svg {
  display: block;
}

.contact-link:hover {
  color: var(--primary);
}

/* ========================================
   LOGOUT
   ======================================== */
.logout-container {
  text-align: center;
  margin-top: 32px;
  padding-bottom: 32px;
}

/* ========================================
   UTILITY
   ======================================== */
.hidden {
  display: none !important;
}

.full {
  width: 100%;
}

/* ========================================
   RESPONSIVE
   ======================================== */
@media (max-width: 640px) {
  .app-header .logo {
    height: 44px;
  }

  .app-header .logo-text {
    font-size: 1.05rem;
  }

  .container {
    width: 96%;
    margin: 16px auto 32px;
  }

  .card {
    padding: 20px;
  }

  .card h2 {
    font-size: 1.15rem;
  }

  .error-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .preview-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .search-wrapper {
    max-width: none;
    width: 100%;
  }

  .preview-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .modal-actions {
    flex-direction: column;
  }

  .modal-actions .primary-btn,
  .modal-actions .secondary-btn {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ========================================
   FORM ACTIONS WITH SFTP
   ======================================== */
.form-actions-sftp {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-top: 24px;
  flex-wrap: wrap;
}

.sftp-option {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sftp-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sftp-title {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
}

.sftp-hint {
  font-size: 0.75rem;
  color: var(--muted);
}

@media (max-width: 640px) {
  .form-actions-sftp {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .sftp-option {
    justify-content: center;
  }
}
