:root {
  --bg: #eef2f0;
  --surface: #ffffff;
  --surface-2: #f7faf8;
  --line: #e1e8e3;
  --line-strong: #cdd8d1;
  --text: #14201a;
  --muted: #6b7a70;
  --green: #1f9d57;
  --green-dark: #157a42;
  --green-soft: #e7f5ec;
  --yellow: #e2a00f;
  --yellow-soft: #fbf2da;
  --red: #d24439;
  --red-soft: #fbe7e5;
  --shadow: 0 1px 2px rgba(20, 32, 26, 0.04), 0 4px 16px rgba(20, 32, 26, 0.06);
  --radius: 12px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-columns: 264px 1fr;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: linear-gradient(180deg, #182a20 0%, #122019 100%);
  color: #e8efe9;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.2px;
  margin-bottom: 22px;
  padding: 0 6px;
  display: flex;
  align-items: center;
  gap: 9px;
}

.brand::before {
  content: "";
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 4px rgba(31, 157, 87, 0.25);
}

.nav-button,
.sync-button,
.debug-link {
  width: 100%;
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 0 14px;
  color: #cdd8cf;
  background: transparent;
  text-align: left;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s, color 0.15s;
}

.nav-button:hover {
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
}

.nav-button.active {
  background: rgba(31, 157, 87, 0.18);
  color: #fff;
  border-color: rgba(31, 157, 87, 0.5);
}

.sync-button {
  margin-top: 16px;
  text-align: center;
  background: var(--green);
  color: #fff;
  font-weight: 700;
}

.sync-button:hover {
  background: var(--green-dark);
}

.sync-button:disabled {
  opacity: 0.7;
  cursor: progress;
}

.tools {
  margin-top: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding-top: 10px;
}

.tools > summary {
  list-style: none;
  cursor: pointer;
  color: #8a998f;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  padding: 8px 6px;
  user-select: none;
}

.tools > summary::-webkit-details-marker {
  display: none;
}

.tools > summary::after {
  content: " ▾";
}

.tools[open] > summary::after {
  content: " ▴";
}

.debug-link {
  min-height: 34px;
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #9fb0a4;
  font-size: 13px;
  font-weight: 500;
}

.debug-link:hover {
  background: rgba(255, 255, 255, 0.05);
  color: #fff;
}

.debug-link.export {
  color: #b9e8cb;
}

/* ---------- Content ---------- */
.content {
  min-width: 0;
  padding: 26px 28px 40px;
}

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

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 26px;
  line-height: 1.2;
  letter-spacing: -0.3px;
}

#subtitle {
  margin-top: 6px;
  color: var(--muted);
  font-size: 14px;
}

select,
input {
  min-height: 40px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  padding: 0 12px;
  background: #fff;
  font: inherit;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

select:focus,
input:focus {
  outline: none;
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(31, 157, 87, 0.15);
}

.client-name-input {
  min-height: 32px;
  width: 100%;
  min-width: 140px;
  padding: 0 9px;
  border-style: dashed;
  border-color: var(--yellow);
  background: var(--yellow-soft);
}

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

/* ---------- Metrics ---------- */
.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

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

.metric span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.metric strong {
  font-size: 25px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Highlight the bonus metric (last top card) and accent cards */
.metrics .metric:last-child,
.metric.accent {
  background: linear-gradient(135deg, #1f9d57 0%, #157a42 100%);
  border-color: transparent;
  color: #fff;
}

.metrics .metric:last-child span,
.metric.accent span {
  color: rgba(255, 255, 255, 0.85);
}

/* ---------- Panels & tables ---------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.manager-summary {
  margin-bottom: 18px;
}

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

.panel-header h2 {
  font-size: 16px;
  font-weight: 700;
}

.panel-header input[type="search"] {
  min-width: 260px;
}

.table-wrap {
  overflow: auto;
  max-height: 70vh;
}

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

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

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  border-bottom: 1px solid var(--line-strong);
}

tbody tr {
  transition: background 0.1s;
}

tbody tr:nth-child(even) {
  background: var(--surface-2);
}

tbody tr:hover {
  background: var(--green-soft);
}

td.num,
th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

td.strong {
  font-weight: 700;
}

.admin-only {
  display: none;
}

.admin-view .admin-only {
  display: table-cell;
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  min-height: 23px;
  border-radius: 999px;
  padding: 0 11px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
}

.badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 7px;
  background: currentColor;
}

.badge {
  background: var(--surface-2);
  color: var(--muted);
}

.badge.green {
  background: var(--green-soft);
  color: var(--green-dark);
}

.pay-cell {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.mini-btn.active {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}

.badge.yellow {
  background: var(--yellow-soft);
  color: #9a6d05;
}

.badge.red {
  background: var(--red-soft);
  color: var(--red);
}

/* Client-type select inside the table */
td select {
  min-height: 34px;
  border-radius: 8px;
}

/* ---------- Login ---------- */
.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #16241c, #0f1a14);
}

.login-overlay[hidden] {
  display: none;
}

.login-card {
  width: 320px;
  max-width: 90vw;
  background: #fff;
  border-radius: 14px;
  padding: 28px 26px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-brand {
  font-size: 22px;
  font-weight: 700;
}

.login-sub {
  color: var(--muted);
  margin-bottom: 4px;
}

.login-card input {
  width: 100%;
}

.login-card button {
  min-height: 44px;
  border: none;
  border-radius: 9px;
  background: var(--green);
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
}

.login-card button:hover {
  background: var(--green-dark);
}

.login-error {
  color: var(--red);
  font-size: 13px;
  min-height: 16px;
}

/* ---------- Account block + role gating ---------- */
.account {
  margin-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  color: #cdd8cf;
}

.account button {
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: transparent;
  color: #cdd8cf;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
}

.account button:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

/* Hide admin-only controls for manager accounts */
body.role-manager .admin-ui {
  display: none !important;
}

/* ---------- Modal (month comparison) ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 30px 16px;
  background: rgba(15, 26, 20, 0.55);
  overflow: auto;
}

.modal[hidden] {
  display: none;
}

.modal-card {
  width: min(1100px, 96vw);
  background: var(--surface);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

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

.modal-head h2 {
  font-size: 17px;
}

.modal-close {
  border: none;
  background: transparent;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
}

.modal-controls {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 16px 18px;
  flex-wrap: wrap;
}

.modal-controls label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.cmp-summary {
  padding: 0 18px 14px;
}

.cmp-orders {
  max-height: 50vh;
  margin: 0 18px 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
}

td.up { color: var(--green-dark); }
td.down { color: var(--red); }

/* ---------- View switching ---------- */
.view-block {
  display: none;
}

body[data-view="manager"] .v-manager,
body[data-view="orders"] .v-orders,
body[data-view="bonuses"] .v-bonuses,
body[data-view="calculator"] .v-calculator,
body[data-view="finance"] .v-finance,
body[data-view="directory"] .v-directory,
body[data-view="accounts"] .v-accounts {
  display: block;
}

tbody tr.group td {
  background: var(--surface-2);
  font-weight: 700;
  border-top: 2px solid var(--line-strong);
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 6px;
}

.period-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.period-field input {
  min-height: 40px;
}

.manager-pick {
  min-width: 200px;
}

/* ---------- Calculator ---------- */
.calc-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 14px;
  padding: 18px;
}

.calc-tiers {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  padding: 0 18px 14px;
}

.tier {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
}

.tier b {
  font-size: 16px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.tier.accent {
  background: var(--green-soft);
  border-color: var(--green);
  color: var(--green-dark);
}

.tier.accent b {
  color: var(--green-dark);
}

td.negative {
  color: var(--red);
}

tbody tr.muted-row {
  opacity: 0.55;
}

.calc-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 18px 14px;
}

.calc-hint {
  color: var(--muted);
  font-size: 13px;
}

.total-row td {
  border-top: 2px solid var(--line-strong);
  font-weight: 700;
  background: var(--surface-2);
}

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

.name-input {
  min-height: 32px;
  width: 100%;
  min-width: 140px;
  padding: 0 9px;
  border-style: dashed;
  border-color: var(--yellow);
  background: var(--yellow-soft);
}

/* ---------- Charts / dashboard ---------- */
.charts {
  display: grid;
  grid-template-columns: 1.6fr 1fr 1fr;
  gap: 20px;
  padding: 18px;
}

.chart-card h3 {
  margin: 0 0 14px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.bar-chart {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bar-row {
  display: grid;
  grid-template-columns: 140px 1fr auto;
  align-items: center;
  gap: 10px;
  font-size: 13px;
}

.bar-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}

.bar-track {
  background: var(--surface-2);
  border-radius: 6px;
  height: 14px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 6px;
  background: linear-gradient(90deg, #2bb56a, #157a42);
  transform-origin: left;
  animation: grow 0.7s cubic-bezier(0.22, 1, 0.36, 1);
}

.bar-fill.alt {
  background: linear-gradient(90deg, #e2a00f, #c98605);
}

.bar-value {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--text);
}

.donut-wrap {
  display: flex;
  align-items: center;
  gap: 18px;
}

.donut {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  flex-shrink: 0;
  animation: pop 0.5s cubic-bezier(0.22, 1, 0.36, 1);
  position: relative;
}

.donut::after {
  content: "";
  position: absolute;
  inset: 22px;
  background: var(--surface);
  border-radius: 50%;
}

.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1;
  font-weight: 700;
  font-size: 20px;
}

.donut-center small {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}

.legend {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
}

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

.legend i {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  display: inline-block;
}

/* ---------- Accounts ---------- */
.account-forms {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 18px;
}

.acc-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  background: var(--surface-2);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.acc-card h3 {
  margin: 0 0 4px;
  font-size: 14px;
}

.acc-card label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.acc-msg {
  font-size: 13px;
  min-height: 16px;
  margin: 0;
}

.acc-msg.ok {
  color: var(--green-dark);
}

.acc-msg.err {
  color: var(--red);
}

.acc-actions {
  display: flex;
  gap: 6px;
}

.acc-actions button {
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: 7px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
}

.acc-actions button.danger {
  color: var(--red);
  border-color: var(--red-soft);
}

.acc-actions button:hover {
  border-color: var(--green);
}

@media (max-width: 920px) {
  .account-forms {
    grid-template-columns: 1fr;
  }
}

/* ---------- Manager multi-select ---------- */
.manager-filter {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 8px;
  padding: 16px 18px;
}

.manager-chip {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.12s, border-color 0.12s;
}

.manager-chip:hover {
  border-color: var(--green);
}

.manager-chip.checked {
  background: var(--green-soft);
  border-color: var(--green);
  font-weight: 600;
}

.manager-chip input {
  min-height: auto;
  accent-color: var(--green);
  width: 16px;
  height: 16px;
}

.manager-chip .chip-bonus {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

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

.mini-btn {
  min-height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}

.mini-btn:hover {
  border-color: var(--green);
  background: var(--green-soft);
}

/* ---------- Paid-out orders ---------- */
tbody tr.paid-out {
  color: var(--muted);
  background: repeating-linear-gradient(
    45deg,
    var(--surface-2),
    var(--surface-2) 10px,
    #eef2ef 10px,
    #eef2ef 20px
  );
}

tbody tr.paid-out .num.strong {
  text-decoration: line-through;
}

.bonus-paid-box {
  width: 17px;
  height: 17px;
  accent-color: var(--green);
  cursor: pointer;
  min-height: auto;
}

.comm-input {
  min-height: 30px;
  width: 92px;
  padding: 0 8px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ---------- Animations ---------- */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes grow {
  from {
    transform: scaleX(0);
  }
  to {
    transform: scaleX(1);
  }
}

@keyframes pop {
  from {
    transform: scale(0.85);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.metric,
.panel {
  animation: fadeInUp 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.metric:nth-child(2) { animation-delay: 0.05s; }
.metric:nth-child(3) { animation-delay: 0.1s; }
.metric:nth-child(4) { animation-delay: 0.15s; }

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

/* ---------- Responsive ---------- */
@media (max-width: 920px) {
  body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .brand {
    grid-column: 1 / -1;
    margin-bottom: 6px;
  }

  .sync-button {
    grid-column: 1 / -1;
    margin-top: 6px;
  }

  .tools {
    grid-column: 1 / -1;
    margin-top: 6px;
  }

  .topbar,
  .panel-header {
    align-items: stretch;
    flex-direction: column;
  }

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

  .panel-header input[type="search"] {
    min-width: 0;
  }

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

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

  .calc-summary,
  .calc-tiers {
    grid-template-columns: 1fr 1fr;
  }

  .calc-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .bar-row {
    grid-template-columns: 110px 1fr auto;
  }
}
