/* responsive.css — mobile & tablet layer for every page.
   Linked LAST in <head> so it wins over each page's inline <style> on small screens.
   Desktop (> 900px) is untouched. Print output is untouched (all rules are screen-only). */

@media screen {

  /* Touch screens: no "lift on hover" jumps (a tap leaves :hover stuck on mobile) */
  @media (hover: none) {
    .card:hover,
    .table tbody tr:hover,
    button:hover, .btn:hover,
    .stat-card:hover,
    .brand img:hover,
    .role-badge:hover,
    .badge:hover {
      transform: none !important;
    }
    button:hover::before, .btn:hover::before { width: 0; height: 0; }
  }

  /* Never let wide media push the page sideways */
  img, video, canvas, svg { max-width: 100%; }

  /* ---------- Landscape phones & tablets ---------- */
  @media (max-width: 1100px) {
    /* Header items wrap onto a second line instead of running off-screen */
    .header {
      flex-wrap: wrap;
      row-gap: 10px;
    }

    .header > div:last-child { margin-left: auto; }

    /* Search boxes with a fixed desktop minimum width can shrink */
    .table-header input,
    .controls input {
      min-width: 0 !important;
    }

    .table-header .controls {
      flex-wrap: wrap;
      max-width: 100%;
    }

    .controls input { flex: 1 1 180px; }
  }

  /* ---------- Tablet & below: sidebar becomes a swipeable menu bar ---------- */
  @media (max-width: 900px) {
    .content-wrapper {
      flex-direction: column !important;
      gap: 12px !important;
    }

    .sidebar {
      position: static !important;
      flex: none !important;
      width: 100% !important;
      max-width: 100% !important;
    }

    .sidebar > .card {
      padding: 6px !important;
      margin-bottom: 0 !important;
      border-radius: 14px;
    }

    ul.sidebar-nav {
      display: flex !important;
      flex-wrap: nowrap;
      gap: 4px;
      overflow-x: auto;
      -webkit-overflow-scrolling: touch;
      scrollbar-width: none;
      scroll-snap-type: x proximity;
    }

    ul.sidebar-nav::-webkit-scrollbar { display: none; }

    ul.sidebar-nav li {
      flex: 0 0 auto;
      scroll-snap-align: start;
      margin: 0 !important;
    }

    ul.sidebar-nav li a {
      padding: 9px 14px !important;
      font-size: 14px !important;
      white-space: nowrap;
      gap: 8px !important;
      border-radius: 10px !important;
      min-height: 40px;
    }

    ul.sidebar-nav li a .icon { font-size: 16px !important; }

    .main-content {
      width: 100%;
      min-width: 0;
    }

    /* Table toolbars (title + date/search/buttons) wrap instead of overflowing */
    .table-header {
      flex-wrap: wrap;
      gap: 10px;
    }

    .table-header .controls,
    .controls,
    .pagination-controls,
    .filters, .filter-bar, .toolbar {
      flex-wrap: wrap;
      max-width: 100%;
    }

    .table-header .controls { width: 100%; }

    .table-header input,
    .controls input,
    .controls select {
      min-width: 0 !important;
      flex: 1 1 160px;
    }

    .controls .btn,
    .controls button {
      width: auto !important;
      flex: 0 0 auto;
    }
  }

  /* ---------- Phones & small tablets ---------- */
  @media (max-width: 768px) {
    .container {
      padding: 0 12px !important;
      margin: 12px auto !important;
    }

    /* Compact header: logo + name on one line, role + logout on the next */
    .header {
      flex-direction: row !important;
      flex-wrap: wrap;
      align-items: center !important;
      gap: 10px !important;
      padding: 10px 12px !important;
      margin-bottom: 12px !important;
      border-radius: 16px !important;
    }

    .header .brand {
      flex: 1 1 auto;
      width: auto !important;
      min-width: 0;
      justify-content: flex-start !important;
      font-size: 15px !important;
      white-space: normal;
      line-height: 1.25;
      gap: 10px;
    }

    .header .brand img {
      width: 44px !important;
      height: 44px !important;
      flex-shrink: 0;
    }

    .header .brand img.brand-logo-wide {
      width: 88px !important;
    }

    .header > div:last-child {
      flex-direction: row !important;
      flex: 1 1 100%;
      width: 100% !important;
      align-items: center !important;
      gap: 8px !important;
      white-space: normal;
    }

    .header .role-badge {
      flex: 1 1 auto;
      min-width: 0;
      width: auto !important;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      text-align: left;
      display: block;
    }

    .header > div:last-child .btn,
    .header > div:last-child button {
      width: auto !important;
      flex: 0 0 auto;
      padding: 8px 14px !important;
      font-size: 13px !important;
    }

    /* Receptionist clinic switcher sits on its own row */
    .header .clinic-header-container {
      order: 1;
      flex: 1 1 100%;
      margin: 0 !important;
    }

    .header > div:last-child { order: 2; }

    .card {
      padding: 14px !important;
      margin-bottom: 12px !important;
      border-radius: 14px;
    }

    .card h2 { font-size: 17px !important; }

    /* Big page titles scale down on phones */
    .container h1 {
      font-size: clamp(20px, 6vw, 26px) !important;
      line-height: 1.25 !important;
    }

    /* Header-row buttons (e.g. "+ Add Staff") keep their natural width */
    .card > .flex > .btn,
    .card > .flex > button,
    .card > div[style*="space-between"] > .btn,
    .card > div[style*="space-between"] > button {
      width: auto !important;
      flex: 0 0 auto;
    }

    /* Title + search/filters rows inside cards wrap instead of squeezing */
    .card > .flex,
    .card > div[style*="space-between"] {
      flex-wrap: wrap;
      gap: 10px;
    }

    .card > .flex > input,
    .card > .flex > select,
    .card > div[style*="space-between"] > input,
    .card > div[style*="space-between"] > select {
      width: 100% !important;
      max-width: 100% !important;
      flex: 1 1 100%;
    }

    /* Inline fixed widths on form controls should never exceed the screen */
    input, select, textarea { max-width: 100%; }

    /* Generic modal boxes fit the screen and scroll internally */
    .modal-box, .modal-content, .modal-card, .modal {
      max-width: calc(100vw - 24px) !important;
      max-height: calc(100vh - 24px);
      max-height: calc(100dvh - 24px);
      overflow-y: auto;
    }

    /* Modal close (✕) buttons stay compact instead of full-width bars */
    .modal-header > button,
    .modal-header > .btn,
    button.btn.danger[onclick^="closeModal"],
    .close-btn,
    .modal-close {
      width: 40px !important;
      height: 40px !important;
      min-width: 40px;
      padding: 0 !important;
      flex: 0 0 auto !important;
      border-radius: 10px !important;
    }

    .modal-header {
      gap: 12px;
      align-items: center;
    }

    .modal-header h3,
    .modal-header h2 {
      flex: 1 1 auto;
      min-width: 0;
    }

    /* Form fields get a visible edge (the theme's --line border is white on white) */
    input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
    select,
    textarea {
      border-color: #dbe3ec;
    }

    /* Long medicine details wrap rather than being cut off */
    .med-list .med-details {
      white-space: normal !important;
      overflow: visible !important;
    }

    /* Touch-friendly minimum size for controls */
    button, .btn, input, select { min-height: 40px; }
    input[type="checkbox"], input[type="radio"] { min-height: 0; }
  }

  /* ---------- Phones: tables become stacked cards ---------- */
  @media (max-width: 640px) {
    .table-responsive {
      overflow-x: visible !important;
      box-shadow: none !important;
    }

    table.table:not(.no-stack) {
      display: block;
      width: 100% !important;
      min-width: 0 !important;
      box-shadow: none !important;
      overflow: visible !important;
      border-radius: 0;
      background: transparent;
    }

    table.table:not(.no-stack) thead { display: none !important; }

    table.table:not(.no-stack) tbody {
      display: block;
      width: 100%;
    }

    table.table:not(.no-stack) tr {
      display: block;
      width: 100%;
      background: #fff;
      border: 1px solid #e2e8f0;
      border-radius: 12px;
      padding: 4px 12px;
      margin-bottom: 10px;
      box-shadow: 0 1px 3px rgba(15, 23, 42, 0.05);
    }

    /* Each cell: column name pinned on the left, content flowing right-aligned */
    table.table:not(.no-stack) td {
      display: block;
      position: relative;
      width: 100% !important;
      min-height: 36px;
      padding: 8px 0 8px 38% !important;
      border: none !important;
      border-bottom: 1px dashed #edf2f7 !important;
      text-align: right !important;
      white-space: normal !important;
      overflow-wrap: anywhere;
      font-size: 14px;
      line-height: 1.45;
    }

    table.table:not(.no-stack) td:last-child { border-bottom: none !important; }

    table.table:not(.no-stack) td[data-label]::before {
      content: attr(data-label);
      position: absolute;
      left: 0;
      top: 10px;
      width: 36%;
      text-align: left;
      font-size: 11px;
      font-weight: 700;
      letter-spacing: 0.4px;
      line-height: 1.3;
      text-transform: uppercase;
      color: #64748b;
    }

    /* Cells without a column name (and spanning rows) use the full width */
    table.table:not(.no-stack) td:not([data-label]),
    table.table:not(.no-stack) td[data-label=""],
    table.table:not(.no-stack) td[colspan] {
      padding-left: 0 !important;
    }

    table.table:not(.no-stack) td[data-label=""]::before,
    table.table:not(.no-stack) td[colspan]::before { content: none !important; }

    /* Empty / loading / "no records" rows that span all columns */
    table.table:not(.no-stack) td[colspan] { text-align: center !important; }

    table.table:not(.no-stack) tr:has(> td[colspan]) {
      border-style: dashed;
      box-shadow: none;
    }

    table.table:not(.no-stack) td > * { max-width: 100%; }
    table.table:not(.no-stack) td img { max-width: 100%; height: auto; }

    /* Action cells: buttons side by side, right-aligned, not stretched */
    table.table:not(.no-stack) td .btn,
    table.table:not(.no-stack) td button {
      width: auto !important;
      min-height: 38px;
      padding: 8px 14px !important;
      font-size: 13px !important;
      margin: 2px 0 2px 6px;
      vertical-align: middle;
    }

    /* Toolbars: search box gets its own full-width row */
    .controls .search-bar,
    .controls input[type="search"],
    .controls input[type="text"] {
      flex: 1 1 100% !important;
    }

    /* Pharmacist checklist rows: status dropdown moves under the medicine text */
    .med-list li {
      grid-template-columns: auto minmax(0, 1fr) !important;
      row-gap: 8px !important;
    }

    .med-list li > select,
    .med-list li > div:last-child:not(:only-child) {
      grid-column: 2;
    }

    /* Summary stat tiles: two per row instead of one tall column */
    .stats-grid,
    .summary-grid,
    .stat-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
      gap: 10px !important;
    }

    .stat-card {
      min-height: 0 !important;
      padding: 14px !important;
      border-radius: 16px !important;
    }

    .stat-value { font-size: 20px !important; overflow-wrap: anywhere; }
    .stat-label { font-size: 12px !important; }
  }

  /* ================= PRINT DOCUMENTS (prescription / invoice / receipt) =================
     On screens narrower than an A4 sheet the document reflows to fit, so it can be read
     without sideways scrolling. Printing is unaffected: these rules are screen-only and
     each page's own A4 layout applies on paper. */
  @media (max-width: 820px) {
    body:has(> .page) { background: #f1f5f9 !important; }

    .page {
      width: auto !important;
      max-width: 100% !important;
      min-height: 0 !important;
      margin: 8px !important;
      padding: 16px 14px !important;
      border-radius: 10px;
    }

    .page header {
      flex-wrap: wrap;
      gap: 10px;
      align-items: center !important;
    }

    .page header .logo {
      width: 96px !important;
      height: auto;
    }

    .page header .clinic-info { min-width: 0; }
    .page header .clinic-info h1 { font-size: 17px !important; }
    .page h2.main-title { font-size: 16px !important; }

    /* Patient / doctor / date rows wrap */
    .page .patient-info-row {
      flex-wrap: wrap;
      gap: 2px 16px;
    }

    .page .patient-info-row > div { min-width: 0; }

    /* Two-column prescription body stacks: medicines first, then tests */
    .page .rx-columns { flex-direction: column; }

    .page .rx-column-left,
    .page .rx-column-right {
      width: 100% !important;
      padding: 0 !important;
      border-left: none !important;
    }

    .page .rx-column-right {
      border-top: 1px solid #e2e8f0;
      padding-top: 10px !important;
      margin-top: 6px;
    }

    /* Procedure invoices */
    .page .info-grid { grid-template-columns: 1fr !important; }
    .page .info-row { flex-wrap: wrap; gap: 4px; }
    .page .summary-table, .page .payment-section table { width: 100%; font-size: 12px; }
    .page .summary-table th, .page .summary-table td,
    .page .payment-section th, .page .payment-section td { padding: 6px 4px !important; }
    .page .balance-summary, .page .financials { flex-wrap: wrap; gap: 6px; }

    /* Footers: clinic addresses stack and centre */
    .page footer .clinic-info {
      flex-direction: column;
      gap: 8px;
    }

    .page footer .clinic-info > div { text-align: center !important; }

    /* ---- Dr. Imran (Sadiq) prescription template ---- */
    .page header.imr-header { justify-content: center !important; }
    .page .imr-doctor { flex: 1 1 100%; }

    .page .imr-logo {
      transform: scale(0.8);
      transform-origin: center;
      margin: -8px auto;
    }

    .page .imr-patient {
      flex-wrap: wrap;
      gap: 10px 14px;
    }

    .page .imr-patient > div:first-child { flex: 1 1 100% !important; }
    .page .imr-patient > div { flex: 1 1 40% !important; width: auto !important; }

    .page .imr-summary-top { flex-direction: column; }

    .page .imr-history,
    .page .imr-diagnosis {
      width: 100% !important;
      border-right: none !important;
    }

    .page .imr-history { border-bottom: 1px solid #cbd5e1; }

    .page .imr-vitals { flex-wrap: wrap; }

    .page .imr-vitals-title {
      flex: 1 1 100%;
      padding: 6px 12px !important;
      border-right: none !important;
      border-bottom: 1px solid #cbd5e1;
    }

    .page .imr-vital {
      flex: 1 1 33.33%;
      border-bottom: 1px solid #e2e8f0;
    }

    .page .imr-body { flex-direction: column; }

    .page .imr-meds,
    .page .imr-side {
      width: 100% !important;
      padding: 0 !important;
      border-left: none !important;
    }

    .page .imr-side {
      border-top: 1px solid #94a3b8;
      padding-top: 10px !important;
      margin-top: 10px;
    }

    .page .imr-investigations { min-height: 0 !important; }

    /* Print button stays reachable at the bottom of the screen */
    .print-button {
      position: sticky;
      bottom: 12px;
      z-index: 20;
      width: calc(100% - 32px) !important;
      max-width: 360px;
      min-height: 46px;
      margin: 12px auto !important;
      box-shadow: 0 6px 20px rgba(15, 23, 42, 0.25);
    }
  }

  /* Phones: English/Urdu medicine lines stack (each English line followed by its Urdu) */
  @media (max-width: 480px) {
    .page .med-body { grid-template-columns: minmax(0, 1fr) !important; }

    .page .med-body > .urdu-text {
      border-left: none !important;
      padding-left: 0 !important;
    }

    .page .med-body > .urdu-text:empty { display: none; }

    .page .imr-vital { flex: 1 1 50%; }
  }

  /* ---------- Small phones ---------- */
  @media (max-width: 400px) {
    .container { padding: 0 8px !important; }
    .card { padding: 12px !important; }
    .header .brand { font-size: 14px !important; }
  }
}
