/* `hidden` MUST WIN. Production spill: `.shell{display:flex}` overrode the [hidden] attribute, so the app shell painted
   under the sign-in card. Any class that sets display would do the same — this rule is first and !important on purpose. */
[hidden]{ display: none !important; }

/* BrainTrust console — THE SHELL: boot state, sign-in, sidebar, top bar, page header, menus, error bar, phone layout.
   Tokens from css/tokens.css; components from css/console.css. */
/* `clip`, not `hidden`: overflow-x:hidden on body makes it a scroll container and breaks the sticky sidebar/top bar. */
html, body{ max-width: 100%; overflow-x: clip; }
body{ min-height: 100vh; min-height: 100dvh; }

/* ── boot: a neutral state until auth.js decides (no flash of either the shell or the sign-in card) ───────── */
.boot{ position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--sp-5); background: var(--bg); z-index: 200; }
.boot .brandmark{ opacity: .9; }
.boot-bar{ width: 120px; height: 3px; border-radius: var(--r-full); background: var(--surface-2); overflow: hidden; position: relative; }
.boot-bar::after{ content: ""; position: absolute; inset: 0; width: 40%; background: var(--accent); border-radius: inherit; animation: bootbar 1.1s var(--ease) infinite; }
@keyframes bootbar{ from{ transform: translateX(-100%); } to{ transform: translateX(250%); } }

/* ── sign-in ─────────────────────────────────────────────────────────────── */
.signin{ min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: var(--sp-10) var(--sp-4) var(--sp-6); position: relative; isolation: isolate;
  background: radial-gradient(1200px 600px at 50% -10%, var(--accent-soft), transparent 60%), var(--bg); }
.signin::before{ content: ""; position: absolute; inset: 0; z-index: -1; pointer-events: none; opacity: .5;
  background-image: linear-gradient(var(--border) 1px, transparent 1px), linear-gradient(90deg, var(--border) 1px, transparent 1px);
  background-size: 40px 40px; -webkit-mask-image: radial-gradient(ellipse 60% 50% at 50% 30%, #000 30%, transparent 75%); mask-image: radial-gradient(ellipse 60% 50% at 50% 30%, #000 30%, transparent 75%); }
.signin-main{ width: 100%; max-width: 400px; flex: 1; display: flex; flex-direction: column; justify-content: center; }
.signin-brand{ display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--sp-3); margin-bottom: var(--sp-6); }
.signin-brand h1{ font-size: var(--fs-xl); font-weight: var(--fw-semibold); letter-spacing: -.01em; }
.signin-brand p{ margin: 0; color: var(--text-muted); font-size: var(--fs-sm); max-width: 34ch; }
.signin-card{ background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-xl); padding: var(--sp-6); box-shadow: var(--shadow-lg); }
.signin-card h2{ font-size: var(--fs-lg); font-weight: var(--fw-semibold); margin-bottom: var(--sp-1); }
.signin-card .sub{ color: var(--text-muted); margin: 0 0 var(--sp-5); font-size: var(--fs-sm); }
.signin-card input[type="email"], .signin-card input[type="password"]{ width: 100%; height: 40px; font-size: var(--fs-md); }
.signin-card .field{ margin-bottom: var(--sp-4); }
/* the security check is a real field of the form: labelled, framed like an input, with a live hint under it */
.ts-field{ margin-bottom: var(--sp-5); }
.ts-slot{ min-height: 65px; display: flex; align-items: center; justify-content: center; border-radius: var(--r-md); overflow: hidden; }
.ts-slot > div{ width: 100%; }
.ts-slot iframe{ max-width: 100%; }
.ts-slot.loading{ color: var(--text-muted); font-size: var(--fs-xs); gap: var(--sp-2); border: 1px dashed var(--border-strong); background: var(--surface-sunken); }
.ts-slot .banner{ margin: 0; width: 100%; }
.ts-field .field-hint{ display: flex; align-items: center; gap: 6px; min-height: 18px; }
.signin-sent .sent-ttl{ display: inline-flex; align-items: center; gap: 6px; font-size: var(--fs-sm); color: var(--text-2); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-full); padding: 4px 12px; margin-bottom: var(--sp-5); }
.signin-msg{ margin-top: var(--sp-4); }
.signin-msg:empty{ display: none; }
.signin-msg .banner{ margin: 0; }
.signin-sent{ text-align: center; }
.signin-sent .sent-ico{ width: 48px; height: 48px; border-radius: 50%; background: var(--success-soft); color: var(--success); display: inline-flex; align-items: center; justify-content: center; margin-bottom: var(--sp-4); }
.signin-sent h2{ margin-bottom: var(--sp-2); }
.signin-sent p{ color: var(--text-muted); }
.signin-sent .addr{ color: var(--text); font-weight: var(--fw-medium); overflow-wrap: anywhere; }
.signin-dev{ margin-top: var(--sp-4); font-size: var(--fs-xs); color: var(--text-muted); text-align: center; }
.signin-foot{ margin-top: var(--sp-8); display: flex; gap: var(--sp-3); align-items: center; justify-content: center; color: var(--text-muted); font-size: var(--fs-xs); flex-wrap: wrap; }
.signin-foot a{ color: var(--text-2); }
.signin-foot .sep{ width: 3px; height: 3px; border-radius: 50%; background: var(--border-strong); }
.signin-theme{ position: absolute; top: var(--sp-4); right: var(--sp-4); }

/* ── app shell ─────────────────────────────────────────────────────────────── */
.shell{ display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: 100vh; min-height: 100dvh; transition: grid-template-columns var(--dur) var(--ease); }
.shell.collapsed{ grid-template-columns: var(--sidebar-w-collapsed) minmax(0, 1fr); }

/* sidebar */
.side{ position: sticky; top: 0; height: 100vh; height: 100dvh; display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--border); min-width: 0; z-index: 40; }
.side-head{ display: flex; align-items: center; gap: var(--sp-3); height: var(--topbar-h); padding: 0 var(--sp-4); border-bottom: 1px solid var(--border); flex: none; }
.side-brand{ display: flex; align-items: center; gap: 10px; min-width: 0; color: var(--text); text-decoration: none !important; border-radius: var(--r-md); }
.side-brand .names{ min-width: 0; line-height: 1.2; }
.side-brand b{ display: block; font-size: var(--fs-md); font-weight: var(--fw-semibold); letter-spacing: -.01em; }
.side-brand span{ display: block; font-size: var(--fs-2xs); color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-nav{ flex: 1; overflow-y: auto; overflow-x: hidden; padding: var(--sp-3) var(--sp-3) var(--sp-4); }
.nav-group + .nav-group{ margin-top: var(--sp-4); }
.nav-label{ font-size: var(--fs-2xs); font-weight: var(--fw-semibold); color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; padding: 0 var(--sp-2) 6px; white-space: nowrap; }
.nav-item{ display: flex; align-items: center; gap: 10px; height: 34px; padding: 0 var(--sp-2); border-radius: var(--r-md); color: var(--text-2); text-decoration: none !important; font-size: var(--fs-sm); font-weight: var(--fw-medium); white-space: nowrap; position: relative; }
.nav-item .ico{ color: var(--text-muted); width: 18px; height: 18px; }
.nav-item:hover{ background: var(--surface-2); color: var(--text); }
.nav-item:hover .ico{ color: var(--text-2); }
.nav-item[aria-current="page"]{ background: var(--accent-soft); color: var(--accent-text); }
.nav-item[aria-current="page"] .ico{ color: var(--accent-text); }
.nav-item .lbl{ overflow: hidden; text-overflow: ellipsis; }
.side-foot{ flex: none; border-top: 1px solid var(--border); padding: var(--sp-2) var(--sp-3); }
.side-foot .btn{ width: 100%; justify-content: flex-start; gap: 10px; padding: 0 var(--sp-2); color: var(--text-muted); }
.side-foot .btn .ico{ width: 18px; height: 18px; transition: transform var(--dur) var(--ease); }
/* collapsed: icons only, labels become tooltips */
.shell.collapsed .side-head{ padding: 0; justify-content: center; }
.shell.collapsed .side-brand .names, .shell.collapsed .nav-item .lbl, .shell.collapsed .side-foot .lbl{ display: none; }
.shell.collapsed .nav-label{ height: 1px; padding: 0; margin: 0 var(--sp-2) var(--sp-2); background: var(--border); color: transparent; overflow: hidden; }
.shell.collapsed .nav-group + .nav-group{ margin-top: var(--sp-2); }
.shell.collapsed .nav-item, .shell.collapsed .side-foot .btn{ justify-content: center; padding: 0; }
.shell.collapsed .side-foot .btn .ico{ transform: rotate(180deg); }
.shell.collapsed .side-nav{ overflow: visible; }
.shell.collapsed .nav-item:hover::after, .shell.collapsed .nav-item:focus-visible::after{ content: attr(data-label); position: absolute; left: calc(100% + 10px); top: 50%; transform: translateY(-50%);
  background: var(--surface-inverse); color: var(--text-inverse); font-size: var(--fs-xs); padding: 4px 8px; border-radius: var(--r-sm); white-space: nowrap; box-shadow: var(--shadow-md); pointer-events: none; z-index: 60; }

/* main column */
.main-col{ min-width: 0; display: flex; flex-direction: column; }
.topbar{ position: sticky; top: 0; z-index: 30; height: var(--topbar-h); display: flex; align-items: center; gap: var(--sp-3); padding: 0 var(--sp-6);
  background: color-mix(in srgb, var(--surface) 88%, transparent); -webkit-backdrop-filter: saturate(1.4) blur(10px); backdrop-filter: saturate(1.4) blur(10px); border-bottom: 1px solid var(--border); }
.topbar .menu-btn{ display: none; }
.crumbs{ display: flex; align-items: center; gap: 6px; min-width: 0; font-size: var(--fs-sm); color: var(--text-muted); }
.crumbs ol{ display: flex; align-items: center; gap: 6px; list-style: none; margin: 0; padding: 0; min-width: 0; }
.crumbs li{ display: flex; align-items: center; gap: 6px; white-space: nowrap; min-width: 0; }
.crumbs li + li::before{ content: "/"; color: var(--border-strong); }
.crumbs li:last-child{ color: var(--text); font-weight: var(--fw-medium); overflow: hidden; text-overflow: ellipsis; }
.topbar-actions{ margin-left: auto; display: flex; align-items: center; gap: var(--sp-2); flex: none; }
.env-badge{ text-transform: capitalize; }
/* tenant switcher */
.tenant{ position: relative; display: flex; align-items: center; }
.tenant .ico{ position: absolute; left: 9px; color: var(--text-muted); pointer-events: none; }
.tenant select{ height: 32px; padding-left: 32px; max-width: 220px; font-weight: var(--fw-medium); }
.tenant-key{ display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 var(--sp-3); border: 1px solid var(--border); border-radius: var(--r-md); font-size: var(--fs-xs); color: var(--text-2); background: var(--surface-2); white-space: nowrap; }
/* account menu */
.acct{ position: relative; }
.acct-btn{ display: inline-flex; align-items: center; gap: var(--sp-2); height: 32px; padding: 0 6px 0 4px; border-radius: var(--r-full); border: 1px solid var(--border); background: var(--surface); color: var(--text-2); cursor: pointer; font: inherit; }
.acct-btn:hover{ background: var(--surface-2); }
.avatar{ width: 24px; height: 24px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-text); display: inline-flex; align-items: center; justify-content: center; font-size: var(--fs-2xs); font-weight: var(--fw-semibold); text-transform: uppercase; flex: none; }
.menu{ position: absolute; right: 0; top: calc(100% + 6px); min-width: 260px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); box-shadow: var(--shadow-lg); padding: var(--sp-1); z-index: 70; }
.menu-head{ padding: var(--sp-3); border-bottom: 1px solid var(--border); margin: calc(var(--sp-1) * -1) calc(var(--sp-1) * -1) var(--sp-1); }
.menu-head .who{ font-weight: var(--fw-medium); color: var(--text); overflow-wrap: anywhere; }
.menu-head .role{ font-size: var(--fs-xs); color: var(--text-muted); margin-top: 2px; }
.menu-section{ padding: var(--sp-2) var(--sp-3); display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); font-size: var(--fs-xs); color: var(--text-muted); }
.menu-item{ display: flex; align-items: center; gap: 10px; width: 100%; height: 34px; padding: 0 var(--sp-3); border: 0; background: none; border-radius: var(--r-md); color: var(--text-2); font: inherit; font-size: var(--fs-sm); cursor: pointer; text-align: left; text-decoration: none !important; }
.menu-item:hover, .menu-item:focus-visible{ background: var(--surface-2); color: var(--text); }
.menu-item .ico{ color: var(--text-muted); }
.menu-sep{ height: 1px; background: var(--border); margin: var(--sp-1) 0; }

/* error bar — an API failure, under the top bar */
#errbar{ display: none; margin: var(--sp-4) var(--sp-6) 0; }
#errbar.show{ display: flex; }
#errbar .banner{ width: 100%; margin: 0; }

/* page */
.page{ width: 100%; max-width: var(--content-max); margin: 0 auto; padding: var(--sp-6) var(--sp-6) var(--sp-12); min-width: 0; }
.page:focus{ outline: none; }
.page-head{ display: flex; align-items: flex-start; justify-content: space-between; gap: var(--sp-4); flex-wrap: wrap; margin-bottom: var(--sp-6); }
.page-head h1{ font-size: var(--fs-2xl); font-weight: var(--fw-semibold); letter-spacing: -.02em; }
.page-head .desc{ color: var(--text-muted); margin: 6px 0 0; max-width: 72ch; font-size: var(--fs-sm); }
.page-actions{ display: flex; gap: var(--sp-2); flex-wrap: wrap; align-items: center; }
.page-actions:empty{ display: none; }
#view{ min-width: 0; }

/* scrim for the mobile drawer */
.scrim{ position: fixed; inset: 0; background: var(--scrim); z-index: 45; }

/* ── screen layouts that belong to the shell's screens ──────────────────────── */
.cfg-row{ display: grid; grid-template-columns: minmax(0, 320px) minmax(0, 1fr); gap: var(--sp-3); align-items: center; padding: var(--sp-2) 0; border-bottom: 1px solid var(--border); }
.cfg-row:last-child{ border-bottom: 0; }
.cfg-row .k{ font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--text-2); overflow-wrap: anywhere; }
.cfg-row input{ width: 100%; }
.cfg-row.dirty .k{ color: var(--accent-text); }
.cfg-row.dirty .k::after{ content: " · edited"; font-family: var(--font-sans); color: var(--text-muted); }
.agg-grid{ display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: var(--sp-4); margin-bottom: var(--sp-4); }
.agg-grid > .card{ margin-bottom: 0; }
.agg-card{ display: flex; flex-direction: column; }
.agg-card .row{ display: flex; gap: var(--sp-2); align-items: center; flex-wrap: wrap; }
.agg-card .fields{ display: grid; grid-template-columns: minmax(0, 130px) minmax(0, 1fr); gap: var(--sp-2) var(--sp-3); align-items: center; margin-top: var(--sp-3); }
.agg-card .fields input, .agg-card .fields select{ width: 100%; min-width: 0; }
.agg-card .fields label{ font-size: var(--fs-xs); color: var(--text-muted); }
iframe.gw{ display: block; width: 100%; min-height: 600px; border: 0; background: transparent; color-scheme: normal; }

/* ── responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 900px){
  .shell, .shell.collapsed{ display: block; }
  .side{ position: fixed; left: 0; top: 0; bottom: 0; width: min(84vw, 300px); transform: translateX(-100%); transition: transform var(--dur) var(--ease); box-shadow: var(--shadow-lg); z-index: 50; }
  .shell.drawer-open .side{ transform: none; }
  .shell.collapsed .side-brand .names, .shell.collapsed .nav-item .lbl{ display: block; }
  .shell.collapsed .nav-label{ height: auto; padding: 0 var(--sp-2) 6px; margin: 0; background: none; color: var(--text-muted); }
  .shell.collapsed .nav-item{ justify-content: flex-start; padding: 0 var(--sp-2); }
  .shell.collapsed .nav-item:hover::after{ display: none; }
  .shell.collapsed .side-head{ padding: 0 var(--sp-4); justify-content: flex-start; }
  .side-foot{ display: none; }
  .topbar{ padding: 0 var(--sp-4); gap: var(--sp-2); }
  .topbar .menu-btn{ display: inline-flex; }
  .crumbs li:not(:last-child){ display: none; }
  .crumbs li:last-child::before{ display: none; }
  .crumbs, .crumbs ol{ flex: 1 1 auto; }
  .crumbs li:last-child{ display: block; }
  .page{ padding: var(--sp-5) var(--sp-4) var(--sp-10); }
  .page-head{ margin-bottom: var(--sp-5); }
  .page-head h1{ font-size: var(--fs-xl); }
  #errbar{ margin: var(--sp-3) var(--sp-4) 0; }
  .cfg-row{ grid-template-columns: 1fr; gap: 6px; }
  .agg-card .fields{ grid-template-columns: 1fr; }
  .card{ padding: var(--sp-4); }
}
@media (max-width: 560px){
  .env-badge, .acct-btn .chev{ display: none; }
  .tenant select{ max-width: 132px; }
  .topbar-actions{ gap: 6px; }
  .page-actions{ width: 100%; }
  .tiles{ grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-3); }
  .tile-value, .stat{ font-size: var(--fs-xl); }
}
