/* ============================================================
   Appointments Responsive Styles
   ──────────────────────────────────────────────────────────
   On screens ≤ 1024px (mobile & tablet): the appointments
   table transforms into card-based layouts. Each record
   becomes an independent card that lists all of its details
   as label → value rows.
   Desktop (>1024px): original table layout unchanged.
   ============================================================ */

/* ─── Base responsive layer ─────────────────────────────── */
.appointments-container { min-width: 0; overflow-x: clip; }
.appointments-container * { min-width: 0; }
.appointments-container img, .appointments-container svg { max-width: 100%; }
.appointments-container button, .appointments-container a { touch-action: manipulation; }
.appointments-container .page-header { gap: 16px; flex-wrap: wrap; }
.appointments-container .header-actions { flex-wrap: wrap; }
.appointments-container .header-actions > button,
.appointments-container .header-actions > .user-profile { min-height: 44px; }
.appointments-container .tabs-header { overflow-x: auto; scrollbar-width: thin; }
.appointments-container .tab-btn { min-height: 48px; white-space: nowrap; }
.appointments-container .filters-section { flex-wrap: wrap; }
.appointments-container .search-box { min-width: 220px; }
.appointments-container .date-filter,
.appointments-container .export-btn { min-height: 44px; }
.appointments-container .table-container { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.appointments-container .appointments-table { min-width: 980px; }
.appointments-container .action-btn { width: 44px; height: 44px; min-width: 44px; min-height: 44px; }
.appointments-container .modal-overlay { padding: 12px; overflow-y: auto; align-items: flex-start; }
.appointments-container .modal-content,
.appointments-container .ap-confirm-modal,
.appointments-container .ap-detail-modal { width: min(100%, 600px); max-height: calc(100dvh - 24px); margin-block: 0; }
.appointments-container .modal-body,
.appointments-container .ap-detail-body { overflow-wrap: anywhere; }
.appointments-container .ap-detail-grid { min-width: 0; }
.appointments-container .slot-grid { grid-template-columns: repeat(4, minmax(64px, 1fr)); }
.appointments-container .slot-btn { min-height: 56px; }
.appointments-container .form-input,
.appointments-container select,
.appointments-container textarea { min-height: 44px; max-width: 100%; }
.appointments-container .btn { min-height: 44px; }
.appointments-container .ap-confirm-actions,
.appointments-container .modal-footer { flex-wrap: wrap; }
.appointments-container .ap-confirm-actions > *,
.appointments-container .modal-footer > * { min-width: 120px; }
.appointments-container :focus-visible { outline: 3px solid var(--accent-soft, #dfe1ff); outline-offset: 3px; }

/* ═══════════════════════════════════════════════════════════
   CARD VIEW — MOBILE & TABLET (≤ 1024px)
   ──────────────────────────────────────────────────────────
   The data table transforms into cards. Each <tr> becomes an
   independent card whose cells render as label → value rows.
   Labels come from data-label attributes auto-assigned by
   mobile-table-labels.js from the corresponding <th> text.
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 1024px) {
  /* ─── Page-level tablet adjustments ─────────────────── */
  .appointments-container { padding: 20px 16px !important; margin: -24px -16px !important; }
  .appointments-container > .page-header { align-items: flex-start; }
  .appointments-container > div[style*="repeat(5"] { grid-template-columns: repeat(3, minmax(0, 1fr)) !important; }
  .appointments-container .filters-section { align-items: stretch; }
  .appointments-container .search-box { flex: 1 1 100%; }
  .appointments-container .date-filter-wrapper { flex: 1 1 auto; }

  /* ─── Table → Card Transformation ─────────────────── */

  /* Hide table header — labels live inside each card */
  .appointments-container .appointments-table thead {
      display: none !important;
  }

  /* Reset table display */
  .appointments-container .appointments-table,
  .appointments-container .appointments-table tbody,
  .appointments-container .appointments-table tr,
  .appointments-container .appointments-table td {
      display: block;
      width: 100%;
  }

  /* Remove min-width constraint */
  .appointments-container .appointments-table {
      min-width: 0 !important;
  }

  /* Table container no longer scrolls */
  .appointments-container .table-container {
      overflow-x: visible;
  }

  /* Each record becomes a card */
  .appointments-container .appointments-table tbody tr {
      background: var(--bg-surface, #ffffff);
      border: 1px solid var(--border, #e5e5e7);
      border-radius: 14px;
      margin-bottom: 14px;
      padding: 18px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
      transition: box-shadow 0.2s ease, transform 0.2s ease;
      position: relative;
      overflow: hidden;
  }

  .appointments-container .appointments-table tbody tr:hover {
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
      transform: translateY(-1px);
  }

  /* ─── Urgent/Imminent card styling ────────────────── */
  .appointments-container .appointments-table tbody tr.urgent-row {
      border-right: 4px solid #f59e0b;
      animation: none;
      background: linear-gradient(135deg, #fffbeb 0%, #fef3c7 100%);
  }
  .appointments-container .appointments-table tbody tr.urgent-row::before {
      display: none;
  }
  .appointments-container .appointments-table tbody tr.imminent-row {
      border-right: 4px solid #ef4444;
      background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%);
  }
  .appointments-container .appointments-table tbody tr.imminent-row::before {
      display: none;
  }

  /* ─── Each cell becomes a label → value row ───────── */
  .appointments-container .appointments-table tbody td {
      display: flex;
      align-items: flex-start;
      justify-content: space-between;
      padding: 10px 0;
      border-bottom: 1px solid var(--border-light, #f0f0f2);
      text-align: right;
      gap: 12px;
      min-height: 40px;
      vertical-align: middle;
  }

  .appointments-container .appointments-table tbody td:last-of-type {
      border-bottom: none;
  }

  /* ─── Label from data-label attribute ─────────────── */
  .appointments-container .appointments-table tbody td::before {
      content: attr(data-label);
      font-size: 12px;
      font-weight: 700;
      color: var(--text-muted, #86868b);
      text-transform: uppercase;
      letter-spacing: 0.3px;
      flex-shrink: 0;
      min-width: 72px;
      padding-top: 2px;
      line-height: 1.4;
  }

  /* ─── Value side wraps correctly ──────────────────── */
  .appointments-container .appointments-table tbody td > * {
      flex: 1;
      text-align: right;
  }

  /* ─── Patient cell card layout ─────────────────── */
  .appointments-container .patient-cell {
      flex-direction: row;
      align-items: center;
      gap: 10px;
      flex: 1;
  }

  .appointments-container .patient-avatar {
      width: 34px;
      height: 34px;
      font-size: 13px;
  }

  /* ─── Actions row: horizontal wrap at card bottom ─── */
  .appointments-container .appointments-table tbody td:last-child {
      flex-direction: column;
      align-items: stretch;
      padding-top: 14px;
      margin-top: 6px;
      border-top: 2px solid var(--border, #e5e5e7);
      border-bottom: none;
      gap: 10px;
  }

  .appointments-container .appointments-table tbody td:last-child::before {
      display: none;
  }

  .appointments-container .action-btns {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      justify-content: center;
      padding: 0;
  }

  .appointments-container .action-btn {
      flex: 0 0 auto;
      width: 48px;
      height: 48px;
      border-radius: 12px;
      background: var(--bg-subtle, #f5f5f7);
      border: 1px solid var(--border, #e5e5e7);
      transition: all 0.2s;
  }

  .appointments-container .action-btn:hover {
      background: var(--accent-soft, #f0f0ff);
      border-color: var(--accent, #5856d6);
  }

  /* ─── Status badge in card ─────────────────────────── */
  .appointments-container .status-badge {
      font-size: 11px;
      padding: 4px 10px;
  }

  /* ─── Urgent badge in card ─────────────────────────── */
  .appointments-container .urgent-badge {
      font-size: 9px;
      padding: 2px 6px;
      margin-right: 4px;
  }

  /* ─── "New" badge in card ──────────────────────────── */
  .appointments-container .appointments-table tbody td span[style*="e3f2fd"] {
      font-size: 10px;
      padding: 1px 6px;
  }

  /* ─── Empty state padding ──────────────────────────── */
  .appointments-container .empty-state {
      padding: 48px 16px !important;
  }
}

/* ═══════════════════════════════════════════════════════════
   TABLET CARD GRID (768px – 1024px)
   ──────────────────────────────────────────────────────────
   Tablets have enough width for two record cards per row.
   ═══════════════════════════════════════════════════════════ */
@media (min-width: 768px) and (max-width: 1024px) {
    .appointments-container .appointments-table tbody {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 14px;
    }

    .appointments-container .appointments-table tbody tr {
        margin-bottom: 0;
    }
}

/* ═══════════════════════════════════════════════════════════
   MOBILE PAGE ADJUSTMENTS (< 768px)
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    .appointments-container { padding: 16px 12px !important; margin: -16px -12px !important; }
    .appointments-container .page-header { flex-direction: column; align-items: stretch; margin-bottom: 20px; }
    .appointments-container .header-actions { width: 100%; justify-content: stretch; }
    .appointments-container .header-actions > button:first-child { flex: 1; justify-content: center; }
    .appointments-container .user-profile { flex: 1; justify-content: flex-start; }
    .appointments-container > div[style*="repeat(5"] { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 12px !important; }
    .appointments-container .stat-card { padding: 16px !important; }
    .appointments-container .filters-section { padding: 14px !important; }
    .appointments-container .search-box,
    .appointments-container .date-filter,
    .appointments-container .export-btn { width: 100%; }
    .appointments-container #date-filter-wrapper { width: 100%; }
    .appointments-container #date-filter-wrapper > .date-filter { width: 100%; justify-content: space-between; }
    .appointments-container #date-filter-wrapper > div[x-show] { position: fixed !important; inset: 12px; top: auto; left: 12px; width: auto !important; max-height: calc(100dvh - 24px); overflow-y: auto; }
    .appointments-container .modal-footer { flex-direction: column-reverse; }
    .appointments-container .modal-footer > * { width: 100%; }
    .appointments-container .ap-detail-grid { grid-template-columns: 1fr; }
    .appointments-container .ap-detail-actions { flex-direction: column; }
    .appointments-container .ap-detail-actions > * { width: 100%; flex: none !important; }
    .appointments-container .slot-grid { grid-template-columns: repeat(3, minmax(72px, 1fr)); }
    .appointments-container .modal-content,
    .appointments-container .ap-confirm-modal,
    .appointments-container .ap-detail-modal { border-radius: 14px; }
}

/* ═══════════════════════════════════════════════════════════
   SMALL MOBILE (< 480px)
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 479px) {
    .appointments-container { padding-inline: 8px !important; margin-inline: -8px !important; }
    .appointments-container .page-title { font-size: 24px; }
    .appointments-container .header-actions { flex-direction: column; }
    .appointments-container .header-actions > * { width: 100%; }
    .appointments-container > div[style*="repeat(5"] { grid-template-columns: 1fr !important; }
    .appointments-container .tabs-header { padding-inline: 8px; }
    .appointments-container .tab-btn { padding-inline: 16px; }
    .appointments-container .slot-grid { grid-template-columns: repeat(2, minmax(100px, 1fr)); }
    .appointments-container .ap-confirm-inner,
    .appointments-container .ap-detail-body,
    .appointments-container .modal-body { padding: 18px 16px; }
    .appointments-container .ap-detail-header { padding: 20px 16px !important; }
    .appointments-container .ap-detail-header-left { gap: 10px; }
    .appointments-container .ap-detail-title { font-size: 17px; }

    /* ─── Cards on very small screens ─────────────────── */
    .appointments-container .appointments-table tbody tr {
        padding: 14px;
        margin-bottom: 12px;
        border-radius: 12px;
    }

    .appointments-container .appointments-table tbody td {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
        padding: 8px 0;
    }

    .appointments-container .appointments-table tbody td::before {
        font-size: 11px;
        min-width: auto;
    }

    .appointments-container .appointments-table tbody td > * {
        width: 100%;
        text-align: right;
    }

    .appointments-container .action-btns {
        gap: 6px;
    }

    .appointments-container .action-btn {
        width: 44px;
        height: 44px;
    }

    /* Patient cell stays row on small screens */
    .appointments-container .patient-cell {
        flex-direction: row;
        flex-wrap: nowrap;
    }
}

/* ═══════════════════════════════════════════════════════════
   EXTRA SMALL (< 360px)
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 360px) {
    .appointments-container .slot-grid { grid-template-columns: 1fr 1fr; gap: 6px; }
    .appointments-container .slot-btn { padding-inline: 4px; font-size: 12px; }
    .appointments-container .ap-confirm-actions { flex-direction: column-reverse; }
    .appointments-container .ap-confirm-actions > * { width: 100%; }
}

/* ═══════════════════════════════════════════════════════════
   DARK MODE OVERRIDES (Mobile & Tablet Cards)
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 1024px) {
    [data-theme="dark"] .appointments-container .appointments-table tbody tr {
        background: var(--bg-surface, #1e1e2e);
        border-color: var(--border, #3a3a4a);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    }

    [data-theme="dark"] .appointments-container .appointments-table tbody td {
        border-bottom-color: var(--border-light, #2a2a3a);
    }

    [data-theme="dark"] .appointments-container .appointments-table tbody td:last-child {
        border-top-color: var(--border, #3a3a4a);
    }

    [data-theme="dark"] .appointments-container .appointments-table tbody td::before {
        color: var(--text-muted, #9ca3af);
    }

    [data-theme="dark"] .appointments-container .action-btn {
        background: var(--bg-subtle, #2a2a3a);
        border-color: var(--border, #3a3a4a);
    }

    [data-theme="dark"] .appointments-container .appointments-table tbody tr.urgent-row {
        background: linear-gradient(135deg, rgba(245,158,11,0.1) 0%, rgba(217,119,6,0.1) 100%);
    }

    [data-theme="dark"] .appointments-container .appointments-table tbody tr.imminent-row {
        background: linear-gradient(135deg, rgba(239,68,68,0.1) 0%, rgba(220,38,38,0.1) 100%);
    }
}

/* ═══════════════════════════════════════════════════════════
   REDUCED MOTION
   ═══════════════════════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
  .appointments-container *,
  .appointments-container *::before,
  .appointments-container *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
  }
}
