/* =============================================================================
   Shell del viewer: header con titolo e hamburger · menu laterale · area
   dashboard · barra di navigazione ◄ ● ► · footer sempre visibile.
   Layout a colonna alto quanto la finestra: solo l'area centrale scorre.
   ============================================================================= */

:root {
  --shell-header-h: 52px;
  --shell-footer-h: 30px;
  --shell-menu-w: 260px;
  --shell-bg: var(--surface-solid);
  --shell-border: var(--border);
  --shell-ease: cubic-bezier(.4, 0, .2, 1);
}

body:has(.app-shell) { overflow: hidden; }

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* --------------------------------- Header ---------------------------------- */

.app-header {
  flex: 0 0 var(--shell-header-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--shell-bg);
  border-bottom: 1px solid var(--shell-border);
  z-index: 30;
}
.app-header h3 {
  margin: 0;
  font: 700 18px/1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  letter-spacing: .02em;
  color: var(--text);
}
.app-theme-toggle { margin-left: 4px; }
/* menu utente (src/viewer/authui.js) in fondo a destra */
.app-user-slot { margin-left: auto; display: flex; align-items: center; }
.app-user-slot:empty { display: none; }
.app-header-badge {
  padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600;
  background: rgba(251, 191, 36, .18); color: #d97706;
}

/* Hamburger animato: tre barre → X */
.app-menu-toggle {
  position: relative;
  width: 40px; height: 40px;
  padding: 0;
  border: 1px solid var(--shell-border);
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  transition: background-color .15s ease, border-color .15s ease;
}
.app-menu-toggle:hover { background: var(--hover); border-color: var(--border-strong); }
.app-menu-toggle:focus-visible { outline: 2px solid #3b82f6; outline-offset: 2px; }
.app-menu-toggle span {
  position: absolute;
  left: 10px; right: 10px;
  height: 2px;
  border-radius: 2px;
  background: var(--text);
  transition: transform .3s var(--shell-ease), opacity .2s ease, top .3s var(--shell-ease);
}
.app-menu-toggle span:nth-child(1) { top: 13px; }
.app-menu-toggle span:nth-child(2) { top: 19px; }
.app-menu-toggle span:nth-child(3) { top: 25px; }
.is-menu-open .app-menu-toggle span:nth-child(1) { top: 19px; transform: rotate(45deg); }
.is-menu-open .app-menu-toggle span:nth-child(2) { opacity: 0; transform: scaleX(.2); }
.is-menu-open .app-menu-toggle span:nth-child(3) { top: 19px; transform: rotate(-45deg); }

/* ------------------------------ Corpo + menu -------------------------------- */

.app-body {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

/* Il menu è una colonna che si allarga da 0 a --shell-menu-w: il contenuto accanto
   si restringe orizzontalmente (niente sovrapposizione su desktop). */
.app-menu {
  flex: 0 0 0;
  width: 0;
  overflow: hidden;
  background: var(--surface-solid);
  border-right: 1px solid transparent;
  transition: flex-basis .3s var(--shell-ease), width .3s var(--shell-ease), border-color .3s ease;
  z-index: 20;
}
.is-menu-open .app-menu {
  flex-basis: var(--shell-menu-w);
  width: var(--shell-menu-w);
  border-right-color: var(--shell-border);
}
.app-menu-inner {
  width: var(--shell-menu-w);
  height: 100%;
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  transform: translateX(-24px);
  opacity: 0;
  transition: transform .3s var(--shell-ease), opacity .25s ease;
}
.is-menu-open .app-menu-inner { transform: none; opacity: 1; }

.app-menu-title {
  margin: 0 4px 6px;
  font: 600 11px system-ui, sans-serif;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
}
.app-menu-panels { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.app-menu-item {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background-color .12s ease, color .12s ease;
}
.app-menu-item:hover { background: rgba(148, 163, 184, .1); color: var(--text); }
.app-menu-item.is-active { background: rgba(59, 130, 246, .16); border-color: rgba(59, 130, 246, .35); color: var(--accent-text); }
/* accordion dei gruppi di pannelli: testata espandibile + pannelli del gruppo (senza numeri) */
.app-menu-group { display: flex; flex-direction: column; }
.app-menu-group-head {
  width: 100%; display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border: 0; border-radius: 8px; background: transparent;
  color: var(--text); font: 600 13px system-ui, sans-serif; text-align: left; cursor: pointer;
}
.app-menu-group-head:hover { background: rgba(148, 163, 184, .1); }
.app-menu-group-head:focus-visible { outline: 2px solid #3b82f6; outline-offset: -2px; }
.app-menu-group-chev { flex: 0 0 auto; width: 7px; height: 7px; border-right: 2px solid var(--text-muted); border-bottom: 2px solid var(--text-muted);
  transform: rotate(-45deg); transition: transform .2s ease; margin: 0 3px 0 1px; }
.app-menu-group.is-open .app-menu-group-chev { transform: rotate(45deg); }
.app-menu-group-name { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-menu-group.is-current .app-menu-group-name { color: var(--accent-text); }
.app-menu-group-count { flex: 0 0 auto; min-width: 20px; padding: 0 6px; border-radius: 999px; background: rgba(148, 163, 184, .14);
  color: var(--text-muted); font-size: 11px; line-height: 18px; text-align: center; }
.app-menu-group-panels { list-style: none; margin: 2px 0 6px 13px; padding: 0 0 0 8px; border-left: 1px solid var(--shell-border);
  display: flex; flex-direction: column; gap: 2px; }
.app-menu-group-panels[hidden] { display: none; }
.app-menu-group-panels .app-menu-item { padding: 6px 10px; font-size: 13.5px; }
.app-menu-item-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-menu-links { display: flex; flex-direction: column; gap: 4px; }
.app-menu-loading { padding: 8px 10px; color: var(--text-faint); font-size: 13px; font-style: italic; }
.app-menu-link {
  padding: 8px 10px; font-size: 14px; border-radius: 8px; color: var(--accent-text); text-decoration: none;
}
.app-menu-link:hover { background: rgba(59, 130, 246, .12); }

/* Area della dashboard: prende lo spazio restante e scorre verticalmente */
.app-main {
  flex: 1 1 auto;
  min-width: 0;
  overflow-y: auto;
}
.app-shell #dashboard { min-height: 0; padding-bottom: 0; }

.app-menu-backdrop { display: none; }

/* ------------------------- Navigazione + footer ----------------------------- */

.app-nav-slot { flex: 0 0 auto; z-index: 25; }
.app-shell .nav {
  position: relative;
  height: 56px;
}
.app-shell .nav button:not(.nav-dot) { width: 42px; height: 42px; font-size: 18px; }

/* Rete di sicurezza: se la barra ◄ ● ► finisse fuori dalla shell (fixed, da nav.css)
   resta comunque sopra il footer invece di coprirlo. */
body:has(.app-shell) > .nav { bottom: var(--shell-footer-h); }

.app-footer {
  flex: 0 0 var(--shell-footer-h);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--footer-bg);
  border-top: 1px solid var(--shell-border);
  color: var(--text-faint);
  font-size: 12px;
  z-index: 25;
}

/* Modalità kiosk: menu laterale nascosto (anche l'hamburger) finché non si torna alla navigazione manuale */
.app-shell.is-kiosk .app-menu-toggle { visibility: hidden; }

/* ------------------------------ Schermi stretti ----------------------------- */

@media (max-width: 640px) {
  /* Il menu si sovrappone: su uno schermo stretto spingere lascerebbe troppo poco spazio */
  .app-menu { position: absolute; inset: 0 auto 0 0; box-shadow: 8px 0 24px var(--shadow-color, rgba(0,0,0,.)); }
  .is-menu-open .app-menu-backdrop {
    display: block; position: absolute; inset: 0; z-index: 15;
    background: var(--surface-2);
  }
  .app-header h3 { font-size: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .app-menu, .app-menu-inner, .app-menu-toggle span { transition: none; }
}
/* "Smart DashBoard – <gruppo>": il nome del gruppo corrente compare solo a menu chiuso (a menu aperto è nell'accordion) */
.app-header-group { font-weight: 500; color: var(--text-muted); }
.is-menu-open .app-header-group { display: none; }

/* badge delle notifiche non lette (notifications.js): testata del gruppo, voci dei pannelli, hamburger */
.app-badge { flex: 0 0 auto; min-width: 18px; height: 18px; margin-left: auto; padding: 0 5px; border-radius: 999px; background: var(--badge, #dc2626); color: #fff; font: 700 11px/18px system-ui, sans-serif; text-align: center; box-sizing: border-box; }
.app-menu-group-head .app-badge + .app-menu-group-count { margin-left: 6px; }
.app-menu-item .app-badge { margin-left: 8px; }
.app-badge.is-critical { animation: app-badge-pulse 1.4s ease-in-out infinite; }
.app-menu-toggle { position: relative; }
.app-menu-toggle.has-badge::after { content: attr(data-badge); position: absolute; top: -4px; right: -6px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 999px; background: #dc2626; color: #fff; font: 700 10px/16px system-ui, sans-serif; text-align: center; box-sizing: border-box; box-shadow: 0 0 0 2px var(--surface); }
.app-menu-toggle.is-critical::after { background: #9f1239; animation: app-badge-pulse 1.4s ease-in-out infinite; }
@keyframes app-badge-pulse { 50% { box-shadow: 0 0 0 4px rgb(220 38 38 / .25); } }
@media (prefers-reduced-motion: reduce) { .app-badge.is-critical, .app-menu-toggle.is-critical::after { animation: none; } }
