/**
 * Mobile Polish
 *
 * Touch-friendly sizing, responsive spacing, and mobile-specific fixes.
 * Uses engine CSS variables (--text, --bg, --bg-alt, --accent, --border, etc.)
 */

/* ================================================================
   TOUCH TARGETS (44x44px Minimum - Apple HIG & Material Design)
   ================================================================ */

@media (max-width: 768px) {
  a,
  button,
  input[type="submit"],
  input[type="button"],
  input[type="checkbox"],
  input[type="radio"],
  select,
  .btn,
  .card__link,
  .nav-link,
  .social-link {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Exception for inline text links - add padding instead */
  p a,
  li a,
  .prose a {
    min-height: auto;
    min-width: auto;
    padding: 0.5rem 0.25rem;
    display: inline;
  }

  /* Checkbox and radio inputs need proper hit area */
  input[type="checkbox"],
  input[type="radio"] {
    width: 44px;
    height: 44px;
    cursor: pointer;
  }

  /* Icon-only buttons need explicit sizing */
  .btn--icon,
  .close-btn {
    width: 44px;
    height: 44px;
    padding: 0;
  }
}

/* ================================================================
   MOBILE NAVIGATION ENHANCEMENTS
   ================================================================ */

@media (max-width: 768px) {
  /* Hamburger Menu - Improved spacing and tap target */
  .navbar-toggle {
    width: 48px;
    height: 48px;
    padding: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    cursor: pointer;
    -webkit-tap-highlight-color: rgba(37, 99, 235, 0.1);
  }

  .navbar-toggle span {
    display: block;
    width: 24px;
    height: 2px;
    background: var(--text, #2e2e2e);
    transition: all 0.3s ease;
    position: relative;
  }

  .navbar-toggle span::before,
  .navbar-toggle span::after {
    content: '';
    position: absolute;
    width: 24px;
    height: 2px;
    background: var(--text, #2e2e2e);
    transition: all 0.3s ease;
  }

  .navbar-toggle span::before {
    top: -8px;
  }

  .navbar-toggle span::after {
    bottom: -8px;
  }

  /* Mobile nav items - generous spacing */
  .navbar-menu .nav-link {
    min-height: 56px;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border, #e1e4e8);
    font-size: 1.125rem;
    -webkit-tap-highlight-color: rgba(37, 99, 235, 0.1);
  }

  .navbar-menu .nav-link:active {
    background: var(--bg-alt, #f8f9fa);
  }
}

/* ================================================================
   MOBILE-SPECIFIC SPACING ADJUSTMENTS
   ================================================================ */

@media (max-width: 768px) {
  /* Reduce hero text on small screens */
  .hero__title {
    font-size: var(--text-2xl, 1.75rem);
  }

  .hero__subtitle {
    font-size: var(--text-base, 1rem);
  }

  /* Single column cards on mobile */
  .card-grid,
  .feature-grid {
    grid-template-columns: 1fr;
  }

  /* Tighter section spacing */
  .section {
    padding: var(--space-lg, 2rem) 0;
  }

  .section__inner {
    padding: 0 var(--space-sm, 0.75rem);
  }

  /* Stats row stacks vertically */
  .stats-row {
    flex-direction: column;
    gap: var(--space-md, 1.5rem);
  }

  /* Heading spacing */
  h1, h2, h3, h4, h5, h6 {
    margin-top: 1.5rem;
    margin-bottom: 1rem;
  }

  /* Paragraph spacing */
  p {
    margin-bottom: 1.25rem;
  }

  /* List spacing */
  ul, ol {
    margin-bottom: 1.25rem;
    padding-left: 1.5rem;
  }

  li {
    margin-bottom: 0.5rem;
  }
}

/* ================================================================
   ENHANCED MOBILE FORM INPUTS
   ================================================================ */

@media (max-width: 768px) {
  /* Larger, easier to tap inputs */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  input[type="tel"],
  input[type="url"],
  input[type="number"],
  textarea,
  select {
    min-height: 48px;
    padding: 12px 16px;
    font-size: 16px; /* Prevents iOS zoom on focus */
    border-radius: var(--radius, 6px);
    border: 2px solid var(--border, #e1e4e8);
    background: var(--bg, #fff);
    color: var(--text, #2e2e2e);
    width: 100%;
    -webkit-appearance: none;
    appearance: none;
  }

  /* Textarea specific */
  textarea {
    min-height: 120px;
    resize: vertical;
  }

  /* Focus states - highly visible */
  input:focus,
  textarea:focus,
  select:focus {
    outline: 3px solid var(--accent, #ff7a59);
    outline-offset: 2px;
    border-color: var(--accent, #ff7a59);
  }

  /* Form labels - generous spacing */
  label {
    display: block;
    margin-bottom: 8px;
    font-size: 0.9375rem;
    font-weight: 500;
    color: var(--text-light, #555);
  }

  /* Form groups */
  .form-group {
    margin-bottom: 24px;
  }

  /* Submit buttons */
  input[type="submit"],
  button[type="submit"],
  .btn--submit {
    min-height: 52px;
    padding: 14px 24px;
    font-size: 1.0625rem;
    font-weight: 600;
    border-radius: var(--radius, 6px);
    width: 100%;
    margin-top: 8px;
  }

  /* Remove iOS input shadows */
  input,
  textarea,
  select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
  }

  /* Improve iOS select dropdown */
  select {
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12'%3E%3Cpath fill='%23555' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 16px center;
    padding-right: 40px;
  }
}

/* ================================================================
   MOBILE-OPTIMIZED CARD LAYOUTS
   ================================================================ */

@media (max-width: 768px) {
  /* Card spacing and sizing */
  .card {
    margin-bottom: 16px;
    border-radius: var(--radius-lg, 12px);
    padding: 20px;
    -webkit-tap-highlight-color: rgba(37, 99, 235, 0.1);
  }

  /* Clickable cards - full tap area */
  .card--clickable,
  .card__link {
    display: block;
    text-decoration: none;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
  }

  .card--clickable:active {
    transform: scale(0.98);
    background: var(--bg-alt, #f8f9fa);
  }

  /* Card images */
  .card__image {
    border-radius: var(--radius, 6px);
    margin-bottom: 16px;
    width: 100%;
    height: auto;
  }

  /* Card titles */
  .card__title {
    font-size: 1.25rem;
    line-height: 1.4;
    margin-bottom: 8px;
  }

  /* Card metadata */
  .card__meta {
    font-size: 0.875rem;
    color: var(--text-light, #555);
    margin-bottom: 12px;
  }

  /* Card content */
  .card__content {
    font-size: 0.9375rem;
    line-height: 1.6;
  }
}

/* ================================================================
   STICKY HEADERS FOR MOBILE
   ================================================================ */

@media (max-width: 768px) {
  /* Sticky header */
  .site-header,
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    background: var(--nav-bg, #fff);
    border-bottom: 1px solid var(--border, #e1e4e8);
    box-shadow: var(--shadow-sm, 0 1px 3px rgba(0, 0, 0, 0.08));
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
  }

  /* Account for fixed header */
  body {
    padding-top: 60px;
  }

  /* Sticky table headers */
  .table--sticky thead {
    position: sticky;
    top: 60px;
    background: var(--bg-alt, #f8f9fa);
    z-index: 10;
  }

  /* Sticky section headers */
  .section__header--sticky {
    position: sticky;
    top: 60px;
    background: var(--bg, #fff);
    z-index: 10;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border, #e1e4e8);
  }
}

/* ================================================================
   BOTTOM NAVIGATION
   ================================================================ */

@media (max-width: 768px) {
  /* Bottom navigation bar */
  .bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 64px;
    background: var(--bg, #fff);
    border-top: 1px solid var(--border, #e1e4e8);
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 90;
    padding-bottom: env(safe-area-inset-bottom);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
  }

  /* Bottom nav items */
  .bottom-nav__item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 56px;
    text-decoration: none;
    color: var(--text-light, #555);
    font-size: 0.75rem;
    transition: color 0.2s ease;
    -webkit-tap-highlight-color: rgba(37, 99, 235, 0.1);
  }

  .bottom-nav__item:active {
    background: var(--bg-alt, #f8f9fa);
  }

  .bottom-nav__item--active {
    color: var(--accent, #ff7a59);
  }

  /* Bottom nav icons */
  .bottom-nav__icon {
    width: 24px;
    height: 24px;
    margin-bottom: 4px;
  }

  /* Add padding to page content to account for bottom nav */
  body.has-bottom-nav {
    padding-bottom: calc(64px + env(safe-area-inset-bottom));
  }
}

/* ================================================================
   iOS SAFE AREA SUPPORT
   ================================================================ */

@media (max-width: 768px) {
  @supports (padding: env(safe-area-inset-bottom)) {
    body {
      padding-top: max(60px, env(safe-area-inset-top));
    }

    .site-header,
    .navbar {
      padding-top: env(safe-area-inset-top);
      height: calc(60px + env(safe-area-inset-top));
    }

    .bottom-nav {
      padding-bottom: env(safe-area-inset-bottom);
      height: calc(64px + env(safe-area-inset-bottom));
    }
  }
}

/* ================================================================
   RESPONSIVE TABLES
   ================================================================ */

@media (max-width: 768px) {
  /* Responsive table wrapper */
  .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -16px;
    padding: 0 16px;
  }

  table {
    min-width: 100%;
    font-size: 0.875rem;
  }

  th,
  td {
    padding: 12px 8px;
    white-space: nowrap;
  }

  /* Stack tables vertically on very small screens */
  @media (max-width: 480px) {
    .table--stack {
      display: block;
    }

    .table--stack thead {
      display: none;
    }

    .table--stack tbody,
    .table--stack tr,
    .table--stack td {
      display: block;
      width: 100%;
    }

    .table--stack tr {
      margin-bottom: 16px;
      border: 1px solid var(--border, #e1e4e8);
      border-radius: var(--radius, 6px);
      padding: 12px;
    }

    .table--stack td {
      text-align: right;
      padding: 8px 0;
      position: relative;
      padding-left: 50%;
    }

    .table--stack td::before {
      content: attr(data-label);
      position: absolute;
      left: 0;
      width: 45%;
      padding-right: 10px;
      white-space: nowrap;
      text-align: left;
      font-weight: 600;
      color: var(--text-light, #555);
    }
  }
}

/* ================================================================
   ACCESSIBILITY IMPROVEMENTS
   ================================================================ */

/* Remove tap highlight on non-interactive elements */
div,
span,
p,
h1, h2, h3, h4, h5, h6,
img {
  -webkit-tap-highlight-color: transparent;
}

/* Add tap highlight to interactive elements */
@media (max-width: 768px) {
  a,
  button,
  input,
  select,
  textarea,
  [role="button"],
  [tabindex]:not([tabindex="-1"]) {
    -webkit-tap-highlight-color: rgba(37, 99, 235, 0.1);
  }

  /* Skip to content link for mobile */
  .skip-to-content {
    position: fixed;
    top: -100px;
    left: 16px;
    z-index: 1001;
    background: var(--accent, #ff7a59);
    color: #fff;
    padding: 12px 20px;
    border-radius: var(--radius, 6px);
    font-weight: 600;
    text-decoration: none;
    transition: top 0.3s ease;
  }

  .skip-to-content:focus {
    top: 16px;
  }
}

/* ================================================================
   PREVENT HORIZONTAL OVERFLOW
   ================================================================ */

body {
  overflow-x: hidden;
}

img,
video,
iframe {
  max-width: 100%;
  height: auto;
}

/* ================================================================
   RESPECT REDUCED MOTION PREFERENCE
   ================================================================ */

/* ================================================================
   MOBILE CONTAINER & SECTION SPACING
   ================================================================ */

@media (max-width: 768px) {
  .container {
    padding-left: 16px;
    padding-right: 16px;
  }

  /* Mobile animation duration cap */
  * {
    animation-duration: max(0.01ms, min(var(--animation-duration, 0.3s), 0.3s));
  }
}

/* Scrollable smooth scroll helper */
.scrollable {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}

/* ================================================================
   RESPECT REDUCED MOTION PREFERENCE
   ================================================================ */

@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;
  }
}
