/*
 * Internal portal (admin.domain.tld): navigation, sign-in pages, tables,
 * access matrix and loading states. Uses the tokens from base.css so the
 * portal and the public site stay one brand.
 */

/* ---- Header / navigation ------------------------------------------- */

/* Two rows once signed in: brand + account on top, software links below,
   so the bar keeps working as more software modules are added. */
.portal-header {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0 20px;
  min-height: 68px;
  padding: 10px max(20px, calc((100vw - var(--container)) / 2)) 0;
  background: var(--navy);
  color: var(--paper);
}

.portal-brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--paper);
  font-weight: 900;
  white-space: nowrap;
}

.portal-brand img {
  width: 38px;
  height: 38px;
  object-fit: contain;
  border-radius: 6px;
  background: var(--paper);
}

.portal-brand,
.portal-user {
  padding-bottom: 10px;
}

.portal-nav {
  order: 3;
  display: flex;
  flex: 1 0 100%;
  align-items: center;
  gap: 2px;
  min-width: 0;
  margin: 0 -12px;
  overflow-x: auto;
  scrollbar-width: none;
}

.portal-nav::-webkit-scrollbar {
  display: none;
}

.portal-nav a {
  padding: 10px 12px 8px;
  border-bottom: 3px solid transparent;
  color: rgba(255, 255, 255, 0.82);
  font-weight: 700;
  white-space: nowrap;
}

.portal-nav a:hover {
  color: var(--paper);
}

.portal-nav a[aria-current="page"] {
  color: var(--paper);
  border-bottom-color: var(--amber);
}

.portal-nav .nav-divider {
  flex: 0 0 1px;
  align-self: stretch;
  margin: 8px 10px;
  background: rgba(255, 255, 255, 0.22);
}

.portal-user {
  display: flex;
  align-items: center;
  gap: 12px;
  white-space: nowrap;
}

.portal-user span {
  color: rgba(255, 255, 255, 0.75);
  font-size: 14px;
}

.portal-user a {
  padding: 8px 12px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: var(--radius);
  color: var(--paper);
  font-weight: 700;
}

.portal-user a:hover {
  background: rgba(255, 255, 255, 0.12);
}

.portal-header a:focus-visible,
.portal-page a:focus-visible,
.portal-page button:focus-visible,
.portal-page select:focus-visible,
.portal-page input:focus-visible {
  outline: 3px solid var(--amber);
  outline-offset: 2px;
}

@media (max-width: 720px) {
  .portal-brand span,
  .portal-user span {
    display: none;
  }
}

/* ---- Page frame ---------------------------------------------------- */

.portal-page {
  max-width: var(--container);
  margin: 0 auto;
  padding: 36px 24px 72px;
}

.portal-page.narrow-page {
  max-width: 760px;
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.page-head h1 {
  color: var(--navy);
  font-size: 32px;
  line-height: 1.15;
}

.page-head p {
  margin-top: 6px;
  color: var(--muted);
  max-width: 62ch;
}

.back-link {
  display: inline-block;
  margin-bottom: 14px;
  color: var(--teal);
  font-weight: 700;
}

.portal-page h2 {
  margin: 40px 0 14px;
  color: var(--navy);
  font-size: 20px;
}

.panel {
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
}

.panel + .panel {
  margin-top: 20px;
}

.panel h2 {
  margin-top: 0;
}

.muted {
  color: var(--muted);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.form-grid .wide {
  grid-column: 1 / -1;
}

.form-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.hint {
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
}

@media (max-width: 700px) {
  .form-grid {
    grid-template-columns: 1fr;
  }
}

/* ---- Portal home: software list ------------------------------------ */

.software-list {
  display: grid;
  gap: 10px;
  list-style: none;
  padding: 0;
  margin: 0;
}

.software-link {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 4px 20px;
  padding: 18px 22px;
  border: 1px solid var(--line);
  border-left: 5px solid var(--teal);
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink);
}

.software-link:hover {
  border-left-color: var(--amber);
  background: var(--soft);
}

.software-link strong {
  color: var(--navy);
  font-size: 19px;
}

.software-link small {
  grid-column: 1;
  color: var(--muted);
  font-size: 15px;
}

.software-link .level {
  grid-row: 1 / span 2;
  grid-column: 2;
}

.software-list.admin-tools .software-link {
  border-left-color: var(--navy-2);
}

/* ---- Badges -------------------------------------------------------- */

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
  background: var(--soft);
  color: var(--muted);
  border: 1px solid var(--line);
}

.badge.active,
.badge.readwrite {
  color: var(--success);
  background: #edf8f2;
  border-color: #bfe3cf;
}

.badge.read {
  color: var(--teal);
  background: #eaf6f7;
  border-color: #b8dfe2;
}

.badge.write {
  color: #8a5a00;
  background: #fff6e3;
  border-color: #f3d79a;
}

.badge.disabled {
  color: #8a5a00;
  background: #fff6e3;
  border-color: #f3d79a;
}

.badge.revoked {
  color: var(--danger);
  background: #fff1ef;
  border-color: #f2c3bd;
}

.badge.admin {
  color: var(--paper);
  background: var(--navy);
  border-color: var(--navy);
}

/* ---- Tables -------------------------------------------------------- */

.table-wrap {
  /* Keeps absolutely positioned .sr-only text inside the scroll box. */
  position: relative;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 15px;
}

.data-table th,
.data-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

.data-table thead th {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  background: var(--soft);
  white-space: nowrap;
}

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

.data-table td a.row-title {
  color: var(--navy);
  font-weight: 800;
}

.data-table td small,
.data-table th small {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.data-table tbody th {
  font-weight: 400;
}

.data-table tr.is-dim td {
  opacity: 0.6;
}

/* ---- Access matrix ------------------------------------------------- */

.access-table th:first-child,
.access-table td:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  min-width: 200px;
  background: var(--paper);
  box-shadow: 1px 0 0 var(--line);
}

.access-table thead th:first-child {
  background: var(--soft);
}

.access-table select {
  min-width: 150px;
  min-height: 38px;
  padding: 6px 10px;
  font-weight: 700;
}

.access-table select[data-level="read"] { color: var(--teal); background: #f2fafb; }
.access-table select[data-level="write"] { color: #8a5a00; background: #fffaf0; }
.access-table select[data-level="readwrite"] { color: var(--success); background: #f3fbf6; }
.access-table select[data-level="none"] { color: var(--muted); }

.access-table select.is-changed {
  border-color: var(--amber);
  box-shadow: 0 0 0 3px rgba(245, 165, 36, 0.25);
}

.save-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 16px;
  margin-top: 16px;
  padding: 14px 0;
  background: linear-gradient(to top, var(--paper) 70%, rgba(255, 255, 255, 0));
}

.save-bar [data-change-count] {
  color: var(--muted);
  font-weight: 700;
}

.save-bar.has-changes [data-change-count] {
  color: #8a5a00;
}

/* ---- Account detail ------------------------------------------------ */

.detail-list {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 10px 24px;
  margin: 0;
}

.detail-list dt {
  color: var(--muted);
  font-weight: 700;
}

.detail-list dd {
  margin: 0;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 16px;
  padding: 16px 0;
  border-top: 1px solid var(--line);
}

.action-row:first-of-type {
  border-top: 0;
  padding-top: 0;
}

.action-row > div {
  flex: 1 1 320px;
}

.action-row p {
  color: var(--muted);
  font-size: 15px;
}

.action-row strong {
  color: var(--navy);
}

/* ---- Sign-in pages ------------------------------------------------- */
/* These rules used to live only in public.css, which the admin layout
   never loaded, so the sign-in pages rendered unstyled. */

.auth-shell {
  min-height: calc(100vh - 60px);
  display: grid;
  place-items: center;
  padding: 56px 18px;
  background: var(--soft);
}

.auth-panel {
  width: min(420px, 100%);
  padding: 32px;
  border: 1px solid var(--line);
  border-top: 5px solid var(--amber);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.auth-panel img {
  width: 72px;
  height: 72px;
  object-fit: contain;
  margin-bottom: 16px;
}

.auth-panel h1 {
  margin-bottom: 6px;
  color: var(--navy);
  font-size: 28px;
}

.auth-panel > p {
  margin-bottom: 20px;
  color: var(--muted);
}

.auth-panel .stack-form {
  margin-bottom: 18px;
}

.auth-panel .stack-form .button {
  width: 100%;
  margin-top: 6px;
}

.auth-panel > a {
  color: var(--teal);
  font-weight: 700;
}

/* ---- Error / empty ------------------------------------------------- */

.portal-error {
  max-width: 560px;
  margin: 72px auto;
  padding: 0 24px;
}

.portal-error .code {
  color: var(--amber-2);
  font-size: 15px;
  font-weight: 900;
}

.portal-error h1 {
  margin: 6px 0 10px;
  color: var(--navy);
  font-size: 32px;
}

.portal-error p {
  margin-bottom: 22px;
  color: var(--muted);
}

/* ---- Loading state for form buttons -------------------------------- */

.button.is-loading {
  position: relative;
  pointer-events: none;
  opacity: 0.85;
  transform: none;
}

.button.is-loading::before {
  content: "";
  width: 16px;
  height: 16px;
  margin-right: 10px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: portal-spin 0.7s linear infinite;
}

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

@media (prefers-reduced-motion: reduce) {
  .button.is-loading::before {
    animation-duration: 2s;
  }
}

/* The website CMS sub-navigation sits under the portal header. */
.admin-subnav {
  top: 104px;
}

/* ---- Management module --------------------------------------------- */

/* Components set their own display, which would otherwise beat [hidden]. */
.portal-page [hidden] {
  display: none !important;
}

.portal-page.wide-page {
  max-width: 1400px;
}

.page-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.button.small {
  min-height: 34px;
  padding: 6px 12px;
  font-size: 14px;
}

a.button.is-loading {
  pointer-events: none;
}

/* Filter bar: one block holding every filter. */
.filter-bar {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  align-items: end;
  gap: 14px 16px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--soft);
}

.filter-bar label {
  font-size: 14px;
}

.filter-bar input,
.filter-bar select {
  min-height: 40px;
  padding: 8px 10px;
}

.date-filter {
  grid-column: span 3;
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.date-filter legend {
  margin-bottom: 6px;
  padding: 0;
  color: var(--navy);
  font-size: 14px;
  font-weight: 800;
}

.date-filter label {
  display: block;
  min-width: 0;
}

.date-filter input,
.date-filter select {
  min-width: 0;
}

.date-sep {
  color: var(--muted);
  font-weight: 700;
}

.filter-actions {
  display: flex;
  gap: 8px;
}

@media (max-width: 980px) {
  .date-filter {
    grid-column: 1 / -1;
  }
}

@media (max-width: 760px) {
  .date-filter {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }

  .date-filter select {
    grid-column: 1 / -1;
  }

  .date-sep {
    display: none;
  }
}

/* Results */
.results-loading {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  color: var(--teal);
  font-weight: 700;
}

.results-loading[hidden] {
  display: none;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: portal-spin 0.7s linear infinite;
}

.results-error {
  margin-top: 14px;
  padding: 10px 14px;
  border: 1px solid #f2c3bd;
  border-radius: var(--radius);
  background: #fff1ef;
  color: var(--danger);
  font-weight: 700;
}

.results {
  margin-top: 16px;
  transition: opacity 120ms ease;
}

.results.is-busy {
  opacity: 0.55;
}

.results-summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 18px;
  margin-bottom: 12px;
}

.summary-totals {
  margin-left: 8px;
  color: var(--muted);
  font-size: 14px;
}

.results-note {
  flex-basis: 100%;
  color: #8a5a00;
  font-size: 14px;
  font-weight: 700;
}

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  border: 1px solid #b8dfe2;
  border-radius: 999px;
  background: #eaf6f7;
  color: var(--navy);
  font-size: 14px;
}

.chip:hover {
  border-color: var(--teal);
}

.chip-x {
  color: var(--teal);
  font-size: 18px;
  line-height: 1;
}

.chip.clear {
  border-color: var(--line);
  background: var(--paper);
  color: var(--teal);
  font-weight: 700;
}

/* Clickable rows. The name is also a real link for keyboard users. */
.client-table {
  /* Scrolls inside .table-wrap on narrow screens instead of crushing columns. */
  min-width: 980px;
}

.client-table td {
  vertical-align: top;
}

.client-row {
  cursor: pointer;
}

.client-row:hover td,
.client-row:focus-within td {
  background: #f3f8fd;
}

.client-row:hover a.row-title {
  text-decoration: underline;
}

.data-table .num {
  text-align: right;
  white-space: nowrap;
}

/* Not .row-actions: admin.css gives that class display: grid. */
.client-actions {
  width: 1%;
  text-align: right;
}

.client-actions form {
  margin: 0;
}

.badge.pending {
  color: #8a5a00;
  background: #fff6e3;
  border-color: #f3d79a;
}

.badge.completed {
  color: var(--success);
  background: #edf8f2;
  border-color: #bfe3cf;
}

.badge.review {
  margin-top: 4px;
  color: var(--danger);
  background: #fff1ef;
  border-color: #f2c3bd;
}

.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 18px;
  color: var(--muted);
  font-weight: 700;
}

/* Record form */
.form-fieldset {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.form-fieldset:disabled input,
.form-fieldset:disabled select,
.form-fieldset:disabled textarea {
  background: var(--soft);
  color: var(--ink);
}

.client-form .panel h3 {
  margin: 28px 0 4px;
  color: var(--navy);
  font-size: 17px;
}

.form-grid.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

/* A hint under one field shouldn't stretch the input beside it. */
.client-form .form-grid {
  align-items: start;
}

@media (max-width: 700px) {
  .form-grid.three {
    grid-template-columns: 1fr;
  }
}

.calc-field {
  display: grid;
  gap: 6px;
  color: var(--navy);
  font-weight: 800;
}

.calc-field output {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--soft);
  font-variant-numeric: tabular-nums;
}

.installments-table {
  margin-top: 12px;
}

.installments-table th,
.installments-table td {
  padding: 8px 10px;
  vertical-align: top;
}

.installments-table td:first-child {
  width: 1%;
  padding-top: 18px;
  color: var(--muted);
  font-weight: 800;
}

.installments-table input,
.installments-table select {
  min-width: 140px;
  min-height: 40px;
  padding: 8px 10px;
}

.installments-table td.num input {
  text-align: right;
}

.installments-table tr.needs-review td {
  background: #fff8ec;
}

.installments-table .source {
  display: block;
  margin-top: 4px;
  color: #8a5a00;
  font-size: 13px;
}

.installments-table .row-error {
  display: block;
  margin-top: 4px;
  color: var(--danger);
  text-align: left;
  white-space: normal;
}

/* Narrow screens: each installment becomes a compact card instead of a
   table that hides Type and Amount off to the right. */
@media (max-width: 700px) {
  .installments-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }

  .installments-table tbody tr {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) minmax(0, 1fr);
    gap: 8px;
    padding: 12px;
    border-bottom: 1px solid var(--line);
  }

  .installments-table tbody td {
    display: block;
    padding: 0;
    border: 0;
  }

  .installments-table tbody td:first-child {
    grid-row: span 2;
    padding-top: 10px;
  }

  .installments-table tr.needs-review {
    background: #fff8ec;
  }

  .installments-table tr.needs-review td {
    background: transparent;
  }

  .installments-table input,
  .installments-table select {
    width: 100%;
    min-width: 0;
  }
}

.totals {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 20px 0 6px;
}

.totals div {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--soft);
}

.totals dt {
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.totals dd {
  margin: 2px 0 0;
  color: var(--navy);
  font-size: 20px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.totals .pending {
  border-color: #f3d79a;
  background: #fffaf0;
}

@media (max-width: 700px) {
  .totals {
    grid-template-columns: 1fr;
  }
}

.notice {
  margin-bottom: 18px;
  padding: 12px 16px;
  border: 1px solid #b8dfe2;
  border-radius: var(--radius);
  background: #eaf6f7;
  color: var(--navy);
  font-weight: 600;
}

.notice.review {
  margin: 12px 0 0;
  border-color: #f3d79a;
  background: #fff6e3;
  color: #6b4600;
}

.form-errors {
  margin-bottom: 20px;
  padding: 16px 20px;
  border: 1px solid #f2c3bd;
  border-left: 5px solid var(--danger);
  border-radius: var(--radius);
  background: #fff1ef;
}

.form-errors:focus {
  outline: 3px solid var(--amber);
  outline-offset: 2px;
}

.form-errors h2 {
  margin: 0 0 6px;
  color: var(--danger);
  font-size: 18px;
}

.form-errors ul {
  margin: 0;
  padding-left: 20px;
}

.sticky-footer {
  position: sticky;
  bottom: 0;
  z-index: 5;
  padding: 14px 0;
  background: linear-gradient(to top, var(--paper) 70%, rgba(255, 255, 255, 0));
}

.danger-zone {
  margin-top: 36px;
  border-color: #f2c3bd;
}

.danger-zone h2 {
  color: var(--danger);
}

.danger-zone p {
  margin-bottom: 14px;
}

/* ---- Audit log ------------------------------------------------------ */

.date-filter.dates-only {
  grid-column: span 2;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
}

@media (max-width: 760px) {
  .date-filter.dates-only {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }
}

.audit-table code,
.detail-list code {
  font-size: 12px;
  color: var(--muted);
}

.change-table th[scope="row"] {
  width: 30%;
  color: var(--navy);
  font-weight: 700;
}

.change-table td {
  overflow-wrap: anywhere;
}

.change-table tr.is-changed td,
.change-table tr.is-changed th {
  background: #fffaf0;
}

.change-table tr.is-changed td:last-child {
  font-weight: 700;
}

.badge.create {
  color: var(--success);
  background: #edf8f2;
  border-color: #bfe3cf;
}

.badge.update {
  color: #8a5a00;
  background: #fff6e3;
  border-color: #f3d79a;
}

.badge.delete {
  color: var(--danger);
  background: #fff1ef;
  border-color: #f2c3bd;
}

.change-lists {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.change-lists h4 {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 14px;
}

.change-lists pre,
.raw-changes pre {
  max-height: 420px;
  margin: 0;
  padding: 12px;
  overflow: auto;
  border-radius: var(--radius);
  background: var(--soft);
  font-size: 13px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.raw-changes summary {
  color: var(--teal);
  font-weight: 800;
  cursor: pointer;
}

.raw-changes[open] summary {
  margin-bottom: 12px;
}

@media (max-width: 700px) {
  .change-lists {
    grid-template-columns: 1fr;
  }
}

/* ---- Backups -------------------------------------------------------- */

.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}

.status-grid .panel + .panel {
  margin-top: 0;
}

.status-grid .big {
  color: var(--navy);
  font-size: 20px;
  font-weight: 800;
}

.notice-panel {
  margin-top: 20px;
  border-left: 5px solid var(--teal);
}

.backup-table code {
  font-size: 12px;
}

.backup-table .hash {
  display: inline-block;
  max-width: 180px;
  overflow-wrap: anywhere;
  color: var(--muted);
}

.backup-actions {
  white-space: nowrap;
}

.backup-actions form {
  display: inline-block;
}

.table-wrap + .hint {
  margin-top: 10px;
}
