/* ==========================================================================
   Layout — App shell, sidebar, topbar, auth layout
   ========================================================================== */

/* ==========================================================================
   APP SHELL (authenticated)
   ========================================================================== */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  min-height: 100dvh;
  background: var(--surface-app);
}

@media (max-width: 1023.98px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "main";
  }
}

.app-sidebar {
  grid-area: sidebar;
  position: sticky;
  top: 0;
  height: 100dvh;
  z-index: var(--z-sidebar);
}

.app-topbar {
  grid-area: topbar;
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
}

.app-main {
  grid-area: main;
  min-width: 0; /* prevent overflow */
  padding: var(--page-py) var(--page-px) var(--sp-16);
  max-width: 100%;
}

.page-container {
  max-width: var(--content-max);
  margin: 0 auto;
}

/* Page header */
.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--sp-4);
  margin-bottom: var(--sp-6);
  padding-bottom: var(--sp-5);
  border-bottom: 1px solid var(--border-subtle);
}
.page-header__titles {
  min-width: 0;
}
.page-header__eyebrow {
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  color: var(--brand-600);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.25rem;
}
[data-theme="dark"] .page-header__eyebrow { color: var(--brand-300); }
.page-header__title {
  font-size: var(--fs-3xl);
  font-weight: var(--fw-bold);
  letter-spacing: var(--tracking-tight);
  line-height: 1.15;
  color: var(--text-primary);
  margin: 0;
}
.page-header__subtitle {
  font-size: var(--fs-md);
  color: var(--text-tertiary);
  margin-top: 0.375rem;
}
.page-header__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-2);
}

/* Section */
.section {
  margin-bottom: var(--sp-8);
}
.section__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}
.section__title {
  font-size: var(--fs-lg);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  letter-spacing: var(--tracking-snug);
}
.section__subtitle {
  font-size: var(--fs-sm);
  color: var(--text-tertiary);
  margin-top: 0.125rem;
}

/* ==========================================================================
   SIDEBAR
   ========================================================================== */
.sidebar {
  display: flex;
  flex-direction: column;
  width: var(--sidebar-w);
  height: 100dvh;
  background: var(--surface-1);
  border-right: 1px solid var(--border-subtle);
  overflow: hidden;
  transition: transform var(--dur-slow) var(--ease-out);
}

.sidebar__brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--sp-5);
  height: var(--topbar-h);
  border-bottom: 1px solid var(--border-subtle);
  text-decoration: none;
  color: var(--text-primary);
  flex-shrink: 0;
}
.sidebar__brand:hover { color: var(--text-primary); }

.sidebar__logo {
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--r-lg);
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
  flex-shrink: 0;
  box-shadow: none;
}
.sidebar__logo [data-lucide] { width: 1.25rem; height: 1.25rem; stroke-width: 2.25; }

.sidebar__brand-text {
  min-width: 0;
  line-height: 1.15;
}
.sidebar__brand-title {
  font-size: var(--fs-sm);
  font-weight: var(--fw-bold);
  letter-spacing: var(--tracking-snug);
  color: var(--text-primary);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar__brand-sub {
  font-size: var(--fs-xs);
  color: var(--text-tertiary);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar__nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-3) var(--sp-2_5);
  scrollbar-width: thin;
}
.sidebar__section {
  margin-bottom: var(--sp-4);
}
.sidebar__label {
  padding: var(--sp-2) var(--sp-3) var(--sp-1);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.sidebar__item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0.6rem 0.75rem;
  margin: 2px 0;
  border-radius: var(--r-lg);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  line-height: 1.25;
  position: relative;
  transition:
    background-color var(--dur-fast) var(--ease-out),
    color var(--dur-fast) var(--ease-out);
}
.sidebar__item [data-lucide] {
  width: 1.125rem;
  height: 1.125rem;
  color: var(--text-tertiary);
  flex-shrink: 0;
  transition: color var(--dur-fast) var(--ease-out);
}
.sidebar__item:hover {
  background: var(--state-hover-bg);
  color: var(--text-primary);
}
.sidebar__item:hover [data-lucide] { color: var(--text-secondary); }

.sidebar__item.is-active,
.sidebar__item[aria-current="page"] {
  background: var(--brand-surface);
  color: var(--brand-700);
  font-weight: var(--fw-semibold);
}
[data-theme="dark"] .sidebar__item.is-active,
[data-theme="dark"] .sidebar__item[aria-current="page"] {
  color: var(--brand-200);
}
.sidebar__item.is-active [data-lucide],
.sidebar__item[aria-current="page"] [data-lucide] {
  color: var(--brand-600);
}
[data-theme="dark"] .sidebar__item.is-active [data-lucide],
[data-theme="dark"] .sidebar__item[aria-current="page"] [data-lucide] {
  color: var(--brand-300);
}
.sidebar__item.is-active::before {
  content: "";
  position: absolute;
  left: -10px;
  top: 25%;
  bottom: 25%;
  width: 3px;
  background: var(--brand-600);
  border-radius: var(--r-full);
}

.sidebar__item-label {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar__badge {
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  padding: 0.1rem 0.4rem;
  border-radius: var(--r-full);
  background: var(--brand-surface);
  color: var(--brand-700);
}

.sidebar__footer {
  padding: var(--sp-3) var(--sp-2_5);
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.sidebar__user {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-lg);
  background: var(--surface-0);
  border: 1px solid var(--border-subtle);
}
.sidebar__user-info {
  min-width: 0;
  line-height: 1.2;
  flex: 1;
}
.sidebar__user-name {
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar__user-role {
  font-size: var(--fs-xs);
  color: var(--text-tertiary);
  display: block;
}

/* Sidebar mobile drawer */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--gray-950) 50%, transparent);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: var(--z-backdrop);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-slow) var(--ease-out);
}
.sidebar-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

@media (max-width: 1023.98px) {
  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    transform: translateX(-100%);
    width: min(80vw, 320px);
    box-shadow: var(--shadow-xl);
  }
  .app-sidebar.is-open {
    transform: translateX(0);
  }
  .sidebar { width: 100%; }
}

/* ==========================================================================
   TOPBAR
   ========================================================================== */
.topbar {
  height: var(--topbar-h);
  background: color-mix(in srgb, var(--surface-1) 85%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--page-px);
}

.topbar__toggle {
  display: inline-flex;
}

.topbar__title {
  font-size: var(--fs-md);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  margin: 0;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__search {
  flex: 1;
  max-width: 28rem;
  margin: 0 var(--sp-3);
  position: relative;
}
.topbar__search .form-control {
  padding-left: 2.5rem;
  background: var(--surface-0);
  border-color: var(--border-subtle);
}
.topbar__search .form-control:focus {
  background: var(--surface-1);
}
.topbar__search [data-lucide] {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  width: 1rem;
  height: 1rem;
  color: var(--text-muted);
  pointer-events: none;
}
.topbar__search kbd {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.7rem;
  pointer-events: none;
}
@media (max-width: 767.98px) {
  .topbar__search { display: none; }
}

.topbar__actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: var(--sp-1);
}

.topbar__divider {
  width: 1px;
  height: 1.5rem;
  background: var(--border-base);
  margin: 0 var(--sp-1);
}

.topbar-profile {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 0.3rem 0.4rem 0.3rem 0.625rem;
  border-radius: var(--r-full);
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  transition:
    background-color var(--dur-fast) var(--ease-out),
    border-color var(--dur-fast) var(--ease-out);
}
.topbar-profile:hover {
  background: var(--state-hover-bg);
  border-color: var(--border-subtle);
}
.topbar-profile__name {
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  line-height: 1.1;
}
.topbar-profile__role {
  font-size: var(--fs-xs);
  color: var(--text-tertiary);
  line-height: 1.1;
}
@media (max-width: 639.98px) {
  .topbar-profile__meta { display: none; }
}

.topbar-relative { position: relative; }

/* ==========================================================================
   AUTH LAYOUT — split panel
   ========================================================================== */
.auth-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 1fr;
  background: var(--surface-app);
}
@media (min-width: 1024px) {
  .auth-shell {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }
}

.auth-hero {
  display: none;
  position: relative;
  overflow: hidden;
  background: var(--gradient-brand);
  color: #ffffff;
  padding: var(--sp-12);
}
@media (min-width: 1024px) {
  .auth-hero { display: flex; flex-direction: column; }
}
.auth-hero::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--gradient-mesh);
  opacity: 0.9;
  pointer-events: none;
  mix-blend-mode: soft-light;
}
.auth-hero::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at top left, rgb(255 255 255 / 0.12), transparent 50%),
    radial-gradient(ellipse at bottom right, rgb(0 0 0 / 0.3), transparent 60%);
  pointer-events: none;
}
.auth-hero > * { position: relative; z-index: 1; }

.auth-hero__brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  font-weight: var(--fw-bold);
  font-size: var(--fs-lg);
}
.auth-hero__brand-mark {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: var(--r-lg);
  background: rgb(255 255 255 / 0.15);
  border: 1px solid rgb(255 255 255 / 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}
.auth-hero__brand-mark [data-lucide] {
  width: 1.375rem;
  height: 1.375rem;
}

.auth-hero__body {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  max-width: 30rem;
  margin-top: var(--sp-10);
}
.auth-hero__title {
  font-size: var(--fs-4xl);
  font-weight: var(--fw-bold);
  line-height: 1.1;
  letter-spacing: var(--tracking-tight);
  margin-bottom: var(--sp-5);
  color: #ffffff;
}
.auth-hero__description {
  font-size: var(--fs-lg);
  line-height: 1.6;
  color: color-mix(in srgb, #ffffff 88%, transparent);
  margin-bottom: var(--sp-8);
}
.auth-hero__features {
  display: grid;
  gap: var(--sp-3);
  list-style: none;
  padding: 0;
  margin: 0;
}
.auth-hero__feature {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: rgb(255 255 255 / 0.08);
  border: 1px solid rgb(255 255 255 / 0.15);
  border-radius: var(--r-lg);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  font-size: var(--fs-sm);
}
.auth-hero__feature-icon {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: var(--r-md);
  background: rgb(255 255 255 / 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.auth-hero__feature-icon [data-lucide] {
  width: 0.95rem;
  height: 0.95rem;
}

.auth-hero__footer {
  margin-top: auto;
  padding-top: var(--sp-8);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: var(--fs-sm);
  color: color-mix(in srgb, #ffffff 75%, transparent);
}

.auth-main {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: var(--sp-6) var(--sp-4);
  min-height: 100dvh;
  position: relative;
}
@media (min-width: 1024px) {
  .auth-main {
    padding: var(--sp-10) var(--sp-8);
  }
}

.auth-main__container {
  width: 100%;
  max-width: 26rem;
  margin: 0 auto;
}

.auth-main__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--sp-8);
}
.auth-main__brand-mobile {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-weight: var(--fw-bold);
  color: var(--text-primary);
}
.auth-main__brand-mobile .sidebar__logo {
  width: 2rem;
  height: 2rem;
}
@media (min-width: 1024px) {
  .auth-main__brand-mobile { visibility: hidden; }
}

.auth-main__title {
  font-size: var(--fs-3xl);
  font-weight: var(--fw-bold);
  letter-spacing: var(--tracking-tight);
  color: var(--text-primary);
  line-height: 1.15;
  margin-bottom: 0.5rem;
}
.auth-main__subtitle {
  font-size: var(--fs-md);
  color: var(--text-tertiary);
  margin-bottom: var(--sp-8);
}

.auth-main__footer {
  margin-top: var(--sp-10);
  text-align: center;
  font-size: var(--fs-sm);
  color: var(--text-tertiary);
}

/* ==========================================================================
   PAGE BACKGROUND (optional ambient gradient for special pages)
   ========================================================================== */
.page-ambient {
  position: relative;
}
.page-ambient::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--gradient-mesh);
  opacity: 0.55;
  pointer-events: none;
  z-index: 0;
  mask-image: linear-gradient(180deg, #000 0%, transparent 70%);
  -webkit-mask-image: linear-gradient(180deg, #000 0%, transparent 70%);
}
.page-ambient > * { position: relative; z-index: 1; }

/* ==========================================================================
   GRID HELPERS (layout-specific)
   ========================================================================== */
.grid {
  display: grid;
  gap: var(--sp-5);
}
.grid-1  { grid-template-columns: 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)); }

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

.grid-auto-fit {
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}
.grid-auto-fit-lg {
  grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr));
}

/* Dashboard-specific split */
.grid-dashboard-main {
  display: grid;
  gap: var(--sp-5);
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
}
@media (max-width: 1199.98px) {
  .grid-dashboard-main { grid-template-columns: 1fr; }
}

/* ==========================================================================
   CHART CONTAINER
   ========================================================================== */
.chart-container {
  position: relative;
  height: 320px;
  width: 100%;
}
.chart-container--sm { height: 200px; }
.chart-container--lg { height: 420px; }

/* ==========================================================================
   EMPTY STATE
   ========================================================================== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: var(--sp-10) var(--sp-6);
  gap: var(--sp-3);
}
.empty-state__illustration {
  width: 10rem;
  max-width: 100%;
  margin-bottom: var(--sp-2);
  opacity: 0.9;
}
.empty-state__title {
  font-size: var(--fs-lg);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
}
.empty-state__body {
  font-size: var(--fs-sm);
  color: var(--text-tertiary);
  max-width: 28rem;
}
.empty-state__action {
  margin-top: var(--sp-4);
}

/* ==========================================================================
   SIDEBAR COLLAPSED (desktop)
   ========================================================================== */
@media (min-width: 1024px) {
  .sidebar-collapsed {
    grid-template-columns: var(--sidebar-w-collapsed) 1fr;
  }
  .sidebar-collapsed .sidebar {
    width: var(--sidebar-w-collapsed);
  }
  .sidebar-collapsed .sidebar__brand-text,
  .sidebar-collapsed .sidebar__label,
  .sidebar-collapsed .sidebar__item-label,
  .sidebar-collapsed .sidebar__badge,
  .sidebar-collapsed .sidebar__user-info {
    display: none;
  }
  .sidebar-collapsed .sidebar__brand {
    justify-content: center;
    padding: 0;
  }
  .sidebar-collapsed .sidebar__item {
    justify-content: center;
    padding: 0.6rem;
  }
  .sidebar-collapsed .sidebar__item [data-lucide] {
    width: 1.25rem;
    height: 1.25rem;
  }
  .sidebar-collapsed .sidebar__user {
    justify-content: center;
    padding: var(--sp-2);
  }
  .sidebar-collapsed .sidebar__nav {
    padding: var(--sp-3) var(--sp-1);
  }
  .sidebar-collapsed .sidebar__footer {
    padding: var(--sp-3) var(--sp-1);
  }
}
