* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --bg: #f1f5f9;
  --sidebar-bg: #1e293b;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #3b82f6;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-light: #64748b;
  --border: #e2e8f0;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
}

body { font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); }

/* Login Page */
.login-wrapper {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
}
.login-box {
  background: var(--card-bg); padding: 40px; border-radius: 12px;
  width: 400px; max-width: 90vw; box-shadow: 0 25px 50px rgba(0,0,0,0.25);
}
.login-box h1 { text-align: center; margin-bottom: 8px; color: var(--primary); font-size: 24px; }
.login-box p.subtitle { text-align: center; color: var(--text-light); margin-bottom: 24px; font-size: 14px; }
.login-box .error-msg { background: #fef2f2; color: var(--danger); padding: 10px; border-radius: 6px; margin-bottom: 16px; font-size: 14px; display: none; }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 4px; font-weight: 600; font-size: 14px; color: var(--text-light); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px; transition: border-color 0.2s;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}
.form-group textarea { resize: vertical; min-height: 80px; }
.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }

/* Buttons */
.btn {
  padding: 10px 20px; border: none; border-radius: 6px; font-size: 14px;
  font-weight: 600; cursor: pointer; transition: all 0.2s; display: inline-flex;
  align-items: center; gap: 6px;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: #15803d; }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #b91c1c; }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover { background: #cbd5e1; }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-block { width: 100%; justify-content: center; }

/* Layout */
.app-layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; background: var(--sidebar-bg); color: var(--sidebar-text);
  padding: 0; position: fixed; top: 0; left: 0; bottom: 0;
  z-index: 100; transition: transform 0.3s;
  display: flex; flex-direction: column;
}
.sidebar-header { padding: 20px; border-bottom: 1px solid rgba(255,255,255,0.1); flex-shrink: 0; }
.sidebar-header h2 { color: white; font-size: 18px; }
.sidebar-header small { color: var(--sidebar-text); font-size: 12px; }
.sidebar-nav { padding: 12px 0; flex: 1; overflow-y: auto; }
.sidebar-nav a {
  display: flex; align-items: center; gap: 10px; padding: 10px 20px;
  color: var(--sidebar-text); text-decoration: none; font-size: 14px; transition: all 0.2s;
}
.sidebar-nav a:hover, .sidebar-nav a.active {
  background: rgba(255,255,255,0.1); color: white;
}
.sidebar-nav a.active { border-left: 3px solid var(--sidebar-active); }
.sidebar-nav .nav-section { padding: 16px 20px 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: #475569; }
.sidebar-footer { flex-shrink: 0; width: 100%; padding: 16px 20px; border-top: 1px solid rgba(255,255,255,0.1); }
.sidebar-footer .user-info { font-size: 13px; color: white; margin-bottom: 8px; }
.sidebar-footer .user-role { font-size: 11px; color: var(--sidebar-text); text-transform: uppercase; }

.main-content { margin-left: 240px; flex: 1; padding: 24px; min-height: 100vh; }

/* Top Bar */
.top-bar {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px;
}
.top-bar h1 { font-size: 22px; }
.top-bar-actions { display: flex; gap: 8px; }

/* Filter Bar */
.filter-bar {
  display: flex; gap: 8px; margin-bottom: 16px; align-items: center; flex-wrap: wrap;
}
.filter-bar select {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; background: var(--card-bg); color: var(--text);
}
.filter-bar select:focus { outline: none; border-color: var(--primary); }
.filter-search {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; background: var(--card-bg); min-width: 180px; transition: border-color 0.2s;
}
.filter-search:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,0.1); }
.filter-search::placeholder { color: var(--text-light); }
.filter-bar input[type="date"] {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; background: var(--card-bg); color: var(--text);
}

/* Pagination */
.pagination-bar {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  padding: 12px 0; font-size: 14px; color: var(--text-light);
}
.pagination-bar button {
  padding: 6px 14px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--card-bg); cursor: pointer; font-size: 13px; color: var(--text);
}
.pagination-bar button:hover:not(:disabled) { background: var(--primary); color: white; border-color: var(--primary); }
.pagination-bar button:disabled { opacity: 0.4; cursor: not-allowed; }
.pagination-bar .page-info { font-weight: 500; }

/* Cards */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card {
  background: var(--card-bg); padding: 20px; border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.stat-card .stat-label { font-size: 13px; color: var(--text-light); margin-bottom: 4px; }
.stat-card .stat-value { font-size: 28px; font-weight: 700; }
.stat-card .stat-value.revenue { color: var(--success); }

.card {
  background: var(--card-bg); border-radius: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}
.card-header {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.card-header h3 { font-size: 16px; }
.card-body { padding: 20px; }

/* Tables */
.table-wrapper { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: left; font-size: 14px; border-bottom: 1px solid var(--border); }
th { font-weight: 600; color: var(--text-light); font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; background: #f8fafc; }
tr:hover td { background: #f8fafc; }

/* Status badges */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 20px;
  font-size: 12px; font-weight: 600;
}
.badge-pending { background: #fef3c7; color: #92400e; }
.badge-confirmed { background: #dbeafe; color: #1e40af; }
.badge-in_production { background: #e0e7ff; color: #3730a3; }
.badge-shipped { background: #d1fae5; color: #065f46; }
.badge-delivered { background: #dcfce7; color: #166534; }
.badge-cancelled { background: #fee2e2; color: #991b1b; }
.badge-admin { background: #ede9fe; color: #5b21b6; }
.badge-user { background: #e0e7ff; color: #3730a3; }
.badge-standard { background: #dbeafe; color: #1e40af; }
.badge-special { background: #fef3c7; color: #92400e; }
.badge-direct { background: #d1fae5; color: #065f46; }
.badge-distributor { background: #e0e7ff; color: #3730a3; }
.badge-lead { background: #f1f5f9; color: #475569; }
.badge-qualified { background: #dbeafe; color: #1e40af; }
.badge-proposal { background: #e0e7ff; color: #3730a3; }
.badge-negotiation { background: #fef3c7; color: #92400e; }
.badge-closed_won { background: #dcfce7; color: #166534; }
.badge-closed_lost { background: #fee2e2; color: #991b1b; }
.badge-meeting { background: #dbeafe; color: #1e40af; }
.badge-call { background: #d1fae5; color: #065f46; }
.badge-visit { background: #fef3c7; color: #92400e; }
.badge-presentation { background: #e0e7ff; color: #3730a3; }
.badge-event { background: #ede9fe; color: #5b21b6; }
.badge-email { background: #f1f5f9; color: #475569; }
.badge-other { background: #f3f4f6; color: #374151; }

/* Modal */
.modal-backdrop {
  display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5); z-index: 200; align-items: center; justify-content: center;
}
.modal-backdrop.active { display: flex; }
.modal {
  background: var(--card-bg); border-radius: 12px; width: 600px; max-width: 90vw;
  max-height: 90vh; overflow-y: auto; box-shadow: 0 25px 50px rgba(0,0,0,0.25);
}
.modal-header {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.modal-header h3 { font-size: 16px; }
.modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text-light); }
.modal-body { padding: 20px; }
.modal-footer { padding: 16px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.modal-wide { width: 800px; }
.modal-full { width: 95vw; max-width: 1100px; }

/* Quill editor in modal */
#quillEditorContainer { height: 350px; margin-bottom: 12px; }
#quillEditorContainer .ql-editor { min-height: 280px; }
.quote-preview { border: 1px solid var(--border); padding: 24px; margin: 12px 0; background: white; }
.quote-preview img { max-width: 100%; }

/* Spec rows */
.spec-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }
.spec-row input { flex: 1; }
.spec-row .btn-remove { padding: 6px 10px; }

/* Order items */
.order-item-row { border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin-bottom: 10px; }
.order-item-row .form-row { margin-bottom: 8px; }

/* Activity list */
.activity-item { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.activity-item:last-child { border-bottom: none; }
.activity-time { color: var(--text-light); white-space: nowrap; min-width: 140px; }

/* Mobile-only elements: hidden on desktop */
.mobile-header,
.mobile-bottom-nav,
.mobile-more-sheet,
.mobile-more-overlay,
.mobile-user-menu,
.mobile-user-overlay { display: none !important; }

/* Mobile Header */
.mobile-header-title { font-size: 17px; font-weight: 700; }
.mobile-header-user {
  width: 34px; height: 34px; border-radius: 50%; background: var(--sidebar-active);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; cursor: pointer; color: white;
}

/* Mobile User Menu */
.mobile-user-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.3); z-index: 160; }
.mobile-user-menu { position: fixed; top: 56px; right: 8px; z-index: 170; background: white; border-radius: 12px; width: 260px; box-shadow: 0 10px 40px rgba(0,0,0,0.2); overflow: hidden; }
.mobile-user-menu-header {
  padding: 16px; background: var(--sidebar-bg); color: white;
}
.mobile-user-menu-name { font-weight: 700; font-size: 15px; }
.mobile-user-menu-role { font-size: 12px; color: var(--sidebar-text); text-transform: uppercase; margin-top: 2px; }
.mobile-user-menu-item {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px;
  border: none; background: none; width: 100%; text-align: left;
  font-size: 14px; color: var(--text); cursor: pointer;
  border-top: 1px solid var(--border);
}
.mobile-logout-btn { color: var(--danger); font-weight: 600; }

/* Mobile Bottom Nav */
.mobile-nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  text-decoration: none; color: var(--text-light); font-size: 10px; font-weight: 600;
  padding: 6px 0; min-width: 56px; transition: color 0.2s;
}
.mobile-nav-item svg { stroke: var(--text-light); transition: stroke 0.2s; }
.mobile-nav-item.active { color: var(--primary); }
.mobile-nav-item.active svg { stroke: var(--primary); }

/* Mobile More Sheet */
.mobile-more-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.4); z-index: 130; }
.mobile-more-sheet { position: fixed; bottom: 0; left: 0; right: 0; z-index: 140; background: white; border-radius: 16px 16px 0 0; padding: 8px 0 calc(20px + env(safe-area-inset-bottom, 0)); max-height: 70vh; box-shadow: 0 -4px 30px rgba(0,0,0,0.15); }
.mobile-more-handle {
  width: 36px; height: 4px; background: #d1d5db; border-radius: 2px;
  margin: 4px auto 12px;
}
.mobile-more-item {
  display: flex; align-items: center; gap: 14px; padding: 14px 24px;
  text-decoration: none; color: var(--text); font-size: 15px; font-weight: 500;
}
.mobile-more-item svg { stroke: var(--text-light); }
.mobile-more-item:active { background: var(--bg); }

/* Responsive */
@media (max-width: 768px) {
  /* Prevent zoom / horizontal scroll */
  html, body { overflow-x: hidden; width: 100%; }

  /* Mobile elements ON */
  .mobile-header { display: flex !important; position: fixed; top: 0; left: 0; right: 0; z-index: 120; height: 56px; background: var(--sidebar-bg); color: white; padding: 0 16px; align-items: center; justify-content: space-between; box-shadow: 0 2px 8px rgba(0,0,0,0.15); }
  .mobile-bottom-nav { display: flex !important; position: fixed; bottom: 0; left: 0; right: 0; z-index: 120; height: 64px; background: white; border-top: 1px solid var(--border); justify-content: space-around; align-items: center; padding-bottom: env(safe-area-inset-bottom, 0); box-shadow: 0 -2px 10px rgba(0,0,0,0.06); }
  .mobile-more-overlay.active { display: block !important; }
  .mobile-more-sheet.active { display: block !important; }
  .mobile-user-overlay.active { display: block !important; }
  .mobile-user-menu.active { display: block !important; }

  /* Sidebar OFF */
  .sidebar { display: none !important; }

  /* Layout */
  .main-content { margin-left: 0; padding: 12px; padding-top: 64px; padding-bottom: 76px; overflow-x: hidden; }
  .top-bar { flex-direction: column; gap: 10px; align-items: flex-start; }
  .top-bar h1 { display: none; }
  .top-bar-actions { width: 100%; }
  .top-bar-actions .btn { width: 100%; justify-content: center; }
  .form-row { flex-direction: column; }

  /* Stats */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-card { padding: 12px; }
  .stat-card .stat-label { font-size: 11px; }
  .stat-card .stat-value { font-size: 18px; }

  /* Cards */
  .card { border-radius: 8px; }
  .card-body { padding: 10px; }
  .card-header { padding: 10px 14px; }

  /* Tables - compact and hide columns */
  .table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
  table { font-size: 12px; min-width: 0; }
  th, td { padding: 6px 6px; white-space: nowrap; }

  /* Hide less important columns on mobile via nth-child */
  /* Orders table: hide Channel(3), Subtotal(4), VAT(5), Commission(6), Date(9) => show Order#, Customer, Net Total, Status, Actions */
  #ordersTable th:nth-child(3), #ordersTable td:nth-child(3),
  #ordersTable th:nth-child(4), #ordersTable td:nth-child(4),
  #ordersTable th:nth-child(5), #ordersTable td:nth-child(5),
  #ordersTable th:nth-child(6), #ordersTable td:nth-child(6),
  #ordersTable th:nth-child(9), #ordersTable td:nth-child(9) { display: none; }

  /* Opportunities table: hide Channel(3), Expected Close(6) => show Name, Customer, Value, Stage, Actions */
  #opportunitiesTable th:nth-child(3), #opportunitiesTable td:nth-child(3),
  #opportunitiesTable th:nth-child(6), #opportunitiesTable td:nth-child(6) { display: none; }

  /* Customers table: hide Email(3), Phone(4) => show Company, Contact, Actions */
  #customersTable th:nth-child(3), #customersTable td:nth-child(3),
  #customersTable th:nth-child(4), #customersTable td:nth-child(4) { display: none; }

  /* Contacts table: hide Title(3), Email(4), Phone(5) => show Company, Name, Primary, Actions */
  #contactsTable th:nth-child(3), #contactsTable td:nth-child(3),
  #contactsTable th:nth-child(4), #contactsTable td:nth-child(4),
  #contactsTable th:nth-child(5), #contactsTable td:nth-child(5) { display: none; }

  /* Products table: hide Specifications(4) => show Name, Type, Price, Actions */
  #productsTable th:nth-child(4), #productsTable td:nth-child(4) { display: none; }

  /* Distributors table: hide Email(3), Phone(4) => show Name, Contact, Commission, Actions */
  #distributorsTable th:nth-child(3), #distributorsTable td:nth-child(3),
  #distributorsTable th:nth-child(4), #distributorsTable td:nth-child(4) { display: none; }

  /* Users table: hide Email(3) => show Username, Name, Role, Status, Actions */
  #usersTable th:nth-child(3), #usersTable td:nth-child(3) { display: none; }

  /* Dashboard recent orders: hide Channel(3), Date(6) */
  #dashRecentOrders th:nth-child(3), #dashRecentOrders td:nth-child(3),
  #dashRecentOrders th:nth-child(6), #dashRecentOrders td:nth-child(6) { display: none; }

  /* Dashboard upcoming payments: hide Customer(3), Label(4) */
  #dashUpcomingPayments th:nth-child(3), #dashUpcomingPayments td:nth-child(3),
  #dashUpcomingPayments th:nth-child(4), #dashUpcomingPayments td:nth-child(4) { display: none; }

  /* Dashboard top distributors: hide Commission(4) */
  #dashTopDist th:nth-child(4), #dashTopDist td:nth-child(4) { display: none; }

  /* Dashboard 2-col grid to 1-col */
  #page-dashboard div[style*="grid-template-columns:1fr 1fr"] { grid-template-columns: 1fr !important; }

  /* Modals - full screen sheet from bottom */
  .modal-backdrop.active { align-items: flex-end; }
  .modal, .modal-wide, .modal-full {
    width: 100vw !important; max-width: 100vw !important;
    border-radius: 12px 12px 0 0; max-height: 92vh;
    margin: 0;
  }
  .modal-body { padding: 14px; overflow-x: hidden; word-wrap: break-word; }
  .modal-body table { table-layout: fixed; width: 100%; }
  .modal-body th, .modal-body td { white-space: normal; word-break: break-word; }
  .modal-body .form-row { flex-direction: column; }
  .modal-footer { flex-wrap: wrap; padding: 12px 14px; }
  .modal-footer .btn { flex: 1; min-width: 0; justify-content: center; font-size: 13px; padding: 10px 8px; }
  .modal-header { padding: 12px 14px; }
  .modal-header h3 { font-size: 15px; }

  /* Buttons */
  .btn { padding: 10px 14px; font-size: 13px; }
  .btn-sm { padding: 6px 10px; font-size: 11px; }

  /* Activity */
  .activity-item { flex-direction: column; gap: 2px; }
  .activity-time { min-width: unset; font-size: 11px; }

  /* Quill editor smaller on mobile */
  #quillEditorContainer { height: 200px; }
  #quillEditorContainer .ql-editor { min-height: 150px; }

  /* Order item rows in modals */
  .order-item-row { padding: 8px; }
  .order-item-row .form-row { flex-direction: column; }
  .spec-row { flex-wrap: wrap; }
}

/* Utility */
.hidden { display: none !important; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-16 { margin-bottom: 16px; }
.clickable { cursor: pointer; }
.no-data { text-align: center; padding: 40px; color: var(--text-light); }
