/* =========================================================================
   Reset, typography and the application shell.
   Only semantic tokens are referenced here -- never a primitive.
   ========================================================================= */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.55;
  font-feature-settings: var(--numeric);
  color: var(--text);
  background: var(--canvas);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* The theme swap itself is animated, so switching does not feel like a
   page reload. Only colours transition; layout must never. */
body, .sidebar, .card, .topbar, .table-wrap {
  transition: background-color var(--dur) var(--ease),
              border-color var(--dur) var(--ease),
              color var(--dur) var(--ease);
}

h1, h2, h3, h4 { margin: 0; font-weight: 620; letter-spacing: -0.011em; line-height: 1.25; }
h1 { font-size: var(--text-lg); }
h2 { font-size: var(--text-md); margin-top: var(--sp-6); margin-bottom: var(--sp-3); }
h3 { font-size: var(--text-base); }
p { margin: 0 0 var(--sp-3); }
p:last-child { margin-bottom: 0; }

a {
  color: var(--brand);
  text-decoration: none;
  text-underline-offset: 2px;
  transition: color var(--dur-fast) var(--ease);
}
a:hover { color: var(--brand-hover); text-decoration: underline; }

code, kbd {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--surface-sunken);
  padding: 0.1em 0.35em;
  border-radius: var(--radius-sm);
}
/* Font only, no chip. A masked document number fills a whole table cell, and
   a background on it reads as a highlighted row rather than as monospace. */
.mono { font-family: var(--font-mono); font-size: 0.92em; letter-spacing: 0.02em; }

/* A single visible focus treatment for keyboard users, everywhere. */
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

hr { border: 0; border-top: 1px solid var(--border); margin: var(--sp-5) 0; }

.muted     { color: var(--text-muted); }
.subtle    { color: var(--text-subtle); }
.right     { text-align: right; }
.center    { text-align: center; }
.nowrap    { white-space: nowrap; }
.num       { font-variant-numeric: tabular-nums; }
.spacer    { margin-left: auto; }
.stack > * + * { margin-top: var(--sp-3); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}

/* Visible only when focused. Every page starts with a dozen sidebar links,
   and a keyboard user should not have to walk them to reach the table. */
.skip-link {
  position: fixed;
  top: var(--sp-2); left: var(--sp-2);
  z-index: 100;
  padding: var(--sp-2) var(--sp-4);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  transform: translateY(-200%);
  transition: transform var(--dur) var(--ease);
}
.skip-link:focus { transform: none; text-decoration: none; }

/* =========================================================================
   Shell: fixed sidebar, sticky topbar, scrolling content.
   ========================================================================= */

.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100vh;
  min-height: 100dvh;   /* mobile browsers, where 100vh includes the chrome */
}
/* minmax(0,…) on the column is not enough on its own: a wide table inside a
   grid item still resolves to its content width unless the item itself is
   allowed to be narrower than its contents. */
.col { min-width: 0; }

/* ---- sidebar ---- */
.sidebar {
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  padding: var(--sp-4) var(--sp-3);
  overflow-y: auto;
  overscroll-behavior: contain;
  z-index: 40;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-2) var(--sp-5);
  color: var(--sidebar-active);
  text-decoration: none;
  min-width: 0;
}
.brand:hover { text-decoration: none; color: var(--sidebar-active); }

/* The emblem is multicoloured artwork on a transparent field, and its purple
   and blue sit close in value to the indigo sidebar. A light tile is what
   keeps it legible without recolouring somebody's registered mark. */
.brand-mark {
  flex: none;
  width: 2.15rem;
  height: 2.6rem;
  border-radius: var(--radius-sm);
  background: var(--white);
  display: grid;
  place-items: center;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
}
.brand-mark img { display: block; height: 1.95rem; width: auto; }

.brand-text { min-width: 0; }
.brand-name {
  display: block;
  font-weight: 660;
  font-size: var(--text-md);
  letter-spacing: -0.012em;
  line-height: 1.15;
  white-space: nowrap;
}
.brand-sub {
  display: block;
  font-size: var(--text-xs);
  color: var(--sidebar-text);
  font-weight: 450;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-group + .nav-group { margin-top: var(--sp-5); }
.nav-label {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-weight: 600;
  color: var(--sidebar-text);
  opacity: 0.65;
  padding: 0 var(--sp-2) var(--sp-2);
}
.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0.5rem var(--sp-2);
  margin-bottom: 1px;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  font-size: var(--text-sm);
  font-weight: 500;
  position: relative;
  transition: background-color var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease);
}
.nav-item:hover { background: var(--sidebar-hover); color: var(--sidebar-active); text-decoration: none; }
.nav-item.is-active { background: var(--sidebar-hover); color: var(--sidebar-active); font-weight: 580; }
/* The active marker slides between items rather than blinking. */
.nav-item.is-active::before {
  content: "";
  position: absolute;
  left: calc(var(--sp-2) * -1 - 1px);
  top: 20%; bottom: 20%;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--accent);
}
.nav-item .icon { flex: none; width: 1.05rem; height: 1.05rem; opacity: 0.9; }
.nav-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .count {
  margin-left: auto;
  flex: none;
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  background: rgb(255 255 255 / 0.12);
  color: var(--sidebar-active);
  padding: 0.05rem 0.4rem;
  border-radius: var(--radius-pill);
}
/* Not white: --danger lightens on the dark themes, where white on it is
   2.6:1. --text-on-danger is whichever of light or dark actually reads. */
.nav-item .count.is-alert { background: var(--danger); color: var(--text-on-danger); }

.sidebar-foot { margin-top: auto; padding-top: var(--sp-5); }

/* The drawer scrim. Only ever visible on small screens with JS. */
.nav-scrim { display: none; }

/* ---- topbar ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--gutter);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  font-size: var(--text-md);
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar .crumb { color: var(--text-subtle); font-size: var(--text-sm); }

.nav-toggle { display: none; margin-left: calc(var(--sp-2) * -1); }

main {
  padding: var(--gutter);
  max-width: var(--content-max);
  margin: 0 auto;
  width: 100%;
}

.page-head { margin-bottom: var(--sp-5); }
.page-head .sub { color: var(--text-muted); font-size: var(--text-sm); margin: var(--sp-1) 0 0; }
.page-head-row { display: flex; align-items: flex-start; gap: var(--sp-4); flex-wrap: wrap; }
.page-head-row > .actions { margin-top: 0; }

/* =========================================================================
   Responsive.

   Four states, because "desktop and mobile" is not what this application is
   used on. An advisor has a 27" iMac on the desk, a 13" laptop in a client's
   office, and a phone in the car park.

     >= 1440px   more sidebar, wider content, larger gutter
     1024-1440   the default
      780-1024   sidebar collapses to an icon rail -- tablets and split
                 windows, where 15.5rem of chrome is a quarter of the screen
      <  780px   sidebar becomes an off-canvas drawer

   The drawer is the one part that needs JavaScript, so it is gated on
   [data-js], which an inline script in the head sets before first paint.
   Without it the sidebar stays a horizontal scrolling strip: less pretty,
   but every link is still reachable, which is the part that matters.
   ========================================================================= */

@media (min-width: 1440px) {
  :root { --sidebar-w: 16.5rem; --content-max: 88rem; --gutter: var(--sp-6); }
}

/* Very wide monitors: stop the content column growing without limit, but let
   it use more of the screen than an 82rem column does at 2560px. */
@media (min-width: 1920px) {
  :root { --content-max: 104rem; }
}

/* ---- icon rail ---- */
@media (max-width: 1024px) and (min-width: 780px) {
  .shell { grid-template-columns: var(--rail-w) minmax(0, 1fr); }
  .sidebar { padding: var(--sp-4) var(--sp-2); align-items: center; }
  .brand { padding: var(--sp-1) 0 var(--sp-5); justify-content: center; }
  .brand-text, .nav-label, .sidebar-foot { display: none; }
  .nav-group { width: 100%; }
  .nav-item { justify-content: center; padding: 0.55rem 0; }
  .nav-text { display: none; }
  .nav-item .icon { width: 1.2rem; height: 1.2rem; }
  .nav-item.is-active::before { left: -0.5rem; }
  /* The badge cannot sit beside a hidden label, so it becomes a dot on the
     icon. Losing the number is fine; losing "there is something here" is not. */
  .nav-item .count {
    position: absolute;
    top: 0.3rem; right: 0.75rem;
    margin: 0;
    min-width: 0.45rem; height: 0.45rem;
    padding: 0;
    font-size: 0;
    line-height: 0;
    background: var(--accent);
  }
  .nav-item .count.is-alert { background: var(--danger); }
}

/* ---- drawer ---- */
@media (max-width: 779px) {
  .shell { grid-template-columns: minmax(0, 1fr); }

  /* No-JS fallback: a horizontal strip that scrolls. */
  .sidebar {
    position: static;
    height: auto;
    flex-direction: row;
    align-items: center;
    gap: var(--sp-2);
    overflow-x: auto;
    padding: var(--sp-2) var(--sp-3);
    border-right: 0;
    border-bottom: 1px solid var(--sidebar-border);
  }
  .brand { padding: 0 var(--sp-3) 0 0; }
  .brand-sub, .nav-label, .sidebar-foot { display: none; }
  .nav-group { display: flex; gap: var(--sp-1); }
  .nav-group + .nav-group { margin-top: 0; }
  .nav-item { white-space: nowrap; }
  .nav-item.is-active::before {
    inset: auto 15% 0 15%; width: auto; height: 2px; border-radius: 2px 2px 0 0;
  }

  main { padding: var(--sp-4) var(--sp-3); }
  .topbar { padding: 0 var(--sp-3); }

  /* With JavaScript, upgrade the strip to a real drawer. */
  [data-js] .nav-toggle { display: inline-flex; }
  [data-js] .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(17rem, 82vw);
    height: 100dvh;
    flex-direction: column;
    align-items: stretch;
    overflow-y: auto;
    padding: var(--sp-4) var(--sp-3);
    border-right: 1px solid var(--sidebar-border);
    border-bottom: 0;
    transform: translateX(-100%);
    transition: transform var(--dur-slow) var(--ease);
    box-shadow: var(--shadow-lg);
  }
  [data-js] .brand { padding: var(--sp-2) var(--sp-2) var(--sp-5); }
  [data-js] .brand-sub { display: block; }
  [data-js] .nav-label { display: block; }
  [data-js] .nav-group { display: block; }
  [data-js] .nav-group + .nav-group { margin-top: var(--sp-5); }
  [data-js] .nav-item.is-active::before {
    inset: 20% auto 20% calc(var(--sp-2) * -1 - 1px);
    width: 3px; height: auto; border-radius: 0 3px 3px 0;
  }
  [data-js][data-nav="open"] .sidebar { transform: none; }

  [data-js][data-nav="open"] .nav-scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 35;
    border: 0;
    padding: 0;
    background: var(--overlay);
    animation: fade var(--dur) var(--ease);
  }
  /* The drawer is a modal surface: the page behind it must not scroll. */
  [data-js][data-nav="open"] body { overflow: hidden; }
}

/* Phones. Below this the topbar cannot hold a name as well as a heading. */
@media (max-width: 560px) {
  :root { --gutter: var(--sp-3); }
  .topbar { gap: var(--sp-2); }
  .account-trigger .who { display: none; }
  .account-trigger .chev { display: none; }
}
