/* =========================================================================
   CIANCALEONI Preventivi — Design system
   Stile "Data-Dense Dashboard": navy/slate professionale, Fira Code per
   titoli e numeri (allineamento tabulare), Fira Sans per il testo corrente.
   ========================================================================= */

@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;600;700&family=Fira+Sans:wght@300;400;500;600;700&display=swap');

:root {
  --color-primary: #0F172A;
  --color-on-primary: #FFFFFF;
  --color-secondary: #334155;
  --color-accent: #2563EB;
  --color-accent-hover: #1D4ED8;
  --color-accent-soft: #EFF6FF;
  --color-background: #F8FAFC;
  --color-surface: #FFFFFF;
  --color-foreground: #020617;
  --color-muted: #E8ECF1;
  --color-muted-foreground: #64748B;
  --color-border: #E2E8F0;
  --color-destructive: #DC2626;
  --color-destructive-hover: #B91C1C;
  --color-success: #16A34A;
  --color-success-bg: #F0FDF4;
  --color-warning: #D97706;
  --color-warning-bg: #FFFBEB;
  --color-ring: #0F172A;

  --font-body: 'Fira Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'Fira Code', ui-monospace, SFMono-Regular, monospace;

  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 10px;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.14);

  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-5: 20px; --space-6: 24px; --space-8: 32px; --space-10: 40px;

  --sidebar-width: 220px;
  --header-height: 56px;
}

:root[data-theme="dark"] {
  /* --color-primary resta la stessa navy scura di sempre in entrambi i
     temi: è il colore "brand" fisso di sidebar/auth/toast, non un
     ruolo semantico che si inverte col tema. */
  --color-secondary: #CBD5E1;
  --color-accent: #38BDF8;
  --color-accent-hover: #7DD3FC;
  --color-accent-soft: #0C2340;
  --color-background: #0B1220;
  --color-surface: #131B2C;
  --color-foreground: #F1F5F9;
  --color-muted: #1B2436;
  --color-muted-foreground: #94A3B8;
  --color-border: #24304A;
  --color-destructive: #F87171;
  --color-destructive-hover: #FCA5A5;
  --color-success: #4ADE80;
  --color-success-bg: #0F2A1A;
  --color-warning: #FBBF24;
  --color-warning-bg: #2A2107;
  --color-ring: #38BDF8;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-secondary: #CBD5E1;
    --color-accent: #38BDF8;
    --color-accent-hover: #7DD3FC;
    --color-accent-soft: #0C2340;
    --color-background: #0B1220;
    --color-surface: #131B2C;
    --color-foreground: #F1F5F9;
    --color-muted: #1B2436;
    --color-muted-foreground: #94A3B8;
    --color-border: #24304A;
    --color-destructive: #F87171;
    --color-destructive-hover: #FCA5A5;
    --color-success: #4ADE80;
    --color-success-bg: #0F2A1A;
    --color-warning: #FBBF24;
    --color-warning-bg: #2A2107;
    --color-ring: #38BDF8;
  }
}

* { box-sizing: border-box; }
html { color-scheme: light dark; }

body {
  margin: 0;
  background: var(--color-background);
  color: var(--color-foreground);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  font-family: var(--font-mono);
  font-weight: 600;
  margin: 0 0 var(--space-2);
  letter-spacing: -0.01em;
}
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 14px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--color-muted-foreground); }

a { color: var(--color-accent); }

/* Numeri e codici sempre in monospace per allineamento tabulare */
.num, .code, input[type="number"], td.num, td.code {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }

::selection { background: var(--color-accent); color: var(--color-on-primary); }

/* Focus visibile ovunque (accessibilità) */
:focus-visible {
  outline: 2px solid var(--color-ring);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------------------------------------------------------------------
   Layout applicativo: sidebar + header + contenuto
   --------------------------------------------------------------------- */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--color-primary);
  color: var(--color-on-primary);
  padding: var(--space-5) var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
}
.sidebar-brand { padding: 0 var(--space-2); }
.sidebar-brand small { display: block; font-weight: 500; color: #fff; opacity: 0.92; font-size: 12.5px; margin-top: var(--space-2); font-family: var(--font-body); line-height: 1.4; }

/* Il logo ha wordmark scura pensata per sfondi chiari: un chip bianco
   fisso lo rende leggibile sia in sidebar (sempre scura) sia in dark
   mode, indipendentemente da dove viene mostrato. */
.brand-logo-chip {
  display: inline-flex; align-items: center;
  background: #fff;
  border-radius: var(--radius-md);
  padding: 7px 12px;
}
.brand-logo-chip img { display: block; height: 26px; width: auto; }

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar-nav a {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: rgba(255,255,255,0.72);
  text-decoration: none;
  font-size: 14px;
  transition: background 150ms ease, color 150ms ease;
  cursor: pointer;
}
.sidebar-nav a svg { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar-nav a:hover { background: rgba(255,255,255,0.08); color: #fff; }
.sidebar-nav a.active { background: rgba(255,255,255,0.14); color: #fff; font-weight: 500; }

.sidebar-footer { border-top: 1px solid rgba(255,255,255,0.12); padding-top: var(--space-3); display: flex; flex-direction: column; gap: var(--space-2); }
.sidebar-user { font-size: 13px; opacity: 0.8; padding: 0 var(--space-2); }
.sidebar-footer button { width: 100%; }

.main { display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: var(--header-height);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 var(--space-6);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 16px; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: var(--space-2); }
.topbar-left { display: flex; align-items: center; gap: var(--space-3); }

.content { padding: var(--space-6); flex: 1; min-width: 0; }

.btn-menu-toggle { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: var(--sidebar-width);
    transform: translateX(-100%); transition: transform 200ms ease; z-index: 50;
  }
  .sidebar.open { transform: translateX(0); }
  .content { padding: var(--space-4); }
  .btn-menu-toggle { display: inline-flex; }
  .sidebar-backdrop {
    display: none; position: fixed; inset: 0; background: rgba(2,6,23,0.5); z-index: 40;
  }
  .sidebar-backdrop.open { display: block; animation: fade-in 150ms ease; }
}

/* ---------------------------------------------------------------------
   Componenti base
   --------------------------------------------------------------------- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-header {
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
  flex-wrap: wrap;
}
.card-header > * { min-width: 0; }
.card-header .btn { white-space: nowrap; flex-shrink: 0; }
.card-body { padding: var(--space-5); }
.card + .card { margin-top: var(--space-4); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  padding: 9px 16px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-size: 14px; font-weight: 500;
  cursor: pointer;
  transition: background 150ms ease, border-color 150ms ease, opacity 150ms ease, transform 100ms ease;
  min-height: 38px;
  line-height: 1;
}
.btn:active { transform: scale(0.98); }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--color-accent); color: #fff; }
.btn-primary:hover { background: var(--color-accent-hover); }
.btn-secondary { background: var(--color-surface); border-color: var(--color-border); color: var(--color-foreground); }
.btn-secondary:hover { background: var(--color-muted); }
.btn-ghost { background: transparent; color: var(--color-secondary); }
.btn-ghost:hover { background: var(--color-muted); }
.btn-danger { background: transparent; color: var(--color-destructive); border-color: var(--color-border); }
.btn-danger:hover { background: var(--color-destructive); color: #fff; border-color: var(--color-destructive); }
.btn-sm { padding: 6px 10px; font-size: 13px; min-height: 30px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-icon { padding: 8px; min-width: 34px; }

.sidebar .btn-ghost { color: rgba(255,255,255,0.75); }
.sidebar .btn-ghost:hover { background: rgba(255,255,255,0.1); color: #fff; }

/* Form ------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field label {
  font-size: 12px; font-weight: 500; color: var(--color-muted-foreground);
  display: flex; align-items: center; gap: 4px;
}
.field label .req { color: var(--color-destructive); }
.field .hint { font-size: 12px; color: var(--color-muted-foreground); }
.field .error { font-size: 12px; color: var(--color-destructive); }

input, select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-foreground);
  min-height: 38px;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}
textarea { min-height: 72px; resize: vertical; }
input:hover, select:hover { border-color: var(--color-secondary); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-accent) 20%, transparent);
}
input.invalid, select.invalid { border-color: var(--color-destructive); }
input:disabled, select:disabled { background: var(--color-muted); opacity: 0.7; cursor: not-allowed; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--space-4);
}
.form-grid.cols-wide { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }

.checkbox-row { display: flex; align-items: center; gap: var(--space-2); }
.checkbox-row input[type="checkbox"] { width: 18px; height: 18px; min-height: 0; }

/* Tabelle ------------------------------------------------------------ */
.table-wrap { overflow-x: auto; border-radius: var(--radius-md); border: 1px solid var(--color-border); }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left; font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--color-muted-foreground);
  background: var(--color-muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
  position: sticky; top: 0;
}
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--color-border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--color-muted); }
td.num, th.num { text-align: right; }
td.center { text-align: center; }
table input[type="number"], table input[type="text"] { min-height: 32px; padding: 4px 8px; }

.table-empty { padding: var(--space-8); text-align: center; color: var(--color-muted-foreground); }
td.truncate { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Badge / stato ------------------------------------------------------ */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 500;
  background: var(--color-muted); color: var(--color-secondary);
}
.badge-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-bozza { background: var(--color-muted); color: var(--color-muted-foreground); }
.badge-inviato { background: var(--color-warning-bg); color: var(--color-warning); }
.badge-accettato { background: var(--color-success-bg); color: var(--color-success); }
.badge-archiviato { background: var(--color-muted); color: var(--color-muted-foreground); }

/* Riepilogo totali (sidebar destra nel form preventivo) -------------- */
.totali-box { position: sticky; top: calc(var(--header-height) + var(--space-4)); }
.totali-riga { display: flex; justify-content: space-between; align-items: baseline; padding: 7px 0; font-size: 13.5px; }
.totali-riga.divider { border-top: 1px solid var(--color-border); margin-top: var(--space-2); padding-top: var(--space-3); }
.totali-riga .val { font-family: var(--font-mono); font-weight: 500; }
.totali-riga.totale-finale { font-size: 18px; font-weight: 700; }
.totali-riga.totale-finale .val { font-family: var(--font-mono); }
.totali-riga.margine .val { color: var(--color-success); }
.totali-riga.margine.negativo .val { color: var(--color-destructive); }

/* Badge icona nelle intestazioni sezione ------------------------------*/
.section-icon {
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  width: 38px; height: 38px; border-radius: var(--radius-md);
  background: var(--color-accent);
  color: #fff;
}
.section-icon svg { width: 19px; height: 19px; }
.section-heading { display: flex; align-items: center; gap: var(--space-3); text-align: left; }
.section-heading-text h2 { font-size: 15px; }
.section-heading-text p { font-family: var(--font-body); font-weight: 400; font-size: 12.5px; color: var(--color-muted-foreground); margin: 2px 0 0; text-transform: none; letter-spacing: normal; }
.cat-section summary.section-heading { justify-content: space-between; }
.cat-section summary.section-heading > .chev { margin-left: auto; }

/* Icona interna ai campi (input/select) --------------------------------*/
.field-icon { position: relative; }
.field-icon > span[data-icon] {
  position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
  width: 16px; height: 16px; color: var(--color-muted-foreground); pointer-events: none;
  display: flex; align-items: center; justify-content: center;
}
.field-icon > span[data-icon] svg { width: 100%; height: 100%; }
.field-icon input, .field-icon select { padding-left: 34px; }
.field-icon select { padding-right: 30px; }

/* Profilo utente in topbar ---------------------------------------------*/
.user-menu { position: relative; }
.user-menu-trigger {
  display: flex; align-items: center; gap: var(--space-2);
  padding: 5px 10px 5px 5px; border-radius: 999px; border: 1px solid var(--color-border);
  background: var(--color-surface); cursor: pointer; font-size: 13.5px;
}
.user-menu-trigger:hover { background: var(--color-muted); }
.user-avatar {
  width: 26px; height: 26px; border-radius: 50%; background: var(--color-accent); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; flex-shrink: 0;
}
.user-menu-dropdown {
  position: absolute; top: calc(100% + 6px); right: 0; min-width: 190px;
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-md);
  box-shadow: var(--shadow-md); overflow: hidden; z-index: 40;
}
.user-menu-dropdown button, .user-menu-dropdown a {
  display: flex; align-items: center; gap: var(--space-2); width: 100%; text-align: left;
  padding: 9px 12px; font-size: 13.5px; background: none; border: none; cursor: pointer; color: var(--color-foreground);
}
.user-menu-dropdown button:hover, .user-menu-dropdown a:hover { background: var(--color-muted); }
.user-menu-dropdown svg { width: 16px; height: 16px; color: var(--color-muted-foreground); }
.user-menu-dropdown hr { border: none; border-top: 1px solid var(--color-border); margin: 4px 0; }

/* Toggle unità sconto (€ / %) -------------------------------------------*/
.sconto-row { display: flex; gap: 6px; }
.sconto-row .field-icon { flex: 1; }
.btn-unita { flex-shrink: 0; width: 42px; }
.btn-unita.attivo { background: var(--color-accent); color: #fff; border-color: var(--color-accent); }

/* Autocompletamento (campo Cliente) ----------------------------------*/
.autocomplete-field { position: relative; }
.autocomplete-dropdown {
  position: absolute; top: 100%; left: 0; right: 0; margin-top: 4px;
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-md);
  box-shadow: var(--shadow-md); max-height: 260px; overflow-y: auto; z-index: 30;
}
.autocomplete-item { padding: 9px 12px; cursor: pointer; font-size: 13.5px; border-bottom: 1px solid var(--color-border); }
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover, .autocomplete-item.attivo { background: var(--color-muted); }
.autocomplete-item .nome { color: var(--color-foreground); }
.autocomplete-item .dettaglio { color: var(--color-muted-foreground); font-size: 12px; margin-top: 2px; }
.autocomplete-empty { padding: 10px 12px; color: var(--color-muted-foreground); font-size: 13px; }

/* Tabs ----------------------------------------------------------------*/
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--color-border); margin-bottom: var(--space-5); overflow-x: auto; }
.tab-btn {
  padding: 10px 16px; border: none; background: transparent; cursor: pointer;
  font-size: 13.5px; font-weight: 500; color: var(--color-muted-foreground);
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
  transition: color 150ms ease, border-color 150ms ease;
}
.tab-btn:hover { color: var(--color-foreground); }
.tab-btn.active { color: var(--color-accent); border-bottom-color: var(--color-accent); }

/* Toast ---------------------------------------------------------------*/
#toast-container { position: fixed; bottom: var(--space-5); right: var(--space-5); z-index: 100; display: flex; flex-direction: column; gap: var(--space-2); }
.toast {
  background: var(--color-primary); color: var(--color-on-primary);
  padding: 12px 16px; border-radius: var(--radius-md); box-shadow: var(--shadow-lg);
  font-size: 13.5px; min-width: 240px; max-width: 360px;
  animation: toast-in 200ms ease;
}
.toast.success { background: var(--color-success); color: #fff; }
.toast.error { background: var(--color-destructive); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* Modal -----------------------------------------------------------------*/
.modal-overlay {
  position: fixed; inset: 0; background: rgba(2, 6, 23, 0.5);
  display: flex; align-items: center; justify-content: center; padding: var(--space-4);
  z-index: 60; animation: fade-in 150ms ease;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.modal { background: var(--color-surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); width: 100%; max-width: 480px; max-height: 90vh; overflow-y: auto; }
.modal-header { padding: var(--space-5); border-bottom: 1px solid var(--color-border); display: flex; justify-content: space-between; align-items: center; }
.modal-body { padding: var(--space-5); display: flex; flex-direction: column; gap: var(--space-4); }
.modal-footer { padding: var(--space-4) var(--space-5); border-top: 1px solid var(--color-border); display: flex; justify-content: flex-end; gap: var(--space-2); }

/* Utility ----------------------------------------------------------- */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: var(--space-1); } .gap-2 { gap: var(--space-2); } .gap-3 { gap: var(--space-3); } .gap-4 { gap: var(--space-4); }
.mt-2 { margin-top: var(--space-2); } .mt-4 { margin-top: var(--space-4); } .mt-6 { margin-top: var(--space-6); }
.mb-2 { margin-top: var(--space-2); } .mb-4 { margin-bottom: var(--space-4); }
.text-muted { color: var(--color-muted-foreground); }
.text-sm { font-size: 12.5px; }
.text-right { text-align: right; }
.w-full { width: 100%; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
.grid-main-side { display: grid; grid-template-columns: 1fr 320px; gap: var(--space-5); align-items: start; }
@media (max-width: 1100px) { .grid-main-side { grid-template-columns: 1fr; } .totali-box { position: static; } }
.cursor-pointer { cursor: pointer; }
.hidden { display: none !important; }
.section-title { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-3); }
.empty-state { text-align: center; padding: var(--space-10) var(--space-4); color: var(--color-muted-foreground); }
.empty-state svg { width: 40px; height: 40px; margin-bottom: var(--space-3); opacity: 0.5; }

/* Auth page ----------------------------------------------------------*/
.auth-page {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--color-primary); padding: var(--space-4);
}
.auth-card { width: 100%; max-width: 380px; background: var(--color-surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); padding: var(--space-8); }
.auth-brand { text-align: center; margin-bottom: var(--space-6); }
.auth-brand h1 { font-size: 20px; }
.auth-brand p { color: var(--color-muted-foreground); font-size: 13px; margin: 4px 0 0; }
