:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-soft: #eef3f8;
  --ink: #20252b;
  --muted: #687482;
  --line: #d9e0e7;
  --blue: #1f5f99;
  --blue-soft: #dceafa;
  --teal: #1f7a75;
  --green: #528f2d;
  --green-soft: #dff0d2;
  --red: #b63232;
  --red-soft: #fde5e3;
  --gray-strong: #686f77;
  --shadow: 0 12px 32px rgba(27, 39, 51, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
}

.topbar {
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  min-height: 54px;
  padding: 8px 18px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.eyebrow {
  color: #f05fa8;
  font-size: 12px;
  font-weight: 700;
  margin: 0 0 2px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  color: #1d338c;
  font-size: 28px;
  letter-spacing: 0;
  line-height: 1;
}

h2 {
  font-size: 17px;
  letter-spacing: 0;
}

main {
  display: grid;
  gap: 10px;
  padding: 12px 18px 20px;
}

.status-cluster {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  justify-content: flex-end;
  text-align: right;
}

.logout-form {
  margin: 0;
}

.badge {
  background: var(--blue-soft);
  border: 1px solid #bfd5ee;
  color: var(--blue);
  display: inline-flex;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 8px;
}

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

.control-deck {
  align-items: end;
  background: var(--surface);
  border: 1px solid var(--line);
  display: grid;
  gap: 8px;
  grid-template-columns: minmax(560px, 0.96fr) minmax(0, 1.04fr);
  padding: 8px;
}

.toolbar {
  align-items: end;
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(4, minmax(100px, 1fr)) 102px;
}

.toolbar label {
  display: grid;
  gap: 3px;
}

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

.toolbar input,
.toolbar select {
  background: #f9fbfd;
  border: 1px solid var(--line);
  color: var(--ink);
  height: 32px;
  padding: 0 10px;
}

.toolbar button,
.ghost-button {
  border: 1px solid var(--blue);
  height: 32px;
  padding: 0 10px;
}

.toolbar button {
  background: var(--blue);
  color: #fff;
  font-weight: 700;
}

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

.custom-control[hidden] {
  display: none;
}

.summary-grid {
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.summary-grid article {
  background: #f9fbfd;
  border: 1px solid var(--line);
  min-height: 56px;
  padding: 7px 9px;
}

.summary-grid span {
  color: var(--muted);
  display: block;
  font-size: 11px;
  font-weight: 700;
  margin-bottom: 5px;
}

.summary-grid strong {
  display: block;
  font-size: 19px;
  letter-spacing: 0;
  line-height: 1.15;
}

.summary-grid strong.negative,
.dfc-table .negative {
  color: var(--red);
}

.summary-grid strong.positive,
.dfc-table .positive {
  color: var(--teal);
}

.alerts-panel {
  display: grid;
  gap: 6px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.action-strip {
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  min-height: 38px;
  padding: 4px 8px;
}

.view-tabs {
  align-items: center;
  background: #eef3f8;
  border: 1px solid var(--line);
  display: inline-flex;
  padding: 3px;
}

.view-tab {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-weight: 800;
  min-height: 28px;
  padding: 0 12px;
}

.view-tab.active {
  background: var(--blue);
  color: #fff;
}

.view-panel {
  display: grid;
  gap: 10px;
}

.view-panel[hidden] {
  display: none;
}

.alert-item {
  align-items: center;
  background: #fff8e8;
  border: 1px solid #ead9a9;
  color: #5f4a12;
  display: flex;
  font-size: 13px;
  gap: 12px;
  justify-content: space-between;
  min-height: 30px;
  padding: 5px 8px;
}

.alert-item strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.alert-actions {
  align-items: center;
  display: flex;
  gap: 12px;
}

.text-link {
  background: transparent;
  border: 0;
  color: var(--blue);
  font-weight: 800;
  height: auto;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.text-link:disabled {
  color: var(--muted);
  cursor: progress;
}

.report-section,
.drilldown {
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: 0 8px 20px rgba(27, 39, 51, 0.06);
}

.drilldown {
  bottom: 18px;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  position: fixed;
  right: 18px;
  top: 76px;
  width: min(780px, calc(100vw - 36px));
  z-index: 40;
}

.section-heading {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  min-height: 42px;
  padding: 6px 10px;
}

.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 325px);
}

.inline-table-wrap {
  max-height: 420px;
}

.dfc-table {
  border-collapse: collapse;
  font-size: 13px;
  min-width: 980px;
  width: 100%;
}

.compact-table {
  min-width: 860px;
}

.dfc-table th,
.dfc-table td {
  border-bottom: 1px solid var(--line);
  height: 30px;
  padding: 0;
}

.dfc-table th {
  background: #edf1f5;
  color: #536478;
  font-weight: 800;
  position: sticky;
  top: 0;
  z-index: 5;
}

.dfc-table th:first-child,
.dfc-table td:first-child {
  left: 0;
  position: sticky;
  width: 360px;
  z-index: 6;
}

.dfc-table th:first-child {
  background: #edf1f5;
}

.row-label {
  align-items: center;
  background: var(--surface);
  display: flex;
  gap: 6px;
  height: 30px;
  min-width: 360px;
  padding: 0 10px;
}

.dfc-table tr[data-row-type="group"] .row-label,
.dfc-table tr[data-row-type="group"] td {
  background: var(--blue-soft);
  color: var(--blue);
}

.dfc-table tr[data-row-type="subtotal"] .row-label,
.dfc-table tr[data-row-type="subtotal"] td {
  background: #f2f5f8;
  color: #20252b;
  font-weight: 800;
}

.dfc-table tr[data-row-type="subtotal"] .positive,
.dfc-table tr[data-row-type="subtotal"] .value-button.positive {
  color: #1f5f99 !important;
}

.dfc-table tr[data-row-type="subtotal"] .negative,
.dfc-table tr[data-row-type="subtotal"] .value-button.negative {
  color: #c92a2a !important;
}

.dfc-table tr[data-row-id="resultado_total"] .row-label,
.dfc-table tr[data-row-id="resultado_total"] td {
  background: var(--green-soft);
  color: #123d14;
}

.dfc-table tr[data-row-id="resultado_total"] .negative {
  color: #9e1f1f;
}

.indent {
  display: inline-block;
  width: 14px;
}

.value-button,
.value-text {
  align-items: center;
  background: transparent;
  border: 0;
  color: inherit;
  display: flex;
  height: 30px;
  justify-content: flex-end;
  min-width: 112px;
  padding: 0 10px;
  text-align: right;
  width: 100%;
}

.value-button:hover,
.value-button.selected {
  background: rgba(31, 95, 153, 0.12);
  outline: 2px solid rgba(31, 95, 153, 0.28);
  outline-offset: -2px;
}

.dfc-table tr[data-row-type="subtotal"] .value-button:hover {
  background: rgba(255, 255, 255, 0.18);
}

#drilldownContent {
  overflow: auto;
}

.trend-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  padding: 14px;
}

.trend-grid article {
  background: #f9fbfd;
  border: 1px solid var(--line);
  min-height: 96px;
  padding: 13px;
}

.trend-grid span,
.trend-grid small {
  color: var(--muted);
  display: block;
  font-size: 12px;
  font-weight: 700;
}

.trend-grid strong {
  display: block;
  font-size: 23px;
  letter-spacing: 0;
  line-height: 1.15;
  margin: 8px 0 8px;
}

.trend-grid strong.positive,
.trend-grid small.positive,
.variation-table .positive {
  color: var(--teal);
}

.trend-grid strong.negative,
.trend-grid small.negative,
.variation-table .negative {
  color: var(--red);
}

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

.chart-card {
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  min-width: 0;
}

.chart-card-wide {
  grid-column: 1 / -1;
}

.chart-heading {
  border-bottom: 1px solid var(--line);
  min-height: 58px;
  padding: 12px 14px;
}

.chart-heading h3 {
  font-size: 16px;
  letter-spacing: 0;
  margin: 0 0 4px;
}

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

.chart-box {
  min-height: 320px;
  padding: 12px 12px 10px;
}

.chart-svg {
  display: block;
  height: auto;
  max-height: 330px;
  width: 100%;
}

.chart-grid-line {
  stroke: var(--line);
  stroke-width: 1;
}

.chart-zero-line {
  stroke: var(--gray-strong);
  stroke-dasharray: 4 5;
  stroke-width: 1.2;
}

.chart-y-label {
  fill: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-anchor: end;
}

.chart-x-label {
  fill: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-anchor: middle;
}

.chart-legend {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  padding: 0 6px 6px;
}

.chart-legend span {
  align-items: center;
  color: var(--muted);
  display: inline-flex;
  font-size: 12px;
  font-weight: 700;
  gap: 6px;
}

.chart-legend i {
  display: inline-block;
  height: 10px;
  width: 10px;
}

.chart-empty {
  color: var(--muted);
  padding: 18px 14px;
}

#evolutionVariationTable {
  overflow: auto;
}

.variation-table {
  border-collapse: collapse;
  font-size: 13px;
  min-width: 520px;
  width: 100%;
}

.variation-table th,
.variation-table td {
  border-bottom: 1px solid var(--line);
  padding: 11px 12px;
  text-align: right;
  vertical-align: top;
}

.variation-table th {
  background: #f2f5f8;
  color: var(--muted);
  font-size: 12px;
}

.variation-table th:first-child,
.variation-table td:first-child {
  text-align: left;
}

.empty-state {
  color: var(--muted);
  padding: 18px 14px;
}

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

.drilldown-table th,
.drilldown-table td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: left;
  vertical-align: top;
}

.drilldown-table th {
  background: #f2f5f8;
  color: var(--muted);
  font-size: 12px;
}

.amount-cell {
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
}

@media (max-width: 980px) {
  .control-deck,
  .summary-grid,
  .trend-grid,
  .analytics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .control-deck,
  .alerts-panel {
    grid-template-columns: 1fr;
  }

  .toolbar button {
    grid-column: span 2;
  }
}

@media (max-width: 1140px) {
  .control-deck {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .topbar,
  .section-heading {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  main {
    padding: 12px;
  }

  .control-deck,
  .toolbar,
  .summary-grid,
  .trend-grid,
  .analytics-grid {
    grid-template-columns: 1fr;
  }

  .toolbar button {
    grid-column: auto;
  }

  .alert-item,
  .alert-actions,
  .action-strip,
  .view-tabs {
    align-items: flex-start;
    flex-direction: column;
  }

  .action-strip {
    justify-content: flex-start;
  }

  .view-tabs {
    justify-self: stretch;
    width: 100%;
  }

  .view-tab {
    width: 100%;
  }

  .table-wrap {
    max-height: calc(100vh - 360px);
  }

    justify-self: stretch;
    width: 100%;
  }

  .view-tab {
    width: 100%;
  }

  .table-wrap {
    max-height: calc(100vh - 360px);
  }

  .drilldown {
    bottom: 12px;
    left: 12px;
    right: 12px;
    top: 96px;
    width: auto;
  }

  .status-cluster {
    align-items: flex-start;
    justify-content: flex-start;
    text-align: left;
  }
}

/* Config Section Layout */
.config-global-actions {
  display: flex;
  gap: 10px;
}

.primary-button {
  background: var(--blue);
  border: 1px solid var(--blue);
  color: #fff;
  font-weight: 700;
  height: 32px;
  padding: 0 14px;
}
.primary-button:hover {
  background: #194e7e;
}

.danger-button {
  background: #fff;
  border: 1px solid var(--red);
  color: var(--red);
  font-weight: 700;
  height: 32px;
  padding: 0 14px;
}
.danger-button:hover {
  background: var(--red-soft);
}

.secondary-button {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--ink);
  font-weight: 700;
  height: 32px;
  padding: 0 14px;
}

.compact-btn {
  font-size: 12px;
  height: 28px;
  padding: 0 10px;
}

.config-layout {
  display: grid;
  gap: 16px;
  grid-template-columns: 1.1fr 0.9fr;
  margin-top: 10px;
}

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

.config-left-panel,
.config-right-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 210px);
  min-height: 480px;
}

.panel-header {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  min-height: 48px;
  padding: 10px 14px;
  background: #fcfdfe;
}

.panel-header h3 {
  font-size: 15px;
  margin: 0;
  color: var(--blue);
}

.config-tree,
.config-list {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
}

/* Rules Tree Styles */
.tree-node {
  border-left: 2px dashed var(--line);
  margin-left: 10px;
  padding-left: 10px;
}
.tree-node.root-node {
  border-left: 0;
  margin-left: 0;
  padding-left: 0;
}

.tree-row {
  align-items: center;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 4px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  padding: 8px 12px;
}
.tree-row.group-row {
  background: var(--blue-soft);
  border-color: #bfd5ee;
}
.tree-row.subtotal-row {
  background: #f1f3f5;
  border-color: #ced4da;
  font-weight: 700;
}

.tree-row-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.row-type-indicator {
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  color: var(--muted);
}
.row-type-indicator.type-group {
  color: var(--blue);
}
.row-type-indicator.type-subtotal {
  color: var(--gray-strong);
}

.tree-row-title {
  font-size: 13px;
}
.tree-row-id {
  font-size: 11px;
  color: var(--muted);
  font-family: monospace;
}

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

.delete-row-btn {
  background: transparent;
  border: 0;
  color: var(--red);
  font-size: 16px;
  padding: 4px;
}
.delete-row-btn:hover {
  background: var(--red-soft);
  border-radius: 4px;
}

/* Category Mapping inside lines */
.mapped-categories-box {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
  padding: 6px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 4px;
}

.mapped-category-tag {
  align-items: center;
  background: #f1f3f5;
  border: 1px solid #dee2e6;
  border-radius: 3px;
  color: #495057;
  display: inline-flex;
  font-size: 11px;
  gap: 4px;
  padding: 2px 6px;
}

.remove-cat-btn {
  background: transparent;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  padding: 0 2px;
}
.remove-cat-btn:hover {
  color: var(--red);
}

.add-cat-selector {
  border: 1px solid var(--line);
  border-radius: 3px;
  font-size: 11px;
  height: 22px;
  padding: 0 4px;
  background: #fff;
}

/* Categories List in Right Panel */
.categories-filter {
  display: flex;
  background: #eef3f8;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px;
}

.filter-tab {
  background: transparent;
  border: 0;
  border-radius: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  height: 24px;
  padding: 0 10px;
}
.filter-tab.active {
  background: var(--blue);
  color: #fff;
}

.cat-item-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 4px;
  margin-bottom: 8px;
  padding: 10px;
}
.cat-item-card.orphan {
  border-left: 3px solid #f0a020;
  background: #fffdf6;
}

.cat-item-name {
  font-size: 12px;
  font-weight: 700;
  word-break: break-all;
}

.cat-item-status-row {
  align-items: center;
  display: flex;
  justify-content: space-between;
  margin-top: 6px;
  gap: 10px;
}

.cat-status-badge {
  font-size: 10px;
  font-weight: 800;
  padding: 2px 6px;
  border-radius: 10px;
}
.cat-status-badge.mapped {
  background: var(--green-soft);
  color: var(--green);
}
.cat-status-badge.orphan {
  background: #ffeeba;
  color: #856404;
}

.cat-assign-action {
  display: flex;
  gap: 4px;
  align-items: center;
}

.cat-assign-select {
  border: 1px solid var(--line);
  border-radius: 3px;
  font-size: 11px;
  height: 24px;
  max-width: 160px;
  background: #fff;
}

/* Modals */
.modal {
  align-items: center;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  height: 100%;
  justify-content: center;
  left: 0;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 100;
}

.modal-dialog {
  max-width: 480px;
  padding: 16px;
  width: 100%;
}

.modal-content {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.modal-header h3 {
  font-size: 16px;
  margin: 0;
}

.close-btn {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 24px;
  cursor: pointer;
}

#addRowForm {
  padding: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.form-group label {
  font-size: 12px;
  font-weight: 700;
  color: var(--ink);
}

.form-group input,
.form-group select {
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 4px;
  height: 36px;
  padding: 0 10px;
  width: 100%;
}

.modal-footer {
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 16px;
  background: #f8fafc;
}

