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

:root {
  --primary: #4f46e5;
  --primary-light: #818cf8;
  --primary-dark: #3730a3;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #3b82f6;
  --bg: #f1f5f9;
  --bg-white: #ffffff;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --border-focus: #818cf8;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 15px rgba(0,0,0,0.1), 0 4px 6px rgba(0,0,0,0.05);
  --radius: 8px;
  --radius-lg: 12px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }

/* ========== Layout ========== */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* ========== Sidebar ========== */
.sidebar {
  width: 240px;
  background: var(--bg-white);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
  box-shadow: var(--shadow);
  transition: transform 0.3s ease;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-logo {
  width: 36px;
  height: 36px;
  background: var(--primary);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  font-weight: bold;
}

.sidebar-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.sidebar-nav {
  flex: 1;
  padding: 12px 8px;
  overflow-y: auto;
}

.nav-section {
  margin-bottom: 8px;
}

.nav-section-title {
  padding: 8px 12px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 14px;
  transition: all 0.15s ease;
  margin-bottom: 2px;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.nav-item:hover {
  background: #f1f5f9;
  color: var(--text);
}

.nav-item.active {
  background: #eef2ff;
  color: var(--primary);
  font-weight: 600;
}

.nav-item .nav-icon {
  font-size: 18px;
  width: 22px;
  text-align: center;
  flex-shrink: 0;
}

/* ========== Main Content ========== */
.main-content {
  flex: 1;
  margin-left: 240px;
  padding: 0;
  min-height: 100vh;
}

.page-header {
  background: var(--bg-white);
  border-bottom: 1px solid var(--border);
  padding: 16px 28px;
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
}

.page-body {
  padding: 24px 28px;
  max-width: 1400px;
}

/* ========== Cards ========== */
.card {
  background: var(--bg-white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  margin-bottom: 20px;
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
}

.card-body {
  padding: 20px;
}

/* ========== Stats Cards ========== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-white);
  border-radius: var(--radius-lg);
  padding: 20px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.stat-card .stat-label {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.stat-card .stat-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
}

.stat-card .stat-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}

.stat-card.primary .stat-value { color: var(--primary); }
.stat-card.success .stat-value { color: var(--success); }
.stat-card.warning .stat-value { color: var(--warning); }
.stat-card.info .stat-value { color: var(--info); }

/* ========== Tables ========== */
.table-container {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

table th {
  background: #f8fafc;
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  font-size: 13px;
}

table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

table tbody tr:hover {
  background: #f8fafc;
}

table .text-right { text-align: right; }
table .text-center { text-align: center; }

/* ========== Forms ========== */
.form-group {
  margin-bottom: 16px;
}

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-label .required {
  color: var(--danger);
  margin-left: 2px;
}

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--text);
  background: var(--bg-white);
  transition: border-color 0.15s, box-shadow 0.15s;
  font-family: inherit;
  line-height: 1.5;
}

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

.form-textarea {
  resize: vertical;
  min-height: 60px;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.form-inline {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  flex-wrap: wrap;
}

.form-inline .form-group {
  margin-bottom: 0;
}

/* ========== Buttons ========== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
  white-space: nowrap;
  line-height: 1.5;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

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

.btn-success {
  background: var(--success);
  color: white;
}
.btn-success:hover:not(:disabled) { background: #059669; }

.btn-warning {
  background: var(--warning);
  color: white;
}
.btn-warning:hover:not(:disabled) { background: #d97706; }

.btn-danger {
  background: var(--danger);
  color: white;
}
.btn-danger:hover:not(:disabled) { background: #dc2626; }

.btn-outline {
  background: transparent;
  border-color: var(--border);
  color: var(--text-secondary);
}
.btn-outline:hover:not(:disabled) {
  background: #f8fafc;
  color: var(--text);
  border-color: var(--text-muted);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}

.btn-lg {
  padding: 12px 24px;
  font-size: 16px;
}

.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
}

/* ========== Badges ========== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.badge-primary { background: #eef2ff; color: var(--primary); }
.badge-success { background: #d1fae5; color: #065f46; }
.badge-warning { background: #fef3c7; color: #92400e; }

/* ========== Modal ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease;
}

.modal {
  background: var(--bg-white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 90%;
  max-width: 600px;
  max-height: 85vh;
  overflow-y: auto;
  animation: slideUp 0.25s ease;
}

.modal-lg { max-width: 800px; }

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--bg-white);
  z-index: 1;
}

.modal-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 20px;
  color: var(--text-muted);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-close:hover {
  background: #f1f5f9;
  color: var(--text);
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  position: sticky;
  bottom: 0;
  background: var(--bg-white);
}

/* ========== Tabs ========== */
.tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  margin-bottom: 20px;
}

.tab-item {
  padding: 10px 20px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all 0.15s;
  font-family: inherit;
}

.tab-item:hover { color: var(--text); }
.tab-item.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

/* ========== Empty State ========== */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}

.empty-state .empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state h3 {
  font-size: 16px;
  margin-bottom: 6px;
  color: var(--text-secondary);
}

.empty-state p {
  font-size: 13px;
  margin-bottom: 16px;
}

/* ========== Alerts / Messages ========== */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-size: 13px;
}

.alert-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.alert-warning { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }
.alert-danger { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* ========== Toast ========== */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  border-radius: var(--radius);
  color: white;
  font-size: 14px;
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.3s ease;
  min-width: 250px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-warning { background: var(--warning); }

/* ========== Upload Area ========== */
.upload-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  background: #fafbfc;
}

.upload-area:hover {
  border-color: var(--primary-light);
  background: #eef2ff;
}

.upload-area .upload-icon {
  font-size: 40px;
  margin-bottom: 8px;
}

.upload-area p {
  color: var(--text-secondary);
  font-size: 14px;
}

.upload-area .upload-hint {
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 4px;
}

/* ========== Animations ========== */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes slideIn {
  from { opacity: 0; transform: translateX(100px); }
  to { opacity: 1; transform: translateX(0); }
}

/* ========== Responsive ========== */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .main-content {
    margin-left: 0;
  }
  .page-body {
    padding: 16px;
  }
  .page-header {
    padding: 12px 16px;
  }
  .form-row {
    grid-template-columns: 1fr;
  }
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .modal {
    width: 95%;
    max-height: 90vh;
  }
}

/* ========== Mobile Menu Button ========== */
.menu-toggle {
  display: none;
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text);
  border-radius: var(--radius);
  align-items: center;
  justify-content: center;
}

@media (max-width: 768px) {
  .menu-toggle {
    display: flex;
  }
}

/* ========== Search/Filter bar ========== */
.filter-bar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: flex-end;
  margin-bottom: 16px;
}

.filter-bar .form-group {
  margin-bottom: 0;
  min-width: 140px;
}

/* ========== Chart container ========== */
.chart-container {
  position: relative;
  width: 100%;
  height: 350px;
}

.chart-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

@media (max-width: 768px) {
  .chart-row {
    grid-template-columns: 1fr;
  }
}

/* ========== Pagination ========== */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  margin-top: 16px;
}

.pagination button {
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: white;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
}

.pagination button:hover:not(:disabled) {
  background: #f1f5f9;
  color: var(--text);
}

.pagination button.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.pagination button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ========== Client detail row ========== */
.db-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 12px;
  margin-top: auto;
  padding-top: 12px;
  padding-bottom: 12px;
  border-top: 1px solid var(--border);
}

.db-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
}

/* ========== Searchable Select ========== */
.ss-wrapper {
  position: relative;
  width: 100%;
}

.ss-input-box {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-white);
  transition: border-color 0.15s, box-shadow 0.15s;
  cursor: text;
  min-height: 38px;
}

.ss-input-box:hover { border-color: var(--text-muted); }
.ss-input-box.ss-open {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
  border-radius: var(--radius) var(--radius) 0 0;
}

.ss-search-input {
  flex: 1;
  border: none !important;
  outline: none !important;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: transparent;
  min-width: 0;
  border-radius: var(--radius);
}
.ss-search-input:focus { outline: none; box-shadow: none; }

.ss-clear {
  display: none;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 14px;
  flex-shrink: 0;
  margin-right: 2px;
}
.ss-clear:hover { background: #e2e8f0; color: var(--text); }

.ss-arrow {
  color: var(--text-muted);
  font-size: 10px;
  padding: 0 10px;
  flex-shrink: 0;
}

.ss-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  max-height: 240px;
  overflow-y: auto;
  background: var(--bg-white);
  border: 1px solid var(--border-focus);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  z-index: 500;
  box-shadow: var(--shadow-md);
}

.ss-option {
  padding: 9px 12px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.ss-option:hover { background: #f1f5f9; }
.ss-option.ss-highlight { background: #eef2ff; }
.ss-option.ss-selected { background: #eef2ff; color: var(--primary); font-weight: 500; }

.ss-sub-label { font-size: 12px; color: var(--text-muted); }

.ss-empty {
  padding: 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* ========== Login Page ========== */
.login-overlay {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  z-index: 9000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: white;
  border-radius: 16px;
  padding: 40px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  animation: slideUp 0.4s ease;
}

.login-logo {
  text-align: center;
  margin-bottom: 28px;
}

.login-logo-icon {
  width: 56px;
  height: 56px;
  background: var(--primary);
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 26px;
  font-weight: bold;
  margin-bottom: 10px;
}

.login-logo h2 {
  font-size: 20px;
  color: var(--text);
  font-weight: 700;
}

.login-logo p {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
}

.login-error {
  background: #fef2f2;
  color: #991b1b;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 16px;
  display: none;
}

.login-remember {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  font-size: 13px;
  color: var(--text-secondary);
}

.login-remember input[type="checkbox"] { accent-color: var(--primary); }

.login-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-actions .btn { justify-content: center; width: 100%; padding: 11px; }

/* ========== User Header ========== */
.user-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

.user-name {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}

.user-role {
  font-size: 11px;
  color: var(--text-muted);
}

.btn-logout {
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: none;
  font-family: inherit;
}
.btn-logout:hover { color: var(--danger); border-color: var(--danger); }

/* ========== History Timeline ========== */
.history-list { }

.history-item {
  position: relative;
  padding: 12px 0 12px 28px;
  border-left: 2px solid var(--border);
}

.history-item:last-child { border-left-color: transparent; }

.history-dot {
  position: absolute;
  left: -6px;
  top: 16px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
}

.history-item.original .history-dot { background: var(--success); }

.history-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
  display: flex;
  gap: 8px;
}

.history-content {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.history-content span {
  display: inline-block;
  margin-right: 12px;
}

/* ========== User Management ========== */
.user-mgmt-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

@media (max-width: 768px) {
  .user-mgmt-grid { grid-template-columns: 1fr; }
}

.change-password-section {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
