/* ===== Medteal — Design System ===== */
:root {
  color-scheme: light dark;
  --c-primary: #1a56db;
  --c-primary-light: #e1effe;
  --c-danger: #dc2626;
  --c-danger-light: #fee2e2;
  --c-warn: #f59e0b;
  --c-warn-light: #fef3c7;
  --c-success: #16a34a;
  --c-success-light: #dcfce7;
  --c-bg: #f8fafc;
  --c-surface: #ffffff;
  --c-border: #e2e8f0;
  --c-text: #1e293b;
  --c-text-secondary: #64748b;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,.1);
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --c-bg: #0f172a;
    --c-surface: #1e293b;
    --c-border: #334155;
    --c-text: #f1f5f9;
    --c-text-secondary: #94a3b8;
    --c-primary-light: #1e3a5f;
    --c-danger-light: #461111;
    --c-warn-light: #422006;
    --c-success-light: #052e16;
    --shadow: 0 1px 3px rgba(0,0,0,.3);
    --shadow-md: 0 4px 12px rgba(0,0,0,.4);
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: var(--font);
  line-height: 1.5;
  color: var(--c-text);
  background: var(--c-bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--c-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- Layout shell --- */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px;
  background: var(--c-surface);
  border-right: 1px solid var(--c-border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  transition: transform .25s;
}
.sidebar.closed { transform: translateX(-100%); }
.sidebar-header {
  padding: 16px 18px;
  border-bottom: 1px solid var(--c-border);
  display: flex; align-items: center; gap: 10px;
}
.sidebar-header h1 { font-size: 18px; margin: 0; white-space: nowrap; font-weight: 800; letter-spacing: -0.02em; }
.sidebar-header .logo {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  display: block;
  flex-shrink: 0;
  object-fit: contain;
}
.sidebar nav { flex: 1; overflow-y: auto; padding: 8px 0; }
.sidebar nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 18px;
  color: var(--c-text-secondary);
  font-size: 14px;
  border-left: 3px solid transparent;
  transition: all .15s;
}
.sidebar nav a:hover, .sidebar nav a.active {
  color: var(--c-primary);
  background: var(--c-primary-light);
  border-left-color: var(--c-primary);
  text-decoration: none;
}
.sidebar nav a .icon { width: 18px; text-align: center; flex-shrink: 0; }
.sidebar nav .nav-section {
  padding: 18px 18px 6px;
  font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .8px;
  color: var(--c-text-secondary); opacity: .7;
}
.sidebar-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--c-border);
  font-size: 12px; color: var(--c-text-secondary);
}

.main-content { flex: 1; margin-left: 240px; min-height: 100vh; }
.topbar {
  height: 56px;
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
  display: flex; align-items: center;
  padding: 0 24px;
  gap: 12px;
  position: sticky; top: 0; z-index: 50;
}
.topbar .menu-btn {
  display: none; background: none; border: none;
  color: var(--c-text); font-size: 22px; cursor: pointer; padding: 4px;
}
.topbar .breadcrumb { font-size: 14px; color: var(--c-text-secondary); }
.topbar .spacer { flex: 1; }
.topbar .notif-btn {
  position: relative; background: none; border: none;
  color: var(--c-text); font-size: 20px; cursor: pointer; padding: 4px;
}
.topbar .notif-btn .badge-count {
  position: absolute; top: -4px; right: -6px;
  background: var(--c-danger); color: #fff;
  font-size: 10px; min-width: 16px; height: 16px;
  border-radius: 999px; display: flex; align-items: center; justify-content: center;
  padding: 0 4px;
}
.topbar .user-pill {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 12px 4px 4px;
  border-radius: 999px;
  border: 1px solid var(--c-border);
  font-size: 13px; cursor: pointer; background: none; color: var(--c-text);
}
.topbar .user-pill .avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--c-primary-light); color: var(--c-primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600;
}

.page { padding: 24px; max-width: 1200px; }
.page-header { margin-bottom: 20px; }
.page-header h2 { margin: 0 0 4px; font-size: 22px; }
.page-header p { margin: 0; color: var(--c-text-secondary); font-size: 14px; }

/* --- Cards --- */
.card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-lg);
  padding: 18px;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 14px; }
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-header h3 { margin: 0; font-size: 16px; }

/* --- Stat cards --- */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat-card { text-align: center; padding: 20px 16px; }
.stat-card .stat-value { font-size: 32px; font-weight: 700; line-height: 1.1; }
.stat-card .stat-label { font-size: 13px; color: var(--c-text-secondary); margin-top: 4px; }
.stat-card.success .stat-value { color: var(--c-success); }
.stat-card.warn .stat-value { color: var(--c-warn); }
.stat-card.danger .stat-value { color: var(--c-danger); }
.stat-card.primary .stat-value { color: var(--c-primary); }

/* --- Tables --- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left; padding: 10px 12px;
  border-bottom: 2px solid var(--c-border);
  color: var(--c-text-secondary); font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .5px;
}
tbody td { padding: 10px 12px; border-bottom: 1px solid var(--c-border); vertical-align: top; }
tbody tr:hover { background: var(--c-primary-light); }

/* --- Badges/pills --- */
.badge {
  display: inline-block; padding: 2px 10px;
  border-radius: 999px; font-size: 12px; font-weight: 600;
  white-space: nowrap;
}
.badge-success { background: var(--c-success-light); color: var(--c-success); }
.badge-warn { background: var(--c-warn-light); color: #92400e; }
.badge-danger { background: var(--c-danger-light); color: var(--c-danger); }
.badge-primary { background: var(--c-primary-light); color: var(--c-primary); }
.badge-neutral { background: var(--c-border); color: var(--c-text-secondary); }

.priority-critical { border-left: 3px solid var(--c-danger); }
.priority-high { border-left: 3px solid var(--c-warn); }
.priority-medium { border-left: 3px solid var(--c-primary); }
.priority-low { border-left: 3px solid var(--c-border); }

/* --- Buttons --- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: var(--radius);
  border: 1px solid var(--c-border);
  background: var(--c-surface); color: var(--c-text);
  font-size: 14px; font-weight: 500; cursor: pointer;
  font-family: var(--font);
  transition: all .15s;
}
.btn:hover { border-color: var(--c-text-secondary); }
.btn-primary { background: var(--c-primary); color: #fff; border-color: var(--c-primary); }
.btn-primary:hover { opacity: .9; }
.btn-success { background: var(--c-success); color: #fff; border-color: var(--c-success); }
.btn-danger { background: var(--c-danger); color: #fff; border-color: var(--c-danger); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-icon { padding: 6px; border-radius: 8px; min-width: 32px; justify-content: center; }

/* --- Forms --- */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; color: var(--c-text-secondary); margin-bottom: 4px; }
.form-control {
  width: 100%; padding: 9px 12px;
  border: 1px solid var(--c-border); border-radius: var(--radius);
  background: var(--c-surface); color: var(--c-text);
  font-size: 14px; font-family: var(--font);
}
.form-control:focus { outline: none; border-color: var(--c-primary); box-shadow: 0 0 0 3px rgba(26,86,219,.15); }
select.form-control {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; padding-right: 30px;
}
textarea.form-control { resize: vertical; min-height: 80px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-check { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.form-check input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--c-primary); }

/* --- Filters bar --- */
.filters-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.filters-bar .search-input { min-width: 220px; }

/* --- Grid helpers --- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.flex-center { display: flex; align-items: center; gap: 8px; }
.gap-sm { gap: 8px; }
.gap-md { gap: 14px; }
.mt-sm { margin-top: 8px; }
.mt-md { margin-top: 16px; }
.mb-md { margin-bottom: 16px; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-sm { font-size: 13px; }
.text-muted { color: var(--c-text-secondary); }

/* --- Modal --- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 200;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.modal-overlay.open { opacity: 1; pointer-events: auto; }
.modal {
  background: var(--c-surface); border-radius: var(--radius-lg);
  width: 90%; max-width: 600px; max-height: 85vh; overflow-y: auto;
  box-shadow: var(--shadow-md); padding: 24px;
}
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-header h3 { margin: 0; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--c-text-secondary); padding: 4px; }

/* --- Toast --- */
.toast-container { position: fixed; bottom: 20px; right: 20px; z-index: 300; display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 12px 18px; border-radius: var(--radius);
  background: var(--c-surface); border: 1px solid var(--c-border);
  box-shadow: var(--shadow-md); font-size: 14px;
  animation: slideUp .25s ease-out;
}
.toast.success { border-left: 4px solid var(--c-success); }
.toast.error { border-left: 4px solid var(--c-danger); }
.toast.warning { border-left: 4px solid var(--c-warn); }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* --- Calendar --- */
.cal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.cal-header h3 { margin: 0; font-size: 16px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; font-size: 13px; }
.cal-grid .day-header { font-weight: 600; color: var(--c-text-secondary); padding: 6px 0; font-size: 11px; }
.cal-grid .day {
  padding: 8px 4px; border-radius: 6px; cursor: pointer; position: relative;
  min-height: 36px; display: flex; align-items: center; justify-content: center;
}
.cal-grid .day:hover { background: var(--c-primary-light); }
.cal-grid .day.today { font-weight: 700; color: var(--c-primary); background: var(--c-primary-light); }
.cal-grid .day.has-event::after {
  content: ''; position: absolute; bottom: 3px; left: 50%; transform: translateX(-50%);
  width: 5px; height: 5px; border-radius: 50%; background: var(--c-danger);
}
.cal-grid .day.has-event-warn::after { background: var(--c-warn); }
.cal-grid .day.empty { pointer-events: none; }

/* --- Progress bar --- */
.progress-bar { height: 8px; border-radius: 999px; background: var(--c-border); overflow: hidden; }
.progress-bar .fill { height: 100%; border-radius: 999px; transition: width .4s; }
.progress-bar .fill.green { background: var(--c-success); }
.progress-bar .fill.yellow { background: var(--c-warn); }
.progress-bar .fill.red { background: var(--c-danger); }
.progress-bar .fill.blue { background: var(--c-primary); }

/* --- Timeline / activity --- */
.activity-list { list-style: none; margin: 0; padding: 0; }
.activity-list li {
  padding: 10px 0; border-bottom: 1px solid var(--c-border);
  display: flex; gap: 10px; font-size: 14px;
}
.activity-list li:last-child { border-bottom: none; }
.activity-list .act-icon {
  width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; flex-shrink: 0;
}
.activity-list .act-time { font-size: 12px; color: var(--c-text-secondary); }

/* --- Empty state --- */
.empty-state { text-align: center; padding: 40px 20px; color: var(--c-text-secondary); }
.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { margin: 4px 0; }

/* --- Auth pages --- */
.auth-hidden { display: none !important; }
.auth-wrapper {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--c-bg); padding: 24px;
}
.auth-card {
  width: 100%; max-width: 420px;
  background: var(--c-surface); border: 1px solid var(--c-border);
  border-radius: var(--radius-lg); padding: 32px;
  box-shadow: var(--shadow-md);
}
.auth-header { text-align: center; margin-bottom: 24px; }
.auth-header .logo-lg { width: 56px; height: 56px; display: block; margin: 0 auto; border-radius: 14px; object-fit: contain; }
.auth-header h1 { font-size: 22px; margin: 10px 0 4px; font-weight: 800; letter-spacing: -0.02em; }
.auth-header p { margin: 0; }
.auth-error { background: var(--c-danger-light); color: var(--c-danger); padding: 10px 12px; border-radius: var(--radius); font-size: 13px; margin-top: 10px; }
.auth-footer { text-align: center; margin-top: 18px; }
.btn-full { width: 100%; justify-content: center; }

/* --- Plans/Billing Grid --- */
.plans-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 20px;
}
.plan-card {
  position: relative;
  background: var(--c-surface);
  border: 2px solid var(--c-border);
  border-radius: var(--radius-lg);
  padding: 24px;
  display: flex; flex-direction: column;
  transition: border-color .2s, box-shadow .2s;
}
.plan-card:hover { border-color: var(--c-primary); box-shadow: var(--shadow-md); }
.plan-current { border-color: var(--c-success); background: var(--c-success-light); }
.plan-recommended { border-color: var(--c-primary); }
.plan-badge {
  position: absolute; top: -12px; right: 16px;
  background: var(--c-primary); color: #fff;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px;
  padding: 4px 12px; border-radius: 12px;
}
.plan-header { text-align: center; margin-bottom: 16px; }
.plan-header h3 { margin: 0 0 8px; font-size: 20px; }
.plan-price { font-size: 32px; font-weight: 800; color: var(--c-text); }
.plan-price span { font-size: 14px; font-weight: 400; color: var(--c-text-secondary); }
.plan-features { list-style: none; padding: 0; margin: 0 0 20px; flex: 1; }
.plan-features li { padding: 6px 0; font-size: 13px; border-bottom: 1px solid var(--c-border); }
.plan-features li:last-child { border-bottom: none; }
.plan-action { margin-top: auto; }

/* --- Team Table --- */
.form-control-sm { padding: 4px 8px; font-size: 12px; height: auto; }

/* --- Mobile PWA enhancements --- */
@media (max-width: 768px) {
  .plans-grid { grid-template-columns: 1fr; }
  .plan-card { padding: 16px; }
}

/* Safe area for notch devices */
@supports(padding: max(0px)) {
  .sidebar { padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  .topbar { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
}

/* --- Tabs --- */
.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
  border-bottom: 2px solid var(--c-border);
  padding-bottom: 8px;
}
.tab {
  padding: 10px 16px;
  border: none;
  background: transparent;
  color: var(--c-text-secondary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: all .15s;
  margin-bottom: -10px;
}
.tab:hover {
  color: var(--c-primary);
  background: var(--c-primary-light);
  border-radius: var(--radius) var(--radius) 0 0;
}
.tab.active {
  color: var(--c-primary);
  border-bottom-color: var(--c-primary);
  font-weight: 600;
}

/* --- Additional Stats Variants --- */
.stat-card.stat-success { background: var(--c-success-light); }
.stat-card.stat-success .stat-value { color: var(--c-success); }
.stat-card.stat-danger { background: var(--c-danger-light); }
.stat-card.stat-danger .stat-value { color: var(--c-danger); }
.stat-card.stat-warning { background: var(--c-warn-light); }
.stat-card.stat-warning .stat-value { color: var(--c-warn); }
.stat-card.stat-info { background: var(--c-primary-light); }
.stat-card.stat-info .stat-value { color: var(--c-primary); }

/* --- Alert Box --- */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin: 12px 0;
  border-left: 4px solid;
}
.alert-danger {
  background: var(--c-danger-light);
  border-color: var(--c-danger);
  color: var(--c-danger);
}
.alert-warning {
  background: var(--c-warn-light);
  border-color: var(--c-warn);
  color: var(--c-warn);
}
.alert-success {
  background: var(--c-success-light);
  border-color: var(--c-success);
  color: var(--c-success);
}

/* --- Button Group --- */
.btn-group {
  display: flex;
  gap: 8px;
}

/* Standalone PWA mode */
@media (display-mode: standalone) {
  .topbar { padding-top: max(12px, env(safe-area-inset-top)); }
  .sidebar { padding-top: max(12px, env(safe-area-inset-top)); }
}

/* Touch-friendly targets for mobile */
@media (pointer: coarse) {
  .btn { min-height: 44px; }
  .sidebar nav a { min-height: 44px; }
  .form-control { min-height: 44px; }
  .table td, .table th { padding: 12px 10px; }
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main-content { margin-left: 0; }
  .topbar .menu-btn { display: block; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .grid-2, .grid-3, .form-row { grid-template-columns: 1fr; }
  .page { padding: 16px; }
  .filters-bar { flex-direction: column; align-items: stretch; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
}
