:root{
  /* Base variables - common to both themes */
  --primary-900:#2c5282; --primary-700:#3182ce; --primary-500:#4299e1; --primary-300:#90cdf4;
  --ok:#38a169; --danger:#e53e3e;
  --font-size:14px; --cell-pad-y:8px; --cell-pad-x:12px; --header-pad-y:12px; --header-pad-x:12px;
  --btn-h:36px; --btn-pad-x:12px; --radius:8px;
}

/* Light theme (default) - Softer, easier on the eyes */
:root:not([data-theme="dark"]),
[data-theme="light"] {
  --text:#1a202c; --muted:#64748b; --bg:#f1f5f9; --card:#fafbfc; --border:#cbd5e0;
  --row:#fafbfc; --row-alt:#f1f5f9; --row-hover:#e2e8f0; --completed:#f0fdf4;
  --input-bg:#fafbfc;
  --input-border:#cbd5e0;
  --input-focus:#4299e1;
  --shadow-light:0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-medium:0 4px 12px rgba(0, 0, 0, 0.06);
  --shadow-large:0 10px 25px rgba(0, 0, 0, 0.06);
  --calendar-icon-filter: invert(0.4);
}
*{box-sizing:border-box}
body{ margin:0; font-family: ui-sans-serif,system-ui,Segoe UI,Roboto,Arial;
      font-size:var(--font-size); color:var(--text); background:var(--bg); 
      -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }

/* Enhanced Mobile Responsiveness */
@media (max-width: 768px) {
  .app { margin: 10px; padding: 0 8px; }
  .topbar { padding: 8px 12px; flex-direction: column; gap: 8px; }
  .brand { font-size: 14px; }
  .header-right { width: 100%; justify-content: center; }
  .search input { width: 150px; }
  
  /* Larger touch targets for mobile */
  .btn { height: 36px; padding: 0 12px; font-size: 14px; }
  input, select { height: 36px; padding: 8px 12px; font-size: 14px; }
  
  /* Ensure critical columns remain visible on mobile */
  table th:nth-child(6), table td:nth-child(6) { width: 20%; } /* Notes - smaller on mobile */
  table th:nth-child(10), table td:nth-child(10) { min-width: 60px; } /* Completed - always visible */
  table th:nth-child(11), table td:nth-child(11) { min-width: 80px; } /* Actions - always visible */
}

@media (max-width: 480px) {
  .app { margin: 5px; padding: 0 5px; }
  .topbar { padding: 6px 8px; }
  .search input { width: 120px; }
  .btn { height: 40px; padding: 0 10px; min-width: 44px; } /* iOS touch target */
}
.app{ max-width:100%; margin:0px auto; padding:0 14px; }
.topbar{ background:var(--primary-700); border:1px solid var(--border); border-radius:12px;
         padding:0px 12px; display:flex; gap:12px; align-items:center; justify-content:space-between;
         box-shadow:var(--shadow-medium); color:white;}
.brand{ display:flex; align-items:center; gap:10px; font-weight:700; }
.brand .dot{ width:10px; height:10px; border-radius:50%; background:var(--primary-300);
             box-shadow:0 0 0 2px rgba(110,192,178,.25);}
.header-right{ display:flex; gap:8px; align-items:center; flex-wrap:wrap; }
.search{
  height:var(--btn-h); display:flex; align-items:center; gap:8px; background:rgba(255,255,255,.2);
  border:1px solid rgba(255,255,255,.3); border-radius:var(--radius); padding:0 12px; color:white;
}
.search input{ background:transparent; border:none; outline:none; color:white; width:180px; }
.search input::placeholder{ color:rgba(255,255,255,.7); }
.btn{ appearance:none; border:1px solid transparent; border-radius:var(--radius);
      height:var(--btn-h); padding:0 var(--btn-pad-x); background:var(--primary-500); color:white;
      font-weight:600; cursor:pointer; transition:opacity .15s ease, transform .04s ease; }
.btn:hover{opacity:.95} .btn:active{transform:scale(.98)}
.btn.ghost{ background:transparent; border-color:var(--border); color:var(--text)}
.btn.danger{ background:transparent; border-color:var(--danger); color:var(--danger); }

/* Navigation button overrides - clean, comfortable styling */
.topbar .btn.ghost { 
  background:rgba(255,255,255,0.08) !important; 
  border:1px solid rgba(255,255,255,0.15) !important; 
  border-radius:8px !important;
  color:rgba(255,255,255,0.9) !important;
  height:var(--btn-h) !important; 
  padding:0 14px !important;
  font-weight:500 !important;
  font-size:14px !important;
  text-decoration:none !important;
  display:inline-flex !important;
  align-items:center !important;
  justify-content:center !important;
  line-height:1 !important;
  transition:all 0.2s ease !important;
  backdrop-filter:blur(4px) !important;
}
.topbar .btn.ghost:hover { 
  background:rgba(255,255,255,0.15) !important;
  border-color:rgba(255,255,255,0.25) !important;
  color:white !important;
  transform:translateY(-1px) !important;
  box-shadow:0 2px 8px rgba(0,0,0,0.15) !important;
}
.topbar .btn.ghost:active { 
  transform:translateY(0) !important; 
  box-shadow:0 1px 4px rgba(0,0,0,0.1) !important;
}

/* Admin navigation button styling - same clean style but preserve colors/size */
#adminSubNav a {
  text-decoration:none !important;
  display:inline-flex !important;
  align-items:center !important;
  justify-content:center !important;
  line-height:1 !important;
  transition:all 0.2s ease !important;
  backdrop-filter:blur(4px) !important;
  border-radius:4px !important;
}
#adminSubNav a:hover {
  transform:translateY(-1px) !important;
  box-shadow:0 1px 4px rgba(0,0,0,0.1) !important;
}
#adminSubNav a:active {
  transform:translateY(0) !important;
  box-shadow:0 0.5px 2px rgba(0,0,0,0.05) !important;
}
.iconbtn{ height:var(--btn-h); width:var(--btn-h); padding:0; display:inline-grid; place-items:center;
          background:transparent; border:1px solid var(--border); color:var(--text); border-radius:6px; }
.iconbtn:hover{ background:var(--row-hover); }
.card{ margin-top:16px; background:var(--card); border:1px solid var(--border);
       border-radius:12px; box-shadow:var(--shadow-medium); overflow:hidden; }
.form{ display:grid; gap:12px; grid-template-columns: repeat(6, minmax(0,1fr)) auto;
       padding:16px; border-bottom:1px solid var(--border); }
/* Allow pages to opt-out of the global .form grid by using a unique form class */
.form.optout-grid { display:block; }
.form input, .form select{ width:100%; height:var(--btn-h); padding:0 12px; background:var(--input-bg); color:var(--text);
             border:2px solid var(--input-border); border-radius:8px; outline:none; transition:border-color 0.2s ease; }
.form input:focus, .form select:focus{ border-color:var(--input-focus); box-shadow:0 0 0 3px rgba(16, 185, 129, 0.1); }
.form .btn{ height:var(--btn-h); }
.table-wrap{ overflow-x: auto; max-width: 100%; }
table{ width: 100%; border-collapse: separate; border-spacing: 0; table-layout: fixed; min-width: 100%; }
thead th{ position:sticky; top:0; z-index:1; background:var(--primary-700); color:white; text-align:left;
          font-weight:700; padding:var(--header-pad-y) var(--header-pad-x); border-bottom:1px solid var(--border); white-space:nowrap;}
tbody td{ padding:var(--cell-pad-y) var(--cell-pad-x); border-bottom:1px solid var(--border);
          background:var(--row); vertical-align:middle; }
tbody tr:nth-child(even) td{ background:var(--row-alt); }
tbody tr:hover td{ background:var(--row-hover); }
tbody tr.completed td{ background:var(--completed) !important; }

/* Column width distribution */
table th:nth-child(1), table td:nth-child(1) { width: 10%; } /* Added Date */
table th:nth-child(2), table td:nth-child(2) { width: 10%; } /* Order Date */
table th:nth-child(3), table td:nth-child(3) { width: 8%; } /* INT */
table th:nth-child(4), table td:nth-child(4) { width: 10%; } /* Stock */
table th:nth-child(5), table td:nth-child(5) { width: 12%; } /* Reference */
table th:nth-child(6), table td:nth-child(6) { width: 25%; } /* Notes */
table th:nth-child(7), table td:nth-child(7) { width: 8%; } /* SP */
table th:nth-child(8), table td:nth-child(8) { width: 12%; } /* Return Via */
table th:nth-child(9), table td:nth-child(9) { width: 8%; } /* Notified */
table th:nth-child(10), table td:nth-child(10) { width: 8%; } /* Completed */
table th:nth-child(11), table td:nth-child(11) { width: 9%; } /* Actions */

/* Cell specific styles */
td.cell{ white-space:nowrap; overflow: hidden; text-overflow: ellipsis; }
td.cell[data-key="notes"]{ white-space:normal; }

.cell[contenteditable="true"]{ outline:none; }
.cell[contenteditable="true"]:focus{ background:var(--row-hover); border-radius:6px; box-shadow: inset 0 0 0 2px var(--primary-500); }
.pill{ display:inline-flex; align-items:center; gap:6px; border:1px solid var(--ok);
       padding:4px 8px; border-radius:999px; color:var(--ok); background:rgba(5, 150, 105, 0.1); white-space:nowrap; }

/* Role-based pill styling */
.pill.role-admin {
  border-color: #dc2626;
  color: #dc2626;
  background: rgba(220, 38, 38, 0.1);
  font-weight: 600;
  transition: all 0.2s ease;
}

.pill.role-admin:hover {
  background: rgba(220, 38, 38, 0.15);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(220, 38, 38, 0.2);
}

.pill.role-editor {
  border-color: #f59e0b;
  color: #f59e0b;
  background: rgba(245, 158, 11, 0.1);
  font-weight: 600;
  transition: all 0.2s ease;
}

.pill.role-editor:hover {
  background: rgba(245, 158, 11, 0.15);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(245, 158, 11, 0.2);
}

.pill.role-viewer {
  border-color: #10b981;
  color: #10b981;
  background: rgba(16, 185, 129, 0.1);
  font-weight: 600;
  transition: all 0.2s ease;
}

.pill.role-viewer:hover {
  background: rgba(16, 185, 129, 0.15);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(16, 185, 129, 0.2);
}
.check{ width:13px; height:13px; display:inline-block; }
.row-actions{ display:flex; gap:6px; flex-wrap:nowrap; }
.muted{opacity:.75} .small{font-size:.92em}
.footer{ padding:12px 16px; display:flex; gap:12px; align-items:center; justify-content:space-between;
         border-top:1px solid var(--border); background:var(--card); }
.error{ margin-top:12px; padding:12px 16px; border:2px solid var(--danger); background:#fef2f2; color:var(--danger); border-radius:8px; display:none; }

/* Loading States & Enhanced Feedback */
.loading {
  position: relative;
  pointer-events: none;
  opacity: 0.7;
}
.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  border: 2px solid var(--border);
  border-top: 2px solid var(--primary-500);
  border-radius: 50%;
  animation: loading-spin 0.8s linear infinite;
}
@keyframes loading-spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Success feedback */
.success-feedback {
  background: var(--ok) !important;
  color: white !important;
  transition: all 0.3s ease;
}

/* Enhanced Button States */
.btn {
  position: relative;
  transition: all 0.2s ease;
}
.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.btn.success {
  background: var(--ok);
  border-color: var(--ok);
}

/* Enhanced Form Feedback */
input:focus, select:focus {
  outline: none;
  border-color: var(--primary-500);
  box-shadow: 0 0 0 2px rgba(20, 106, 91, 0.2);
}
input.error-field {
  border-color: var(--danger);
  box-shadow: 0 0 0 2px rgba(185, 28, 28, 0.2);
}
input.success-field {
  border-color: var(--ok);
  box-shadow: 0 0 0 2px rgba(22, 163, 74, 0.2);
}

/* Enhanced Table Interactions */
tbody tr {
  transition: all 0.2s ease;
}
tbody tr:hover {
  transform: scale(1.01);
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

/* Toast Notifications */
.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 12px 16px;
  border-radius: 8px;
  font-weight: 600;
  z-index: 1000;
  transform: translateX(400px);
  transition: transform 0.3s ease;
}
.toast.show {
  transform: translateX(0);
}
.toast.success {
  background: var(--ok);
  color: white;
}
.toast.error {
  background: var(--danger);
  color: white;
}
.toast.info {
  background: var(--primary-500);
  color: white;
}

/* Keyboard shortcuts help */
.help-icon {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 40px;
  height: 40px;
  background: var(--primary-500);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  transition: all 0.2s ease;
  z-index: 999;
}
.help-icon:hover {
  transform: scale(1.1);
  background: var(--primary-300);
}

.tooltip {
  position: absolute;
  bottom: 50px;
  right: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 12px;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  transform: scale(0);
  transition: transform 0.2s ease;
  transform-origin: bottom right;
}
.tooltip.show {
  transform: scale(1);
}
.tooltip-shortcut {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 4px 0;
  gap: 12px;
}
.tooltip-key {
  background: var(--border);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 11px;
}

/* Inline select for Return Via cells */
.rv-select{
  height:28px; background:var(--input-bg); color:var(--text);
  border:1px solid var(--input-border); border-radius:6px; padding:0 8px; font-size:13px;
}

/* Reference badges */
.ref-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  margin-right: 4px;
  line-height: 1;
}

.ref-work-order { background: #dbeafe; color: #1e40af; }
.ref-delivery-ticket { background: #dcfce7; color: #166534; }
.ref-invoice { background: #f3e8ff; color: #7c3aed; }
.ref-default { background: #f1f5f9; color: #475569; }

.reference-cell {
  white-space: nowrap;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Dark theme */
[data-theme="dark"],
html[data-theme="dark"],
.theme-dark {
  --text:#e5eaf1 !important; --muted:#b8c2cf !important; --bg:#0f172a !important; --card:#1f2937 !important; --border:#374151 !important;
  --row:#1f2937 !important; --row-alt:#111827 !important; --row-hover:#374151 !important; --completed:#064e3b !important;
  --input-bg:#374151 !important; --input-border:#4b5563 !important; --input-focus:#10b981 !important;
  --shadow-light:0 1px 3px rgba(0, 0, 0, 0.3) !important;
  --shadow-medium:0 4px 12px rgba(0, 0, 0, 0.3) !important; 
  --shadow-large:0 10px 25px rgba(0, 0, 0, 0.4) !important;
  --calendar-icon-filter: invert(0.8);
}

[data-theme="dark"] .search,
.theme-dark .search { 
  background:rgba(0,0,0,.3) !important; 
  border-color:rgba(255,255,255,.2) !important; 
}

[data-theme="dark"] .error,
.theme-dark .error { 
  background:#431317 !important; 
  color:#fecaca !important; 
}

[data-theme="dark"] .pickup-form-card,
.theme-dark .pickup-form-card {
  background: #1f2937 !important;
  border-color: #374151 !important;
}

[data-theme="dark"] .pickup-form-title,
.theme-dark .pickup-form-title {
  color: #f9fafb !important;
}

[data-theme="dark"] .form-label,
.theme-dark .form-label {
  color: #d1d5db !important;
}

[data-theme="dark"] .form-input, 
[data-theme="dark"] .form-select, 
[data-theme="dark"] .form-textarea,
.theme-dark .form-input, 
.theme-dark .form-select, 
.theme-dark .form-textarea {
  background: #374151 !important;
  border-color: #4b5563 !important;
  color: #f9fafb !important;
}

[data-theme="dark"] .return-reason-wrap,
.theme-dark .return-reason-wrap {
  background: #69adc97c !important;
  border-color: #10b981 !important;
}
