:root {
  color-scheme: light;
  --brand: #3c4a2d;
  --brand-dark: #2c3721;
  --surface-1: #fcfcfb;
  --page: #f4f3ee;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid-line: #e1e0d9;
  --axis-line: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);

  --series-1: #2a78d6; /* blue */
  --series-2: #eb6834; /* orange */
  --series-3: #1baf7a; /* aqua */
  --series-4: #eda100; /* yellow */
  --series-5: #e87ba4; /* magenta */
  --series-6: #008300; /* green */
  --series-7: #4a3aa7; /* violet */
  --series-8: #e34948; /* red */
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid-line: #2c2c2a;
    --axis-line: #383835;
    --border: rgba(255, 255, 255, 0.1);

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
}

.topbar {
  background: var(--brand);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
}

.brand { display: flex; align-items: center; gap: 10px; }
.logo-mark { width: 26px; height: 26px; color: #f4f1e0; flex-shrink: 0; }
.brand-mark { font-weight: 800; font-size: 20px; }
.brand-name { font-size: 15px; opacity: 0.85; }

.main-tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  overflow-x: auto;
}
.main-tab-btn {
  border: none;
  background: none;
  padding: 14px 18px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.main-tab-btn:hover { color: var(--text-primary); }
.main-tab-btn.active { color: var(--brand); border-bottom-color: var(--brand); }

.tab-panel { display: none; flex-direction: column; gap: 20px; }
.tab-panel.active { display: flex; }

.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-user {
  font-size: 13px;
  opacity: 0.85;
  margin-right: 4px;
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 0;
}
.btn-ghost.btn-on-brand { border-color: rgba(255, 255, 255, 0.5); color: #fff; }
.btn-ghost.btn-on-brand:hover { background: rgba(255, 255, 255, 0.15); }

.account-info { display: flex; flex-direction: column; gap: 10px; }
.account-info-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--grid-line);
}
.account-info-label { font-size: 13px; color: var(--text-muted); }
.account-info-value { font-size: 13px; color: var(--text-primary); font-weight: 600; text-align: right; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(11, 11, 11, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-card { width: 100%; max-width: 360px; }

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 16px 60px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
}

.panel h3 {
  margin: 0 0 14px;
  font-size: 15px;
  color: var(--text-primary);
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.chart-header h3 { margin: 0; }

.period-toggle {
  display: flex;
  gap: 4px;
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
}
.period-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
}
.period-btn.active { background: var(--brand); color: #fff; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
}

.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
}

.stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.filter-row, .sales-form {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: end;
}

.field { display: flex; flex-direction: column; gap: 6px; min-width: 150px; }
.field label { font-size: 12px; color: var(--text-secondary); }
.field-actions { flex-direction: row; gap: 10px; }

input, select {
  padding: 8px 10px;
  border: 1px solid var(--axis-line);
  border-radius: 8px;
  font-size: 14px;
  background: var(--surface-1);
  color: var(--text-primary);
}

.btn {
  padding: 9px 16px;
  border-radius: 8px;
  border: none;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}

.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-ghost { background: transparent; border: 1px solid var(--axis-line); color: var(--text-secondary); }
.btn-export { background: #fff; color: var(--brand); }
.btn-export:hover { background: #eee; }

.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.history-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
}
.badge-create { background: #0ca30c; }
.badge-update { background: #eda100; }
.badge-delete { background: #d03b3b; }
.badge-undo { background: var(--series-1); }

.history-detail { font-size: 13px; color: var(--text-secondary); }
.history-detail .diff-field { color: var(--text-primary); font-weight: 600; }
.history-undone-label { font-size: 12px; color: var(--text-muted); font-style: italic; }

.chart-wide { grid-column: 1 / -1; }

.table-wrap { overflow-x: auto; }

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

th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--grid-line);
  white-space: nowrap;
}

th { color: var(--text-muted); font-weight: 600; font-size: 12px; }
td { color: var(--text-primary); font-variant-numeric: tabular-nums; }

#historyTable .history-detail { white-space: normal; min-width: 220px; }

.undo-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  color: var(--series-1);
  font-weight: 600;
}

.row-actions button {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  margin-right: 8px;
  color: var(--series-1);
}

.row-actions .delete-btn { color: var(--series-8); }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 24px 0;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding-top: 14px;
  font-size: 13px;
  color: var(--text-secondary);
}

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