/* =============================================================
   1. Tokens
   ============================================================= */
:root {
  --primary: #1e40af;
  --primary-light: #3b82f6;
  --primary-dark: #1e3a8a;
  --secondary: #059669;
  --secondary-light: #10b981;
  --accent: #1e40af;
  --bg: #f0f4f8;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --border-focus: #3b82f6;
  --success: #10b981;
  --warning: #f59e0b;
  --error: #ef4444;
  --cream: #ffffff;
  --sans: 'Inter', system-ui, -apple-system, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-soft: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.12);
  --radius: 12px;
  --radius-sm: 8px;
  --gutter: 1rem;
}

/* =============================================================
   2. Reset & base
   ============================================================= */
*, *::before, *::after { box-sizing: border-box; margin: 0; }
html { -webkit-text-size-adjust: 100%; tab-size: 2; scroll-behavior: smooth; }
body {
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: clip;
  overscroll-behavior-y: none;
}
img, svg, video { display: block; max-width: 100%; }
img { height: auto; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
a { color: var(--primary); text-decoration: none; transition: color .2s var(--ease-out); }
a:hover { color: var(--primary-dark); }
p { text-wrap: pretty; }
h1, h2, h3, h4 { text-wrap: balance; line-height: 1.2; letter-spacing: -0.02em; color: var(--text); }
::selection { background: var(--primary); color: #fff; }
:focus-visible {
  outline: 2px solid var(--primary-light);
  outline-offset: 3px;
  border-radius: 4px;
}
.skip-link {
  position: fixed; top: -100px; left: 1rem;
  padding: .6rem 1rem; background: var(--primary); color: #fff;
  z-index: 9999; border-radius: var(--radius-sm); font-weight: 500;
}
.skip-link:focus { top: 1rem; }

/* =============================================================
   3. Utilities
   ============================================================= */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); border: 0;
}
.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--gutter);
}
.container--narrow { max-width: 900px; }

/* =============================================================
   4. Typography
   ============================================================= */
h1 { font-size: clamp(1.5rem, 4vw, 2.25rem); font-weight: 700; }
h2 { font-size: clamp(1.25rem, 3vw, 1.75rem); font-weight: 600; margin-bottom: .75rem; }
h3 { font-size: 1.125rem; font-weight: 600; margin-bottom: .5rem; }
.subtitle { font-size: 1.0625rem; color: var(--text-muted); line-height: 1.5; }

/* =============================================================
   5. Header
   ============================================================= */
.site-header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 100;
}
.site-header .container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: .75rem;
  padding-bottom: .75rem;
  gap: 1rem;
}
.logo {
  display: flex; align-items: center; gap: .5rem;
  font-weight: 700; font-size: 1.125rem; color: var(--primary);
  white-space: nowrap;
}
.logo svg { width: 28px; height: 28px; flex-shrink: 0; }
.nav-links {
  display: flex; gap: .25rem;
  list-style: none; padding: 0;
  flex-wrap: wrap; justify-content: flex-end;
}
.nav-links a {
  display: inline-block;
  padding: .375rem .625rem;
  border-radius: var(--radius-sm);
  font-size: .8125rem;
  font-weight: 500;
  color: var(--text-muted);
  transition: background .2s, color .2s;
  white-space: nowrap;
}
.nav-links a:hover,
.nav-links a.active {
  background: var(--bg);
  color: var(--primary);
}
.nav-toggle {
  display: none;
  padding: .5rem;
}
.nav-toggle svg { width: 24px; height: 24px; }

/* =============================================================
   6. Page hero / title
   ============================================================= */
.page-hero {
  padding: 2rem 0 1rem;
  text-align: center;
}
.page-hero h1 { margin-bottom: .5rem; }
.page-hero .subtitle { max-width: 600px; margin: 0 auto; }

/* =============================================================
   7. Tool card
   ============================================================= */
.tool-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}
.tool-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
  gap: .75rem;
}
.currency-select {
  display: flex; align-items: center; gap: .5rem;
  font-size: .875rem; color: var(--text-muted);
}
.currency-select select {
  padding: .375rem .625rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit; font-size: .875rem;
  background: #fff;
  color: var(--text);
  cursor: pointer;
}

/* =============================================================
   8. Form inputs
   ============================================================= */
.calc-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
.form-group { display: flex; flex-direction: column; gap: .25rem; }
.form-group label {
  font-size: .8125rem;
  font-weight: 600;
  color: var(--text);
  display: flex; align-items: center; gap: .25rem;
}
.form-group label .hint {
  font-weight: 400;
  color: var(--text-muted);
  font-size: .75rem;
}
.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}
.input-wrapper .prefix,
.input-wrapper .suffix {
  position: absolute;
  font-size: .875rem;
  color: var(--text-muted);
  pointer-events: none;
  z-index: 1;
}
.input-wrapper .prefix { left: .75rem; }
.input-wrapper .suffix { right: .75rem; }
.input-wrapper input.has-prefix { padding-left: 2rem; }
.input-wrapper input.has-suffix { padding-right: 2.5rem; }

input[type="number"],
input[type="text"],
select {
  width: 100%;
  padding: .625rem .75rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: .9375rem;
  color: var(--text);
  background: #fff;
  transition: border-color .2s var(--ease-out), box-shadow .2s var(--ease-out);
}
input:focus, select:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(59,130,246,.15);
  outline: none;
}
input.invalid {
  border-color: var(--error);
  box-shadow: 0 0 0 3px rgba(239,68,68,.1);
}
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

.toggle-group {
  display: flex;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.toggle-group button {
  flex: 1;
  padding: .5rem .75rem;
  font-size: .8125rem;
  font-weight: 500;
  border-right: 1px solid var(--border);
  background: #fff;
  color: var(--text-muted);
  transition: background .2s, color .2s;
}
.toggle-group button:last-child { border-right: none; }
.toggle-group button.active {
  background: var(--primary);
  color: #fff;
}

.range-group {
  display: flex; flex-direction: column; gap: .25rem;
}
.range-row {
  display: flex; align-items: center; gap: .75rem;
}
.range-row input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  border: none;
  padding: 0;
}
.range-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}
.range-value {
  min-width: 4rem;
  text-align: right;
  font-weight: 600;
  font-size: .875rem;
  font-family: var(--mono);
}

/* =============================================================
   9. Results
   ============================================================= */
.results-section {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}
.results-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.result-box {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 1rem;
  text-align: center;
}
.result-box.primary {
  background: var(--primary);
  color: #fff;
}
.result-box.primary .result-label { color: rgba(255,255,255,.8); }
.result-label {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  margin-bottom: .25rem;
}
.result-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-family: var(--mono);
  line-height: 1.2;
}
.result-box.primary .result-value { font-size: 1.75rem; }

/* =============================================================
   10. Charts
   ============================================================= */
.chart-container {
  position: relative;
  width: 100%;
  margin: 1.5rem 0;
  background: #fff;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  padding: 1rem;
}
.chart-title {
  font-size: .875rem;
  font-weight: 600;
  margin-bottom: .75rem;
  color: var(--text);
}
.chart-canvas {
  width: 100%;
  height: 280px;
  display: block;
}
.chart-legend {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-top: .75rem;
  flex-wrap: wrap;
}
.chart-legend-item {
  display: flex;
  align-items: center;
  gap: .375rem;
  font-size: .75rem;
  color: var(--text-muted);
}
.chart-legend-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* =============================================================
   11. Amortization table
   ============================================================= */
.table-wrapper {
  overflow-x: auto;
  margin: 1.5rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.amort-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .8125rem;
}
.amort-table th {
  background: var(--bg);
  padding: .625rem .75rem;
  text-align: right;
  font-weight: 600;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  position: sticky; top: 0;
}
.amort-table th:first-child { text-align: left; }
.amort-table td {
  padding: .5rem .75rem;
  text-align: right;
  border-bottom: 1px solid var(--border);
  font-family: var(--mono);
  font-size: .8125rem;
}
.amort-table td:first-child {
  text-align: left;
  font-family: var(--sans);
  font-weight: 500;
}
.amort-table tbody tr:hover { background: rgba(59,130,246,.04); }
.amort-table tbody tr:last-child td { border-bottom: none; }
.table-scroll-hint {
  max-height: 400px;
  overflow-y: auto;
}

/* =============================================================
   12. Export buttons
   ============================================================= */
.export-bar {
  display: flex;
  gap: .5rem;
  flex-wrap: wrap;
  margin: 1rem 0;
}
.btn {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding: .5rem 1rem;
  border-radius: var(--radius-sm);
  font-size: .8125rem;
  font-weight: 600;
  transition: background .2s var(--ease-out), transform .15s var(--ease-out);
  cursor: pointer;
  border: none;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn--primary {
  background: var(--primary);
  color: #fff;
}
.btn--primary:hover { background: var(--primary-dark); }
.btn--secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn--secondary:hover { background: var(--border); }
.btn--success {
  background: var(--secondary);
  color: #fff;
}
.btn--success:hover { background: #047857; }

/* =============================================================
   13. Ad slots (placeholders)
   ============================================================= */
.ad-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  background: rgba(0,0,0,.015);
  margin: 1.5rem 0;
}
.ad-slot--leaderboard {
  width: 100%;
  max-width: 728px;
  height: 90px;
  margin-left: auto;
  margin-right: auto;
}
.ad-slot--rectangle {
  width: 300px;
  height: 250px;
}
.ad-slot--sidebar {
  width: 160px;
  height: 600px;
  position: sticky;
  top: 80px;
}
.ad-slot--native {
  width: 100%;
  max-width: 728px;
  height: 90px;
  margin-left: auto;
  margin-right: auto;
}

/* =============================================================
   14. Content layout with sidebar
   ============================================================= */
.page-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.5rem;
  padding-bottom: 2rem;
}
.page-main { min-width: 0; }

/* =============================================================
   15. SEO content sections
   ============================================================= */
.content-section {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}
.content-section h2 { color: var(--primary-dark); }
.content-section p {
  margin-bottom: 1rem;
  color: var(--text);
  line-height: 1.7;
}
.content-section p:last-child { margin-bottom: 0; }
.content-section ul, .content-section ol {
  margin: .75rem 0 1rem 1.25rem;
  color: var(--text);
}
.content-section li { margin-bottom: .375rem; }
.formula-box {
  background: var(--bg);
  border-left: 3px solid var(--primary);
  padding: 1rem 1.25rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-family: var(--mono);
  font-size: .875rem;
  margin: 1rem 0;
  overflow-x: auto;
}

/* =============================================================
   16. FAQ
   ============================================================= */
.faq-section { margin-bottom: 1.5rem; }
.faq-item {
  border-bottom: 1px solid var(--border);
}
.faq-item:last-child { border-bottom: none; }
.faq-item summary {
  padding: 1rem 0;
  font-weight: 600;
  font-size: .9375rem;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  color: var(--text);
  transition: color .2s;
}
.faq-item summary:hover { color: var(--primary); }
.faq-item summary::after {
  content: '+';
  font-size: 1.25rem;
  font-weight: 400;
  color: var(--text-muted);
  flex-shrink: 0;
  transition: transform .2s var(--ease-out);
}
.faq-item[open] summary::after {
  content: '-';
}
.faq-item summary::-webkit-details-marker { display: none; }
.faq-answer {
  padding: 0 0 1rem;
  color: var(--text-muted);
  line-height: 1.7;
  font-size: .9375rem;
}

/* =============================================================
   17. Cross-links
   ============================================================= */
.cross-links {
  margin: 2rem 0;
}
.cross-links h2 { text-align: center; margin-bottom: 1.25rem; }
.cross-links-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
.cross-link-card {
  display: flex;
  align-items: center;
  gap: 1rem;
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  transition: box-shadow .2s var(--ease-out), transform .15s var(--ease-out);
  color: var(--text);
}
.cross-link-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
  color: var(--text);
}
.cross-link-icon {
  width: 48px; height: 48px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem;
  flex-shrink: 0;
}
.cross-link-icon.mortgage { background: #dbeafe; }
.cross-link-icon.loan { background: #d1fae5; }
.cross-link-icon.compound { background: #fef3c7; }
.cross-link-icon.margin { background: #ede9fe; }
.cross-link-text h3 { font-size: .9375rem; margin-bottom: .125rem; }
.cross-link-text p {
  font-size: .8125rem;
  color: var(--text-muted);
  margin: 0;
  line-height: 1.4;
}

/* =============================================================
   18. Footer
   ============================================================= */
.site-footer {
  background: #fff;
  border-top: 1px solid var(--border);
  padding: 1.5rem 0;
  margin-top: 2rem;
}
.footer-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .75rem;
  text-align: center;
}
.footer-disclaimer {
  font-size: .75rem;
  color: var(--text-muted);
  max-width: 600px;
  line-height: 1.5;
}
.footer-links {
  display: flex; gap: 1rem;
  list-style: none; padding: 0;
}
.footer-links a {
  font-size: .75rem;
  color: var(--text-muted);
}
.footer-links a:hover { color: var(--primary); }

/* =============================================================
   19. Hub page
   ============================================================= */
.hub-hero {
  padding: 3rem 0 2rem;
  text-align: center;
}
.hub-hero h1 { margin-bottom: .75rem; }
.hub-hero .subtitle { max-width: 650px; margin: 0 auto; font-size: 1.125rem; }
.hub-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.25rem;
  margin: 2rem 0;
}
.hub-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
  border: 1px solid transparent;
  transition: box-shadow .25s var(--ease-out), border-color .25s, transform .2s var(--ease-out);
  color: var(--text);
}
.hub-card:hover {
  box-shadow: var(--shadow-lg);
  border-color: var(--primary-light);
  transform: translateY(-3px);
  color: var(--text);
}
.hub-card-icon {
  width: 56px; height: 56px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.75rem;
}
.hub-card h2 { font-size: 1.125rem; margin-bottom: 0; }
.hub-card p { font-size: .875rem; color: var(--text-muted); margin: 0; line-height: 1.5; }
.hub-card-arrow {
  display: inline-flex; align-items: center; gap: .375rem;
  font-size: .8125rem; font-weight: 600; color: var(--primary);
  margin-top: auto;
}
.privacy-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  padding: .75rem;
  background: #f0fdf4;
  border-radius: var(--radius-sm);
  font-size: .8125rem;
  color: #166534;
  margin: 1rem 0;
}

/* =============================================================
   20. Calculation mode toggle (margin calculator)
   ============================================================= */
.mode-tabs {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 1.25rem;
}
.mode-tab {
  flex: 1;
  padding: .5rem .75rem;
  font-size: .8125rem;
  font-weight: 600;
  border-radius: 6px;
  text-align: center;
  transition: background .2s, color .2s;
  color: var(--text-muted);
}
.mode-tab.active {
  background: #fff;
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

/* =============================================================
   21. Responsive
   ============================================================= */
@media (min-width: 540px) {
  .calc-grid { grid-template-columns: 1fr 1fr; }
  .results-grid { grid-template-columns: repeat(2, 1fr); }
  .hub-grid { grid-template-columns: 1fr 1fr; }
  .cross-links-grid { grid-template-columns: 1fr 1fr; }
}

@media (min-width: 720px) {
  :root { --gutter: 1.5rem; }
  .tool-card { padding: 2rem; }
  .results-grid { grid-template-columns: repeat(3, 1fr); }
  .ad-slot--leaderboard { height: 90px; }
}

@media (min-width: 960px) {
  .nav-toggle { display: none; }
  .page-layout {
    grid-template-columns: 1fr 180px;
  }
  .page-sidebar {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }
  .calc-grid--four { grid-template-columns: repeat(4, 1fr); }
}

@media (min-width: 1280px) {
  :root { --gutter: 2rem; }
  .results-grid--four { grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 959px) {
  .page-sidebar { display: none; }
  .nav-links { display: none; }
  .nav-toggle { display: block; }
  .nav-links.open {
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 100%;
    left: 0; right: 0;
    background: #fff;
    border-bottom: 1px solid var(--border);
    padding: .5rem 1rem;
    box-shadow: var(--shadow-md);
  }
}

/* =============================================================
   22. Print
   ============================================================= */
@media print {
  .site-header, .site-footer, .ad-slot, .export-bar,
  .cross-links, .page-sidebar, .nav-toggle { display: none; }
  body { background: #fff; }
  .tool-card, .content-section { box-shadow: none; border: 1px solid #ddd; }
  .amort-table { font-size: 9pt; }
  .chart-canvas { max-height: 200px; }
}

/* =============================================================
   23. Reduced motion (intrusive only)
   ============================================================= */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

/* =============================================================
   24. JS-disabled fallback
   ============================================================= */
.noscript-msg {
  background: #fef3c7;
  color: #92400e;
  padding: 1rem;
  border-radius: var(--radius-sm);
  text-align: center;
  font-size: .875rem;
}
