:root, :host {
  --ink: #1f2937;
  --muted: #4b5563;
  --purple: #6431f6;
  --secondary: #7345f7;
  --line: #e3e6ed;
  --wash: #f6ebfe;
  --results: #eff3f7;
  --gutter: 17px;
  font: 16px/1.5 Poppins, Arial, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
:host { display: block; }
[hidden] { display: none !important; }
body, h2, h3, p { margin: 0; }
/* Once the host handles sizing, keep touch scrolling on the host page. */
html.embedded-autosize, .embedded-autosize body { overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
button, summary, select { cursor: pointer; }
:focus-visible { outline: 3px solid var(--secondary); outline-offset: 3px; }

.calculator {
  max-width: 1040px;
  margin: 12px auto;
  width: calc(100% - 24px);
  background: white;
  border-radius: 14px;
  box-shadow: 0 3px 18px #1f29371c;
}

.workspace, .results { display: grid; min-width: 0; }
.results { padding: var(--gutter); background: var(--results); border-radius: 14px 14px 0 0; }
.panel { min-width: 0; }
.panel + .panel { margin-top: 21px; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 6px 12px; }
h2 { font-size: 1.12rem; font-weight: 600; letter-spacing: -.025em; }
h3 { font-size: .90rem; font-weight: 600; letter-spacing: -.018em; }
.inputs { display: grid; gap: 16px; align-content: start; padding: var(--gutter); min-width: 0; }
label, legend { display: block; font-size: .82rem; font-weight: 600; }
label:not(.check), legend { margin-bottom: 6px; }
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
.segmented {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border-radius: 10px;
  background: #e9ebf1;
}
.segmented label:not(.check) { position: relative; margin: 0; cursor: pointer; }
.segmented input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; margin: 0; }
.segmented span {
  display: grid;
  place-items: center;
  height: 100%;
  min-height: 44px;
  padding: 4px;
  text-align: center;
  border: 1px solid transparent;
  border-radius: 7px;
  line-height: 1.22;
  font-size: .88rem;
  color: var(--muted);
}
.segmented input:checked + span { background: white; border-color: #d8dbe7; color: var(--purple); box-shadow: 0 1px 5px #1f293714; }
.segmented input:focus-visible + span { outline: 3px solid var(--secondary); outline-offset: 1px; }

.field, select {
  min-height: 46px;
  width: 100%;
  border: 1px solid #cfd5df;
  border-radius: 8px;
  background: white;
}
.field { display: flex; align-items: center; padding: 0 11px; gap: 8px; }
.field input { width: 100%; min-width: 0; padding: 10px 0; border: 0; background: transparent; outline: 0; font-variant-numeric: tabular-nums; }
.field input::placeholder { color: #6b7280; }
.field:focus-within { outline: 2px solid var(--secondary); outline-offset: 1px; }
.field > span, .help { color: var(--muted); font-size: .79rem; }
.help { margin-top: 4px; }
select { padding: 8px 10px; }
.pair { display: grid; align-items: end; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
input[type="range"] {
  display: block;
  width: 100%;
  height: 28px;
  margin: 4px 0 -4px;
  accent-color: var(--purple);
  cursor: pointer;
}
input[type="range"]:disabled { accent-color: #9ca3af; cursor: default; }
.financing { margin: 0; padding: 9px 11px; border-radius: 8px; background: var(--wash); font-size: .8rem; }
.financing > div, .mi-rate { display: flex; justify-content: space-between; align-items: baseline; gap: 4px 9px; flex-wrap: wrap; }
.financing > div + div { margin-top: 5px; font-weight: 600; }
.financing dd { margin: 0; font-variant-numeric: tabular-nums; }
.mi-rate output { font-size: .82rem; font-weight: 600; }
details { border-top: 1px solid var(--line); }
summary { padding: 13px 0 0; min-height: 44px; color: var(--purple); font-size: .85rem; font-weight: 600; }
.escrow-fields { display: grid; gap: 12px; padding: 4px 0; }
.check { display: flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; }
.check input { accent-color: var(--purple); width: 18px; height: 18px; margin: 0; }

.funding-hero {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  padding: 17px;
  border-radius: 10px;
  background: linear-gradient(110deg, #322075, #4d24bc 72%, #5627c7);
  color: white;
}
.funding-hero::after {
  content: "";
  position: absolute;
  z-index: -1;
  top: -142px;
  right: -89px;
  width: 220px;
  height: 220px;
  border: 1px solid #ffffff26;
  border-radius: 50%;
  box-shadow: 0 0 0 20px #ffffff0f, 0 0 0 40px #ffffff0c, 0 0 0 60px #ffffff08;
}
.funding h2 { color: #f4edff; font-size: 1.04rem; }
.cost {
  margin: 3px 0 4px;
  color: white;
  font-size: clamp(2.25rem, 8vw, 3.35rem);
  letter-spacing: -.055em;
  line-height: 1.15;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.cost-note { color: #f4edff; font-size: .82rem; }
.credit-share { color: var(--purple); font-weight: 600; }
.credit-balance {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 2px 10px;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid #ffffff40;
  color: #f4edff;
  font-size: .82rem;
}
.credit-balance strong { color: #fff; font-variant-numeric: tabular-nums; }
.comparison { margin-top: 15px; padding-top: 13px; }
.scenarios { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; margin-top: 10px; }
.scenarios button {
  position: relative;
  display: grid;
  gap: 3px;
  align-content: start;
  min-height: 79px;
  padding: 8px 3px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: white;
  text-align: center;
  overflow-wrap: anywhere;
}
.scenarios span, .scenarios small { color: var(--muted); font-size: .71rem; }
.scenarios strong { font-size: 1.04rem; line-height: 1.25; font-weight: 600; letter-spacing: -.035em; }
.scenarios button[aria-pressed="true"] { border-color: var(--purple); background: var(--wash); color: var(--purple); }
.scenarios button[aria-pressed="true"] span { color: var(--purple); font-weight: 600; }
.scenarios button:hover:not(:disabled) { border-color: var(--purple); background: var(--wash); }
.scenarios button:disabled { cursor: default; }

#type-comparison { margin-top: 12px; }
#type-comparison summary { min-height: 39px; }
#type-comparison caption { text-align: left; color: var(--muted); padding-bottom: 5px; font-size: .76rem; }
.selected-type { background: var(--wash); color: #452394; }

.rate-label { font-size: .74rem; font-weight: 600; color: var(--purple); background: var(--wash); border-radius: 20px; padding: 5px 9px; }
.empty-payment {
  margin-top: 14px;
  padding: 22px 12px;
  text-align: center;
  background: #f8fafc;
  border: 1px dashed #cdd5e1;
  border-radius: 10px;
  color: var(--muted);
  font-size: .83rem;
}
.payment-chart {
  --stack-height: 124px;
  margin: 8px 0 5px;
  padding: 4px 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-variant-numeric: tabular-nums;
}
.payment-key {
  display: flex;
  justify-content: center;
  gap: clamp(4px, 1.7vw, 9px);
  margin-top: 1px;
  white-space: nowrap;
  color: var(--muted);
  font-size: .67rem;
}
.payment-key abbr { display: inline-flex; align-items: center; gap: 3px; text-decoration: none; }
.payment-key abbr::before, .breakdown-component dt::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 2px;
  background: var(--component-fill);
}
.payment-principal { --component-fill: #7345f733; --component-active: #9874fa; }
.payment-mi { --component-fill: #29b68b; --component-active: #11936d; }
.payment-taxes { --component-fill: #43a4e0; --component-active: #237fbb; }
.payment-insurance { --component-fill: #f08da9; --component-active: #dc5b86; }
.payment-subsidy {
  --component-fill: repeating-linear-gradient(135deg, #f7c744 0 5px, #ffe58c 5px 8px);
  --component-active: #edac12;
}

.payment-plot { position: relative; }
.payment-columns {
  position: relative;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: 4px;
  align-items: start;
}
.payment-columns::before {
  content: "";
  position: absolute;
  inset: 1.4rem 0 auto;
  height: var(--stack-height);
  background: repeating-linear-gradient(to bottom, #e9e6f1 0 1px, transparent 1px 25%);
  border-bottom: 1px solid #e9e6f1;
  pointer-events: none;
}
.payment-column {
  display: grid;
  justify-items: center;
  gap: 1px;
  min-width: 0;
  padding: 0 1px 1px;
  border: 0;
  text-align: center;
}
.payment-stack {
  display: flex;
  flex-direction: column-reverse;
  width: min(78%, 76px);
  height: var(--stack-height);
  margin-bottom: 2px;
}
.payment-slice {
  position: relative;
  width: 100%;
  flex-basis: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  background: var(--component-fill);
  transition: transform .15s ease, box-shadow .15s ease;
}
.payment-slice:last-child { border-radius: 4px 4px 0 0; }
.payment-slice + .payment-slice { box-shadow: inset 0 -1px #ffffffb3; }
.payment-slice[aria-expanded="true"] {
  z-index: 1;
  transform: scaleX(1.09);
  background: var(--component-active);
  box-shadow: 0 1px 5px #1f293749;
}
.payment-slice:focus-visible { outline-offset: 1px; }
@media (prefers-reduced-motion: reduce) {
  .payment-slice { transition: none; }
}
.payment-period {
  color: var(--muted);
  font-size: .67rem;
  overflow-wrap: anywhere;
}
.payment-amount {
  min-height: 1.4rem;
  color: var(--purple);
  font-size: clamp(.67rem, 2.6vw, .85rem);
  overflow-wrap: anywhere;
}
.payment-breakdown {
  position: absolute;
  z-index: 2;
  bottom: 100%;
  width: min(100%, 208px);
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  box-shadow: 0 4px 12px #1f293725;
  font-size: .71rem;
}
.payment-breakdown h3 { padding-inline: 4px; font-size: .73rem; }
.payment-breakdown.breakdown-below { top: 100%; bottom: auto; }
.payment-breakdown dl { margin: 2px 0 0; }
.payment-breakdown dl > div { display: flex; justify-content: space-between; gap: 6px; padding: 1px 4px; }
.payment-breakdown dd { margin: 0; }
.breakdown-component dt::before { margin-right: 4px; }
.breakdown-selected { border-radius: 3px; background: var(--wash); font-weight: 600; }
.payment-breakdown .breakdown-total { margin-top: 2px; border-top: 1px solid var(--line); font-weight: 600; }

.ipc { margin: 8px 0; padding: 0 2px; }
.ipc summary { min-height: 40px; padding-top: 9px; }
.ipc-property { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 7px; margin-bottom: 6px; }
.ipc-property label { margin: 0; font-size: .72rem; }
.ipc-property select { width: auto; min-height: 30px; padding: 3px 5px; font-size: .74rem; }
.ipc-limit { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; padding-bottom: 6px; font-size: .76rem; }
.ipc-limit strong { color: var(--purple); font-weight: 600; }
.ipc-warning {
  width: fit-content;
  margin-top: 8px;
  padding: 5px 8px;
  border-radius: 5px;
  background: #fff2c9;
  color: #563500;
  font-size: .74rem;
  font-weight: 600;
}

table { width: 100%; border-collapse: collapse; text-align: right; font-variant-numeric: tabular-nums; font-size: .78rem; }
th, td { padding: 8px 5px; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
th:first-child { text-align: left; }
th { font-weight: 600; }
thead { color: var(--muted); }
thead th { font-size: .72rem; }
#payments tr:last-child { background: var(--wash); color: var(--purple); }
#payments tr:last-child td:nth-child(2) { font-weight: 600; }
tbody small { display: block; font-size: .68rem; color: var(--muted); font-weight: 400; }
@media (min-width: 730px) {
  :root { --gutter: 25px; }
  .workspace { grid-template-columns: minmax(0, .46fr) minmax(0, .54fr); }
  .results { grid-area: 1 / 2; border-left: 1px solid var(--line); border-radius: 0 14px 14px 0; align-content: start; }
  .inputs { grid-area: 1 / 1; gap: 16px; }
  .scenarios { gap: 8px; }
  .scenarios button { padding: 8px 5px; }
  .payment-chart { --stack-height: 140px; }
  th, td { padding-inline: 8px; }
}
