/* ============================================================
 * NextGen MIS — Document viewer
 * ------------------------------------------------------------
 * Per-row Inspect/Download actions + a full-screen modal that
 * pages through a record's documents with rotate/zoom controls.
 *
 * Used by the beneficiary + applicant detail pages via the
 * <x-doc-viewer> component. No backend route is required: the
 * existing `file.download.file` route serves files INLINE
 * (response()->file()), so the same URL feeds both the <img>
 * preview and the HTML5 `download` action.
 * ============================================================ */

/* ---------- Per-row actions (in the documents table) ---------- */
.ng-doc-actions {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  justify-content: flex-end;
  width: 100%;
}

.ng-doc-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text-muted);
  cursor: pointer;
  text-decoration: none;
  transition: background-color var(--motion-fast) var(--ease-standard),
              color var(--motion-fast) var(--ease-standard),
              border-color var(--motion-fast) var(--ease-standard);
}

.ng-doc-action:hover,
.ng-doc-action:focus-visible {
  background: var(--color-brand-primary-soft);
  border-color: var(--color-brand-primary-soft);
  color: var(--color-brand-primary);
  text-decoration: none;
}

/* Destructive row action (Delete), for the EDITABLE documents table on the
   registration edit form. The read-only detail pages carry Inspect/Download
   only. Resting state stays muted so the row reads as one set of icons; the
   danger colour appears on hover/focus, where the intent is confirmed. Both
   tokens flip in dark mode — this replaces a hardcoded #B65829 that did not. */
.ng-doc-action--danger:hover,
.ng-doc-action--danger:focus-visible {
  background: var(--color-feedback-danger-bg);
  border-color: var(--color-feedback-danger-bg);
  color: var(--color-feedback-danger);
}

.ng-doc-action > i {
  font-size: var(--font-size-md);
  line-height: 1;
}

/* Filename rendered as a quiet inspect trigger (keeps the link affordance) */
.ng-doc-filename {
  padding: 0;
  border: 0;
  background: none;
  color: var(--color-link);
  font-weight: var(--font-weight-medium);
  text-align: left;
  cursor: pointer;
}

.ng-doc-filename:hover,
.ng-doc-filename:focus-visible {
  color: var(--color-link-hover);
  text-decoration: underline;
}

/* Hover/focus tooltip explaining each row action (Inspect / Download). */
.ng-doc-action[data-tooltip] { position: relative; }
.ng-doc-action[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  /* Anchor to the icon's right edge and extend LEFT (into the table). A centred
     tooltip on the last column overflowed the scroll container's right edge,
     which tripped the DataTables horizontal-scroll fade ("more table" shadow). */
  right: 0;
  white-space: nowrap;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  background: var(--color-neutral-900);
  color: var(--color-text-inverse);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  line-height: 1.2;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--motion-fast) var(--ease-standard);
  z-index: 5;
}
.ng-doc-action[data-tooltip]:hover::after,
.ng-doc-action[data-tooltip]:focus-visible::after {
  opacity: 1;
}

/* ---------- Overlay + dialog ---------- */
.ng-doc-viewer {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  background: var(--color-bg-overlay);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity var(--motion-base) var(--ease-standard),
              visibility var(--motion-base) var(--ease-standard);
}

.ng-doc-viewer.is-open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.ng-doc-viewer__dialog {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(1360px, 100%);
  height: min(92vh, 960px);
  background: var(--color-bg-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  transform: translateY(8px) scale(0.985);
  transition: transform var(--motion-base) var(--ease-emphasis);
}

.ng-doc-viewer.is-open .ng-doc-viewer__dialog {
  transform: none;
}

/* Corner close */
.ng-doc-viewer__close {
  position: absolute;
  top: var(--space-3);
  right: var(--space-3);
  z-index: 2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background-color var(--motion-fast) var(--ease-standard),
              color var(--motion-fast) var(--ease-standard);
}

.ng-doc-viewer__close:hover,
.ng-doc-viewer__close:focus-visible {
  background: var(--color-bg-sunken);
  color: var(--color-text-strong);
}

.ng-doc-viewer__close::before {
  content: "";
  width: 18px;
  height: 18px;
  background: currentColor;
  -webkit-mask: url(/img/icons/phosphor/x.svg) center / contain no-repeat;
          mask: url(/img/icons/phosphor/x.svg) center / contain no-repeat;
}

/* ---------- Body: aside + main ---------- */
.ng-doc-viewer__body {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
}

.ng-doc-viewer__aside {
  flex: 0 0 368px;
  width: 368px;
  height: 100%;
  padding: var(--space-6);
  border-right: 1px solid var(--color-border-default);
  overflow-y: auto;
  background: var(--color-bg-raised);
}

/* Record identity header — anchors the panel (whose document is this?) */
.ng-doc-viewer__record {
  margin: 0 0 var(--space-5);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--color-border-default);
}

.ng-doc-viewer__record-name {
  margin: 0 0 2px;
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  line-height: 1.25;
  color: var(--color-text-strong);
}

.ng-doc-viewer__record-meta {
  margin: 0;
  font-size: var(--font-size-sm);
  font-variant-numeric: tabular-nums;
  color: var(--color-text-muted);
}

.ng-doc-viewer__main {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;
}

/* ---------- Aside content (record context) ---------- */
.ng-doc-viewer__panel-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin: 0 0 var(--space-3);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-text-faint);
}

.ng-doc-viewer__count-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--radius-full);
  background: var(--color-bg-sunken);
  color: var(--color-text-muted);
  font-size: var(--font-size-xs);
  letter-spacing: 0;
}

.ng-doc-viewer__facts {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));   /* two-column fact grid */
  gap: var(--space-4) var(--space-5);
}

.ng-doc-viewer__fact {
  display: grid;
  gap: 3px;
  margin: 0;
}

.ng-doc-viewer__fact dt {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-medium);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-text-faint);
}

.ng-doc-viewer__fact dd {
  margin: 0;
  font-size: var(--font-size-base);
  line-height: 1.4;
  color: var(--color-text-default);
}

/* Sections after the first get clear separation */
.ng-doc-viewer__facts + .ng-doc-viewer__panel-title {
  margin-top: var(--space-5);
}

.ng-doc-viewer__divider {
  height: 1px;
  margin: var(--space-5) 0;
  border: 0;
  background: var(--color-border-subtle);
}

.ng-doc-viewer__child {
  padding: var(--space-3);
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-lg);
  background: var(--color-bg-raised);
}

.ng-doc-viewer__child + .ng-doc-viewer__child {
  margin-top: var(--space-3);
}

.ng-doc-viewer__child-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.ng-doc-viewer__child-name {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-strong);
}

/* ---------- Main: toolbar ---------- */
.ng-doc-viewer__toolbar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  padding-right: var(--space-12);   /* clear the corner close button */
  border-bottom: 1px solid var(--color-border-default);
}

.ng-doc-viewer__doc-title {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ng-doc-viewer__tools {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  flex: 0 0 auto;
}

.ng-doc-viewer__tool {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  background: var(--color-bg-surface);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background-color var(--motion-fast) var(--ease-standard),
              color var(--motion-fast) var(--ease-standard),
              border-color var(--motion-fast) var(--ease-standard);
}

.ng-doc-viewer__tool:hover:not(:disabled),
.ng-doc-viewer__tool:focus-visible {
  background: var(--color-bg-sunken);
  color: var(--color-text-strong);
}

.ng-doc-viewer__tool:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ng-doc-viewer__tool::before {
  content: "";
  width: 16px;
  height: 16px;
  background: currentColor;
  -webkit-mask: var(--dv-icon) center / contain no-repeat;
          mask: var(--dv-icon) center / contain no-repeat;
}

.ng-doc-viewer__tool--rotate-ccw { --dv-icon: url(/img/icons/phosphor/arrow-counter-clockwise.svg); }
.ng-doc-viewer__tool--rotate-cw  { --dv-icon: url(/img/icons/phosphor/arrow-clockwise.svg); }
.ng-doc-viewer__tool--zoom-out   { --dv-icon: url(/img/icons/phosphor/minus.svg); }
.ng-doc-viewer__tool--zoom-in    { --dv-icon: url(/img/icons/phosphor/plus.svg); }

.ng-doc-viewer__tool-sep {
  width: 1px;
  align-self: stretch;
  margin: var(--space-1) var(--space-2);
  background: var(--color-border-default);
}

.ng-doc-viewer__zoom-value {
  min-width: 48px;
  text-align: center;
  font-size: var(--font-size-sm);
  font-variant-numeric: tabular-nums;
  color: var(--color-text-muted);
}

/* ---------- Main: stage ---------- */
.ng-doc-viewer__stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  overflow: hidden;            /* pan via drag (transform), not scrollbars */
  background: var(--color-bg-sunken);
  touch-action: none;          /* let the pointer drag pan instead of scrolling */
}

/* Base view hints click-to-magnify; while magnified a click resets; manual zoom = grab-to-pan. */
.ng-doc-viewer[data-kind="image"]              .ng-doc-viewer__stage { cursor: zoom-in; }
.ng-doc-viewer.is-magnified[data-kind="image"] .ng-doc-viewer__stage { cursor: zoom-out; }
.ng-doc-viewer.is-zoomed[data-kind="image"]    .ng-doc-viewer__stage { cursor: grab; }
.ng-doc-viewer.is-grabbing                     .ng-doc-viewer__stage { cursor: grabbing; }

.ng-doc-viewer__img {
  max-width: 100%;
  max-height: 100%;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);    /* subtle paper-lift off the sunken stage */
  background: var(--color-neutral-0);
  transform-origin: center center;
  transition: transform var(--motion-fast) var(--ease-standard);
  user-select: none;
  -webkit-user-drag: none;     /* suppress the native image drag-ghost */
}

/* While dragging, the image must track the pointer with no easing lag */
.ng-doc-viewer.is-grabbing .ng-doc-viewer__img { transition: none; }

/* Loading spinner over the stage while the image fetches */
.ng-doc-viewer__loader {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
}
.ng-doc-viewer.is-loading .ng-doc-viewer__loader { display: flex; }
.ng-doc-viewer.is-loading .ng-doc-viewer__img { visibility: hidden; }

.ng-doc-viewer__frame {
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--color-neutral-0);
}

.ng-doc-viewer__fallback {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  max-width: 320px;
  text-align: center;
  color: var(--color-text-muted);
}

.ng-doc-viewer__fallback::before {
  content: "";
  width: 48px;
  height: 48px;
  background: var(--color-text-disabled);
  -webkit-mask: url(/img/icons/phosphor/file.svg) center / contain no-repeat;
          mask: url(/img/icons/phosphor/file.svg) center / contain no-repeat;
}

/* visibility helpers toggled by JS */
.ng-doc-viewer[data-kind="image"] .ng-doc-viewer__frame,
.ng-doc-viewer[data-kind="image"] .ng-doc-viewer__fallback,
.ng-doc-viewer[data-kind="pdf"]   .ng-doc-viewer__img,
.ng-doc-viewer[data-kind="pdf"]   .ng-doc-viewer__fallback,
.ng-doc-viewer[data-kind="other"] .ng-doc-viewer__img,
.ng-doc-viewer[data-kind="other"] .ng-doc-viewer__frame {
  display: none;
}

/* rotate/zoom tools only make sense for images */
.ng-doc-viewer:not([data-kind="image"]) .ng-doc-viewer__tools {
  display: none;
}

/* ---------- Footer ---------- */
.ng-doc-viewer__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;   /* pager + actions sit together on the right */
  gap: var(--space-5);
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--color-border-default);
}

.ng-doc-viewer__pager {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.ng-doc-viewer__nav {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--color-border-default);
  border-radius: var(--radius-md);
  background: var(--color-bg-surface);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background-color var(--motion-fast) var(--ease-standard),
              color var(--motion-fast) var(--ease-standard);
}

.ng-doc-viewer__nav:hover:not(:disabled),
.ng-doc-viewer__nav:focus-visible {
  background: var(--color-bg-sunken);
  color: var(--color-text-strong);
}

.ng-doc-viewer__nav:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ng-doc-viewer__nav::before {
  content: "";
  width: 16px;
  height: 16px;
  background: currentColor;
  -webkit-mask: var(--dv-icon) center / contain no-repeat;
          mask: var(--dv-icon) center / contain no-repeat;
}

.ng-doc-viewer__nav--prev { --dv-icon: url(/img/icons/phosphor/caret-left.svg); }
.ng-doc-viewer__nav--next { --dv-icon: url(/img/icons/phosphor/caret-right.svg); }

.ng-doc-viewer__page {
  min-width: 56px;
  text-align: center;
  font-size: var(--font-size-sm);
  font-variant-numeric: tabular-nums;
  color: var(--color-text-muted);
}

.ng-doc-viewer__footer-actions {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

/* Hide the pager entirely when a record has a single document */
.ng-doc-viewer[data-single="true"] .ng-doc-viewer__pager {
  visibility: hidden;
}

/* ---------- Body scroll lock while open ---------- */
body.ng-doc-viewer-open {
  overflow: hidden;
}

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
  .ng-doc-viewer {
    padding: var(--space-3);
  }

  .ng-doc-viewer__dialog {
    height: min(92vh, 100%);
  }

  .ng-doc-viewer__body {
    flex-direction: column;
  }

  .ng-doc-viewer__aside {
    flex: 0 0 auto;
    width: 100%;
    max-height: 38%;
    border-right: 0;
    border-bottom: 1px solid var(--color-border-default);
  }
}
