/**
 * Mobile-only layout and touch UX (max-width breakpoints).
 * Desktop styles are unchanged — all rules live inside @media blocks.
 */

/* ── ≤900px — matches sidebar drawer breakpoint ── */
@media (max-width: 900px) {
  html {
    -webkit-text-size-adjust: 100%;
  }

  body.tb-app {
    overflow-x: hidden;
  }

  body.sidebar-mobile-open {
    overflow: hidden;
  }

  .sidebar {
    width: min(88vw, 300px);
  }

  .app-main .container,
  .container {
    padding: 1rem 0.85rem 1.35rem;
    padding-left: max(0.85rem, env(safe-area-inset-left));
    padding-right: max(0.85rem, env(safe-area-inset-right));
  }

  .app-topbar {
    padding-top: max(0.75rem, env(safe-area-inset-top));
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  .card {
    padding: 1.15rem 1.1rem;
    margin-bottom: 1rem;
    border-radius: 12px;
  }

  .flash {
    font-size: 0.875rem;
    line-height: 1.45;
  }

  .sidebar-link,
  .sidebar-group-toggle,
  .sidebar-signout {
    min-height: 2.75rem;
  }
}

/* ── ≤768px — phones and narrow tablets ── */
@media (max-width: 768px) {
  .tb-page-header {
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
  }

  .tb-page-header .tb-page-icon {
    width: 42px;
    height: 42px;
    border-radius: 10px;
  }

  .tb-page-header h1,
  .card h1 {
    font-size: 1.2rem;
    line-height: 1.25;
  }

  .tb-page-header .lead,
  .card p.lead {
    font-size: 0.875rem;
  }

  /* Action bars — full-width tap targets */
  .tb-form-actions,
  .actions {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .tb-form-actions .btn,
  .actions .btn,
  .tb-form-actions a.btn {
    width: 100%;
    text-align: center;
    min-height: 2.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.65rem 1rem;
  }

  .tb-form-actions.primary-bar {
    margin: 0 -1.1rem -1.15rem;
    padding: 1rem 1.1rem;
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }

  .sticky-actions {
    margin-left: -0.85rem;
    margin-right: -0.85rem;
    margin-bottom: -0.5rem;
    padding: 0.85rem max(0.85rem, env(safe-area-inset-right))
      max(0.85rem, env(safe-area-inset-bottom))
      max(0.85rem, env(safe-area-inset-left));
    border-radius: 12px 12px 0 0;
  }

  .sticky-actions .tb-form-actions {
    margin: 0;
    padding: 0;
  }

  /* Questionnaire tabs — horizontal scroll */
  .tab-nav-card {
    padding: 0.85rem 0.75rem;
  }

  .tab-nav {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    gap: 0.4rem;
    padding-bottom: 0.35rem;
    margin: 0 -0.25rem;
    scrollbar-width: none;
  }

  .tab-nav::-webkit-scrollbar {
    display: none;
  }

  .tab-btn {
    flex: 0 0 auto;
    scroll-snap-align: start;
    min-height: 2.5rem;
    padding: 0.45rem 0.7rem;
  }

  .tab-btn .tab-label {
    max-width: 7.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Triage & answer controls */
  .tb-triage-card {
    padding: 0.9rem 1rem;
  }

  .tb-pill span,
  .tb-answer-pill span {
    min-height: 2.5rem;
    padding: 0.55rem 1rem;
    display: inline-flex;
    align-items: center;
  }

  .tb-question-card {
    padding: 1rem 0.9rem;
  }

  /* Data tables — swipe to scroll wide tables */
  .card:has(> .tb-form-section > table.data),
  .card:has(> table.data),
  .tb-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-left: -0.15rem;
    margin-right: -0.15rem;
    padding-left: 0.15rem;
    padding-right: 0.15rem;
  }

  .card table.data:not(.tb-staff-table) {
    min-width: 36rem;
  }

  .card table.data:not(.tb-staff-table) th,
  .card table.data:not(.tb-staff-table) td {
    white-space: nowrap;
  }

  .card table.data:not(.tb-staff-table) td:last-child {
    white-space: normal;
  }

  /* Staff accounts — card rows on small screens */
  .tb-staff-table-card .tb-table-wrap {
    overflow: visible;
    margin-left: 0;
    margin-right: 0;
    padding: 0;
  }

  .tb-staff-table {
    min-width: 0;
    border: none;
  }

  .tb-staff-table thead {
    display: none;
  }

  .tb-staff-table tbody tr {
    display: block;
    background: #fff;
    border: 1px solid var(--tb-border);
    border-radius: 12px;
    padding: 0.85rem 0.9rem;
    margin-bottom: 0.75rem;
    box-shadow: 0 2px 8px rgba(20, 71, 143, 0.05);
  }

  .tb-staff-table tbody tr:last-child {
    margin-bottom: 0;
  }

  .tb-staff-table td {
    display: block;
    padding: 0.35rem 0;
    border: none;
    text-align: left;
  }

  .tb-staff-table td::before {
    display: block;
    font-size: 0.68rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--tb-muted);
    margin-bottom: 0.15rem;
  }

  .tb-staff-table td:nth-child(1)::before { content: "Username"; }
  .tb-staff-table td:nth-child(2)::before { content: "Display name"; }
  .tb-staff-table td:nth-child(3)::before { content: "Email"; }
  .tb-staff-table td:nth-child(4)::before { content: "Role"; }
  .tb-staff-table td:nth-child(5)::before { content: "Status"; }
  .tb-staff-table td:nth-child(6)::before { content: "Actions"; }

  .tb-staff-table .tb-cell-email {
    max-width: none;
    word-break: break-word;
  }

  .tb-staff-table .tb-user-actions {
    text-align: left;
    padding-top: 0.5rem;
    margin-top: 0.35rem;
    border-top: 1px dashed var(--tb-border);
  }

  .tb-staff-table .tb-action-bar {
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .tb-staff-table .tb-action-btn {
    min-height: 2.5rem;
  }

  .tb-staff-table .tb-action-menu-panel {
    left: 0;
    right: auto;
  }

  /* Reports & charts */
  .report-hero {
    padding: 1.35rem 1.15rem;
    border-radius: 12px;
  }

  .report-hero .score-big {
    font-size: 2.75rem;
  }

  .metrics {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.65rem;
  }

  .metric .val {
    font-size: 1.45rem;
  }

  .charts {
    grid-template-columns: 1fr;
  }

  .meta-grid {
    grid-template-columns: 1fr 1fr;
  }

  .answer-legend .legend-grid {
    grid-template-columns: 1fr;
  }

  /* Auth screens */
  .tb-auth-layout {
    min-height: auto;
  }

  .tb-auth-brand {
    padding: 1.5rem 1.25rem;
  }

  .tb-auth-form {
    padding: 1.5rem 1.25rem;
  }

  /* Disable hover lift on touch — avoids sticky pressed state */
  .btn-primary:hover,
  .tb-action-btn--primary:hover {
    transform: none;
  }
}

/* ── ≤480px — small phones ── */
@media (max-width: 480px) {
  .tb-page-header {
    align-items: flex-start;
  }

  .metrics {
    grid-template-columns: 1fr;
  }

  .meta-grid {
    grid-template-columns: 1fr;
  }

  .tb-stepper {
    gap: 0.35rem;
  }

  .tb-step-line {
    min-width: 12px;
    max-width: 20px;
  }

  .home-hero-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .home-hero-actions .btn {
    width: 100%;
    text-align: center;
    min-height: 2.75rem;
  }
}
