:root {
  --bg: #F8FAFC;
  --surface: #FFFFFF;
  --surface-2: #F1F5F9;
  --border: #E2E8F0;
  --border-focus: #3B82F6;
  --text: #0F172A;
  --text-muted: #64748B;
  --accent: #2563EB;
  --accent-hover: #1D4ED8;
  --accent-light: #EFF6FF;
  --danger: #DC2626;
  --danger-light: #FEF2F2;
  --success-light: #ECFDF5;
  --chart-green: #10B981;
  --chart-purple: #8B5CF6;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.07), 0 1px 2px rgba(0,0,0,.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,.07), 0 2px 4px -2px rgba(0,0,0,.05);
}

[data-theme="dark"] {
  --bg: #0F172A;
  --surface: #1E293B;
  --surface-2: #273548;
  --border: #334155;
  --border-focus: #60A5FA;
  --text: #F1F5F9;
  --text-muted: #94A3B8;
  --accent: #3B82F6;
  --accent-hover: #60A5FA;
  --accent-light: #1E3A5F;
  --danger: #F87171;
  --danger-light: #2D1515;
  --success-light: #0D2B1E;
  --chart-green: #34D399;
  --chart-purple: #A78BFA;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

/* ── Topbar ────────────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.topbar-brand {
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  letter-spacing: -0.01em;
}

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

.topbar-username {
  font-weight: 500;
  color: var(--text-muted);
  font-size: 13px;
}

/* ── Nav ───────────────────────────────────────────────────────── */

.nav-tabs {
  display: flex;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.nav-tab {
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: color 120ms, border-color 120ms;
  margin-bottom: -1px;
  white-space: nowrap;
  font-family: inherit;
}

.nav-tab:hover { color: var(--text); }

.nav-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* ── Main content ──────────────────────────────────────────────── */

.main-content {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 16px 64px;
}

/* ── Cards ─────────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text);
}

/* ── Forms ─────────────────────────────────────────────────────── */

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
  align-items: end;
}

.form-grid label {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-grid label > span {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

input, select {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 120ms, box-shadow 120ms;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

input:focus, select:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(59,130,246,.12);
}

input:disabled {
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: not-allowed;
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748B' stroke-width='2.5'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
  cursor: pointer;
}

.input-computed {
  background: var(--surface-2);
  color: var(--text-muted);
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
}

.form-actions {
  display: flex;
  align-items: flex-end;
}

.checkbox-label {
  flex-direction: row !important;
  align-items: center !important;
  gap: 8px !important;
}

.checkbox-label input {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
  cursor: pointer;
}

/* ── Buttons ───────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 36px;
  padding: 0 14px;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  border-radius: var(--radius);
  border: none;
  cursor: pointer;
  text-decoration: none;
  transition: background 120ms, opacity 120ms;
  white-space: nowrap;
  line-height: 1;
}

.btn:active { opacity: 0.85; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; pointer-events: none; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }

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

.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.btn-sm { height: 30px; padding: 0 10px; font-size: 13px; }

.btn-icon { width: 30px; padding: 0; }

.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  padding: 4px 0;
  text-decoration: none;
  transition: opacity 120ms;
}
.btn-link:hover { opacity: 0.75; }

.btn-icon-danger {
  width: 26px;
  height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--danger-light);
  color: var(--danger);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  transition: background 120ms;
}
.btn-icon-danger:hover { background: #fecaca; }

/* ── Tables ────────────────────────────────────────────────────── */

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

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

th {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

td {
  padding: 10px 10px;
  border-bottom: 1px solid var(--bg);
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }
tr:hover td { background: #FAFBFC; }

/* ── Badges ────────────────────────────────────────────────────── */

.badge {
  display: inline-block;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 600;
  border-radius: 20px;
  background: var(--accent-light);
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.chip {
  display: inline-block;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* ── Typography ────────────────────────────────────────────────── */

.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; }
.text-muted { color: var(--text-muted); }
.label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 2px;
}

/* ── Login ─────────────────────────────────────────────────────── */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(135deg, #EFF6FF 0%, #F8FAFC 50%, #F0FDF4 100%);
}

[data-theme="dark"] .login-wrap {
  background: linear-gradient(135deg, #0F172A 0%, #1E293B 100%);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  box-shadow: var(--shadow-md);
}

.login-logo {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: var(--accent-light);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
}

.login-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 4px;
  letter-spacing: -0.01em;
}

.login-sub {
  color: var(--text-muted);
  margin-bottom: 24px;
  font-size: 14px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

/* ── Alerts ────────────────────────────────────────────────────── */

.alert {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 14px;
}

.alert-err {
  background: var(--danger-light);
  color: #991b1b;
  border: 1px solid #fecaca;
}

/* ── Toasts ────────────────────────────────────────────────────── */

.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 9999;
  pointer-events: none;
}

.toast {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  max-width: 340px;
  box-shadow: var(--shadow-md);
  pointer-events: all;
  animation: toast-in 200ms ease-out;
}

.toast-ok { background: #064e3b; color: #ecfdf5; }
.toast-err { background: #7f1d1d; color: #fef2f2; }

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

/* ── Settlement summary ────────────────────────────────────────── */

.summary-grid {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 14px;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.summary-row:last-child { border-bottom: none; }
.summary-row.sub { background: var(--surface-2); font-weight: 600; }
.summary-row.deduct { color: var(--text-muted); }
.summary-row.final { font-weight: 700; font-size: 15px; padding: 12px 14px; border-radius: var(--radius); }
.summary-row.final.positive { background: #C6EFCE; color: #375623; }
.summary-row.final.negative { background: #FFC7CE; color: #9C0006; }

/* ── Info grid (settlement detail) ────────────────────────────── */

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

/* ── Export row ────────────────────────────────────────────────── */

.export-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── Back button ───────────────────────────────────────────────── */

.back-btn { margin-bottom: 16px; }

/* ── Empty / loading ───────────────────────────────────────────── */

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

.loading {
  text-align: center;
  color: var(--text-muted);
  padding: 48px;
}

/* ── Charts & Admin ────────────────────────────────────────────── */

.section-hint {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 20px;
  line-height: 1.6;
}

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 24px;
}

.chart-box {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.chart-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
}

.stats-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.stat-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}

.stat-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* ── Responsive ────────────────────────────────────────────────── */

@media (max-width: 640px) {
  .topbar { padding: 0 16px; }
  .topbar-brand { font-size: 14px; }
  .main-content { padding: 16px 12px 48px; }
  .card { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .nav-tabs { padding: 0 12px; }
  .nav-tab { padding: 10px 12px; font-size: 13px; }
  .info-grid { grid-template-columns: 1fr 1fr; }
  .charts-grid { grid-template-columns: 1fr; }
  .stats-row { grid-template-columns: 1fr 1fr; }
}

/* ─── Inline return-number edit ─────────────────────────────────────────────── */

.return-edit {
  display: flex;
  align-items: center;
}

.input-inline-number {
  width: 80px;
  padding: 3px 6px;
  font-size: 13px;
  font-family: var(--font-mono, monospace);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-align: right;
  transition: border-color 0.15s;
}

.input-inline-number:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}

/* ─── Clickable table rows ──────────────────────────────────────────────────── */

.tr-clickable {
  cursor: pointer;
}

.tr-clickable:hover td {
  background: var(--accent-light);
}

.td-action {
  color: var(--accent);
  font-size: 13px;
  white-space: nowrap;
  text-align: right;
}

/* ─── Bon preview chip ──────────────────────────────────────────────────────── */

.bon-preview {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--accent-light);
  border: 1px solid var(--border-focus);
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--accent);
  font-weight: 500;
}

/* ─── Tree / Folder view ───────────────────────────────────────────────────── */

.tree-root {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tree-event {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.tree-event-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--surface-2);
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  font-size: 15px;
}

.tree-event-header:hover {
  background: var(--border);
}

.tree-event-body {
  border-top: 1px solid var(--border);
}

.tree-staff {
  border-bottom: 1px solid var(--border);
}

.tree-staff:last-child {
  border-bottom: none;
}

.tree-staff-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 8px 28px;
  cursor: pointer;
  user-select: none;
  font-size: 14px;
  font-weight: 500;
}

.tree-staff-header:hover {
  background: var(--surface-2);
}

.tree-staff-body {
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tree-settlement-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 14px 7px 48px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.tree-settlement-row:last-child {
  border-bottom: none;
}

.tree-settlement-id {
  min-width: 40px;
}

.tree-settlement-date {
  color: var(--text-muted);
  flex: 1;
}

.tree-toggle {
  font-size: 10px;
  color: var(--text-muted);
  min-width: 12px;
}

.tree-icon {
  font-size: 15px;
}

.tree-label {
  flex: 1;
}

.tree-count {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--border);
  border-radius: 10px;
  padding: 1px 7px;
  font-weight: 500;
}

/* ─── Staff list (expand/collapse) ─────────────────────────────────────────── */

.staff-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.staff-row {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.staff-row-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  user-select: none;
  background: var(--surface-2);
  font-size: 14px;
}

.staff-row-header:hover {
  background: var(--border);
}

.staff-name {
  flex: 1;
}

.staff-events-summary {
  color: var(--text-muted);
  font-size: 13px;
}

.staff-row-detail {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.staff-event-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.staff-event-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--accent-light);
  color: var(--accent);
  border-radius: 12px;
  padding: 2px 10px 2px 10px;
  font-size: 13px;
  font-weight: 500;
}

.tag-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--accent);
  font-size: 14px;
  padding: 0 0 0 4px;
  line-height: 1;
  opacity: 0.7;
}

.tag-remove:hover {
  opacity: 1;
  color: var(--danger);
}

.staff-add-event {
  display: flex;
  gap: 8px;
  align-items: center;
}

.staff-add-event select {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
}

/* ─── Checkbox group ────────────────────────────────────────────────────────── */

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

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  background: var(--surface);
  transition: border-color 0.15s, background 0.15s;
}

.checkbox-label:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

.checkbox-label input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--accent);
}
