/* ─────────────────────────────────────────────────────────────
   Base + Reset
   ───────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Cairo', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: #f5f7fa;
  color: #1f2937;
  direction: rtl;
  text-align: right;
  line-height: 1.6;
}

a { color: #2563eb; text-decoration: none; }
a:hover { text-decoration: underline; }

/* ─────────────────────────────────────────────────────────────
   Layout
   ───────────────────────────────────────────────────────────── */
.container {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 16px;
}

.topbar {
  background: #ffffff;
  border-bottom: 1px solid #e5e7eb;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 24px;
}

.brand {
  font-size: 1.25rem;
  font-weight: 700;
  color: #111827;
  text-decoration: none;
}

.mainnav {
  display: flex;
  gap: 4px;
  flex: 1;
}

.mainnav a {
  padding: 8px 14px;
  border-radius: 8px;
  color: #374151;
  font-weight: 600;
  font-size: 0.95rem;
}

.mainnav a:hover { background: #f3f4f6; text-decoration: none; }
.mainnav a.active { background: #eff6ff; color: #1d4ed8; }

.userbox {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-name { font-weight: 600; font-size: 0.9rem; }
.user-role {
  font-size: 0.75rem;
  color: #6b7280;
  background: #f3f4f6;
  padding: 2px 8px;
  border-radius: 12px;
}

.footer {
  text-align: center;
  color: #9ca3af;
  padding: 32px 16px;
  font-size: 0.85rem;
}

/* ─────────────────────────────────────────────────────────────
   Page header
   ───────────────────────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.page-header h1 {
  margin: 0;
  font-size: 1.6rem;
  color: #111827;
}

.page-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ─────────────────────────────────────────────────────────────
   Cards
   ───────────────────────────────────────────────────────────── */
.card {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.card-title {
  margin: 0 0 16px;
  font-size: 1.15rem;
  color: #111827;
}

.section-title {
  margin: 24px 0 12px;
  font-size: 1rem;
  color: #374151;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 6px;
}

/* ─────────────────────────────────────────────────────────────
   Stats
   ───────────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 20px;
  text-align: center;
}

.stat-value {
  font-size: 2rem;
  font-weight: 700;
  color: #1d4ed8;
}

.stat-label {
  color: #6b7280;
  font-size: 0.9rem;
  margin-top: 4px;
}

/* ─────────────────────────────────────────────────────────────
   Buttons
   ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-block;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.btn:hover { text-decoration: none; }
.btn-sm { padding: 4px 10px; font-size: 0.8rem; }
.btn-block { display: block; width: 100%; }

.btn-primary { background: #2563eb; color: #fff; }
.btn-primary:hover { background: #1d4ed8; }

.btn-outline {
  background: #fff;
  color: #2563eb;
  border-color: #bfdbfe;
}
.btn-outline:hover { background: #eff6ff; }

.btn-ghost { background: #f3f4f6; color: #374151; }
.btn-ghost:hover { background: #e5e7eb; }

.btn-danger { background: #dc2626; color: #fff; }
.btn-danger:hover { background: #b91c1c; }

.btn-success { background: #16a34a; color: #fff; }
.btn-success:hover { background: #15803d; }

/* ─────────────────────────────────────────────────────────────
   Forms
   ───────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }

.form-group label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  color: #374151;
  margin-bottom: 6px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="tel"],
input[type="number"],
input[type="date"],
select,
textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.95rem;
  background: #fff;
  color: #111827;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}

input:disabled {
  background: #f9fafb;
  color: #6b7280;
}

textarea { resize: vertical; min-height: 60px; }

.input-error { border-color: #dc2626 !important; }

.field-error {
  color: #dc2626;
  font-size: 0.82rem;
  margin-top: 4px;
}

.hint {
  color: #6b7280;
  font-size: 0.78rem;
  display: block;
  margin-top: 4px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}

.form-actions {
  display: flex;
  gap: 8px;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid #e5e7eb;
}

.search-form {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.search-form input[type="text"] { flex: 1; min-width: 220px; }

.filter-form label {
  display: flex;
  flex-direction: column;
  font-size: 0.8rem;
  color: #6b7280;
  gap: 4px;
}

.filter-form input[type="date"],
.filter-form input[type="number"] { width: auto; }

.toggle-deleted {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  color: #374151;
  cursor: pointer;
  user-select: none;
}

.toggle-deleted input { width: auto; }

.inline-form { display: inline-block; margin: 0; }

/* ─────────────────────────────────────────────────────────────
   Tables
   ───────────────────────────────────────────────────────────── */
.table-wrap {
  overflow-x: auto;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.data-table thead {
  background: #f9fafb;
}

.data-table th,
.data-table td {
  padding: 10px 14px;
  text-align: right;
  border-bottom: 1px solid #e5e7eb;
}

.data-table th {
  font-weight: 700;
  color: #374151;
  font-size: 0.85rem;
  white-space: nowrap;
}

.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: #f9fafb; }

.data-table td.actions {
  white-space: nowrap;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.row-deleted { opacity: 0.6; background: #fef2f2; }

/* ─────────────────────────────────────────────────────────────
   Badges
   ───────────────────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-success { background: #dcfce7; color: #166534; }
.badge-danger  { background: #fee2e2; color: #991b1b; }

/* ─────────────────────────────────────────────────────────────
   Alerts
   ───────────────────────────────────────────────────────────── */
.alert {
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 0.92rem;
}

.alert-error {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

.alert-success {
  background: #f0fdf4;
  color: #166534;
  border: 1px solid #bbf7d0;
}

/* ─────────────────────────────────────────────────────────────
   Detail grid (patient profile)
   ───────────────────────────────────────────────────────────── */
.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 14px 24px;
  margin: 0;
}

.detail-grid > div { min-width: 0; }
.detail-grid > div.full { grid-column: 1 / -1; }

.detail-grid dt {
  font-size: 0.78rem;
  color: #6b7280;
  font-weight: 600;
  margin-bottom: 2px;
}

.detail-grid dd {
  margin: 0;
  color: #111827;
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.patient-banner {
  padding: 12px 16px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  margin-bottom: 20px;
  color: #1e40af;
}

.muted { color: #6b7280; }

/* ─────────────────────────────────────────────────────────────
   Pagination
   ───────────────────────────────────────────────────────────── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.page-info {
  font-size: 0.85rem;
  color: #6b7280;
}

/* ─────────────────────────────────────────────────────────────
   Empty state
   ───────────────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  color: #9ca3af;
  padding: 32px 16px;
  font-size: 0.95rem;
}

/* ─────────────────────────────────────────────────────────────
   Login page
   ───────────────────────────────────────────────────────────── */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #eff6ff 0%, #f5f7fa 100%);
  padding: 16px;
}

.login-card {
  background: #fff;
  border-radius: 16px;
  padding: 40px 32px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.08);
}

.login-brand {
  text-align: center;
  font-size: 1.5rem;
  font-weight: 700;
  color: #1d4ed8;
  margin-bottom: 8px;
}

.login-title {
  text-align: center;
  margin: 0 0 28px;
  font-size: 1.3rem;
  color: #111827;
}

/* ─────────────────────────────────────────────────────────────
   Error pages
   ───────────────────────────────────────────────────────────── */
.error-page {
  text-align: center;
  padding: 60px 20px;
}

.error-code {
  font-size: 5rem;
  font-weight: 700;
  color: #d1d5db;
  line-height: 1;
}

.error-page h1 {
  margin: 8px 0 12px;
  color: #111827;
}

.error-page p { color: #6b7280; margin-bottom: 24px; }

.error-stack {
  text-align: left;
  direction: ltr;
  background: #1f2937;
  color: #f9fafb;
  padding: 16px;
  border-radius: 8px;
  font-size: 0.8rem;
  overflow-x: auto;
  margin-top: 24px;
}

/* ─────────────────────────────────────────────────────────────
   Responsive
   ───────────────────────────────────────────────────────────── */
@media (max-width: 720px) {
  .topbar-inner { flex-wrap: wrap; gap: 12px; }
  .mainnav { order: 3; width: 100%; overflow-x: auto; }
  .userbox { margin-inline-start: auto; }
  .page-header { flex-direction: column; align-items: flex-start; }
}

/* ─────────────────────────────────────────────────────────────
   Clickable table rows
   ───────────────────────────────────────────────────────────── */
.row-link {
  cursor: pointer;
}

.row-link:hover {
  background: #f0f7ff;
}

/* ─────────────────────────────────────────────────────────────
   Dashboard date filter
   ───────────────────────────────────────────────────────────── */
.date-filter {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.date-filter label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  color: #374151;
}

.date-filter input[type="date"] {
  width: auto;
}

/* ─────────────────────────────────────────────────────────────
   Last visit panel (on new visit form)
   ───────────────────────────────────────────────────────────── */
.last-visit-panel {
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
}

.last-visit-panel h3 {
  margin: 0 0 12px;
  font-size: 1rem;
  color: #374151;
}

.last-visit-panel.empty {
  text-align: center;
  color: #9ca3af;
  font-size: 0.9rem;
}

/* ─────────────────────────────────────────────────────────────
   Attachments
   ───────────────────────────────────────────────────────────── */
.attachments {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}

.attachments-list {
  margin-bottom: 16px;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;
  font-size: 0.9rem;
}

.attachment-item .file-name {
  font-weight: 600;
  flex: 1;
  min-width: 150px;
  word-break: break-all;
}

.attachment-item .file-meta,
.attachment-item .file-user {
  color: #6b7280;
  font-size: 0.8rem;
}

.dropzone {
  border: 2px dashed #bfdbfe;
  border-radius: 8px;
  padding: 16px;
  text-align: center;
  background: #f8fbff;
  transition: background 0.15s, border-color 0.15s;
}

.dropzone.dragover {
  background: #dbeafe;
  border-color: #2563eb;
}

.dropzone-inner {
  display: block;
  cursor: pointer;
  color: #374151;
  font-size: 0.9rem;
}

.dropzone-inner input[type="file"] {
  display: none;
}

.dropzone-inner p {
  margin: 4px 0;
}

.dropzone-inner .hint {
  color: #6b7280;
  font-size: 0.78rem;
}

.dropzone-preview {
  margin-top: 12px;
  text-align: right;
}

.preview-item {
  padding: 4px 8px;
  font-size: 0.85rem;
  color: #374151;
  border-bottom: 1px solid #e5e7eb;
}

.preview-item:last-child {
  border-bottom: none;
}

.preview-warn {
  color: #b45309;
  font-weight: 600;
}

.preview-error {
  color: #b91c1c;
  font-weight: 600;
}

/* ─────────────────────────────────────────────────────────────
   Rich text rendered output
   ───────────────────────────────────────────────────────────── */
mark {
  background: #fef08a;
  padding: 0 2px;
  border-radius: 2px;
}

del {
  color: #9ca3af;
}

/* ─────────────────────────────────────────────────────────────
   Mobile tweaks
   ───────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .container {
    padding: 16px 12px;
  }

  .card,
  .attachments {
    padding: 16px 14px;
  }

  .attachment-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .date-filter {
    flex-direction: column;
    align-items: stretch;
  }

  .date-filter label {
    justify-content: space-between;
  }
}