/* ==========================================================================
   Layout — app shell: sidebar, topbar, main area, page grids
   ========================================================================== */

.pw-app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100vh;
}

/* ---- Sidebar ------------------------------------------------------ */
.pw-sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  padding: var(--space-5) var(--space-4);
  background: var(--surface);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}

.pw-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: var(--space-1) var(--space-2);
  color: var(--ink);
  text-decoration: none;
}
.pw-brand:hover { color: var(--ink); text-decoration: none; }
.pw-brand__logo { height: 44px; width: auto; }
/* Logo artwork per theme: colour logo on light, inverse (white/sky) on dark. */
.pw-logo--inverse { display: none; }
[data-theme="dark"] .pw-logo--default { display: none; }
[data-theme="dark"] .pw-logo--inverse { display: block; }

.pw-study-switcher {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  text-align: left;
}
.pw-study-switcher:hover { border-color: var(--line-2); }
.pw-study-switcher__logo {
  width: 40px; height: 40px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: var(--accent-ink);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: var(--text-sm);
  flex-shrink: 0;
}
.pw-study-switcher__text { display: flex; flex-direction: column; gap: 2px; flex-grow: 1; min-width: 0; }
.pw-study-switcher__name { font-weight: 700; font-size: var(--text-base); }
.pw-study-switcher__org { font-size: var(--text-xs); color: var(--ink-3); }

.pw-nav { display: flex; flex-direction: column; gap: 2px; }
.pw-nav__link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--control-h);
  padding: 0 var(--space-3);
  border-radius: var(--radius-md);
  color: var(--ink-2);
  font-weight: 500;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.pw-nav__link:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.pw-nav__link.is-active { background: var(--brand-soft); color: var(--brand-text); font-weight: 600; }
.pw-nav__label { flex-grow: 1; }
.pw-nav__badge {
  min-width: 24px; height: 22px; padding: 0 7px;
  border-radius: var(--radius-pill);
  background: var(--crit-soft); color: var(--crit);
  font-size: var(--text-xs); font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
}

/* Participants + condition areas (collapsible sub-navigation) */
.pw-nav__group { display: flex; flex-direction: column; gap: 2px; }
.pw-nav__row { position: relative; display: flex; align-items: center; }
.pw-nav__row > .pw-nav__link { flex-grow: 1; padding-right: calc(var(--control-h) + 2px); }
.pw-nav__toggle {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--ink-3);
}
.pw-nav__toggle:hover { background: var(--surface); color: var(--ink); }
.pw-nav__toggle .icon { transition: transform var(--dur) var(--ease); }
.pw-nav__group.is-open .pw-nav__toggle .icon { transform: rotate(90deg); }

/* Nested links sit in a bordered, indented list (same look as the follow-on
   questions in the data-entry forms). The border lines up under the parent icon;
   the active item darkens its own stretch of the border. */
ul.pw-subnav[role] {
  display: none; list-style: none;
  margin: 2px 0 6px calc(var(--space-3) + 8px);
  padding: 0 0 0 var(--space-2);
  border-left: 3px solid var(--brand-soft);
}
.pw-nav__group.is-open ul.pw-subnav[role] { display: flex; flex-direction: column; gap: 1px; }
.pw-subnav__link {
  position: relative;
  display: block;
  padding: 7px var(--space-3) 7px var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-weight: 500;
  line-height: 1.3;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
/* Active/hover marker drawn over the list's border */
.pw-subnav__link::before {
  content: '';
  position: absolute; top: 4px; bottom: 4px;
  left: calc(-1 * var(--space-2) - 3px);
  width: 3px; border-radius: 3px;
  background: transparent;
  transition: background var(--dur-fast) var(--ease);
}
.pw-subnav__link:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.pw-subnav__link:hover::before { background: var(--line-2); }
.pw-subnav__link.is-active { color: var(--brand-text); font-weight: 600; background: var(--brand-soft); }
.pw-subnav__link.is-active::before { background: var(--brand); }
/* Action items (Add participant, Import) keep a small icon */
.pw-subnav__link--icon { display: flex; align-items: center; gap: 8px; padding-left: 10px; }
.pw-subnav__link--icon .icon { width: 16px; height: 16px; flex-shrink: 0; color: var(--ink-3); }
.pw-subnav__link--icon:hover .icon, .pw-subnav__link--icon.is-active .icon { color: var(--brand-text); }

.pw-sidebar__spacer { flex-grow: 1; }

/* Study switcher (badge, study code, dropdown) — hidden for now; it doesn't switch
   studies yet. Kept in the markup; see docs/TODO.md "Navigation". */
.pw-sidebar .pw-sidebar__study { display: none; }

.pw-user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border-top: 1px solid var(--line);
}
.pw-user__text { display: flex; flex-direction: column; gap: 2px; flex-grow: 1; min-width: 0; }
.pw-user__name { font-weight: 600; }

/* ---- Main column + topbar ---------------------------------------- */
.pw-main { display: flex; flex-direction: column; min-width: 0; }

.pw-topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-10);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.pw-topbar__search { width: min(520px, 100%); flex-shrink: 1; min-width: 0; }
.pw-topbar > .pw-btn { flex-shrink: 0; }
.pw-topbar__spacer { flex-grow: 1; }
.pw-topbar .pw-topbar__menu { display: none; }

.pw-content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  gap: var(--space-7);
  padding: var(--space-8) var(--space-10) var(--space-10);
  width: 100%;
  max-width: 1600px;
}

.pw-page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-6);
  flex-wrap: wrap;
}
.pw-page-head__text { display: flex; flex-direction: column; gap: 6px; }
.pw-page-head__lede { font-size: var(--text-lg); color: var(--ink-2); }
.pw-page-head__lede strong { color: var(--ink); }
.pw-page-head__actions { display: flex; gap: var(--space-2); flex-wrap: wrap; }
.pw-page-head__count { font-size: var(--text-2xl); color: var(--ink-3); font-weight: 500; margin-left: var(--space-3); }

.pw-back { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; align-self: flex-start; }

.pw-breadcrumb { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-md); color: var(--ink-3); }
.pw-breadcrumb a { font-weight: 600; }

/* ---- Grids -------------------------------------------------------- */
.pw-grid { display: grid; gap: var(--space-4); }
.pw-grid--4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.pw-grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
.pw-grid--main-side { grid-template-columns: minmax(0, 1.75fr) minmax(0, 1fr); gap: var(--space-6); align-items: start; }
.pw-grid--form-side { grid-template-columns: minmax(0, 1fr) 380px; gap: var(--space-6); align-items: start; }
.pw-grid--profile { grid-template-columns: 220px minmax(0, 1fr) 300px; gap: var(--space-6); align-items: start; }
.pw-col { display: flex; flex-direction: column; gap: var(--space-6); min-width: 0; }

/* ---- Responsive --------------------------------------------------- */
@media (max-width: 1280px) {
  .pw-grid--profile { grid-template-columns: 200px minmax(0, 1fr); }
  .pw-grid--profile > :last-child { grid-column: 1 / -1; }
}

@media (max-width: 1100px) {
  .pw-grid--4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .pw-grid--main-side,
  .pw-grid--form-side { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 900px) {
  .pw-app { grid-template-columns: minmax(0, 1fr); }
  .pw-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-w);
    z-index: 50;
    transform: translateX(-100%);
    transition: transform var(--dur) var(--ease);
    box-shadow: var(--shadow-lg);
  }
  .is-nav-open .pw-sidebar { transform: none; }
  .pw-topbar { padding: 0 var(--space-4); }
  .pw-topbar .pw-topbar__menu { display: inline-flex; }
  .pw-content { padding: var(--space-6) var(--space-4) var(--space-8); }
  .pw-grid--profile { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 600px) {
  .pw-grid--4, .pw-grid--2 { grid-template-columns: minmax(0, 1fr); }
  h1 { font-size: var(--text-3xl); }
  .pw-topbar__search .pw-kbd { display: none; }
  .pw-topbar .pw-topbar__cta { width: var(--control-h); padding: 0; }
  .pw-topbar__cta-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}

.pw-nav__link--button { width: 100%; border: 0; background: transparent; text-align: left; font: inherit; font-weight: 500; }

/* Urgent (break-glass) changes awaiting review — shown to admins and super admins */
.pw-urgent {
  display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap;
  margin: var(--space-3) var(--space-8) 0; padding: 10px var(--space-4);
  border-radius: var(--radius-md); background: var(--crit-soft); color: var(--crit); font-size: var(--text-sm);
}
.pw-urgent__link { margin-left: auto; font-weight: 600; color: var(--crit); }
@media (max-width: 900px) { .pw-urgent { margin: var(--space-3) var(--space-4) 0; } }
