@import url('design-system.css');

/* ============================================
   Inventory & Accounting System
   App-Specific Styles
   v3.2 - Immersive UI/UX Pro Max
   ============================================ */

/* ============================================
   LAYOUT SHELL
   ============================================ */

.app-shell {
  display: flex;
  min-height: 100vh;
  background: var(--bg);
}

/* Sidebar */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: var(--z-sidebar);
  display: flex;
  flex-direction: column;
  transition: width var(--transition-slow), transform var(--transition-slow);
  overflow: hidden;
}

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

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--topbar-height);
  padding: 0 var(--space-4);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-brand-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, var(--primary-500), var(--primary-700));
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
}

.sidebar-brand-text {
  flex: 1;
  min-width: 0;
  transition: opacity var(--transition-fast);
}

.sidebar.is-collapsed .sidebar-brand-text {
  opacity: 0;
  width: 0;
  overflow: hidden;
  pointer-events: none;
}

.sidebar-brand-text h5 {
  font-size: var(--text-sm);
  font-weight: var(--font-bold);
  margin: 0;
  color: var(--text-primary);
  white-space: nowrap;
}

.sidebar-brand-text small {
  font-size: var(--text-xs);
  color: var(--text-muted);
  white-space: nowrap;
}

.sidebar-toggle {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  background: transparent;
  border: none;
  cursor: pointer;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.sidebar-toggle:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

/* Collapsed sidebar: center the toggle and hide brand content */
.sidebar.is-collapsed .sidebar-brand {
  justify-content: center;
  padding: 0 var(--space-2);
}

.sidebar.is-collapsed .sidebar-brand-icon {
  display: none;
}

.sidebar.is-collapsed .sidebar-toggle {
  margin: 0;
}

.sidebar-search {
  padding: var(--space-3) var(--space-4);
  flex-shrink: 0;
}

.sidebar-search-input {
  width: 100%;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3) var(--space-2) var(--space-8);
  color: var(--text-primary);
  font-size: var(--text-xs);
  transition: all var(--transition-fast);
}

.sidebar-search-input:focus {
  outline: none;
  border-color: var(--primary-500);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.sidebar-search-input::placeholder {
  color: var(--text-muted);
}

.sidebar-search-icon {
  position: absolute;
  left: var(--space-5);
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.sidebar-nav-wrap {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 var(--space-3);
}

.sidebar-nav {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-section-title {
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: var(--space-4) var(--space-3) var(--space-2);
  transition: opacity var(--transition-fast);
}

.sidebar.is-collapsed .nav-section-title {
  opacity: 0;
  height: 0;
  padding: 0;
  overflow: hidden;
}

.nav-group {
  margin-bottom: var(--space-1);
}

.nav-group-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  cursor: pointer;
  transition: all var(--transition-fast);
  white-space: nowrap;
}

.nav-group-toggle:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.nav-group-toggle.active {
  background: rgba(99, 102, 241, 0.12);
  color: var(--primary-400);
}

.nav-group-toggle .nav-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.nav-group-toggle .nav-arrow {
  margin-left: auto;
  width: 16px;
  height: 16px;
  transition: transform var(--transition-fast);
  flex-shrink: 0;
}

.nav-group-toggle[aria-expanded="true"] .nav-arrow {
  transform: rotate(180deg);
}

.sidebar.is-collapsed .nav-group-toggle .nav-arrow,
.sidebar.is-collapsed .nav-group-toggle .nav-label {
  opacity: 0;
  width: 0;
  overflow: hidden;
}

.nav-group-items {
  list-style: none;
  padding: 0;
  margin: 0;
  overflow: hidden;
}

.nav-group-items .nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3) var(--space-2) var(--space-8);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  transition: all var(--transition-fast);
  white-space: nowrap;
}

.nav-group-items .nav-link:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.nav-group-items .nav-link.active {
  background: rgba(99, 102, 241, 0.1);
  color: var(--primary-400);
}

.nav-group-items .nav-link .nav-icon {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.5;
  flex-shrink: 0;
}

.sidebar.is-collapsed .nav-group-items {
  display: none;
}

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

.sidebar-user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.sidebar-user-avatar {
  width: 34px;
  height: 34px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--primary-500), var(--primary-700));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: var(--text-xs);
  font-weight: var(--font-bold);
  flex-shrink: 0;
}

.sidebar-user-info {
  flex: 1;
  min-width: 0;
  transition: opacity var(--transition-fast);
}

.sidebar.is-collapsed .sidebar-user-info {
  opacity: 0;
}

.sidebar-user-name {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-user-role {
  font-size: var(--text-xs);
  color: var(--text-muted);
  text-transform: capitalize;
}

.sidebar-logout {
  color: var(--text-muted);
  padding: var(--space-1);
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.sidebar-logout:hover {
  color: var(--danger);
  background: var(--danger-subtle);
}

/* Sidebar backdrop — z-index MUST be between main-content and sidebar so sidebar stays clickable */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: calc(var(--z-sidebar) - 1);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  pointer-events: none;
}

.sidebar-backdrop.show {
  display: block;
  pointer-events: auto;
}

/* Main content area */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: margin-left var(--transition-slow);
}

.sidebar.is-collapsed ~ .main-content {
  margin-left: var(--sidebar-width-collapsed);
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  height: var(--topbar-height);
  background: var(--topbar-bg);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 0 var(--space-5);
  display: flex;
  align-items: center;
  gap: var(--space-4);
}

html[data-theme="dark"] .topbar {
  background: rgba(15, 17, 21, 0.92) !important;
}

.topbar-mobile-toggle {
  display: none;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  cursor: pointer;
}

.topbar-mobile-toggle:hover {
  background: var(--surface-raised);
  color: var(--text-primary);
}

.topbar-title {
  flex: 1;
  min-width: 0;
}

.topbar-title h1 {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  margin: 0;
  color: var(--text-primary);
}

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

.topbar-btn {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition-fast);
  position: relative;
}

.topbar-btn:hover {
  background: var(--surface-raised);
  color: var(--text-primary);
  border-color: var(--border);
}

.topbar-btn .badge-dot {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 8px;
  height: 8px;
  background: var(--danger);
  border-radius: 50%;
  border: 2px solid var(--surface);
}

.topbar-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--primary-500), var(--primary-700));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: var(--text-xs);
  font-weight: var(--font-bold);
}

/* Global search */
.global-search {
  position: relative;
  width: 320px;
  max-width: 100%;
}

.global-search-input {
  width: 100%;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: var(--space-2) var(--space-3) var(--space-2) var(--space-8);
  color: var(--text-primary);
  font-size: var(--text-sm);
  transition: all var(--transition-fast);
}

.global-search-input:focus {
  outline: none;
  border-color: var(--primary-500);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.global-search-icon {
  position: absolute;
  left: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
}

.global-search-kbd {
  position: absolute;
  right: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  font-size: var(--text-xs);
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 var(--space-1);
}

/* Global search results dropdown */
.global-search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: var(--space-2);
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  max-height: 400px;
  overflow-y: auto;
  z-index: var(--z-dropdown);
  display: none;
}

.global-search-loading,
.global-search-empty {
  padding: var(--space-4);
  text-align: center;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.global-search-group {
  border-bottom: 1px solid var(--border);
}

.global-search-group:last-child {
  border-bottom: none;
}

.global-search-group-header {
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--surface);
}

.global-search-item {
  display: flex;
  flex-direction: column;
  padding: var(--space-2) var(--space-3);
  text-decoration: none;
  color: var(--text-primary);
  transition: background var(--transition-fast);
}

.global-search-item:hover {
  background: var(--primary-50);
  text-decoration: none;
}

.global-search-item-title {
  font-size: var(--text-sm);
  font-weight: 500;
}

.global-search-item-subtitle {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Page content */
.page-content {
  flex: 1;
  padding: var(--space-6);
}

.content-wrapper {
  max-width: 1600px;
  margin: 0 auto;
}

/* Page header */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.page-header-title {
  flex: 1;
  min-width: 0;
}

.page-header-title h2 {
  font-size: var(--text-2xl);
  font-weight: var(--font-bold);
  margin: 0 0 var(--space-1);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.page-header-title p {
  margin: 0;
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

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

/* ============================================
   COMPONENTS
   ============================================ */

/* KPI / Stat Cards */
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  transition: all var(--transition-fast);
  height: 100%;
  position: relative;
  overflow: hidden;
}

.stat-card:hover {
  border-color: var(--border-strong);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.stat-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--primary-500);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

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

.stat-card-primary::before { background: var(--primary-500); }
.stat-card-success::before { background: var(--success); }
.stat-card-warning::before { background: var(--warning); }
.stat-card-danger::before { background: var(--danger); }
.stat-card-info::before { background: var(--info); }

.stat-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.stat-card-icon {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.stat-card-icon.primary { background: linear-gradient(135deg, var(--primary-500), var(--primary-700)); }
.stat-card-icon.success { background: linear-gradient(135deg, var(--success), var(--success)); }
.stat-card-icon.warning { background: linear-gradient(135deg, var(--warning), var(--warning)); }
.stat-card-icon.danger { background: linear-gradient(135deg, var(--danger), var(--danger)); }
.stat-card-icon.info { background: linear-gradient(135deg, var(--info), var(--info)); }

.stat-card-label {
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.stat-card-value {
  font-size: var(--text-3xl);
  font-weight: var(--font-bold);
  color: var(--text-primary);
  line-height: var(--leading-tight);
  margin-bottom: var(--space-2);
  font-family: var(--font-mono);
}

.stat-card-trend {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  padding: 0.125rem var(--space-2);
  border-radius: var(--radius-full);
}

.stat-card-trend.up {
  background: var(--success-subtle);
  color: var(--success);
}

.stat-card-trend.down {
  background: var(--danger-subtle);
  color: var(--danger);
}

.stat-card-trend.neutral {
  background: var(--surface-raised);
  color: var(--text-secondary);
}

/* KPI mini sparkline */
.stat-sparkline {
  height: 32px;
  margin-top: var(--space-3);
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: var(--space-12) var(--space-6);
}

.empty-state-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto var(--space-4);
  border-radius: var(--radius-xl);
  background: var(--surface-raised);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.empty-state h4 {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  margin-bottom: var(--space-2);
}

.empty-state p {
  color: var(--text-secondary);
  font-size: var(--text-sm);
  margin-bottom: var(--space-4);
  max-width: 360px;
  margin-left: auto;
  margin-right: auto;
}

/* Filter bar */
.filter-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  margin-bottom: var(--space-5);
}

.filter-bar-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-3);
}

.filter-group {
  flex: 1;
  min-width: 160px;
}

.filter-group label {
  display: block;
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  color: var(--text-secondary);
  margin-bottom: var(--space-1);
}

.filter-actions {
  display: flex;
  gap: var(--space-2);
}

/* Data table toolbar */
.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  flex-wrap: wrap;
}

.table-toolbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.table-wrapper {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  overflow: hidden;
}

/* ERP Table enhancements */
.table-wrapper .table {
  margin-bottom: 0;
}

.table-wrapper .table th {
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  background: var(--bg-elevated);
  border-bottom: 2px solid var(--border);
  padding: var(--space-3) var(--space-4);
  white-space: nowrap;
}

.table-wrapper .table td {
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  vertical-align: middle;
  border-bottom: 1px solid var(--border-subtle);
}

.table-wrapper .table tbody tr:hover {
  background: var(--surface-hover);
}

/* Financial value styling */
.table .text-end {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
}

.table .text-danger {
  color: var(--danger) !important;
  font-weight: var(--font-medium);
}

.table .text-success {
  color: var(--success) !important;
  font-weight: var(--font-medium);
}

/* Sticky header for long tables */
.table-responsive {
  max-height: 600px;
  overflow-y: auto;
}

.table-responsive .table thead th {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background: var(--bg-elevated);
  border-bottom: 2px solid var(--border);
}

/* Status pill */
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.375em;
  padding: 0.25em 0.625em;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
}

.status-pill::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.status-pill.paid,
.status-pill.completed,
.status-pill.active,
.status-pill.ok { background: var(--success-subtle); color: var(--success); }

.status-pill.partial,
.status-pill.warning,
.status-pill.pending { background: var(--warning-subtle); color: var(--warning); }

.status-pill.draft,
.status-pill.secondary { background: var(--surface-raised); color: var(--text-secondary); }

.status-pill.cancelled,
.status-pill.danger,
.status-pill.low { background: var(--danger-subtle); color: var(--danger); }

.status-pill.confirmed,
.status-pill.primary { background: var(--info-subtle); color: var(--info); }

/* Action dropdown */
.action-dropdown .dropdown-toggle {
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: var(--radius-md);
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition-fast);
}

.action-dropdown .dropdown-toggle:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.action-dropdown .dropdown-toggle::after {
  display: none;
}

/* Quick action tiles */
.quick-actions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--space-4);
}

.quick-action-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  text-decoration: none;
  transition: all var(--transition-fast);
}

.quick-action-tile:hover {
  border-color: var(--primary-500);
  background: rgba(99, 102, 241, 0.08);
  color: var(--text-primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-glow);
}

.quick-action-tile-icon {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-md);
  background: var(--surface-raised);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--primary-400);
}

.quick-action-tile span {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  text-align: center;
}

/* Timeline / Feed */
.feed-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.feed-item {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--border-subtle);
}

.feed-item:last-child {
  border-bottom: none;
}

.feed-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.feed-icon.sale { background: var(--success-subtle); color: var(--success); }
.feed-icon.purchase { background: var(--info-subtle); color: var(--info); }
.feed-icon.payment { background: var(--primary-500); color: white; }

.feed-content {
  flex: 1;
  min-width: 0;
}

.feed-title {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--text-primary);
  margin-bottom: var(--space-1);
}

.feed-meta {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.feed-amount {
  text-align: right;
  flex-shrink: 0;
}

.feed-amount .amount {
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  font-family: var(--font-mono);
}

.feed-amount .time {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Section divider */
.section-title {
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: var(--space-3);
}

/* Form section */
.form-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: var(--space-5);
  overflow: hidden;
}

.form-section-header {
  padding: var(--space-4) var(--space-5);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.form-section-header h5 {
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  margin: 0;
  color: var(--text-primary);
}

.form-section-header p {
  font-size: var(--text-xs);
  color: var(--text-muted);
  margin: var(--space-1) 0 0;
}

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

/* Sticky form actions */
.form-actions-sticky {
  position: sticky;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: var(--space-4) 0;
  margin-top: var(--space-6);
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  z-index: var(--z-sticky);
}

/* Invoice line items */
.line-items-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

.line-items-table th {
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: var(--space-3);
  border-bottom: 1px solid var(--border);
}

.line-items-table td {
  padding: var(--space-2);
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: top;
}

.line-item-product-select {
  min-width: 240px;
}

.line-item-stock-hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
  margin-top: var(--space-1);
}

/* Totals panel */
.totals-panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
}

.totals-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-2) 0;
  font-size: var(--text-sm);
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-subtle);
}

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

.totals-row.total {
  font-size: var(--text-lg);
  font-weight: var(--font-bold);
  color: var(--text-primary);
  padding-top: var(--space-3);
  margin-top: var(--space-2);
  border-top: 2px solid var(--border);
}

/* Contextual alerts banner */
.context-alerts {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}

.context-alert {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
}

.context-alert.warning {
  background: var(--warning-subtle);
  color: var(--warning);
}

.context-alert.danger {
  background: var(--danger-subtle);
  color: var(--danger);
}

.context-alert.info {
  background: var(--info-subtle);
  color: var(--info);
}

/* ============================================
   AUTH PAGES
   ============================================ */

.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at 20% 30%, rgba(99, 102, 241, 0.06) 0%, transparent 40%),
    radial-gradient(circle at 80% 70%, rgba(16, 185, 129, 0.04) 0%, transparent 40%),
    var(--bg);
  padding: var(--space-6);
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--topbar-bg-auth);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: var(--space-8);
  box-shadow: var(--shadow-xl);
}

html[data-theme="dark"] .auth-card {
  background: rgba(22, 25, 30, 0.97) !important;
}

.auth-brand {
  text-align: center;
  margin-bottom: var(--space-8);
}

.auth-brand-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto var(--space-4);
  border-radius: var(--radius-lg);
  background: linear-gradient(135deg, var(--primary-500), var(--primary-700));
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.auth-brand h3 {
  font-size: var(--text-xl);
  font-weight: var(--font-bold);
  margin: 0 0 var(--space-1);
  color: var(--text-primary);
}

.auth-brand p {
  color: var(--text-secondary);
  font-size: var(--text-sm);
  margin: 0;
}

.auth-footer {
  text-align: center;
  margin-top: var(--space-6);
  color: var(--text-muted);
  font-size: var(--text-xs);
}

/* ============================================
   TOAST NOTIFICATIONS
   ============================================ */

.toast-container {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  min-width: 320px;
  max-width: 420px;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-4);
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  animation: toastIn 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.toast.removing {
  animation: toastOut 0.2s ease forwards;
}

.toast-success { border-left: 3px solid var(--success); }
.toast-danger { border-left: 3px solid var(--danger); }
.toast-warning { border-left: 3px solid var(--warning); }
.toast-info { border-left: 3px solid var(--info); }

.toast-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.toast-success .toast-icon { color: var(--success); }
.toast-danger .toast-icon { color: var(--danger); }
.toast-warning .toast-icon { color: var(--warning); }
.toast-info .toast-icon { color: var(--info); }

.toast-content {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  margin-bottom: var(--space-1);
}

.toast-message {
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

.toast-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: var(--space-1);
  border-radius: var(--radius-md);
  flex-shrink: 0;
  transition: all var(--transition-fast);
}

.toast-close:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

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

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

/* ============================================
   PAGE LOADER
   ============================================ */

.page-loader {
   position: fixed;
   inset: 0;
   background: var(--loading-bg);
   backdrop-filter: blur(4px);
   z-index: var(--z-loader);
   display: flex;
   align-items: center;
   justify-content: center;
   opacity: 0;
   pointer-events: none;
   transition: opacity var(--transition-base);
}

html[data-theme="dark"] .page-loader {
   background: rgba(15, 17, 21, 0.92) !important;
}

.page-loader.active {
  opacity: 1;
  pointer-events: auto;
}

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

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
  .sidebar,
  .topbar,
  .page-header-actions,
  .filter-bar,
  .btn,
  .toast-container,
  .page-loader,
  .skip-link,
  .no-print {
    display: none !important;
  }

  .main-content {
    margin-left: 0 !important;
    padding: 0 !important;
  }

  .page-content {
    padding: 0 !important;
  }

  .card {
    border: 1px solid #ddd !important;
    box-shadow: none !important;
    break-inside: avoid;
  }

  .table {
    font-size: 10pt !important;
  }

  body {
    background: white !important;
    color: black !important;
  }

  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 8pt;
    color: #666;
  }

  .nav-link[href]::after,
  .dropdown-item[href]::after {
    content: none;
  }
}

/* ============================================
   RESPONSIVE
   ============================================ */

@media (max-width: 1199px) {
  .global-search {
    width: 240px;
  }
}

@media (max-width: 991px) {
  .sidebar {
    transform: translateX(-100%);
    width: var(--sidebar-width);
    z-index: var(--z-sidebar);
    will-change: transform;
    -webkit-transform: translateX(-100%);
  }

  .sidebar.is-open {
    transform: translateX(0);
    -webkit-transform: translateX(0);
  }

  .sidebar.is-collapsed {
    width: var(--sidebar-width);
  }

  .sidebar.is-collapsed .sidebar-brand-text,
  .sidebar.is-collapsed .nav-section-title,
  .sidebar.is-collapsed .nav-group-toggle .nav-label,
  .sidebar.is-collapsed .nav-group-toggle .nav-arrow,
  .sidebar.is-collapsed .sidebar-user-info {
    opacity: 1;
    width: auto;
  }

  .sidebar.is-collapsed .nav-group-items {
    display: block;
  }

  .main-content {
    margin-left: 0;
  }

  .topbar-mobile-toggle {
    display: flex;
  }

  .global-search {
    display: none;
  }

  /* Mobile table improvements */
  .table-responsive {
    -webkit-overflow-scrolling: touch;
    overflow-x: auto;
    -ms-overflow-style: none;
  }

  .table-responsive .table {
    min-width: 600px;
  }

  .form-section-body {
    padding: var(--space-4);
  }

  .line-item-product-select {
    min-width: 0;
    width: 100%;
  }

  .totals-panel {
    padding: var(--space-4);
  }
  .page-content {
    padding: var(--space-3);
  }

  .page-header {
    flex-direction: column;
    gap: var(--space-3);
  }

  .page-header-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .page-header-actions .btn {
    flex: 1;
    min-width: 0;
  }

  .filter-bar-inner {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-group {
    min-width: 100%;
  }

  .filter-actions {
    width: 100%;
    justify-content: stretch;
  }

  .filter-actions .btn {
    flex: 1;
  }

  .table-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-2);
  }

  .table-toolbar-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .auth-card {
    padding: var(--space-5);
  }

  .toast {
    min-width: auto;
    max-width: calc(100vw - var(--space-6));
    left: var(--space-3);
    right: var(--space-3);
  }

  .toast-container {
    left: var(--space-3);
    right: var(--space-3);
    top: var(--space-3);
  }

  /* Ensure charts don't overflow */
  .card-body canvas {
    width: 100% !important;
    height: auto !important;
  }

  /* Responsive KPI cards */
  .stat-card-header {
    flex-wrap: wrap;
  }

  .stat-card-label {
    width: 100%;
    margin-bottom: var(--space-1);
  }

  /* Fix dropdown menus on mobile */
  .dropdown-menu {
    max-width: calc(100vw - var(--space-8));
    min-width: 180px;
  }

  /* Fix bulk toolbar on mobile */
  .bulk-toolbar {
    flex-direction: column;
    gap: var(--space-2);
    align-items: stretch;
  }

  /* Fix feed list on mobile */
  .feed-item {
    flex-wrap: wrap;
  }

  .feed-amount {
    width: 100%;
    text-align: left;
    margin-top: var(--space-1);
  }

  /* Ensure bottom sheet feel for modals */
  .modal-content {
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    margin-top: auto;
  }

  .modal-dialog {
    align-items: flex-end;
    margin: 0;
    max-width: 100%;
  }
}

@media (max-width: 575px) {
  .topbar {
    padding: 0 var(--space-3);
  }

  .topbar-title h1 {
    font-size: var(--text-base);
  }

  .stat-card-value {
    font-size: var(--text-2xl);
  }

  .quick-actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Fix dropdown overflow on small screens */
  .dropdown-menu {
    max-width: calc(100vw - var(--space-8));
    width: auto;
  }
}

/* Mobile table improvements */
@media (max-width: 991px) {
  .table-responsive {
    -webkit-overflow-scrolling: touch;
    overflow-x: auto;
    -ms-overflow-style: none;
  }

  /* Make tables scrollable on mobile */
  .table-responsive .table {
    min-width: 600px;
  }

  /* Fix form sections on mobile */
  .form-section-body {
    padding: var(--space-4);
  }

  /* Fix line item product select on mobile */
  .line-item-product-select {
    min-width: 0;
    width: 100%;
  }

  /* Fix totals panel on mobile */
  .totals-panel {
    padding: var(--space-4);
  }

  .totals-row {
    font-size: var(--text-xs);
  }

  .totals-row.total {
    font-size: var(--text-base);
  }

  /* Fix modal on mobile */
  .modal-dialog {
    margin: var(--space-3);
    max-width: calc(100vw - var(--space-6));
  }

  .modal-body {
    padding: var(--space-4);
  }

  .modal-header,
  .modal-footer {
    padding: var(--space-3) var(--space-4);
  }

  /* Fix chart containers */
  .card-body canvas {
    max-width: 100%;
  }

  /* Fix form actions sticky bar */
  .form-actions-sticky {
    flex-direction: column;
    gap: var(--space-2);
  }

  .form-actions-sticky .btn {
    width: 100%;
    justify-content: center;
  }
}

/* Extra small screens (320px - 374px) */
@media (max-width: 374px) {
  .page-content {
    padding: var(--space-2);
  }

  .topbar {
    padding: 0 var(--space-2);
  }

  .topbar-title h1 {
    font-size: var(--text-sm);
  }

  .stat-card {
    padding: var(--space-2);
  }

  .stat-card-value {
    font-size: var(--text-xl);
  }

  .stat-card-icon {
    width: 32px;
    height: 32px;
  }

  .stat-card-header {
    margin-bottom: var(--space-2);
  }

  .quick-actions-grid {
    grid-template-columns: 1fr;
    gap: var(--space-2);
  }

  .quick-action-tile {
    padding: var(--space-2);
    flex-direction: row;
    gap: var(--space-2);
  }

  .quick-action-tile-icon {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
  }

  .card-body {
    padding: var(--space-2);
  }

  .card-header {
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-xs);
  }

  .btn {
    padding: var(--space-2) var(--space-3);
    min-height: 44px;
    font-size: var(--text-xs);
  }

  .table-wrapper .table td,
  .table-wrapper .table th {
    padding: var(--space-1-5);
    font-size: var(--text-xs);
  }

  .filter-bar {
    padding: var(--space-2);
  }

  .page-header {
    margin-bottom: var(--space-3);
  }

  .page-header-title h2 {
    font-size: var(--text-lg);
  }

  .auth-card {
    padding: var(--space-3);
  }

  .global-search {
    display: none;
  }

  /* Mobile table card layout */
  .table-responsive .table {
    min-width: 480px;
  }

  .form-section-body {
    padding: var(--space-3);
  }

  .modal-dialog {
    margin: var(--space-1);
    max-width: calc(100vw - var(--space-2));
  }

  .context-alert {
    font-size: var(--text-xs);
    padding: var(--space-2) var(--space-3);
  }

  .feed-item {
    gap: var(--space-2);
  }

  .feed-amount .amount {
    font-size: var(--text-xs);
  }

  .topbar-actions {
    gap: var(--space-1);
  }

  .topbar-btn {
    width: 40px;
    height: 40px;
  }
}

/* Small phones (375px - 575px) */
@media (min-width: 375px) and (max-width: 575px) {
  .page-content {
    padding: var(--space-3);
  }

  .stat-card {
    padding: var(--space-3);
  }

  .stat-card-value {
    font-size: var(--text-xl);
  }

  .quick-actions-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-2);
  }

  .card-body {
    padding: var(--space-3);
  }

  .card-header {
    padding: var(--space-3) var(--space-4);
  }

  .table-responsive .table {
    min-width: 500px;
  }

  .filter-bar-inner {
    gap: var(--space-2);
  }

  .page-header-title h2 {
    font-size: var(--text-xl);
  }

  .btn {
    min-height: 44px;
  }

  .topbar-actions {
    gap: var(--space-1);
  }

  .topbar-btn {
    width: 40px;
    height: 40px;
  }
}

/* Tablets (576px - 767px) */
@media (min-width: 576px) and (max-width: 767px) {
  .quick-actions-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* Small laptops (768px - 991px) */
@media (min-width: 768px) and (max-width: 991px) {
  .quick-actions-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* Laptops (992px - 1199px) */
@media (min-width: 992px) and (max-width: 1199px) {
  .quick-actions-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* Large screens (1200px - 1365px) */
@media (min-width: 1200px) and (max-width: 1365px) {
  .content-wrapper {
    max-width: 1400px;
  }
}

/* Full HD (1366px - 1919px) */
@media (min-width: 1366px) and (max-width: 1919px) {
  .content-wrapper {
    max-width: 1600px;
  }
}

/* 4K and above (1920px+) */
@media (min-width: 1920px) {
  .content-wrapper {
    max-width: 1800px;
  }

  .stat-card-value {
    font-size: var(--text-4xl);
  }
}

/* Bulk toolbar */
.bulk-toolbar {
  background: var(--info-subtle);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  padding: var(--space-3) var(--space-4);
}

.bulk-toolbar + .table-responsive .table,
.bulk-toolbar + .table {
  border-radius: 0 0 var(--radius-md) var(--radius-md);
}

/* Sidebar search no-results */
.sidebar-search-no-results {
  display: none;
  text-align: center;
  padding: var(--space-8) var(--space-4);
  color: var(--text-muted);
}

.sidebar-search-no-results.visible {
  display: block;
}

/* Notification dropdown */
.notification-dropdown {
  max-width: min(320px, calc(100vw - 2rem));
}

/* Notification empty state */
.notification-empty {
  padding: var(--space-8) var(--space-4);
}

/* Progress thin variant */
.progress-thin {
  height: 4px;
}

/* Progress large variant */
.progress-lg {
  height: 20px;
}

/* Quick action icon colors */
.quick-action-icon-success { color: var(--success); }
.quick-action-icon-info { color: var(--info); }
.quick-action-icon-primary { color: var(--primary-400); }
.quick-action-icon-warning { color: var(--warning); }

/* Bulk action select */
.bulk-action-select {
  width: auto;
  min-width: 160px;
}

/* Company truncation */
.company-truncate {
  max-width: 120px;
}

/* Hidden export tables */
.export-table-hidden {
  display: none;
}

/* Visual column for progress bars */
.col-visual {
  width: 40%;
}

/* Toast progress bar */
.toast-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 2px;
  background: currentColor;
  opacity: 0.3;
}

/* DataTables controls */
.table-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.dataTables_wrapper .dataTables_filter input {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  color: var(--text-primary);
}

.dataTables_wrapper .dataTables_info,
.dataTables_wrapper .dataTables_paginate {
  color: var(--text-secondary);
  font-size: var(--text-sm);
  padding: var(--space-3) var(--space-4);
}

/* Responsive DataTables controls */
@media (max-width: 767px) {
  .table-controls {
    flex-direction: column;
    align-items: stretch;
  }
  
  .dataTables_wrapper .dataTables_filter {
    width: 100%;
  }
  
  .dataTables_wrapper .dataTables_filter input {
    width: 100%;
  }
  
  .dataTables_wrapper .dataTables_length select {
    min-width: 70px;
  }
}

/* Sticky table headers */
.table-responsive thead th {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background: var(--surface);
  border-bottom: 2px solid var(--border-strong);
  white-space: nowrap;
}

/* Highlight totals row */
.table-total td {
  font-weight: var(--font-bold);
  border-top: 2px solid var(--border-strong);
  background: var(--surface-raised);
}

/* Negative amounts in red */
.amount-negative {
  color: var(--danger);
}



/* Table wrapper toolbar */
.table-wrapper .table-toolbar + .bulk-toolbar + .table-responsive,
.table-wrapper .bulk-toolbar + .table-responsive {
  border-radius: 0 0 var(--radius-md) var(--radius-md);
}

/* Text helpers */
.text-sm {
  font-size: var(--text-sm);
}

/* ============================================
   ACCESSIBILITY IMPROVEMENTS
   ============================================ */

/* Screen reader only */
.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;
}

/* ============================================
   MODAL HIERARCHY FIX
   ============================================ */
.modal {
  z-index: var(--z-modal) !important;
}

/* COMPLETELY DISABLE ALL modal backdrops - they cannot block anything */
.modal-backdrop,
.modal-backdrop.fade,
.modal-backdrop.show,
.modal-backdrop.fade.show {
  display: none !important;
  visibility: hidden !important;
  opacity: 0 !important;
  pointer-events: none !important;
  width: 0 !important;
  height: 0 !important;
  position: absolute !important;
  z-index: -1 !important;
}

/* Remove body modal-open styling that blocks scroll */
body.modal-open {
  overflow: auto !important;
  padding-right: 0 !important;
  position: static !important;
}

/* Prevent sidebar-open from blocking body scroll incorrectly */
body.sidebar-open {
  overflow: hidden !important;
  position: fixed;
  width: 100%;
  height: 100%;
}

/* Fix: Ensure modal can be closed properly */
.modal.show {
  display: block !important;
}
