/* ============================================================
   CarePoint Admin — Light Theme Dashboard
   Adapted from madeonmerit admin patterns (vanilla CSS)
   ============================================================ */

:root {
  --bg: #f8f7f6;
  --surface: #ffffff;
  --surface-hover: #fafaf9;
  --border: #e5e2de;
  --border-light: #f0eeeb;
  --text: #1a1a1a;
  --text-muted: #71717a;
  --text-light: #a1a1aa;
  --primary: #3366ff;
  --primary-light: #dbeafe;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --success-light: #dcfce7;
  --warning: #ca8a04;
  --warning-light: #fef9c3;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --info: #0891b2;
  --info-light: #cffafe;
  --purple: #7c3aed;
  --purple-light: #ede9fe;
  --pink: #db2777;
  --pink-light: #fce7f3;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --sidebar-w: 220px;
  --header-h: 48px;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Header ---- */
.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 20;
}
.header-left { display: flex; align-items: center; gap: 10px; }
.header-right { display: flex; align-items: center; gap: 8px; }
.header-logo { font-size: 17px; font-weight: 700; color: var(--primary); }
.header-sub { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.sidebar-toggle { background: none; border: none; cursor: pointer; color: var(--text-muted); padding: 4px; border-radius: 4px; }
.sidebar-toggle:hover { background: var(--border-light); color: var(--text); }

/* ---- Layout ---- */
.admin-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ---- Sidebar ---- */
.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 8px;
  flex-shrink: 0;
  transition: width 0.2s ease;
}
.sidebar.collapsed { width: 52px; }
.sidebar.collapsed .nav-label, .sidebar.collapsed .nav-heading { display: none; }
.sidebar.collapsed .nav-item { justify-content: center; padding: 8px; }

.nav-section { margin-bottom: 8px; }
.nav-heading {
  padding: 6px 10px 4px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-light);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  text-decoration: none;
  transition: all 0.15s;
}
.nav-item:hover { background: var(--border-light); color: var(--text); }
.nav-item.active { background: var(--primary-light); color: var(--primary-dark); }
.nav-item svg { flex-shrink: 0; }

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

.page { animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.page-title { font-size: 22px; font-weight: 700; margin-bottom: 20px; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.page-header .page-title { margin-bottom: 0; }
.section-title { font-size: 13px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; margin: 20px 0 8px; }

/* ---- Cards ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow);
}

/* ---- Metrics Grid ---- */
.metrics-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.metric-card {
  padding: 16px;
  border-radius: var(--radius-lg);
  border: 1px solid;
  transition: box-shadow 0.2s;
}
.metric-card:hover { box-shadow: var(--shadow-md); }
.metric-label { font-size: 12px; font-weight: 600; margin-bottom: 4px; }
.metric-value { font-size: 28px; font-weight: 700; line-height: 1.1; }
.metric-sub { font-size: 11px; margin-top: 4px; }

.metric-blue { background: var(--primary-light); border-color: #bfdbfe; }
.metric-blue .metric-value { color: var(--primary-dark); }
.metric-blue .metric-label, .metric-blue .metric-sub { color: #1e40af; }

.metric-green { background: var(--success-light); border-color: #bbf7d0; }
.metric-green .metric-value { color: var(--success); }
.metric-green .metric-label, .metric-green .metric-sub { color: #166534; }

.metric-red { background: var(--danger-light); border-color: #fecaca; }
.metric-red .metric-value { color: var(--danger); }
.metric-red .metric-label, .metric-red .metric-sub { color: #991b1b; }

.metric-purple { background: var(--purple-light); border-color: #ddd6fe; }
.metric-purple .metric-value { color: var(--purple); }
.metric-purple .metric-label, .metric-purple .metric-sub { color: #5b21b6; }

.metric-yellow { background: var(--warning-light); border-color: #fde68a; }
.metric-yellow .metric-value { color: var(--warning); }
.metric-yellow .metric-label, .metric-yellow .metric-sub { color: #854d0e; }

/* ---- Two Column ---- */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }

/* ---- Tables ---- */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  text-align: left;
  padding: 8px 12px;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
}
.data-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: top;
}
.data-table tr:hover { background: var(--surface-hover); }
.data-table tr.expandable { cursor: pointer; }
.data-table tr.detail-row { background: #fafaf8; }
.data-table tr.detail-row td { padding: 12px 16px; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.detail-label { font-size: 11px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; }
.detail-value { font-size: 13px; margin-top: 2px; }

/* ---- Badges ---- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.badge-mock { background: #fef3c7; color: #92400e; }
.badge-er { background: var(--danger-light); color: var(--danger); }
.badge-urgent_care { background: #fff7ed; color: #c2410c; }
.badge-clinic { background: var(--success-light); color: var(--success); }
.badge-virtual { background: var(--info-light); color: var(--info); }
.badge-pharmacy { background: var(--purple-light); color: var(--purple); }
.badge-self_care { background: #f4f4f5; color: #52525b; }
.badge-mental_health_crisis { background: var(--pink-light); color: var(--pink); }
.badge-active { background: var(--success-light); color: var(--success); }
.badge-completed { background: var(--primary-light); color: var(--primary); }
.badge-rerouted { background: #fff7ed; color: #c2410c; }
.badge-emergency { background: var(--danger-light); color: var(--danger); }
.badge-acute { background: #fff7ed; color: #c2410c; }
.badge-chronic { background: var(--info-light); color: var(--info); }
.badge-mental_health { background: var(--pink-light); color: var(--pink); }

/* ---- Load Bars ---- */
.load-bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
}
.load-bar-row + .load-bar-row { border-top: 1px solid var(--border-light); }
.lb-name { width: 200px; font-size: 12px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-track { flex: 1; height: 10px; background: #f0eeeb; border-radius: 5px; overflow: hidden; }
.lb-fill { height: 100%; border-radius: 5px; transition: width 0.5s ease, background 0.3s; }
.lb-fill.low { background: var(--success); }
.lb-fill.med { background: var(--warning); }
.lb-fill.high { background: var(--danger); }
.lb-pct { width: 40px; text-align: right; font-size: 12px; font-weight: 600; }
.lb-wait { width: 55px; text-align: right; font-size: 11px; color: var(--text-muted); }

/* ---- Patient Cards ---- */
.patient-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.patient-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  cursor: pointer;
  transition: box-shadow 0.2s, border-color 0.2s;
}
.patient-card:hover { box-shadow: var(--shadow-md); border-color: var(--primary); }
.pc-name { font-size: 14px; font-weight: 600; }
.pc-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.pc-conditions { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.pc-barriers { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }
.barrier-tag { font-size: 9px; padding: 1px 6px; border-radius: 8px; background: #fef3c7; color: #92400e; }

/* ---- Facility Grid ---- */
.facility-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.facility-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow);
  transition: box-shadow 0.2s;
}
.facility-card:hover { box-shadow: var(--shadow-md); }
.fc-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.fc-name { font-size: 14px; font-weight: 600; }
.fc-address { font-size: 11px; color: var(--text-muted); margin-bottom: 8px; }
.fc-load { margin: 8px 0; }
.fc-load-track { height: 8px; background: #f0eeeb; border-radius: 4px; overflow: hidden; }
.fc-load-fill { height: 100%; border-radius: 4px; transition: width 0.5s; }
.fc-load-label { display: flex; justify-content: space-between; font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.fc-services { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 8px; }
.service-tag { font-size: 9px; padding: 2px 6px; border-radius: 8px; background: var(--primary-light); color: var(--primary-dark); }
.fc-footer { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; font-size: 12px; color: var(--text-muted); }

/* ---- Session Feed ---- */
.session-feed-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  font-size: 12px;
}
.session-feed-item + .session-feed-item { border-top: 1px solid var(--border-light); }
.sf-complaint { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-right: 8px; }
.sf-confidence { font-size: 11px; color: var(--text-muted); margin-right: 8px; }

/* ---- Analytics Bars ---- */
.bar-chart-item { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.bar-chart-item + .bar-chart-item { border-top: 1px solid var(--border-light); }
.bc-label { width: 120px; font-size: 12px; font-weight: 500; }
.bc-track { flex: 1; height: 24px; background: #f0eeeb; border-radius: 4px; overflow: hidden; position: relative; }
.bc-fill { height: 100%; border-radius: 4px; display: flex; align-items: center; justify-content: flex-end; padding-right: 6px; transition: width 0.5s; }
.bc-fill span { font-size: 11px; font-weight: 600; color: #fff; }
.bc-count { width: 40px; text-align: right; font-size: 12px; font-weight: 600; }

/* ---- Conversation Log ---- */
.convo-log { max-height: 300px; overflow-y: auto; }
.convo-msg { padding: 6px 10px; margin: 4px 0; border-radius: 8px; font-size: 13px; line-height: 1.4; }
.convo-patient { background: var(--primary-light); color: var(--primary-dark); margin-left: 40px; }
.convo-carepoint { background: #f4f4f5; color: var(--text); margin-right: 40px; }
.convo-role { font-size: 10px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); margin-bottom: 2px; }

/* ---- Routing Embed ---- */
.routing-embed { height: calc(100vh - 120px); }
.routing-embed iframe { width: 100%; height: 100%; border: none; border-radius: var(--radius-lg); }

/* ---- Filters ---- */
.filters select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  background: var(--surface);
  color: var(--text);
}

/* ---- Buttons ---- */
.btn { padding: 6px 14px; border: none; border-radius: var(--radius); cursor: pointer; font-size: 13px; font-weight: 500; transition: all 0.15s; text-decoration: none; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text-muted); }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }

/* ---- Empty State ---- */
.empty { text-align: center; padding: 32px; color: var(--text-muted); font-size: 13px; }

/* ---- Responsive ---- */
@media (max-width: 1024px) {
  .metrics-grid { grid-template-columns: repeat(2, 1fr); }
  .facility-grid { grid-template-columns: repeat(2, 1fr); }
  .two-col { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .sidebar { display: none; }
  .sidebar.mobile-open { display: block; position: fixed; top: var(--header-h); left: 0; bottom: 0; z-index: 30; box-shadow: var(--shadow-md); }
  .metrics-grid { grid-template-columns: 1fr 1fr; }
  .facility-grid { grid-template-columns: 1fr; }
  .patient-cards { grid-template-columns: 1fr; }
}

/* ---- AI Assistant Drawer ---- */
.assistant-drawer {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 420px;
  max-width: 100vw;
  height: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  z-index: 50;
  transform: translateY(100%);
  transition: transform 0.3s ease;
}
.assistant-drawer.open { transform: translateY(0); }

.assistant-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, #3366ff10, #7c3aed10);
  border-radius: 12px 12px 0 0;
}

.assistant-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.assistant-msg {
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  max-width: 90%;
  animation: fadeIn 0.2s ease;
}
.msg-user {
  background: var(--primary);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}
.msg-assistant {
  background: #f4f4f5;
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}
.msg-assistant code { background: #e4e4e7; padding: 1px 4px; border-radius: 3px; font-size: 12px; }
.msg-loading { color: var(--text-muted); font-style: italic; }

.typing-dots { display: inline-flex; gap: 4px; }
.typing-dots span {
  width: 6px; height: 6px; background: var(--text-muted);
  border-radius: 50%; animation: typingDot 1.4s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingDot {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

.assistant-input-area {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}
.assistant-input-area input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
}
.assistant-input-area input:focus { border-color: var(--primary); }

/* ---- Integrations Grid ---- */
.integrations-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 14px; }
.integration-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow);
  transition: box-shadow 0.2s;
}
.integration-card:hover { box-shadow: var(--shadow-md); }
.ic-header { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.ic-icon { width: 36px; height: 36px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 18px; }
.ic-name { font-size: 15px; font-weight: 600; }
.ic-desc { font-size: 12px; color: var(--text-muted); margin-bottom: 12px; line-height: 1.4; }
.ic-status { font-size: 12px; font-weight: 600; }
.ic-status.connected { color: var(--success); }
.ic-status.not-configured { color: var(--text-muted); }

/* ---- Form Inputs ---- */
.form-input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  background: var(--surface);
}
.form-input:focus { outline: none; border-color: var(--primary); }

/* ---- Integration Forms ---- */
.form-label { display: block; font-size: 12px; font-weight: 600; color: var(--text-muted); margin-bottom: 4px; }
.int-setup { margin-top: 8px; }
.int-setup summary { font-size: 12px; font-weight: 600; color: var(--primary); cursor: pointer; user-select: none; }
.int-setup summary:hover { color: var(--primary-dark); }
.setup-steps { padding-left: 20px; margin: 8px 0; font-size: 12px; color: var(--text-muted); line-height: 1.8; }
.setup-steps a { color: var(--primary); }
.setup-steps code { background: var(--border-light); padding: 1px 5px; border-radius: 3px; font-size: 11px; }
.int-note { margin-top: 8px; font-size: 11px; color: var(--text-light); font-style: italic; }
