/* ============================================
   Mobile Optimizations for Idle Crusade
   ============================================ */

/* Z-Index Management for Mobile
   Ensures proper stacking order on mobile devices
   ============================================ */
@media (max-width: 768px) {
  /* Z-Index Hierarchy (from bottom to top):
     1. Base content (z-index: 1)
     10. Sticky controls (z-index: 9-10)
     50. Dropdowns/Tooltips (z-index: 50-100)
     1000. Navigation (z-index: 1000)
     2000. Modals (z-index: 2000+)
  */

  /* Base content layer */
  .profile-hero,
  .battle-card,
  .main-content {
    position: relative;
    z-index: 1;
  }

  /* Sticky controls layer */
  .skills-controls,
  .inventory-controls {
    z-index: 9;
  }

  /* Dropdown/tooltip layer */
  .more-btn-wrapper {
    z-index: 50;
  }

  .more-menu,
  .tooltip,
  .dropdown-menu {
    z-index: 999;
  }

  /* Navigation layer - always on top of content */
  .top-bar,
  #topBar,
  .nav-container {
    z-index: 1000 !important;
  }

  /* Modal layer - above everything */
  .inventory-modal,
  .modal,
  .modal-backdrop {
    z-index: 2000 !important;
  }

  .toast-container {
    z-index: 2100 !important;
  }
}

/* Touch-friendly tap targets (minimum 44x44px) */
@media (max-width: 768px) {
  /* Ensure all interactive elements are at least 44px */
  button,
  a,
  .activity-card,
  .skill-card,
  .nav-link,
  .inventory-slot,
  .equipment-slot,
  input[type="button"],
  input[type="submit"],
  select {
    min-height: 44px;
    min-width: 44px;
  }

  /* Larger touch targets for primary actions */
  .btn-primary,
  .start-training-btn,
  .start-combat-btn,
  .start-boss-combat-btn {
    min-height: 52px;
    font-size: var(--font-size-base);
    padding: var(--space-2) var(--space-4);
  }

  /* Improve spacing between tappable elements */
  .activities-grid {
    gap: var(--space-3);
  }

  .skills-grid {
    gap: var(--space-3);
  }

  /* Larger navbar items */
  .nav-link {
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-size-base);
  }

  /* Mobile-friendly navigation */
  .nav-links {
    gap: var(--space-1);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .nav-links::-webkit-scrollbar {
    display: none;
  }

  /* Sticky navigation on mobile */
  .top-bar {
    position: sticky;
    top: 0;
    z-index: 1000;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-default);
  }

  /* Full-width containers on mobile */
  .main-container {
    padding: var(--space-2);
    max-width: 100%;
  }

  .profile-container,
  .combat-container,
  .map-container,
  .leaderboard-page {
    padding: var(--space-2);
    max-width: 100%;
  }

  /* Stack sidebar below content on mobile */
  .battle-container {
    grid-template-columns: 1fr;
  }

  .sidebar {
    order: 2;
    position: relative !important;
    top: 0 !important;
    max-height: none;
  }

  .main-battle-area {
    order: 1;
  }

  /* Improve readability */
  body {
    font-size: 16px; /* Prevent iOS zoom on input focus */
  }

  h1 {
    font-size: clamp(1.5rem, 5vw, 2rem);
  }

  h2 {
    font-size: clamp(1.25rem, 4vw, 1.75rem);
  }

  h3 {
    font-size: clamp(1.125rem, 3.5vw, 1.5rem);
  }

  /* Larger form inputs */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  select,
  textarea {
    min-height: 44px;
    font-size: 16px; /* Prevent iOS zoom */
    padding: var(--space-2);
  }

  /* Mobile-friendly modals */
  .inventory-modal,
  .modal {
    z-index: 2000 !important; /* Above everything including navigation */
  }

  .inventory-modal-content,
  .modal-content {
    max-width: 95vw;
    max-height: 90vh;
    margin: var(--space-2);
    position: relative;
    z-index: 2001;
  }

  /* Scrollable modal content */
  .inventory-grid,
  .modal-body {
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Larger inventory slots */
  .inventory-slot {
    min-height: 80px;
    padding: var(--space-2);
  }

  .inventory-slot-image {
    width: 40px;
    height: 40px;
  }

  /* Improve activity cards */
  .activity-card {
    padding: var(--space-3);
  }

  .activity-card-name {
    font-size: var(--font-size-lg);
  }

  /* Better skill cards */
  .skill-card {
    padding: var(--space-3);
  }

  /* Horizontal scrolling for tabs */
  .combat-tabs,
  .inventory-tabs,
  .leaderboard-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
  }

  .combat-tabs::-webkit-scrollbar,
  .inventory-tabs::-webkit-scrollbar,
  .leaderboard-tabs::-webkit-scrollbar {
    display: none;
  }

  /* Improve table readability */
  .leaderboard-table {
    font-size: var(--font-size-sm);
  }

  .leaderboard-table th,
  .leaderboard-table td {
    padding: var(--space-2);
  }

  /* Mobile-friendly XP bar */
  .xp-bar-container {
    height: 32px;
  }

  .xp-bar-center {
    font-size: var(--font-size-sm);
  }

  /* Improve combat UI */
  .combatant-avatar {
    width: 56px;
    height: 56px;
  }

  .hp-bar-single {
    height: 28px;
  }

  .hp-text-overlay {
    font-size: var(--font-size-xs);
  }

  /* Better stance selector */
  .stance-segmented {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-1);
  }

  .stance-option {
    padding: var(--space-2);
    font-size: var(--font-size-sm);
  }

  /* Improve stats display */
  .stats-compact {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-2);
  }

  /* Mobile-friendly equipment grid */
  .equipment-grid {
    grid-template-columns: 1fr;
    gap: var(--space-2);
  }

  /* Better area cards */
  .area-card {
    padding: var(--space-3);
  }

  /* Improve map controls */
  .map-controls {
    flex-direction: column;
    gap: var(--space-2);
  }

  .map-controls-left,
  .map-controls-right {
    width: 100%;
  }

  /* Mobile-friendly player list */
  .player-list-item {
    padding: var(--space-2);
  }

  .player-list-portrait {
    width: 36px;
    height: 36px;
  }

  /* Improve chat */
  .chat-container {
    bottom: 0;
    max-height: 50vh;
  }

  .chat-messages {
    max-height: 40vh;
  }

  .chat-input-container {
    padding: var(--space-2);
  }

  /* Better toast notifications */
  .toast-container {
    bottom: var(--space-2);
    left: var(--space-2);
    right: var(--space-2);
    max-width: calc(100% - var(--space-4));
  }

  .toast {
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-size-sm);
  }

  /* Improve profile page */
  .profile-hero {
    flex-direction: column;
    align-items: flex-start;
    padding: var(--space-3);
    position: relative;
    z-index: 1; /* Below navigation */
  }

  .hero-kpis {
    width: 100%;
    justify-content: space-between;
  }

  .profile-content-grid {
    grid-template-columns: 1fr;
  }

  /* Fix more button wrapper positioning */
  .more-btn-wrapper {
    position: relative;
    z-index: 50; /* Below navigation but above content */
  }

  .more-menu {
    position: fixed !important;
    top: auto !important;
    right: var(--space-2) !important;
    margin-top: var(--space-1);
    z-index: 999 !important; /* Below navigation */
  }

  /* Ensure navigation is always on top */
  .top-bar,
  #topBar {
    z-index: 1000 !important;
    position: sticky !important;
    top: 0 !important;
  }

  .nav-container,
  .nav-links,
  .nav-link {
    position: relative;
    z-index: inherit;
  }

  /* Fix sticky controls on profile to not overlap nav */
  .skills-controls {
    top: calc(var(--navbar-height) + var(--space-1)) !important;
    z-index: 9 !important; /* Below navigation */
  }

  /* Better skills grid on profile */
  .skills-compact-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: var(--space-2);
  }

  /* Improve inventory on profile */
  .inventory-compact-grid {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: var(--space-2);
  }

  /* Mobile-friendly leaderboard */
  .leaderboard-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Better boss battle UI */
  .battle-card {
    padding: var(--space-3);
  }

  /* Improve food sidebar */
  #sidebarFoodCard {
    padding: var(--space-2);
  }

  .sidebar-food-item {
    padding: var(--space-2);
  }

  /* Combat log improvements */
  #combatLog {
    font-size: var(--font-size-xs);
    max-height: 200px;
  }

  /* Reduce animations on mobile for performance */
  @media (prefers-reduced-motion: no-preference) {
    * {
      animation-duration: 0.2s !important;
      transition-duration: 0.15s !important;
    }
  }

  /* Prevent text selection on interactive elements */
  button,
  .activity-card,
  .skill-card,
  .nav-link,
  .tab {
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  /* Improve focus states for accessibility */
  button:focus-visible,
  a:focus-visible,
  input:focus-visible,
  select:focus-visible {
    outline: 2px solid var(--brand-primary);
    outline-offset: 2px;
  }

  /* Better spacing for sections */
  .section-header {
    margin-bottom: var(--space-3);
  }

  /* Improve panel headers */
  .panel-header {
    padding-bottom: var(--space-2);
    margin-bottom: var(--space-3);
  }

  /* Mobile-friendly dropdown */
  select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23adbac7' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-2) center;
    padding-right: var(--space-6);
  }

  /* Improve status bar */
  .status-bar {
    gap: var(--space-1);
    padding: var(--space-1) 0;
  }

  .status-pill {
    padding: 6px var(--space-2);
    font-size: var(--font-size-xs);
  }

  /* Better activity filters */
  .activity-filters {
    flex-direction: column;
    gap: var(--space-2);
    align-items: stretch;
  }

  .material-filter {
    width: 100%;
  }

  /* Improve empty states */
  .empty-state {
    padding: var(--space-6);
  }

  .empty-state-icon {
    font-size: 2.5rem;
  }

  /* Better loading states */
  .loading-state {
    padding: var(--space-6);
  }
}

/* Extra small devices (phones in portrait) */
@media (max-width: 480px) {
  /* Even larger touch targets */
  .btn-primary,
  .start-training-btn {
    min-height: 56px;
    font-size: var(--font-size-lg);
  }

  /* Reduce padding for more content */
  .main-container {
    padding: var(--space-1);
  }

  /* Single column layouts */
  .skills-compact-grid,
  .inventory-compact-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .stats-compact {
    grid-template-columns: 1fr;
  }

  /* Smaller avatars */
  .combatant-avatar {
    width: 48px;
    height: 48px;
  }

  .profile-avatar {
    width: 48px;
    height: 48px;
  }

  /* Compact navigation */
  .nav-link span {
    display: none;
  }

  .nav-link {
    padding: var(--space-2);
  }

  /* Smaller modals */
  .inventory-modal-content {
    padding: var(--space-2);
  }

  /* Compact tables */
  .leaderboard-table {
    font-size: var(--font-size-xs);
  }

  .leaderboard-table th,
  .leaderboard-table td {
    padding: var(--space-1);
  }

  /* Hide less important info */
  .player-tooltip-level,
  .area-tooltip-level {
    display: none;
  }

  /* Compact hero section */
  .profile-hero {
    padding: var(--space-2);
  }

  .hero-kpi {
    font-size: var(--font-size-xs);
  }

  /* Smaller inventory items */
  .inventory-slot-image {
    width: 32px;
    height: 32px;
  }

  .inventory-slot-name {
    font-size: 9px;
  }

  /* Compact activity cards */
  .activity-card {
    padding: var(--space-2);
  }

  .activity-card-name {
    font-size: var(--font-size-base);
  }

  /* Smaller skill tiles */
  .skill-tile {
    min-height: 80px;
  }

  .skill-icon-ring {
    width: 36px;
    height: 36px;
  }

  .skill-icon {
    width: 20px;
    height: 20px;
  }
}

/* Landscape mode optimizations */
@media (max-width: 768px) and (orientation: landscape) {
  /* Reduce vertical spacing in landscape */
  .main-container {
    padding: var(--space-1) var(--space-2);
  }

  .battle-card,
  .profile-panel {
    padding: var(--space-2);
  }

  /* Compact headers */
  .skill-header,
  .combat-header {
    margin-bottom: var(--space-2);
  }

  /* Side-by-side layout for combat */
  .battle-container {
    grid-template-columns: 1fr 1fr;
    gap: var(--space-2);
  }

  /* Smaller HP bars */
  .hp-bar-single {
    height: 24px;
  }

  /* Compact stats */
  .stats-compact {
    grid-template-columns: repeat(4, 1fr);
    gap: var(--space-1);
  }
}

/* iOS-specific fixes */
@supports (-webkit-touch-callout: none) {
  /* Fix iOS Safari bottom bar overlap */
  .chat-container,
  .fab,
  .toast-container {
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Fix iOS input zoom */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px !important;
  }

  /* Smooth scrolling */
  .inventory-grid,
  .modal-body,
  .chat-messages,
  .nav-links {
    -webkit-overflow-scrolling: touch;
  }
}

/* Android-specific fixes */
@media (max-width: 768px) {
  /* Fix Android Chrome address bar */
  .main-container {
    min-height: calc(100vh - var(--navbar-height));
  }

  /* Better touch feedback */
  button:active,
  .activity-card:active,
  .skill-card:active {
    transform: scale(0.98);
  }
}

/* High DPI displays (Retina) */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  /* Sharper images */
  img {
    image-rendering: -webkit-optimize-contrast;
  }

  /* Crisper borders */
  .battle-card,
  .profile-panel,
  .activity-card {
    border-width: 0.5px;
  }
}

/* Accessibility improvements for mobile */
@media (max-width: 768px) {
  /* Larger focus indicators */
  *:focus-visible {
    outline-width: 3px;
    outline-offset: 3px;
  }

  /* Better contrast for small text */
  .text-secondary,
  .text-muted {
    color: var(--text-secondary);
    font-weight: var(--font-weight-medium);
  }

  /* Ensure readable font sizes */
  small,
  .text-xs {
    font-size: max(12px, var(--font-size-xs));
  }
}

/* Performance optimizations */
@media (max-width: 768px) {
  /* Reduce expensive effects */
  .battle-card,
  .profile-panel,
  .activity-card {
    will-change: transform;
  }

  /* Simplify shadows */
  .battle-card,
  .profile-panel {
    box-shadow: var(--elevation-1);
  }

  /* Disable hover effects on touch devices */
  @media (hover: none) {
    .activity-card:hover,
    .skill-card:hover,
    .nav-link:hover {
      transform: none;
      box-shadow: none;
    }
  }
}
