/* ==========================================================================
   Printlane — application shell and responsive behaviour.

   Three layouts from one DOM:
     >=1100px  fixed sidebar + topbar + optional right rail
      720-1099 collapsible sidebar as an overlay drawer, no right rail
      <720     mobile: bottom tab bar, sheets instead of dialogs, stacked rails
   ========================================================================== */

/* ------------------------------------------------------------- sign-in gate */
.auth-gate { min-height: 100dvh; }
.auth-wrap {
  min-height: 100dvh; display: grid; place-items: center; padding: var(--gutter);
  background:
    radial-gradient(900px 480px at 50% -8%,
      color-mix(in srgb, var(--brand-500) 14%, transparent), transparent 70%);
}
.auth-card { width: min(400px, 100%); box-shadow: var(--sh-md); }

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  /* The explicit rows are required, not cosmetic. The sidebar uses
     `grid-row: 1 / -1`, and -1 resolves against the EXPLICIT grid — with no
     rows declared it resolved to line 1, so the span collapsed into row 1 and
     the sidebar's 100dvh height inflated that row to a full viewport. The main
     column then began one screen below the fold. */
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 100dvh;
}

/* ------------------------------------------------------------- sidebar */
.sidebar {
  grid-row: 1 / -1;
  display: flex; flex-direction: column; gap: var(--sp-4);
  padding: var(--sp-4) var(--sp-3);
  background: var(--surface); border-right: 1px solid var(--line);
  position: sticky; top: 0; height: 100dvh; overflow-y: auto;
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: .55rem; padding: .4rem .5rem; }
.brand-mark { flex: none; width: 28px; height: 28px; }
.brand-name { font-size: var(--t-xl); font-weight: 700; letter-spacing: -.03em;
              color: var(--brand-600); }

.org-switch {
  display: flex; align-items: center; gap: .55rem; width: 100%;
  padding: .6rem .7rem; background: var(--surface); cursor: pointer;
  border: 1px solid var(--line-2); border-radius: var(--r-md);
  font-weight: 600; font-size: var(--t-md); text-align: left;
}
.org-switch:hover { border-color: var(--accent); }
.org-switch .grow { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.org-switch svg { flex: none; width: 16px; height: 16px; color: var(--ink-3); }

.nav { display: grid; gap: 2px; }
.nav a, .nav button {
  display: flex; align-items: center; gap: .7rem; width: 100%;
  padding: .62rem .7rem; border-radius: var(--r-md); cursor: pointer;
  border: 0; background: none; color: var(--ink-2);
  font-size: var(--t-md); font-weight: 550; text-align: left;
  text-decoration: none;
  transition: background var(--speed) var(--ease), color var(--speed) var(--ease);
}
.nav a:hover, .nav button:hover { background: var(--surface-3); color: var(--ink);
                                   text-decoration: none; }
.nav a.is-active, .nav button.is-active {
  background: var(--accent-bg); color: var(--brand-700); font-weight: 650;
}
:root[data-theme="dark"] .nav a.is-active,
:root[data-theme="dark"] .nav button.is-active { color: var(--brand-500); }
.nav svg { flex: none; width: 19px; height: 19px; }
.nav .badge { margin-left: auto; font-size: var(--t-xs); font-weight: 700;
  background: var(--brand-600); color: var(--brand-ink); border-radius: var(--r-full);
  padding: .08rem .4rem; min-width: 1.2rem; text-align: center; }
.nav .badge.is-warn { background: var(--warn); color: #fff; }

.sidebar-foot { margin-top: auto; display: grid; gap: var(--sp-3);
                padding-top: var(--sp-3); }
.hub-state { display: flex; align-items: center; gap: .5rem; padding: .45rem .6rem;
  font-size: var(--t-base); font-weight: 600; color: var(--ok); }
.hub-state.is-down { color: var(--danger); }
.hub-state.is-stale { color: var(--warn); }
.user-card {
  display: flex; align-items: center; gap: .6rem; width: 100%;
  padding: .55rem .6rem; border-radius: var(--r-md); cursor: pointer;
  background: none; border: 1px solid transparent; text-align: left;
  border-top: 1px solid var(--line); border-radius: 0;
  padding-top: var(--sp-3);
}
.user-card:hover { background: var(--surface-3); border-radius: var(--r-md); }
.user-card .nm { font-weight: 650; font-size: var(--t-md); line-height: 1.25; }
.user-card .rl { font-size: var(--t-sm); color: var(--ink-3); }

/* ------------------------------------------------------------- topbar */
.topbar {
  grid-column: 2; position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: var(--sp-3);
  height: var(--topbar-h); padding: 0 var(--gutter);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: 1px solid var(--line);
}
.crumbs { display: flex; align-items: center; gap: .45rem; font-size: var(--t-md);
          color: var(--ink-2); min-width: 0; }
.crumbs a { color: var(--ink-2); }
.crumbs a:hover { color: var(--accent); }
.crumbs .sep { color: var(--ink-3); }
.crumbs .cur { color: var(--ink); font-weight: 600; }
.crumbs svg { width: 17px; height: 17px; }

.search { margin-left: auto; width: min(420px, 38vw); }
.search input { background: var(--surface-2); border-radius: var(--r-md); }
.topbar-actions { display: flex; align-items: center; gap: var(--sp-2); flex: none; }
.bell { position: relative; }
.bell .dot-badge { position: absolute; top: 6px; right: 6px; width: 8px; height: 8px;
  border-radius: 50%; background: var(--brand-500);
  border: 2px solid var(--surface); }

.menu-btn { display: none; }

/* ------------------------------------------------------------- main */
.main { grid-column: 2; min-width: 0; padding: var(--sp-6) var(--gutter) var(--sp-9); }
/* minmax(0,1fr): see the note on .stack in base.css — an implicit `auto` track
   is floored at min-content, so one wide descendant widened every row of the
   page and scrolled the whole document sideways on a phone. */
.page { max-width: var(--maxw); margin: 0 auto;
        display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-5); }
.page-head { display: flex; align-items: flex-start; justify-content: space-between;
             gap: var(--sp-4); flex-wrap: wrap; }
.page-title { display: grid; gap: .25rem; min-width: 0; }
.page-title .sub { color: var(--ink-2); font-size: var(--t-lg); }
.page-actions { display: flex; gap: var(--sp-2); flex-wrap: wrap; }

/* content + right rail */
.with-rail { display: grid; gap: var(--sp-4); grid-template-columns: minmax(0, 1fr); }
@media (min-width: 1180px) {
  .with-rail { grid-template-columns: minmax(0, 1fr) 340px; align-items: start; }
  .rail { position: sticky; top: calc(var(--topbar-h) + var(--sp-4)); }
}
.rail { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-4); }

.grid-kpi { display: grid; gap: var(--sp-3);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); }
.grid-cards { display: grid; gap: var(--sp-4);
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 330px), 1fr)); }
.grid-2 { display: grid; gap: var(--sp-4); grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) { .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.grid-tight { display: grid; gap: var(--sp-2);
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 230px), 1fr)); }

/* ------------------------------------------------------------- responsive */
.scrim { position: fixed; inset: 0; z-index: 39; background: rgb(6 14 11 / .45);
         backdrop-filter: blur(2px); }

@media (max-width: 1099px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; left: 0; top: 0; width: min(292px, 86vw);
    transform: translateX(-102%); transition: transform .24s var(--ease);
    box-shadow: var(--sh-lg);
  }
  .app.nav-open .sidebar { transform: none; }
  .topbar, .main { grid-column: 1; }
  .menu-btn { display: inline-flex; }
  .search { width: auto; flex: 1; min-width: 0; }
}

@media (max-width: 720px) {
  :root { --topbar-h: 56px; }
  .crumbs { display: none; }
  .search { order: 3; flex-basis: 100%; }
  .topbar { height: auto; padding-top: var(--sp-2); padding-bottom: var(--sp-2);
            flex-wrap: wrap; }
  .main { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
  .page-head { gap: var(--sp-3); }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1; }
  .card-pad { padding: var(--sp-4); }
  h1 { font-size: var(--t-3xl); }
}

/* bottom tab bar, phones only */
.tabbar { display: none; }
@media (max-width: 720px) {
  .tabbar {
    position: fixed; inset: auto 0 0 0; z-index: 38;
    /* minmax(0,1fr), not 1fr: a bare 1fr is minmax(auto,1fr) whose auto floor
       is min-content, so the five labels refused to shrink and pushed the bar
       to 506px inside a 390px screen -- which scrolled the whole page
       sideways. */
    display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr);
    background: color-mix(in srgb, var(--surface) 94%, transparent);
    backdrop-filter: blur(14px) saturate(160%);
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar button {
    display: grid; justify-items: center; gap: 2px; border: 0; background: none;
    padding: .5rem .2rem .45rem; cursor: pointer; color: var(--ink-3);
    font-size: var(--t-xs); font-weight: 600;
    min-width: 0;            /* grid items default to min-width:auto */
  }
  .tabbar button svg { width: 21px; height: 21px; }
  .tabbar button > span {
    max-width: 100%; overflow: hidden; text-overflow: ellipsis;
    white-space: nowrap;
  }
  .tabbar button.is-active { color: var(--accent); }
}

/* print: give the operator a clean paper record of a job */
@media print {
  .sidebar, .topbar, .tabbar, .toast-host, .page-actions, .btn { display: none !important; }
  .app { display: block; }
  .main { padding: 0; }
  .card { border-color: #ccc; box-shadow: none; break-inside: avoid; }
  body { background: #fff; }
}
