/* 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 "Admin 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; }
}

/* ══════════════════════════════════════════════════════════════════════════
   RESPONSIVE BASE — 2026-08-01
   ─────────────────────────────────────────────────────────────────────────
   Every page's content lives inside `.main`, which is `overflow:hidden`. So a
   box that cannot shrink below its own content is not merely ugly on a phone:
   it is CLIPPED, and that content becomes unreachable with no scrollbar to
   reveal it. Measured across all 74 pages at 375px, 38 of them were losing
   content this way — up to 1163px of it on orders.html — while reporting no
   horizontal page scroll at all, which is why it went unnoticed for so long.

   The cause is `min-width:auto`, the default, which stops a flex/grid child
   shrinking past its min-content size. Setting it to 0 on LAYOUT boxes fixes
   that and is a no-op everywhere else: outside flex/grid, `auto` is already 0.
   Leaf controls (button, input, a, span, label) are deliberately left out so
   captions and icons keep their intrinsic size — `label` was in this list
   briefly and squeezed customer-360's "called" chip from 57px to 22px.
   ══════════════════════════════════════════════════════════════════════ */
:where(.main, body:not(:has(.main))) :where(div, section, article, aside, header, footer, form,
             ul, ol, li, nav, dl, dd, figure, table, pre, p,
             h1, h2, h3, h4, details, summary) { min-width: 0; }

/* …except inside a horizontal scroller, where content keeping its intrinsic
   width is the entire point — that is what there is to scroll. Without this
   exception a tab strip collapses into itself instead of scrolling. The list
   is every selector declaring `overflow-x:auto|scroll` in the 74 pages, so it
   is exhaustive rather than guessed; it must stay AFTER the rule above, which
   it ties with on specificity and wins only by source order. */
:where(.main, body:not(:has(.main))) :where(.at-scroll, .au-controls, .au-filters, .cf-filters, .cl-filters,
             .cp-filters, .ct-filters, .cw-statbar, .detail-tabs, .health-subnav,
             .ig-filters, .kanban, .knav, .leads-tabs, .li-wrap, .live-strip,
             .matrix-wrap, .mc-filters, .mc-health, .mwrap, .ot-filters,
             .profile-tabs, .queue-tabs, .rp-filters, .sc-filters, .scroll,
             .sg-filters, .state-tabs, .stepper, .table-wrap, .tabs, .tblwrap,
             .tk-filters, .tl, .twrap, .work-tabs, .li-table,
             [class*="-scroll"]) * { min-width: auto; }

/* ── Toolbars wrap instead of overflowing — at EVERY width ──────────────
   The dominant layout on these pages is a single-line toolbar:
     [ title + subtitle ] [ flex:1 spacer ] [ search ] [ button ] [ button ]
   At `flex-wrap:nowrap` the buttons are pushed straight out of the viewport
   and cannot be tapped. This is not a phone-only problem: orders.html
   overflows its own toolbar by 480px on a 1280px desktop. Wrapping is the
   honest answer at any width — where everything already fits, the rule does
   nothing whatsoever.

   `flex-wrap` is ignored on non-flex boxes, so applying it by naming
   convention is safe. Restricted to names meaning "a row of things": a flex
   COLUMN told to wrap spills sideways instead. Tab strips are left out on
   purpose — they are scrollers, and scrolling is the right idiom there.   */
:where(.main, body:not(:has(.main))) :where([class*="toolbar"], [class*="-head"], [class*="head-"],
             [class*="ttl-row"], [class*="title-row"], [class*="-actions"],
             [class*="-tools"], [class*="-row"], [class*="-bar"],
             [class*="filters"]) {
  flex-wrap: wrap;
  row-gap: 8px;
}

@media (max-width: 820px) {
  /* Media never dictates layout width. Phone-only, and `svg` is excluded on
     purpose: a percentage max-width on an icon inside a shrink-to-fit button
     resolves against a zero basis and collapses the icon completely — measured
     at 14px to 0px on every icon button of order-detail.html.               */
  :where(.main, body:not(:has(.main))) :where(img, video, canvas, iframe, embed) { max-width: 100%; }

  /* Wide tables scroll in place instead of widening (and so clipping) the page. */
  :where(.main, body:not(:has(.main))) :where(div, section, article, td):has(> table) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* A desktop-sized fixed width becomes a maximum, not a demand. `min-width`
     still wins over this, so tables inside a scroller keep scrolling.       */
  :where(.main, body:not(:has(.main))) :where(div, section, aside, article, form, table) { max-width: 100%; }

  /* Long unbroken strings — URLs, AWB numbers, JSON blobs, e-mail addresses —
     set a min-content floor that no amount of `min-width:0` can get under.
     Scoped to phones because `anywhere` also changes intrinsic sizing, and so
     would quietly re-flow desktop table columns.                            */
  :where(.main, body:not(:has(.main))) :where(td, th, p, li, dd, code, pre, .muted) { overflow-wrap: anywhere; }
}

/* ── The sidebar must actually LEAVE on a phone ─────────────────────────
   Pre-existing and app-wide: below 768px `.sidebar` becomes `position:fixed`
   but was never translated off-canvas, so the collapsed icon rail sat on top
   of the page permanently — measured at 375px, it covered the left ~100px of
   every screen and orders.html rendered its heading as "rders". Every page
   was unusable on a phone no matter how well its own layout behaved.

   Two things were wrong. The rail never moved, and `.app[data-sidebar=
   "collapsed"]` is declared OUTSIDE any media query, so it out-specifies the
   bare `.app` rule here and kept reserving a grid column for it. Hence the
   columns are re-stated per attribute value below — dropping the attribute
   would silently lose to the desktop rule.

   `sidebarOpen: window.innerWidth > 768` already defaults the drawer closed on
   phones (63 of 64 pages; recall-admin.html hardcodes `true`), and the
   hamburger and the backdrop already toggle it. Only the CSS was missing. */
@media (max-width: 768px) {
  .app,
  .app[data-sidebar="collapsed"],
  .app[data-sidebar="open"] { grid-template-columns: 0 1fr; }

  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 40;
    width: var(--sidebar-w);
    transform: translateX(-100%);
    transition: transform var(--dur) var(--ease-spring);
    box-shadow: var(--shadow-lg);
  }
  .app[data-sidebar="open"] .sidebar { transform: none; }

  /* The drawer is a full menu, never the desktop icon rail: undo the
     collapsed-state hiding for the one case where it is actually on screen. */
  .app[data-sidebar="open"] .brand-word { display: flex; }
  .app[data-sidebar="open"] .nav-label,
  .app[data-sidebar="open"] .nav-section-label { display: block; }
  .app[data-sidebar="open"] .nav-item .badge-count { display: inline-block; }
  .app[data-sidebar="open"] .user-meta { display: flex; }
}

/* ── Touch targets ─────────────────────────────────────────────────────
   Measured on orders.html at 375px: all 23 interactive controls were under
   36px tall, median 32px, smallest 26px — below both Apple's 44pt and
   Material's 48dp guidance, on a screen operated by thumb.

   A minimum HEIGHT is the least invasive correction available: it changes no
   width, so the wrapped toolbars above keep exactly the arrangement they were
   verified with. 40px is a deliberate compromise — a data-dense CRM with 44px
   rows everywhere grows unusably tall, and 40px clears the practical
   mis-tap threshold. Checkboxes and radios are left alone: stretching them
   distorts the control itself rather than its hit area.                    */
@media (max-width: 820px) {
  :where(.main, body:not(:has(.main)))
    :where(button, select, textarea, [role="button"], a[class*="btn"],
           a[class*="tbtn"], input:not([type="checkbox"]):not([type="radio"])) {
    min-height: 40px;
  }
}
