/* ==========================================================================
   DeltaSmart Accessibility & Final Polish
   WCAG AA compliance, mobile optimization, print styles, and polish.
   All values reference --ds-* design tokens from design-tokens.css.
   ========================================================================== */

/* --------------------------------------------------------------------------
   Skip-to-Content Link
   Hidden offscreen until focused; appears at top of page for keyboard users.
   -------------------------------------------------------------------------- */

.ds-skip-link {
  position: absolute;
  top: -100%;
  left: var(--ds-space-4);
  z-index: calc(var(--ds-z-toast) + 1);
  background-color: var(--ds-brand-primary);
  color: var(--ds-text-on-primary);
  padding: var(--ds-space-2) var(--ds-space-4);
  border-radius: var(--ds-radius-md);
  font-size: var(--ds-font-size-sm);
  font-weight: var(--ds-font-weight-semibold);
  text-decoration: none;
  box-shadow: var(--ds-shadow-lg);
  transition: top var(--ds-duration-fast) var(--ds-ease-default);
}

.ds-skip-link:focus {
  top: var(--ds-space-2);
  outline: 2px solid var(--ds-text-on-primary);
  outline-offset: 2px;
}

/* --------------------------------------------------------------------------
   Screen Reader Only Utility
   Visually hidden but accessible to assistive technology.
   -------------------------------------------------------------------------- */

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

/* --------------------------------------------------------------------------
   Focus-Visible Rings
   Consistent, accessible focus indicators on ALL interactive elements.
   Uses :focus-visible so mouse users don't see the ring, keyboard users do.
   -------------------------------------------------------------------------- */

a:focus-visible,
button:focus-visible,
[role="button"]:focus-visible,
[role="tab"]:focus-visible,
[role="menuitem"]:focus-visible,
[tabindex]:focus-visible,
select:focus-visible,
textarea:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--ds-brand-primary);
  outline-offset: 2px;
}

/* Override for dark-background contexts (sidebar) */
.ds-sidebar a:focus-visible,
.ds-sidebar button:focus-visible {
  outline-color: var(--ds-brand-accent);
}

/* Remove default browser outline when :focus-visible applies */
a:focus:not(:focus-visible),
button:focus:not(:focus-visible),
[role="button"]:focus:not(:focus-visible),
[role="tab"]:focus:not(:focus-visible) {
  outline: none;
}

/* --------------------------------------------------------------------------
   Touch Target Sizing (Mobile)
   WCAG 2.5.5: minimum 44x44px touch targets on mobile devices.
   -------------------------------------------------------------------------- */

@media (max-width: 767.98px) {
  /* Buttons, links that act as buttons, and form controls */
  .btn,
  .ds-btn,
  .ds-nav-item,
  .ds-topbar-btn,
  .ds-topbar-hamburger,
  .ds-sidebar-toggle,
  .ds-sidebar-user,
  .page-link,
  .form-select,
  .form-control,
  .nav-link,
  .dropdown-item,
  .list-group-item-action {
    min-height: 44px;
    min-width: 44px;
  }

  /* Ensure pagination links have adequate touch area */
  .ds-pagination-pages .page-link {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Kanban cards need adequate tap area */
  .kanban-card {
    min-height: 44px;
  }
}

/* --------------------------------------------------------------------------
   Mobile Overflow Prevention
   Prevent horizontal overflow at narrow viewports (320px minimum).
   -------------------------------------------------------------------------- */

html {
  overflow-x: hidden;
}

body {
  overflow-x: hidden;
  max-width: 100vw;
}

/* Tables: always allow horizontal scroll within container */
.ds-table-responsive,
.table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

/* Prevent long text from causing overflow */
.ds-page-title,
.ds-topbar-title,
.ds-entity-card-title,
.ds-notification-message,
.kanban-card-title a {
  overflow-wrap: break-word;
  word-break: break-word;
}

/* Ensure images don't overflow containers */
img {
  max-width: 100%;
  height: auto;
}

/* Narrow viewport (320px) fixes */
@media (max-width: 374.98px) {
  .container-fluid {
    padding-left: var(--ds-space-2);
    padding-right: var(--ds-space-2);
  }

  .ds-page-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .ds-page-actions .btn {
    width: 100%;
    justify-content: center;
  }
}

/* --------------------------------------------------------------------------
   Lazy-Load Images
   Fade-in effect for images loaded with loading="lazy".
   -------------------------------------------------------------------------- */

img[loading="lazy"] {
  opacity: 0;
  transition: opacity var(--ds-duration-normal) var(--ds-ease-default);
}

img[loading="lazy"].loaded,
img[loading="lazy"][complete] {
  opacity: 1;
}

/* Fallback: show images that have loaded natively */
img[loading="lazy"] {
  opacity: 1;
}

/* --------------------------------------------------------------------------
   Hover States for Interactive Elements
   Consistent hover transitions on all clickable components.
   -------------------------------------------------------------------------- */

a,
button,
.btn,
.ds-btn,
.ds-nav-item,
.ds-topbar-btn,
.page-link,
.dropdown-item,
.list-group-item-action,
.kanban-card {
  transition: color var(--ds-duration-fast) var(--ds-ease-default),
              background-color var(--ds-duration-fast) var(--ds-ease-default),
              border-color var(--ds-duration-fast) var(--ds-ease-default),
              box-shadow var(--ds-duration-fast) var(--ds-ease-default),
              transform var(--ds-duration-fast) var(--ds-ease-default);
}

/* --------------------------------------------------------------------------
   Reduced Motion
   Respect user's motion preferences for accessibility.
   -------------------------------------------------------------------------- */

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

  .ds-skip-link {
    transition: none;
  }
}

/* --------------------------------------------------------------------------
   Color Contrast Helpers
   Ensure WCAG AA contrast (4.5:1 for normal text, 3:1 for large).
   -------------------------------------------------------------------------- */

/* Ensure badge text is readable on all backgrounds */
.badge.bg-warning {
  color: #000000;
}

.badge.bg-info {
  color: #000000;
}

/* Muted text meets minimum contrast on both light and dark backgrounds */
[data-bs-theme="dark"] .text-muted,
[data-bs-theme="dark"] .ds-text-muted {
  color: var(--ds-text-muted) !important;
}

/* --------------------------------------------------------------------------
   Print Stylesheet
   Hide non-essential UI, optimize for paper output.
   -------------------------------------------------------------------------- */

@media print {
  /* Hide navigation and interactive elements */
  .ds-sidebar,
  .ds-topbar,
  .ds-sidebar-backdrop,
  .ds-skip-link,
  .ds-quick-actions,
  .ds-search-card,
  .ds-pagination,
  .ds-page-actions,
  .ds-form-actions,
  .ds-card-header-actions,
  .kanban-filter-bar,
  .kanban-quickview,
  .kanban-quickview-backdrop,
  .ds-topbar-btn,
  .ds-theme-toggle,
  .dropdown,
  .toast-container,
  .modal,
  .modal-backdrop,
  footer,
  .btn,
  .ds-btn {
    display: none !important;
  }

  /* Reset layout for print */
  .ds-main {
    margin-left: 0 !important;
    padding-top: 0 !important;
  }

  body {
    background: #ffffff !important;
    color: #000000 !important;
    font-size: 12pt;
  }

  .container-fluid {
    padding: 0 !important;
    max-width: 100% !important;
  }

  /* Cards should be flat for print */
  .card,
  .ds-content-card,
  .ds-stat-card,
  .ds-entity-card {
    box-shadow: none !important;
    border: 1px solid #cccccc !important;
    break-inside: avoid;
  }

  /* Tables: ensure borders for readability */
  .ds-table,
  .table {
    border-collapse: collapse;
  }

  .ds-table td,
  .ds-table th,
  .table td,
  .table th {
    border: 1px solid #cccccc !important;
    padding: 4pt 8pt;
  }

  /* Links: show URL after link text */
  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #666666;
  }

  /* Don't show URL for nav links, buttons, or hash links */
  .ds-nav-item[href]::after,
  .ds-breadcrumb-link[href]::after,
  .ds-stat-card-link[href]::after,
  a[href^="#"]::after,
  a[href^="javascript"]::after {
    content: none;
  }

  /* Page breaks */
  h1, h2, h3, h4, h5, h6 {
    break-after: avoid;
  }

  .ds-page-header {
    break-inside: avoid;
  }

  /* Ensure dark mode doesn't affect print */
  * {
    color-adjust: exact;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

/* --------------------------------------------------------------------------
   ARIA Role Styling
   Visual affordances for custom component roles.
   -------------------------------------------------------------------------- */

/* Tab panels: ensure they're only visible when active */
[role="tabpanel"][hidden] {
  display: none;
}

/* Ensure status badges are announced properly */
.badge[role="status"] {
  /* Already visually styled; this just documents the semantic role */
}
