/* Ayurvedasar v6 — Shared App Shell (sidebar + topbar)
   Imported by every page mockup so chrome is consistent.
*/

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas: "sidebar topbar" "sidebar main";
  height: 100vh;
  transition: grid-template-columns var(--dur) var(--ease-spring);
}
.app[data-sidebar="collapsed"] { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }

/* ── Sidebar ────────────────────────────────────────────────────── */
.sidebar {
  grid-area: sidebar;
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 18px 12px;
  border-bottom: 1px solid var(--line);
}
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px;
  background: linear-gradient(135deg, var(--saffron) 0%, var(--mulberry) 100%);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display); font-weight: 600; font-size: 16px; color: #0F0E0B;
  flex-shrink: 0;
}
.brand-word { display: flex; flex-direction: column; line-height: 1.1; }
.brand-name {
  font-family: var(--font-display); font-size: 16px; font-weight: 500;
  letter-spacing: -0.005em; color: var(--ink); white-space: nowrap;
  font-variation-settings: 'opsz' 32, 'SOFT' 30;
}
.brand-sub {
  font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-faint); font-weight: 600;
}
[data-sidebar="collapsed"] .brand-word { display: none; }
[data-sidebar="collapsed"] .nav-label { display: none; }
[data-sidebar="collapsed"] .nav-section-label { display: none; }
[data-sidebar="collapsed"] .nav-item .badge-count { display: none; }
[data-sidebar="collapsed"] .user-meta { display: none; }
[data-sidebar="collapsed"] .sidebar-collapse-cta { display: none; }

.nav { padding: 12px; flex: 1; overflow-y: auto; }
.nav-section { margin-top: 18px; }
.nav-section:first-child { margin-top: 4px; }
.nav-section-label {
  font-size: 10px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-faint); padding: 0 10px 6px;
}

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: var(--radius);
  color: var(--ink-muted); font-size: 13px; font-weight: 500;
  cursor: pointer; user-select: none; text-decoration: none;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
  position: relative; margin-bottom: 1px;
}
.nav-item:hover { background: var(--surface-hover); color: var(--ink); }
.nav-item.active { background: var(--saffron-soft); color: var(--saffron-text); }
.nav-item.active::before {
  content: ''; position: absolute; left: -12px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 16px; background: var(--saffron); border-radius: 0 999px 999px 0;
}
.nav-item-icon { width: 16px; height: 16px; flex-shrink: 0; }
.badge-count {
  margin-left: auto; font-size: 10px; font-weight: 600;
  background: var(--surface-hover); color: var(--ink-muted);
  padding: 1px 6px; border-radius: 999px; min-width: 18px; text-align: center;
}
.nav-item.active .badge-count { background: var(--saffron); color: #0F0E0B; }
.nav-item.warning .badge-count { background: var(--terracotta); color: #0F0E0B; }

.sidebar-footer {
  border-top: 1px solid var(--line);
  padding: 10px 12px;
  display: flex; align-items: center; gap: 10px;
}
.sidebar-footer .avatar {
  width: 32px; height: 32px; font-size: 11px;
  background: var(--saffron-soft); color: var(--saffron-text);
  border-color: color-mix(in srgb, var(--saffron) 30%, transparent);
}
.user-meta { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.user-name {
  font-size: 13px; font-weight: 600; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-role { font-size: 11px; color: var(--ink-faint); }

/* ── Topbar ─────────────────────────────────────────────────────── */
.topbar {
  grid-area: topbar;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; padding: 0 16px; gap: 12px;
}

.topbar-toggle {
  background: transparent; border: 1px solid var(--line); color: var(--ink-muted);
  width: 30px; height: 30px; border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: all var(--dur-fast) var(--ease-out);
}
.topbar-toggle:hover { color: var(--ink); border-color: var(--line-strong); background: var(--surface-hover); }

.search {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface-elevated); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: var(--radius);
  width: 360px; max-width: 100%; color: var(--ink-muted);
  cursor: pointer; transition: border-color var(--dur-fast) var(--ease-out);
}
.search:hover { border-color: var(--line-strong); }
.search-kbd {
  margin-left: auto; font-family: var(--font-mono); font-size: 11px;
  background: var(--surface); border: 1px solid var(--line);
  padding: 1px 6px; border-radius: 4px; color: var(--ink-faint);
}

.topbar-spacer { flex: 1; }

.health-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
  background: var(--moss-soft); color: var(--moss-text);
  border: 1px solid color-mix(in srgb, var(--moss) 25%, transparent);
  cursor: pointer;
}
.health-pill-dot {
  width: 6px; height: 6px; border-radius: 999px; background: var(--moss);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--moss) 25%, transparent);
}
.health-pill-meta {
  font-family: var(--font-mono); font-size: 10px;
  color: var(--moss-text); opacity: 0.8;
}

.icon-btn {
  width: 32px; height: 32px; border-radius: var(--radius); border: 1px solid transparent;
  background: transparent; color: var(--ink-muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all var(--dur-fast) var(--ease-out);
  position: relative;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--ink); border-color: var(--line); }
.icon-btn .notification-dot {
  position: absolute; top: 6px; right: 6px;
  width: 7px; height: 7px; border-radius: 999px; background: var(--saffron);
  border: 2px solid var(--surface);
}

.presence-switcher {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 8px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface-elevated);
  cursor: pointer; font-size: 12px; font-weight: 500; color: var(--ink-muted);
}
.presence-switcher-dot { width: 8px; height: 8px; border-radius: 999px; background: var(--moss); }

/* ── Main area + responsive base ──────────────────────────────── */
.main { grid-area: main; overflow: hidden; min-height: 0; display: flex; flex-direction: column; }

.sidebar-overlay {
  display: none; position: fixed; inset: 0; z-index: 35;
  background: color-mix(in srgb, #0F0E0B 60%, transparent);
  backdrop-filter: blur(2px);
}

@media (max-width: 768px) {
  .app { grid-template-columns: 0 1fr; }
  .app[data-sidebar="open"] { grid-template-columns: var(--sidebar-w) 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 40; box-shadow: var(--shadow-lg); }
  .app[data-sidebar="open"] .sidebar-overlay { display: block; }
  .search { width: auto; flex: 1; }
  .search span:not(.search-kbd) { display: none; }
  .health-pill span:not(.health-pill-dot):not(.health-pill-meta) { display: none; }
  .health-pill .health-pill-meta { display: none; }
  .presence-switcher span:not(.presence-switcher-dot) { display: none; }
  .topbar { gap: 8px; padding: 0 12px; }
}

/* ── Role-based nav gating ───────────────────────────────────────────
   _api.js adds `role-<rolename>` to <body> after /api/auth/me resolves.
   Until it resolves, body has none of these classes — we err on the side
   of HIDING admin/grow sections during the loading flash (a sub-second
   blink is fine; showing admin nav to a sales_agent for a beat is not).

   Visibility rules (mirror server/auth.js ROLE_GROUPS):
     Admin  → super_admin only
     Grow   → super_admin + sales_manager
     Team   → super_admin + sales_manager + sales_agent + vaidya + support + ops
              (everyone except viewer)
     Knowledge → all roles
     Operate + Catalog → all roles (no class needed)
*/

/* Default: hide gated sections until we know the role */
.nav-section-admin, .nav-section-grow { display: none; }

/* Admin: super_admin only */
body.role-super_admin .nav-section-admin { display: block; }

/* Grow: super_admin + sales_manager */
body.role-super_admin .nav-section-grow,
body.role-sales_manager .nav-section-grow { display: block; }

/* Team section: hide for plain viewer (everyone else sees it) */
body.role-viewer .nav-section-team { display: none; }

/* Knowledge: always visible (no override needed) */

/* Loading state — before /api/auth/me resolves, dim the user-meta block
   so it doesn't show stale "Amritesh Jain" SSR fallback as if it were live. */
body:not([class*="role-"]) .user-meta { opacity: 0.4; }
body[class*="role-"] .user-meta { opacity: 1; transition: opacity .15s ease-out; }

/* W sprint — A11y: visible keyboard focus rings (WCAG 2.4.7). Replaces UA default
 * which is often invisible on dark themes. Uses focus-visible so mouse clicks
 * don't trigger the ring (only keyboard tab/Shift+Tab/Space). */
*:focus { outline: 0; }  /* defer to focus-visible */
*:focus-visible {
  outline: 2px solid var(--saffron, #d8a64a);
  outline-offset: 2px;
  border-radius: 4px;
}
button:focus-visible, a:focus-visible, [role=button]:focus-visible {
  outline: 2px solid var(--saffron, #d8a64a);
  outline-offset: 2px;
}
/* Skip link — appears only when focused via keyboard */
.skip-link {
  position: absolute; top: -100px; left: 8px; z-index: 9999;
  background: var(--saffron, #d8a64a); color: #0F0E0B; padding: 8px 14px;
  border-radius: 6px; font-weight: 700; text-decoration: none;
  transition: top 0.15s ease-out;
}
.skip-link:focus { top: 8px; outline: 2px solid #0F0E0B; outline-offset: 2px; }

/* W sprint — Mobile audit fixes. Tested at 375×667 (iPhone SE) and 768×1024 (iPad).
 * Patches the worst horizontal-overflow culprits and broken drawer sheets.   */
@media (max-width: 480px) {
  body { -webkit-text-size-adjust: 100%; }
  /* Force tables to scroll inside their wrapper instead of breaking layout */
  .table-wrap, .ct-scroll, .ot-scroll, .cl-scroll, .mc-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table { min-width: 100%; }
  /* Sheets / drawers: snap to full-width on mobile */
  .sheet, .bld, .builder, .du-cmdk { width: 100vw !important; max-width: 100vw !important; border-radius: 0 !important; }
  .sheet-backdrop, .bld-back, .builder-back { padding: 0 !important; }
  /* Topbar collapses */
  .topbar .search { display: none; }
  .topbar-spacer { flex: 1; }
  /* Stat grids drop to 1-col */
  .mc-stats, .cl-stats, .ord-stats, .ct-stats, .sg-stats, .co-stats { grid-template-columns: 1fr !important; }
  .mstat, .cstat, .ostat, .sstat, .fstat { border-right: 0 !important; border-bottom: 1px solid var(--line); }
  /* KPI grids */
  .kpi-mini-grid, .kpis { grid-template-columns: 1fr 1fr !important; }
}
@media (max-width: 768px) {
  /* Sidebar overlays as drawer */
  .app[data-sidebar="open"] .sidebar-overlay { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 35; }
  .app[data-sidebar="open"] .sidebar { z-index: 40; box-shadow: var(--shadow-lg, 0 16px 40px rgba(0,0,0,0.4)); }
  /* Prevent inputs from triggering iOS zoom */
  input, textarea, select { font-size: max(14px, 1rem) !important; }
}
