:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --success: #16a34a;
  --warning: #f59e0b;
  --bg: #f8fafc;
  --bg-secondary: #f1f5f9;
  --surface: #ffffff;
  --text: #1e293b;
  --text-secondary: #64748b;
  --border: #e2e8f0;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.hidden { display: none !important; }

/* Login */
.screen { display: none; min-height: 100vh; }
.screen.active { display: block; }

.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 60px 20px;
  text-align: center;
}

.login-logo {
  margin-bottom: 32px;
  color: var(--primary);
}

.login-logo h1 { font-size: 24px; margin-top: 12px; color: var(--text); }

.login-tabs { display: flex; gap: 8px; margin-bottom: 24px; }
.tab-btn {
  flex: 1;
  padding: 10px;
  border: none;
  background: var(--bg-secondary);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 14px;
}
.tab-btn.active { background: var(--primary); color: white; }

.auth-form { display: none; }
.auth-form.active { display: block; }

.auth-form input, .auth-form select {
  width: 100%;
  padding: 12px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
}

.auth-form button {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
}

.auth-form button:hover { background: var(--primary-hover); }

.forgot-link {
  display: block;
  margin-top: 12px;
  color: var(--text-secondary);
  font-size: 13px;
  text-decoration: none;
}

.error { color: var(--danger); font-size: 13px; margin-top: 8px; }
.success { color: var(--success); font-size: 13px; margin-top: 8px; }

/* Topbar */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-left { display: flex; align-items: center; gap: 12px; }
.menu-btn { background: none; border: none; cursor: pointer; padding: 4px; }
.topbar-title { font-weight: 600; font-size: 16px; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-name { font-size: 13px; color: var(--text-secondary); }

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius);
  color: var(--text-secondary);
}
.btn-icon:hover { background: var(--bg-secondary); color: var(--text); }

/* Main Layout */
.main-layout { display: flex; min-height: calc(100vh - 57px); }

/* Sidebar */
.sidebar {
  width: 220px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 0;
  flex-shrink: 0;
}

.sidebar-nav { display: flex; flex-direction: column; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 14px;
}
.nav-item:hover { background: var(--bg-secondary); color: var(--text); }
.nav-item.active { background: #eff6ff; color: var(--primary); border-right: 3px solid var(--primary); }

/* Content */
.content { flex: 1; padding: 24px; overflow-y: auto; }

.view { display: none; }
.view.active { display: block; }

/* Dashboard */
.dashboard-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
  margin-bottom: 32px;
}

.stat-card {
  background: var(--surface);
  padding: 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.stat-value { font-size: 32px; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }

.dashboard-section {
  background: var(--surface);
  padding: 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.dashboard-section h3 { margin-bottom: 16px; font-size: 16px; }

/* Calendar */
.calendar-nav {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.calendar-nav span { font-weight: 600; min-width: 180px; text-align: center; }

.calendar-view-toggle {
  display: flex;
  gap: 0;
  margin-left: auto;
  align-items: center;
}
.cal-view-btn {
  padding: 6px 14px;
  font-size: 13px;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  cursor: pointer;
  color: var(--text-secondary);
}
.cal-view-btn:first-child { border-radius: 6px 0 0 6px; }
.cal-view-btn:nth-child(3) { border-radius: 0 6px 6px 0; }
.cal-view-btn:not(:first-child) { border-left: none; }
.cal-view-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  background: var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.calendar-day-header {
  background: var(--bg-secondary);
  padding: 8px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.calendar-day {
  background: var(--surface);
  min-height: 100px;
  padding: 8px;
  cursor: pointer;
}

.calendar-day:hover { background: var(--bg-secondary); }
.calendar-day.other-month { color: var(--text-secondary); opacity: 0.5; }
.calendar-day.today { background: #eff6ff; }

.calendar-day-num { font-size: 14px; font-weight: 500; margin-bottom: 4px; }

.calendar-event {
  font-size: 11px;
  padding: 2px 6px;
  background: var(--primary);
  color: white;
  border-radius: 4px;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Week view */
.week-grid {
  display: grid;
  grid-template-columns: 60px repeat(7, 1fr);
  gap: 1px;
  background: var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.week-time-label {
  background: var(--bg-secondary);
  padding: 4px 6px;
  font-size: 11px;
  color: var(--text-secondary);
  text-align: right;
  min-height: 48px;
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
}
.week-cell {
  background: var(--surface);
  min-height: 48px;
  padding: 2px 4px;
  cursor: pointer;
  position: relative;
}
.week-cell:hover { background: var(--bg-secondary); }
.week-cell.today { background: #eff6ff; }
.week-header {
  background: var(--bg-secondary);
  padding: 8px 4px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
}
.week-header.today { background: #dbeafe; color: var(--primary); }
.week-header-corner { background: var(--bg-secondary); }
.week-event {
  font-size: 10px;
  padding: 2px 4px;
  background: var(--primary);
  color: white;
  border-radius: 3px;
  margin-bottom: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Day view */
.day-grid {
  display: grid;
  grid-template-columns: 70px 1fr;
  gap: 1px;
  background: var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.day-time-label {
  background: var(--bg-secondary);
  padding: 8px 8px;
  font-size: 12px;
  color: var(--text-secondary);
  text-align: right;
  min-height: 56px;
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
}
.day-cell {
  background: var(--surface);
  min-height: 56px;
  padding: 4px 8px;
  cursor: pointer;
  position: relative;
}
.day-cell:hover { background: var(--bg-secondary); }
.day-event {
  font-size: 13px;
  padding: 6px 10px;
  background: var(--primary);
  color: white;
  border-radius: 6px;
  margin-bottom: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.day-event-name { font-weight: 500; }
.day-event-meta { font-size: 11px; opacity: 0.85; }

/* Tables */
.table-container { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }

table { width: 100%; border-collapse: collapse; }

th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--bg-secondary); font-weight: 600; font-size: 13px; color: var(--text-secondary); }
td { font-size: 14px; }
tr:hover { background: var(--bg-secondary); }

.search-input {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  width: 250px;
}

.view-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.view-header h2 { font-size: 20px; }

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

.btn-back {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 14px;
  padding: 8px 0;
}

.btn-primary, .btn-secondary, .btn-danger {
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: none;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: var(--bg-secondary); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }

.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }

/* Patient Detail */
.patient-info {
  background: var(--surface);
  padding: 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
}

.patient-info-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.patient-field { margin-bottom: 12px; }
.patient-field-label { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; }
.patient-field-value { font-size: 15px; font-weight: 500; }

.patient-tabs { display: flex; gap: 4px; margin-bottom: 20px; border-bottom: 1px solid var(--border); }
.patient-tab { display: none; }
.patient-tab.active { display: block; }

.tab {
  padding: 12px 20px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-header h3 { font-size: 16px; }

/* Timeline */
.timeline-list { display: flex; flex-direction: column; gap: 16px; }

.timeline-item {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.timeline-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

.timeline-date { font-weight: 600; font-size: 14px; }

.timeline-content { padding: 16px; }

.timeline-section { margin-bottom: 16px; }
.timeline-section:last-child { margin-bottom: 0; }

.timeline-section-title {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.timeline-section-content { font-size: 14px; line-height: 1.6; white-space: pre-wrap; }

/* Tags */
.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  margin-right: 4px;
  margin-bottom: 4px;
}

.tag-active { background: #dcfce7; color: #166534; }
.tag-stopped { background: #fee2e2; color: #991b1b; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 { font-size: 16px; }

.btn-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-secondary);
}

.modal-body { padding: 20px; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
}
.form-group textarea { min-height: 100px; resize: vertical; }

/* Full-screen visit form */
.modal.modal-fullscreen {
  max-width: 95vw;
  width: 95vw;
  max-height: 95vh;
  height: 95vh;
  display: flex;
  flex-direction: column;
}
.modal-fullscreen .modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}
.visit-form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}
.visit-form-left, .visit-form-right {
  display: flex;
  flex-direction: column;
  gap: 0;
}
@media (max-width: 768px) {
  .visit-form-grid { grid-template-columns: 1fr; }
}

/* Examination tabs */
.exam-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
  border-bottom: 2px solid var(--border);
  padding-bottom: 10px;
}
.exam-tab {
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  transition: all 0.15s;
  user-select: none;
}
.exam-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.exam-tab.enabled {
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 500;
}
.exam-tab.enabled.active {
  color: #fff;
}
.exam-panel { display: none; }
.exam-panel.active { display: block; }
.exam-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  flex-wrap: wrap;
  gap: 8px;
}
.exam-panel-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}
.exam-panel-actions button {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  cursor: pointer;
}
.exam-panel-actions button:hover { background: var(--border); }
.exam-findings { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.exam-finding { display: flex; align-items: center; gap: 8px; padding: 6px 12px; background: var(--surface); border-radius: var(--radius); font-size: 13px; border: 1px solid var(--border); }
.exam-finding input:checked + label { color: var(--primary); }
.exam-custom-add {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  align-items: center;
}
.exam-custom-add input {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}
.exam-custom-add button {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
  cursor: pointer;
}

/* Investigations Grid */
.investigations-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.inv-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: pointer;
}

.inv-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.15); }

.inv-preview {
  height: 150px;
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.inv-preview img { width: 100%; height: 100%; object-fit: cover; }

.inv-info { padding: 12px; }
.inv-type { font-size: 12px; color: var(--text-secondary); }
.inv-filename { font-size: 14px; font-weight: 500; margin: 4px 0; }
.inv-date { font-size: 12px; color: var(--text-secondary); }

/* Schedule List */
.schedule-list { display: flex; flex-direction: column; gap: 12px; }
.schedule-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius);
}
.schedule-time { font-weight: 600; min-width: 80px; }
.schedule-patient { flex: 1; }
.schedule-type { font-size: 12px; color: var(--text-secondary); }

/* Empty State */
.empty-state {
  padding: 40px;
  text-align: center;
  color: var(--text-secondary);
}

/* Settings */
.settings-card {
  background: var(--surface);
  padding: 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.settings-card h3 { margin-bottom: 16px; font-size: 16px; }

.status-msg { margin-top: 12px; font-size: 13px; }

/* Responsive */
@media (max-width: 768px) {
  .sidebar { position: fixed; left: -220px; top: 57px; height: calc(100vh - 57px); z-index: 50; transition: left 0.2s; }
  .sidebar.open { left: 0; }
  .content { padding: 16px; }
  .view-header { flex-direction: column; align-items: flex-start; gap: 12px; }
  .header-actions { width: 100%; flex-wrap: wrap; }
  .search-input { width: 100%; }
  .calendar-day { min-height: 60px; }
}

/* Autocomplete dropdown for patient search in bookings */
.autocomplete-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 240px;
  overflow-y: auto;
  z-index: 1000;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
}
.autocomplete-item {
  padding: 10px 14px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  border-bottom: 1px solid var(--border);
}
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover { background: var(--bg-secondary); }
.autocomplete-new:hover { background: rgba(37,99,235,0.06); }

/* Day bookings list in modal */
.day-bookings-list { display: flex; flex-direction: column; gap: 8px; }
.day-booking-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  gap: 12px;
  cursor: grab;
  border: 1px solid transparent;
  transition: border-color 0.15s;
}
.day-booking-item:hover { border-color: var(--border); }
.day-booking-item[draggable]:active { cursor: grabbing; opacity: 0.7; }
.day-booking-main { display: flex; gap: 12px; align-items: center; flex: 1; min-width: 0; }
.day-booking-time {
  font-size: 15px;
  font-weight: 600;
  font-family: var(--mono);
  min-width: 50px;
  color: var(--primary);
}
.day-booking-info { flex: 1; min-width: 0; }
.day-booking-actions { display: flex; gap: 6px; flex-shrink: 0; flex-wrap: wrap; }
@media (max-width: 600px) {
  .day-booking-item { flex-direction: column; align-items: flex-start; }
  .day-booking-actions { width: 100%; justify-content: flex-end; }
}
