/* ================================================================
   MOBILE UI POLISH
   Comprehensive mobile enhancements for optimal touch experience
   ================================================================ */

/* ================================================================
   TOUCH TARGETS (44x44px Minimum - Apple HIG & Material Design)
   ================================================================ */

/* Ensure all interactive elements meet minimum touch target size */
@media (max-width: 768px) {
  a,
  button,
  input[type="submit"],
  input[type="button"],
  input[type="checkbox"],
  input[type="radio"],
  select,
  .btn,
  .card__link,
  .nav__item,
  .menu-toggle,
  .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,
  .article__content a {
    min-height: auto;
    min-width: auto;
    padding: 0.5rem 0.25rem;
    display: inline;
  }

  /* Exception for command symbol cards, grid children, not standalone buttons */
  .command-symbol-card {
    min-height: auto;
    min-width: auto;
    display: flex;
  }

  /* 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,
  .hamburger-menu,
  .close-btn {
    width: 44px;
    height: 44px;
    padding: 0;
  }
}

/* ================================================================
   MOBILE NAVIGATION ENHANCEMENTS
   ================================================================ */

@media (max-width: 768px) {
  /* Hamburger Menu - Improved spacing and tap target */
  .menu-toggle,
  .hamburger-menu {
    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(14, 165, 233, 0.1);
  }

  .menu-toggle span,
  .hamburger-menu span {
    display: block;
    width: 24px;
    height: 2px;
    background: var(--text-primary, #e2e8f0);
    transition: all 0.3s ease;
    position: relative;
  }

  .menu-toggle span::before,
  .menu-toggle span::after,
  .hamburger-menu span::before,
  .hamburger-menu span::after {
    content: '';
    position: absolute;
    width: 24px;
    height: 2px;
    background: var(--text-primary, #e2e8f0);
    transition: all 0.3s ease;
  }

  .menu-toggle span::before,
  .hamburger-menu span::before {
    top: -8px;
  }

  .menu-toggle span::after,
  .hamburger-menu span::after {
    bottom: -8px;
  }

  /* Mobile navigation menu */
  .nav--mobile {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-secondary, #1e293b);
    z-index: 1000;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .nav--mobile.is-active {
    transform: translateX(0);
  }

  /* Mobile nav items - generous spacing */
  .nav--mobile .nav__item {
    min-height: 56px;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.08));
    font-size: 1.125rem;
    -webkit-tap-highlight-color: rgba(14, 165, 233, 0.1);
  }

  .nav--mobile .nav__item:active {
    background: var(--bg-tertiary, #162032);
  }

  /* Prevent iOS momentum scrolling issues */
  .nav--mobile {
    position: fixed;
    touch-action: pan-y;
  }
}

/* ================================================================
   MOBILE-SPECIFIC SPACING ADJUSTMENTS
   ================================================================ */

@media (max-width: 768px) {
  /* Increase padding for better readability */
  body {
    padding-top: 60px; /* Account for fixed header */
  }

  .container {
    padding-left: 16px;
    padding-right: 16px;
  }

  /* Reduce hero text on small screens */
  .hero__title {
    font-size: var(--text-2xl, 1.75rem);
  }

  .hero__subtitle {
    font-size: var(--text-base, 1rem);
  }

  /* Section spacing */
  section {
    padding-top: 2rem;
    padding-bottom: 2rem;
  }

  /* Stats row stacks vertically */
  .stats-row {
    flex-direction: column;
    gap: 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: 8px;
    border: 2px solid var(--border-medium, rgba(255, 255, 255, 0.15));
    background: var(--bg-card, #1a2536);
    color: var(--text-primary, #e2e8f0);
    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-primary, #0ea5e9);
    outline-offset: 2px;
    border-color: var(--accent-primary, #0ea5e9);
  }

  /* Form labels - generous spacing */
  label {
    display: block;
    margin-bottom: 8px;
    font-size: var(--text-base);
    font-weight: 500;
    color: var(--text-secondary, #94a3b8);
  }

  /* 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: var(--text-lg);
    font-weight: 600;
    border-radius: 8px;
    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='%23e5e5e7' 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) {
  /* Single column card layout */
  .card-grid,
  .cards,
  .grid {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  /* Card spacing and sizing */
  .card {
    margin-bottom: 16px;
    border-radius: 12px;
    padding: 20px;
    -webkit-tap-highlight-color: rgba(14, 165, 233, 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-tertiary, #162032);
  }

  /* Card images */
  .card__image {
    border-radius: 8px;
    margin-bottom: 16px;
    width: 100%;
    height: auto;
  }

  /* Card headers */
  .card__header {
    margin-bottom: 12px;
  }

  .card__title {
    font-size: 1.25rem;
    line-height: 1.4;
    margin-bottom: 8px;
  }

  /* Card metadata */
  .card__meta {
    font-size: var(--text-sm);
    color: var(--text-secondary, #94a3b8);
    margin-bottom: 12px;
  }

  /* Card content */
  .card__content {
    font-size: var(--text-base);
    line-height: 1.6;
  }
}

/* ================================================================
   STICKY HEADERS FOR MOBILE
   ================================================================ */

@media (max-width: 768px) {
  /* Sticky header, position only, do NOT override display/flex/height
     which breaks the navbar's internal layout (hamburger menu, dropdowns) */
  .header,
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9999999;
    background: var(--navbar-bg, #1e293b);
    border-bottom: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.08));
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
  }

  /* Sticky table headers */
  .table--sticky thead {
    position: sticky;
    top: 60px;
    background: var(--bg-secondary, #1e293b);
    z-index: 10;
  }

  /* Sticky section headers */
  .section__header--sticky {
    position: sticky;
    top: 60px;
    background: var(--bg-primary, #0f172a);
    z-index: 10;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.08));
  }
}

/* ================================================================
   BOTTOM NAVIGATION IMPROVEMENTS
   ================================================================ */

@media (max-width: 768px) {
  /* Bottom navigation bar */
  .bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 64px;
    background: var(--bg-secondary, #1e293b);
    border-top: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.08));
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 90;
    padding-bottom: env(safe-area-inset-bottom); /* iOS notch support */
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.15);
  }

  /* 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-secondary, #94a3b8);
    font-size: var(--text-xs);
    transition: color 0.2s ease;
    -webkit-tap-highlight-color: rgba(14, 165, 233, 0.1);
  }

  .bottom-nav__item:active {
    background: var(--bg-tertiary, #162032);
  }

  .bottom-nav__item--active {
    color: var(--accent-primary, #0ea5e9);
  }

  /* 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) {
  /* Safe area insets for notch devices */
  @supports (padding: env(safe-area-inset-bottom)) {
    body {
      padding-top: max(60px, env(safe-area-inset-top));
    }

    .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));
    }

    .nav--mobile {
      padding-top: env(safe-area-inset-top);
    }
  }
}

/* ================================================================
   MOBILE TABLES
   ================================================================ */

@media (max-width: 768px) {
  /* Responsive table wrapper */
  .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -16px; /* Extend to edges */
    padding: 0 16px;
  }

  /* Table improvements */
  table {
    min-width: 100%;
    font-size: var(--text-sm);
  }

  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-subtle, rgba(255, 255, 255, 0.08));
      border-radius: 8px;
      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-secondary, #94a3b8);
    }
  }
}

/* ================================================================
   MOBILE PERFORMANCE OPTIMIZATIONS
   ================================================================ */

@media (max-width: 768px) {
  /* Reduce animation complexity on mobile */
  @media (prefers-reduced-motion: no-preference) {
    * {
      animation-duration: 0.3s !important;
      transition-duration: 0.3s !important;
    }
  }

  /* Optimize scrolling performance */
  .scrollable,
  .overflow-auto,
  .table-wrapper {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
  }

  /* Prevent horizontal overflow */
  body {
    overflow-x: hidden;
  }

  /* Media optimization */
  img,
  video,
  iframe {
    max-width: 100%;
    height: auto;
  }
}

/* ================================================================
   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(14, 165, 233, 0.1);
  }

  /* Skip to content link for mobile */
  .skip-to-content {
    position: fixed;
    top: -100px;
    left: 16px;
    z-index: 1001;
    background: var(--accent-primary, #0ea5e9);
    color: var(--bg-primary, #0f172a);
    padding: 12px 20px;
    border-radius: 8px;
    font-weight: 600;
    text-decoration: none;
    transition: top 0.3s ease;
  }

  .skip-to-content:focus {
    top: 16px;
  }
}

/* ================================================================
   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;
  }

  .nav--mobile {
    transition: none;
  }
}
