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

:root {
  --bg: #0f172a;
  --bg-card: #1e293b;
  --bg-secondary: #1e293b;
  --bg-input: #0f172a;
  --border: #334155;
  --text: #f1f5f9;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --income: #10b981;
  --expense: #ef4444;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

/* ===== AUTH PAGE ===== */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 50%, #0f172a 100%);
  padding: 20px;
}

.auth-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
}

.auth-logo {
  text-align: center;
  margin-bottom: 32px;
}

.auth-logo .logo-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 12px;
}

.auth-logo h1 {
  font-size: 24px;
  font-weight: 700;
  background: linear-gradient(135deg, #6366f1, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.auth-logo p { color: var(--text-muted); font-size: 14px; margin-top: 4px; }

.auth-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-input);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 28px;
}

.auth-tab {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.auth-tab.active {
  background: var(--primary);
  color: white;
  box-shadow: 0 2px 8px rgba(99,102,241,0.4);
}

.auth-form { display: none; flex-direction: column; gap: 16px; }
.auth-form.active { display: flex; }

.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: 13px; font-weight: 500; color: var(--text-muted); }

.form-group input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  color: var(--text);
  font-size: 15px;
  transition: border-color 0.2s, box-shadow 0.2s;
  outline: none;
}

.form-group input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,0.15);
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover { background: var(--primary-hover); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(99,102,241,0.4); }
.btn-primary:active { transform: translateY(0); }

.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: #059669; }

.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #dc2626; }

.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg-card); color: var(--text); }

.btn-sm { padding: 7px 14px; font-size: 13px; }

.alert {
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
  display: none;
}

.alert.show { display: block; }
.alert-error { background: rgba(239,68,68,0.1); border: 1px solid rgba(239,68,68,0.3); color: #fca5a5; }
.alert-success { background: rgba(16,185,129,0.1); border: 1px solid rgba(16,185,129,0.3); color: #6ee7b7; }

/* ===== LAYOUT DASHBOARD ===== */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ===== SIDEBAR ===== */
.sidebar {
  width: 260px;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 24px 0;
  flex-shrink: 0;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 24px 28px;
  border-bottom: 1px solid var(--border);
}

.sidebar-logo .logo-emoji { font-size: 28px; }
.sidebar-logo h2 { font-size: 17px; font-weight: 700; color: var(--text); }
.sidebar-logo span { font-size: 11px; color: var(--text-muted); display: block; }

.sidebar-nav {
  flex: 1;
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
}

.nav-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 12px 12px 6px;
  font-weight: 600;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
  font-size: 14px;
  font-weight: 500;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.nav-item:hover { background: rgba(99,102,241,0.1); color: var(--text); }
.nav-item.active { background: rgba(99,102,241,0.15); color: var(--primary); }
.nav-item .nav-icon { font-size: 18px; width: 22px; text-align: center; }

.sidebar-user {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #a78bfa);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; color: white; flex-shrink: 0;
}

.user-info .user-name { font-size: 13px; font-weight: 600; }
.user-info .user-email { font-size: 11px; color: var(--text-muted); }

.btn-logout {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 4px;
  border-radius: 6px;
  transition: color 0.2s;
}

.btn-logout:hover { color: var(--danger); }

/* ===== MAIN CONTENT ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  padding: 20px 32px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  background: var(--bg-card);
}

.topbar h1 { font-size: 20px; font-weight: 700; }
.topbar-actions { display: flex; gap: 10px; align-items: center; }

.page-content {
  flex: 1;
  overflow-y: auto;
  padding: 28px 32px;
}

/* ===== STATS CARDS ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
  margin-bottom: 28px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: transform 0.2s, box-shadow 0.2s;
}

.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }

.stat-label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.stat-value {
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.stat-value.income { color: var(--income); }
.stat-value.expense { color: var(--expense); }
.stat-value.balance { color: var(--primary); }

.stat-sub { font-size: 12px; color: var(--text-muted); }

.stat-icon {
  width: 42px; height: 42px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  margin-bottom: 4px;
}

/* ===== CHARTS ===== */
.charts-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 20px;
  margin-bottom: 28px;
}

.chart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.chart-header h3 { font-size: 15px; font-weight: 600; }

.chart-container { position: relative; }

/* ===== TRANSACTIONS TABLE ===== */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.card-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.card-header h3 { font-size: 15px; font-weight: 600; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.filter-select, .filter-input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  color: var(--text);
  font-size: 13px;
  outline: none;
  cursor: pointer;
}

.filter-select:focus, .filter-input:focus { border-color: var(--primary); }

.tx-list { display: flex; flex-direction: column; }

.tx-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}

.tx-item:last-child { border-bottom: none; }
.tx-item:hover { background: rgba(255,255,255,0.02); }

.tx-icon {
  width: 40px; height: 40px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.tx-info { flex: 1; min-width: 0; }
.tx-desc { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.tx-amount { font-size: 15px; font-weight: 700; flex-shrink: 0; }
.tx-amount.income { color: var(--income); }
.tx-amount.expense { color: var(--expense); }

.tx-actions { display: flex; gap: 6px; }

.btn-icon {
  width: 30px; height: 30px;
  border: none; border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}

.btn-icon:hover { background: var(--border); color: var(--text); }
.btn-icon.delete:hover { background: rgba(239,68,68,0.15); color: var(--danger); }

.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.empty-state .empty-icon { font-size: 48px; margin-bottom: 16px; }
.empty-state p { font-size: 15px; }

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  backdrop-filter: blur(4px);
}

.modal-overlay.open { display: flex; }

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.modal-header {
  padding: 24px 28px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h2 { font-size: 18px; font-weight: 700; }

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
  width: 32px; height: 32px;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}

.modal-close:hover { background: var(--border); color: var(--text); }

.modal-body { padding: 24px 28px; display: flex; flex-direction: column; gap: 16px; }
.modal-footer { padding: 16px 28px 24px; display: flex; justify-content: flex-end; gap: 10px; }

/* ===== TYPE TOGGLE ===== */
.type-toggle {
  display: flex;
  gap: 4px;
  background: var(--bg-input);
  border-radius: 10px;
  padding: 4px;
}

.type-btn {
  flex: 1;
  padding: 9px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.type-btn.active.income { background: var(--income); color: white; }
.type-btn.active.expense { background: var(--expense); color: white; }

/* ===== BADGE ===== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.badge-income { background: rgba(16,185,129,0.15); color: var(--income); }
.badge-expense { background: rgba(239,68,68,0.15); color: var(--expense); }

/* ===== CATEGORY COLORS ===== */
.cat-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ===== UPLOAD ZONE ===== */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 40px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
}

.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--primary);
  background: rgba(99,102,241,0.05);
}

.upload-zone .upload-icon { font-size: 40px; margin-bottom: 12px; }
.upload-zone p { color: var(--text-muted); font-size: 14px; }
.upload-zone strong { color: var(--primary); }

/* ===== MONTH SELECTOR ===== */
.period-selector {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 12px;
}

.period-selector select {
  background: none;
  border: none;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  outline: none;
  cursor: pointer;
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #475569; }

/* ===== LOADING ===== */
.spinner {
  width: 20px; height: 20px;
  border: 2px solid rgba(255,255,255,0.2);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.4; transform: scale(0.7); } }

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
  .charts-grid { grid-template-columns: 1fr; }
}

/* ===== MOBILE SIDEBAR TOGGLE ===== */
#sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text);
  font-size: 24px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 8px;
  line-height: 1;
  flex-shrink: 0;
}
#sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 199;
  backdrop-filter: blur(2px);
}

/* ===== TABLET (≤ 900px) ===== */
@media (max-width: 900px) {
  .page-content { padding: 20px 20px; }
}

/* ===== MOBILE (≤ 768px) ===== */
@media (max-width: 768px) {
  /* Hamburger & backdrop */
  #sidebar-toggle { display: flex; }
  #sidebar-backdrop.open { display: block; }

  /* Sidebar → slide-in drawer */
  .sidebar {
    position: fixed;
    top: 0; left: -270px;
    height: 100%;
    z-index: 200;
    transition: left 0.25s ease;
    width: 260px;
    box-shadow: none;
  }
  .sidebar.open {
    left: 0;
    box-shadow: 8px 0 32px rgba(0,0,0,0.5);
  }

  /* App layout */
  .app { height: auto; min-height: 100svh; overflow: visible; }
  .main { overflow: visible; height: auto; min-height: 100svh; }

  /* Topbar */
  .topbar {
    padding: 12px 14px;
    position: sticky; top: 0; z-index: 100;
    flex-wrap: nowrap;
    gap: 8px;
  }
  .topbar h1 { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar-actions { gap: 6px; flex-shrink: 0; }
  .period-selector { padding: 4px 8px; gap: 4px; }
  .period-selector select { font-size: 12px; max-width: 80px; }

  /* Page content */
  .page-content { padding: 14px; overflow-x: hidden; height: auto; }

  /* Stats */
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
  .stat-card { padding: 14px; }
  .stat-value { font-size: 21px; }

  /* Cards */
  .card { overflow: visible; }
  .card-header { padding: 12px 14px; }

  /* Tables: horizontal scroll */
  .card > div[style*="overflow-x:auto"],
  .card > div[style*="overflow-x: auto"] { -webkit-overflow-scrolling: touch; }
  #timbrature-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  #timbrature-table-wrap table { min-width: 560px; }

  /* Payroll tabs: scrollable row */
  #page-payroll > div:first-child {
    width: 100% !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  #page-payroll > div:first-child > button { flex-shrink: 0; }

  /* Payroll turni 2-col → 1-col */
  #payroll-tab-turni-content > div[style] { grid-template-columns: 1fr !important; }

  /* Payroll accessi 2-col → 1-col */
  #payroll-tab-accessi-content > div[style*="grid"] { grid-template-columns: 1fr !important; }

  /* Magazzino 2-col grid */
  #mag-grid { grid-template-columns: 1fr !important; }

  /* Modals */
  .modal-overlay { padding: 12px; }
  .modal { border-radius: 16px; }
  .modal-body { padding: 16px 18px; }
  .modal-footer { padding: 12px 18px 16px; }
  .modal-header { padding: 16px 18px 14px; }
}

/* ===== SMALL MOBILE (≤ 480px) ===== */
@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .period-selector { display: none; }
  .topbar h1 { font-size: 15px; }
  .page-content { padding: 10px; }
}

/* ===== FATTURE — Variazioni prezzo ===== */
.inv-price-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(239, 68, 68, 0.12) !important;
  color: #ef4444 !important;
  border: none;
  border-radius: 4px;
  padding: 1px 5px !important;
  font-size: 12px !important;
  cursor: pointer;
  vertical-align: middle;
  transition: background 0.15s;
  line-height: 1.4;
}
.inv-price-btn:hover,
.inv-price-btn.active {
  background: rgba(239, 68, 68, 0.28) !important;
}
