/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-0: #0a0a0c;
  --bg-1: #111116;
  --bg-2: #18181f;
  --bg-3: #22222c;
  --border: #2a2a36;
  --border-light: #35354a;
  --violet: #7c5cfc;
  --violet-dim: #5a3fd4;
  --violet-glow: rgba(124,92,252,0.18);
  --white: #f0f0f4;
  --muted: #7a7a96;
  --muted-light: #a0a0b8;
  --green: #3de89e;
  --green-dim: rgba(61,232,158,0.12);
  --red: #f05b6e;
  --red-dim: rgba(240,91,110,0.12);
  --font: 'Plus Jakarta Sans', sans-serif;
  --radius: 16px;
  --radius-sm: 10px;
  --sidebar-w: 230px;
}

html, body { height: 100%; background: var(--bg-0); color: var(--white); font-family: var(--font); font-size: 14px; line-height: 1.6; -webkit-font-smoothing: antialiased; }
body { display: flex; min-height: 100vh; }

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: var(--bg-1);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 32px 16px 24px;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
}

.logo { display: flex; align-items: center; gap: 10px; margin-bottom: 40px; padding: 0 8px; }
.logo-icon { font-size: 20px; color: var(--violet); }
.logo-text { font-size: 17px; font-weight: 700; letter-spacing: -0.3px; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  color: var(--muted); text-decoration: none;
  font-size: 13.5px; font-weight: 500; transition: all 0.18s ease;
}
.nav-item:hover { color: var(--white); background: var(--bg-3); }
.nav-item.active { color: var(--white); background: var(--violet-glow); border: 1px solid rgba(124,92,252,0.22); }
.nav-item svg { flex-shrink: 0; }

.nav-back { color: var(--muted); font-size: 12px; opacity: 0.7; margin-bottom: 2px; }
.nav-back:hover { opacity: 1; color: var(--white); }
.nav-divider { height: 1px; background: var(--border); margin: 6px 4px 8px; }

.sidebar-bottom { padding-top: 24px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }

.upload-btn-side {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--bg-3); border: 1px solid var(--border-light);
  color: var(--muted-light); padding: 10px 14px; border-radius: var(--radius-sm);
  font-family: var(--font); font-size: 13px; font-weight: 500; cursor: pointer;
  transition: all 0.18s; text-align: center; text-decoration: none;
}
.upload-btn-side:hover { color: var(--white); border-color: var(--violet); background: var(--violet-glow); }

.upload-btn-secondary { opacity: 0.55; font-size: 12px; padding: 8px 14px; }
.upload-btn-secondary:hover { opacity: 1; }

.sidebar-footer { font-size: 11px; color: var(--border-light); text-align: center; margin-top: 4px; }

/* ===== MAIN ===== */
.main { margin-left: var(--sidebar-w); flex: 1; position: relative; min-height: 100vh; }

/* ===== PAGES ===== */
.page { display: none; padding: 40px 48px; }
.page.active { display: block; }

/* ===== HEADER ===== */
.header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 36px; gap: 20px; flex-wrap: wrap; }
.header-title h1 { font-size: 30px; font-weight: 800; letter-spacing: -0.8px; line-height: 1.15; }
.subtitle { color: var(--muted); font-size: 13.5px; margin-top: 4px; }

.selectors { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.selector {
  background: var(--bg-2); border: 1px solid var(--border); color: var(--white);
  padding: 9px 32px 9px 14px; border-radius: var(--radius-sm);
  font-family: var(--font); font-size: 13px; font-weight: 500; cursor: pointer; outline: none;
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237a7a96' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
  transition: border-color 0.18s;
}
.selector:hover, .selector:focus { border-color: var(--violet); }
.selector option { background: var(--bg-2); }

.btn-primary {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--violet); color: white;
  padding: 12px 24px; border-radius: var(--radius-sm);
  font-family: var(--font); font-size: 14px; font-weight: 600; cursor: pointer;
  transition: background 0.18s, transform 0.1s; border: none; text-decoration: none;
}
.btn-primary:hover { background: var(--violet-dim); transform: translateY(-1px); }

/* ===== KPI GRID ===== */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px; }
.kpi-card {
  background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 26px 26px 22px; position: relative; overflow: hidden;
  transition: border-color 0.2s, transform 0.2s;
}
.kpi-card::before {
  content: ''; position: absolute; top: 0; left: 0; right: 0; height: 2px;
  background: var(--kpi-color, var(--violet));
}
.kpi-card:hover { border-color: var(--border-light); transform: translateY(-2px); }
.kpi-label { font-size: 11px; font-weight: 600; letter-spacing: 0.8px; text-transform: uppercase; color: var(--muted); margin-bottom: 10px; }
.kpi-value { font-size: 28px; font-weight: 700; letter-spacing: -0.8px; }
.kpi-badge {
  position: absolute; top: 22px; right: 22px;
  width: 34px; height: 34px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; font-weight: 700;
}
.kpi-sub {
  display: inline-flex; align-items: center;
  margin-top: 10px; padding: 5px 11px;
  border-radius: 100px; font-size: 12px; font-weight: 700; letter-spacing: 0.01em;
}
.kpi-sub.positive { background: rgba(61,232,158,0.10); border: 1px solid rgba(61,232,158,0.22); color: var(--green); }
.kpi-sub.negative { background: rgba(240,91,110,0.10); border: 1px solid rgba(240,91,110,0.22); color: var(--red); }
.kpi-sub.neutral { background: rgba(124,92,252,0.12); border: 1px solid rgba(124,92,252,0.28); color: var(--violet); }
.kpi-sub:empty { display: none; }

/* ===== CHARTS ===== */
.charts-row { display: flex; gap: 16px; margin-bottom: 16px; }
.charts-row.two-col { display: grid; grid-template-columns: 1fr 1fr; }
.charts-row.three-col { display: grid; grid-template-columns: 1fr 1fr 1fr; }
.chart-card { background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 26px; flex: 1; transition: border-color 0.2s; }
.chart-card:hover { border-color: var(--border-light); }
.chart-wide { width: 100%; }
.chart-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 22px; gap: 12px; }
.chart-title { font-size: 15px; font-weight: 700; letter-spacing: -0.2px; }
.chart-subtitle { font-size: 12px; color: var(--muted); display: block; margin-top: 2px; }
.chart-wrap { position: relative; height: 220px; }
.chart-wrap-donut { height: 280px; display: flex; align-items: center; justify-content: center; }
.chart-wrap-mom { position: relative; height: 200px; }
.chart-wrap-tall { height: 340px; }
.chart-stat-pill { font-size: 12px; font-weight: 600; color: var(--violet); background: var(--violet-glow); border: 1px solid rgba(124,92,252,0.25); padding: 5px 12px; border-radius: 999px; white-space: nowrap; }

/* ===== FILTER BAR ===== */
.filter-bar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.tx-count { font-size: 12px; color: var(--muted); font-weight: 500; margin-left: 4px; }

/* ===== TABLE ===== */
.table-card { background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 32px; }
.inv-table { width: 100%; border-collapse: collapse; }
.inv-table thead { background: var(--bg-3); }
.inv-table th { padding: 13px 18px; font-size: 11px; font-weight: 600; letter-spacing: 0.8px; text-transform: uppercase; color: var(--muted); text-align: left; border-bottom: 1px solid var(--border); }
.inv-table td { padding: 13px 18px; font-size: 13.5px; font-weight: 400; color: var(--muted-light); border-bottom: 1px solid var(--border); transition: color 0.15s; }
.inv-table tbody tr:last-child td { border-bottom: none; }
.inv-table tbody tr:hover td { color: var(--white); background: rgba(255,255,255,0.02); }
.text-right { text-align: right !important; }
.td-amount { font-weight: 600; font-size: 14px; color: var(--white); }

.sortable-th { cursor: pointer; user-select: none; white-space: nowrap; transition: color 0.15s; }
.sortable-th:hover { color: var(--white); }
.sortable-th.sort-active { color: var(--violet); }
.sort-icon { display: inline-block; margin-left: 5px; font-size: 11px; opacity: 0.5; transition: opacity 0.15s; }
.sortable-th:hover .sort-icon,
.sortable-th.sort-active .sort-icon { opacity: 1; }

.inv-total-row td { border-top: 1px solid var(--border-light) !important; border-bottom: none !important; background: var(--bg-3); padding-top: 15px !important; padding-bottom: 15px !important; }

.cat-pill { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 600; letter-spacing: 0.3px; background: var(--violet-glow); color: var(--violet); border: 1px solid rgba(124,92,252,0.2); }

/* ===== LOADING STATE ===== */
.loading-state {
  display: none;
  flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  color: var(--muted); font-size: 14px;
}
.loading-state.visible { display: flex; }

@keyframes spin { to { transform: rotate(360deg); } }
.loading-spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border-light);
  border-top-color: var(--violet);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ===== EMPTY STATE ===== */
.empty-state {
  display: none; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 80px 40px;
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  background: var(--bg-2); border: 1px dashed var(--border-light);
  border-radius: var(--radius); min-width: 380px;
}
.empty-state.visible { display: flex; }
.empty-icon { font-size: 44px; color: var(--violet); margin-bottom: 20px; opacity: 0.5; }
.empty-state h2 { font-size: 20px; font-weight: 700; margin-bottom: 8px; }
.empty-state p { color: var(--muted); font-size: 14px; max-width: 320px; line-height: 1.7; margin-bottom: 28px; }

/* ===== GESTIONE SECTION ===== */
.gestione-section {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
}
.gestione-title { font-size: 13px; font-weight: 700; letter-spacing: 0.6px; text-transform: uppercase; color: var(--muted); margin-bottom: 14px; }
.gestione-list { display: flex; flex-direction: column; gap: 6px; }
.gestione-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 14px;
}
.gestione-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.gestione-nome { flex: 1; font-size: 14px; font-weight: 500; color: var(--white); }
.gestione-del { background: none; border: none; color: var(--muted); font-size: 13px; cursor: pointer; padding: 2px 8px; border-radius: 6px; transition: all 0.15s; font-family: var(--font); }
.gestione-del:hover { color: var(--red); background: var(--red-dim); }
.gestione-fixed { font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing: 0.04em; }
.gestione-add-row { display: flex; gap: 8px; align-items: center; }
.gestione-add-input {
  flex: 1; background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 14px; color: var(--white);
  font-family: var(--font); font-size: 14px; outline: none;
  transition: border-color 0.2s;
}
.gestione-add-input:focus { border-color: var(--violet); }
.gestione-add-input::placeholder { color: var(--muted); }
.gestione-add-btn {
  background: var(--violet); color: #fff; border: none; border-radius: 10px;
  padding: 10px 18px; font-family: var(--font); font-size: 14px; font-weight: 600;
  cursor: pointer; transition: opacity 0.2s; white-space: nowrap;
}
.gestione-add-btn:hover { opacity: 0.85; }

/* ===== DRAG AND DROP ===== */
.gestione-grip { cursor: grab; display: flex; align-items: center; margin-right: 2px; flex-shrink: 0; opacity: 0.5; transition: opacity 0.15s; }
.gestione-grip:hover { opacity: 1; }
.gestione-item.dragging { opacity: 0.4; }
.gestione-item.drag-over { box-shadow: 0 0 0 2px var(--violet); }

/* ===== COLOR PICKER ===== */
.color-input {
  width: 36px; height: 36px;
  border: 1px solid var(--border); border-radius: 8px;
  cursor: pointer; padding: 2px; background: var(--bg-3);
  flex-shrink: 0;
}

/* ===== HEATMAP ===== */
.heatmap-wrap { overflow-x: auto; }
.heatmap-grid { display: grid; gap: 3px; min-width: max-content; }
.heatmap-cell {
  width: 44px; height: 36px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 600; cursor: default;
  transition: opacity 0.15s; position: relative;
}
.heatmap-cell:hover { opacity: 0.85; }
.heatmap-label {
  font-size: 11px; color: var(--muted); white-space: nowrap;
  display: flex; align-items: center; padding: 0 4px;
}
.heatmap-header {
  font-size: 10px; color: var(--muted); text-align: center; font-weight: 600;
}

/* ===== CATEGORY FILTER PILLS ===== */
.cat-filter-pills { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.cat-filter-pill {
  padding: 5px 12px; border-radius: 999px; font-size: 12px; font-weight: 600;
  border: 1px solid var(--border); background: var(--bg-3); color: var(--muted);
  cursor: pointer; transition: all 0.15s;
}
.cat-filter-pill.active { border-color: currentColor; background: rgba(255,255,255,0.06); }

/* ===== MOBILE BOTTOM NAV ===== */
.bottom-nav {
  display: none;
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--bg-1); border-top: 1px solid var(--border);
  padding: 8px 0 max(8px, env(safe-area-inset-bottom));
  z-index: 200;
}
.bottom-nav-items { display: flex; justify-content: space-around; }
.bottom-nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  color: var(--muted); font-size: 10px; font-weight: 600;
  text-decoration: none; cursor: pointer; background: none; border: none;
  font-family: var(--font); padding: 4px 8px; border-radius: 8px;
  transition: color 0.15s;
}
.bottom-nav-item.active, .bottom-nav-item:hover { color: var(--violet); }
.bottom-nav-item svg { width: 20px; height: 20px; }

/* ===== ERROR BANNER ===== */
.error-banner {
  display: none;
  align-items: center; gap: 16px;
  background: rgba(240,91,110,0.1); border: 1px solid rgba(240,91,110,0.3);
  border-radius: var(--radius-sm); padding: 12px 18px; margin: 16px 48px 0;
  font-size: 13px; color: var(--muted-light);
}
.error-banner.visible { display: flex; }

/* ===== ARCHIVIO MESI ===== */
.archivio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
  margin-bottom: 32px;
}
.archivio-card {
  background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 24px; transition: border-color 0.2s, transform 0.15s;
}
.archivio-card:hover { border-color: var(--border-light); transform: translateY(-2px); }
.archivio-month-label { font-size: 15px; font-weight: 700; letter-spacing: -0.2px; color: var(--muted-light); margin-bottom: 6px; }
.archivio-total { font-size: 26px; font-weight: 800; letter-spacing: -0.8px; color: var(--white); margin-bottom: 4px; }
.archivio-meta { font-size: 12px; color: var(--muted); margin-bottom: 20px; min-height: 18px; line-height: 1.5; }
.archivio-actions { display: flex; flex-direction: column; gap: 8px; }
.archivio-edit { width: 100%; padding: 10px 14px; font-size: 13px; justify-content: center; }
.archivio-actions-row2 { display: flex; gap: 8px; }
.archivio-delete-btn, .archivio-dup-btn {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 5px;
  background: var(--bg-3); border: 1px solid var(--border-light);
  color: var(--muted); padding: 9px 10px; border-radius: var(--radius-sm);
  font-family: var(--font); font-size: 12px; font-weight: 600;
  cursor: pointer; transition: all 0.18s; white-space: nowrap; text-decoration: none;
}
.archivio-delete-btn:hover { color: var(--red); border-color: var(--red); background: var(--red-dim); }
.archivio-dup-btn:hover { color: var(--violet); border-color: var(--violet); background: var(--violet-glow); }

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: var(--bg-0); }
::-webkit-scrollbar-thumb { background: var(--violet); border-radius: 4px; }

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .main { margin-left: 0; }
  .page { padding: 20px 16px 80px; }
  .bottom-nav { display: block; }
  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi-card { padding: 16px 14px 12px; }
  .kpi-value { font-size: 17px; letter-spacing: -0.3px; }
  .kpi-label { font-size: 9.5px; letter-spacing: 0.5px; }
  .kpi-badge { width: 28px; height: 28px; font-size: 13px; top: 14px; right: 12px; }
  .kpi-sub { font-size: 11px; padding: 4px 9px; }
  .charts-row.two-col { grid-template-columns: 1fr; }
  .charts-row.three-col { grid-template-columns: 1fr; }
  .charts-row { flex-direction: column; }
  .header { flex-direction: column; }
  .selectors { width: 100%; }
  .selector { flex: 1; }
}
