/* App shell (lib/shell.js): side rail on desktop (labelled ≥ 1280 px, compact 901–1279 px), top bar +
   bottom tab bar on phones (≤ 900 px), user menu (popover), connection banner, NEW-alarm counter.
   Owned by lib — views never style the shell. */

.shell { display: grid; grid-template-columns: var(--nav-w) minmax(0, 1fr); height: 100dvh; background: var(--bg); }
.shell-main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
main { flex: 1; overflow: hidden; min-height: 0; position: relative; }
.topbar, .tabbar { display: none; }

.skip-link { position: absolute; left: var(--sp-2); top: -60px; z-index: var(--z-tooltip); padding: 8px 14px; border-radius: var(--r-sm); background: var(--accent); color: var(--on-accent); font-weight: var(--fw-semibold); }
.skip-link:focus { top: var(--sp-2); }

/* ---- side rail ------------------------------------------------------------------------------------------------ */
.sidenav { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--line); padding: var(--sp-3) var(--sp-2); gap: var(--sp-3); z-index: var(--z-nav); }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 8px; color: var(--ink); text-decoration: none; }
.brand:hover { text-decoration: none; }
.brand img { flex: none; display: block; }
.brand-word { font-weight: var(--fw-bold); font-size: var(--fs-lg); letter-spacing: .14em; text-transform: uppercase; }
.nav-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.nav-item {
  position: relative; display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 9px 10px; border-radius: var(--r-md); border: 0; background: none;
  color: var(--ink-2); font-weight: var(--fw-medium); text-decoration: none; text-align: left;
  transition: background-color var(--dur-2) var(--ease-out), color var(--dur-2) var(--ease-out);
}
.nav-item:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.nav-item[aria-current="page"] { background: var(--surface-3); color: var(--ink); }
.nav-item[aria-current="page"]::before { content: ""; position: absolute; left: -8px; top: 8px; bottom: 8px; width: 3px; border-radius: 0 3px 3px 0; background: var(--accent); }
.nav-item .icon { flex: none; }
.nav-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidenav-foot { margin-top: auto; display: flex; flex-direction: column; gap: var(--sp-2); padding-top: var(--sp-3); border-top: 1px solid var(--line); }
.tenant select { width: 100%; }
.tenant-name { padding: 0 10px; color: var(--ink-2); font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn-state { display: flex; align-items: center; gap: 8px; padding: 0 10px; font-size: var(--fs-sm); color: var(--ink-2); }
.conn-state:not(.ok) { color: var(--danger-ink); }
.conn-state:not(.ok) .conn { animation: pulse-dot 1.4s var(--ease-in-out) infinite; }
/* The healthy state is silent: the rail only says something when the live link is down ("Reconnecting…"). */
.sidenav .conn-state.ok { display: none; }

/* "Mutes · N" (only while something is muted): quiet, neutral — a reminder, not an alarm. */
.nav-mutes[hidden] { display: none; }
.nav-mutes { margin-top: var(--sp-2); color: var(--ink-2); }
.nav-badge { min-width: 22px; height: 22px; padding: 0 6px; border-radius: var(--r-pill); display: inline-grid; place-items: center;
  background: var(--neutral-soft); color: var(--ink-2); border: 1px solid var(--line-strong); font: var(--fw-semibold) var(--fs-xs)/1 var(--font-mono); }
.menu-item .nav-badge { margin-left: auto; }
.menu-item[data-mutes][hidden] { display: none; }

/* The NEW-alarm counter: filled danger, pulsing ring while any alarm is unacknowledged. */
.nav-count {
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: var(--r-pill); display: inline-grid; place-items: center;
  background: var(--danger); color: var(--on-danger); font: var(--fw-bold) var(--fs-xs)/1 var(--font-mono);
}
.nav-count[hidden] { display: none; }
.has-new .nav-count { animation: ring var(--pulse) var(--ease-out) infinite; }
@keyframes ring { 0% { box-shadow: 0 0 0 0 var(--danger-line); } 70%, 100% { box-shadow: 0 0 0 7px transparent; } }
@keyframes pulse-dot { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) {
  .has-new .nav-count, .has-new .alarm-pill { animation: none !important; box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--danger); }
  .conn-state:not(.ok) .conn { animation: none !important; }
}

/* compact rail: icons over small labels */
@media (max-width: 1279px) and (min-width: 901px) {
  :root { --nav-w: 84px; }
  .sidenav { padding: var(--sp-3) 6px; align-items: stretch; }
  .brand { justify-content: center; padding: 4px 0 8px; }
  .brand-word { display: none; }
  .nav-item { flex-direction: column; gap: 3px; padding: 8px 2px; text-align: center; font-size: var(--fs-2xs); }
  .nav-item[aria-current="page"]::before { left: -6px; }
  .nav-label { width: 100%; }
  .nav-item .nav-count, .nav-mutes .nav-badge { position: absolute; top: 2px; right: 8px; min-width: 18px; height: 18px; font-size: var(--fs-2xs); padding: 0 4px; }
  .tenant, .conn-state .conn-label { display: none; }
  .conn-state { justify-content: center; padding: 0; }
  .nav-user .nav-label { display: none; }
  .nav-user { align-items: center; }
}

/* ---- shift (lib/shift.js): the rail button + the Shift & handover dialog --------------------------------------------- */
.nav-shift { font-size: var(--fs-sm); }
.nav-shift.shift-empty { color: var(--warn-ink); }
.shift-dot { position: absolute; top: 9px; left: 26px; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 2px var(--surface); }
.shift-dot[hidden] { display: none; }
.shift-dlg { width: min(560px, calc(100vw - 32px)); }
.shift-sec { display: flex; flex-direction: column; gap: var(--sp-2); padding: var(--sp-3) 0; border-top: 1px solid var(--line); }
.shift-sec:first-of-type { border-top: 0; padding-top: 0; }
.shift-row { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); }
.shift-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--sp-2); }
.shift-list li { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-1) var(--sp-2); }
.shift-list .mono { margin-left: auto; }
.shift-av { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; background: var(--ok-soft); color: var(--ok-ink); border: 1px solid var(--ok-line); font-weight: var(--fw-bold); font-size: var(--fs-xs); }
.shift-form { display: flex; flex-direction: column; gap: var(--sp-2); }
.shift-form textarea { width: 100%; resize: vertical; }
.shift-hint { font-size: var(--fs-xs); }
.shift-notes { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--sp-2); max-height: 44vh; overflow: auto; }
.shift-notes li { padding: var(--sp-2) var(--sp-3); background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r-md); }
.shift-notes p { white-space: pre-wrap; font-size: var(--fs-sm); margin-top: 2px; }
.shift-nmeta { display: flex; align-items: baseline; justify-content: space-between; gap: var(--sp-2); font-size: var(--fs-sm); }
@media (max-width: 1279px) and (min-width: 901px) { .nav-shift .nav-label { font-size: var(--fs-2xs); } .shift-dot { left: 50%; margin-left: 6px; top: 6px; } }

/* ---- connection banner ---------------------------------------------------------------------------------------- */
.conn-banner { display: flex; align-items: center; gap: var(--sp-2); padding: 8px var(--gutter); background: var(--danger); color: var(--on-danger); font-weight: var(--fw-semibold); font-size: var(--fs-sm); }
.conn-banner[hidden] { display: none; }
.conn-banner .icon { flex: none; }

/* ---- user menu (popover) ---------------------------------------------------------------------------------------- */
.menu {
  position: fixed; inset: auto auto 12px calc(var(--nav-w) + 8px); margin: 0; width: 290px; max-height: calc(100dvh - 24px); overflow: auto;
  padding: var(--sp-2); border: 1px solid var(--line-strong); border-radius: var(--r-lg); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-3);
}
.menu:popover-open { animation: menu-in var(--dur-3) var(--ease-out); }
@keyframes menu-in { from { opacity: 0; transform: translateY(6px); } }
.menu-head { padding: var(--sp-2) var(--sp-2) var(--sp-3); border-bottom: 1px solid var(--line); margin-bottom: var(--sp-1); }
.menu-user { font-weight: var(--fw-semibold); overflow-wrap: anywhere; }
.menu-sub { font-size: var(--fs-sm); color: var(--ink-2); }
.menu-group { padding: var(--sp-1) 0; border-bottom: 1px solid var(--line); }
.menu-group:last-child { border-bottom: 0; }
.menu-group .seg { display: flex; margin: 2px var(--sp-1) var(--sp-2); }
.menu-group .tenant { padding: var(--sp-1); }
.menu-label { font-size: var(--fs-xs); color: var(--ink-2); padding: var(--sp-1) var(--sp-2); font-weight: var(--fw-semibold); }
.menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px var(--sp-2); border: 0; border-radius: var(--r-sm);
  background: none; color: var(--ink); text-align: left; text-decoration: none; font-weight: var(--fw-medium);
}
.menu-item:hover { background: var(--surface-2); text-decoration: none; }
.menu-item .icon { color: var(--ink-2); flex: none; }
.menu-item kbd { margin-left: auto; }
.menu-item[aria-current="page"] { background: var(--surface-3); }
.menu-phone { display: none; }

/* ---- phones: top bar + bottom tab bar --------------------------------------------------------------------------- */
@media (max-width: 900px) {
  :root { --shell-top: var(--topbar-h); --shell-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px)); }
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; }
  .sidenav { display: none; }
  .topbar {
    display: flex; align-items: center; gap: var(--sp-2); height: var(--topbar-h); padding: 0 var(--sp-2) 0 var(--sp-3);
    padding-top: env(safe-area-inset-top, 0px); box-sizing: content-box;
    background: var(--surface); border-bottom: 1px solid var(--line); z-index: var(--z-nav);
  }
  .topbar .brand { padding: 0; }
  .topbar-title { flex: 1; min-width: 0; font-weight: var(--fw-semibold); font-size: var(--fs-lg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  /* The NEW count is a real button (.countpill, 40 px on touch): it opens the most urgent new alarm. */
  .alarm-pill { border: 0; cursor: pointer; font-family: var(--font-ui); padding: 4px 14px; }
  .alarm-pill[hidden] { display: none; }
  .alarm-pill:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
  .has-new .alarm-pill { animation: ring var(--pulse) var(--ease-out) infinite; }
  .conn-compact { padding: 0 4px; }
  .conn-compact.ok { display: none; }             /* only worth a pixel when something is wrong */
  .tabbar {
    display: flex; height: var(--shell-bottom); padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--surface); border-top: 1px solid var(--line); z-index: var(--z-nav);
  }
  .tab {
    flex: 1; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    border: 0; background: none; color: var(--ink-2); font-size: var(--fs-2xs); font-weight: var(--fw-semibold); text-decoration: none;
  }
  .tab:hover { text-decoration: none; color: var(--ink); }
  .tab[aria-current="page"], .tab.on { color: var(--ink); }
  .tab[aria-current="page"]::before, .tab.on::before { content: ""; position: absolute; top: 0; left: 28%; right: 28%; height: 3px; border-radius: 0 0 3px 3px; background: var(--accent); }
  .tab .nav-count { position: absolute; top: 5px; left: calc(50% + 6px); min-width: 18px; height: 18px; font-size: var(--fs-2xs); padding: 0 4px; }
  .menu { inset: auto 0 0 0; width: 100%; max-height: 85dvh; border-radius: var(--r-xl) var(--r-xl) 0 0; padding-bottom: calc(var(--sp-3) + env(safe-area-inset-bottom, 0px)); }
  @keyframes menu-in { from { transform: translateY(100%); } }
  .menu-phone { display: block; }
  .menu-desk { display: none; }
}

/* ---- client portal brand: the client's mark + "Monitored by <monitoring company>" ------------------------------------ */
.brand-portal { gap: var(--sp-3); align-items: center; min-width: 0; }
.brand-logo { display: block; max-width: 120px; object-fit: contain; border-radius: var(--r-xs); }
.brand-mono { flex: none; display: grid; place-items: center; width: 32px; height: 32px; border-radius: var(--r-md); background: var(--accent-soft); color: var(--accent-ink); font-weight: var(--fw-bold); font-size: var(--fs-lg); border: 1px solid var(--accent); }
.brand-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.brand-client { font-weight: var(--fw-bold); font-size: var(--fs-md); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand-by { font-size: var(--fs-2xs); color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .brand-mono { width: 26px; height: 26px; font-size: var(--fs-sm); }
.topbar .brand-logo { max-width: 72px; }
@media (max-width: 1279px) and (min-width: 901px) { .brand-text { display: none; } }

/* ---- two-factor grace banner (every page until enrolled) ---------------------------------------------------------------- */
.twofa-banner { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-2) var(--gutter); background: var(--caution-soft); border-bottom: 1px solid var(--caution-line); color: var(--ink); font-size: var(--fs-sm); }
.twofa-banner > span { flex: 1; min-width: 0; }
.twofa-banner .icon { flex: none; color: var(--caution-ink); }
@media (max-width: 600px) { .twofa-banner { flex-wrap: wrap; } .twofa-banner .btn { margin-left: auto; } }
/* a client logo is usually wide: the mark on its own line, "Monitored by" under it */
.brand-portal:has(.brand-logo) { flex-direction: column; align-items: flex-start; gap: var(--sp-1); }
.brand-portal:has(.brand-logo) .brand-client { display: none; }
.sidenav .brand-portal .brand-logo { max-width: 170px; max-height: 44px; }
.topbar .brand-portal { flex-direction: row; }
/* Alarm sound blocked by the browser (app.js) — stays until a click/key resumes audio */
.sound-banner { position: sticky; top: 0; z-index: var(--z-toast, 50); display: flex; flex-wrap: wrap; gap: var(--sp-1) var(--sp-2); padding: 8px var(--gutter); background: var(--warn); color: var(--on-warn); font-size: var(--fs-sm); cursor: pointer; }
