/* Layouts: app shell, control shell, auth, onboarding, error pages (docs/DESIGN-DIRECTION.md §4) */

/* App shell ----------------------------------------------------------- */

.app-shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.app-sidebar { display: none; }

.app-topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  min-height: calc(var(--topbar-height) + var(--safe-top));
  padding: var(--safe-top) max(var(--space-2), var(--safe-right)) 0 max(var(--space-2), var(--safe-left));
  background: color-mix(in srgb, var(--color-bg) 90%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--color-border);
}

.app-topbar__left,
.app-topbar__right {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  min-width: 0;
}

.app-topbar__left { flex: 1 1 auto; padding-left: var(--space-1); }

/* The mark sits at the far left of the top bar on phones and tablets and links Home; from
 * 900px the sidebar carries the brand in the top-left corner instead. */
.app-topbar__brand {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: var(--touch-target);
  flex-shrink: 0;
  border-radius: var(--radius-sm);
}

.app-topbar__brand img { width: 28px; height: 28px; border-radius: 7px; }
.app-topbar__brand:hover { background: var(--color-surface-sunken); }

/* The sidebar toggle only exists where the sidebar does (from 900px). */
.app-topbar__sidebar-toggle { display: none; }

/* The action cluster is a row of fixed 44px targets: it must never shrink, or the account
 * button lands past the viewport edge. The title absorbs all shrinkage (it truncates). */
.app-topbar__right { flex: 0 0 auto; }

/* On phones a page action keeps its icon only; the label stays in the accessible name. */
@media (max-width: 639.98px) {
  .app-topbar__right .button { padding-inline: var(--space-2); }
  .app-topbar__right .button > .icon + span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

.app-topbar__title {
  font-family: var(--font-display);
  font-size: var(--text-heading);
  line-height: var(--leading-heading);
  font-weight: var(--weight-display);
  letter-spacing: var(--tracking-tight);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.app-topbar__title:empty { display: none; }
.app-topbar .avatar { margin-left: var(--space-1); }

/* The avatar is a 44px button that opens the account menu sheet. */
.app-topbar__account {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--touch-target);
  height: var(--touch-target);
  border-radius: var(--radius-full);
  background: transparent;
  border: 0;
  cursor: pointer;
  flex-shrink: 0;
}

.app-topbar__account:hover { background: var(--color-surface-sunken); }
.app-topbar__account .avatar { margin: 0; }

/* Pending approvals: a count on the avatar button and the sidebar user card, because the
 * Approvals link itself lives inside the account sheet. */
.app-topbar__account-badge {
  position: absolute;
  top: 0;
  right: -2px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: var(--radius-full);
  background: var(--color-danger-bright);
  color: var(--color-text-on-danger);
  border: 2px solid var(--color-surface);
  font-size: var(--text-min);
  font-weight: var(--weight-semibold);
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.app-content {
  flex: 1 1 auto;
  width: 100%;
  max-width: var(--content-max);
  margin-inline: auto;
  padding: var(--space-3) max(var(--gutter), var(--safe-right)) calc(var(--bottom-nav-height) + var(--safe-bottom) + var(--space-8)) max(var(--gutter), var(--safe-left));
  outline: none;
}

.app-content--narrow { max-width: var(--content-narrow); }

/* Sidebar navigation (desktop) */

.app-sidebar__brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  margin-bottom: var(--space-4);
  color: var(--color-text);
  text-decoration: none;
  font-family: var(--font-display);
  font-weight: var(--weight-display);
  font-size: var(--text-heading);
  letter-spacing: var(--tracking-tight);
}

.app-sidebar__brand:hover { text-decoration: none; }

.app-sidebar__brand img {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
}

.app-sidebar__cta { margin: var(--space-4) 0 var(--space-2); }

.app-nav {
  margin-top: var(--space-2);
  flex: 1 1 auto;
  overflow-y: auto;
}

.app-nav ul {
  display: flex;
  flex-direction: column;
  gap: 2px;
  list-style: none;
  padding: 0;
  margin: 0;
}

.app-nav__link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: var(--touch-target);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-weight: var(--weight-medium);
  font-size: var(--text-label);
  line-height: var(--leading-label);
  text-decoration: none;
  transition: background var(--duration-fast), color var(--duration-fast);
}

.app-nav__link:hover {
  background: var(--color-surface-sunken);
  color: var(--color-text);
  text-decoration: none;
}

.app-nav__link.is-active { background: var(--color-primary-soft); color: var(--color-primary-soft-text); font-weight: var(--weight-semibold); }
.app-nav__link.is-active .icon { color: var(--color-primary-text); }

.app-nav__link--button {
  background: none;
  border: 0;
  cursor: pointer;
}

.app-nav__badge {
  margin-left: auto;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--radius-full);
  background: var(--color-danger-bright);
  color: var(--color-text-on-danger);
  font-size: var(--text-min);
  font-weight: var(--weight-semibold);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.app-sidebar__footer {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
}

.app-sidebar__logout { margin: 0; }

/* User card at the bottom of the sidebar: name · language · currency; opens the account menu. */
.app-sidebar__user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 56px;
  padding: var(--space-2) var(--space-2);
  border-radius: var(--radius-md);
  color: var(--color-text);
  text-align: left;
  cursor: pointer;
  border: 0;
  background: transparent;
}

.app-sidebar__user:hover { background: var(--color-surface-sunken); }
.app-sidebar__user .avatar { flex: 0 0 auto; }

.app-sidebar__user-text {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  line-height: var(--leading-label);
}

.app-sidebar__user-name {
  font-weight: var(--weight-semibold);
  font-size: var(--text-label);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-sidebar__user-meta {
  font-size: var(--text-caption);
  line-height: var(--leading-caption);
  color: var(--color-text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-sidebar__user-row {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.app-sidebar__user-row > .app-sidebar__user { flex: 1 1 auto; min-width: 0; }

/* The one app menu sheet ("More" in the bottom nav, the avatar, the sidebar user card): the
 * account row, the space switcher, then every other section as a .list-row with an icon tile. */

.app-menu {
  display: flex;
  flex-direction: column;
}

.app-menu .app-nav__link { min-height: 48px; font-size: var(--text-body); }
.app-menu__divider { border-top: 1px solid var(--color-border); margin: var(--space-2) 0; }
.app-menu__space { margin-bottom: var(--space-3); }

.app-menu__row {
  width: 100%;
  min-height: 52px;
  padding: var(--space-2) var(--space-1);
  border-top: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  text-decoration: none;
}

.app-menu > .app-menu__row:first-child,
.app-menu__divider + .app-menu__row { border-top: 0; }
.app-menu__row:hover { background: var(--color-surface-sunken); text-decoration: none; }
.app-menu__row .list-row__title { font-weight: var(--weight-medium); white-space: normal; }
.app-menu__row.is-active .list-row__title,
.app-menu__row.is-active .list-row__chevron { color: var(--color-primary-text); }
.app-menu__row.is-active .icon-tile { --tile-bg: var(--color-primary-soft); --tile-ink: var(--color-primary-soft-text); }
.app-menu__row--danger .list-row__title { color: var(--color-danger); }
.app-menu__row .app-nav__badge { margin-right: var(--space-1); }

button.app-menu__row {
  font: inherit;
  background: none;
  border-left: 0;
  border-right: 0;
  border-bottom: 0;
  text-align: left;
  cursor: pointer;
}

/* The Sign out form wraps the last row: the form is invisible, the row keeps its divider. */
.app-menu__form { display: contents; }
.app-menu__form > .app-menu__row { border-top: 1px solid var(--color-border); }

.app-menu__user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-1) var(--space-3);
  color: inherit;
  text-decoration: none;
}

.app-menu__user:hover { text-decoration: none; }
.app-menu__user .app-sidebar__user-text { flex: 1 1 auto; min-width: 0; display: grid; }
.app-menu__user-name { font-weight: var(--weight-semibold); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-menu__user-meta { font-size: var(--text-caption); line-height: var(--leading-caption); color: var(--color-text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Bottom navigation (mobile): Home · Budgets · + · Insights · More */

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: var(--z-sticky);
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  align-items: end;
  height: calc(var(--bottom-nav-height) + var(--safe-bottom));
  padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  box-shadow: var(--shadow-nav);
}

.bottom-nav__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: var(--bottom-nav-height);
  min-width: 0;
  padding: 0 2px;
  color: var(--color-text-muted);
  font-size: var(--text-caption);
  line-height: var(--leading-caption);
  font-weight: var(--weight-medium);
  text-decoration: none;
  border-radius: var(--radius-md);
  -webkit-user-select: none;
  user-select: none;
}

.bottom-nav__item > span {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bottom-nav__item--button { background: none; border: 0; font: inherit; font-size: var(--text-caption); font-weight: var(--weight-medium); cursor: pointer; padding: 0 2px; }
.bottom-nav__item:hover { text-decoration: none; color: var(--color-text); }

/* Active: icon and label in primary, with a soft pill behind the icon. */
.bottom-nav__item.is-active { color: var(--color-primary-text); font-weight: var(--weight-semibold); }
.bottom-nav__item .icon { border-radius: var(--radius-full); padding: 3px 8px; width: 40px; height: 28px; box-sizing: border-box; }
.bottom-nav__item.is-active .icon { background: var(--color-primary-soft); stroke-width: 2.3; }

/* The raised round primary + button */
.bottom-nav__add {
  justify-self: center;
  width: 56px;
  height: 56px;
  margin-bottom: calc(var(--bottom-nav-height) - 52px);
  border-radius: var(--radius-full);
  background: var(--color-primary);
  color: var(--color-text-on-primary);
  box-shadow: var(--shadow-fab);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transform: translateY(-14px);
  transition: transform var(--duration-fast) var(--ease-spring), background var(--duration-fast);
  border: 4px solid var(--color-surface);
}

.bottom-nav__add:hover { background: var(--color-primary-hover); }
.bottom-nav__add:active { transform: translateY(-12px) scale(0.96); }

/* Offline banner */

.offline-banner {
  position: fixed;
  left: 50%;
  top: calc(var(--safe-top) + var(--space-2));
  z-index: var(--z-toast);
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  max-width: calc(100% - 2 * var(--space-4));
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-full);
  background: var(--color-warning-soft);
  color: var(--color-warning-soft-text);
  border: 1px solid color-mix(in srgb, var(--color-warning) 35%, transparent);
  font-size: var(--text-label);
  font-weight: var(--weight-medium);
  box-shadow: var(--shadow-md);
}

/* Desktop: sidebar replaces bottom nav */

@media (min-width: 900px) {
  .app-shell { flex-direction: row; }

  .app-sidebar {
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100dvh;
    width: var(--sidebar-width);
    flex-shrink: 0;
    padding: var(--space-4) var(--space-3);
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    overflow-y: auto;
  }

  .app-topbar { padding-inline: var(--space-6); min-height: var(--topbar-height); }
  .app-topbar__left { padding-left: 0; }
  .app-topbar__brand { display: none; }
  .app-topbar__sidebar-toggle { display: inline-flex; margin-right: var(--space-1); }

  /* Collapsed: a 56px icon rail. html.sidebar-collapsed is set before first paint (base.html)
   * and toggled by the data-sidebar-toggle buttons / the "[" key (app.js). Labels stay in the
   * DOM for screen readers and show as a tooltip to the right on hover or focus. */
  html.sidebar-collapsed { --sidebar-width: 56px; }
  .app-sidebar { transition: width var(--duration) var(--ease), padding var(--duration) var(--ease); }
  html.sidebar-collapsed .app-sidebar { padding-inline: var(--space-1); overflow: visible; }
  html.sidebar-collapsed .app-nav { overflow: visible; }
  html.sidebar-collapsed .app-sidebar__brand { justify-content: center; padding-inline: 0; gap: 0; }
  html.sidebar-collapsed .app-sidebar__name,
  html.sidebar-collapsed .workspace-switcher,
  html.sidebar-collapsed .app-sidebar__user-text,
  html.sidebar-collapsed .app-sidebar__user > .icon:last-child { display: none; }
  html.sidebar-collapsed .app-nav__link,
  html.sidebar-collapsed .app-sidebar__cta,
  html.sidebar-collapsed .app-sidebar__user { position: relative; width: 48px; min-height: 44px; margin-inline: auto; padding: 0; justify-content: center; gap: 0; }
  html.sidebar-collapsed .app-sidebar__cta { height: 44px; margin-block: var(--space-3) var(--space-2); }
  html.sidebar-collapsed .app-sidebar__user { height: 48px; }
  html.sidebar-collapsed .app-sidebar__user .avatar { margin: 0; }
  html.sidebar-collapsed .app-nav__badge { position: absolute; top: 3px; right: 3px; margin: 0; min-width: 18px; height: 18px; padding: 0 4px; }
  html.sidebar-collapsed :is(.app-nav__link, .app-sidebar__cta) > span {
    position: absolute;
    left: calc(100% + 10px);
    top: 50%;
    transform: translateY(-50%);
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    background: var(--color-surface-inverse);
    color: var(--color-text-inverse);
    font-size: var(--text-caption);
    line-height: var(--leading-caption);
    font-weight: var(--weight-medium);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    z-index: var(--z-dropdown);
    transition: opacity var(--duration-fast);
  }
  html.sidebar-collapsed :is(.app-nav__link, .app-sidebar__cta):is(:hover, :focus-visible) > span { opacity: 1; }
  .app-sidebar__collapse { margin-top: var(--space-1); }
  .app-sidebar__collapse .icon { transition: transform var(--duration) var(--ease); }
  html.sidebar-collapsed .app-sidebar__collapse .icon,
  html.sidebar-collapsed .app-topbar__sidebar-toggle .icon { transform: rotate(180deg); }

  .app-topbar__title { font-size: var(--text-title); line-height: var(--leading-title); }
  .app-topbar__menu { display: none; }

  .app-content { padding: var(--space-6) var(--space-6) var(--space-12); }
  .bottom-nav { display: none; }
}

@media (min-width: 1200px) {
  .app-content { padding-inline: var(--space-8); }
}

/* Control centre shell: navy sidebar painted with the summary-card tokens ------------------ */

.app-shell--control .app-sidebar { background: var(--color-surface-inverse); border-right-color: transparent; }

.app-shell--control .app-sidebar__brand,
.app-shell--control .app-nav__link { color: var(--color-hero-text-muted); }
.app-shell--control .app-sidebar__brand { color: var(--color-hero-text); }

.app-shell--control .app-nav__link:hover { background: var(--overlay-light); color: var(--color-hero-text); }
.app-shell--control .app-nav__link.is-active { background: var(--overlay-light); color: var(--color-hero-text); }

.app-shell--control .app-nav__link.is-active .icon { color: var(--color-hero-primary); }
.app-shell--control .app-sidebar__footer { border-top-color: var(--color-hero-border); }

.app-shell--control .app-content { max-width: 1400px; padding-bottom: var(--space-12); }
.app-shell--control .app-topbar__brand { display: inline-flex; }
@media (min-width: 900px) { .app-shell--control .app-topbar__brand { display: none; } }
.app-shell--control .app-topbar__menu { display: inline-flex; }

@media (min-width: 900px) {
  .app-shell--control .app-topbar__menu { display: none; }
}

/* Auth layout (login / register / reset) ------------------------------ */

.auth-layout {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: calc(var(--space-6) + var(--safe-top)) var(--space-4) calc(var(--space-6) + var(--safe-bottom));
  background: var(--color-bg);
}

.auth-layout__brand {
  display: flex;
  justify-content: center;
  margin-bottom: var(--space-6);
}

.auth-layout__brand img { height: 40px; width: auto; }

.auth-layout__card {
  width: 100%;
  max-width: 440px;
  margin-inline: auto;
  padding: var(--space-6);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  align-self: center;
}

.auth-layout__title {
  font-size: var(--text-title);
  line-height: var(--leading-title);
  font-weight: var(--weight-display);
  margin-bottom: var(--space-1);
}

.auth-layout__lead {
  color: var(--color-text-muted);
  font-size: var(--text-label);
  margin-bottom: var(--space-6);
}

.auth-layout__footer {
  text-align: center;
  font-size: var(--text-label);
  color: var(--color-text-muted);
  margin-top: var(--space-6);
}

.auth-layout__links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-2) var(--space-4);
  margin-top: var(--space-4);
  font-size: var(--text-label);
}

@media (min-width: 640px) {
  .auth-layout__card { padding: var(--space-8); }
}

/* Onboarding layout --------------------------------------------------- */

.onboarding-layout { max-width: 560px; margin-inline: auto; }
.onboarding-layout__progress { margin-bottom: var(--space-6); }

.onboarding-layout__title {
  font-size: var(--text-title);
  line-height: var(--leading-title);
  font-weight: var(--weight-display);
  margin-bottom: var(--space-2);
}

.onboarding-layout__lead { color: var(--color-text-muted); margin-bottom: var(--space-6); }

.onboarding-layout__actions {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  margin-top: var(--space-8);
}

/* Error / offline pages ----------------------------------------------- */

body.error-page {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.error-page__main {
  flex: 1 1 auto;
  display: grid;
  place-items: center;
  padding: calc(var(--space-8) + var(--safe-top)) var(--space-4) calc(var(--space-8) + var(--safe-bottom));
}

.error-page__card {
  width: 100%;
  max-width: 460px;
  text-align: center;
  padding: var(--space-8) var(--space-6);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
}

.error-page__card h1 { font-size: var(--text-title); margin-bottom: var(--space-3); }
.error-page__card p { color: var(--color-text-muted); }

.error-page__code {
  font-size: var(--text-label);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-wide);
  color: var(--color-primary-text);
  margin-bottom: var(--space-2);
}

.error-page__icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--color-primary-soft);
  color: var(--color-primary-text);
  margin-bottom: var(--space-4);
}

.error-page__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-2);
  margin-top: var(--space-6);
}

.error-page__brand {
  display: flex;
  justify-content: center;
  padding-top: var(--space-6);
}

.error-page__brand img { height: 32px; width: auto; }

/* Standalone (installed PWA) adjustments ------------------------------ */

body.is-standalone .app-topbar { padding-top: var(--safe-top); }

/* Focus/anchor targets scroll clear of the sticky top bar (WCAG 2.2 Focus Not Obscured). */
html {
  scroll-padding-top: calc(var(--topbar-height, 56px) + var(--safe-top, env(safe-area-inset-top, 0px)) + 8px);
  scroll-padding-bottom: calc(var(--bottom-nav-height, 64px) + env(safe-area-inset-bottom, 0px) + 8px);
}
