/* The app shell: a dark chrome bar across the top, with a navigation rail and a context rail
   hanging off it.

   The nav rail is open by default and the context rail closed - accounting work is navigation-heavy
   (twelve sections, ninety-odd screens), and a user who cannot see where they are spends their time
   guessing. Both rails collapse to a zero-width column so the content becomes full-bleed, which is
   what the wider grids need.

   Rail visibility is driven by attributes on <html> (data-sidebar / data-context) that App.razor
   renders server-side from cookies before the first byte. That ordering is what keeps a rail from
   flashing open on load; js/shell.js only flips the live page. */

.app-shell {
  display: grid;
  grid-template-columns: 244px minmax(0, 1fr) 0;
  grid-template-rows: 48px 1fr;
  grid-template-areas:
    "topbar topbar topbar"
    "sidebar main context";
  min-height: 100vh;
  transition: grid-template-columns 0.16s ease;
}

:root:not([data-sidebar="expanded"]) .app-shell {
  grid-template-columns: 0 minmax(0, 1fr) 0;
}

:root[data-context="open"] .app-shell {
  grid-template-columns: 244px minmax(0, 1fr) 330px;
}

:root:not([data-sidebar="expanded"])[data-context="open"] .app-shell {
  grid-template-columns: 0 minmax(0, 1fr) 330px;
}

@media (prefers-reduced-motion: reduce) {
  .app-shell {
    transition: none;
  }
}

/* ---- Chrome bar ----------------------------------------------------------------------------- */
/* Fixed dark in both themes - see the :root block in tokens.light.css. */

.app-topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px 0 6px;
  background-color: var(--color-chrome-bg-raised);
  color: var(--color-chrome-fg);
  position: relative;
  z-index: 50;

  & .app-topbar-lead,
  & .app-topbar-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    min-width: 0;
  }

  & .app-topbar-actions {
    margin-left: auto;
    gap: 2px;
  }
}

.app-brand {
  display: flex;
  align-items: baseline;
  gap: 3px;
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-chrome-fg);
  text-decoration: none;
  white-space: nowrap;
  padding: 0 14px 0 8px;
  border-right: 1px solid var(--color-chrome-border);
  margin-right: 4px;

  &:hover {
    text-decoration: none;
  }

  & .app-brand-mark {
    color: var(--color-brand-emphasis);
  }

  & .app-brand-tail {
    font-weight: 400;
    color: var(--color-chrome-fg-muted);
  }
}

.app-context-label {
  font-size: 12px;
  color: var(--color-chrome-fg-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Every control that sits on the chrome bar. Deliberately not .btn - the button component is built
   against the canvas tier and turns invisible on a near-black background. */
.chrome-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background-color: transparent;
  color: var(--color-chrome-fg-muted);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;

  &:hover:not(:disabled) {
    background-color: var(--color-chrome-bg-hover);
    color: var(--color-chrome-fg);
  }

  &:focus-visible {
    outline: 2px solid var(--color-brand-emphasis);
    outline-offset: -1px;
  }

  &[aria-expanded="true"],
  &[aria-pressed="true"] {
    background-color: var(--color-chrome-bg-hover);
    color: var(--color-chrome-fg);
  }

  & .chrome-btn-glyph {
    font-size: 15px;
  }
}

/* The command-palette affordance. Wider than the icon buttons and left-aligned, because it is a
   search field's stand-in rather than a toggle. */
.chrome-search {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  min-width: 200px;
  padding: 0 8px 0 10px;
  border: 1px solid var(--color-chrome-border);
  border-radius: var(--radius-md);
  background-color: var(--color-chrome-bg);
  color: var(--color-chrome-fg-subtle);
  font-size: 12.5px;
  cursor: pointer;

  &:hover {
    border-color: var(--color-chrome-fg-subtle);
    color: var(--color-chrome-fg-muted);
  }

  & kbd {
    margin-left: auto;
    font-family: var(--font-mono);
    font-size: 10.5px;
    padding: 1px 5px;
    border: 1px solid var(--color-chrome-border);
    border-radius: var(--radius-sm);
    color: var(--color-chrome-fg-subtle);
  }
}

/* Unread marker on the notification bell. */
.chrome-btn-dot {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 15px;
  height: 15px;
  padding: 0 3px;
  border-radius: var(--radius-pill);
  background-color: var(--color-brand-emphasis);
  color: var(--color-brand-fg);
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 700;
  line-height: 15px;
  text-align: center;
}

.chrome-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background-color: var(--color-brand-emphasis);
  color: var(--color-brand-fg);
  font-family: var(--font-display);
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

/* ---- Rails ----------------------------------------------------------------------------------- */

.app-sidebar {
  grid-area: sidebar;
  border-right: 1px solid var(--color-border-default);
  background-color: var(--color-canvas-subtle);
  display: flex;
  flex-direction: column;
  padding: 10px 8px;
  overflow-x: hidden;
  overflow-y: auto;

  & .app-sidebar-footer {
    margin-top: auto;
    padding-top: 10px;
    border-top: 1px solid var(--color-border-muted);
    font-size: 11.5px;
    color: var(--color-fg-subtle);
  }
}

/* Right context rail - page-supplied content: a document's totals, a record's history, a report's
   parameters. */
.app-context {
  grid-area: context;
  border-left: 1px solid var(--color-border-default);
  background-color: var(--color-canvas-subtle);
  display: flex;
  flex-direction: column;
  overflow-x: hidden;

  & .app-context-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--color-border-default);

    & h2 {
      margin: 0;
      font-family: var(--font-display);
      font-size: 14px;
      font-weight: 600;
      letter-spacing: 0.08em;
      text-transform: uppercase;
      color: var(--color-fg-default);
    }
  }

  & .app-context-body {
    padding: 12px;
    overflow-y: auto;
  }
}

/* Both rails collapse the same way: a zero-width column plus visibility:hidden, so their links
   leave the tab order instead of staying focusable at zero width. overflow-x:hidden above clips
   the content while the column animates. */
:root:not([data-sidebar="expanded"]) .app-sidebar,
:root:not([data-context="open"]) .app-context {
  visibility: hidden;
  border-color: transparent;
}

.app-main {
  grid-area: main;
  padding: 16px 20px 32px;
  overflow-x: auto;
  max-width: 100%;
}

/* ---- Navigation ------------------------------------------------------------------------------ */

.nav-section {
  margin-top: 2px;

  &:first-child {
    margin-top: 0;
  }
}

.nav-section-header {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  background: transparent;
  border: none;
  padding: 6px 8px;
  font-family: var(--font-display);
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--color-fg-muted);
  cursor: pointer;
  border-radius: var(--radius-md);
  text-align: left;

  &:hover {
    background-color: var(--color-neutral-subtle);
    color: var(--color-fg-default);
  }

  &:focus-visible {
    outline: 2px solid var(--color-accent-emphasis);
    outline-offset: 1px;
  }

  /* A collapsed section holding the current page still says so, so the user is never lost after
     collapsing the section they are working in. */
  &[data-contains-active="true"] {
    color: var(--color-accent-fg);
  }
}

.nav-section-chevron {
  display: inline-flex;
  font-size: 9px;
  width: 10px;
  transition: transform 0.12s ease;

  &[data-expanded="true"] {
    transform: rotate(90deg);
  }
}

.nav-section-glyph {
  width: 16px;
  text-align: center;
  font-size: 13px;
}

.nav-section-body {
  display: flex;
  flex-direction: column;
  gap: 1px;
  margin: 2px 0 6px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px 5px 30px;
  border-radius: var(--radius-md);
  color: var(--color-fg-default);
  font-size: 13px;
  text-decoration: none;

  &:hover {
    background-color: var(--color-neutral-subtle);
    text-decoration: none;
  }

  /* A left marker rather than a full fill, so the active item still reads when the rail is
     scrolled and several sections are open at once. */
  &.active {
    background-color: var(--color-accent-subtle);
    color: var(--color-accent-fg);
    font-weight: 600;
    box-shadow: inset 2px 0 0 var(--color-accent-emphasis);
  }

  &:focus-visible {
    outline: 2px solid var(--color-accent-emphasis);
    outline-offset: -2px;
  }
}

/* A count riding on a nav item - approvals waiting, invoices overdue. */
.nav-link-count {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 10.5px;
  font-weight: 600;
  padding: 0 5px;
  border-radius: var(--radius-pill);
  background-color: var(--color-neutral-subtle);
  color: var(--color-fg-muted);
}

.nav-link.active .nav-link-count {
  background-color: var(--color-accent-emphasis);
  color: var(--color-fg-on-emphasis);
}

/* ---- Page header ------------------------------------------------------------------------------ */

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 16px;

  & .page-header-titles {
    min-width: 0;

    & h1 {
      margin-bottom: 2px;
      font-family: var(--font-display);
      font-size: 24px;
      font-weight: 600;
      letter-spacing: -0.01em;
    }

    & .page-header-lede {
      margin: 0;
      color: var(--color-fg-muted);
      font-size: 13px;
      max-width: 68ch;
    }
  }

  & .page-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }
}

.breadcrumb {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
  font-size: 12px;
  color: var(--color-fg-muted);
  margin-bottom: 3px;

  & .breadcrumb-sep {
    color: var(--color-fg-subtle);
  }
}

/* ---- Sign-in and other chrome-less pages ------------------------------------------------------ */

.empty-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(1100px 520px at 50% -10%, var(--color-accent-subtle), transparent 70%),
    var(--color-canvas-default);
}

.login-page {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-lg);
  padding: 28px;
  background-color: var(--color-canvas-default);
  box-shadow: var(--shadow-overlay);

  & .login-brand {
    display: flex;
    align-items: baseline;
    gap: 4px;
    font-family: var(--font-display);
    font-size: 20px;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    margin-bottom: 20px;

    & .app-brand-mark {
      color: var(--color-accent-fg);
    }

    & .app-brand-tail {
      font-weight: 400;
      color: var(--color-fg-muted);
    }
  }

  & h1 {
    font-size: 18px;
    margin-bottom: 2px;
  }

  & > p {
    margin-bottom: 20px;
    color: var(--color-fg-muted);
    font-size: 13px;
  }
}

/* ---- Narrow viewports -------------------------------------------------------------------------- */
/* Under ~900px there is no room to push content aside for a 244px rail, so the rails stop being grid
   columns and float over the content instead. The grid stays collapsed at every combination of the
   data-* attributes; only the rails' own positioning changes. */

@media (max-width: 900px) {
  .app-shell,
  :root[data-sidebar="expanded"] .app-shell,
  :root[data-context="open"] .app-shell,
  :root[data-sidebar="expanded"][data-context="open"] .app-shell {
    grid-template-columns: 0 minmax(0, 1fr) 0;
  }

  .app-sidebar,
  .app-context {
    position: fixed;
    top: 48px;
    bottom: 0;
    width: min(290px, 84vw);
    z-index: 40;
    box-shadow: var(--shadow-overlay);
  }

  .app-sidebar { left: 0; }
  .app-context { right: 0; }

  .app-main { padding: 12px; }

  .chrome-search { display: none; }

  .page-header {
    flex-direction: column;
    align-items: stretch;
  }
}

/* ---- Context-rail content ------------------------------------------------------------------------ */

.ctx-stat {
  display: flex;
  flex-direction: column;
  gap: 1px;
  border: 1px solid var(--color-border-default);
  background-color: var(--color-canvas-default);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  margin-bottom: 8px;
}

.ctx-stat-value {
  font-family: var(--font-display);
  font-size: 28px;
  font-weight: 600;
  line-height: 1.05;
  color: var(--color-fg-default);
  font-variant-numeric: tabular-nums;
}

.ctx-stat-note {
  font-size: 11px;
  color: var(--color-fg-muted);
}

.ctx-empty {
  font-size: 12px;
  line-height: 1.6;
  margin-top: 6px;
  color: var(--color-fg-muted);
}

.ctx-card {
  position: relative;
  border: 1px solid var(--color-border-default);
  background-color: var(--color-canvas-default);
  border-radius: var(--radius-sm);
  padding: 9px 11px 9px 13px;
  margin-top: 8px;
  overflow: hidden;

  /* Accent spine. */
  &::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background-color: var(--color-accent-emphasis);
  }

  & .ctx-card-title {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 5px;
  }

  & .ctx-card-row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    font-size: 11.5px;
    color: var(--color-fg-muted);
    padding: 2px 0;

    & b {
      font-family: var(--font-mono);
      font-weight: 600;
      color: var(--color-fg-default);
    }
  }
}
