/* ==========================================================================
   FieldGrid UI v2 — App shell
   Topbar (fixed) + left nav (collapsible → icon rail → mobile drawer, with
   accordion groups) + content
   ========================================================================== */

.skip-link {
  position: absolute;
  top: var(--s-2);
  left: var(--s-2);
  z-index: var(--z-toast);
  padding: var(--s-2) var(--s-4);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-pop);
  font-weight: var(--fw-medium);
  transform: translateY(-200%);
  transition: transform var(--t-fast) var(--ease);
}
.skip-link:focus-visible { transform: translateY(0); }

/* ==========================================================================
   Topbar
   ========================================================================== */

.topbar {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: var(--z-topbar);
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: 0 var(--s-4);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.topbar-lead {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  width: calc(var(--nav-w) - var(--s-4));
  flex: none;
  transition: width var(--t-mid) var(--ease);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  min-width: 0;
  color: var(--ink);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-tight);
}
.brand:hover { color: var(--ink); }
.brand-mark {
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: var(--r-sm);
}
.brand-name { font-size: var(--fs-md); }

/* Nav collapse toggle — desktop */
.nav-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  flex: none;
  border-radius: var(--r-sm);
  color: var(--ink-muted);
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.nav-toggle:hover { background: var(--surface-hover); color: var(--ink); }

.topbar-mid {
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  flex: none;
}

/* ---- Omnibox (opens the command palette) -------------------------------- */
.omnibox {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  width: 100%;
  max-width: 460px;
  height: 34px;
  padding: 0 var(--s-2) 0 var(--s-3);
  background: var(--surface-sunk);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  color: var(--ink-muted);
  font-size: var(--fs-sm);
  text-align: left;
  transition: background var(--t-fast) var(--ease),
              border-color var(--t-fast) var(--ease);
}
.omnibox:hover { background: var(--surface-active); }
.omnibox:focus-visible {
  background: var(--surface);
  border-color: var(--accent-border);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.omnibox-label { flex: 1; min-width: 0; }

.kbd {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  padding: 2px var(--s-1);
  min-width: 20px;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  font-family: var(--font-ui);
  font-size: var(--fs-2xs);
  font-weight: var(--fw-medium);
  color: var(--ink-muted);
  line-height: 1.4;
}

/* ==========================================================================
   Left nav — collapsible to an icon rail, with accordion groups (pure-CSS
   grid-rows expand/collapse, no JS height math) and a sliding active pill.
   ========================================================================== */

/* When a sys-banner is live, offset nav and main by its height */
.has-sys-banner { --sys-banner-h: 36px; }

.nav {
  position: fixed;
  top: calc(var(--topbar-h) + var(--sys-banner-h, 0px));
  bottom: 0;
  left: 0;
  z-index: var(--z-nav);
  width: var(--nav-w);
  display: flex;
  flex-direction: column;
  background: var(--canvas);
  border-right: 1px solid var(--line);
  transition: width var(--t-mid) var(--ease),
              transform var(--t-mid) var(--ease);
}

.nav-scroll {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  padding: var(--s-3) var(--s-3) var(--s-4);
}

/* ---- Bottom "Collapse" bar — pinned outside the scroll area ---- */
.nav-foot {
  flex: none;
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3);
  border-top: 1px solid var(--line);
  background: none;
  color: var(--ink-muted);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  cursor: pointer;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.nav-foot:hover { background: var(--surface-hover); color: var(--ink); }
.nav-foot-chevron {
  flex: none;
  width: 16px;
  height: 16px;
  transition: transform var(--t-mid) var(--ease);
}
[data-nav="rail"] .nav-foot-chevron { transform: rotate(180deg); }
[data-nav="rail"] .nav-foot { justify-content: center; }
[data-nav="rail"] .nav-foot-label { display: none; }

/* ---- Signed-in user box — sits above the Collapse bar ------------------- */
.nav-user { flex: none; border-top: 1px solid var(--line); }
.nav-user-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--s-2);
  padding: var(--s-3);
  background: none;
  border: none;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  transition: background var(--t-fast) var(--ease);
}
.nav-user-trigger:hover,
.nav-user-trigger[aria-expanded="true"] { background: var(--surface-hover); }

.nav-user-info { flex: 1; min-width: 0; }
.nav-user-name {
  display: block;
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.nav-user-role {
  display: block;
  font-size: var(--fs-2xs);
  color: var(--ink-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.nav-user-caret {
  flex: none;
  width: 14px;
  height: 14px;
  color: var(--ink-faint);
  transition: transform var(--t-fast) var(--ease);
}
.nav-user-trigger[aria-expanded="true"] .nav-user-caret { transform: rotate(180deg); }

/* The dropdown opens upward (.menu-up) since this trigger sits at the
   very bottom of the viewport. Left/right anchoring is computed by
   ui.js at open time (from the trigger's real position, with a
   viewport-edge check), not by CSS. */

[data-nav="rail"] .nav-user-info,
[data-nav="rail"] .nav-user-caret { display: none; }
[data-nav="rail"] .nav-user-trigger { justify-content: center; padding-left: 0; padding-right: 0; }

.nav-link {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-2) var(--s-3);
  margin-left: 2px; /* room for is-active's border-left so it doesn't shift layout */
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  border-left: 2px solid transparent;
  color: var(--ink-soft);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  transition: background var(--t-fast) var(--ease),
              color var(--t-fast) var(--ease),
              border-color var(--t-fast) var(--ease);
}
.nav-link:hover { background: var(--surface-hover); color: var(--ink); }
.nav-link + .nav-link { margin-top: 1px; }

/* The highlight lives directly on the active link — no separately
   positioned element to keep in sync, so it can't drift out of
   alignment the way a JS-computed overlay could. */
.nav-link.is-active {
  background: var(--accent-subtle);
  border-left-color: var(--accent);
  color: var(--accent-text);
  font-weight: var(--fw-semibold);
}
.nav-link.is-active .nav-icon { color: var(--accent); }

.nav-icon {
  flex: none;
  width: 18px;
  height: 18px;
  color: var(--ink-muted);
  transition: color var(--t-fast) var(--ease);
}
.nav-link:hover .nav-icon { color: var(--ink-soft); }

.nav-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.nav-count {
  flex: none;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-full);
  background: var(--danger);
  color: #fff;
  font-size: var(--fs-2xs);
  font-weight: var(--fw-semibold);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* ---- Group headings — static labels, subtle color, always-visible links */
.nav-group {
  padding: var(--s-4) var(--s-3) var(--s-1);
  font-size: var(--fs-2xs);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--ink-faint);
}
.nav-group:first-child { padding-top: var(--s-1); }

/* ---- Rail (collapsed sidebar → icons only) ------------------------------- */
[data-nav="rail"] .nav { width: var(--nav-w-rail); }
[data-nav="rail"] .nav-scroll { padding-left: var(--s-2); padding-right: var(--s-2); }
/* The topbar brand (logo + "FieldGrid") stays put regardless of rail
   state — it's no longer sharing this box with a collapse toggle, so
   there's nothing forcing it to shrink or hide in sync with the sidebar. */

[data-nav="rail"] .nav-label,
[data-nav="rail"] .nav-group { display: none; }
[data-nav="rail"] .nav-link {
  justify-content: center;
  padding-left: 0;
  padding-right: 0;
  margin-left: 0;
  border-left-width: 0;
}
[data-nav="rail"] .nav-link.is-active { border-radius: var(--r-sm); } /* no left edge to round off in rail */
[data-nav="rail"] .nav-count {
  position: absolute;
  top: 2px;
  right: 2px;
  min-width: 15px;
  height: 15px;
  padding: 0 3px;
  font-size: 9px;
}

[data-nav="rail"] .main { margin-left: var(--nav-w-rail); }

/* Rail tooltips */
[data-nav="rail"] .nav-link::after {
  content: attr(data-label);
  position: absolute;
  left: calc(100% + var(--s-2));
  top: 50%;
  transform: translateY(-50%);
  z-index: var(--z-menu);
  padding: var(--s-1) var(--s-2);
  background: var(--ink);
  color: #fff;
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  white-space: nowrap;
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-pop);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t-fast) var(--ease);
}
[data-nav="rail"] .nav-link:hover::after { opacity: 1; }

/* ---- Scrim (mobile drawer) ---------------------------------------------- */
.nav-scrim {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-nav) - 1);
  background: rgba(24, 24, 27, 0.32);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--t-mid) var(--ease), visibility var(--t-mid);
}

/* ==========================================================================
   Content region
   ========================================================================== */

.main {
  margin-left: var(--nav-w);
  padding-top: calc(var(--topbar-h) + var(--sys-banner-h, 0px));
  min-height: 100vh;
  transition: margin-left var(--t-mid) var(--ease);
}

.page {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--s-5) var(--gutter) var(--s-8);
}
.page-wide { max-width: none; }

/* ---- Page header -------------------------------------------------------- */
.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--s-4);
  flex-wrap: wrap;
  margin-bottom: var(--s-5);
}

.page-title {
  font-size: var(--fs-2xl);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-tight);
  line-height: var(--lh-tight);
}
.page-sub {
  margin-top: var(--s-1);
  font-size: var(--fs-sm);
  color: var(--ink-muted);
}
.page-actions {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  flex-wrap: wrap;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  margin-bottom: var(--s-2);
  font-size: var(--fs-xs);
  color: var(--ink-muted);
}
.breadcrumb a { color: var(--ink-muted); }
.breadcrumb a:hover { color: var(--ink); }
.breadcrumb-sep { color: var(--ink-faint); }

/* ---- Section ------------------------------------------------------------ */
.section + .section { margin-top: var(--s-6); }
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-4);
  margin-bottom: var(--s-3);
}
.section-title {
  font-size: var(--fs-lg);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-tight);
}
.section-sub {
  font-size: var(--fs-sm);
  color: var(--ink-muted);
  margin-top: 2px;
}

/* ---- Grid helpers ------------------------------------------------------- */
.grid { display: grid; gap: var(--s-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-auto { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

/* Detail layout: main column + sticky summary rail */
.split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: var(--s-5);
  align-items: start;
}
.split-rail { position: sticky; top: calc(var(--topbar-h) + var(--sys-banner-h, 0px) + var(--s-5)); }

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 1180px) {
  .split { grid-template-columns: minmax(0, 1fr) 300px; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 960px) {
  .split { grid-template-columns: minmax(0, 1fr); }
  .split-rail { position: static; }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Below this the nav becomes an off-canvas drawer */
@media (max-width: 900px) {
  :root { --gutter: var(--s-4); }

  .nav {
    transform: translateX(-100%);
    box-shadow: var(--shadow-overlay);
    background: var(--surface);
    width: var(--nav-w);
  }
  [data-nav-open] .nav { transform: translateX(0); }
  [data-nav-open] .nav-scrim { opacity: 1; visibility: visible; }

  /* Rail mode is desktop-only — the drawer always shows full labels */
  [data-nav="rail"] .nav { width: var(--nav-w); }
  [data-nav="rail"] .nav-scroll { padding-left: var(--s-3); padding-right: var(--s-3); }
  [data-nav="rail"] .nav-label,
  [data-nav="rail"] .nav-group { display: block; }
  [data-nav="rail"] .nav-link {
    justify-content: flex-start;
    padding-left: var(--s-3);
    padding-right: var(--s-3);
    margin-left: 2px;
    border-left-width: 2px;
  }
  [data-nav="rail"] .nav-link.is-active { border-radius: 0 var(--r-sm) var(--r-sm) 0; }
  [data-nav="rail"] .nav-link::after { content: none; }
  [data-nav="rail"] .nav-count { position: static; min-width: 18px; height: 18px; padding: 0 5px; font-size: var(--fs-2xs); }

  .main,
  [data-nav="rail"] .main { margin-left: 0; }

  .topbar-lead { width: auto; }
  .nav-toggle { display: none; }
  .nav-drawer-toggle { display: flex; }

  .page { padding-top: var(--s-4); }
  .page-head { margin-bottom: var(--s-4); }
}

@media (min-width: 901px) {
  .nav-drawer-toggle { display: none; }
}

@media (max-width: 720px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: minmax(0, 1fr); }
  .page-title { font-size: var(--fs-xl); }
  .topbar { padding: 0 var(--s-3); gap: var(--s-2); }
  .topbar-mid { display: none; }
  .omnibox-compact { display: flex; }
}

@media (min-width: 721px) {
  .omnibox-compact { display: none; }
}

/* ==========================================================================
   Embedded mode — customer portal pages render inside an iframe
   ========================================================================== */
[data-embed] .topbar,
[data-embed] .nav,
[data-embed] .nav-scrim { display: none; }
[data-embed] .main { margin-left: 0; padding-top: 0; }
