/* ==========================================================================
   STILI GENERALI E DESIGN SYSTEM DELL'APPLICAZIONE
   ========================================================================== */

:root {
  --bg-app: #0f172a;
  --bg-sidebar: #1e293b;
  --bg-card: #273549;
  --bg-input: #0f172a;
  --border-color: #334155;
  --border-focus: #10b981;
  --primary: #10b981;
  --primary-hover: #059669;
  --primary-glow: rgba(16, 185, 129, 0.25);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --accent-red: #ef4444;
  --accent-red-hover: #dc2626;
  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-doc: 'Lora', Georgia, 'Times New Roman', serif;
  --doc-color: #111827;
  --doc-accent: #047857;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sheet: 0 25px 50px -12px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.05);
}

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

body {
  font-family: var(--font-ui);
  background-color: var(--bg-app);
  color: var(--text-main);
  line-height: 1.5;
  min-height: 100vh;
  overflow: hidden;
}

/* ==========================================================================
   LAYOUT PRINCIPALE
   ========================================================================== */

.app-layout {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* ==========================================================================
   SIDEBAR FORM (PANNELLO SINISTRO)
   ========================================================================== */

.sidebar {
  width: 480px;
  min-width: 420px;
  max-width: 520px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 10;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.3);
}

.sidebar-nav-tabs {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  background: rgba(15, 23, 42, 0.95);
  border-bottom: 1px solid var(--border-color);
  backdrop-filter: blur(8px);
}

.nav-hub-link {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
  transition: all 0.2s;
  flex-shrink: 0;
}

.nav-hub-link:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
}

.doc-tabs-group {
  display: flex;
  background: rgba(0, 0, 0, 0.35);
  padding: 3px;
  border-radius: 8px;
  flex-grow: 1;
  gap: 3px;
}

.doc-tab-btn {
  flex: 1;
  text-align: center;
  font-size: 0.76rem;
  font-weight: 600;
  text-decoration: none;
  padding: 0.35rem 0.4rem;
  border-radius: 6px;
  color: var(--text-muted);
  transition: all 0.2s;
  white-space: nowrap;
}

.doc-tab-btn:hover {
  color: #fff;
}

.doc-tab-btn.active {
  background: var(--primary);
  color: #0f172a;
  box-shadow: 0 2px 8px var(--primary-glow);
}

.sidebar-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(30, 41, 59, 0.95);
  backdrop-filter: blur(8px);
}

.brand {
  display: flex;
  align-items: center;
}

.brand h1 {
  font-size: 1.15rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 0.75rem;
  color: var(--primary);
  font-weight: 500;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
}

/* CONTAINER SCROLLABILE DEL FORM */
.form-container {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem 3rem 1.5rem;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}

.form-container::-webkit-scrollbar {
  width: 6px;
}
.form-container::-webkit-scrollbar-track {
  background: transparent;
}
.form-container::-webkit-scrollbar-thumb {
  background-color: var(--border-color);
  border-radius: 4px;
}

/* SEZIONI E FIELDSET */
.form-section {
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  border-radius: var(--radius-md);
  padding: 1.1rem;
  margin-bottom: 1.25rem;
  transition: border-color 0.2s;
}

.section-custom-header {
  border-color: rgba(16, 185, 129, 0.35);
  background: linear-gradient(180deg, rgba(39, 53, 73, 0.95), rgba(30, 41, 59, 0.95));
}

.form-section:focus-within {
  border-color: rgba(16, 185, 129, 0.5);
}

.section-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: #e2e8f0;
  padding: 0 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.step-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  background: var(--primary);
  color: #042f2e;
  font-size: 0.75rem;
  font-weight: 700;
  border-radius: 50%;
}

/* UTILITY FLEX & SPACING */
.d-flex {
  display: flex;
}
.justify-between {
  justify-content: space-between;
}
.items-center {
  align-items: center;
}
.mb-2 {
  margin-bottom: 0.5rem;
}
.mt-2 {
  margin-top: 0.75rem;
}
.text-xs {
  font-size: 0.72rem;
}
.text-right {
  text-align: right;
}

/* CONTROLLI LOGO */
.logo-controls-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.25rem;
}

.file-input-hidden {
  display: none;
}

.file-upload-btn {
  cursor: pointer;
}

/* GRIGLIE E CAMPI */
.form-grid {
  display: grid;
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.grid-2 {
  grid-template-columns: 1fr 1fr;
}

.grid-3 {
  grid-template-columns: 2fr 1fr;
}

.span-2 {
  grid-column: span 1;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 0.75rem;
}

.form-grid .form-group {
  margin-top: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.form-grid .form-group label {
  margin-bottom: 0.35rem;
}

.form-grid .form-group input,
.form-grid .form-group select {
  margin-top: auto;
}

label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
}

input[type="text"],
textarea,
select {
  width: 100%;
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  font-family: var(--font-ui);
  font-size: 0.825rem;
  padding: 0.55rem 0.75rem;
  outline: none;
  transition: all 0.2s ease;
}

input[type="text"]:focus,
textarea:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-glow);
}

input[type="text"],
#input-art1-oggetto {
  text-transform: uppercase;
}

#input-art3-clausole {
  text-transform: none;
}

textarea {
  resize: vertical;
  min-height: 60px;
  line-height: 1.4;
}

#input-art1-oggetto {
  min-height: 220px;
}

#input-art3-clausole {
  min-height: 150px;
}

/* SELETTORI GENERE TOGGLE */
.gender-toggle-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-top: 0.25rem;
}

.gender-radio {
  cursor: pointer;
  position: relative;
}

.gender-radio input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.radio-custom {
  display: block;
  padding: 0.5rem 0.65rem;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 0.72rem;
  font-weight: 500;
  text-align: center;
  color: var(--text-muted);
  transition: all 0.2s ease;
}

.gender-radio input:checked + .radio-custom {
  background: rgba(16, 185, 129, 0.15);
  border-color: var(--primary);
  color: #34d399;
  font-weight: 600;
  box-shadow: 0 0 0 1px var(--primary);
}

/* PULSANTI UI */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  font-family: var(--font-ui);
  font-size: 0.825rem;
  font-weight: 600;
  padding: 0.55rem 1rem;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.75rem;
}

.btn-primary {
  background: linear-gradient(135deg, #10b981, #059669);
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.btn-primary:hover {
  background: linear-gradient(135deg, #059669, #047857);
  box-shadow: 0 6px 16px rgba(16, 185, 129, 0.4);
  transform: translateY(-1px);
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-main);
  border: 1px solid var(--border-color);
}

.btn-secondary:hover {
  background: #334155;
  color: #fff;
}

.btn-outline-danger {
  background: transparent;
  color: var(--accent-red);
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.btn-outline-danger:hover {
  background: rgba(239, 68, 68, 0.15);
  border-color: var(--accent-red);
}

.btn-icon {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  border-radius: var(--radius-sm);
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-icon:hover {
  background: #334155;
}

/* ==========================================================================
   AREA ANTEPRIMA PRINCIPALE (MAIN PREVIEW)
   ========================================================================== */

.preview-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #090d16;
  position: relative;
  overflow: hidden;
}

.preview-toolbar {
  height: 56px;
  padding: 0 1.5rem;
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 5;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.badge-status {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.3);
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.25rem 0.6rem;
  border-radius: 9999px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.sheet-dim-info {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--bg-sidebar);
  padding: 3px 6px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}

#zoom-level {
  font-size: 0.75rem;
  min-width: 44px;
  text-align: center;
  color: var(--text-muted);
  font-weight: 500;
}

/* SCROLL WRAPPER DEL FOGLIO */
.sheet-wrapper {
  flex: 1;
  overflow: auto;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 2.5rem 1.5rem 4rem 1.5rem;
}

/* ==========================================================================
   FOGLIO A4 (210mm x 297mm) - RENDERING PERFETTO & TIPOGRAFIA
   ========================================================================== */

.a4-page {
  width: 210mm;
  min-height: 297mm;
  height: 297mm;
  background: #ffffff;
  color: var(--doc-color);
  font-family: var(--font-doc);
  box-shadow: var(--shadow-sheet);
  box-sizing: border-box;
  padding: 13mm 18mm 12mm 18mm;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transform-origin: top center;
  transition: transform 0.15s ease-out;
  position: relative;
  overflow: hidden;
}

/* 1. HEADER CENTRATO CON LOGO */
.doc-header {
  text-align: center;
  margin-bottom: 0.9rem;
}

.header-logo {
  max-height: 54px;
  max-width: 180px;
  height: auto;
  width: auto;
  object-fit: contain;
  display: block;
  margin: 0 auto 0.35rem auto;
}

.header-logo.hidden-logo {
  display: none;
}

.header-circolo-name {
  font-family: var(--font-ui);
  font-size: 8.5pt;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #1e3a29;
  text-transform: uppercase;
}

/* 2. TITOLO CENTRATO (CON SPAZIATURA DEDICATA SOPRA E SOTTO) */
.doc-title-wrapper {
  text-align: center;
  margin: 1.4rem 0 1.5rem 0;
}

.doc-main-title {
  font-family: var(--font-doc);
  font-size: 13.5pt;
  font-weight: 700;
  color: #000000;
  letter-spacing: 0.02em;
}

/* 3. RIGA DATA (SX) E DESTINATARIO (DX) */
.doc-meta-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 1.1rem;
  font-size: 10.5pt;
}

.meta-left {
  text-align: left;
}

.meta-right {
  text-align: right;
}

/* 4. CORPO DEL TESTO GIUSTIFICATO */
.doc-body-paragraph {
  font-size: 10pt;
  line-height: 1.48;
  text-align: justify;
  text-justify: inter-word;
  hyphens: auto;
  color: #1a1a1a;
  margin-bottom: 0.65rem;
}

.text-justify {
  text-align: justify;
  text-justify: inter-word;
}

/* 5. AZIONE CENTRATA "affida" */
.doc-action-center {
  text-align: center;
  font-size: 10.5pt;
  font-weight: 600;
  margin: 0.65rem 0;
  text-transform: lowercase;
  letter-spacing: 0.05em;
}

/* 6. ARTICOLI */
.doc-articles {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin-top: 0.4rem;
  margin-bottom: 0.8rem;
}

.doc-article-item {
  font-size: 9.5pt;
  line-height: 1.38;
}

.article-heading {
  font-weight: 700;
  margin-bottom: 0.15rem;
  color: #000000;
}

.article-subheading {
  font-weight: 400;
  font-style: italic;
  font-size: 8.5pt;
  color: #444444;
}

.article-content {
  text-align: justify;
  text-justify: inter-word;
  color: #1f2937;
}

/* 7. SEZIONE FIRME */
.doc-signatures-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-top: 1rem;
  margin-bottom: 1rem;
  padding: 0 0.5rem;
}

.signature-box {
  width: 46%;
  font-size: 9.5pt;
  line-height: 1.4;
}

.left-box {
  text-align: left;
}

.right-box {
  text-align: right;
}

.sig-role {
  font-weight: 600;
  color: #111827;
  margin-bottom: 0.2rem;
}

.sig-name {
  font-weight: 500;
  color: #1f2937;
  margin: 0.15rem 0 1.2rem 0;
  min-height: 1.2em;
}

.sig-line {
  letter-spacing: 2px;
  color: #555555;
  font-size: 9pt;
}

/* 8. FOOTER UFFICIALE LEGAMBIENTE */
.doc-footer {
  text-align: center;
  border-top: 0.75pt solid #9ca3af;
  padding-top: 0.4rem;
  margin-top: auto;
  font-family: var(--font-ui);
  font-size: 6.8pt;
  line-height: 1.35;
  color: #374151;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.footer-line1 {
  font-weight: 700;
  color: #111827;
  margin-bottom: 1px;
}

.footer-line2 {
  font-weight: 500;
  color: #4b5563;
  margin-bottom: 1px;
}

.footer-line3 {
  font-weight: 600;
  color: #374151;
}

/* UTILITIES STILE ANTEPRIMA */
[data-field]:not([data-field="art3-clausole"]) {
  text-transform: uppercase;
}

.grammar-text {
  text-transform: none !important;
}

.preview-val.font-semibold {
  font-weight: 600;
}

.preview-val.font-mono {
  font-family: 'Courier New', Courier, monospace;
  font-weight: 600;
}

.preview-val.uppercase {
  text-transform: uppercase;
}

.preview-val.empty-field {
  display: inline-block;
  min-width: 60px;
  border-bottom: 1px dotted #94a3b8;
  color: #94a3b8;
  font-style: italic;
  font-size: 0.9em;
}

/* ==========================================================================
   STAMPA & ESPORTAZIONE PDF VETTORIALE (@MEDIA PRINT)
   ========================================================================== */

@media print {
  @page {
    size: A4 portrait;
    margin: 0;
  }

  html, body {
    width: 210mm !important;
    height: 297mm !important;
    background: #ffffff !important;
    color: #000000 !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: visible !important;
  }

  .sidebar,
  .preview-toolbar,
  .brand,
  button,
  .zoom-controls {
    display: none !important;
  }

  .app-layout {
    display: block !important;
    height: auto !important;
    width: auto !important;
  }

  .preview-area {
    display: block !important;
    background: #ffffff !important;
    padding: 0 !important;
    margin: 0 !important;
  }

  .sheet-wrapper {
    display: block !important;
    padding: 0 !important;
    margin: 0 !important;
  }

  .a4-page {
    width: 210mm !important;
    height: 297mm !important;
    min-height: 297mm !important;
    max-height: 297mm !important;
    padding: 14mm 18mm 12mm 18mm !important;
    box-shadow: none !important;
    transform: none !important;
    page-break-after: avoid !important;
    page-break-inside: avoid !important;
  }

  .preview-val.empty-field {
    border-bottom: 1px dotted #555555;
    color: transparent;
  }
}
