/*
 * The calculators: trioni/price-calculator, trioni/granalla-calculator, trioni/pawn-calculator and
 * trioni/custody-calculator. Each one is a compact cream «boleta de tasación» with a double gold
 * rule: title + controls + figure + one button + one legal line. Loaded only when one renders.
 * Flat --calc-surface (a gradient would drop --muted below 4.5:1); text in ink, figures in
 * --calc-figure (4.86:1), control borders --field-border (3.16:1).
 */
.calc {
  --focus: var(--ink);
  position: relative;
  display: grid;
  gap: var(--calc-gap);
  padding: var(--calc-pad);
  border: 1px solid var(--calc-frame);
  border-radius: 8px;
  background: var(--calc-surface);
  color: var(--text);
  font-size: var(--fs-base);
  line-height: var(--lh-body);
  box-shadow: 0 28px 52px -34px rgba(122, 102, 64, 0.55);
}

/* Inner gold rule: the second line of the frame. */
.calc::before {
  content: '';
  position: absolute;
  inset: 6px;
  border: 1px solid var(--calc-rule);
  border-radius: 5px;
  pointer-events: none;
}

.calc > * {
  position: relative;
}

/* Author display rules (grid, flex) would otherwise beat the hidden attribute. */
.calc [hidden] {
  display: none !important;
}

/* Title on the left; the Oro/Plata or Por valor/Por peso switch, when it lives here, on the right. */
.calc__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2) var(--s-3);
}

/* A calculator is title + controls + figure + one button + one legal line: no kicker, no seal. */
.calc__kicker {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.calc__seal {
  display: none;
}

/* Beats .prose h3 on content pages. */
.calc .calc__head :is(h2, h3) {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: var(--fs-xl);
  font-weight: 400;
  line-height: 1.15;
}

.calc__stale:has(> [hidden]) {
  display: none;
}

.calc fieldset {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

/* ---------- Choices: metal / mode pill and karat chips ---------- */
/* Segmented switch: each option is a 44 px target. */
.calc__metal {
  display: grid;
  grid-template-columns: repeat(2, minmax(72px, 1fr));
  border: 1px solid var(--field-border);
  border-radius: 999px;
  background: var(--calc-field);
}

.calc__head > .calc__metal {
  flex: none;
}

.calc__metal label {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 44px;
  padding-inline: var(--s-3);
  border-radius: 999px;
  color: var(--text-soft);
  font-size: var(--fs-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
}

.calc__metal label:hover {
  color: var(--ink);
}

.calc__metal input,
.karat-chip input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

/* The gold fill alone is 2.59:1 on --calc-field: the --gold-ui edge keeps the state at 3.73:1. */
.calc__metal label:has(input:checked) {
  background: var(--gold);
  box-shadow: inset 0 0 0 1px var(--gold-ui);
  color: var(--ink);
}

.calc__metal label:has(input:focus-visible),
.karat-chip:has(input:focus-visible) {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.calc__karats legend,
.calc__weight > label,
.calc__months > label {
  margin-bottom: 6px;
  color: var(--text-soft);
  font-size: var(--fs-sm);
  font-weight: 600;
}

/* Every karat on one row, whatever the count (five gold, two silver). */
.calc__chips {
  display: grid;
  grid-auto-columns: minmax(0, 1fr);
  grid-auto-flow: column;
  gap: 6px;
}

.karat-chip {
  position: relative;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 0;
  min-height: 44px;
  padding: var(--s-1);
  border: 1px solid var(--field-border);
  border-radius: 6px;
  color: var(--ink);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}

.karat-chip:hover {
  border-color: var(--gold-ui);
  transform: translateY(-1px);
}

/* 2px border: the padding gives back the extra pixel so the chip does not grow. */
.karat-chip:has(input:checked) {
  padding: calc(var(--s-1) - 1px);
  border: 2px solid var(--gold-ui);
  background: var(--calc-field);
}

.karat-chip__mark {
  font-family: var(--font-serif);
  font-size: 1.125rem;
  line-height: 1.1;
  font-variant-numeric: lining-nums;
}

.karat-chip__label {
  color: var(--muted);
  font-size: var(--fs-2xs);
  font-weight: 600;
  line-height: 1.2;
}

.karat-chip:has(input:checked) .karat-chip__label {
  color: var(--calc-figure);
}

.calc__hint {
  margin-top: 6px;
  color: var(--muted);
  font-size: var(--fs-xs);
}

/* ---------- Fields: weight, value, months ---------- */
/*
 * «Peso» and, on the same 44 px line to its right, the folded «¿No sabes cuánto pesa?»
 * (details.calc__more). The details spans the row so its presets open under that line;
 * only its own controls take the pointer, so the label underneath stays clickable.
 */
.calc__weight,
.calc__months {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

.calc__weight > *,
.calc__months > * {
  grid-column: 1 / -1;
}

.calc__weight:has(> .calc__more) > label:first-child {
  display: flex;
  grid-row: 1;
  grid-column: 1;
  align-items: center;
  align-self: start;
  min-height: 44px;
  margin-bottom: 0;
}

.calc__weight + .calc__weight {
  margin-top: var(--s-3);
}

.calc__weight > .calc__metal {
  justify-self: start;
  margin-bottom: var(--s-2);
}

/* The weight changes with − / + and the keyboard: no slider (the grain calculator hides its own). */
.calc__weight > .calc__range {
  display: none;
}

.calc__weight-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  gap: var(--s-2);
}

/* Without JS the steppers stay hidden: the field takes the whole row. */
.calc__weight-row:has(> .stepper[hidden]) {
  grid-template-columns: minmax(0, 1fr);
}

.stepper {
  min-height: 44px;
  border: 1px solid var(--field-border);
  border-radius: 6px;
  background: var(--calc-field);
  color: var(--ink);
  font-size: var(--fs-lg);
  transition: border-color var(--dur-fast) var(--ease);
}

.stepper:hover {
  border-color: var(--gold-ui);
}

.calc .field__control {
  display: flex;
  align-items: center;
  min-height: 44px;
  border: 1px solid var(--field-border);
  border-radius: 6px;
  background: var(--calc-field);
  transition: border-color var(--dur-fast) var(--ease);
}

.calc .field__control:hover {
  border-color: var(--gold-ui);
}

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

.calc .field__control input {
  width: 100%;
  min-width: 0;
  height: 42px;
  padding: 0 var(--s-3);
  border: 0;
  background: transparent;
  color: var(--ink);
  font-size: var(--fs-lg);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.calc .field__control input:focus {
  outline: none;
}

.calc .field__suffix {
  padding-right: var(--s-3);
  color: var(--muted);
}

.calc__range {
  width: 100%;
  min-height: 24px; /* WCAG 2.5.8: the native slider is 16 px tall */
  margin: var(--s-3) 0 0;
  accent-color: var(--gold-ui);
}

.calc__months-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: var(--s-3);
  align-items: center;
}

.calc__months-row .calc__range {
  margin: 0;
}

.calc__months-value {
  min-width: 6.5ch;
  color: var(--ink);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* Empty until there is an error: it takes no room, but the live region stays rendered. */
.calc .field__error {
  color: var(--closed);
  font-size: var(--fs-xs);
  font-weight: 600;
}

.calc .field__error:not(:empty) {
  margin-top: var(--s-1);
}

[data-calc-grams-field].is-invalid .field__control,
.calc .is-invalid .field__control,
.calc.is-invalid .field__control,
.calc .field__control:has(input[aria-invalid="true"]) {
  border-color: var(--closed);
}

/* Invalid input: the total, formula and hint still show the last valid quantity. */
.calc.is-invalid .calc__result {
  opacity: 0.4;
}

/* ---------- «¿No sabes cuánto pesa?»: folded weight presets ---------- */
.calc .calc__more {
  grid-row: 1;
  grid-column: 1 / -1;
  pointer-events: none;
}

.calc__more > * {
  pointer-events: auto;
}

.calc__more > summary {
  display: flex;
  align-items: center;
  width: max-content;
  max-width: 100%;
  min-height: 44px;
  margin-left: auto;
  color: var(--text-soft);
  font-size: var(--fs-xs);
  text-decoration: underline dotted var(--gold-ui);
  text-underline-offset: 4px;
  cursor: pointer;
  list-style: none;
}

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

.calc__more > summary::after {
  content: '';
  flex: none;
  width: 6px;
  height: 6px;
  margin-left: var(--s-2);
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg);
  transition: transform var(--dur-fast) var(--ease);
}

.calc__more[open] > summary::after {
  transform: rotate(-135deg);
}

.calc__more > summary:hover {
  color: var(--ink);
}

.calc__more > summary:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.calc__more .calc__presets {
  margin: 0 0 var(--s-2);
}

.calc__presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: var(--s-2);
}

.calc__presets button {
  min-height: 44px;
  padding: 0 var(--s-3);
  border: 1px solid var(--field-border);
  border-radius: 999px;
  background: transparent;
  color: var(--text-soft);
  font-size: var(--fs-xs);
  transition: border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease);
}

.calc__presets button:hover,
.calc__presets button[aria-pressed="true"] {
  border-color: var(--gold-ui);
  background: var(--calc-field);
  color: var(--ink);
}

/* ---------- Result: label and formula on the left, the figure on the right ---------- */
.calc__result {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 2px var(--s-3);
  padding-top: var(--s-3);
  border-top: 1px dashed var(--line-strong);
}

.calc__result-label {
  grid-row: 1;
  grid-column: 1;
  color: var(--muted);
  font-size: var(--fs-sm);
}

.calc__output {
  grid-row: 1 / span 2;
  grid-column: 2;
  align-self: center;
  color: var(--calc-figure);
  font-family: var(--font-serif);
  font-size: var(--fs-figure);
  line-height: 1.05;
  font-variant-numeric: tabular-nums lining-nums;
  letter-spacing: -0.01em;
  text-align: right;
  white-space: nowrap;
}

.calc__formula {
  grid-row: 2;
  grid-column: 1;
  align-self: start;
  color: var(--text-soft);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
}

.calc__lines {
  display: grid;
  grid-column: 1 / -1;
  gap: var(--s-1);
  margin-top: var(--s-2);
}

.calc__lines > div {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 2px var(--s-3);
  font-size: var(--fs-sm);
  line-height: 1.45;
}

.calc__lines dt {
  color: var(--text-soft);
}

.calc__lines dd {
  margin-left: auto;
  color: var(--ink);
  font-weight: 600;
  font-variant-numeric: tabular-nums lining-nums;
  white-space: nowrap;
}

.calc__lines .calc__line--strong {
  padding-top: var(--s-1);
  border-top: 1px solid var(--line-strong);
}

.calc__line--strong dt {
  color: var(--ink);
  font-weight: 600;
}

.calc__line--strong dd {
  color: var(--calc-figure);
  font-size: var(--fs-base);
}

.calc .btn--gold {
  border-color: var(--gold-ui);
}

.calc__fine,
.calc__terms {
  color: var(--muted);
  font-size: var(--fs-2xs);
  line-height: 1.45;
}

/* Terms and fine print read as one legal block under the button. */
.calc__terms + .calc__fine {
  margin-top: calc(var(--s-1) - var(--calc-gap));
}

.calc__paused {
  color: var(--ink);
}

/* ---------- Pawn calculator (trioni/pawn-calculator): «Te damos» and «Devuelves», same weight ---------- */
/* Side by side while both amounts fit; a long amount wraps the second figure under the first. */
.calc__pawn {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-3) var(--s-4);
  padding-top: var(--s-3);
  border-top: 1px dashed var(--line-strong);
}

/* Label on top, amount at the bottom: the two amounts stay aligned when one label wraps. */
.calc__pawn-figure {
  display: grid;
  grid-template-rows: 1fr auto;
  flex: 1 1 0;
  gap: 2px;
  min-width: max-content;
  margin: 0;
}

/* Width 0 / min-width 100 %: the label wraps instead of widening its figure (only amounts decide). */
.calc__pawn-label {
  width: 0;
  min-width: 100%;
  color: var(--text-soft);
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: 1.3;
}

.calc__pawn-label small {
  color: var(--muted);
  font-size: var(--fs-xs);
  font-weight: 400;
}

.calc__pawn-amount {
  color: var(--calc-figure);
  font-family: var(--font-serif);
  font-size: var(--fs-figure);
  line-height: 1.05;
  font-variant-numeric: tabular-nums lining-nums;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.calc__note {
  flex-basis: 100%;
  margin: 0;
  color: var(--muted);
  font-size: var(--fs-xs);
}

.calc__note:empty {
  display: none;
}

/* The WhatsApp button stays discreet: an outlined 44 px button, not a full-width gold bar. */
.calc .calc__pawn-cta {
  justify-self: start;
  min-height: 44px;
  padding: 0 var(--s-4);
  font-size: var(--fs-sm);
}

/* The page's representative example right under the panel reads as its fine print (muted 4.99:1). */
.calc--pawn + p {
  margin-top: var(--s-2);
  color: var(--muted);
  line-height: 1.45;
}

/* ---------- Grain calculator (trioni/granalla-calculator): buy by the gram ---------- */
.calc--buy {
  --focus: var(--ink);
}

.calc__tiers {
  display: grid;
  gap: var(--s-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.calc__tier {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--s-1) var(--s-3);
  padding: var(--s-2) var(--s-3);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  color: var(--text-soft);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  transition: border-color var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease);
}

.calc__tier > span:last-child {
  white-space: nowrap;
}

.calc__tier[aria-current="true"] {
  border-color: var(--gold-ui);
  background: var(--calc-field);
  color: var(--ink);
}

.calc__tier-now {
  color: var(--calc-figure);
  font-weight: 600;
}

.calc__saving {
  min-height: 1.5em;
  color: var(--muted);
  font-size: var(--fs-xs);
}

.calc__pro {
  color: var(--muted);
  font-size: var(--fs-2xs);
  line-height: 1.45;
}

/* Content pages: the calculator keeps a form width instead of the text measure. */
.page-content .tg-section > .calc {
  max-width: 36rem;
}

/* ---------- Responsive ---------- */
@media (min-width: 560px) {
  .calc {
    padding: var(--calc-pad-wide);
  }
}

@media (max-width: 559px) {
  .calc .calc__head :is(h2, h3) {
    font-size: 1.375rem;
  }

  .calc__output {
    font-size: 2.125rem;
  }
}

/* 320 px phones: the five gold karats still fit on one row; the figure goes under its label. */
@media (max-width: 359px) {
  .calc__chips {
    grid-auto-columns: minmax(44px, 1fr);
    gap: var(--s-1);
  }

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

  .calc__output {
    grid-row: 2;
    grid-column: 1;
    text-align: left;
  }

  .calc__formula {
    grid-row: 3;
  }
}
