/* ============================================================
   Aportize - Rebalanceamento por aporte
   Carregar depois de chrome.css e calculator.css.
   Mesma linguagem visual do comparador de renda fixa.
   Requer <main class="wrap calculator-page cr-page">.
   Nao redefine :root, body, .wrap ou os tokens do site.
   ============================================================ */

.calculator-page.cr-page {
  --cr-column-gap: clamp(56px, 6vw, 96px);
  padding-top: clamp(36px, 5vw, 72px);
  padding-bottom: clamp(64px, 8vw, 112px);
  background: transparent;
  color: var(--text);
  font-family: var(--sans);
  color-scheme: dark;
}

/* Os estados continuam sob controle de contribution-rebalancing.js. */
.cr-page [hidden] {
  display: none !important;
}

.cr-page button,
.cr-page input,
.cr-page summary {
  -webkit-tap-highlight-color: transparent;
}

.cr-page button,
.cr-page input {
  margin: 0;
  font-family: var(--sans);
}

.cr-page button {
  appearance: none;
  cursor: pointer;
  touch-action: manipulation;
  transition:
    color var(--t),
    background-color var(--t),
    border-color var(--t);
}

.cr-page button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.cr-page button:focus-visible,
.cr-page summary:focus-visible,
.cr-page a:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: 4px;
}

.cr-page input {
  caret-color: var(--green);
}
.cr-page input::placeholder {
  color: var(--muted);
  opacity: 1;
}

/* ---------- Abertura ---------- */

.cr-page .intro {
  margin: 0 0 clamp(36px, 4.5vw, 60px);
}

.cr-page .intro__copy {
  min-width: 0;
}

.cr-page .intro .eyebrow {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 500;
  letter-spacing: 0.01em;
}

.cr-page .intro h1 {
  max-width: none;
  margin: 0;
  color: var(--text);
  font-size: clamp(2rem, 4.5vw, 4rem);
  font-weight: 600;
  line-height: 1.06;
  letter-spacing: -0.052em;
  text-wrap: balance;
  overflow-wrap: break-word;
}

.cr-page .intro h1 span {
  color: var(--muted);
}

.cr-page .intro .intro__description {
  max-width: 50ch;
  margin: 22px 0 0;
  color: var(--text-secondary);
  font-size: 1rem;
  line-height: 1.65;
  letter-spacing: -0.01em;
}

/* O alinhamento com a navbar vem exclusivamente do .wrap global. */
.cr-page .calculator-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
  gap: 40px;
  padding-top: clamp(28px, 3vw, 40px);
  border-top: 1px solid var(--hairline);
  background: none;
}

.cr-page #calculator-form {
  display: grid;
  min-width: 0;
  gap: 30px;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
}

.cr-page .section-label,
.cr-page .section-header h2 {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.9375rem;
  font-weight: 500;
  line-height: 1.4;
  letter-spacing: -0.015em;
}

.cr-page .section-header {
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin: 0;
}

.cr-page .field-hint,
.cr-page .hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.6;
}

/* ---------- Aporte: numero em destaque, sem painel ---------- */

.cr-page .primary-input {
  min-width: 0;
  margin: 0;
}
.cr-page .primary-input .section-label {
  margin: 0;
}

.cr-page .primary-input__field {
  gap: 12px;
  margin-top: 12px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--hairline);
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  transition: border-color var(--t);
}

.cr-page .primary-input__field:focus-within {
  border-color: var(--green);
}

.cr-page .primary-input__currency {
  color: var(--muted);
  font-family: var(--mono);
  font-size: clamp(1.125rem, 1.7vw, 1.5rem);
  font-weight: 400;
  letter-spacing: -0.03em;
}

.cr-page #contribution {
  flex: 1 1 0;
  min-width: 0;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  outline: none;
  color: var(--text);
  background: transparent;
  font-family: var(--mono);
  font-size: clamp(2.25rem, 4.4vw, 4rem);
  font-weight: 400;
  line-height: 1.15;
  letter-spacing: -0.055em;
  font-variant-numeric: tabular-nums;
}

/* ---------- Carteira: linhas abertas, nao cards ---------- */

.cr-page .portfolio {
  min-width: 0;
  container-type: inline-size;
  container-name: cr-portfolio;
}

.cr-page .section-description {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.6;
}

.cr-page .target-total {
  flex: none;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.5;
  white-space: nowrap;
  transition: color var(--t);
}

.cr-page .target-total b {
  color: var(--text-secondary);
  font-family: var(--mono);
  font-weight: 400;
  font-variant-numeric: tabular-nums;
}

.cr-page .target-total[data-error="1"],
.cr-page .target-total[data-error="1"] b {
  color: var(--red);
}

.cr-page .rows {
  display: grid;
  gap: 20px;
  min-width: 0;
  margin-top: 22px;
}

.cr-page .row {
  display: grid;
  grid-template-columns: minmax(0, 0.8fr) minmax(0, 1.2fr) 44px;
  grid-template-areas: "name name remove" "target value value";
  align-items: end;
  gap: 12px;
  min-width: 0;
  margin: 0;
  padding: 0 0 20px;
  border-bottom: 1px solid var(--hairline);
  background: transparent;
  /* Nao usar overflow:hidden: o menu precisa sair da linha. */
}

.cr-page .field--name {
  grid-area: name;
}
.cr-page .field--target {
  grid-area: target;
}
.cr-page .field--value {
  grid-area: value;
}
.cr-page .field {
  min-width: 0;
  gap: 7px;
}

.cr-page .row .field-label {
  display: block;
  margin: 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.5;
}

.cr-page .input-shell {
  min-width: 0;
  height: 48px;
  padding: 0 13px;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  background: var(--surface-2);
  box-shadow: none;
  transition:
    border-color var(--t),
    background-color var(--t);
}

.cr-page .input-shell:focus-within {
  border-color: var(--green);
  box-shadow: none;
}

.cr-page .input-shell input {
  min-width: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  outline: none;
  color: var(--text);
  background: transparent;
  font-family: var(--sans);
  font-size: 1rem;
  font-weight: 400;
  line-height: normal;
}

.cr-page .input-shell--numeric input {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.cr-page .input-shell__prefix,
.cr-page .input-shell__suffix {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.8125rem;
}

/* ---------- Sugestoes: mesmos hooks do JavaScript original ---------- */

.cr-page .input-shell--suggestions {
  position: relative;
}
.cr-page .input-shell--suggestions.is-open {
  z-index: 3;
}

.cr-page .input-shell__arrow {
  flex: none;
  width: 7px;
  height: 7px;
  margin-left: 2px;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: translateY(-2px) rotate(45deg);
  transition:
    transform var(--t),
    border-color var(--t);
  pointer-events: none;
}

.cr-page .input-shell--suggestions.is-open .input-shell__arrow {
  transform: translateY(1px) rotate(225deg);
  border-color: var(--green);
}

.cr-page .suggestions {
  position: absolute;
  z-index: 60;
  top: calc(100% + 6px);
  left: 0;
  /* O menu pode ser mais largo que o campo, mas cabe no mobile. */
  width: max-content;
  min-width: 100%;
  max-width: min(20rem, calc(100vw - 2 * var(--gutter) - 56px));
  max-height: min(280px, 45vh);
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0;
  padding: 6px;
  list-style: none;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  color: var(--text-secondary);
  background: var(--surface);
  box-shadow: 0 16px 32px var(--bg);
  scrollbar-width: thin;
}

.cr-page .suggestions li {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: calc(var(--r-sm) - 4px);
  color: var(--text-secondary);
  font-size: 0.9375rem;
  font-weight: 400;
  line-height: 1.4;
  white-space: normal;
  overflow-wrap: anywhere;
  cursor: pointer;
}

.cr-page .suggestions li[aria-selected="true"] {
  color: var(--green);
  background: var(--green-contrast);
}

/* ---------- Controles ---------- */

.cr-page .remove-button {
  grid-area: remove;
  align-self: end;
  display: grid;
  place-items: center;
  width: 44px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: var(--r-sm);
  color: var(--muted);
  background: transparent;
  font-size: 1.5rem;
  font-weight: 400;
  line-height: 1;
}

.cr-page .add-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  min-height: 44px;
  margin: 8px 0 0;
  padding: 0 4px;
  border: 0;
  border-radius: 4px;
  color: var(--green);
  background: transparent;
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1.5;
  text-align: left;
}

.cr-page .add-button__icon {
  font-size: 1.375rem;
  line-height: 1;
}

.cr-page .warning {
  margin: 12px 0 0;
  color: var(--red);
  font-size: 0.8125rem;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.cr-page .warning:empty {
  display: none;
}

/* ---------- Aporte recorrente ---------- */

.cr-page .recurrence {
  min-width: 0;
}

.cr-page .recurrence .field {
  max-width: 13.5rem;
}

.cr-page .recurrence .input-shell {
  height: 48px;
  padding: 0 13px;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  background: var(--surface-2);
}

.cr-page .recurrence .input-shell:focus-within {
  border-color: var(--green);
}

/* ---------- Exportar / importar CSV ---------- */

.cr-page .portfolio__io {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 20px;
  margin-top: 16px;
}

.cr-page .io-status {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.cr-page .io-status:empty {
  display: none;
}

.cr-page .io-status[data-error="1"] {
  color: var(--red);
}

/* ---------- Projeção do aporte recorrente ---------- */

.cr-page .projection {
  margin-top: 26px;
  padding-top: 22px;
  border-top: 1px solid var(--hairline);
}

.cr-page .projection__title {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.9375rem;
  font-weight: 500;
  letter-spacing: -0.015em;
}

.cr-page .projection__meta {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.cr-page .projection__list {
  display: grid;
  gap: 12px;
  margin: 16px 0 0;
  padding: 0;
  list-style: none;
}

.cr-page .projection__list li {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 2px 16px;
  min-width: 0;
}

.cr-page .projection__name {
  color: var(--text-secondary);
  font-size: 0.875rem;
  overflow-wrap: anywhere;
}

.cr-page .projection__figures {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.cr-page .projection__figures b {
  color: var(--text-secondary);
  font-weight: 400;
}

.cr-page .projection .summary {
  margin-top: 18px;
}

/* ---------- Resultado: valores primeiro, barras em segundo plano ---------- */

.cr-page .result,
.cr-page .result-empty {
  position: static;
  min-width: 0;
  width: 100%;
  margin: 0;
  padding: 32px 0 0;
  border: 0;
  border-top: 1px solid var(--hairline);
  border-radius: 0;
  color: var(--text);
  background: transparent;
  box-shadow: none;
}

.cr-page .result > .section-label {
  margin: 0;
  color: var(--green);
  font-size: 0.8125rem;
  font-weight: 500;
}

.cr-page #result-rows {
  min-width: 0;
  margin-top: 8px;
}

.cr-page .allocation-result {
  min-width: 0;
  padding: 22px 0;
  border-bottom: 1px solid var(--hairline);
}

.cr-page .allocation-result__top {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px 16px;
  min-width: 0;
  margin-bottom: 14px;
}

.cr-page .allocation-result__name {
  flex: 1 1 8rem;
  min-width: 0;
  color: var(--text);
  font-size: 1rem;
  font-weight: 500;
  line-height: 1.45;
  letter-spacing: -0.015em;
  overflow-wrap: anywhere;
}

.cr-page .allocation-result__value {
  flex: 0 1 auto;
  max-width: 100%;
  margin-left: auto;
  overflow-x: auto;
  color: var(--green);
  font-family: var(--mono);
  font-size: 1.125rem;
  font-weight: 400;
  line-height: 1.5;
  letter-spacing: -0.035em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  scrollbar-width: thin;
}

.cr-page .allocation-result--zero .allocation-result__name {
  color: var(--text-secondary);
}
.cr-page .allocation-result--zero .allocation-result__value {
  color: var(--muted);
}

/* O JS continua definindo width e left. Nao usar flex nem gap nesta barra. */
.cr-page .allocation-bar {
  position: relative;
  height: 4px;
  overflow: hidden;
  background: var(--surface-2);
}

.cr-page .allocation-bar__current,
.cr-page .allocation-bar__contribution {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  display: block;
  max-width: 100%;
  transition:
    width var(--t),
    left var(--t),
    background-color var(--t);
}

.cr-page .allocation-bar__current {
  background: var(--muted);
}
.cr-page .allocation-bar__contribution {
  background: var(--green);
}
.cr-page .allocation-result--above .allocation-bar__current {
  background: var(--red);
}

.cr-page .allocation-result__footer {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 5px 16px;
  margin-top: 11px;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.6;
}

.cr-page .allocation-result__movement,
.cr-page .allocation-result__target {
  min-width: 0;
  overflow-wrap: anywhere;
}

.cr-page .allocation-result__target {
  margin-left: auto;
}

.cr-page .allocation-result__movement b,
.cr-page .allocation-result__target b {
  color: var(--text-secondary);
  font-family: var(--mono);
  font-weight: 400;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.cr-page .allocation-result__target b {
  color: var(--muted);
}
.cr-page .allocation-result--above .allocation-result__movement b:last-child {
  color: var(--red);
}

.cr-page .allocation-result__note {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.8125rem;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.cr-page .summary {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px 16px;
  margin: 0;
  padding: 20px 0 4px;
  color: var(--muted);
  font-size: 0.875rem;
  line-height: 1.5;
}

.cr-page .summary__value {
  min-width: 0;
  max-width: 100%;
  margin-left: auto;
  overflow-x: auto;
  color: var(--text-secondary);
  font-family: var(--mono);
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  scrollbar-width: thin;
}

.cr-page .summary__value i {
  color: var(--muted);
  font-style: normal;
}
.cr-page .summary__value b {
  color: var(--green);
  font-weight: 400;
}

.cr-page #copy-result {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 48px;
  margin-top: 18px;
  padding: 12px 18px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  color: var(--text-secondary);
  background: var(--surface);
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1.5;
}

/* ---------- Explicacoes sem competir com o resultado ---------- */

.cr-page .note {
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.7;
  overflow-wrap: anywhere;
}

.cr-page .assumptions {
  margin-top: 20px;
  border-top: 1px solid var(--hairline);
}

.cr-page .assumptions summary {
  position: relative;
  display: block;
  min-height: 48px;
  padding: 15px 28px 15px 0;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  font-weight: 400;
  line-height: 1.5;
  list-style: none;
  cursor: pointer;
}

.cr-page .assumptions summary::-webkit-details-marker {
  display: none;
}

.cr-page .assumptions summary::after {
  content: "+";
  position: absolute;
  top: 11px;
  right: 2px;
  color: var(--muted);
  font-size: 1.25rem;
  font-weight: 400;
}

.cr-page .assumptions[open] summary::after {
  content: "\2212";
}
.cr-page .assumptions__content {
  padding-bottom: 10px;
}
.cr-page .assumptions__content .note:first-child {
  margin-top: 0;
}

/* Sem dados: nenhuma distribuicao ficticia na pagina de producao. */
.cr-page .result:not([hidden]) + .result-empty {
  display: none;
}
.cr-page .result-empty {
  min-height: 240px;
}
.cr-page .result-empty > .section-label {
  color: var(--muted);
  font-size: 0.8125rem;
}

.cr-page .result-empty__title {
  margin: 20px 0 0;
  color: var(--text-secondary);
  font-size: clamp(1.75rem, 2.7vw, 2.375rem);
  font-weight: 500;
  line-height: 1.15;
  letter-spacing: -0.04em;
}

.cr-page .result-empty__description {
  max-width: 34ch;
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 0.875rem;
  line-height: 1.65;
}

.cr-page .noscript {
  margin: 0 0 28px;
  padding: 16px 0;
  border-block: 1px solid var(--hairline);
  color: var(--text-secondary);
  background: transparent;
}

/* ---------- Interacao ---------- */

@media (hover: hover) {
  .cr-page .primary-input__field:hover:not(:focus-within) {
    border-color: var(--muted);
  }
  .cr-page .suggestions li:hover {
    color: var(--text);
    background: var(--surface-2);
  }
  .cr-page .remove-button:not(:disabled):hover {
    color: var(--red);
    background: var(--surface);
  }
  .cr-page .add-button:not(:disabled):hover {
    color: var(--text);
  }
  .cr-page .assumptions summary:hover {
    color: var(--text);
  }

  .cr-page #copy-result:not(:disabled):hover {
    color: var(--text);
    border-color: var(--muted);
    background: var(--surface-2);
  }
}

/* A largura DA CARTEIRA decide a linha unica, nao a largura da tela.
   Sem suporte a container queries, o fallback de duas linhas e utilizavel. */
@container cr-portfolio (min-width: 28rem) {
  .cr-page .row {
    grid-template-columns: minmax(0, 1fr) 4.75rem 9.5rem 44px;
    grid-template-areas: "name target value remove";
    gap: 10px;
    padding-bottom: 16px;
  }

  .cr-page .rows {
    gap: 16px;
  }

  /* Esconde visualmente, sem tirar o rotulo da arvore de acessibilidade. */
  .cr-page .row:not(:first-child) .field-label {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

/* Mesmo alinhamento e breakpoint das outras duas calculadoras. */
@media (min-width: 1180px) {
  .cr-page .calculator-layout {
    grid-template-columns: minmax(0, 1.05fr) minmax(0, 0.95fr);
    column-gap: var(--cr-column-gap);
    background-image: linear-gradient(var(--hairline), var(--hairline));
    background-size: 1px 100%;
    background-repeat: no-repeat;
    background-position: calc(52.5% - var(--cr-column-gap) * 0.025) 0;
  }

  .cr-page .result,
  .cr-page .result-empty {
    padding: 0;
    border-top: 0;
  }
}

/* Sem sticky: a lista de classes pode crescer livremente. */
@media (max-width: 619px) {
  .cr-page .intro .eyebrow {
    margin-bottom: 16px;
    font-size: 0.75rem;
  }
  .cr-page .intro h1 {
    font-size: clamp(1.875rem, 8.5vw, 3rem);
    line-height: 1.08;
  }
  .cr-page .intro .intro__description {
    margin-top: 18px;
    font-size: 0.9375rem;
  }
  .cr-page .intro__break {
    display: none;
  }
  .cr-page #calculator-form {
    gap: 28px;
  }
  .cr-page .primary-input__field {
    gap: 10px;
  }
  .cr-page #contribution {
    font-size: clamp(2.125rem, 10.5vw, 3.5rem);
  }
  .cr-page .input-shell,
  .cr-page .remove-button {
    height: 52px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cr-page *,
  .cr-page *::before,
  .cr-page *::after {
    transition-duration: 1ms !important;
    animation-duration: 1ms !important;
  }
}

@media (forced-colors: active) {
  .cr-page .input-shell,
  .cr-page .suggestions {
    border: 1px solid ButtonText;
  }
  .cr-page button:focus-visible,
  .cr-page summary:focus-visible,
  .cr-page a:focus-visible {
    outline-color: Highlight;
  }
  .cr-page .input-shell:focus-within,
  .cr-page .primary-input__field:focus-within {
    border-color: Highlight;
  }
  .cr-page .suggestions li[aria-selected="true"] {
    outline: 2px solid Highlight;
  }

  .cr-page .allocation-bar,
  .cr-page .allocation-bar__current,
  .cr-page .allocation-bar__contribution {
    forced-color-adjust: none;
  }
  .cr-page .allocation-bar {
    background: Canvas;
    outline: 1px solid CanvasText;
  }
  .cr-page .allocation-bar__current {
    background: GrayText;
  }
  .cr-page .allocation-bar__contribution {
    background: Highlight;
  }
  .cr-page .allocation-result--above .allocation-bar__current {
    background: CanvasText;
  }
}
