/*
 * TESSARAKT Fund Management System
 * Sprint 6 — Customer Receipts presentation
 *
 * All rules are scoped to #page-receipts. Receipt posting, invoice linking,
 * filters, imports, exports, and calculations remain controlled by app.js.
 */

#page-receipts.receipts-page {
  width: 100%;
  min-width: 0;
  gap: var(--space-6);
}

#page-receipts .receipts-page-header {
  align-items: center;
  padding-bottom: var(--space-5);
  border-bottom: 1px solid var(--color-border-subtle);
}

#page-receipts .receipts-page-header .page-title-block {
  display: grid;
  min-width: 0;
  gap: var(--space-2);
}

#page-receipts .receipts-page-header .ui-helper-text {
  max-width: 48rem;
  font-size: var(--font-size-sm);
}

/* Existing KPIs, with no new metrics introduced. */
#page-receipts .receipts-kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-4);
  margin: 0;
}

#page-receipts .receipt-summary-card {
  min-width: 0;
  min-height: 9.5rem;
  align-content: start;
  gap: var(--space-2);
  padding: var(--space-5);
  overflow: hidden;
  border-color: var(--color-border-default);
  border-radius: var(--radius-lg);
  background: var(--color-bg-surface);
  box-shadow: var(--shadow-sm);
}

#page-receipts .receipt-summary-card::before {
  width: 3px;
}

#page-receipts .receipt-summary-card .ui-card-kpi__value {
  margin-top: var(--space-2);
  color: var(--receipt-accent, var(--color-info));
  font-size: clamp(1.65rem, 2.2vw, 2.25rem);
  overflow-wrap: anywhere;
}

#page-receipts .receipt-summary-card .ui-card-kpi__meta {
  margin-top: auto;
  padding-top: var(--space-3);
}

/* Add/edit form */
#page-receipts .receipts-form-card {
  margin: 0;
  padding: var(--space-5);
  border-radius: var(--radius-lg);
  background: var(--color-bg-surface);
  box-shadow: var(--shadow-sm);
}

#page-receipts .receipts-form-card .card-title {
  margin-bottom: var(--space-5);
}

#page-receipts .receipts-form-card .form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-4);
}

#page-receipts .receipts-form-card .form-grid > div {
  display: grid;
  min-width: 0;
  gap: var(--space-2);
}

#page-receipts .receipts-form-card .form-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-5);
  padding-top: var(--space-4);
  border-top: 1px solid var(--color-border-subtle);
}

/* Search, filters, and actions */
#page-receipts .receipts-toolbar {
  position: sticky;
  top: calc(var(--size-header) + var(--space-2));
  z-index: calc(var(--z-sticky) + 5);
  display: grid;
  grid-template-columns: minmax(15rem, 1.4fr) minmax(32rem, 2.6fr) auto;
  align-items: end;
  gap: var(--space-3);
  margin: 0;
  padding: var(--space-4);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-lg);
  background: color-mix(in srgb, var(--color-bg-surface) 94%, transparent);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(16px);
}

#page-receipts .receipts-toolbar .toolbar-search,
#page-receipts .receipts-toolbar .toolbar-filters,
#page-receipts .receipts-toolbar .toolbar-actions {
  min-width: 0;
}

#page-receipts .receipts-toolbar .toolbar-search {
  width: 100%;
}

#page-receipts .receipts-toolbar .toolbar-filters {
  display: grid;
  grid-template-columns: minmax(10rem, 1.25fr) minmax(8rem, 0.9fr) repeat(2, minmax(8.5rem, 1fr));
  gap: var(--space-3);
}

#page-receipts .receipts-toolbar .filter-field {
  display: grid;
  min-width: 0;
  gap: var(--space-2);
}

#page-receipts .receipts-toolbar .ui-input,
#page-receipts .receipts-toolbar .ui-select {
  width: 100%;
  min-width: 0;
}

#page-receipts .receipts-toolbar .toolbar-actions {
  justify-content: flex-end;
}

#page-receipts .action-menu {
  position: relative;
}

#page-receipts .action-menu > summary {
  min-width: 7.5rem;
  list-style: none;
}

#page-receipts .action-menu > summary::-webkit-details-marker {
  display: none;
}

#page-receipts .action-menu-list {
  z-index: var(--z-dropdown);
  min-width: 14rem;
  padding: var(--space-2);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  background: var(--color-bg-surface-elevated);
  box-shadow: var(--shadow-lg);
}

#page-receipts .action-menu-list .ui-btn {
  width: 100%;
  justify-content: flex-start;
}

/* Enterprise table adoption */
#page-receipts .receipts-table-container {
  --ui-table-min-width: 92rem;
  --ui-table-max-height: min(70vh, 46rem);
  margin: 0;
  overflow: auto;
  isolation: isolate;
}

#page-receipts .receipts-table {
  --receipt-view-row-height: 4.75rem;
  width: 92rem;
  min-width: 92rem;
  max-width: none;
  table-layout: fixed;
}

#page-receipts .receipts-table th,
#page-receipts .receipts-table td {
  box-sizing: border-box;
  height: auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: clip;
}

#page-receipts .receipts-table th:nth-child(1),
#page-receipts .receipts-table td:nth-child(1) {
  width: 8rem;
}

#page-receipts .receipts-table th:nth-child(2),
#page-receipts .receipts-table td:nth-child(2) {
  width: 17rem;
}

#page-receipts .receipts-table th:nth-child(3),
#page-receipts .receipts-table td:nth-child(3) {
  width: 10rem;
}

#page-receipts .receipts-table th:nth-child(4),
#page-receipts .receipts-table td:nth-child(4) {
  width: 11rem;
}

#page-receipts .receipts-table th:nth-child(5),
#page-receipts .receipts-table td:nth-child(5) {
  width: 13rem;
}

#page-receipts .receipts-table th:nth-child(6),
#page-receipts .receipts-table td:nth-child(6) {
  width: 19rem;
}

#page-receipts .receipts-table th:nth-child(7),
#page-receipts .receipts-table td:nth-child(7) {
  width: 13rem;
  min-width: 13rem;
}

#page-receipts .receipts-table td:nth-child(2) strong,
#page-receipts .receipts-table td:nth-child(2) .entity-subline {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  overflow-wrap: anywhere;
  white-space: normal;
  word-break: break-word;
}

#page-receipts .receipts-table .entity-subline {
  margin-top: var(--space-1);
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}

#page-receipts .receipts-table td:nth-child(5) strong,
#page-receipts .receipts-table td:nth-child(6) {
  max-width: 100%;
  overflow: hidden;
  overflow-wrap: anywhere;
  white-space: normal;
  word-break: break-word;
}

#page-receipts .receipts-table td:nth-child(6) {
  overflow: hidden;
}

#page-receipts .receipts-table td:last-child .ui-btn {
  min-height: var(--size-control-sm);
  margin: var(--space-0-5);
  padding-inline: var(--space-3);
  font-size: var(--font-size-sm);
}

#page-receipts .receipts-table thead th {
  z-index: calc(var(--z-sticky) + 6);
}

#page-receipts .receipts-table thead .ui-table-column--frozen-first,
#page-receipts .receipts-table thead .ui-table-column--frozen-last {
  z-index: calc(var(--z-sticky) + 8);
}

#page-receipts .receipts-table tbody .ui-table-column--frozen-first,
#page-receipts .receipts-table tbody .ui-table-column--frozen-last {
  /*
   * Frozen body cells only need to sit above neighbouring body cells while
   * scrolling horizontally. Keeping them below the thead stacking context
   * prevents Date/Manage values from painting over the sticky header.
   */
  z-index: calc(var(--z-sticky) - 1);
  background: var(--color-bg-surface);
  background-clip: padding-box;
}

#page-receipts .receipts-table tbody tr:nth-child(even) .ui-table-column--frozen-first,
#page-receipts .receipts-table tbody tr:nth-child(even) .ui-table-column--frozen-last {
  background: color-mix(in srgb, var(--color-bg-surface) 97%, var(--color-info));
}

#page-receipts .receipts-table tbody tr:hover .ui-table-column--frozen-first,
#page-receipts .receipts-table tbody tr:hover .ui-table-column--frozen-last {
  background: var(--color-bg-hover);
}

#page-receipts .receipts-table tbody input,
#page-receipts .receipts-table tbody select {
  min-width: 0;
}

#page-receipts .receipts-table tbody td:nth-child(2) .ui-input + .ui-input,
#page-receipts .receipts-table tbody td:nth-child(3) .ui-input + .ui-input {
  margin-top: var(--space-2) !important;
}

#page-receipts .receipts-table-container .table-head-label {
  display: inline-flex;
  width: 100%;
  min-width: 0;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

#page-receipts .receipts-table-container .sort-indicator,
#page-receipts .receipts-table-container .filter-icon-btn {
  color: var(--color-text-muted);
}

#page-receipts .receipts-table-container .sort-indicator.active,
#page-receipts .receipts-table-container .filter-icon-btn.active {
  color: var(--color-info);
}

#page-receipts .receipts-table-container .column-filter-popover {
  z-index: var(--z-popover);
  border-color: var(--color-border-default);
  background: var(--color-bg-surface-elevated);
  box-shadow: var(--shadow-overlay);
}

#page-receipts .receipts-table-container .table-filter-note {
  position: sticky;
  left: 0;
  padding: var(--space-2) var(--space-4);
  border-top: 1px solid var(--color-border-subtle);
  background: var(--color-bg-surface);
  color: var(--color-text-muted);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
}

#page-receipts .receipts-table .empty-row td {
  position: sticky;
  left: 0;
  height: 14rem;
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  text-align: center !important;
}

@media (max-width: 1440px) {
  #page-receipts .receipts-toolbar {
    grid-template-columns: minmax(14rem, 1fr) minmax(0, 2fr);
  }

  #page-receipts .receipts-toolbar .toolbar-actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
  }
}

@media (max-width: 1080px) {
  #page-receipts .receipts-kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #page-receipts .receipts-toolbar {
    position: static;
    grid-template-columns: minmax(0, 1fr);
  }

  #page-receipts .receipts-toolbar .toolbar-filters {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #page-receipts .receipts-toolbar .toolbar-actions {
    grid-column: auto;
    justify-content: flex-start;
  }
}

@media (max-width: 760px) {
  #page-receipts .receipts-page-header,
  #page-receipts .receipts-form-card .form-actions {
    align-items: stretch;
    flex-direction: column;
  }

  #page-receipts .receipts-kpi-grid,
  #page-receipts .receipts-form-card .form-grid,
  #page-receipts .receipts-toolbar .toolbar-filters {
    grid-template-columns: minmax(0, 1fr);
  }

  #page-receipts .receipts-form-card .form-actions .ui-btn {
    width: 100%;
  }
}

[data-theme="light"] #page-receipts .receipt-summary-card,
[data-theme="light"] #page-receipts .receipts-form-card,
[data-theme="light"] #page-receipts .receipts-toolbar {
  background: var(--color-bg-surface);
}

/* Keep long imported values inside their own columns. Several legacy table
   rules intentionally expose entity text; receipts need stricter containment
   because imported references and remarks can be unusually long. */
html[lang][data-theme] body #app-shell #page-receipts .receipts-table tbody td.entity-cell,
html[lang][data-theme] body #app-shell #page-receipts .receipts-table tbody td.receipt-reference-cell,
html[lang][data-theme] body #app-shell #page-receipts .receipts-table tbody td.receipt-remarks-cell {
  max-width: 100%;
  overflow: hidden !important;
  white-space: normal !important;
  overflow-wrap: anywhere !important;
  word-break: normal !important;
}

html[lang][data-theme] body #app-shell #page-receipts .receipts-table :is(
  .receipt-cell-primary,
  .receipt-cell-reference,
  .receipt-cell-notes,
  .entity-subline
) {
  display: -webkit-box !important;
  width: 100%;
  max-width: 100%;
  overflow: hidden !important;
  white-space: normal !important;
  text-overflow: ellipsis !important;
  overflow-wrap: anywhere !important;
  word-break: normal !important;
  -webkit-box-orient: vertical;
}

html[lang][data-theme] body #app-shell #page-receipts .receipts-table :is(
  .receipt-cell-primary,
  .entity-subline
) {
  -webkit-line-clamp: 1 !important;
  line-clamp: 1 !important;
}

html[lang][data-theme] body #app-shell #page-receipts .receipts-table :is(
  .receipt-cell-reference,
  .receipt-cell-notes
) {
  -webkit-line-clamp: 2 !important;
  line-clamp: 2 !important;
}

/* Read-only records use a uniform grid height. Editing rows remain auto-sized
   so their stacked form controls are never clipped. */
html[lang][data-theme] body #app-shell #page-receipts .receipts-table tbody tr.receipt-view-row {
  height: var(--receipt-view-row-height) !important;
}

html[lang][data-theme] body #app-shell #page-receipts .receipts-table tbody tr.receipt-view-row > td {
  height: var(--receipt-view-row-height) !important;
  max-height: var(--receipt-view-row-height) !important;
  padding-block: var(--space-2) !important;
  vertical-align: middle !important;
}

html[lang][data-theme] body #app-shell #page-receipts .receipts-table tbody tr.receipt-view-row .receipt-row-actions {
  height: calc(var(--receipt-view-row-height) - (2 * var(--space-2)));
}

/* Variable-height table rows and a horizontally frozen final table-cell do not
   share a reliable height in Chromium. Keep Manage in the normal table flow so
   its cell always matches the row and can never reveal text underneath it. */
html[lang][data-theme] body #app-shell #page-receipts .receipts-table thead .ui-table-column--frozen-last {
  right: auto !important;
  box-shadow: none !important;
}

html[lang][data-theme] body #app-shell #page-receipts .receipts-table tbody .ui-table-column--frozen-last {
  position: static !important;
  right: auto !important;
  z-index: auto !important;
  overflow: hidden !important;
  isolation: auto;
  box-shadow: none !important;
}

#page-receipts .receipt-row-actions {
  position: relative;
  display: flex;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  overflow: hidden;
}

#page-receipts .receipt-row-actions .ui-btn {
  flex: 0 0 auto;
  min-width: 4.25rem;
  margin: 0 !important;
}
