/* ===== Base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--text-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3,
p {
  margin: 0;
}

a {
  color: var(--accent);
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

code {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

.skip-link {
  position: absolute;
  left: var(--space-4);
  top: -48px;
  padding: var(--space-2) var(--space-3);
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: var(--radius-sm);
  z-index: 10;
}

.skip-link:focus {
  top: var(--space-2);
}

.money,
.price-input,
.metric-value,
.compare td {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* ===== Topbar ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}

.brand-title {
  font-size: var(--text-lg);
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand-sub {
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.topbar-actions {
  display: flex;
  gap: var(--space-2);
  flex: none;
}

/* ===== Layout ===== */
.layout {
  display: grid;
  /* minmax(0, 1fr): a coluna não cresce para caber a tabela; a tabela rola na própria caixa */
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-5);
  max-width: 1440px;
  margin: 0 auto;
  padding: var(--space-5);
}

@media (min-width: 1024px) {
  .layout {
    grid-template-columns: 360px minmax(0, 1fr);
    align-items: start;
  }
}

.sidebar,
.results {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-4);
  align-content: start;
  min-width: 0;
}

.results:focus {
  outline: none;
}

/* ===== Card ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  box-shadow: var(--shadow-sm);
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.card-head h2,
.section-head h2 {
  font-size: var(--text-md);
  font-weight: 600;
}

.pill {
  padding: 2px var(--space-2);
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  color: var(--text-2);
  font-size: var(--text-xs);
  font-weight: 500;
  white-space: nowrap;
}

.hint {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.stack {
  display: grid;
  gap: var(--space-3);
}

.row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3);
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 40px;
  padding: 0 var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition:
    background var(--dur) var(--ease),
    border-color var(--dur) var(--ease),
    transform var(--dur) var(--ease);
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-ghost {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
}

.btn-danger {
  color: var(--critical);
}

.btn-sm {
  min-height: 32px;
  padding: 0 var(--space-3);
  font-size: var(--text-xs);
}

.btn-icon {
  width: 40px;
  padding: 0;
  background: var(--surface);
  border-color: var(--border-strong);
  font-size: var(--text-lg);
}

.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: inherit;
  text-decoration: underline;
  cursor: pointer;
}

/* ===== Fields ===== */
.field {
  display: grid;
  gap: var(--space-1);
  min-width: 0;
}

.field-label {
  color: var(--text-2);
  font-size: var(--text-xs);
  font-weight: 600;
}

input[type="text"],
input[type="search"],
input[type="url"] {
  width: 100%;
  min-height: 40px;
  padding: 0 var(--space-3);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  transition: border-color var(--dur) var(--ease);
}

input:hover {
  border-color: var(--text-muted);
}

input[aria-invalid="true"] {
  border-color: var(--critical);
  background: var(--critical-soft);
}

.field-error {
  color: var(--critical);
  font-size: var(--text-xs);
  font-weight: 500;
}

/* ===== Tabs ===== */
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-1);
  padding: var(--space-1);
  margin-bottom: var(--space-3);
  background: var(--surface-2);
  border-radius: var(--radius-md);
}

.tabs button {
  min-height: 34px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-2);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
}

.tabs button[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* ===== Search ===== */
.search-results {
  display: grid;
  max-height: 260px;
  margin: var(--space-2) 0 0;
  padding: var(--space-1);
  overflow-y: auto;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
}

.search-results li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-sm);
}

.search-results li[aria-selected="true"],
.search-results li:hover {
  background: var(--accent-soft);
}

.search-results .sku {
  margin-right: var(--space-2);
}

.search-results .empty {
  color: var(--text-muted);
  cursor: default;
}

#search-empty {
  margin-top: var(--space-2);
}

.sku {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* ===== Import ===== */
.dropzone {
  position: relative;
  display: grid;
  gap: var(--space-1);
  justify-items: center;
  padding: var(--space-5) var(--space-3);
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-md);
  text-align: center;
  cursor: pointer;
  transition:
    border-color var(--dur) var(--ease),
    background var(--dur) var(--ease);
}

.dropzone:hover,
.dropzone.is-dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.dropzone:focus-within {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.dropzone input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.dropzone-title {
  font-weight: 600;
  font-size: var(--text-sm);
}

.import-report:empty {
  display: none;
}

.import-report {
  padding: var(--space-3);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  font-size: var(--text-xs);
}

.import-report ul {
  margin: var(--space-2) 0 0;
  padding-left: var(--space-4);
  max-height: 140px;
  overflow-y: auto;
}

.import-report .ok {
  color: var(--good);
  font-weight: 600;
}

.import-report .bad {
  color: var(--critical);
  font-weight: 600;
}

.catalog-tools {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

/* ===== Selected product ===== */
.selected {
  display: grid;
  gap: var(--space-3);
  margin-top: var(--space-4);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border);
}

.selected-head {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: var(--space-2);
}

.selected-name {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.cost-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3);
  margin: 0;
}

.cost-grid > div {
  padding: var(--space-3);
  background: var(--surface-2);
  border-radius: var(--radius-md);
  min-width: 0;
}

.cost-grid dt {
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.cost-grid dd {
  margin: var(--space-1) 0 0;
  font-size: var(--text-lg);
  font-weight: 600;
}

.cost-grid input {
  min-height: 32px;
  padding: 0 var(--space-2);
  font-family: var(--font-mono);
  font-size: var(--text-md);
  font-weight: 600;
}

.info {
  cursor: help;
}

.kit {
  margin: 0;
  padding: 0;
  border: 0;
}

.kit legend {
  margin-bottom: var(--space-2);
  color: var(--text-2);
  font-size: var(--text-xs);
  font-weight: 600;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-1);
  margin-bottom: var(--space-2);
}

.segmented button {
  min-height: 36px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
}

.segmented button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

/* ===== Sliders ===== */
.slider-field + .slider-field {
  margin-top: var(--space-4);
}

.slider-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: var(--space-1);
  font-size: var(--text-sm);
  font-weight: 600;
}

.slider-head output {
  font-family: var(--font-mono);
  color: var(--accent);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

/* ===== Rates ===== */
.rates summary {
  margin-bottom: 0;
  cursor: pointer;
  list-style: none;
}

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

.rates summary::after {
  content: "▾";
  color: var(--text-muted);
  transition: transform var(--dur) var(--ease);
}

.rates[open] summary {
  margin-bottom: var(--space-3);
}

.rates[open] summary::after {
  transform: rotate(180deg);
}

.rates summary h2 {
  margin-right: auto;
}

.rates-form {
  display: grid;
  gap: var(--space-4);
  margin: var(--space-3) 0;
}

.rate-group h3 {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
  font-size: var(--text-sm);
}

.rate-group h3 small {
  color: var(--text-muted);
  font-weight: 500;
}

.rate-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2);
}

.rate-field {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.rate-field span {
  color: var(--text-2);
  font-size: var(--text-xs);
  line-height: 1.3;
}

.rate-field .with-unit {
  position: relative;
}

.rate-field input {
  min-height: 34px;
  padding-right: 32px;
  font-family: var(--font-mono);
  font-size: var(--text-sm);
}

.rate-field .unit {
  position: absolute;
  right: var(--space-2);
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: var(--text-xs);
  pointer-events: none;
}

.rate-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  grid-column: 1 / -1;
  font-size: var(--text-xs);
  color: var(--text-2);
}

.rate-toggle input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

/* ===== Alert ===== */
.alert {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: var(--text-sm);
  font-weight: 500;
}

.alert-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface-2);
  font-weight: 700;
}

.alert.ok {
  background: var(--good-soft);
  border-color: transparent;
  color: var(--good);
}

.alert.warn {
  background: var(--warning-soft);
  border-color: transparent;
  color: var(--warning);
}

.alert.bad {
  background: var(--critical-soft);
  border-color: transparent;
  color: var(--critical);
}

/* ===== Insights ===== */
.insights {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: var(--space-3);
}

.tile {
  display: grid;
  gap: 2px;
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  min-width: 0;
}

.tile-label {
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.tile-value {
  font-size: var(--text-lg);
  font-weight: 600;
  overflow-wrap: anywhere;
}

.tile-sub {
  color: var(--text-2);
  font-size: var(--text-xs);
}

/* ===== Channel cards ===== */
.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-4);
  margin-bottom: var(--space-3);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-4);
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--text-2);
  font-size: var(--text-xs);
}

.legend li {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
}

.s-base {
  background: var(--series-base);
}

.s-fees {
  background: var(--series-fees);
}

.s-tax {
  background: var(--series-tax);
}

.s-profit {
  background: var(--series-profit);
}

.channels {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
  gap: var(--space-3);
}

.channel {
  display: grid;
  gap: var(--space-3);
  align-content: start;
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  min-width: 0;
}

.channel.is-best {
  border-color: var(--good);
  box-shadow: 0 0 0 1px var(--good);
}

.channel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-2);
}

.channel-head h3 {
  font-size: var(--text-md);
  font-weight: 700;
}

.fee-label {
  flex: none;
  white-space: nowrap;
  color: var(--text-2);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
}

.channel-title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1) var(--space-2);
  min-width: 0;
}

.badge {
  padding: 1px var(--space-2);
  border-radius: var(--radius-pill);
  font-size: var(--text-xs);
  font-weight: 600;
}

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

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

.price-block {
  display: grid;
  gap: var(--space-1);
}

.price-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.price-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.price-row .currency {
  color: var(--text-muted);
  font-weight: 600;
}

.price-input {
  flex: 1;
  min-width: 0;
  min-height: 44px !important;
  font-size: var(--text-xl) !important;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.price-actions {
  display: flex;
  gap: var(--space-1);
}

.sale-line {
  color: var(--text-2);
  font-size: var(--text-sm);
}

.sale-line s {
  color: var(--text-muted);
}

.metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2);
  margin: 0;
}

.metric {
  padding: var(--space-2) var(--space-3);
  background: var(--surface-2);
  border-radius: var(--radius-md);
  min-width: 0;
}

.metric dt {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.metric dd {
  margin: 0;
}

.metric-value {
  font-size: var(--text-md);
  font-weight: 600;
}

.metric-value.neg {
  color: var(--critical);
}

.status {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  font-weight: 600;
}

.status.ok {
  color: var(--good);
}

.status.below-target {
  color: var(--warning);
}

.status.loss,
.status.infeasible {
  color: var(--critical);
}

/* Barra de composição: segmentos com 2px de vão, pontas arredondadas */
.bar {
  display: flex;
  gap: 2px;
  height: 14px;
}

.bar span {
  min-width: 2px;
  height: 100%;
  cursor: default;
}

.bar span:first-child {
  border-radius: 4px 0 0 4px;
}

.bar span:last-child {
  border-radius: 0 4px 4px 0;
}

.bar span:only-child {
  border-radius: 4px;
}

.bar .seg-base {
  background: var(--series-base);
}

.bar .seg-fees {
  background: var(--series-fees);
}

.bar .seg-tax {
  background: var(--series-tax);
}

.bar .seg-profit {
  background: var(--series-profit);
}

.bar span:hover,
.bar span:focus-visible {
  filter: brightness(1.12);
}

/* ===== Table ===== */
.table-card {
  padding-bottom: var(--space-2);
}

.table-wrap {
  overflow-x: auto;
  margin: 0 calc(-1 * var(--space-4));
  padding: 0 var(--space-4);
}

.compare {
  width: 100%;
  min-width: 640px;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.compare th,
.compare td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
  text-align: right;
  white-space: nowrap;
}

.compare th:first-child,
.compare td:first-child {
  text-align: left;
  font-family: var(--font-sans);
  font-weight: 600;
}

.compare thead th {
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.compare tbody tr:last-child td {
  border-bottom: 0;
}

.compare .neg {
  color: var(--critical);
}

.compare .cash {
  font-weight: 700;
}

/* ===== Tooltip, toast, footer ===== */
.tooltip {
  position: fixed;
  z-index: 20;
  max-width: 240px;
  padding: var(--space-2) var(--space-3);
  background: var(--text);
  color: var(--surface);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  pointer-events: none;
  box-shadow: var(--shadow-md);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: var(--space-5);
  z-index: 30;
  padding: var(--space-2) var(--space-4);
  background: var(--text);
  color: var(--surface);
  border-radius: var(--radius-pill);
  font-size: var(--text-sm);
  font-weight: 500;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  transition:
    opacity var(--dur) var(--ease),
    transform var(--dur) var(--ease);
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.copy-short {
  display: none;
}

.footer {
  padding: var(--space-5);
  color: var(--text-muted);
  font-size: var(--text-xs);
  text-align: center;
}

/* ===== Até tablet: uma coluna; taxas vão para o fim, depois dos resultados ===== */
@media (max-width: 1023px) {
  .sidebar {
    display: contents;
  }

  .results {
    order: 2;
  }

  #rates-panel {
    order: 3;
  }
}

/* ===== Telas pequenas ===== */
@media (max-width: 640px) {
  .topbar {
    padding: var(--space-3) var(--space-4);
  }

  .brand-sub {
    display: none;
  }

  .brand-title {
    font-size: var(--text-md);
  }

  .layout {
    padding: var(--space-4);
    gap: var(--space-4);
  }

  #copy-all {
    padding: 0 var(--space-3);
  }

  .copy-long {
    display: none;
  }

  .copy-short {
    display: inline;
  }

  .insights {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .tile {
    padding: var(--space-3);
  }
}

@media (max-width: 380px) {
  .row-2,
  .rate-fields {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0ms !important;
  }
}

@media (forced-colors: active) {
  .bar span {
    forced-color-adjust: none;
    border: 1px solid CanvasText;
  }
}
