/* Tablets and phones. Loaded last; desktop styles are unchanged above 1100px. Printing is unaffected. */

.menu-btn, .filters-toggle { display: none; }
.narrow-only { display: none; }

@media screen {
  /* ---------- Tablet (iPad portrait and smaller) ---------- */
  @media (max-width: 1100px) {
    /* Unscheduled Tx: carrier and insurance fold into the patient cell so the follow-up columns stay on screen */
    .unsched-table .col-carrier, .unsched-table .col-ins { display: none; }
    .unsched-table .narrow-only { display: block; }
    .unsched-table th, .unsched-table td { padding-left: 8px; padding-right: 8px; }
    .unsched-table td.card-title { min-width: 200px; }
  }

  /* ---------- Menu collapses behind ☰ ---------- */
  @media (max-width: 900px) {
    .topbar { position: relative; padding: 10px 14px; gap: 10px; }
    .topbar .brand { margin-right: auto; }
    .menu-btn {
      display: grid; place-items: center; order: 3; width: 42px; height: 38px; font-size: 20px; line-height: 1;
      background: rgba(255,255,255,.14); color: #fff; border: 1px solid rgba(255,255,255,.25); border-radius: 8px; cursor: pointer;
    }
    .user-chip { order: 2; }
    .topbar nav {
      display: none; position: absolute; top: 100%; left: 0; right: 0; z-index: 40; margin: 0;
      flex-direction: column; gap: 2px; padding: 8px; background: var(--accent); box-shadow: 0 10px 24px rgba(0,0,0,.2);
    }
    body.nav-open .topbar nav { display: flex; }
    .topbar nav a { padding: 12px 14px; font-size: 16px; }
    .topbar nav a[hidden] { display: none; }
  }

  /* ---------- Phone ---------- */
  @media (max-width: 640px) {
    main { padding: 16px 12px 48px; }
    h1.page { font-size: 20px; }
    .user-chip .role { display: none; }
    .user-chip { max-width: 45vw; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

    /* Tables that become one card per row. Cells with data-label show their column name. */
    table.cards, table.cards tbody { display: block; width: 100%; }
    table.cards thead tr:not(:first-child):not(.col-filters) { display: none; }
    table.cards thead tr:first-child { display: flex; flex-wrap: wrap; gap: 4px; padding: 8px 10px; background: #f9fafb; border-bottom: 1px solid var(--line-strong); }
    table.cards thead tr:first-child th { display: block; border: 0; padding: 0; background: none; }
    table.cards thead tr:first-child th:empty, table.cards thead th .sub { display: none; }
    table.cards thead tr:first-child th .sort { padding: 5px 9px; border: 1px solid var(--line-strong); border-radius: 999px; background: #fff; font-size: 12.5px; }
    table.cards:not(:has(.sort)) thead { display: none; }
    table.cards thead tr.col-filters { display: block; }
    table.cards thead tr.col-filters td { display: none; }
    table.cards thead tr.col-filters td:first-child { display: block; padding: 8px 10px; }
    table.cards tbody tr { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
    table.cards tbody td { display: block; padding: 0; border: 0; text-align: left; min-width: 0; }
    table.cards tbody td.card-title, table.cards tbody td.empty, table.cards tbody td[colspan] { grid-column: 1 / -1; }
    table.cards tbody td.card-action { grid-column: 1 / -1; text-align: right; }
    table.cards tbody td[data-label]::before {
      content: attr(data-label); display: block; margin-bottom: 2px;
      font-size: 11px; font-weight: 700; letter-spacing: .03em; text-transform: uppercase; color: var(--muted);
    }
    table.cards .chk { display: none; }
    table.cards .narrow-only { display: inline-block; }
    table.cards .dnf-note { max-width: none; white-space: normal; }
    .table-scroll:has(> table.cards) { overflow: visible; }
    table.cards td.card-title > .link, table.cards td.card-title > strong { font-weight: 700; font-size: 16px; }
    /* Saved plans: name first, then date / procedures / portion in one row; tapping the card opens it */
    .plan-table.cards tbody tr { cursor: pointer; grid-template-columns: repeat(3, 1fr); }
    .plan-table.cards td.card-title { order: -1; font-size: 16px; }
    .plan-table.cards td.card-action { display: none; }
    .plan-table.cards tbody td.r { text-align: left; }
    .access-users.cards tbody tr { padding-left: 4px; padding-right: 4px; }

    /* Filters fold away behind a button; toolbars stack */
    .filters-toggle { display: flex; align-items: center; gap: 8px; width: 100%; background: none; border: 0; padding: 4px 0; font-weight: 700; cursor: pointer; text-align: left; }
    .filters-toggle .sub { flex: 1; font-weight: 400; }
    .filter-panel:not(.open) .filter-grid { display: none; }
    .filter-panel.open .filters-toggle { margin-bottom: 12px; }
    .filter-panel { padding: 12px 14px; }
    .filter-grid { grid-template-columns: 1fr; }
    .task-toolbar, .task-filters { flex-direction: column; align-items: stretch; width: 100%; }
    .task-filters input, .task-filters select { width: 100%; }
    .seg { display: flex; overflow-x: auto; }
    .seg a { white-space: nowrap; flex: 1; text-align: center; padding: 8px 10px; }
    .list-summary .dnf-toggle { margin-left: 0; }
    .today-head { flex-wrap: wrap; gap: 6px; }
    .progress-row { flex-wrap: wrap; gap: 4px; }

    /* Windows (patient follow-up, task editor, data tables) fill the screen */
    .modal-backdrop { padding: 0; }
    .modal, .modal.wide, .modal.wide:has(#ad-table) { max-width: none; min-height: 100%; border-radius: 0; }
    .modal-body { padding: 10px; }
    .modal { min-width: 0; }
    .modal-body > *, .pat-top > div { min-width: 0; }
    .note-cell { overflow-wrap: anywhere; min-width: 160px; }
    .modal-head .icon-btn { flex: none; }
    .modal-head { padding: 14px; }
    .pat-top { gap: 10px; }
    .avatar { width: 44px; height: 44px; font-size: 20px; }
    .action-btns .btn { flex: 1 1 calc(50% - 8px); }
    .tabs { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .tab { white-space: nowrap; }
    .form-foot { flex-wrap: wrap; }
    .form-foot .btn { flex: 1; }
    .next-row input { max-width: none; }
    .card-head { flex-wrap: wrap; gap: 6px; }
    .modal-foot { flex-wrap: wrap; }
    .tk-grid { grid-template-columns: 1fr 1fr; }
    .popover { width: calc(100vw - 16px); max-width: 320px; }

    /* Settings */
    .settings-layout { gap: 12px; }
    .settings-nav { overflow-x: auto; flex-wrap: nowrap; }
    .settings-nav a { white-space: nowrap; }
    .access-matrix { font-size: 13px; }
    .access-matrix td, .access-matrix th { padding: 8px 6px; }
    .savebar { flex-wrap: wrap; }

    /* Analytics */
    .tiles { grid-template-columns: 1fr 1fr; gap: 8px; }
    .tile { padding: 10px 12px; }
    .tile-value { font-size: 21px; }
    .tile .sub { font-size: 11.5px; }
    .chart-legend, .chart-legend .stack-key { flex-wrap: wrap; }
    .analytics-page .filter-row label { flex: 1 1 140px; }
    .analytics-page .filter-row select, .analytics-page .filter-row input { width: 100%; }

    /* Treatment plan preview: the paper keeps its printed size and scrolls sideways */
    .paper-wrap { margin: 0 -12px; padding: 0 12px; }
  }

  /* ---------- Touch screens (iPad, phone) ---------- */
  @media (pointer: coarse) {
    /* 16px text keeps iOS Safari from zooming in when a field is tapped */
    input, select, textarea { font-size: 16px; }
    .btn.small { min-height: 36px; }
    .plan-dd { padding: 7px 8px; }
    .task-check input { width: 26px; height: 26px; }
    .check input[type=checkbox] { width: 20px; height: 20px; }
    .link { padding: 4px 0; }
    .tab { padding: 11px 14px; }
  }
}
