/* ============================================================
   POGCM — Mobile / Small-screen Enhancement Stylesheet
   Purpose: compact the existing admin layout on phones and
   small screens, and eliminate horizontal scrollbars WITHOUT
   changing any PHP / markup.
   Loaded AFTER the main style.css so it overrides.
   ============================================================ */

/* ---- Global: stop horizontal overflow anywhere ---- */
html, body {
  max-width: 100%;
  overflow-x: hidden;
}

.container-scroller,
.page-body-wrapper,
.main-panel,
.content-wrapper {
  max-width: 100%;
  min-width: 0;
}

/* Bootstrap rows sometimes cause a 15px horizontal scroll on phones */
@media (max-width: 767.98px) {
  .row { margin-left: 0; margin-right: 0; }
  .row > [class*="col-"] { padding-left: 8px; padding-right: 8px; }
}

/* ============================================================
   TABLES — this is the #1 cause of horizontal scrollbars.
   On phones, turn each <tr> into a stacked card and hide the
   header row. Each <td> shows its column name via the
   data-label attribute if present, or the th text via JS fallback.
   ============================================================ */

/* Always let table-responsive behave, but on mobile we don't need
   the horizontal scroll because we stack. */
@media (max-width: 767.98px) {
  .table-responsive {
    overflow-x: visible !important;
    border: 0 !important;
  }

  .table,
  .table thead,
  .table tbody,
  .table tr,
  .table th,
  .table td {
    display: block;
    width: 100%;
    box-sizing: border-box;
  }

  /* Hide the real header row on phones — we show labels per cell instead. */
  .table thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }

  .table tr {
    margin-bottom: 10px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 6px 8px;
    background: #fff;
  }

  .table td {
    border: 0;
    border-bottom: 1px dashed #eef0f3;
    padding: 6px 6px 6px 42%;
    position: relative;
    text-align: left;
    white-space: normal !important;
    word-break: break-word;
    min-height: 28px;
  }
  .table td:last-child { border-bottom: 0; }

  /* Column label from data-label (we inject this via JS below). */
  .table td::before {
    content: attr(data-label);
    position: absolute;
    left: 6px;
    top: 6px;
    width: 38%;
    font-weight: 600;
    color: #475569;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    white-space: normal;
  }

  /* If a td has no data-label, use a compact inline style instead. */
  .table td:not([data-label]) {
    padding-left: 6px;
  }
  .table td:not([data-label])::before { content: none; }

  /* Action cells: let buttons wrap nicely */
  .table td .btn,
  .table td .btn-sm {
    margin: 2px 2px 0 0;
  }
}

/* On tablets, just let the table-responsive scroll — but keep it
   contained to the viewport so the PAGE doesn't scroll horizontally. */
@media (min-width: 768px) {
  .table-responsive {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* ============================================================
   CARDS — tighten padding on phones so more content fits
   ============================================================ */
@media (max-width: 767.98px) {
  .content-wrapper { padding: 12px 10px !important; }
  .card            { margin-bottom: 12px; border-radius: 8px; }
  .card-body       { padding: 12px !important; }
  .card-title      { font-size: 1rem; margin-bottom: 10px; }

  h1 { font-size: 1.4rem; }
  h2 { font-size: 1.25rem; }
  h3 { font-size: 1.1rem; }
  h4 { font-size: 1rem; }
  h5 { font-size: 0.95rem; }
  h6 { font-size: 0.9rem; }
  p, li, td, th, label { font-size: 0.88rem; }
}

/* ============================================================
   FORMS — stack labels above inputs, full width on phones
   ============================================================ */
@media (max-width: 767.98px) {
  .form-inline { display: flex; flex-wrap: wrap; gap: 6px; }
  .form-inline > * { flex: 1 1 100%; }
  .form-inline .form-control,
  .form-inline select,
  .form-inline .btn { width: 100%; }

  .form-group { margin-bottom: 10px; }

  .form-control,
  .form-control-sm,
  select.form-control,
  textarea.form-control {
    width: 100%;
    font-size: 0.92rem;
  }

  .input-group { flex-wrap: wrap; }
  .input-group > .form-control,
  .input-group > .input-group-prepend,
  .input-group > .input-group-append {
    width: 100%;
    flex: 1 1 100%;
  }
  .input-group-text { width: 100%; justify-content: center; }
}

/* ============================================================
   BUTTONS & BUTTON GROUPS — wrap, don't overflow
   ============================================================ */
@media (max-width: 767.98px) {
  .btn-group,
  .btn-toolbar {
    flex-wrap: wrap;
    gap: 4px;
  }
  .btn-group > .btn { border-radius: 4px !important; }

  .btn { white-space: normal; }     /* let long labels wrap */
  .btn-sm { padding: 4px 8px; font-size: 0.8rem; }

  /* Remove min-width enforcers from specific modules that caused overflow */
  .counter-btn { min-width: 0 !important; margin: 2px !important; flex: 1 1 auto; }
}

/* ============================================================
   MODALS — full-screen-ish on phones for usability
   ============================================================ */
@media (max-width: 575.98px) {
  .modal-dialog {
    margin: 8px;
    max-width: calc(100% - 16px);
  }
  .modal-body  { padding: 12px; }
  .modal-header, .modal-footer { padding: 10px 12px; }
  .modal-footer { flex-wrap: wrap; gap: 6px; }
  .modal-footer .btn { flex: 1 1 auto; }
}

/* ============================================================
   NAV TABS & PILLS — scroll horizontally within their strip only
   so they never push the whole page wide
   ============================================================ */
@media (max-width: 767.98px) {
  .nav-tabs, .nav-pills {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    border-bottom: 1px solid #dee2e6;
  }
  .nav-tabs::-webkit-scrollbar,
  .nav-pills::-webkit-scrollbar { height: 0; }
  .nav-tabs .nav-item,
  .nav-pills .nav-item { flex: 0 0 auto; }
  .nav-tabs .nav-link,
  .nav-pills .nav-link { padding: 6px 10px; font-size: 0.85rem; }
}

/* ============================================================
   IMAGES / MEDIA — never wider than their container
   ============================================================ */
img, video, iframe, canvas, svg {
  max-width: 100%;
  height: auto;
}

/* Pre / code blocks — wrap on small screens instead of scrolling */
@media (max-width: 767.98px) {
  pre, code {
    white-space: pre-wrap !important;
    word-wrap: break-word;
    font-size: 0.8rem;
  }
}

/* ============================================================
   MODULE-SPECIFIC FIXES for widths/nowraps found in the pages
   ============================================================ */
@media (max-width: 767.98px) {

  /* attendance_logbook.php — fixed-width name column */
  .table th[style*="min-width"],
  .table td[style*="min-width"] {
    min-width: 0 !important;
  }

  /* donations.php / offering.php denomination grid: 2 cols instead of 3 */
  .denom-grid {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 6px !important;
  }

  /* praise_worship_lineup.php — let long song titles wrap on tight screens */
  .song-picker-title,
  .lineup-song-name {
    white-space: normal !important;
    overflow: visible !important;
    text-overflow: clip !important;
  }

  /* Generic: kill any inline min-width that breaks phones */
  [style*="min-width: 260px"],
  [style*="min-width:260px"],
  [style*="min-width: 200px"],
  [style*="min-width:200px"] {
    min-width: 0 !important;
  }

  /* Any ad-hoc nowrap that's causing squeeze */
  .text-nowrap { white-space: normal !important; }
}

/* ============================================================
   TYPE-BASED COMPACT SPACING ON VERY SMALL SCREENS
   ============================================================ */
@media (max-width: 420px) {
  .content-wrapper { padding: 10px 8px !important; }
  .card-body       { padding: 10px !important; }
  .btn             { font-size: 0.82rem; padding: 5px 10px; }
  .form-control    { font-size: 0.88rem; padding: 6px 8px; }
}

/* ============================================================
   PRINT — keep the existing print rules working
   ============================================================ */
@media print {
  html, body { overflow: visible !important; }
  .table, .table thead, .table tbody, .table tr, .table th, .table td {
    display: revert !important;
    width: auto !important;
  }
  .table thead tr { position: static !important; }
  .table td::before { content: none !important; }
  .table td { padding: 4px 6px !important; }
}
