/* ==========================================================================
   SHELL — CRM app shell (SCREENS G-02), platform console shell, and the
   system pages (501 "Coming soon" stub, G-04 error pages).

   Class names match templates/layouts/{app,platform}.html, templates/stub.html
   and templates/errors/*. The richer shell partials in templates/components/
   shell/ (brand, icons in the nav, phone bottom bar, avatar menu, banner stack)
   use the same classes, so the layouts can adopt them without CSS changes.

   Look: the CRM is dark by default (UI-DARK-1, Brian's dashboard mockup) —
   navy sidebar, near-black top bar, gold "you are here"; the light theme keeps
   the ivory canvas and the stone sidebar. Colours come only from tokens.
   ========================================================================== */

@layer shell {

  /* ---------------------------------------------------------- app shell
     Look (UI-DARK-1): navy sidebar and near-black top bar from Brian's
     dashboard mockup; light theme keeps the stone sidebar. Every colour
     comes from the --color-shell-* tokens (tokens.css §8.2 / §8.3). */
  .app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
    min-height: 100vh;
    min-height: 100dvh;
  }

  .app-sidebar {
    position: sticky;
    top: 0;
    align-self: start;
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    height: 100vh;
    height: 100dvh;
    padding: var(--space-3) var(--space-3) var(--space-3);
    overflow-y: auto;
    border-right: var(--border-width) solid var(--color-shell-sidebar-border);
    background-color: var(--color-shell-sidebar-bg);
    scrollbar-width: thin;
  }

  /* ---- Brand lockup: gold house mark, spaced serif name, small gold sub-label ---- */
  .app-brand {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1-5);
    padding: var(--space-3) var(--space-2) var(--space-4);
    border-bottom: var(--border-width) solid var(--color-shell-sidebar-border);
    color: var(--color-shell-brand-text);
    text-align: center;
    text-decoration: none;
  }

  .app-brand:hover {
    color: var(--color-shell-brand-text);
  }

  .app-brand:hover .app-brand__name {
    text-decoration: underline;
    text-underline-offset: 0.2em;
  }

  .app-brand__mark {
    display: grid;
    flex: none;
    place-items: center;
    color: var(--color-text-accent);
  }

  .app-brand__logo {
    display: block;
    width: 2.5rem;
    height: auto;
  }

  /* The company logo (N-161), when one is uploaded: at most 2.5rem tall, never stretched, never
     wider than the rail. The name beside it stays the link's accessible name (alt=""). */
  .app-brand__img {
    display: block;
    width: auto;
    max-width: 100%;
    height: 2.5rem;
    object-fit: contain;
  }

  .app-brand__mark--image {
    max-width: 100%;
  }

  .app-brand__text {
    display: grid;
    gap: var(--space-0-5);
    min-width: 0;
    max-width: 100%;
  }

  .app-brand__name {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    font-family: var(--font-family-display);
    font-size: var(--text-md);
    font-weight: var(--font-weight-display-strong);
    letter-spacing: 0.06em;
    line-height: var(--leading-snug);
    text-transform: uppercase;
    text-wrap: balance;
  }

  .app-brand__sub {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    color: var(--color-shell-brand-sub);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-semibold);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
  }

  .app-brand__sub::before,
  .app-brand__sub::after {
    content: "";
    flex: 0 1 1.5rem;
    height: 1px;
    background-color: currentColor;
    opacity: 0.6;
  }

  /* ---- Navigation ---- */
  .app-nav {
    display: grid;
    gap: var(--space-0-5);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .app-nav a {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 2.25rem;
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-control);
    color: var(--color-shell-nav-text);
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    line-height: 1.25;
    text-decoration: none;
    transition: background-color var(--motion-duration-fast) var(--motion-ease-standard),
      color var(--motion-duration-fast) var(--motion-ease-standard);
  }

  .app-nav a .icon {
    color: var(--color-text-muted);
    transition: color var(--motion-duration-fast) var(--motion-ease-standard);
  }

  .app-nav a:hover {
    background-color: var(--color-shell-nav-hover-bg);
    color: var(--color-text);
  }

  .app-nav a:hover .icon {
    color: var(--color-text);
  }

  .app-nav a[aria-current="page"] {
    background-color: var(--color-shell-nav-active-bg);
    box-shadow: inset var(--border-width-accent) 0 0 var(--color-border-accent);
    color: var(--color-shell-nav-active-text);
    font-weight: var(--font-weight-semibold);
  }

  .app-nav a[aria-current="page"] .icon {
    color: var(--color-shell-nav-active-icon);
  }

  .app-nav__label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-nav .count {
    margin-left: auto;
  }

  .app-nav__section {
    margin: 0;
    padding: var(--space-2) var(--space-3) var(--space-1);
    border-top: var(--border-width) solid var(--color-shell-sidebar-border);
    color: var(--color-text-muted);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-semibold);
    letter-spacing: var(--tracking-caps);
    text-transform: uppercase;
  }

  .app-sidebar-footer {
    margin-top: auto;
  }

  .app-sidebar-footer:empty {
    display: none;
  }

  .app-sidebar-footer:empty + .app-sidebar-user {
    margin-top: auto;
  }

  /* ---- Signed-in member at the bottom of the sidebar ---- */
  .app-sidebar-user {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-2) 0;
    border-top: var(--border-width) solid var(--color-shell-sidebar-border);
  }

  .app-sidebar-user__text {
    display: grid;
    min-width: 0;
  }

  .app-sidebar-user__name {
    overflow: hidden;
    color: var(--color-text);
    font-size: var(--text-sm);
    font-weight: var(--font-weight-semibold);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-sidebar-user__meta {
    overflow: hidden;
    color: var(--color-text-muted);
    font-size: var(--text-xs);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ---- Main column and top bar ---- */
  .app-main {
    display: flex;
    flex-direction: column;
    min-width: 0;
  }

  .app-topbar {
    position: sticky;
    top: 0;
    z-index: var(--z-header);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: var(--header-height);
    padding: var(--space-1-5) var(--page-gutter);
    border-bottom: var(--border-width) solid var(--color-border-subtle);
    background-color: var(--color-shell-topbar-bg);
  }

  .app-topbar-brand {
    display: none;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
    color: var(--color-shell-brand-text);
    font-family: var(--font-family-display);
    font-weight: var(--font-weight-display-strong);
    letter-spacing: 0.05em;
    text-decoration: none;
    text-transform: uppercase;
  }

  .app-topbar-brand .app-brand__logo {
    width: 1.75rem;
  }

  .app-topbar-brand .app-brand__img {
    max-width: 6rem;
    height: 1.75rem;
  }

  .app-topbar-brand__name {
    overflow: hidden;
    font-size: var(--text-sm);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-topbar-left,
  .app-topbar-right {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  .app-topbar-left:empty {
    display: none;
  }

  .app-topbar-search {
    flex: 1 1 auto;
    min-width: 0;
    max-width: 36rem;
  }

  .app-topbar-search:empty {
    display: none;
  }

  .app-topbar-search .field {
    gap: 0;
  }

  .app-topbar-search .control {
    min-height: var(--control-height-sm);
    border-radius: var(--radius-full);
    background-color: var(--color-surface);
  }

  .app-topbar-right {
    margin-left: auto;
  }

  .app-account-menu {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-1) var(--space-4);
    margin: 0;
    padding: 0;
    font-size: var(--text-sm);
    list-style: none;
  }

  .app-account-menu a {
    color: var(--color-text-secondary);
    font-weight: var(--font-weight-medium);
    text-decoration: none;
  }

  .app-account-menu a:hover {
    color: var(--color-text);
    text-decoration: underline;
  }

  .app-banners {
    display: grid;
  }

  .app-main > main {
    flex: 1 0 auto;
    width: 100%;
    max-width: var(--container-max);
    padding: var(--space-6) var(--page-gutter) var(--space-12);
  }

  /* Tablet (md–lg): the sidebar collapses to a 72px rail when the nav carries
     icons (the shell partials); icons + short labels, tooltips on hover. */
  @media (min-width: 48em) and (max-width: 63.99em) {
    .app-shell:has(> .app-sidebar .icon) {
      grid-template-columns: var(--sidebar-width-collapsed) minmax(0, 1fr);
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar {
      padding-inline: var(--space-1-5);
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-brand {
      justify-content: center;
      padding: var(--space-2) 0 var(--space-3);
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-brand__text {
      position: absolute;
      width: 1px;
      height: 1px;
      overflow: hidden;
      clip-path: inset(50%);
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-nav a {
      flex-direction: column;
      justify-content: center;
      gap: var(--space-1);
      min-height: 3.5rem;
      padding: var(--space-1-5) var(--space-1);
      font-size: var(--text-xs);
      text-align: center;
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-nav__label {
      max-width: 100%;
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-nav .count {
      position: absolute;
      top: var(--space-1);
      right: var(--space-1);
      margin: 0;
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-nav__section {
      padding-inline: 0;
      text-align: center;
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-sidebar-user {
      justify-content: center;
      padding-inline: 0;
    }

    .app-shell:has(> .app-sidebar .icon) > .app-sidebar .app-sidebar-user__text {
      position: absolute;
      width: 1px;
      height: 1px;
      overflow: hidden;
      clip-path: inset(50%);
    }
  }

  /* Phones (< md): bottom tab bar, 64px, icon + text label (13px minimum). */
  .app-bottom-nav {
    display: none;
  }

  @media (max-width: 47.99em) {
    /* WCAG 2.4.11: focus scrolled into view must clear the fixed bottom bar (CR-W13-7). */
    html:has(.app-bottom-nav) {
      scroll-padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom, 0px) + var(--space-4));
    }

    .app-shell {
      display: block;
      padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom, 0px));
    }

    .app-shell:has(> .app-bottom-nav) > .app-sidebar {
      display: none;
    }

    /* The brand moves into the top bar while the sidebar is hidden. */
    .app-shell:has(> .app-bottom-nav) .app-topbar-brand {
      display: flex;
    }

    /* Layouts without the dedicated phone bar: the sidebar becomes a
       horizontally scrolling bottom bar. */
    .app-shell:not(:has(> .app-bottom-nav)) > .app-sidebar {
      position: fixed;
      inset: auto 0 0 0;
      z-index: var(--z-header);
      flex-direction: row;
      align-items: stretch;
      gap: 0;
      height: auto;
      padding: 0 var(--space-1) env(safe-area-inset-bottom, 0px);
      overflow-x: auto;
      overflow-y: hidden;
      border-top: var(--border-width) solid var(--color-border);
      border-right: 0;
      background-color: var(--color-surface);
    }

    .app-shell:not(:has(> .app-bottom-nav)) > .app-sidebar .app-nav {
      display: flex;
      gap: 0;
    }

    .app-shell:not(:has(> .app-bottom-nav)) > .app-sidebar .app-nav a {
      flex-direction: column;
      justify-content: center;
      gap: var(--space-0-5);
      min-width: 4.75rem;
      min-height: var(--bottom-nav-height);
      padding: var(--space-1) var(--space-2);
      border-radius: 0;
      font-size: var(--text-xs);
      white-space: nowrap;
    }

    .app-shell:not(:has(> .app-bottom-nav)) > .app-sidebar .app-nav a[aria-current="page"] {
      box-shadow: inset 0 var(--border-width-accent) 0 var(--color-border-accent);
    }

    .app-shell:not(:has(> .app-bottom-nav)) > .app-sidebar :is(.app-sidebar-footer, .app-brand, .app-nav__section, .app-sidebar-user) {
      display: none;
    }

    .app-bottom-nav {
      position: fixed;
      inset: auto 0 0 0;
      z-index: var(--z-header);
      display: block;
      padding-bottom: env(safe-area-inset-bottom, 0px);
      border-top: var(--border-width) solid var(--color-border);
      background-color: var(--color-shell-sidebar-bg);
    }

    /* One 56px row: the brand shrinks (ellipsis) before anything wraps, so the sticky bar never
       grows past --header-height and hides focused content (WCAG 2.4.11). */
    .app-topbar {
      flex-wrap: nowrap;
      gap: var(--space-2);
    }

    .app-topbar-brand {
      flex: 1 1 0;
      min-width: 2rem;
    }

    .app-topbar-brand .app-brand__mark {
      flex: none;
    }

    .app-topbar-left,
    .app-topbar-right {
      flex: none;
      gap: var(--space-1);
    }

    /* Phones: the search box would make the sticky top bar taller than --header-height and hide
       focused content under it; Search stays in the "More" sheet instead. */
    .app-topbar-search {
      display: none;
    }

    .app-main > main {
      padding: var(--space-4) var(--page-gutter) var(--space-10);
    }
  }

  .app-bottom-nav__list {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .app-bottom-nav__link {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-0-5);
    width: 100%;
    min-height: var(--bottom-nav-height);
    padding: var(--space-1);
    border: 0;
    background: none;
    color: var(--color-shell-nav-text);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-medium);
    line-height: 1.2;
    text-decoration: none;
  }

  .app-bottom-nav__link[aria-current="page"] {
    color: var(--color-shell-nav-active-text);
    font-weight: var(--font-weight-semibold);
  }

  .app-bottom-nav__link[aria-current="page"]::before {
    content: "";
    position: absolute;
    top: 0;
    right: 25%;
    left: 25%;
    height: var(--border-width-accent);
    border-radius: 0 0 var(--border-width-accent) var(--border-width-accent);
    background-color: var(--color-border-accent);
  }

  .app-bottom-nav__link[aria-current="page"] .icon {
    color: var(--color-shell-nav-active-icon);
  }

  .app-bottom-nav__label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-bottom-nav__link .count {
    position: absolute;
    top: var(--space-1-5);
    left: calc(50% + var(--space-1-5));
  }

  .app-bottom-nav__link:focus-visible {
    outline-offset: calc(-1 * var(--focus-ring-width));
  }

  .more-sheet__list {
    display: grid;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .more-sheet__list a,
  .more-sheet__list button {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    min-height: 3.5rem;
    padding: 0 var(--space-2);
    border: 0;
    border-bottom: var(--border-width) solid var(--color-border-subtle);
    background: none;
    color: var(--color-text);
    font-size: var(--text-md);
    text-align: left;
    text-decoration: none;
  }

  .more-sheet__list a:hover {
    background-color: var(--color-surface-hover);
  }

  /* Account menu (avatar button) in the top bar. */
  .account-menu > summary {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-height);
    padding: 0 var(--space-2) 0 var(--space-1);
    border-radius: var(--radius-full);
    cursor: pointer;
  }

  .account-menu > summary:hover {
    background-color: var(--color-surface-hover);
  }

  .account-menu__who {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3);
    border-bottom: var(--border-width) solid var(--color-border-subtle);
    margin-bottom: var(--space-1);
  }

  .account-menu__photo {
    position: relative;
    display: inline-flex;
    flex: none;
    border-radius: var(--radius-full);
  }

  .account-menu__photo .avatar { --avatar-size: 3rem; border-color: var(--color-border); }

  .account-menu__photo-badge {
    position: absolute;
    right: -0.2rem;
    bottom: -0.2rem;
    display: grid;
    place-items: center;
    width: 1.4rem;
    height: 1.4rem;
    border: 2px solid var(--color-surface);
    border-radius: var(--radius-full);
    background: var(--color-primary-bg);
    color: var(--color-primary-text);
  }

  .account-menu__identity { display: grid; min-width: 0; }
  .account-menu__photo-link { font-size: var(--text-xs); margin-top: var(--space-1); }

  .account-menu__name {
    font-weight: var(--font-weight-semibold);
  }

  .account-menu__meta {
    color: var(--color-text-secondary);
    font-size: var(--text-sm);
  }


  /* ------------------------------------------------------ platform console */
  .platform-topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2) var(--space-4);
    min-height: var(--header-height);
    padding: var(--space-2) var(--page-gutter);
    border-bottom: var(--border-width) solid var(--color-border-subtle);
    /* The same bar as the CRM shell (it was the inverse surface: a white strip above the dark pages). */
    background-color: var(--color-shell-topbar-bg);
    color: var(--color-text);
  }

  .platform-topbar a {
    color: var(--color-text-secondary);
    font-weight: var(--font-weight-medium);
  }

  .platform-topbar a:hover {
    color: var(--color-text);
  }

  .platform-title {
    font-weight: var(--font-weight-semibold);
  }

  .platform-topbar ~ main {
    width: 100%;
    max-width: var(--container-max);
    padding: var(--space-6) var(--page-gutter) var(--space-12);
  }


  /* ------------------------------------ system pages: 501 stub and errors */
  .stub-page,
  .error-page {
    display: grid;
    align-content: center;
    justify-items: start;
    gap: var(--space-3);
    width: 100%;
    max-width: 40rem;
    min-height: 80vh;
    min-height: 80dvh;
    margin: 0 auto;
    padding: var(--space-12) var(--page-gutter);
  }

  .stub-page::before {
    content: "";
    width: 2.5rem;
    height: 2.5rem;
    margin-bottom: var(--space-1);
    background-color: var(--color-text-accent);
    mask: url("../icons/lucide/hourglass.3bc9881188cb.svg") center / contain no-repeat;
  }

  .stub-page > :is(h1, h2, h3),
  .error-page > h1,
  .permission-denied > h1 {
    font-size: var(--text-3xl);
  }

  .stub-page > p,
  .error-page > p,
  .permission-denied > p {
    max-width: var(--measure-narrow);
    color: var(--color-text-secondary);
  }

  .stub-route code {
    color: var(--color-text-secondary);
    font-size: var(--text-sm);
  }

  .permission-denied {
    display: grid;
    justify-items: start;
    gap: var(--space-3);
  }

  .icon-lock {
    position: relative;
    display: grid;
    place-items: center;
    width: 3rem;
    height: 3rem;
    border-radius: var(--radius-full);
    background-color: var(--color-surface-sunken);
  }

  .icon-lock::before {
    content: "";
    width: 1.5rem;
    height: 1.5rem;
    background-color: var(--color-text-secondary);
    mask: url("../icons/lucide/lock.ea710bcf0871.svg") center / contain no-repeat;
  }

  .error-links {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-5);
    margin-top: var(--space-3);
    font-weight: var(--font-weight-medium);
  }

  .error-reference {
    color: var(--color-text-muted);
    font-size: var(--text-xs);
  }
}
