/* ============================================================
   style.css
   Design System "CiulloTracker"
   Minimalista, monocromatico, elementi Squircle, Mobile-First
   ============================================================ */

/* ---------- Reset di base ---------- */
* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  min-width: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* ---------- Palette monocromatica (tema chiaro di default) ---------- */
:root {
  --bg: #f4f4f5;
  --surface: #ffffff;
  --surface-alt: #f0f0f1;
  --text: #0a0a0a;
  --text-muted: #6b6b70;
  --border: #e3e3e6;
  --accent: #0a0a0a;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 8px 24px rgba(0,0,0,0.05);
  --radius-lg: 28px;
  --radius-md: 20px;
  --radius-sm: 14px;
  --nav-w: 240px;
}

[data-theme="dark"] {
  --bg: #0a0a0b;
  --surface: #161618;
  --surface-alt: #1e1e21;
  --text: #f5f5f5;
  --text-muted: #9a9aa0;
  --border: #2a2a2e;
  --accent: #ffffff;
  --accent-text: #0a0a0a;
  --danger: #ff6b60;
  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px rgba(0,0,0,0.4);
}

/* Direzione visiva comune: bianco, nero e movimento discreto. */
:root,
[data-theme="dark"] {
  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --accent: #111111;
  --accent-text: #ffffff;
  --danger: #333333;
}

[data-theme="dark"] {
  --accent: #f5f5f5;
  --accent-text: #0a0a0a;
  --danger: #f5f5f5;
}

body {
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  background-image: radial-gradient(circle at 1px 1px, rgba(0,0,0,0.04) 1px, transparent 0);
  background-size: 28px 28px;
}

[data-theme="dark"] body {
  background-image: radial-gradient(circle at 1px 1px, rgba(255,255,255,0.06) 1px, transparent 0);
}

body > .app-shell { animation: page-enter 420ms ease both; }

@keyframes page-enter {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.chart-card canvas,
#chart-modal-canvas {
  filter: saturate(1.18) contrast(1.04);
}

a { color: inherit; text-decoration: none; }
ul { list-style: none; }

/* ---------- Utility "Squircle" ----------
   Approssimazione dell'angolo continuo in stile iOS tramite
   border-radius elevato + curva su tutti gli elementi chiave. */
.squircle {
  border-radius: var(--radius-md);
}
.squircle-lg { border-radius: var(--radius-lg); }
.squircle-sm { border-radius: var(--radius-sm); }

/* ============================================================
   LAYOUT APP (Sidebar desktop / Hamburger mobile)
   ============================================================ */
.app-shell {
  display: flex;
  min-height: 100vh;
}

/* --- Sidebar (desktop) / Drawer (mobile) --- */
.sidebar {
  width: var(--nav-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 24px 16px;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  z-index: 100;
}
.sidebar.open { transform: translateX(0); }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 24px;
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}
.brand-logo {
  width: 28px;
  height: 28px;
  display: inline-block;
  vertical-align: middle;
  object-fit: contain;
  /* Opzionale: adatta i colori al tema scuro/chiaro */
  filter: invert(var(--logo-invert, 0));
  transition: transform 0.2s ease;
}
.brand-logo:hover {
  transform: scale(1.1);
}

/* In tema scuro, se la favicon è nera, la inverti per renderla bianca */
[data-theme="dark"] .brand-logo {
  --logo-invert: 1;
}

.nav-links { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.92rem;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}
.nav-link:hover { background: var(--surface-alt); color: var(--text); }
.nav-link.active { background: var(--accent); color: var(--accent-text); }
.nav-link svg { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  font-size: 0.85rem;
}
.user-avatar {
  width: 30px; height: 30px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.8rem;
  text-transform: uppercase;
}
.role-badge {
  font-size: 0.68rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.04em;
}

/* --- Overlay per drawer mobile --- */
.sidebar-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 90;
}
.sidebar-overlay.show { display: block; }

/* --- Topbar mobile --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 80;
}
.hamburger-btn, .theme-toggle-btn {
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-alt);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text);
}
.hamburger-btn svg, .theme-toggle-btn svg { width: 20px; height: 20px; }

/* --- Contenuto principale --- */
.main-content {
  flex: 1;
  min-width: 0;
  width: 100%;
  padding: 20px 16px 40px;
}

.site-footer {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 18px 24px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.82rem;
}
.site-footer-brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 700;
}
.site-footer-logo { width: 20px; height: 20px; }
.site-footer-copy { flex: 1; }
.site-footer-logout-form { margin-left: auto; }
.site-footer-logout { display: inline-flex; align-items: center; gap: 7px; }
.site-footer-logout svg { width: 16px; height: 16px; }

/* Animazioni leggere per carte e elementi */
@keyframes card-pop {
  from { opacity: 0; transform: translateY(6px) scale(0.995); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.card { animation: card-pop 320ms cubic-bezier(.2,.9,.3,1) both; }
.expense-item { transition: transform 180ms ease, box-shadow 180ms ease; }
.expense-item:hover { transform: translateY(-4px); box-shadow: 0 6px 20px rgba(0,0,0,0.06); }

/* Segmented control per Spesa/Ingresso */
.segmented { display: inline-flex; gap: 6px; background: var(--surface-alt); padding: 4px; border-radius: 999px; }
.segmented-item { padding: 6px 10px; border-radius: 999px; font-weight:600; font-size:0.92rem; cursor:pointer; display:inline-flex; align-items:center; gap:8px; }
.segmented-item input { margin:0; accent-color: var(--accent); }
.segmented-item input[type=radio] { width:18px; height:18px; }

/* Stile fullscreen per grafici (fallback) */
.chart-card.fullscreen {
  position: fixed; inset: 12px; z-index: 9999; width: calc(100% - 24px); height: calc(100% - 24px);
  display: flex; flex-direction: column; padding: 20px; background: var(--surface);
}
.chart-card.fullscreen canvas { flex: 1; width: 100% !important; height: 100% !important; }


/* ============================================================
   COMPONENTI COMUNI
   ============================================================ */
.page-title {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}
.page-subtitle { color: var(--text-muted); font-size: 0.92rem; margin-bottom: 20px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 20px;
  box-shadow: var(--shadow);
}

/* Neutralizza gli accenti legacy nelle viste EJS. */
.kpi-value.positive,
.kpi-value.negative,
.expense-amount,
.increment-popup .popup-value.new.positive,
.increment-popup .popup-value.new.negative,
.qty-btn.inc,
.qty-btn.dec,
.badge-success,
.badge-danger,
.toast-notification.success,
.toast-notification.error {
  color: var(--text) !important;
}

.qty-btn.inc:hover,
.qty-btn.dec:hover,
.btn-danger:hover {
  background: var(--text) !important;
  color: var(--surface) !important;
}

.increment-popup .popup-confirm.positive,
.increment-popup .popup-confirm.negative {
  background: var(--accent) !important;
  color: var(--accent-text) !important;
}

.badge-success,
.badge-danger {
  background: var(--surface-alt) !important;
  border: 1px solid var(--border);
}

.grid { display: grid; gap: 16px; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }

@media (max-width: 800px) {
  .grid-3 { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
}

/* --- KPI cards --- */
.kpi-card { display: flex; flex-direction: column; gap: 6px; }
.kpi-label { font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-value { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.02em; }

/* --- Bottoni --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  border: none;
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { opacity: 0.85; }
.btn-secondary { background: var(--surface-alt); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-block { width: 100%; }
.btn-sm { padding: 8px 14px; font-size: 0.82rem; }

/* --- Form / Input --- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field label { font-size: 0.85rem; font-weight: 600; color: var(--text-muted); }
.input, select.input, textarea.input {
  width: 100%;
  padding: 13px 16px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
}
.input:focus, select.input:focus, textarea.input:focus {
  outline: none;
  border-color: var(--accent);
}
.input-amount { font-size: 1.6rem; font-weight: 700; text-align: center; }

.form-row { display: flex; gap: 12px; }
.form-row > .field { flex: 1; min-width: 0; }

/* --- Accordion "Avanzate" --- */
.accordion { border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; margin-bottom: 20px; }
.accordion-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  cursor: pointer;
  background: var(--surface-alt);
  font-weight: 600;
  font-size: 0.9rem;
}
.accordion-header svg { width: 18px; height: 18px; transition: transform 0.2s; }
.accordion-header.open svg { transform: rotate(180deg); }
.accordion-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.25s ease;
  padding: 0 18px;
}
.accordion-body.open { max-height: 500px; padding: 18px; }

/* --- Alert / messaggi --- */
.alert { padding: 14px 16px; border-radius: var(--radius-sm); font-size: 0.88rem; margin-bottom: 16px; }
.alert-error { background: rgba(179,38,30,0.08); color: var(--danger); border: 1px solid rgba(179,38,30,0.25); }
.alert-success { background: rgba(20,140,80,0.08); color: #148c50; border: 1px solid rgba(20,140,80,0.25); }
[data-theme="dark"] .alert-success { color: #4ade80; }

/* --- Lista spese (feed) --- */
.expense-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
}
.expense-item:last-child { margin-bottom: 0; }
.expense-meta { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.expense-cat {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}
.expense-name { font-weight: 600; font-size: 0.94rem; }
.expense-sub { font-size: 0.78rem; color: var(--text-muted); }
.expense-amount { font-weight: 700; font-size: 1rem; white-space: nowrap; }

/* --- Tabella storico --- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
}
table.data-table { width: 100%; border-collapse: collapse; min-width: 640px; }
table.data-table th, table.data-table td {
  text-align: left;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 0.87rem;
  white-space: nowrap;
}
table.data-table th {
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  background: var(--surface-alt);
}
table.data-table tr:last-child td { border-bottom: none; }

.filters-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
  align-items: flex-end;
}
.filters-bar .field { margin-bottom: 0; min-width: 140px; flex: 1; }

.export-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 20px; }

/* --- Chart container --- */
.chart-card { min-height: 320px; }
.chart-title { font-weight: 700; font-size: 0.95rem; margin-bottom: 14px; }

/* ============================================================
   LOGIN
   ============================================================ */
.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: var(--bg);
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 36px 28px;
  box-shadow: var(--shadow);
}
.login-logo {
  width: 52px; height: 52px;
  background: var(--accent);
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 20px;
  color: var(--accent-text);
  font-weight: 800;
  font-size: 1.3rem;
}
.login-title { text-align: center; font-size: 1.2rem; font-weight: 700; margin-bottom: 4px; }
.login-subtitle { text-align: center; color: var(--text-muted); font-size: 0.88rem; margin-bottom: 24px; }
.login-card .alert { overflow-wrap: anywhere; }

.new-expense-layout {
  display: grid;
  grid-template-columns: minmax(0, 480px) minmax(320px, 1fr);
  gap: 20px;
  align-items: start;
  max-width: 1080px;
}
.new-expense-form-card,
.recent-expenses-card { min-width: 0; }
.recent-expenses-card .chart-title { margin-bottom: 16px; }
.recent-expenses-link {
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 400;
  text-decoration: underline;
}

/* ============================================================
   RESPONSIVE: DESKTOP
   ============================================================ */
@media (min-width: 900px) {
  .sidebar { position: sticky; transform: none; }
  .sidebar-overlay { display: none !important; }
  .topbar { display: none; }
  .main-content { padding: 32px 40px 48px; }
  .hamburger-btn { display: none; }
}
@media (max-width: 899px) {
  .app-shell { display: block; }
  .main-content { padding: 20px 14px 32px; }
  .new-expense-layout { grid-template-columns: 1fr; }
  .grid-2,
  .grid-4 { grid-template-columns: 1fr !important; }
  .card { max-width: 100%; }
  .chart-card { min-width: 0; }
  .chart-modal-content { width: calc(100% - 24px); max-height: calc(100dvh - 24px); }
  .chart-modal-header { padding: 14px 16px; }
  .chart-modal-body { min-height: 0; padding: 16px; }
}
@media (max-width: 520px) {
  .page-title { font-size: 1.25rem; }
  .page-subtitle { margin-bottom: 16px; }
  .card { padding: 16px; }
  .form-row { flex-direction: column; gap: 0; }
  .btn { min-height: 44px; padding-inline: 16px; }
  .segmented { display: flex; width: 100%; }
  .segmented-item { flex: 1; justify-content: center; min-height: 42px; }
  .filters-bar { padding: 16px; }
  .filters-bar .field { min-width: 0; width: 100%; }
  .export-bar { padding: 12px 14px; }
  .export-bar .btn { flex: 1 1 140px; }
  .expense-item { align-items: flex-start; padding: 12px; }
  .expense-amount { margin-left: 8px; }
  .site-footer { padding: 16px; }
  .site-footer-copy { flex-basis: 100%; order: 3; }
  .site-footer-logout-form { margin-left: auto; }
  .site-footer-credits { flex-basis: 100%; order: 4; line-height: 1.5; }
  .login-screen { align-items: flex-start; padding: max(24px, env(safe-area-inset-top)) 12px 24px; }
  .login-card { padding: 28px 20px; margin-top: 4vh; }
  .login-logo { margin-bottom: 16px; }
}

/* Piccola utility per empty states */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* === MODAL GRAFICI === */
.chart-modal-overlay {
  position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  animation: fadeIn 0.2s ease-out;
}
.chart-modal-content {
  background: var(--bg-color, #ffffff);
  color: var(--text-color, #0a0a0a);
  width: 90%;
  max-width: 900px;
  max-height: 90vh;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: slideUp 0.3s ease-out;
}
.chart-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border-color, #e3e3e6);
}
.chart-modal-header h3 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
}
.chart-modal-close-btn {
  background: none;
  border: none;
  font-size: 2rem;
  cursor: pointer;
  color: var(--text-color, #0a0a0a);
  line-height: 1;
  padding: 0 8px;
  transition: transform 0.2s;
}
.chart-modal-close-btn:hover {
  transform: scale(1.2);
}
.chart-modal-body {
  flex: 1;
  padding: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 400px;
}
.chart-modal-body canvas {
  max-width: 100%;
  max-height: 65vh;
}
.chart-modal-footer {
  padding: 14px 24px;
  border-top: 1px solid var(--border-color, #e3e3e6);
  font-size: 0.9rem;
  color: var(--text-secondary, #6b6b70);
  text-align: center;
  background: rgba(0,0,0,0.02);
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* Pulsanti incremento/decremento */
.qty-btn-group {
    display: flex;
    gap: 4px;
    align-items: center;
}
.qty-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: var(--bg-secondary);
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.qty-btn:hover {
    background: var(--bg-hover);
    transform: scale(1.1);
}
.qty-btn:active {
    transform: scale(0.9);
}
.qty-btn.inc { color: #4ECDC4; }
.qty-btn.dec { color: #FF6B6B; }

/* Notifiche toast */
.toast-notification {
    position: fixed;
    bottom: 30px;
    right: 30px;
    padding: 12px 24px;
    border-radius: 12px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    font-weight: 500;
    z-index: 9999;
    animation: slideIn 0.3s ease;
    max-width: 90%;
}
.toast-notification.success {
    border-left: 4px solid #4ECDC4;
    color: #4ECDC4;
}
.toast-notification.error {
    border-left: 4px solid #FF6B6B;
    color: #FF6B6B;
}
.toast-notification.fade-out {
    animation: slideOut 0.3s ease forwards;
}

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}
@keyframes slideOut {
    from { transform: translateX(0); opacity: 1; }
    to { transform: translateX(100%); opacity: 0; }
}

/* Miglioramenti grafici generali */
.chart-card {
    transition: all 0.3s;
}
.chart-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0,0,0,0.1);
}