/* ==========================================================================
   CareGrid on phones and small tablets (doctor and reception panels first).
   Loaded by base.html after each page's own styles, so these win on small
   screens only. Desktop and print are untouched.
   ========================================================================== */
@media screen and (max-width: 768px) {

  html, body { overflow-x: clip; }
  body { -webkit-text-size-adjust: 100%; }

  /* ---------- page shell ---------- */
  .panel, body > .container, .container { padding-left: 10px !important; padding-right: 10px !important; }
  .panel { padding-top: 8px !important; }
  .dashboard-grid, .container { grid-template-columns: minmax(0, 1fr) !important; gap: 12px !important; }
  .card { padding: 14px 12px !important; border-radius: 14px !important; min-width: 0; }
  .card-title { font-size: 1rem !important; flex-wrap: wrap; gap: 8px; }

  /* ---------- header: title + menu on top, buttons in one swipeable row ---------- */
  .panel-header, .header {
    position: relative;
    flex-wrap: wrap !important;
    gap: 8px !important;
    padding: 12px 12px 10px !important;
    margin-bottom: 12px !important;
    border-radius: 14px !important;
  }
  .panel-header h2, .header h1 {
    flex: 1 1 100%;
    font-size: 1.08rem !important;
    line-height: 1.25 !important;
    letter-spacing: 0 !important;
    padding-right: 48px;
    margin: 0 !important;
  }
  .header-actions {
    width: 100%;
    flex-wrap: wrap !important;
    gap: 6px !important;
  }
  .header-actions > * { flex: 0 0 auto; }
  .header-actions .nav-controls { margin-right: 0 !important; gap: 6px !important; }
  .header-actions a.back-btn[href^="javascript:history.back"] { display: none !important; }  /* same as the arrow */
  .header-actions .hamburger {
    position: absolute;
    top: 12px; right: 12px;
    width: 36px !important; height: 34px !important;
    padding: 8px 7px;
    border-radius: 9px;
    background: rgba(255, 255, 255, .14);
    justify-content: space-between;
    box-sizing: border-box;
  }
  .header-actions .hamburger span { height: 2.5px !important; width: 100% !important; margin: 0 !important; }
  .nav-btn { width: 36px !important; height: 34px !important; font-size: .95rem !important; }
  .back-btn { padding: 8px 12px !important; font-size: .82rem !important; white-space: nowrap; border-radius: 9px !important; }

  /* ---------- side menus ---------- */
  .side-menu, .doctor-sidebar { width: min(300px, 86vw) !important; max-width: 86vw; box-sizing: border-box; overflow-y: auto; }
  .side-menu:not(.active), .doctor-sidebar:not(.active) { right: -110vw !important; box-shadow: none !important; }
  .side-menu a, .doctor-sidebar a { padding-top: 12px !important; padding-bottom: 12px !important; gap: 12px; }
  .side-menu a i, .doctor-sidebar a i { width: 20px; text-align: center; }

  /* ---------- number cards: three small tiles in a row ---------- */
  .stats-grid, .stats-row, .summary-grid, .stat-cards { grid-template-columns: repeat(3, minmax(0, 1fr)) !important; gap: 8px !important; }
  .stat-card { padding: 12px 8px !important; text-align: center; min-width: 0; }
  .stat-card .stat-icon { width: 34px !important; height: 34px !important; font-size: 1rem !important; margin: 0 auto 6px !important; border-radius: 10px !important; padding: 0 !important; display: flex !important; align-items: center; justify-content: center; }
  .stat-card .stat-value { font-size: 1.35rem !important; line-height: 1.1; }
  .stat-card .stat-label { font-size: .72rem !important; line-height: 1.2; }
  .search-result-count { min-width: 0 !important; }

  /* ---------- forms: no zoom on focus (iOS needs 16px), easy to tap ---------- */
  input[type="text"], input[type="number"], input[type="tel"], input[type="search"], input[type="email"],
  input[type="password"], input[type="date"], input:not([type]), select, textarea {
    font-size: 16px !important;
    min-height: 40px;
    max-width: 100%;
  }
  textarea { min-height: 72px; }
  .form-grid, .form-row { grid-template-columns: minmax(0, 1fr) !important; }

  /* ---------- buttons ---------- */
  .action-buttons { flex-direction: column !important; gap: 8px !important; }
  .action-buttons > *, .action-btn { width: 100% !important; justify-content: center; min-height: 44px; }

  /* ---------- wide tables scroll inside their card ---------- */
  table:not(#prescriptionTable):not(.no-mobile-scroll) {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  table:not(#prescriptionTable) th, table:not(#prescriptionTable) td { white-space: nowrap; }

  /* ---------- popups fit the screen ---------- */
  .universal-modal, .modal-content, .modal-box, .modal-dialog {
    max-width: calc(100vw - 20px) !important;
  }
  .universal-modal-footer { flex-wrap: wrap; }
  .universal-modal-footer button { flex: 1 1 auto; min-height: 42px; }

  /* =====================================================================
     Doctor: prescription screen
     ===================================================================== */
  .template-selector { flex-direction: row !important; flex-wrap: wrap !important; }
  .template-search-wrapper { flex: 1 1 100% !important; }
  .template-selector .btn-template { flex: 1 1 0 !important; width: auto !important; justify-content: center; min-height: 42px; }
  .rx-doctor-switch { width: 100%; }
  .rx-doctor-switch select { flex: 1; min-width: 0 !important; }

  /* each medicine becomes a small card */
  .prescription-table-wrapper { margin: 0 0 10px !important; padding: 0 !important; overflow: visible !important; }
  #prescriptionTable { display: block; min-width: 0 !important; width: 100%; }
  #prescriptionTable thead { display: none; }
  #prescriptionTable tbody { display: block; }
  #prescriptionTable tr.prescription-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 8px 8px;
    padding: 10px;
    margin-bottom: 8px;
    border-radius: 12px;
    background: rgba(0, 0, 0, .14);
  }
  #prescriptionTable tr.prescription-row > td {
    display: block;
    width: auto !important;
    padding: 0 !important;
    border: 0 !important;
    min-width: 0;
  }
  #prescriptionTable td.medicine-input-cell, #prescriptionTable td.days-cell { grid-column: 1 / -1; }
  #prescriptionTable td.qty-cell::before, #prescriptionTable td.dosage-form-cell::before,
  #prescriptionTable td.frequency-cell::before, #prescriptionTable td.urdu-cell::before,
  #prescriptionTable td.days-cell::before {
    display: block;
    font-size: .68rem;
    font-weight: 700;
    letter-spacing: .05em;
    text-transform: uppercase;
    opacity: .75;
    margin-bottom: 3px;
  }
  #prescriptionTable td.qty-cell::before { content: "Qty"; }
  #prescriptionTable td.dosage-form-cell::before { content: "Form"; }
  #prescriptionTable td.frequency-cell::before { content: "Frequency"; }
  #prescriptionTable td.urdu-cell::before { content: "Urdu"; }
  #prescriptionTable td.days-cell::before { content: "Duration"; }
  #prescriptionTable input, #prescriptionTable select { width: 100% !important; box-sizing: border-box; }
  #prescriptionTable .urdu-description { min-height: 40px; display: flex; align-items: center; justify-content: flex-end; }
  #prescriptionTable .duration-wrapper { display: flex !important; gap: 6px; flex-wrap: nowrap; }
  #prescriptionTable .duration-wrapper select { flex: 1 1 0; width: auto !important; }
  #prescriptionTable .note-toggle-btn { flex: 0 0 auto; min-height: 40px; padding: 0 12px; }
  #prescriptionTable tr.medicine-note-row { display: block; margin: -4px 0 8px; }
  #prescriptionTable tr.medicine-note-row td { display: block; padding: 0 !important; border: 0 !important; }
  #prescriptionTable .autocomplete-results { left: 0 !important; right: 0 !important; width: auto !important; min-width: 0 !important; max-width: 100% !important; }

  .patient-info-card { min-width: 0 !important; width: auto !important; order: -1; }   /* find the patient first */
  .dashboard-grid { display: flex !important; flex-direction: column; }
  .dashboard-grid > * { min-width: 0; }
  .clinical-notes-grid, .vitals-grid { grid-template-columns: minmax(0, 1fr) !important; }

  /* queue / print queue tabs on the screen edge: small round buttons at the bottom */
  .queue-toggle-btn {
    top: auto !important; bottom: 14px; left: 12px !important;
    transform: none !important;
    writing-mode: horizontal-tb !important;
    border-radius: 999px !important;
    padding: 10px 16px !important;
    font-size: .85rem !important;
    gap: 8px;
  }
  .queue-panel { width: min(360px, 92vw) !important; max-width: 92vw; }
}

/* very small phones */
@media screen and (max-width: 380px) {
  .header-actions .nav-controls .nav-btn:nth-child(2) { display: none; }   /* forward */
}

@media screen and (max-width: 768px) {
  .patient-search-bar { flex-wrap: wrap; }
  .patient-search-bar .search-input-wrapper { flex: 1 1 100%; }
  .filter-buttons, .period-buttons { display: grid !important; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px !important; }
  .filter-buttons > *, .period-buttons > * { width: 100%; justify-content: center; margin: 0 !important; }
  .date-input-group, .custom-range, .date-range { flex-direction: column; align-items: stretch !important; width: 100%; }
}

@media screen and (max-width: 768px) {
  .patient-search-bar .search-input-wrapper { width: 100% !important; max-width: none !important; }
  .search-result-count:empty { display: none; }
  .filter-buttons > *, .period-buttons > * { white-space: nowrap; font-size: .82rem !important; padding-left: 6px !important; padding-right: 6px !important; }

  /* reception form */
  .age-inputs { display: grid !important; grid-template-columns: repeat(3, minmax(0, 1fr) auto); gap: 6px !important; align-items: center; }
  .age-inputs input { width: 100% !important; min-width: 0; }
  .age-inputs .age-label { font-size: .78rem; }
  #id_doctor, select.form-control { width: 100% !important; }

  /* print queue tab: small round button at the bottom left, like the doctor's queue */
  body .pq-toggle {
    top: auto !important; bottom: 14px !important; left: 12px !important;
    transform: none !important;
    writing-mode: horizontal-tb !important;
    flex-direction: row !important;
    border-radius: 999px !important;
    padding: 10px 16px !important;
    gap: 8px !important;
    height: auto !important; width: auto !important;
  }
  body .pq-toggle span { writing-mode: horizontal-tb !important; transform: none !important; }
  body .pq-panel { width: min(380px, 94vw) !important; max-width: 94vw; }
  body { padding-bottom: 64px; }   /* room for the round buttons */
}

@media screen and (max-width: 768px) {
  body .pq-panel:not(.open), body .queue-panel:not(.active):not(.open) { box-shadow: none !important; }
  .lt-head h2, .lt-title { white-space: nowrap; }
}
/* touch screens: keyboard shortcut hints mean nothing */
@media (hover: none) and (pointer: coarse) {
  [class$="-keys"], [class*="-keys "] { display: none !important; }
}
