:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --sidebar: #0f172a;
  --sidebar-ink: #cbd5e1;
  --sidebar-active: #1e293b;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 230px; background: var(--sidebar); color: var(--sidebar-ink);
  display: flex; flex-direction: column; position: fixed; top: 0; bottom: 0; left: 0;
}
.sidebar .brand {
  padding: 20px 18px; font-size: 16px; font-weight: 700; color: #fff;
  border-bottom: 1px solid #1e293b; display: flex; align-items: center; gap: 8px;
}
.sidebar .brand small { display:block; font-weight: 400; font-size: 11px; color: var(--sidebar-ink); }
.nav { flex: 1; padding: 10px 8px; overflow-y: auto; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin: 2px 0;
  border-radius: 8px; color: var(--sidebar-ink); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--sidebar-active); color: #fff; }
.nav a.active { background: var(--brand); color: #fff; }
.nav .nav-ico { width: 18px; text-align: center; opacity: .9; }
.userbox {
  padding: 14px 16px; border-top: 1px solid #1e293b; font-size: 13px;
}
.userbox .uname { color: #fff; font-weight: 600; }
.userbox .urole { color: var(--sidebar-ink); font-size: 12px; }
.userbox button {
  margin-top: 8px; width: 100%; padding: 7px; border: 1px solid #334155;
  background: transparent; color: var(--sidebar-ink); border-radius: 7px; cursor: pointer;
}
.userbox button:hover { background: var(--sidebar-active); color: #fff; }

.main { flex: 1; margin-left: 230px; min-width: 0; }
.topbar {
  background: var(--panel); border-bottom: 1px solid var(--line);
  padding: 16px 26px; display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 20;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.nav-toggle {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  width: 38px; height: 38px; border-radius: 8px; cursor: pointer; font-size: 18px; line-height: 1;
  flex: 0 0 auto;
}
.nav-toggle:hover { background: #f3f4f6; }
.topbar h1 { font-size: 19px; margin: 0; }

/* Collapsed sidebar (toggled via the ☰ button, state persisted) */
.sidebar-collapsed .sidebar { width: 64px; }
.sidebar-collapsed .sidebar .brand { justify-content: center; padding: 20px 0; }
.sidebar-collapsed .sidebar .brand small { display: none; }
.sidebar-collapsed .nav a { justify-content: center; padding: 10px 0; }
.sidebar-collapsed .nav a span:not(.nav-ico) { display: none; }
.sidebar-collapsed .userbox .uname,
.sidebar-collapsed .userbox .urole { display: none; }
.sidebar-collapsed .userbox button { font-size: 0; padding: 7px 0; }
.sidebar-collapsed .userbox button::before { content: "⎋"; font-size: 16px; }
.sidebar-collapsed .main { margin-left: 64px; }
.topbar .sub { color: var(--muted); font-size: 13px; }
.content { padding: 24px 26px; }

/* ---------- Cards / panels ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-bottom: 20px;
}
.panel-head {
  padding: 14px 18px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.panel-head h2 { font-size: 15px; margin: 0; }
.panel-head .count { color: var(--muted); font-size: 13px; font-weight: 500; }
.panel-body { padding: 16px 18px; }
.panel-body.p0 { padding: 0; }

/* KPI grid */
.kpis { display: grid; grid-template-columns: repeat(auto-fit,minmax(160px,1fr)); gap: 14px; margin-bottom: 20px; }
.kpi {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow);
}
.kpi .k-val { font-size: 26px; font-weight: 700; }
.kpi .k-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.kpi.accent { border-left: 4px solid var(--brand); }
.kpi.green { border-left: 4px solid #16a34a; }
.kpi.red { border-left: 4px solid #dc2626; }
.kpi.amber { border-left: 4px solid #d97706; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background:#fafbfd; }
tbody tr:hover { background: #f8fafc; }
td.actions { text-align: right; white-space: nowrap; }
td.editable:hover { background: #eef2ff; outline: 1px dashed #c7d2fe; }
td.editable select, td.editable input { padding: 5px 7px; font-size: 13px; }

/* Fixed-size cells for the Main Interface table (scrolls horizontally) */
#tbl { overflow-x: auto; }
#main-root table { table-layout: fixed; min-width: 1650px; }
#main-root th, #main-root td {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#main-root td.editable select, #main-root td.editable input { width: 100%; min-width: 0; }
#main-root td.editing { white-space: normal; overflow: visible; }
#main-root th:nth-child(1),  #main-root td:nth-child(1)  { width: 150px; } /* Name */
#main-root th:nth-child(2),  #main-root td:nth-child(2)  { width: 130px; } /* Phone */
#main-root th:nth-child(3),  #main-root td:nth-child(3)  { width: 110px; } /* Source */
#main-root th:nth-child(4),  #main-root td:nth-child(4)  { width: 110px; } /* Ad */
#main-root th:nth-child(5),  #main-root td:nth-child(5)  { width: 130px; } /* Service */
#main-root th:nth-child(6),  #main-root td:nth-child(6)  { width: 120px; } /* Doctor */
#main-root th:nth-child(7),  #main-root td:nth-child(7)  { width: 120px; } /* Status */
#main-root th:nth-child(8),  #main-root td:nth-child(8)  { width: 140px; } /* Appointment */
#main-root th:nth-child(9),  #main-root td:nth-child(9)  { width: 120px; } /* Next follow-up */
#main-root th:nth-child(10), #main-root td:nth-child(10) { width: 90px;  } /* Paid */
#main-root th:nth-child(11), #main-root td:nth-child(11) { width: 110px; } /* Review */
#main-root th:nth-child(12), #main-root td:nth-child(12) { width: 110px; } /* Test result */
#main-root th:nth-child(13), #main-root td:nth-child(13) { width: 200px; } /* Last note */
#main-root th:nth-child(14), #main-root td:nth-child(14) { width: 110px; } /* Open / Delete */

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  font-size: 13px; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:hover { background: #f3f4f6; text-decoration: none; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--brand); }
.btn-danger { color: #dc2626; border-color: #fecaca; }
.btn-danger:hover { background: #fef2f2; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.badge-blue { background: #dbeafe; color: #1e40af; }
.badge-cyan { background: #cffafe; color: #0e7490; }
.badge-green { background: #dcfce7; color: #166534; }
.badge-amber { background: #fef3c7; color: #92400e; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-purple { background: #ede9fe; color: #5b21b6; }
.badge-grey { background: #e5e7eb; color: #374151; }

/* ---------- Forms ---------- */
.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; color: #374151; }
.form-row-inline { display: flex; align-items: center; gap: 8px; }
.form-row-inline label { margin: 0; }
input, select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 14px; font-family: inherit; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
input[type=checkbox] { width: auto; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.toolbar input, .toolbar select { width: auto; min-width: 160px; }
.toolbar .grow { flex: 1; min-width: 200px; }

/* ---------- Checkbox dropdown ---------- */
.cdd { position: relative; }
.cdd-toggle {
  width: 100%; text-align: left; padding: 9px 11px; border: 1px solid var(--line);
  border-radius: 8px; background: #fff; cursor: pointer; font: inherit; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cdd-toggle::after { content: "▾"; float: right; color: var(--muted); margin-left: 6px; }
.cdd-panel {
  position: absolute; z-index: 60; top: calc(100% + 2px); left: 0; min-width: 100%;
  background: #fff; border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow);
  max-height: 220px; overflow: auto; padding: 6px;
}
.cdd-item { display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 6px; white-space: nowrap; cursor: pointer; }
.cdd-item:hover { background: #f3f4f6; }
.cdd-item input { width: auto; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center; padding: 50px 16px; overflow-y: auto;
}
.modal { background: #fff; border-radius: var(--radius); width: 480px; max-width: 100%; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal-wide { width: 760px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-x { border: none; background: none; font-size: 18px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #111827; color: #fff; padding: 11px 18px; border-radius: 10px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 200; box-shadow: var(--shadow);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast-success { background: #166534; }
.toast-error { background: #991b1b; }

/* ---------- Doctor schedule (day grid) ---------- */
.sched-table { min-width: 100%; }
.sched-table th, .sched-table td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.sched-table thead th { position: sticky; top: 0; background: #f1f5f9; z-index: 1; }
.sched-table td.tcol { font-weight: 700; white-space: nowrap; width: 70px; color: #475569; background: #fafbfd; text-align: center; }
.sched-table td.cell { cursor: pointer; vertical-align: top; min-width: 150px; }
.sched-table td.na { color: #cbd5e1; text-align: center; }
.sched-table td.free { color: #94a3b8; font-size: 12px; }
.sched-table td.free:hover { background: #eef2ff; color: var(--brand); }
.sched-table td.booked { background: #ecfdf5; }
.sched-table td.booked.pending { background: #fffbeb; }
.sched-table td.booked:hover { filter: brightness(0.97); }
.sched-table .cust { font-weight: 600; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }
.sched-table .svc { font-size: 11px; color: var(--muted); }

/* ---------- Misc ---------- */
.spinner { padding: 40px; text-align: center; color: var(--muted); }
.empty { padding: 36px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 700; margin: 0 0 10px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.tab { padding: 9px 14px; cursor: pointer; border-bottom: 2px solid transparent; font-weight: 600; color: var(--muted); }
.tab.active { color: var(--brand); border-bottom-color: var(--brand); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.field { margin-bottom: 10px; }
.field .lbl { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.field .val { font-weight: 500; }
.timeline-item { padding: 10px 0; border-bottom: 1px dashed var(--line); }
.timeline-item .meta { font-size: 12px; color: var(--muted); }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg,#0f172a,#1e3a8a); padding: 20px; }
.login-card { background: #fff; border-radius: 16px; padding: 36px; width: 380px; max-width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p.sub { margin: 0 0 22px; color: var(--muted); font-size: 13px; }
.login-card .btn { width: 100%; justify-content: center; margin-top: 6px; }
.login-err { background: #fef2f2; color: #991b1b; padding: 9px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; display: none; }

@media (max-width: 820px) {
  .sidebar { width: 64px; }
  .sidebar .brand small, .nav a span, .userbox .uname, .userbox .urole, .userbox button { display: none; }
  .main { margin-left: 64px; }
  .form-grid, .grid-2 { grid-template-columns: 1fr; }
}
