/* MedDRG r17.1 — mobile (phones) + tablet/desktop. Breakpoint: 768px */

:root {
  --touch: 44px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --banner-h: 48px;
}
html { scroll-padding-top: calc(var(--banner-h) + 8px); }
.card h3, .card h2, .step, .num, .dz-title { scroll-margin-top: calc(var(--banner-h) + 8px); }

/* ---- shared touch targets ---- */
.btn, .tab, .tab-select, select, input[type="date"], input[type="search"],
input[type="email"], input[type="password"], input[type="text"], input[type="file"] + .btn {
  min-height: var(--touch);
}
.btn { padding: 12px 16px; }
.btn.small { min-height: 40px; padding: 8px 12px; }
input, select, textarea { font-size: 16px; } /* iOS zoom prevention */

.mobile-only { display: none !important; }
.desktop-only { display: initial; }

/* ---- tablet/desktop: keep tidy current layout ---- */
@media (min-width: 768px) {
  .topbar { flex-wrap: nowrap; }
  .who { flex-wrap: wrap; justify-content: flex-end; }
  .tabs { flex-wrap: wrap; overflow: visible; }
  .sticky-actions-mobile { position: static; box-shadow: none; padding: 0; }
}

/* ---- phones ~360–430px ---- */
@media (max-width: 767px) {
  .mobile-only { display: block !important; }
  .desktop-only { display: none !important; }
  .tabs.desktop-only { display: none !important; }

  body { overflow-x: hidden; }
  .trial-banner { font-size: 13px; padding: 10px 12px; line-height: 1.35; }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 12px;
  }
  .topbar .brand { width: 100%; }
  .who {
    width: 100%;
    flex-wrap: wrap;
    justify-content: flex-start;
    text-align: left;
    gap: 8px;
  }
  .who .btn, .who a.btn {
    flex: 1 1 calc(50% - 8px);
    text-align: center;
    min-height: var(--touch);
  }

  .wrap, .wrap.wide { margin: 12px auto; padding: 0 10px calc(24px + var(--safe-bottom)); max-width: 100%; }
  .card { padding: 14px; border-radius: 14px; }
  .card.narrow { margin: 16px auto; max-width: 100%; }

  /* tabs → select on mobile (admin) */
  .tab-select {
    display: block !important;
    width: 100%;
    margin-bottom: 12px;
    min-height: var(--touch);
    font-size: 16px;
    font-weight: 700;
    border: 1px solid var(--md-primary);
    border-radius: 12px;
    background: var(--md-primary);
    color: var(--md-on-primary);
    padding: 10px 12px;
  }
  .tabs {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 6px;
    margin-bottom: 10px;
    padding-bottom: 4px;
  }
  .tabs.hide-on-mobile-select { display: none !important; }
  .tab { flex: 0 0 auto; white-space: nowrap; min-height: var(--touch); }

  .filters {
    flex-direction: column;
    align-items: stretch;
  }
  .filters label { min-width: 0; width: 100%; }
  .filter-btns { width: 100%; }
  .filter-btns .btn { flex: 1; }

  /* tables → cards */
  .tablewrap { overflow: visible; margin-top: 8px; }
  table.grid.cardify thead { display: none; }
  table.grid.cardify,
  table.grid.cardify tbody { display: block; width: 100%; }
  table.grid.cardify tr {
    display: block;
    background: var(--md-surface);
    border: 1px solid var(--md-border);
    border-radius: 14px;
    padding: 10px 12px;
    margin-bottom: 10px;
    box-shadow: 0 1px 2px rgba(0,0,0,.04);
  }
  table.grid.cardify td {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: flex-start;
    padding: 6px 0;
    border-bottom: 1px dashed var(--md-bg);
    white-space: normal;
    word-break: break-word;
  }
  table.grid.cardify td:last-child { border-bottom: 0; }
  table.grid.cardify td::before {
    content: attr(data-label);
    flex: 0 0 38%;
    max-width: 42%;
    color: var(--md-muted);
    font-weight: 600;
    font-size: 12px;
    padding-right: 8px;
  }
  table.grid.cardify td.card-hide { display: none; }
  table.grid.cardify td.card-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 8px;
    padding-top: 10px;
  }
  table.grid.cardify td.card-actions::before { display: none; }
  table.grid.cardify td.card-actions .btn { flex: 1 1 auto; min-width: 120px; }

  /* tables that stay scrollable (mark with .scroll-on-mobile) */
  .tablewrap.scroll-on-mobile { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tablewrap.scroll-on-mobile table.grid { min-width: 640px; }
  .tablewrap.scroll-on-mobile table.grid.cardify thead { display: table-header-group; }
  .tablewrap.scroll-on-mobile table.grid.cardify,
  .tablewrap.scroll-on-mobile table.grid.cardify tbody,
  .tablewrap.scroll-on-mobile table.grid.cardify tr { display: table; width: 100%; }
  .tablewrap.scroll-on-mobile table.grid.cardify tr { display: table-row; border: 0; padding: 0; margin: 0; box-shadow: none; border-radius: 0; }
  .tablewrap.scroll-on-mobile table.grid.cardify td { display: table-cell; }
  .tablewrap.scroll-on-mobile table.grid.cardify td::before { content: none; }

  /* r16.1: action bars are INLINE (not sticky) so they never cover section headers.
     Primary buttons still full-width and ≥44px for touch. */
  .actions.sticky-actions-mobile,
  .sticky-actions-mobile {
    position: static;
    z-index: auto;
    background: transparent;
    border-top: 0;
    box-shadow: none;
    margin: 12px 0 0;
    padding: 0;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
  }
  .actions.sticky-actions-mobile .btn.primary,
  .sticky-actions-mobile .btn.primary { flex: 1 1 100%; min-height: var(--touch); }

  /* bulk import CTA: full-width block + help text below (no overlap) */
  .bulk-import-cta {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    margin: 12px 0;
  }
  .bulk-import-cta .btn { width: 100%; text-align: center; }
  .bulk-import-cta .muted { margin: 0; }

  /* full-screen modals */
  .modal {
    align-items: stretch;
    justify-content: stretch;
    padding: 0;
  }
  .modal-box {
    max-width: none;
    width: 100%;
    min-height: 100%;
    border-radius: 0;
    padding: 16px 16px calc(16px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
  }
  .modal-box .actions { margin-top: auto; }
  .rf-modal { padding: 0; }
  .rf-panel {
    max-width: none;
    margin: 0;
    border-radius: 0;
    min-height: 100%;
    padding: 14px 14px calc(28px + var(--safe-bottom));
  }
  .rf-actions.sticky { top: 0; }

  .an-row { flex-direction: column; align-items: stretch; }
  .an-input { max-width: none; width: 100%; min-height: var(--touch); }
  .dz-buttons .btn { flex: 1 1 calc(50% - 8px); min-height: var(--touch); }
  .dropzone { padding: 20px 12px; }
  .an-grid { grid-template-columns: 1fr; }

  .imp-preview th, .imp-preview td { font-size: 12px; }
  /* import table also cardifies via .cardify */

  .toast { bottom: calc(18px + var(--safe-bottom)); width: calc(100% - 24px); left: 12px; transform: none; }
  .foot { padding-bottom: calc(24px + var(--safe-bottom)); }
}

/* header alias used by import.html */
.top { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 20px; background: var(--md-surface); border-bottom: 1px solid var(--md-border); }
@media (max-width: 767px) {
  .top { flex-direction: column; align-items: stretch; padding: 12px; }
}
