/* Shared chrome + component styles for the sidebar-nav app (Dashboard/
   Tickets/Projects/...). Loaded once via <link>, same mechanism
   portal-shell.js uses -- see that file's top comment for why this exists
   instead of each page redefining its own copy. Pages that use this shell
   should keep their own inline <style> to page-specific rules only. */

:root {
  --bg: #080c14;
  --bg-elevated: #10151f;
  --bg-elevated-2: #161c29;
  --border: #1f2733;
  --text: #eef1f5;
  --text-muted: #93a0b3;
  --accent: #4fd1c5;
  --accent-soft: rgba(79, 209, 197, 0.12);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.12);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.12);
  /* Set by env-banner.js to the banner's actual rendered height (0px when
     hidden, i.e. on prod). .sidebar below subtracts it from 100vh -- see
     that rule for why this exists. */
  --env-banner-height: 0px;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100vh; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.6;
}
a { color: var(--accent); text-decoration: none; }

/* Sticky, not static -- a "this is test data" warning is more useful
   staying visible while you scroll than scrolling away after one screen.
   z-index above the mobile sidebar drawer (20) so the drawer never covers
   it when open. */
#envBanner { position: sticky; top: 0; z-index: 21; }

#portal-shell { display: flex; min-height: 100vh; }

/* ---- Sidebar ---- */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  /* #portal-shell is a flex row with default align-items:stretch, so
     without this the sidebar stretches to match .shell-main's height --
     which, on a page with enough dashboard content to scroll, means the
     sidebar (and the account switcher/sign-out pinned to its bottom)
     grows taller than the viewport and scrolls out of view along with
     the page. position:sticky + a fixed viewport height keeps it pinned
     in place while only .shell-main scrolls.
     top/height both subtract --env-banner-height (0px on prod, where
     there's no banner) -- without that, a height:100vh sidebar sitting
     below the banner in normal flow is banner-height taller than the
     space actually below it, pushing the footer (sign-out) that many
     pixels below the fold on beta/staging. */
  position: sticky;
  top: var(--env-banner-height);
  align-self: flex-start;
  height: calc(100vh - var(--env-banner-height));
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0.9rem;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0 0.4rem 1.25rem;
  margin-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-weight: 800;
  font-size: 0.95rem;
}
.sidebar-brand-mark {
  width: 32px;
  height: 32px;
  object-fit: contain;
  flex-shrink: 0;
}
.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  /* The scrollable part of the sidebar -- flex:1 claims exactly the space
     left after the brand row and footer, min-height:0 is what actually
     lets a flex child shrink below its content size and scroll instead of
     pushing the footer (account switcher, sign-out) below the fold. Only
     this list scrolls now; the footer stays pinned regardless of how many
     nav links there are. */
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}
.sidebar-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.6rem 0.7rem;
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 600;
}
.sidebar-link:hover { background: var(--bg-elevated-2); color: var(--text); }
.sidebar-link.active { background: var(--accent-soft); color: var(--accent); }
.sidebar-link .nav-icon { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar-footer {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.sidebar-tenant { display: flex; align-items: center; gap: 0.6rem; padding: 0 0.4rem; }
.sidebar-tenant-logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  object-fit: contain;
  background: #fff;
  padding: 3px;
  flex-shrink: 0;
}
.sidebar-tenant-logo-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 800;
  font-size: 0.68rem;
  padding: 0;
}
.sidebar-tenant-name { font-weight: 700; font-size: 0.9rem; color: var(--text); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-tenant-switcher {
  min-width: 0;
  flex: 1;
  background: var(--bg-elevated-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  font-size: 0.85rem;
}
.sidebar-user {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0 0.4rem;
  font-size: 0.8rem;
}
.sidebar-user-email { color: var(--text-muted); word-break: break-all; }
.sidebar-user a { color: var(--text-muted); font-weight: 600; }
.sidebar-user a:hover { color: var(--text); }

.sidebar-backdrop { display: none; }

/* ---- Top bar ---- */
.shell-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(8, 12, 20, 0.85);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar-menu-toggle {
  display: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  width: 34px;
  height: 34px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.topbar-menu-toggle svg { width: 18px; height: 18px; }
.topbar-tenant {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding-right: 1rem;
  margin-right: 0.25rem;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-tenant-logo {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  object-fit: contain;
  background: #fff;
  padding: 3px;
  flex-shrink: 0;
}
.topbar-tenant-logo-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 800;
  font-size: 0.85rem;
  padding: 0;
}
.topbar-tenant-name { font-weight: 700; font-size: 0.95rem; color: #fff; white-space: nowrap; }
@media (max-width: 640px) { .topbar-tenant-name { display: none; } }

.topbar-titles { flex: 1; min-width: 0; }
.topbar-titles h1 { font-size: 1.3rem; color: #fff; margin: 0; }
.topbar-titles p { font-size: 0.85rem; color: var(--text-muted); margin: 0.15rem 0 0; }
.topbar-actions { display: flex; align-items: center; gap: 0.75rem; flex-shrink: 0; }
.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 0.8rem;
  flex-shrink: 0;
  overflow: hidden;
}
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.shell-content { padding: 1.75rem; flex: 1; }
.shell-loading { padding: 4rem 1.5rem; text-align: center; color: var(--text-muted); width: 100%; }

.module-gate { padding: 4rem 1.5rem; text-align: center; color: var(--text-muted); }
.module-gate-sub { font-size: 0.85rem; margin-top: 0.3rem; }

@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 0 0 32px rgba(0,0,0,0.5);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 19;
  }
  .sidebar-backdrop.open { display: block; }
  .topbar-menu-toggle { display: flex; }
  .shell-content { padding: 1.25rem; }
}

/* ---- Shared components (dashboard/tickets/projects) ---- */
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem;
}
.card-title { font-size: 1rem; font-weight: 700; color: #fff; margin: 0 0 1rem; }

/* Admin-authored per-tenant rich text (set on the tenant's edit form in
   /portal/admin.html) -- lives at the top of the Dashboard as a
   personalized notice, since it's short, often empty, and page-worthy of
   its own sidebar entry for most clients. */
.tenant-message {
  padding: 1.1rem 1.25rem;
  margin-bottom: 1.25rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 0.92rem;
  line-height: 1.6;
}
.tenant-message h1 { font-size: 1.2rem; color: #fff; margin: 0 0 0.6rem; }
.tenant-message h2 { font-size: 1.05rem; color: #fff; margin: 0 0 0.6rem; }
.tenant-message h3 { font-size: 1rem; color: #fff; margin: 0 0 0.6rem; }
.tenant-message p { margin: 0 0 0.7rem; }
.tenant-message p:last-child { margin-bottom: 0; }
.tenant-message ul, .tenant-message ol { margin: 0 0 0.7rem; padding-left: 1.4rem; }
.tenant-message a { color: var(--accent); text-decoration: underline; }
.tenant-message img { max-width: 100%; height: auto; border-radius: 8px; }

/* Same typography as .tenant-message, minus the outer card chrome --
   for admin-authored rich-text Dashboard widgets, which already sit
   inside their own .card. */
.rich-body { color: var(--text-muted); font-size: 0.88rem; line-height: 1.6; }
.rich-body h1 { font-size: 1.1rem; color: #fff; margin: 0 0 0.5rem; }
.rich-body h2 { font-size: 1rem; color: #fff; margin: 0 0 0.5rem; }
.rich-body h3 { font-size: 0.95rem; color: #fff; margin: 0 0 0.5rem; }
.rich-body p { margin: 0 0 0.6rem; }
.rich-body p:last-child { margin-bottom: 0; }
.rich-body ul, .rich-body ol { margin: 0 0 0.6rem; padding-left: 1.3rem; }
.rich-body a { color: var(--accent); text-decoration: underline; }
.rich-body img { max-width: 100%; height: auto; border-radius: 8px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 12px; padding: 1.1rem 1.25rem; }
.stat-label { font-size: 0.72rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-muted); font-weight: 700; margin-bottom: 0.5rem; }
.stat-value { font-size: 1.7rem; font-weight: 800; color: #fff; }
.stat-sub { font-size: 0.78rem; color: var(--text-muted); margin-top: 0.35rem; }

.pill { display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.2rem 0.6rem; border-radius: 999px; font-size: 0.75rem; font-weight: 700; white-space: nowrap; }
.pill-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
/* Status: open = actionable, in_progress = being worked, resolved = done */
.pill-open, .pill-operational { background: var(--accent-soft); color: var(--accent); }
.pill-in_progress, .pill-degraded { background: var(--warning-soft); color: var(--warning); }
.pill-resolved { background: rgba(147,160,179,0.14); color: var(--text-muted); }
/* Priority: 4 distinct severity tiers, low to urgent */
.pill-low { background: rgba(147,160,179,0.14); color: var(--text-muted); }
.pill-normal { background: var(--accent-soft); color: var(--accent); }
.pill-high { background: var(--warning-soft); color: var(--warning); }
.pill-urgent { background: var(--danger-soft); color: var(--danger); }

.progress-track { background: var(--bg-elevated-2); border-radius: 999px; height: 6px; overflow: hidden; }
.progress-fill { background: var(--accent); height: 100%; border-radius: 999px; }

.empty-state { text-align: center; padding: 2rem 0; color: var(--text-muted); font-size: 0.9rem; }
.error-msg { color: var(--danger); font-size: 0.85rem; margin-top: 0.6rem; }

.btn {
  display: inline-block;
  padding: 0.6rem 1.3rem;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.9rem;
  border: 1px solid transparent;
  cursor: pointer;
  background: var(--accent);
  color: #06231f;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { background: transparent; color: var(--text); border-color: var(--border); }
.icon-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 0.4rem 0.7rem;
  font-size: 0.8rem;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent); }

table.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
table.data-table th { text-align: left; font-size: 0.72rem; letter-spacing: 0.05em; text-transform: uppercase; color: var(--text-muted); padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--border); }
table.data-table td { padding: 0.75rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data-table tbody tr:hover { background: var(--bg-elevated-2); cursor: pointer; }
table.data-table tbody tr.active { background: var(--accent-soft); }
.table-scroll { overflow-x: auto; }

/* Admin tables commonly run 5-7 columns (account/subject/status/priority/
   assigned/updated/actions) -- there's no sensible way to fit that many on
   a phone screen, and horizontal scrolling inside a scroll container that
   itself sits inside a scrolling page is easy to miss entirely (nothing
   visually hints the table continues sideways). Below this width every
   data-table instead collapses each row into its own card, stacking cells
   vertically with a label taken from the cell's own data-label attribute --
   the standard responsive-table pattern. Callers just need to add
   data-label="Column Name" to each <td> (skip it on a cell that doesn't
   need a visible label, e.g. an actions cell whose buttons already say
   what they do). */
@media (max-width: 640px) {
  .table-scroll { overflow-x: visible; }
  table.data-table thead { display: none; }
  table.data-table, table.data-table tbody { display: block; width: 100%; }
  table.data-table { min-width: 0 !important; }
  /* Two fields per line instead of one -- roughly half the card height of
     a strict one-field-per-line stack, nothing hidden. Cells lay out via
     CSS grid so the actions cell (the one <td> with no data-label) can
     force itself onto its own full-width row regardless of how many
     labeled fields came before it -- buttons need room, and pairing them
     with a field looks cramped. */
  table.data-table tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0 0.85rem;
    margin-bottom: 0.75rem;
  }
  table.data-table tr:last-child { margin-bottom: 0; }
  table.data-table td {
    display: flex;
    align-items: baseline;
    gap: 0.4rem;
    min-width: 0;
    padding: 0.55rem 0;
    border-bottom: 1px solid var(--border);
  }
  table.data-table td:nth-child(odd) { padding-right: 0.5rem; }
  table.data-table td:nth-child(even) { padding-left: 0.5rem; }
  table.data-table td:last-child { border-bottom: none; }
  table.data-table td::before {
    content: attr(data-label);
    font-size: 0.66rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
    font-weight: 700;
    flex-shrink: 0;
  }
  table.data-table td:not([data-label]) {
    grid-column: 1 / -1;
    flex-wrap: wrap;
    padding: 0.6rem 0;
  }
}
