/*
 * App chrome on Flask/Jinja pages: layout for the slots rendered by
 * pages/shell_macros.jinja and filled by js/app-shell/render.tsx.
 * Loaded only when a page sets `app_shell` (see pgf.html.jinja).
 *
 * Sizes mirror js/app-shell/constants.ts (HEADER_HEIGHT, DRAWER_WIDTH,
 * RAIL_WIDTH, PAGE_PANEL_*); 900px is MUI's `md` breakpoint (sidebar becomes
 * an overlay below it). Colours mirror ChromeTheme (the app bar, signed in
 * or not, and the sidebar), MatTheme (the page) and BootstrapDarkTheme
 * (the logged-out footer).
 *
 * Signed in, the app bar and sidebar are one dark frame and the page is a
 * white rounded panel inset in it: <body> (the scroll container) is that
 * panel, over the dark <html> background.
 */

:root {
  --shell-header-h: 48px;
  --shell-drawer-w: 260px;
  --shell-rail-w: 64px;
  --shell-nav-w: var(--shell-drawer-w);
  /* MUI's leavingScreen duration + sharp easing (Sidebar's transition) */
  --shell-transition: 195ms cubic-bezier(0.4, 0, 0.6, 1);

  /* MatTheme palette */
  --shell-bg: #fff;
  --shell-text: rgba(0, 0, 0, 0.87);
  /* chromeBorderColor: divider at CHROME_BORDER_OPACITY (0.6) */
  --shell-chrome-border: rgba(0, 0, 0, 0.072);

  /* ChromeTheme: CHROME_BG (blue slate) */
  --shell-chrome-bg: #151a28;
  /* PAGE_PANEL_INSET (8px) / PAGE_PANEL_RADIUS */
  --shell-panel-inset: 8px;
  --shell-panel-radius: 12px;
}

html[data-shell-sidebar="rail"] {
  --shell-nav-w: var(--shell-rail-w);
}

/* ---- Slots ------------------------------------------------------------ */

/* Same z-index as Bootstrap's fixed-top navbar it replaces: above sticky
   bars (1020), below Bootstrap modals (1050) and MUI overlays (1200+). */
.app-shell-header {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  height: var(--shell-header-h);
  z-index: 1030;
}

/* Just below the header, so the app bar's shadow falls over the sidebar. */
.app-shell-nav {
  position: fixed;
  top: var(--shell-header-h);
  bottom: 0;
  left: 0;
  width: var(--shell-nav-w);
  z-index: 1029;
  transition: width var(--shell-transition);
}

/* The React Drawer root fills the slot (in the dashboard a flex row
   stretches it), so the sidebar's paper and border run full height. */
.app-shell-nav > .MuiDrawer-root {
  height: 100%;
}

/* Scroll the page below the app bar, as in the dashboard (where Main is the
   only scroll region): <body> is the scroll container, offset by the header,
   so the scrollbar never runs beside the app bar. !important beats
   Bootstrap's .h-100 on <body>. */
html[data-shell] {
  height: 100%;
  overflow: hidden;
}

html[data-shell] body {
  height: calc(100% - var(--shell-header-h)) !important;
  margin-top: var(--shell-header-h);
  overflow-y: auto;
}

/* Signed in: <body> is the rounded page panel, inset from the sidebar, the
   app bar and the window's right and bottom edges, over the dark chrome. */
/* The chrome colour: the app bar and sidebar are transparent over it (as
   in the dashboard's AppShell). */
html[data-shell="app"] {
  background: var(--shell-chrome-bg);
}

html[data-shell="app"] body {
  height: calc(
    100% - var(--shell-header-h) - var(--shell-panel-inset)
  ) !important;
  margin: var(--shell-header-h) var(--shell-panel-inset)
    var(--shell-panel-inset);
  border-radius: var(--shell-panel-radius);
}

@media (min-width: 900px) {
  html[data-shell="app"] body {
    margin-left: var(--shell-nav-w);
    transition: margin-left var(--shell-transition);
  }
}

/* Below md the sidebar is a temporary overlay (portalled to <body>). */
@media (max-width: 899.98px) {
  .app-shell-nav {
    display: none;
  }
}

/* ---- Placeholders (removed once React renders the chrome) -------------- */

.app-shell-placeholder {
  box-sizing: border-box;
  height: 100%;
}

/* Signed in: transparent over the chrome colour, no borders (as
   ShellAppBar / Sidebar). */
.app-shell-placeholder--bar,
.app-shell-placeholder--nav {
  background: transparent;
}

/* Logged out: the chrome colour and hairline (ShellAppBar's public
   variant), so the bar doesn't change colour when React renders. */
html[data-shell="public"] .app-shell-placeholder--bar {
  background: var(--shell-chrome-bg);
  border-bottom: 1px solid rgba(255, 255, 255, 0.14);
}

/* ---- Legacy layout fixes ----------------------------------------------- */

/* Sticky page bars sat under the 52px Bootstrap navbar; <body> now scrolls
   below the app bar, so they stick at its top. (The "Logged in as" title
   panels are not rendered on app-shell pages at all.) */
html[data-shell] .sticky-offset {
  top: 0;
}

/* 100vw "full-bleed" bars would slide under the sidebar. */
html[data-shell] .full-width-div {
  width: auto;
  margin-left: 0;
}

/* ---- Light mode for signed-in pages ------------------------------------ */

html[data-shell="app"] body {
  background-color: var(--shell-bg);
  background-image: none;
  color: var(--shell-text);
}

/* ---- Signed-in content area: page padding ------------------------------ */

/* The shell pads every page, as the dashboard's Main does: keep in sync with
   PAGE_PADDING in js/themes/layout.ts ({ px: 3, py: 2 } = 24px / 16px).
   Pages (React islands or plain Jinja) add none of their own; edge-to-edge
   pages set `app_shell_full_bleed` (--full-bleed). Bootstrap's centred
   .container and its pt-1 / pb-4 / px-0 utilities (!important) are dropped. */
:root {
  --shell-page-px: 24px;
  --shell-page-py: 16px;
}

html[data-shell="app"] .app-shell-main {
  padding: var(--shell-page-py) var(--shell-page-px);
}

/* Secondary content blocks (content_2 / content_3): side padding only, so
   empty ones take no space. */
html[data-shell="app"] .app-shell-main-extra {
  padding: 0 var(--shell-page-px);
}

html[data-shell="app"] .app-shell-main--full-bleed {
  padding: 0;
}

html[data-shell="app"] .app-shell-main > .container,
html[data-shell="app"] .app-shell-main-extra > .container {
  max-width: none;
  margin: 0;
  padding: 0 !important;
}

/* ---- Legacy light-on-dark text on the light content area --------------- */

/* Legacy templates were written for the dark page background: white /
   light text (Bootstrap .text-white / .text-light, the #home-page headings)
   and the inline-white PolymRize logo snippets (pgf.html.jinja's
   PolymRize_*_light <font> tags). On signed-in shell pages they sit on white,
   so map them to the theme's text / logo colours. Text on genuinely dark or
   coloured surfaces (buttons, badges, any .bg-* utility) keeps its colour.
   Remove once those templates are cleaned up. */
html[data-shell="app"]
  :is(.app-shell-main, .app-shell-main-extra)
  :is(.text-white, .text-light):not(.btn, .badge, [class*="bg-"]):not(
    .btn *,
    .badge *,
    [class*="bg-"] *
  ),
html[data-shell="app"] :is(.app-shell-main, .app-shell-main-extra) #home-page {
  color: var(--shell-text) !important;
}

/* MatTheme palette.logo (primary / secondary). */
html[data-shell="app"]
  :is(.app-shell-main, .app-shell-main-extra)
  font[style*="color:white"] {
  color: #333333 !important;
}

html[data-shell="app"]
  :is(.app-shell-main, .app-shell-main-extra)
  font[style*="#cdcdcd"] {
  color: #999999 !important;
}

/* ---- Legacy page wrappers on the padded content area -------------------- */

/* Pages framed for the dark background: `alert alert-light` boxes (a light
   card, often bordered and with inline padding) around the page or around
   sections of it — settings, admin, lists, job status / results — and top
   margins (`mt-5` headings / heading rows) that cleared the fixed legacy
   navbar. On signed-in shell pages the content area is already white and
   padded, so those cards become plain containers and the top margins go.
   The CSV drop zone (also built from .alert-light) keeps its box; other
   alert colours (real notifications) are untouched. Remove once those
   templates are cleaned up. */
html[data-shell="app"]
  :is(.app-shell-main, .app-shell-main-extra)
  .alert.alert-light:not(.fileDropZone) {
  padding: 0 !important;
  /* !important: beats Bootstrap's .border utility on these wrappers */
  border: 0 !important;
  border-radius: 0;
  background: transparent;
  color: inherit;
}

html[data-shell="app"] .app-shell-main > .container > h1.mt-5:first-child,
html[data-shell="app"] .app-shell-main .row.mt-5:has(#home-page) {
  margin-top: 0 !important;
}

/* ---- Dashboard boot frame ---------------------------------------------- */

/* The /dashboard SPA's chrome before JavaScript (pages/dashboard/
   index.html.jinja renders it inside #dashboardRoot; React clears it on its
   first render and draws AppShell in the same place). Not the Flask layout
   above: there <body> is the page panel, here the frame is its own 100vh
   flex box, laid out like AppShell: space for the app bar and sidebar over
   the chrome colour, and the empty white page panel. The sidebar's width
   comes from <html data-shell-sidebar> (the server reads the cookie), as on
   Flask pages. */
.shell-boot {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: var(--shell-chrome-bg);
}

.shell-boot__bar {
  flex: none;
  height: var(--shell-header-h);
}

.shell-boot__row {
  display: flex;
  flex: 1;
  min-height: 0;
}

.shell-boot__nav {
  flex: none;
  width: var(--shell-nav-w);
}

/* Main: PAGE_PANEL_INSET on the right and bottom, PAGE_PADDING inside. */
.shell-boot__panel {
  flex: 1;
  min-width: 0;
  margin: 0 var(--shell-panel-inset) var(--shell-panel-inset) 0;
  padding: var(--shell-page-py) var(--shell-page-px);
  border-radius: var(--shell-panel-radius);
  background: var(--shell-bg);
}

/* Below md the sidebar is an overlay: no column, the panel inset all round. */
@media (max-width: 899.98px) {
  .shell-boot__nav {
    display: none;
  }

  .shell-boot__panel {
    margin-left: var(--shell-panel-inset);
  }
}
