/* =============================================================================
   App shell layout: sidebar + topbar + content
   ========================================================================== */

.app-shell {
  display: grid;
  grid-template-columns: 248px 1fr;
  grid-template-rows: 56px 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  height: 100vh;
  overflow: hidden;
}

/* --- Sidebar ------------------------------------------------------------- */
.sidebar {
  grid-area: sidebar;
  display: flex;
  flex-direction: column;
  background: var(--bg-canvas-alt);
  border-right: 1px solid var(--border-subtle);
  padding: var(--space-4) var(--space-3);
  overflow-y: auto;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-2) var(--space-5);
  font-weight: 700;
  font-size: var(--text-md);
  letter-spacing: -0.01em;
}
.sidebar-brand .brand-mark {
  width: 28px; height: 28px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent-500), var(--violet-500));
  display: flex; align-items: center; justify-content: center;
  color: white;
  flex-shrink: 0;
}
.sidebar-brand .brand-mark svg { width: 16px; height: 16px; }

.sidebar-section { margin-bottom: var(--space-5); }
.sidebar-section-title {
  padding: 0 var(--space-2);
  margin-bottom: var(--space-2);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-2);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  color: var(--text-secondary);
  margin-bottom: 1px;
  position: relative;
  transition: background-color var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.nav-item svg { width: 17px; height: 17px; flex-shrink: 0; color: var(--text-tertiary); }
.nav-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.nav-item.is-active { background: var(--bg-selected); color: var(--text-primary); font-weight: 500; }
.nav-item.is-active svg { color: var(--accent-strong); }
.nav-item .nav-badge { margin-left: auto; }
.nav-item .folder-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-tertiary); flex-shrink: 0; }

.account-switcher {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  margin-bottom: var(--space-4);
  cursor: pointer;
  transition: border-color var(--duration-fast) var(--ease-out);
}
.account-switcher:hover { border-color: var(--border-strong); }
.account-switcher-info { min-width: 0; flex: 1; }
.account-switcher-name { font-size: var(--text-sm); font-weight: 500; }
.account-switcher-email { font-size: 11px; color: var(--text-tertiary); }

.sidebar-footer { margin-top: auto; padding-top: var(--space-4); border-top: 1px solid var(--border-subtle); }

/* --- Topbar --------------------------------------------------------------- */
.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-canvas);
  min-width: 0; /* lets .topbar-search shrink/truncate instead of forcing overflow */
}
/* Hidden on desktop — the sidebar is always visible there. Revealed at the
   ≤860px breakpoint below, where the sidebar becomes an off-canvas panel. */
.sidebar-toggle-btn { display: none; flex-shrink: 0; }
.topbar-search {
  flex: 1;
  min-width: 0;
  max-width: 480px;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: 34px;
  padding: 0 var(--space-3);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-tertiary);
  font-size: var(--text-sm);
  cursor: pointer;
}
.topbar-search svg { width: 15px; height: 15px; flex-shrink: 0; }
.topbar-search-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-search .kbd { margin-left: auto; flex-shrink: 0; }
.topbar-actions { display: flex; align-items: center; gap: var(--space-2); margin-left: auto; flex-shrink: 0; }

/* Off-canvas sidebar backdrop (mobile only — see the ≤860px breakpoint).
   pointer-events: none while closed is essential: this sits at inset:0
   over the whole page, and forgetting it here would silently block every
   click on the page even when fully transparent. */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(4, 4, 8, 0.5);
  z-index: calc(var(--z-sticky) - 1);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-base) var(--ease-out);
}
.sidebar-backdrop.is-open { opacity: 1; pointer-events: auto; }

/* --- Main content area ---------------------------------------------------- */
.main {
  grid-area: main;
  overflow-y: auto;
  background: var(--bg-canvas);
}
.main-inner { max-width: 1180px; margin: 0 auto; padding: var(--space-8) var(--space-6); }
.main-inner-wide { max-width: 1440px; margin: 0 auto; padding: var(--space-6); }

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}
.page-header h1 { margin-bottom: var(--space-1); }
.page-header-desc { color: var(--text-tertiary); font-size: var(--text-sm); }

.grid { display: grid; gap: var(--space-4); }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }
/* Grid items default to min-width: auto, meaning a track refuses to shrink
   below the intrinsic width of its content — a long unbroken string (a
   connection-test error, an activity description) or a chart canvas can
   force the whole row wider than the viewport regardless of
   grid-template-columns. This is the Grid equivalent of the flex
   min-height: 0 issue elsewhere in this file. */
.grid > * { min-width: 0; }

/* --- Mail split-pane (folders live in the persistent sidebar; list | reading) */
.mail-layout {
  grid-area: main;
  display: grid;
  grid-template-columns: minmax(340px, 420px) 1fr;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}
.mail-list-pane {
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: var(--bg-canvas);
}
.mail-reading-pane {
  position: relative; /* anchors the .pane-loader overlay */
  min-height: 0;
  overflow-y: auto;
  background: var(--bg-canvas);
}
/* Mobile-only "back to list" affordance (see mail.js + the ≤860px
   breakpoint below). Hidden on desktop where both panes show at once. */
.mail-back-btn {
  display: none;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-canvas);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: 500;
  position: sticky;
  top: 0;
  z-index: 1;
}
.mail-back-btn svg { width: 16px; height: 16px; }

/* --- Auth pages ------------------------------------------------------------- */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  background:
    radial-gradient(1200px 600px at 15% -10%, rgba(99, 102, 241, 0.16), transparent 60%),
    radial-gradient(900px 500px at 100% 110%, rgba(139, 92, 246, 0.12), transparent 60%),
    var(--bg-canvas);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  padding: var(--space-8);
  animation: fade-in var(--duration-slow) var(--ease-out);
}
.auth-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-6);
}
.auth-brand .brand-mark {
  width: 40px; height: 40px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent-500), var(--violet-500));
  display: flex; align-items: center; justify-content: center;
  color: white;
  box-shadow: var(--shadow-glow);
}
.auth-brand .brand-mark svg { width: 22px; height: 22px; }
.auth-footer-link { text-align: center; margin-top: var(--space-5); font-size: var(--text-sm); color: var(--text-tertiary); }
.auth-footer-link a { color: var(--accent-strong); font-weight: 500; }
.auth-footer-link a:hover { text-decoration: underline; }

/* =============================================================================
   Responsive
   Three breakpoints:
     1080px — tablet: narrow the mail list column, nothing else changes yet.
     860px  — small tablet / large phone: sidebar becomes an off-canvas
              panel, the mail split-pane becomes two full-width views.
     640px  — phone: stack two-column forms, collapse the topbar search
              label to icon-only, tighten spacing.
   Mobile-first purists would flip this (base styles for small screens,
   min-width media queries layering up) — this file is desktop-first
   because the product's primary surface (a dense mail client) is
   desktop-shaped, and retrofitting three narrower breakpoints on top
   is more tractable here than rebuilding the cascade direction.
   ========================================================================== */

@media (max-width: 1080px) {
  .mail-layout { grid-template-columns: minmax(300px, 360px) 1fr; }
}

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

  .sidebar-toggle-btn { display: inline-flex; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(300px, 84vw);
    transform: translateX(-100%);
    transition: transform var(--duration-base) var(--ease-out);
    z-index: var(--z-sticky);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.is-open { transform: translateX(0); }
  .sidebar-backdrop { display: block; }

  .topbar-search-label { display: none; }
  .topbar-search { max-width: none; flex: 0 0 auto; width: 36px; justify-content: center; padding: 0; }
  .topbar-search .kbd { display: none; }

  /* Two full-width views (list, reading pane) instead of a side-by-side
     split — toggled by `.is-reading` on #mail-layout (see mail JS). */
  .mail-layout { grid-template-columns: 1fr; }
  .mail-list-pane { display: flex; }
  .mail-reading-pane { display: none; }
  .mail-layout.is-reading .mail-list-pane { display: none; }
  .mail-layout.is-reading .mail-reading-pane { display: block; }
  .mail-back-btn { display: flex; }

  .grid-cols-2, .grid-cols-3, .grid-cols-4 {
    grid-template-columns: 1fr;
  }
  /* .dashboard-charts-row / .dashboard-lists-row are NOT overridden here on
     purpose — dashboard.css loads AFTER this file (see base.html's
     {% block extra_css %} placement), so an unconditional rule there would
     always win this tie regardless of viewport width and silently no-op
     this override. The responsive version lives in dashboard.css itself,
     after its base rule, to make load order irrelevant. */

  .main-inner, .main-inner-wide { padding: var(--space-5) var(--space-4); }
}

@media (max-width: 640px) {
  .field-row { flex-direction: column; gap: 0; }
  .field-row > .field.field-narrow { flex-basis: auto; max-width: none; }

  .page-header { flex-direction: column; align-items: stretch; }
  /* The action area (a single button, or a wrapper div holding several —
     e.g. the dashboard's "Open inbox" + "Manage accounts") goes full width
     and wraps its own children instead of forcing each button to 100%,
     which would fight a row of several buttons instead of stacking them. */
  .page-header > *:last-child { width: 100%; flex-wrap: wrap; }

  /* NB: .reading-pane-* / .attachment-list mobile overrides live in
     mail.css, not here — mail.css loads after this file (per-page, via
     {% block extra_css %}), so an override here would lose an
     equal-specificity tie against mail.css's base rules no matter what
     the media query says. See the note at the bottom of mail.css. */

  .modal, .modal.modal-lg { width: calc(100vw - 1.5rem); }
  .command-palette { top: 8%; width: calc(100vw - 1.5rem); }

  .auth-card { padding: var(--space-6) var(--space-5); }
}
