:root {
  --bg-0: #0a0a0c; --bg-1: #111116; --bg-2: #18181f; --bg-3: #22222c;
  --border: #2a2a36; --border-light: #35354a;
  --violet: #7c5cfc; --violet-dim: #7c5cfc1a;
  --green: #3de89e; --green-dim: #3de89e1a;
  --red: #f05b6e; --red-dim: #f05b6e1a;
  --blue: #38bdf8; --blue-dim: rgba(56,189,248,0.12);
  --white: #f0f0f4; --muted: #7a7a96; --muted-light: #a0a0b8;
  --font: 'Plus Jakarta Sans', sans-serif; --radius: 20px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --ease: cubic-bezier(.23, 1, .32, 1);
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { min-height: 100dvh; }
body { font-family: var(--font); background: var(--bg-0); color: var(--white); }

/* ── AUTH ── */
#authOverlay { position: fixed; inset: 0; background: var(--bg-0); z-index: 200; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-box { width: 100%; max-width: 380px; background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 32px 28px; }
.auth-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 28px; }
.auth-logo svg { width: 28px; height: 28px; }
.auth-logo span { font-size: 18px; font-weight: 800; letter-spacing: -.3px; }
.auth-title { font-size: 22px; font-weight: 800; margin-bottom: 6px; letter-spacing: -.4px; }
.auth-sub { font-size: 14px; color: var(--muted); margin-bottom: 24px; }
.auth-field { margin-bottom: 14px; }
.auth-field label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 6px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.auth-field input { width: 100%; background: var(--bg-3); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; color: var(--white); font-family: var(--font); font-size: 15px; outline: none; transition: border-color .2s; }
.auth-field input:focus { border-color: var(--violet); }
#registerFields { display: none; }
.auth-btn { width: 100%; background: var(--violet); color: #fff; border: none; border-radius: 12px; padding: 14px; font-family: var(--font); font-size: 15px; font-weight: 700; cursor: pointer; margin-top: 6px; transition: opacity .2s, transform .12s; }
.auth-btn:hover { opacity: .85; }
.auth-btn:active { transform: scale(.98); }
.auth-toggle { text-align: center; margin-top: 16px; font-size: 13px; color: var(--muted); }
.auth-toggle a { color: var(--violet); cursor: pointer; font-weight: 600; }
#authError { color: var(--red); font-size: 13px; margin-top: 10px; display: none; background: var(--red-dim); border: 1px solid #f05b6e33; border-radius: 8px; padding: 8px 12px; }

/* ── HEADER ── */
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 16px) 20px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-0);
  position: sticky; top: 0; z-index: 50;
}
.back-btn { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; font-weight: 600; text-decoration: none; transition: color .2s; padding: 6px 0; }
.back-btn:hover { color: var(--white); }
.back-btn svg { width: 18px; height: 18px; }
.header-title { font-size: 16px; font-weight: 700; letter-spacing: -.2px; }

/* ── LAYOUT ── */
.page-body { max-width: 720px; margin: 0 auto; padding: 20px 16px calc(var(--safe-bottom) + 48px); display: flex; flex-direction: column; gap: 12px; }
#mainView { display: none; flex-direction: column; gap: 12px; }
#mainView.visible { display: flex; }
.card { background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; min-width: 0; }
.card-label { font-size: 11px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: .08em; margin-bottom: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.card-head .card-label { margin-bottom: 0; }
.error-banner { display: none; background: var(--red-dim); border: 1px solid #f05b6e33; color: var(--red); border-radius: 12px; padding: 10px 14px; font-size: 13px; font-weight: 500; }

.loading-box { display: flex; justify-content: center; padding: 64px 0; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--border-light); border-top-color: var(--blue); border-radius: 50%; animation: spin .75s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── FILTRI ── */
.filters { display: flex; gap: 10px; }
.filters select, .field select {
  flex: 1; width: 100%; background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px;
  padding: 11px 34px 11px 14px; color: var(--white); font-family: var(--font); font-size: 14px; font-weight: 600;
  outline: none; appearance: none; -webkit-appearance: none; cursor: pointer;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path d='M1 1l4 4 4-4' stroke='%237a7a96' stroke-width='1.5' fill='none' stroke-linecap='round'/></svg>");
  background-repeat: no-repeat; background-position: right 14px center;
  transition: border-color .2s;
}
.field select { background-color: var(--bg-3); }
.filters select:focus-visible, .field select:focus { border-color: var(--blue); }

/* ── EMPTY ── */
.empty-card { display: none; text-align: center; padding: 28px 22px; }
.empty-card.visible { display: block; }
.empty-icon { width: 52px; height: 52px; border-radius: 16px; background: var(--blue-dim); color: var(--blue); display: flex; align-items: center; justify-content: center; margin: 0 auto 14px; }
.empty-icon svg { width: 26px; height: 26px; }
.empty-title { font-size: 18px; font-weight: 800; letter-spacing: -.3px; margin-bottom: 8px; }
.empty-sub { font-size: 14px; color: var(--muted); line-height: 1.55; margin: 0 auto 18px; max-width: 420px; }

/* ── KPI ── */
.kpi-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.kpi { background: var(--bg-2); border: 1px solid var(--border); border-radius: 16px; padding: 14px; min-width: 0; }
.kpi.tot { grid-column: 1 / -1; padding: 18px; }
.kpi-label { font-size: 10px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: .06em; margin-bottom: 7px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-value { font-size: 17px; font-weight: 800; letter-spacing: -.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.kpi.tot .kpi-value { font-size: 28px; color: var(--blue); letter-spacing: -.8px; }
.kpi-sub { font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }

/* ── GRAFICO ── */
.chart-box { position: relative; height: 230px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: -4px 0 12px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--muted); font-weight: 600; }
.legend-dot { width: 9px; height: 9px; border-radius: 3px; }
.legend-line { width: 14px; height: 0; border-top: 2px dashed var(--muted-light); }

/* ── STATISTICHE ── */
.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.stat { background: var(--bg-3); border-radius: 14px; padding: 12px; min-width: 0; }
.stat-label { font-size: 10px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px; line-height: 1.35; }
.stat-value { font-size: 15px; font-weight: 800; letter-spacing: -.3px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-value small { display: block; font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: 0; margin-top: 2px; }
.stat-value.pos { color: var(--green); }
.stat-value.neg { color: var(--red); }

/* ── LISTE ── */
.add-btn { display: inline-flex; align-items: center; gap: 6px; background: var(--blue-dim); color: var(--blue); border: 1px solid rgba(56,189,248,0.25); border-radius: 10px; padding: 8px 12px; min-height: 36px; font-family: var(--font); font-size: 12.5px; font-weight: 700; cursor: pointer; transition: background .18s, transform .12s; }
.add-btn:hover { background: rgba(56,189,248,0.2); }
.add-btn:active { transform: scale(.96); }
.add-btn svg { width: 13px; height: 13px; }

.row { display: flex; align-items: center; gap: 12px; padding: 12px 8px; margin: 0 -8px; border-radius: 12px; border: none; background: transparent; width: calc(100% + 16px); text-align: left; color: inherit; font-family: var(--font); cursor: pointer; transition: background .15s; }
.row:hover { background: var(--bg-3); }
.row:active { transform: scale(.995); }
.row + .row { border-top: 1px solid var(--border); }
.row:focus-visible { outline: 2px solid var(--blue); outline-offset: -2px; }
.row-info { flex: 1; min-width: 0; display: block; }
.row-top { display: flex; align-items: center; gap: 7px; }
.row-sub, .row-net, .row-gross { display: block; }
.row-title { font-size: 14px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 12px; color: var(--muted); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.row-amount { display: block; text-align: right; flex-shrink: 0; }
.row-net { font-size: 15px; font-weight: 800; letter-spacing: -.3px; font-variant-numeric: tabular-nums; }
.row-gross { font-size: 11px; color: var(--muted); font-weight: 600; margin-top: 3px; font-variant-numeric: tabular-nums; }
.pill { font-size: 9.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; color: var(--blue); background: var(--blue-dim); padding: 2px 7px; border-radius: 6px; flex-shrink: 0; }
.pill.violet { color: var(--violet); background: var(--violet-dim); }
.empty-msg { font-size: 14px; color: var(--muted); text-align: center; padding: 22px 0 10px; }

.sources-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 10px; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--bg-3); border: 1px solid var(--border-light); color: var(--white); border-radius: 999px; padding: 7px 12px; min-height: 34px; font-family: var(--font); font-size: 12.5px; font-weight: 700; cursor: pointer; transition: border-color .18s, transform .12s; }
.chip:hover { border-color: var(--muted); }
.chip:active { transform: scale(.96); }
.chip span { color: var(--muted); font-weight: 600; }
.chip.ghost { background: transparent; border-style: dashed; color: var(--muted); }

/* ── BOTTONI ── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: none; border-radius: 14px; padding: 14px 18px; font-family: var(--font); font-size: 15px; font-weight: 700; cursor: pointer; transition: opacity .2s, transform .12s, background .18s; }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--blue); color: #04131c; }
.btn-primary:hover { opacity: .88; }
.btn-secondary { background: var(--bg-3); color: var(--white); border: 1px solid var(--border-light); }
.btn-danger-ghost { background: transparent; color: var(--red); border: 1px solid #f05b6e44; }
.btn-danger-ghost:hover { background: var(--red-dim); }
.link-btn { background: none; border: none; color: var(--blue); font-family: var(--font); font-size: 12px; font-weight: 700; cursor: pointer; padding: 4px 0; }
.link-btn[hidden] { display: none; }

/* ── MODALI ── */
.modal { position: fixed; inset: 0; z-index: 150; background: rgba(5,5,8,.66); display: none; align-items: center; justify-content: center; padding: 20px; }
.modal.open { display: flex; }
.modal-panel {
  width: 100%; max-width: 460px; max-height: calc(100dvh - 40px); overflow-y: auto;
  background: var(--bg-2); border: 1px solid var(--border-light); border-radius: var(--radius); padding: 22px 20px;
  animation: panelIn 240ms var(--ease);
}
@keyframes panelIn { from { opacity: 0; transform: translateY(14px) scale(.98); } to { opacity: 1; transform: none; } }
.modal-title { font-size: 18px; font-weight: 800; letter-spacing: -.3px; margin-bottom: 18px; }
.field { margin-bottom: 14px; min-width: 0; }
.field label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 6px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.field input { width: 100%; background: var(--bg-3); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; color: var(--white); font-family: var(--font); font-size: 16px; outline: none; transition: border-color .2s; font-variant-numeric: tabular-nums; }
.field input:focus { border-color: var(--blue); }
.field input.input-strong { font-size: 18px; font-weight: 800; color: var(--blue); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.label-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 2px; }
.label-row label { margin-bottom: 0; }
.field-hint { font-size: 12px; color: var(--muted); line-height: 1.5; margin-top: 7px; }
.preview-line { font-size: 13px; color: var(--muted); font-weight: 600; margin: -4px 0 14px; font-variant-numeric: tabular-nums; }
.preview-line strong { color: var(--blue); }
.pct-input { position: relative; }
.pct-input span { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); font-weight: 700; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; background: var(--bg-3); border: 1px solid var(--border); border-radius: 12px; padding: 4px; }
.segmented button { background: transparent; border: none; border-radius: 9px; padding: 10px 8px; color: var(--muted); font-family: var(--font); font-size: 13px; font-weight: 700; cursor: pointer; transition: background .18s, color .18s; }
.segmented button[aria-checked="true"] { background: var(--blue); color: #04131c; }
.form-error { color: var(--red); font-size: 13px; font-weight: 500; margin: 4px 0 12px; display: none; background: var(--red-dim); border: 1px solid #f05b6e33; border-radius: 8px; padding: 8px 12px; }
.modal-actions { display: flex; gap: 8px; margin-top: 18px; }
.modal-actions .btn { flex: 1; padding: 13px 10px; font-size: 14px; }
.modal-actions .btn-danger-ghost { flex: 0 0 auto; }

/* ── MOBILE ── */
@media (max-width: 600px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .chart-box { height: 200px; }
  .card { padding: 18px 16px; }
  /* Modali come bottom sheet */
  .modal { align-items: flex-end; padding: 0; }
  .modal-panel { max-width: none; border-radius: 22px 22px 0 0; border-bottom: none; padding: 22px 18px calc(var(--safe-bottom) + 18px); max-height: 92dvh; animation-name: sheetIn; }
  @keyframes sheetIn { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
}
@media (max-width: 420px) {
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .kpi:last-child { grid-column: 1 / -1; }
  .kpi.tot .kpi-value { font-size: 26px; }
  .modal-actions { flex-wrap: wrap; }
  .modal-actions .btn-danger-ghost { flex: 1 1 100%; order: 3; }
}
@media (prefers-reduced-motion: reduce) {
  .modal-panel { animation: none; }
  .spinner { animation-duration: 1.5s; }
}
