/* ==========================================================================
   Clinic Express System - Guimaras Provincial Hospital Unified Stylesheet
   ========================================================================== */

:root {
  --primary-deep: #0b5e7e;
  --primary-light: #1e88a8;
  --primary-dark: #06384d;
  --accent-gold: #ffc107;
  --bg-app: #f4f7f9;
  --text-dark: #1e293b;
  --text-muted: #64748b;
  --border-color: #e2e8f0;
  --white: #ffffff;
  --shadow-sm: 0 4px 12px rgba(11, 94, 126, 0.08);
  --shadow-lg: 0 25px 50px -12px rgba(0, 30, 50, 0.25);
}

* { 
  box-sizing: border-box; 
}

/* ==========================================================================
   1. LOGIN PAGE STYLES (index.php)
   ========================================================================== */
body.login-body-page, body {
  background: linear-gradient(135deg, #0b5e7e 0%, #1e88a8 50%, #4fc3f7 100%);
  font-family: 'Inter', 'Muli', 'Segoe UI', Tahoma, sans-serif;
  margin: 0;
  padding: 30px 15px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.login-container {
  width: 100%;
  max-width: 680px;
  margin: auto;
}

.login-card {
  background: var(--white);
  border-radius: 32px;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.login-banner {
  width: 100%;
  background: #eef9fd;
  padding: 30px 20px 20px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #e2edf2;
}

.login-banner img {
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  border-radius: 16px;
  filter: drop-shadow(0 6px 12px rgba(0,0,0,0.06));
}

.login-header {
  background: var(--primary-deep);
  color: var(--white);
  padding: 18px;
  text-align: center;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.login-body {
  padding: 40px;
  flex-grow: 1;
}

.error-message {
  background: #ffebee;
  color: #c62828;
  padding: 12px 16px;
  border-radius: 12px;
  margin-bottom: 20px;
  text-align: center;
  font-size: 14px;
  border-left: 4px solid #c62828;
  font-weight: 600;
}

.datetime {
  text-align: center;
  color: var(--text-muted);
  margin-bottom: 25px;
  font-size: 13px;
  padding: 10px 14px;
  background: #f4f9fe;
  border-radius: 10px;
  border: 1px solid #e2edf2;
  font-weight: 600;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 700;
  color: #1a2c3c;
}

.form-control {
  width: 100%;
  padding: 14px 18px;
  border: 2px solid #e2edf2;
  border-radius: 12px;
  font-size: 15px;
  transition: all 0.3s ease;
  background: #fafcfd;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary-light);
  background: var(--white);
  box-shadow: 0 0 0 4px rgba(30, 136, 168, 0.15);
}

.btn-login {
  width: 100%;
  background: var(--primary-deep);
  color: white;
  border: none;
  padding: 15px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 8px 20px rgba(11, 94, 126, 0.25);
  margin-top: 5px;
}

.btn-login:hover {
  background: var(--primary-light);
  transform: translateY(-1px);
}

.btn-login:active {
  transform: scale(0.99);
}

.demo-section {
  margin-top: 30px;
  padding-top: 22px;
  border-top: 2px dashed #e2edf2;
  font-size: 13px;
}

.demo-title {
  font-weight: 800;
  color: var(--primary-deep);
  margin-bottom: 10px;
  display: block;
  text-transform: uppercase;
  font-size: 0.8rem;
  letter-spacing: 0.5px;
}

.demo-group {
  margin-bottom: 12px;
}

.demo-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 5px;
}

.demo-user {
  display: inline-block;
  background: #eef9fd;
  color: var(--primary-deep);
  border: 1px solid #cce4ee;
  padding: 5px 10px;
  border-radius: 20px;
  text-decoration: none !important;
  transition: all 0.2s ease;
  font-family: monospace;
  font-size: 12px;
  font-weight: 600;
}

.demo-user:hover {
  background: var(--primary-deep);
  color: white;
  border-color: var(--primary-deep);
}

.footer {
  margin-top: 25px;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-size: 13px;
  font-weight: 600;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}


/* ==========================================================================
   2. ENTERPRISE DASHBOARD STYLES (main.php)
   ========================================================================== */
body.main-dashboard-page {
  background-color: var(--bg-app);
  overflow: hidden;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.pulmo-banner {
  background: linear-gradient(135deg, #ffffff 0%, #f4f9fe 100%);
  padding: 16px 24px;
  border-bottom: 3px solid var(--primary-light);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
  flex-shrink: 0;
}

.brand-icon-circle {
  font-size: 2rem;
  color: var(--primary-deep);
  background: rgba(11, 94, 126, 0.1);
  width: 55px;
  height: 55px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  flex-shrink: 0;
}

.brand-title-banner {
  font-family: 'Playfair Display', serif;
  font-weight: 700;
  color: var(--primary-deep);
  font-size: 1.5rem;
  letter-spacing: 0.5px;
  margin: 0;
  line-height: 1.2;
}

.brand-sub-banner {
  font-size: 0.85rem;
  text-transform: uppercase;
  font-weight: 600;
  letter-spacing: 1.5px;
  color: var(--text-muted);
  margin-top: 3px;
}

.navbar-pulmo {
  background: linear-gradient(135deg, var(--primary-deep) 0%, var(--primary-dark) 100%) !important;
  border-bottom: 3px solid #1479a0;
  padding: 0.75rem 1.5rem;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
  z-index: 1030;
}

.navbar-pulmo .navbar-brand {
  font-family: 'Playfair Display', serif;
  font-weight: 700;
  color: #ffffff !important;
  font-size: 1.4rem;
  letter-spacing: 1px;
}

.navbar-pulmo .nav-link {
  color: rgba(255, 255, 255, 0.9) !important;
  font-weight: 600;
  font-size: 0.95rem;
  margin: 0 6px;
  transition: all 0.2s ease;
  padding: 0.5rem 1rem !important;
  border-radius: 8px;
}

.navbar-pulmo .nav-link:hover {
  color: #ffffff !important;
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-1px);
}

.user-info-panel {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 6px 14px;
  border-left: 4px solid var(--accent-gold);
  line-height: 1.3;
}

.user-info-panel .fw-bold {
  font-size: 0.85rem;
  letter-spacing: 0.5px;
}

.btn-toggle-panel {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #ffffff;
  border-radius: 8px;
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  font-weight: 600;
  transition: all 0.2s ease;
}

.btn-toggle-panel:hover {
  background: rgba(255, 255, 255, 0.25);
  border-color: #ffffff;
  color: #ffffff;
}

.app-container {
  display: flex;
  height: calc(100vh - 87px - 66px);
  width: 100%;
  position: relative;
  overflow: hidden;
}

.main-panel {
  flex: 1;
  background: #ffffff;
  overflow: hidden;
  position: relative;
}

.main-panel iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

.side-panel {
  flex: 0 0 380px;
  background: #f8fafc;
  border-left: 1px solid var(--border-color);
  overflow: hidden;
  transition: flex 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.25s ease;
  position: relative;
}

.side-panel iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: #f1f5f9;
  display: block;
}

.side-panel.collapsed {
  flex: 0 0 45px;
}

.side-panel.collapsed iframe {
  display: none;
}

.side-panel .collapse-handle {
  display: none;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-size: 0.85rem;
  color: var(--primary-deep);
  letter-spacing: 2px;
  cursor: pointer;
  user-select: none;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  background: #eef4f8;
  border-right: 1px solid var(--border-color);
  padding: 15px 4px;
  font-weight: 700;
  width: 100%;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.side-panel .collapse-handle:hover {
  background: #e2edf5;
}

.side-panel.collapsed .collapse-handle {
  display: flex;
}


/* ==========================================================================
   3. RESPONSIVE ADJUSTMENTS
   ========================================================================== */
@media (max-width: 640px) {
  body.login-body-page {
    padding: 15px 10px;
  }
  .login-body {
    padding: 25px 20px;
  }
}

@media (max-width: 992px) {
  .pulmo-banner {
    padding: 12px 15px;
  }
  .brand-title-banner {
    font-size: 1.15rem;
  }
  .brand-icon-circle {
    width: 45px;
    height: 45px;
    font-size: 1.5rem;
  }
  .app-container {
    flex-direction: column;
    height: calc(100vh - 70px - 56px);
  }
  .side-panel {
    flex: 0 0 45vh;
    border-left: none;
    border-top: 1px solid var(--border-color);
    width: 100%;
  }
  .side-panel.collapsed {
    flex: 0 0 45px;
    width: 100%;
  }
}


/* ==========================================================================
   FULL-WIDTH 100% UNIFIED HEADER SYSTEM
   ========================================================================== */
.master-command-header {
  width: 100%;
  position: sticky;
  top: 0;
  z-index: 1050;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
}

.hospital-identity-bar {
  width: 100%;
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  padding: 10px 0;
}

.hospital-identity-bar .container-fluid {
  max-width: 100%;
  padding-left: 2rem;
  padding-right: 2rem;
}

.master-hospital-title {
  font-family: 'Playfair Display', serif;
  font-weight: 800;
  color: var(--primary-deep);
  font-size: 1.25rem;
  letter-spacing: 0.5px;
  margin: 0;
  line-height: 1.2;
}

.master-hospital-subtitle {
  font-size: 0.72rem;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-top: 2px;
}

.navbar-pulmo {
  width: 100%;
  background: linear-gradient(135deg, #0b5e7e 0%, #06384d 100%) !important;
  border-bottom: 2px solid #1e88a8;
  padding: 0.5rem 2rem !important;
}

.navbar-pulmo .container-fluid {
  max-width: 100%;
  padding: 0;
}

.navbar-pulmo .navbar-brand {
  font-family: 'Playfair Display', serif;
  font-weight: 800;
  color: #ffffff !important;
  font-size: 1.25rem;
  letter-spacing: 1px;
}

.navbar-pulmo .nav-link {
  color: rgba(255, 255, 255, 0.9) !important;
  font-weight: 700;
  font-size: 0.82rem;
  letter-spacing: 0.5px;
  margin: 0 3px;
  padding: 0.45rem 0.85rem !important;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.navbar-pulmo .nav-link:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #ffffff !important;
}

.app-container {
  display: flex;
  height: calc(100vh - 110px);
  width: 100%;
  position: relative;
  overflow: hidden;
}


/* ==========================================================================
   4. PATIENT REGISTER STYLES (patient.php)
   ========================================================================== */
body {
  font-family: 'Karla', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: #f4f8f8;
  color: #1e293b;
  min-height: 100vh;
  letter-spacing: -0.01em;
}

.main-card {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(0, 139, 139, 0.1);
  border-radius: 20px;
  box-shadow: 0 12px 35px rgba(0, 139, 139, 0.06);
  padding: 35px;
  margin-bottom: 40px;
  margin-top: 20px;
}

.register-header {
  font-family: 'Playfair Display', serif;
  font-weight: 700;
  color: #006f6f;
  margin: 0;
  letter-spacing: -0.02em;
}

.matches-badge {
  font-family: 'Karla', sans-serif;
  background-color: rgba(0, 139, 139, 0.08);
  color: #008b8b;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 30px;
  border: 1px solid rgba(0, 139, 139, 0.15);
}

.btn-teal {
  background-color: #008b8b;
  color: white !important;
  font-weight: 600;
  padding: 10px 22px;
  border-radius: 30px;
  transition: all 0.25s ease;
  border: none;
  box-shadow: 0 4px 12px rgba(0, 139, 139, 0.15);
}

.btn-teal:hover {
  background-color: #006f6f;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 139, 139, 0.25);
}

.btn-outline-teal {
  color: #008b8b !important;
  border: 1.5px solid #008b8b;
  font-weight: 600;
  padding: 10px 22px;
  border-radius: 30px;
  transition: all 0.25s ease;
  background-color: transparent;
}

.btn-outline-teal:hover {
  background-color: rgba(0, 139, 139, 0.05);
  border-color: #006f6f;
  color: #006f6f !important;
  transform: translateY(-2px);
}

#search-container {
  position: relative;
}

.search-input-group {
  display: flex;
  align-items: center;
  background: #ffffff;
  border: 1.5px solid rgba(0, 139, 139, 0.2);
  border-radius: 30px;
  padding: 4px 6px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0,0,0,0.02);
}

.search-input-group:focus-within {
  border-color: #008b8b;
  box-shadow: 0 0 0 4px rgba(0, 139, 139, 0.1);
}

.search-icon-affix {
  color: #008b8b;
  font-size: 1.15rem;
  margin-left: 18px;
  margin-right: 10px;
}

.search-input-field {
  border: none !important;
  outline: none !important;
  box-shadow: none !important;
  font-size: 1rem;
  padding: 8px 10px;
  background: transparent !important;
}

.search-btn {
  background-color: #008b8b;
  color: white !important;
  border-radius: 30px !important;
  font-weight: 600;
  padding: 8px 25px !important;
  transition: all 0.2s;
}

.search-btn:hover {
  background-color: #006f6f;
}

#patient-suggestions {
  position: absolute;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(0, 139, 139, 0.15);
  border-radius: 16px;
  max-height: 350px;
  overflow-y: auto;
  z-index: 1050;
  width: 100%;
  display: none;
  margin-top: 6px;
  box-shadow: 0 10px 25px rgba(0, 139, 139, 0.08);
  padding: 8px 0;
}

.suggestion-item {
  padding: 12px 20px;
  cursor: pointer;
  border-bottom: 1px solid rgba(0,0,0,0.03);
  transition: all 0.2s;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 12px;
}

.suggestion-item:last-child {
  border-bottom: none;
}

.suggestion-item:hover, .suggestion-item.active {
  background-color: rgba(0, 139, 139, 0.05);
  color: #006f6f;
  padding-left: 24px;
}

.avatar-badge {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9rem;
  color: #fff;
  flex-shrink: 0;
  text-shadow: 0 1px 1px rgba(0,0,0,0.08);
}

.patient-table {
  border-collapse: separate !important;
  border-spacing: 0 10px !important;
  width: 100%;
}

.patient-table tr {
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.02);
  border-radius: 12px;
  transition: all 0.2s ease;
}

.patient-table tr:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 139, 139, 0.08);
  background-color: #ffffff;
}

.patient-table th {
  background-color: #008b8b !important;
  color: white !important;
  border: none !important;
  font-weight: 700;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  padding: 14px 18px !important;
}

.patient-table th:first-child {
  border-top-left-radius: 12px;
  border-bottom-left-radius: 12px;
}

.patient-table th:last-child {
  border-top-right-radius: 12px;
  border-bottom-right-radius: 12px;
}

.patient-table td {
  border: none !important;
  background: #fff;
  padding: 18px !important;
  vertical-align: middle !important;
  font-size: 0.95rem;
}

.patient-table td:first-child {
  border-top-left-radius: 12px;
  border-bottom-left-radius: 12px;
  font-weight: 600;
  color: #4e7a7a;
}

.patient-table td:last-child {
  border-top-right-radius: 12px;
  border-bottom-right-radius: 12px;
}

.patient-name-link {
  font-weight: 700;
  color: #1e293b;
  text-decoration: none !important;
  transition: color 0.15s;
  font-size: 1.02rem;
}

.patient-name-link:hover {
  color: #008b8b;
}

.visit-badge {
  font-size: 0.8rem;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 20px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.visit-active {
  background-color: #e6f7f0;
  color: #00875a;
  border: 1px solid rgba(0, 135, 90, 0.15);
}

.visit-none {
  background-color: #f1f5f9;
  color: #475569;
  border: 1px solid rgba(71, 85, 105, 0.1);
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 35px;
}

.pagination-container .pagination {
  gap: 6px;
}

.pagination-container .page-item .page-link {
  border: none;
  background-color: rgba(255, 255, 255, 0.8);
  color: #008b8b;
  font-weight: 600;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  box-shadow: 0 2px 6px rgba(0,0,0,0.03);
}

.pagination-container .page-item.active .page-link {
  background-color: #008b8b;
  color: white !important;
  box-shadow: 0 4px 10px rgba(0, 139, 139, 0.25);
}

.pagination-container .page-link:hover {
  background-color: rgba(0, 139, 139, 0.1);
  color: #006f6f;
  transform: scale(1.05);
}

<?php
require_once("head.php");

// Ensure UTF‑8
mysqli_set_charset($connect_mysql, "utf8");

$id = secure_string("get", "id");
$goVisit = secure_string("get", "goVisit");

// Main patient query with JOIN
$sql = "SELECT
            p.*,
            DATE_FORMAT(p.BDay, '%M %d, %Y') AS bday,
            r.religion AS religion_name
        FROM patients p
        LEFT JOIN religion r ON r.religion_id = p.religion_id
        WHERE p.PatientID = ?";
$stmt = mysqli_prepare($connect_mysql,$sql);
if (!$stmt) {
    die("Prepare failed: " . mysqli_error($connect_mysql));
}
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (!$result \vert{}\vert{} mysqli_num_rows($result) == 0) {
    die("Patient not found.");
}
$row = mysqli_fetch_assoc($result);
mysqli_stmt_close($stmt);

// Assign variables (using null coalescing for safety)
$patientID   =$id;
$patient_id  =$row['Patient_id'] ?? '';
$lname       =$row['Lname'] ?? '';
$fname       =$row['Fname'] ?? '';
$mname       =$row['Mname'] ?? '';
$address     =$row['Address'] ?? '';
$home_num    =$row['HomeNum'] ?? '';
$office_add  =$row['OfficeAddress'] ?? '';
$office_num  =$row['OfficeNum'] ?? '';
$occupation  =$row['Occupation'] ?? '';
$religion    =$row['religion_name'] ?? '';
$gender      =$row['Sex'] ?? '';
$status      =$row['Status'] ?? '';
$height      = stripslashes($row['Height'] ?? '');
$weight      =$row['Weight'] ?? '';
$referral    =$row['ReferedBy'] ?? '';
$bday        =$row['BDay'] ?? '';
$bdate       =$row['bday'] ?? '';
$age         =$row['Age'] ?? '';
$picture     =$row['picture'] ?? '';
$clinic      =$row['clinic'] ?? '';
$hname       =$row['hname'] ?? '';
$hage        =$row['hage'] ?? '';
$fb          =$row['fb'] ?? '';
$nname       =$row['nname'] ?? '';
$Email       =$row['Email'] ?? '';
$Husband     =$row['Husband'] ?? '';
$information =$row['information'] ?? '';

// Fetch current user role and profile with multi-key session fallback
$userId =$_SESSION['userid'] ?? $_SESSION['user_id'] ?? $_SESSION['UserID'] ?? $_SESSION['id'] ?? '';$isAdmin = false;
$isSec = false;
$profileID = '';$dname = '';

// Check direct session admin flags if set
if (isset($_SESSION['isAdmin'])) {
    $isAdmin = (bool)$_SESSION['isAdmin'];
} elseif (isset($_SESSION['admin'])) {
    $isAdmin = in_array(strtoupper(trim((string)$_SESSION['admin'])), ['Y', '1', 'YES', 'TRUE', 'ADMIN'], true);
}
if ($userId) {$userSql = "SELECT Admin, profileID FROM users WHERE UserID = ? LIMIT 1";
    $userStmt = mysqli_prepare($connect_mysql,$userSql);
    if ($userStmt) {
        mysqli_stmt_bind_param($userStmt, "s", $userId);
        mysqli_stmt_execute($userStmt);
        $userRes = mysqli_stmt_get_result($userStmt);
        if ($userData = mysqli_fetch_assoc($userRes)) {
            $adminVal = strtoupper(trim((string)($userData['Admin'] ?? '')));
            if (in_array($adminVal, ['Y', '1', 'YES', 'TRUE', 'ADMIN'], true)) {$isAdmin = true;
            }
            $isSec     = !$isAdmin;
            $profileID =$userData['profileID'] ?? '';
        }
        mysqli_stmt_close($userStmt);
    }
}

// --- NEW: Fetch patient relationships (bidirectional lookup) ---
$relationships = [];$sql_rel = "SELECT pr.RelationshipID, pr.RelationshipType, pr.IsEmergencyContact, pr.Notes,
                   CASE WHEN pr.PatientID = ? THEN r.PatientID ELSE p.PatientID END AS related_patient_pk,
                   CASE WHEN pr.PatientID = ? THEN r.Patient_id ELSE p.Patient_id END AS related_patient_code,
                   CASE WHEN pr.PatientID = ? THEN r.Fname ELSE p.Fname END AS rel_fname,
                   CASE WHEN pr.PatientID = ? THEN r.Mname ELSE p.Mname END AS rel_mname,
                   CASE WHEN pr.PatientID = ? THEN r.Lname ELSE p.Lname END AS rel_lname
            FROM patient_relationships pr
            LEFT JOIN patients p ON pr.PatientID = p.PatientID
            LEFT JOIN patients r ON pr.RelatedPatientID = r.PatientID
            WHERE pr.PatientID = ? OR pr.RelatedPatientID = ?";
$stmtRel = mysqli_prepare($connect_mysql,$sql_rel);
if ($stmtRel) {
    mysqli_stmt_bind_param($stmtRel, "iiiiiii", $id, $id,$id, $id,$id, $id,$id);
    mysqli_stmt_execute($stmtRel);
    $resRel = mysqli_stmt_get_result($stmtRel);
    while ($rel_row = mysqli_fetch_assoc($resRel)) {
        $relationships[] =$rel_row;
    }
    mysqli_stmt_close($stmtRel);
}
// --- END RELATIONSHIP QUERY ---

// Fetch doctors assigned to this patient
$doctors = [];$sql_doctors = "SELECT p.dname
                FROM profile p
                INNER JOIN patient_doctors pd ON p.profileID = pd.profile_id
                WHERE pd.patient_id = ?";
$stmtDocs = mysqli_prepare($connect_mysql,$sql_doctors);
if ($stmtDocs) {
    mysqli_stmt_bind_param($stmtDocs, "i", $patientID);
    mysqli_stmt_execute($stmtDocs);
    $resDocs = mysqli_stmt_get_result($stmtDocs);
    while ($doc_row = mysqli_fetch_assoc($resDocs)) {
        $doctors[] =$doc_row['dname'];
    }
    mysqli_stmt_close($stmtDocs);
}
$doctors_list = !empty($doctors) ? implode(', ', $doctors) : 'None assigned';

// Fetch profile fields (doctor's details) if admin
$doc2 = $title =$title2 = $clinic =$clinic2 = $clinic3 =$narlic = $ptr =$logo = $wm =$specialty = '';
if ($isAdmin && $profileID) {$sqlProfile = "SELECT * FROM profile WHERE profileID = ?";
    $stmtProf = mysqli_prepare($connect_mysql,$sqlProfile);
    if ($stmtProf) {
        mysqli_stmt_bind_param($stmtProf, "s", $profileID);
        mysqli_stmt_execute($stmtProf);
        $resProf = mysqli_stmt_get_result($stmtProf);
        if ($rowProfile = mysqli_fetch_assoc($resProf)) {
            $dname     =$rowProfile['dname'] ?? '';
            $doc2      =$rowProfile['doc2'] ?? '';
            $title     =$rowProfile['title'] ?? '';
            $title2    =$rowProfile['title2'] ?? '';
            $clinic    =$rowProfile['clinic'] ?? '';
            $clinic2   =$rowProfile['clinic2'] ?? '';
            $clinic3   =$rowProfile['clinic3'] ?? '';
            $narlic    =$rowProfile['narlic'] ?? '';
            $ptr       =$rowProfile['ptr'] ?? '';
            $logo      =$rowProfile['logo'] ?? '';
            $wm        =$rowProfile['wm'] ?? '';
            $specialty =$rowProfile['specialty'] ?? '';
        }
        mysqli_stmt_close($stmtProf);
    }
}

$error_msg = htmlspecialchars(trim($_GET['error_msg'] ?? ''), ENT_QUOTES, 'UTF-8');
$success_msg = htmlspecialchars(trim($_GET['success_msg'] ?? ''), ENT_QUOTES, 'UTF-8');

// Check if patient has revision history
$has_revisions = false;
$sqlCheckRev = "SELECT COUNT(*) as revision_count
                FROM patient_log
                WHERE PatientID = ?
                AND (log LIKE '%Fname:%' OR log LIKE '%Lname:%')";
$stmtRev = mysqli_prepare($connect_mysql,$sqlCheckRev);
if ($stmtRev) {
    mysqli_stmt_bind_param($stmtRev, "i", $id);
    mysqli_stmt_execute($stmtRev);
    $resRev = mysqli_stmt_get_result($stmtRev);
    if ($revData = mysqli_fetch_assoc($resRev)) {
        $has_revisions = ($revData['revision_count'] > 0);
    }
    mysqli_stmt_close($stmtRev);
}
?>

<!-- Modern CSS, Typography & Layout -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@600;700&family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">

<style>
   :root {
    --primary: #008b8b;
    --primary-light: #20b2aa;
    --primary-dark: #005656;
    --accent-teal: #0d9488;
    --bg-soft: #f0f7f7;
    --card-bg: rgba(255, 255, 255, 0.88);
    --shadow-sm: 0 4px 12px rgba(0, 139, 139, 0.05);
    --shadow-md: 0 10px 25px rgba(0, 139, 139, 0.08);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background-color: var(--bg-soft) !important;
    color: #1e293b;
    padding-bottom: 3rem;
}

.glass-header {
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(0, 139, 139, 0.12);
    padding: 18px 24px;
    box-shadow: var(--shadow-sm);
    border-radius: 20px;
    margin-bottom: 24px;
}

.header-title {
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    color: var(--primary-dark);
    margin: 0;
    font-size: 1.6rem;
    letter-spacing: -0.3px;
}

.doctor-assigned-badge {
    font-size: 0.825rem;
    font-weight: 600;
    color: var(--accent-teal);
    background-color: rgba(13, 148, 136, 0.08);
    border: 1px solid rgba(13, 148, 136, 0.18);
    border-radius: 30px;
    padding: 5px 14px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.glass-card {
    background: var(--card-bg);
    backdrop-filter: blur(10px);
    border-radius: 1.5rem;
    box-shadow: var(--shadow-md);
    border: 1px solid rgba(255, 255, 255, 0.7);
    overflow: hidden;
    margin-bottom: 1.5rem;
    padding: 24px;
}

.nav-pills-custom {
    background: rgba(0, 139, 139, 0.05);
    padding: 6px;
    border-radius: 35px;
    border: 1px solid rgba(0, 139, 139, 0.1);
    gap: 4px;
    display: inline-flex;
    flex-wrap: wrap;
}

.nav-pills-custom .nav-link {
    font-size: 0.825rem;
    font-weight: 600;
    color: #0f172a;
    padding: 9px 20px;
    border-radius: 25px;
    background: #0DCAF0;
    border: 1px solid #cbd5e1;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    gap: 6px;
}

.nav-pills-custom .nav-link:hover {
    color: var(--primary-dark);
    background: #ffffff;
    border-color: var(--primary);
    transform: translateY(-1px);
}

.nav-pills-custom .nav-link.active {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%) !important;
    color: #ffffff !important;
    box-shadow: 0 4px 14px rgba(0, 139, 139, 0.3);
    transform: translateY(-1px);
}

.info-label {
    font-size: 0.725rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #64748b;
    font-weight: 600;
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.info-value {
    font-size: 0.95rem;
    color: #0f172a;
    font-weight: 500;
}

.info-block {
    background: rgba(255, 255, 255, 0.65);
    border: 1px solid rgba(0, 139, 139, 0.1);
    border-radius: 14px;
    padding: 12px 16px;
    height: 100%;
    transition: all 0.2s ease;
}

.info-block:hover {
    background: #ffffff;
    border-color: var(--primary-light);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 139, 139, 0.06);
}

.section-title {
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    color: var(--primary-dark);
    border-left: 4px solid var(--primary);
    padding-left: 12px;
    margin-bottom: 20px;
    font-size: 1.3rem;
}

.patient-badge {
    font-