/* ============================================================
   School CRM — Application Shell Layout
   Sidebar + topbar + content grid. Collapses to an icon rail at
   1024px and an off-canvas drawer under 768px.
   ============================================================ */

:root {
  --sidebar-width: 248px;
  --sidebar-width-rail: 68px;
  --header-height: 60px;
}

/* ─── App Shell ──────────────────────────────────────────── */
.app-shell {
  display: flex;
  height: 100vh;
  background: var(--surface);
  overflow: hidden;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
  overflow: hidden;
}

.page-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-6);
}

/* ─── Sidebar ────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  height: 100vh;
  flex-shrink: 0;
  background: var(--surface-elevated);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  z-index: var(--z-sidebar);
  transition: width var(--duration-base, 200ms) var(--ease-out);
}

.sidebar-brand {
  padding: 18px var(--space-4) 14px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.sidebar-logo {
  width: 32px; height: 32px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--brand);
  color: var(--text-inverse);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-sm);
}

.sidebar-brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.sidebar-brand-name {
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-brand-sub {
  font-size: var(--text-xs);
  color: var(--text-muted);
  white-space: nowrap;
}

.sidebar-search {
  padding: var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
}

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

.nav-group { margin-bottom: var(--space-3); }

.nav-group-label {
  padding: var(--space-2) var(--space-3) 4px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: var(--tracking-wider, 0.06em);
  text-transform: uppercase;
  color: var(--text-muted);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  position: relative;
}

.nav-item:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 600;
}

.nav-item:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 1px;
}

.nav-icon { flex-shrink: 0; width: 18px; height: 18px; }
.nav-icon .ic { width: 18px; height: 18px; }

.nav-badge {
  margin-left: auto;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--radius-full);
  background: var(--danger-fill);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.user-card {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  cursor: pointer;
}

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

.user-info { min-width: 0; flex: 1; }

.user-name {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Collapsed icon rail */
.app-shell.is-collapsed .sidebar { width: var(--sidebar-width-rail); }
.app-shell.is-collapsed .sidebar-brand-text,
.app-shell.is-collapsed .sidebar-search,
.app-shell.is-collapsed .nav-group-label,
.app-shell.is-collapsed .nav-item span:not(.nav-badge),
.app-shell.is-collapsed .user-info { display: none; }
.app-shell.is-collapsed .nav-item { justify-content: center; }
.app-shell.is-collapsed .nav-badge {
  position: absolute; top: 2px; right: 2px; margin-left: 0;
}

/* ─── Topbar ─────────────────────────────────────────────── */
.topbar {
  height: var(--header-height);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-6);
  background: var(--surface-elevated);
  border-bottom: 1px solid var(--border-subtle);
  z-index: var(--z-header);
}

.topbar-left { display: flex; align-items: center; gap: var(--space-3); flex: 1; min-width: 0; }
.topbar-right { display: flex; align-items: center; gap: var(--space-2); }

.sidebar-toggle {
  display: none;
}

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

.page-title-group { min-width: 0; }

.page-title {
  font-family: var(--font-display);
  font-size: var(--text-2xl, 1.5rem);
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.page-subtitle {
  font-size: var(--text-sm);
  color: var(--text-muted);
  margin-top: var(--space-1);
}

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

/* ─── Toolbar / filter bar ───────────────────────────────── */
.toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}

.toolbar-left { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.toolbar-right { display: flex; align-items: center; gap: var(--space-2); margin-left: auto; }
.toolbar-spacer { flex: 1; }

.filter-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.filter-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 5px var(--space-3);
  border-radius: var(--radius-full);
  border: 1px solid var(--border-subtle);
  background: var(--surface-elevated);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
}

.filter-pill:hover { border-color: var(--border-strong); }
.filter-pill.active {
  background: var(--brand-soft);
  border-color: var(--brand);
  color: var(--brand);
}

.view-toggle { display: inline-flex; border: 1px solid var(--border-subtle); border-radius: var(--radius-md); overflow: hidden; }
.view-btn {
  padding: var(--space-1) var(--space-3);
  background: var(--surface-elevated);
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: var(--text-sm);
}
.view-btn.active { background: var(--brand-soft); color: var(--brand); }
.view-btn + .view-btn { border-left: 1px solid var(--border-subtle); }

/* ─── Grids ──────────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--space-5); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-5); }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-5); }
.grid-auto { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: var(--space-5); }

.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.detail-layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: var(--space-5);
  align-items: start;
}

/* Kanban board: see css/components.css (.kanban, .kanban-col, .kanban-card, ...) */

/* ─── Timeline ───────────────────────────────────────────── */
.timeline { display: flex; flex-direction: column; gap: 0; }
.timeline-item {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  border-left: 2px solid var(--border-subtle);
  padding-left: var(--space-4);
  margin-left: 6px;
  position: relative;
}
.timeline-dot {
  position: absolute;
  left: -7px; top: var(--space-3);
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--brand);
  border: 2px solid var(--surface-elevated);
}
.timeline-content { flex: 1; min-width: 0; }
.timeline-title { font-size: var(--text-sm); font-weight: 600; color: var(--text-primary); }
.timeline-time { font-size: var(--text-xs); color: var(--text-muted); margin-top: 2px; }

/* ─── Tabs (layout wrapper; visual in components.css) ────── */
.tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--border-subtle); margin-bottom: var(--space-5); }

/* ─── Responsive ─────────────────────────────────────────── */
/* Tablet: collapse to an icon rail. Bounded at 769px so these rules never
   compete with the mobile drawer below — otherwise the more specific
   `.app-shell:not(...)` selectors would win on specificity regardless of
   source order and the drawer would open at rail width. */
@media (min-width: 769px) and (max-width: 1024px) {
  .app-shell:not(.force-expanded) .sidebar { width: var(--sidebar-width-rail); }
  .app-shell:not(.force-expanded) .sidebar-brand-text,
  .app-shell:not(.force-expanded) .sidebar-search,
  .app-shell:not(.force-expanded) .nav-group-label,
  .app-shell:not(.force-expanded) .nav-item span:not(.nav-badge),
  .app-shell:not(.force-expanded) .user-info { display: none; }
  .app-shell:not(.force-expanded) .nav-item { justify-content: center; }
}

@media (max-width: 1024px) {
  .detail-layout { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .sidebar-toggle { display: inline-flex; }

  /* Off-canvas drawer at full label width — never a rail. */
  .app-shell .sidebar {
    position: fixed;
    left: 0; top: 0;
    width: 280px;
    transform: translateX(-100%);
    transition: transform var(--duration-base, 200ms) var(--ease-out);
    box-shadow: var(--shadow-lg);
  }
  .app-shell.is-mobile-open .sidebar { transform: translateX(0); }
  /* Restore each element's own display, not a blanket flex — `.user-info`
     and `.sidebar-brand-text` stack their children, so flattening them to a
     row runs the name and the role together. */
  .app-shell .sidebar-brand-text { display: flex; }
  .app-shell .user-info { display: block; }
  .app-shell .sidebar-search,
  .app-shell .nav-group-label { display: block; }
  .app-shell .nav-item span:not(.nav-badge) { display: inline; }
  .app-shell .nav-item { justify-content: flex-start; }

  /* Dim the page behind the open drawer so the tap-to-close target reads. */
  .app-shell.is-mobile-open .main-content::after {
    content: "";
    position: fixed; inset: 0;
    background: var(--surface-overlay);
    z-index: calc(var(--z-sidebar) - 1);
  }

  .page-body { padding: var(--space-4); }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .page-header { flex-direction: column; align-items: stretch; }
  .kanban-col { flex-basis: 260px; }
}

@media (max-width: 480px) {
  .stats-row { grid-template-columns: 1fr; }
}

/* ==========================================================================
   COMMAND PALETTE TRIGGER  (replaces the inline sidebar search box)
   ========================================================================== */

.palette-trigger {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  height: var(--control-h);
  padding: 0 var(--space-2) 0 var(--space-3);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-elevated);
  color: var(--text-muted);
  font-size: var(--text-sm);
  cursor: pointer;
  text-align: left;
}
.palette-trigger:hover { border-color: var(--border-strong); color: var(--text-secondary); }
.palette-trigger .ic { width: 15px; height: 15px; flex: none; }
.palette-trigger span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-trigger .palette-kbd { flex: none; }

.skip-link {
  position: absolute;
  left: -9999px;
  top: var(--space-2);
  z-index: var(--z-toast);
  padding: var(--space-2) var(--space-4);
  background: var(--brand);
  color: var(--on-brand);
  border-radius: var(--radius-md);
}
.skip-link:focus { left: var(--space-2); }

.user-card { position: relative; cursor: pointer; }
.user-card .user-caret { margin-left: auto; color: var(--text-faint); display: inline-flex; }
.user-card .user-caret .ic { width: 14px; height: 14px; }
.user-card:hover { background: var(--surface-hover); }
.user-card:focus-visible { outline: 2px solid var(--border-focus); outline-offset: 2px; }

/* The sidebar collapses to a rail on wide screens, a drawer on narrow ones. */
.app-shell.is-collapsed .palette-trigger span,
.app-shell.is-collapsed .palette-trigger .palette-kbd { display: none; }
