:root {
  --app-bg: #f4f7fb;
  --app-surface: #ffffff;
  --app-surface-soft: #f8fafc;
  --app-border: #e2e8f0;
  --app-text: #172033;
  --app-muted: #6b7280;
  --app-nav: #0f172a;
  --app-nav-2: #162238;
  --app-primary: #1d8cf8;
  --app-primary-dark: #0b6fd3;
  --app-danger: #dc3545;
  --app-success: #198754;
  --app-warning: #d97706;
  --app-radius: 16px;
  --app-shadow: 0 10px 30px rgba(15, 23, 42, .08);
}

html, body { min-height: 100%; }
body {
  background: var(--app-bg);
  color: var(--app-text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: .95rem;
}

a { text-decoration: none; }

.app-navbar {
  background: linear-gradient(135deg, var(--app-nav), var(--app-nav-2));
  box-shadow: 0 6px 20px rgba(15, 23, 42, .18);
}
.app-navbar .navbar-brand {
  display: flex;
  align-items: center;
  gap: .7rem;
  font-weight: 700;
  letter-spacing: -.02em;
}
.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: linear-gradient(135deg, #42a5ff, var(--app-primary));
  color: #fff;
  font-size: .78rem;
  font-weight: 800;
  box-shadow: 0 6px 16px rgba(29, 140, 248, .3);
}
.app-navbar .nav-link {
  border-radius: 9px;
  padding: .55rem .78rem !important;
  color: rgba(255,255,255,.72) !important;
  font-weight: 500;
}
.app-navbar .nav-link:hover,
.app-navbar .nav-link.active {
  background: rgba(255,255,255,.1);
  color: #fff !important;
}
.user-chip {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  padding: .4rem .7rem;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 999px;
  background: rgba(255,255,255,.06);
  color: #fff;
  font-size: .86rem;
}
.user-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #42d392;
  box-shadow: 0 0 0 3px rgba(66,211,146,.15);
}

.app-main { max-width: 1500px; }
.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.35rem;
}
.page-kicker {
  color: var(--app-primary-dark);
  text-transform: uppercase;
  letter-spacing: .08em;
  font-size: .73rem;
  font-weight: 800;
}
.page-title {
  margin: .15rem 0 .25rem;
  font-size: clamp(1.55rem, 2vw, 2rem);
  font-weight: 750;
  letter-spacing: -.035em;
}
.page-subtitle { margin: 0; color: var(--app-muted); }

.card,
.panel {
  border: 1px solid var(--app-border) !important;
  border-radius: var(--app-radius) !important;
  box-shadow: var(--app-shadow) !important;
  background: var(--app-surface);
}
.card-body { padding: 1.25rem; }
.panel-title {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -.015em;
}
.panel-subtitle { color: var(--app-muted); font-size: .84rem; }

.stat-card { position: relative; overflow: hidden; }
.stat-card::after {
  content: "";
  position: absolute;
  width: 110px;
  height: 110px;
  right: -44px;
  top: -52px;
  border-radius: 50%;
  background: rgba(29,140,248,.08);
}
.stat-label { color: var(--app-muted); font-weight: 600; font-size: .84rem; }
.stat-value { font-size: 2rem; font-weight: 800; letter-spacing: -.04em; line-height: 1.05; margin-top: .25rem; }
.stat-icon {
  display: inline-grid;
  place-items: center;
  min-width: 34px;
  height: 34px;
  padding: 0 .55rem;
  border-radius: 10px;
  background: rgba(29,140,248,.1);
  color: var(--app-primary-dark);
  font-size: .74rem;
  font-weight: 800;
}

.form-label { font-size: .84rem; font-weight: 650; color: #344054; }
.form-control, .form-select {
  border-radius: 10px;
  border-color: #d7dee9;
  min-height: 42px;
}
.form-control:focus, .form-select:focus {
  border-color: rgba(29,140,248,.72);
  box-shadow: 0 0 0 .22rem rgba(29,140,248,.12);
}
textarea.form-control { min-height: auto; }
.form-text { color: #8791a3; }
.form-check-input:checked { background-color: var(--app-primary); border-color: var(--app-primary); }
.form-section {
  padding: 1rem;
  border: 1px solid var(--app-border);
  border-radius: 12px;
  background: var(--app-surface-soft);
}
.form-section-title { font-size: .82rem; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; color: #526175; margin-bottom: .75rem; }

.btn { border-radius: 10px; font-weight: 650; }
.btn-primary { background: var(--app-primary); border-color: var(--app-primary); }
.btn-primary:hover { background: var(--app-primary-dark); border-color: var(--app-primary-dark); }
.btn-soft-primary { background: rgba(29,140,248,.09); color: var(--app-primary-dark); border: 1px solid rgba(29,140,248,.15); }
.btn-soft-primary:hover { background: rgba(29,140,248,.16); color: #075ba9; }
.btn-soft-danger { background: rgba(220,53,69,.07); color: #b02a37; border: 1px solid rgba(220,53,69,.15); }
.btn-soft-danger:hover { background: rgba(220,53,69,.13); color: #842029; }

.table { margin-bottom: 0; }
.table > :not(caption) > * > * { padding: .78rem .68rem; border-bottom-color: #edf1f6; }
.table thead th {
  background: #f8fafc;
  color: #667085;
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .045em;
  font-weight: 800;
  border-bottom: 1px solid var(--app-border);
  white-space: nowrap;
}
.table tbody tr:hover { background: #fbfdff; }
.table tbody tr:last-child td { border-bottom: 0; }
.table-responsive { border-radius: 12px; }
.person-name { font-weight: 700; color: #243247; }
.muted-code { color: #778398; font-size: .84rem; }
.note-cell { white-space: pre-wrap; min-width: 210px; color: #536177; font-size: .86rem; }

.check-badge,
.permission-badge,
.status-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  font-weight: 750;
  line-height: 1;
}
.check-badge { width: 24px; height: 24px; background: rgba(25,135,84,.1); color: var(--app-success); font-size: .78rem; }
.check-badge.off { background: #f1f3f6; color: #a2abb8; }
.permission-badge { padding: .32rem .55rem; background: rgba(29,140,248,.09); color: #0b6fc2; font-size: .72rem; }
.permission-badge.off { background: #f2f4f7; color: #98a2b3; }
.status-badge { padding: .38rem .62rem; font-size: .74rem; }
.status-badge.active { background: rgba(25,135,84,.1); color: #157347; }
.status-badge.inactive { background: rgba(220,53,69,.08); color: #b02a37; }

.empty-state {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--app-muted);
  border: 1px dashed #d7dee9;
  border-radius: 12px;
  background: #fbfcfe;
}
.legend-box {
  margin-top: 1rem;
  padding: .7rem .85rem;
  border-radius: 10px;
  background: #f8fafc;
  color: #758195;
  font-size: .78rem;
  border: 1px solid #edf1f5;
}
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  flex-wrap: wrap;
}
.search-box { max-width: 300px; }

.app-footer { color: #8a95a7; font-size: .78rem; padding: .35rem 0 1.6rem; }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(circle at 10% 20%, rgba(29,140,248,.16), transparent 30%),
    radial-gradient(circle at 90% 80%, rgba(46,204,113,.08), transparent 30%),
    #eef3f9;
}
.login-shell { width: min(940px, 100%); overflow: hidden; border-radius: 22px; background: #fff; box-shadow: 0 24px 70px rgba(15,23,42,.16); border: 1px solid rgba(226,232,240,.9); }
.login-side { min-height: 500px; padding: 2.4rem; color: #fff; background: linear-gradient(145deg, #0f172a, #182a48); display: flex; flex-direction: column; justify-content: space-between; }
.login-side .brand-mark { width: 42px; height: 42px; }
.login-copy h1 { font-size: 2rem; letter-spacing: -.04em; font-weight: 800; }
.login-copy p { color: rgba(255,255,255,.72); max-width: 34rem; }
.login-form { padding: 2.4rem; }

.report-header { border-bottom: 2px solid #172033; padding-bottom: .85rem; margin-bottom: 1.15rem; }
.report-title { font-size: 1.5rem; font-weight: 800; letter-spacing: -.03em; }
.report-meta { color: #667085; font-size: .82rem; }

@media (max-width: 991.98px) {
  .navbar-collapse { padding-top: .7rem; }
  .user-actions { margin-top: .65rem; }
  .page-header { align-items: flex-start; flex-direction: column; }
}

@media (max-width: 575.98px) {
  .app-main { padding-left: .8rem !important; padding-right: .8rem !important; }
  .card-body { padding: 1rem; }
  .page-title { font-size: 1.55rem; }
  .login-side { min-height: 250px; padding: 1.6rem; }
  .login-form { padding: 1.5rem; }
}

@media print {
  body { background: #fff !important; font-size: 10pt; }
  .app-navbar, .app-footer, .no-print { display: none !important; }
  .app-main { max-width: 100% !important; padding: 0 !important; }
  .card, .panel { box-shadow: none !important; border: 1px solid #d8dee8 !important; break-inside: avoid; }
  .table-responsive { overflow: visible !important; }
  .table > :not(caption) > * > * { padding: .38rem .35rem; }
  .table thead th { background: #f2f4f7 !important; color: #222 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .note-cell { min-width: 170px; }
  @page { size: landscape; margin: 10mm; }
}

/* ===== Layout sidebar ===== */
:root { --sidebar-width: 272px; }
.app-body { overflow-x: hidden; }
.app-sidebar {
  --bs-offcanvas-width: var(--sidebar-width);
  background: linear-gradient(180deg, #0f172a 0%, #111d33 58%, #0b1324 100%);
  color: #fff;
  border-right: 1px solid rgba(255,255,255,.07) !important;
}
.sidebar-inner {
  min-height: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 1.35rem 1rem 1rem;
  overflow-y: auto;
}
.sidebar-mobile-header { background: transparent; color: #fff; border-bottom: 1px solid rgba(255,255,255,.08); }
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: .8rem;
  color: #fff;
  padding: .2rem .35rem 1.55rem;
  margin-bottom: .35rem;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar-brand:hover { color: #fff; }
.sidebar-brand-copy { display: flex; flex-direction: column; line-height: 1.18; }
.sidebar-brand-copy strong { font-size: .98rem; letter-spacing: -.02em; }
.sidebar-brand-copy small { color: rgba(255,255,255,.52); font-size: .72rem; margin-top: .18rem; }
.sidebar-label {
  padding: 1rem .65rem .55rem;
  color: rgba(255,255,255,.38);
  font-size: .65rem;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}
.sidebar-nav { display: grid; gap: .35rem; }
.sidebar-link {
  display: flex;
  align-items: center;
  gap: .72rem;
  padding: .7rem .75rem;
  border-radius: 11px;
  color: rgba(255,255,255,.7);
  font-weight: 600;
  transition: background .18s ease, color .18s ease, transform .18s ease;
}
.sidebar-link:hover { color: #fff; background: rgba(255,255,255,.07); transform: translateX(2px); }
.sidebar-link.active { color: #fff; background: rgba(29,140,248,.18); box-shadow: inset 3px 0 0 #42a5ff; }
.sidebar-icon {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  flex: 0 0 30px;
  background: rgba(255,255,255,.07);
  color: rgba(255,255,255,.76);
  font-size: .72rem;
  font-weight: 850;
}
.sidebar-link.active .sidebar-icon { background: rgba(66,165,255,.18); color: #7fc2ff; }
.sidebar-spacer { flex: 1; min-height: 2rem; }
.sidebar-user {
  display: flex;
  align-items: center;
  gap: .72rem;
  padding: .85rem .7rem;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 12px;
  background: rgba(255,255,255,.045);
}
.sidebar-avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #42a5ff, #1d8cf8);
  font-weight: 800;
}
.sidebar-user-copy { min-width: 0; display: flex; flex-direction: column; }
.sidebar-user-copy strong { font-size: .84rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-user-copy small { color: rgba(255,255,255,.45); font-size: .68rem; }
.sidebar-logout {
  margin-top: .55rem;
  display: block;
  text-align: center;
  padding: .6rem .75rem;
  border-radius: 10px;
  color: rgba(255,255,255,.58);
  font-size: .8rem;
  font-weight: 600;
}
.sidebar-logout:hover { color: #fff; background: rgba(255,255,255,.06); }

.app-content { min-height: 100vh; }
.mobile-topbar {
  position: sticky;
  top: 0;
  z-index: 1020;
  min-height: 62px;
  display: flex;
  align-items: center;
  gap: .85rem;
  padding: .7rem 1rem;
  background: rgba(255,255,255,.96);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--app-border);
  box-shadow: 0 4px 18px rgba(15,23,42,.05);
}
.mobile-menu-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--app-border);
  border-radius: 10px;
  background: #fff;
  display: grid;
  place-content: center;
  gap: 4px;
}
.mobile-menu-btn span { display: block; width: 18px; height: 2px; background: #334155; border-radius: 2px; }

@media (min-width: 992px) {
  .app-sidebar {
    position: fixed !important;
    inset: 0 auto 0 0 !important;
    width: var(--sidebar-width) !important;
    height: 100vh !important;
    transform: none !important;
    visibility: visible !important;
    z-index: 1035;
  }
  .app-content { margin-left: var(--sidebar-width); width: calc(100% - var(--sidebar-width)); }
}

/* ===== Toolbar / modals ===== */
.page-header { align-items: center; }
.page-actions { display: flex; gap: .6rem; flex-wrap: wrap; }
.modal-content {
  border: 0;
  border-radius: 18px;
  box-shadow: 0 24px 70px rgba(15,23,42,.2);
  overflow: hidden;
}
.modal-header { padding: 1.15rem 1.3rem; border-bottom: 1px solid var(--app-border); background: #fbfcfe; }
.modal-title { font-size: 1rem; font-weight: 800; letter-spacing: -.02em; }
.modal-body { padding: 1.3rem; }
.modal-footer { padding: 1rem 1.3rem; border-top: 1px solid var(--app-border); background: #fbfcfe; }
.modal-dialog-scrollable .modal-body { overflow-y: auto; }
.creation-hint { color: var(--app-muted); font-size: .82rem; margin-top: .25rem; }
.group-selector-card .card-body { padding: 1rem 1.15rem; }
.group-selector-row { display: flex; align-items: end; gap: .75rem; flex-wrap: wrap; }
.group-selector-row .group-select-wrap { flex: 1 1 280px; max-width: 430px; }
.action-row { display: inline-flex; gap: .4rem; flex-wrap: wrap; justify-content: flex-end; }

@media (max-width: 767.98px) {
  .page-header { align-items: flex-start; flex-direction: column; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1 1 auto; }
  .toolbar { align-items: stretch; }
  .search-box { max-width: none; width: 100%; }
}

@media print {
  .app-sidebar, .mobile-topbar, .no-print, .sidebar-inner { display: none !important; }
  .app-content { margin-left: 0 !important; width: 100% !important; }
  .app-main { max-width: none !important; padding: 0 !important; }
  .app-footer { display: none !important; }
}
@media (max-width: 991.98px) {
  .app-sidebar .sidebar-inner { height: calc(100vh - 69px); min-height: 0; }
}

/* ===== Moduli avanzati ===== */
.sidebar-search{display:flex;gap:.35rem;margin:.2rem .35rem .45rem}.sidebar-search input{width:100%;min-width:0;background:rgba(255,255,255,.075);border:1px solid rgba(255,255,255,.08);border-radius:10px;color:#fff;padding:.58rem .7rem;font-size:.8rem;outline:none}.sidebar-search input::placeholder{color:rgba(255,255,255,.42)}.sidebar-search input:focus{border-color:rgba(66,165,255,.6);box-shadow:0 0 0 3px rgba(66,165,255,.10)}.sidebar-search button{border:0;border-radius:10px;background:rgba(29,140,248,.2);color:#8dcbff;width:38px;font-weight:800}
.quick-actions{display:flex;gap:.65rem;flex-wrap:wrap}.metric-mini{padding:.8rem 1rem;border:1px solid var(--app-border);background:#fbfdff;border-radius:12px}.metric-mini strong{display:block;font-size:1.2rem}.metric-mini small{color:var(--app-muted)}
.qual-badges{display:flex;gap:.3rem;flex-wrap:wrap}.qual-badge{display:inline-flex;align-items:center;padding:.28rem .5rem;border-radius:999px;background:#eef6ff;color:#1469b8;font-size:.7rem;font-weight:800;border:1px solid #dcecff}.qual-badge.role{background:#f5f0ff;color:#6f42c1;border-color:#e7ddff}.qual-badge.warn{background:#fff6df;color:#9a6700;border-color:#f8e3a3}
.filter-row{display:flex;gap:.5rem;flex-wrap:wrap}.filter-chip{border:1px solid var(--app-border);background:#fff;color:#526175;border-radius:999px;padding:.42rem .7rem;font-size:.78rem;font-weight:700}.filter-chip.active,.filter-chip:hover{background:#eaf4ff;color:#0869bf;border-color:#bddcff}
.balance-list{display:grid;gap:.55rem}.balance-alert{display:flex;align-items:flex-start;gap:.65rem;padding:.75rem .85rem;border:1px solid var(--app-border);border-radius:11px;background:#fff}.balance-dot{width:9px;height:9px;margin-top:.35rem;border-radius:50%;background:#f0ad4e;flex:0 0 9px}.balance-alert.danger .balance-dot{background:#dc3545}.balance-alert.info .balance-dot{background:#0dcaf0}.balance-alert .small{color:#6d7889}
.drag-handle{cursor:grab;user-select:none;color:#9aa5b4;font-size:1.15rem;padding:.25rem}.dragging{opacity:.45}.drop-target{outline:2px dashed #7cbcff;outline-offset:-2px}
.group-card-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:1rem}.group-summary-card{border:1px solid var(--app-border);border-radius:14px;background:#fff;padding:1rem}.group-summary-card h3{font-size:1rem;font-weight:800;margin:0}.group-summary-card .meta{color:#7b8798;font-size:.78rem}.group-summary-card .numbers{display:flex;gap:.9rem;margin-top:.8rem}.group-summary-card .numbers span{font-size:.76rem;color:#667085}.group-summary-card .numbers strong{display:block;color:#263449;font-size:1rem}
.history-line{position:relative;padding-left:1.35rem;padding-bottom:1.2rem}.history-line:before{content:"";position:absolute;left:.28rem;top:.55rem;bottom:-.2rem;width:1px;background:#e3e9f1}.history-line:last-child:before{display:none}.history-line:after{content:"";position:absolute;left:0;top:.35rem;width:.58rem;height:.58rem;border-radius:50%;background:#1d8cf8;box-shadow:0 0 0 4px #eaf4ff}.history-time{font-size:.72rem;color:#8a95a7}.history-title{font-weight:750;color:#2c3a4f}.history-desc{color:#667085;font-size:.84rem}
.detail-hero{display:flex;justify-content:space-between;gap:1rem;align-items:flex-start;padding:1.25rem;border:1px solid var(--app-border);border-radius:16px;background:linear-gradient(135deg,#fff,#f7fbff)}
.report-brand{display:flex;align-items:center;gap:.65rem}.report-brand-mark{width:38px;height:38px;border-radius:10px;background:#0f172a;color:#fff;display:grid;place-items:center;font-weight:900}.report-section-title{font-size:.72rem;text-transform:uppercase;letter-spacing:.08em;color:#7a8698;font-weight:800;margin-bottom:.6rem}.report-signature{margin-top:1.5rem;border-top:1px solid #dce3ec;padding-top:.65rem;color:#7d8795;font-size:.78rem}
.backup-card{border:1px solid var(--app-border);border-radius:14px;padding:1rem;background:#fff}.backup-card .icon{width:42px;height:42px;border-radius:12px;background:#edf6ff;color:#1472c6;display:grid;place-items:center;font-weight:900}
@media print{.app-sidebar,.mobile-topbar,.sidebar-search{display:none!important}.app-content{margin-left:0!important}.report-page .card{break-inside:avoid}.report-page .table{font-size:8.5pt}.report-page .note-cell{min-width:120px}.report-brand-mark{-webkit-print-color-adjust:exact;print-color-adjust:exact}}
