/* ── Accessibility ────────────────────────────────────────────────────────── */
.skip-link {
  position: absolute;
  top: -100%;
  left: var(--space-4);
  z-index: 9999;
  background: var(--color-primary);
  color: #fff;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  font-weight: 600;
  text-decoration: none;
  transition: top 0.1s;
}
.skip-link:focus { top: var(--space-2); }

/* ── App shell ────────────────────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-rows: 56px auto 1fr;
  grid-template-columns: 220px 1fr;
  grid-template-areas:
    "topbar  topbar"
    "banner  banner"
    "sidebar main";
  min-height: 100vh;
}

#attention-banner { grid-area: banner; }
#attention-banner:empty { display: none; }

/* ── Topbar ───────────────────────────────────────────────────────────────── */
#topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: 0 var(--space-6);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 100;
}

#topbar .brand {
  font-weight: 700;
  font-size: var(--text-lg);
  color: var(--color-primary);
  letter-spacing: -0.02em;
  text-decoration: none;
}

#topbar .spacer { flex: 1; }

#topbar .user-info {
  font-size: var(--text-sm);
  color: var(--color-muted);
}

/* notification bell */
#btn-notifications {
  position: relative;
  background: none;
  border: none;
  font-size: 1.25rem;
  color: var(--color-muted);
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  transition: color var(--transition);
}
#btn-notifications:hover { color: var(--color-text); }

#notif-badge {
  position: absolute;
  top: 2px; right: 2px;
  background: var(--color-danger);
  color: #fff;
  font-size: 0.6rem;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

#btn-logout {
  background: none;
  border: 1px solid var(--color-border);
  color: var(--color-muted);
  font-size: var(--text-sm);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
  transition: color var(--transition), border-color var(--transition);
}
#btn-logout:hover { color: var(--color-danger); border-color: var(--color-danger); }

/* ── Sidebar ──────────────────────────────────────────────────────────────── */
#sidebar {
  grid-area: sidebar;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: var(--space-6) 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  overflow-y: auto;
}

.nav-section {
  padding: var(--space-3) var(--space-4) var(--space-1);
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-muted);
}

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-4);
  color: var(--color-text);
  font-size: var(--text-sm);
  font-weight: 500;
  border-radius: 0;
  text-decoration: none;
  transition: background var(--transition), color var(--transition);
  border-left: 3px solid transparent;
}
.nav-link:hover {
  background: var(--color-subtle);
  text-decoration: none;
}
.nav-link.active {
  background: #eff6ff;
  color: var(--color-primary);
  border-left-color: var(--color-primary);
}

/* Unread-count pill, injected next to the Chat nav link by notifications.js
   (sourced from the same 60s notification poll — no separate fetch). */
.nav-badge {
  margin-left: auto;
  background: var(--color-danger);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* ── Main content ─────────────────────────────────────────────────────────── */
#main-content {
  grid-area: main;
  padding: var(--space-8);
  overflow-y: auto;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-6);
}

.page-header h1 { font-size: var(--text-xl); }

/* When the user is not logged in, hide the shell chrome */
body.guest #sidebar,
body.guest #topbar,
body.guest #attention-banner { display: none; }

body.guest #app {
  display: block;
}

body.guest #main-content {
  padding: 0;
}

/* ── Theme toggle button ─────────────────────────────────────────────────── */
#btn-theme {
  background: none;
  border: none;
  font-size: 1.15rem;
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
  line-height: 1;
}
#btn-theme:hover { background: var(--color-subtle); }

/* ── Dark theme layout overrides ─────────────────────────────────────────── */
[data-theme="dark"] .nav-link.active {
  background: rgba(96, 165, 250, .15);
  color: var(--color-primary);
  border-left-color: var(--color-primary);
}

/* ── Mobile (≤ 768 px) ────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  #app {
    /* minmax(0, 1fr), not bare 1fr: grid tracks default to min-width:auto,
       which refuses to shrink below the widest child's min-content size —
       e.g. the sidebar's nowrap nav-link strip — and forces the whole page
       to overflow horizontally instead of letting that child scroll on its
       own (it already has overflow-x:auto for exactly that purpose). */
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 56px auto auto 1fr;
    grid-template-areas:
      "topbar"
      "banner"
      "sidebar"
      "main";
  }

  #topbar {
    padding: 0 var(--space-3);
    gap: var(--space-2);
  }

  /* Name/role label is the least essential item in the row — drop it before
     the row is forced to overflow (icons + Sign out stay reachable). */
  #topbar .user-info {
    display: none;
  }

  #sidebar {
    border-right: none;
    border-bottom: 1px solid var(--color-border);
    flex-direction: row;
    flex-wrap: wrap;
    padding: var(--space-2) var(--space-2);
    gap: var(--space-1);
    overflow-x: auto;
    overflow-y: hidden;
  }

  .nav-section { display: none; }

  .nav-link {
    padding: var(--space-2) var(--space-3);
    border-left: none;
    border-bottom: 3px solid transparent;
    border-radius: var(--radius-sm);
    white-space: nowrap;
  }

  .nav-link.active {
    border-left-color: transparent;
    border-bottom-color: var(--color-primary);
  }

  #main-content {
    padding: var(--space-4);
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-3);
  }
}
