/* ============================================================
   Launch Fiber Services — App-Shell Design System v1
   ============================================================
   Full design-token system + component library for all 9 portals.
   Aligns with existing admin.html tokens. Adds the shared topbar,
   sidebar, modal, toast, skeleton, and layout primitives that make
   every portal look like ONE cohesive application.

   Load order:
     1. <link rel="stylesheet" href="/css/app-shell.css">
     2. Portal's own <style> block (overrides fine — use tokens)
     3. <script src="/js/app-shell.js">

   Token naming is backward-compatible with the legacy set already
   in admin.html / timeclock.html / design.html. Legacy tokens
   (--white, --gray-light, --gray-border) remain available so
   existing inline styles don't break.
   ============================================================ */

/* ─────────────────────────────────────────────────────────────
   1. DESIGN TOKENS
   ───────────────────────────────────────────────────────────── */
:root {
  /* ── Brand ──────────────────────────────────────────────── */
  --brand-primary:       #1B5FA0;
  --brand-primary-dark:  #134880;
  --brand-primary-light: #E8F0FB;
  --brand-accent:        #0F7B3C;

  /* ── Legacy aliases (keep for backward compat) ────────── */
  --primary:       var(--brand-primary);
  --primary-dark:  var(--brand-primary-dark);
  --primary-light: var(--brand-primary-light);
  --primary-text:  #FFFFFF;

  /* ── Surfaces ────────────────────────────────────────────
     surface-0 = pure white cards on surface-1 page canvas
     surface-1 = page canvas / sidebar bg
     surface-2 = card / panel background
     surface-3 = table header / subtle band              */
  --surface-0: #ffffff;
  --surface-1: #F5F7FA;
  --surface-2: #FFFFFF;
  --surface-3: #F0F0F0;

  /* ── Legacy surface aliases ───────────────────────────── */
  --white:      #FFFFFF;
  --gray-light: #F5F7FA;
  --row-alt:    #F0F0F0;

  /* ── Text ─────────────────────────────────────────────── */
  --text-1:       #212529;
  --text-2:       #495057;
  --text-muted:   #5A6470;

  /* ── Legacy text aliases ──────────────────────────────── */
  --text:           var(--text-1);
  --text-secondary: var(--text-2);

  /* ── Borders ─────────────────────────────────────────── */
  --border-weak:   #EEF1F5;
  --border-strong: #DEE2E6;
  --gray-border:   #DEE2E6;  /* legacy alias */
  --gray:          #6B6B6B;

  /* ── Semantic status ─────────────────────────────────── */
  --success:       #28A745;
  --success-light: #E8F5E9;
  --success-text:  #1B5E20;
  --warning:       #FFC107;
  --warning-light: #FFF8E1;
  --warning-text:  #7A4F0E;
  --danger:        #DC3545;
  --danger-light:  #FDECEA;
  --danger-text:   #B71C1C;
  --info:          #1B5FA0;
  --info-light:    #E8F0FB;
  --info-text:     #0D47A1;

  /* ── Typography ──────────────────────────────────────── */
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', Menlo, 'Courier New', monospace;
  --font:      var(--font-sans);  /* legacy alias */

  /* Type scale */
  --text-xs:   11px;
  --text-sm:   12px;
  --text-base: 14px;
  --text-md:   15px;
  --text-lg:   16px;
  --text-xl:   18px;
  --text-2xl:  22px;
  --text-3xl:  28px;

  /* ── Spacing scale ───────────────────────────────────── */
  --space-1:  4px;
  --space-2:  8px;
  --space-3:  12px;
  --space-4:  16px;
  --space-5:  20px;
  --space-6:  24px;
  --space-8:  32px;
  --space-10: 40px;
  --space-12: 48px;
  --space-16: 64px;

  /* ── Border radius ───────────────────────────────────── */
  --radius-sm: 4px;
  --radius:    8px;
  --radius-md: 8px;   /* alias */
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-pill: 9999px;

  /* ── Shadows ─────────────────────────────────────────── */
  --shadow-xs: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow:    0 2px 8px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.10);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
  --shadow-xl: 0 20px 60px rgba(0,0,0,0.18);

  /* ── Layout shell ─────────────────────────────────────── */
  --topbar-h:  56px;
  --sidebar-w: 240px;
  --sidebar-collapsed-w: 56px;

  /* ── Transitions ─────────────────────────────────────── */
  --transition-fast:   0.12s ease;
  --transition:        0.18s ease;
  --transition-slow:   0.3s ease;

  /* ── Chart ───────────────────────────────────────────── */
  --chart-bar: #BBDEFB;

  /* ── Deep / hover / gradient surface tokens (Wave 202A) ──
     Page-deep canvas (body), card-deep (overrides for popup
     panels), interactive hover state, danger-button hover,
     clock-card gradient stops. Light values keep parity with
     existing surface system; dark block overrides below.    */
  --bg-deep:         #F0F0F0;
  --surface-deep:    #FFFFFF;
  --surface-hover:   #F5F7FA;
  --danger-dark:     #B02A37;
  --clock-grad-from: #E8F0FB;
  --clock-grad-to:   #C5D9EE;

  /* Modal backdrop — Wave 203 */
  --modal-backdrop:  rgba(0, 0, 0, 0.45);
}

/* ─────────────────────────────────────────────────────────────
   2. DARK MODE TOKENS
   ───────────────────────────────────────────────────────────── */
html[data-theme="dark"] {
  /* Brand — lighter so it reads on dark bg (WCAG AA 4.5:1 min) */
  --brand-primary:       #4A90D9;
  --brand-primary-dark:  #1B5FA0;
  --brand-primary-light: #1E3A5C;
  --primary:       #4A90D9;
  --primary-dark:  #1B5FA0;
  --primary-light: #1E3A5C;

  /* Surfaces */
  --surface-0: #111827;
  --surface-1: #1A1F26;
  --surface-2: #1D2430;
  --surface-3: #1F2630;
  --white:      #1D2430;
  --gray-light: #1A1F26;
  --row-alt:    #1F2630;

  /* Text */
  --text-1:         #E8EAED;
  --text-2:         #C2C7CD;
  --text-muted:     #9BA1A8;
  --text:           #E8EAED;
  --text-secondary: #C2C7CD;

  /* Borders */
  --border-weak:   #252A33;
  --border-strong: #2D3540;
  --gray-border:   #2D3540;

  /* Status — brighter on dark bg */
  --success:       #5DD27A;
  --success-light: #1B3D24;
  --success-text:  #A5D6A7;
  --warning:       #FFCA28;
  --warning-light: #3D2F0F;
  --warning-text:  #FFCC80;
  --danger:        #EF5350;
  --danger-light:  #3D1F23;
  --danger-text:   #F8B4BD;
  --info:          #4A90D9;
  --info-light:    #1E3A5C;
  --info-text:     #90CAF9;

  --chart-bar: #3D4A5C;

  /* Wave 202A deep/hover/gradient tokens — dark overrides */
  --bg-deep:         #0F1419;
  --surface-deep:    #1D2430;
  --surface-hover:   #3A4250;
  --danger-dark:     #B02A37;
  --clock-grad-from: #1E3A5C;
  --clock-grad-to:   #0E2440;

  /* Modal backdrop — Wave 203 (higher opacity on dark) */
  --modal-backdrop:  rgba(0, 0, 0, 0.70);

  color-scheme: dark;
}

/* ─────────────────────────────────────────────────────────────
   3. BASE RESET
   ───────────────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  -webkit-text-size-adjust: 100%;
  font-size: 14px;
}

body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  color: var(--text-1);
  background: var(--surface-1);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ─────────────────────────────────────────────────────────────
   4. ACCESSIBILITY
   ───────────────────────────────────────────────────────────── */
/* Screen-reader only utility */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* Visible on keyboard focus only */
.focus-visible,
*:focus-visible {
  outline: 2px solid var(--brand-primary);
  outline-offset: 2px;
}

/* Skip nav */
.skip-nav {
  position: absolute;
  left: -9999px; top: auto;
  width: 1px; height: 1px;
  overflow: hidden;
  z-index: 9999;
  text-decoration: none;
}
.skip-nav:focus {
  position: fixed;
  left: 50%; top: 8px;
  transform: translateX(-50%);
  width: auto; height: auto;
  padding: 10px 20px;
  background: var(--brand-primary);
  color: #fff;
  font-weight: 600;
  border-radius: var(--radius);
  font-size: var(--text-base);
  z-index: 9999;
}

/* ─────────────────────────────────────────────────────────────
   5. APP TOPBAR
   ───────────────────────────────────────────────────────────── */
.app-topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: var(--brand-primary-dark, #134880);
  color: #fff;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-6);
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  flex-shrink: 0;
}

.app-topbar-logo {
  height: 38px;
  width: auto;
  object-fit: contain;
  flex-shrink: 0;
}

.app-topbar-brand {
  font-size: var(--text-lg);
  font-weight: 700;
  letter-spacing: -0.01em;
  color: #fff;
  white-space: nowrap;
  flex-shrink: 0;
}

.app-topbar-divider {
  width: 1px;
  height: 28px;
  background: rgba(255,255,255,0.25);
  flex-shrink: 0;
}

.app-topbar-title {
  font-size: var(--text-sm);
  font-weight: 500;
  color: rgba(255,255,255,0.75);
  letter-spacing: 0.02em;
}

.app-topbar-spacer {
  flex: 1;
}

.app-topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

/* Back-to-launcher button */
.app-topbar-back {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: var(--text-xs);
  font-weight: 500;
  text-decoration: none;
  border: 1px solid rgba(255,255,255,0.22);
  transition: background var(--transition-fast);
  flex-shrink: 0;
}
.app-topbar-back:hover {
  background: rgba(255,255,255,0.22);
  color: #fff;
  text-decoration: none;
}

/* Icon button in topbar */
.app-topbar-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.12);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: var(--text-base);
  transition: background var(--transition-fast);
  flex-shrink: 0;
  text-decoration: none;
}
.app-topbar-icon-btn:hover {
  background: rgba(255,255,255,0.22);
  color: #fff;
}

/* User menu chip */
.app-topbar-user {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 4px 10px 4px 4px;
  border-radius: var(--radius-pill);
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.2);
  cursor: pointer;
  transition: background var(--transition-fast);
  font-size: var(--text-sm);
  color: #fff;
  position: relative;
}
.app-topbar-user:hover {
  background: rgba(255,255,255,0.22);
}
.app-topbar-user-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(255,255,255,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  flex-shrink: 0;
}
.app-topbar-user-name {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.app-topbar-user-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 200px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-2) 0;
  z-index: 200;
  display: none;
}
.app-topbar-user:hover .app-topbar-user-dropdown,
.app-topbar-user[aria-expanded="true"] .app-topbar-user-dropdown {
  display: block;
}
.app-topbar-user-dropdown-item {
  display: block;
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  color: var(--text-1);
  text-decoration: none;
  transition: background var(--transition-fast);
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}
.app-topbar-user-dropdown-item:hover {
  background: var(--surface-3);
  color: var(--text-1);
}
.app-topbar-user-dropdown-divider {
  height: 1px;
  background: var(--border-weak);
  margin: var(--space-2) 0;
}
.app-topbar-user-role {
  font-size: var(--text-xs);
  color: var(--text-muted);
  padding: 0 var(--space-4) var(--space-2);
}

/* ─────────────────────────────────────────────────────────────
   6. APP SIDEBAR
   ───────────────────────────────────────────────────────────── */
.app-sidebar {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  background: var(--surface-2);
  border-right: 1px solid var(--border-weak);
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 90;
  transition: width var(--transition-slow);
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-sm);
}

.app-sidebar.collapsed {
  width: var(--sidebar-collapsed-w);
}

.app-sidebar-nav {
  flex: 1;
  padding: var(--space-3) 0;
  list-style: none;
}

.app-sidebar-section {
  padding: var(--space-2) var(--space-4) var(--space-1);
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
}

.app-sidebar.collapsed .app-sidebar-section {
  opacity: 0;
  height: 0;
  padding: 0;
}

.app-sidebar-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  color: var(--text-2);
  text-decoration: none;
  border-radius: 0;
  transition: background var(--transition-fast), color var(--transition-fast);
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  position: relative;
  white-space: nowrap;
}
.app-sidebar-item i {
  width: 18px;
  text-align: center;
  flex-shrink: 0;
  font-size: var(--text-base);
  color: var(--text-muted);
  transition: color var(--transition-fast);
}
.app-sidebar-item:hover {
  background: var(--brand-primary-light);
  color: var(--brand-primary-dark);
}
.app-sidebar-item:hover i {
  color: var(--brand-primary);
}
.app-sidebar-item.active {
  background: var(--brand-primary-light);
  color: var(--brand-primary-dark);
  font-weight: 600;
}
.app-sidebar-item.active i {
  color: var(--brand-primary);
}
.app-sidebar-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 4px; bottom: 4px;
  width: 3px;
  background: var(--brand-primary);
  border-radius: 0 2px 2px 0;
}

.app-sidebar-badge {
  margin-left: auto;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  background: var(--brand-primary);
  color: #fff;
  border-radius: var(--radius-pill);
  font-size: 10px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
  flex-shrink: 0;
}

.app-sidebar-item-label {
  overflow: hidden;
  transition: opacity var(--transition);
}

.app-sidebar.collapsed .app-sidebar-item-label,
.app-sidebar.collapsed .app-sidebar-badge {
  opacity: 0;
  width: 0;
  overflow: hidden;
}

.app-sidebar-footer {
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border-weak);
}

/* ─────────────────────────────────────────────────────────────
   7. APP CONTENT AREA
   ───────────────────────────────────────────────────────────── */
/* When using topbar + no sidebar */
.app-with-topbar {
  padding-top: var(--topbar-h);
}

/* When using topbar + sidebar */
.app-with-sidebar {
  padding-top: var(--topbar-h);
  padding-left: var(--sidebar-w);
  transition: padding-left var(--transition-slow);
}
.app-with-sidebar.sidebar-collapsed {
  padding-left: var(--sidebar-collapsed-w);
}

.app-content {
  padding: var(--space-6);
  min-height: calc(100vh - var(--topbar-h));
}

.app-content-inner {
  max-width: 1400px;
  margin: 0 auto;
}

/* ─────────────────────────────────────────────────────────────
   8. CARDS
   ───────────────────────────────────────────────────────────── */
.card {
  background: var(--surface-2);
  border: 1px solid var(--border-weak);
  border-radius: var(--radius-lg);
  overflow: visible;
}

.card-header {
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-weak);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface-2);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.card-title {
  font-size: var(--text-md);
  font-weight: 600;
  color: var(--text-1);
}

.card-subtitle {
  font-size: var(--text-sm);
  color: var(--text-muted);
  margin-top: 2px;
}

.card-body {
  padding: var(--space-5);
}

.card-footer {
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--border-weak);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  background: var(--surface-1);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

/* Card with shadow variant */
.card-elevated {
  box-shadow: var(--shadow-md);
  border-color: transparent;
}

/* Stat card */
.stat-card {
  background: var(--surface-2);
  border: 1px solid var(--border-weak);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
}
.stat-card-label {
  font-size: var(--text-sm);
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: var(--space-2);
}
.stat-card-value {
  font-size: var(--text-3xl);
  font-weight: 700;
  color: var(--text-1);
  line-height: 1.1;
}
.stat-card-delta {
  font-size: var(--text-sm);
  margin-top: var(--space-1);
  display: flex;
  align-items: center;
  gap: 4px;
}
.stat-card-delta.up { color: var(--success); }
.stat-card-delta.down { color: var(--danger); }

/* Responsive grid for cards */
.responsive-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--space-4);
}
.responsive-grid-2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-4);
}
.responsive-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-4);
}
.responsive-grid-4 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-4);
}

/* ─────────────────────────────────────────────────────────────
   9. BUTTONS
   ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 7px 14px;
  border-radius: var(--radius);
  font-size: var(--text-sm);
  font-weight: 500;
  font-family: var(--font-sans);
  cursor: pointer;
  border: none;
  transition: background var(--transition-fast), box-shadow var(--transition-fast), color var(--transition-fast);
  white-space: nowrap;
  text-decoration: none;
  line-height: 1.4;
  vertical-align: middle;
  position: relative;
}
.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
  pointer-events: none;
}
.btn:focus-visible {
  outline: 2px solid var(--brand-primary);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--brand-primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--brand-primary-dark);
  color: #fff;
  text-decoration: none;
}
.btn-primary:active:not(:disabled) {
  background: var(--brand-primary-dark);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.15);
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text-1);
  border: 1px solid var(--border-strong);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--surface-1);
  color: var(--text-1);
  text-decoration: none;
}
html[data-theme="dark"] .btn-secondary {
  background: #3A4250;
  border-color: var(--border-strong);
}
html[data-theme="dark"] .btn-secondary:hover:not(:disabled) {
  background: #4A5260;
}

.btn-danger {
  background: var(--surface-2);
  color: var(--danger);
  border: 1px solid #F5C6CB;
}
.btn-danger:hover:not(:disabled) {
  background: var(--danger-light);
  color: var(--danger);
  text-decoration: none;
}

.btn-success {
  background: var(--success);
  color: #fff;
}
.btn-success:hover:not(:disabled) {
  background: #218838;
  color: #fff;
  text-decoration: none;
}

.btn-ghost {
  background: transparent;
  color: var(--text-2);
  border: none;
}
.btn-ghost:hover:not(:disabled) {
  background: var(--surface-3);
  color: var(--text-1);
  text-decoration: none;
}

.btn-link {
  background: transparent;
  color: var(--brand-primary);
  border: none;
  padding: 0;
  font-size: inherit;
  font-weight: inherit;
}
.btn-link:hover:not(:disabled) {
  text-decoration: underline;
  color: var(--brand-primary-dark);
}

/* Sizes */
.btn-xs { padding: 3px 8px; font-size: var(--text-xs); border-radius: var(--radius-sm); }
.btn-sm { padding: 4px 10px; font-size: var(--text-sm); }
.btn-lg { padding: 10px 20px; font-size: var(--text-md); }
.btn-xl { padding: 13px 26px; font-size: var(--text-lg); }

/* Icon-only */
.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  justify-content: center;
  border-radius: var(--radius);
}
.btn-icon.btn-sm { width: 28px; height: 28px; }
.btn-icon.btn-lg { width: 40px; height: 40px; }

/* Loading spinner inside button */
.btn-loading::after {
  content: '';
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: btn-spin 0.7s linear infinite;
}
@keyframes btn-spin {
  to { transform: rotate(360deg); }
}

/* ─────────────────────────────────────────────────────────────
   10. FORM CONTROLS
   ───────────────────────────────────────────────────────────── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-4);
}
.form-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-4);
}
.form-group.span2 { grid-column: span 2; }
.form-group.span3 { grid-column: span 3; }

label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-1);
}
.form-label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-1);
  display: block;
  margin-bottom: var(--space-1);
}
.form-label-required::after {
  content: ' *';
  color: var(--danger);
  font-weight: 400;
}

.input,
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="url"],
input[type="search"],
input[type="date"],
input[type="datetime-local"],
select,
textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: var(--text-sm);
  font-family: var(--font-sans);
  color: var(--text-1);
  background: var(--surface-2);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  outline: none;
  line-height: 1.5;
}
.input:focus,
input:focus,
select:focus,
textarea:focus {
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 3px rgba(27,95,160,0.12);
}
.input:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: var(--surface-3);
}
.input.error,
input.error,
select.error {
  border-color: var(--danger);
}
.input.error:focus,
input.error:focus {
  box-shadow: 0 0 0 3px rgba(220,53,69,0.12);
}

.form-hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
}
.form-error {
  font-size: var(--text-xs);
  color: var(--danger);
  display: flex;
  align-items: center;
  gap: 4px;
}

.input-group {
  display: flex;
}
.input-group .input,
.input-group input {
  border-radius: var(--radius) 0 0 var(--radius);
  flex: 1;
}
.input-group-addon {
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-left: none;
  padding: 7px 10px;
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: var(--text-sm);
  color: var(--text-muted);
  white-space: nowrap;
  display: flex;
  align-items: center;
}

select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%236B6B6B' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

textarea {
  resize: vertical;
  min-height: 80px;
}

/* Checkbox + radio */
.form-check {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
}
.form-check input[type="checkbox"],
.form-check input[type="radio"] {
  width: auto;
  accent-color: var(--brand-primary);
  cursor: pointer;
}
.form-check-label {
  font-size: var(--text-sm);
  color: var(--text-1);
  cursor: pointer;
}

html[data-theme="dark"] .input,
html[data-theme="dark"] input[type="text"],
html[data-theme="dark"] input[type="email"],
html[data-theme="dark"] input[type="password"],
html[data-theme="dark"] input[type="number"],
html[data-theme="dark"] input[type="tel"],
html[data-theme="dark"] input[type="date"],
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
  background: var(--surface-2);
  color: var(--text-1);
  border-color: var(--border-strong);
}

html[data-theme="dark"] select option {
  background: var(--surface-2);
  color: var(--text-1);
}

/* ─────────────────────────────────────────────────────────────
   11. MODALS
   ───────────────────────────────────────────────────────────── */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}
.modal-backdrop.open {
  display: flex;
}
/* Also support old class name used in portals */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open {
  display: flex;
}
html[data-theme="dark"] .modal-backdrop,
html[data-theme="dark"] .modal-overlay {
  background: rgba(0,0,0,0.65);
}

.modal,
.modal-content,
.modal-dialog {
  background: var(--surface-2);
  border-radius: var(--radius-xl);
  width: 90%;
  max-width: 680px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-xl);
  display: flex;
  flex-direction: column;
}
.modal-sm  { max-width: 440px; }
.modal-lg  { max-width: 860px; }
.modal-xl  { max-width: 1100px; }
.modal-full { max-width: 98vw; max-height: 98vh; }

.modal-header {
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--border-weak);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--surface-2);
  z-index: 1;
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  flex-shrink: 0;
}
html[data-theme="dark"] .modal-header {
  background: var(--surface-2);
  border-color: var(--border-weak);
}

.modal-title {
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--text-1);
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: var(--text-xl);
  line-height: 1;
  padding: var(--space-1);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition-fast), color var(--transition-fast);
}
.modal-close:hover {
  background: var(--surface-3);
  color: var(--text-1);
}

.modal-body {
  padding: var(--space-6);
  flex: 1;
  overflow-y: auto;
}

.modal-footer {
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border-weak);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  background: var(--surface-1);
  border-radius: 0 0 var(--radius-xl) var(--radius-xl);
  position: sticky;
  bottom: 0;
  flex-shrink: 0;
}
html[data-theme="dark"] .modal-footer {
  background: var(--surface-2);
  border-color: var(--border-weak);
}

/* ─────────────────────────────────────────────────────────────
   12. TOAST NOTIFICATIONS
   ───────────────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  top: calc(var(--topbar-h) + 12px);
  right: 14px;
  z-index: 99999;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: 360px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  background: var(--surface-2);
  border: 1px solid var(--border-weak);
  border-left: 4px solid var(--text-muted);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow-md);
  font-size: var(--text-base);
  line-height: 1.4;
  color: var(--text-1);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: toast-in 0.18s ease-out;
}
.toast.toast-success { border-left-color: var(--success); }
.toast.toast-error   { border-left-color: var(--danger); }
.toast.toast-warning { border-left-color: var(--warning); }
.toast.toast-info    { border-left-color: var(--info); }
.toast.toast-leaving { animation: toast-out 0.15s ease-in forwards; }

.toast-message { flex: 1; word-break: break-word; }
.toast-action-btn {
  background: transparent;
  border: 0;
  color: var(--brand-primary);
  font-weight: 500;
  cursor: pointer;
  padding: 0;
  font-size: var(--text-sm);
  white-space: nowrap;
}
.toast-action-btn:hover { text-decoration: underline; }
.toast-close-btn {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  cursor: pointer;
  font-size: var(--text-lg);
  line-height: 1;
  padding: 0 0 0 4px;
  flex-shrink: 0;
}

@keyframes toast-in  { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes toast-out { from { transform: translateX(0); opacity: 1; } to { transform: translateX(20px); opacity: 0; } }

/* ─────────────────────────────────────────────────────────────
   13. EMPTY STATE
   ───────────────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: var(--space-12) var(--space-6);
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}
.empty-state-icon {
  font-size: 42px;
  opacity: 0.28;
  line-height: 1;
}
.empty-state-title {
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--text-2);
}
.empty-state-desc {
  font-size: var(--text-sm);
  color: var(--text-muted);
  max-width: 340px;
  line-height: 1.6;
}

/* ─────────────────────────────────────────────────────────────
   14. LOADING SKELETONS
   ───────────────────────────────────────────────────────────── */
.skeleton {
  background: var(--surface-3);
  border-radius: var(--radius-sm);
  overflow: hidden;
  position: relative;
}
.skeleton::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg,
    transparent 0%,
    rgba(255,255,255,0.5) 50%,
    transparent 100%
  );
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s ease-in-out infinite;
}
html[data-theme="dark"] .skeleton::after {
  background: linear-gradient(90deg,
    transparent 0%,
    rgba(255,255,255,0.08) 50%,
    transparent 100%
  );
  background-size: 200% 100%;
}
@keyframes skeleton-shimmer {
  0%   { background-position: -200% 0; }
  100% { background-position:  200% 0; }
}

/* Pre-made skeleton shapes */
.skeleton-text   { height: 14px; margin-bottom: var(--space-2); }
.skeleton-text.wide    { width: 80%; }
.skeleton-text.medium  { width: 60%; }
.skeleton-text.narrow  { width: 40%; }
.skeleton-heading      { height: 22px; width: 50%; margin-bottom: var(--space-3); }
.skeleton-avatar       { width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; }
.skeleton-btn          { height: 32px; width: 100px; border-radius: var(--radius); }
.skeleton-card         { height: 120px; border-radius: var(--radius-lg); }

/* ─────────────────────────────────────────────────────────────
   15. BADGES + STATUS CHIPS
   ───────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: var(--text-xs);
  font-weight: 600;
  white-space: nowrap;
}
.badge-success  { background: var(--success-light);  color: var(--success-text);  }
.badge-warning  { background: var(--warning-light);  color: var(--warning-text);  }
.badge-danger   { background: var(--danger-light);   color: var(--danger-text);   }
.badge-info     { background: var(--info-light);     color: var(--info-text);     }
.badge-primary  { background: var(--brand-primary-light); color: var(--brand-primary-dark); }
.badge-neutral  { background: var(--surface-3);      color: var(--text-muted);    }

/* Status dot */
.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-dot.active  { background: var(--success); }
.status-dot.warning { background: var(--warning); }
.status-dot.error   { background: var(--danger);  }
.status-dot.muted   { background: var(--text-muted); }

/* ─────────────────────────────────────────────────────────────
   16. TABLES
   ───────────────────────────────────────────────────────────── */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-weak);
}

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}
table.data-table thead th {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  background: var(--surface-1);
  border-bottom: 1px solid var(--border-weak);
  white-space: nowrap;
}
table.data-table tbody td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-weak);
  color: var(--text-1);
  vertical-align: middle;
}
table.data-table tbody tr:last-child td {
  border-bottom: none;
}
table.data-table tbody tr:hover td {
  background: var(--surface-1);
}
html[data-theme="dark"] table.data-table tbody tr:hover td {
  background: var(--surface-3);
}

/* ─────────────────────────────────────────────────────────────
   17. ALERTS / INLINE BANNERS
   ───────────────────────────────────────────────────────────── */
.alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  font-size: var(--text-sm);
  border: 1px solid transparent;
}
.alert-success { background: var(--success-light); color: var(--success-text); border-color: #c3e6cb; }
.alert-warning { background: var(--warning-light); color: var(--warning-text); border-color: #ffeeba; }
.alert-danger  { background: var(--danger-light);  color: var(--danger-text);  border-color: #f5c6cb; }
.alert-info    { background: var(--info-light);    color: var(--info-text);    border-color: #b8daff; }
.alert-icon    { flex-shrink: 0; margin-top: 1px; }

/* ─────────────────────────────────────────────────────────────
   18. TYPOGRAPHY UTILITIES
   ───────────────────────────────────────────────────────────── */
h1 { font-size: var(--text-3xl); font-weight: 700; color: var(--text-1); line-height: 1.2; }
h2 { font-size: var(--text-2xl); font-weight: 700; color: var(--text-1); line-height: 1.3; }
h3 { font-size: var(--text-xl);  font-weight: 600; color: var(--text-1); line-height: 1.4; }
h4 { font-size: var(--text-lg);  font-weight: 600; color: var(--text-1); line-height: 1.4; }
h5 { font-size: var(--text-md);  font-weight: 600; color: var(--text-1); line-height: 1.5; }
h6 { font-size: var(--text-base);font-weight: 600; color: var(--text-1); line-height: 1.5; }

.page-title    { font-size: var(--text-2xl); font-weight: 700; color: var(--text-1); }
.section-title { font-size: var(--text-lg);  font-weight: 600; color: var(--text-1); }
.text-muted    { color: var(--text-muted); }
.text-secondary { color: var(--text-2); }
.text-success  { color: var(--success); }
.text-danger   { color: var(--danger);  }
.text-warning  { color: var(--warning); }
.text-primary  { color: var(--brand-primary); }
.text-sm       { font-size: var(--text-sm); }
.text-xs       { font-size: var(--text-xs); }
.text-mono     { font-family: var(--font-mono); }
.font-medium   { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold     { font-weight: 700; }
.truncate      { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ─────────────────────────────────────────────────────────────
   19. LAYOUT UTILITIES
   ───────────────────────────────────────────────────────────── */
.flex         { display: flex; }
.flex-col     { display: flex; flex-direction: column; }
.items-center { align-items: center; }
.items-start  { align-items: flex-start; }
.justify-between { justify-content: space-between; }
.justify-end     { justify-content: flex-end; }
.justify-center  { justify-content: center; }
.flex-1          { flex: 1; }
.flex-wrap       { flex-wrap: wrap; }
.gap-1  { gap: var(--space-1); }
.gap-2  { gap: var(--space-2); }
.gap-3  { gap: var(--space-3); }
.gap-4  { gap: var(--space-4); }
.gap-6  { gap: var(--space-6); }

.p-0  { padding: 0; }
.p-2  { padding: var(--space-2); }
.p-4  { padding: var(--space-4); }
.p-6  { padding: var(--space-6); }
.mt-2 { margin-top: var(--space-2); }
.mt-4 { margin-top: var(--space-4); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-4 { margin-bottom: var(--space-4); }
.ml-auto { margin-left: auto; }

.w-full  { width: 100%; }
.h-full  { height: 100%; }

.rounded    { border-radius: var(--radius); }
.rounded-lg { border-radius: var(--radius-lg); }
.rounded-full { border-radius: var(--radius-pill); }

.shadow    { box-shadow: var(--shadow); }
.shadow-md { box-shadow: var(--shadow-md); }
.shadow-lg { box-shadow: var(--shadow-lg); }

.border       { border: 1px solid var(--border-weak); }
.border-strong { border: 1px solid var(--border-strong); }

.hidden { display: none !important; }
.invisible { visibility: hidden; }
.no-print { }

/* ─────────────────────────────────────────────────────────────
   20. DIVIDER
   ───────────────────────────────────────────────────────────── */
.divider {
  height: 1px;
  background: var(--border-weak);
  margin: var(--space-4) 0;
}
.divider-vertical {
  width: 1px;
  background: var(--border-weak);
  align-self: stretch;
}

/* ─────────────────────────────────────────────────────────────
   21. CODE BLOCKS
   ───────────────────────────────────────────────────────────── */
code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--surface-3);
  padding: 2px 5px;
  border-radius: var(--radius-sm);
  color: var(--text-1);
}
pre {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  background: var(--surface-3);
  padding: var(--space-4);
  border-radius: var(--radius);
  overflow-x: auto;
  line-height: 1.6;
}

/* ─────────────────────────────────────────────────────────────
   22. RESPONSIVE BREAKPOINTS
   ───────────────────────────────────────────────────────────── */

/* Tablet */
@media (max-width: 1024px) {
  .responsive-grid-4 { grid-template-columns: repeat(2, 1fr); }
  .responsive-grid-3 { grid-template-columns: repeat(2, 1fr); }
}

/* Mobile */
@media (max-width: 768px) {
  :root {
    --topbar-h: 52px;
    --sidebar-w: 220px;
  }

  .app-content { padding: var(--space-4); }

  .app-sidebar {
    transform: translateX(-100%);
    transition: transform var(--transition-slow);
    z-index: 150;
  }
  .app-sidebar.open {
    transform: translateX(0);
  }
  .app-with-sidebar { padding-left: 0; }

  .responsive-grid,
  .responsive-grid-2,
  .responsive-grid-3,
  .responsive-grid-4 {
    grid-template-columns: 1fr;
  }

  .form-grid,
  .form-grid-3 {
    grid-template-columns: 1fr;
  }
  .form-group.span2,
  .form-group.span3 { grid-column: span 1; }

  .modal, .modal-content, .modal-dialog {
    width: 98%;
    max-height: 95vh;
  }

  .app-topbar { padding: 0 var(--space-4); gap: var(--space-2); }
  .app-topbar-title { display: none; }
  .app-topbar-user-name { display: none; }

  .toast-container { left: 8px; right: 8px; top: calc(var(--topbar-h) + 8px); max-width: none; }

  button:not([class*="btn-icon"]):not(.modal-close),
  input:not([type="checkbox"]):not([type="radio"]),
  select, textarea {
    min-height: 40px;
    font-size: 16px; /* prevent iOS zoom */
  }

  table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

@media (max-width: 480px) {
  .app-content { padding: var(--space-3); }
  .card-body { padding: var(--space-4); }
  .modal-body { padding: var(--space-4); }
  .modal-header { padding: var(--space-4); }
  .modal-footer { padding: var(--space-3) var(--space-4); }
  .btn { padding: 8px 12px; }
  h1 { font-size: var(--text-2xl); }
  h2 { font-size: var(--text-xl); }
}

/* ─────────────────────────────────────────────────────────────
   23. PRINT
   ───────────────────────────────────────────────────────────── */
@media print {
  .no-print,
  .app-topbar,
  .app-sidebar,
  nav, .nav, button,
  .modal-backdrop,
  .toast-container { display: none !important; }
  body { background: #fff !important; color: #000 !important; }
  .app-with-topbar,
  .app-with-sidebar { padding: 0 !important; }
  .card { box-shadow: none; border-color: #ccc; }
}

/* ─────────────────────────────────────────────────────────────
   24. REDUCED MOTION
   ───────────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

/* ─────────────────────────────────────────────────────────────
   25. DARK MODE COMPONENT OVERRIDES
   ───────────────────────────────────────────────────────────── */
html[data-theme="dark"] body {
  background: #0F1419;
}
html[data-theme="dark"] .card,
html[data-theme="dark"] .card-header,
html[data-theme="dark"] .card-footer,
html[data-theme="dark"] .modal,
html[data-theme="dark"] .modal-content,
html[data-theme="dark"] .modal-dialog {
  background: var(--surface-2);
  border-color: var(--border-weak);
}
html[data-theme="dark"] .modal-backdrop,
html[data-theme="dark"] .modal-overlay {
  background: rgba(0,0,0,0.65);
}
html[data-theme="dark"] table.data-table thead th {
  background: var(--surface-2);
  color: var(--text-muted);
}
html[data-theme="dark"] table.data-table tbody td {
  border-color: var(--border-weak);
}
html[data-theme="dark"] .stat-card {
  background: var(--surface-2);
  border-color: var(--border-weak);
}
html[data-theme="dark"] .app-sidebar {
  background: var(--surface-2);
  border-color: var(--border-weak);
}
html[data-theme="dark"] .alert-success { background: var(--success-light); color: var(--success-text); }
html[data-theme="dark"] .alert-warning { background: var(--warning-light); color: var(--warning-text); }
html[data-theme="dark"] .alert-danger  { background: var(--danger-light);  color: var(--danger-text); }
html[data-theme="dark"] .alert-info    { background: var(--info-light);     color: var(--info-text); }
