/* =============================================================
   IMMEDICARE PORTAL — MOBILE LAYER
   All mobile styles scoped to max-width:879px
   Desktop (880px+) untouched except minor touch-target boosts
   ============================================================= */

/* ── Safe area tokens (iPhone notch / home bar) ── */
:root {
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --tab-h: 64px; /* bottom tab bar height */
}

/* ── Base: smooth momentum scrolling everywhere ── */
html { -webkit-overflow-scrolling: touch; }
* { -webkit-tap-highlight-color: transparent; }

/* ── Ensure body doesn't scroll behind fixed bars ── */
@media (max-width: 879px) {
  body { padding-bottom: 0; }
}

/* =============================================================
   TOPBAR — replace generic header with app chrome
   ============================================================= */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--plum);
  color: #fff;
  padding: 0 16px;
  padding-top: var(--sat);
  height: calc(54px + var(--sat));
  position: sticky;
  top: 0;
  z-index: 100;
  gap: 12px;
}
@media (min-width: 880px) { .topbar { display: none; } }

/* Logo / page title area */
.tb-logo { font-family: var(--d); font-size: 17px; font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Back button — shown via PHP class .tb-back */
.tb-back {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,.12);
  color: #fff; flex-shrink: 0;
  text-decoration: none;
  transition: background .15s;
}
.tb-back:hover { background: rgba(255,255,255,.2); }
.tb-back svg { display: block; }

/* Right action slot (upload button etc.) */
.tb-action {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,.12);
  color: #fff; flex-shrink: 0;
  text-decoration: none;
  transition: background .15s;
}
.tb-action:hover { background: rgba(255,255,255,.2); }

/* =============================================================
   BOTTOM TAB BAR — the core of the app experience
   ============================================================= */
.bottom-nav {
  display: none;
}
@media (max-width: 879px) {
  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: calc(var(--tab-h) + var(--sab));
    padding-bottom: var(--sab);
    background: var(--plum);
    border-top: 1px solid rgba(255,255,255,.12);
    z-index: 100;
    align-items: stretch;
  }
  .bottom-nav a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: rgba(255,255,255,.5);
    text-decoration: none;
    font-size: 10.5px;
    font-weight: 600;
    letter-spacing: .02em;
    padding: 8px 4px;
    transition: color .15s;
    position: relative;
  }
  .bottom-nav a.active { color: #fff; }
  .bottom-nav a.active svg { stroke: #fff; }
  .bottom-nav a svg { stroke: rgba(255,255,255,.5); transition: stroke .15s; }
  /* Active indicator dot */
  .bottom-nav a.active::after {
    content: '';
    position: absolute;
    bottom: calc(6px + var(--sab));
    width: 4px; height: 4px;
    border-radius: 50%;
    background: var(--brand2);
  }
  /* Badge on tab (e.g. unread count) */
  .tab-badge {
    position: absolute;
    top: 8px; right: calc(50% - 18px);
    background: #E53E3E;
    color: #fff;
    font-size: 9px; font-weight: 700;
    min-width: 16px; height: 16px;
    border-radius: 999px;
    display: flex; align-items: center; justify-content: center;
    padding: 0 4px;
    line-height: 1;
  }
}

/* =============================================================
   SHELL / MAIN — add bottom padding for tab bar
   ============================================================= */
@media (max-width: 879px) {
  .shell { grid-template-columns: 1fr !important; }
  .main {
    padding: 16px 16px calc(var(--tab-h) + var(--sab) + 24px) !important;
    max-width: 100% !important;
  }
}

/* =============================================================
   PAGE HEAD — compact on mobile
   ============================================================= */
@media (max-width: 879px) {
  .page-head {
    flex-direction: column;
    gap: 10px;
    margin-bottom: 18px;
  }
  .page-head h1 { font-size: 20px; }
  .page-head .ph-sub { font-size: 13px; }
  /* Full-width primary action */
  .page-head > .btn, .page-head > a.btn {
    width: 100%;
    justify-content: center;
  }
}

/* =============================================================
   STAT CARDS — 2-col grid on mobile
   ============================================================= */
@media (max-width: 879px) {
  .grid-stats {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px;
    margin-bottom: 18px;
  }
  .stat { padding: 14px 14px; }
  .stat .val { font-size: 24px !important; }
  .stat .val.serif { font-size: 22px !important; }
}

/* =============================================================
   CARDS — full-bleed on mobile (no side margin)
   ============================================================= */
@media (max-width: 879px) {
  .card { border-radius: 14px; }
  .card-pad { padding: 16px; }
  .card-head { padding: 14px 16px; }
  .card-head h2 { font-size: 14.5px; }
  .mt16 { margin-top: 12px; }
  .mt24 { margin-top: 16px; }
}

/* =============================================================
   TABLES → DOC CARDS (mobile-only)
   Documents list, Queue, Admin — hide table, show cards
   ============================================================= */
@media (max-width: 879px) {
  /* Hide table entirely on mobile */
  .tbl { display: none; }

  /* Doc card list — PHP outputs these alongside the table */
  .doc-card-list { display: flex; flex-direction: column; }
  .doc-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--line);
    text-decoration: none;
    color: inherit;
    transition: background .12s;
    -webkit-user-select: none;
  }
  .doc-card:last-child { border-bottom: none; }
  .doc-card:active { background: var(--lilac); }
  .doc-card-icon {
    width: 42px; height: 42px;
    border-radius: 10px;
    background: var(--lilac);
    color: var(--brand);
    display: flex; align-items: center; justify-content: center;
    font-size: 11px; font-weight: 700;
    flex-shrink: 0;
    letter-spacing: .03em;
  }
  .doc-card-body { flex: 1; min-width: 0; }
  .doc-card-name {
    font-size: 14px; font-weight: 600;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    color: var(--ink); margin-bottom: 4px;
  }
  .doc-card-meta {
    display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  }
  .doc-card-date { font-size: 11.5px; color: var(--muted); }
  .doc-card-chevron {
    color: var(--line); flex-shrink: 0;
  }
}

/* Hide doc-card-list on desktop, show tbl */
@media (min-width: 880px) {
  .doc-card-list { display: none; }
}

/* =============================================================
   FILTER BAR — horizontal scroll chips on mobile
   ============================================================= */
@media (max-width: 879px) {
  .filter-bar {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 12px 16px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-bottom: 1px solid var(--line);
  }
  .filter-bar::-webkit-scrollbar { display: none; }

  /* Hide old select-based filters on mobile */
  .filter-selects { display: none; }

  /* Chip style */
  .filter-chip {
    display: inline-flex; align-items: center;
    padding: 7px 14px;
    border-radius: 999px;
    font-size: 13px; font-weight: 600;
    white-space: nowrap;
    background: var(--lilac);
    color: var(--muted);
    border: 1.5px solid var(--line);
    text-decoration: none;
    transition: .15s;
    flex-shrink: 0;
  }
  .filter-chip.active {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
  }
}
@media (min-width: 880px) {
  .filter-bar { display: none; }
  .filter-selects { display: flex; }
}

/* =============================================================
   BUTTONS — minimum 44px tap target
   ============================================================= */
@media (max-width: 879px) {
  .btn { min-height: 44px; font-size: 15px; border-radius: 12px; }
  .btn.sm { min-height: 38px; font-size: 13px; }
  /* Stack button pairs */
  .btn-row {
    display: flex; flex-direction: column; gap: 10px;
  }
  .btn-row .btn { width: 100%; }
}

/* =============================================================
   FORMS — touch-friendly inputs
   ============================================================= */
@media (max-width: 879px) {
  .field input, .field select, .field textarea {
    font-size: 16px !important; /* prevent iOS auto-zoom */
    padding: 13px 14px;
    border-radius: 12px;
  }
  select {
    font-size: 16px !important;
    padding: 13px 36px 13px 14px;
    border-radius: 12px;
  }
  .row2 { grid-template-columns: 1fr !important; }
  .auth-card { padding: 28px 20px; border-radius: 16px; }
}

/* =============================================================
   UPLOAD PAGE — full-width tap area on mobile
   ============================================================= */
@media (max-width: 879px) {
  .drop {
    padding: 32px 20px;
    border-radius: 16px;
  }
  .drop h3 { font-size: 15px; }
  /* Make the whole drop zone one big tap button */
  .drop-tap-hint {
    display: inline-flex; align-items: center; gap: 8px;
    margin-top: 14px;
    background: var(--brand);
    color: #fff;
    padding: 12px 24px;
    border-radius: 10px;
    font-size: 15px; font-weight: 600;
    cursor: pointer;
  }
}

/* =============================================================
   CHAT COMPOSE — safe area + larger tap area on mobile
   ============================================================= */
@media (max-width: 879px) {
  .chat-thread { max-height: 340px; }
  .chat-compose { padding: 10px 12px; }
  .chat-compose-input { font-size: 16px !important; } /* no iOS zoom */
  .chat-send-btn { width: 46px; height: 46px; }
}

/* =============================================================
   DOCUMENT DETAIL — mobile layout
   ============================================================= */
@media (max-width: 879px) {
  /* Stat grid: 2x2 */
  .grid-stats { grid-template-columns: 1fr 1fr !important; }

  /* Page head: stack filename and buttons */
  .page-head > div:first-child h1 { font-size: 15px; word-break: break-word; }

  /* Status bar: stacked */
  .status-action-bar { flex-direction: column; align-items: stretch; gap: 10px; }
  .status-action-bar select { width: 100%; }
  .status-action-bar .btn { width: 100%; }

  /* Activity rows: tighter */
  .activity-row { padding: 11px 16px; gap: 10px; }
  .activity-when { font-size: 11px; }
}

/* =============================================================
   MODAL — full-screen on mobile
   ============================================================= */
@media (max-width: 879px) {
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal-box {
    border-radius: 20px 20px 0 0;
    max-width: 100% !important;
    max-height: 90vh;
  }
  .modal-body { max-height: calc(90vh - 60px); }
  /* Full-screen preview */
  .preview-modal-inner {
    border-radius: 0 !important;
    height: 100vh !important;
    max-width: 100% !important;
  }
}

/* =============================================================
   SECURITY PAGE — OTP input sizing on mobile
   ============================================================= */
@media (max-width: 879px) {
  .otp-input-row input {
    width: 40px; height: 50px; font-size: 20px;
  }
  .recovery-codes { grid-template-columns: 1fr 1fr; font-size: 12px; }
}

/* =============================================================
   ADMIN / USER TABLES — card list on mobile
   ============================================================= */
@media (max-width: 879px) {
  /* admin-card-list: same pattern as doc-card-list */
  .admin-card-list { display: flex; flex-direction: column; }
  .admin-card {
    display: flex; align-items: center; gap: 12px;
    padding: 14px 16px; border-bottom: 1px solid var(--line);
  }
  .admin-card:last-child { border-bottom: none; }
  .admin-card-av {
    width: 38px; height: 38px; border-radius: 50%;
    background: var(--lilac); color: var(--brand);
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 14px; flex-shrink: 0;
  }
  .admin-card-body { flex: 1; min-width: 0; }
  .admin-card-name { font-size: 14px; font-weight: 600; color: var(--ink); }
  .admin-card-meta { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
  .admin-card-actions { display: flex; gap: 6px; flex-shrink: 0; }
}
@media (min-width: 880px) {
  .admin-card-list { display: none; }
}

/* =============================================================
   TOGGLE ROW — full touch target
   ============================================================= */
@media (max-width: 879px) {
  .toggle-row { padding: 16px 0; }
  .toggle { width: 50px; height: 28px; }
  .toggle .track:before { width: 22px; height: 22px; }
  .toggle input:checked + .track:before { transform: translateX(22px); }
}

/* =============================================================
   MISC MOBILE POLISH
   ============================================================= */
@media (max-width: 879px) {
  /* No horizontal overflow at all */
  .shell, .main { overflow-x: hidden; }

  /* Toasts: full width on mobile */
  .flashes { top: calc(54px + var(--sat) + 10px); left: 12px; right: 12px; max-width: none; }
  .toast { border-radius: 12px; }

  /* File queue: fill width */
  .file-queue { margin-top: 10px; }
  .file-row { padding: 10px; border-radius: 10px; }

  /* Badges: slightly larger touch-friendly */
  .badge { font-size: 11.5px; padding: 4px 10px; }
}

/* =============================================================
   PWA / ADD TO HOME SCREEN FEEL
   ============================================================= */
@media (display-mode: standalone) {
  .topbar { padding-top: calc(var(--sat) + 4px); height: calc(58px + var(--sat)); }
}
