/* =========================================================================
   Northgrid — Design tokens
   Palette: deep slate + indigo primary + teal accent, flat design.
   Type: Space Grotesk (display/brand), Inter (UI/body), JetBrains Mono (data)
   ========================================================================= */

:root {
  --font-display: 'Space Grotesk', sans-serif;
  --font-body: 'Inter', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;

  --sidebar-w: 264px;
  --sidebar-w-collapsed: 72px;
  --topbar-h: 64px;

  --transition-fast: 150ms ease;
  --transition-med: 220ms ease;
}

/* ---- Dark theme (default) ---- */
[data-theme='dark'] {
  --bg: #0b0f14;
  --surface: #131922;
  --surface-2: #1b2330;
  --surface-hover: #212b3a;
  --border: #232c3a;
  --border-strong: #2e3a4c;

  --text-primary: #e8edf5;
  --text-secondary: #8d99ac;
  --text-tertiary: #5f6b7d;

  --primary: #4c6fff;
  --primary-hover: #6180ff;
  --primary-text: #ffffff;
  --primary-soft: rgba(76, 111, 255, 0.14);

  --accent: #23d4a6;
  --accent-soft: rgba(35, 212, 166, 0.14);

  --warning: #f2b84b;
  --warning-soft: rgba(242, 184, 75, 0.14);

  --danger: #f2555a;
  --danger-hover: #ff6c70;
  --danger-soft: rgba(242, 85, 90, 0.14);

  --logo-line: #4c6fff;
  --logo-dot: #4c6fff;
  --logo-accent: #23d4a6;
  --logo-core: #e8edf5;

  --shadow-modal: 0 20px 60px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

/* ---- Light theme ---- */
[data-theme='light'] {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --surface-hover: #eef1f7;
  --border: #e1e6ee;
  --border-strong: #d3dae5;

  --text-primary: #17202e;
  --text-secondary: #5c6b82;
  --text-tertiary: #8896ab;

  --primary: #3554e1;
  --primary-hover: #2a44c4;
  --primary-text: #ffffff;
  --primary-soft: rgba(53, 84, 225, 0.09);

  --accent: #12b892;
  --accent-soft: rgba(18, 184, 146, 0.1);

  --warning: #c8860a;
  --warning-soft: rgba(200, 134, 10, 0.12);

  --danger: #d6383f;
  --danger-hover: #b92e34;
  --danger-soft: rgba(214, 56, 63, 0.1);

  --logo-line: #3554e1;
  --logo-dot: #3554e1;
  --logo-accent: #12b892;
  --logo-core: #17202e;

  --shadow-modal: 0 20px 50px rgba(23, 32, 46, 0.18);
  color-scheme: light;
}

/* =========================================================================
   Reset & base
   ========================================================================= */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  transition: background var(--transition-med), color var(--transition-med);
}
h1, h2, h3, h4 { font-family: var(--font-display); font-weight: 600; margin: 0; color: var(--text-primary); }
p { margin: 0; }
button { font-family: inherit; }
input, select, textarea { font-family: inherit; }
a { color: inherit; text-decoration: none; }
::selection { background: var(--primary-soft); }

.icon {
  width: 18px; height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  display: block;
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* =========================================================================
   Login view
   ========================================================================= */
.login-view {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6);
  overflow: hidden;
}
.login-view[hidden] { display: none; }

.login-bg {
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(circle at 1px 1px, var(--border-strong) 1px, transparent 1px);
  background-size: 28px 28px;
  opacity: .5;
  mask-image: radial-gradient(ellipse at 50% 35%, black 0%, transparent 70%);
}

.login-card {
  position: relative;
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-8) var(--space-6) var(--space-6);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-8);
}
.login-logo { width: 40px; height: 40px; flex-shrink: 0; }
.login-brand-text { display: flex; flex-direction: column; }
.login-brand-name { font-family: var(--font-display); font-weight: 700; font-size: 18px; letter-spacing: -.01em; }
.login-brand-sub { font-size: 12px; color: var(--text-secondary); }

.login-title { font-size: 24px; margin-bottom: var(--space-1); }
.login-subtitle { color: var(--text-secondary); font-size: 13px; margin-bottom: var(--space-6); display: block; }

.login-form { display: flex; flex-direction: column; gap: var(--space-4); }

.field { display: flex; flex-direction: column; gap: var(--space-2); }
.field-label { font-size: 12.5px; font-weight: 600; color: var(--text-secondary); }
.field input, .field select, .field textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 10px 12px;
  font-size: 14px;
  width: 100%;
  transition: border-color var(--transition-fast), background var(--transition-fast);
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--primary);
  background: var(--surface);
}
.field input::placeholder { color: var(--text-tertiary); }

.field-input-wrap { position: relative; display: flex; align-items: center; }
.field-input-wrap input { padding-right: 40px; }
.field-icon-btn {
  position: absolute; right: 6px;
  background: none; border: none; cursor: pointer;
  color: var(--text-tertiary);
  padding: 6px;
  display: flex;
  border-radius: var(--radius-sm);
}
.field-icon-btn:hover { color: var(--text-primary); background: var(--surface-hover); }

.form-error {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
}

.theme-toggle {
  position: absolute;
  top: var(--space-6);
  right: var(--space-6);
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.theme-toggle:hover { color: var(--text-primary); border-color: var(--border-strong); }

.icon-sun, .icon-moon { position: absolute; transition: opacity var(--transition-fast), transform var(--transition-fast); }
[data-theme='dark'] .icon-sun { opacity: 0; transform: scale(.6) rotate(-30deg); }
[data-theme='dark'] .icon-moon { opacity: 1; transform: scale(1) rotate(0); }
[data-theme='light'] .icon-sun { opacity: 1; transform: scale(1) rotate(0); }
[data-theme='light'] .icon-moon { opacity: 0; transform: scale(.6) rotate(30deg); }

/* =========================================================================
   Buttons
   ========================================================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
  white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-block { width: 100%; }

.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-secondary { background: var(--surface-2); color: var(--text-primary); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-hover); border-color: var(--border-strong); }

.btn-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.btn-danger:hover:not(:disabled) { background: var(--danger); color: #fff; }

.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-ghost:hover:not(:disabled) { color: var(--text-primary); background: var(--surface-hover); }

.btn-sm { padding: 6px 10px; font-size: 12.5px; }

.btn-spinner {
  width: 15px; height: 15px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: #fff;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =========================================================================
   App shell
   ========================================================================= */
.app-view {
  display: flex;
  min-height: 100vh;
}
.app-view[hidden] { display: none; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: sticky;
  top: 0;
  transition: width var(--transition-med);
  z-index: 40;
}

.sidebar-top {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-4);
  border-bottom: 1px solid var(--border);
  min-height: var(--topbar-h);
}

.icon-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text-primary); }

.sidebar-brand { display: flex; align-items: center; gap: var(--space-2); overflow: hidden; }
.sidebar-logo { width: 26px; height: 26px; flex-shrink: 0; }
.sidebar-brand-name {
  font-family: var(--font-display); font-weight: 700; font-size: 15.5px;
  white-space: nowrap; letter-spacing: -.01em;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4) var(--space-3);
}

.nav-section { margin-bottom: var(--space-5); }
.nav-section-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  padding: 0 var(--space-2);
  margin-bottom: var(--space-2);
  white-space: nowrap;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 9px var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 2px;
  white-space: nowrap;
  border-left: 2px solid transparent;
}
.nav-link:hover { background: var(--surface-hover); color: var(--text-primary); }
.nav-link.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-left-color: var(--primary);
}
.nav-link .icon { flex-shrink: 0; width: 18px; height: 18px; }
.nav-link-label { overflow: hidden; text-overflow: ellipsis; }

.sidebar-bottom {
  border-top: 1px solid var(--border);
  padding: var(--space-3);
}

.sidebar-user-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 8px var(--space-2);
  border-radius: var(--radius-sm);
  width: 100%;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  text-align: left;
}
.sidebar-user-row:hover { background: var(--surface-hover); }
.sidebar-user { cursor: default; }
.sidebar-user:hover { background: transparent; }

.icon-wrap { width: 20px; height: 20px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; position: relative; }
.sidebar-theme-row .icon-wrap .icon { position: absolute; }

.sidebar-label { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; }

.user-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 11.5px; font-weight: 700;
  flex-shrink: 0;
}
.user-meta { display: flex; flex-direction: column; gap: 1px; }
.user-name { color: var(--text-primary); font-weight: 600; }
.user-type { font-size: 11px; color: var(--text-tertiary); text-transform: capitalize; }
.sidebar-logout:hover { color: var(--danger); }

.sidebar-scrim { display: none; }

/* ---- Collapsed sidebar (icon rail) ---- */
.sidebar.collapsed { width: var(--sidebar-w-collapsed); }
.sidebar.collapsed .sidebar-brand-name,
.sidebar.collapsed .nav-section-label,
.sidebar.collapsed .nav-link-label,
.sidebar.collapsed .sidebar-label,
.sidebar.collapsed .user-meta {
  display: none;
}
.sidebar.collapsed .sidebar-top { justify-content: center; }
.sidebar.collapsed .sidebar-brand { display: none; }
.sidebar.collapsed .nav-link { justify-content: center; padding: 10px; }
.sidebar.collapsed .sidebar-user-row { justify-content: center; }
.sidebar.collapsed .nav-section { margin-bottom: var(--space-3); }

/* =========================================================================
   Main content
   ========================================================================= */
.app-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-6);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 20;
}
#topbar-hamburger { display: none; }

.page-title { font-size: 19px; }

.page-root {
  padding: var(--space-6);
  flex: 1;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
  flex-wrap: wrap;
}
.page-header-text { display: flex; flex-direction: column; gap: 2px; }
.page-header-title { font-size: 17px; }
.page-header-subtitle { font-size: 13px; color: var(--text-secondary); }

/* =========================================================================
   Dashboard tiles
   ========================================================================= */
.tiles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  position: relative;
  overflow: hidden;
}
.tile::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; height: 3px;
  background: var(--tile-accent, var(--primary));
}
.tile-users { --tile-accent: var(--primary); }
.tile-warehouses { --tile-accent: var(--accent); }
.tile-orders { --tile-accent: var(--warning); }

.tile-label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}
.tile-icon {
  width: 28px; height: 28px;
  border-radius: var(--radius-sm);
  background: var(--tile-accent, var(--primary));
  opacity: .16;
  position: absolute;
  top: var(--space-5);
  right: var(--space-5);
}
.tile-value {
  font-family: var(--font-mono);
  font-size: 32px;
  font-weight: 500;
  color: var(--text-primary);
}

.section-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  margin-bottom: var(--space-5);
}
.section-block-title { font-size: 14.5px; margin-bottom: var(--space-4); }

/* =========================================================================
   Tables
   ========================================================================= */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-tertiary);
  background: var(--surface-2);
  padding: 12px var(--space-4);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody td {
  padding: 12px var(--space-4);
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  color: var(--text-primary);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
.cell-secondary { color: var(--text-secondary); }
.cell-mono { font-family: var(--font-mono); font-size: 12.5px; color: var(--text-secondary); }
.cell-actions { display: flex; gap: var(--space-2); justify-content: flex-end; white-space: nowrap; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  text-transform: capitalize;
}
.badge-primary { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.badge-accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

.empty-state {
  padding: var(--space-10) var(--space-6);
  text-align: center;
  color: var(--text-secondary);
}
.empty-state-title { color: var(--text-primary); font-weight: 600; margin-bottom: var(--space-1); font-size: 14px; }
.empty-state-sub { font-size: 13px; }

/* =========================================================================
   ERP connections
   ========================================================================= */
.erp-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: var(--space-4);
}
.erp-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.erp-card-head { display: flex; align-items: center; gap: var(--space-3); }
.erp-mark {
  width: 42px; height: 42px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
  color: #fff;
  flex-shrink: 0;
}
.erp-name { font-weight: 600; font-size: 15px; }
.erp-desc { font-size: 12.5px; color: var(--text-secondary); }
.erp-status { font-size: 12px; color: var(--text-tertiary); }

/* =========================================================================
   Forms (modal)
   ========================================================================= */
.form-grid { display: flex; flex-direction: column; gap: var(--space-4); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
.form-actions {
  display: flex; justify-content: flex-end; gap: var(--space-3);
  margin-top: var(--space-2);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border);
}
.field-hint { font-size: 12px; color: var(--text-tertiary); }

/* =========================================================================
   Modal
   ========================================================================= */
.modal-root { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: var(--space-4); }
.modal-root[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(5, 8, 12, 0.6); }
[data-theme='light'] .modal-backdrop { background: rgba(23, 32, 46, 0.35); }
.modal {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 460px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-modal);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-5) var(--space-5) var(--space-4);
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 16px; }
.modal-body { padding: var(--space-5); overflow-y: auto; }

/* =========================================================================
   Toasts
   ========================================================================= */
.toast-container {
  position: fixed;
  bottom: var(--space-5);
  right: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 200;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 13px;
  color: var(--text-primary);
  min-width: 240px;
  max-width: 340px;
  box-shadow: var(--shadow-modal);
  animation: toast-in .18s ease;
}
.toast-success { border-left-color: var(--accent); }
.toast-error { border-left-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* =========================================================================
   Utility
   ========================================================================= */
.text-secondary { color: var(--text-secondary); }
.mono { font-family: var(--font-mono); }
.hidden { display: none !important; }

/* =========================================================================
   Responsive
   ========================================================================= */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-modal);
  }
  .sidebar.mobile-open { transform: translateX(0); }
  .sidebar.collapsed { width: var(--sidebar-w); }
  .sidebar.collapsed .sidebar-brand-name,
  .sidebar.collapsed .nav-section-label,
  .sidebar.collapsed .nav-link-label,
  .sidebar.collapsed .sidebar-label,
  .sidebar.collapsed .user-meta { display: block; }
  .sidebar.collapsed .sidebar-brand { display: flex; }
  .sidebar.collapsed .nav-link { justify-content: flex-start; padding: 9px var(--space-3); }
  .sidebar.collapsed .sidebar-user-row { justify-content: flex-start; }

  .sidebar-scrim.visible {
    display: block;
    position: fixed; inset: 0;
    background: rgba(5,8,12,.5);
    z-index: 39;
  }

  #topbar-hamburger { display: flex; }
  #sidebar-toggle { display: none; }
  .app-content { width: 100%; }
  .form-row { grid-template-columns: 1fr; }
  .page-root { padding: var(--space-4); }
  .cell-actions { justify-content: flex-start; }
}