/**
 * KaiNet shared responsive utilities.
 *
 * Drop-in stylesheet that adds tablet/mobile breakpoints to the custom-CSS
 * dev pages (admin_*, billing, profile, status, preflight_chat_ui, etc.).
 * Targets the shared class names those pages already use:
 *   .page-wrapper / .wrap   — page container
 *   .page-header / .header  — top bar
 *   .card                   — content card
 *   .table-wrap.responsive,
  .history-table-wrap.responsive  — opt-in card-collapse table
 *   .btn                    — buttons
 *   form inputs / selects   — touch sizing
 *
 * Pages opt into table card-collapse by adding `responsive` to their existing
 * `.table-wrap` class and `data-label="..."` to each <td>.
 */

/* ── Tablet (≤1024px) ─────────────────────────────────────────────── */
@media (max-width: 1024px) {
  .page-wrapper,
  .wrap {
    padding-left: 20px !important;
    padding-right: 20px !important;
    padding-top: 28px !important;
  }

  .page-header,
  .header {
    flex-wrap: wrap;
    gap: 12px;
  }
}

/* ── Phone / small tablet (≤768px) ────────────────────────────────── */
@media (max-width: 768px) {
  .page-wrapper,
  .wrap {
    padding-left: 16px !important;
    padding-right: 16px !important;
    padding-top: 24px !important;
    padding-bottom: 48px !important;
  }

  /* Stack header into a column so the title and actions don't compete */
  .page-header,
  .header {
    flex-direction: column;
    align-items: flex-start;
  }
  .page-header-right,
  .header-right {
    width: 100%;
    flex-wrap: wrap;
  }

  /* Heading scale-down */
  .page-title,
  .header h1,
  h1 {
    font-size: 1.4rem !important;
    line-height: 1.25;
  }
  .section-heading {
    font-size: 1.1rem !important;
  }

  /* Card padding shrinks so content has room */
  .card {
    padding: 20px !important;
    border-radius: 14px !important;
  }

  /* Touch targets ≥44px */
  .btn,
  button.btn,
  a.btn {
    min-height: 44px;
    padding: 12px 20px;
  }
  .btn-sm {
    min-height: 40px;
    padding: 10px 16px;
  }

  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  input[type="number"],
  input[type="url"],
  select,
  textarea,
  .search-input {
    min-height: 44px;
    font-size: 16px; /* prevents iOS zoom-on-focus */
  }

  /* Toolbar / filter rows wrap cleanly */
  .toolbar,
  .filters,
  .filter-bar {
    flex-wrap: wrap;
    gap: 10px;
  }
  .toolbar > *,
  .filters > * {
    flex: 1 1 auto;
    min-width: 0;
  }
}

/* ── Phone (≤640px) ───────────────────────────────────────────────── */
@media (max-width: 640px) {
  /* Side-by-side button rows wrap to full width */
  .actions,
  .button-row,
  .header-actions {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }
  .actions .btn,
  .button-row .btn,
  .header-actions .btn {
    width: 100%;
  }

  /* ── Card-collapse tables ─────────────────────────────────────────
   * Pages opt in by adding `responsive` to a wrapper class
   * (`.table-wrap` or `.history-table-wrap`) and `data-label` to each
   * <td>. The table reflows to a vertical card per row.
   */
  .table-wrap.responsive,
  .history-table-wrap.responsive {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    overflow: visible !important;
  }
  .table-wrap.responsive table,
  .table-wrap.responsive thead,
  .table-wrap.responsive tbody,
  .table-wrap.responsive tr,
  .table-wrap.responsive td,
  .history-table-wrap.responsive table,
  .history-table-wrap.responsive thead,
  .history-table-wrap.responsive tbody,
  .history-table-wrap.responsive tr,
  .history-table-wrap.responsive td {
    display: block;
    width: 100%;
  }
  .table-wrap.responsive thead,
  .history-table-wrap.responsive thead {
    position: absolute;
    left: -9999px;
    height: 0;
    overflow: hidden;
  }
  .table-wrap.responsive tbody tr,
  .history-table-wrap.responsive tbody tr {
    background: var(--card, #ffffff);
    border-radius: 14px;
    box-shadow: var(--shadow, 0 12px 40px rgba(22, 28, 38, 0.06));
    margin-bottom: 12px;
    padding: 6px 4px;
    cursor: default;
  }
  .table-wrap.responsive tbody tr:hover,
  .history-table-wrap.responsive tbody tr:hover {
    background: var(--card, #ffffff);
  }
  .table-wrap.responsive tbody td,
  .history-table-wrap.responsive tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 10px 16px !important;
    border-bottom: 1px solid var(--border, rgba(22, 28, 38, 0.06));
    text-align: right;
    min-height: 0;
  }
  .table-wrap.responsive tbody td:last-child,
  .history-table-wrap.responsive tbody td:last-child {
    border-bottom: none;
  }
  .table-wrap.responsive tbody td::before,
  .history-table-wrap.responsive tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted, #64748b);
    text-align: left;
    flex-shrink: 0;
  }
  /* Cells with no data-label fall back to centered (e.g. action button rows) */
  .table-wrap.responsive tbody td:not([data-label]),
  .history-table-wrap.responsive tbody td:not([data-label]) {
    justify-content: center;
    text-align: center;
  }

  /* Fallback for non-opt-in tables: at least make them scrollable */
  .table-wrap:not(.responsive) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Smaller heading scale on phones */
  .page-title,
  .header h1,
  h1 {
    font-size: 1.25rem !important;
  }
}

/* ── Body-level safety: prevent stray horizontal overflow ─────────── */
@media (max-width: 1024px) {
  html, body {
    overflow-x: hidden;
  }
}
