/* ========================================
   BLOOD CAFÉ – MOBILE & CROSS-PLATFORM
   Responsive Design & Touch Optimization
   Phase 5: Mobile Polish
   ======================================== */

/* ========================================
   TOUCH TARGET OPTIMIZATION
   ======================================== */

/* Ensure all interactive elements meet 44x44px minimum */
@media (max-width: 768px) {
  button, .btn, a.btn, .clickable,
  input[type="button"],
  input[type="submit"],
  select {
    min-height: 48px;
    min-width: 48px;
    padding: 12px 20px;
    font-size: 16px; /* Prevents zoom on iOS */
  }

  /* Navigation buttons */
  .nav-btn {
    min-width: 64px;
    min-height: 64px;
    padding: 12px;
  }

  /* Bottom navigation touch optimization */
  .bottom-nav {
    padding: 8px 0;
  }

  .bottom-nav a {
    min-width: 60px;
    min-height: 60px;
  }

  /* Form inputs */
  input, textarea, select {
    min-height: 48px;
    font-size: 16px; /* Prevents zoom */
    padding: 12px 16px;
  }

  /* Sphere selectors */
  .sphere-option {
    min-height: 60px;
    padding: 16px;
  }

  /* Card interactions */
  .card, .panel {
    padding: 20px;
  }

  /* Links and text buttons - exclude mobile nav items */
  a:not(.mobile-nav-item) {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 8px;
  }

  /* Icon buttons */
  button[class*="icon"], .icon-btn {
    min-width: 48px;
    min-height: 48px;
  }

  /* Close buttons in modals */
  .modal-close, [class*="close"] {
    min-width: 48px;
    min-height: 48px;
    padding: 12px;
  }

  /* Tab buttons */
  .tab, [role="tab"] {
    min-height: 48px;
    padding: 12px 20px;
  }

  /* Dropdown items */
  .dropdown-item, select option {
    min-height: 48px;
    padding: 12px;
  }
}

/* ========================================
   RESPONSIVE LAYOUT IMPROVEMENTS
   ======================================== */

/* Mobile-first breakpoints */
@media (max-width: 480px) {
  /* Small phones */
  .wrap {
    padding: 16px;
  }

  h1 {
    font-size: 28px;
  }

  h2 {
    font-size: 22px;
  }

  h3 {
    font-size: 18px;
  }

  /* Single column layouts */
  .grid-2, .grid-3, .grid-4,
  [class*="grid"] {
    grid-template-columns: 1fr !important;
  }

  /* Stack navigation */
  .nav {
    flex-direction: column;
    gap: 8px;
    width: 100%;
  }

  .nav .btn {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 768px) {
  /* Tablets and large phones */
  .calculator-grid,
  .comparison-grid {
    grid-template-columns: 1fr !important;
  }

  /* Two-column grids become single column */
  .squad-grid {
    grid-template-columns: 1fr !important;
  }

  /* Reduce spacing on mobile */
  .wrap {
    padding: 20px;
  }

  /* Header adjustments */
  header .wrap, .header .wrap {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }

  .brand {
    font-size: 18px;
  }

  /* Modal full-screen on mobile */
  .modal-content {
    width: 95%;
    max-width: 95%;
    max-height: 90vh;
    margin: 5vh auto;
  }

  /* Table scrolling */
  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }

  /* Reduce animations on mobile for performance */
  * {
    animation-duration: 0.2s !important;
  }

  /* Simplify shadows on mobile */
  .card, .panel, button {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2) !important;
  }

  .card:hover, .panel:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3) !important;
  }
}

@media (min-width: 769px) and (max-width: 1024px) {
  /* Tablets */
  .grid-4 {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  .grid-3 {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ========================================
   LANDSCAPE ORIENTATION
   ======================================== */

@media (max-height: 500px) and (orientation: landscape) {
  /* Landscape phone mode */
  .header, header {
    position: relative; /* Remove sticky */
  }

  .bottom-nav {
    display: none; /* Hide bottom nav in landscape */
  }

  .modal-content {
    max-height: 95vh;
    overflow-y: auto;
  }
}

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

@media (max-width: 768px) {
  /* Hide desktop navigation */
  .header .nav {
    display: none;
  }

  /* Ensure bottom nav is visible */
  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
  }

  /* Add padding to body to account for bottom nav */
  body {
    padding-bottom: 80px;
  }

  /* Scroll to top button repositioning */
  #scrollToTop {
    bottom: 100px !important; /* Above bottom nav */
    right: 20px !important;
  }
}

/* ========================================
   TOUCH-FRIENDLY FORMS
   ======================================== */

@media (max-width: 768px) {
  /* Larger form elements */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  textarea,
  select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    border-radius: 8px;
  }

  /* Better select styling */
  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='%23fff' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 40px;
  }

  /* Better checkbox/radio sizing */
  input[type="checkbox"],
  input[type="radio"] {
    width: 24px;
    height: 24px;
    min-width: 24px;
    min-height: 24px;
  }

  /* Label spacing */
  label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    min-height: 24px;
    display: flex;
    align-items: center;
  }

  /* Form groups */
  .form-group {
    margin-bottom: 20px;
  }
}

/* ========================================
   MOBILE-OPTIMIZED CARDS
   ======================================== */

@media (max-width: 768px) {
  .card, .panel {
    margin-bottom: 16px;
  }

  /* Sphere cards grid */
  .sphere-selector {
    grid-template-columns: 1fr !important;
    max-height: 400px;
  }

  /* Config cards */
  .config-card {
    padding: 16px;
  }

  /* Stat cards */
  .stat-card {
    padding: 20px 16px;
  }

  .stat-value {
    font-size: 28px;
  }
}

/* ========================================
   MOBILE TEXT OPTIMIZATION
   ======================================== */

@media (max-width: 768px) {
  /* Prevent text overflow */
  * {
    word-wrap: break-word;
    overflow-wrap: break-word;
  }

  /* Better line height on mobile */
  p, li {
    line-height: 1.7;
  }

  /* Larger body text */
  body {
    font-size: 16px;
  }

  /* Reduce letter spacing on mobile */
  h1, h2, h3, h4, h5, h6 {
    letter-spacing: 0.3px;
  }
}

/* ========================================
   MOBILE SCROLLING IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  /* Smooth scrolling */
  html {
    -webkit-overflow-scrolling: touch;
  }

  /* Better scroll containers */
  .scroll-container {
    -webkit-overflow-scrolling: touch;
    overflow-y: auto;
  }

  /* Hide scrollbar on mobile for cleaner look */
  .sphere-selector::-webkit-scrollbar {
    width: 4px;
  }

  .sphere-selector::-webkit-scrollbar-thumb {
    background: rgba(168, 85, 247, 0.5);
  }
}

/* ========================================
   MOBILE MODAL IMPROVEMENTS
   ======================================== */

@media (max-width: 768px) {
  .modal {
    align-items: flex-start;
    padding: 0;
  }

  .modal-content {
    border-radius: 12px 12px 0 0;
    margin-top: 10vh;
    max-height: 90vh;
    overflow-y: auto;
  }

  /* Modal header sticky */
  .modal-header {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--panel, #1a1f2e);
    padding: 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  }

  /* Modal close button */
  .modal-close {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 11;
  }
}

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

@media (max-width: 768px) {
  /* Stack table rows on mobile */
  .table-responsive {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Alternative: Card-style tables */
  table.mobile-cards {
    border: 0;
  }

  table.mobile-cards thead {
    display: none;
  }

  table.mobile-cards tr {
    display: block;
    margin-bottom: 16px;
    background: var(--card, #232d3f);
    border-radius: 8px;
    padding: 12px;
  }

  table.mobile-cards td {
    display: block;
    text-align: right;
    padding: 8px;
    border: 0;
  }

  table.mobile-cards td::before {
    content: attr(data-label);
    float: left;
    font-weight: 600;
    color: var(--brand-bright, #a855f7);
  }
}

/* ========================================
   MOBILE NOTIFICATIONS
   ======================================== */

@media (max-width: 768px) {
  .notification, .toast {
    left: 10px;
    right: 10px;
    top: 10px;
    max-width: calc(100% - 20px);
  }
}

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

@media (max-width: 768px) {
  /* Disable expensive effects on mobile */
  body::before {
    animation: none;
    opacity: 0.5;
  }

  /* Simplified backgrounds - NO fixed attachment */
  body {
    background: var(--bg, #0a0e14);
    background-attachment: scroll !important;
    animation: none;
  }

  /* Reduce blur effects - CRITICAL for scroll performance */
  [class*="glass"], [class*="blur"],
  .header, .mobile-nav, .panel, .card,
  .build-card, .char, #synergyPanel,
  .sphere-suggestions, .toast {
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }

  /* GPU acceleration for fixed elements */
  .header, .mobile-nav, .toast, #synergyPanel {
    will-change: transform;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    contain: layout style;
  }

  /* Simplified shadows - reduces paint complexity */
  .panel, .card, .build-card, .slot-card {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4) !important;
  }

  /* Simplified gradients */
  [class*="gradient"] {
    background-size: 100% 100% !important;
  }

  /* Disable text-shadow glow effects */
  h1, h2, h3, .section-title, .slot-title {
    text-shadow: none !important;
  }
}

/* Tablet performance */
@media (max-width: 1024px) {
  body {
    background-attachment: scroll !important;
  }

  .header, .mobile-nav {
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }
}

/* ========================================
   SAFE AREA INSETS (iOS Notch)
   ======================================== */

@supports (padding: max(0px)) {
  .header, header {
    padding-left: max(24px, env(safe-area-inset-left));
    padding-right: max(24px, env(safe-area-inset-right));
    padding-top: max(12px, env(safe-area-inset-top));
  }

  .bottom-nav {
    padding-left: max(0px, env(safe-area-inset-left));
    padding-right: max(0px, env(safe-area-inset-right));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  body {
    padding-bottom: max(80px, calc(80px + env(safe-area-inset-bottom)));
  }
}

/* ========================================
   DARK MODE SUPPORT
   ======================================== */

@media (prefers-color-scheme: dark) {
  /* Already using dark theme, but ensure consistency */
  :root {
    color-scheme: dark;
  }
}

/* ========================================
   HIGH CONTRAST MODE
   ======================================== */

@media (prefers-contrast: high) {
  button, .btn {
    border: 2px solid currentColor;
  }

  .card, .panel {
    border: 1px solid currentColor;
  }

  a {
    text-decoration: underline;
  }
}

/* ========================================
   REDUCED DATA MODE
   ======================================== */

@media (prefers-reduced-data: reduce) {
  /* Disable background images/gradients to save data */
  body::before {
    display: none;
  }

  [class*="gradient"] {
    background-image: none !important;
  }
}

/* ========================================
   PRINT STYLES
   ======================================== */

@media print {
  /* Hide non-essential elements */
  .header, header,
  .bottom-nav,
  .nav,
  #scrollToTop,
  .modal,
  [class*="notification"] {
    display: none !important;
  }

  /* Optimize for printing */
  body {
    background: white;
    color: black;
  }

  .card, .panel {
    border: 1px solid #000;
    break-inside: avoid;
  }

  a {
    color: #000;
    text-decoration: underline;
  }

  /* Show URLs after links */
  a[href]::after {
    content: " (" attr(href) ")";
  }
}

/* ========================================
   ACCESSIBILITY ENHANCEMENTS
   ======================================== */

/* Focus visible for keyboard navigation */
*:focus-visible {
  outline: 3px solid var(--brand-bright, #a855f7);
  outline-offset: 2px;
}

/* High visibility focus for mobile */
@media (max-width: 768px) {
  *:focus-visible {
    outline-width: 4px;
    outline-offset: 3px;
  }
}

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

/* Skip to content link */
.skip-to-content:focus {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 10000;
  padding: 12px 20px;
  background: var(--brand, #7c3aed);
  color: white;
  text-decoration: none;
  border-radius: 8px;
}

/* ========================================
   MOBILE SPHERE AUTOCOMPLETE
   Enhanced dropdown for touch devices
   ======================================== */

@media (max-width: 768px) {
  /* Main autocomplete container - higher z-index for mobile */
  .sphere-autocomplete-container {
    z-index: 99999 !important;
  }

  /* Suggestion dropdown - mobile optimized */
  .sphere-suggestions {
    background: rgba(13, 17, 23, 0.98) !important;
    border: 3px solid #00d9ff !important;
    border-radius: 16px !important;
    padding: 12px !important;
    box-shadow:
      0 8px 32px rgba(0, 0, 0, 0.6),
      0 0 20px rgba(0, 217, 255, 0.3),
      inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
    max-height: 50vh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    animation: slideDownMobile 0.2s ease-out;
  }

  @keyframes slideDownMobile {
    from {
      opacity: 0;
      transform: translateY(-10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  /* Individual suggestion items - larger touch targets */
  .sphere-suggestion-item {
    display: flex !important;
    align-items: center !important;
    gap: 16px !important;
    padding: 16px 14px !important;
    min-height: 64px !important;
    cursor: pointer !important;
    background: rgba(0, 0, 0, 0.3) !important;
    border: 2px solid rgba(255, 255, 255, 0.1) !important;
    border-radius: 12px !important;
    margin-bottom: 10px !important;
    transition: all 0.15s ease !important;
  }

  .sphere-suggestion-item:last-child {
    margin-bottom: 0 !important;
  }

  .sphere-suggestion-item:active {
    background: rgba(0, 217, 255, 0.25) !important;
    border-color: #00d9ff !important;
    transform: scale(0.98);
  }

  /* Color indicator - more visible */
  .sphere-suggestion-color {
    width: 8px !important;
    height: 48px !important;
    border-radius: 4px !important;
    flex-shrink: 0 !important;
    box-shadow: 0 0 8px currentColor;
  }

  /* Text container */
  .sphere-suggestion-text {
    flex: 1 !important;
    min-width: 0 !important;
  }

  /* Sphere name - larger font */
  .sphere-suggestion-name {
    font-size: 18px !important;
    font-weight: 700 !important;
    color: #ffffff !important;
    margin-bottom: 6px !important;
    letter-spacing: 0.3px !important;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  }

  /* Category label - more visible */
  .sphere-suggestion-category {
    font-size: 13px !important;
    font-weight: 600 !important;
    color: rgba(255, 255, 255, 0.7) !important;
    text-transform: uppercase !important;
    letter-spacing: 1px !important;
    background: rgba(255, 255, 255, 0.1);
    padding: 4px 8px;
    border-radius: 6px;
    display: inline-block;
  }

  /* Header label for dropdown */
  .sphere-suggestions::before {
    content: "Select a Sphere";
    display: block;
    font-size: 12px;
    font-weight: 700;
    color: #00d9ff;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    padding: 8px 12px 16px 12px;
    border-bottom: 1px solid rgba(0, 217, 255, 0.3);
    margin-bottom: 12px;
    text-align: center;
  }

  /* Scrollbar for mobile */
  .sphere-suggestions::-webkit-scrollbar {
    width: 6px !important;
  }

  .sphere-suggestions::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.4) !important;
    border-radius: 3px !important;
  }

  .sphere-suggestions::-webkit-scrollbar-thumb {
    background: linear-gradient(135deg, #00d9ff 0%, #0080ff 100%) !important;
    border-radius: 3px !important;
  }
}

/* Extra small phones - even more optimization */
@media (max-width: 400px) {
  .sphere-suggestions {
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    max-width: calc(100vw - 16px) !important;
  }

  .sphere-suggestion-item {
    padding: 14px 12px !important;
    gap: 12px !important;
  }

  .sphere-suggestion-name {
    font-size: 16px !important;
  }

  .sphere-suggestion-category {
    font-size: 11px !important;
  }
}
