:root {
  --primary: #0891b2;
  --primary-dark: #0e7490;
  --primary-light: #cffafe;
  --primary-subtle: rgba(8, 145, 178, 0.06);
  --success: #10b981;
  --success-light: #d1fae5;
  --success-subtle: rgba(16, 185, 129, 0.06);
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --danger-subtle: rgba(239, 68, 68, 0.06);
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --warning-subtle: rgba(245, 158, 11, 0.06);
  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --bg: #f8fafc;
  --card: #ffffff;
  --border: #e2e8f0;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.04), 0 4px 16px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 4px 24px rgba(15, 23, 42, 0.08), 0 12px 40px rgba(15, 23, 42, 0.06);
  --shadow-xl: 0 12px 48px rgba(15, 23, 42, 0.12), 0 24px 64px rgba(15, 23, 42, 0.08);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  --sidebar-bg: #0f172a;
  --sidebar-width: 240px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

input, textarea, [contenteditable] {
  -webkit-user-select: auto;
  user-select: auto;
}

/* Typography */
h1, h2, h3, h4 { margin: 0 0 12px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.2; }
h1 { font-size: 28px; }
h2 { font-size: 22px; }
h3 { font-size: 18px; }
h4 { font-size: 15px; }

p, li { line-height: 1.6; }

small, .text-sm { font-size: 13px; }
.text-xs { font-size: 11px; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }

/* Layout */
.app-container {
  max-width: 1024px;
  margin: 0 auto;
  padding: 20px;
  min-height: 100vh;
}

@media (max-width: 640px) {
  .app-container { padding: 12px; }
  h1 { font-size: 22px; }
  h2 { font-size: 20px; }
}

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

.card:hover { box-shadow: var(--shadow-lg); transform: translateY(-1px); }

.card-compact {
  padding: 16px;
  border-radius: var(--radius-sm);
}

.card-smooth {
  border-radius: calc(var(--radius) * 2);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 24px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  min-height: 48px;
  touch-action: manipulation;
  user-select: none;
  position: relative;
  overflow: hidden;
  white-space: nowrap;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  box-shadow: 0 4px 16px rgba(8, 145, 178, 0.3);
}

.btn-primary:hover { filter: brightness(1.1); box-shadow: 0 6px 20px rgba(8, 145, 178, 0.4); }

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

.btn-secondary:hover { background: var(--border); }

.btn-success {
  background: linear-gradient(135deg, var(--success), #059669);
  color: white;
  box-shadow: 0 4px 16px rgba(16, 185, 129, 0.3);
}

.btn-success:hover { filter: brightness(1.1); }

.btn-danger {
  background: linear-gradient(135deg, var(--danger), #dc2626);
  color: white;
  box-shadow: 0 4px 16px rgba(239, 68, 68, 0.3);
}

.btn-danger:hover { filter: brightness(1.1); }

.btn-warning {
  background: linear-gradient(135deg, var(--warning), #d97706);
  color: white;
  box-shadow: 0 4px 16px rgba(245, 158, 11, 0.3);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: 10px 16px;
}

.btn-ghost:hover { background: var(--bg); }

.btn-sm { padding: 10px 16px; font-size: 13px; min-height: 40px; }
.btn-xs { padding: 6px 10px; font-size: 12px; min-height: 32px; }

.btn-icon {
  width: 36px; height: 36px; padding: 0; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center;
}

.btn-group {
  display: inline-flex;
  background: var(--bg);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  overflow: hidden;
}
.btn-group .btn {
  border-radius: 0;
  border: none;
  border-right: 1px solid var(--border);
}
.btn-group .btn:last-child { border-right: none; }

/* FAB */
.fab {
  position: fixed;
  bottom: 28px;
  right: 28px;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: none;
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 24px rgba(8, 145, 178, 0.4);
  cursor: pointer;
  z-index: 100;
  transition: var(--transition);
}

.fab:hover { transform: scale(1.08); box-shadow: 0 12px 32px rgba(8, 145, 178, 0.5); }
.fab:active { transform: scale(0.95); }

/* Inputs */
input, select, textarea {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  transition: var(--transition);
  outline: none;
  -webkit-appearance: none;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px var(--primary-light);
}

input::placeholder, textarea::placeholder { color: var(--text-muted); }

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.input-icon-wrap {
  position: relative;
}
.input-icon-wrap input { padding-left: 44px; }
.input-icon-wrap .input-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 16px;
  pointer-events: none;
}

/* Table */
.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: white;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: white;
}

th {
  text-align: left;
  padding: 14px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 14px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }
tr { transition: background 0.15s; }
tr:hover td { background: rgba(248,250,252,0.8); }

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  gap: 4px;
}

.badge-green { background: var(--success-light); color: #047857; }
.badge-red { background: var(--danger-light); color: #b91c1c; }
.badge-amber { background: var(--warning-light); color: #b45309; }
.badge-blue { background: var(--primary-light); color: var(--primary-dark); }
.badge-gray { background: var(--bg); color: var(--text-muted); }

/* Progress / status dot */
.status-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  margin-right: 6px; flex-shrink: 0;
}
.status-draft { background: var(--text-muted); }
.status-pending { background: var(--warning); }
.status-in_progress { background: var(--primary); }
.status-delivered { background: var(--success); }

/* Balance display */
.balance-lg {
  font-size: 36px;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.balance-positive { color: var(--danger); }
.balance-negative { color: var(--success); }
.balance-neutral { color: var(--text-secondary); }

/* Grid layouts */
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }

@media (max-width: 640px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* Toolbar */
.toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.toolbar .spacer { flex: 1; }

/* Search */
.search-box {
  position: relative;
  flex: 1;
  min-width: 200px;
}

.search-box input {
  padding-left: 44px;
  border-radius: 30px;
}

.search-box .icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 16px;
}

/* Header bar */
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.header-bar h1 { margin: 0; }

/* Empty state */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state .icon {
  font-size: 64px;
  margin-bottom: 16px;
  opacity: 0.3;
}

/* Stat chips */
.stat-chip {
  background: white;
  border-radius: var(--radius-sm);
  padding: 16px;
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  transition: var(--transition);
}

.stat-chip:hover {
  box-shadow: var(--shadow);
  transform: translateY(-1px);
}

.stat-chip .icon-box {
  width: 40px; height: 40px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; flex-shrink: 0;
}

.stat-chip .info { min-width: 0; }
.stat-chip .info .value { font-size: 20px; font-weight: 800; line-height: 1.2; }
.stat-chip .info .label { font-size: 11px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; margin-top: 2px; }

/* Modal / Drawer */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
  display: none;
  align-items: flex-end;
  justify-content: center;
  z-index: 200;
  opacity: 0;
  transition: opacity 0.3s;
}

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

.modal-sheet {
  background: var(--card);
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.modal-overlay.open .modal-sheet {
  transform: translateY(0);
}

.modal-sheet h3 {
  font-size: 18px;
  margin-bottom: 20px;
}

@media (min-width: 641px) {
  .modal-overlay { align-items: center; }
  .modal-sheet {
    border-radius: var(--radius);
    transform: translateY(20px) scale(0.96);
  }
  .modal-overlay.open .modal-sheet {
    transform: translateY(0) scale(1);
  }
}

/* Nav badge (unseen count) */
.nav-badge {
  position: absolute; top: -4px; right: -6px;
  min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 8px; background: #dc2626; color: #fff;
  font-size: 9px; font-weight: 800; line-height: 16px;
  text-align: center; pointer-events: none;
}
.nav-tab { position: relative; }

/* Loading */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(248, 250, 252, 0.7);
  backdrop-filter: blur(3px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 300;
}

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

.spinner {
  width: 44px;
  height: 44px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* Toast */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 400;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  background: var(--card);
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  border-left: 4px solid var(--primary);
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 260px;
  max-width: 400px;
  font-size: 14px;
  font-weight: 500;
  pointer-events: auto;
  animation: slideIn 0.3s ease-out;
}

.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warning); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Patient cards grid */
.patient-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.patient-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: var(--transition);
  position: relative;
  overflow: hidden;
}

.patient-card::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 4px;
  background: var(--primary);
  opacity: 0;
  transition: opacity 0.2s;
}

.patient-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.patient-card:hover::before { opacity: 1; }

.patient-card.archived::before { background: var(--text-muted); }

/* Prescriptions filters */
.filters-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  align-items: flex-end;
}

.filters-bar > div { flex: 1; min-width: 140px; }
.filters-bar label { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin-bottom: 4px; }
.filters-bar input, .filters-bar select {
  padding: 10px 14px;
  font-size: 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: white;
  width: 100%;
}

/* RX list */
.rx-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.rx-item {
  background: white;
  border-radius: var(--radius-sm);
  padding: 16px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.rx-item:hover { border-color: var(--primary); box-shadow: var(--shadow); }
.rx-item .left { flex: 1; min-width: 0; }
.rx-item .right { text-align: right; flex-shrink: 0; }
.rx-item .patient-name { font-size: 15px; font-weight: 700; margin-bottom: 2px; }
.rx-item .meta { font-size: 12px; color: var(--text-muted); }
.rx-item .amount { font-size: 18px; font-weight: 800; color: var(--primary); }

/* Animations */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.animate-fade { animation: fadeIn 0.4s ease-out; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Utility */
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-muted { color: var(--text-muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-primary { color: var(--primary); }
.text-sm { font-size: 13px; }
.text-xs { font-size: 11px; }
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mt-3 { margin-top: 24px; }
.mb-1 { margin-bottom: 8px; }
.mb-2 { margin-bottom: 16px; }
.mb-3 { margin-bottom: 24px; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: 8px; }
.gap-2 { gap: 16px; }
.gap-3 { gap: 24px; }
.w-full { width: 100%; }
.hidden { display: none !important; }
