/* Tema scuro in stile Apple, coerente con il gestionale dello studio:
   superfici scure stratificate, angoli molto arrotondati, ombre morbide,
   accento blu iOS. Font di sistema (SF Pro sul Mac). */

:root {
  --bg: #000000;            /* sfondo pagina, nero Apple */
  --panel: #1c1c1e;         /* superficie primaria (card) */
  --panel-2: #2c2c2e;       /* superficie secondaria (input, righe hover) */
  --panel-3: #3a3a3c;       /* bordi pieni, separatori marcati */
  --border: rgba(255, 255, 255, 0.09);
  --text: #f5f5f7;
  --muted: #98989f;
  --accent: #0a84ff;        /* blu iOS scuro */
  --accent-soft: rgba(10, 132, 255, 0.16);
  --pos: #30d158;           /* verde iOS */
  --neg: #ff453a;           /* rosso iOS */
  --warn: #ffd60a;          /* giallo iOS */
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 1px rgba(0, 0, 0, .35), 0 8px 28px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }

html { color-scheme: dark; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
        Segoe UI, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  letter-spacing: -.01em;
}

::selection { background: var(--accent-soft); }

/* ------------------------------------------------------------------ layout */

header.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  /* Il margine superiore tiene conto di notch e Dynamic Island quando l'app
     è aggiunta alla schermata Home (display a tutto schermo). */
  padding: max(12px, env(safe-area-inset-top)) max(22px, env(safe-area-inset-right))
           12px max(22px, env(safe-area-inset-left));
  background: rgba(22, 22, 24, 0.82);
  backdrop-filter: blur(22px) saturate(1.6);
  -webkit-backdrop-filter: blur(22px) saturate(1.6);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 17px; letter-spacing: -.02em; }
.brand img { display: block; flex: none; }
.brand span { color: var(--muted); font-weight: 400; font-size: 13px; }
.brand .brand-nome { color: var(--text); font-weight: 700; font-size: 17px; }

nav.tabs { display: flex; gap: 2px; margin-left: 10px; flex-wrap: wrap; }

nav.tabs button {
  background: none;
  border: 0;
  padding: 7px 14px;
  border-radius: 999px;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
nav.tabs button:hover { background: var(--panel-2); color: var(--text); }
nav.tabs button.active { background: var(--accent); color: #fff; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }

.user-chip {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--muted);
}
.user-chip img { width: 28px; height: 28px; border-radius: 50%; border: 1px solid var(--border); }

main {
  padding: 24px max(22px, env(safe-area-inset-right)) max(48px, env(safe-area-inset-bottom))
           max(22px, env(safe-area-inset-left));
  max-width: 1240px;
  margin: 0 auto;
}

.view { display: none; }
.view.active { display: block; animation: fade .22s cubic-bezier(.3, .7, .3, 1); }
@keyframes fade { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: none; } }

/* ------------------------------------------------------------------ pannelli */

.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 980px) {
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr; }
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
  min-width: 0;
}

.card h2 {
  margin: 0 0 14px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: .05em;
  text-transform: uppercase;
}

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }

.kpi {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -.03em;
  font-variant-numeric: tabular-nums;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", sans-serif;
}
.kpi.pos { color: var(--pos); }
.kpi.neg { color: var(--neg); }
.kpi-sub { font-size: 12.5px; color: var(--muted); margin-top: 5px; line-height: 1.55; }

.delta.pos { color: var(--pos); }
.delta.neg { color: var(--neg); }

/* Riga principale della dashboard: un dato dominante piu' due di contorno. */
.hero { display: grid; grid-template-columns: 1.6fr 1fr; gap: 16px; }
@media (max-width: 980px) { .hero { grid-template-columns: 1fr; } }

.hero-main { display: flex; flex-direction: column; justify-content: center; }
.kpi-xl { font-size: 44px; line-height: 1.1; margin-top: 2px; }
@media (max-width: 640px) { .kpi-xl { font-size: 34px; } }

/* Avvisi: compaiono solo quando c'e' qualcosa da segnalare. */
.alert {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 14px 16px; border-radius: var(--radius);
  background: var(--panel); border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.alert .alert-icon { font-size: 18px; line-height: 1.3; }
.alert b { display: block; font-size: 14px; margin-bottom: 2px; }
.alert span { font-size: 12.5px; color: var(--muted); }
.alert.warn { border-color: rgba(255, 214, 10, .35); }
.alert.bad { border-color: rgba(255, 69, 58, .35); }
.alert.good { border-color: rgba(48, 209, 88, .3); }
.alert button {
  margin-left: auto; align-self: center;
  background: none; border: 0; color: var(--accent);
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap;
}

/* ------------------------------------------------------------------ tabelle */

.table-wrap { overflow-x: auto; margin: 0 -6px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th {
  text-align: left;
  font-weight: 600;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--panel-3);
  white-space: nowrap;
}
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr { transition: background .12s ease; }
tbody tr:hover { background: var(--panel-2); }
tbody tr:hover td:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
tbody tr:hover td:last-child { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.pos { color: var(--pos); }
td.neg { color: var(--neg); }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--panel-2);
  font-size: 12px; font-weight: 500; white-space: nowrap;
  color: var(--text);
}
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }

/* ------------------------------------------------------------------ form */

form.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }

label.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 500; color: var(--muted); }
label.field.grow { flex: 1 1 180px; min-width: 140px; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  min-width: 0;
  transition: border-color .15s ease, background .15s ease;
}
input::placeholder, textarea::placeholder { color: #6e6e73; }
input:hover, select:hover, textarea:hover { background: var(--panel-3); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--panel-2);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { font-family: ui-monospace, "SF Mono", SFMono-Regular, Menlo, monospace; font-size: 13px; }
input[type="month"], input[type="date"] { color-scheme: dark; }

button.btn {
  font: inherit;
  font-weight: 600;
  padding: 9px 18px;
  border-radius: 999px;
  border: 0;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: filter .15s ease, transform .06s ease, background .15s ease;
}
button.btn:hover { filter: brightness(1.12); }
button.btn:active { transform: scale(.97); }
button.btn.ghost { background: var(--panel-2); color: var(--accent); }
button.btn.ghost:hover { background: var(--panel-3); filter: none; }
button.btn.danger { background: var(--panel-2); color: var(--neg); }

button.icon {
  background: none; border: 0; cursor: pointer; color: var(--muted);
  font-size: 15px; padding: 4px 7px; border-radius: 8px;
  transition: background .12s ease, color .12s ease;
}
button.icon:hover { background: var(--panel-3); color: var(--text); }

.seg { display: inline-flex; background: var(--panel-2); border-radius: 999px; overflow: hidden; padding: 2px; }
.seg button { background: none; border: 0; padding: 6px 14px; font: inherit; font-size: 13px; font-weight: 500; color: var(--muted); cursor: pointer; border-radius: 999px; }
.seg button.active { background: var(--accent); color: #fff; }

/* --------------------------------------------- ricerca categorie */

.cat-picker { position: relative; display: block; min-width: 200px; }
.cat-picker .cat-cerca { width: 100%; padding-right: 32px; }

.cat-picker .cat-pulisci {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: none; border: 0; color: var(--muted); cursor: pointer;
  font-size: 13px; padding: 5px 7px; border-radius: 6px; line-height: 1;
}
.cat-picker .cat-pulisci:hover { background: var(--panel-3); color: var(--text); }

.cat-lista {
  position: absolute; z-index: 50; top: calc(100% + 5px); left: 0; right: 0;
  max-height: 320px; overflow-y: auto;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 14px 40px rgba(0, 0, 0, .55);
  padding: 5px;
  -webkit-overflow-scrolling: touch;
}

.cat-voce {
  display: flex; align-items: center; gap: 9px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
}
.cat-voce.evidenziata { background: var(--accent-soft); }
.cat-voce:hover { background: var(--panel-3); }

.cat-testo { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.cat-testo b { font-weight: 600; font-size: 14px; }
.cat-testo span {
  font-size: 11.5px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.cat-vuoto { padding: 14px 10px; color: var(--muted); font-size: 13px; text-align: center; }

/* Sul telefono l'elenco resta comodo da toccare. */
@media (max-width: 820px) {
  .cat-lista { max-height: 46vh; }
  .cat-voce { padding: 11px 10px; }
}

/* ------------------------------------------------------------------ budget */

.bar { height: 7px; border-radius: 999px; background: var(--panel-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; transition: width .35s cubic-bezier(.3, .7, .3, 1); }

.budget-row { padding: 13px 0; border-bottom: 1px solid var(--border); }
.budget-row:last-child { border-bottom: 0; }
.budget-top { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-bottom: 8px; font-size: 14px; }
.budget-num { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }

/* ------------------------------------------------------------------ grafici */

svg.chart { width: 100%; display: block; overflow: visible; }
svg.chart text { fill: var(--muted); font-size: 11px; }
svg.chart .grid-line { stroke: rgba(255, 255, 255, 0.07); stroke-width: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-top: 14px; font-size: 13px; }
.legend div { display: flex; align-items: center; gap: 7px; color: var(--muted); }
.legend b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

.empty { color: var(--muted); font-size: 13.5px; padding: 26px 0; text-align: center; }

/* ------------------------------------------------------------------ toast e modale */

#toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: rgba(44, 44, 46, 0.92);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 11px 20px; border-radius: 999px; font-size: 14px; font-weight: 500;
  opacity: 0; pointer-events: none;
  transition: all .25s cubic-bezier(.3, .7, .3, 1);
  z-index: 100;
  box-shadow: var(--shadow);
}
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.error { background: rgba(90, 26, 22, 0.92); color: #ffb4ab; border-color: rgba(255, 69, 58, .35); }

dialog {
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--panel);
  color: var(--text);
  padding: 22px;
  max-width: 520px;
  width: calc(100% - 40px);
  box-shadow: 0 24px 70px rgba(0, 0, 0, .6);
}
dialog::backdrop { background: rgba(0, 0, 0, .55); backdrop-filter: blur(6px); }
dialog h3 { margin: 0 0 16px; font-size: 17px; letter-spacing: -.02em; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* ------------------------------------------------------------------ login e sblocco */

.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: radial-gradient(1100px 550px at 50% -12%, rgba(10, 132, 255, 0.13), transparent 60%), var(--bg);
}
.login-card { max-width: 400px; width: 100%; text-align: center; padding: 30px 28px; }
.login-card h1 { font-size: 24px; margin: 0 0 8px; letter-spacing: -.03em; }
.login-card p { color: var(--muted); font-size: 14px; margin: 0 0 22px; }

.google-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 12px 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--panel-2);
  color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background .15s ease;
}
.google-btn:hover { background: var(--panel-3); }

/* ------------------------------------------- app di inserimento rapido */

.pagina-aggiungi main.aggiungi-main {
  max-width: 520px;
  padding-top: 20px;
}

.seg-grande { display: flex; width: 100%; }
.seg-grande button { flex: 1; padding: 12px 0; font-size: 15px; font-weight: 600; }

/* L'importo è il campo protagonista: grande, subito raggiungibile. */
.importo-riga {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  margin-top: 24px;
}
.importo-valuta { font-size: 30px; color: var(--muted); font-weight: 600; }
.importo-riga input {
  background: none; border: 0; padding: 0;
  font-size: 54px; font-weight: 700; letter-spacing: -.03em;
  text-align: center; width: 100%; max-width: 300px;
  font-variant-numeric: tabular-nums;
}
.importo-riga input:focus { outline: none; box-shadow: none; }
.importo-riga input::placeholder { color: var(--panel-3); }

.btn-invio {
  width: 100%; margin-top: 20px; padding: 15px 0; font-size: 16px;
  border-radius: 14px;
}

.pagina-aggiungi .cat-lista { max-height: 40vh; }

/* ------------------------------------------------------- iPhone e iPad */

/* Safari ingrandisce la pagina quando si tocca un campo con testo sotto i
   16px: fissandoli a 16px l'app resta ferma dove sta. */
@media (max-width: 1024px) {
  input, select, textarea { font-size: 16px; }
}

/* Comportamento da applicazione quando è aggiunta alla schermata Home:
   niente selezione accidentale del testo, niente evidenziazione al tocco,
   niente rimbalzo elastico della pagina. */
@media (display-mode: standalone) {
  body {
    overscroll-behavior-y: none;
    -webkit-user-select: none;
    user-select: none;
  }
  /* I dati restano selezionabili e copiabili. */
  table, .kpi, .kpi-sub, .legend, code, input, textarea, select { -webkit-user-select: text; user-select: text; }
}

* { -webkit-tap-highlight-color: transparent; }

/* Aree toccabili adeguate al dito e schede scorrevoli in orizzontale. */
@media (max-width: 820px) {
  nav.tabs {
    order: 3;
    width: 100%;
    margin-left: 0;
    overflow-x: auto;
    flex-wrap: nowrap;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  nav.tabs::-webkit-scrollbar { display: none; }
  nav.tabs button { padding: 9px 14px; white-space: nowrap; }

  header.topbar { flex-wrap: wrap; gap: 10px; }
  .topbar-right { margin-left: auto; }

  button.icon { padding: 8px 10px; font-size: 16px; }
  button.btn { padding: 11px 18px; }
  .card { padding: 16px; }
  main { padding-top: 16px; }

  /* Su schermo stretto i moduli diventano una colonna sola. */
  form.row { gap: 12px; }
  label.field, label.field.grow { flex: 1 1 100%; min-width: 0; }
  label.field input, label.field select { width: 100%; }
  form.row > button.btn { width: 100%; }
}

/* iPad in verticale: due colonne restano leggibili, quattro no. */
@media (min-width: 821px) and (max-width: 1024px) {
  .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hero { grid-template-columns: 1fr 1fr; }
}

/* Il grafico principale non deve diventare una striscia sui telefoni. */
@media (max-width: 640px) {
  .kpi { font-size: 24px; }
  svg.chart { min-height: 150px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
}
