/* ==========================================================================
   Innovation Hub — layout.css
   App frame: sidebar, top bar, main area, page header, content grids.
   Attributes on <html> drive the frame:
     data-sidebar="light" | "brand"       tenant default (white or deep purple)
     data-sidebar-collapsed="true"        user's choice on desktop
     data-theme="light" | "dark"          resolved from the user's preference
   ========================================================================== */

/* ---------- Frame ---------- */
.app {
  display: grid;
  grid-template-columns: var(--ih-sidebar) minmax(0, 1fr);
  min-block-size: 100vh;
}
html[data-sidebar-collapsed="true"] .app { grid-template-columns: var(--ih-sidebar-collapsed) minmax(0, 1fr); }

.app-main { display: flex; flex-direction: column; min-inline-size: 0; }
.app-content { flex: 1; inline-size: 100%; max-inline-size: var(--ih-content-max); margin-inline: auto; padding: var(--ih-space-8) var(--ih-space-8) var(--ih-space-16); }
.app-content.is-wide { max-inline-size: none; }

/* ---------- Sidebar ---------- */
.sidebar {
  --sb-bg: var(--ih-surface);
  --sb-border: var(--ih-border);
  --sb-text: var(--ih-text-muted);
  --sb-text-strong: var(--ih-text);
  --sb-heading: var(--ih-text-subtle);
  --sb-hover: var(--ih-surface-sunken);
  --sb-active-bg: var(--ih-primary-soft);
  --sb-active-text: var(--ih-primary);
  --sb-active-bar: var(--ih-primary);

  position: sticky; inset-block-start: 0; block-size: 100vh; z-index: 40;
  display: flex; flex-direction: column;
  background: var(--sb-bg); border-inline-end: 1px solid var(--sb-border);
  overflow: hidden;
}
html[data-theme="dark"] .sidebar { --sb-active-text: var(--ih-text); }

/* Deep purple variant (tenant setting) */
html[data-sidebar="brand"] .sidebar {
  --sb-bg: var(--ih-sidebar-brand-bg, var(--ih-deep));
  --sb-border: transparent;
  --sb-text: rgba(255, 255, 255, 0.74);
  --sb-text-strong: #FFFFFF;
  --sb-heading: rgba(255, 255, 255, 0.5);
  --sb-hover: rgba(255, 255, 255, 0.08);
  --sb-active-bg: rgba(255, 255, 255, 0.14);
  --sb-active-text: #FFFFFF;
  --sb-active-bar: var(--ih-yellow-400);
}
html[data-sidebar="brand"][data-theme="dark"] .sidebar { --sb-bg: var(--ih-sidebar-brand-bg-dark, #1A1433); }

.sidebar-head {
  display: flex; align-items: center; gap: var(--ih-space-3);
  block-size: var(--ih-topbar); padding-inline: var(--ih-space-5); flex: none;
}
.sidebar-logo { display: flex; align-items: center; min-inline-size: 0; }
.sidebar-logo img { block-size: 34px; inline-size: auto; max-inline-size: 170px; }
.logo-compact { display: none; }
.logo-compact img { block-size: 32px; }

/* Logo variant follows the surface it sits on:
   .on-light-surface shows on white; .on-dark-surface on deep purple or dark theme. */
.on-dark-surface { display: none !important; }
html[data-sidebar="brand"] .sidebar .on-light-surface,
html[data-theme="dark"] .on-light-surface { display: none !important; }
html[data-sidebar="brand"] .sidebar .on-dark-surface,
html[data-theme="dark"] .on-dark-surface { display: block !important; }

.sidebar-nav { flex: 1; overflow-y: auto; overflow-x: hidden; padding: var(--ih-space-2) var(--ih-space-3) var(--ih-space-4); scrollbar-width: thin; }
.nav-section + .nav-section { margin-block-start: var(--ih-space-5); }
.nav-heading {
  padding: 0 var(--ih-space-3) var(--ih-space-2); font-size: 11px; font-weight: 700;
  letter-spacing: 0.08em; text-transform: uppercase; color: var(--sb-heading); white-space: nowrap;
}
html[lang="ar"] .nav-heading { letter-spacing: 0; font-size: 12px; text-transform: none; }

.nav-link {
  position: relative; display: flex; align-items: center; gap: var(--ih-space-3);
  min-block-size: 40px; padding: 0 var(--ih-space-3); border-radius: var(--ih-radius-md);
  color: var(--sb-text); font-weight: 600; font-size: var(--ih-text-sm); white-space: nowrap;
  transition: background var(--ih-duration-fast) var(--ih-ease), color var(--ih-duration-fast) var(--ih-ease);
}
.nav-link:hover { background: var(--sb-hover); color: var(--sb-text-strong); text-decoration: none; }
.nav-link[aria-current="page"] { background: var(--sb-active-bg); color: var(--sb-active-text); }
.nav-link[aria-current="page"]::before {
  content: ""; position: absolute; inset-inline-start: calc(var(--ih-space-3) * -1); inset-block: 8px;
  inline-size: 3px; border-start-end-radius: 3px; border-end-end-radius: 3px; background: var(--sb-active-bar);
}
.nav-link .icon { inline-size: 20px; block-size: 20px; }
.nav-label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.nav-count {
  min-inline-size: 22px; block-size: 20px; padding: 0 6px; border-radius: 10px; display: grid; place-items: center;
  font-size: 11px; font-weight: 700; background: var(--ih-primary); color: var(--ih-text-on-brand);
}
html[data-sidebar="brand"] .nav-count { background: var(--ih-yellow-400); color: var(--ih-neutral-900); }

.sidebar-foot { flex: none; padding: var(--ih-space-3); border-block-start: 1px solid var(--sb-border); }
html[data-sidebar="brand"] .sidebar-foot { border-block-start-color: rgba(255, 255, 255, 0.1); }
.powered-by { display: flex; align-items: center; gap: 8px; padding: 6px var(--ih-space-3) 2px; font-size: 11px; color: var(--sb-heading); white-space: nowrap; }
.powered-by img { block-size: 16px; inline-size: auto; }
html:not([data-tenant-logo="custom"]) .powered-by { display: none; }

/* Collapsed sidebar (desktop only): icons only, labels become tooltips */
@media (min-width: 1024px) {
  html[data-sidebar-collapsed="true"] .sidebar :is(.nav-label, .nav-heading, .nav-count, .powered-by span) { display: none; }
  html[data-sidebar-collapsed="true"] .sidebar .nav-section + .nav-section { margin-block-start: var(--ih-space-3); padding-block-start: var(--ih-space-3); border-block-start: 1px solid var(--sb-border); }
  html[data-sidebar-collapsed="true"][data-sidebar="brand"] .sidebar .nav-section + .nav-section { border-block-start-color: rgba(255, 255, 255, 0.1); }
  html[data-sidebar-collapsed="true"] .sidebar .nav-link { justify-content: center; padding: 0; }
  html[data-sidebar-collapsed="true"] .sidebar-head { justify-content: center; padding-inline: 0; }
  html[data-sidebar-collapsed="true"] .logo-full { display: none; }
  html[data-sidebar-collapsed="true"] .logo-compact { display: block; }
  html[data-sidebar-collapsed="true"] .powered-by { justify-content: center; padding-inline: 0; }
}
.collapse-btn .icon { transition: transform var(--ih-duration-base) var(--ih-ease); }
html[data-sidebar-collapsed="true"] .collapse-btn .icon,
[dir="rtl"] .collapse-btn .icon { transform: scaleX(-1); }
html[dir="rtl"][data-sidebar-collapsed="true"] .collapse-btn .icon { transform: none; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; inset-block-start: 0; z-index: 30;
  display: flex; align-items: center; gap: var(--ih-space-3);
  block-size: var(--ih-topbar); padding-inline: var(--ih-space-6);
  background: color-mix(in srgb, var(--ih-surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-block-end: 1px solid var(--ih-border);
}
.topbar::before { /* brand line: the logo frame's gradient, 3px */
  content: ""; position: absolute; inset-inline: 0; inset-block-start: 0; block-size: 3px; background: var(--ih-gradient);
}
[dir="rtl"] .topbar::before { transform: scaleX(-1); }
.topbar-logo { display: none; flex: none; }
.topbar-logo img { block-size: 28px; inline-size: auto; max-inline-size: none; }
.topbar-search { flex: 1; max-inline-size: 440px; }
.topbar-actions { display: flex; align-items: center; gap: var(--ih-space-1); margin-inline-start: auto; }
.topbar .menu-btn { display: none; }

.user-chip { display: flex; align-items: center; gap: var(--ih-space-3); padding: 4px; padding-inline-end: 10px; border: 0; background: transparent; border-radius: var(--ih-radius-full); }
.user-chip:hover { background: var(--ih-surface-sunken); }
.user-chip .user-text { display: grid; text-align: start; line-height: 1.2; }
.user-chip .user-name { font-weight: 600; font-size: var(--ih-text-sm); }
.user-chip .user-role { font-size: var(--ih-text-xs); color: var(--ih-text-subtle); }

/* ---------- Page header ---------- */
.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--ih-space-4); flex-wrap: wrap; margin-block-end: var(--ih-space-6); }
.page-header .breadcrumb { margin-block-end: var(--ih-space-2); }
.page-title { font-size: var(--ih-text-2xl); line-height: 1.3; font-weight: 700; }
.page-subtitle { margin-block-start: 4px; color: var(--ih-text-muted); }
.page-actions { display: flex; gap: var(--ih-space-2); flex-wrap: wrap; }

/* ---------- Content grids ---------- */
.stack { display: grid; gap: var(--ih-space-6); align-content: start; }
.stack-sm { display: grid; gap: var(--ih-space-3); align-content: start; }
.grid { display: grid; gap: var(--ih-space-4); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-main-side { grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); }
.grid-list-detail { grid-template-columns: minmax(240px, 1fr) minmax(0, 2fr); align-items: start; }
.span-2 { grid-column: span 2; }
.cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--ih-space-3); }
.spread { display: flex; align-items: center; justify-content: space-between; gap: var(--ih-space-3); }

/* ---------- Scrim for the mobile sidebar and drawers ---------- */
.scrim { position: fixed; inset: 0; z-index: 35; background: rgba(18, 16, 24, 0.5); opacity: 0; pointer-events: none; transition: opacity var(--ih-duration-base) var(--ih-ease); }
html.nav-open .scrim, html.drawer-open .scrim { opacity: 1; pointer-events: auto; }

/* ---------- Responsive ---------- */
@media (max-width: 1279px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 1023px) {
  .app, html[data-sidebar-collapsed="true"] .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset-block: 0; inset-inline-start: 0; inline-size: min(86vw, var(--ih-sidebar));
    transform: translateX(-100%); transition: transform var(--ih-duration-slow) var(--ih-ease); box-shadow: var(--ih-shadow-lg);
  }
  [dir="rtl"] .sidebar { transform: translateX(100%); }
  html.nav-open .sidebar { transform: none; }
  .collapse-btn { display: none !important; }
  .topbar .menu-btn { display: inline-flex; }
  .topbar-search { display: none; }
  .topbar-logo { display: block; }
  .topbar { padding-inline: var(--ih-space-4); }
  .app-content { padding: var(--ih-space-6) var(--ih-space-5) var(--ih-space-12); }
  .grid-3, .grid-main-side, .grid-list-detail { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 767px) {
  .topbar-search { display: none; }
  .user-chip .user-text { display: none; }
  .topbar { gap: var(--ih-space-1); padding-inline: var(--ih-space-2) var(--ih-space-3); }
  .topbar [data-lang-label], .topbar .user-chip > .icon { display: none; }
  .topbar [data-action="toggle-lang"] { inline-size: 40px; padding: 0; }
  .topbar-logo { min-inline-size: 0; }
  .topbar-logo img { block-size: 24px; }
  .topbar-actions { gap: 0; }
  .user-chip { padding-inline-end: 4px; }
  .app-content { padding: var(--ih-space-5) var(--ih-space-4) var(--ih-space-12); }
  .grid-2, .grid-4 { grid-template-columns: minmax(0, 1fr); }
  .span-2 { grid-column: auto; }
  .page-title { font-size: var(--ih-text-xl); }
  .page-actions { inline-size: 100%; }
}

/* ---------- Tenant logo switch (data-tenant-logo on <html>) ---------- */
html[data-tenant-logo="custom"] .brand-ih,
html:not([data-tenant-logo="custom"]) .brand-tenant { display: none !important; }
.collapse-btn { inline-size: 100%; border: 0; background: none; }
/* Grid and flex children may shrink below their content's width (prevents sideways overflow) */
.stack > *, .stack-sm > *, .grid > *, .doc-layout > * { min-inline-size: 0; }
.collapse-btn { justify-content: flex-start; text-align: start; }
/* Figure cards: as many columns as fit, never narrower than 180px */
.grid-stats { grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); }

/* Template-only audience switcher (top bar) */
.audience-switch { margin-inline-end: var(--ih-space-2); }
.audience-switch .btn { border-style: dashed; }
.audience-switch .menu-item[aria-current="true"] { background: var(--ih-primary-soft); color: var(--ih-primary); font-weight: 600; }
html[data-theme="dark"] .audience-switch .menu-item[aria-current="true"] { color: var(--ih-text); }
@media (max-width: 1279px) { .audience-switch .audience-label > span { display: none; } }
@media (max-width: 767px) { .audience-switch .audience-label { display: none; } .audience-switch { margin-inline-end: 0; } }

/* ---------- Operator console (html[data-area="console"]) ----------
   Same components and tokens; a fixed charcoal side menu and an "Operator console" label,
   so platform staff always know they are outside a customer's space. Tenant branding never applies. */
html[data-sidebar="console"] .sidebar {
  --sb-bg: #17151F;
  --sb-border: transparent;
  --sb-text: rgba(255, 255, 255, 0.72);
  --sb-text-strong: #FFFFFF;
  --sb-heading: rgba(255, 255, 255, 0.46);
  --sb-hover: rgba(255, 255, 255, 0.07);
  --sb-active-bg: rgba(255, 255, 255, 0.12);
  --sb-active-text: #FFFFFF;
  --sb-active-bar: var(--ih-yellow-400);
}
html[data-sidebar="console"] .sidebar .on-light-surface { display: none !important; }
html[data-sidebar="console"] .sidebar .on-dark-surface { display: block !important; }
html[data-sidebar="console"] .sidebar-foot { border-block-start-color: rgba(255, 255, 255, 0.1); }
html[data-sidebar="console"] .nav-count { background: var(--ih-yellow-400); color: var(--ih-neutral-900); }
.console-tag { display: inline-flex; align-items: center; gap: 6px; margin: 0 var(--ih-space-5) var(--ih-space-3); padding: 4px 10px; border-radius: var(--ih-radius-full); background: rgba(244, 200, 39, 0.14); color: var(--ih-yellow-300); font-size: 11px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; white-space: nowrap; }
html[lang="ar"] .console-tag { letter-spacing: 0; text-transform: none; font-size: 12px; }
.console-tag .icon { inline-size: 14px; block-size: 14px; }
@media (min-width: 1024px) { html[data-sidebar-collapsed="true"] .console-tag span { display: none; } html[data-sidebar-collapsed="true"] .console-tag { margin-inline: auto; } }
html[data-area="console"] .topbar::before { background: repeating-linear-gradient(-45deg, var(--ih-yellow-400) 0 8px, #17151F 8px 16px); }
html[data-area="console"] .tenant-only { display: none !important; }
.console-scope { display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: var(--ih-radius-md); background: var(--ih-surface-sunken); font-size: var(--ih-text-sm); font-weight: 600; color: var(--ih-text-muted); white-space: nowrap; }
.console-scope .icon { inline-size: 16px; block-size: 16px; color: var(--ih-accent-text); }
@media (max-width: 767px) { .console-scope span { display: none; } }

/* Tenant initials tile (console lists) */
.org-tile { inline-size: 36px; block-size: 36px; flex: none; border-radius: var(--ih-radius-md); display: grid; place-items: center; font-size: 12px; font-weight: 800; color: #fff; background: var(--ih-purple-600); }
.org-tile.t2 { background: #1D5FA8; } .org-tile.t3 { background: #0F766E; } .org-tile.t4 { background: #9A3B5B; } .org-tile.t5 { background: #7A5A12; } .org-tile.t6 { background: #433F51; }
.org-tile-lg { inline-size: 56px; block-size: 56px; border-radius: var(--ih-radius-lg); font-size: 18px; }

/* System status strip */
.status-strip { display: flex; align-items: center; gap: var(--ih-space-3); padding: var(--ih-space-4) var(--ih-space-5); border-radius: var(--ih-radius-lg); border: 1px solid var(--ih-warning-border); background: var(--ih-warning-bg); }
.status-strip.is-ok { border-color: var(--ih-success-border); background: var(--ih-success-bg); }
.status-strip .icon { color: var(--ih-warning); }
.status-strip.is-ok .icon { color: var(--ih-success); }
.status-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: var(--ih-space-3); }
.service { display: flex; align-items: center; gap: 10px; padding: 12px var(--ih-space-4); border: 1px solid var(--ih-border); border-radius: var(--ih-radius-md); background: var(--ih-surface); font-size: var(--ih-text-sm); font-weight: 600; }
.service .state { inline-size: 10px; block-size: 10px; border-radius: 50%; background: var(--ih-success); flex: none; box-shadow: 0 0 0 3px var(--ih-success-bg); }
.service .state.is-warn { background: var(--ih-warning); box-shadow: 0 0 0 3px var(--ih-warning-bg); }
.service .state.is-down { background: var(--ih-danger); box-shadow: 0 0 0 3px var(--ih-danger-bg); }
.service small { margin-inline-start: auto; font-weight: 500; color: var(--ih-text-subtle); font-size: var(--ih-text-xs); }

/* Framework stages editor */
.stage-list { list-style: none; display: grid; gap: var(--ih-space-2); counter-reset: st; }
.stage-item { display: grid; grid-template-columns: 20px 32px minmax(0, 1fr) auto; gap: var(--ih-space-3); align-items: center; padding: 12px var(--ih-space-4); border: 1px solid var(--ih-border); border-radius: var(--ih-radius-md); background: var(--ih-surface); }
.stage-item .grip { color: var(--ih-text-subtle); cursor: grab; }
.stage-item .no { counter-increment: st; inline-size: 32px; block-size: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--ih-primary-soft); color: var(--ih-primary); font-weight: 800; font-size: 13px; }
.stage-item .no::before { content: counter(st); }
html[data-theme="dark"] .stage-item .no { color: var(--ih-link); }
.stage-item b { display: block; }
.stage-item span.sub { font-size: var(--ih-text-xs); color: var(--ih-text-subtle); }
.stage-add { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 12px; border: 1.5px dashed var(--ih-border-strong); border-radius: var(--ih-radius-md); background: none; color: var(--ih-text-muted); font-weight: 600; inline-size: 100%; }
.stage-add:hover { border-color: var(--ih-primary); color: var(--ih-primary); }
.framework-card { display: grid; gap: var(--ih-space-3); padding: var(--ih-space-5); color: inherit; }
.framework-card:hover { text-decoration: none; }
.framework-card[aria-current="true"] { border-color: var(--ih-primary); box-shadow: 0 0 0 1px var(--ih-primary); }

/* Import preview: rows with problems */
.table tr.has-error td { background: color-mix(in srgb, var(--ih-danger-bg) 70%, transparent); }
.table tr.has-warning td { background: color-mix(in srgb, var(--ih-warning-bg) 70%, transparent); }
.cell-error { display: block; font-size: var(--ih-text-xs); color: var(--ih-danger); font-weight: 600; }
.cell-warn { display: block; font-size: var(--ih-text-xs); color: var(--ih-warning); font-weight: 600; }
.wizard-steps { display: flex; gap: var(--ih-space-2); list-style: none; overflow-x: auto; scrollbar-width: none; }
.wizard-steps li { flex: 1; min-inline-size: 140px; display: flex; align-items: center; gap: 10px; padding: 12px var(--ih-space-4); border-radius: var(--ih-radius-md); background: var(--ih-surface); border: 1px solid var(--ih-border); font-size: var(--ih-text-sm); font-weight: 600; color: var(--ih-text-subtle); }
.wizard-steps .step-no { inline-size: 26px; block-size: 26px; border-radius: 50%; display: grid; place-items: center; border: 1.5px solid var(--ih-border-strong); font-size: 12px; flex: none; }
.wizard-steps li.is-done { color: var(--ih-text-muted); }
.wizard-steps li.is-done .step-no { background: var(--ih-success); border-color: var(--ih-success); color: #fff; }
.wizard-steps li[aria-current="step"] { color: var(--ih-text); border-color: var(--ih-primary); box-shadow: 0 0 0 1px var(--ih-primary); }
.wizard-steps li[aria-current="step"] .step-no { background: var(--ih-primary); border-color: var(--ih-primary); color: var(--ih-text-on-brand); }
