/* Platform console (/platform/) and the "View as" banner. Builds on app.css tokens.
   Phone first: tables render as stacked cards; at 900px and up they become real tables. */

.p-sr {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---- View as banner (every page) ---- */
.impersonation-banner { background: #b42318; color: #fff; }
.impersonation-inner {
  max-width: 640px; margin: 0 auto; padding: 8px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.impersonation-text { margin: 0; font-size: 0.95rem; line-height: 1.3; min-width: 0; overflow-wrap: anywhere; }
.impersonation-dash { opacity: 0.8; }
@media (max-width: 519px) { .impersonation-dash { display: none; } }
.impersonation-form { margin: 0; flex: 0 0 auto; }
.impersonation-end { background: #fff; border-color: #fff; color: #b42318; min-height: 40px; }
/* The top bar is sticky, so its red edge keeps reminding the admin after the banner scrolls away. */
body:has(.impersonation-banner) .topbar { border-bottom: 3px solid #b42318; }

/* ---- Wider pages for the console ---- */
.pnav { display: flex; gap: 4px; }
.pnav-link {
  display: inline-flex; align-items: center; min-height: 40px; padding: 6px 10px; border-radius: var(--radius-sm);
  color: var(--muted); font-weight: 600; font-size: 0.9rem; text-decoration: none; white-space: nowrap;
}
.pnav-link.active { color: var(--brand); background: #eef1f5; }
/* v3: Leads queue count and short labels so four links fit on a phone. */
.pnav-count {
  display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; margin-left: 4px;
  padding: 0 6px; border-radius: 999px; background: #b42318; color: #fff; font-size: 0.75rem; font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.pnav-short { display: none; }
@media (max-width: 519px) {
  .pnav { gap: 0; }
  .pnav-link { padding: 6px 7px; font-size: 0.85rem; }
  .pnav-long { display: none; }
  .pnav-short { display: inline; }
}

/* ---- Tenant list ---- */
.psearch { display: flex; gap: 8px; margin-bottom: 12px; }
.psearch input { flex: 1; min-width: 0; }
.pfilters { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 14px; scrollbar-width: none; }
.pfilter {
  display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 6px 14px; flex: 0 0 auto;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
  color: var(--text); font-weight: 600; font-size: 0.9rem; text-decoration: none;
}
.pfilter.active { background: var(--brand); border-color: var(--brand); color: var(--brand-contrast); }
.pfilter-count { font-size: 0.8rem; opacity: 0.75; font-variant-numeric: tabular-nums; }

.chip-org-pending { background: #fdf0dc; color: #9a5d00; text-decoration: none; }
.chip-org-active { background: #dcf1e5; color: var(--ok); }
.chip-org-suspended { background: #fbe4e1; color: var(--danger); }
.chip-prole-owner { background: var(--brand); color: var(--brand-contrast); }
.chip-prole-admin { background: #e3edf8; color: var(--info); }
.chip-prole-inspector { background: #eef1f5; color: var(--muted); }

/* Tables as cards on phones */
.ptable { width: 100%; border-collapse: collapse; }
.ptable thead { display: none; }
.ptable tbody { display: block; }
.ptable tr {
  display: block; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px; margin-bottom: 10px;
}
.ptable td { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 3px 0; font-size: 0.92rem; }
.ptable td::before { content: attr(data-label); color: var(--muted); font-weight: 600; font-size: 0.85rem; flex: 0 0 auto; }
.ptable td > * { min-width: 0; text-align: right; overflow-wrap: break-word; }
.ptable td.cell-main { display: block; padding: 0 0 6px; }
.ptable td.cell-main > * { text-align: left; }
.ptable td.cell-main::before, .ptable td.cell-actions::before { content: none; }
.ptable td.cell-actions { justify-content: flex-end; gap: 8px; padding-top: 10px; }
.ptable td.cell-actions form { margin: 0; }
.ptable td.cell-actions .btn { min-height: 44px; }
.ptable .row-sub { white-space: normal; }
.tenant-name { font-weight: 700; font-size: 1.02rem; color: var(--text); text-decoration: none; }
a.tenant-name:hover { text-decoration: underline; }
.tenant-row.is-pending { border-color: #f0c77f; background: #fffaf1; }
.member-table .chip { margin-top: 4px; }

/* Audit rows */
.audit-action {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85rem; font-weight: 600;
  background: #eef1f5; border-radius: 6px; padding: 1px 6px; white-space: nowrap;
}
.audit-when { display: flex !important; justify-content: space-between; align-items: center; gap: 8px; }
.audit-when time { color: var(--muted); font-size: 0.85rem; text-align: right; }
.audit-detail { overflow-wrap: anywhere; }
.storage-part { white-space: nowrap; }
.ptable td.audit-action-cell { display: none; }
.ptable td.audit-detail { display: block; }
.ptable td.audit-detail::before { display: block; margin-bottom: 2px; }
.ptable td.audit-detail { text-align: left; }
.section-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.audit-filters { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; margin-bottom: 8px; }
.audit-filter-buttons { grid-column: span 2; display: flex; gap: 8px; margin-bottom: 14px; }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 8px 0 24px; }

/* ---- Tenant detail ---- */
.ptitle { align-items: flex-start; }
.ptitle h1 { overflow-wrap: anywhere; }
.pdetail { display: grid; gap: 0; }
.pdetail-usage, .pdetail-rest, .pdetail-side { min-width: 0; }
.stat-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat-num { font-size: 1.5rem; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 0.82rem; margin-top: 2px; }
.pmeta { margin: 10px 0 0; }
.pdetail-side .card p:last-child { margin-bottom: 12px; }
.pdetail-side .btn-row form { flex: 1 1 140px; margin: 0; }

/* Feature switches */
.pswitches { margin-bottom: 0; }
.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 52px; padding: 8px 16px; border-bottom: 1px solid var(--border); font-weight: 600; cursor: pointer;
}
.switch {
  appearance: none; -webkit-appearance: none; flex: 0 0 48px; width: 48px; height: 28px; margin: 0;
  border-radius: 999px; background: #cfd6de; position: relative; cursor: pointer; transition: background 0.15s;
}
.switch::before {
  content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.15s;
}
.switch:checked { background: var(--ok); }
.switch:checked::before { transform: translateX(20px); }
.switch:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.switch-save { padding: 12px 16px; }
.switch-save p { margin: 8px 0 0; }

@media (prefers-reduced-motion: reduce) {
  .switch, .switch::before { transition: none; }
}

/* ---- Desktop ---- */
@media (min-width: 900px) {
  body.platform-page .page, body.platform-page .topbar-inner, body.platform-page .impersonation-inner { max-width: 1200px; }
  body.platform-page .page { padding: 24px; }

  .psearch { max-width: 560px; }

  .ptable-wrap {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); overflow: hidden; margin-bottom: 16px;
  }
  .ptable { display: table; }
  .ptable thead { display: table-header-group; }
  .ptable tbody { display: table-row-group; }
  .ptable tr { display: table-row; border: 0; border-radius: 0; box-shadow: none; padding: 0; margin: 0; }
  .ptable th, .ptable td {
    display: table-cell; padding: 10px 12px; border-bottom: 1px solid var(--border);
    text-align: left; vertical-align: middle; font-size: 0.92rem;
  }
  .ptable tbody tr:last-child td { border-bottom: 0; }
  .ptable th {
    background: #f9fafb; color: var(--muted); font-size: 0.75rem; font-weight: 700;
    letter-spacing: 0.05em; text-transform: uppercase; line-height: 1.3;
  }
  .ptable td::before { content: none; }
  .ptable td > * { text-align: inherit; }
  .ptable td.cell-main { display: table-cell; padding: 10px 12px; min-width: 220px; }
  .tenant-table td.cell-main { width: 26%; }
  .ptable td.nowrap { white-space: nowrap; }
  .ptable td.cell-actions { display: table-cell; padding-top: 10px; text-align: right; white-space: nowrap; }
  .ptable td.cell-actions form { display: inline-block; }
  .ptable .num { text-align: right; font-variant-numeric: tabular-nums; }
  .ptable tbody tr:hover td { background: #fafbfc; }
  .tenant-row.is-pending td { background: #fffaf1; }

  .audit-when { display: table-cell !important; white-space: nowrap; }
  .audit-when .audit-action { display: none; }
  .audit-when time { color: var(--text); }
  .ptable td.audit-action-cell { display: table-cell; white-space: nowrap; }
  .ptable td.audit-detail { display: table-cell; min-width: 200px; }

  .audit-filters { grid-template-columns: 260px 260px auto; align-items: end; gap: 0 12px; }
  .audit-filter-buttons { grid-column: auto; margin-bottom: 14px; }

  .pdetail {
    grid-template-columns: minmax(0, 1.65fr) minmax(300px, 1fr); grid-template-rows: auto 1fr;
    grid-template-areas: "usage side" "rest side"; column-gap: 24px; align-items: start;
  }
  .pdetail-usage { grid-area: usage; }
  .pdetail-rest { grid-area: rest; }
  .pdetail-side { grid-area: side; position: sticky; top: 80px; }
  .stat-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

/* Phones: the platform nav needs the whole top bar, so drop the tenant brand on platform pages. */
@media (max-width: 640px) {
  .topbar-inner:has(.pnav) .brand { display: none; }
  .topbar-inner:has(.pnav) .topbar-actions { flex: 1; justify-content: space-between; }
  .topbar-inner:has(.pnav) .pnav { width: 100%; justify-content: space-between; }
}
