/* ==========================================================================
   AdminLTE Accessibility Styles - WCAG 2.1 AA Compliance
   ========================================================================== */

/* Skip Links - WCAG 2.4.1: Bypass Blocks */
.skip-link {
  position: absolute;
  top: -40px;
  left: 6px;
  z-index: 999999;
  padding: 8px 16px;
  font-weight: 600;
  color: var(--bs-white);
  text-decoration: none;
  background: var(--bs-primary);

  &:focus {
    top: 0;
    outline: 3px solid var(--bs-warning);
    outline-offset: 2px;
  }

  &:hover {
    // `--bs-primary-emphasis` does not exist; Bootstrap emits
    // `--bs-primary-text-emphasis`, which is dark in light mode and light in
    // dark mode — so pair it with the body background for the text colour.
    color: var(--bs-body-bg);
    text-decoration: none;
    background: var(--bs-primary-text-emphasis);
  }
}

/* Enhanced Focus Indicators - WCAG 2.4.7: Focus Visible */
.focus-enhanced {
  &:focus {
    outline: 3px solid var(--bs-focus-ring-color, #0d6efd);
    outline-offset: 2px;
    box-shadow: 0 0 0 .25rem rgba(13, 110, 253, .25);
  }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
  .card {
    border: 2px solid;
  }

  .btn {
    border-width: 2px;
  }

  .nav-link {
    border: 1px solid transparent;

    &:hover,
    &:focus {
      border-color: currentcolor;
    }
  }
}

/* Reduced Motion Support - WCAG 2.3.3: Animation from Interactions */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }

  .fade {
    opacity: 1 !important;
    /* stylelint-disable-next-line property-disallowed-list */
    transition: none !important;
  }

  .collapse {
    /* stylelint-disable-next-line property-disallowed-list */
    transition: none !important;
  }

  .modal.fade .modal-dialog {
    transform: none !important;
  }
}

/* Screen Reader Only Content */
.sr-only {
  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;
}

.sr-only-focusable:focus {
  position: static !important;
  width: auto !important;
  height: auto !important;
  padding: inherit !important;
  margin: inherit !important;
  overflow: visible !important;
  clip: auto !important;
  white-space: normal !important;
}

/* Focus Trap Utilities */
.focus-trap {
  &:focus {
    box-shadow: 0 0 0 .25rem rgba(13, 110, 253, .25);
  }
}

/* Accessible Color Combinations - WCAG 1.4.3: Contrast (Minimum) */
.text-accessible-primary {
  color: #003d82; /* 4.5:1 contrast on white */
}

.text-accessible-success {
  color: #0f5132; /* 4.5:1 contrast on white */
}

.text-accessible-danger {
  color: #842029; /* 4.5:1 contrast on white */
}

.text-accessible-warning {
  color: #664d03; /* 4.5:1 contrast on white */
}

/* ARIA Live Regions */
.live-region {
  position: absolute;
  left: -10000px;
  width: 1px;
  height: 1px;
  overflow: hidden;

  &.live-region-visible {
    position: static;
    left: auto;
    width: auto;
    height: auto;
    overflow: visible;
  }
}

/* Enhanced Error States - WCAG 3.3.1: Error Identification */
.form-control.is-invalid {
  border-color: var(--bs-danger);

  &:focus {
    border-color: var(--bs-danger);
    box-shadow: 0 0 0 .25rem rgba(220, 53, 69, .25);
  }
}

.invalid-feedback {
  &[role="alert"] {
    font-weight: 600;
  }
}

/* Target Size - WCAG 2.5.8: Target Size (Minimum) */
.touch-target {
  min-width: 44px;
  min-height: 44px;

  &.touch-target-small {
    min-width: 24px;
    min-height: 24px;
  }
}

/* Table Accessibility */
.table-accessible {
  th {
    font-weight: 600;
    background-color: var(--bs-secondary-bg);

    &[scope="col"] {
      border-bottom: 2px solid var(--bs-border-color);
    }

    &[scope="row"] {
      border-right: 2px solid var(--bs-border-color);
    }
  }

  caption {
    padding: .75rem;
    font-weight: 600;
    color: var(--bs-secondary);
    text-align: left;
    caption-side: top;
  }
}

/* Navigation Landmarks */
nav[role="navigation"] {
  &:not([aria-label]):not([aria-labelledby]) {
    &::before {
      position: absolute;
      left: -10000px;
      content: "Navigation";
    }
  }
}

/* Form Fieldset Styling */
fieldset {
  padding: 1rem;
  margin-bottom: 1rem;
  border: 1px solid var(--bs-border-color);

  legend {
    padding: 0 .5rem;
    margin-bottom: .5rem;
    font-size: 1.1em;
    font-weight: 600;
  }
}

/* Loading States */
.loading[aria-busy="true"] {
  position: relative;
  pointer-events: none;

  &::after {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin-top: -10px;
    margin-left: -10px;
    content: "";
    border: 2px solid var(--bs-primary);
    border-top-color: transparent;
    animation: spin 1s linear infinite;
  }

  @media (prefers-reduced-motion: reduce) {
    &::after {
      border-top-color: var(--bs-primary);
      animation: none;
    }
  }
}

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

/* Dark Mode Accessibility */
[data-bs-theme="dark"] {
  .text-accessible-primary {
    color: #6ea8fe;
  }

  .text-accessible-success {
    color: #75b798;
  }

  .text-accessible-danger {
    color: #f1aeb5;
  }

  .text-accessible-warning {
    color: #ffda6a;
  }
}

/* Print Accessibility */
@media print {
  // Printers drop backgrounds, so anything that takes its text colour from one
  // — a `.btn-primary`, a nav link on a coloured sidebar — would print white on
  // nothing. Force legible ink instead.
  .skip-link,
  .btn,
  .nav-link {
    color: #000 !important;
    background: transparent !important;
  }

  // A button should still read as a button on paper. Nav links are ordinary
  // text in print: the sidebar prints by design, so outlining every item put a
  // black box around the whole menu, which was never the intent (#6108).
  .btn {
    border: 1px solid #000 !important;
  }

  // The HTML5 Boilerplate convention: a reader holding paper cannot follow a
  // link, so print where it goes.
  a[href^="http"]::after {
    font-size: .8em;
    content: " (" attr(href) ")";
  }

  // `data-lte-print="plain"` — for pages that print as a document (invoice,
  // quote, work order, packing slip) rather than as a copy of a web page: no
  // URLs after links, no outline around buttons. Goes on <html>, <body> or any
  // container; text stays black on white either way (#6108).
  [data-lte-print~="plain"] {
    a[href^="http"]::after {
      content: none;
    }

    .btn {
      border-color: transparent !important;
    }
  }

  /* Print Layout — paged media cannot scroll, so every sticky position and
     every scroll container has to be unwound here or the browser clips what
     is inside it: that is what printed the main content blank in 4.0.0
     (#5982). `:not(.d-print-none)` on the rule that forces `display`, so
     Bootstrap's print display utilities keep working (#6106). */
  .app-header,
  .app-sidebar,
  .app-footer {
    position: static !important;
  }

  .app-wrapper:not(.d-print-none) {
    display: grid !important;
    grid-template-rows: auto 1fr auto !important;
    grid-template-columns: auto 1fr !important;
  }

  // `.app-main`'s bottom padding is breathing room above the fold; on paper it
  // is trailing whitespace, and 12px of it is enough to tip a page whose
  // content ends near a sheet boundary onto one more, empty sheet — which is
  // what UI/ribbons.html did. The bottom area's negative margin exists only to
  // cancel that padding, so it goes with it (#6113).
  .app-main {
    width: auto !important;
    padding-bottom: 0 !important;
  }

  .app-content-bottom-area {
    margin-bottom: 0 !important;
  }

  .app-main,
  .app-content,
  .sidebar-wrapper {
    overflow: visible !important;
  }

  .sidebar-wrapper {
    height: auto !important;
  }

  .app-sidebar {
    min-width: 200px !important;
    max-width: 200px !important;
    max-height: none !important;
    margin-left: 0 !important;
  }

  // Paper has no viewport to fill. `vh` and `vw` resolve against the page area
  // in print, so `min-height: 100vh` made the wrapper exactly one sheet tall
  // however short the content was: a three-line page still reserved a whole
  // sheet, the footer was pushed to the foot of it, and anything that followed
  // opened a second page. `max-width: 100vw` is the same measurement applied
  // sideways — it caps the layout at the width of one sheet, which says
  // nothing useful once the content is being paginated (#6113).
  .app-wrapper {
    min-height: 0 !important;
  }

  .app-wrapper,
  .app-header,
  .app-main,
  .app-footer {
    max-width: none !important;
  }

  // The shell — top bar, sidebar menu, footer — is navigation and chrome, not
  // content. Printing it spent the first 200px of every sheet on a menu the
  // reader cannot click and added the sidebar's own length to the page count,
  // so an invoice came out squeezed into a narrow column and two pages long
  // (#6113). v3 hid all three; v4 now does the same. Bring one back with
  // Bootstrap's `.d-print-block` / `.d-print-flex`, or the whole shell with
  // `data-lte-print="app"` on <html> or <body>, for a page that really does
  // print as a copy of the screen.
  html:not([data-lte-print~="app"]) body:not([data-lte-print~="app"]) {
    .app-header,
    .app-sidebar,
    .app-footer {
      &:not(.d-print-block):not(.d-print-flex) {
        display: none !important;
      }
    }
  }

  // The backdrop behind the off-canvas sidebar has nothing to print in any
  // mode.
  .sidebar-overlay {
    display: none !important;
  }
}
