/**
 * Shell applicativa — layout comune a tutte le pagine.
 *
 * Breakpoint di riferimento (UIUX_Specifica_Consultorio, cap. 5.1),
 * coerenti con le classi di Bootstrap5 (md=768px, lg=1200px qui
 * personalizzato per allinearsi alla soglia desktop richiesta):
 *   smartphone : < 768px
 *   tablet     : 768–1199px
 *   desktop    : >= 1200px
 */

:root {
  --shell-sidebar-width: 260px;
  --shell-sidebar-width-collapsed: 72px;
  --shell-header-height: 64px;
  --shell-footer-height: 48px;
  --shell-bottom-tab-height: 56px;
}

html, body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ===================== HEADER ===================== */

.shell-header {
  height: var(--shell-header-height);
  background-color: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 1rem;
  position: sticky;
  top: 0;
  z-index: 1030;
}

.shell-header__logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  color: var(--color-primary);
  text-decoration: none;
  white-space: nowrap;
}

.shell-header__logo-mark {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background-color: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}

.shell-header__titles {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
  overflow: hidden;
}

.shell-header__page-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.shell-header__breadcrumb {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 4px;
}

.shell-header__breadcrumb a {
  color: var(--color-text-muted);
  text-decoration: none;
}
.shell-header__breadcrumb a:hover { text-decoration: underline; }

.shell-breadcrumb-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  background: var(--color-primary-light);
  color: var(--color-primary) !important;
  font-size: 0.75rem;
  flex-shrink: 0;
  text-decoration: none !important;
  transition: background 0.15s;
}
.shell-breadcrumb-back:hover { background: var(--color-primary); color: #fff !important; }

.shell-breadcrumb-sep {
  color: var(--color-text-muted);
  opacity: 0.4;
  font-size: 0.7rem;
}
.shell-breadcrumb-current {
  color: var(--color-text);
  font-weight: 600;
}

.shell-header__spacer {
  flex: 1 1 auto;
}

.shell-header__hamburger {
  display: none;
}

.shell-header__user {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  white-space: nowrap;
}

.shell-header__user-name {
  font-size: 0.85rem;
  line-height: 1.1;
}

.shell-header__user-role {
  font-size: 0.7rem;
  color: var(--color-text-muted);
}

.shell-header__notif-btn {
  position: relative;
}

.shell-header__notif-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  font-size: 0.6rem;
}

/* ===================== BODY LAYOUT (sidebar + content) ===================== */

.shell-body {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
}

/* ===================== SIDEBAR ===================== */

.shell-sidebar {
  width: var(--shell-sidebar-width);
  flex-shrink: 0;
  background-color: var(--color-sidebar-bg);
  color: var(--color-sidebar-text);
  display: flex;
  flex-direction: column;
  transition: width 0.15s ease;
  overflow-x: hidden;
}

.shell-sidebar__toggle {
  display: flex;
  justify-content: flex-end;
  padding: 0.5rem;
}

.shell-sidebar__toggle button {
  background: transparent;
  border: none;
  color: var(--color-sidebar-text);
  width: 36px;
  height: 36px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.shell-sidebar__toggle button:hover {
  background-color: var(--color-sidebar-hover-bg);
}

.shell-sidebar__nav {
  flex: 1 1 auto;
  overflow-y: auto;
  padding-bottom: 1rem;
}

.shell-sidebar__group-title {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-sidebar-text-muted);
  padding: 0.85rem 1rem 0.25rem;
  white-space: nowrap;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  user-select: none;
}
.shell-sidebar__group-title:hover {
  color: var(--color-sidebar-text);
}
.shell-sidebar__group-chevron {
  font-size: 0.55rem;
  transition: transform 0.2s ease;
  flex-shrink: 0;
}
.shell-sidebar__group-title[aria-expanded="false"] .shell-sidebar__group-chevron {
  transform: rotate(-90deg);
}

.shell-sidebar__item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  color: var(--color-sidebar-text);
  text-decoration: none;
  font-size: 0.875rem;
  white-space: nowrap;
  border-left: 3px solid transparent;
}

.shell-sidebar__item:hover {
  background-color: var(--color-sidebar-hover-bg);
  color: var(--color-sidebar-text);
}

.shell-sidebar__item.is-active {
  background-color: var(--color-sidebar-active-bg);
  color: var(--color-sidebar-active-text);
  border-left-color: var(--color-tertiary);
  font-weight: 600;
}

.shell-sidebar__item-icon {
  width: 20px;
  text-align: center;
  flex-shrink: 0;
}

.shell-sidebar__item-label {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
}

.shell-sidebar__item-badge {
  flex-shrink: 0;
}

/* Stato compresso: solo icona, etichette nascoste */
.shell-sidebar.is-collapsed {
  width: var(--shell-sidebar-width-collapsed);
}
.shell-sidebar.is-collapsed .shell-sidebar__group-title,
.shell-sidebar.is-collapsed .shell-sidebar__item-label,
.shell-sidebar.is-collapsed .shell-sidebar__item-badge {
  display: none;
}
.shell-sidebar.is-collapsed .collapse:not(.show) {
  display: block !important;
}
.shell-sidebar.is-collapsed .shell-sidebar__item {
  justify-content: center;
  padding: 0.65rem 0;
}

/* ===================== CONTENUTO ===================== */

.shell-content {
  flex: 1 1 auto;
  min-width: 0;
  padding: 1.5rem;
  padding-bottom: calc(1.5rem + var(--shell-bottom-tab-height));
}

/* ===================== FOOTER ===================== */

.shell-footer {
  height: var(--shell-footer-height);
  border-top: 1px solid var(--color-border);
  background-color: var(--color-surface);
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1rem;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  flex-wrap: wrap;
}

.shell-footer__env-badge {
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ===================== BOTTOM TAB (smartphone) ===================== */

.shell-bottom-tab {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--shell-bottom-tab-height);
  background-color: var(--color-surface);
  border-top: 1px solid var(--color-border);
  z-index: 1030;
  align-items: stretch;
}

.shell-bottom-tab__item {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--color-text-muted);
  text-decoration: none;
  font-size: 0.65rem;
  min-height: 44px;
}

.shell-bottom-tab__item.is-active {
  color: var(--color-primary);
}

/* ===================== RESPONSIVE ===================== */

/* Tablet: 768–1199px -> sidebar compressa di default */
@media (max-width: 1199.98px) {
  .shell-sidebar:not(.is-user-expanded) {
    width: var(--shell-sidebar-width-collapsed);
  }
  .shell-sidebar:not(.is-user-expanded) .shell-sidebar__group-title,
  .shell-sidebar:not(.is-user-expanded) .shell-sidebar__item-label,
  .shell-sidebar:not(.is-user-expanded) .shell-sidebar__item-badge {
    display: none;
  }
  .shell-sidebar:not(.is-user-expanded) .shell-sidebar__item {
    justify-content: center;
    padding: 0.65rem 0;
  }
}

/* Smartphone: < 768px -> sidebar a comparsa (offcanvas), bottom-tab visibile */
@media (max-width: 767.98px) {
  .shell-header__hamburger {
    display: inline-flex;
  }

  .shell-body > .shell-sidebar {
    display: none;
  }

  .shell-bottom-tab {
    display: flex;
  }

  .shell-content {
    padding: 1rem;
    padding-bottom: calc(1rem + var(--shell-bottom-tab-height));
  }

  /* Dentro l'offcanvas la sidebar deve sempre mostrare le etichette:
     la regola tablet qui sopra (.shell-sidebar:not(.is-user-expanded))
     si applica anche sotto i 767.98px e vince per specificità pari a
     parità di selettore — queste regole hanno la stessa specificità
     (3 classi) e arrivano dopo nel foglio, quindi la annullano. */
  .offcanvas .shell-sidebar {
    width: 100%;
  }
  .offcanvas .shell-sidebar__toggle {
    display: none;
  }
  .offcanvas .shell-sidebar .shell-sidebar__group-title {
    display: flex;
  }
  .offcanvas .shell-sidebar .shell-sidebar__item-label {
    display: block;
  }
  .offcanvas .shell-sidebar .shell-sidebar__item-badge {
    display: inline-block;
  }
  .offcanvas .shell-sidebar .shell-sidebar__item {
    justify-content: flex-start;
    padding: 0.6rem 1rem;
  }
}

@media (min-width: 768px) {
  .shell-offcanvas-sidebar {
    display: none !important;
  }
}

/* ── Card assistito / operatore ──────────────────────────────────────────── */
.card-assistito,
.card-operatore {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
}
.card-assistito--compact,
.card-operatore--compact {
  box-shadow: none;
}
.card-assistito:hover,
.card-operatore:hover {
  background: var(--color-bg-hover, #f8f9fa);
}

/* ── Cartella: episodio card touch ───────────────────────────────────────── */
@media (max-width: 768px) {
  .cartella-ep-card .btn {
    min-height: 40px;
    display: inline-flex;
    align-items: center;
  }
}

/* ── Fase 1.8 / Blocco C: viste tablet-first cartella-episodio ──────────────
 * I form CON4 (registrazione/modifica prestazione) sono densi per natura
 * (30+ campi tracciato regionale): su tablet (768–1199px) Bootstrap li
 * mostrerebbe comunque su 3-4 colonne strette (col-lg-* passa a full width
 * solo sotto i 992px), con select/input troppo piccoli per l'uso col dito
 * in ambulatorio. Qui si allarga la sola area di tocco, senza toccare la
 * densità informativa su desktop.
 */
@media (min-width: 768px) and (max-width: 1199.98px) {
  .form-con4 .form-select-sm,
  .form-con4 .form-control-sm {
    min-height: 44px;
    font-size: 0.95rem;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
  }
  .form-con4 .row.g-2 {
    row-gap: 0.75rem !important;
  }
  .form-con4 .form-label {
    margin-bottom: 0.35rem;
  }
  .form-con4 button[type="submit"] {
    min-height: 44px;
    padding-left: 1.25rem;
    padding-right: 1.25rem;
  }
}

/* Tabelle di cartella/episodio: sotto i 992px passano a card compatte
 * riga-per-riga invece di scorrimento orizzontale, più leggibili col dito. */
@media (max-width: 991.98px) {
  .table-tablet-stack thead {
    display: none;
  }
  .table-tablet-stack tbody tr {
    display: block;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    margin-bottom: 0.5rem;
    padding: 0.5rem 0.75rem;
  }
  .table-tablet-stack tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    border: none;
    padding: 0.35rem 0;
  }
  .table-tablet-stack tbody td[data-label]::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--color-text-muted, #6c757d);
    font-size: 0.8rem;
    flex-shrink: 0;
  }
}

/* ── Contenuto procedure (editor + dettaglio) ───────────────────────────────
   Rete di sicurezza per le tabelle inserite nell'editor delle sezioni
   procedura: le celle vengono già salvate con un bordo inline, ma il
   contenuto può anche arrivare da un copia-incolla (Word, browser) privo di
   stili propri — qui garantiamo comunque righe/colonne visibili. */
.editor-area table,
.procedura-contenuto table {
  border-collapse: collapse;
}
.editor-area table td,
.editor-area table th,
.procedura-contenuto table td,
.procedura-contenuto table th {
  border: 1px solid #adb5bd;
  padding: 4px;
}
/* Ridimensionamento nativo del browser (maniglia in basso a destra) —
   solo nell'editor, non nel dettaglio in sola lettura. Serve
   overflow:auto perché resize non si applica con overflow:visible. */
.editor-area table td,
.editor-area table th {
  resize: both;
  overflow: auto;
  min-width: 30px;
  min-height: 20px;
}
.editor-area .tabella-cella-attiva {
  outline: 2px solid #0d6efd;
  outline-offset: -2px;
}

/* ── Utility ─────────────────────────────────────────────────────────────── */
.min-width-0 { min-width: 0; }

/* Contenimento verticale per blocchi con molte righe dentro una form
   (liste, checkbox, tabelle annidate): l'altezza resta specifica per
   blocco (style="max-height:...px", scelta caso per caso), questa
   classe aggiunge solo il comportamento comune (scroll verticale) e lo
   stringe ulteriormente su mobile, dove lo spazio verticale è scarso.
   Compatibile con .table-responsive già presente sullo stesso elemento. */
.form-scroll-inner {
  overflow-y: auto;
}
@media (max-width: 767.98px) {
  .form-scroll-inner {
    max-height: 220px !important;
  }
}

/* Modalità "card" opt-in per le griglie con molte colonne su mobile,
   alternativa allo scroll orizzontale di .table-responsive: non
   richiede toccare il markup delle righe, solo aggiungere
   class="table-card-mobile" alla tabella e data-label="..." su ogni
   <td> (l'intestazione <thead> resta invariata, serve solo per
   desktop/tablet dove la regola non si applica). */
@media (max-width: 767.98px) {
  .table-card-mobile thead {
    display: none;
  }
  .table-card-mobile,
  .table-card-mobile tbody,
  .table-card-mobile tr,
  .table-card-mobile td {
    display: block;
    width: 100%;
  }
  .table-card-mobile tr {
    margin-bottom: .75rem;
    border: 1px solid var(--bs-border-color);
    border-radius: var(--bs-border-radius);
    padding: .5rem;
  }
  .table-card-mobile td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: .5rem;
    padding: .35rem .5rem;
    border: 0;
  }
  .table-card-mobile td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--bs-secondary-color);
  }
  .table-card-mobile td[data-label=""]::before,
  .table-card-mobile td:not([data-label])::before {
    content: none;
  }
}
