/**
 * MUWEI Management System — International-grade design system
 * Supports: responsive layout, accessibility (WCAG 2.1), print, RTL-ready
 */

:root {
  /* MUWEI brand — aligned with muwei.org logo & website (sustainability, agriculture, empowerment) */
  --muwei-primary: #0d5c2e;
  --muwei-primary-hover: #166534;
  --muwei-accent: #15803d;
  --muwei-success: #166534;
  --muwei-success-bg: #dcfce7;
  --muwei-danger: #b91c1c;
  --muwei-danger-bg: #fee2e2;
  --muwei-warning: #b45309;
  --muwei-warning-bg: #fef3c7;
  /* Neutrals */
  --surface: #ffffff;
  --surface-alt: #f0fdf4;
  --border: #bbf7d0;
  --text: #1e293b;
  --text-muted: #475569;
  /* Spacing & typography */
  --radius: 8px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(13,92,46,0.08);
  --shadow-md: 0 4px 6px -1px rgba(13,92,46,0.1), 0 2px 4px -2px rgba(13,92,46,0.06);
  --font-sans: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --focus-ring: 2px solid var(--muwei-accent);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
body {
  font-family: var(--font-sans);
  margin: 0;
  background: var(--surface-alt);
  color: var(--text);
  line-height: 1.5;
  font-size: 16px;
}

/* When loaded inside modal iframe: center all main content */
body.is-modal {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
}
body.is-modal .container {
  width: 100% !important;
  max-width: 100% !important;
  padding: 1rem !important;
  margin: 0 auto !important;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  flex: 1;
}
body.is-modal main {
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  width: 100% !important;
  max-width: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
  flex: 1;
}
body.is-modal main > *:not([aria-live]):not(.breadcrumb-nav) {
  width: 100%;
  max-width: 56rem;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
}
body.is-modal main > [aria-live],
body.is-modal main > .breadcrumb-nav {
  width: 100%;
  max-width: 100%;
  display: block;
}
body.is-modal .form-shell {
  width: 100% !important;
  max-width: 56rem !important;
  margin-left: auto !important;
  margin-right: auto !important;
  margin-inline: auto !important;
  display: block !important;
  padding: 0 2rem !important;
  box-sizing: border-box !important;
  position: relative !important;
  align-self: center !important;
  flex-shrink: 0 !important;
}
body.is-modal .skip-link { display: none; }

/* Modal (popup forms) */
.app-modal {
  width: min(1000px, calc(100vw - 2rem));
  max-width: 1000px;
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  box-shadow: var(--shadow-md);
  background: var(--surface);
}
.app-modal::backdrop {
  background: rgba(15, 23, 42, 0.55);
}
.app-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
}
.app-modal-title {
  font-weight: 700;
  color: var(--text);
  font-size: 0.95rem;
}
.app-modal-close {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  width: 44px;
  height: 44px;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
}
.app-modal-close:hover { background: var(--border); }
.app-modal-close:focus { outline: var(--focus-ring); outline-offset: 2px; }
.app-modal-body {
  padding: 0;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.app-modal-frame {
  width: 100%;
  height: min(78vh, 720px);
  border: 0;
  display: block;
}
.app-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--surface-alt);
}

/* Screen reader only (a11y) */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* Skip link (a11y) */
.skip-link {
  position: absolute;
  top: -100%;
  left: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--muwei-primary);
  color: #fff;
  text-decoration: none;
  border-radius: var(--radius-sm);
  z-index: 9999;
  transition: top 0.2s;
}
.skip-link:focus { top: 0.5rem; outline: var(--focus-ring); outline-offset: 2px; }

/* Navbar */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: var(--muwei-primary);
  color: #fff;
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: nowrap;
  gap: 0.75rem;
  min-width: 0;
}
body.has-navbar main { padding-top: 4.5rem; }
.navbar-brand { font-weight: 700; font-size: 1.125rem; text-decoration: none; color: #fff; display: inline-flex; align-items: center; flex-shrink: 0; }
.navbar-brand:hover { color: #e2e8f0; }
.navbar-logo { height: 38px; width: auto; max-width: 140px; object-fit: contain; display: block; }
.navbar-nav { display: flex; align-items: center; gap: 0.5rem; flex-wrap: nowrap; flex: 1 1 auto; min-width: 0; overflow-x: auto; -webkit-overflow-scrolling: touch; white-space: nowrap; }
.navbar a { color: #fff; text-decoration: none; padding: 0.5rem 0.75rem; min-height: 44px; min-width: 44px; display: inline-flex; align-items: center; justify-content: center; border-radius: var(--radius-sm); box-sizing: border-box; }
.navbar a:hover { background: rgba(255,255,255,0.15); }
.navbar a:focus { outline: var(--focus-ring); outline-offset: 2px; }
.navbar .user-menu { display: flex; align-items: center; gap: 0.75rem; flex-shrink: 0; }
.navbar .user-account-link { display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; overflow: hidden; text-decoration: none; border: 2px solid rgba(255,255,255,0.9); background: rgba(255,255,255,0.95); box-sizing: border-box; }
.navbar .user-account-link:hover { opacity: 0.95; border-color: #fff; }
.navbar .user-avatar { width: 36px; height: 36px; min-width: 36px; min-height: 36px; object-fit: cover; border-radius: 50%; display: block; vertical-align: middle; flex-shrink: 0; }
.navbar .user-avatar-placeholder { width: 36px; height: 36px; min-width: 36px; min-height: 36px; display: inline-flex; align-items: center; justify-content: center; background: #fff; color: var(--muwei-primary); border-radius: 50%; font-size: 1rem; font-weight: 600; }
.navbar .user-name { font-size: 0.875rem; opacity: 0.95; }

/* Container */
.container { max-width: 1280px; margin: 0 auto; padding: 1.5rem; }
@media (min-width: 768px) { .container { padding: 2rem; } }

/* Center form-shell containers - use multiple selectors for maximum compatibility */
.container .form-shell,
main .form-shell,
main.container .form-shell {
  margin-left: auto !important;
  margin-right: auto !important;
  margin-inline: auto !important;
  display: block !important;
  width: auto !important;
  max-width: 56rem !important;
  min-width: min(56rem, calc(100% - 4rem)) !important;
  box-sizing: border-box !important;
  position: relative;
}

/* Page header */
.page-header { margin-bottom: 1.5rem; }
.page-header h1 { margin: 0 0 0.25rem 0; font-size: 1.5rem; font-weight: 700; }
.page-header .subtitle { color: var(--text-muted); font-size: 0.875rem; }

/* Cards */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}
.card-title { margin: 0 0 1rem 0; font-size: 1.125rem; font-weight: 600; }

/* Login page */
.login-card { text-align: center; }
.login-card .form-group,
.login-card .form-hint { text-align: left; }
.login-logo-wrap { width: 100%; display: flex; justify-content: center; align-items: center; margin-bottom: 1rem; }
.login-logo { max-width: 200px; height: auto; display: block; margin-inline: auto; }

/* Buttons — min 44px touch target for accessibility */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 1rem;
  min-height: 44px;
  min-width: 44px;
  border-radius: var(--radius-sm);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background 0.15s, color 0.15s;
}
.btn-sm { min-height: 36px; min-width: 36px; padding: 0.375rem 0.75rem; font-size: 0.8125rem; }
.btn:focus { outline: var(--focus-ring); outline-offset: 2px; }
.btn-primary { background: var(--muwei-accent); color: #fff; }
.btn-primary:hover { background: var(--muwei-primary-hover); }
.btn-secondary { background: var(--surface-alt); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--muwei-danger); color: #fff; }
.btn-danger:hover { background: #991b1b; }
.btn + .btn { margin-left: 0.5rem; }

/* Alerts */
.alert { padding: 0.75rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; }
.alert-success { background: var(--muwei-success-bg); color: var(--muwei-success); }
.alert-error { background: var(--muwei-danger-bg); color: var(--muwei-danger); }
.alert-warning { background: var(--muwei-warning-bg); color: var(--muwei-warning); }
.alert-info { background: #dcfce7; color: #166534; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 600px; }
th, td { padding: 0.75rem 1rem; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--surface-alt); font-weight: 600; font-size: 0.875rem; }
tbody tr:hover { background: var(--surface-alt); }
tbody tr:focus-within { background: var(--surface-alt); }
table a { color: var(--muwei-accent); text-decoration: none; }
table a:hover { text-decoration: underline; }

/* Forms */
/* Center primary data-entry forms across the system.
   Excludes filter/search forms and inline forms (e.g., navbar logout). */
main form:not(.filter-card):not([style*="display:inline"]) {
  margin-inline: auto;
  max-width: 56rem;
  width: 100%;
}

/* Generic centered form container for create/edit pages */
.form-shell {
  width: 100% !important;
  max-width: 56rem !important;
  margin-left: auto !important;
  margin-right: auto !important;
  margin-inline: auto !important;
  padding: 1.5rem 2rem 2rem !important;
  display: block !important;
  box-sizing: border-box !important;
  position: relative !important;
  float: none !important;
  text-align: left;
}

/* Ensure form-shell is centered in all contexts, especially modals */
body.is-modal .form-shell,
body.is-modal .container .form-shell,
body.is-modal main .form-shell {
  width: auto !important;
  max-width: 56rem !important;
  min-width: min(56rem, calc(100% - 4rem)) !important;
  margin-left: auto !important;
  margin-right: auto !important;
  margin-inline: auto !important;
  display: block !important;
  padding: 0 2rem !important;
  box-sizing: border-box !important;
}

/* Ensure form content inside form-shell is left-aligned */
.form-shell form {
  width: 100%;
  margin: 1rem auto 0;
  padding: 0;
}

/* Form-shell heading for edit/create pages */
.form-shell h1 {
  text-align: left;
  margin-top: 0;
  margin-bottom: 1rem;
  font-size: 1.4rem;
  font-weight: 600;
}

/* In modals, ensure everything is properly centered */
body.is-modal .form-shell h1 {
  text-align: left;
  width: 100%;
  max-width: 56rem;
}

/* Keep filter/search forms full-width inside cards */
main form.filter-card {
  max-width: none;
  margin-inline: 0;
}

.form-group { margin-bottom: 1rem; }
.form-group label { display: block; margin-bottom: 0.25rem; font-weight: 500; font-size: 0.875rem; }
.required { color: var(--muwei-danger); }
.invalid-feedback { font-size: 0.875rem; color: var(--muwei-danger); margin-top: 0.25rem; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="date"], textarea, select {
  width: 100%;
  max-width: 28rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  display: block;
  margin-left: 0;
  margin-right: 0;
}

/* In modal forms, ensure inputs are properly contained */
body.is-modal .form-shell input[type="text"],
body.is-modal .form-shell input[type="email"],
body.is-modal .form-shell input[type="password"],
body.is-modal .form-shell input[type="number"],
body.is-modal .form-shell input[type="date"],
body.is-modal .form-shell textarea,
body.is-modal .form-shell select {
  width: 100%;
  max-width: 28rem;
  margin-left: 0;
  margin-right: 0;
}
input:focus, textarea:focus, select:focus {
  outline: var(--focus-ring);
  outline-offset: 0;
  border-color: var(--muwei-accent);
}
textarea { min-height: 100px; resize: vertical; }
.form-hint { font-size: 0.8125rem; color: var(--text-muted); margin-top: 0.25rem; }

/* Badges */
.badge { display: inline-block; padding: 0.25rem 0.5rem; border-radius: 9999px; font-size: 0.75rem; font-weight: 500; }
.badge-active { background: var(--muwei-success-bg); color: var(--muwei-success); }
.badge-draft { background: var(--surface-alt); color: var(--text-muted); }
.badge-pending { background: var(--muwei-warning-bg); color: var(--muwei-warning); }
.badge-closed { background: var(--surface-alt); color: var(--text); }
.badge-approved { background: var(--muwei-success-bg); color: var(--muwei-success); }

/* Dashboard */
.dashboard { max-width: 64rem; }
.dashboard-header {
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}
.dashboard-title {
  margin: 0 0 0.35rem 0;
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.02em;
}
.dashboard-welcome {
  margin: 0 0 0.5rem 0;
  font-size: 1rem;
  color: var(--text-muted);
}
.dashboard-time {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--text-muted);
}
.dashboard-time-label { font-weight: 500; color: var(--text); }
.dashboard-timezone { color: var(--text-muted); font-weight: 400; }

/* KPI cards (dashboard) */
.dashboard-kpis { margin-bottom: 2rem; }
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 1.25rem; }
.kpi-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  border-left: 4px solid var(--muwei-primary);
}
.kpi-card .kpi-label { font-size: 0.875rem; color: var(--text-muted); margin-bottom: 0.35rem; font-weight: 500; }
.kpi-card .kpi-value { font-size: 1.75rem; font-weight: 700; color: var(--muwei-primary); }

/* Quick actions */
.quick-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }

/* Dashboard quick links */
.dashboard-quicklinks { margin-top: 1.5rem; padding: 1.75rem; }
.dashboard-quicklinks .card-title { margin-bottom: 1.25rem; font-size: 1.1rem; }
.quick-actions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
}
.quick-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.625rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.9375rem;
  font-weight: 500;
  text-decoration: none;
  text-align: center;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  transition: background 0.2s, border-color 0.2s, color 0.2s, box-shadow 0.2s, transform 0.15s;
}
.quick-link:hover {
  background: var(--border);
  border-color: var(--muwei-accent);
  color: var(--muwei-primary);
  box-shadow: var(--shadow);
  transform: translateY(-1px);
}
.quick-link:focus {
  outline: var(--focus-ring);
  outline-offset: 2px;
}
.quick-link-primary {
  background: var(--muwei-primary);
  border-color: var(--muwei-primary);
  color: #fff;
}
.quick-link-primary:hover {
  background: var(--muwei-primary-hover);
  border-color: var(--muwei-primary-hover);
  color: #fff;
}

/* Dashboard sections */
.dashboard-section { margin-top: 1.5rem; }
.dashboard-two-col {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.25rem;
}
.metric-title {
  margin: 0 0 0.75rem 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}
.metric-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  /* Override global table min-width so dashboard columns don't extend past the card */
  min-width: 0;
}
.metric-table th,
.metric-table td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.metric-table th {
  background: var(--surface-alt);
  font-weight: 600;
}

/* Requisition pipeline cards */
.pipeline-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem;
}
.pipeline-card {
  background: var(--surface-alt);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
}
.pipeline-label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}
.pipeline-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--muwei-primary);
}
.pipeline-amount {
  font-size: 0.8125rem;
  color: var(--text-muted);
}

/* Pagination row: one horizontal bar with "Showing", nav, and per-page */
.pagination-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem 1.5rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

/* Wrapper for "Showing X to Y of Z" + pagination nav (from default view) */
.pagination-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
}

.pagination-info {
  margin: 0;
  font-size: 0.875rem;
  color: var(--text-muted);
}

.pagination-info .font-medium {
  font-weight: 600;
  color: var(--text);
}

/* Nav containing ul.pagination */
.pagination-nav {
  display: inline-block;
}

.pagination {
  list-style: none;
  margin: 0;
  padding: 0;
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem;
}

.pagination li {
  display: inline-flex;
}

.pagination li a,
.pagination li span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.25rem;
  min-height: 2.25rem;
  padding: 0.375rem 0.5rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  text-decoration: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.pagination li a:hover {
  background: var(--surface-alt);
  border-color: var(--muwei-accent);
  color: var(--muwei-accent);
}

.pagination li a:focus {
  outline: var(--focus-ring);
  outline-offset: 2px;
}

.pagination li.current span {
  background: var(--muwei-primary);
  border-color: var(--muwei-primary);
  color: #fff;
}

.pagination li.disabled span {
  opacity: 0.6;
  cursor: default;
  color: var(--text-muted);
}

/* Per-page selector: aligned in same row, clear labels, good contrast */
.per-page-wrap {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0;
  font-size: 0.875rem;
  color: var(--text-muted);
}

.per-page-label {
  margin: 0;
  font-weight: 500;
  color: var(--text);
}

.per-page-links {
  display: inline-flex;
  gap: 0.25rem;
  align-items: center;
}

.per-page-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2rem;
  min-height: 2rem;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.per-page-link:hover {
  background: var(--surface-alt);
  border-color: var(--muwei-accent);
  color: var(--muwei-accent);
}

.per-page-current {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2rem;
  min-height: 2rem;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 600;
  background: var(--muwei-primary);
  color: #fff;
  border: 1px solid var(--muwei-primary);
}

/* Breadcrumbs */
.breadcrumb { list-style: none; margin: 0 0 1rem 0; padding: 0; display: flex; flex-wrap: wrap; gap: 0.35rem; font-size: 0.875rem; color: var(--text-muted); }
.breadcrumb li { display: inline-flex; align-items: center; }
.breadcrumb li:not(:last-child)::after { content: "›"; margin-left: 0.35rem; color: var(--text-muted); }
.breadcrumb a { color: var(--muwei-accent); text-decoration: none; }
.breadcrumb a:hover { text-decoration: underline; }
.breadcrumb [aria-current="page"] { color: var(--text); font-weight: 500; }
.breadcrumb-nav { margin-bottom: 0.5rem; }

/* Empty state (lists with no data) */
.empty-state { padding: 2rem 1.5rem; text-align: center; color: var(--text-muted); }
.empty-state p { margin: 0 0 1rem 0; font-size: 1rem; }
.empty-state .btn { min-height: 44px; }

/* Filter row (search + filters) */
.filter-row { display: flex; flex-wrap: wrap; gap: 1rem; align-items: flex-end; }
.filter-card .form-group label { font-size: 0.8125rem; }
.filter-card input, .filter-card select { max-width: 14rem; }

/* Footer (optional) */
.app-footer { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border); font-size: 0.8125rem; color: var(--text-muted); text-align: center; }

/* Print */
@media print {
  .navbar, .btn, .skip-link, .no-print { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border: 1px solid #ddd; }
}

/* RTL-ready */
[dir="rtl"] .navbar-nav { flex-direction: row-reverse; }
[dir="rtl"] th, [dir="rtl"] td { text-align: right; }
