:root {
  --granata: #7A1F2E;
  --granata-dark: #5A1521;
  --granata-light: #A0324A;
  --white: #ffffff;
  --gray-50: #fafafa;
  --gray-100: #f4f4f5;
  --gray-200: #e4e4e7;
  --gray-300: #d4d4d8;
  --gray-500: #71717a;
  --gray-700: #3f3f46;
  --gray-900: #18181b;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #f59e0b;
  --radius: 12px;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(122,31,46,0.15);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ===== TEMA SCURO ===== */
:root[data-theme="dark"] {
  --granata: #A0324A;
  --granata-dark: #7A1F2E;
  --granata-light: #C04A62;
  --white: #1a1a1a;
  --gray-50: #0f0f0f;
  --gray-100: #1a1a1a;
  --gray-200: #2a2a2a;
  --gray-300: #3a3a3a;
  --gray-500: #a0a0a0;
  --gray-700: #d0d0d0;
  --gray-900: #f0f0f0;
  --success: #22c55e;
  --danger: #ef4444;
  --warning: #f59e0b;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  width: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--gray-50);
  color: var(--gray-900);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

html::-webkit-scrollbar { display: none; }
html { -ms-overflow-style: none; scrollbar-width: none; }

#app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  width: 100%;
  overflow-x: hidden;
}

/* ===== HEADER ===== */
.app-header {
  background: linear-gradient(135deg, var(--granata), var(--granata-dark));
  color: var(--white);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-left { display: flex; align-items: center; gap: 12px; }
.logo { width: 40px; height: 40px; }
.brand h1 { font-size: 16px; font-weight: 700; }
.brand .subtitle { font-size: 12px; opacity: 0.85; }
.header-right { display: flex; align-items: center; gap: 8px; }
.user-name { font-size: 13px; opacity: 0.9; }
.icon-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  color: var(--white);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: rgba(255,255,255,0.25); }

.theme-toggle {
  background: rgba(255,255,255,0.15);
  border: none;
  color: var(--white);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}
.theme-toggle:hover {
  background: rgba(255,255,255,0.25);
  transform: rotate(180deg);
}

.view {
  flex: 1;
  padding: 16px;
  padding-bottom: calc(100px + var(--safe-bottom));
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  overflow-x: hidden;
}

/* ===== BOTTOM NAV - AREA DI TOCCO INGRANDITA ===== */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--white);
  border-top: 1px solid var(--gray-200);
  display: flex;
  justify-content: space-around;
  padding: 12px 2px calc(16px + var(--safe-bottom));
  z-index: 99;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
}
.nav-item {
  flex: 1;
  background: none;
  border: none;
  padding: 12px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--gray-500);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  min-width: 0;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-height: 56px;
}
.nav-item .nav-icon { 
  font-size: 26px;
  line-height: 1;
  display: block;
}
.nav-item.active { 
  color: var(--granata); 
  font-weight: 700; 
}
.nav-item:active {
  transform: scale(0.92);
  opacity: 0.9;
}

/* ===== BUTTONS ===== */
.btn {
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
}
.btn-primary { background: var(--granata); color: var(--white); }
.btn-primary:hover { background: var(--granata-dark); }
.btn-secondary { background: var(--white); color: var(--granata); border: 2px solid var(--granata); }
.btn-secondary:hover { background: var(--granata); color: var(--white); }
.btn-ghost { background: transparent; color: var(--granata); }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== FORMS ===== */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--gray-700);
}
.form-control {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  font-size: 15px;
  background: var(--white);
  color: var(--gray-900);
  transition: border-color 0.2s;
  min-height: 44px;
}
.form-control:focus {
  outline: none;
  border-color: var(--granata);
  box-shadow: 0 0 0 3px rgba(122,31,46,0.1);
}
select.form-control { 
  appearance: none; 
  background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3e%3cpath fill='%2371717a' d='M6 9L1 4h10z'/%3e%3c/svg%3e"); 
  background-repeat: no-repeat; 
  background-position: right 12px center; 
  padding-right: 36px; 
}

/* ===== CARD ===== */
.card {
  background: var(--white);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.card-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 12px;
  color: var(--granata);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ===== LOGIN - SFONDO GRANATA SOLO QUI ===== */
.login-container {
  height: 100vh;
  height: 100dvh;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: linear-gradient(180deg, #7A1F2E 0%, #6B1A27 100%);
  overflow: hidden;
  position: relative;
}

.login-box {
  background: var(--white);
  border-radius: 20px;
  padding: 32px 24px;
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  margin-top: 50px;
}

.login-logo {
  width: 80px;
  height: 80px;
  margin: 0 auto 16px;
  display: block;
}
.login-title {
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  color: var(--granata);
  margin-bottom: 4px;
}
.login-subtitle {
  text-align: center;
  font-size: 14px;
  color: var(--gray-500);
  margin-bottom: 24px;
}
.role-tabs {
  display: flex;
  background: var(--gray-100);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 20px;
}
.role-tab {
  flex: 1;
  padding: 10px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  color: var(--gray-500);
  min-height: 44px;
}
.role-tab.active {
  background: var(--white);
  color: var(--granata);
  box-shadow: var(--shadow);
}
.divider {
  text-align: center;
  margin: 16px 0;
  color: var(--gray-500);
  font-size: 13px;
  position: relative;
}
.divider::before, .divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 40%;
  height: 1px;
  background: var(--gray-200);
}
.divider::before { left: 0; }
.divider::after { right: 0; }
.btn-google {
  background: var(--white);
  color: var(--gray-900);
  border: 1px solid var(--gray-300);
  width: 100%;
}
.btn-google:hover { background: var(--gray-50); }

.login-links {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 20px;
  flex-wrap: nowrap;
}
.login-links .link {
  font-size: 13px;
  white-space: nowrap;
}
.login-links .separator {
  color: var(--gray-300);
  font-size: 12px;
}
.link { color: var(--granata); text-decoration: none; font-weight: 500; }
.link:hover { text-decoration: underline; }

/* ===== CALENDAR ===== */
.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.calendar-header h2 { font-size: 18px; color: var(--granata); text-transform: capitalize; }
.calendar-nav { display: flex; gap: 8px; }
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  background: var(--white);
  padding: 12px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.calendar-day-name {
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--gray-500);
  padding: 4px;
  text-transform: uppercase;
}
.calendar-day {
  aspect-ratio: 1;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 4px 2px;
  font-size: 13px;
  cursor: pointer;
  background: var(--gray-50);
  position: relative;
  transition: all 0.15s;
}
.calendar-day:hover { background: var(--gray-100); }
.calendar-day.other-month { opacity: 0.3; }
.calendar-day.today { background: var(--granata); color: var(--white); font-weight: 700; }
.calendar-day.has-event::after {
  content: '';
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--granata);
  position: absolute;
  bottom: 4px;
}
.calendar-day.today.has-event::after { background: var(--white); }

/* ===== TABLE ===== */
.table-wrap { overflow-x: auto; background: var(--white); border-radius: var(--radius); box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; font-size: 14px; border-bottom: 1px solid var(--gray-200); }
th { background: var(--granata); color: var(--white); font-weight: 600; font-size: 12px; text-transform: uppercase; }
tr:last-child td { border-bottom: none; }
tr:nth-child(even) td { background: var(--gray-50); }

/* ===== BADGE ===== */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge-success { background: #dcfce7; color: var(--success); }
.badge-danger { background: #fee2e2; color: var(--danger); }
.badge-warning { background: #fef3c7; color: var(--warning); }
.badge-granata { background: rgba(122,31,46,0.1); color: var(--granata); }

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 120px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--gray-900);
  color: var(--white);
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  opacity: 0;
  transition: all 0.3s;
  z-index: 1000;
  max-width: 90%;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ===== INSTALL BANNER ===== */
.install-banner {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  background: var(--white);
  padding: 8px 12px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 150;
  border-left: 4px solid var(--granata);
  max-width: 420px;
  margin: 0 auto;
}
.install-banner span { flex: 1; font-size: 12px; }

/* ===== UTILITIES ===== */
.hidden { display: none !important; }
.text-center { text-align: center; }
.text-granata { color: var(--granata); }
.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mb-2 { margin-bottom: 8px; }
.mb-4 { margin-bottom: 16px; }
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }

/* ===== LOADING ===== */
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--granata);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== ATTENDANCE ===== */
.attendance-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--gray-200);
}
.attendance-row:last-child { border-bottom: none; }
.attendance-buttons { display: flex; gap: 6px; }
.att-btn {
  padding: 8px 14px;
  border: 1px solid var(--gray-300);
  background: var(--white);
  color: var(--gray-900);
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  min-height: 40px;
}
.att-btn.active-presente { background: #dcfce7; color: var(--success); border-color: var(--success); }
.att-btn.active-assente { background: #fee2e2; color: var(--danger); border-color: var(--danger); }

/* ===== BOTTOM SHEET ===== */
.bottom-sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 199;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s;
}
.bottom-sheet-overlay.show { opacity: 1; visibility: visible; }

.bottom-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--white);
  border-radius: 20px 20px 0 0;
  padding: 16px 16px calc(24px + var(--safe-bottom));
  z-index: 200;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
  max-height: 70vh;
  overflow-y: auto;
}
.bottom-sheet.show { transform: translateY(0); }

.bottom-sheet-handle {
  width: 40px;
  height: 4px;
  background: var(--gray-300);
  border-radius: 2px;
  margin: 0 auto 16px;
}

.bottom-sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--gray-200);
}
.bottom-sheet-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--granata);
  display: flex;
  align-items: center;
  gap: 8px;
}
.bottom-sheet-close {
  background: var(--gray-100);
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--gray-700);
  min-height: 44px;
  min-width: 44px;
}
.bottom-sheet-close:hover { background: var(--gray-200); }

.bottom-sheet-content { display: flex; flex-direction: column; gap: 8px; }

.bottom-sheet-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--gray-50);
  border: 2px solid transparent;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.bottom-sheet-item:hover {
  background: rgba(122, 31, 46, 0.05);
  border-color: var(--granata);
  transform: translateX(4px);
}
.bottom-sheet-item:active { transform: translateX(4px) scale(0.98); }
.bottom-sheet-item-icon { font-size: 24px; width: 32px; text-align: center; }
.bottom-sheet-item-label { font-size: 15px; font-weight: 600; color: var(--gray-900); flex: 1; }

.nav-item-gear { position: relative; }
.nav-item-gear.active { color: var(--granata); }
.nav-item-gear .nav-icon { transition: transform 0.3s; }
.nav-item-gear.open .nav-icon { transform: rotate(90deg); }

/* ===== DARK MODE ===== */
:root[data-theme="dark"] .calendar-day { background: var(--gray-200); }
:root[data-theme="dark"] .calendar-day:hover { background: var(--gray-300); }
:root[data-theme="dark"] .calendar-day.today { background: var(--granata); color: var(--white); }
:root[data-theme="dark"] .badge-success { background: rgba(34,197,94,0.2); }
:root[data-theme="dark"] .badge-danger { background: rgba(239,68,68,0.2); }
:root[data-theme="dark"] .badge-warning { background: rgba(245,158,11,0.2); }
:root[data-theme="dark"] .badge-granata { background: rgba(160,50,74,0.2); }
:root[data-theme="dark"] .btn-secondary { background: var(--gray-200); color: var(--granata-light); border-color: var(--granata-light); }
:root[data-theme="dark"] .btn-secondary:hover { background: var(--granata); color: var(--white); }
:root[data-theme="dark"] .btn-google { background: var(--gray-200); color: var(--gray-900); border-color: var(--gray-300); }
:root[data-theme="dark"] .btn-google:hover { background: var(--gray-300); }
:root[data-theme="dark"] .role-tab.active { background: var(--gray-100); color: var(--granata-light); }
:root[data-theme="dark"] .install-banner { border-left-color: var(--granata-light); }
:root[data-theme="dark"] .nav-item.active { color: var(--granata-light); }
:root[data-theme="dark"] .bottom-sheet { background: var(--gray-100); }
:root[data-theme="dark"] .bottom-sheet-item { background: var(--gray-200); }
:root[data-theme="dark"] .bottom-sheet-item:hover { background: rgba(160, 50, 74, 0.15); border-color: var(--granata-light); }
:root[data-theme="dark"] .bottom-sheet-close { background: var(--gray-200); color: var(--gray-900); }
:root[data-theme="dark"] .bottom-sheet-header { border-bottom-color: var(--gray-300); }

/* ===== MOBILE ===== */
@media (max-width: 480px) {
  .view { padding: 12px; padding-bottom: calc(110px + var(--safe-bottom)); }
  .calendar-day { font-size: 11px; }
  th, td { padding: 8px 6px; font-size: 12px; }
  
  /* NAV MOBILE - TOCCO ANCORA PIÙ GRANDE */
  .bottom-nav { 
    padding: 10px 0 calc(14px + var(--safe-bottom)); 
  }
  .nav-item { 
    font-size: 10px; 
    padding: 10px 2px;
    min-height: 60px; 
    gap: 4px;
  }
  .nav-item .nav-icon { 
    font-size: 24px; 
  }
  
  .bottom-sheet-item { padding: 12px 14px; }
  .bottom-sheet-item-icon { font-size: 22px; width: 28px; }
  .bottom-sheet-item-label { font-size: 14px; }
  
  /* LOGIN COMPATTO SU MOBILE */
  .login-box {
    padding: 20px 16px;
    margin-top: 60px;
    max-height: 80vh;
    max-height: 80dvh;
  }
  .login-logo {
    width: 60px;
    height: 60px;
    margin-bottom: 12px;
  }
  .login-title {
    font-size: 18px;
    margin-bottom: 2px;
  }
  .login-subtitle {
    font-size: 12px;
    margin-bottom: 16px;
  }
  .role-tabs {
    margin-bottom: 16px;
  }
  .role-tab {
    padding: 8px;
    font-size: 13px;
    min-height: 40px;
  }
  .form-group {
    margin-bottom: 12px;
  }
  .form-control {
    padding: 10px 12px;
    font-size: 14px;
    min-height: 40px;
  }
  .btn {
    padding: 10px 16px;
    font-size: 14px;
    min-height: 40px;
  }
  .divider {
    margin: 12px 0;
    font-size: 12px;
  }
  .login-links {
    margin-top: 16px;
    gap: 8px;
  }
  .login-links .link {
    font-size: 11px;
  }
  .login-links .separator {
    font-size: 10px;
  }
  
  /* Banner install più piccolo */
  .install-banner {
    padding: 6px 10px;
    top: 8px;
    left: 8px;
    right: 8px;
  }
  .install-banner span {
    font-size: 11px;
  }
  .install-banner .btn {
    padding: 6px 12px;
    font-size: 12px;
    min-height: 32px;
  }
}

@media (max-width: 360px) {
  .nav-item { font-size: 9px; padding: 4px 1px; }
  .nav-item .nav-icon { font-size: 20px; }
  .bottom-sheet-item { padding: 10px 12px; }
  .bottom-sheet-item-icon { font-size: 20px; width: 26px; }
  .bottom-sheet-item-label { font-size: 13px; }
  
  .login-box {
    padding: 16px 12px;
    margin-top: 65px;
  }
  .login-title { font-size: 16px; }
  .login-links .link { font-size: 10px; }
}

/* ===== DESKTOP ===== */
@media (min-width: 768px) {
  .login-box {
    max-width: 480px;
    padding: 40px 32px;
  }
  .login-title { font-size: 26px; }
  .login-logo { width: 100px; height: 100px; }
}

@media (min-width: 1024px) {
  .login-box { max-width: 520px; }
}