/* ===== RESET & BASE ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  /* Background */
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-2: #cbd5e1;

  /* Text */
  --text: #0f172a;
  --text-muted: #64748b;
  --text-dim: #94a3b8;

  /* Medical Blue (sidebar, header, accent) */
  --blue: #1d4ed8;
  --blue-mid: #2563eb;
  --blue-light: #eff6ff;
  --blue-border: #bfdbfe;

  /* Critical Red */
  --red: #dc2626;
  --red-light: #fff1f1;
  --red-chip-bg: #fee2e2;
  --red-chip-txt: #b91c1c;
  --red-border: #fecaca;
  --red-section: #fef2f2;

  /* Trend Amber */
  --amber: #d97706;
  --amber-light: #fffdf0;
  --amber-chip-bg: #fef3c7;
  --amber-chip-txt: #92400e;
  --amber-border: #fde68a;
  --amber-section: #fffbeb;

  /* Stable Green */
  --green: #16a34a;
  --green-light: #f0fdf4;
  --green-chip-bg: #dcfce7;
  --green-chip-txt: #15803d;
  --green-border: #bbf7d0;
  --green-section: #f0fdf4;

  /* Dimensions */
  --sidebar-w: 220px;
  --topbar-h: 56px;
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  -webkit-font-smoothing: antialiased;
  font-size: 13px;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: var(--blue);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 50;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.logo-icon {
  width: 42px;
  height: 42px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--blue);
  flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.2);
}

.logo-icon svg {
  width: 26px;
  height: 26px;
}

.logo-name {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  line-height: 1.1;
}

.logo-sub {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.6);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-top: 1px;
}

.sidebar-section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.1px;
  color: rgba(255, 255, 255, 0.8);
  padding: 16px 16px 6px;
}

.sidebar-nav {
  padding: 4px 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: 7px;
  color: rgba(255, 255, 255, 0.75);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  margin-bottom: 2px;
}

.nav-item svg {
  flex-shrink: 0;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
}

.nav-badge {
  margin-left: auto;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 8px;
  border-radius: 20px;
  font-family: 'JetBrains Mono', monospace;
}

.nav-badge-red {
  background: rgba(239, 68, 68, 0.25);
  color: #fca5a5;
}

.nav-badge-amber {
  background: rgba(251, 191, 36, 0.2);
  color: #fde68a;
}

.nav-badge-green {
  background: rgba(52, 211, 153, 0.2);
  color: #6ee7b7;
}

.sidebar-shift {
  padding: 4px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.shift-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
}

.sidebar-footer {
  margin-top: auto;
  padding: 12px 16px;
  font-size: 10px;
  color: rgba(255, 255, 255, 0.65);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-credit {
  color: rgba(255, 255, 255, 0.85);
  text-decoration: none;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.2px;
  transition: color 0.15s ease, opacity 0.15s ease;
}

.sidebar-credit:hover {
  color: #fff;
  opacity: 1;
}

/* ===== MAIN PANEL ===== */
.main-panel {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== TOP BAR ===== */
.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 40;
  gap: 16px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.topbar-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}

.topbar-date {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 400;
}

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

.topbar-clock {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--surface-2);
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
}

.topbar-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--blue);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: opacity 0.15s;
}

.topbar-btn:hover {
  opacity: 0.88;
}

.topbar-btn.spinning svg {
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ===== AI SEARCH ===== */
.search-bar-wrap {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 24px;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border-2);
  border-radius: 8px;
  padding: 8px 14px;
  max-width: 700px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-bar:focus-within {
  border-color: var(--blue-mid);
  box-shadow: 0 0 0 3px var(--blue-light);
}

.search-bar svg {
  color: var(--blue-mid);
  flex-shrink: 0;
}

.search-bar input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-size: 13px;
  color: var(--text);
  font-family: inherit;
}

.search-bar input::placeholder {
  color: var(--text-dim);
}

.search-btn {
  background: var(--blue);
  color: #fff;
  border: none;
  padding: 4px 12px;
  border-radius: 5px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}

.search-clear-btn {
  background: none;
  border: 1px solid var(--border-2);
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 5px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}

.search-clear-btn:hover {
  background: var(--surface-2);
}

/* ===== CONTENT AREA ===== */
.content {
  padding: 20px 24px 60px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ===== TRIAGE SECTIONS ===== */
.triage-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

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

.section-header-red {
  background: var(--red-section);
  border-left: 4px solid var(--red);
}

.section-header-amber {
  background: var(--amber-section);
  border-left: 4px solid var(--amber);
}

.section-header-green {
  background: var(--green-section);
  border-left: 4px solid var(--green);
}

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

.section-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot-red {
  background: var(--red);
}

.dot-amber {
  background: var(--amber);
}

.dot-green {
  background: var(--green);
}

.section-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

.section-desc {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 1px;
}

.section-count {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 20px;
  white-space: nowrap;
  flex-shrink: 0;
}

.count-red {
  background: var(--red-chip-bg);
  color: var(--red-chip-txt);
}

.count-amber {
  background: var(--amber-chip-bg);
  color: var(--amber-chip-txt);
}

.count-green {
  background: var(--green-chip-bg);
  color: var(--green-chip-txt);
}

/* ===== PATIENT TABLE ===== */
.patient-table {
  width: 100%;
  overflow-x: auto;
}

.table-header {
  display: grid;
  grid-template-columns: 160px 70px 100px 80px 80px 90px 90px 100px 1fr;
  gap: 0;
  padding: 8px 20px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-dim);
}

.table-header span {
  padding: 0 4px;
}

.table-body {
  display: flex;
  flex-direction: column;
}

.table-row {
  display: grid;
  grid-template-columns: 160px 70px 100px 80px 80px 90px 90px 100px 1fr;
  gap: 0;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  align-items: center;
  cursor: pointer;
  transition: background 0.12s;
  border-left: 3px solid transparent;
}

.table-row:last-child {
  border-bottom: none;
}

.table-row:hover {
  background: var(--surface-2);
}

.table-row>span {
  padding: 0 4px;
}

.row-red {
  border-left-color: var(--red);
}

.row-red:hover {
  background: var(--red-light);
}

.row-amber {
  border-left-color: var(--amber);
}

.row-amber:hover {
  background: var(--amber-light);
}

.row-patient-name {
  font-weight: 600;
  color: var(--text);
  font-size: 13px;
}

.row-patient-diag {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 1px;
}

.row-room {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.row-bg {
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  font-weight: 700;
}

.row-bg-red {
  color: var(--red);
}

.row-bg-amber {
  color: var(--amber);
}

.row-bg-green {
  color: var(--green);
}

.row-secondary {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  color: var(--text-muted);
}

.row-change {
  font-size: 12px;
  font-weight: 600;
}

.row-change-down {
  color: var(--red);
}

.row-change-up {
  color: var(--amber);
}

.row-change-flat {
  color: var(--text-dim);
}

.row-notes {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 220px;
}

/* Status chips */
.chip {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.chip-hypo {
  background: var(--red-chip-bg);
  color: var(--red-chip-txt);
}

.chip-hyper {
  background: var(--red-chip-bg);
  color: var(--red-chip-txt);
}

.chip-rising {
  background: var(--amber-chip-bg);
  color: var(--amber-chip-txt);
}

.chip-falling {
  background: var(--blue-light);
  color: var(--blue);
  border: 1px solid var(--blue-border);
}

.chip-stable {
  background: var(--green-chip-bg);
  color: var(--green-chip-txt);
}

/* Empty state */
.table-empty {
  padding: 24px 20px;
  color: var(--text-muted);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ===== ALL CLEAR GRID ===== */
.clear-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1px;
  background: var(--border);
}

.clear-card {
  background: var(--surface);
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.12s;
}

.clear-card:hover {
  background: var(--green-light);
}

.clear-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.clear-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 1px;
}

.clear-bg {
  font-family: 'JetBrains Mono', monospace;
  font-size: 20px;
  font-weight: 700;
  color: var(--green);
  margin-top: 6px;
}

.clear-unit {
  font-size: 11px;
  color: var(--text-dim);
  font-weight: 400;
  margin-left: 2px;
}

/* ===== MODAL ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-overlay.open {
  display: flex;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 640px;
  width: 100%;
  position: relative;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
  animation: modalIn 0.2s ease;
  max-height: 90vh;
  overflow-y: auto;
}

@keyframes modalIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 13px;
  transition: all 0.15s;
}

.modal-close:hover {
  background: var(--border);
  color: var(--text);
}

.modal-status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: 10px;
}

.modal-name {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
  margin-bottom: 2px;
}

.modal-subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.modal-bg-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.modal-bg-val {
  font-family: 'JetBrains Mono', monospace;
  font-size: 44px;
  font-weight: 700;
  line-height: 1;
}

.modal-bg-unit {
  font-size: 14px;
  color: var(--text-muted);
}

.modal-section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-dim);
  margin: 14px 0 8px;
}

/* Glucose history chart */
.modal-chart-container {
  height: 140px;
  width: 100%;
  margin: 10px 0 20px;
  position: relative;
}

.modal-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.modal-cell {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 10px 12px;
}

.modal-cell-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  margin-bottom: 3px;
}

.modal-cell-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

/* AI Insight in modal */
.modal-ai-section {
  background: var(--blue-light);
  border: 1px solid var(--blue-border);
  border-radius: 8px;
  padding: 14px 16px;
}

.modal-ai-header {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--blue-mid);
  margin-bottom: 10px;
}

.ai-risk-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 20px;
  margin-bottom: 8px;
}

.ai-risk-high {
  background: var(--red-chip-bg);
  color: var(--red-chip-txt);
}

.ai-risk-medium {
  background: var(--amber-chip-bg);
  color: var(--amber-chip-txt);
}

.ai-risk-low {
  background: var(--green-chip-bg);
  color: var(--green-chip-txt);
}

.ai-summary-text {
  font-size: 13px;
  line-height: 1.55;
  color: var(--text);
}

.ai-loading {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  display: flex;
  align-items: center;
  gap: 8px;
}

.ai-loading::after {
  content: '';
  width: 12px;
  height: 12px;
  border: 2px solid var(--border-2);
  border-top-color: var(--blue-mid);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {

  /* Sidebar & Layout */
  .sidebar {
    display: none;
  }

  .main-panel {
    margin-left: 0;
  }

  .topbar {
    padding: 0 16px;
  }

  .topbar-title {
    font-size: 15px;
  }

  .topbar-date,
  .topbar-clock {
    display: none;
  }

  .topbar-btn {
    padding: 6px 10px;
  }

  /* Search */
  .search-bar-wrap {
    padding: 12px 16px;
  }

  .search-bar input {
    font-size: 14px;
  }

  /* Content Area */
  .content {
    padding: 16px 16px 60px;
    gap: 16px;
  }

  /* Triage Sections */
  .triage-section {
    background: transparent;
    border: none;
    overflow: visible;
  }

  .section-header {
    border-radius: 8px;
    border: 1px solid var(--border);
    margin-bottom: 12px;
  }

  .section-header-red {
    border-left-color: var(--red);
    background: var(--surface);
  }

  .section-header-amber {
    border-left-color: var(--amber);
    background: var(--surface);
  }

  .section-header-green {
    border-left-color: var(--green);
    background: var(--surface);
  }

  /* Convert Table to Cards */
  .patient-table {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .table-header {
    display: none;
  }

  .table-row {
    display: flex;
    flex-direction: column;
    padding: 14px 16px;
    gap: 8px;
    border-radius: 10px;
    border: 1px solid var(--border);
    border-left: 4px solid transparent;
    background: var(--surface);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
  }

  .row-red {
    border-left-color: var(--red);
  }

  .row-amber {
    border-left-color: var(--amber);
  }

  .table-row>span {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 0;
  }

  /* Name row styling */
  .table-row>span:nth-child(1) {
    flex-direction: column;
    align-items: flex-start;
    margin-bottom: 4px;
  }

  .row-patient-name {
    font-size: 15px;
  }

  .row-patient-diag {
    font-size: 12px;
  }

  /* Hidden middle values */
  .table-row>span:nth-child(4),
  .table-row>span:nth-child(5) {
    display: none;
  }

  /* Inject Labels as Key-Value pairs */
  .table-row>span:nth-child(2)::before {
    content: "Room";
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
  }

  .table-row>span:nth-child(3)::before {
    content: "Current BG";
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
  }

  .table-row>span:nth-child(6)::before {
    content: "12h Change";
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
  }

  .table-row>span:nth-child(7)::before {
    content: "Last Insulin";
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
  }

  .table-row>span:nth-child(8)::before {
    content: "Status";
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
  }

  .table-row>span:nth-child(9)::before {
    content: "Notes";
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
  }

  /* Value Overrides */
  .row-notes {
    white-space: normal;
    text-align: right;
    max-width: 65%;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  /* All Clear Cards Grid */
  .clear-grid {
    gap: 12px;
    grid-template-columns: 1fr;
    background: transparent;
  }

  .clear-card {
    border-radius: 8px;
    border: 1px solid var(--border);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02);
  }

  /* Modal Bottom Sheet */
  .modal-overlay {
    align-items: flex-end;
    padding: 0;
  }

  .modal {
    border-radius: 16px 16px 0 0;
    margin-bottom: 0;
    max-height: 90vh;
    padding: 24px 20px;
    animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  }

  @keyframes slideUp {
    from {
      transform: translateY(100%);
    }

    to {
      transform: translateY(0);
    }
  }

  .modal-grid {
    grid-template-columns: 1fr;
  }

  .modal-bg-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}