:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --danger-soft: #fef2f2;
  --success: #22c55e;
  --success-hover: #16a34a;
  --success-soft: #f0fdf4;
  --warning: #f59e0b;
  --border: #e2e8f0;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.04);
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.05), 0 2px 4px -2px rgb(0 0 0 / 0.05);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.08), 0 4px 6px -4px rgb(0 0 0 / 0.04);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-full: 9999px;
  --sidebar-width: 260px;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
}

/* App shell */
.app-shell {
  min-height: 100vh;
}

.app-shell.with-sidebar {
  padding-left: var(--sidebar-width);
}

/* Top bar */
.top-bar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  align-items: center;
  padding: 0 16px;
  gap: 14px;
  z-index: 90;
  box-shadow: var(--shadow-sm);
}

.menu-toggle {
  background: none;
  border: none;
  font-size: 1.4rem;
  color: var(--text);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  line-height: 1;
}

.menu-toggle:hover {
  background: var(--surface-2);
}

.top-bar-title {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--primary);
}

/* Sidebar */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  box-shadow: var(--shadow-lg);
}

.sidebar-brand {
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
}

.sidebar-brand .logo {
  font-weight: 800;
  font-size: 1.25rem;
  color: var(--primary);
  text-decoration: none;
  letter-spacing: -0.02em;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.sidebar-brand .logo::before {
  content: "◆";
  font-size: 1rem;
}

.sidebar-close {
  display: none;
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--muted);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
}

.sidebar-close:hover {
  background: var(--surface-2);
  color: var(--text);
}

.sidebar-nav {
  flex: 1;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.95rem;
  transition: all 0.15s ease;
}

.sidebar-link:hover {
  background: var(--primary-soft);
  color: var(--primary);
}

.sidebar-group .sidebar-link {
  justify-content: flex-start;
}

.sidebar-chevron {
  margin-left: auto;
  font-size: 0.75rem;
  transition: transform 0.2s ease;
}

.sidebar-group.open .sidebar-chevron {
  transform: rotate(180deg);
}

.sidebar-submenu {
  display: none;
  flex-direction: column;
  gap: 4px;
  padding-left: 18px;
  border-left: 2px solid var(--border);
  margin-left: 22px;
}

.sidebar-group.open .sidebar-submenu,
.sidebar-submenu:has(.sidebar-link.active) {
  display: flex;
}

.sidebar-submenu .sidebar-link {
  padding: 10px 14px;
  font-size: 0.9rem;
}

.sidebar-submenu .sidebar-icon {
  font-size: 0.95rem;
}

.sidebar-link.active {
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow);
}

.sidebar-icon {
  font-size: 1.1rem;
  width: 24px;
  text-align: center;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-user-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: var(--primary-soft);
  color: var(--primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.95rem;
}

.sidebar-user-name {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-logout {
  width: 100%;
  background: var(--surface-2);
  color: var(--danger);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

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

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 95;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.sidebar-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

/* Main content */
.main-content {
  padding: 32px 28px 60px;
}

/* Login page header */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 0;
  box-shadow: var(--shadow-sm);
}

.site-header .container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

h1, h2, h3 {
  margin-top: 0;
  font-weight: 700;
  letter-spacing: -0.01em;
}

h1 {
  font-size: 2rem;
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  gap: 12px;
}

h2 {
  font-size: 1.25rem;
  margin: 36px 0 18px;
}

a.button,
button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.15s ease;
  box-shadow: var(--shadow-sm);
}

a.button:hover,
button:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow);
}

.button.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.button.secondary:hover {
  background: var(--surface-2);
}

.button.danger {
  background: var(--danger);
}

.button.danger:hover {
  background: var(--danger-hover);
}

.button.success {
  background: var(--success);
}

.button.success:hover {
  background: var(--success-hover);
}

.button.small {
  padding: 6px 12px;
  font-size: 0.82rem;
}

.alert {
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  margin-bottom: 24px;
  font-weight: 500;
  border-left: 4px solid;
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

.alert-success {
  background: var(--success-soft);
  color: #166534;
  border-color: var(--success);
}

.alert-error {
  background: var(--danger-soft);
  color: #991b1b;
  border-color: var(--danger);
}

.login-box {
  max-width: 400px;
  margin: 100px auto;
  background: var(--surface);
  padding: 36px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.login-box h1 {
  justify-content: center;
  margin-bottom: 24px;
}

.login-box form {
  text-align: left;
}

form label,
.form-grid label {
  display: block;
  margin-bottom: 18px;
  font-weight: 500;
  font-size: 0.9rem;
  color: var(--text);
}

form input,
form select,
form textarea {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  background: var(--surface);
  color: var(--text);
  transition: all 0.15s ease;
}

form input:focus,
form select:focus,
form textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.08);
}

form select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%2364748b' viewBox='0 0 16 16'%3E%3Cpath d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 38px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 18px;
  background: var(--surface);
  padding: 28px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.form-grid .full {
  grid-column: 1 / -1;
}

.actions {
  grid-column: 1 / -1;
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 8px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 12px;
  text-align: center;
}

.toolbar {
  margin-bottom: 22px;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  background: var(--surface);
  padding: 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.filters input,
.filters select {
  width: auto;
  min-width: 200px;
  margin-top: 0;
}

.filters .button {
  margin-left: auto;
}

.data-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.data-table th,
.data-table td {
  padding: 14px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.data-table th {
  background: var(--surface-2);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.data-table tbody tr {
  transition: background 0.12s ease;
}

.data-table tbody tr:hover {
  background: var(--primary-soft);
}

.data-table tr:last-child td {
  border-bottom: none;
}

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius);
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 12px;
  border-radius: var(--radius-full);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

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

.status-new,
.status-pending,
.status-not_shipped {
  background: #fef3c7;
  color: #92400e;
}

.status-contacted,
.status-confirmed,
.status-in_transit {
  background: #dbeafe;
  color: #1e40af;
}

.status-qualified,
.status-shipped {
  background: #e0e7ff;
  color: #3730a3;
}

.status-converted,
.status-completed,
.status-delivered {
  background: #dcfce7;
  color: #166534;
}

.status-lost,
.status-cancelled,
.status-returned {
  background: #fee2e2;
  color: #991b1b;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 18px;
  margin-bottom: 28px;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 14px;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
  text-align: left;
  border: 1px solid var(--border);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.card strong {
  display: block;
  font-size: 1.75rem;
  margin-bottom: 6px;
  color: var(--text);
}

.card span {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
}

.card.highlight {
  border-left: 5px solid var(--primary);
  background: linear-gradient(135deg, var(--surface) 0%, var(--primary-soft) 100%);
}

.empty {
  color: var(--muted);
  font-style: italic;
  padding: 24px 0;
}

.fade-out {
  opacity: 0;
  transition: opacity 0.5s ease;
}

section {
  margin-bottom: 32px;
}

.chart-wrap {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  height: 360px;
}

.lead-summary {
  background: var(--surface);
  padding: 24px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
}

.lead-summary p {
  margin: 0;
  padding: 8px 0;
}

.comments {
  margin: 24px 0;
}

.comment {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 18px;
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease;
}

.comment:hover {
  border-color: var(--primary);
}

.comment-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 10px;
}

.comment-body {
  white-space: pre-wrap;
  line-height: 1.6;
  color: var(--text);
}

.comment form {
  margin-top: 12px;
}

.page-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 24px;
}

.page-title-row h1 {
  margin: 0;
}

.quantity-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 18px;
  grid-column: 1 / -1;
}

.total-price-card {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 22px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  text-align: left;
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}

.total-price-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.total-price-amount {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.02em;
}

.total-price-detail {
  font-size: 0.85rem;
  color: var(--muted);
  width: 100%;
  margin-top: 6px;
}

.order-quantity-card {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 22px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.order-quantity-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.order-quantity-title {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
}

.order-quantity-card select {
  width: auto;
  min-width: 140px;
  margin: 0;
  background-color: var(--surface);
  border-color: var(--border);
  color: var(--text);
  font-weight: 500;
}

.order-quantity-card select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.08);
}

.order-quantity-card .quantity-row {
  margin-bottom: 14px;
}

.order-quantity-card .quantity-row.mode-sachets .qty-cartons,
.order-quantity-card .quantity-row.mode-cartons .qty-sachets {
  display: none;
}

.order-quantity-card .qty-kg {
  margin-bottom: 0;
}

.order-quantity-card label.qty-sachets,
.order-quantity-card label.qty-cartons,
.order-quantity-card label.qty-kg {
  color: var(--text);
  font-weight: 500;
}

.order-quantity-card input {
  background: var(--surface);
  border-color: var(--border);
  -moz-appearance: textfield;
  appearance: textfield;
}

.order-quantity-card input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.08);
}

.order-quantity-card input::-webkit-outer-spin-button,
.order-quantity-card input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.order-quantity-card input:focus {
  border-color: #f97316;
  box-shadow: 0 0 0 4px rgba(249, 115, 22, 0.12);
}

.sales-order-lines {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow-sm);
}

.sales-order-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.sales-order-title {
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
}

.sales-order-table-wrap {
  overflow-x: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.sales-order-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  min-width: 760px;
}

.sales-order-table th,
.sales-order-table td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.sales-order-table th {
  background: var(--surface-2);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  white-space: nowrap;
}

.sales-order-table tr:last-child td {
  border-bottom: none;
}

.sales-order-table select,
.sales-order-table input {
  width: 100%;
  min-width: 100px;
  margin: 0;
  padding: 9px 11px;
  font-size: 0.9rem;
}

.sales-order-table .line-total {
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  text-align: right;
}

.sales-order-table .remove-line {
  padding: 6px 10px;
  font-size: 1rem;
  line-height: 1;
}

.sales-order-lines .empty {
  margin: 0;
  padding: 18px 0 0;
  text-align: center;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--primary-soft);
  color: var(--primary);
}

@media (max-width: 900px) {
  .app-shell.with-sidebar {
    padding-left: 0;
  }

  .top-bar {
    display: flex;
  }

  .main-content {
    padding-top: 80px;
  }

  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

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

  .sidebar-overlay {
    display: block;
  }

  .sidebar-close {
    display: inline-flex;
  }
}

@media (max-width: 640px) {
  .main-content {
    padding: 80px 18px 40px;
  }

  .filters {
    flex-direction: column;
    align-items: stretch;
  }

  .filters input,
  .filters select,
  .filters .button {
    width: 100%;
    margin-left: 0;
  }

  .form-grid {
    padding: 18px;
  }

  .actions {
    flex-direction: column;
    align-items: stretch;
  }

  .actions .button,
  .actions button {
    width: 100%;
  }

  .data-table th,
  .data-table td {
    padding: 12px;
  }

  h1 {
    font-size: 1.6rem;
  }
}
