@layer components {
  /* Shared chrome and primitives for the authenticated app surfaces
     (client portal and broker workspace): sidebar, desktop/mobile topbars,
     drawer, content column, cards, panels, modals, tiles, eyebrow labels,
     serif titles, stacks, chips, flat rows, summary carets, and empty
     states. Structural constants default here on .app-body; a surface's
     body class overrides them in its own stylesheet only when it genuinely
     diverges (e.g. --app-content-max-width on .workspace-body). */
  .app-body {
    --app-header-height: 85px; /* keys sidebar/topbar alignment */
    --app-sidebar-width: 236px; /* approved mockup constant */
  }

  /* Sidebar (desktop) */
  .app-sidebar {
    width: var(--app-sidebar-width);
    flex-shrink: 0;
    position: fixed;
    inset: 0 auto 0 0;
    background: var(--surface-card);
    border-right: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    z-index: var(--z-nav);
  }
  .app-sidebar-brand {
    height: var(--app-header-height);
    padding: 0 var(--spacing-md);
    display: flex;
    flex-direction: column;
    justify-content: center;
    border-bottom: 1px solid var(--border-subtle);
  }
  .app-sidebar-subtitle {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    margin-top: var(--spacing-nudge);
  }
  .app-nav {
    padding: var(--spacing-sm) var(--spacing-xs);
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
    overflow-y: auto;
  }
  .app-nav-row {
    --icon-size: 18px;
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
    padding: 12px; /* nav tap-target rhythm */
    border-radius: var(--radius-md);
    text-decoration: none;
    color: var(--text-secondary);
    font-size: var(--text-sm);
    font-weight: 500;
    border-left: 2px solid transparent;
    transition: background 0.2s, color 0.2s;
  }
  .app-nav-row:hover {
    background: var(--hover-wash);
  }
  .app-nav-row.active {
    background: var(--accent-wash);
    border-left-color: var(--accent);
    color: var(--accent);
    font-weight: 600;
  }
  .app-signout {
    display: block;
    width: 100%;
    margin-top: var(--spacing-sm);
    padding: var(--spacing-xxs) var(--spacing-xs);
    background: none;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: var(--text-xs);
    font-weight: 600;
    cursor: pointer;
    transition: border-color 0.2s, color 0.2s;
  }
  .app-signout:hover {
    border-color: var(--accent);
    color: var(--accent);
  }

  /* Main column */
  .app-main {
    display: block;
    margin-left: var(--app-sidebar-width);
    min-height: 100vh;
  }
  .app-content {
    padding: var(--spacing-lg);
    max-width: var(--app-content-max-width, none);
  }
  .app-content > .form-alert {
    margin-bottom: var(--spacing-md);
  }

  /* Topbar (desktop) */
  .app-topbar-desktop {
    height: var(--app-header-height);
    padding: 0 var(--spacing-lg);
    border-bottom: 1px solid var(--border-subtle);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--surface-page);
    position: sticky;
    top: 0;
    z-index: var(--z-nav);
  }
  .app-topbar-desktop h1,
  .app-topbar-mobile-content h1 {
    --title-size: var(--text-xl);
  }
  .app-topbar-actions {
    display: flex;
    gap: var(--spacing-xs);
  }

  /* Base card */
  .app-card {
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
    box-shadow: var(--shadow-card);
  }

  /* Panel: the same chrome unpadded, clipping its own children so full-bleed
     rows (table heads, grouped lists, banded tables) meet the rounded corner.
     Compose with the screen's sizing class: `app-panel ws-client-table`. */
  .app-panel {
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    overflow: hidden;
  }

  /* Modal card. The workspace's native <dialog> and the portal's centered
     request form are the same treatment; --flow is the in-normal-flow variant
     (no ::backdrop, so it centers itself). Width is a per-variant property. */
  .app-modal {
    --app-modal-width: 440px; /* modal-card width per design spec */
    width: var(--app-modal-width);
    max-width: calc(100vw - 2 * var(--spacing-sm));
    border: none;
    border-radius: var(--radius-xl);
    background: var(--surface-card);
    box-shadow: var(--shadow-float);
    padding: var(--spacing-lg);
  }
  .app-modal--wide {
    --app-modal-width: 560px; /* the templates modal needs the extra row width */
  }
  /* In normal flow the width is a cap, not a size: a <dialog> sizes itself
     against the viewport, but a block in the content column has to shrink to
     whatever the column leaves it (and 100vw would over-measure it, because
     the reserved scrollbar gutter is not part of the column). */
  .app-modal--flow {
    width: auto;
    max-width: var(--app-modal-width);
    margin-inline: auto;
  }
  .app-modal::backdrop {
    background: var(--surface-scrim);
  }
  .app-modal h3 {
    margin-bottom: var(--spacing-xxs);
  }
  .app-modal-subtitle {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    margin-bottom: var(--spacing-md);
  }
  .app-modal--flow .form-alert {
    margin-bottom: var(--spacing-sm);
  }
  .app-modal-actions {
    display: flex;
    gap: var(--spacing-xs);
    margin-top: var(--spacing-sm);
  }
  /* Actions split evenly across the card (the dialogs' cancel/submit pair). */
  .app-modal-actions--fill > * {
    flex: 1;
  }
  .app-modal-actions--fill .btn-primary,
  .app-modal-actions--fill .btn-secondary {
    text-align: center;
    justify-content: center;
  }
  /* Trailing destructive action, below the actions row */
  .app-modal-delete {
    margin-top: var(--spacing-sm);
    background: none;
    border: none;
    padding: 0;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--semantic-danger);
    cursor: pointer;
  }

  /* Eyebrow label: the uppercase, letter-spaced micro-heading shared by field
     labels, section eyebrows, kanban column heads, and table heads. A variant
     overrides --label-size/-weight/-color/-tracking instead of restating the
     treatment. (.ws-field > label is in the selector list, not a class on the
     element, because form.label generates those labels with no class of their
     own — so the workspace's form labels get the variant API too.)

     Give a variant a real value, never `inherit`: a CSS-wide keyword arriving
     through var() substitution is invalid at computed-value time, so the
     property lands on its initial value (black text), not the parent's. */
  .app-label,
  .ws-field > label {
    font-size: var(--label-size, var(--text-xs));
    font-weight: var(--label-weight, 600);
    color: var(--label-color, var(--text-secondary));
    text-transform: uppercase;
    letter-spacing: var(--label-tracking, var(--tracking-wide));
  }

  /* Serif heading: card, panel, and section titles. Variants override
     --title-size / --title-weight (never with `inherit` — see .app-label). */
  .app-title {
    font-family: var(--font-serif);
    font-size: var(--title-size, var(--text-lg));
    font-weight: var(--title-weight, 500);
  }

  /* Secondary body line under a row's title, and the tertiary note under a
     value. Repeated across both surfaces' rows, cards, and metrics. */
  .app-desc {
    font-size: var(--text-xs);
    color: var(--text-secondary);
    margin-top: var(--spacing-xxs);
    max-width: 60ch;
  }
  .app-note {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    margin-top: var(--spacing-xxs);
  }

  /* Lead-in copy above a page's first content block (account pages) */
  .app-page-intro {
    font-size: var(--text-sm);
    color: var(--text-secondary);
    max-width: 70ch;
    margin-bottom: var(--spacing-md);
  }

  /* <summary> rows: hide the native marker, and with --caret draw our own
     disclosure triangle that rotates when the <details> is open. */
  .app-summary {
    list-style: none;
  }
  .app-summary::-webkit-details-marker {
    display: none;
  }
  .app-summary--caret::before {
    content: "\25B8";
    color: var(--caret-color, currentColor);
    transition: transform 0.2s;
  }
  [open] > .app-summary--caret::before {
    transform: rotate(90deg);
  }

  /* Vertical list stacks */
  .app-stack {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
  }
  .app-stack--snug {
    gap: var(--spacing-xs);
  }

  /* Meta chips (static or linked) and the row that holds them */
  /* Connected-accounts rows (portal + workspace account pages): provider
     logo + name/meta cluster inside an app-flat-row. */
  .app-identity {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);

    svg {
      width: 18px; /* provider logo, matches the sign-in buttons */
      height: 18px;
      flex: none;
    }
  }

  .app-chip-row {
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
    margin-top: var(--spacing-xs);
    flex-wrap: wrap;
  }
  .app-chip {
    --icon-size: 14px;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text-secondary);
    background: var(--surface-cream-dark);
    padding: var(--spacing-xxs) var(--spacing-xs);
    border-radius: var(--radius-full);
    display: inline-flex;
    align-items: center;
    gap: var(--spacing-xxs);
    text-decoration: none;
  }
  .app-chip:any-link:hover {
    color: var(--accent);
  }

  /* Flat list rows (leading content + trailing meta) */
  .app-flat-row {
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: var(--spacing-sm) var(--spacing-md);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--spacing-sm);
    flex-wrap: wrap;
  }
  .app-flat-row--top {
    align-items: flex-start;
  }

  /* Icon tile: a centered square wash behind an icon. Variants override
     --tile-size / --tile-radius / --tile-wash. */
  .app-tile {
    width: var(--tile-size, 44px); /* icon tile, design-spec */
    height: var(--tile-size, 44px);
    border-radius: var(--tile-radius, var(--radius-md));
    background: var(--tile-wash, var(--accent-wash));
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  /* Empty states (compose with .app-card) */
  .app-empty {
    text-align: center;
    padding: var(--spacing-2xl) var(--spacing-md);
  }
  .app-empty-icon {
    --tile-size: 56px;
    --tile-radius: var(--radius-lg);
    display: inline-flex; /* centered by the empty state's text-align */
    margin-bottom: var(--spacing-sm);
  }
  .app-empty h3 {
    --title-size: var(--text-md);
    margin-bottom: var(--spacing-xxs);
  }
  .app-empty p {
    font-size: var(--text-sm);
    color: var(--text-tertiary);
    max-width: 44ch;
    margin: 0 auto;
  }
  .app-empty p:not(:last-child) {
    margin-bottom: var(--spacing-md);
  }

  /* Mobile topbar + drawer, hidden above the breakpoint */
  .app-topbar-mobile,
  .app-drawer {
    display: none;
  }

  @media (max-width: 860px) {
    .app-topbar-desktop,
    .app-sidebar {
      display: none;
    }
    .app-main {
      margin-left: 0;
    }
    .app-content {
      padding: var(--spacing-sm);
    }

    .app-topbar-mobile {
      display: block;
      position: sticky;
      top: 0;
      z-index: var(--z-sticky);
      background: var(--surface-frost);
      backdrop-filter: blur(16px);
    }
    .app-topbar-mobile-bar {
      padding: var(--spacing-sm);
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid var(--border-subtle);
    }
    .app-hamburger {
      width: 40px; /* minimum comfortable tap target */
      height: 40px;
      border-radius: var(--radius-md);
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: var(--spacing-xxs);
      border: 1px solid var(--border-default);
      background: transparent;
      cursor: pointer;
    }
    .app-hamburger span {
      display: block;
      width: 18px;
      height: 2px;
      background: var(--text-primary);
    }
    .app-topbar-mobile-content {
      padding: var(--spacing-sm);
    }
    .app-topbar-mobile-content h1 {
      --title-size: var(--text-lg);
    }

    .app-drawer {
      display: block;
      position: fixed;
      inset: 0;
      z-index: var(--z-drawer);
      visibility: hidden;
      pointer-events: none;
    }
    .app-drawer.app-drawer--open {
      visibility: visible;
      pointer-events: auto;
    }
    .app-drawer-backdrop {
      position: absolute;
      inset: 0;
      background: var(--surface-scrim);
      opacity: 0;
      transition: opacity 0.2s;
    }
    .app-drawer--open .app-drawer-backdrop {
      opacity: 1;
    }
    .app-drawer-panel {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      width: 78%;
      max-width: 320px; /* drawer cap per design spec */
      background: var(--surface-card);
      box-shadow: var(--shadow-float);
      padding: var(--spacing-md);
      display: flex;
      flex-direction: column;
      gap: 2px;
      transform: translateX(100%);
      transition: transform 0.25s;
    }
    .app-drawer--open .app-drawer-panel {
      transform: translateX(0);
    }
    .app-drawer-title {
      --title-size: var(--text-md);
      margin-bottom: var(--spacing-sm);
    }

    /* Only the flow variant tightens up: a <dialog> already floats above the
       content column and keeps its roomier padding. */
    .app-modal--flow {
      padding: var(--spacing-md);
    }
  }
}
