:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --ink: #1f2937;
  --muted: #64748b;
  --line: #d8dee9;
  --blue: #1d4ed8;
  --green: #047857;
  --red: #b42318;
  --amber: #b45309;
  --violet: #6d28d9;
  --cyan: #0e7490;
  --shadow: 0 10px 28px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 264px 1fr;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", Arial, sans-serif;
}

.sidebar {
  min-height: 100vh;
  background: #111827;
  color: #e5e7eb;
  padding: 20px 14px;
  position: sticky;
  top: 0;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 4px 8px 18px;
  border-bottom: 1px solid rgba(255,255,255,0.12);
  margin-bottom: 14px;
}

.brand strong { display: block; font-size: 16px; }
.brand span { display: block; color: #9ca3af; font-size: 12px; margin-top: 2px; }

.mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  background: #f8fafc;
  color: #111827;
  font-weight: 800;
  border-radius: 8px;
}

nav { display: grid; gap: 4px; }

.nav-button {
  width: 100%;
  border: 0;
  background: transparent;
  color: #d1d5db;
  text-align: left;
  padding: 10px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.nav-button:hover,
.nav-button.active {
  background: #263244;
  color: #ffffff;
}

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

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

h1 {
  font-size: 24px;
  line-height: 1.2;
  margin: 0 0 4px;
  letter-spacing: 0;
}

h2 {
  font-size: 17px;
  line-height: 1.25;
  margin: 0 0 12px;
  letter-spacing: 0;
}

p { margin: 0; }

#metaText,
.muted {
  color: var(--muted);
  font-size: 13px;
}

.button,
select,
input {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 0 12px;
  font: inherit;
}

.button {
  cursor: pointer;
  font-weight: 600;
}

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

.button.secondary {
  background: #f8fafc;
}

.button:disabled {
  cursor: wait;
  opacity: 0.7;
}

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

.content { display: grid; gap: 16px; }

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

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

.kpi {
  padding: 16px;
  min-height: 102px;
}

.kpi span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0;
}

.kpi strong {
  display: block;
  font-size: 25px;
  margin-top: 10px;
  line-height: 1.15;
}

.grid-2 {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(320px, 0.8fr);
  gap: 16px;
}

.panel {
  padding: 16px;
}

.agent-overview {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 10px;
  margin: 12px 0 16px;
}

.agent-overview-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #f8fafc;
  min-height: 88px;
}

.agent-overview-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0;
}

.agent-overview-item strong {
  display: block;
  margin-top: 8px;
  font-size: 20px;
  line-height: 1.2;
}

.agent-overview-item small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  line-height: 1.3;
}

.chart {
  width: 100%;
  height: 340px;
}

.bar-row {
  display: grid;
  grid-template-columns: 94px 1fr 92px;
  gap: 10px;
  align-items: center;
  margin: 9px 0;
  font-size: 13px;
}

.bar-track {
  height: 9px;
  background: #e5e7eb;
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: var(--blue);
}

.bar-fill.green { background: var(--green); }
.bar-fill.amber { background: var(--amber); }
.bar-fill.violet { background: var(--violet); }
.bar-fill.cyan { background: var(--cyan); }

.phase-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

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

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

.phase-line-chart {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 8px;
}

.phase-line-chart svg {
  display: block;
  min-width: 720px;
  width: 100%;
  height: auto;
}

.line-grid {
  stroke: #e5e7eb;
  stroke-width: 1;
}

.line-axis {
  stroke: #94a3b8;
  stroke-width: 1.2;
}

.line-axis-label {
  fill: var(--muted);
  font-size: 12px;
}

.phase-line polyline {
  fill: none;
  stroke: var(--line-color);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.phase-line circle {
  fill: #fff;
  stroke: var(--line-color);
  stroke-width: 2.5;
}

.phase-peak-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
}

.phase-peak-summary strong {
  color: var(--ink);
}

.blue { background: var(--blue); }
.green { background: var(--green); }
.amber { background: var(--amber); }
.violet { background: var(--violet); }
.cyan { background: var(--cyan); }

.table-wrap {
  overflow: auto;
  max-height: 620px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

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

th, td {
  border-bottom: 1px solid #edf0f5;
  padding: 9px 10px;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  background: #f8fafc;
  color: #334155;
  z-index: 1;
}

tr:hover td { background: #fbfdff; }

tr.fully-paid-lot td {
  background: #ecfdf5;
}

tr.fully-paid-lot:hover td {
  background: #dcfce7;
}

.danger { color: var(--red); font-weight: 700; }
.good { color: var(--green); font-weight: 700; }
.warn { color: var(--amber); font-weight: 700; }

.alert-panel {
  border-color: #f59e0b;
  background: #fffbeb;
}

.toast-notice {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 20;
  max-width: min(360px, calc(100vw - 44px));
  border: 1px solid #bbf7d0;
  border-radius: 8px;
  background: #f0fdf4;
  color: #166534;
  box-shadow: var(--shadow);
  padding: 13px 15px;
  font-size: 14px;
  font-weight: 700;
}

.toast-notice.error {
  border-color: #fecaca;
  background: #fef2f2;
  color: var(--red);
}

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 12px;
}

.toolbar input,
.toolbar select {
  min-width: 240px;
}

.row-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.icon-button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font-weight: 700;
  padding: 7px 9px;
  cursor: pointer;
}

.icon-button:hover {
  background: #f8fafc;
}

.danger-button {
  border-color: #fecaca;
  color: var(--red);
}

.contract-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.contract-badge.drafted {
  background: #f1f5f9;
  color: #475569;
}

.contract-badge.signed {
  background: #dbeafe;
  color: #1d4ed8;
}

.contract-badge.notarized {
  background: #dcfce7;
  color: #15803d;
}

.contract-badge.notarized-copy-given {
  background: #fef3c7;
  color: #a16207;
}

.edit-payment-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 12px;
  align-items: end;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  padding: 14px;
  margin-bottom: 14px;
}

.edit-payment-form h2 {
  grid-column: span 4;
  margin: 0;
}

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

.form-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.commission-pay-controls {
  display: flex;
  gap: 6px;
  align-items: center;
  min-width: 150px;
}

.commission-pay-controls input {
  min-width: 0;
}

.commission-table input.compact-input {
  width: 145px;
  min-width: 0;
}

.receipt {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  overflow: auto;
}

.doc-frame {
  width: 100%;
  min-height: 720px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.lookup-layout {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) auto auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 12px;
}

.lookup-layout .client-search-field {
  grid-column: auto;
}

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

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

.form-grid input,
.form-grid select {
  width: 100%;
}

.client-search-field {
  display: grid;
  gap: 6px;
  position: relative;
  grid-column: span 2;
}

.field-hint {
  min-height: 16px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 400;
  line-height: 1.25;
}

.penalty-panel {
  grid-column: span 4;
  display: grid;
  grid-template-columns: minmax(240px, 1fr) auto minmax(220px, .7fr);
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  padding: 14px;
}

.penalty-panel.has-penalty {
  border-color: #f59e0b;
  background: #fffbeb;
}

.penalty-panel.is-clear {
  border-color: #bbf7d0;
  background: #f0fdf4;
}

.penalty-panel span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.penalty-panel strong {
  display: block;
  margin-top: 4px;
  color: var(--ink);
  font-size: 16px;
}

.penalty-panel small {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.subpanel {
  margin-top: 18px;
}

.spread {
  justify-content: space-between;
  align-items: center;
}

.penalty-review-wrap input {
  min-width: 110px;
}

.penalty-review-wrap .penalty-reason-field {
  min-width: 180px;
}

tr.needs-penalty td {
  background: #fffbeb;
}

.penalty-status {
  display: inline-flex;
  min-width: 76px;
  justify-content: center;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  background: #eef2f7;
  color: #475569;
}

.penalty-status.suggested {
  background: #fef3c7;
  color: #92400e;
}

.penalty-status.recorded {
  background: #dcfce7;
  color: #166534;
}

.penalty-status.waived {
  background: #fee2e2;
  color: #991b1b;
}

.penalty-status.clear {
  background: #e0f2fe;
  color: #075985;
}

.check-line {
  display: flex !important;
  grid-template-columns: none !important;
  align-items: center;
  gap: 8px !important;
  white-space: nowrap;
}

.check-line input {
  width: auto;
}

.link-button {
  border: 0;
  background: transparent;
  color: var(--blue);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}

.agent-client-detail td {
  background: #f8fafc !important;
  padding: 14px;
}

.agent-client-groups {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
}

.agent-breakdown-note {
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.agent-client-group {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 12px;
}

.agent-client-group h3 {
  margin: 0 0 10px;
  font-size: 13px;
}

.agent-client-group h3 small {
  color: var(--muted);
  font-size: 12px;
}

.agent-client-group ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.agent-client-group li {
  display: grid;
  gap: 3px;
  border-top: 1px solid #eef2f7;
  padding-top: 8px;
}

.agent-client-group li:first-child {
  border-top: 0;
  padding-top: 0;
}

.agent-client-group li span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.waiver-reason {
  margin: 0;
}

.suggestions {
  position: absolute;
  z-index: 5;
  top: 64px;
  left: 0;
  right: 0;
  max-height: 360px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 6px;
}

.suggestion {
  width: 100%;
  height: auto;
  display: grid;
  gap: 3px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  padding: 10px;
  cursor: pointer;
}

.suggestion:hover,
.suggestion:focus {
  background: #eef4ff;
  outline: none;
}

.suggestion strong {
  font-size: 14px;
}

.suggestion span,
.suggestion small,
.suggestion-empty {
  color: var(--muted);
  font-size: 12px;
}

.suggestion-empty {
  padding: 12px;
}

.receipt table td {
  white-space: pre-wrap;
  min-width: 110px;
}

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

.note {
  padding: 12px 14px;
  border-left: 4px solid var(--amber);
  background: #fff7ed;
  border-radius: 6px;
  font-size: 13px;
}

@media (max-width: 980px) {
  body { grid-template-columns: 1fr; }
  .sidebar { min-height: auto; position: static; }
  nav { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .main { padding: 18px; }
  .topbar { align-items: flex-start; flex-direction: column; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .agent-overview { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .edit-payment-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .edit-payment-form h2 { grid-column: span 2; }
  .client-search-field { grid-column: span 2; }
  .penalty-panel { grid-column: span 2; grid-template-columns: 1fr; align-items: stretch; }
  .lookup-layout { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
  nav { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: 1fr; }
  .toolbar input,
  .toolbar select,
  .button { width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .edit-payment-form { grid-template-columns: 1fr; }
  .edit-payment-form h2 { grid-column: span 1; }
  .client-search-field { grid-column: span 1; }
  .penalty-panel { grid-column: span 1; }
}
