/* SAF Dashboard base styles.
 *
 * Phase Z: shell chrome + mode namespaces (empty hooks for tab plugins
 * to scope CSS under .saf-dash--admin / --user / --marketing).
 * Bump SAF_DASH_VERSION on every edit (LiteSpeed cache-busts by ?ver=).
 */

:root {
  --saf-dash-brand: #1a1a2e;
  --saf-dash-bg: #ffffff;
  --saf-dash-bg-rail: #f6f7fa;
  --saf-dash-border: #e3e4ea;
  --saf-dash-text: #1a1a2e;
  --saf-dash-text-muted: #5b6072;
  --saf-dash-link: var(--saf-dash-brand);
  --saf-dash-rail-width: 240px;
  --saf-dash-gap: 1.5rem;
  --saf-dash-radius: 8px;
  --saf-dash-shadow: 0 1px 3px rgba(20, 22, 35, 0.06);
}

/* Shell layout */
.saf-dash {
  display: flex;
  gap: var(--saf-dash-gap);
  min-height: 60vh;
  color: var(--saf-dash-text);
  background: var(--saf-dash-bg);
}

.saf-dash__rail {
  width: var(--saf-dash-rail-width);
  flex-shrink: 0;
  background: var(--saf-dash-bg-rail);
  border: 1px solid var(--saf-dash-border);
  border-radius: var(--saf-dash-radius);
  padding: 1rem 0.75rem;
  box-shadow: var(--saf-dash-shadow);
}

.saf-dash__rail-header {
  margin-bottom: 1rem;
  padding: 0 0.25rem;
}

.saf-dash__role-pill {
  display: inline-block;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
  background: var(--saf-dash-brand);
  color: #fff;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
}

.saf-dash__rail-empty {
  color: var(--saf-dash-text-muted);
  font-size: 0.875rem;
  margin: 0.5rem 0.25rem;
}

.saf-dash__tablist {
  list-style: none;
  margin: 0;
  padding: 0;
}

.saf-dash__tab {
  margin: 0.125rem 0;
}

.saf-dash__tab-link,
.saf-dash__subtab-link {
  display: block;
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  text-decoration: none;
  color: var(--saf-dash-text);
  transition: background-color 120ms ease;
}

.saf-dash__tab-link:hover,
.saf-dash__subtab-link:hover {
  background: rgba(26, 26, 46, 0.06);
}

.saf-dash__tab-link[aria-selected="true"] {
  background: var(--saf-dash-brand);
  color: #fff;
  font-weight: 600;
}

.saf-dash__subtablist {
  list-style: none;
  margin: 0.25rem 0 0.5rem 1rem;
  padding: 0;
  border-left: 2px solid var(--saf-dash-border);
}

.saf-dash__subtab-link {
  font-size: 0.875rem;
  color: var(--saf-dash-text-muted);
}

/* Main column */
.saf-dash__main {
  flex: 1;
  min-width: 0;
}

.saf-dash__crumbs {
  font-size: 0.875rem;
  color: var(--saf-dash-text-muted);
  margin-bottom: 1rem;
}

.saf-dash__crumb-sep {
  margin: 0 0.4rem;
  opacity: 0.6;
}

.saf-dash__body {
  border: 1px solid var(--saf-dash-border);
  border-radius: var(--saf-dash-radius);
  padding: 1.5rem;
  background: var(--saf-dash-bg);
  box-shadow: var(--saf-dash-shadow);
  /* Hard horizontal boundary for every tab. Several per-tab SPAs render a
     display:grid root (.saf-events-spa / .saf-peers-spa / etc.) whose single
     column sizes to the min-content width of a bare data table (~685px) —
     on a phone that pushed the whole PAGE into horizontal scroll. overflow-x
     here was not enough alone because grid tracks still expand; pairing it
     with min-width:0 on the SPA grid roots (below) lets the track shrink and
     the table scroll inside its panel. `clip` (not `auto`) avoids creating a
     scroll container that would trap the absolutely-positioned CRUD drawer. */
  overflow-x: clip;
}

/* Per-tab SPA roots are display:grid/flex; without min-width:0 a wide child
   (data table) blows the track out past the viewport. This generic rule
   covers current (.saf-*-spa) and future SPA panels in one place. */
.saf-dash__body > [class*="-spa"],
.saf-dash__body > [class*="-spa"] > * {
  min-width: 0;
}

.saf-dash__body[aria-hidden="true"] {
  display: none;
}

/* Global overflow containment for ALL dashboard tabs.
 *
 * Text inputs (esp. <input type=search>) default to an intrinsic width of
 * ~20 chars (~450px) and will NOT shrink below it without an explicit cap.
 * Inside a flex/grid toolbar that intrinsic width becomes the track's
 * min-content size, which stretches the whole panel — and on a phone, the
 * whole page — wider than the viewport. This was the root cause of
 * horizontal page-scroll on Events / Peers / Mail / Commerce at 375px.
 *
 * Capping every form control + media element to its container's width
 * (box-sizing already border-box site-wide) lets the panel shrink to fit
 * any viewport. Scoped to .saf-dash__body so it never touches the public
 * site or the drawer's positioned layout. */
.saf-dash__body input,
.saf-dash__body select,
.saf-dash__body textarea,
.saf-dash__body img,
.saf-dash__body table {
  max-width: 100%;
}
.saf-dash__body input[type="search"],
.saf-dash__body input[type="text"],
.saf-dash__body input[type="email"],
.saf-dash__body input[type="url"],
.saf-dash__body input[type="number"] {
  /* Override the UA intrinsic size so these never set the min-content
     width of their flex/grid track. They still grow to fill via flex. */
  min-width: 0;
}

.saf-dash__empty-state h2 {
  margin-top: 0;
}

.saf-dash__tab-error {
  color: #a40000;
  background: #fdecec;
  border: 1px solid #f5b3b3;
  padding: 0.75rem 1rem;
  border-radius: 4px;
}

/* Access denied */
.saf-dash--denied {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4rem 1rem;
  text-align: center;
  min-height: 40vh;
}

.saf-dash__denied h2 {
  margin: 0 0 0.5rem;
}

.saf-dash__signin-link {
  display: inline-block;
  margin-top: 0.75rem;
  padding: 0.5rem 1.25rem;
  background: var(--saf-dash-brand);
  color: #fff;
  border-radius: 4px;
  text-decoration: none;
}

/* Toasts */
.saf-dash__toasts {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 9999;
  pointer-events: none;
}

.saf-dash__toast {
  pointer-events: auto;
  padding: 0.75rem 1rem;
  border-radius: var(--saf-dash-radius);
  background: #1a1a2e;
  color: #fff;
  box-shadow: 0 4px 12px rgba(20, 22, 35, 0.18);
  font-size: 0.875rem;
  max-width: 320px;
}

.saf-dash__toast--success {
  background: #1f7a37;
}
.saf-dash__toast--error {
  background: #a40000;
}
.saf-dash__toast--info {
  background: var(--saf-dash-brand);
}

/* Confirm modal */
.saf-dash__modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 24, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
}

.saf-dash__modal {
  background: #fff;
  border-radius: var(--saf-dash-radius);
  padding: 1.5rem;
  max-width: 420px;
  width: 90%;
  box-shadow: 0 10px 30px rgba(10, 12, 24, 0.3);
}

.saf-dash__modal-msg {
  margin: 0 0 1rem;
  color: var(--saf-dash-text);
}

.saf-dash__modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.saf-dash__btn {
  border: 1px solid var(--saf-dash-border);
  background: #fff;
  color: var(--saf-dash-text);
  padding: 0.4rem 0.9rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
}

.saf-dash__btn--ghost {
  background: #f6f7fa;
}
.saf-dash__btn--primary {
  background: var(--saf-dash-brand);
  color: #fff;
  border-color: var(--saf-dash-brand);
}

/* Mode namespaces (empty in Phase Z; tab plugins populate) */
.saf-dash--admin {
}
.saf-dash--user {
}
.saf-dash--marketing {
}

/* Responsive */
@media (max-width: 768px) {
  .saf-dash {
    flex-direction: column;
  }
  .saf-dash__rail {
    width: 100%;
  }
  /* Shell-level backstop: no tab's content may push the whole page wider
     than the viewport (caused horizontal page-scroll on Events/Peers/Mail
     at 375). The base .saf-dash-table carries min-width:480px for column
     legibility; on a phone that floor exceeds the screen. Tabs that wrap
     their table in .saf-dash-table-wrap already scroll it; for bare tables
     (mail recent-campaigns, events list, peers applications) relax the
     floor so the table reflows to fit instead of stretching the document.
     A wrapped table keeps its scroll behaviour and is unaffected. */
  .saf-dash__body .saf-dash-table:not(.saf-dash-table-wrap .saf-dash-table) {
    min-width: 0;
  }
  /* Header/lede/toolbars are flex/grid tracks that inherit the wide
     table's intrinsic width; let them shrink and wrap on narrow screens. */
  .saf-dash__body .saf-dash-section-header {
    flex-wrap: wrap;
  }
  .saf-dash__body .saf-dash-lede {
    min-width: 0;
  }
}

/* ============================================================
   Phase AA: shared tab-content primitives
   Used by Events / Metrics / Peers / Settings tab render_callbacks.
   ============================================================ */

/*
 * Break out of the theme's .saf-container--narrow (800px max-width) so
 * the dashboard has room to breathe. The theme's page.php wraps content
 * in a narrow container by default; widen to the standard 1200px when
 * the dashboard shell is present on the page.
 */
.saf-container--narrow:has(.saf-dash) {
  max-width: var(--saf-container, 1200px);
}
/* The theme caps .saf-content (prose) to ~620px; widen for the dashboard. */
.saf-content:has(.saf-dash) {
  max-width: none;
}
article:has(.saf-dash) {
  width: 100%;
}

.saf-dash-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-block: 0 1rem;
  flex-wrap: wrap;
}
.saf-dash-section-header h2 {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.2;
}
.saf-dash-subhead {
  margin-block: 1.5rem 0.5rem;
  font-size: 1.05rem;
  color: #444;
  font-weight: 600;
}
.saf-dash-lede {
  margin-block: 0 1rem;
  color: #555;
}

.saf-dash-button {
  display: inline-block;
  padding: 0.45rem 0.9rem;
  background: var(--saf-dash-brand);
  color: #fff;
  border-radius: 4px;
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  border: 1px solid var(--saf-dash-brand);
  cursor: pointer;
  white-space: nowrap;
}
.saf-dash-button:hover,
.saf-dash-button:focus {
  filter: brightness(0.92);
  text-decoration: none;
  color: #fff;
}

.saf-dash-empty {
  margin: 0.5rem 0 1rem;
  color: #777;
  font-style: italic;
}

.saf-dash__tab-denied {
  padding: 1rem;
  border: 1px solid #f0c0c0;
  background: #fdecec;
  border-radius: 4px;
  color: #7a1414;
}

/*
 * Tables: scroll horizontally on narrow containers instead of letting cells
 * wrap to 6+ lines. Wrap the table in a div with class .saf-dash-table-wrap
 * if you want explicit overflow control; otherwise the table itself gets
 * the scroll behavior on the surrounding flex/grid context.
 */
.saf-dash-table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.saf-dash-table {
  width: 100%;
  min-width: 480px;
  border-collapse: collapse;
  margin-block: 0.5rem 1rem;
  font-size: 0.95rem;
}
.saf-dash-table th,
.saf-dash-table td {
  text-align: left;
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid #ececef;
  vertical-align: middle;
}
.saf-dash-table thead th {
  font-weight: 600;
  font-size: 0.78rem;
  color: #555;
  letter-spacing: 0.03em;
  border-bottom: 2px solid #d8d8dc;
  background: #f9f9fb;
}
.saf-dash-table tbody tr:hover {
  background: #f6f7fa;
}
.saf-dash-table td:last-child {
  text-align: right;
  white-space: nowrap;
}
/* First column (title/name) gets more room and can wrap; date columns stay one line. */
.saf-dash-table td:first-child {
  font-weight: 500;
}
.saf-dash-table td:nth-child(2),
.saf-dash-table td:nth-child(3) {
  white-space: nowrap;
}

/*
 * Cards: container-driven auto-fit grid. Cards stay >= 150px wide so labels
 * like "Pending applications" don't truncate. Drops columns automatically
 * as the container narrows, regardless of viewport width.
 */
.saf-dash-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.75rem;
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
}
.saf-dash-card {
  padding: 0.9rem 1rem;
  background: #fff;
  border: 1px solid #e3e3e8;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
}
.saf-dash-card__value {
  font-size: 1.6rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: #1a1a1f;
  line-height: 1.1;
}
.saf-dash-card__label {
  font-size: 0.78rem;
  color: #666;
  letter-spacing: 0.02em;
  /* No uppercase: long labels like "With consent to follow up" stay legible. */
  text-transform: none;
  font-weight: 500;
}

.saf-dash-links {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
