:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --panel: #ffffff;
  --text: #18212a;
  --muted: #64717d;
  --line: #d9e0e5;
  --income: #137a5b;
  --expense: #b83d4b;
  --accent: #2457a6;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

.login-page {
  display: grid;
  place-items: center;
}

.login-shell {
  width: min(420px, calc(100% - 32px));
}

.login-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 60px rgba(24, 33, 42, 0.16);
  display: grid;
  gap: 16px;
  padding: 26px;
}

.login-card h1 {
  font-size: 32px;
}

.form-error {
  color: var(--expense);
  font-size: 14px;
  min-height: 18px;
}

.app {
  width: 100%;
  margin: 0 auto;
  padding: 28px 16px;
}

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

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(28px, 4vw, 44px);
}

h2 {
  font-size: 18px;
  margin-bottom: 16px;
}

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

.summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin-bottom: 16px;
}

.summary article,
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(24, 33, 42, 0.06);
}

.summary article {
  padding: 18px;
}

.summary span {
  color: var(--muted);
  display: block;
  font-size: 14px;
  margin-bottom: 8px;
}

.summary strong {
  font-size: 28px;
}

.period-panel,
.comparison-grid {
  display: grid;
  gap: 16px;
  margin-bottom: 16px;
}

.period-panel {
  align-items: end;
  grid-template-columns: 1fr 220px 120px;
}

.comparison-grid {
  grid-template-columns: 0.75fr 1.25fr;
  margin-top: 16px;
}

.tabs {
  display: flex;
  gap: 8px;
  margin: 4px 0 16px;
  overflow-x: auto;
}

.tab-button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  min-height: 40px;
  padding: 0 16px;
  white-space: nowrap;
}

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

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.panel {
  padding: 18px;
}

form {
  display: grid;
  gap: 14px;
}

label {
  color: var(--muted);
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 700;
}

input,
select,
button {
  border-radius: 6px;
  font: inherit;
  min-height: 42px;
}

input,
select {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 8px 10px;
}

button {
  background: var(--accent);
  border: 0;
  color: #fff;
  cursor: pointer;
  font-weight: 700;
  padding: 0 14px;
}

button:hover {
  filter: brightness(0.94);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.secondary-button,
.icon-button {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--text);
}

.icon-button {
  border-radius: 999px;
  min-height: 34px;
  min-width: 34px;
  padding: 0;
}

.row-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.small-button {
  min-height: 32px;
  padding: 0 10px;
}

.danger-button {
  background: var(--expense);
}

.concept-list {
  margin-top: 8px;
}

.concept-list-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  max-height: 260px;
  overflow: auto;
}

.concept-list-table {
  border-collapse: collapse;
  font-size: 12px;
  min-width: 700px;
  width: 100%;
}

.concept-list-table th,
.concept-list-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px 8px;
  text-align: left;
  vertical-align: top;
  white-space: normal;
}

.concept-list-table th {
  background: #f8fafb;
  color: var(--muted);
  font-size: 11px;
  position: sticky;
  top: 0;
}

.concept-list-table td:last-child,
.concept-list-table th:last-child {
  width: 180px;
}

.concept-actions-cell {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.concept-list-table td:nth-child(5),
.concept-list-table th:nth-child(5) {
  font-weight: 700;
  min-width: 90px;
}

.concept-actions-cell {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.empty-table-row {
  color: var(--muted);
  padding: 16px 8px;
  text-align: center;
}

.settings-list {
  display: grid;
  gap: 10px;
}

.settings-row {
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 8px;
  grid-template-columns: 1.6fr 1fr 1fr;
  padding: 8px 10px;
}

.settings-row span {
  color: var(--muted);
  font-size: 12px;
}

.settings-row input {
  min-width: 0;
}

.compare-result {
  min-height: 142px;
}

.compare-result:empty::before {
  color: var(--muted);
  content: "Seleccione dos periodos para comparar.";
}

.compare-table {
  display: grid;
  gap: 10px 12px;
  grid-template-columns: 1fr repeat(3, minmax(110px, auto));
  overflow-x: auto;
}

.compare-table span,
.compare-table strong {
  white-space: nowrap;
}

.concept-summary {
  display: grid;
  gap: 14px;
  grid-template-columns: 1fr 1fr minmax(180px, 0.45fr);
}

.concept-column {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.concept-column h3 {
  font-size: 15px;
  margin: 0 0 12px;
}

.concept-row,
.concept-total,
.concept-difference,
.concept-category,
.concept-subtotal {
  align-items: center;
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.concept-row {
  border-bottom: 1px solid var(--line);
  padding: 10px 0;
}

.concept-row span,
.concept-total span,
.concept-difference span {
  color: var(--muted);
}

.concept-total {
  padding-top: 14px;
}

.concept-category {
  background: #eaf1f5;
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  font-weight: 700;
  margin-top: 10px;
  padding: 12px;
}

.concept-category span {
  color: var(--text);
  display: grid;
  gap: 2px;
}

.concept-category small,
.level-label {
  color: var(--muted);
  display: block;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
}

.concept-subtotal {
  color: var(--text);
  font-weight: 700;
  padding: 10px 4px 4px 14px;
}

.concept-detail {
  padding-left: 14px;
}

.concept-difference {
  align-items: flex-start;
  background: #f8fafb;
  border: 1px solid var(--line);
  border-radius: 8px;
  flex-direction: column;
  justify-content: center;
  padding: 18px;
}

.concept-difference strong {
  font-size: 28px;
}

.empty-state,
.muted-row {
  color: var(--muted);
}

.table-wrap {
  overflow-x: auto;
}

.matrix-wrap {
  overflow-x: auto;
}

.matrix-toolbar {
  align-items: end;
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin: -4px 0 12px;
}

.matrix-toolbar label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.matrix-toolbar select {
  min-width: 150px;
}

.matrix-table {
  border: 1px solid var(--line);
  font-size: 11px;
  min-width: 1240px;
}

.matrix-table th,
.matrix-table td {
  border-bottom: 1px solid #e6ebef;
  padding: 5px 7px;
  vertical-align: middle;
}

.matrix-table th {
  background: #f3f6f8;
  color: #53606b;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0;
  position: sticky;
  text-transform: uppercase;
  top: 0;
  z-index: 2;
}

.matrix-table td.right,
.matrix-table th.right {
  font-variant-numeric: tabular-nums;
}

.matrix-table .level-label {
  color: #6f7b86;
  font-size: 8px;
  line-height: 1;
}

.matrix-cell-button {
  background: transparent;
  border: 1px solid transparent;
  color: inherit;
  cursor: pointer;
  font: inherit;
  font-weight: inherit;
  min-height: 22px;
  min-width: 66px;
  padding: 2px 4px;
  text-align: right;
}

.matrix-cell-button:hover,
.matrix-cell-button:focus {
  background: #ffffff;
  border-color: var(--accent);
  border-radius: 4px;
  outline: none;
}

.matrix-table th:first-child,
.matrix-table td:first-child,
.matrix-table th:nth-child(2),
.matrix-table td:nth-child(2) {
  background: #fff;
  position: sticky;
  z-index: 3;
}

.matrix-table th:first-child,
.matrix-table td:first-child {
  left: 0;
}

.matrix-table th:nth-child(2),
.matrix-table td:nth-child(2) {
  left: 96px;
}

.matrix-table th:first-child,
.matrix-table td:first-child {
  min-width: 96px;
}

.matrix-table th:nth-child(2),
.matrix-table td:nth-child(2) {
  min-width: 170px;
}

.matrix-total-row {
  background: #f6f8fa;
  border-top: 2px solid #cfd8df;
  font-weight: 700;
}

.matrix-bank-section td {
  color: #17202a;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.matrix-gnb-section td,
.matrix-gnb-row,
.matrix-gnb-row td:first-child,
.matrix-gnb-row td:nth-child(2) {
  background: #fff7ea;
}

.matrix-ueno-section td,
.matrix-ueno-row,
.matrix-ueno-row td:first-child,
.matrix-ueno-row td:nth-child(2) {
  background: #eef9f6;
}

.matrix-other-section td {
  background: #f0f3f6;
}

.matrix-gnb-row.matrix-subtotal-row,
.matrix-gnb-row.matrix-subtotal-row td:first-child,
.matrix-gnb-row.matrix-subtotal-row td:nth-child(2) {
  background: #fffaf2;
}

.matrix-ueno-row.matrix-subtotal-row,
.matrix-ueno-row.matrix-subtotal-row td:first-child,
.matrix-ueno-row.matrix-subtotal-row td:nth-child(2) {
  background: #f6fcfa;
}

.matrix-bank-row {
  background: #fff7e8;
  border-top: 1px solid #cfd8df;
  font-weight: 800;
}

.matrix-bank-reference-row {
  background: #fffdf8;
  color: var(--muted);
  font-weight: 700;
}

.matrix-bank-income-row {
  background: #f3fbf7;
  font-weight: 800;
}

.matrix-bank-expense-row {
  background: #fff5f6;
  font-weight: 800;
}

.matrix-gnb-row.matrix-bank-row,
.matrix-gnb-row.matrix-bank-reference-row,
.matrix-gnb-row.matrix-bank-income-row,
.matrix-gnb-row.matrix-bank-expense-row,
.matrix-gnb-row.matrix-bank-row td:first-child,
.matrix-gnb-row.matrix-bank-row td:nth-child(2),
.matrix-gnb-row.matrix-bank-reference-row td:first-child,
.matrix-gnb-row.matrix-bank-reference-row td:nth-child(2),
.matrix-gnb-row.matrix-bank-income-row td:first-child,
.matrix-gnb-row.matrix-bank-income-row td:nth-child(2),
.matrix-gnb-row.matrix-bank-expense-row td:first-child,
.matrix-gnb-row.matrix-bank-expense-row td:nth-child(2) {
  background: #fff7ea;
}

.matrix-ueno-row.matrix-bank-row,
.matrix-ueno-row.matrix-bank-reference-row,
.matrix-ueno-row.matrix-bank-income-row,
.matrix-ueno-row.matrix-bank-expense-row,
.matrix-ueno-row.matrix-bank-row td:first-child,
.matrix-ueno-row.matrix-bank-row td:nth-child(2),
.matrix-ueno-row.matrix-bank-reference-row td:first-child,
.matrix-ueno-row.matrix-bank-reference-row td:nth-child(2),
.matrix-ueno-row.matrix-bank-income-row td:first-child,
.matrix-ueno-row.matrix-bank-income-row td:nth-child(2),
.matrix-ueno-row.matrix-bank-expense-row td:first-child,
.matrix-ueno-row.matrix-bank-expense-row td:nth-child(2) {
  background: #eef9f6;
}

.matrix-accumulated-row {
  background: #f1f7ee;
  font-weight: 800;
}

.matrix-category-row {
  background: #eef3f6;
  font-weight: 700;
}

.matrix-subtotal-row {
  background: #fbfcfd;
  font-weight: 700;
}

.matrix-subtotal-row td:nth-child(2) {
  padding-left: 18px;
}

.formula-notes {
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  font-size: 12px;
  margin-top: 12px;
}

.modal {
  display: none;
  inset: 0;
  padding: 10px;
  position: fixed;
  z-index: 20;
}

.modal.active {
  align-items: center;
  display: flex;
  justify-content: center;
}

.modal-backdrop {
  background: rgba(24, 33, 42, 0.42);
  inset: 0;
  position: absolute;
}

.modal-dialog {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 80px rgba(24, 33, 42, 0.28);
  max-height: calc(100vh - 36px);
  overflow-y: auto;
  padding: 18px;
  position: relative;
  width: min(96vw, 1400px);
}

.concept-dialog {
  max-height: calc(100vh - 20px);
  min-height: calc(100vh - 20px);
  width: calc(100vw - 20px);
}

.concept-dialog .modal-header p {
  font-size: 13px;
  margin-top: 4px;
}

.concept-dialog #concept-form {
  gap: 16px;
}

.concept-form-grid {
  align-items: end;
  display: grid;
  gap: 12px;
  grid-template-columns: 160px 1.1fr 1.1fr 230px 170px;
}

.hidden-field {
  display: none;
}

.concept-form-grid label,
.search-field {
  font-size: 12px;
}

.concept-form-grid input,
.concept-form-grid select,
.search-field input {
  font-size: 14px;
}

.concept-browser {
  background: #f8fafb;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.search-field {
  margin-bottom: 12px;
}

.search-field input {
  border-color: #c7d2da;
  min-height: 46px;
}

.concept-list {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  max-height: calc(100vh - 330px);
  overflow-y: auto;
  padding-right: 4px;
}

.concept-card {
  background: #fff;
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  display: grid;
  gap: 12px;
  padding: 12px;
}

.concept-card.egreso {
  border-left-color: var(--expense);
}

.concept-card.ingreso {
  border-left-color: var(--income);
}

.concept-card-main {
  align-items: start;
  display: flex;
  gap: 10px;
  justify-content: space-between;
}

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

.concept-card h3 {
  font-size: 15px;
  line-height: 1.25;
  margin: 3px 0 0;
}

.concept-sign {
  align-items: center;
  border-radius: 999px;
  display: inline-flex;
  font-size: 18px;
  font-weight: 800;
  height: 32px;
  justify-content: center;
  min-width: 32px;
}

.concept-sign.positive {
  background: rgba(19, 122, 91, 0.12);
  color: var(--income);
}

.concept-sign.negative {
  background: rgba(184, 61, 75, 0.12);
  color: var(--expense);
}

.concept-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.concept-card-meta span {
  background: #eef3f6;
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
  padding: 5px 8px;
}

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

.concept-card-actions button {
  flex: 1;
}

.concept-empty {
  background: #fff;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  grid-column: 1 / -1;
  padding: 22px;
  text-align: center;
}

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

.modal-header h2 {
  margin-bottom: 0;
}

body.modal-open {
  overflow: hidden;
}

table {
  border-collapse: collapse;
  width: 100%;
}

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

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

.right {
  text-align: right;
}

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

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

#toast {
  background: #18212a;
  border-radius: 8px;
  bottom: 18px;
  color: #fff;
  left: 50%;
  opacity: 0;
  padding: 12px 16px;
  pointer-events: none;
  position: fixed;
  transform: translateX(-50%) translateY(10px);
  transition: 180ms ease;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 760px) {
  .summary,
  .period-panel,
  .comparison-grid,
  .concept-summary {
    grid-template-columns: 1fr;
  }

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

  .actions {
    justify-content: stretch;
  }

  .actions button {
    flex: 1;
  }

  .app {
    padding: 18px 10px;
  }

  .modal {
    padding: 8px;
  }

  .concept-dialog {
    max-height: calc(100vh - 16px);
    min-height: calc(100vh - 16px);
    width: calc(100vw - 16px);
  }

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

  .concept-list {
    grid-template-columns: 1fr;
    max-height: calc(100vh - 420px);
  }
}
