/* Base + page grid: topbar / (sidebar + main) / footer */

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  /* The app's text: one family, one base size and line height (tokens in
     variables.css). The root stays at the browser's 16px, so a user's own
     font-size setting still scales everything. */
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  font-weight: var(--weight-regular);
  background: var(--color-bg);
  color: var(--color-text);
  transition: background var(--transition-medium), color var(--transition-medium);
}

h1,
h2,
h3,
h4 {
  line-height: var(--leading-tight);
  font-weight: var(--weight-semibold);
}

/* Shares, fractions and amounts line up digit under digit in every table. */
table {
  font-variant-numeric: tabular-nums;
}

/* height, not min-height: pins the grid to the viewport so the middle
   (1fr) row is a real scroll container via .main's own overflow-y (below)
   instead of growing past the viewport and pushing the footer down when a
   page (e.g. a long waris list) has more content than fits on screen. */
.app-shell {
  display: grid;
  grid-template-rows: var(--topbar-height) 1fr var(--footer-height);
  grid-template-columns: 1fr;
  height: 100vh;
}

/* ---------- Top bar ---------- */

.topbar {
  grid-row: 1;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding-inline: var(--space-4);
  border-block-end: 1px solid var(--color-border);
  background: var(--color-surface);
}

.topbar__sidebar-toggle {
  margin-inline-end: var(--space-1);
}

.topbar__brand {
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
  letter-spacing: 0.01em;
  background: var(--brand-gradient);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  user-select: none;
}

.topbar__spacer {
  flex: 1;
}

/* ---------- Body: sidebar + main ---------- */

.app-body {
  grid-row: 2;
  display: flex;
  min-height: 0;
}

.sidebar {
  flex: 0 0 var(--sidebar-width);
  width: var(--sidebar-width);
  /* Navigation only (sessions.css): a few links and the short Recent list. */
  overflow: hidden;
  /* Column so a footer block (e.g. the Feedback button) can sit at the
     bottom: .sidebar__nav takes the free space (below). */
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border-inline-end: 1px solid var(--color-border);
  transition: flex-basis var(--transition-medium), width var(--transition-medium);
}

.sidebar[data-collapsed="true"] {
  flex-basis: var(--sidebar-width-collapsed);
  width: var(--sidebar-width-collapsed);
}

.sidebar__nav {
  flex: 1 1 auto;
  min-block-size: 0;
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.sidebar__section-label {
  padding-inline: var(--space-2);
  margin-block: var(--space-2) var(--space-1);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
}

.sidebar[data-collapsed="true"] .sidebar__section-label,
.sidebar[data-collapsed="true"] .nav-item__label {
  opacity: 0;
  pointer-events: none;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-2-5);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  border: none;
  background: transparent;
  font: inherit;
  text-align: start;
  width: 100%;
}

.nav-item:hover {
  background: var(--color-surface-alt);
}

.nav-item[aria-current="page"] {
  background: var(--color-surface-alt);
  color: var(--color-accent);
  font-weight: var(--weight-semibold);
}

.nav-item__icon {
  flex: 0 0 auto;
  width: 1.25rem;
  text-align: center;
}

.nav-item__label {
  transition: opacity var(--transition-fast);
}

/* Collapsed rail: each item's own label becomes its tooltip, beside the icon,
   fading and sliding in on hover or keyboard focus (no `title`: the browser's
   tooltip is slow to appear and can't be eased). Wide screens only: on a
   narrow one the collapsed sidebar has no width at all (below), and nothing
   may spill out of it. */
@media (min-width: 721px) {
  .sidebar[data-collapsed="true"] {
    /* Let the tooltips out, and above the workspace beside the rail. */
    overflow: visible;
    position: relative;
    z-index: 5;
  }

  .sidebar[data-collapsed="true"] .nav-item {
    position: relative;
    overflow: visible;
  }

  .sidebar[data-collapsed="true"] .nav-item__label {
    position: absolute;
    inset-block-start: 50%;
    inset-inline-start: calc(100% + var(--space-3));
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    background: var(--color-text);
    color: var(--color-surface);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    line-height: var(--leading-tight);
    box-shadow: var(--shadow-md);
    visibility: hidden;
    transform: translate(calc(-1 * var(--space-1)), -50%);
    /* Leaving: fade out at once. `visibility` waits for the fade to end. */
    transition: opacity var(--transition-fast), transform var(--transition-fast), visibility 0s linear 150ms;
  }

  .sidebar[data-collapsed="true"] .nav-item__label:dir(rtl) {
    transform: translate(var(--space-1), -50%);
  }

  .sidebar[data-collapsed="true"] .nav-item:hover .nav-item__label,
  .sidebar[data-collapsed="true"] .nav-item:focus-visible .nav-item__label {
    opacity: 1;
    visibility: visible;
    transform: translate(0, -50%);
    /* Arriving: a short pause, so sweeping the pointer down the rail doesn't flash every label. */
    transition-delay: 80ms;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar[data-collapsed="true"] .nav-item__label {
    transition-property: opacity, visibility;
  }
}

/* ---------- Sidebar footer (pinned to the bottom of the sidebar; e.g. Feedback, Back to app) ---------- */

.sidebar__footer {
  flex: 0 0 auto;
  position: sticky;
  inset-block-end: 0;
  padding: var(--space-2) var(--space-3) var(--space-3);
  border-block-start: 1px solid var(--color-border);
  background: var(--color-surface);
}

.sidebar__footer .nav-item:focus-visible {
  outline: var(--focus-ring);
  outline-offset: -2px;
}

/* The app's Feedback button: a quiet, muted item. It is always there but
   rarely the next thing to do, so it must not outweigh "New" (sessions.css),
   the sidebar's one filled button. */
.sidebar__feedback {
  inline-size: 100%;
  color: var(--color-text-muted);
}

.sidebar__feedback:hover {
  color: var(--color-text);
}

.sidebar__footer .sidebar__feedback:focus-visible {
  outline-offset: 2px;
}

.main {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: var(--space-6);
}

/* ---------- Workspace: bar + main (index.html only) ----------
   The case controls (save state, guide switch) sit in a bar above the
   scrolling .main, so they stay in view. Wraps .main in a column; admin.html
   has no wrapper and keeps .main directly in .app-body. */

.workspace {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.workspace > .main {
  min-height: 0;
}

/* One screen at a time: the dashboard, or a calculation (its bar + form).
   js/router.js sets data-view from the address. */
.workspace[data-view="dashboard"] > .workspace-bar,
.workspace[data-view="dashboard"] > .main,
.workspace[data-view="case"] > .dashboard {
  display: none;
}

.workspace-bar {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-3);
  /* One height whatever it holds: its items come and go (the save indicator
     is hidden with no case open, the Guide switch when an admin turns the
     guide off). Fits the tallest one, the Guide switch, plus padding and
     border; a minimum, not a fixed height, so a narrow screen can still wrap. */
  min-block-size: 3.25rem;
  padding-block: var(--space-2);
  padding-inline: var(--space-6);
  border-block-end: 1px solid var(--color-border);
  background: var(--color-surface);
}

/* Start of the bar: back to the dashboard, then the open calculation's title. */
.workspace-bar__case {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex: 1 1 12rem;
  min-inline-size: 0;
}

.workspace-bar__controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
}

/* ---------- Footer ---------- */

.footer {
  grid-row: 3;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-inline: var(--space-4);
  border-block-start: 1px solid var(--color-border);
  background: var(--color-surface);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

@media (max-width: 720px) {
  .sidebar {
    position: fixed;
    inset-block: var(--topbar-height) var(--footer-height);
    inset-inline-start: 0;
    z-index: 20;
    box-shadow: var(--shadow-md);
  }

  .sidebar[data-collapsed="true"] {
    flex-basis: 0;
    width: 0;
    border-inline-end: none;
  }
}
