:root {
  --bg: #f6f7f4;
  --panel: #ffffff;
  --text: #1e2528;
  --muted: #647174;
  --line: #dce2dc;
  --accent: #0f766e;
  --accent-2: #2563eb;
  --danger: #b42318;
  --warning: #b45309;
  --input-bg: #ffffff;
  --secondary-bg: #e6f3f1;
  --secondary-text: #0b5d57;
  --nav-active-bg: #e6f3f1;
  --nav-active-line: #c8e3df;
  --notice-bg: #eef6ff;
  --notice-text: #1d4f86;
  --error-bg: #fef3f2;
  --code-bg: rgba(0, 0, 0, 0.06);
  --hover: #f1f7f6;
  --chip-bg: #e6f3f1;
  --chip-line: #c8e3df;
  --chip-text: #0b5d57;
  --total-bg: #f8fbfa;
  --modal-backdrop: rgba(15, 23, 42, 0.28);
  --drawer-backdrop: rgba(15, 23, 42, 0.22);
  --shadow: 0 12px 35px rgba(31, 41, 55, 0.08);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101615;
  --panel: #18211f;
  --text: #edf4f1;
  --muted: #a8bbb4;
  --line: #2c3d39;
  --accent: #35b8a6;
  --accent-2: #74a7ff;
  --danger: #ff8a80;
  --warning: #f4bd62;
  --input-bg: #111a18;
  --secondary-bg: #183d38;
  --secondary-text: #99f0df;
  --nav-active-bg: #183d38;
  --nav-active-line: #2f6d64;
  --notice-bg: #142b3f;
  --notice-text: #b8dcff;
  --error-bg: #3a1f22;
  --code-bg: rgba(255, 255, 255, 0.1);
  --hover: #1f302c;
  --chip-bg: #183d38;
  --chip-line: #2f6d64;
  --chip-text: #b7fff0;
  --total-bg: #152925;
  --modal-backdrop: rgba(0, 0, 0, 0.54);
  --drawer-backdrop: rgba(0, 0, 0, 0.44);
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

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

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
  min-height: 40px;
  padding: 0 16px;
}

button:disabled {
  cursor: wait;
  opacity: 0.65;
}

button.secondary {
  background: var(--secondary-bg);
  color: var(--secondary-text);
}

button.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
}

.nav-link {
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  display: inline-flex;
  font-size: 14px;
  font-weight: 800;
  min-height: 40px;
  padding: 0 12px;
  text-decoration: none;
}

.nav-link.active {
  background: var(--nav-active-bg);
  border-color: var(--nav-active-line);
  color: var(--secondary-text);
}

.eyebrow {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  margin: 0 0 4px;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-shell {
  width: min(430px, 100%);
}

.login-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 22px;
  padding: 28px;
}

.login-form,
.filters {
  display: grid;
  gap: 14px;
}

label {
  color: var(--muted);
  display: grid;
  gap: 7px;
  font-size: 13px;
  font-weight: 700;
}

input,
select {
  background: var(--input-bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  min-height: 40px;
  padding: 8px 10px;
  width: 100%;
}

.notice,
.error,
.sync-status {
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.45;
  padding: 12px;
}

.notice,
.sync-status {
  background: var(--notice-bg);
  color: var(--notice-text);
}

.error {
  background: var(--error-bg);
  color: var(--danger);
}

code {
  background: var(--code-bg);
  border-radius: 4px;
  padding: 2px 4px;
}

.muted {
  color: var(--muted);
}

.topbar {
  align-items: center;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  min-height: 78px;
  padding: 16px 24px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar h1 {
  font-size: 22px;
}

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

.layout {
  display: grid;
  gap: 20px;
  grid-template-columns: 290px 1fr;
  padding: 20px;
}

.filters {
  align-self: start;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  max-height: calc(100vh - 118px);
  overflow-y: auto;
  padding: 18px;
  position: sticky;
  top: 98px;
}

.filters h2,
.applied-filters h2,
.chart-panel h2,
.table-section h2 {
  font-size: 16px;
}

.amount-grid {
  display: grid;
  gap: 10px;
  grid-template-columns: 1fr 1fr;
}

.filter-actions {
  display: grid;
  gap: 10px;
  grid-template-columns: 1fr 1fr;
}

.content {
  display: grid;
  gap: 18px;
  min-width: 0;
}

.applied-filters {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 12px;
  padding: 14px 16px;
}

.applied-filters-header {
  align-items: center;
  display: flex;
  justify-content: space-between;
}

.applied-filters-header span,
.applied-filter-empty {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.filter-chip-list {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.filter-chip {
  align-items: center;
  background: var(--chip-bg);
  border: 1px solid var(--chip-line);
  color: var(--chip-text);
  display: inline-flex;
  gap: 8px;
  max-width: 100%;
  min-height: 34px;
  padding: 0 10px;
}

.filter-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.filter-chip strong {
  color: var(--chip-text);
  font-size: 16px;
  line-height: 1;
}

.kpis {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
}

.expenses-kpis {
  grid-template-columns: repeat(6, minmax(130px, 1fr));
}

.kpi,
.chart-panel,
.table-section {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.kpi {
  display: grid;
  gap: 8px;
  min-height: 96px;
  padding: 16px;
}

.kpi span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.kpi strong {
  font-size: 24px;
  line-height: 1.1;
}

.chart-grid {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(2, minmax(280px, 1fr));
}

.chart-panel {
  display: grid;
  gap: 14px;
  min-height: 310px;
  padding: 16px;
}

.chart-panel.wide {
  grid-column: 1 / -1;
  min-height: 260px;
}

.chart-panel canvas {
  max-height: 250px;
  width: 100%;
}

.section-heading {
  align-items: center;
  display: flex;
  justify-content: space-between;
  padding: 16px;
}

.section-heading span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

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

table {
  border-collapse: collapse;
  min-width: 1220px;
  width: 100%;
}

.pagination {
  align-items: center;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding: 14px 16px;
}

.pagination span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

th,
td {
  border-top: 1px solid var(--line);
  font-size: 13px;
  padding: 11px 12px;
  text-align: left;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

th.sortable {
  cursor: pointer;
  user-select: none;
  position: relative;
  padding-right: 22px;
}

th.sortable:hover {
  color: var(--text);
}

th.sortable::after {
  content: "↕";
  position: absolute;
  right: 6px;
  opacity: 0.35;
  font-size: 11px;
}

th.sortable.asc::after {
  content: "▲";
  opacity: 1;
  color: var(--accent);
}

th.sortable.desc::after {
  content: "▼";
  opacity: 1;
  color: var(--accent);
}

tfoot td {
  background: var(--total-bg);
  border-top: 2px solid var(--chip-line);
  font-weight: 800;
}

.customer-totals-row td {
  white-space: nowrap;
}

tr[data-customer-id] {
  cursor: pointer;
}

tr[data-customer-id]:hover {
  background: var(--hover);
}

.pill {
  border-radius: 999px;
  display: inline-flex;
  font-size: 12px;
  font-weight: 800;
  padding: 4px 8px;
}

.pill.good {
  background: var(--secondary-bg);
  color: var(--secondary-text);
}

.pill.bad {
  background: var(--error-bg);
  color: var(--danger);
}

.drawer {
  background: var(--panel);
  border-left: 1px solid var(--line);
  bottom: 0;
  box-shadow: -18px 0 40px rgba(31, 41, 55, 0.12);
  display: grid;
  grid-template-rows: auto 1fr;
  max-width: 560px;
  position: fixed;
  right: 0;
  top: 0;
  transform: translateX(105%);
  transition: transform 160ms ease;
  width: min(560px, 94vw);
  z-index: 30;
}

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

.drawer-header {
  align-items: start;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  padding: 20px;
}

#detailBody {
  display: grid;
  gap: 18px;
  overflow-y: auto;
  padding: 20px;
}

.detail-list {
  display: grid;
  gap: 8px;
}

.detail-row {
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 8px;
  grid-template-columns: 160px 1fr;
  padding: 9px 0;
}

.detail-row span:first-child {
  color: var(--muted);
  font-weight: 700;
}

.backdrop {
  background: var(--drawer-backdrop);
  bottom: 0;
  left: 0;
  position: fixed;
  right: 0;
  top: 0;
  z-index: 20;
}

.modal-backdrop {
  background: var(--modal-backdrop);
  bottom: 0;
  left: 0;
  position: fixed;
  right: 0;
  top: 0;
  z-index: 40;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 0;
  left: 50%;
  max-height: min(780px, calc(100vh - 40px));
  max-width: 520px;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
  position: fixed;
  top: 50%;
  transform: translate(-50%, -48%);
  transition: opacity 140ms ease, transform 140ms ease;
  width: min(520px, calc(100vw - 28px));
  z-index: 50;
}

.modal.open {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, -50%);
}

.modal-header {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  padding: 16px;
}

.modal-header h2 {
  font-size: 16px;
}

.modal-form {
  display: grid;
  gap: 14px;
  overflow-y: auto;
  padding: 16px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .filters {
    max-height: none;
    overflow: visible;
    position: static;
  }

  .kpis {
    grid-template-columns: repeat(2, minmax(160px, 1fr));
  }
}

@media (max-width: 760px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 14px;
  }

  .topbar-actions,
  .topbar-actions form {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  .layout {
    padding: 12px;
  }

  .kpis,
  .chart-grid,
  .amount-grid {
    grid-template-columns: 1fr;
  }

  .chart-panel.wide {
    grid-column: auto;
  }

  .detail-row {
    grid-template-columns: 1fr;
  }
}
