:root {
  /* ── Surfaces / Backgrounds ── */
  --bg-root: #050d12;
  --bg-sidebar: #0a1219;
  --bg-surface: #0c1720;
  --bg-card: #101d28;
  --bg-card-hover: #142430;
  --bg-elevated: #18293a;
  --bg-default: transparent;
  --bg-input: #0e1a24;
  --bg-overlay: rgba(5, 13, 18, 0.85);

  /* ── Accent (Emerald) ── */
  --accent-50: #ecfdf5;
  --accent-100: #d1fae5;
  --accent-200: #a7f3d0;
  --accent-300: #6ee7b7;
  --accent-400: #34d399;
  --accent-500: #10b981;
  --accent-600: #059669;
  --accent-700: #047857;
  --accent-800: #065f46;
  --accent-900: #064e3b;
  --accent-950: #022c22;
  --accent-surface: #0b2e1f;
  --accent-surface-hover: #0f3d2a;

  /* ── Text ── */
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --text-disabled: #475569;
  --text-accent: #34d399;

  /* ── Borders ── */
  --border-default: #172432;
  --border-subtle: #111e2b;
  --border-strong: #1e3347;
  --border-accent: #10b981;

  /* ── Status ── */
  --status-success: #22c55e;
  --status-success-muted: rgba(34, 197, 94, 0.12);
  --status-error: #ef4444;
  --status-error-muted: rgba(239, 68, 68, 0.12);
  --status-warning: #f59e0b;
  --status-warning-muted: rgba(245, 158, 11, 0.12);
  --status-info: #3b82f6;
  --status-info-muted: rgba(59, 130, 246, 0.12);

  /* ── Chart ── */
  --chart-1: #34d399;
  --chart-2: #10b981;
  --chart-3: #059669;
  --chart-gradient-start: rgba(52, 211, 153, 0.35);
  --chart-gradient-end: rgba(16, 185, 129, 0.02);

  /* ── Spacing (4px grid) ── */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;
  --space-16: 4rem;

  /* ── Border Radius ── */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  --radius-full: 9999px;

  /* ── Shadows ── */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  --shadow-card: 0 2px 8px rgba(0, 0, 0, 0.25);

  /* ── Transitions ── */
  --transition-fast: 150ms ease;
  --transition-base: 200ms ease;
  --transition-slow: 300ms ease;

  /* ── Sidebar ── */
  --sidebar-width: 260px;
  --sidebar-collapsed-width: 72px;

  /* ── Typography (Accessibility: 16px base) ── */
  --text-xs: 0.8125rem;
  --text-sm: 0.9375rem;
  --text-base: 1rem;
  --text-md: 1.0625rem;
  --text-lg: 1.25rem;
  --text-xl: 1.5rem;
  --text-2xl: 1.75rem;
  --text-3xl: 2.25rem;
  --text-4xl: 2.75rem;

  --weight-normal: 400;
  --weight-medium: 500;
  --weight-semibold: 600;
  --weight-bold: 700;

  --leading-tight: 1.2;
  --leading-normal: 1.5;
  --leading-relaxed: 1.65;

  /* ── Table Spacing ── */
  --table-cell-padding-x: 1rem;
  --table-cell-padding-y: 0.875rem;
  --table-header-padding-y: 1rem;
}

/* ── Large Text Mode (Accessibility) ── */
[data-font-size="large"] {
  --text-xs: 1rem;
  --text-sm: 1.125rem;
  --text-base: 1.25rem;
  --text-md: 1.375rem;
  --text-lg: 1.625rem;
  --text-xl: 2rem;
  --text-2xl: 2.5rem;
  --text-3xl: 3rem;
  --text-4xl: 3.5rem;

  --table-cell-padding-x: 1.5rem;
  --table-cell-padding-y: 1.125rem;
  --table-header-padding-y: 1.25rem;
}

/* ── Light Theme ── */
[data-theme="light"] {
  /* Surfaces / Backgrounds */
  --bg-root: #ffffff;
  --bg-sidebar: #f8fafc;
  --bg-surface: #f8fafc;
  --bg-card: #ffffff;
  --bg-card-hover: #f1f5f9;
  --bg-elevated: #f1f5f9;
  --bg-input: #ffffff;
  --bg-overlay: rgba(255, 255, 255, 0.9);

  /* Text (WCAG AA compliant) */
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;
  --text-disabled: #94a3b8;
  --text-accent: #059669;

  /* Borders */
  --border-default: #e2e8f0;
  --border-subtle: #f1f5f9;
  --border-strong: #cbd5e1;
  --border-accent: #10b981;

  /* Status (adjusted for light backgrounds) */
  --status-success: #16a34a;
  --status-success-muted: rgba(22, 163, 74, 0.12);
  --status-error: #dc2626;
  --status-error-muted: rgba(220, 38, 38, 0.12);
  --status-warning: #d97706;
  --status-warning-muted: rgba(217, 119, 6, 0.12);
  --status-info: #2563eb;
  --status-info-muted: rgba(37, 99, 235, 0.12);

  /* Chart */
  --chart-1: #10b981;
  --chart-2: #059669;
  --chart-3: #047857;
  --chart-gradient-start: rgba(16, 185, 129, 0.25);
  --chart-gradient-end: rgba(5, 150, 105, 0.02);

  /* Shadows (lighter for light mode) */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
  --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.08);

  /* Accent surfaces for light mode */
  --accent-surface: #ecfdf5;
  --accent-surface-hover: #d1fae5;
}

/* ── Base ── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg-root);
  color: var(--text-primary);
  font-family: 'Geist Sans', system-ui, -apple-system, sans-serif;
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a {
  color: inherit;
  text-decoration: none;
}

/* ── Utility Classes ── */

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  transition: background var(--transition-base);
}

a.card:hover, button.card:hover {
  background: var(--bg-card-hover);
}

/* Icon sizes matching Next.js */
.icon-sm {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

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

.icon-container {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  border-radius: var(--radius-md);
}

.stat-label {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
}

.stat-value {
  font-size: var(--text-3xl);
  font-weight: var(--weight-bold);
  color: var(--text-primary);
  line-height: var(--leading-tight);
  letter-spacing: -0.02em;
}

.badge-positive {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  color: var(--status-success);
  background: var(--status-success-muted);
  padding: 2px 8px;
  border-radius: var(--radius-full);
}

.badge-negative {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  color: var(--status-error);
  background: var(--status-error-muted);
  padding: 2px 8px;
  border-radius: var(--radius-full);
}

.section-label {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: var(--space-2) var(--space-3);
}

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

.nav-item:hover {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.nav-item-active {
  color: var(--text-accent);
  background: var(--accent-surface);
}

.nav-item-active:hover {
  background: var(--accent-surface-hover);
  color: var(--text-accent);
}

.tab {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-muted);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
  cursor: pointer;
  border: none;
  background: transparent;
}

.tab:hover {
  color: var(--text-primary);
}

.tab-active {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

/* ── Preference Toggle States ── */
#font-size-toggle.font-size-large {
  color: var(--text-accent);
  background: var(--accent-surface);
}

#font-size-toggle.font-size-large:hover {
  background: var(--accent-surface-hover);
}

/* ── Tables ── */
table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

th, td {
  padding: var(--table-cell-padding-y) var(--table-cell-padding-x);
  text-align: left;
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

th {
  padding-top: var(--table-header-padding-y);
  padding-bottom: var(--table-header-padding-y);
}

thead {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg-card);
}

.table-container {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

td, th {
  min-width: 60px;
}

.col-numeric {
  white-space: nowrap;
  text-align: right;
}

/* ── Scrollbar ── */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-default);
  border-radius: var(--radius-full);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--border-strong);
}

/* ── Sidebar (desktop baseline) ── */
.sidebar {
  width: var(--sidebar-width);
  z-index: 40;
}

/* ── Sidebar Backdrop ── */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 39;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.sidebar-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

/* ── Main content (desktop baseline) ── */
.main-content {
  margin-left: var(--sidebar-width);
}

/* ── Category/Subcategory Filter Buttons ── */
.cat-btn,
.cat-btn-active {
  padding: 0.375rem 0.75rem;
  font-size: 12px;
  font-weight: 600;
  border: none;
  border-radius: 4px;
  transition: all 0.15s ease;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

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

.cat-btn:hover {
  background: var(--bg-card-hover);
  color: var(--text-primary);
}

.cat-btn-active {
  background: var(--accent-500);
  color: white;
}

.subcat-btn,
.subcat-btn-active {
  padding: 0.25rem 0.5rem;
  font-size: 11px;
  font-weight: 500;
  border: none;
  border-radius: 3px;
  transition: all 0.15s ease;
}

.subcat-btn {
  background: var(--bg-surface);
  color: var(--text-muted);
  border: 1px solid var(--border-subtle);
}

.subcat-btn:hover {
  background: var(--bg-elevated);
  color: var(--text-secondary);
}

.subcat-btn-active {
  background: var(--accent-400);
  color: white;
}

/* Mobile: larger filter buttons for easier tapping */
@media (max-width: 767px) {
  .cat-btn,
  .cat-btn-active {
    padding: 0.5rem 0.75rem;
    font-size: 12px;
    min-width: 44px;
    min-height: 36px;
  }
  
  .subcat-btn,
  .subcat-btn-active {
    padding: 0.4rem 0.6rem;
    font-size: 11px;
    min-height: 32px;
  }
  
  .category-filter,
  .subcat-filter {
    gap: 6px !important;
  }
}

/* ── Physical Count Button (for warehouse users) ── */
.col-physical {
  width: 70px;
}

.physical-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 50px;
  padding: 0.35rem 0.5rem;
  font-size: 12px;
  font-weight: 600;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.physical-btn.has-value {
  background: var(--accent-500);
  color: white;
}

.physical-btn.has-value:hover {
  background: var(--accent-600);
}

.physical-btn.no-value {
  background: var(--status-warning);
  color: #000;
}

.physical-btn.no-value:hover {
  background: #e6b800;
}

.physical-input {
  width: 70px;
  padding: 0.35rem 0.5rem;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  border: 2px solid var(--accent-500);
  border-radius: 4px;
  background: var(--bg-input);
  color: var(--text-primary);
  outline: none;
}

.physical-edit-container {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Mobile: much larger physical count button */
@media (max-width: 767px) {
  .col-physical {
    width: 100% !important;
  }
  
  .physical-btn {
    width: 100%;
    min-height: 44px;
    padding: 0.75rem 1rem;
    font-size: 16px;
    border-radius: 6px;
  }
  
  .physical-btn.no-value {
    font-weight: 700;
  }
  
  .physical-input {
    width: 100%;
    min-height: 44px;
    padding: 0.75rem;
    font-size: 18px;
    border-width: 2px;
  }
  
  .physical-edit-container {
    width: 100%;
  }
}

/* ── Desktop Table Fit ── */
.card-table {
  table-layout: auto;
  width: 100%;
}

.card-table th,
.card-table td {
  font-size: var(--text-xs);
  padding: 0.25rem 0.375rem;
  white-space: nowrap;
}

.card-table th {
  font-size: 10px;
  padding: 0.5rem 0.375rem;
}

/* Inventory table specific - more compact */
.inventory-table {
  table-layout: fixed;
}

.inventory-table th,
.inventory-table td {
  font-size: 11px;
  padding: 0.2rem 0.25rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.inventory-table th {
  font-size: 9px;
}

/* Column widths for inventory table */
.inventory-table .col-location { width: 65px; }
.inventory-table .col-description { width: 160px; white-space: normal; line-height: 1.3; }
.inventory-table .col-sku { width: 55px; }
.inventory-table .col-category { width: 70px; }
.inventory-table .col-number { width: 45px; text-align: right; }
.inventory-table .col-variance { width: 50px; text-align: right; }
.inventory-table .col-po { width: 80px; }
.inventory-table .col-currency { width: 50px; text-align: right; }
.inventory-table .col-actions { width: 30px; }

/* Ensure table container scrolls horizontally */
.table-scroll-container {
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}

/* Fix main content overflow - critical for wide tables */
.main-content {
  overflow-x: hidden;
  min-width: 0;
  max-width: calc(100vw - var(--sidebar-width));
}

.p-6 {
  max-width: 100%;
  overflow-x: hidden;
}

.card {
  max-width: 100%;
}

/* ── Mobile Responsive Card Tables ── */
@media (max-width: 767px) {
  .card-table {
    border-collapse: collapse;
  }

  .card-table thead {
    display: none;
  }

  .card-table tbody {
    display: block;
  }

  .card-table tbody tr {
    display: block;
    margin-bottom: var(--space-3);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    background: var(--bg-card);
  }

  .card-table tbody tr:last-child {
    margin-bottom: 0;
  }

  .card-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-1) 0;
    border: none;
    min-width: unset !important;
    max-width: unset !important;
    width: 100% !important;
    text-align: right;
    white-space: normal;
  }

  .card-table td::before {
    content: attr(data-label);
    font-weight: var(--weight-semibold);
    color: var(--text-secondary);
    font-size: var(--text-sm);
    flex-shrink: 0;
    margin-right: var(--space-3);
    text-align: left;
  }

  .card-table td[data-label=""]::before,
  .card-table td:not([data-label])::before {
    display: none;
  }

  .card-table td:empty {
    display: none;
  }

  .card-table .card-table-category td {
    display: block;
    text-align: left;
    font-weight: var(--weight-bold);
    font-size: var(--text-sm);
    color: var(--text-accent);
    padding: var(--space-1) 0;
  }

  .card-table .card-table-category td::before {
    display: none;
  }

  .card-table .card-table-category {
    background: var(--bg-elevated);
    border-color: var(--border-strong);
  }

  .card-table .card-table-footer td {
    font-weight: var(--weight-bold);
  }

  .card-table .card-table-footer {
    background: var(--bg-elevated);
    border-color: var(--border-accent);
  }

  .card-table td input,
  .card-table td select {
    max-width: 50%;
    min-width: 3rem;
  }
}

/* ── Sidebar Collapse Toggle ── */
.sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
  flex-shrink: 0;
  margin-left: auto;
}

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

.sidebar-toggle svg {
  transition: transform var(--transition-base);
}

/* ── Sidebar Collapsed State ── */
[data-sidebar-collapsed="true"] .sidebar {
  width: var(--sidebar-collapsed-width);
}

[data-sidebar-collapsed="true"] .sidebar .sidebar-logo-text,
[data-sidebar-collapsed="true"] .sidebar .nav-item span,
[data-sidebar-collapsed="true"] .sidebar .sidebar-user-info {
  opacity: 0;
  width: 0;
  overflow: hidden;
  white-space: nowrap;
}

[data-sidebar-collapsed="true"] .sidebar .sidebar-logo {
  justify-content: center;
  padding-left: 0;
  padding-right: 0;
}

[data-sidebar-collapsed="true"] .sidebar .sidebar-toggle {
  position: absolute;
  right: 50%;
  transform: translateX(50%);
  margin-left: 0;
}

[data-sidebar-collapsed="true"] .sidebar .sidebar-toggle svg {
  transform: rotate(180deg);
}

[data-sidebar-collapsed="true"] .sidebar nav {
  padding-left: var(--space-2);
  padding-right: var(--space-2);
}

[data-sidebar-collapsed="true"] .sidebar .nav-item {
  justify-content: center;
  padding-left: var(--space-2);
  padding-right: var(--space-2);
  gap: 0;
}

[data-sidebar-collapsed="true"] .sidebar .sidebar-user {
  justify-content: center;
  padding-left: var(--space-2);
  padding-right: var(--space-2);
}

[data-sidebar-collapsed="true"] .sidebar .sidebar-user-avatar {
  margin-right: 0;
}

[data-sidebar-collapsed="true"] .sidebar .sidebar-logout {
  display: none;
}

[data-sidebar-collapsed="true"] .main-content {
  margin-left: var(--sidebar-collapsed-width);
  max-width: calc(100vw - var(--sidebar-collapsed-width));
}


/* ── Mobile Layout ── */
@media (max-width: 767px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    z-index: 50;
  }

  .sidebar.sidebar-open {
    transform: translateX(0);
  }

  .main-content {
    margin-left: 0 !important;
    max-width: 100vw !important;
  }

  /* Disable collapse on mobile - always show full sidebar when open */
  [data-sidebar-collapsed="true"] .sidebar {
    width: var(--sidebar-width);
  }

  [data-sidebar-collapsed="true"] .sidebar .sidebar-logo-text,
  [data-sidebar-collapsed="true"] .sidebar .nav-item span,
  [data-sidebar-collapsed="true"] .sidebar .sidebar-user-info {
    opacity: 1;
    width: auto;
  }

  [data-sidebar-collapsed="true"] .sidebar .sidebar-logo {
    justify-content: flex-start;
    padding-left: var(--space-5);
    padding-right: var(--space-5);
  }

  [data-sidebar-collapsed="true"] .sidebar nav {
    padding-left: var(--space-3);
    padding-right: var(--space-3);
  }

  [data-sidebar-collapsed="true"] .sidebar .nav-item {
    justify-content: flex-start;
    padding-left: var(--space-3);
    padding-right: var(--space-3);
    gap: var(--space-3);
  }

  [data-sidebar-collapsed="true"] .sidebar .sidebar-user {
    justify-content: flex-start;
    padding-left: var(--space-4);
    padding-right: var(--space-4);
  }

  [data-sidebar-collapsed="true"] .sidebar .sidebar-logout {
    display: flex;
  }

  [data-sidebar-collapsed="true"] .main-content {
    margin-left: 0;
    max-width: 100vw;
  }

  .sidebar-toggle {
    display: none;
  }
}
