/* layout.css: the app shell (README §12.4) and the layout helpers every view shares.
   Breakpoints: compact < 768 px, medium 768–1023 px, wide ≥ 1024 px.

   The shell switches layout through a handful of custom properties, set once per breakpoint below.
   [data-preview] forces one layout regardless of the viewport: the component gallery uses it to show the
   wide and the compact shell side by side. Nothing in the shell is position: fixed; the bars are sticky,
   so the same markup also works inside a scrolling preview frame. */
@layer layout {
  .shell {
    --shell-height: var(--viewport-height);
    /* compact and medium */
    --sidenav-display: none;
    --bottomnav-display: flex;
    --topbar-brand-display: flex;
    --topbar-account-display: flex;
    --topbar-actions-display: none;
    --back-label-display: none;
    --content-inset: var(--spacing-4);
    --panel-inset: var(--spacing-4);
    --page-gap: var(--spacing-4);
    --page-title-size: var(--text-xl);
    --page-title-leading: var(--leading-xl);
    --topbar-heading-direction: column; /* compact: the meta (autosave label) sits under the title */
    --topbar-heading-align: stretch;
    --actionbar-offset: calc(var(--bottomnav-height) + env(safe-area-inset-bottom, 0px));
    --actionbar-button-height: var(--control-height-lg);
    --actionbar-primary-flex: 1 1 auto; /* compact: the primary (or danger) action stretches */
    --safe-bottom: 0px;

    display: flex;
    align-items: stretch;
    min-height: var(--shell-height);
  }
  @media (min-width: 768px) {
    .shell {
      --page-title-size: var(--text-2xl);
      --page-title-leading: var(--leading-2xl);
      --topbar-heading-direction: row;
      --topbar-heading-align: center;
      --actionbar-primary-flex: none;
    }
  }
  @media (min-width: 1024px) {
    .shell {
      --sidenav-display: flex;
      --bottomnav-display: none;
      --topbar-brand-display: none;
      --topbar-account-display: none;
      --topbar-actions-display: flex;
      --back-label-display: inline;
      --content-inset: var(--spacing-6);
      --panel-inset: var(--spacing-6);
      --page-gap: var(--spacing-6);
      --actionbar-offset: 0px;
      --actionbar-button-height: var(--control-height);
    }
  }
  .shell[data-preview='compact'] {
    --sidenav-display: none;
    --bottomnav-display: flex;
    --topbar-brand-display: flex;
    --topbar-account-display: flex;
    --topbar-actions-display: none;
    --back-label-display: none;
    --content-inset: var(--spacing-4);
    --panel-inset: var(--spacing-4);
    --page-gap: var(--spacing-4);
    --page-title-size: var(--text-xl);
    --page-title-leading: var(--leading-xl);
    --topbar-heading-direction: column;
    --topbar-heading-align: stretch;
    --actionbar-offset: var(--bottomnav-height);
    --actionbar-button-height: var(--control-height-lg);
    --actionbar-primary-flex: 1 1 auto;
  }
  .shell[data-preview='wide'] {
    --sidenav-display: flex;
    --bottomnav-display: none;
    --topbar-brand-display: none;
    --topbar-account-display: none;
    --topbar-actions-display: flex;
    --back-label-display: inline;
    --content-inset: var(--spacing-6);
    --panel-inset: var(--spacing-6);
    --page-gap: var(--spacing-6);
    --page-title-size: var(--text-2xl);
    --page-title-leading: var(--leading-2xl);
    --topbar-heading-direction: row;
    --topbar-heading-align: center;
    --actionbar-offset: 0px;
    --actionbar-button-height: var(--control-height);
    --actionbar-primary-flex: none;
  }
  /* Focused flows (the claim editor on a phone) may hide the bottom nav: ctx.setHeader({ bottomNav: false }). */
  .shell[data-bottomnav='hidden'] {
    --bottomnav-display: none;
    --actionbar-offset: 0px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
  }

  /* ---- SideNav (wide) ------------------------------------------------------------------------- */
  .sidenav {
    position: sticky;
    top: 0;
    display: var(--sidenav-display);
    flex: none;
    flex-direction: column;
    gap: var(--spacing-4);
    align-self: flex-start;
    width: var(--sidenav-width);
    height: var(--shell-height);
    padding: 0 var(--spacing-4) var(--spacing-4);
    overflow-y: auto;
    background: var(--color-surface-1);
    border-inline-end: var(--border-hairline) solid var(--color-border);
  }
  .sidenav__footer {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-1);
    margin-block-start: auto;
    padding-block-start: var(--spacing-3);
    border-top: var(--border-hairline) solid var(--color-border);
  }

  /* ---- Body column: top bar, offline banner, main, page action bar, bottom nav ------------------ */
  .shell__body {
    display: flex;
    flex: 1 1 0;
    flex-direction: column;
    min-width: 0;
    min-height: var(--shell-height);
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--spacing-3);
    min-height: calc(var(--topbar-height) + env(safe-area-inset-top, 0px));
    padding-block-start: env(safe-area-inset-top, 0px);
    padding-inline: max(var(--content-inset), env(safe-area-inset-left, 0px))
      max(var(--content-inset), env(safe-area-inset-right, 0px));
    background: var(--color-surface-1);
    border-bottom: var(--border-hairline) solid var(--color-border);
  }
  /* The compact top bar always shows the mark, linking to / (README §12.8 #3), a back link or not. */
  .topbar__brand {
    display: var(--topbar-brand-display);
    flex: none;
    align-items: center;
    justify-content: flex-start;
    min-width: var(--control-height);
    min-height: var(--control-height);
  }
  .topbar__back {
    display: inline-flex;
    flex: none;
    align-items: center;
    gap: var(--spacing-1);
    min-width: var(--control-height);
    min-height: var(--control-height);
    color: var(--color-accent-text);
    font-weight: var(--weight-medium);
    text-decoration: none;
    white-space: nowrap;
  }
  .topbar__back-label {
    display: var(--back-label-display);
  }
  /* The slot the back link is rendered into: no box of its own, so an empty slot adds no gap. */
  .topbar__back-slot {
    display: contents;
  }
  /* The title and the meta (the autosave label) share one block: side by side from medium; on compact the meta
     sits under the title, so a status never squeezes a phone's title down to a few letters. */
  .topbar__heading {
    display: flex;
    flex: 1 1 0;
    flex-direction: var(--topbar-heading-direction, row);
    align-items: var(--topbar-heading-align, center);
    column-gap: var(--spacing-3);
    min-width: 0;
  }
  .topbar__title {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    font-size: var(--page-title-size);
    line-height: var(--page-title-leading);
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .topbar__meta {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--spacing-2);
    color: var(--color-text-muted);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    line-height: var(--leading-xs);
    white-space: nowrap;
  }
  .topbar__meta:empty,
  .topbar__actions:empty {
    display: none;
  }
  .topbar__actions {
    display: var(--topbar-actions-display);
    flex: none;
    align-items: center;
    gap: var(--spacing-2);
  }
  .topbar__account {
    display: var(--topbar-account-display);
    flex: none;
  }

  /* shell.js measures the bars (--shell-topbar-height, --shell-top-inset: the top bar plus an open offline banner,
     --shell-bottom-inset: the action bar plus the bottom nav); the token values are the fallback before that. */
  .shell__banner {
    position: sticky;
    top: var(--shell-topbar-height, calc(var(--topbar-height) + env(safe-area-inset-top, 0px)));
    z-index: var(--z-sticky);
    flex: none;
  }

  .main {
    /* Sticky table headers stop under the sticky top bar, and under the offline banner while it is open. */
    --table-sticky-top: var(--shell-top-inset, calc(var(--topbar-height) + env(safe-area-inset-top, 0px)));
    display: flex;
    flex: 1 0 auto;
    flex-direction: column;
    min-width: 0;
    padding: var(--content-inset);
    padding-block-end: var(--spacing-8);
  }
  /* Pages without the shell (sign-in, 404 when signed out) own the whole viewport and their own insets. */
  .main--bare {
    --table-sticky-top: 0px;
    min-height: var(--viewport-height);
    padding: 0;
  }
  .view {
    display: flex;
    flex: 1 0 auto;
    flex-direction: column;
    min-width: 0;
  }

  /* The page action bar: the primary action's home on compact and medium (README §12.1). Its buttons' own
     rules (height, stretch) are in components.css, after .btn: the components layer wins over this one.
     It wraps rather than pushing an action off screen when a view gives it more than fits. */
  .actionbar {
    position: sticky;
    bottom: var(--actionbar-offset, 0px);
    z-index: var(--z-sticky);
    display: flex;
    flex: none;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: var(--spacing-3);
    padding: var(--spacing-3) max(var(--content-inset, var(--spacing-4)), env(safe-area-inset-right, 0px))
      calc(var(--spacing-3) + var(--safe-bottom, 0px))
      max(var(--content-inset, var(--spacing-4)), env(safe-area-inset-left, 0px));
    background: var(--color-surface-1);
    border-top: var(--border-hairline) solid var(--color-border);
  }
  .actionbar:empty {
    display: none;
  }
  .actionbar > [data-align='start'] {
    margin-inline-end: auto;
  }

  .bottomnav {
    position: sticky;
    bottom: 0;
    z-index: var(--z-nav);
    display: var(--bottomnav-display);
    flex: none;
    min-height: calc(var(--bottomnav-height) + env(safe-area-inset-bottom, 0px));
    padding-block-end: env(safe-area-inset-bottom, 0px);
    padding-inline: env(safe-area-inset-left, 0px) env(safe-area-inset-right, 0px);
    background: var(--color-surface-1);
    border-top: var(--border-hairline) solid var(--color-border);
  }
  /* Five columns, so the New claim square is always the middle one (README §12.4): Overview, Claims, New, Approvals,
     Account. Without Approvals (employees) Account takes the last two columns, centred in the right half. */
  .bottomnav__list {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    width: 100%;
  }
  .bottomnav__list[data-items='4'] > li:last-child {
    grid-column: span 2;
  }

  /* ---- Page layout helpers ---------------------------------------------------------------------- */
  /* A page shares the top bar's left axis: content starts at the inset and is capped, never centred. */
  .page {
    display: flex;
    flex-direction: column;
    gap: var(--page-gap, var(--spacing-6));
    width: 100%;
    max-width: var(--content-max);
    min-width: 0;
  }
  .page--form {
    max-width: var(--form-max);
  }
  .page--center {
    flex: 1 0 auto;
    align-items: center;
    justify-content: center;
    max-width: none;
  }

  .section {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-3);
    min-width: 0;
  }
  .section__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--spacing-3);
    min-width: 0;
  }

  .stack {
    display: flex;
    flex-direction: column;
    gap: var(--stack-gap, var(--spacing-4));
    min-width: 0;
  }
  .stack--1 {
    --stack-gap: var(--spacing-1);
  }
  .stack--2 {
    --stack-gap: var(--spacing-2);
  }
  .stack--3 {
    --stack-gap: var(--spacing-3);
  }
  .stack--6 {
    --stack-gap: var(--spacing-6);
  }
  .stack--8 {
    --stack-gap: var(--spacing-8);
  }

  .cluster {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--cluster-gap, var(--spacing-3));
    min-width: 0;
  }
  .cluster--1 {
    --cluster-gap: var(--spacing-1);
  }
  .cluster--2 {
    --cluster-gap: var(--spacing-2);
  }
  .cluster--4 {
    --cluster-gap: var(--spacing-4);
  }
  .cluster--between {
    justify-content: space-between;
  }
  .cluster--end {
    justify-content: flex-end;
  }
  .cluster--top {
    align-items: flex-start;
  }
  .cluster--baseline {
    align-items: baseline;
  }
  .cluster--nowrap {
    flex-wrap: nowrap;
  }
  .grow {
    flex: 1 1 0;
    min-width: 0;
  }
  .push-end {
    margin-inline-start: auto;
  }

  .grid {
    display: grid;
    gap: var(--grid-gap, var(--spacing-4));
    min-width: 0;
  }
  /* KPI tiles: 2 × 2 on compact and medium, one row of four on wide (README §13.3). */
  .grid--kpi {
    --grid-gap: var(--spacing-3);
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .grid--cards {
    grid-template-columns: repeat(auto-fill, minmax(min(var(--card-min-width), 100%), 1fr));
  }
  .grid--2,
  .grid--3 {
    grid-template-columns: minmax(0, 1fr);
  }
  @media (min-width: 768px) {
    .grid--2 {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .grid--3 {
      grid-template-columns: repeat(3, minmax(0, 1fr));
    }
  }
  @media (min-width: 1024px) {
    .grid--kpi {
      --grid-gap: var(--spacing-4);
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }
  }

  /* Main column plus a fixed aside (claim detail with the approver panel). Stacks below wide. */
  .split {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--spacing-6);
    align-items: start;
  }
  @media (min-width: 1024px) {
    .split {
      grid-template-columns: minmax(0, 1fr) var(--aside-width);
    }
    .split--even {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .split__aside {
      position: sticky;
      top: calc(var(--shell-top-inset, var(--topbar-height)) + var(--spacing-6));
    }
  }

  /* A surface that holds edge-to-edge content: tables, list rows, timelines. */
  .panel {
    min-width: 0;
    background: var(--color-surface-1);
    border: var(--border-hairline) solid var(--color-border);
  }
  .panel__header,
  .panel__footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--spacing-3);
    min-height: var(--table-row-height);
    padding: var(--spacing-1) var(--panel-inset, var(--spacing-4));
  }
  .panel__header {
    border-bottom: var(--border-hairline) solid var(--color-border);
  }
  .panel__footer {
    border-top: var(--border-hairline) solid var(--color-border);
  }
  .panel__body {
    padding: var(--panel-inset, var(--spacing-4));
  }

  /* Filters above a table: controls wrap, labels stay above their fields. */
  .toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: var(--spacing-3);
    min-width: 0;
  }
  .toolbar__grow {
    flex: 1 1 var(--col-person);
    min-width: 0;
  }

  .divider {
    width: 100%;
  }
}
