/**
 * OTP Design System - Utilities
 * Responsive helpers, layout utilities, and visibility classes
 * Part of Phase 3: CSS Consolidation
 *
 * This file contains:
 * - Responsive helpers (mobile-only, desktop-only)
 * - Print styles and visibility utilities
 * - Mobile-specific navigation overrides
 * - Responsive breakpoints for all components
 */

/* ============================================================================
   RESPONSIVE NAVIGATION
   Mobile-first approach with desktop enhancements
   ============================================================================ */

/* Mobile-first: hide desktop-only items by default */
.desktop-only {
  display: none;
}

.mobile-only {
  display: block;
}

/* Desktop breakpoint (768px and up) */
@media (min-width: 768px) {
  .desktop-only {
    display: block;
  }

  .mobile-only {
    display: none;
  }
}

/* Tablet breakpoint (1024px and up) - optional for finer control */
@media (min-width: 1024px) {
  nav ul li {
    padding: 0.5rem 0.75rem;
  }
}

/* ============================================================================
   TABLET RESPONSIVE SIDEBAR
   Auto-collapse sidebar on tablet devices (768px - 1024px)

   RCA (Root Cause Analysis):
   The original responsive design used a binary mobile/desktop approach with
   a single 768px breakpoint. On tablets (768px-1024px), the full 240px sidebar
   consumed 25-30% of the screen width, leaving insufficient space for main
   content. This was especially problematic in portrait orientation.

   Fix:
   Auto-collapse the sidebar on tablet screens to show only icons (64px),
   which increases main content area by ~175px while maintaining navigation
   visibility. Users can still expand the sidebar by clicking the toggle.
   ============================================================================ */

@media (min-width: 768px) and (max-width: 1023px) {
  /*
   * TABLET RESPONSIVE SIDEBAR - Auto-collapse on tablet devices
   *
   * On tablets, the sidebar defaults to a collapsed icon-only state to maximize
   * content area. Users can still manually expand by clicking the toggle button.
   * The .collapsed class is controlled by Datastar and persisted to localStorage.
   */

  /* Default: Collapse sidebar on tablets when user has explicitly collapsed */
  .app-layout:has(.app-sidebar.collapsed) {
    grid-template-columns: var(--sidebar-width-collapsed) 1fr;
  }

  .app-sidebar.collapsed {
    width: var(--sidebar-width-collapsed);
  }

  /* Also collapse when no explicit preference (first visit behavior) */
  .app-layout {
    grid-template-columns: var(--sidebar-width-collapsed) 1fr;
  }

  .app-sidebar {
    width: var(--sidebar-width-collapsed);
  }

  /* Default collapsed styles for tablet */
  .app-sidebar .sidebar-link .link-label {
    opacity: 0;
    width: 0;
    overflow: hidden;
  }

  .app-sidebar .group-label {
    display: none;
    visibility: hidden;
    width: 0;
    overflow: hidden;
  }

  .app-sidebar .sidebar-group-header .chevron {
    display: none;
  }

  .app-sidebar .sidebar-group-header {
    padding: 0.75rem 1rem;
  }

  .app-sidebar .sidebar-link {
    justify-content: center;
    padding: var(--spacing-sm);
  }

  .app-sidebar .sidebar-link[aria-current="page"] {
    padding-left: var(--spacing-sm);
  }

  .app-sidebar .sidebar-link .link-icon {
    width: auto;
  }

  /* Tooltip on hover for collapsed icons */
  .app-sidebar .sidebar-link::after {
    content: attr(title);
    position: absolute;
    left: calc(100% + var(--spacing-sm));
    top: 50%;
    transform: translateY(-50%);
    background: var(--pico-tooltip-background-color, #333);
    color: var(--pico-tooltip-color, white);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: var(--pico-border-radius);
    font-size: var(--font-size-xs);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-fast);
    z-index: 1000;
    box-shadow: var(--shadow-md, 0 2px 8px rgba(0,0,0,0.15));
  }

  .app-sidebar .sidebar-link:hover::after {
    opacity: 1;
  }

  /* Tablet: Collapse sidebar header */
  .app-sidebar .sidebar-header {
    flex-direction: column;
    gap: var(--spacing-sm);
    padding: var(--spacing-sm);
  }

  .app-sidebar .sidebar-logo {
    justify-content: center;
  }

  .app-sidebar .logo-text {
    opacity: 0;
    width: 0;
    overflow: hidden;
  }

  .app-sidebar .sidebar-collapse-btn {
    margin: 0 auto;
  }

  /* Tablet: Collapse quick actions */
  .app-sidebar .sidebar-quick-actions {
    padding: var(--spacing-xs) var(--spacing-xs);
  }

  .app-sidebar .sidebar-action-link {
    justify-content: center;
    padding: var(--spacing-sm);
  }

  .app-sidebar .action-label,
  .app-sidebar .action-kbd {
    opacity: 0;
    width: 0;
    overflow: hidden;
    position: absolute;
  }

  /* Tooltip for collapsed quick actions */
  .app-sidebar .sidebar-action-link::after {
    content: attr(title);
    position: absolute;
    left: calc(100% + var(--spacing-sm));
    top: 50%;
    transform: translateY(-50%);
    background: var(--pico-tooltip-background-color, #333);
    color: var(--pico-tooltip-color, white);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: var(--pico-border-radius);
    font-size: var(--font-size-xs);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-fast);
    z-index: 1000;
    box-shadow: var(--shadow-md, 0 2px 8px rgba(0,0,0,0.15));
  }

  .app-sidebar .sidebar-action-link:hover::after {
    opacity: 1;
  }

  /* Tablet: Collapse user section */
  .app-sidebar .sidebar-user-summary {
    justify-content: center;
  }

  .app-sidebar .user-info {
    display: none;
  }

  .app-sidebar .sidebar-user-menu {
    left: 100%;
    bottom: 0;
    right: auto;
    min-width: 180px;
    margin-bottom: 0;
    margin-left: var(--spacing-xs);
    box-shadow: 4px 0 12px rgba(0, 0, 0, 0.1);
  }
}

/* Mobile dropdown improvements */
@media (max-width: 767px) {
  /* Make mobile dropdown overlay on top of content */
  .mobile-only details.dropdown {
    position: relative;
  }

  .mobile-only details.dropdown[open] > ul {
    position: absolute !important;
    right: 0;
    top: 100%;
    box-shadow: var(--pico-box-shadow);
    border: 1px solid var(--pico-muted-border-color);
    background: var(--pico-background-color);
    margin-top: 0.5rem;
    min-width: 280px;
    max-width: calc(100vw - 2rem);
    max-height: calc(100vh - 100px);
    overflow-y: auto;
    z-index: 999;
    pointer-events: auto;
  }

  /* Increase tap targets on mobile */
  .mobile-only details.dropdown ul li {
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .mobile-only details.dropdown ul li a {
    padding: 0.75rem 1rem;
    display: block;
    color: var(--pico-color);
    text-decoration: none;
    cursor: pointer;
    pointer-events: auto;
    position: relative;
    z-index: 1;
  }

  .mobile-only details.dropdown ul li a:hover,
  .mobile-only details.dropdown ul li a:active,
  .mobile-only details.dropdown ul li a:focus {
    background-color: var(--pico-secondary-hover);
  }

  .mobile-only details.dropdown ul li strong {
    padding: 0.75rem 1rem;
    display: block;
    color: var(--pico-primary);
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  .mobile-only details.dropdown ul li small {
    padding: 0.25rem 1rem;
    display: block;
    opacity: 0.7;
  }

  /* Make hamburger button more prominent */
  .mobile-only summary {
    font-size: 1.5rem;
    padding: 0.5rem 1rem;
    cursor: pointer;
  }

  /* Dividers in mobile menu */
  .mobile-only details.dropdown ul li hr {
    margin: 0.5rem 0;
  }
}

/* ============================================================================
   SEARCH COMPONENT RESPONSIVENESS
   Mobile adjustments for search modal and results
   ============================================================================ */

@media (max-width: 768px) {
  .global-search-container {
    flex: 1;
    max-width: 100%;
    margin: 0.5rem 0;
  }

  .search-results {
    max-height: 300px;
  }

  dialog#search-modal article.search-modal-article {
    margin: 1rem;
    padding: 0.75rem;
    max-width: calc(100vw - 2rem);
    max-height: calc(100vh - 2rem);
  }

  #search-results-container {
    max-height: calc(100vh - 180px);
  }
}

/* ============================================================================
   FORM RESPONSIVENESS
   Mobile adjustments for forms and field layouts
   ============================================================================ */

@media (max-width: 768px) {
  .form-container {
    margin: 1rem auto;
  }

  .field-row {
    grid-template-columns: 1fr;
  }

  .form-actions {
    flex-direction: column-reverse;
  }

  .form-actions button,
  .form-actions a[role="button"] {
    width: 100%;
  }

  .inline-fields {
    flex-direction: column;
  }

  .inline-fields .field-short {
    width: 100%;
  }

  .date-range {
    flex-direction: column;
    align-items: stretch;
  }

  .date-range-separator {
    padding: 0;
    text-align: center;
  }
}

/* ============================================================================
   DETAIL VIEW RESPONSIVENESS
   Mobile adjustments for detail pages
   ============================================================================ */

@media (max-width: 768px) {
  .detail-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .metadata-grid {
    grid-template-columns: 1fr;
  }

  .detail-tabs a {
    padding: 0.5rem 0.75rem;
    font-size: 0.8rem;
  }

  .action-bar {
    width: 100%;
  }

  .action-bar button,
  .action-bar a {
    flex: 1;
    min-width: 0;
  }
}

/* ============================================================================
   LIST VIEW RESPONSIVENESS
   Mobile adjustments for tables and list views
   ============================================================================ */

@media (max-width: 768px) {
  table th,
  table td {
    padding: 0.375rem;
    font-size: 0.75rem;
  }

  .compact-button {
    padding: 0.25rem 0.5rem;
    font-size: 0.7rem;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
}

/* ============================================================================
   PRINT STYLES
   Styles for PDF export and print media
   ============================================================================ */

/* Hide elements that shouldn't appear in PDF */
.no-print {
    display: none !important;
}

/* Print media query for actual printing */
@media print {
    /* Hide navigation and UI elements */
    header,
    nav,
    footer,
    .no-print,
    button,
    details.dropdown {
        display: none !important;
    }

    /* Optimize page for printing */
    body {
        margin: 0;
        padding: 0;
    }

    main {
        margin: 0 !important;
        padding: 1rem !important;
    }

    /* Remove page breaks inside elements */
    table,
    figure,
    article {
        page-break-inside: avoid;
    }

    /* Ensure links are visible in print */
    a {
        text-decoration: underline;
    }

    /* Show link URLs in print (optional) */
    a[href]:after {
        content: " (" attr(href) ")";
        font-size: 0.8em;
        color: #666;
    }

    /* Don't show URLs for internal links */
    a[href^="#"]:after,
    a[href^="/"]:after {
        content: "";
    }
}
