/* ==========================================================================
   LAYOUT SYSTEM — shared by build, airfact, trade
   Edit here; all three apps symlink this file.
   ========================================================================== */

/* Container System */
.container {
  width: 100%;
  max-width: var(--container-max-width);
  margin: 0 auto;
  padding: 0 var(--space-4);
}

.container-fluid {
  width: 100%;
  padding: 0 var(--space-4);
}

/* Main Layout Structure */
.main-content {
  margin-left: var(--sidebar-width);
  padding: var(--content-pad);
  /* Tighter gap between chrome and content — Linear/Vercel-style. The
     topbar already provides a horizontal divider line, so the content can
     sit close to it without feeling crowded. */
  padding-top: calc(var(--topbar-height, 80px) + var(--space-5));
  width: calc(100% - var(--sidebar-width));
  min-height: 100vh;
  box-sizing: border-box;
}

/* ──────────────────────────────────────────────────────────────────────────
   App Topbar — single chrome strip, absorbs the legacy mobile-header (left
   hamburger, mobile only) and per-page page-header (title left, actions
   right). Always renders for authenticated dash pages. Pinned to top, sits
   to the right of the sidebar on desktop and full-width on mobile. */
.app-topbar {
  position: fixed;
  top: 0;
  left: var(--sidebar-width);
  right: 0;
  height: var(--topbar-height, 80px);
  display: flex;
  align-items: center;
  gap: var(--space-6);
  /* Vertical padding matches the sidebar's outer padding (chrome rhythm).
     Horizontal padding matches .main-content so the title's left edge and
     the actions' right edge sit on the same vertical grid as the page body
     (table headers, cards, breadcrumbs all line up). */
  padding: var(--chrome-pad-y) var(--content-pad);
  background: var(--bg);
  z-index: 90;
  box-sizing: border-box;
}

/* Bottom separator inset to match the content frame — a full-width
   border-bottom would extend past the title on each side and misalign
   with the table/card edges below. Pseudo-element keeps the line in
   sync with content padding via the shared --content-pad token. */
.app-topbar::after {
  content: "";
  position: absolute;
  left: var(--content-pad);
  right: var(--content-pad);
  bottom: 0;
  height: 1px;
  background: var(--border);
}

.app-topbar-hamburger {
  display: none;
  background: transparent;
  border: none;
  padding: var(--space-2);
  cursor: pointer;
  color: var(--text);
  border-radius: var(--radius);
}
.app-topbar-hamburger:hover { background: var(--color-surface-2, #f5f5f5); }

.app-topbar-title {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: var(--space-2);
}

/* Title + subtitle stack vertically inside this wrapper; the back arrow
   (when present) sits as a row-sibling of this wrapper so it renders
   LEFT of the title block, not stacked above it. */
.app-topbar-title-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  flex: 1 1 auto;
}

.app-topbar-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  margin-right: var(--space-2);
  border-radius: var(--radius);
  color: var(--text-secondary);
  text-decoration: none;
  flex-shrink: 0;
}
.app-topbar-back:hover { background: var(--color-surface-2, #f5f5f5); color: var(--text); }

.app-topbar-h1 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  line-height: 1.2;
  min-width: 0;
  letter-spacing: -0.01em;
}

/* Breadcrumb chain — parents rendered as links, current page is the
   .app-topbar-text leaf. Separator (›) sits between every crumb. */
.app-topbar-crumbs {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}
.app-topbar-crumbs:empty { display: none; }
.app-topbar-crumb {
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color var(--transition);
}
a.app-topbar-crumb:hover { color: var(--text); }
.app-topbar-crumb-sep {
  color: var(--text-muted);
  font-weight: 400;
  flex-shrink: 0;
}

/* Subtitle — stacks below the title as a sibling of the h1 (the
   .app-topbar-title parent is flex-direction:column). Single line, ellipsis
   on overflow, muted weight so the title stays the primary anchor. */
.app-topbar-subtitle {
  margin: 2px 0 0;
  font-size: 0.8125rem;
  font-weight: 400;
  color: var(--text-muted);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* Hide the element when no text has been set (e.g. subtitle_id provided
   but JS hasn't populated it yet) — prevents an empty line nudging the
   title off-center. */
.app-topbar-subtitle:empty { display: none; }
.app-topbar-icon { display: inline-flex; align-items: center; color: var(--text-secondary); flex-shrink: 0; }
.app-topbar-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-topbar-subtitle {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--text-muted, var(--text-secondary));
  line-height: 1.3;
  font-weight: 400;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.75;
}

.app-topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  flex-shrink: 0;
}

.app-topbar-page-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.app-topbar-divider {
  width: 1px;
  height: 28px;
  background: var(--border);
  margin: 0 var(--space-1);
}

/* Inbox bell — same chrome-strip footprint as the avatar so the two read as
   a paired set. Popover is wired by topbar-inbox.js, paints from
   /api/v1/notifications and stays in sync via the `xz-inbox-changed` event
   that the approval drawer + Xori FAB bell already broadcast. */
.app-topbar-inbox { position: relative; }

.app-topbar-iconbtn {
  position: relative;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--color-surface-2, #f5f5f5);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  transition: background var(--transition), border-color var(--transition);
}
.app-topbar-iconbtn:hover { background: var(--border); }
.app-topbar-iconbtn[aria-expanded="true"] { border-color: var(--primary); }
.app-topbar-iconbtn svg { display: block; }

.app-topbar-inbox-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--danger, #dc2626);
  color: #fff;
  font-size: 0.6875rem;
  font-weight: 600;
  line-height: 16px;
  text-align: center;
  pointer-events: none;
}
.app-topbar-inbox-badge[hidden] { display: none; }

.app-topbar-inbox-popover {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 360px;
  max-width: 92vw;
  max-height: 480px;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 200;
  overflow: hidden;
}
.app-topbar-inbox-popover[hidden] { display: none; }

/* On narrow phones the right-anchored absolute popover would extend off the
   left edge of the viewport. Span fixed across the viewport instead so the
   inbox stays fully visible whatever the device width. */
@media (max-width: 480px) {
  .app-topbar-inbox-popover {
    position: fixed;
    top: var(--topbar-height, 80px);
    left: var(--space-2);
    right: var(--space-2);
    width: auto;
    max-width: none;
  }
}

.app-topbar-inbox-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--text);
}
.app-topbar-inbox-link {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--primary);
  text-decoration: none;
}
.app-topbar-inbox-link:hover { text-decoration: underline; }

.app-topbar-inbox-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--space-1);
}
.app-topbar-inbox-loading,
.app-topbar-inbox-empty,
.app-topbar-inbox-error {
  padding: var(--space-4) var(--space-3);
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.875rem;
}

.app-topbar-inbox-row {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
}
.app-topbar-inbox-row:hover { background: var(--color-surface-2, #f5f5f5); }
.app-topbar-inbox-row.unread { background: var(--primary-light); }
.app-topbar-inbox-row.unread:hover { background: var(--primary-light); filter: brightness(0.97); }
.app-topbar-inbox-row.requires-action { border-left: 3px solid var(--warning, #f59e0b); }

.app-topbar-inbox-avatar {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-surface-2, #f5f5f5);
  border: 1px solid var(--border);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.app-topbar-inbox-text {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.app-topbar-inbox-title {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.app-topbar-inbox-body-line {
  font-size: 0.8125rem;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.app-topbar-inbox-time {
  font-size: 0.75rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.app-topbar-inbox-cta {
  color: var(--warning, #f59e0b);
  font-weight: 500;
}

/* User avatar + popover */
.app-topbar-user { position: relative; }

.app-topbar-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--color-surface-2, #f5f5f5);
  color: var(--text);
  font-weight: 600;
  font-size: 0.8125rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  transition: background var(--transition), border-color var(--transition);
}
.app-topbar-avatar:hover { background: var(--border); }
.app-topbar-avatar[aria-expanded="true"] { border-color: var(--primary); }

.app-topbar-user-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 280px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: var(--space-1);
  z-index: 200;
}

/* Apps grid inside the user menu — replaces the old sidebar app launcher.
   Compact tile row that wraps to a second row past 4 apps. */
.app-topbar-user-apps {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-2);
  padding: var(--space-3);
}
.app-topbar-user-app {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
  padding: var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-decoration: none;
  transition: background var(--transition), border-color var(--transition), transform var(--transition);
}
.app-topbar-user-app:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
}
.app-topbar-user-app.is-current {
  border-color: var(--primary);
  background: var(--primary-light);
  cursor: default;
  pointer-events: none;
}
.app-topbar-user-app img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
}
.app-topbar-user-app-fallback {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.app-topbar-user-divider {
  height: 1px;
  background: var(--border);
  margin: var(--space-1) calc(-1 * var(--space-1));
}

.app-topbar-user-links { padding: var(--space-1) 0; }
.app-topbar-user-favicon { border-radius: var(--radius-sm); }
.app-topbar-user-link-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1 1 auto;
  min-width: 0;
}

/* Cross-app transition overlay — same Corelia pulse animation used on the
   login-pending and logout views, so going from one app to another feels
   like the same family of moments. Hidden by default; topbar JS toggles it
   on app-tile clicks. The next app's first paint replaces it naturally. */
.app-transition-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 9999;
}
.app-transition-overlay[hidden] { display: none; }
.app-transition-mark {
  display: block;
  width: 160px;
  height: 160px;
  /* object-fit:contain guards against any non-square viewBox surprises in
     the SVG — the logo is preserved in its native aspect ratio inside the
     square box rather than being stretched to fill. */
  object-fit: contain;
  animation: app-transition-pulse 1.1s ease-in-out infinite;
  transform-origin: center;
  will-change: transform, filter;
}
@keyframes app-transition-pulse {
  0%, 100% { transform: scale(0.94); filter: drop-shadow(0 4px 12px rgba(25,49,83,0.08)); }
  50%      { transform: scale(1.0);  filter: drop-shadow(0 10px 28px rgba(25,49,83,0.22)); }
}
@media (prefers-reduced-motion: reduce) {
  .app-transition-mark { animation: none; }
}

.app-topbar-user-info {
  padding: var(--space-3);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--space-1);
}
.app-topbar-user-name { font-weight: 600; font-size: 0.875rem; color: var(--text); }
.app-topbar-user-email { font-size: 0.75rem; color: var(--text-secondary); margin-top: 2px; word-break: break-all; }

.app-topbar-user-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  background: transparent;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  text-align: left;
}
.app-topbar-user-item:hover { background: var(--color-surface-2, #f5f5f5); }
.app-topbar-user-item-danger:hover { background: rgba(239,68,68,0.08); color: var(--danger, #ef4444); }
.app-topbar-user-icon { display: inline-flex; align-items: center; color: var(--text-secondary); }
.app-topbar-user-item-danger:hover .app-topbar-user-icon { color: var(--danger, #ef4444); }

/* Content Layout Patterns */
.content-layout {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-6);
  width: 100%;
}

.content-layout.cards {
  align-items: flex-start;
  justify-content: flex-start;
}

.content-layout.rows {
  flex-direction: column;
}

.content-layout.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-8);
}

/* Card Layout Constraints */
.content-layout.cards > .card {
  flex: 0 0 auto;
  width: 100%;
  min-width: 320px;
  max-width: 480px;
}

.cards-constrained-sm > .card { max-width: 400px; }
.cards-constrained > .card    { max-width: 480px; }
.cards-constrained-lg > .card { max-width: 640px; }

/* Page Structure */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: var(--space-6);
}

.page-header-content {
  flex: 1;
}

.page-header-content h1 {
  font-size: 1.875rem;
  font-weight: 700;
  margin: 0 0 var(--space-2) 0;
  color: var(--text);
  line-height: 1.2;
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.page-header-subtitle,
.header-subtitle {
  color: var(--text-secondary);
  font-size: 1rem;
  margin: 0;
  line-height: 1.4;
}

.page-header-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-3);
  flex-shrink: 0;
  margin-left: var(--space-8);
}

/* Section Organization */
.section-group {
  margin-bottom: var(--space-10);
}

.section-header {
  margin-bottom: var(--space-6);
  padding-bottom: var(--space-3);
  border-bottom: 2px solid var(--border);
}

.section-title {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
  margin: 0;
}

/* Panel Title — inline heading for sub-panels, drawers, modal sections.
   Used by section_header() / UIComponents::sectionHeader().
   Smaller than .section-title (which is for top-level page sections) and
   independent of .card-title (which is card-bound). */
.panel-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 600;
  color: var(--text);
  margin: 0 0 var(--space-3);
  font-size: 0.9rem;
}

.panel-title > svg {
  flex-shrink: 0;
}

.panel-title-sm { font-size: 0.8125rem; }
.panel-title-lg { font-size: 1rem; }
.panel-title-xl { font-size: 1.125rem; }

/* Right-aligned action slot (badge, button) inside a .panel-title */
.panel-title-actions {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

/* NOTE: .stat-card (no prefix) is NOT shared — it means different things:
     • trade : .stat-card = marketplace tile (app-custom.css)
     • build   : .stat-card = small inline project metric (project-styles.css)
   The shared dashboard pattern uses .dash-stat-card / .dash-stats-row /
   .dash-grid — see dashboard.css (rendered by stat_card() UIComponent). */

/* Auth Content Layout (login / register / forgot-password pages) */
.auth-content-layout {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100%;
}

.auth-card-logo {
  display: flex;
  justify-content: center;
  margin-bottom: 1.5rem;
}

.auth-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 2rem;
  width: 100%;
  max-width: 480px;
}

/* Embed layout (iframe mode — no sidebar, no chrome) */
body.embed-layout {
  background: var(--bg, #f9fafb);
  overflow-x: hidden;
}

body.embed-layout #app {
  display: block;
}

.embed-content {
  padding: 1rem 1.5rem;
  max-width: 100%;
}

/* Mobile Responsive */
@media (max-width: 768px) {
  .app-topbar {
    left: 0;
    /* On mobile, content padding shrinks to chrome-pad-x-self (16px) for
       both the topbar and main-content — they share the same horizontal
       grid as the sidebar's column padding. */
    padding: var(--chrome-pad-y) var(--chrome-pad-x-self);
  }
  .app-topbar::after {
    left: var(--chrome-pad-x-self);
    right: var(--chrome-pad-x-self);
  }
  .app-topbar-hamburger { display: inline-flex; }
  /* On mobile we trade visible page actions for screen real estate — they
     stay accessible inside the page body if the page also renders them. */
  .app-topbar-page-actions,
  .app-topbar-divider { display: none; }

  .main-content {
    margin-left: 0;
    padding-top: calc(var(--topbar-height, 80px) + var(--space-4));
    width: 100%;
    padding-left: var(--chrome-pad-x-self);
    padding-right: var(--chrome-pad-x-self);
  }

  .content-layout.cards {
    flex-direction: column;
  }

  .content-layout.cards > .card {
    max-width: none;
    width: 100%;
  }

  .content-layout.split {
    grid-template-columns: 1fr;
  }

  .page-header {
    flex-direction: column;
    gap: var(--space-4);
    text-align: left;
    padding: var(--space-6);
  }

  .page-header-actions {
    width: 100%;
    margin-left: 0;
  }
}
