/* =====================================================================
   MOBILE LAYER — loads last. Desktop (>=1024px) is untouched.
   ===================================================================== */

/* 1. ROOT CAUSE FIX: desktop sidebar must never show below 1024px.
      (Tailwind's .flex was overriding display:none, squeezing the page.) */
@media (max-width: 1023.98px) {
  .sidebar-desktop { display: none !important; }
}
@media (min-width: 1024px) {
  .sidebar-desktop { display: flex !important; position: sticky; top: 49px; align-self: flex-start; height: calc(100vh - 49px); overflow-y: auto; }
}

/* 2. Page-level scroll safety without breaking sticky header */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { overflow-x: hidden; }
@supports (overflow: clip) {
  html, body { overflow-x: clip; }
}
body { min-height: 100vh; min-height: 100dvh; }
#main-view-container { min-width: 0; }

/* 3. Phones & small tablets */
@media (max-width: 767.98px) {

  /* Header: compact, single row, never wraps */
  header.sticky {
    padding-top: max(0.4rem, env(safe-area-inset-top));
    padding-left: max(0.6rem, env(safe-area-inset-left));
    padding-right: max(0.6rem, env(safe-area-inset-right));
    gap: 0.4rem;
  }
  header.sticky .text-xs.font-bold { font-size: 0.8rem; }
  #hdr-freshness { font-size: 9px; padding: 2px 6px; letter-spacing: 0; }
  #btn-toggle-theme, #btn-toggle-currency, header.sticky button[aria-label="Refresh prices"] {
    min-height: 34px; min-width: 34px;
  }

  /* Main content area: breathing room + room for bottom nav */
  #main-view-container {
    padding: 0.75rem 0.75rem calc(5.25rem + env(safe-area-inset-bottom)) !important;
  }

  /* Bottom nav: larger, thumb-friendly, always above content */
  .mobile-bottom-nav {
    box-shadow: 0 -4px 18px rgba(2, 6, 23, 0.12);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  .mobile-nav-btn { min-height: 56px; font-size: 10.5px; -webkit-tap-highlight-color: transparent; }
  .mobile-nav-btn svg { width: 20px; height: 20px; }
  .mobile-nav-btn.active { position: relative; }
  .mobile-nav-btn.active::before {
    content: ""; position: absolute; top: 0; left: 28%; right: 28%; height: 3px;
    border-radius: 0 0 4px 4px; background: #3b82f6;
  }

  /* More drawer: scrollable bottom sheet */
  #mobile-more-drawer > div {
    max-height: 85vh; max-height: 85dvh; overflow-y: auto;
    padding-bottom: calc(1.5rem + env(safe-area-inset-bottom));
  }
  #mobile-more-drawer .nav-item { padding: 0.8rem 0.9rem; font-size: 0.92rem; }

  /* Modals become full-width bottom sheets */
  #app-modal { padding: 0 !important; align-items: flex-end !important; }
  #app-modal-body {
    max-width: 100% !important; width: 100%;
    border-radius: 1.25rem 1.25rem 0 0 !important;
    max-height: 92vh !important; max-height: 92dvh !important;
    padding: 1rem 1rem calc(1.25rem + env(safe-area-inset-bottom)) !important;
    -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  }

  /* Toasts sit above the bottom nav, full width */
  #app-toast {
    left: 0.75rem !important; right: 0.75rem !important;
    bottom: calc(4.75rem + env(safe-area-inset-bottom)) !important;
    text-align: center;
  }

  /* Lock screen */
  #app-lock-screen { padding: 1rem; }

  /* Cards, banners and buttons that overflowed */
  .kpi-card, .glass-panel { padding-left: 0.85rem; padding-right: 0.85rem; }
  .kpi-card:hover { transform: none; }
  main button, main a { -webkit-tap-highlight-color: transparent; }
  main button { min-height: 36px; }
  main .flex.gap-2.shrink-0 { flex-wrap: wrap; width: 100%; }
  main .flex.gap-2.shrink-0 > button { flex: 1 1 auto; }
  main h1, main h2, main h3 { overflow-wrap: anywhere; }
  main .mono { overflow-wrap: anywhere; }

  /* Form controls must be allowed to shrink inside flex rows (browser default min-width is intrinsic) */
  main input, main select, main textarea { min-width: 0; max-width: 100%; }

  /* Inputs with a fixed min-width must be allowed to shrink inside flex rows */
  main input.term-input[class*="min-w-"] { min-width: 0 !important; flex: 1 1 100%; }

  /* Any table that survives on mobile scrolls inside its own box */
  main table { min-width: 560px; }
  main .overflow-x-auto { -webkit-overflow-scrolling: touch; }
  main .overflow-x-auto::-webkit-scrollbar { height: 4px; }

  /* Charts: predictable height, never wider than screen */
  main canvas { max-width: 100% !important; }
  main div[style*="height"] canvas { height: 100% !important; }

  /* Inputs: no iOS/Android zoom, comfortable targets */
  main input, main select, main textarea,
  #app-modal-body input, #app-modal-body select, #app-modal-body textarea {
    font-size: 16px !important; min-height: 42px;
  }
  input[type="checkbox"], input[type="radio"] { min-height: 0 !important; width: 1.1rem; height: 1.1rem; }
}

/* 4. Very small phones (<=360px) */
@media (max-width: 380px) {
  #hdr-freshness { display: none; }
  .mobile-nav-btn { font-size: 9.5px; }
}

/* 5. Tablet nav: horizontal scroll without ugly scrollbar */
@media (min-width: 768px) and (max-width: 1023.98px) {
  nav.overflow-x-auto { scrollbar-width: none; }
  nav.overflow-x-auto::-webkit-scrollbar { display: none; }
}

/* 6. Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* Toast never blocks taps on buttons underneath it */
#app-toast { pointer-events: none; z-index: 70 !important; }
