/*
 * WPU Knowledge Infrastructure Platform V5.9
 * Final systemwide UI/UX refinement authority.
 * Loaded after legacy/versioned visual layers so shared fixes remain centralized.
 */
:root {
  --wpu-ui-primary: var(--wpu-primary, var(--rp-primary, #0b4f9c));
  --wpu-ui-primary-strong: color-mix(in srgb, var(--wpu-ui-primary) 86%, #000 14%);
  --wpu-ui-primary-soft: color-mix(in srgb, var(--wpu-ui-primary) 9%, #fff 91%);
  --wpu-ui-accent: var(--wpu-accent, #d3a514);
  --wpu-ui-canvas: #f5f7fa;
  --wpu-ui-surface: #ffffff;
  --wpu-ui-surface-subtle: #f8fafc;
  --wpu-ui-surface-muted: #eef2f6;
  --wpu-ui-border: #dbe2ea;
  --wpu-ui-border-strong: #c7d0da;
  --wpu-ui-text: #172033;
  --wpu-ui-text-muted: #617083;
  --wpu-ui-text-soft: #7c8998;
  --wpu-ui-success: #197447;
  --wpu-ui-warning: #9b6500;
  --wpu-ui-danger: #b42318;
  --wpu-ui-info: #1769aa;
  --wpu-ui-radius-control: 8px;
  --wpu-ui-radius-card: 12px;
  --wpu-ui-radius-dialog: 14px;
  --wpu-ui-shadow-soft: 0 1px 2px rgba(16,24,40,.04), 0 8px 24px rgba(16,24,40,.045);
  --wpu-ui-shadow-float: 0 16px 44px rgba(16,24,40,.14);
  --wpu-ui-control-h: 38px;
  --wpu-ui-content: 1440px;
  --wpu-ui-text-content: 920px;
  --wpu-ui-space-1: 4px;
  --wpu-ui-space-2: 8px;
  --wpu-ui-space-3: 12px;
  --wpu-ui-space-4: 16px;
  --wpu-ui-space-5: 20px;
  --wpu-ui-space-6: 24px;
  --wpu-ui-space-8: 32px;
  --wpu-ui-z-sticky: 1020;
  --wpu-ui-z-sidebar: 1030;
  --wpu-ui-z-dropdown: 1050;
  --wpu-ui-z-offcanvas: 1060;
  --wpu-ui-z-modal: 1080;
  --wpu-ui-z-toast: 1090;
}

html {
  text-rendering: optimizeLegibility;
  -webkit-text-size-adjust: 100%;
}

body.wpu62-admin-body,
body.discovery-body,
body.researcher-portal-body,
body.wpu-library-public,
body.wpu-auth-body,
body.wpu-setup-page,
body.wpu-install-body,
body.wpu-legal-body {
  color: var(--wpu-ui-text);
  font-family: "Segoe UI", Inter, system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.48;
  background: var(--wpu-ui-canvas);
}

body :where(h1,h2,h3,h4,h5,h6) {
  color: var(--wpu-ui-text);
  letter-spacing: -.012em;
}
body :where(h1) { font-size: clamp(1.55rem, 2vw, 1.95rem); line-height: 1.18; }
body :where(h2) { font-size: clamp(1.12rem, 1.45vw, 1.38rem); line-height: 1.25; }
body :where(h3) { font-size: 1.03rem; line-height: 1.3; }
body :where(p,li,td,th,label,small,span,a,button,input,select,textarea) { min-width: 0; }
body :where(p,li,td,th,.text-break,.card-title,.page-title,.resource-title,.record-title,.rp-project-title) {
  overflow-wrap: anywhere;
}

body :where(a) { text-underline-offset: 2px; }
body :where(a,button,input,select,textarea,[tabindex]):focus-visible {
  outline: 3px solid color-mix(in srgb, var(--wpu-ui-primary) 32%, transparent);
  outline-offset: 2px;
}

/* Shared content geometry */
.wpu62-admin-body .content-area,
.researcher-portal-body .rp-main,
.discovery-body main,
.wpu-library-public main {
  min-width: 0;
}
.wpu62-admin-body .content-area { padding: clamp(18px, 2vw, 28px); }
.researcher-portal-body .rp-main,
.discovery-body main { width: min(100%, var(--wpu-ui-content)); margin-inline: auto; }
.researcher-portal-body .rp-main { padding: clamp(18px, 2.1vw, 28px) clamp(14px, 2.4vw, 28px) 40px; }

/* Page headers */
body :where(.page-header,.admin-page-header,.rp-page-header,.wpu-page-header,.library-page-header,.section-page-header,.page-title-row) {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--wpu-ui-space-4);
  flex-wrap: wrap;
  min-width: 0;
  margin-bottom: var(--wpu-ui-space-5);
}
body :where(.page-header,.admin-page-header,.rp-page-header,.wpu-page-header,.library-page-header) > :first-child {
  flex: 1 1 420px;
  min-width: 0;
}
body :where(.page-header-actions,.header-actions,.page-actions,.rp-page-actions,.command-actions) {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: var(--wpu-ui-space-2);
  min-width: 0;
}
body :where(.page-subtitle,.page-description,.rp-page-subtitle,.section-description,.help-text) {
  color: var(--wpu-ui-text-muted);
  max-width: 78ch;
  margin-bottom: 0;
}

/* Cards and panels */
body :where(.card,.panel,.surface,.rp-card,.wpu-card,.library-card,.admin-card,.metric-card,.stat-card,.kpi-card) {
  border: 1px solid var(--wpu-ui-border);
  border-radius: var(--wpu-ui-radius-card);
  background: var(--wpu-ui-surface);
  box-shadow: none;
  min-width: 0;
}
body :where(.card,.panel,.rp-card,.wpu-card,.library-card,.admin-card) :where(.card-header,.panel-header,.rp-card-header) {
  border-bottom-color: var(--wpu-ui-border);
  background: var(--wpu-ui-surface-subtle);
}
body :where(.metric-card,.stat-card,.kpi-card) { padding: var(--wpu-ui-space-4); }
body :where(.metric-card,.stat-card,.kpi-card) :where(.value,.metric-value,.stat-value,.kpi-value) {
  font-size: clamp(1.35rem, 2.1vw, 1.85rem);
  line-height: 1.08;
  font-weight: 700;
  letter-spacing: -.025em;
}
body :where(.metric-card,.stat-card,.kpi-card) :where(.label,.metric-label,.stat-label,.kpi-label) {
  color: var(--wpu-ui-text-muted);
  font-size: .78rem;
  font-weight: 600;
}

/* Buttons */
body :where(.btn,.rp-btn-primary,.wpu-btn,button:not(.dropdown-toggle):not(.accordion-button):not(.btn-close)) {
  border-radius: var(--wpu-ui-radius-control);
}
body :where(.btn) {
  min-height: var(--wpu-ui-control-h);
  padding: .45rem .82rem;
  font-weight: 600;
  line-height: 1.15;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  white-space: normal;
}
body :where(.btn-sm) { min-height: 32px; padding: .32rem .62rem; font-size: .82rem; }
body :where(.btn-primary,.rp-btn-primary) {
  background-color: var(--wpu-ui-primary);
  border-color: var(--wpu-ui-primary);
}
body :where(.btn-primary,.rp-btn-primary):hover { background-color: var(--wpu-ui-primary-strong); border-color: var(--wpu-ui-primary-strong); }
body :where(.btn-outline-primary) { color: var(--wpu-ui-primary); border-color: color-mix(in srgb, var(--wpu-ui-primary) 45%, var(--wpu-ui-border)); }
body :where(.btn-outline-primary):hover { color: #fff; background: var(--wpu-ui-primary); border-color: var(--wpu-ui-primary); }
body :where(.btn-link) { min-height: auto; }
body :where(.btn-danger,.btn-outline-danger) { --bs-btn-border-radius: var(--wpu-ui-radius-control); }
body :where(.btn i,.btn .bi,button i,button .bi) { flex: 0 0 auto; line-height: 1; }

/* Forms */
body :where(.form-control,.form-select,.input-group-text,.form-check-input) {
  border-color: var(--wpu-ui-border-strong);
  box-shadow: none;
}
body :where(.form-control,.form-select,.input-group-text) {
  border-radius: var(--wpu-ui-radius-control);
  min-height: var(--wpu-ui-control-h);
  font-size: .9rem;
}
body :where(textarea.form-control) { min-height: 96px; resize: vertical; }
body :where(.form-control,.form-select):focus {
  border-color: color-mix(in srgb, var(--wpu-ui-primary) 64%, #fff);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--wpu-ui-primary) 13%, transparent);
}
body :where(.form-label,label) { color: var(--wpu-ui-text); font-weight: 600; }
body :where(.form-text,.invalid-feedback,.valid-feedback) { font-size: .8rem; }
body :where(.input-group) { min-width: 0; }
body :where(.input-group > .form-control,.input-group > .form-select) { min-width: 0; }
body input[type="file"] { max-width: 100%; }

/* Search/filter/command bars */
body :where(.toolbar,.command-bar,.filter-bar,.search-bar,.search-toolbar,.filters,.rp-toolbar,.wpu-toolbar) {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--wpu-ui-space-2);
  min-width: 0;
}
body :where(.toolbar,.command-bar,.filter-bar,.search-toolbar,.rp-toolbar,.wpu-toolbar) :where(.form-control,.form-select) {
  width: auto;
  min-width: min(220px, 100%);
  flex: 1 1 220px;
}

/* Tables: local overflow only */
body :where(.table-responsive,.table-scroll,.wpu-table-wrap,.wpu-ui-table-scroll) {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--wpu-ui-radius-card);
}
body :where(.table) {
  --bs-table-bg: transparent;
  --bs-table-striped-bg: #f8fafc;
  --bs-table-hover-bg: #f5f8fb;
  margin-bottom: 0;
  color: var(--wpu-ui-text);
  font-size: .86rem;
  vertical-align: middle;
}
body :where(.table > :not(caption) > * > *) { padding: .62rem .72rem; border-bottom-color: var(--wpu-ui-border); }
body :where(.table thead th) {
  color: #475467;
  background: #f7f9fc;
  font-size: .76rem;
  font-weight: 700;
  text-transform: none;
  letter-spacing: .01em;
  border-bottom-width: 1px;
}
body :where(.table td.actions,.table th.actions,.table .table-actions,.table .actions-cell) {
  white-space: nowrap;
  width: 1%;
}
body :where(.table td code,.table .record-id,.table .source-id) { font-size: .78rem; }
.wpu-ui-table-scroll[data-wpu-ui-scrollable="1"] { border: 1px solid var(--wpu-ui-border); background: var(--wpu-ui-surface); }
.wpu-ui-table-scroll[data-wpu-ui-scrollable="1"] > table { min-width: max-content; width: 100%; }

/* Badges/status */
body :where(.badge,.status-badge,.status-pill,.chip,.tag,.rp-status) {
  border-radius: 999px;
  font-size: .74rem;
  line-height: 1.15;
  font-weight: 650;
  padding: .31rem .54rem;
  vertical-align: middle;
}
body :where(.badge.bg-success,.status-live,.status-approved,.status-active) { background-color: color-mix(in srgb, var(--wpu-ui-success) 12%, #fff) !important; color: var(--wpu-ui-success) !important; }
body :where(.badge.bg-warning,.status-warning,.status-pending,.status-candidate) { background-color: color-mix(in srgb, var(--wpu-ui-warning) 12%, #fff) !important; color: #795000 !important; }
body :where(.badge.bg-danger,.status-danger,.status-failed) { background-color: color-mix(in srgb, var(--wpu-ui-danger) 10%, #fff) !important; color: var(--wpu-ui-danger) !important; }
body :where(.badge.bg-info,.status-info,.status-discovery) { background-color: color-mix(in srgb, var(--wpu-ui-info) 10%, #fff) !important; color: var(--wpu-ui-info) !important; }

/* Alerts */
body :where(.alert) { border-radius: 10px; border-width: 1px; padding: .78rem .95rem; }
body :where(.alert-info) { background: #f1f7fd; border-color: #cfe2f4; color: #20496b; }
body :where(.alert-success) { background: #f0f8f4; border-color: #cce8d8; color: #225c3d; }
body :where(.alert-warning) { background: #fff8e8; border-color: #eddcaf; color: #684d09; }
body :where(.alert-danger) { background: #fff3f2; border-color: #f1cbc7; color: #842d28; }

/* Dropdowns / popovers */
body :where(.dropdown-menu,.popover,.wpu-v3-popover) {
  max-width: min(360px, calc(100vw - 24px));
  border: 1px solid var(--wpu-ui-border);
  border-radius: 10px;
  box-shadow: var(--wpu-ui-shadow-float);
}
body :where(.dropdown-item) { white-space: normal; overflow-wrap: anywhere; }

/* Modals/offcanvas */
body :where(.modal-dialog) { max-width: min(var(--bs-modal-width, 720px), calc(100vw - 24px)); margin: 12px auto; }
body :where(.modal-content) { border: 1px solid var(--wpu-ui-border); border-radius: var(--wpu-ui-radius-dialog); box-shadow: var(--wpu-ui-shadow-float); max-height: calc(100dvh - 24px); overflow: hidden; }
body :where(.modal-body) { overflow-y: auto; overscroll-behavior: contain; }
body :where(.modal-header,.modal-footer) { flex: 0 0 auto; border-color: var(--wpu-ui-border); }
body :where(.offcanvas) { max-width: min(92vw, 360px); }

/* Pagination / tabs */
body :where(.pagination) { flex-wrap: wrap; gap: 3px; }
body :where(.page-link) { color: var(--wpu-ui-primary); border-color: var(--wpu-ui-border); border-radius: 7px; min-width: 34px; text-align: center; }
body :where(.nav-tabs) { border-bottom-color: var(--wpu-ui-border); gap: 2px; }
body :where(.nav-tabs .nav-link) { color: var(--wpu-ui-text-muted); border-radius: 8px 8px 0 0; font-weight: 600; }
body :where(.nav-tabs .nav-link.active) { color: var(--wpu-ui-primary); border-color: var(--wpu-ui-border) var(--wpu-ui-border) var(--wpu-ui-surface); }

/* Empty states */
body :where(.empty-state,.rp-empty-state,.wpu-empty-state,.library-empty-state) {
  text-align: center;
  padding: clamp(26px, 4vw, 44px) 18px;
  color: var(--wpu-ui-text-muted);
  background: var(--wpu-ui-surface-subtle);
  border: 1px dashed var(--wpu-ui-border-strong);
  border-radius: var(--wpu-ui-radius-card);
}
body :where(.empty-state,.rp-empty-state,.wpu-empty-state,.library-empty-state) :where(i,.bi) { font-size: clamp(1.6rem, 3vw, 2.3rem); color: var(--wpu-ui-text-soft); }

/* Admin shell */
.wpu62-admin-body .app-shell { min-width: 0; background: var(--wpu-ui-canvas); }
.wpu62-admin-body .sidebar.wpu365-sidebar {
  background: #fff;
  border-right: 1px solid var(--wpu-ui-border);
  box-shadow: none;
}
.wpu62-admin-body .wpu365-brand-row { border-bottom-color: var(--wpu-ui-border); }
.wpu62-admin-body .wpu365-nav-group > summary { border-radius: 8px; }
.wpu62-admin-body .wpu365-nav-link { border-radius: 7px; min-height: 36px; gap: 10px; }
.wpu62-admin-body .wpu365-nav-link.is-active {
  color: var(--wpu-ui-primary);
  background: var(--wpu-ui-primary-soft);
  box-shadow: inset 3px 0 0 var(--wpu-ui-accent);
}
.wpu62-admin-body .main-wrap { min-width: 0; }
.wpu62-admin-body .admin-app-footer { border-top: 1px solid var(--wpu-ui-border); background: var(--wpu-ui-surface); }
.wpu62-admin-body :where(.content-area > .container,.content-area > .container-fluid) { max-width: none; }

/* Public / Research Hub */
.discovery-body { background: #f7f9fb; }
.discovery-body :where(.container,.container-xl,.container-xxl) { min-width: 0; }
.discovery-body :where(.research-card,.resource-card,.result-card,.tool-card,.database-card) {
  border: 1px solid var(--wpu-ui-border);
  border-radius: var(--wpu-ui-radius-card);
  box-shadow: none;
  background: var(--wpu-ui-surface);
}
.discovery-body :where(.wpu-unified-site-header,.public-header,.site-header) { box-shadow: 0 1px 0 rgba(16,24,40,.08); }
.discovery-body :where(.search-results,.results-grid,.resource-grid,.tool-grid) > * { min-width: 0; }

/* Researcher productivity shell */
.researcher-portal-body { background: #f6f8fb; }
.researcher-portal-body .rp-topbar { box-shadow: 0 1px 0 rgba(16,24,40,.08); }
.researcher-portal-body .rp-desktop-nav a,
.researcher-portal-body .rp-mobile-nav a { border-radius: 7px; }
.researcher-portal-body .rp-desktop-nav a.is-current { background: color-mix(in srgb, #fff 13%, transparent); }
.researcher-portal-body :where(.rp-section,.rp-panel,.rp-workspace-panel) { min-width: 0; }
.researcher-portal-body :where(.rp-auth-card,.rp-form-card) { max-width: 560px; margin-inline: auto; }
.researcher-portal-body :where(.rp-project-grid,.rp-card-grid,.rp-output-grid,.rp-library-grid) { align-items: stretch; }

/* Library */
.wpu-library-public { background: #f7f9fb; }
.wpu-library-public .library-shell { width: min(100% - 28px, var(--wpu-ui-content)); margin-inline: auto; min-width: 0; }
.wpu-library-public :where(.wpu-v3-header,.library-site-header) { box-shadow: 0 1px 0 rgba(16,24,40,.08); }
.wpu-library-public :where(.wpu-v3-card,.library-result,.catalog-card,.collection-card,.database-card) {
  border-color: var(--wpu-ui-border);
  border-radius: var(--wpu-ui-radius-card);
  box-shadow: none;
}
.wpu-library-public .wpu-v3-footer { border-top: 1px solid var(--wpu-ui-border); }

/* Auth / installer */
.wpu-auth-body,
.wpu-setup-page,
.wpu-install-body {
  min-height: 100dvh;
  background: linear-gradient(180deg, #f7f9fc 0%, #eef3f8 100%);
}
.wpu-auth-body :where(.login-card,.auth-card,.setup-card),
.wpu-setup-page :where(.setup-card),
.wpu-install-body .install-card {
  border: 1px solid var(--wpu-ui-border);
  border-radius: var(--wpu-ui-radius-dialog);
  box-shadow: var(--wpu-ui-shadow-soft);
  background: var(--wpu-ui-surface);
}
.wpu-install-body .install-shell { width: min(100% - 28px, 1040px); margin-inline: auto; }

/* Legal surfaces */
.wpu-legal-body .wpu-legal-main { width: min(100% - 28px, var(--wpu-ui-text-content)); margin-inline: auto; }
.wpu-legal-body .wpu-legal-card { border: 1px solid var(--wpu-ui-border); border-radius: var(--wpu-ui-radius-card); background: var(--wpu-ui-surface); box-shadow: none; }
.wpu-legal-body .wpu-legal-hero h1 { font-size: clamp(1.6rem, 3vw, 2.1rem); }

/* Floating AIRA / fixed helpers */
body :where(.aira-floating,.floating-aira,.aira-launcher,.wpu-aira-launcher) {
  max-width: calc(100vw - 24px);
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
}

/* Global overflow guard */
html, body { max-width: 100%; }
body { overflow-x: clip; }
body :where(img,svg,video,canvas,iframe) { max-width: 100%; }
body :where(.row,.d-flex,.d-grid,.grid,.card,.panel,.container,.container-fluid) > * { min-width: 0; }

@media (max-width: 1024px) {
  .wpu62-admin-body .content-area { padding: 18px; }
  body :where(.page-header-actions,.header-actions,.page-actions,.rp-page-actions) { justify-content: flex-start; }
}

@media (max-width: 768px) {
  :root { --wpu-ui-control-h: 40px; }
  .wpu62-admin-body .content-area,
  .researcher-portal-body .rp-main { padding: 16px 12px 32px; }
  body :where(.page-header,.admin-page-header,.rp-page-header,.wpu-page-header,.library-page-header) { gap: 10px; }
  body :where(.page-header-actions,.header-actions,.page-actions,.rp-page-actions) { width: 100%; }
  body :where(.page-header-actions,.header-actions,.page-actions,.rp-page-actions) > :where(.btn,button,a.btn) { flex: 0 1 auto; }
  body :where(.toolbar,.command-bar,.filter-bar,.search-toolbar,.rp-toolbar,.wpu-toolbar) { align-items: stretch; }
  body :where(.toolbar,.command-bar,.filter-bar,.search-toolbar,.rp-toolbar,.wpu-toolbar) :where(.form-control,.form-select) { flex-basis: 100%; width: 100%; }
  body :where(.modal-dialog) { margin: 8px auto; max-width: calc(100vw - 16px); }
  body :where(.modal-content) { max-height: calc(100dvh - 16px); }
  .wpu-library-public .library-shell { width: min(100% - 20px, var(--wpu-ui-content)); }
}

@media (max-width: 430px) {
  body.wpu62-admin-body,
  body.discovery-body,
  body.researcher-portal-body,
  body.wpu-library-public,
  body.wpu-auth-body,
  body.wpu-setup-page,
  body.wpu-install-body { font-size: 13.8px; }
  body :where(.btn) { min-height: 42px; }
  body :where(.page-header-actions,.header-actions,.page-actions,.rp-page-actions) > :where(.btn,button,a.btn) { flex: 1 1 auto; }
  body :where(.dropdown-menu,.popover,.wpu-v3-popover) { max-width: calc(100vw - 16px); }
  body :where(.card-body,.panel-body,.rp-card-body) { padding: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
  }
}
