/* ==========================================================================
   Shell applicativa — rail, topbar, area contenuto
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  height: 100vh; height: 100dvh;
  transition: grid-template-columns var(--dur-slow) var(--ease-out);
}
.app[data-rail="collapsed"] { grid-template-columns: var(--rail-w-collapsed) 1fr; }

/* --- Rail ----------------------------------------------------------------- */
.rail {
  display: flex; flex-direction: column;
  background: var(--bg-rail);
  color: #E8E1D6;
  overflow: hidden;
  position: relative;
  z-index: 30;
}


.rail__brand {
  display: flex; align-items: center; gap: var(--s-3);
  height: var(--topbar-h); flex: none;
  padding: 0 var(--s-4);
  position: relative; z-index: 1;
}
.brand-mark {
  width: 30px; height: 30px; flex: none;
  border-radius: 9px;
  display: grid; place-items: center;
  background: var(--brand);
  color: #fff;
}
.brand-text { min-width: 0; }
.brand-text__name {
  font-family: var(--font-display);
  font-size: var(--fs-base); font-weight: 660;
  letter-spacing: -0.02em; color: #FCF8F2; line-height: 1.1;
}
.brand-text__sub {
  font-size: 9.5px; font-weight: 620; letter-spacing: var(--tracking-caps);
  text-transform: uppercase; color: rgba(232,225,214,0.42); margin-top: 1px;
}

.rail__nav { flex: 1; overflow-y: auto; overflow-x: hidden; padding: var(--s-2) var(--s-3) var(--s-4); position: relative; z-index: 1; }
.rail__nav::-webkit-scrollbar { width: 6px; }
.rail__nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border: 0; }

.rail__group + .rail__group { margin-top: var(--s-5); }
.rail__group-label {
  padding: 0 var(--s-3) var(--s-2);
  font-size: 9.5px; font-weight: 680; letter-spacing: var(--tracking-caps);
  text-transform: uppercase; color: rgba(232,225,214,0.32);
}

.rail-item {
  display: flex; align-items: center; gap: var(--s-3);
  width: 100%;
  height: 34px; padding: 0 var(--s-3);
  border-radius: var(--r-sm);
  font-size: var(--fs-md); font-weight: 520;
  color: rgba(232,225,214,0.68);
  position: relative;
  transition: background var(--dur-fast), color var(--dur-fast);
}
.rail-item + .rail-item { margin-top: 1px; }
.rail-item:hover { background: rgba(255,255,255,0.06); color: #F6F1E9; }
.rail-item .ic { flex: none; opacity: 0.9; }
.rail-item__label { flex: 1; text-align: left; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rail-item.is-active {
  background: rgba(255,255,255,0.095);
  color: #FFF9F0; font-weight: 590;
}
.rail-item.is-active::before {
  content: ""; position: absolute; left: -12px; top: 8px; bottom: 8px;
  width: 3px; border-radius: 0 3px 3px 0;
  background: var(--brand);
}
.rail-item .pill-count { flex: none; }

.app[data-rail="collapsed"] .rail__group-label,
.app[data-rail="collapsed"] .brand-text,
.app[data-rail="collapsed"] .rail-item__label,
.app[data-rail="collapsed"] .rail__foot-txt { display: none; }
.app[data-rail="collapsed"] .rail-item { justify-content: center; padding: 0; }
.app[data-rail="collapsed"] .rail-item .pill-count {
  position: absolute; top: 2px; right: 4px; min-width: 15px; height: 15px;
  font-size: 9px; padding: 0 3px;
}
.app[data-rail="collapsed"] .rail__group + .rail__group { margin-top: var(--s-3); padding-top: var(--s-3); border-top: 1px solid rgba(255,255,255,0.07); }

.rail__foot {
  flex: none; padding: var(--s-3);
  border-top: 1px solid rgba(255,255,255,0.07);
  position: relative; z-index: 1;
}
.rail-user {
  display: flex; align-items: center; gap: var(--s-3);
  width: 100%; padding: var(--s-2);
  border-radius: var(--r-sm);
  transition: background var(--dur-fast);
}
.rail-user:hover { background: rgba(255,255,255,0.07); }
.rail-user__txt { min-width: 0; text-align: left; }
.rail-user__name { display: block; font-size: var(--fs-sm); font-weight: 580; color: #F2ECE2; }
.rail-user__role { display: block; font-size: var(--fs-2xs); color: rgba(232,225,214,0.42); }

/* --- Colonna principale --------------------------------------------------- */
.main {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
  background: var(--bg-canvas);
}

.topbar {
  height: var(--topbar-h); flex: none;
  display: flex; align-items: center; gap: var(--s-3);
  padding: 0 var(--s-5);
  border-bottom: 1px solid var(--border-subtle);
  background: color-mix(in srgb, var(--bg-surface) 82%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  position: relative; z-index: 20;
}

.topbar__title { min-width: 0; }
.topbar__title h1 {
  font-size: var(--fs-lg); font-weight: 640;
  letter-spacing: var(--tracking-snug);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar__crumb { font-size: var(--fs-2xs); color: var(--text-tertiary); font-weight: 550; }

.icon-btn {
  width: 34px; height: 34px; flex: none;
  border-radius: var(--r-sm);
  display: grid; place-items: center;
  color: var(--text-secondary);
  position: relative;
  transition: background var(--dur-fast), color var(--dur-fast);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
.icon-btn.is-on { background: var(--brand-soft); color: var(--text-brand); }
.icon-btn__dot {
  position: absolute; top: 6px; right: 6px;
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--c-red-500);
  box-shadow: 0 0 0 2px var(--bg-surface);
}

/* Selettore sede */
.venue-switch {
  display: flex; align-items: center; gap: var(--s-2);
  height: 36px; padding: 0 var(--s-2) 0 var(--s-2);
  border-radius: var(--r-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  transition: border-color var(--dur-fast), background var(--dur-fast);
  max-width: 240px;
}
.venue-switch:hover { border-color: var(--border-default); background: var(--bg-hover); }
.venue-switch__logo {
  width: 24px; height: 24px; border-radius: 6px; flex: none;
  display: grid; place-items: center;
  font-size: 11px; font-weight: 700; color: #fff;
  background: var(--c-ink-700);
}
.venue-switch__name { display: block; font-size: var(--fs-sm); font-weight: 600; letter-spacing: var(--tracking-snug); }
.venue-switch__meta { display: block; font-size: 10px; color: var(--text-tertiary); line-height: 1.1; }

/* Stato servizio */
.service-pill {
  display: flex; align-items: center; gap: var(--s-2);
  height: 36px; padding: 0 var(--s-3);
  border-radius: var(--r-sm);
  font-size: var(--fs-sm); font-weight: 570;
  background: var(--st-ready-bg); color: var(--st-ready-fg);
  border: 1px solid transparent;
  transition: all var(--dur-fast);
}
.service-pill:hover { border-color: currentColor; }
.service-pill--off { background: var(--st-done-bg); color: var(--st-done-fg); }
.service-pill--paused { background: var(--st-alert-bg); color: var(--st-alert-fg); }
.service-pill__dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.service-pill:not(.service-pill--off) .service-pill__dot { --pulse-c: currentColor; animation: pulse-ring 2.4s var(--ease-out) infinite; }

/* Area vista */
.view {
  flex: 1; min-height: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
}
.view__inner { max-width: var(--content-max); margin: 0 auto; padding: var(--s-6); }
.view__inner--wide { max-width: none; }
.view--flush { overflow: hidden; display: flex; flex-direction: column; }

/* --- Popover -------------------------------------------------------------- */
.popover {
  position: fixed; z-index: 90;
  min-width: 220px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-md);
  box-shadow: var(--sh-lg);
  padding: var(--s-2);
  animation: pop-in var(--dur-base) var(--ease-out);
  max-height: min(560px, 80vh);
  overflow-y: auto;
}
@keyframes pop-in { from { opacity: 0; transform: translateY(-5px) scale(0.985) } to { opacity: 1; transform: none } }

.menu-item {
  display: flex; align-items: center; gap: var(--s-3);
  width: 100%; padding: 7px var(--s-3);
  border-radius: var(--r-xs);
  font-size: var(--fs-md); font-weight: 520;
  color: var(--text-primary);
  text-align: left;
  transition: background var(--dur-fast);
}
.menu-item:hover { background: var(--bg-hover); }
.menu-item .ic { color: var(--text-tertiary); flex: none; }
.menu-item.is-active { background: var(--brand-faint); color: var(--text-brand); font-weight: 600; }
.menu-item.is-active .ic { color: var(--text-brand); }
.menu-item--danger { color: var(--c-red-700); }
.menu-item--danger .ic { color: currentColor; }
.menu-sep { height: 1px; background: var(--border-subtle); margin: var(--s-2) calc(var(--s-2) * -1); }
.menu-label {
  padding: var(--s-2) var(--s-3) 4px;
  font-size: var(--fs-2xs); font-weight: 660;
  letter-spacing: var(--tracking-caps); text-transform: uppercase;
  color: var(--text-tertiary);
}

/* --- Centro notifiche ----------------------------------------------------- */
.notif-panel { width: 380px; padding: 0; }
.notif-panel__head {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--s-3) var(--s-4);
  border-bottom: 1px solid var(--border-subtle);
  position: sticky; top: 0; background: var(--bg-surface); z-index: 1;
}
.notif {
  display: flex; gap: var(--s-3);
  padding: var(--s-3) var(--s-4);
  border-bottom: 1px solid var(--border-subtle);
  transition: background var(--dur-fast);
  cursor: pointer; text-align: left; width: 100%;
}
.notif:hover { background: var(--bg-hover); }
.notif.is-unread { background: var(--brand-faint); }
.notif.is-unread:hover { background: var(--brand-soft); }
.notif__ic {
  width: 30px; height: 30px; border-radius: var(--r-sm); flex: none;
  display: grid; place-items: center;
}
.notif__title { display: block; font-size: var(--fs-md); font-weight: 570; line-height: 1.35; }
.notif__body { display: block; font-size: var(--fs-xs); color: var(--text-secondary); line-height: 1.4; margin-top: 1px; }
.notif__time { display: block; font-size: var(--fs-2xs); color: var(--text-tertiary); margin-top: 3px; }
.notif__prio {
  width: 3px; border-radius: 3px; flex: none; align-self: stretch;
  background: var(--c-red-500);
}

/* --- Command palette ------------------------------------------------------ */
.cmdk {
  position: fixed; z-index: 110;
  top: 16vh; left: 50%; transform: translateX(-50%);
  width: min(620px, calc(100vw - 32px));
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--r-xl);
  box-shadow: var(--sh-xl);
  overflow: hidden;
  animation: modal-in var(--dur-base) var(--ease-out);
}
.cmdk__input {
  width: 100%; height: 54px; padding: 0 var(--s-5);
  border: none; background: none; font-size: var(--fs-lg);
  border-bottom: 1px solid var(--border-subtle);
}
.cmdk__input:focus { outline: none; }
.cmdk__list { max-height: 46vh; overflow-y: auto; padding: var(--s-2); }
.cmdk__item {
  display: flex; align-items: center; gap: var(--s-3);
  width: 100%; padding: var(--s-2) var(--s-3);
  border-radius: var(--r-sm); text-align: left;
  font-size: var(--fs-md);
}
.cmdk__item[aria-selected="true"] { background: var(--brand-faint); }
.cmdk__item .ic { color: var(--text-tertiary); flex: none; }
.cmdk__kbd { margin-left: auto; }
kbd {
  display: inline-block; padding: 1px 5px; min-width: 19px; text-align: center;
  border-radius: 4px; border: 1px solid var(--border-default);
  background: var(--bg-inset);
  font-family: var(--font-sans); font-size: 10.5px; font-weight: 600;
  color: var(--text-tertiary);
}

/* --- Tablet: rail sempre a icone, l'espansione diventa un pannello sopra -- */
@media (max-width: 1180px) and (min-width: 761px) {
  .app, .app[data-rail] { grid-template-columns: var(--rail-w-collapsed) 1fr; }
  .app .rail__group-label,
  .app .brand-text,
  .app .rail-item__label,
  .app .rail__foot-txt { display: none; }
  .app .rail-item { justify-content: center; padding: 0; }
  .app .rail-item .pill-count {
    position: absolute; top: 2px; right: 4px; min-width: 15px; height: 15px;
    font-size: 9px; padding: 0 3px;
  }
  .app[data-rail="expanded"] .rail {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: var(--rail-w); z-index: 100; box-shadow: var(--sh-xl);
  }
  .app[data-rail="expanded"] .rail__group-label,
  .app[data-rail="expanded"] .brand-text,
  .app[data-rail="expanded"] .rail-item__label,
  .app[data-rail="expanded"] .rail__foot-txt { display: block; }
  .app[data-rail="expanded"] .rail-item { justify-content: flex-start; padding: 0 var(--s-3); }
  .app[data-rail="expanded"] .rail-item .pill-count { position: static; min-width: 19px; height: 19px; font-size: var(--fs-2xs); }
}

/* --- Telefono: il rail diventa una barra in basso ------------------------- */
@media (max-width: 760px) {
  .app, .app[data-rail] { grid-template-columns: 1fr; }

  .rail {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    z-index: 60;
    height: 60px;
    flex-direction: row;
    padding-bottom: env(safe-area-inset-bottom);
    border-top: 1px solid rgba(255,255,255,0.08);
  }
  .rail::after { display: none; }
  .rail__brand, .rail__foot { display: none; }
  .rail__nav {
    display: flex; align-items: center; gap: var(--s-1);
    padding: 0 var(--s-2);
    overflow-x: auto; overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    width: 100%;
  }
  .rail__nav::-webkit-scrollbar { display: none; height: 0; }
  .rail__nav { scrollbar-width: none; }
  .view { overflow-x: hidden; }
  .rail__group { display: flex; gap: var(--s-1); }
  .rail__group + .rail__group { margin-top: 0; }
  .rail__group-label { display: none; }
  .rail-item {
    flex-direction: column; justify-content: center; gap: 2px;
    height: 52px; min-width: 62px; padding: 0 var(--s-2);
  }
  .rail-item + .rail-item { margin-top: 0; }
  .rail-item__label { display: block; flex: none; font-size: 9.5px; font-weight: 600; text-align: center; }
  .rail-item.is-active::before {
    left: 50%; right: auto; top: 2px; bottom: auto;
    transform: translateX(-50%);
    width: 20px; height: 3px; border-radius: 0 0 3px 3px;
  }
  .rail-item .pill-count {
    position: absolute; top: 2px; right: 8px;
    min-width: 15px; height: 15px; font-size: 9px; padding: 0 3px;
  }

  .main { padding-bottom: calc(60px + env(safe-area-inset-bottom)); }
  .view__inner { padding: var(--s-3); }
  .topbar { padding: 0 var(--s-3); gap: var(--s-2); }
  .topbar__crumb { display: none; }
  .topbar__title h1 { font-size: var(--fs-base); }

  .drawer, .drawer--wide { width: 100vw; border-left: 0; }
  .drawer__foot, .modal__foot { flex-wrap: wrap; }
  .drawer__body { padding: var(--s-4); }
  .modal, .modal--lg { width: calc(100vw - 20px); max-height: calc(100vh - 120px); }
  .toaster { bottom: calc(72px + env(safe-area-inset-bottom)); width: calc(100vw - 24px); }
}

@media (max-width: 860px) {
  .hide-sm { display: none !important; }
  .topbar__crumb { max-width: 46vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
