/* App shell: persistent sidebar + dynamic header + main slot + widget overlay.
   RTL-first. All values via tokens from _design-tokens.css. No hex literals.
*/

:root {
  --sidebar-w: 13.75rem;
  --sidebar-collapsed-w: 68px;
}

html, body {
  height: 100%;
  overflow: hidden;
}

/* Ensure [hidden] wins over component display rules (flex/grid) */
[hidden] {
  display: none !important;
}

/* ----- APP SHELL GRID -----
   Grid areas reverse automatically in RTL, so "sidebar" visually anchors right.
*/
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "sidebar header"
    "sidebar main";
  height: 100vh;
}

/* Chromeless mode (auth screens, future onboarding) — main spans full viewport */
body.is-chromeless .app-shell {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "main";
  width: 100vw;
}

body.is-chromeless .app-sidebar,
body.is-chromeless .app-header {
  display: none;
}

body.is-chromeless .app-main {
  width: 100%;
  min-width: 0;
}

/* Make user-card cursor:pointer to hint it triggers sign-out when authed */
.app-sidebar .user-card {
  cursor: pointer;
}

/* ----- SIDEBAR ----- */

.app-sidebar {
  grid-area: sidebar;
  background: var(--color-surface);
  border-inline-end: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  /* visible + raised so hover tooltips can escape the narrow rail */
  overflow: visible;
  position: relative;
  z-index: 50;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: var(--space-3);
  padding-inline: var(--space-3);
  height: var(--shell-h);
  border-block-end: 1px solid var(--color-border);
  font-weight: var(--fw-bold);
  font-size: var(--fs-body-sm);
  color: var(--color-primary);
  text-decoration: none;
  flex-shrink: 0;
  min-width: 0;
}

.sidebar-brand .shell-brand-lockup {
  height: 28px;
  width: auto;
  display: block;
}

.sidebar-brand span:not(.shell-brand-mark) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.sidebar-brand:hover {
  text-decoration: none;
}

.sidebar-nav {
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
  padding: var(--space-3) var(--space-2);
}

.sidebar-nav ul {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-link {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  gap: var(--space-3);
  width: 100%;
  min-height: var(--touch-min);
  padding-inline: var(--space-3);
  padding-block: var(--space-2);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-weight: var(--fw-medium);
  font-size: var(--fs-body-sm);
  text-decoration: none;
  text-align: start;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}

.sidebar-link:hover {
  background: var(--color-surface-alt);
  color: var(--color-text);
  text-decoration: none;
}

.sidebar-link:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.sidebar-link.is-active {
  background: var(--color-info-bg);
  color: var(--color-primary);
  font-weight: var(--fw-semibold);
  position: relative;
}

.sidebar-link.is-active::before {
  content: "";
  position: absolute;
  inset-block: var(--space-2);
  inset-inline-end: -2px;
  width: 3px;
  background: var(--color-accent);
  border-radius: var(--radius-pill);
}

.sidebar-link .ico {
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.sidebar-link .ico svg {
  width: 24px;
  height: 24px;
  display: block;
}

.sidebar-link.is-active .ico svg {
  stroke-width: 2.35;
}

.sidebar-link .label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: start;
  line-height: var(--lh-normal);
}

.sidebar-footer {
  padding: var(--space-3);
  border-block-start: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  flex-shrink: 0;
}

.sidebar-team-link {
  margin-block-end: var(--space-1);
}

/* H6/H7/H8 — stacked monthly budget in sidebar footer */
.sidebar-spend {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3);
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--fs-caption);
}

.sidebar-budget-line {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-budget-line + .sidebar-budget-line {
  padding-block-start: var(--space-2);
  border-block-start: 1px solid var(--color-border);
}

.sidebar-budget-label {
  color: var(--color-text-muted);
}

.sidebar-budget-spent,
.sidebar-budget-remaining,
.sidebar-spend .sidebar-budget-spent,
.sidebar-spend .sidebar-budget-remaining {
  font-family: var(--font-mono);
  font-size: var(--fs-body-sm);
  color: var(--color-text);
  font-weight: var(--fw-semibold);
}

.user-card {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  min-height: var(--touch-min);
  min-width: 0;
}

.user-card:hover {
  background: var(--color-surface-alt);
}

.user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
  text-align: start;
}

.user-name {
  font-size: var(--fs-body-sm);
  font-weight: var(--fw-semibold);
  color: var(--color-text);
}

.user-tenant {
  font-size: var(--fs-caption);
  color: var(--color-text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ----- HEADER (dynamic) ----- */

.app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: 0 var(--space-6);
  background: var(--color-primary);
  color: var(--color-text-inverse);
  border-block-end: 1px solid var(--color-primary-hover);
  box-shadow: var(--shadow-sm);
  min-width: 0;
  min-height: var(--shell-h);
}

.app-header .header-title,
.app-header .header-crumbs,
.app-header .header-crumbs a {
  color: var(--color-text-inverse);
}

.app-header .btn-secondary {
  background: transparent;
  color: var(--color-text-inverse);
  border-color: var(--color-border-strong);
}

.app-header .btn-primary {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: var(--color-primary);
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.header-flow-menu {
  align-self: flex-start;
  margin-block-end: var(--space-1);
}

.header-flow-menu summary {
  list-style: none;
  cursor: pointer;
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  display: grid;
  place-items: center;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-alt);
  font-size: var(--fs-body);
}

.header-flow-menu summary::-webkit-details-marker { display: none; }

.header-flow-menu-panel {
  position: absolute;
  margin-block-start: var(--space-1);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  min-width: 180px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  padding: var(--space-2);
  gap: var(--space-1);
}

.header-flow-menu-item {
  display: block;
  width: 100%;
  text-align: start;
  padding: var(--space-2) var(--space-3);
  min-height: var(--touch-min);
  border: 0;
  background: transparent;
  font: inherit;
  color: var(--color-text);
  text-decoration: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.header-flow-menu-item:hover {
  background: var(--color-surface-alt);
  text-decoration: none;
}

.header-flow-menu[open] {
  position: relative;
}

/* ----- Org switcher (header) ----- */

.org-switcher {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-block-end: 2px;
  min-height: 36px;
}

.org-switcher-label {
  font-size: var(--fs-caption);
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.org-switcher-static {
  font-size: var(--fs-body-sm);
  font-weight: var(--fw-semibold);
  color: var(--color-text);
  padding: var(--space-1) 0;
}

.org-switcher-control {
  position: relative;
  min-width: 0;
}

.org-switcher-trigger {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  min-height: var(--touch-min);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  font: inherit;
  font-size: var(--fs-body-sm);
  font-weight: var(--fw-semibold);
  color: var(--color-text);
  cursor: pointer;
  max-width: min(280px, 100%);
}

.org-switcher-trigger:hover {
  border-color: var(--color-primary);
}

.org-switcher-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.org-switcher-chevron {
  font-size: 10px;
  opacity: 0.6;
  flex-shrink: 0;
}

.org-switcher-menu {
  position: absolute;
  inset-block-start: calc(100% + 4px);
  inset-inline-start: 0;
  z-index: 200;
  min-width: 220px;
  max-width: min(320px, 90vw);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-2);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.org-switcher-menu[hidden] {
  display: none !important;
}

.org-switcher-option {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  padding: var(--space-3);
  min-height: var(--touch-min);
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  font: inherit;
  text-align: start;
  cursor: pointer;
}

.org-switcher-option:hover {
  background: var(--color-surface-alt);
}

.org-switcher-option.is-active {
  background: var(--color-primary-bg, var(--color-info-bg));
}

.org-switcher-option-name {
  font-weight: var(--fw-semibold);
  font-size: var(--fs-body-sm);
  color: var(--color-text);
}

.org-switcher-option-role {
  font-size: var(--fs-caption);
  color: var(--color-text-muted);
}

.header-crumbs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  font-size: var(--fs-caption);
  color: var(--color-text-muted);
  min-width: 0;
  line-height: var(--lh-relaxed);
}

.header-crumbs a {
  color: var(--color-text-muted);
  text-decoration: none;
}

.header-crumbs a:hover {
  color: var(--color-text);
  text-decoration: underline;
}

.header-crumbs .sep {
  opacity: 0.4;
  unicode-bidi: isolate;
}

.header-crumbs [aria-current="page"] {
  color: var(--color-text);
  font-weight: var(--fw-medium);
}

.header-title {
  font-size: var(--fs-h4);
  font-weight: var(--fw-bold);
  color: var(--color-text);
  line-height: var(--lh-snug);
  padding-block: var(--space-1);
  min-width: 0;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-wrap: balance;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
}

/* ----- MAIN ----- */

.app-main {
  grid-area: main;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--color-bg);
  position: relative;
}

.view-slot {
  min-height: 100%;
}

.view-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--color-text-muted);
  font-size: var(--fs-body-sm);
  background: var(--color-bg);
  z-index: 10;
}

/* ----- WIDGET OVERLAY ----- */

.widget-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
}

.widget-scrim {
  position: absolute;
  inset: 0;
  background: rgba(15, 27, 45, 0.55);
  backdrop-filter: blur(4px);
}

.widget-frame {
  position: relative;
  background: var(--color-surface);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  overflow: hidden;
}

/* Widget can request a layout: side-drawer (default) or centered modal */
.widget-overlay[data-layout="drawer"] .widget-frame {
  width: var(--drawer-w);
  height: 100vh;
  margin-inline-end: auto;
  animation: drawer-in var(--dur-slow) var(--ease-out);
}

.widget-overlay[data-layout="modal"] {
  align-items: center;
  justify-content: center;
}

.widget-overlay[data-layout="modal"] .widget-frame {
  width: min(720px, calc(100vw - var(--space-8)));
  max-height: calc(100vh - var(--space-8));
  border-radius: var(--radius-xl);
  margin: var(--space-4);
}

.widget-overlay[data-layout="full"] .widget-frame {
  width: 100vw;
  height: 100vh;
}

@keyframes drawer-in {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}

[dir="ltr"] .widget-overlay[data-layout="drawer"] .widget-frame {
  margin-inline-end: 0;
  margin-inline-start: auto;
}

.widget-close {
  position: absolute;
  top: var(--space-3);
  inset-inline-end: var(--space-3);
  width: var(--touch-min);
  height: var(--touch-min);
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  border-radius: var(--radius-pill);
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: 18px;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-sm);
  z-index: 5;
  border: 1px solid var(--color-border);
}

.widget-close:hover {
  background: var(--color-surface-alt);
  color: var(--color-text);
}

.widget-slot {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* When widget is open: hide sidebar + header but preserve grid */
body.is-widget-open .app-sidebar,
body.is-widget-open .app-header {
  visibility: hidden;
}

body.is-widget-open .app-main {
  visibility: hidden;
}

/* ----- RESPONSIVE ----- */

/* Expanded sidebar: icon + persistent label at all widths */

@media (max-width: 768px) {
  :root {
    --sidebar-w: 12.5rem;
  }

  .sidebar-link {
    min-height: var(--touch-mobile);
  }
}

/* ----- Global shell error banners ----- */

.shell-auth-banner,
.shell-network-banner {
  position: fixed;
  inset-inline: var(--space-4);
  bottom: var(--space-4);
  z-index: 200;
  max-width: 480px;
  margin-inline-start: auto;
  padding: var(--space-4);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.shell-auth-banner {
  background: var(--color-warning-bg);
  border: 1px solid var(--color-warning);
  color: var(--color-text);
}

.shell-network-banner {
  background: var(--color-error-bg);
  border: 1px solid var(--color-error);
  color: var(--color-text);
}

.shell-auth-banner p,
.shell-network-banner p {
  margin: 0;
  font-size: var(--fs-body-sm);
  line-height: var(--lh-relaxed);
}

.shell-auth-actions {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* ----- Focus rings on custom controls (views) ----- */

.view-slot button:focus-visible,
.widget-slot button:focus-visible,
.view-slot [role="radio"]:focus-visible,
.widget-slot [role="radio"]:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.view-slot .star-btn:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* WCAG AA: warning badge text on warning bg */
.view-slot .badge[data-tone="warning"],
.widget-slot .badge[data-tone="warning"] {
  color: #8B5A14;
  background: var(--color-warning-bg);
  border: 1px solid var(--color-warning);
}

@media (max-width: 640px) {
  .app-shell {
    grid-template-rows: auto 1fr;
  }

  .app-header {
    padding: var(--space-2) var(--space-4);
    gap: var(--space-2);
    min-height: 56px;
  }

  .header-left {
    gap: var(--space-1);
  }

  .header-title {
    font-size: var(--fs-body-sm);
    font-weight: var(--fw-semibold);
    line-height: var(--lh-normal);
    -webkit-line-clamp: 1;
    color: var(--color-text);
  }

  .org-switcher-label {
    font-size: 11px;
  }

  .header-actions .btn {
    min-height: var(--touch-min);
    padding: 0 var(--space-3);
  }
}

/* ----- LEGACY MOCKUP SHELL OVERRIDE -----
   The extracted views still contain top-shell-nav code from the old mocks.
   Hide it so the app shell owns navigation.
*/
.view-slot .shell-nav,
.widget-slot .shell-nav {
  display: none;
}

.view-slot > .shell {
  display: block;
  min-height: auto;
}
