/* ==========================================================================
   App shell · Sidebar · Topbar · Filter bar · Page grid · Mobile nav
   ========================================================================== */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: 100%;
  height: 100vh;
  height: 100dvh;
  transition: grid-template-columns var(--t-base) var(--ease);
}
.app-shell.is-collapsed { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }

/* --- Sidebar -------------------------------------------------------------- */

.app-sidebar {
  grid-column: 1;
  background: var(--navy-700);
  color: #fff;
  display: flex;
  flex-direction: column;
  min-width: 0;
  z-index: var(--z-sidebar);
  border-inline-end: 1px solid var(--navy-800);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  height: var(--topbar-h);
  padding-inline: var(--s-4);
  flex: 0 0 auto;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.brand-mark {
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  border-radius: 8px;
  background: linear-gradient(140deg, var(--coral-600), var(--gold-600));
  display: grid;
  place-items: center;
  font-weight: var(--fw-bold);
  font-size: var(--fs-sm);
  color: #fff;
}
.brand-text { min-width: 0; }
.brand-name {
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-sub {
  font-size: var(--fs-xxs);
  color: var(--navy-200);
  white-space: nowrap;
}

.nav {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--s-3) var(--s-2) var(--s-6);
}
.nav-group + .nav-group { margin-top: var(--s-1); }

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  width: 100%;
  padding: 9px var(--s-3);
  border-radius: var(--r-sm);
  color: var(--navy-200);
  font-size: var(--fs-body);
  font-weight: var(--fw-medium);
  text-align: start;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
  min-height: 38px;
}
.nav-item:hover { background: rgba(255, 255, 255, 0.07); color: #fff; }
.nav-item.is-active { background: rgba(229, 107, 93, 0.16); color: #fff; }
.nav-item.is-active .nav-icon { color: var(--coral-600); }
.nav-item.is-open { color: #fff; }

.nav-icon {
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  color: var(--navy-200);
}
.nav-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.nav-caret {
  flex: 0 0 auto;
  font-size: 10px;
  color: var(--navy-400);
  transition: transform var(--t-fast) var(--ease);
}
.nav-item.is-open .nav-caret { transform: rotate(90deg); }
html[dir="rtl"] .nav-item.is-open .nav-caret { transform: rotate(-90deg); }

.nav-children {
  overflow: hidden;
  max-height: 0;
  transition: max-height var(--t-base) var(--ease);
}
.nav-children.is-open { max-height: 520px; }
.nav-child {
  display: block;
  padding: 7px var(--s-3) 7px var(--s-10);
  border-radius: var(--r-sm);
  color: var(--navy-200);
  font-size: var(--fs-sm);
  position: relative;
  min-height: 34px;
}
html[dir="rtl"] .nav-child { padding: 7px var(--s-10) 7px var(--s-3); }
.nav-child:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }
.nav-child.is-active { color: #fff; font-weight: var(--fw-semibold); background: rgba(255, 255, 255, 0.08); }
.nav-child.is-active::before {
  content: "";
  position: absolute;
  inset-inline-start: 22px;
  top: 50%;
  width: 5px;
  height: 5px;
  margin-top: -2.5px;
  border-radius: 50%;
  background: var(--coral-600);
}

.sidebar-foot {
  flex: 0 0 auto;
  padding: var(--s-3);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.role-pill {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  width: 100%;
  padding: var(--s-2) var(--s-3);
  border-radius: var(--r-sm);
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
  font-size: var(--fs-xs);
  text-align: start;
  min-height: 40px;
}
.role-pill:hover { background: rgba(255, 255, 255, 0.11); }
.role-pill-meta { min-width: 0; flex: 1 1 auto; }
.role-pill-name { font-weight: var(--fw-semibold); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.role-pill-role { color: var(--navy-200); font-size: var(--fs-xxs); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar {
  flex: 0 0 26px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--navy-500);
  display: grid;
  place-items: center;
  font-size: var(--fs-xxs);
  font-weight: var(--fw-bold);
  color: #fff;
}

/* Collapsed sidebar (desktop) */
.app-shell.is-collapsed .brand-text,
.app-shell.is-collapsed .nav-label,
.app-shell.is-collapsed .nav-caret,
.app-shell.is-collapsed .nav-children,
.app-shell.is-collapsed .role-pill-meta { display: none; }
.app-shell.is-collapsed .nav-item,
.app-shell.is-collapsed .role-pill { justify-content: center; padding-inline: 0; }
.app-shell.is-collapsed .brand { justify-content: center; padding-inline: 0; }

/* --- Main column ---------------------------------------------------------- */

.app-main {
  grid-column: 2;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

.app-topbar {
  flex: 0 0 auto;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding-inline: var(--s-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: var(--z-topbar);
}
.topbar-title { min-width: 0; flex: 1 1 auto; }
.topbar-crumbs {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-xxs);
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
}
.topbar-crumbs .sep { color: var(--border-strong); }
.topbar-page {
  font-size: var(--fs-h2);
  font-weight: var(--fw-semibold);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-actions { display: flex; align-items: center; gap: var(--s-2); flex: 0 0 auto; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--r-sm);
  display: grid;
  place-items: center;
  color: var(--text-2);
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
  position: relative;
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--bg-sunken); color: var(--text); }
.icon-btn:active { transform: scale(0.95); }
.icon-btn .dot {
  position: absolute;
  top: 7px;
  inset-inline-end: 7px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--coral-600);
  border: 2px solid var(--surface);
}

/* --- Page body ------------------------------------------------------------ */

.page-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--s-5) var(--s-5) var(--s-12);
}
.page-inner { max-width: var(--page-max); margin-inline: auto; }

.page-head {
  display: flex;
  align-items: flex-start;
  gap: var(--s-4);
  flex-wrap: wrap;
  margin-bottom: var(--s-4);
}
.page-head-main { flex: 1 1 320px; min-width: 0; }
.page-title { font-size: var(--fs-h1); font-weight: var(--fw-semibold); }
.page-sub { font-size: var(--fs-sm); color: var(--text-2); margin-top: 2px; }
.page-head-aside { display: flex; align-items: center; gap: var(--s-2); flex-wrap: wrap; }

/* Section heading inside a page */
.section-head {
  display: flex;
  align-items: baseline;
  gap: var(--s-3);
  margin: var(--s-6) 0 var(--s-3);
  flex-wrap: wrap;
}
.section-head:first-child { margin-top: 0; }
.section-title { font-size: var(--fs-h2); font-weight: var(--fw-semibold); }
.section-note { font-size: var(--fs-xs); color: var(--text-3); }

/* --- Grid ----------------------------------------------------------------- */

.grid { display: grid; gap: var(--s-4); }
.grid-kpi { grid-template-columns: repeat(auto-fit, minmax(212px, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2-1 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.grid-1-2 { grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); }
.stack { display: grid; gap: var(--s-4); }
.row { display: flex; align-items: center; gap: var(--s-2); flex-wrap: wrap; }
.row-end { display: flex; align-items: center; gap: var(--s-2); justify-content: flex-end; flex-wrap: wrap; }
.spacer { flex: 1 1 auto; }

@media (max-width: 1280px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 1080px) {
  .grid-2, .grid-2-1, .grid-1-2 { grid-template-columns: minmax(0, 1fr); }
}

/* --- Mobile bottom navigation -------------------------------------------- */

.bottom-nav { display: none; }
.mobile-scrim { display: none; }

@media (max-width: 768px) {
  .app-shell { grid-template-columns: 1fr; }
  .app-sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    width: 282px;
    max-width: 86vw;
    transform: translateX(-104%);
    transition: transform var(--t-base) var(--ease);
    z-index: calc(var(--z-drawer-scrim) + 1);
  }
  html[dir="rtl"] .app-sidebar { transform: translateX(104%); }
  .app-sidebar.is-open,
  html[dir="rtl"] .app-sidebar.is-open { transform: translateX(0); }
  .app-main { grid-column: 1; }

  .mobile-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 26, 43, 0.5);
    z-index: var(--z-drawer-scrim);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--t-base) var(--ease);
  }
  .mobile-scrim.is-open { opacity: 1; pointer-events: auto; }

  .app-topbar { height: 52px; padding-inline: var(--s-3); gap: var(--s-2); }
  .topbar-crumbs { display: none; }
  .topbar-page { font-size: var(--fs-h3); }

  .page-body {
    padding: var(--s-3) var(--s-3);
    padding-bottom: calc(var(--bottomnav-h) + 28px + env(safe-area-inset-bottom, 0px));
  }
  .page-title { font-size: var(--fs-h2); }

  .grid-kpi { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s-3); }
  .grid, .stack { gap: var(--s-3); }

  .bottom-nav {
    display: flex;
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: var(--z-bottomnav);
    box-shadow: 0 -2px 12px rgba(16, 26, 43, 0.07);
  }
  .bn-item {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: var(--text-3);
    font-size: var(--fs-xxs);
    font-weight: var(--fw-medium);
    min-height: 44px;
  }
  .bn-item.is-active { color: var(--navy-700); }
  .bn-item.is-active .bn-icon { color: var(--coral-600); }
  .bn-item:active { transform: scale(0.94); transition: transform var(--t-fast); }
  .bn-icon { width: 22px; height: 22px; display: grid; place-items: center; }
  .bn-label { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .bn-primary .bn-icon {
    width: 42px;
    height: 42px;
    margin-top: -14px;
    border-radius: 50%;
    background: var(--coral-600);
    color: #fff !important;
    box-shadow: 0 4px 14px rgba(229, 107, 93, 0.42);
  }

  .more-sheet {
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    z-index: var(--z-more-menu);
    background: var(--surface);
    border-radius: 20px 20px 0 0;
    max-height: 82vh;
    overflow-y: auto;
    transform: translateY(102%);
    transition: transform var(--t-base) var(--ease);
    padding-bottom: calc(var(--bottomnav-h) + 16px + env(safe-area-inset-bottom, 0px));
    box-shadow: var(--shadow-lg);
  }
  .more-sheet.is-open { transform: translateY(0); }
  .sheet-handle {
    width: 40px;
    height: 4px;
    border-radius: var(--r-pill);
    background: var(--border-strong);
    margin: 10px auto 2px;
  }
}

@media (max-width: 400px) {
  .grid-kpi { grid-template-columns: minmax(0, 1fr); }
}

@media (min-width: 769px) {
  .bottom-nav, .more-sheet, .mobile-scrim { display: none !important; }
}
