/* static/styles/mobile.css
   Responsive breakpoints + off-canvas sidebar + PWA/field-crew optimisations
   Breakpoints: 1100 | 900 | 768 | 480
   ------------------------------------------------------------------ */

/* ================================================================
   0.  SHARED PWA / TOUCH POLISH  (applies at all widths)
   ================================================================ */

/* Respect the notch / home-indicator safe areas */
:root {
  --safe-top:    env(safe-area-inset-top,    0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left,   0px);
  --safe-right:  env(safe-area-inset-right,  0px);
  --bottom-nav-h: 60px;
}

/* Smooth scrolling on iOS */
* { -webkit-overflow-scrolling: touch; }

/* Prevent double-tap zoom on interactive elements */
button, .btn, .nav-item, .tab, select, input[type="checkbox"], input[type="radio"] {
  touch-action: manipulation;
}

/* Remove 300ms tap delay everywhere */
html { touch-action: manipulation; }

/* ================================================================
   0b. OFFLINE / CONNECTIVITY INDICATOR  (always visible when needed)
   ================================================================ */

#pwa-offline-banner {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 9999;
  background: #f59e0b;
  color: #000;
  text-align: center;
  font-size: .85rem;
  font-weight: 600;
  padding: .4rem 1rem calc(.4rem + var(--safe-top));
  padding-top: calc(.4rem + var(--safe-top));
}
#pwa-offline-banner.visible { display: block; }

/* nudge everything down when the banner shows */
body.pwa-offline #main-content  { padding-top: calc(var(--safe-top) + 2.5rem) !important; }
body.pwa-offline .sidebar       { top: calc(var(--safe-top) + 2rem) !important; }

/* ================================================================
   0c. PWA INSTALL PROMPT BANNER  (shown when beforeinstallprompt fires)
   ================================================================ */

#pwa-install-banner {
  display: none;
  position: fixed;
  bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + .5rem);
  left: 1rem; right: 1rem;
  z-index: 9998;
  background: var(--navy, #1e3a5f);
  color: #fff;
  border-radius: 12px;
  padding: .85rem 1rem;
  box-shadow: 0 4px 20px rgba(0,0,0,.4);
  display: none;
  align-items: center;
  gap: .75rem;
}
#pwa-install-banner.visible { display: flex; }
#pwa-install-banner .pwa-install-icon { font-size: 1.6rem; flex-shrink: 0; }
#pwa-install-banner .pwa-install-text { flex: 1; font-size: .875rem; line-height: 1.35; }
#pwa-install-banner .pwa-install-text strong { display: block; }
#pwa-install-banner .pwa-install-actions { display: flex; gap: .5rem; flex-shrink: 0; }
#pwa-install-btn  { background: #2563eb; color: #fff; border: none; border-radius: 6px; padding: .45rem .9rem; font-weight: 600; cursor: pointer; font-size: .85rem; }
#pwa-install-dismiss { background: transparent; color: rgba(255,255,255,.6); border: none; cursor: pointer; font-size: 1.2rem; padding: 0 .25rem; }

/* 0d. BRANDED PWA INSTALL BANNERS (Solstice, Luxis, Outpost) */
.pwa-brand-banner { display: none; transition: opacity 0.3s ease, transform 0.3s ease; opacity: 0; transform: translateY(-8px); }
.pwa-brand-banner.visible { display: flex !important; opacity: 1; transform: translateY(0); }

/* Login page app badges hover */
.login-app-badge:hover { transform: translateY(-1px); box-shadow: 0 2px 12px rgba(0,0,0,0.3); }

/* ================================================================
   1.  TABLET WIDE  (≤ 1100px)
   ================================================================ */
@media (max-width: 1100px) {
  .sidebar { width: 200px; }
  #main-content { margin-left: 200px; padding: 1.25rem; }

  .grid-cols-3 { grid-template-columns: 1fr 1fr !important; }
  .grid-cols-4 { grid-template-columns: 1fr 1fr !important; }
}

/* ================================================================
   2.  TABLET  (≤ 900px) — sidebar collapses to icon rail
   ================================================================ */
@media (max-width: 900px) {
  .sidebar {
    width: 56px;
    overflow: hidden;
  }
  .sidebar .nav-item span,
  .sidebar .nav-section-label,
  .sidebar .user-info-text,
  .sidebar .logo-text { display: none; }
  .sidebar .nav-item { justify-content: center; padding: .6rem .5rem; }
  .sidebar .logo { justify-content: center; padding: .75rem .5rem; }
  #main-content { margin-left: 56px; }

  /* Two-col tables become scrollable */
  .table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

/* ================================================================
   3.  MOBILE  (≤ 768px) — new 5-item bottom nav + More drawer
   ================================================================ */

/* ── Mobile bottom nav (always hidden above 768px) ── */
.mobile-bottom-nav,
.mobile-more-overlay,
.mobile-more-drawer { display: none; }

@media (max-width: 768px) {

  /* ── Hide desktop sidebar entirely ── */
  #sidebar { display: none !important; }

  /* ── 5-item bottom nav bar ── */
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: calc(var(--bottom-nav-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: rgba(10,14,26,.97);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border, rgba(184,204,255,.12));
    z-index: 1000;
    align-items: center;
    justify-content: space-around;
  }
  .mobile-nav-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 6px 0;
    min-width: 56px;
    min-height: 48px;
    border: none;
    background: transparent;
    color: var(--muted, #5A6A88);
    cursor: pointer;
    font-family: inherit;
    position: relative;
    transition: color .15s;
  }
  .mobile-nav-tab .material-symbols-outlined { font-size: 24px; }
  .mobile-nav-label { font-size: 10px; font-weight: 500; }
  .mobile-nav-tab:hover { color: var(--app-text-muted, #8294B8); }
  .mobile-nav-tab.active { color: var(--blue, #2E75B6); }
  .mobile-nav-tab.active .material-symbols-outlined {
    font-variation-settings: 'FILL' 1, 'wght' 400, 'GRAD' 0, 'opsz' 24;
  }
  .mobile-nav-badge {
    position: absolute;
    top: 4px; right: 8px;
    width: 8px; height: 8px;
    background: #E74C3C;
    border-radius: 50%;
    border: 1.5px solid rgba(10,14,26,.97);
  }

  /* ── More drawer (half-sheet) ── */
  .mobile-more-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 1100;
    opacity: 0;
    transition: opacity .25s;
  }
  .mobile-more-overlay.open { display: block; opacity: 1; }

  .mobile-more-drawer {
    display: flex;
    flex-direction: column;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    max-height: 80dvh;
    background: var(--color-bg, #0F1320);
    border-top: 1px solid var(--border, rgba(184,204,255,.12));
    border-radius: 16px 16px 0 0;
    z-index: 1200;
    transform: translateY(100%);
    transition: transform .3s cubic-bezier(.32,.72,0,1);
    overflow: hidden;
  }
  .mobile-more-drawer.open { transform: translateY(0); }

  .mobile-more-handle {
    width: 36px; height: 4px;
    background: var(--border, rgba(184,204,255,.22));
    border-radius: 2px;
    margin: 8px auto 4px;
    flex-shrink: 0;
  }
  .mobile-more-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 20px 12px;
    flex-shrink: 0;
  }
  .mobile-more-title { font-size: 16px; font-weight: 600; }
  .mobile-more-close {
    width: 32px; height: 32px; border: none; background: transparent;
    color: var(--muted, #8294B8); font-size: 24px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    border-radius: 50%;
  }
  .mobile-more-close:hover { background: rgba(184,204,255,.08); }

  /* Quick actions row */
  .mobile-quick-actions {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    padding: 0 16px 16px;
    flex-shrink: 0;
  }
  .mobile-qa-btn {
    display: flex; flex-direction: column; align-items: center; gap: 6px;
    padding: 12px 4px; border-radius: 10px;
    border: 1px solid var(--border, rgba(184,204,255,.12));
    background: rgba(184,204,255,.04);
    color: var(--app-text-primary, #E6EBFF);
    cursor: pointer; font-family: inherit;
  }
  .mobile-qa-btn:active { background: rgba(184,204,255,.08); }
  .mobile-qa-icon {
    width: 36px; height: 36px; border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
  }
  .mobile-qa-icon .material-symbols-outlined { font-size: 20px; }
  .mobile-qa-icon.blue   { background: rgba(46,117,182,.15); color: #2E75B6; }
  .mobile-qa-icon.green  { background: rgba(39,174,96,.15);  color: #27AE60; }
  .mobile-qa-icon.amber  { background: rgba(245,158,11,.15); color: #F59E0B; }
  .mobile-qa-icon.purple { background: rgba(139,92,246,.15);  color: #A78BFA; }
  .mobile-qa-label { font-size: 11px; font-weight: 500; text-align: center; }

  /* Module list */
  .mobile-more-modules {
    flex: 1;
    overflow-y: auto;
    padding: 0 16px 16px;
    -webkit-overflow-scrolling: touch;
  }
  .mobile-more-modules::-webkit-scrollbar { width: 4px; }
  .mobile-more-modules::-webkit-scrollbar-thumb { background: rgba(255,255,255,.08); border-radius: 2px; }

  .mobile-more-group { margin-bottom: 12px; }
  .mobile-more-group-label {
    font-size: 10px; font-weight: 600; text-transform: uppercase;
    letter-spacing: .7px; color: var(--muted, #5A6A88);
    padding: 8px 4px 6px;
  }
  .mobile-more-item {
    display: flex; align-items: center; gap: 12px;
    width: 100%; padding: 10px 8px;
    border: none; background: transparent;
    color: var(--app-text-muted, #8294B8);
    cursor: pointer; font-family: inherit; font-size: 14px;
    border-radius: 8px; text-align: left;
  }
  .mobile-more-item:active { background: rgba(184,204,255,.08); }
  .mobile-more-item .material-symbols-outlined { font-size: 20px; color: var(--muted, #5A6A88); }

  /* User footer */
  .mobile-more-footer {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 20px calc(12px + var(--safe-bottom));
    border-top: 1px solid var(--border, rgba(184,204,255,.12));
    flex-shrink: 0;
  }
  .mobile-more-user { display: flex; align-items: center; gap: 10px; flex: 1; }
  .mobile-more-avatar {
    width: 32px; height: 32px; border-radius: 50%;
    background: linear-gradient(135deg, #2E75B6, #27AE60);
    display: flex; align-items: center; justify-content: center;
    font-size: 12px; font-weight: 600; color: #fff; flex-shrink: 0;
  }
  .mobile-more-username { font-size: 13px; font-weight: 500; color: var(--app-text-primary, #E6EBFF); }
  .mobile-more-userrole { font-size: 11px; color: var(--muted, #5A6A88); }
  .mobile-more-settings,
  .mobile-more-logout {
    width: 36px; height: 36px; border-radius: 8px;
    border: none; background: rgba(184,204,255,.06);
    color: var(--muted, #8294B8); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
  }
  .mobile-more-settings:active,
  .mobile-more-logout:active { background: rgba(184,204,255,.1); }

  /* ── Main content adjustments ── */
  #main {
    margin-left: 0 !important;
    margin-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom));
    padding-bottom: 0;
    border-radius: 0 !important;
  }
  #main > .content {
    padding: 1rem !important;
    padding-top: calc(1rem + var(--safe-top)) !important;
  }

  /* ── Simplified mobile topbar ── */
  .topbar-timeclock,
  .topbar-icon-group .topbar-icon-btn[aria-label="Email"],
  .topbar-icon-group .topbar-icon-btn[aria-label="Calendar"],
  .topbar-icon-group .topbar-icon-btn[aria-label="Messages"],
  .topbar-icon-group .topbar-icon-btn[aria-label="Workspace Settings"],
  .topbar-subtitle,
  #topbar-action { display: none !important; }

  .topbar {
    padding: 0 12px !important;
    gap: 8px !important;
  }
  .topbar-title { font-size: 16px !important; }
  .topbar-right { gap: 2px !important; }

  /* top header/toolbar */
  .page-header { padding: .75rem 0; }
  .page-header h1, .page-header .page-title { font-size: 1.2rem; }

  /* cards go full width */
  .card { border-radius: 8px; }
  .grid-cols-2,
  .grid-cols-3,
  .grid-cols-4 { grid-template-columns: 1fr !important; }

  /* stat cards: 2-up */
  .stat-cards,
  .stats-row { grid-template-columns: 1fr 1fr !important; gap: .75rem !important; }

  /* modals go full-screen on mobile */
  .modal-overlay { align-items: flex-end; }
  .modal {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 90dvh;
    border-radius: 16px 16px 0 0;
    overflow-y: auto;
    padding-top: .5rem;
  }
  .modal::before {
    content: '';
    display: block;
    width: 36px; height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: 0 auto .75rem;
  }

  /* tabs become a scrollable pill row */
  .tabs {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: .25rem;
    padding-bottom: .25rem;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }
  .tab {
    flex: 0 0 auto;
    white-space: nowrap;
    scroll-snap-align: start;
    padding: .4rem .85rem;
    border-radius: 20px;
    font-size: .8rem;
  }

  /* tables scroll horizontally */
  .table, table { font-size: .8rem; }
  .table-wrap, .card > .table, .card > table {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    display: block;
  }

  /* form grids collapse */
  .form-row, [style*="grid-template-columns: 1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* hide less-critical columns on small screens via data-hide-mobile */
  [data-hide-mobile] { display: none !important; }
}

/* ================================================================
   4.  MOBILE PORTRAIT SMALL  (≤ 480px) — extra polish
   ================================================================ */
@media (max-width: 480px) {

  /* Apple / Android minimum touch-target (44px minimum for interactive elements) */
  input, select, button, .btn, .nav-item, .tab { min-height: 44px; }
  a, [role="button"] { min-height: 44px; display: inline-flex; align-items: center; }

  /* Stat cards go 1-up on very small screens */
  .stat-cards,
  .stats-row { grid-template-columns: 1fr !important; }

  /* ── Responsive table stacking (vertical on small screens) ──
     Tables stack into card-like rows with data-label attributes shown as labels */
  table {
    display: block;
    width: 100%;
  }

  thead {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }

  tbody {
    display: block;
    width: 100%;
  }

  tr {
    display: block;
    margin-bottom: .75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: .5rem .75rem;
    background: var(--app-field-bg);
    width: 100%;
  }

  td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: .5rem;
    border-bottom: 1px solid var(--border);
    padding: .45rem 0;
    font-size: .85rem;
    line-height: 1.4;
    width: 100%;
  }

  td:last-child { border-bottom: none; }

  td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--app-text-muted);
    white-space: nowrap;
    flex-shrink: 0;
    min-width: 90px;
  }

  /* ── Table → Card-stack pattern ──
     Columns must set  data-label="Column Name"  on each <td>
     for this to work correctly.  Falls back gracefully otherwise. */
  .card-stack-mobile table,
  .card-stack-mobile thead,
  .card-stack-mobile tbody,
  .card-stack-mobile th,
  .card-stack-mobile td,
  .card-stack-mobile tr { display: block; }

  .card-stack-mobile thead tr {
    position: absolute;
    top: -9999px; left: -9999px;
  }

  .card-stack-mobile tr {
    margin-bottom: .75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: .5rem .75rem;
    background: var(--app-field-bg);
  }

  .card-stack-mobile td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: .5rem;
    border-bottom: 1px solid var(--grey);
    padding: .45rem 0;
    font-size: .85rem;
    line-height: 1.4;
  }

  .card-stack-mobile td:last-child { border-bottom: none; }

  .card-stack-mobile td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--app-text-muted);
    white-space: nowrap;
    flex-shrink: 0;
    min-width: 90px;
  }

  /* field-log / daily-log entries: bigger text for gloved fingers */
  .field-log-entry input,
  .field-log-entry textarea,
  .field-log-entry select { font-size: 1rem; }

  /* ── Float action button (FAB) for primary CTA on field pages ── */
  .pwa-fab {
    position: fixed;
    bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 1rem);
    right: 1rem;
    width: 56px; height: 56px;
    border-radius: 50%;
    background: var(--blue, #2563eb);
    color: #fff;
    font-size: 1.5rem;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 4px 16px rgba(37,99,235,.45);
    border: none; cursor: pointer;
    z-index: 900;
    transition: transform .15s;
  }
  .pwa-fab:active { transform: scale(.92); }
}

/* ================================================================
   5.  WINDOW CONTROLS OVERLAY  (desktop PWA installed, title bar space)
   ================================================================ */
@media (display-mode: window-controls-overlay) {
  #main-content {
    padding-top: calc(env(titlebar-area-height, 40px) + 1rem) !important;
  }
  .sidebar {
    padding-top: env(titlebar-area-height, 40px);
  }
}

/* ================================================================
   6.  STANDALONE PWA  (installed on home screen)
   ================================================================ */
@media (display-mode: standalone) {
  /* Extra top padding for the status bar */
  #main-content {
    padding-top: calc(var(--safe-top) + 1rem);
  }
  /* iOS Safari notch — sidebar top */
  .sidebar {
    padding-top: var(--safe-top);
  }
}

/* ================================================================
   7.  PRINT  (basic print styles so reports are usable)
   ================================================================ */
/* ═══════════════════════════════════════════════════════════════════════════
   PHASE 5-7 MOBILE ADDITIONS — User menu, favorites in drawer, touch fixes
   ═══════════════════════════════════════════════════════════════════════════ */

/* ─── Mobile: Hide individual topbar icons (already simplified) ─────────── */
@media (max-width: 768px) {
  /* Topbar user dropdown: full-width on mobile */
  .topbar-user-dropdown {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: var(--radius-lg, 12px) var(--radius-lg, 12px) 0 0;
    max-height: 70dvh;
    overflow-y: auto;
    animation: mobileSheetSlideUp 0.25s ease-out;
  }
  @keyframes mobileSheetSlideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }
  .topbar-user-dropdown.open {
    display: block;
  }
  .topbar-user-dropdown-item {
    min-height: 48px;
    padding: 12px 20px;
    font-size: 15px;
  }
  .topbar-user-dropdown-item .material-symbols-outlined {
    font-size: 22px;
  }

  /* Notification tabs on mobile */
  .topbar-notify-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .topbar-notify-tab {
    min-height: 44px;
    font-size: 12px;
  }

  /* Sidebar favorites hidden on mobile (appear in More drawer instead) */
  .sidebar-favorites {
    display: none;
  }

  /* KPI trend badges: slightly smaller on mobile */
  .role-kpi-trend {
    font-size: 10px;
    padding: 1px 4px;
  }
}

/* ─── Mobile: Touch target enforcement (44px minimum) ───────────────────── */
@media (max-width: 480px) {
  button, a, [role="button"],
  input[type="checkbox"], input[type="radio"],
  select, .nav-item, .tab, .badge,
  .topbar-icon-btn, .role-quick-action,
  .sidebar-fav-item, .topbar-notify-tab {
    min-height: 44px;
  }
  /* Prevent any horizontal overflow */
  .content, .section, .card, .table-wrap {
    max-width: 100vw;
    overflow-x: hidden;
  }
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  /* Forms single-column on small screens */
  .form-row, .form-grid {
    grid-template-columns: 1fr !important;
    flex-direction: column !important;
  }
}

/* ─── Favorites in More Drawer (mobile) ─────────────────────────────────── */
.mobile-more-favorites {
  padding: 8px 16px;
  border-bottom: 1px solid var(--color-border, rgba(255,255,255,0.08));
}
.mobile-more-favorites .nav-section {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--color-text-muted, #888);
  margin-bottom: 4px;
}
.mobile-more-favorites .mobile-fav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: var(--radius-sm, 4px);
  color: var(--color-text, #E0E6ED);
  font-size: 14px;
  cursor: pointer;
  min-height: 44px;
}
.mobile-more-favorites .mobile-fav-item:active {
  background: var(--color-hover, rgba(255,255,255,0.06));
}

@media print {
  .sidebar, #pwa-offline-banner, #pwa-install-banner,
  .btn, .modal-overlay { display: none !important; }
  #main-content { margin: 0 !important; padding: 0 !important; }
  .card { break-inside: avoid; }
  table { font-size: .75rem; }
}
