/* ============================================================
   FlashFreight Mobile-First CSS
   Responsive layer on top of Tailwind — no desktop styles overridden
   ============================================================ */

/* ── CSS Custom Properties ─────────────────────────────────── */
:root {
  --ff-primary:      #137fec;
  --ff-bg-dark:      #101922;
  --ff-surface-dark: #1e2936;
  --ff-border:       rgba(51, 65, 85, 0.5);

  --bottom-nav-height: 60px;
  --mobile-header-height: 56px;

  /* Safe-area insets for notched phones */
  --safe-top:    env(safe-area-inset-top,    0px);
  --safe-right:  env(safe-area-inset-right,  0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left,   0px);
}

/* ── Mobile Typography Scaling ──────────────────────────────── */
@media (max-width: 640px) {
  html { font-size: 15px; }

  h1 { font-size: 1.25rem !important; }
  h2 { font-size: 1.1rem  !important; }
  h3 { font-size: 1rem    !important; }
}

/* ── Touch-Friendly Minimum Target Sizes ────────────────────── */
@media (max-width: 1023px) {
  button,
  a[role="button"],
  [data-touch-target] {
    min-height: 44px;
    min-width:  44px;
  }

  /* Smaller inline links excluded */
  nav a,
  .sidebar-nav-link {
    min-height: 44px;
  }
}

/* ── Mobile Top Header Bar ──────────────────────────────────── */
#ff-mobile-header {
  display: none;
}

@media (max-width: 1023px) {
  #ff-mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 500;
    height: var(--mobile-header-height);
    padding: 0 1rem;
    padding-top: var(--safe-top);
    background: var(--ff-surface-dark);
    border-bottom: 1px solid var(--ff-border);
    flex-shrink: 0;
  }

  #ff-mobile-header .ff-logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  #ff-mobile-header .ff-logo img {
    height: 28px;
    width: auto;
  }

  #ff-mobile-header .ff-logo span {
    color: #fff;
    font-weight: 700;
    font-size: 0.9rem;
  }

  #ff-hamburger-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    background: rgba(255,255,255,0.05);
    border: 1px solid var(--ff-border);
    border-radius: 0.5rem;
    cursor: pointer;
    color: #fff;
    transition: background 0.15s;
  }

  #ff-hamburger-btn:hover {
    background: rgba(255,255,255,0.1);
  }
}

/* ── Sidebar Off-Canvas Overlay (Mobile) ────────────────────── */
@media (max-width: 1023px) {
  /* Sidebar is off-canvas on mobile */
  #sidebar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    height: 100% !important;
    width: 280px !important;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 600;
    padding-top: var(--safe-top);
  }

  #sidebar.mobile-open {
    transform: translateX(0);
  }

  /* Desktop layout: no transform override needed */
  #sidebar-container {
    position: static;
  }
}

/* Backdrop overlay */
#ff-sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 590;
  opacity: 0;
  transition: opacity 0.3s ease;
}

#ff-sidebar-backdrop.visible {
  opacity: 1;
}

@media (min-width: 1024px) {
  #ff-sidebar-backdrop {
    display: none !important;
  }

  /* Restore sidebar to normal desktop flow */
  #sidebar {
    position: relative !important;
    transform: none !important;
    height: 100% !important;
    width: 16rem !important; /* w-64 */
  }

  #sidebar.w-20 {
    width: 5rem !important;
  }
}

/* ── Bottom Navigation Bar ──────────────────────────────────── */
#ff-bottom-nav {
  display: none;
}

@media (max-width: 1023px) {
  #ff-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(var(--bottom-nav-height) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: var(--ff-surface-dark);
    border-top: 1px solid var(--ff-border);
    z-index: 500;
    align-items: stretch;
  }

  #ff-bottom-nav a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: #64748b;
    text-decoration: none;
    font-size: 0.6rem;
    font-weight: 600;
    transition: color 0.15s;
    padding: 0 0.25rem;
    min-height: 44px;
  }

  #ff-bottom-nav a .material-symbols-outlined {
    font-size: 22px;
    line-height: 1;
  }

  #ff-bottom-nav a:hover,
  #ff-bottom-nav a.active {
    color: var(--ff-primary);
  }

  #ff-bottom-nav a.active .material-symbols-outlined {
    font-variation-settings: 'FILL' 1, 'wght' 500, 'GRAD' 0, 'opsz' 24;
  }

  /* Spacing so content isn't hidden behind bottom nav */
  body {
    padding-bottom: calc(var(--bottom-nav-height) + var(--safe-bottom));
  }
}

/* ── Main content layout on mobile ──────────────────────────── */
@media (max-width: 1023px) {
  /* Reset the full-height flex layout that assumes sidebar is always visible */
  .flex.h-screen.overflow-hidden,
  body.h-screen.flex.overflow-hidden {
    display: block;
    height: auto;
    overflow: visible;
  }

  /* Main scrolling area */
  main.flex-1,
  .flex-1.overflow-auto,
  .flex-1.flex.flex-col.overflow-hidden {
    height: auto;
    overflow: visible;
    min-height: calc(100vh - var(--mobile-header-height));
  }

  /* Content that was h-screen should scroll naturally */
  body {
    height: auto !important;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
  }
}

/* ── Tables → Card View on Mobile ───────────────────────────── */
@media (max-width: 767px) {
  .mobile-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    /* Subtle scroll indicator */
    background:
      linear-gradient(to right, var(--ff-bg-dark) 30%, transparent),
      linear-gradient(to left, var(--ff-bg-dark) 30%, transparent) right,
      radial-gradient(farthest-side at 0%, rgba(255,255,255,0.15), transparent),
      radial-gradient(farthest-side at 100%, rgba(255,255,255,0.15), transparent) right;
    background-repeat: no-repeat;
    background-size: 40px 100%, 40px 100%, 14px 100%, 14px 100%;
    background-attachment: local, local, scroll, scroll;
  }

  /* Ensure tables inside scroll containers don't shrink */
  .mobile-table-scroll table {
    min-width: 600px;
  }
}

/* ── Modals: Fullscreen on Mobile ────────────────────────────── */
@media (max-width: 767px) {
  /* Fixed-position modal backdrop */
  .modal-backdrop {
    align-items: flex-end !important;
  }

  /* Modal panel becomes full-screen bottom sheet */
  .modal-panel {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 90vh !important;
    border-radius: 1rem 1rem 0 0 !important;
    margin: 0 !important;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* ── Grid Responsiveness Utilities ──────────────────────────── */
/* These classes can be added alongside existing grid classes */
@media (max-width: 639px) {
  .grid-mobile-2 { grid-template-columns: repeat(2, 1fr) !important; }
  .grid-mobile-1 { grid-template-columns: repeat(1, 1fr) !important; }
}

/* ── Scroll behavior improvements ───────────────────────────── */
* {
  -webkit-tap-highlight-color: transparent;
}

@media (max-width: 1023px) {
  .overflow-y-auto,
  .overflow-auto {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
  }
}

/* ── Input / Form touch targets ─────────────────────────────── */
@media (max-width: 1023px) {
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  input[type="number"],
  select,
  textarea {
    font-size: 16px; /* Prevents iOS auto-zoom on focus */
    min-height: 44px;
  }
}

/* ── Smooth scrolling for anchor navigation ─────────────────── */
html {
  scroll-behavior: smooth;
}

/* ── Reduced motion support ─────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  #sidebar {
    transition: none !important;
  }

  #ff-sidebar-backdrop {
    transition: none !important;
  }
}

/* ── Right sidebar on dashboard: hidden on mobile ───────────── */
@media (max-width: 1023px) {
  #dashboard-sidebar {
    display: none !important;
  }

  /* Remove the right-sidebar toggle button visual prominence */
  #right-sidebar-toggle {
    display: none;
  }
}

/* ── Header search: hidden on mobile (use dedicated search) ─── */
@media (max-width: 1023px) {
  .desktop-search-bar {
    display: none;
  }
}

/* ── Live map fullscreen on mobile ──────────────────────────── */
@media (max-width: 1023px) {
  #live-map-wrapper,
  #live-map {
    height: calc(100vh - var(--mobile-header-height) - var(--bottom-nav-height) - var(--safe-bottom)) !important;
  }
}

/* ── Drawer panels: full width on mobile ────────────────────── */
@media (max-width: 767px) {
  #detail-drawer {
    width: 100% !important;
    right: 0 !important;
  }
}

/* ── Notification dropdown: full width on mobile ────────────── */
@media (max-width: 1023px) {
  #notification-dropdown {
    position: fixed !important;
    top: auto !important;
    bottom: calc(var(--bottom-nav-height) + var(--safe-bottom)) !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    border-radius: 1rem 1rem 0 0 !important;
  }
}

/* ── KPI Card compact on very small screens ─────────────────── */
@media (max-width: 380px) {
  .kpi-card-value {
    font-size: 1.25rem !important;
  }
}

/* ── Header padding adjustments on mobile ───────────────────── */
@media (max-width: 1023px) {
  header.flex.items-center,
  .page-header {
    padding-left: 1rem;
    padding-right: 1rem;
    padding-top: 0.75rem;
    padding-bottom: 0.75rem;
  }

  /* Reduce inner padding on all page main content areas */
  .px-6 { padding-left: 1rem; padding-right: 1rem; }
  .px-8 { padding-left: 1rem; padding-right: 1rem; }
}
