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

:root {
  --primary: #1565C0;
  --primary-light: #42A5F5;
  --primary-dark: #0D47A1;
  --accent: #00BCD4;
  --white: #FFFFFF;
  --gray-50: #F8FAFC;
  --gray-100: #F1F5F9;
  --gray-200: #E2E8F0;
  --gray-300: #CBD5E1;
  --gray-400: #94A3B8;
  --gray-500: #64748B;
  --gray-600: #475569;
  --gray-700: #334155;
  --gray-800: #1E293B;
  --gray-900: #0F172A;
  --success: #10B981;
  --success-light: #D1FAE5;
  --warning: #F59E0B;
  --warning-light: #FEF3C7;
  --danger: #EF4444;
  --danger-light: #FEE2E2;
  --info: #3B82F6;
  --info-light: #DBEAFE;
  --sidebar-w: 260px;
  --topbar-h: 64px;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -2px rgba(0,0,0,.1);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -4px rgba(0,0,0,.1);
  --radius: 12px;
  --radius-sm: 8px;
}

html { scroll-behavior: smooth; }
body {
  font-family: 'Inter', sans-serif;
  background: var(--gray-100);
  color: var(--gray-700);
  line-height: 1.5;
}

/* LOGIN SCREEN */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #EBF5FB 0%, #E8F8F5 50%, #F0F4FF 100%);
  padding: 20px;
}
.login-card {
  background: white;
  border-radius: 24px;
  padding: 48px 40px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
}
.login-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: center;
  margin-bottom: 8px;
}
.login-logo h1 {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--primary);
}
.logo-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1.3rem;
}
.login-subtitle {
  text-align: center;
  color: var(--gray-500);
  margin-bottom: 32px;
  font-size: .95rem;
}
.login-footer {
  text-align: center;
  margin-top: 24px;
}
.login-footer a {
  color: var(--gray-500);
  text-decoration: none;
  font-size: .9rem;
  transition: color .2s;
}
.login-footer a:hover { color: var(--primary); }
.login-demo {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--gray-200);
  text-align: center;
}
.login-demo p { font-size: .85rem; color: var(--gray-500); margin-bottom: 12px; }
.demo-accounts { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.demo-btn {
  padding: 6px 14px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: var(--gray-50);
  cursor: pointer;
  font-size: .8rem;
  font-family: inherit;
  color: var(--gray-600);
  transition: all .2s;
  display: flex;
  align-items: center;
  gap: 6px;
}
.demo-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--info-light);
}

/* FORM */
.form-group { margin-bottom: 20px; }
.form-group label {
  display: block;
  font-weight: 600;
  font-size: .85rem;
  color: var(--gray-700);
  margin-bottom: 6px;
}
.form-group label i { margin-right: 4px; color: var(--gray-400); }
.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 11px 14px;
  border: 2px solid var(--gray-200);
  border-radius: var(--radius-sm);
  font-size: .95rem;
  font-family: inherit;
  transition: border-color .2s;
  background: white;
  color: var(--gray-800);
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.form-group textarea { resize: vertical; min-height: 80px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ALERTS */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: .9rem;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.alert-danger { background: var(--danger-light); color: #B91C1C; }
.alert-success { background: var(--success-light); color: #065F46; }
.alert-warning { background: var(--warning-light); color: #92400E; }
.alert-info { background: var(--info-light); color: #1E40AF; }

/* BUTTONS */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 22px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: .9rem;
  font-family: inherit;
  text-decoration: none;
  border: none;
  cursor: pointer;
  transition: all .2s;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-outline { background: transparent; color: var(--primary); border: 2px solid var(--primary); }
.btn-outline:hover { background: var(--primary); color: white; }
.btn-ghost { background: transparent; color: var(--gray-600); }
.btn-ghost:hover { background: var(--gray-100); }
.btn-sm { padding: 6px 14px; font-size: .8rem; }
.btn-block { width: 100%; justify-content: center; }
.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--gray-500);
  font-size: 1.1rem;
  padding: 8px;
  border-radius: var(--radius-sm);
  transition: .2s;
}
.btn-icon:hover { background: var(--gray-100); color: var(--gray-800); }

/* DASHBOARD LAYOUT */
.dashboard {
  display: flex;
  min-height: 100vh;
}

/* SIDEBAR */
.sidebar {
  width: var(--sidebar-w);
  background: var(--gray-900);
  color: white;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
  transition: transform .3s;
}
.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar-logo {
  height: 180px;
  width: auto;
  filter: brightness(0) invert(1);
}
.sidebar-close {
  display: none;
  background: none;
  border: none;
  color: white;
  font-size: 1.2rem;
  cursor: pointer;
  margin-left: auto;
}
.sidebar-nav {
  flex: 1;
  padding: 16px 12px;
  overflow-y: auto;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  color: var(--gray-400);
  text-decoration: none;
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all .2s;
  margin-bottom: 2px;
}
.nav-item:hover { background: rgba(255,255,255,.06); color: white; }
.nav-item.active { background: var(--primary); color: white; }
.nav-item i { width: 20px; text-align: center; font-size: .95rem; }
.nav-section {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--gray-500);
  padding: 20px 14px 8px;
  font-weight: 700;
}

.sidebar-user {
  padding: 16px;
  border-top: 1px solid rgba(255,255,255,.08);
  display: flex;
  align-items: center;
  gap: 10px;
}
.user-avatar {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary-light), var(--accent));
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: .9rem;
}
.user-info { flex: 1; min-width: 0; }
.user-name { font-weight: 600; font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: .75rem; color: var(--gray-400); text-transform: capitalize; }
.sidebar-user .btn-icon { color: var(--gray-400); }
.sidebar-user .btn-icon:hover { color: white; background: rgba(255,255,255,.1); }

/* MAIN */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-w);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.topbar {
  height: var(--topbar-h);
  background: white;
  border-bottom: 1px solid var(--gray-200);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.sidebar-toggle { display: none; }
.page-title { font-size: 1.2rem; font-weight: 700; color: var(--gray-900); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.paziente-selector select {
  padding: 8px 12px;
  border: 2px solid var(--gray-200);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: .85rem;
  background: white;
  cursor: pointer;
}
.paziente-selector select:focus { outline: none; border-color: var(--primary); }

.content {
  flex: 1;
  padding: 24px;
}

/* CARDS & WIDGETS */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}
.stat-card {
  background: white;
  border-radius: var(--radius);
  padding: 24px;
  border: 1px solid var(--gray-200);
  display: flex;
  align-items: flex-start;
  gap: 16px;
}
.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}
.stat-info h3 { font-size: 1.8rem; font-weight: 800; color: var(--gray-900); line-height: 1; }
.stat-info p { font-size: .85rem; color: var(--gray-500); margin-top: 4px; }

.card {
  background: white;
  border-radius: var(--radius);
  border: 1px solid var(--gray-200);
  margin-bottom: 24px;
}
.card-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--gray-200);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.card-header h3 { font-size: 1rem; font-weight: 700; color: var(--gray-900); }
.card-body { padding: 24px; }

/* PILLS TODAY */
.pills-timeline { display: flex; flex-direction: column; gap: 12px; }
.pill-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--gray-200);
  background: var(--gray-50);
  transition: .2s;
}
.pill-item:hover { border-color: var(--primary-light); }
.pill-time {
  font-weight: 700;
  font-size: .95rem;
  color: var(--gray-900);
  min-width: 56px;
}
.pill-scomparto {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: .85rem;
  color: white;
}
.pill-info { flex: 1; min-width: 0; }
.pill-name { font-weight: 600; font-size: .9rem; color: var(--gray-900); }
.pill-molecule { font-size: .8rem; color: var(--gray-500); }
.pill-status {
  padding: 4px 12px;
  border-radius: 50px;
  font-size: .75rem;
  font-weight: 700;
}
.pill-status.presa { background: var(--success-light); color: #065F46; }
.pill-status.mancata { background: var(--danger-light); color: #B91C1C; }
.pill-status.attesa { background: var(--warning-light); color: #92400E; }

/* TABLE */
.table-wrapper { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}
th {
  text-align: left;
  padding: 12px 16px;
  font-weight: 600;
  color: var(--gray-500);
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .5px;
  border-bottom: 2px solid var(--gray-200);
}
td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--gray-100);
  color: var(--gray-700);
}
tr:hover td { background: var(--gray-50); }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 50px;
  font-size: .75rem;
  font-weight: 600;
}
.badge-success { background: var(--success-light); color: #065F46; }
.badge-danger { background: var(--danger-light); color: #B91C1C; }
.badge-warning { background: var(--warning-light); color: #92400E; }
.badge-info { background: var(--info-light); color: #1E40AF; }

/* DEVICE STATUS */
.device-card {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}
.device-item {
  background: var(--gray-50);
  border-radius: var(--radius-sm);
  padding: 18px;
  text-align: center;
  border: 1px solid var(--gray-200);
}
.device-item .value {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--gray-900);
  margin-bottom: 4px;
}
.device-item .label { font-size: .8rem; color: var(--gray-500); }
.online-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 6px;
}
.online-dot.on { background: var(--success); box-shadow: 0 0 6px rgba(16,185,129,.5); }
.online-dot.off { background: var(--danger); }

/* CHART */
.chart-container {
  position: relative;
  height: 300px;
  width: 100%;
}

/* CALENDAR */
.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.calendar-header h4 { font-weight: 700; color: var(--gray-900); }
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}
.cal-day-name {
  text-align: center;
  font-size: .75rem;
  font-weight: 700;
  color: var(--gray-500);
  padding: 8px;
  text-transform: uppercase;
}
.cal-day {
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  font-size: .85rem;
  font-weight: 600;
  cursor: default;
  border: 1px solid transparent;
  gap: 2px;
}
.cal-day.today { border-color: var(--primary); background: var(--info-light); }
.cal-day .cal-dots { display: flex; gap: 3px; }
.cal-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}
.cal-dot.green { background: var(--success); }
.cal-dot.red { background: var(--danger); }
.cal-dot.yellow { background: var(--warning); }

/* MODAL */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}
.modal {
  background: white;
  border-radius: 20px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 32px;
}
.modal h3 {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--gray-900);
  margin-bottom: 20px;
}
.modal-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 24px; }

/* SOS */
.sos-list { display: flex; flex-direction: column; gap: 12px; }
.sos-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--gray-50);
  border-radius: var(--radius-sm);
  border: 1px solid var(--gray-200);
}
.sos-item input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--gray-200);
  border-radius: 6px;
  font-family: inherit;
  font-size: .9rem;
}
.sos-item input:focus { outline: none; border-color: var(--primary); }

/* EMPTY STATE */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--gray-400);
}
.empty-state i { font-size: 3rem; margin-bottom: 16px; }
.empty-state h3 { font-size: 1.1rem; color: var(--gray-600); margin-bottom: 8px; }
.empty-state p { font-size: .9rem; }

/* LOADING */
.loading-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* RESPONSIVE */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: block; }
  .sidebar-toggle { display: block; }
  .main-content { margin-left: 0; }
  .content { padding: 16px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .form-row { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .login-card { padding: 32px 24px; }
}
