:root {
  --bg: #f6f7f8;
  --surface: #ffffff;
  --line: #d9dee3;
  --text: #1f2933;
  --muted: #65717f;
  --accent: #0f766e;
  --accent-weak: #d9f2ed;
  --warn: #b45309;
  --danger: #b42318;
  --shadow: 0 12px 30px rgba(31, 41, 51, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
  display: grid;
  grid-template-columns: 260px 1fr;
}

body.backoffice-page {
  display: block;
  background: #f5f7f8;
}

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

button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  min-height: 36px;
  padding: 0 14px;
  cursor: pointer;
}

button:hover { border-color: var(--accent); }
button:disabled { color: var(--muted); cursor: not-allowed; }

.danger-button {
  color: #fff;
  border-color: var(--danger);
  background: var(--danger);
}

.secondary-button,
.inspect-member-button {
  min-height: 32px;
  padding: 0 12px;
  color: var(--accent);
  border-color: var(--accent);
  background: #f7fbfa;
  white-space: nowrap;
}

input, select, textarea {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 6px;
  min-height: 36px;
  padding: 8px 10px;
  color: var(--text);
}

textarea { resize: vertical; }

.sidebar {
  background: #102326;
  color: #f4fbf9;
  min-height: 100vh;
  padding: 22px 16px;
  position: sticky;
  top: 0;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 26px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: var(--accent);
  display: grid;
  place-items: center;
  font-weight: 700;
}

.brand strong, .brand small {
  display: block;
  line-height: 1.35;
}

.brand small { color: #b8c7c4; }

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

.nav-item {
  width: 100%;
  color: #eaf4f2;
  background: transparent;
  border-color: transparent;
  text-align: left;
}

.nav-item.active,
.nav-item:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.16);
}

.role-picker {
  display: grid;
  gap: 8px;
  margin-top: 28px;
  color: #c8d8d5;
  font-size: 13px;
}

.role-picker select {
  width: 100%;
}

.main {
  min-width: 0;
  padding: 24px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: start;
  margin-bottom: 22px;
}

h1, h2, p { margin: 0; }
h1 { font-size: 25px; }
h2 { font-size: 16px; }
.topbar p { color: var(--muted); margin-top: 5px; }

.icon-button {
  width: 38px;
  min-width: 38px;
  padding: 0;
  font-size: 20px;
}

.view { display: none; }
.view.active { display: block; }
.hidden { display: none !important; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}

.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  font-size: 28px;
  margin-top: 6px;
}

.metric.alert strong { color: var(--warn); }

.two-col {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.panel.wide { max-width: 1040px; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-head p {
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
}

.stack {
  display: grid;
  gap: 10px;
}

.stat-row,
.post-row {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.stat-row span,
.post-row small {
  color: var(--muted);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.toolbar input[type="search"] {
  width: min(420px, 100%);
}

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

.mail-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.mail-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.mail-form label input,
.mail-form label select,
.mail-form label textarea {
  width: 100%;
  color: var(--text);
}

.mail-grid {
  display: grid;
  grid-template-columns: 160px minmax(180px, 1fr) minmax(220px, 1fr);
  gap: 12px;
}

.mail-actions,
.mail-confirm {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}

.mail-confirm label {
  width: min(280px, 100%);
}

.mail-status {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  color: var(--muted);
  background: #f8faf9;
}

.mail-status.error {
  border-color: #f1b4ae;
  color: var(--danger);
  background: #fff7f6;
}

.mail-preview-grid {
  margin-top: 16px;
}

.mail-sample {
  display: grid;
  gap: 10px;
}

.mail-sample pre {
  margin: 0;
  white-space: pre-wrap;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  background: #f8faf9;
  font-family: inherit;
  line-height: 1.6;
}

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
  box-shadow: var(--shadow);
}

.member-tree-panel {
  background: #eef1ec;
  border: 1px solid #b8c0b7;
  border-radius: 8px;
  margin-bottom: 14px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.member-tree-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border-bottom: 1px solid #c9d1c7;
}

.member-tree-controls p {
  color: var(--muted);
  margin-top: 4px;
  font-size: 13px;
}

.member-tree-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.member-tree-actions input {
  width: 190px;
}

.member-tree-wrap {
  height: 360px;
  overflow: auto;
  background: #fff;
}

.member-tree {
  min-width: 1180px;
  padding: 8px 10px 18px;
  font-family: "MS Gothic", "Osaka-Mono", "Yu Gothic UI", monospace;
  font-size: 14px;
  line-height: 1.38;
}

.tree-row {
  white-space: nowrap;
  min-height: 22px;
  display: flex;
  align-items: baseline;
}

.tree-indent {
  display: inline-block;
  width: 24px;
  height: 18px;
  flex: 0 0 24px;
  border-left: 1px dotted #a7a7a7;
}

.tree-toggle {
  width: 16px;
  min-width: 16px;
  height: 16px;
  min-height: 16px;
  padding: 0;
  margin-right: 4px;
  line-height: 12px;
  border-radius: 2px;
  font-family: inherit;
  font-size: 12px;
}

.tree-toggle.leaf {
  border-color: transparent;
  background: transparent;
  cursor: default;
}

.tree-node-text {
  padding: 1px 4px;
  border-radius: 2px;
  cursor: pointer;
}

.tree-node-text:hover,
.tree-node-text.selected {
  outline: 2px solid #2d78bd;
  background: #eaf4ff;
}

.tree-node-text.inactive {
  color: #747b80;
}

.tree-node-text.paused {
  background: #fff4d6;
}

.tree-status {
  display: inline-block;
  min-width: 32px;
  margin-right: 5px;
  padding: 1px 4px;
  border: 1px solid #aab3b8;
  border-radius: 3px;
  background: #f4f7f8;
  text-align: center;
  font: 12px "Yu Gothic UI", Meiryo, sans-serif;
}

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

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

th {
  color: var(--muted);
  background: #fafbfc;
  font-weight: 600;
}

tr:hover td { background: #f7fbfa; }
.clickable { cursor: pointer; }

.badge {
  display: inline-flex;
  min-width: 44px;
  justify-content: center;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  background: #eef2f4;
  color: var(--text);
}

.badge.active { background: var(--accent-weak); color: #075e58; }
.badge.warn { background: #fff3d6; color: var(--warn); }
.badge.stop { background: #fde8e7; color: var(--danger); }

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(620px, 100vw);
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: -20px 0 40px rgba(31, 41, 51, 0.16);
  transform: translateX(105%);
  transition: transform 160ms ease;
  z-index: 10;
  display: grid;
  grid-template-rows: auto 1fr;
}

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

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

.drawer-head p {
  color: var(--muted);
  margin-top: 4px;
}

.drawer-body {
  padding: 18px;
  overflow: auto;
}

.detail-grid {
  display: grid;
  grid-template-columns: 150px 1fr;
  gap: 8px 14px;
  margin-bottom: 18px;
}

.detail-grid span {
  color: var(--muted);
}

.section-title {
  margin: 20px 0 10px;
  font-size: 15px;
}

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

.requirements p {
  margin-bottom: 14px;
  color: var(--muted);
}

.rule-grid {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 10px 14px;
  align-items: start;
}

.rule-grid span {
  color: var(--muted);
}

.role-grid-table {
  display: grid;
  gap: 10px;
}

.commission-source-panel {
  margin-top: 16px;
}

.commission-source {
  display: grid;
  gap: 12px;
}

.source-verdict {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #f8faf9;
}

.source-verdict strong {
  display: block;
  margin-bottom: 6px;
}

.source-kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 10px;
}

.source-kpi {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #fff;
}

.source-kpi span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.source-kpi strong {
  display: block;
  margin-top: 4px;
  font-size: 20px;
}

.source-warnings {
  margin: 8px 0 0;
  padding-left: 20px;
  color: #7c3f00;
}

.report-block {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #101820;
  color: #e8f2f0;
  overflow: auto;
  white-space: pre;
  font: 13px/1.7 "MS Gothic", "Osaka-Mono", monospace;
}

.backoffice-shell {
  min-height: 100vh;
  padding: 32px 18px;
  display: grid;
  place-items: center;
}

.backoffice-login {
  width: min(620px, 100%);
  display: grid;
  gap: 24px;
}

.backoffice-logo {
  width: min(560px, 88vw);
  height: auto;
  justify-self: center;
  object-fit: contain;
}

.backoffice-logo.small {
  width: min(320px, 52vw);
  justify-self: start;
}

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

.backoffice-login-form label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.backoffice-login-form input {
  width: 100%;
  font-size: 16px;
}

.backoffice-login-form button {
  background: #0aa4c8;
  color: #fff;
  border-color: #0aa4c8;
}

.backoffice-login-form .link-button {
  min-height: auto;
  padding: 0;
  border: 0;
  background: transparent;
  color: #0a6f88;
  justify-self: center;
  text-decoration: underline;
}

.login-message {
  min-height: 20px;
  color: var(--danger);
  font-size: 13px;
}

.password-result {
  display: grid;
  gap: 12px;
}

.password-card {
  border: 1px solid #b8e1dc;
  border-radius: 8px;
  padding: 12px;
  background: #f0fbf9;
  display: grid;
  gap: 6px;
}

.password-card span {
  color: var(--muted);
  font-size: 13px;
}

.password-card strong {
  font-size: 22px;
  letter-spacing: 0;
}

.customer-link {
  color: #0a6f88;
  font-weight: 700;
}

.backoffice-dashboard {
  width: min(1040px, 100%);
  display: grid;
  gap: 16px;
  align-self: start;
}

.backoffice-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
}

.statement-status {
  color: var(--accent);
}

.statement-kpis {
  margin-bottom: 0;
}

.statement-lines {
  display: grid;
  gap: 8px;
}

.btc-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr));
  gap: 10px;
}

.btc-grid > div {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #f8faf9;
}

.btc-grid span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.btc-grid strong {
  display: block;
  margin-top: 5px;
  font-size: 18px;
  letter-spacing: 0;
}

.payment-form {
  display: grid;
  grid-template-columns: 170px 170px minmax(260px, 1fr);
  gap: 12px;
  align-items: end;
}

.payment-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.payment-form select,
.payment-form input {
  width: 100%;
  min-height: 40px;
}

.payment-actions {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.payment-notice {
  margin-top: 12px;
  display: grid;
  gap: 6px;
  border: 1px solid #d9e4e5;
  border-radius: 8px;
  padding: 12px;
  background: #f7fbfa;
  color: #33484b;
  font-size: 13px;
}

.payment-notice p {
  margin: 0;
}

.wallet-risk {
  color: var(--danger);
  font-size: 12px;
  line-height: 1.5;
}

.payment-journal {
  margin-top: 14px;
  display: grid;
  gap: 8px;
}

.payment-journal h3 {
  margin: 0;
  font-size: 15px;
}

.payment-journal p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.master-commission-table {
  max-height: 520px;
}

.master-commission-table table,
.commission-history-table table {
  min-width: 980px;
}

.commission-history-table {
  max-height: 420px;
}

.master-commission-table th,
.commission-history-table th {
  position: sticky;
  top: 0;
  z-index: 1;
}

.statement-line {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) 140px 90px;
  gap: 10px;
  align-items: center;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  background: #fff;
  color: inherit;
  font: inherit;
  cursor: pointer;
  text-align: left;
}

.statement-line:hover,
.statement-line.is-open {
  border-color: var(--accent);
  background: #f7fbfa;
}

.statement-line span {
  text-align: right;
  font-weight: 700;
}

.statement-line small {
  color: var(--muted);
  text-align: right;
}

.statement-detail {
  border: 1px solid var(--line);
  border-top: 0;
  border-radius: 0 0 6px 6px;
  margin-top: -8px;
  padding: 10px 12px 12px;
  background: #fbfcfc;
  overflow-x: auto;
}

.statement-detail-table table {
  min-width: 760px;
}

.statement-detail-table td:first-child strong,
.statement-detail-table td:first-child small {
  display: block;
}

.statement-detail-table td:first-child small,
.statement-basis,
.statement-detail-empty {
  color: var(--muted);
  font-size: 12px;
}

.statement-basis {
  margin: 8px 0 0;
}

.statement-detail-empty {
  margin: 0;
}

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

.role-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.role-card h3 {
  margin: 0 0 8px;
  font-size: 15px;
}

.role-card dl {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 6px 12px;
  margin: 0;
  font-size: 13px;
}

.role-card dt { color: var(--muted); }
.role-card dd { margin: 0; }

.treemap-panel {
  margin-top: 16px;
}

.treemap-wrap {
  width: 100%;
  height: 500px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #f8faf9;
}

#referralTreemap {
  width: 100%;
  height: 100%;
  display: block;
}

.treemap-cell {
  cursor: pointer;
}

.treemap-cell rect {
  stroke: rgba(255, 255, 255, 0.88);
  stroke-width: 2;
  transition: filter 120ms ease, stroke 120ms ease;
}

.treemap-cell:hover rect {
  filter: brightness(0.94);
  stroke: #102326;
}

.treemap-label {
  fill: #0f1f22;
  font-size: 13px;
  font-weight: 700;
  pointer-events: none;
}

.treemap-sub {
  fill: rgba(15, 31, 34, 0.78);
  font-size: 12px;
  pointer-events: none;
}

.treemap-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}

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

.treemap-legend i {
  width: 14px;
  height: 10px;
  border-radius: 2px;
  display: inline-block;
}

.legend-high { background: #66c2a5; }
.legend-mid { background: #fee08b; }
.legend-low { background: #f46d43; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: #142427;
  color: white;
  border-radius: 8px;
  padding: 10px 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

.toast.show { opacity: 1; }

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

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

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

  .kpi-grid,
  .two-col {
    grid-template-columns: 1fr;
  }

  .mail-grid {
    grid-template-columns: 1fr;
  }

  .treemap-wrap {
    height: 420px;
  }

  .member-tree-controls {
    align-items: stretch;
    flex-direction: column;
  }

  .member-tree-actions {
    justify-content: flex-start;
  }

  .payment-form {
    grid-template-columns: 1fr;
  }
}
