/**
 * Kalpak EMR — Premium SaaS presentation layer
 * Replaces AdminLTE visual language. AdminLTE/Bootstrap 4 kept for JS hooks only.
 */

/* ==========================================================================
   DESIGN TOKENS
   ========================================================================== */
:root {
  --emr-font: "Inter", system-ui, sans-serif;
  --emr-primary: #2563eb;
  --emr-primary-hover: #1d4ed8;
  --emr-primary-muted: #eff6ff;
  --emr-secondary: #64748b;
  --emr-success: #059669;
  --emr-success-muted: #ecfdf5;
  --emr-warning: #d97706;
  --emr-warning-muted: #fffbeb;
  --emr-danger: #dc2626;
  --emr-danger-muted: #fef2f2;
  --emr-info: #0284c7;
  --emr-bg: #f4f6f9;
  --emr-surface: #ffffff;
  --emr-surface-raised: #ffffff;
  --emr-border: #e2e8f0;
  --emr-border-strong: #cbd5e1;
  --emr-text: #0f172a;
  --emr-text-secondary: #475569;
  --emr-text-muted: #94a3b8;
  --emr-nav: #0c1222;
  --emr-nav-hover: #1a2338;
  --emr-nav-active: #2563eb;
  --emr-radius: 12px;
  --emr-radius-sm: 8px;
  --emr-radius-xs: 6px;
  --emr-shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --emr-shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 8px 24px rgba(15, 23, 42, 0.06);
  --emr-shadow-lg: 0 16px 48px rgba(15, 23, 42, 0.12);
  --emr-space-1: 0.25rem;
  --emr-space-2: 0.5rem;
  --emr-space-3: 0.75rem;
  --emr-space-4: 1rem;
  --emr-space-5: 1.25rem;
  --emr-space-6: 1.5rem;
  --emr-space-8: 2rem;
  --emr-sidebar-w: 260px;
  --emr-topbar-h: 60px;
  --emr-content-max: 1680px;
}

/* ==========================================================================
   ADMINLTE VISUAL RESET (keep structure, hide skin)
   ========================================================================== */
/* Document root owns page scrollbars (not DataTables / inner wrappers) */
html {
  overflow-x: scroll;
  overflow-y: scroll;
  scrollbar-gutter: stable;
}

body.emr-app {
  font-family: var(--emr-font) !important;
  background: var(--emr-bg) !important;
  color: var(--emr-text);
  font-size: 0.9375rem;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow: visible;
  min-height: 100%;
  height: auto;
}

/* Override body.kp-app { overflow-x: hidden } from kalpak-design-system.css */
body.emr-app.kp-app {
  overflow: visible;
}

body.emr-app .wrapper {
  background: var(--emr-bg);
  overflow-x: visible;
  overflow-y: visible;
  height: auto;
  min-height: 0;
}

body.emr-app .content-wrapper {
  background: var(--emr-bg) !important;
  margin-left: 0 !important;
  min-height: calc(100vh - var(--emr-topbar-h));
  height: auto !important;
  max-height: none !important;
  overflow-x: visible !important;
  overflow-y: visible !important;
  padding: 0 !important;
  transition: margin-left 0.3s ease-in-out;
}

body.emr-app .kp-app-content,
body.emr-app .emr-app-content {
  overflow-x: visible;
  overflow-y: visible;
  height: auto;
  max-height: none;
}

body.emr-app .kp-page,
body.emr-app .emr-page {
  overflow-x: visible;
  max-width: none;
}

/* Sidebar width + content offset — must not use !important on base .main-sidebar (breaks collapse/hover) */
@media (min-width: 992px) {
  body.emr-app.sidebar-mini:not(.sidebar-collapse) .main-sidebar,
  body.emr-app.sidebar-mini:not(.sidebar-collapse) .main-sidebar::before {
    width: var(--emr-sidebar-w);
  }

  body.emr-app.sidebar-mini:not(.sidebar-collapse) .content-wrapper,
  body.emr-app.sidebar-mini:not(.sidebar-collapse) .main-footer {
    margin-left: var(--emr-sidebar-w) !important;
  }

  body.emr-app.sidebar-mini.sidebar-collapse .main-sidebar,
  body.emr-app.sidebar-mini.sidebar-collapse .main-sidebar::before {
    width: 4.6rem;
  }

  body.emr-app.sidebar-mini.sidebar-collapse .content-wrapper,
  body.emr-app.sidebar-mini.sidebar-collapse .main-footer {
    margin-left: 4.6rem !important;
  }

  body.emr-app.sidebar-mini.sidebar-collapse .main-sidebar:not(.sidebar-no-expand):hover,
  body.emr-app.sidebar-mini.sidebar-collapse .main-sidebar:not(.sidebar-no-expand).sidebar-focused {
    width: var(--emr-sidebar-w) !important;
    z-index: 1038;
    box-shadow: var(--emr-shadow-lg);
  }
}

body.emr-app .main-footer {
  margin-left: 0;
  background: transparent;
  border: none;
  color: var(--emr-text-muted);
  font-size: 0.75rem;
  padding: var(--emr-space-4);
  transition: margin-left 0.3s ease-in-out;
}

body.emr-app .elevation-1,
body.emr-app .elevation-2,
body.emr-app .elevation-3,
body.emr-app .elevation-4 {
  box-shadow: none !important;
}

/* Kill AdminLTE small-box / info-box candy look globally */
body.emr-app .small-box {
  border-radius: var(--emr-radius);
  box-shadow: var(--emr-shadow);
  border: 1px solid var(--emr-border);
  background: var(--emr-surface) !important;
  color: var(--emr-text) !important;
  position: relative;
  overflow: hidden;
}

body.emr-app .small-box::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--emr-primary);
}

body.emr-app .small-box .inner h3 {
  color: var(--emr-text);
  font-weight: 700;
}

body.emr-app .small-box .inner p {
  color: var(--emr-text-secondary);
}

body.emr-app .small-box .icon {
  color: var(--emr-primary-muted) !important;
  opacity: 1;
}

body.emr-app .small-box-footer {
  background: var(--emr-bg) !important;
  color: var(--emr-primary) !important;
  border-top: 1px solid var(--emr-border);
}

body.emr-app .info-box {
  background: var(--emr-surface);
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius);
  box-shadow: var(--emr-shadow-sm);
}

/* Legacy content-header / content sections */
body.emr-app .content-header {
  background: var(--emr-surface);
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius);
  padding: var(--emr-space-5) var(--emr-space-6);
  margin-bottom: var(--emr-space-6);
  box-shadow: var(--emr-shadow-sm);
}

body.emr-app section.content {
  padding: 0;
}

/* ==========================================================================
   PREMIUM SIDEBAR (.emr-nav on .main-sidebar)
   ========================================================================== */
body.emr-app .main-sidebar {
  background: var(--emr-nav) !important;
  border-right: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: none !important;
  position: fixed;
  left: 0;
  top: var(--emr-topbar-h);
  height: calc(100vh - var(--emr-topbar-h));
  z-index: 1030;
  transition: width 0.3s ease-in-out;
}

body.emr-app .main-sidebar .brand-link {
  display: flex;
  align-items: center;
  gap: var(--emr-space-3);
  padding: var(--emr-space-5) var(--emr-space-4);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: transparent !important;
}

body.emr-app .emr-nav__logo {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: #fff;
  font-weight: 700;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  letter-spacing: -0.03em;
}

body.emr-app .main-sidebar .brand-text {
  font-weight: 600 !important;
  font-size: 0.9375rem !important;
  color: #f8fafc !important;
  letter-spacing: -0.01em;
}

body.emr-app .main-sidebar .sidebar {
  padding: var(--emr-space-3) var(--emr-space-2);
  height: calc(100% - 72px);
  overflow-y: auto;
}

body.emr-app .nav-sidebar > .nav-item > .nav-link {
  border-radius: var(--emr-radius-sm);
  margin: 2px 0;
  padding: 0.6rem 0.75rem;
  color: #94a3b8;
  font-weight: 500;
  font-size: 0.875rem;
  transition: background 0.12s ease, color 0.12s ease;
}

body.emr-app .nav-sidebar > .nav-item > .nav-link p {
  margin: 0;
}

body.emr-app .nav-sidebar > .nav-item > .nav-link:hover {
  background: var(--emr-nav-hover) !important;
  color: #e2e8f0;
}

body.emr-app .nav-sidebar > .nav-item > .nav-link.active {
  background: rgba(37, 99, 235, 0.2) !important;
  color: #fff !important;
  box-shadow: inset 3px 0 0 var(--emr-nav-active);
}

body.emr-app .nav-sidebar .nav-icon {
  width: 1.35rem;
  font-size: 0.9rem;
  margin-right: 0.5rem;
  color: inherit;
}

/* ==========================================================================
   PREMIUM TOP BAR
   ========================================================================== */
body.emr-app .main-header.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--emr-topbar-h);
  min-height: var(--emr-topbar-h);
  z-index: 1040;
  margin: 0 !important;
  border: none !important;
  border-bottom: 1px solid var(--emr-border) !important;
  background: var(--emr-surface) !important;
  box-shadow: var(--emr-shadow-sm) !important;
  padding: 0 var(--emr-space-4) !important;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
}

body.emr-app .main-header .navbar-nav {
  flex-direction: row;
  align-items: center;
}

body.emr-app .main-header .navbar-nav .nav-item {
  display: flex;
  align-items: center;
}

body.emr-app .main-header .navbar-brand {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--emr-text) !important;
}

body.emr-app .main-header .nav-link.emr-topbar__menu-btn {
  width: 40px;
  height: 40px;
  padding: 0 !important;
  margin: 0;
  line-height: 1;
  border-radius: var(--emr-radius-sm);
  border: 1px solid var(--emr-border);
  background: var(--emr-bg);
  color: var(--emr-text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  align-self: center;
  transition: background 0.12s ease;
}

body.emr-app .main-header .nav-link.emr-topbar__menu-btn i {
  line-height: 1;
  margin: 0;
}

body.emr-app .main-header .nav-link.emr-topbar__menu-btn:hover {
  background: var(--emr-primary-muted);
  color: var(--emr-primary);
}

body.emr-app .emr-topbar__clinic select {
  min-width: 160px;
  border-radius: var(--emr-radius-sm);
  border: 1px solid var(--emr-border);
  font-size: 0.8125rem;
  font-weight: 500;
}

body.emr-app .emr-topbar__actions .btn,
body.emr-app .emr-topbar__actions .form-control-sm {
  border-radius: var(--emr-radius-sm);
}

body.emr-app .navbar-dark-mode {
  background: #0f172a !important;
  border-bottom-color: #1e293b !important;
}

body.emr-app .wrapper {
  padding-top: var(--emr-topbar-h);
}

/* ==========================================================================
   APP CONTENT SHELL
   ========================================================================== */
body.emr-app .kp-app-content,
body.emr-app .emr-app-content {
  max-width: var(--emr-content-max);
  margin: 0 auto;
  padding: var(--emr-space-6) var(--emr-space-6) var(--emr-space-8);
  width: 100%;
}

/* Wide list tables must not be capped by the content max-width column */
body.emr-app .kp-app-content:has(.dataTables_wrapper),
body.emr-app .kp-app-content:has(table.dataTable),
body.emr-app .emr-app-content:has(.dataTables_wrapper),
body.emr-app .emr-app-content:has(table.dataTable) {
  max-width: none;
}

@media (max-width: 991.98px) {
  /* Lock page scroll while mobile drawer menu is open */
  body.emr-app.sidebar-open {
    overflow: hidden;
    height: 100%;
  }

  html:has(body.emr-app.sidebar-open) {
    overflow: hidden;
  }

  body.emr-app .content-wrapper {
    margin-left: 0 !important;
    min-height: 0 !important;
    height: auto !important;
    overflow: visible !important;
  }

  body.emr-app .wrapper {
    overflow: visible;
  }

  body.emr-app .main-sidebar {
    top: var(--emr-topbar-h);
    bottom: 0;
    height: auto;
    width: min(var(--emr-sidebar-w), 88vw) !important;
    z-index: 1041;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  body.emr-app .main-sidebar .brand-link {
    flex-shrink: 0;
  }

  body.emr-app .main-sidebar .sidebar {
    flex: 1 1 auto;
    min-height: 0;
    height: auto !important;
    overflow-y: auto !important;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  body.emr-app .kp-app-content,
  body.emr-app .emr-app-content {
    padding: var(--emr-space-4);
  }

  /* Worklist (non-DataTable): page scroll only on mobile too */
  .emr-worklist-scroll,
  .emr-worklist-table .emr-table-wrap {
    overflow: visible !important;
    max-height: none !important;
  }
}

@media (max-width: 767.98px) {
  body.emr-app .kp-app-content,
  body.emr-app .emr-app-content {
    padding: var(--emr-space-3);
  }
}

/* ==========================================================================
   PAGE HEADER
   ========================================================================== */
.emr-page-header {
  margin-bottom: var(--emr-space-6);
}

.emr-breadcrumb {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  list-style: none;
  padding: 0;
  margin: 0 0 var(--emr-space-3);
  font-size: 0.8125rem;
}

.emr-breadcrumb__item {
  color: var(--emr-text-muted);
}

.emr-breadcrumb__item a {
  color: var(--emr-text-secondary);
  text-decoration: none;
}

.emr-breadcrumb__item a:hover {
  color: var(--emr-primary);
}

.emr-breadcrumb__item + .emr-breadcrumb__item::before {
  content: "/";
  margin-right: 0.35rem;
  color: var(--emr-border-strong);
}

.emr-page-header__row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--emr-space-4);
}

.emr-page-header__eyebrow {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--emr-text-muted);
  margin: 0 0 var(--emr-space-1);
}

.emr-page-header__title {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin: 0;
  color: var(--emr-text);
  line-height: 1.25;
}

.emr-page-header__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--emr-space-2);
  align-items: center;
}

/* ==========================================================================
   SURFACE (replaces AdminLTE card panels)
   ========================================================================== */
.emr-surface,
body.emr-app .card {
  background: var(--emr-surface);
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius);
  box-shadow: var(--emr-shadow-sm);
  margin-bottom: var(--emr-space-6);
  overflow: hidden;
}

/* List/DataTable surfaces: grow with table; page scroll only */
body.emr-app .emr-surface:has(.emr-surface__body--table),
body.emr-app .emr-surface:has(.emr-table-wrap),
body.emr-app .emr-surface:has(table.dataTable) {
  overflow: visible;
}

.emr-surface__head,
body.emr-app .card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--emr-space-4) var(--emr-space-5);
  border-bottom: 1px solid var(--emr-border);
  background: var(--emr-surface);
}

.emr-surface__title,
body.emr-app .card-title {
  font-size: 0.9375rem;
  font-weight: 600;
  margin: 0;
  color: var(--emr-text);
  letter-spacing: -0.01em;
}

.emr-surface__body,
body.emr-app .card-body {
  padding: var(--emr-space-5);
}

.emr-surface__body--flush {
  padding: 0;
}

.emr-surface__body--table {
  padding: var(--emr-space-3) var(--emr-space-4) var(--emr-space-4);
}

body.emr-app .card-primary.card-outline,
body.emr-app .card-outline {
  border-top: none;
  border-left: none;
  border-right: none;
}

body.emr-app .card-outline.card-primary .card-header {
  border-top: 3px solid var(--emr-primary);
}

/* ==========================================================================
   PATIENT HERO
   ========================================================================== */
.emr-patient-hero {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: var(--emr-space-5);
  align-items: center;
  padding: var(--emr-space-6);
  background: var(--emr-surface);
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius);
  box-shadow: var(--emr-shadow);
  margin-bottom: var(--emr-space-6);
}

.emr-patient-hero__avatar {
  width: 72px;
  height: 72px;
  border-radius: 16px;
  object-fit: cover;
  border: 2px solid var(--emr-border);
  background: var(--emr-bg);
}

.emr-patient-hero__avatar--placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.75rem;
  color: var(--emr-text-muted);
  background: var(--emr-primary-muted);
}

.emr-patient-hero__name {
  font-size: 1.375rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin: 0 0 var(--emr-space-1);
}

.emr-patient-hero__meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--emr-space-2);
  font-size: 0.8125rem;
  color: var(--emr-text-secondary);
}

.emr-patient-hero__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--emr-space-2);
}

.emr-stat-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.6rem;
  background: var(--emr-bg);
  border-radius: 999px;
  font-weight: 500;
}

/* ==========================================================================
   KPI GRID
   ========================================================================== */
.emr-kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--emr-space-4);
  margin-bottom: var(--emr-space-6);
}

.emr-kpi {
  padding: var(--emr-space-5);
  background: var(--emr-surface);
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius);
  box-shadow: var(--emr-shadow-sm);
  position: relative;
  overflow: hidden;
}

.emr-kpi::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--emr-kpi-accent, var(--emr-primary));
}

.emr-kpi__value {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
  margin: 0 0 var(--emr-space-1);
}

.emr-kpi__label {
  font-size: 0.875rem;
  color: var(--emr-text-secondary);
  margin: 0;
}

/* ==========================================================================
   TABLES
   ========================================================================== */
body.emr-app {
  --emr-table-head-bg: #f8fafc;
  --emr-table-row-bg: var(--emr-surface);
  --emr-table-stripe-bg: rgba(248, 250, 252, 0.85);
  --emr-table-hover-bg: var(--emr-primary-muted);
}

.emr-table-wrap {
  border-radius: var(--emr-radius-sm);
  overflow: visible;
}

/* DataTables + list tables: grow with content; page/document scroll only */
body.emr-app .emr-surface__body--table,
body.emr-app .emr-surface__body--table > .emr-table-wrap,
body.emr-app .emr-surface__body--table .emr-table-wrap,
body.emr-app .emr-surface .emr-table-wrap,
body.emr-app .emr-surface__body--table .dataTables_wrapper,
body.emr-app .emr-surface .dataTables_wrapper,
body.emr-app .kp-page .dataTables_wrapper,
body.emr-app .emr-page .dataTables_wrapper,
body.emr-app .dataTables_wrapper .dataTables_scroll,
body.emr-app .dataTables_wrapper .dataTables_scrollHead,
body.emr-app .dataTables_wrapper .dataTables_scrollHeadInner,
body.emr-app .dataTables_wrapper .dataTables_scrollBody,
body.emr-app .dataTables_wrapper .dataTables_scrollFoot,
body.emr-app .dataTables_wrapper .table-responsive,
body.emr-app .emr-surface__body--table .table-responsive {
  overflow: visible !important;
  max-height: none !important;
  height: auto !important;
}

/* Let wide tables exceed viewport; horizontal scroll on html */
body.emr-app .emr-surface .emr-table-wrap,
body.emr-app .emr-surface__body--table .emr-table-wrap,
body.emr-app .kp-page .emr-table-wrap,
body.emr-app .emr-surface .dataTables_wrapper,
body.emr-app .emr-surface__body--table .dataTables_wrapper,
body.emr-app .kp-page .dataTables_wrapper,
body.emr-app .emr-page .dataTables_wrapper {
  width: max-content;
  min-width: 100%;
  max-width: none;
}

body.emr-app .emr-surface table.dataTable,
body.emr-app .emr-surface__body--table table.dataTable,
body.emr-app .emr-surface .emr-table-wrap > table,
body.emr-app .kp-page table.dataTable,
body.emr-app .kp-page table.emr-table.w-100,
body.emr-app .emr-page table.dataTable.w-100 {
  width: max-content !important;
  min-width: 100%;
  max-width: none;
  table-layout: auto !important;
}

body.emr-app .emr-surface table.dataTable td,
body.emr-app .emr-surface table.dataTable th,
body.emr-app .emr-surface__body--table table.dataTable td,
body.emr-app .emr-surface__body--table table.dataTable th,
body.emr-app .kp-page table.dataTable td,
body.emr-app .kp-page table.dataTable th {
  white-space: nowrap;
}

body.emr-app .dataTables_wrapper .dataTables_scrollBody {
  overflow: visible !important;
}

body.emr-app .table-responsive:not(.emr-clinical-form .table-responsive) {
  border-radius: var(--emr-radius-sm);
  overflow: visible !important;
}

body.emr-app .table,
body.emr-app table.dataTable {
  margin-bottom: 0;
  color: var(--emr-text);
  background-color: var(--emr-table-row-bg);
}

body.emr-app .table thead th,
body.emr-app table.dataTable thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--emr-table-head-bg) !important;
  border-bottom: 2px solid var(--emr-border) !important;
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--emr-text-muted);
  padding: 0.75rem 1rem;
  white-space: nowrap;
}

body.emr-app .table td,
body.emr-app .table th,
body.emr-app table.dataTable td,
body.emr-app table.dataTable th {
  padding: 0.7rem 1rem;
  vertical-align: middle;
  border-color: var(--emr-border) !important;
  font-size: 0.875rem;
  color: var(--emr-text);
}

body.emr-app .table-striped tbody tr:nth-of-type(odd),
body.emr-app table.dataTable.table-striped tbody tr:nth-of-type(odd),
body.emr-app table.dataTable.table-striped > tbody > tr:nth-of-type(odd) {
  background-color: var(--emr-table-stripe-bg) !important;
}

body.emr-app .table-striped tbody tr:nth-of-type(even),
body.emr-app table.dataTable.table-striped tbody tr:nth-of-type(even),
body.emr-app table.dataTable.table-striped > tbody > tr:nth-of-type(even) {
  background-color: var(--emr-table-row-bg) !important;
}

body.emr-app .table-hover tbody tr:hover,
body.emr-app table.dataTable.table-hover tbody tr:hover {
  background: var(--emr-table-hover-bg) !important;
  color: var(--emr-text);
}

/* DataTables */
body.emr-app .dataTables_wrapper .dataTables_filter input,
body.emr-app .dataTables_wrapper .dataTables_length select {
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius-sm);
  padding: 0.35rem 0.6rem;
  background: var(--emr-surface);
  color: var(--emr-text);
}

body.emr-app .dataTables_wrapper .dataTables_length,
body.emr-app .dataTables_wrapper .dataTables_filter,
body.emr-app .dataTables_wrapper .dataTables_info,
body.emr-app .dataTables_wrapper .dataTables_paginate {
  color: var(--emr-text-secondary);
}

body.emr-app table.dataTable > tbody > tr.child {
  background: var(--emr-table-row-bg) !important;
}

body.emr-app table.dataTable > tbody > tr.child ul.dtr-details > li {
  border-bottom-color: var(--emr-border);
  color: var(--emr-text);
}

body.emr-app .dataTables_wrapper .dataTables_paginate .paginate_button.current {
  background: var(--emr-primary) !important;
  border-color: var(--emr-primary) !important;
  color: #fff !important;
  border-radius: var(--emr-radius-xs) !important;
}

/* Action buttons in tables */
body.emr-app .btn-xs {
  padding: 0.25rem 0.45rem;
  font-size: 0.75rem;
  border-radius: var(--emr-radius-xs);
  margin: 0 0.1rem;
  line-height: 1.2;
}

body.emr-app .table .btn-group {
  display: inline-flex;
  gap: 0.2rem;
  flex-wrap: wrap;
}

/* ==========================================================================
   FORMS
   ========================================================================== */
body.emr-app label,
body.emr-app .col-form-label {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--emr-text-secondary);
  margin-bottom: var(--emr-space-1);
}

body.emr-app .form-control,
body.emr-app .custom-select {
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius-sm);
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

body.emr-app .form-control:focus {
  border-color: var(--emr-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.emr-form-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: var(--emr-space-4) var(--emr-space-5);
}

.emr-form-grid > .form-group,
.emr-form-grid > [class*="col-"] {
  margin-bottom: 0;
}

.emr-form-section {
  margin-bottom: var(--emr-space-6);
  padding: var(--emr-space-5) var(--emr-space-6);
  background: var(--emr-surface);
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius);
}

.emr-form-section__label {
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--emr-text-muted);
  margin: 0 0 var(--emr-space-4);
  padding-bottom: var(--emr-space-3);
  border-bottom: 1px solid var(--emr-border);
}

/* Filter bar (list pages) */
.emr-filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--emr-space-3);
  align-items: flex-end;
  padding: var(--emr-space-4);
  background: var(--emr-bg);
  border-radius: var(--emr-radius-sm);
  margin-bottom: var(--emr-space-4);
}

/* Collapsible worklist filters (echo / USG doctor dashboard) */
.emr-filter-panel .emr-surface__body--flush {
  padding: var(--emr-space-3) var(--emr-space-4) var(--emr-space-4);
}

.emr-filter-panel__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--emr-space-3);
  padding: var(--emr-space-3) var(--emr-space-4);
}

.emr-filter-panel__toggle .emr-filter-panel__chevron {
  font-size: 0.65rem;
  transition: transform 0.2s ease;
}

.emr-filter-panel__toggle[aria-expanded="true"] .emr-filter-panel__chevron {
  transform: rotate(180deg);
}

.emr-filter-panel__summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 0.5rem;
  flex: 1;
  min-width: 0;
}

.emr-filter-panel__chip {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--emr-text-secondary);
  background: var(--emr-bg);
  border: 1px solid var(--emr-border);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  white-space: nowrap;
}

.emr-filter-panel__reset {
  font-size: 0.8125rem;
}

.emr-worklist-filters {
  margin: 0;
}

.emr-filter-panel .collapse.show {
  border-top: 1px solid var(--emr-border);
}

/* ==========================================================================
   BUTTONS
   ========================================================================== */
body.emr-app .btn {
  font-weight: 600;
  font-size: 0.8125rem;
  border-radius: var(--emr-radius-sm);
  padding: 0.45rem 0.9rem;
  transition: all 0.12s ease;
}

body.emr-app .btn-primary {
  background: var(--emr-primary);
  border-color: var(--emr-primary);
}

body.emr-app .btn-primary:hover {
  background: var(--emr-primary-hover);
  border-color: var(--emr-primary-hover);
}

body.emr-app .btn-outline-primary {
  color: var(--emr-primary);
  border-color: var(--emr-border-strong);
}

body.emr-app .btn-outline-primary:hover {
  background: var(--emr-primary-muted);
}

.emr-btn-ghost {
  background: transparent;
  border: 1px solid var(--emr-border);
  color: var(--emr-text-secondary);
}

/* ==========================================================================
   BADGES / STATUS
   ========================================================================== */
body.emr-app .badge {
  font-weight: 600;
  font-size: 0.6875rem;
  padding: 0.3em 0.55em;
  border-radius: 999px;
  letter-spacing: 0.02em;
}

body.emr-app .badge-success { background: var(--emr-success-muted); color: var(--emr-success); }
body.emr-app .badge-warning { background: var(--emr-warning-muted); color: var(--emr-warning); }
body.emr-app .badge-danger { background: var(--emr-danger-muted); color: var(--emr-danger); }
body.emr-app .badge-info { background: #e0f2fe; color: var(--emr-info); }
body.emr-app .badge-secondary { background: #f1f5f9; color: var(--emr-secondary); }

.emr-status {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  font-weight: 600;
}

.emr-status::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.emr-status--success { color: var(--emr-success); }
.emr-status--warning { color: var(--emr-warning); }
.emr-status--danger { color: var(--emr-danger); }
.emr-status--neutral { color: var(--emr-secondary); }

/* ==========================================================================
   MODALS
   ========================================================================== */
body.emr-app .modal-content {
  border: none;
  border-radius: var(--emr-radius);
  box-shadow: var(--emr-shadow-lg);
}

body.emr-app .modal-header {
  padding: var(--emr-space-4) var(--emr-space-5);
  border-bottom: 1px solid var(--emr-border);
}

body.emr-app #confirmModal .modal-header.bg-danger {
  background: linear-gradient(135deg, #dc2626, #991b1b) !important;
  border: none;
}

body.emr-app .modal-body {
  padding: var(--emr-space-5);
}

body.emr-app .modal-footer {
  padding: var(--emr-space-4) var(--emr-space-5);
  border-top: 1px solid var(--emr-border);
  background: var(--emr-bg);
}

/* ==========================================================================
   APPOINTMENTS / TIMELINE
   ========================================================================== */
.emr-schedule-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--emr-space-4);
  align-items: flex-end;
  padding: var(--emr-space-5);
  background: var(--emr-bg);
  border-bottom: 1px solid var(--emr-border);
}

.emr-schedule-toolbar .form-label {
  margin-bottom: var(--emr-space-1);
}

/* ==========================================================================
   LOGIN
   ========================================================================== */
.emr-auth {
  min-height: 80vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--emr-space-8) var(--emr-space-4);
}

.emr-auth__panel {
  width: 100%;
  max-width: 420px;
  background: var(--emr-surface);
  border: 1px solid var(--emr-border);
  border-radius: 16px;
  box-shadow: var(--emr-shadow-lg);
  padding: var(--emr-space-8) var(--emr-space-6);
}

.emr-auth__brand {
  text-align: center;
  margin-bottom: var(--emr-space-6);
}

.emr-auth__brand h1 {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin: 0 0 var(--emr-space-2);
}

.emr-auth__brand p {
  color: var(--emr-text-muted);
  font-size: 0.875rem;
  margin: 0;
}

body.kp-public .login-box {
  width: 100%;
  max-width: none;
  margin: 0 !important;
}

/* ==========================================================================
   ALERTS & LOADER
   ========================================================================== */
body.emr-app .alert {
  border: none;
  border-radius: var(--emr-radius-sm);
  font-size: 0.875rem;
}

body.emr-app .global-loader {
  background: rgba(244, 246, 249, 0.92);
  backdrop-filter: blur(4px);
}

/* ==========================================================================
   TABS
   ========================================================================== */
body.emr-app .nav-tabs {
  border-bottom: 1px solid var(--emr-border);
  gap: 0.25rem;
}

body.emr-app .nav-tabs .nav-link {
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--emr-text-muted);
  font-weight: 500;
  font-size: 0.875rem;
  padding: 0.65rem 1rem;
  margin-bottom: -1px;
}

body.emr-app .nav-tabs .nav-link.active {
  color: var(--emr-primary);
  border-bottom-color: var(--emr-primary);
  background: transparent;
}

/* ==========================================================================
   DARK MODE
   ========================================================================== */
.dark-mode.emr-app,
body.emr-app.dark-mode {
  --emr-bg: #0f172a;
  --emr-surface: #1e293b;
  --emr-border: #334155;
  --emr-text: #f1f5f9;
  --emr-text-secondary: #cbd5e1;
  --emr-text-muted: #94a3b8;
  --emr-table-head-bg: #0f172a;
  --emr-table-row-bg: #1e293b;
  --emr-table-stripe-bg: rgba(15, 23, 42, 0.75);
  --emr-table-hover-bg: rgba(37, 99, 235, 0.18);
}

body.emr-app.dark-mode .dataTables_wrapper .dataTables_filter input,
body.emr-app.dark-mode .dataTables_wrapper .dataTables_length select,
.dark-mode.emr-app .dataTables_wrapper .dataTables_filter input,
.dark-mode.emr-app .dataTables_wrapper .dataTables_length select {
  background: #0f172a;
  border-color: var(--emr-border);
  color: var(--emr-text);
}

body.emr-app.dark-mode .dataTables_wrapper .dataTables_paginate .paginate_button,
.dark-mode.emr-app .dataTables_wrapper .dataTables_paginate .paginate_button {
  color: var(--emr-text-secondary) !important;
}

body.emr-app.dark-mode .dataTables_wrapper .dataTables_paginate .paginate_button.disabled,
.dark-mode.emr-app .dataTables_wrapper .dataTables_paginate .paginate_button.disabled {
  color: var(--emr-text-muted) !important;
}

/* Dashboard module inherits via .kp-dashboard */
[x-cloak] { display: none !important; }

/* ==========================================================================
   CLINICAL FORMS (consultation, prescription, vitals)
   ========================================================================== */
.emr-clinical-form .card.card-outline {
  border: 1px solid var(--emr-border);
  border-radius: var(--emr-radius);
  box-shadow: var(--emr-shadow-sm);
  margin-bottom: var(--emr-space-5);
  overflow: hidden;
}

.emr-clinical-form .card.card-outline > .card-header {
  background: var(--emr-bg);
  border-bottom: 1px solid var(--emr-border);
  padding: var(--emr-space-3) var(--emr-space-5);
}

.emr-clinical-form .card.card-outline.card-primary > .card-header {
  border-top: 3px solid var(--emr-primary);
}

.emr-clinical-form .card.card-outline > .card-body {
  padding: var(--emr-space-5);
}

.emr-clinical-form .inv-snap-text {
  max-height: 6rem;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.emr-surface--compact {
  margin-bottom: var(--emr-space-4);
}

.emr-surface--compact .emr-surface__body {
  padding: var(--emr-space-4) var(--emr-space-5);
}

.emr-surface__foot {
  padding: var(--emr-space-3) var(--emr-space-5);
  border-top: 1px solid var(--emr-border);
  background: var(--emr-bg);
}

.emr-rx-surface .table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

/* ==========================================================================
   WORKLIST (ultrasound / USG doctor dashboard — not DataTables)
   ========================================================================== */
.emr-worklist-scroll,
.emr-worklist-table .emr-table-wrap {
  overflow: visible;
  max-height: none;
  width: max-content;
  min-width: 100%;
  max-width: none;
}

.emr-worklist-table__grid thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: #f1f5f9;
}

.emr-worklist-table__grid .thead-light th {
  background: #f1f5f9 !important;
}
