/* Portal shell: sidebar, top bar, content frame. Uses tokens from tokens.css.
   Classes are prefixed (app-*, sb-*) to avoid clashing with Bootstrap. */
:root {
  --sb-w: 260px;
  --sb-w-rail: 72px;
  --topbar-h: 60px;
  --sb-muted: #a7aad0;          /* 5.6:1 on #22234A */
  --sb-line: rgba(255, 255, 255, .09);
}

body.app-auth, body.app-anon {
  font-family: var(--font); background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
.app-sprite { position: absolute; width: 0; height: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: 8px; top: -60px; background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: var(--radius-sm); z-index: 200; box-shadow: var(--shadow); }
.skip-link:focus { top: 8px; }

.app-icon { width: 20px; height: 20px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Sidebar ---------- */
.app-sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--sb-w); z-index: 1030;
  background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; border-right: 1px solid var(--sb-line);
  transition: width .2s ease, transform .25s ease;
}
.sb-brand { height: var(--topbar-h); display: flex; align-items: center; padding: 0 20px; border-bottom: 1px solid var(--sb-line); flex: none; }
.sb-brand a { display: block; overflow: hidden; border-radius: 4px; }
.sb-brand img { display: block; height: 32px; width: auto; max-width: none; }
.sb-nav { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 12px 12px 8px; }
.sb-group + .sb-group { margin-top: 14px; }
.sb-label { margin: 0 0 4px; padding: 6px 12px; font-size: 11px; font-weight: 600; letter-spacing: .09em; text-transform: uppercase; color: var(--sb-muted); white-space: nowrap; line-height: 1.25; }
.sb-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.sb-link {
  position: relative; display: flex; align-items: center; gap: 12px; height: 42px; padding: 0 12px;
  color: var(--sidebar-text); text-decoration: none; border-radius: 10px; font-weight: 500; font-size: var(--fs-sm);
  white-space: nowrap; transition: background .15s, color .15s;
}
.sb-link:hover { background: rgba(255, 255, 255, .07); color: var(--sidebar-text-active); }
.sb-link:focus-visible { outline: 2px solid var(--branding-yellow); outline-offset: -2px; box-shadow: none; }
.sb-link[aria-current="page"] { background: var(--sidebar-active-bg); color: var(--sidebar-text-active); font-weight: 600; }
.sb-link[aria-current="page"]::before { content: ""; position: absolute; left: -12px; top: 8px; bottom: 8px; width: 4px; border-radius: 0 4px 4px 0; background: var(--branding-yellow); }
.sb-text { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.sb-tag { font-size: 10px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; padding: 1px 6px; border-radius: 99px; border: 1px solid var(--sb-muted); color: var(--sb-muted); }

.sb-user { flex: none; margin: 8px 12px 12px; padding: 12px; border-radius: 12px; background: rgba(255, 255, 255, .06); border: 1px solid var(--sb-line); }
.sb-user-head { display: flex; align-items: center; gap: 10px; min-width: 0; }
.app-avatar { width: 36px; height: 36px; flex: none; border-radius: 50%; background: var(--branding-light-blue); color: #0e0f22; display: grid; place-items: center; font-weight: 700; font-size: var(--fs-sm); }
.sb-user-meta { min-width: 0; line-height: 1.3; }
.sb-user-name { color: #fff; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sb-user-role { font-size: var(--fs-xs); color: var(--sb-muted); overflow: hidden; text-overflow: ellipsis; }
.sb-user-links { list-style: none; margin: 10px 0 0; padding: 8px 0 0; border-top: 1px solid var(--sb-line); display: grid; gap: 2px; }
.sb-user-links .sb-link { height: 34px; font-size: 13px; }
/* Short screens: keep the nav scrollable and the user card compact so they never overlap */
@media (max-height: 640px) {
  .sb-user { margin: 6px 10px 8px; padding: 8px; }
  .sb-user-links { margin-top: 6px; padding-top: 6px; }
  .sb-user-links .sb-link { height: 30px; }
}

.sb-backdrop { display: none; }

/* Collapsed rail (desktop) */
html[data-sidebar="collapsed"] { --sb-w: var(--sb-w-rail); }
html[data-sidebar="collapsed"] .sb-brand { padding: 0; justify-content: center; }
html[data-sidebar="collapsed"] .sb-brand a { width: 46px; }
html[data-sidebar="collapsed"] .sb-nav { overflow: visible; padding: 12px 10px 8px; }
html[data-sidebar="collapsed"] .sb-label { height: 1px; padding: 0; margin: 8px 8px; background: var(--sb-line); font-size: 0; }
html[data-sidebar="collapsed"] .sb-link { justify-content: center; padding: 0; }
html[data-sidebar="collapsed"] .sb-link[aria-current="page"]::before { left: -10px; }
html[data-sidebar="collapsed"] .sb-text,
html[data-sidebar="collapsed"] .sb-tag,
html[data-sidebar="collapsed"] .sb-user-meta { display: none; }
html[data-sidebar="collapsed"] .sb-user { margin: 8px 10px 12px; padding: 8px 4px; background: none; border: 0; }
html[data-sidebar="collapsed"] .sb-user-head { justify-content: center; }
/* Tooltips in rail mode */
html[data-sidebar="collapsed"] .sb-link[data-label]::after {
  content: attr(data-label); position: absolute; left: calc(100% + 14px); top: 50%; transform: translateY(-50%);
  background: #0e0f22; color: #fff; padding: 5px 10px; border-radius: 6px; font-size: 12px; font-weight: 500;
  white-space: nowrap; pointer-events: none; opacity: 0; visibility: hidden; transition: opacity .12s; box-shadow: var(--shadow); z-index: 90;
}
html[data-sidebar="collapsed"] .sb-link:hover::after,
html[data-sidebar="collapsed"] .sb-link:focus-visible::after { opacity: 1; visibility: visible; }

/* ---------- Main ---------- */
.app-main { margin-left: var(--sb-w); min-height: 100vh; display: flex; flex-direction: column; transition: margin-left .2s ease; min-width: 0; }
.app-topbar {
  position: sticky; top: 0; z-index: 1020; height: var(--topbar-h); display: flex; align-items: center; gap: 12px; padding: 0 24px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.icon-btn {
  width: 38px; height: 38px; flex: none; display: inline-grid; place-items: center; border-radius: var(--radius-sm);
  background: transparent; border: 1px solid transparent; color: var(--text-muted); cursor: pointer; text-decoration: none; padding: 0;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.icon-btn:focus-visible, .avatar-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; box-shadow: var(--focus-ring); }
.nav-toggle { display: none; }
.crumbs { min-width: 0; line-height: 1.25; }
.crumbs ol { list-style: none; margin: 0; padding: 0; display: flex; gap: 6px; font-size: var(--fs-xs); color: var(--text-muted); white-space: nowrap; }
.crumbs .page-name { font-weight: 600; font-size: var(--fs-base); color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-spacer { flex: 1; }
.theme-toggle .i-sun { display: none; }
[data-theme="dark"] .theme-toggle .i-sun { display: block; }
[data-theme="dark"] .theme-toggle .i-moon { display: none; }

.menu-wrap { position: relative; }
.avatar-btn { border: 0; padding: 0; background: none; border-radius: 50%; cursor: pointer; }
.app-menu {
  position: absolute; right: 0; top: calc(100% + 8px); min-width: 210px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 6px; display: none; z-index: 1040;
}
.app-menu.open { display: block; }
.menu-head { padding: 8px 10px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; line-height: 1.3; }
.menu-head small { color: var(--text-muted); }
.app-menu a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--radius-sm); color: var(--text); text-decoration: none; }
.app-menu a:hover { background: var(--surface-2); }

.app-content { padding: 14px 16px 28px; width: 100%; min-width: 0; }

/* Environment banner */
.env-banner {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; padding: 8px 12px 8px 14px;
  border-radius: var(--radius); border: 1px solid color-mix(in srgb, var(--warning) 45%, var(--border));
  background: color-mix(in srgb, var(--warning) 11%, var(--surface)); color: var(--text); font-size: var(--fs-xs);
}
.env-banner .app-icon { color: var(--warning); }
.env-banner strong { font-size: var(--fs-sm); }
.env-banner p { margin: 0; flex: 1; min-width: 200px; color: var(--text-muted); }

/* ---------- Anonymous pages (login, MFA, password reset): no sidebar ---------- */
body.app-anon { min-height: 100vh; }
.anon-corner { position: fixed; right: 16px; top: 16px; z-index: 10; }
.anon-content { width: min(520px, 100%); margin: 0 auto; padding: 56px 16px 32px; }
.anon-logo { background: var(--sidebar-bg); border-bottom: 3px solid var(--branding-yellow); border-radius: var(--radius-lg); padding: 22px; display: grid; justify-items: center; margin-bottom: 20px; }
.anon-logo img { height: 44px; width: auto; }

/* ---------- Responsive ---------- */
@media (max-width: 767.98px) {
  html[data-sidebar="collapsed"] { --sb-w: 260px; }
  .app-sidebar { transform: translateX(-100%); width: 280px; box-shadow: none; transition: transform .25s ease; }
  html[data-nav="open"] .app-sidebar { transform: none; box-shadow: 0 0 60px rgba(0, 0, 0, .5); }
  .sb-backdrop { display: block; position: fixed; inset: 0; z-index: 1025; background: rgba(14, 15, 34, .55); opacity: 0; visibility: hidden; transition: opacity .2s; }
  html[data-nav="open"] .sb-backdrop { opacity: 1; visibility: visible; }
  html[data-nav="open"] body { overflow: hidden; }
  /* undo rail styling on phones: the drawer is always expanded */
  html[data-sidebar="collapsed"] .sb-tag, html[data-sidebar="collapsed"] .sb-user-meta { display: revert; }
  html[data-sidebar="collapsed"] .sb-text { display: block; }
  html[data-sidebar="collapsed"] .sb-link { justify-content: flex-start; padding: 0 12px; }
  html[data-sidebar="collapsed"] .sb-link::after { display: none; }
  html[data-sidebar="collapsed"] .sb-label { height: auto; padding: 6px 12px; margin: 0 0 4px; background: none; font-size: 11px; }
  html[data-sidebar="collapsed"] .sb-brand { padding: 0 20px; justify-content: flex-start; }
  html[data-sidebar="collapsed"] .sb-brand a { width: auto; }
  html[data-sidebar="collapsed"] .sb-nav { overflow-y: auto; padding: 12px 12px 8px; }
  html[data-sidebar="collapsed"] .sb-user { margin: 8px 12px 12px; padding: 12px; background: rgba(255, 255, 255, .06); border: 1px solid var(--sb-line); }
  html[data-sidebar="collapsed"] .sb-user-head { justify-content: flex-start; }
  .app-main { margin-left: 0; }
  .nav-toggle { display: inline-grid; }
  .collapse-toggle { display: none; }
  .app-topbar { padding: 0 12px; gap: 6px; }
  .app-content { padding: 14px 12px 32px; }
  .crumbs ol { display: none; }
}
@media (prefers-reduced-motion: reduce) { .app-sidebar, .app-main, .sb-link { transition: none !important; } }

/* Compact environment banner on phones: keep the label and the button, drop the sentence */
@media (max-width: 575.98px) {
  .app-content > .env-banner { flex-wrap: nowrap; padding: 6px 10px; margin-bottom: 12px; }
  .app-content > .env-banner p { display: none; }
  .app-content > .env-banner strong { flex: 1; font-size: var(--fs-xs); }
}
