:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --text: #172033;
  --muted: #667085;
  --line: #dfe5ef;
  --primary: #1769e0;
  --primary-strong: #0b4fb3;
  --success: #0f9f6e;
  --danger: #d9304f;
  --warning: #bf7a00;
  --info: #1c7c8c;
  --soft-blue: #eaf2ff;
  --soft-green: #e9f8f1;
  --soft-red: #fff0f3;
  --soft-amber: #fff6df;
  --shadow: 0 12px 32px rgba(20, 32, 54, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
}

body.logged-out .app-shell {
  display: block;
}

body.logged-out .sidebar,
body.logged-out .topbar,
body.logged-out .admin-content {
  display: none !important;
}

body.logged-out .main {
  min-height: 100vh;
  display: grid;
  align-content: center;
  place-items: center;
  gap: 12px;
  padding: 24px;
}

body.logged-out .login-panel {
  width: min(460px, 100%);
  min-height: 0;
  margin: 0;
  padding: 0;
}

body.logged-out .message-band {
  width: min(420px, 100%);
  margin: 0;
}

.sidebar {
  background: #101828;
  color: #fff;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 8px 22px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  background: #22a06b;
  border-radius: 8px;
  font-weight: 800;
}

.brand-title {
  font-weight: 800;
  font-size: 15px;
}

.brand-subtitle {
  color: #b7c0d1;
  margin-top: 3px;
  font-size: 12px;
}

.nav-list {
  display: grid;
  gap: 4px;
  padding-top: 18px;
}

.nav-item {
  width: 100%;
  border: 0;
  color: #d7deea;
  background: transparent;
  text-align: left;
  padding: 11px 12px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.nav-item.active,
.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.nav-icon {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.78;
}

.main {
  padding: 22px 28px 42px;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.25;
}

.topbar p {
  color: var(--muted);
  margin-top: 6px;
}

.topbar-actions,
.filter-row,
.panel-header,
.config-band {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-pill {
  min-width: 88px;
  text-align: center;
  color: var(--muted);
  background: #eef2f8;
  border: 1px solid var(--line);
  padding: 8px 10px;
  border-radius: 999px;
}

.status-pill.ok {
  color: var(--success);
  background: var(--soft-green);
  border-color: #bce8d5;
}

.status-pill.error {
  color: var(--danger);
  background: var(--soft-red);
  border-color: #ffd1da;
}

.config-band {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
}

.login-panel {
  min-height: calc(100vh - 150px);
  display: grid;
  place-items: start center;
  padding-top: 72px;
}

.login-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
}

.login-card h2 {
  font-size: 22px;
}

.login-card p {
  color: var(--muted);
  line-height: 1.6;
}

.login-button {
  width: 100%;
  padding: 11px 14px;
}

.field {
  display: grid;
  gap: 6px;
}

.field label,
.form-stack label {
  color: var(--muted);
  font-size: 12px;
}

.field input,
.filter-row input,
.filter-row select,
.panel-header input,
.form-stack input,
.form-stack select,
.form-stack textarea,
.token-field input {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 6px;
  padding: 9px 10px;
  outline: none;
}

.field input {
  min-width: 250px;
}

.token-field input {
  min-width: 280px;
}

.endpoint-field {
  flex: 1 1 420px;
}

.endpoint-field input {
  width: 100%;
  min-width: 320px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(23, 105, 224, 0.12);
}

.remember-row {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
}

.primary-button,
.icon-button,
.plain-button,
.danger-button,
.success-button {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 9px 12px;
}

.primary-button,
.icon-button {
  color: #fff;
  background: var(--primary);
}

.primary-button:hover,
.icon-button:hover {
  background: var(--primary-strong);
}

.plain-button {
  background: #fff;
  color: var(--text);
  border-color: var(--line);
}

.success-button {
  background: var(--soft-green);
  color: var(--success);
  border-color: #bce8d5;
}

.danger-button {
  background: var(--soft-red);
  color: var(--danger);
  border-color: #ffd1da;
}

.message-band {
  border: 1px solid #cfe0ff;
  background: var(--soft-blue);
  color: #1f4d8f;
  padding: 12px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
}

.message-band.error {
  border-color: #ffd1da;
  background: var(--soft-red);
  color: var(--danger);
}

.hidden {
  display: none !important;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.metric {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  box-shadow: var(--shadow);
  min-height: 98px;
}

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

.metric-value {
  font-size: 24px;
  font-weight: 800;
  margin-top: 10px;
  line-height: 1.15;
}

.metric-note {
  color: var(--muted);
  margin-top: 8px;
  font-size: 12px;
}

.section-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(320px, 0.8fr);
  gap: 14px;
}

.panel {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
  min-width: 0;
}

.panel-header {
  justify-content: space-between;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.panel-header h2 {
  font-size: 17px;
}

.period-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.period-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #fbfcff;
}

.period-title {
  font-weight: 800;
  margin-bottom: 10px;
}

.period-row,
.todo-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 0;
  border-bottom: 1px solid #edf1f7;
}

.period-row:last-child,
.todo-row:last-child {
  border-bottom: 0;
}

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

.todo-row {
  background: #fbfcff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: #fff;
}

.segmented button {
  border: 0;
  background: #fff;
  color: var(--muted);
  padding: 8px 12px;
}

.segmented button.active {
  background: var(--primary);
  color: #fff;
}

canvas {
  width: 100%;
  max-height: 380px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.chart-legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-top: 12px;
  color: var(--muted);
}

.chart-legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.chart-legend i {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.legend-income {
  background: #1769e0;
}

.legend-recharge {
  background: #0f9f6e;
}

.legend-refund {
  background: #d9304f;
}

.legend-withdrawal {
  background: #bf7a00;
}

.legend-net {
  background: #1c7c8c;
}

.table-wrap {
  overflow: auto;
  border: 2px solid transparent;
  border-radius: 0;
  background: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 900px;
  background: #fff;
}

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid #4f5663;
  text-align: center;
  vertical-align: middle;
  white-space: nowrap;
}

th {
  background: #5b7fda;
  color: #fff;
  font-weight: 700;
  position: sticky;
  top: 0;
  z-index: 1;
  border-bottom: 2px solid #fff;
  border-right: 4px solid #fff;
}

th:first-child {
  background: #050505;
}

.sort-button {
  width: 100%;
  border: 0;
  background: transparent;
  color: inherit;
  font-weight: inherit;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  text-align: center;
}

.filter-th {
  position: sticky;
}

.header-filter-button {
  width: 100%;
  border: 0;
  background: transparent;
  color: inherit;
  font-weight: inherit;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.filter-caret {
  width: 16px;
  height: 16px;
  background: #fff;
  border: 1px solid #34a66f;
  position: relative;
}

.filter-caret::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 5px;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid #34a66f;
}

.date-filter-popover {
  position: absolute;
  z-index: 12;
  width: min(460px, calc(100vw - 48px));
  margin-top: 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 48px rgba(16, 24, 40, 0.18);
  padding: 14px;
}

.date-filter-tabs {
  display: flex;
  align-items: center;
  gap: 18px;
  margin-bottom: 12px;
}

.date-filter-tabs button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0 0 6px;
}

.date-filter-tabs button.active {
  color: #079455;
  border-bottom: 2px solid #079455;
}

.date-filter-search input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  margin-bottom: 10px;
}

.date-filter-list {
  min-height: 220px;
  max-height: 320px;
  overflow: auto;
  padding: 2px 0 8px;
}

.overview-export {
  position: relative;
  display: inline-flex;
}

.export-toggle::after {
  content: "";
  display: inline-block;
  margin-left: 8px;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid currentColor;
  vertical-align: middle;
}

.export-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 120px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 12px 28px rgba(16, 24, 40, 0.16);
  padding: 6px;
  z-index: 2;
}

.export-menu button {
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 8px 10px;
  border-radius: 4px;
}

.export-menu button:hover {
  background: #f1f5ff;
}

.date-filter-check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  color: var(--text);
}

.date-filter-check input {
  accent-color: #34a66f;
}

.date-filter-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  border-top: 1px solid #edf1f7;
  padding-top: 12px;
}

tr:hover td {
  background: #f7f9ff;
}

.amount-income {
  color: var(--success);
  font-weight: 700;
}

.amount-expense {
  color: var(--danger);
  font-weight: 700;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  border-radius: 999px;
  background: #eef2f8;
  color: #475467;
  font-size: 12px;
}

.badge.pending {
  color: var(--warning);
  background: var(--soft-amber);
}

.badge.approved,
.badge.finished,
.badge.review,
.badge.ordered {
  color: var(--success);
  background: var(--soft-green);
}

.badge.rejected,
.badge.refunded {
  color: var(--danger);
  background: var(--soft-red);
}

.actions {
  display: flex;
  gap: 8px;
}

.inline-edit {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.rate-input {
  width: 88px;
}

.compact-button {
  padding: 7px 10px;
}

.ledgers-layout {
  grid-template-columns: minmax(0, 1.4fr) 340px;
}

.side-panel {
  align-self: start;
}

.form-stack {
  display: grid;
  gap: 12px;
}

.form-stack label {
  display: grid;
  gap: 7px;
}

dialog {
  width: min(880px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  box-shadow: 0 18px 60px rgba(16, 24, 40, 0.24);
}

dialog::backdrop {
  background: rgba(16, 24, 40, 0.35);
}

.dialog-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

#detailContent {
  margin: 0;
  padding: 16px;
  max-height: 70vh;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  background: #fbfcff;
}
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; white-space: normal; }
.detail-field { border: 1px solid var(--line); border-radius: 8px; padding: 12px; display: grid; gap: 5px; background: #fff; }
.detail-field span { color: var(--muted); font-size: 13px; }
.detail-field strong { word-break: break-word; }
.result-footer { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 14px; color: var(--muted); }
.pagination { display: flex; align-items: center; gap: 8px; }

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

@media (max-width: 1180px) {
  .metrics-grid {
    grid-template-columns: repeat(3, minmax(140px, 1fr));
  }

  .section-grid,
  .ledgers-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 760px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
  }

  .nav-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .main {
    padding: 18px 14px 32px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .metrics-grid,
  .period-grid {
    grid-template-columns: 1fr;
  }

  .field,
  .field input,
  .token-field input,
  .filter-row,
  .panel-header input {
    width: 100%;
  }
}
