/*
 * Vehicle lease worksheet — high-trust consumer fintech visual language
 * Cool neutrals, emerald accent, stacked cards, clear numeric hierarchy
 */
/* --- Tokens: palette, radii, shadows, UI + mono font stacks --- */
:root {
  --bg: #f4f4f5;
  --surface: #ffffff;
  --line: #e4e4e7;
  --line-strong: #d4d4d8;
  --text: #18181b;
  --text-secondary: #52525b;
  --text-tertiary: #71717a;
  --accent: #059669;
  --accent-hover: #047857;
  --accent-muted: #d1fae5;
  --accent-ring: rgba(5, 150, 105, 0.22);
  --field-fill: #ffffff;
  --readonly-fill: #e6e8ee;
  --readonly-border: #c5cad4;
  --danger: #dc2626;
  --warn-bg: #fffbeb;
  --warn-border: #fde68a;
  --warn-text: #92400e;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-card: 0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 12px rgba(15, 23, 42, 0.04);
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, monospace;
}

/* --- Global box model --- */
* {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
}

/* --- Page shell (<1024px scrolls; desktop locks body — see media query) --- */
body {
  min-width: 320px;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
  overflow-y: auto;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Main block vertical rhythm */
main.lease-calculator {
  min-height: 100vh;
  padding: clamp(1rem, 4vw, 2.5rem) 0 2rem;
}

/* Layout wrapper (no chrome; cards are .row-section form) */
.border {
  margin: 0;
  border: none;
  border-radius: 0;
}

/* Content width: capped on small/medium; full bleed in desktop grid */
.calc-content {
  flex: 1 1 auto;
  width: 100%;
  max-width: 38rem;
  margin: 0 auto;
  padding: 0 1rem;
}

/* --- Header: title + subtitle + reset (reset moves to row on ≥1024px) --- */
.heading-bar {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.75rem;
  margin: 0 0 1.75rem;
  padding: 0 0.125rem;
  text-align: left;
  background: none;
  border: none;
}

.heading-lead {
  flex: 1;
  min-width: 0;
}

.heading-lead::after {
  content: '';
  display: block;
  width: 2.75rem;
  height: 3px;
  margin-top: 1rem;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent) 0%, #34d399 100%);
}

.heading-title {
  margin: 0;
  font-size: clamp(1.5rem, 5vw, 1.875rem);
  font-weight: 700;
  letter-spacing: -0.035em;
  color: var(--text);
  line-height: 1.15;
}

.heading-sub {
  margin: 0.375rem 0 0;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-tertiary);
  letter-spacing: -0.01em;
}

/* <1024px: stack the three worksheet columns; page scrolls */
.row-section {
  display: flex;
  flex-direction: column;
  gap: 0.875rem;
  padding: 0;
}

/* One “card” per column / stacked section */
.row-section form {
  width: 100%;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

/* Desktop: fit worksheet in one viewport (no body scroll).
 * Columns 1–2 can overflow vertically (two fieldsets each); per-form overflow-y
 * keeps bottom fields (e.g. Capitalized Cost, Adjusted Residual) reachable. */
@media (min-width: 1024px) {
  html {
    height: 100%;
  }

  body {
    height: 100%;
    overflow: hidden;
  }

  main.lease-calculator {
    height: 100vh;
    max-height: 100vh;
    min-height: 0;
    padding: 0.3rem 0.55rem 0.35rem;
    box-sizing: border-box;
    overflow: hidden;
  }

  .calc-content {
    max-width: none;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 0 0.35rem;
    box-sizing: border-box;
  }

  .border {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .heading-bar {
    flex-direction: row;
    align-items: flex-end;
    justify-content: space-between;
    gap: 0.65rem;
    flex-shrink: 0;
    margin-bottom: 0.25rem;
    padding: 0 0.15rem;
  }

  .heading-lead::after {
    margin-top: 0.35rem;
    height: 2px;
    width: 2rem;
  }

  .heading-title {
    font-size: 1.1rem;
    letter-spacing: -0.03em;
  }

  .heading-sub {
    margin-top: 0.12rem;
    font-size: 0.68rem;
  }

  /* Compact reset: leaves more height for the three-column grid */
  .reset_lease_btn {
    flex-shrink: 0;
    align-self: flex-end;
    min-width: 0;
    padding: 0.28rem 0.62rem;
    font-size: 0.6875rem;
    line-height: 1.2;
    border-radius: 6px;
  }

  /* Options | Cap & residual | Payment — equal-height columns */
  .row-section {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: 1fr;
    gap: 0.45rem;
    align-items: stretch;
    overflow: hidden;
  }

  .row-section br {
    display: none;
  }

  /* Tall cards need an internal scrollbar; overflow hidden clipped last rows. */
  .row-section form {
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: #c4c4cc var(--surface);
  }

  .row-section form::-webkit-scrollbar {
    width: 5px;
  }

  .row-section form::-webkit-scrollbar-thumb {
    background: #c4c4cc;
    border-radius: 99px;
  }

  .row-section form fieldset {
    flex: 0 0 auto;
    padding: 0.4rem 0.45rem;
    border-bottom: 1px solid var(--line);
  }

  .row-section form fieldset.payment_label {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 0.25rem;
    padding: 0.45rem 0.4rem 0.45rem;
  }

  legend {
    margin-bottom: 0.35rem;
    font-size: 0.6rem;
    letter-spacing: 0.07em;
  }

  legend.legend-pillar {
    font-size: 0.62rem;
    padding: 0.3rem 0.55rem;
    margin-bottom: 0.4rem;
    border-radius: 6px;
  }

  .control {
    margin-bottom: 0.18rem;
  }

  .control label {
    width: 8.25rem;
    font-size: 0.72rem;
  }

  .variable,
  .dropdown {
    width: 7.35rem;
    height: 1.62rem;
    font-size: 0.72rem;
    padding-left: 6px;
    border-radius: 6px;
  }

  .variable {
    padding-right: 6px;
  }

  .tax {
    width: 3.55rem;
    height: 1.62rem;
    font-size: 0.72rem;
    padding-right: 6px;
    border-radius: 6px;
  }

  /* Readonly fields: inherit colors/borders from base rule; shrink type with inputs. */
  input[readonly] {
    font-size: 0.72rem;
    padding-left: 6px;
  }

  .payment {
    font-size: clamp(1.35rem, 2.6vh, 1.85rem);
    margin-top: 0.1rem;
    min-height: 1.85rem;
  }

  .payment_hint {
    font-size: 0.58rem;
    padding: 0.28rem 0.4rem;
    margin-top: 0.1rem;
  }

  fieldset.payment_label > legend.payment_label {
    margin-bottom: 0.2rem;
  }
}

/* Laptop-height: denser rows without changing breakpoints */
@media (min-width: 1024px) and (max-height: 760px) {
  .heading-title {
    font-size: 1.05rem;
  }

  .control {
    margin-bottom: 0.12rem;
  }

  .variable,
  .dropdown,
  .tax {
    height: 1.48rem;
  }

  legend {
    margin-bottom: 0.25rem;
  }

  .payment {
    font-size: clamp(1.15rem, 2.5vh, 1.55rem);
    min-height: 1.65rem;
  }
}

/* Small-height 1024px+ viewports: optional micro-scale on the sheet */
@media (min-width: 1024px) and (max-height: 640px) {
  main.lease-calculator {
    padding-top: 0.25rem;
    padding-bottom: 0.25rem;
  }

  .row-section {
    gap: 0.35rem;
  }

  .border {
    transform: scale(0.97);
    transform-origin: top center;
  }
}

/* Entry animation on .row-section.first-row (see index.html). */
.slide-up {
  opacity: 1;
  animation-name: slideUpOpacity;
  animation-iteration-count: 1;
  animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
  animation-duration: 0.55s;
}

@keyframes slideUpOpacity {
  0% {
    transform: translateY(10px);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

/* Secondary fade utility (available for future markup) */
.fade-in {
  opacity: 1;
  animation-name: fadeInOpacity;
  animation-iteration-count: 1;
  animation-timing-function: ease-in;
  animation-duration: 1.5s;
}

@keyframes fadeInOpacity {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

/* Taller stacked layout only when not in desktop grid */
@media (min-height: 850px) and (max-width: 1023px) {
  main.lease-calculator {
    padding-top: 2.5rem;
  }
}

/* --- Fieldsets: sections inside each <form> card; dividers between stacks --- */
fieldset {
  margin: 0;
  padding: 1.125rem 1.25rem;
  border: none;
  border-radius: 0;
  height: fit-content;
  background: transparent;
  border-bottom: 1px solid var(--line);
}

.row-section form fieldset:last-of-type {
  border-bottom: none;
}

legend {
  display: block;
  width: 100%;
  margin: 0 0 0.875rem;
  padding: 0;
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-tertiary);
}

/* Column headers: Options, Cap Reduction, Payment inputs */
legend.legend-pillar {
  width: auto;
  max-width: 100%;
  margin: 0 0 0.75rem;
  padding: 0.42rem 0.85rem;
  font-size: 0.8125rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent-hover);
  border: 1px solid rgba(5, 150, 105, 0.28);
  border-radius: var(--radius-sm);
  background: linear-gradient(180deg, rgba(5, 150, 105, 0.11) 0%, rgba(5, 150, 105, 0.04) 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

/* Form row: label + field (stacked on phone; inline on wider screens) */
.control {
  margin-bottom: 0.5rem;
}

.control:last-child {
  margin-bottom: 0;
}

.control label {
  width: 10rem;
  display: inline-block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-secondary);
  vertical-align: middle;
  letter-spacing: -0.01em;
}

label.label_total,
.control label.label_total {
  color: var(--text);
  font-weight: 600;
}

label {
  margin-bottom: unset;
}

/* Currency-style inputs & selects: tabular numerals */
.variable,
.dropdown {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  width: 8.5rem;
  height: 2.25rem;
  margin: 0 0 0 2px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  font-weight: 500;
  padding-left: 10px;
  background: var(--field-fill);
  color: var(--text);
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease,
    background-color 0.15s ease;
}

.dropdown {
  text-align: center;
  text-indent: 5px;
  cursor: pointer;
  font-weight: 500;
}

.variable {
  text-align: right;
  padding-right: 10px;
}

input:not([readonly]):hover,
.dropdown:hover {
  border-color: #a1a1aa;
}

input:not([readonly]):focus,
.dropdown:focus {
  border-color: var(--accent);
  outline: 0;
  box-shadow: 0 0 0 3px var(--accent-ring);
  background-color: #fff;
}

/* Narrow paired fields (% + computed amount) for cap / PST / GST rows */
.tax {
  position: relative;
  left: 1px;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  width: 4.1rem;
  height: 2.25rem;
  margin: 1px 0;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  text-align: right;
  padding-right: 10px;
  font-size: 0.8125rem;
  font-weight: 500 !important;
  background: var(--field-fill);
  color: var(--text);
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease;
}

.tax:hover {
  border-color: #a1a1aa;
}

.tax:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
  outline: 0;
  background: #fff;
}

/* Calculated / read-only outputs: distinct from editable (#field-fill) inputs. */
input[readonly] {
  background-color: var(--readonly-fill);
  border: 1px solid var(--readonly-border);
  font-size: 0.8125rem;
  font-weight: 500;
  padding-left: 10px;
  color: var(--text-secondary);
  cursor: default;
  box-shadow: inset 0 1px 2px rgba(24, 24, 27, 0.05);
}

.payment_label {
  text-align: center;
}

/* Emphasized payment result panel (third column, bottom fieldset) */
fieldset.payment_label {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  gap: 0.35rem;
  min-height: auto;
  padding: 1.35rem 1.25rem 1.15rem;
  margin: 0;
  background: linear-gradient(180deg, #f0fdf4 0%, #ecfdf5 55%, #ffffff 100%);
  border-bottom: none;
  border-top: 1px solid var(--accent-muted);
}

fieldset.payment_label > legend.payment_label {
  order: 1;
  flex: 0 0 auto;
  color: var(--accent-hover);
  margin-bottom: 0.35rem;
  font-size: 0.6875rem;
  font-weight: 700;
  letter-spacing: 0.11em;
  text-transform: uppercase;
  text-align: center;
  width: 100%;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--accent-muted);
  background: none;
  border-left: none;
  border-radius: 0;
  box-shadow: none;
}

/* DOM order is legend → hint → amount; show as legend → amount → hint */
fieldset.payment_label .payment {
  order: 2;
}

fieldset.payment_label .payment_hint {
  order: 3;
}

/* Large tabular total; text filled by renderCalculation.js */
.payment {
  transform: none;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 2.375rem;
  font-weight: 600;
  line-height: 1;
  letter-spacing: -0.03em;
  color: #047857;
  margin-top: 0.15rem;
  min-height: 2.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  flex: 0 0 auto;
}

/* Missing-field hint under payment amount; .visible from setPaymentHint() in renderCalculation.js */
.payment_hint {
  display: none;
  position: static;
  width: 100%;
  max-width: 100%;
  margin: 0.15rem 0 0;
  padding: 0.45rem 0.65rem;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.6875rem;
  font-weight: 500;
  text-align: center;
  color: var(--warn-text);
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  line-height: 1.35;
  pointer-events: none;
  white-space: normal;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  flex: 0 0 auto;
  align-self: stretch;
}

.payment_hint.visible {
  display: block;
}

/* Clear form: emerald CTA; in header on desktop, full-width on phone */
.reset_lease_btn {
  width: auto;
  min-width: 7.25rem;
  font-family: var(--font);
  font-weight: 600;
  font-size: 0.75rem;
  line-height: 1.25;
  letter-spacing: -0.01em;
  border-radius: 6px;
  margin: 0;
  padding: 0.42rem 0.95rem;
  color: #fff;
  background: linear-gradient(180deg, #10b981 0%, var(--accent) 100%);
  border: 1px solid #059669;
  box-shadow:
    0 1px 2px rgba(5, 150, 105, 0.25),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease,
    box-shadow 0.15s ease,
    transform 0.1s ease;
}

.reset_lease_btn:hover,
.reset_lease_btn:focus {
  color: #fff;
  background: linear-gradient(180deg, #34d399 0%, #059669 100%);
  border-color: #047857;
  box-shadow:
    0 4px 14px rgba(5, 150, 105, 0.35),
    inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.reset_lease_btn:focus {
  outline: 0;
  box-shadow:
    0 0 0 3px var(--accent-ring),
    0 4px 14px rgba(5, 150, 105, 0.25);
}

.reset_lease_btn:active {
  transform: translateY(1px);
}

/* Bootstrap .btn would override gradient/fill if this block is omitted. */
.btn.reset_lease_btn,
.btn.reset_lease_btn:focus {
  color: #fff;
  background: linear-gradient(180deg, #10b981 0%, var(--accent) 100%);
  border-color: #059669;
}

.btn.reset_lease_btn:hover {
  color: #fff;
  background: linear-gradient(180deg, #34d399 0%, #059669 100%);
  border-color: #047857;
}

button.load-points {
  position: absolute;
  padding: 4px 7px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  margin-top: 1.5px;
}

/* Hide native spinners on numeric inputs */
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}

::-webkit-input-placeholder {
  color: #a1a1aa;
}

:-ms-input-placeholder {
  color: #a1a1aa;
}

::placeholder {
  color: #a1a1aa;
}

/* Validation: JS swaps .not-a-number / .reset-not-a-number */
.reset-not-a-number {
  border: 1px solid var(--line-strong);
  color: var(--text);
}

.not-a-number {
  border: 2px solid var(--danger);
  color: var(--danger);
}

.fa-exclamation-circle:hover {
  cursor: pointer;
}

.control input,
.control select {
  outline: none;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type='number'] {
  appearance: textfield;
  -moz-appearance: textfield;
}

/* Page-level scrollbar (body still scrolls when not desktop-locked) */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg);
}

::-webkit-scrollbar-thumb {
  background: #d4d4d8;
  border-radius: 99px;
}

::-webkit-scrollbar-thumb:hover {
  background: #a1a1aa;
}

/* Bootstrap tooltip chrome (required/help icons on labels) */
.tooltip-inner {
  background-color: var(--text) !important;
  color: #fafafa !important;
  font-size: 0.8125rem;
  font-family: var(--font);
  text-align: left;
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.65rem;
  box-shadow: var(--shadow-card);
}

.tooltip .arrow:before {
  border-bottom-color: var(--text);
  border-top-color: var(--text);
}

/* Font Awesome markers in label text — must stay in flow (not absolute) so they
   scroll correctly inside overflow-y:auto form columns. */
.fa {
  position: static;
  display: inline;
  margin-left: 0.25em;
  vertical-align: 0.08em;
  color: var(--accent);
  cursor: help;
  line-height: inherit;
}

.fa-asterisk {
  font-size: 0.65em !important;
  vertical-align: 0.2em;
}

/* Beat Font Awesome defaults so glyphs stay in flow inside scrolling form columns */
.control label .fa {
  position: static !important;
  display: inline !important;
  float: none !important;
}

/* ≤600px: stack labels above fields, larger tap targets, scroll the page */
@media (max-width: 600px) {
  main.lease-calculator {
    padding: 0.75rem 0 1.25rem;
  }

  .calc-content {
    padding: 0 0.75rem;
  }

  .heading-bar {
    margin-bottom: 1.25rem;
  }

  .heading-lead::after {
    margin-top: 0.75rem;
  }

  legend.legend-pillar {
    font-size: 0.72rem;
    padding: 0.35rem 0.55rem 0.35rem 0.6rem;
    margin-bottom: 0.65rem;
  }

  .reset_lease_btn {
    width: 100%;
    max-width: 16rem;
    min-height: 2.35rem;
    padding: 0.45rem 0.85rem;
    font-size: 0.75rem;
    align-self: center;
  }

  fieldset {
    padding: 1rem 1rem;
  }

  .control label {
    width: 100%;
    display: block;
    margin-bottom: 0.35rem;
  }

  .variable,
  .dropdown {
    width: 100%;
    max-width: 100%;
    min-height: 2.5rem;
    height: auto;
    box-sizing: border-box;
  }

  .tax {
    width: calc(50% - 2px);
    max-width: none;
    min-height: 2.5rem;
    height: auto;
    box-sizing: border-box;
  }

  fieldset.payment_label {
    min-height: 0;
    padding: 1rem 0.85rem 1rem;
  }

  .payment {
    font-size: 1.875rem;
  }

  .payment_hint {
    font-size: 0.625rem;
    max-width: calc(100% - 1rem);
    padding: 0.35rem 0.5rem;
  }
}
