/* Tablet and phone layout. Loaded last (file name) so it wins over the others.
   generate_pdf.js leaves this file out of the print copy. */

#drawer-btn, #drawer-shade { display: none; }

/* ---- Touch screens: 16px stops iOS zooming into every field ---- */
@media (pointer: coarse) {
  html body input.form-control, html body .form-select, html body .cf-control input, html body .cf-control select, html body .dash-input-element {
    font-size: 16px !important; height: 40px !important; min-height: 40px !important; }
  html body textarea.form-control, html body .cf-control textarea { font-size: 16px !important; height: auto; min-height: 84px !important; }
  html body .input-group-text { height: 40px !important; min-height: 40px !important; }
  html body .btn, html body .btn.btn-lg { min-height: 42px; }
  html body .dmc-api-demo-root { min-height: 42px; }
  html body .dmc-api-demo-root .mantine-Checkbox-label { padding-top: 13px; padding-bottom: 13px; }
  html body .form-check-input { width: 22px !important; height: 22px !important; }
  html body .form-check-label { padding: 4px 8px; }
  html body .dropdown-item { padding: 12px 14px !important; }
  html body .dash-spreadsheet td, html body .dash-spreadsheet td input { min-height: 40px; }
  html body .iv-row .form-control, html body .iv-row .form-select, html body .dash-spreadsheet td input { font-size: 16px !important; }
  html body #custom_time_input { font-size: 20px !important; }
  html body .input-group { flex-wrap: nowrap; }
}

/* ---- Tablet portrait and phone: side panel becomes a drawer ---- */
@media (max-width: 1023px) {
  html, body { overflow-x: hidden; }
  #page-content, #navbar { margin-left: 0 !important; margin-right: 0 !important; padding: 8px !important; }
  #page-content { padding-bottom: 96px !important; }
  #sidebar { top: 0 !important; left: -22rem !important; width: min(85vw, 20rem) !important; height: 100% !important;
    padding: 12px 12px 96px !important; overflow-y: auto !important; z-index: 10020 !important; transition: left .2s !important; }
  body.drawer-open #sidebar { left: 0 !important; box-shadow: 0 0 40px rgba(0,0,0,.35); }
  body.drawer-open #drawer-shade { display: block; position: fixed; inset: 0; background: rgba(15,25,35,.45); z-index: 10010; }
  #sidebar .btn, #sidebar input:not([type=checkbox]):not([type=radio]) { max-width: 100%; }
  body.has-drawer #drawer-btn { display: block; position: fixed; left: 10px; bottom: calc(14px + env(safe-area-inset-bottom)); z-index: 10000;
    height: 48px; padding: 0 14px; border: 0; border-radius: 24px; background: #1a6fbf; color: #fff; font-size: 15px; font-weight: 600;
    box-shadow: 0 3px 12px rgba(0,0,0,.28); }
  #btn_sidebar { display: none !important; }

  #stepper-nav { bottom: calc(14px + env(safe-area-inset-bottom)) !important; padding: 4px 10px !important; gap: 8px !important; white-space: nowrap; }
  #ai-chat-fab { right: 10px !important; bottom: calc(14px + env(safe-area-inset-bottom)) !important; width: 48px !important; height: 48px !important; }

  .layout-2-page .card { padding: 4px !important; }
  .layout-2-page .card-body { padding: 6px !important; }
  [class*="offset-"] { margin-left: 0 !important; }
  .layout-2-page .row > .col-10, .layout-2-page .row > .col-11, .col-11 { flex: 0 0 100%; max-width: 100%; width: 100%; }

  .cf-chip-table, .cf-chip-table * { max-height: none; }
  .cf-row > [class*="col"] { height: auto !important; min-width: 0; }
  .cf-row { height: auto !important; padding: 4px; }

  .iv-row, .iv-one-line { flex-wrap: wrap !important; }
  .iv-row > div { flex: 1 1 22% !important; max-width: none !important; min-width: 120px !important; }
  .layout3-checklist { flex-wrap: wrap !important; }
  .layout-2-page .btn[style*="20%"] { width: 48% !important; margin: 3px 1% 3px 0; }
  img, canvas, video { max-width: 100%; }
  .modal-dialog { margin-left: auto; margin-right: auto; max-width: min(640px, calc(100vw - 24px)); }
  .dash-spreadsheet-container { -webkit-overflow-scrolling: touch; }
}

/* ---- Phone ---- */
@media (max-width: 767px) {
  #page-content .row { --gutter: 0; }
  #page-content .row > [class*="col"], #page-content .row > .col { flex: 0 0 100% !important; max-width: 100% !important; width: 100% !important; }
  #page-content .row { margin-left: 0 !important; margin-right: 0 !important; height: auto !important; }
  #page-content .row > [class*="col"] { height: auto !important; min-height: 0 !important; padding-left: 4px; padding-right: 4px; }
  #sedation_collapse .row > [class*="col"], #page-content .compact-form .row > .col-2 { flex: 1 1 50% !important; max-width: none !important; width: 50% !important; }
  .ros-row { display: grid !important; grid-template-columns: 1fr 1fr; }
  .ros-col { min-width: 0 !important; }
  .layout-2-page .btn[style*="20%"] { width: 100% !important; margin: 3px 0; }
  .iv-row > div { flex: 1 1 45% !important; }
  .segmented-tight .dmc-api-demo-root, .segmented-wrap .dmc-api-demo-root { min-width: 30%; }
  #stepper-label { display: none; }
  #page-content .input-group { flex-wrap: wrap; width: 100% !important; }
  #page-content .input-group .form-control { flex: 1 1 90px; min-width: 70px; }
  .navbar .row { display: flex; flex-direction: column; align-items: stretch; gap: 6px; }
  .navbar .row > * { max-width: 100%; }
  #patient-save-status { top: auto !important; bottom: 84px; }
}

@media (max-width: 1023px) {
  .cf-chip-table .cf-row, .cf-row { flex-wrap: wrap !important; }
  .cf-chip-table, .cf-chip-table > *, .cf-chip-table .seg-scrollwrap { height: auto !important; }
  .seg-scrollwrap > *, .seg-scrollwrap .mantine-Group-root, .seg-scrollwrap [class*="mantine-CheckboxGroup"] {
    display: flex !important; flex-wrap: wrap !important; width: 100% !important; gap: 6px !important; white-space: normal !important; }
  .cf-row > [class*="col"], .cf-row .cf-label { overflow: visible !important; }
  #auth-status { top: 12px !important; right: 12px !important; bottom: auto !important; left: auto !important; max-width: 60vw; z-index: 900 !important; }
}
@media (max-width: 767px) {
  body.has-drawer #drawer-btn { bottom: calc(76px + env(safe-area-inset-bottom)); }
  .cf-chip-table .cf-row > [class*="col"], .cf-row > [class*="col"] { flex: 0 0 100% !important; max-width: 100% !important; width: 100% !important; }
  /* Vitals need width to be read: the time graphs scroll sideways together. */
  #live-graph, #gas-flow-graph, #temp-ecg-graph { overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; }
  #live-graph .js-plotly-plot, #gas-flow-graph .js-plotly-plot, #temp-ecg-graph .js-plotly-plot { min-width: 820px; }
  #live-graph .draglayer, #gas-flow-graph .draglayer, #temp-ecg-graph .draglayer { pointer-events: none; }
  #live-graph .nsewdrag, #gas-flow-graph .nsewdrag, #temp-ecg-graph .nsewdrag, .draglayer * { pointer-events: none !important; }
}

/* Landscape tablets and small laptops too: no chip may be pushed out of sight. */
@media (max-width: 1400px) {
  /* Chip rows: every option visible, no sideways scrolling */
  .segmented-tight, .segmented-wrap { flex-wrap: wrap !important; white-space: normal !important; }
  .segmented-tight .dmc-api-demo-root, .segmented-wrap .dmc-api-demo-root { flex: 1 1 auto !important; min-width: max-content; }
  .seg-scrollwrap, .cf-chip-table .seg-scrollwrap { overflow: visible !important; white-space: normal !important; }
  .cf-chip-table .cf-row, .cf-row { flex-wrap: wrap !important; }
  .cf-chip-table, .cf-chip-table > *, .cf-chip-table .seg-scrollwrap { height: auto !important; }
  .seg-scrollwrap > *, .seg-scrollwrap .mantine-Group-root { display: flex !important; flex-wrap: wrap !important; width: 100% !important; gap: 6px !important; white-space: normal !important; }
  #page-content .row { margin-left: 0 !important; margin-right: 0 !important; }
  .layout3-checklist { flex-wrap: wrap !important; }
}
