/* ============================================================
   Sagires UI — il sistema di stile comune a tutte le app Sagires
   ------------------------------------------------------------
   Colori, font, misure e componenti scritti una sola volta. Ogni app copia
   questo file (e la cartella fonts/) e usa SOLO queste classi: niente stili
   propri per pulsanti, schede, tabelle, moduli, etichette.

   Nomi delle classi: quelli di Interventi (btn btn-primary, card, badge,
   data-table, topbar...). Le app ereditate dalle altre convenzioni si
   allineano a questi.

   Indice: 1 base · 2 disposizione (barra in alto, menu laterale, pagina) ·
   3 pulsanti · 4 schede e misure · 5 etichette e avvisi · 6 tabelle ·
   7 moduli · 8 finestre e notifiche · 9 utilità
   ============================================================ */

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

/* Inter servito dall'app stessa: le regole di sicurezza accettano solo font-src 'self' */
@font-face {
  font-family: "Inter";
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("../fonts/inter-latin-wght-normal.woff2") format("woff2");
}

/* ---------- 1. Base ---------- */
:root {
  /* Blu navy: struttura (barra in alto, titoli) */
  --brand-50:  #eaf0f8;
  --brand-100: #dbe5f2;
  --brand-600: #2f5480;
  --brand-700: #24466d;
  --brand-800: #1b3455;
  --brand-900: #14263d;

  /* Arancione: l'azione principale (un solo pulsante arancione per schermata, di norma) */
  --accent-100: #fdecd8;
  --accent-400: #f5ad63;
  --accent-500: #f39c4a;
  --accent-600: #e0863a;

  --text-on-navy: #eef1f6;
  --text-on-navy-dim: #b7c4d6;

  /* Significati: non sono colori di decorazione */
  --success: #2a8a68;  --success-100: #e3f4ec;  --success-900: #1d5e47;
  --warning: #9a6500;  --warning-100: #fdf1d3;  --warning-900: #6e4800;
  --danger:  #bf5136;  --danger-100:  #fbeae5;  --danger-900:  #8a3622;
  --info:    #3467a8;  --info-100:    #e6eef9;

  /* Fondo caldo e superfici */
  --bg:        #f5f3ec;
  --surface:   #ffffff;
  --surface-2: #faf8f2;
  --border:    #e7e2d3;
  --border-2:  #d9d2bd;
  --text:      #23293a;
  --text-2:    #6b6558;
  --text-3:    #9c9686;

  --shadow-xs: 0 1px 2px rgba(20, 38, 61, 0.05);
  --shadow-sm: 0 2px 6px rgba(20, 38, 61, 0.06);
  --shadow-md: 0 8px 24px rgba(20, 38, 61, 0.1);
  --shadow-lg: 0 16px 40px rgba(20, 38, 61, 0.16);

  --radius:    8px;    /* campi, pulsanti piccoli, voci di menu */
  --radius-lg: 12px;   /* pulsanti */
  --radius-xl: 18px;   /* schede */

  --topbar-h: 56px;
  --sidebar-w: 240px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
}

html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
::selection { background: var(--accent-100); color: var(--brand-900); }
a { color: var(--brand-700); text-decoration: none; }
a:hover { text-decoration: underline; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
code { font-family: var(--mono); font-size: 0.9em; background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; }
[hidden], .hidden { display: none !important; }
:focus-visible { outline: 3px solid var(--accent-400); outline-offset: 2px; }

/* ---------- 2. Disposizione ---------- */

/* Barra in alto, uguale in tutte le app: logo + «Sagires · NOME APP» a sinistra,
   eventuali schede, poi a destra azioni, pulsante Console, utente, uscita. */
.topbar {
  background: var(--brand-900);
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px;
  position: sticky; top: 0; z-index: 50;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06), var(--shadow-sm);
}
.topbar-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-logo { width: 26px; height: 26px; display: block; flex-shrink: 0; }
.topbar-title { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; color: var(--text-on-navy); white-space: nowrap; }
.topbar-title span { color: var(--accent-400); font-weight: 500; }
.topbar-nav { display: flex; gap: 4px; margin-left: 16px; }
.topbar-tab {
  background: transparent; border: none; cursor: pointer; font: inherit;
  color: var(--text-on-navy-dim); font-size: 13.5px; font-weight: 600;
  padding: 7px 12px; border-radius: var(--radius); text-decoration: none;
  transition: background 0.12s, color 0.12s;
}
.topbar-tab:hover { background: rgba(255, 255, 255, 0.08); color: var(--text-on-navy); text-decoration: none; }
.topbar-tab.is-active { background: rgba(255, 255, 255, 0.12); color: #fff; box-shadow: inset 0 -2px 0 var(--accent-500); }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--text-on-navy-dim); }
.topbar-divider { width: 1px; height: 22px; background: rgba(255, 255, 255, 0.16); }
/* Ritorno alla Sagires Console: in ogni app, sempre con target="sagires-console" */
.topbar-console {
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  font-size: 13px; font-weight: 600; color: var(--text-on-navy);
  padding: 6px 12px; border-radius: 999px; border: 1px solid rgba(255, 255, 255, 0.18);
  transition: background 0.12s, border-color 0.12s;
}
.topbar-console:hover { background: rgba(255, 255, 255, 0.1); border-color: var(--accent-500); text-decoration: none; color: #fff; }
.topbar-console svg { width: 16px; height: 16px; color: var(--accent-400); }
.topbar-user { display: flex; align-items: center; gap: 8px; color: var(--text-on-navy); white-space: nowrap; }
.avatar {
  width: 28px; height: 28px; border-radius: 999px; flex-shrink: 0;
  background: var(--accent-500); color: var(--brand-900);
  font-size: 11.5px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.avatar:empty { display: none; }
.icon-btn {
  background: transparent; border: none; cursor: pointer;
  width: 32px; height: 32px; border-radius: var(--radius);
  color: var(--text-on-navy-dim);
  display: flex; align-items: center; justify-content: center;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { background: rgba(255, 255, 255, 0.1); color: var(--accent-400); }

/* Disposizione A: barra in alto + menu laterale (app complesse) */
.menu-toggle { display: none; }          /* il pulsante ☰ compare solo su telefono */
.sidebar-backdrop { display: none; }
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas: "topbar topbar" "sidebar content";
  min-height: 100vh;
}
.app-shell > .topbar { grid-area: topbar; }
.sidebar {
  grid-area: sidebar;
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 14px 10px; overflow-y: auto;
  position: sticky; top: var(--topbar-h); height: calc(100vh - var(--topbar-h));
}
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 11px; border-radius: var(--radius);
  color: var(--text-2); font-size: 13.5px; font-weight: 500; text-decoration: none;
  transition: background 0.12s, color 0.12s;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-item.active { background: var(--accent-100); color: var(--accent-600); font-weight: 700; }
.nav-item svg { width: 17px; height: 17px; flex-shrink: 0; }
.nav-section { padding: 16px 11px 6px; font-size: 11.5px; font-weight: 700; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.04em; }
.content { grid-area: content; padding: 28px 32px 64px; min-width: 0; }

/* Disposizione B: barra in alto + pagina semplice (app piccole) */
.page { max-width: 1280px; margin: 0 auto; padding: 28px 32px 64px; min-width: 0; }
.page-narrow { max-width: 960px; }

.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-title { font-size: 24px; font-weight: 800; letter-spacing: -0.02em; color: var(--brand-900); text-wrap: balance; }
.page-subtitle { color: var(--text-2); font-size: 13.5px; margin-top: 4px; max-width: 75ch; }
.page-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ---------- 3. Pulsanti ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 9px 16px;
  border: 1px solid transparent; border-radius: var(--radius-lg);
  font: inherit; font-size: 13.5px; font-weight: 600; line-height: 1.2; white-space: nowrap;
  cursor: pointer; text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}
.btn:hover { text-decoration: none; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn-primary   { background: var(--accent-500); color: var(--brand-900); }
.btn-primary:hover:not(:disabled) { background: var(--accent-600); }
.btn-secondary { background: var(--surface); color: var(--brand-800); border-color: var(--border-2); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); border-color: var(--brand-700); }
.btn-ghost     { background: transparent; color: var(--brand-700); }
.btn-ghost:hover:not(:disabled) { background: var(--brand-50); }
.btn-danger    { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #a3432c; }
.btn-success   { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: #22735a; }
.btn-sm    { min-height: 30px; padding: 5px 11px; font-size: 12.5px; border-radius: var(--radius); }
.btn-lg    { min-height: 46px; padding: 12px 22px; font-size: 15px; }
.btn-block { width: 100%; }
.btn-group { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- 4. Schede e misure ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xs); padding: 20px 22px; margin-bottom: 16px;
}
/* Scheda che contiene solo una tabella: senza margine interno, con gli angoli che ritagliano la tabella */
.card-flush { padding: 0; overflow: hidden; }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.card-title { font-size: 16px; font-weight: 700; color: var(--brand-800); }
.card-subtitle { font-size: 13px; color: var(--text-2); margin-top: 2px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xs); padding: 16px 18px;
  --colore: var(--brand-700); border-left: 4px solid var(--colore);
}
.stat-card.is-success { --colore: var(--success); }
.stat-card.is-warning { --colore: var(--accent-600); }
.stat-card.is-danger  { --colore: var(--danger); }
.stat-label { font-size: 11.5px; font-weight: 700; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.stat-value { font-size: 28px; font-weight: 800; letter-spacing: -0.02em; color: var(--brand-900); line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-meta  { font-size: 12.5px; color: var(--text-2); margin-top: 6px; }

.progress { height: 8px; border-radius: 999px; background: var(--border); overflow: hidden; }
.progress > span { display: block; height: 100%; border-radius: 999px; background: var(--success); }
.progress.is-warning > span { background: var(--warning); }
.progress.is-danger > span { background: var(--danger); }

.empty { text-align: center; padding: 40px 16px; color: var(--text-2); }
.empty strong { display: block; color: var(--brand-800); font-size: 15px; margin-bottom: 4px; }

.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--brand-600); border-radius: 50%; animation: gira 0.8s linear infinite; }
@keyframes gira { to { transform: rotate(360deg); } }

/* ---------- 5. Etichette e avvisi ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; line-height: 1.4; white-space: nowrap;
  background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border);
}
.badge-success { background: var(--success-100); color: var(--success-900); border-color: transparent; }
.badge-warning { background: var(--warning-100); color: var(--warning-900); border-color: transparent; }
.badge-danger  { background: var(--danger-100);  color: var(--danger-900);  border-color: transparent; }
.badge-info    { background: var(--info-100);    color: var(--brand-800);   border-color: transparent; }
.badge-accent  { background: var(--accent-100);  color: var(--brand-900);   border-color: transparent; }
.badge-dot::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.alert { padding: 11px 14px; border-radius: var(--radius-lg); border: 1px solid; font-size: 13.5px; margin-bottom: 14px; }
.alert-success { background: var(--success-100); border-color: var(--success); color: var(--success-900); }
.alert-danger  { background: var(--danger-100);  border-color: var(--danger);  color: var(--danger-900); }
.alert-warning { background: var(--warning-100); border-color: var(--warning); color: var(--warning-900); }
.alert-info    { background: var(--info-100);    border-color: var(--info);    color: var(--brand-800); }

/* ---------- 6. Tabelle ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.data-table th {
  text-align: left; padding: 10px 14px; background: var(--surface-2);
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-2);
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
.data-table td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover td { background: var(--surface-2); }
.data-table .num { text-align: right; }

/* ---------- 7. Moduli ---------- */
.form-group { margin-bottom: 16px; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--text); }
label .required { color: var(--danger); }
/* :where() azzera la specificità: i campi hanno questo aspetto di base ma ogni app può ritoccarli con una regola normale */
:where(input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=file])), select, textarea {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border-2); border-radius: var(--radius);
  font: inherit; font-size: 14px; background: var(--surface); color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent-500); box-shadow: 0 0 0 3px var(--accent-100); }
input:disabled, select:disabled, textarea:disabled { background: var(--bg); color: var(--text-2); }
input.invalid, select.invalid, textarea.invalid { border-color: var(--danger); background: var(--danger-100); }
textarea { resize: vertical; min-height: 80px; }
input[type=checkbox], input[type=radio] { accent-color: var(--brand-700); width: 16px; height: 16px; }
.hint { font-size: 12px; color: var(--text-3); margin-top: 4px; }
.input-error { font-size: 12.5px; color: var(--danger); margin-top: 4px; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 13.5px; font-weight: 500; cursor: pointer; margin: 0; }

/* ---------- 8. Finestre e notifiche ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(20, 38, 61, 0.45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 1000; }
.modal { background: var(--surface); border-radius: var(--radius-xl); box-shadow: var(--shadow-lg); width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow: auto; }
.modal-lg { max-width: 800px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--border); }
.modal-title { font-size: 17px; font-weight: 700; color: var(--brand-900); }
.modal-body { padding: 20px 22px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 22px; border-top: 1px solid var(--border); }

.toast-container { position: fixed; top: calc(var(--topbar-h) + 16px); right: 20px; z-index: 2000; display: flex; flex-direction: column; gap: 10px; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--brand-700);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-md); padding: 12px 16px; font-size: 13px;
}
.toast-success { border-left-color: var(--success); }
.toast-error   { border-left-color: var(--danger); }
.toast-warning { border-left-color: var(--accent-500); }

.tabs { display: flex; flex-wrap: wrap; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.tab {
  background: transparent; border: none; border-bottom: 2px solid transparent; margin-bottom: -1px;
  padding: 9px 13px; font: inherit; font-size: 13px; font-weight: 600; color: var(--text-2); cursor: pointer;
}
.tab:hover { color: var(--brand-800); }
.tab.is-active { color: var(--brand-800); border-bottom-color: var(--accent-500); }

/* ---------- 9. Utilità ---------- */
.text-muted { color: var(--text-2); }
.text-sm { font-size: 12px; }
.text-right { text-align: right; }
.flex { display: flex; } .items-center { align-items: center; } .justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; } .gap-3 { gap: 12px; } .wrap { flex-wrap: wrap; }
.mt-2 { margin-top: 8px; } .mt-3 { margin-top: 12px; } .mt-4 { margin-top: 16px; }
.mb-2 { margin-bottom: 8px; } .mb-3 { margin-bottom: 12px; } .mb-4 { margin-bottom: 16px; }

/* ---------- Schermi stretti ---------- */
@media (max-width: 900px) {
  .app-shell { grid-template-columns: minmax(0, 1fr); grid-template-areas: "topbar" "content"; }
  /* Menu laterale su telefono: nascosto, si apre con il pulsante ☰ (js/sagires-ui.js) sopra la pagina */
  .menu-toggle { display: flex; }
  .sidebar {
    position: fixed; left: 0; top: var(--topbar-h); bottom: 0; height: auto;
    width: min(280px, 85vw); z-index: 60; box-shadow: var(--shadow-lg);
    transform: translateX(-100%); visibility: hidden;
    transition: transform 0.2s ease, visibility 0.2s;
  }
  .app-shell.menu-open .sidebar { transform: none; visibility: visible; }
  .sidebar-backdrop { position: fixed; inset: var(--topbar-h) 0 0 0; background: rgba(20, 38, 61, 0.45); z-index: 55; }
  .app-shell.menu-open .sidebar-backdrop { display: block; }
  .content, .page { padding: 20px 16px 48px; }
  .topbar { padding: 0 12px; gap: 8px; }
  .topbar-console span, .topbar-user-nome, .topbar-divider { display: none; }
  /* Le schede della barra scorrono in orizzontale invece di spingere fuori le azioni */
  .topbar-nav { flex: 1 1 0; min-width: 0; margin-left: 4px; overflow-x: auto; scrollbar-width: none; }
  .topbar-nav::-webkit-scrollbar { display: none; }
  .topbar-tab { white-space: nowrap; }
  .topbar-actions { flex-shrink: 0; margin-left: auto; gap: 6px; }
}
@media (max-width: 560px) {
  .topbar-title { display: none; }
  .page-title { font-size: 21px; }
}
@media (prefers-reduced-motion: reduce) { * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; } }
