/* App shell: the main content column owns scrolling. */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  padding-top: env(safe-area-inset-top);
}

.app-shell-body {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.app-main {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: var(--geist-space-6x);
  min-height: 0;
  overflow-y: auto;
  padding: var(--geist-space-6x)
    calc(var(--geist-space-6x) + env(safe-area-inset-right))
    calc(var(--geist-space-10x) + env(safe-area-inset-bottom))
    calc(var(--geist-space-6x) + env(safe-area-inset-left));
}

.app-main > * {
  flex-shrink: 0;
  max-width: var(--content-max);
}

.app-main > .cockpit-section:not(:first-child) {
  margin-top: var(--geist-space-6x);
}

body:not(:has(.app-shell)) .app-main {
  padding: var(--geist-space-6x);
}

/* Sign-in pages: the photo runs edge to edge, so drop the padding and width cap. */
body:not(:has(.app-shell)) .app-main:has(> .auth-screen) {
  padding: 0;
}

.app-main > .auth-screen {
  max-width: none;
}

/* Sidebar */
.app-sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: contain;
  background: var(--canvas);
  box-shadow: 1px 0 0 var(--ds-gray-alpha-300);
  padding-left: env(safe-area-inset-left);
}

.app-sidebar-head {
  display: flex;
  align-items: center;
  justify-content: center;
  height: var(--header-height);
  padding: 0 var(--geist-space-4x);
  border-bottom: 1px solid var(--hairline);
  flex-shrink: 0;
}

.app-sidebar-brand,
.app-sidebar-brand:hover {
  color: var(--ink);
  text-decoration: none;
}

.app-sidebar-brand {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.app-sidebar-brand .brand {
  font-size: var(--text-lg);
}

.app-sidebar-brand:focus-visible,
.nav-item:focus-visible {
  outline: none;
  box-shadow: var(--ds-focus-ring);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.35em;
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  font-weight: 800;
  line-height: 1;
  letter-spacing: 0.06em;
  white-space: nowrap;
}

/* The R mark is a little taller than the capitals, as in the approved lockup. */
.brand-mark {
  height: 1.05em;
  width: auto;
  flex-shrink: 0;
  fill: var(--brand);
}

.app-sidebar-nav {
  display: grid;
  align-content: start;
  gap: 1px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--geist-space-3x) var(--geist-space-2x);
}

/* Accordion sections: a summary row per department, its pages beneath. The
   browser keeps one open at a time because the <details> share a name. */
.nav-section > summary {
  list-style: none;
}

.nav-section > summary::-webkit-details-marker { display: none; }

.nav-section.is-current > summary {
  color: var(--ink);
  font-weight: 500;
}

.nav-section-chevron {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  color: var(--ink-subtle);
  transition: transform .15s cubic-bezier(.4, 0, .2, 1);
}

.nav-section[open] > summary .nav-section-chevron { transform: rotate(90deg); }

/* The page badges are visible inside an open section; the total shows only
   when the section is closed. */
.nav-section[open] > summary .nav-section-badge { display: none; }

.nav-section-items {
  display: grid;
  gap: 1px;
  margin: 1px 0 var(--geist-space-2x);
  padding-left: calc(16px + var(--geist-space-3x));
}

.nav-subitem {
  display: flex;
  align-items: center;
  gap: var(--geist-space-2x);
  padding: 6px var(--geist-space-3x);
  border-radius: var(--radius);
  color: var(--ink-muted);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  line-height: 20px;
  text-decoration: none;
  transition: background-color .15s cubic-bezier(.4, 0, .2, 1), color .15s cubic-bezier(.4, 0, .2, 1);
}

.nav-subitem:hover {
  background: var(--ds-gray-alpha-100);
  color: var(--ink);
}

.nav-subitem.is-active,
.nav-subitem.is-active:hover {
  background: var(--ds-gray-alpha-200);
  color: var(--ink);
  font-weight: 500;
}

.nav-subitem:focus-visible,
.nav-section > summary:focus-visible {
  outline: none;
  box-shadow: var(--ds-focus-ring);
}

.nav-subitem-label { flex: 1; }

@media (pointer: coarse) {
  .nav-subitem { min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  .nav-section-chevron { transition: none; }
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--geist-space-3x);
  padding: var(--geist-space-2x) var(--geist-space-3x);
  border-radius: var(--radius);
  color: var(--ink-muted);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  font-weight: 400;
  line-height: 20px;
  text-decoration: none;
  cursor: pointer;
  transition: background-color .15s cubic-bezier(.4, 0, .2, 1), color .15s cubic-bezier(.4, 0, .2, 1), box-shadow .15s cubic-bezier(.4, 0, .2, 1);
}

.nav-item:hover {
  background: var(--ds-gray-alpha-100);
  color: var(--ink);
}

.app-sidebar .nav-item.is-active,
.app-sidebar .nav-item.is-active:hover {
  background: var(--ds-gray-alpha-200);
  color: var(--ink);
  font-weight: 500;
}

.nav-item-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: inherit;
}

.nav-item-label {
  flex: 1;
}

.app-sidebar .nav-item .badge {
  border-radius: var(--radius-full);
  background: var(--surface-muted);
  box-shadow: var(--ring);
}

.app-sidebar-foot {
  display: grid;
  flex-shrink: 0;
  gap: 1px;
  padding: var(--geist-space-2x);
  box-shadow: 0 -1px 0 var(--ds-gray-alpha-300);
}

.app-sidebar-foot form {
  width: 100%;
  margin: 0;
}

button.nav-item {
  width: 100%;
  appearance: none;
  background: transparent;
  border: 0;
  font: inherit;
  text-align: left;
}

@media (pointer: coarse) {
  .app-sidebar .nav-item { min-height: 44px; }
}

.app-sidebar-signout {
  width: 100%;
}

.status-dot {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  border-radius: var(--radius-full);
  background: var(--ink-subtle);
}

.status-dot.is-running {
  background: var(--info);
  animation: status-pulse 1.5s var(--ease) infinite;
}

.status-dot.is-ok {
  background: var(--success);
}

.status-dot.is-error {
  background: var(--error);
}

@keyframes status-pulse {
  50% { opacity: 0.35; }
}

/* Topbar */
/* Positioned so the search dropdown stacks above page content (local
   navigation sits one layer below), yet stays under the mobile drawer. */
.app-topbar {
  position: relative;
  z-index: var(--z-topbar);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--geist-space-4x);
  height: var(--header-height);
  padding: 0 var(--geist-space-6x);
  flex-shrink: 0;
  background: color-mix(in srgb, var(--canvas) 80%, transparent);
  backdrop-filter: blur(12px);
  border-left: 1px solid var(--hairline);
  border-bottom: 1px solid var(--hairline);
}

.app-topbar-lead,
.app-topbar-user {
  display: flex;
  align-items: center;
  gap: var(--geist-space-3x);
}

.app-topbar-lead {
  min-width: 0;
}

.app-topbar-title {
  overflow: hidden;
  margin: 0;
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  font-weight: var(--font-weight-semibold);
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-topbar-user {
  flex-shrink: 0;
}

.app-topbar-section,
.app-topbar-crumb-separator {
  color: var(--ink-muted);
  font-weight: 400;
}

/* "+ New": creation shortcuts for the records the user may create. */
.app-topbar-new {
  position: relative;
  flex-shrink: 0;
}

.app-topbar-new > summary {
  list-style: none;
  cursor: pointer;
}

.app-topbar-new > summary::-webkit-details-marker { display: none; }

.app-topbar-new-menu {
  position: absolute;
  top: calc(100% + var(--geist-space-2x));
  right: 0;
  z-index: var(--z-popover);
  display: grid;
  gap: 1px;
  min-width: 220px;
  max-height: calc(100vh - var(--header-height) - var(--geist-space-6x));
  overflow-y: auto;
  padding: var(--geist-space-2x);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--ds-shadow-menu);
}

.app-topbar-new-link {
  padding: var(--geist-space-2x) var(--geist-space-3x);
  border-radius: var(--radius);
  color: var(--ink);
  font-size: var(--text-sm);
  line-height: 20px;
  text-decoration: none;
}

.app-topbar-new-link:hover,
.app-topbar-new-link:focus-visible {
  outline: none;
  background: var(--ds-gray-alpha-100);
}

/* Topbar search (UI/UX U7): the box sits between the title and the user. */
.global-search {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 360px;
}

.global-search-form {
  position: relative;
  margin: 0;
}

.global-search-input {
  width: 100%;
  height: 36px;
  padding: 0 44px 0 var(--geist-space-3x);
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--ds-shadow-border);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 20px;
}

.global-search-input::placeholder { color: var(--ink-muted); }

.global-search-input:focus-visible {
  outline: none;
  box-shadow: var(--ds-shadow-border), var(--ds-focus-ring);
}

.global-search-hint {
  position: absolute;
  top: 50%;
  right: var(--geist-space-2x);
  padding: 0 var(--geist-space);
  transform: translateY(-50%);
  border-radius: var(--radius);
  box-shadow: var(--ds-shadow-border);
  color: var(--ink-muted);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  line-height: 18px;
  pointer-events: none;
}

.global-search-results {
  position: absolute;
  top: calc(100% + var(--geist-space-2x));
  left: 0;
  z-index: var(--z-popover);
  display: block;
  width: max(100%, 420px);
  max-width: calc(100vw - var(--geist-space-6x));
  max-height: min(480px, calc(100vh - 120px));
  padding: var(--geist-space);
  overflow-y: auto;
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--ds-shadow-menu);
}

.global-search-results[hidden] { display: none; }

.global-search-group + .global-search-group { margin-top: var(--geist-space-2x); }

.global-search-group-label {
  margin: 0;
  padding: var(--geist-space-2x) var(--geist-space-3x) var(--geist-space);
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-weight: 500;
  line-height: 16px;
}

.global-search-result {
  display: flex;
  min-height: 36px;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--geist-space-3x);
  padding: var(--geist-space-2x) var(--geist-space-3x);
  border-radius: var(--radius);
  color: var(--ink);
  font-size: var(--text-base);
  line-height: 20px;
  text-decoration: none;
}

.global-search-result:hover,
.global-search-result.is-active { background: var(--surface-active); color: var(--ink); }

.global-search-result:focus-visible {
  outline: none;
  box-shadow: var(--ds-focus-ring);
}

.global-search-result-title {
  font-variant-numeric: tabular-nums;
}

.global-search-result-meta {
  overflow: hidden;
  color: var(--ink-muted);
  font-size: var(--text-sm);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.global-search-empty {
  margin: 0;
  padding: var(--geist-space-3x);
  color: var(--ink-muted);
  font-size: var(--text-base);
}

.global-search-page .global-search-group { max-width: 640px; }

.app-topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--geist-space-1x);
}

.app-topbar-sync {
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Amber when the data is old: topbar sync after 24 hours, floor page after 5 minutes. */
.app-topbar-sync .is-stale,
.auto-refresh-stamp.is-stale {
  color: var(--warning);
  font-weight: 600;
}

.app-topbar-sync .is-failed {
  color: var(--error);
  font-weight: 600;
}

.auto-refresh-stamp {
  margin: 0;
  color: var(--ink-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

.app-topbar-divider {
  flex-shrink: 0;
  padding-inline: var(--geist-space-1x);
  color: var(--hairline-strong);
  font-size: var(--text-base);
  line-height: 1;
}

.app-topbar-account {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  width: 36px;
  height: 36px;
  color: var(--ink-muted);
  cursor: default;
}

.app-topbar-menu.btn {
  display: none;
}

.app-topbar .btn-icon {
  min-width: 36px;
  width: 36px;
  height: 36px;
}

.app-shell-backdrop {
  display: none;
}

@media (max-width: 671px) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr);
  }

  .app-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: var(--z-drawer);
    width: var(--sidebar-width);
    visibility: hidden;
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
    box-shadow: var(--ds-shadow-modal);
    transform: translateX(-100%) scale(var(--ds-motion-overlay-scale));
    transform-origin: left center;
    transition: transform var(--ds-motion-overlay-duration) var(--ds-motion-overlay-timing), visibility var(--ds-motion-overlay-duration);
  }

  .app-shell.is-drawer-open .app-sidebar {
    visibility: visible;
    transform: translateX(0) scale(1);
  }

  .app-shell.is-drawer-open .app-shell-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: var(--z-backdrop);
    background: var(--ds-overlay-backdrop-color);
    opacity: var(--ds-overlay-backdrop-opacity);
    animation: shell-backdrop-in var(--ds-motion-overlay-duration) var(--ds-motion-overlay-timing) both;
  }

  @keyframes shell-backdrop-in {
    from { opacity: 0; }
    to { opacity: var(--ds-overlay-backdrop-opacity); }
  }

  .app-topbar,
  .app-main {
    padding-inline: var(--geist-space-3x);
  }

  .app-topbar-menu.btn {
    display: inline-flex;
    flex-shrink: 0;
    width: 44px;
    height: 44px;
  }

  .app-topbar-user {
    display: none;
  }

  .global-search-hint { display: none; }
  .global-search-input { height: 44px; padding-right: var(--geist-space-3x); }

  /* Full-width dropdown under the topbar instead of hanging off the box. */
  .global-search { position: static; }
  .global-search-results {
    top: 100%;
    right: var(--geist-space-3x);
    left: var(--geist-space-3x);
    width: auto;
  }
}

@media (min-width: 672px) {
  .app-sidebar .app-sidebar-signout {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-sidebar {
    transition: none;
  }

  .status-dot.is-running,
  .app-shell.is-drawer-open .app-shell-backdrop:not([hidden]) {
    animation: none;
  }
}
