/*
 * KPCA/KPCA shared design system
 *
 * This layer extends the dashboard visual language to embedded modules.
 * Page-specific layouts opt in with .portal-page so legacy public forms are
 * not unexpectedly rearranged.
 */

:root {
  --portal-surface: #ffffff;
  --portal-surface-subtle: #f8fafc;
  --portal-border: #e5e7eb;
  --portal-border-strong: #d1d5db;
  --portal-text: #1f2937;
  --portal-muted: #64748b;
  --portal-radius-sm: 0.5rem;
  --portal-radius: 0.75rem;
  --portal-radius-lg: 1rem;
  --portal-shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --portal-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
  --portal-success: #198754;
  --portal-warning: #b7791f;
  --portal-danger: #c2413b;
  --portal-info: #2878b5;
}

/* Accessibility primitives shared by the dashboard and embedded modules. */
.portal-skip-link {
  position: fixed;
  z-index: 10000;
  top: 0.75rem;
  left: 0.75rem;
  padding: 0.7rem 1rem;
  border-radius: var(--portal-radius-sm);
  background: var(--kpca-primary, #1a3a52);
  color: #fff !important;
  font-weight: 700;
  text-decoration: none;
  box-shadow: 0 6px 20px rgba(15, 23, 42, 0.25);
  transform: translateY(calc(-100% - 1rem));
  transition: transform 150ms ease;
}

.portal-skip-link:focus {
  transform: translateY(0);
}

.portal-sr-only,
.portal-live-region {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

:where(a, button, input, select, textarea, [tabindex]):focus-visible {
  outline: 3px solid color-mix(in srgb, var(--kpca-accent, #4a9fd8) 55%, white) !important;
  outline-offset: 2px !important;
}

:where(input, select, textarea):user-invalid,
.was-validated :where(input, select, textarea):invalid {
  border-color: var(--portal-danger) !important;
  box-shadow: 0 0 0 3px rgba(194, 65, 59, 0.14) !important;
}

:where(input, select, textarea)[aria-invalid="true"] {
  border-color: var(--portal-danger) !important;
}

[aria-busy="true"] {
  cursor: progress;
}

/* Shared feedback states */
.portal-toast-region {
  position: fixed;
  z-index: 11000;
  top: 1rem;
  right: 1rem;
  display: grid;
  width: min(24rem, calc(100vw - 2rem));
  gap: 0.65rem;
  pointer-events: none;
}

.portal-toast {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.85rem 0.9rem 0.85rem 1rem;
  border: 1px solid var(--portal-border);
  border-left: 4px solid var(--portal-info);
  border-radius: var(--portal-radius-sm);
  background: #fff;
  color: var(--portal-text);
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.18);
  pointer-events: auto;
  animation: portal-toast-in 180ms ease-out;
}

.portal-toast-success { border-left-color: var(--portal-success); }
.portal-toast-warning { border-left-color: #d39e00; }
.portal-toast-danger { border-left-color: var(--portal-danger); }
.portal-toast.is-leaving { opacity: 0; transform: translateX(0.75rem); transition: 180ms ease; }
.portal-toast-message { flex: 1; font-size: 0.875rem; line-height: 1.45; }
.portal-toast-close {
  display: inline-grid;
  width: 1.75rem;
  height: 1.75rem;
  flex: 0 0 1.75rem;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: #64748b;
  font-size: 1.25rem;
  line-height: 1;
}
.portal-toast-close:hover { background: #f1f5f9; color: var(--portal-text); }

.portal-button-spinner {
  display: inline-block;
  width: 1rem;
  height: 1rem;
  margin-right: 0.5rem;
  vertical-align: -0.15rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: portal-spin 650ms linear infinite;
}

.nav-search-empty {
  display: none;
  margin: 0 0.85rem 0.75rem;
  padding: 0.75rem;
  border: 1px dashed var(--portal-border-strong);
  border-radius: var(--portal-radius-sm);
  color: var(--portal-muted);
  font-size: 0.8rem;
  text-align: center;
}
.nav-search-empty.is-visible { display: block; }

@keyframes portal-toast-in {
  from { opacity: 0; transform: translateY(-0.5rem); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes portal-spin { to { transform: rotate(360deg); } }

.portal-page {
  width: 100%;
  max-width: 1600px;
  margin-inline: auto;
  padding: 1.5rem;
}

.portal-page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.portal-page-title {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0;
  color: var(--portal-text);
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.25;
}

.portal-page-title > i {
  display: inline-grid;
  width: 2.5rem;
  height: 2.5rem;
  place-items: center;
  border-radius: var(--portal-radius-sm);
  background: color-mix(in srgb, var(--kpca-primary) 10%, white);
  color: var(--kpca-primary);
  font-size: 1rem;
}

.portal-page-subtitle {
  margin: 0.35rem 0 0 3.25rem;
  color: var(--portal-muted);
  font-size: 0.875rem;
}

.portal-page-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.portal-panel,
.portal-page .table-card,
.portal-page .filters-bar,
.portal-page .info-box {
  background: var(--portal-surface);
  border: 1px solid var(--portal-border);
  border-radius: var(--portal-radius);
  box-shadow: var(--portal-shadow-sm);
}

.portal-page .table-card,
.portal-page .filters-bar {
  padding: 1.25rem;
  margin-bottom: 1.5rem;
}

.portal-page .info-box {
  padding: 1rem 1.25rem;
  margin-bottom: 1.5rem;
  border-left: 4px solid var(--kpca-accent);
  color: var(--portal-text);
}

.portal-page .stat-card {
  display: block;
  min-height: 7.25rem;
  padding: 1.15rem 1.25rem;
  margin-bottom: 1rem;
  background: var(--portal-surface);
  border: 1px solid var(--portal-border);
  border-left: 4px solid var(--kpca-primary);
  border-radius: var(--portal-radius);
  box-shadow: var(--portal-shadow-sm);
  color: inherit;
  text-decoration: none;
  transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}

.portal-page a.stat-card:hover {
  border-color: color-mix(in srgb, var(--kpca-primary) 45%, var(--portal-border));
  box-shadow: var(--portal-shadow);
  color: inherit;
  transform: translateY(-2px);
}

.portal-page .stat-label,
.portal-page .stats-type {
  margin-bottom: 0.45rem;
  color: var(--portal-muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.055em;
  text-transform: uppercase;
}

.portal-page .stat-number,
.portal-page .stats-figure {
  color: var(--portal-text);
  font-size: clamp(1.55rem, 2vw, 2rem);
  font-weight: 700;
  line-height: 1.15;
}

.portal-page .table-card > h5,
.portal-page .portal-panel-title {
  margin-bottom: 1rem !important;
  color: var(--portal-text);
  font-size: 1rem;
  font-weight: 650;
}

.portal-page .table-responsive {
  border: 1px solid var(--portal-border);
  border-radius: var(--portal-radius-sm);
}

.portal-page .table {
  margin-bottom: 0;
  box-shadow: none !important;
}

.portal-page .table thead th {
  background: var(--portal-surface-subtle) !important;
  color: #475569 !important;
  border-bottom: 1px solid var(--portal-border) !important;
  font-size: 0.7rem;
}

.portal-page .table tbody td {
  padding: 0.8rem 0.85rem !important;
  border-color: #eef2f6;
}

.portal-page .table tbody tr:last-child td {
  border-bottom: 0;
}

.portal-page .table tbody tr:hover {
  background: #f8fafc !important;
  transform: none !important;
}

.portal-page .filters-bar label {
  margin-bottom: 0.4rem !important;
  color: #475569;
  font-size: 0.78rem;
  font-weight: 600;
}

.portal-page .filters-bar .row {
  row-gap: 0.85rem;
}

.portal-page code {
  padding: 0.2rem 0.4rem;
  border-radius: 0.35rem;
  background: #f1f5f9;
  color: #334155;
  font-size: 0.78rem;
}

/* Bootstrap 4 badge names still used by legacy modules. */
.badge-primary { background: var(--kpca-primary) !important; color: #fff !important; }
.badge-secondary { background: #64748b !important; color: #fff !important; }
.badge-success { background: var(--portal-success) !important; color: #fff !important; }
.badge-warning { background: #f6c453 !important; color: #4a3500 !important; }
.badge-danger { background: var(--portal-danger) !important; color: #fff !important; }
.badge-info { background: #dceeff !important; color: #145681 !important; }
.badge-light { background: #f1f5f9 !important; color: #334155 !important; }

.portal-page .priority-rush {
  box-shadow: inset 4px 0 0 #dc3545;
}

.portal-page .priority-badge {
  display: inline-flex;
  align-items: center;
  margin-top: 0.3rem;
  padding: 0.25rem 0.45rem;
  border-radius: 999px;
  background: #fee2e2;
  color: #991b1b;
  font-size: 0.68rem;
  font-weight: 700;
}

.portal-page .btn-sm {
  min-width: 2rem;
  min-height: 2rem;
}

.portal-page .progress {
  overflow: hidden;
  border-radius: 999px;
  background: #e9eef5;
}

.portal-page .progress-bar {
  background: var(--kpca-primary);
  font-size: 0.7rem;
  font-weight: 600;
}

.portal-empty-state {
  padding: 2.5rem 1rem !important;
  color: var(--portal-muted);
}

.portal-empty-state i {
  color: #cbd5e1;
}

/* Admissions/list management pattern */
.applications-summary .stat-card {
  min-height: 6.4rem;
  margin-bottom: 0;
}

.applications-toolbar {
  position: sticky;
  top: 0;
  z-index: 20;
  margin-bottom: 1rem;
  padding-top: 0.25rem;
  background: var(--kpca-bg);
}

.applications-tabs {
  display: flex;
  gap: 0.35rem;
  overflow-x: auto;
  padding: 0.4rem;
  border: 1px solid var(--portal-border);
  border-radius: var(--portal-radius) var(--portal-radius) 0 0;
  background: var(--portal-surface);
  scrollbar-width: thin;
}

.applications-tab {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  gap: 0.5rem;
  min-height: 2.5rem;
  padding: 0.55rem 0.8rem;
  border-radius: var(--portal-radius-sm);
  color: #475569;
  font-size: 0.82rem;
  font-weight: 600;
  text-decoration: none;
}

.applications-tab:hover {
  background: #f1f5f9;
  color: var(--kpca-primary);
}

.applications-tab.active {
  background: var(--kpca-primary);
  color: #fff;
}

.applications-tab-count {
  display: inline-grid;
  min-width: 1.45rem;
  height: 1.45rem;
  place-items: center;
  padding-inline: 0.35rem;
  border-radius: 999px;
  background: #e9eef5;
  color: #475569;
  font-size: 0.7rem;
}

.applications-tab.active .applications-tab-count {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.portal-page .applications-filters {
  margin: 0;
  border-top: 0;
  border-radius: 0 0 var(--portal-radius) var(--portal-radius);
  box-shadow: 0 5px 16px rgba(15, 23, 42, 0.05);
}

.applications-filters .input-group-text {
  border: 1.5px solid var(--portal-border);
  border-right: 0;
  background: #fff;
  color: var(--portal-muted);
}

.applications-filters .input-group .form-control {
  border-left: 0 !important;
}

.portal-data-region .table-responsive {
  max-height: 65vh;
  overflow: auto;
}

.portal-data-region .table thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  box-shadow: inset 0 -1px 0 var(--portal-border);
}

.applications-table {
  min-width: 900px;
}

.applications-table th:first-child,
.applications-table td:first-child {
  min-width: 235px;
}

.applications-table th:nth-child(2),
.applications-table td:nth-child(2) {
  min-width: 210px;
}

.applications-table th:nth-child(3),
.applications-table td:nth-child(3) {
  min-width: 175px;
}

.application-name-button {
  display: inline-flex;
  align-items: center;
  gap: 0.7rem;
  max-width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--portal-text);
  text-align: left;
}

.application-name-button:hover strong {
  color: var(--kpca-accent);
  text-decoration: underline;
}

.application-name-button span:last-child {
  min-width: 0;
}

.application-name-button strong,
.application-name-button small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.application-name-button small {
  max-width: 185px;
  margin-top: 0.12rem;
  color: var(--portal-muted);
  font-size: 0.75rem;
}

.application-avatar {
  display: inline-grid;
  width: 2.35rem;
  height: 2.35rem;
  flex: 0 0 2.35rem;
  place-items: center;
  border-radius: 50%;
  background: color-mix(in srgb, var(--kpca-primary) 12%, white);
  color: var(--kpca-primary);
  font-size: 0.78rem;
  font-weight: 700;
}

.application-date {
  color: #475569;
  font-size: 0.8rem;
  white-space: nowrap;
}

.application-actions-button::after {
  display: none;
}

.application-actions-menu {
  min-width: 245px;
  padding: 0.45rem;
  border-color: var(--portal-border);
  border-radius: var(--portal-radius-sm);
  box-shadow: var(--portal-shadow);
}

.application-actions-menu .dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-height: 2.25rem;
  border-radius: 0.4rem;
  font-size: 0.8rem;
}

.application-actions-menu .dropdown-item i {
  width: 1rem;
  color: #64748b;
  text-align: center;
}

.application-actions-menu .dropdown-item.text-danger i {
  color: inherit;
}

.application-details-panel {
  --bs-offcanvas-width: min(430px, 92vw);
}

.application-details-panel .offcanvas-header {
  border-bottom: 1px solid var(--portal-border);
}

.application-detail-list {
  margin: 0 0 1.5rem;
}

.application-detail-list > div {
  display: grid;
  grid-template-columns: 8rem minmax(0, 1fr);
  gap: 1rem;
  padding: 0.8rem 0;
  border-bottom: 1px solid #eef2f6;
}

.application-detail-list dt {
  color: var(--portal-muted);
  font-size: 0.75rem;
  font-weight: 600;
}

.application-detail-list dd {
  margin: 0;
  color: var(--portal-text);
  font-size: 0.85rem;
  font-weight: 550;
  overflow-wrap: anywhere;
}

.portal-page :is(a, button, input, select, textarea):focus-visible {
  outline: 3px solid color-mix(in srgb, var(--kpca-accent) 35%, transparent);
  outline-offset: 2px;
}

@media (max-width: 767.98px) {
  .portal-page { padding: 1rem; }
  .portal-page-header { align-items: flex-start; flex-direction: column; }
  .portal-page-title { font-size: 1.3rem; }
  .portal-page-subtitle { margin-left: 0; }
  .portal-page-actions { width: 100%; }
  .portal-page-actions .btn { flex: 1 1 auto; }
  .portal-page .table-card,
  .portal-page .filters-bar { padding: 1rem; }
  .portal-page .stat-card { min-height: auto; }
  .portal-page .btn,
  .portal-page .form-control,
  .portal-page .form-select,
  .portal-page .page-link {
    min-height: 2.75rem;
  }
  .portal-page .page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.75rem;
  }
  .applications-toolbar { position: static; }
  .applications-tab { min-height: 2.75rem; padding-inline: 0.7rem; }
  .applications-tab i { display: none; }
  .application-detail-list > div { grid-template-columns: 6.5rem minmax(0, 1fr); }

  /* Opt-in mobile table pattern: every database row becomes a readable card. */
  .portal-page .table-responsive:has(.portal-mobile-cards) {
    max-height: none;
    overflow: visible;
    border: 0;
  }

  .portal-mobile-cards {
    display: block;
    min-width: 0 !important;
    background: transparent;
  }

  .portal-mobile-cards thead {
    display: none;
  }

  .portal-mobile-cards tbody {
    display: grid;
    gap: 0.85rem;
  }

  .portal-mobile-cards tbody tr {
    display: grid;
    overflow: visible;
    border: 1px solid var(--portal-border);
    border-radius: var(--portal-radius);
    background: var(--portal-surface);
    box-shadow: var(--portal-shadow-sm);
  }

  .portal-mobile-cards tbody tr:hover {
    background: var(--portal-surface) !important;
  }

  .portal-page .portal-mobile-cards tbody td {
    display: grid;
    grid-template-columns: minmax(6.75rem, 34%) minmax(0, 1fr);
    gap: 0.75rem;
    align-items: center;
    width: 100%;
    min-width: 0 !important;
    padding: 0.7rem 0.85rem !important;
    border-bottom: 1px solid #eef2f6;
    text-align: left !important;
    overflow-wrap: anywhere;
  }

  .portal-mobile-cards tbody td:last-child {
    border-bottom: 0;
  }

  .portal-mobile-cards tbody td::before {
    content: attr(data-label);
    color: var(--portal-muted);
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.045em;
    text-transform: uppercase;
  }

  .portal-mobile-cards tbody td[colspan] {
    display: block;
  }

  .portal-mobile-cards tbody td[colspan]::before {
    display: none;
  }

  .portal-mobile-cards .btn,
  .portal-mobile-cards button,
  .portal-mobile-cards .dropdown-item {
    min-height: 2.75rem;
  }

  .portal-mobile-cards .dropdown-menu {
    max-width: calc(100vw - 2rem);
  }

  .applications-table tbody td:first-child {
    display: block;
    padding: 0.9rem !important;
    background: var(--portal-surface-subtle);
  }

  .applications-table tbody td:first-child::before {
    display: none;
  }

  .applications-table .application-name-button,
  .applications-table .application-name-button small {
    width: 100%;
    max-width: none;
  }

  .applications-table tbody td:last-child {
    display: flex;
    justify-content: flex-end;
    background: var(--portal-surface-subtle);
  }

  .applications-table tbody td:last-child::before {
    margin-right: auto;
    align-self: center;
  }
}

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

@media (forced-colors: active) {
  :where(a, button, input, select, textarea, [tabindex]):focus-visible {
    outline: 2px solid CanvasText !important;
  }
}
