/* ═════════════════════════════════════════════════════════════
   PRIME SELF — MOBILE-OPTIMIZED COMPONENTS
   Mobile-first responsive design for touch-friendly UX
   ═════════════════════════════════════════════════════════════ */

/* ── Touch Target Standards ────────────────────────────────── */
/* All interactive elements minimum 44px (iOS/Android guidelines) */
:root {
  /* --touch-target-min inherited from tokens.css */
  --mobile-padding: 16px;
  --mobile-header-height: 56px;
  --mobile-nav-height: 64px;
}

/* ── Base: Mobile Nav Hidden by Default ───────────────────── */
/* Always hidden unless mobile media query activates it, prevents
   stray visibility in PWA standalone mode or before CSS breakpoints apply. */
.mobile-nav {
  display: none;
}

/* Runtime mobile override for large-screen phones detected in JS */
html.force-mobile-layout .mobile-nav {
  display: flex;
}

html.force-mobile-layout .tabs,
html.force-mobile-layout .sub-tabs {
  display: none;
}

html.force-mobile-layout .container {
  padding-bottom: calc(var(--mobile-nav-height) + var(--space-4));
}

html.force-mobile-layout .auth-status {
  display: none;
}

html.force-mobile-layout .auth-bar {
  gap: 8px;
  flex-shrink: 1;
  min-width: 0;
}

html.force-mobile-layout #exportDataBtn,
html.force-mobile-layout #deleteAccountBtn {
  display: none !important;
}

/* ── Responsive Breakpoints ────────────────────────────────── */
@media (max-width: 768px) {
  
  /* ── Header: Mobile Compact ────────────────────────────── */
  header {
    height: var(--mobile-header-height);
    padding: 0 var(--mobile-padding);
    z-index: var(--z-sticky); /* Above content, below modals */
  }
  
  .logo {
    font-size: 1.1rem;
  }
  
  .logo span {
    display: none; /* Hide tagline on mobile */
  }
  
  /* Logo lockup mobile adjustments */
  .logo-lockup {
    gap: 8px;
  }
  
  .logo-lockup-icon {
    width: 28px;
    height: 28px;
  }
  
  .logo-lockup-primary {
    font-size: 1rem;
  }
  
  .logo-lockup-secondary {
    display: none; /* Hide subtitle on mobile */
  }
  
  /* ── Buttons: Touch-Friendly ───────────────────────────── */
  button, .btn {
    min-height: var(--touch-target-min);
    min-width: var(--touch-target-min);
    padding: 12px 20px;
    font-size: var(--text-sm);
    -webkit-tap-highlight-color: rgba(201, 168, 76, 0.3);
  }
  
  /* Increase tap area for icon buttons */
  button[aria-label] {
    padding: 12px;
  }
  
  /* ── Forms: Mobile Input Optimization ──────────────────── */
  input, textarea, select {
    min-height: var(--touch-target-min);
    font-size: var(--text-base); /* Prevents iOS zoom on focus (>=16px) */
    padding: 12px 16px;
  }
  
  input[type="date"],
  input[type="time"] {
    min-height: var(--touch-target-min);
  }
  
  /* ── Bottom Navigation Bar ─────────────────────────────── */
  .mobile-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--mobile-nav-height);
    background: linear-gradient(180deg, rgba(13,13,26,0.95) 0%, rgba(10,10,15,0.98) 100%);
    backdrop-filter: blur(20px);
    border-top: 1px solid var(--border-emphasis, #7a6030);
    justify-content: space-around;
    align-items: center;
    padding: 8px 0;
    z-index: var(--z-mobile-nav);
    box-shadow: 0 -4px 24px rgba(0,0,0,0.5);
  }
  
  .mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 8px 12px;
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    border-radius: 8px;
    color: var(--text-secondary, #8882a0);
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: rgba(201, 168, 76, 0.2);
  }
  
  .mobile-nav-item:active {
    transform: scale(0.95);
    background: rgba(201, 168, 76, 0.1);
  }
  
  .mobile-nav-item.active {
    color: var(--interactive-primary, #c9a84c);
    background: rgba(201, 168, 76, 0.15);
  }
  
  .mobile-nav-icon {
    font-size: 1.5rem;
    line-height: 1;
  }
  
  .mobile-nav-label {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    text-align: center;
  }
  
  /* ── Main Content: Bottom Padding for Nav ──────────────── */
  .container {
    padding-bottom: calc(var(--mobile-nav-height) + var(--space-4));
  }
  
  /* ── Tabs: Hidden on mobile (sidebar handles this) ────── */
  .tabs {
    display: none;
  }
  
  /* Hide sub-tabs on mobile — bottom nav handles navigation */
  .sub-tabs {
    display: none;
  }
  
  .tab-content {
    padding: var(--mobile-padding);
  }
  
  /* Step guide: stack vertically on mobile */
  .step-guide {
    flex-direction: column;
    gap: 6px;
    padding: 12px;
  }
  
  .step-item {
    min-width: unset;
    width: 100%;
    padding: 10px 12px;
  }
  
  .step-arrow {
    display: none; /* Hide arrows when stacked */
  }
  
  /* ── Cards: Mobile Stacking ────────────────────────────── */
  .card, .profile-card, .transit-card {
    margin-bottom: 16px;
  }
  
  /* ── Modals: Full-Screen on Mobile ─────────────────────── */
  .modal, .auth-modal {
    width: 100%;
    max-width: 100%;
    max-height: 100vh;
    border-radius: 0;
    margin: 0;
  }

  /* ── Pricing Overlays: Bottom Sheet on Mobile ──────────── */
  .pricing-overlay {
    align-items: flex-end;
  }
  .pricing-modal {
    width: 100%;
    max-width: 100%;
    max-height: 85vh;
    border-radius: 20px 20px 0 0;
    margin: 0;
    padding: 24px 20px 32px;
  }
  
  .modal-header {
    position: sticky;
    top: 0;
    background: var(--bg-secondary, #111118);
    z-index: var(--z-sticky);
    border-bottom: 1px solid var(--border-subtle, #2a2a3a);
    padding: 16px;
  }
  
  .modal-close {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
  }
  
  /* ── Swipeable Sections ────────────────────────────────── */
  .swipeable-container {
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none; /* Firefox */
  }
  
  .swipeable-container::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
  }
  
  .swipeable-item {
    scroll-snap-align: start;
    scroll-snap-stop: always;
    flex-shrink: 0;
    width: 100%;
  }
  
  /* ── Collapsible Sections (Accordion) ──────────────────── */
  .collapsible-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    cursor: pointer;
    user-select: none;
    min-height: var(--touch-target-min);
    -webkit-tap-highlight-color: rgba(201, 168, 76, 0.2);
  }
  
  .collapsible-header:active {
    background: rgba(255, 255, 255, 0.05);
  }
  
  .collapsible-icon {
    transition: transform 0.3s;
    font-size: 1.2rem;
  }
  
  .collapsible-header[aria-expanded="true"] .collapsible-icon {
    transform: rotate(180deg);
  }
  
  .collapsible-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease-out;
  }

  .collapsible-content.expanded {
    max-height: 100vh; /* Dynamic: full viewport height for any content */
  }
  
  /* ── Pull-to-Refresh ───────────────────────────────────── */
  .pull-to-refresh {
    position: absolute;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s;
  }
  
  .pull-to-refresh.pulling {
    top: 10px;
  }
  
  .pull-to-refresh.refreshing {
    animation: spin-centered 1s linear infinite;
  }
  
  @keyframes spin-centered {
    from { transform: translateX(-50%) rotate(0deg); }
    to { transform: translateX(-50%) rotate(360deg); }
  }
  
  /* ── Touch-Friendly Chart Viewer ───────────────────────── */
  .chart-container {
    touch-action: pan-x pan-y pinch-zoom;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .bodygraph-canvas {
    max-width: 100%;
    height: auto;
    min-height: 400px;
  }
  
  /* ── Improved Form Layouts ─────────────────────────────── */
  .form-row {
    flex-direction: column;
    gap: 12px;
  }
  
  .form-group {
    margin-bottom: 20px;
  }
  
  label {
    font-size: var(--text-sm);
    margin-bottom: 8px;
  }
  
  /* ── Pricing Grid: Mobile Stacking ─────────────────────── */
  .pricing-grid {
    grid-template-columns: 1fr;
    gap: 16px;
    padding: var(--mobile-padding);
  }
  
  .pricing-card {
    width: 100%;
  }
  
  /* ── Auth Bar: Compact ─────────────────────────────────── */
  .auth-bar {
    gap: 8px;
  }
  
  .auth-status {
    display: none; /* Hide text, show only tier badge on mobile */
  }
  
  .tier-badge {
    font-size: 0.65rem;
    padding: 4px 8px;
  }
  
  /* ── Alerts: Full-Width ────────────────────────────────── */
  .alert {
    border-radius: 8px;
    margin: var(--mobile-padding);
  }
  
  /* ── Loading Indicators: Larger ────────────────────────── */
  .loading {
    font-size: 1.2rem;
    padding: 32px;
  }
  
  /* ── Empty States: Centered ────────────────────────────── */
  .empty-state {
    padding: 48px var(--mobile-padding);
    text-align: center;
  }
  
  /* ── Horizontal Scroll Prevention ──────────────────────── */
  html, body {
    overflow-x: hidden;
    max-width: 100vw;
  }
  
  img, video, canvas, svg, table, pre, .card, .transit-row {
    max-width: 100%;
  }

  /* ── Text Overflow Prevention ───────────────────────────── */
  .card, .tab-content, .alert {
    overflow-wrap: break-word;
    word-break: break-word;
  }

  /* ── Header: Remove all overflow clipping so the lang-switcher dropdown
     can extend below the header. CSS spec coerces overflow-x:clip +
     overflow-y:visible → both clip, so we must set overflow: visible.
     Horizontal overflow is no longer an issue thanks to auth-bar compression
     and hiding of extra buttons below. */
  header {
    overflow: visible;
  }

  /* ── Auth Bar: Shrinkable on small screens ───────────────── */
  .auth-bar {
    flex-shrink: 1;
    min-width: 0;
  }

  /* ── Remove account-management buttons from header on mobile ─ */
  /* These are rare actions; accessible via the sidebar/settings menu instead */
  #exportDataBtn,
  #deleteAccountBtn {
    display: none !important;
  }

  /* ── Lang-switcher: flag-only on mobile to save header space ─ */
  .lang-switcher .lang-name {
    display: none;
  }
  .lang-switcher-btn {
    padding: 4px 6px;
    gap: 0;
  }
  h4 { font-size: var(--text-base); }
  
  p, li {
    line-height: 1.6;
  }
  
  /* ── Spacing: Mobile-Friendly ──────────────────────────── */
  section {
    padding: 20px var(--mobile-padding);
  }
  
  .container {
    padding: 0 var(--mobile-padding);
  }
  
  /* ── Hide Desktop-Only Elements ────────────────────────── */
  .desktop-only {
    display: none !important;
  }
  
  /* ── Show Mobile-Only Elements ─────────────────────────── */
  .mobile-only {
    display: block !important;
  }

  /* ── Sticky Chart CTA (Sprint 19.3) ────────────────────── */
  /* Fixed CTA when chart form scrolls out of viewport */
  .sticky-chart-cta {
    position: fixed;
    bottom: calc(var(--mobile-nav-height) + 20px); /* Above mobile nav */
    left: var(--mobile-padding);
    right: var(--mobile-padding);
    z-index: var(--z-dropdown);
    display: none; /* Hidden by default, shown via JS */
    padding: var(--space-3);
    background: linear-gradient(135deg, rgba(13,13,26,0.97) 0%, rgba(18,18,30,0.98) 100%);
    backdrop-filter: blur(12px);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-emphasis, #7a6030);
    box-shadow: 0 -4px 24px rgba(0,0,0,0.4), 0 0 20px rgba(201, 168, 76, 0.15);
  }

  .sticky-chart-cta.visible {
    display: block;
    animation: slideUp 0.3s ease-out;
  }

  @keyframes slideUp {
    from {
      opacity: 0;
      transform: translateY(20px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .sticky-chart-cta .btn-primary {
    width: 100%;
    font-size: var(--text-base);
    padding: 14px 20px;
  }
}

/* ── Tablet Optimizations (768px - 1024px) ─────────────────── */
@media (min-width: 769px) and (max-width: 1024px) {
  .pricing-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .mobile-nav {
    display: none; /* Hide bottom nav on tablet */
  }
  
  /* Step guide: smaller on tablet */
  .step-item {
    min-width: 140px;
    padding: 8px 10px;
  }
  
  .step-label strong {
    font-size: 0.82rem;
  }
}

/* ── Desktop Optimizations (Hide Mobile Nav) ───────────────── */
@media (min-width: 769px) {
  .mobile-nav {
    display: none;
  }
  
  .mobile-only {
    display: none !important;
  }
  
  .desktop-only {
    display: block !important;
  }
}

/* ── Small Screens: Transit Row Stacking (≤600px) ──────────── */
@media (max-width: 600px) {
  .transit-row {
    grid-template-columns: 1fr;
    gap: var(--space-2);
  }
  
  .planet-name,
  .planet-pos,
  .gate-badge {
    text-align: left;
  }
}

/* ── Extra Small Devices (320px - 480px) ───────────────────── */
@media (max-width: 480px) {
  html {
    font-size: 14px;
  }
  
  .logo {
    font-size: 1rem;
  }
  
  button, .btn {
    font-size: 0.85rem;
    padding: 10px 16px;
  }
  
  .pricing-card h3 {
    font-size: 1.2rem;
  }
  
  .pricing-card .price {
    font-size: 1.8rem;
  }
}

/* ── Landscape Mobile (Height < 500px) ─────────────────────── */
@media (max-height: 500px) and (orientation: landscape) {
  .mobile-nav {
    height: 48px;
    padding: 4px 0;
  }
  
  .mobile-nav-item {
    padding: 4px 8px;
    gap: 2px;
  }
  
  .mobile-nav-icon {
    font-size: 1.2rem;
  }
  
  .mobile-nav-label {
    font-size: 0.6rem;
  }
  
  header {
    height: 48px;
  }
}

/* ── Accessibility: High Contrast Mode ─────────────────────── */
@media (prefers-contrast: high) {
  button, .btn {
    border: 2px solid currentColor;
  }
  
  .mobile-nav {
    border-top: 2px solid var(--interactive-primary, #c9a84c);
  }
}

/* ── Accessibility: Reduced Motion ─────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  
  .collapsible-content {
    transition: none;
  }
}

/* ── Dark Mode Enhancements (Mobile) ───────────────────────── */
@media (prefers-color-scheme: dark) and (max-width: 768px) {
  .mobile-nav {
    background: linear-gradient(180deg, rgba(10,10,15,0.98) 0%, rgba(5,5,8,0.99) 100%);
  }
}

/* ── Mobile "More" Overflow Drawer ────────────────────────── */
.mobile-more-drawer {
  position: fixed;
  inset: 0;
  z-index: var(--z-tooltip);
}

.mobile-more-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
}

.mobile-more-panel {
  position: absolute;
  bottom: 64px; /* above mobile nav */
  left: 0;
  right: 0;
  background: var(--bg-secondary, #111118);
  border-top: 1px solid var(--border-emphasis, #7a6030);
  border-radius: 16px 16px 0 0;
  padding: 8px 16px 16px;
  animation: slideUp 0.2s ease-out;
  max-height: 60vh;
  overflow-y: auto;
}

@keyframes slideUp {
  from { transform: translateY(100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.mobile-more-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 4px 12px;
  color: var(--interactive-primary, #c9a84c);
  font-weight: 600;
  font-size: 0.95rem;
  border-bottom: 1px solid var(--border-subtle, #2a2a3a);
  margin-bottom: 12px;
}

.mobile-more-header button {
  background: none;
  border: none;
  color: var(--text-secondary, #8882a0);
  font-size: 1.5rem;
  padding: 4px 8px;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mobile-more-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.mobile-more-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 4px;
  border-radius: 10px;
  color: var(--text-primary, #e8e6f0);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 500;
  text-align: center;
  min-height: 44px;
  transition: background 0.15s;
  -webkit-tap-highlight-color: rgba(201, 168, 76, 0.2);
}

.mobile-more-item:active {
  background: rgba(201, 168, 76, 0.15);
}

.mobile-more-icon {
  font-size: 1.4rem;
  line-height: 1;
}

/* Hide drawer on desktop */
@media (min-width: 769px) {
  .mobile-more-drawer {
    display: none !important;
  }
}

/* ── DEF-10: Back-to-top floating button ──────────────────────── */
.back-to-top {
  position: fixed;
  bottom: calc(var(--mobile-nav-height, 64px) + 16px);
  right: 16px;
  z-index: 200;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--color-primary, #c9a84c);
  color: #000;
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 44px;
  cursor: pointer;
  box-shadow: 0 2px 12px rgba(0,0,0,0.4);
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
}

.back-to-top.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

@media (min-width: 769px) {
  .back-to-top {
    bottom: 24px;
    right: 24px;
  }
}

.back-to-top:hover {
  background: var(--color-primary-light, #d4b46a);
}

.back-to-top:focus-visible {
  outline: 2px solid var(--color-primary, #c9a84c);
  outline-offset: 3px;
}

/* ═════════════════════════════════════════════════════════════
   END MOBILE OPTIMIZATIONS
   ═════════════════════════════════════════════════════════════ */
