/* This entire file's content lives in the `layout` cascade layer — see
 * base.css's top-of-file `@layer base, layout, components, utilities;`
 * declaration for why (utility classes always win without `!important`,
 * base/layout/components keep their existing relative precedence
 * unchanged). No rules below were touched, only this wrapper added. */
@layer layout {

/* ── Accessibility ────────────────────────────────────────────────────────── */
.skip-link {
  position: absolute;
  top: -100%;
  left: var(--space-4);
  z-index: 9999;
  background: var(--primary);
  color: var(--on-primary);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  font-weight: 600;
  text-decoration: none;
  transition: top 0.1s;
}
.skip-link:focus { top: var(--space-2); }

/* ── App shell ────────────────────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-rows: 56px auto auto auto 1fr;
  grid-template-columns: 220px 1fr;
  grid-template-areas:
    "topbar  topbar"
    "update  update"
    "banner  banner"
    "impersonation impersonation"
    "sidebar main";
  min-height: 100vh;
  min-height: 100dvh;
}

#attention-banner { grid-area: banner; }
#attention-banner:empty { display: none; }

/* Impersonation banner (components/impersonation-banner.js, CLAUDE.md
   §3.1g) gets its own grid row below the ordinary attention banner — both
   can be visible at once (the target's own attention-needed rule can still
   fire during an impersonation session), each spanning the full width the
   same way #attention-banner does. */
#impersonation-banner { grid-area: impersonation; }

/* "App updated" notice (components/update-banner.js): its own row directly
   under the topbar, independent of the attention banner's re-rendered slot;
   only present after an asset answered 410, so the row is otherwise empty. */
#update-banner { grid-area: update; }

/* ── Topbar ───────────────────────────────────────────────────────────────── */
#topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-6);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 100;
}

#topbar .brand {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
  font-size: var(--text-lg);
  color: var(--primary);
  letter-spacing: -0.02em;
  text-decoration: none;
}

#topbar .spacer { flex: 1; }

#topbar .user-info {
  font-size: var(--text-sm);
  color: var(--muted);
}

/* Shared treatment for every plain icon-only topbar button (search, theme,
   notifications): flex-centered, filled hover state, so they read as one
   family instead of each carrying its own copy of the same ruleset (as
   #btn-theme/#btn-notifications used to before menu search added a third). */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--muted);
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
  line-height: 1;
}
.icon-btn:hover { background: var(--surface-sunk); color: var(--text); }

/* Visible keyboard-shortcut hint for the menu-search icon — the shortcut
   already worked (Ctrl/Cmd+K, wired in app.js), it just had no affordance
   beyond a title-attribute tooltip nobody hovers long enough to see. */
#btn-menu-search { gap: var(--space-1); }
.shortcut-hint {
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--muted);
  background: var(--surface-sunk);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1px 5px;
  line-height: 1.4;
}

#notif-badge {
  position: absolute;
  top: 2px; right: 2px;
  background: var(--danger);
  color: var(--on-danger);
  font-size: 0.68rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: var(--r-pill);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

#btn-logout {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: var(--text-sm);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
  transition: color var(--transition), border-color var(--transition);
}
#btn-logout:hover { color: var(--danger); border-color: var(--danger); }

/* Mobile-only (≤768px, below) — the desktop sidebar is always inline, so
   there's nothing to toggle open here. Shown/hidden by display alone, not
   opacity/visibility, so it never becomes an accidental extra Tab stop. */
#btn-nav-toggle { display: none; }

/* ── Sidebar ──────────────────────────────────────────────────────────────── */
#sidebar {
  grid-area: sidebar;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: var(--space-6) 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  overflow-y: auto;
}

/* Off-canvas drawer header (components/nav.js's navDrawerHeaderHtml) — mobile-only,
   see the ≤768px block below; on desktop #sidebar is plain inline nav with
   no such chrome. */
.nav-drawer-header { display: none; }

/* Backdrop behind the mobile drawer (index.html's #nav-backdrop) — hidden
   by default at every width; only ever shown from inside the ≤768px block
   below, and only then when #app carries .nav-open, so a lingering class
   from a resize-while-open edge case can't affect desktop layout at all. */
#nav-backdrop { display: none; }

/* ── Collapsible nav groups ───────────────────────────────────────────────── */
.nav-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: var(--space-3) var(--space-4) var(--space-1);
  background: none;
  border: none;
  font-size: var(--text-eyebrow);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  cursor: pointer;
  transition: color var(--transition);
}
.nav-group-header:hover { color: var(--text); }

.nav-chevron {
  display: inline-flex;
  transition: transform var(--transition);
}
.nav-chevron.collapsed { transform: rotate(-90deg); }

.nav-group-body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-height: 600px;
  transition: max-height var(--transition);
}
.nav-group-body.collapsed { max-height: 0; }

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-4);
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: 500;
  border-radius: 0;
  text-decoration: none;
  transition: background var(--transition), color var(--transition);
  border-left: 3px solid transparent;
}
.nav-link svg { flex-shrink: 0; color: var(--muted); transition: color var(--transition); }
.nav-link:hover {
  background: var(--surface-sunk);
  text-decoration: none;
}
.nav-link:hover svg { color: var(--text); }
.nav-link.active {
  background: var(--p-50);
  color: var(--p-800);
  border-left-color: var(--p-600);
  font-weight: 600;
}
.nav-link.active svg { color: var(--p-600); }

/* Unread-count pill, injected next to the Chat nav link by notifications.js
   (sourced from the same 60s notification poll — no separate fetch). */
.nav-badge {
  margin-left: auto;
  background: var(--danger);
  color: var(--on-danger);
  font-size: 0.68rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--r-pill);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* ── Main content ─────────────────────────────────────────────────────────── */
#main-content {
  grid-area: main;
  padding: var(--space-8);
  overflow-y: auto;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-6);
}

/* ── Scroll-to-top button (components/scroll-to-top.js) ──────────────────── */
/* Fixed to the viewport, not #main-content — so it sits above whatever route
   is currently rendered instead of being wiped by that route's own
   `container.innerHTML` replacement. Toggled via .hidden (utilities layer),
   not its own display rule, so that toggle always wins regardless of layer
   order — see base.css's "Utilities always win" note. */
.scroll-to-top-btn {
  position: fixed;
  right: var(--space-6);
  bottom: var(--space-6);
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: background var(--transition), color var(--transition), transform var(--transition);
}
.scroll-to-top-btn:hover {
  background: var(--primary);
  color: var(--on-primary);
  border-color: var(--primary);
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .scroll-to-top-btn {
    right: var(--space-4);
    bottom: max(var(--space-4), env(safe-area-inset-bottom));
  }
  body.has-bottom-nav .scroll-to-top-btn { bottom: calc(var(--bottom-nav-offset) + var(--space-3)); }
}

/* .page-header h1 needs no override here any more — it reads the new
   role-based --text-title/800/-0.015em straight from base.css's bare h1
   rule, instead of being pinned back down to the old
   flat --text-xl size. */

/* When the user is not logged in, hide the shell chrome */
body.guest #sidebar,
body.guest #topbar,
body.guest #attention-banner,
body.guest .bottom-nav { display: none; }

/* ── Phone bottom bar (components/bottom-nav.js) ─────────────────────────────
   Anesthesiologists and residents only (body.has-bottom-nav), at ≤768px,
   where it replaces the hamburger; More opens the same drawer. Everything
   pinned to the bottom of the viewport moves up by the bar's height. */
.bottom-nav { display: none; }
@media (max-width: 768px) {
  /* On the root so scroll-padding keeps focused and scrolled-to content
     clear of the bar; everything else inherits the offset. */
  :root:has(body.has-bottom-nav) {
    --bottom-nav-offset: calc(60px + env(safe-area-inset-bottom));
    scroll-padding-bottom: var(--bottom-nav-offset);
  }
  body.has-bottom-nav #btn-nav-toggle { display: none; }
  body.has-bottom-nav .bottom-nav:not([hidden]) {
    display: block;
    position: fixed;
    inset: auto 0 0 0;
    z-index: 400;
    padding-bottom: env(safe-area-inset-bottom);
    border-top: 1px solid var(--border);
    background: var(--surface);
    box-shadow: 0 -2px 10px color-mix(in srgb, var(--text) 6%, transparent);
  }
  body.has-bottom-nav #main-content { padding-bottom: calc(var(--space-4) + var(--bottom-nav-offset)); }
}
.bottom-nav-list { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); margin: 0; padding: 0; list-style: none; }
.bottom-nav-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-height: 60px;
  padding: var(--space-1) 2px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: var(--text-xs);
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.bottom-nav-item span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bottom-nav-item[aria-current="page"], .bottom-nav-item.is-active { color: var(--p-600); }
.bottom-nav-item[aria-current="page"]::before, .bottom-nav-item.is-active::before {
  content: '';
  position: absolute;
  inset: 0 25% auto;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: var(--p-600);
}
.bottom-nav-item:focus-visible { outline: 2px solid var(--primary); outline-offset: -3px; }
.bottom-nav-item .bottom-nav-badge { position: absolute; top: 6px; left: calc(50% + 6px); margin: 0; }
@media print {
  .bottom-nav { display: none !important; }
}

body.guest #app {
  display: block;
}

body.guest #main-content {
  padding: 0;
}

/* ── Theme toggle button ─────────────────────────────────────────────────── */
/* color is required here, not optional: buttons don't inherit text colour
   from ancestors the way plain elements do (browsers apply a UA "ButtonText"
   default instead), so without this the button's icon rendered in that
   default dark colour — invisible against the dark-mode topbar. Emoji never
   exposed this, since a colour emoji glyph ignores the CSS `color` property
   entirely; the SVG icon it was replaced with (stroke="currentColor") does
   not. */
/* Legacy emoji-era sizing, kept as a scoped delta on top of .icon-btn. */
#btn-theme { font-size: 1.15rem; }

/* .nav-link.active needs no dark override here any more — it reads colour
   through --p-50/--p-800/--p-600, which already flip under
   [data-theme="dark"] in base.css. */

/* ── Mobile (≤ 768 px) ────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  #app {
    /* No "sidebar" row any more — the drawer is position:fixed (below),
       out of normal grid flow entirely, not a grid cell that just happens
       to be narrow. */
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 56px auto auto auto 1fr;
    grid-template-areas:
      "topbar"
      "update"
      "banner"
      "impersonation"
      "main";
  }

  #topbar {
    padding: 0 var(--space-3);
    gap: var(--space-2);
  }

  /* Name/role label is the least essential item in the row — drop it before
     the row is forced to overflow (icons + Sign out stay reachable). */
  #topbar .user-info {
    display: none;
  }

  /* The shortcut only applies to a physical keyboard — drop it before the
     row is forced to overflow, same reasoning as .user-info above. */
  .shortcut-hint {
    display: none;
  }

  #btn-nav-toggle { display: inline-flex; }

  /* #btn-logout was already the topbar's tightest fit (the only full-text
     button among otherwise icon-only actions) — adding the toggle above
     pushed it past the viewport edge. Icon-only here, like everything else
     in the row; initNavDrawer() (components/nav.js) prepends the icon at boot. */
  #btn-logout .btn-label { display: none; }

  /* Off-canvas drawer: fixed below the sticky topbar, translated fully
     off-screen to the left until #app carries .nav-open (components/nav.js's
     openNavDrawer/closeNavDrawer). Same #sidebar element, same
     renderNavHtml()/wireNavGroups() content as desktop — only the
     positioning changes here, group collapsing and active-link styling
     below are untouched from the base (non-media) rules above. */
  #sidebar {
    grid-area: unset;
    position: fixed;
    top: 56px;
    left: 0;
    bottom: 0;
    width: min(85vw, 300px);
    max-width: 300px;
    border-right: none;
    box-shadow: var(--shadow-lg);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    z-index: 500; /* same tier as .modal-overlay — see shared/base-ui.css */
  }
  #app.nav-open #sidebar {
    transform: translateX(0);
  }

  #nav-backdrop {
    display: block;
    position: fixed;
    inset: 56px 0 0 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 499;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.22s ease;
  }
  #app.nav-open #nav-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .nav-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-4) var(--space-4);
    margin-bottom: var(--space-2);
    border-bottom: 1px solid var(--border);
  }
  .nav-drawer-title {
    font-weight: 700;
    color: var(--primary);
    letter-spacing: -0.02em;
  }

  #main-content {
    padding: var(--space-4);
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }
}

} /* @layer layout */

@layer layout {
@media (max-width: 600px) {
  #topbar { gap: 0; padding-inline: var(--space-2); }
  #topbar .icon-btn, #topbar #btn-logout { width: 44px; height: 44px; flex: 0 0 44px; padding: 0; }
}
}
