/* ==========================================================================
   Layout — sidebar, main shell, page header.
   ========================================================================== */

/* --- App shell --- */
.layout {
  display: flex;
  min-height: 100vh;
}

.layout__main {
  flex: 1;
  min-width: 0;
  margin-left: 240px;
  padding: var(--space-8) var(--space-6);
}

.layout__container {
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
}

/* --- Sidebar --- */
.sidebar {
  width: 240px;
  height: 100vh;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: var(--space-5) var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
}

.sidebar__brand {
  font-size: 18px;
  font-weight: 600;
  color: var(--color-text);
  padding: 6px var(--space-3);
  margin-bottom: var(--space-4);
  letter-spacing: -0.01em;
  text-decoration: none;
}
.sidebar__brand:hover { color: var(--color-text); }

.sidebar__nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar__nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 7px var(--space-3);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  text-decoration: none;
  border-radius: var(--radius-sm);
  transition: background-color 100ms ease-out, color 100ms ease-out;
}

.sidebar__nav-item:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}

.sidebar__nav-item--active {
  background: var(--color-surface-2);
  color: var(--color-text);
  font-weight: 500;
}

.sidebar__spacer { flex: 1; }

.sidebar__user {
  position: relative;
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-3);
  margin-top: var(--space-3);
}

.sidebar__user-btn {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: 6px;
  border-radius: var(--radius-sm);
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--color-text);
  text-align: left;
  min-width: 0;
}
.sidebar__user-btn:hover { background: var(--color-surface-2); }

.sidebar__avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  color: var(--color-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  flex-shrink: 0;
  font-family: var(--font-mono);
}

.sidebar__user-email {
  flex: 1;
  font-size: var(--text-sm);
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.sidebar__user-chevron {
  color: var(--color-text-subtle);
  flex-shrink: 0;
}

.sidebar__user-menu {
  position: absolute;
  left: var(--space-3);
  right: var(--space-3);
  bottom: calc(100% + 6px);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: 4px;
  display: none;
  z-index: 50;
}
.sidebar__user-menu[data-open="true"] { display: block; }

.sidebar__user-menu-section {
  padding: 6px 10px 4px;
  font-size: 10px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-subtle);
}

.sidebar__user-menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  width: 100%;
  padding: 6px 10px;
  font-size: var(--text-sm);
  color: var(--color-text);
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  text-decoration: none;
}
.sidebar__user-menu-item:hover { background: var(--color-surface-2); }

.sidebar__user-menu-item--active::after {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-accent);
  flex-shrink: 0;
}

/* --- Page header --- */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-8);
}

.page-header__titles {
  flex: 1;
  min-width: 0;
}

.page-header__breadcrumb {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.page-header__breadcrumb a {
  color: var(--color-text-muted);
  text-decoration: none;
}
.page-header__breadcrumb a:hover {
  color: var(--color-text);
  text-decoration: underline;
}
.page-header__breadcrumb-sep {
  color: var(--color-text-subtle);
  user-select: none;
}

.page-header__title {
  font-size: var(--text-xl);
  font-weight: 600;
  line-height: 1.2;
  color: var(--color-text);
  margin-bottom: var(--space-2);
  letter-spacing: -0.01em;
  word-break: break-word;
}

.page-header__title--mono {
  font-family: var(--font-mono);
  font-weight: 500;
  letter-spacing: 0;
}

.page-header__subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  line-height: 1.55;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.page-header__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

/* --- Section header (inside main content) --- */
.section-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-3);
}
.section-header__title {
  font-size: var(--text-md);
  font-weight: 500;
  color: var(--color-text);
}
.section-header__description {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin-top: 2px;
}

/* --- Flash messages inside main area --- */
.layout__main .flash {
  margin-bottom: var(--space-5);
}

/* --- Card grid utility --- */
/* For pairing compact reference cards side-by-side. Stacks on narrow viewports.
   Use for key/value cards that are similar in height and purpose (e.g.
   Connection + SMTP credentials). Don't use for cards containing tables,
   long lists, or primary actions. */
.card-grid {
  display: grid;
  gap: var(--space-6);
}

.card-grid--2 {
  grid-template-columns: 1fr 1fr;
}

.card-grid--3 {
  grid-template-columns: repeat(3, 1fr);
}

@media (max-width: 900px) {
  .card-grid--2,
  .card-grid--3 {
    grid-template-columns: 1fr;
  }
}

/* =====================================================================
   Mobile topbar + off-canvas sidebar.

   Above 1024px the sidebar stays pinned (unchanged). Below 1024px the
   sidebar slides off-screen and the top of the page gets a compact topbar
   with a hamburger + the wordmark. Tapping the hamburger toggles
   `.sidebar--open` on the sidebar AND shows a dark scrim that closes the
   drawer on tap-outside.

   Applies to every authenticated page, so navigation still works on
   mobile even though only the dashboard is designed for mobile primary.
   Other pages remain desktop-first but stay usable.
   ===================================================================== */

/* Topbar is always in the DOM — CSS hides it above breakpoint. */
.mobile-topbar {
  display: none;
}

.mobile-topbar__brand {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--color-text);
  text-decoration: none;
}

.mobile-topbar__hamburger {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  cursor: pointer;
}
.mobile-topbar__hamburger:hover { background: var(--color-surface-2); }

.sidebar-scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 9;
}
.sidebar-scrim[data-open="true"] {
  display: block;
}

@media (max-width: 1023px) {
  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg);
    position: sticky;
    top: 0;
    z-index: 8;
  }

  .sidebar {
    transform: translateX(-100%);
    transition: transform 200ms ease-out;
    z-index: 12;
    box-shadow: none;
  }
  .sidebar--open {
    transform: translateX(0);
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.2);
  }

  .layout__main {
    margin-left: 0;
    /* Tighter padding on mobile so content has room. */
    padding: var(--space-5) var(--space-4);
  }
}

/* Tablet reflow — sidebar stays fixed but content gets a bit tighter. */
@media (max-width: 1200px) and (min-width: 1024px) {
  .layout__main {
    padding: var(--space-6) var(--space-5);
  }
}

/* Dashboard filter bar stacks on mobile. */
@media (max-width: 640px) {
  .dashboard-filters {
    flex-direction: column;
    align-items: stretch;
  }
  .dashboard-filters .select {
    width: 100%;
  }
  .dashboard-filters__right {
    margin-left: 0;
    justify-content: space-between;
  }
  .chart-container {
    height: 240px;
    padding: var(--space-3);
  }
  .metric__value {
    font-size: 24px;
  }
}
