@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=Playfair+Display:wght@700;800&display=swap');

:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #eef2ff;
  --primary-hover: #5254cc;

  --ink: #1e293b;
  --ink2: #334155;
  --ink3: #64748b;
  --ink4: #94a3b8;

  --success: #22c55e;
  --success-light: #f0fdf4;
  --warning: #f59e0b;
  --warning-light: #fffbeb;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --info: #3b82f6;
  --info-light: #eff6ff;

  --paper: #f8fafc;
  --card: #ffffff;
  --border: #e2e8f0;
  --border2: #f1f5f9;

  --sidebar-bg: #0f172a;
  --sidebar-active: #6366f1;
  --sidebar-hover: rgba(255,255,255,0.06);
  --sidebar-text: rgba(255,255,255,0.7);
  --sidebar-text-active: #ffffff;
  --sidebar-width: 280px;

  --radius: 12px;
  --radius-sm: 8px;
  --radius-xs: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 4px 12px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.1);
  --shadow-card: 0 0 0 1px rgba(0,0,0,0.06), 0 1px 4px rgba(0,0,0,0.04);

  --font: 'Inter', -apple-system, sans-serif;
  --font-display: 'Playfair Display', serif;
}

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

body {
  font-family: var(--font);
  background: var(--paper);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ═══ APP LAYOUT ═══ */
.app-layout {
  display: flex;
  min-height: 100vh;
  background: var(--paper);
}

/* ═══ SIDEBAR ═══ */
.sidebar {
  position: fixed; left: 0; top: 0; bottom: 0;
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex; flex-direction: column;
  z-index: 100; overflow-y: auto;
}
.sidebar-logo {
  padding: 24px 20px 20px;
  display: flex; align-items: center; gap: 12px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.logo-icon { font-size: 28px; }
.logo-title { color: white; font-weight: 700; font-size: 18px; letter-spacing: -0.3px; }
.logo-sub { color: rgba(255,255,255,0.4); font-size: 10px; text-transform: uppercase; letter-spacing: 1px; }
.sidebar-nav { flex: 1; padding: 12px 12px; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; border-radius: 8px;
  color: var(--sidebar-text); font-size: 14px; font-weight: 500;
  cursor: pointer; margin-bottom: 2px;
  transition: all 0.15s; text-decoration: none;
}
.nav-item:hover { background: var(--sidebar-hover); color: white; }
.nav-item.active { background: var(--sidebar-active); color: white; }
.nav-icon { font-size: 18px; width: 22px; text-align: center; flex-shrink: 0; }
.sidebar-section-label { color: rgba(255,255,255,0.3); font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; padding: 16px 14px 6px; }
.sidebar-user {
  padding: 16px 16px; border-top: 1px solid rgba(255,255,255,0.08);
  display: flex; align-items: center; gap: 10px;
}
.user-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--primary); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 14px; color: white; flex-shrink: 0; }
.user-name { color: white; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { color: rgba(255,255,255,0.4); font-size: 11px; }
.logout-btn { margin-left: auto; background: rgba(255,255,255,0.08); border: none; color: rgba(255,255,255,0.5); width: 30px; height: 30px; border-radius: 6px; cursor: pointer; font-size: 14px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: all 0.15s; }
.logout-btn:hover { background: var(--danger); color: white; }

/* ═══ MAIN CONTENT ═══ */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-width: 0;
  padding: 32px;
}

/* ═══ MOBILE HEADER ═══ */
.mobile-header { display: none; align-items: center; gap: 12px; padding: 14px 16px; background: var(--card); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50; }
.hamburger { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--ink); }
.mobile-title { font-size: 16px; font-weight: 700; color: var(--ink); }
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 99; }
.sidebar-overlay.visible { display: block; }

/* ═══ PAGE SYSTEM ═══ */
.page { display: none; animation: fadeIn 0.2s ease; }
.page.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

/* ═══ PAGE HEADER ═══ */
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 28px;
}
.page-title h1 { font-size: 24px; font-weight: 700; color: var(--ink); }

/* ═══ STAT GRID — FULL WIDTH 4 COLUMNS ═══ */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}
.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px 24px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-card);
  display: flex;
  align-items: center;
  gap: 16px;
}
.stat-icon {
  width: 48px; height: 48px;
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
}
.stat-body { flex: 1; }
.stat-number { font-size: 28px; font-weight: 800; color: var(--ink); line-height: 1; }
.stat-label { font-size: 12px; color: var(--ink3); font-weight: 500; margin-top: 3px; }

/* ═══ PATIENT TABLE — FULL WIDTH ═══ */
.patient-table-wrap { background: var(--card); border-radius: var(--radius); border: 1px solid var(--border); overflow: hidden; }
.patient-table { width: 100%; border-collapse: collapse; }
.patient-table th { padding: 12px 16px; text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--ink3); background: var(--paper); border-bottom: 1px solid var(--border); }
.patient-table td { padding: 14px 16px; border-bottom: 1px solid var(--border2); vertical-align: middle; }
.patient-table tr:last-child td { border-bottom: none; }
.patient-table tr:hover td { background: var(--primary-light); cursor: pointer; }
.patient-avatar-sm { width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 700; color: white; flex-shrink: 0; }

/* ═══ ALERT BANNERS ═══ */
.alert-banner { border-radius: var(--radius); padding: 16px 20px; margin-bottom: 16px; }
.alert-banner.alert-danger { background: var(--danger-light); border: 1px solid #fecaca; }
.alert-banner.alert-warning { background: var(--warning-light); border: 1px solid #fde68a; }
.alert-title { font-weight: 700; font-size: 13px; margin-bottom: 10px; }
.alert-danger .alert-title { color: #b91c1c; }
.alert-warning .alert-title { color: #92400e; }
.alert-item { display: flex; align-items: center; gap: 12px; padding: 8px 12px; border-radius: 8px; margin-bottom: 4px; cursor: pointer; background: rgba(255,255,255,0.5); }
.alert-item:hover { background: rgba(255,255,255,0.8); }
.alert-name { flex: 1; font-weight: 600; font-size: 13px; }
.alert-days { font-size: 12px; color: var(--ink3); }
.alert-arrow { font-size: 12px; color: var(--ink4); }

/* ═══ BUTTONS ═══ */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; border: none; font-family: var(--font); transition: all 0.15s; text-decoration: none; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #dc2626; }
.btn-outline { background: transparent; color: var(--ink2); border: 1px solid var(--border); }
.btn-outline:hover { background: var(--paper); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-lg { padding: 12px 24px; font-size: 15px; font-weight: 600; }

/* ═══ CARDS ═══ */
.card { background: var(--card); border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow-card); }
.card-header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.card-header h2 { font-size: 15px; font-weight: 600; color: var(--ink); }
.card-body { padding: 20px; }
.card-body.p-0 { padding: 0; }

/* ═══ FORMS ═══ */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 13px; font-weight: 500; color: var(--ink2); margin-bottom: 6px; }
.input, .select, textarea { width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 14px; font-family: var(--font); color: var(--ink); background: var(--card); outline: none; transition: border-color 0.15s; }
.input:focus, .select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(99,102,241,0.1); }
.form-row { display: grid; gap: 16px; }
.form-row-2 { grid-template-columns: 1fr 1fr; }
.form-row-3 { grid-template-columns: 1fr 1fr 1fr; }

/* ═══ BADGES ═══ */
.badge { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge-success { background: var(--success-light); color: #16a34a; }
.badge-warning { background: var(--warning-light); color: #92400e; }
.badge-danger { background: var(--danger-light); color: #b91c1c; }
.badge-info { background: var(--info-light); color: #1d4ed8; }
.badge-default { background: var(--paper); color: var(--ink3); border: 1px solid var(--border); }
.badge-primary { background: var(--primary-light); color: var(--primary-dark); }

/* ═══ DATA TABLE ═══ */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th { padding: 11px 16px; text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--ink3); background: var(--paper); border-bottom: 1px solid var(--border); }
.data-table td { padding: 13px 16px; border-bottom: 1px solid var(--border2); font-size: 13px; color: var(--ink2); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.table-actions { display: flex; gap: 6px; align-items: center; }

/* ═══ TOAST ═══ */
#toastContainer { position: fixed; bottom: 24px; right: 24px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { padding: 12px 18px; border-radius: 10px; font-size: 13px; font-weight: 500; opacity: 0; transform: translateY(8px); transition: all 0.25s; max-width: 320px; pointer-events: auto; display: flex; align-items: center; gap: 10px; box-shadow: var(--shadow-md); }
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { background: #1e293b; color: white; }
.toast-error { background: var(--danger); color: white; }
.toast-warning { background: #92400e; color: white; }

/* ═══ MODAL ═══ */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,23,42,0.5); z-index: 1000; display: none; align-items: center; justify-content: center; backdrop-filter: blur(2px); }
.modal-overlay.open { display: flex; }
.modal { background: var(--card); border-radius: 16px; padding: 28px; max-width: 480px; width: 90%; box-shadow: 0 20px 60px rgba(0,0,0,0.2); }
.modal h2 { font-size: 18px; font-weight: 700; color: var(--ink); margin-bottom: 16px; }

/* ═══ SEARCH BAR ═══ */
.search-bar { position: relative; margin-bottom: 20px; }
.search-bar input { width: 100%; padding: 10px 16px 10px 40px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px; background: var(--card); font-family: var(--font); color: var(--ink); outline: none; box-shadow: var(--shadow-card); }
.search-bar input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(99,102,241,0.1); }
.search-icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--ink4); pointer-events: none; font-size: 14px; }

/* ═══ LOADING / EMPTY ═══ */
.loading { display: flex; align-items: center; justify-content: center; padding: 48px; color: var(--ink3); font-size: 14px; gap: 10px; }
.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 60px 20px; color: var(--ink4); text-align: center; }
.empty-state svg, .empty-icon { font-size: 40px; margin-bottom: 12px; opacity: 0.5; }
.empty-state p { font-size: 14px; }

/* ═══ SETTINGS ═══ */
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.settings-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.settings-card h3 { font-size: 15px; font-weight: 600; color: var(--ink); margin-bottom: 6px; }
.settings-card .subtitle { font-size: 13px; color: var(--ink3); margin-bottom: 20px; }
.profile-display { display: flex; align-items: center; gap: 16px; }
.profile-avatar-lg { width: 56px; height: 56px; border-radius: 50%; background: var(--primary); display: flex; align-items: center; justify-content: center; font-size: 22px; font-weight: 700; color: white; }
.profile-email { font-size: 15px; font-weight: 600; color: var(--ink); }
.profile-role { font-size: 12px; color: var(--ink3); margin-top: 2px; }
.security-row { display: flex; align-items: center; justify-content: space-between; padding: 14px 0; border-bottom: 1px solid var(--border2); }
.security-row:last-child { border-bottom: none; padding-bottom: 0; }
.security-label { font-size: 14px; font-weight: 500; color: var(--ink); }
.security-desc { font-size: 12px; color: var(--ink3); margin-top: 2px; }

/* ═══ ADMIN GRID ═══ */
.admin-grid { display: grid; grid-template-columns: 340px 1fr; gap: 24px; align-items: start; }

/* ═══ LOGIN PAGE ═══ */
.login-page { background: var(--paper); min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card { background: var(--card); border-radius: 20px; box-shadow: 0 8px 40px rgba(0,0,0,0.12); padding: 40px; width: 420px; max-width: 95vw; }
.login-logo { text-align: center; margin-bottom: 32px; }
.login-logo-icon { font-size: 48px; display: block; margin-bottom: 8px; }
.login-logo h1 { font-size: 28px; font-weight: 800; color: var(--ink); margin-bottom: 4px; font-family: var(--font-display); }
.login-logo p { font-size: 13px; color: var(--ink3); }
.login-view { display: none; }
.login-view.active { display: block; }
.login-view h2 { font-size: 20px; font-weight: 700; color: var(--ink); margin-bottom: 6px; }
.login-view .sub { font-size: 13px; color: var(--ink3); margin-bottom: 24px; }
.auth-error { background: var(--danger-light); border: 1px solid #fecaca; color: #b91c1c; border-radius: 8px; padding: 10px 14px; font-size: 13px; margin-bottom: 16px; display: none; }
.auth-error.show { display: block; }
.totp-input { text-align: center; letter-spacing: 10px; font-size: 24px; font-weight: 700; }
.back-link { display: inline-flex; align-items: center; gap: 6px; color: var(--ink3); font-size: 13px; cursor: pointer; margin-top: 16px; background: none; border: none; font-family: var(--font); }
.back-link:hover { color: var(--primary); }

/* ═══ RESPONSIVE ═══ */
@media (max-width: 768px) {
  .app-layout { flex-direction: column; }
  .main-content { margin-left: 0; padding: 16px; padding-top: 8px; }
  .sidebar { transform: translateX(-100%); transition: transform 0.25s; }
  .sidebar.open { transform: translateX(0); }
  .mobile-header { display: flex; }

  /* Page header: stack title + button vertically */
  .page-header { flex-direction: column; align-items: flex-start; gap: 12px; margin-bottom: 20px; }
  .page-header .btn { width: 100%; justify-content: center; }
  .page-title h1 { font-size: 22px; }

  /* Stat cards: 2 columns, compact */
  .stat-grid { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
  .stat-card { padding: 14px 12px; gap: 10px; }
  .stat-icon { width: 40px; height: 40px; font-size: 18px; border-radius: 10px; }
  .stat-number { font-size: 22px; }
  .stat-label { font-size: 11px; }

  /* Patient table: horizontal scroll */
  .patient-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .patient-table { min-width: 600px; }

  /* Settings: single column */
  .settings-grid { grid-template-columns: 1fr; }
  .form-row-2, .form-row-3 { grid-template-columns: 1fr; }

  /* Admin page: stack form + table */
  .admin-grid { grid-template-columns: 1fr; }

  /* Search bar */
  .search-bar input { font-size: 16px; /* prevents iOS zoom */ }

  /* Login card */
  .login-card { padding: 28px 20px; }

  /* Toast */
  #toastContainer { bottom: 16px; right: 16px; left: 16px; }
  .toast { max-width: 100%; }
}

@media (max-width: 480px) {
  .stat-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-card { padding: 12px 10px; gap: 8px; }
  .stat-icon { width: 36px; height: 36px; font-size: 16px; }
  .stat-number { font-size: 20px; }
  .main-content { padding: 12px; }
}
